blob: 293e8e7447007bd8e59a467bff632a91fc281816 [file] [log] [blame]
Quentin Colombet2ad1f852016-02-11 17:44:59 +00001//===-- 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
20using namespace llvm;
21
22void 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
30void 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
37void 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
44MachineBasicBlock::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
55MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, unsigned Res,
56 unsigned Op0, unsigned Op1) {
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000057 return buildInstr(Opcode, nullptr, Res, Op0, Op1);
58}
59
60MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode, Type *Ty,
61 unsigned Res, unsigned Op0,
62 unsigned Op1) {
Quentin Colombet2ad1f852016-02-11 17:44:59 +000063 MachineInstr *NewMI =
64 BuildMI(getMF(), DL, getTII().get(Opcode), Res).addReg(Op0).addReg(Op1);
Quentin Colombet74d7d2f2016-02-11 18:53:28 +000065 if (Ty)
66 NewMI->setType(Ty);
67 getMBB().insert(getInsertPt(), NewMI);
68 return NewMI;
69}
70
71MachineInstr *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
79MachineInstr *MachineIRBuilder::buildInstr(unsigned Opcode) {
80 MachineInstr *NewMI = BuildMI(getMF(), DL, getTII().get(Opcode));
Quentin Colombet2ad1f852016-02-11 17:44:59 +000081 getMBB().insert(getInsertPt(), NewMI);
82 return NewMI;
83}