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