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 | |
Quentin Colombet | 000b580 | 2016-03-11 17:27:51 +0000 | [diff] [blame] | 23 | void MachineIRBuilder::setMF(MachineFunction &MF) { |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 24 | this->MF = &MF; |
| 25 | this->MBB = nullptr; |
| 26 | this->TII = MF.getSubtarget().getInstrInfo(); |
| 27 | this->DL = DebugLoc(); |
| 28 | this->MI = nullptr; |
| 29 | } |
| 30 | |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 31 | void MachineIRBuilder::setMBB(MachineBasicBlock &MBB, bool Beginning) { |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 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"); |
Quentin Colombet | 91ebd71 | 2016-03-11 17:27:47 +0000 | [diff] [blame] | 40 | setMBB(*MI.getParent()); |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 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 | |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 56 | //------------------------------------------------------------------------------ |
| 57 | // Build instruction variants. |
| 58 | //------------------------------------------------------------------------------ |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 59 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, LLT Ty) { |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 60 | MachineInstr *NewMI = BuildMI(getMF(), DL, getTII().get(Opcode)); |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 61 | if (Ty.isValid()) { |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame] | 62 | assert(isPreISelGenericOpcode(Opcode) && |
| 63 | "Only generic instruction can have a type"); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 64 | NewMI->setType(Ty); |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame] | 65 | } else |
| 66 | assert(!isPreISelGenericOpcode(Opcode) && |
| 67 | "Generic instruction must have a type"); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 68 | getMBB().insert(getInsertPt(), NewMI); |
| 69 | return NewMI; |
| 70 | } |
| 71 | |
| 72 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 73 | unsigned Op0, unsigned Op1) { |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 74 | return buildInstr(Opcode, LLT{}, Res, Op0, Op1); |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 75 | } |
Quentin Colombet | 8fd6718 | 2016-02-11 21:16:56 +0000 | [diff] [blame] | 76 | |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 77 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, LLT Ty, |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 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 | |
| 88 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res, |
| 89 | unsigned Op0) { |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 90 | MachineInstr *NewMI = buildInstr(Opcode, LLT{}); |
Quentin Colombet | f9b4934 | 2016-03-11 17:27:58 +0000 | [diff] [blame] | 91 | MachineInstrBuilder(getMF(), NewMI).addReg(Res, RegState::Define).addReg(Op0); |
Quentin Colombet | 74d7d2f | 2016-02-11 18:53:28 +0000 | [diff] [blame] | 92 | return NewMI; |
| 93 | } |
| 94 | |
| 95 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode) { |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 96 | return buildInstr(Opcode, LLT{}); |
Quentin Colombet | 2ad1f85 | 2016-02-11 17:44:59 +0000 | [diff] [blame] | 97 | } |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 98 | |
Tim Northover | 62ae568 | 2016-07-20 19:09:30 +0000 | [diff] [blame] | 99 | MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, LLT Ty, |
Quentin Colombet | dd4b137 | 2016-03-11 17:28:03 +0000 | [diff] [blame] | 100 | MachineBasicBlock &BB) { |
| 101 | MachineInstr *NewMI = buildInstr(Opcode, Ty); |
| 102 | MachineInstrBuilder(getMF(), NewMI).addMBB(&BB); |
| 103 | return NewMI; |
| 104 | } |
Tim Northover | bd50546 | 2016-07-22 16:59:52 +0000 | [diff] [blame^] | 105 | |
| 106 | MachineInstr *MachineIRBuilder::buildFrameIndex(LLT Ty, unsigned Res, int Idx) { |
| 107 | MachineInstr *NewMI = buildInstr(TargetOpcode::G_FRAME_INDEX, Ty); |
| 108 | auto MIB = MachineInstrBuilder(getMF(), NewMI); |
| 109 | MIB.addReg(Res, RegState::Define); |
| 110 | MIB.addImm(Idx); |
| 111 | return NewMI; |
| 112 | } |