1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.cpp - MIBuilder--*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file implements the MachineIRBuidler class.
11 //===----------------------------------------------------------------------===//
12 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
13
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/TargetOpcodes.h"
19 #include "llvm/Target/TargetSubtargetInfo.h"
20
21 using namespace llvm;
22
setMF(MachineFunction & MF)23 void MachineIRBuilder::setMF(MachineFunction &MF) {
24 this->MF = &MF;
25 this->MBB = nullptr;
26 this->TII = MF.getSubtarget().getInstrInfo();
27 this->DL = DebugLoc();
28 this->MI = nullptr;
29 }
30
setMBB(MachineBasicBlock & MBB,bool Beginning)31 void MachineIRBuilder::setMBB(MachineBasicBlock &MBB, bool Beginning) {
32 this->MBB = &MBB;
33 Before = Beginning;
34 assert(&getMF() == MBB.getParent() &&
35 "Basic block is in a different function");
36 }
37
setInstr(MachineInstr & MI,bool Before)38 void MachineIRBuilder::setInstr(MachineInstr &MI, bool Before) {
39 assert(MI.getParent() && "Instruction is not part of a basic block");
40 setMBB(*MI.getParent());
41 this->MI = &MI;
42 this->Before = Before;
43 }
44
getInsertPt()45 MachineBasicBlock::iterator MachineIRBuilder::getInsertPt() {
46 if (MI) {
47 if (Before)
48 return MI;
49 if (!MI->getNextNode())
50 return getMBB().end();
51 return MI->getNextNode();
52 }
53 return Before ? getMBB().begin() : getMBB().end();
54 }
55
56 //------------------------------------------------------------------------------
57 // Build instruction variants.
58 //------------------------------------------------------------------------------
buildInstr(unsigned Opcode,Type * Ty)59 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty) {
60 MachineInstr *NewMI = BuildMI(getMF(), DL, getTII().get(Opcode));
61 if (Ty) {
62 assert(isPreISelGenericOpcode(Opcode) &&
63 "Only generic instruction can have a type");
64 NewMI->setType(Ty);
65 } else
66 assert(!isPreISelGenericOpcode(Opcode) &&
67 "Generic instruction must have a type");
68 getMBB().insert(getInsertPt(), NewMI);
69 return NewMI;
70 }
71
buildInstr(unsigned Opcode,unsigned Res,unsigned Op0,unsigned Op1)72 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res,
73 unsigned Op0, unsigned Op1) {
74 return buildInstr(Opcode, nullptr, Res, Op0, Op1);
75 }
76
buildInstr(unsigned Opcode,Type * Ty,unsigned Res,unsigned Op0,unsigned Op1)77 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty,
78 unsigned Res, unsigned Op0,
79 unsigned Op1) {
80 MachineInstr *NewMI = buildInstr(Opcode, Ty);
81 MachineInstrBuilder(getMF(), NewMI)
82 .addReg(Res, RegState::Define)
83 .addReg(Op0)
84 .addReg(Op1);
85 return NewMI;
86 }
87
buildInstr(unsigned Opcode,unsigned Res,unsigned Op0)88 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res,
89 unsigned Op0) {
90 MachineInstr *NewMI = buildInstr(Opcode, nullptr);
91 MachineInstrBuilder(getMF(), NewMI).addReg(Res, RegState::Define).addReg(Op0);
92 return NewMI;
93 }
94
buildInstr(unsigned Opcode)95 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode) {
96 return buildInstr(Opcode, nullptr);
97 }
98
buildInstr(unsigned Opcode,Type * Ty,MachineBasicBlock & BB)99 MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty,
100 MachineBasicBlock &BB) {
101 MachineInstr *NewMI = buildInstr(Opcode, Ty);
102 MachineInstrBuilder(getMF(), NewMI).addMBB(&BB);
103 return NewMI;
104 }
105