Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 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/TargetSubtargetInfo.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | void MachineIRBuilder::setFunction(MachineFunction &MF) { |
| 23 | this->MF = &MF; |
| 24 | this->MBB = nullptr; |
| 25 | this->TII = MF.getSubtarget().getInstrInfo(); |
| 26 | this->DL = DebugLoc(); |
| 27 | this->MI = nullptr; |
| 28 | } |
| 29 | |
| 30 | void MachineIRBuilder::setBasicBlock(MachineBasicBlock &MBB, bool Beginning) { |
| 31 | this->MBB = &MBB; |
| 32 | Before = Beginning; |
| 33 | assert(&getMF() == MBB.getParent() && |
| 34 | "Basic block is in a different function"); |
| 35 | } |
| 36 | |
| 37 | void MachineIRBuilder::setInstr(MachineInstr &MI, bool Before) { |
| 38 | assert(MI.getParent() && "Instruction is not part of a basic block"); |
| 39 | setBasicBlock(*MI.getParent()); |
| 40 | this->MI = &MI; |
| 41 | this->Before = Before; |
| 42 | } |
| 43 | |
| 44 | MachineBasicBlock::iterator MachineIRBuilder::getInsertPt() { |
| 45 | if (MI) { |
| 46 | if (Before) |
| 47 | return MI; |
| 48 | if (!MI->getNextNode()) |
| 49 | return getMBB().end(); |
| 50 | return MI->getNextNode(); |
| 51 | } |
| 52 | return Before ? getMBB().begin() : getMBB().end(); |
| 53 | } |
| 54 | |
| 55 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
| 56 | unsigned Op0, unsigned Op1) { |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame^] | 57 | return buildInstr(Opcode, nullptr, Res, Op0, Op1); |
| 58 | } |
| 59 | |
| 60 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty, |
| 61 | unsigned Res, unsigned Op0, |
| 62 | unsigned Op1) { |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 63 | MachineInstr *NewMI = |
| 64 | BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0).addReg(Op1); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame^] | 65 | if (Ty) |
| 66 | NewMI->setType(Ty); |
| 67 | getMBB().insert(getInsertPt(), NewMI); |
| 68 | return NewMI; |
| 69 | } |
| 70 | |
| 71 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
| 72 | unsigned Op0) { |
| 73 | MachineInstr *NewMI = |
| 74 | BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0); |
| 75 | getMBB().insert(getInsertPt(), NewMI); |
| 76 | return NewMI; |
| 77 | } |
| 78 | |
| 79 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode) { |
| 80 | MachineInstr *NewMI = BuildMI(getMF(), DL, getTII().get(Opcode)); |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 81 | getMBB().insert(getInsertPt(), NewMI); |
| 82 | return NewMI; |
| 83 | } |