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" |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame^] | 18 | #include "llvm/Target/TargetOpcodes.h" |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | void MachineIRBuilder::setFunction(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 | |
| 31 | void MachineIRBuilder::setBasicBlock(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 | |
| 38 | void MachineIRBuilder::setInstr(MachineInstr &MI, bool Before) { |
| 39 | assert(MI.getParent() && "Instruction is not part of a basic block"); |
| 40 | setBasicBlock(*MI.getParent()); |
| 41 | this->MI = &MI; |
| 42 | this->Before = Before; |
| 43 | } |
| 44 | |
| 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 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
| 57 | unsigned Op0, unsigned Op1) { |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 58 | return buildInstr(Opcode, nullptr, Res, Op0, Op1); |
| 59 | } |
| 60 | |
| 61 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty, |
| 62 | unsigned Res, unsigned Op0, |
| 63 | unsigned Op1) { |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 64 | MachineInstr *NewMI = |
| 65 | BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0).addReg(Op1); |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame^] | 66 | if (Ty) { |
| 67 | assert(isPreISelGenericOpcode(Opcode) && |
| 68 | "Only generic instruction can have a type"); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 69 | NewMI->setType(Ty); |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame^] | 70 | } else |
| 71 | assert(!isPreISelGenericOpcode(Opcode) && |
| 72 | "Generic instruction must have a type"); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 73 | getMBB().insert(getInsertPt(), NewMI); |
| 74 | return NewMI; |
| 75 | } |
| 76 | |
| 77 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
| 78 | unsigned Op0) { |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame^] | 79 | assert(!isPreISelGenericOpcode(Opcode) && |
| 80 | "Generic instruction must have a type"); |
| 81 | |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 82 | MachineInstr *NewMI = |
| 83 | BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0); |
| 84 | getMBB().insert(getInsertPt(), NewMI); |
| 85 | return NewMI; |
| 86 | } |
| 87 | |
| 88 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode) { |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame^] | 89 | assert(!isPreISelGenericOpcode(Opcode) && |
| 90 | "Generic instruction must have a type"); |
| 91 | |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 92 | MachineInstr *NewMI = BuildMI(getMF(), DL, getTII().get(Opcode)); |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 93 | getMBB().insert(getInsertPt(), NewMI); |
| 94 | return NewMI; |
| 95 | } |