1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes a function named BuildMI, which is useful for dramatically
11 // simplifying how MachineInstr's are created. It allows use of code like this:
13 // M = BuildMI(X86::ADDrr8, 2).addReg(argVal1).addReg(argVal2);
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
18 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/Support/ErrorHandling.h"
38 DefineNoRead = Define | Undef,
39 ImplicitDefine = Implicit | Define,
40 ImplicitKill = Implicit | Kill
44 class MachineInstrBuilder {
48 MachineInstrBuilder() : MF(0), MI(0) {}
50 /// Create a MachineInstrBuilder for manipulating an existing instruction.
51 /// F must be the machine function that was used to allocate I.
52 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
54 /// Allow automatic conversion to the machine instruction we are working on.
56 operator MachineInstr*() const { return MI; }
57 MachineInstr *operator->() const { return MI; }
58 operator MachineBasicBlock::iterator() const { return MI; }
60 /// addReg - Add a new virtual register operand...
63 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
64 unsigned SubReg = 0) const {
65 assert((flags & 0x1) == 0 &&
66 "Passing in 'true' to addReg is forbidden! Use enums instead.");
67 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
68 flags & RegState::Define,
69 flags & RegState::Implicit,
70 flags & RegState::Kill,
71 flags & RegState::Dead,
72 flags & RegState::Undef,
73 flags & RegState::EarlyClobber,
75 flags & RegState::Debug,
76 flags & RegState::InternalRead));
80 /// addImm - Add a new immediate operand.
82 const MachineInstrBuilder &addImm(int64_t Val) const {
83 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
87 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
88 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
92 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
93 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
97 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
98 unsigned char TargetFlags = 0) const {
99 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
103 const MachineInstrBuilder &addFrameIndex(int Idx) const {
104 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
108 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
110 unsigned char TargetFlags = 0) const {
111 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
115 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
116 unsigned char TargetFlags = 0) const {
117 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
122 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
123 unsigned char TargetFlags = 0) const {
124 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
128 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
130 unsigned char TargetFlags = 0) const {
131 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
135 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
136 unsigned char TargetFlags = 0) const {
137 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
141 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
143 unsigned char TargetFlags = 0) const {
144 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
148 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
149 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
153 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
154 MI->addMemOperand(*MF, MMO);
158 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
159 MachineInstr::mmo_iterator e) const {
160 MI->setMemRefs(b, e);
165 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
166 MI->addOperand(*MF, MO);
170 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
171 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
175 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
176 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
180 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
185 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
190 // Add a displacement from an existing MachineOperand with an added offset.
191 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
192 unsigned char TargetFlags = 0) const {
193 switch (Disp.getType()) {
195 llvm_unreachable("Unhandled operand type in addDisp()");
196 case MachineOperand::MO_Immediate:
197 return addImm(Disp.getImm() + off);
198 case MachineOperand::MO_GlobalAddress: {
199 // If caller specifies new TargetFlags then use it, otherwise the
200 // default behavior is to copy the target flags from the existing
201 // MachineOperand. This means if the caller wants to clear the
202 // target flags it needs to do so explicitly.
204 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
206 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
207 Disp.getTargetFlags());
213 /// BuildMI - Builder interface. Specify how to create the initial instruction
216 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
218 const MCInstrDesc &MCID) {
219 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
222 /// BuildMI - This version of the builder sets up the first operand as a
223 /// destination virtual register.
225 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
227 const MCInstrDesc &MCID,
229 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
230 .addReg(DestReg, RegState::Define);
233 /// BuildMI - This version of the builder inserts the newly-built
234 /// instruction before the given position in the given MachineBasicBlock, and
235 /// sets up the first operand as a destination virtual register.
237 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
238 MachineBasicBlock::iterator I,
240 const MCInstrDesc &MCID,
242 MachineFunction &MF = *BB.getParent();
243 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
245 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
248 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
249 MachineBasicBlock::instr_iterator I,
251 const MCInstrDesc &MCID,
253 MachineFunction &MF = *BB.getParent();
254 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
256 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
259 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
262 const MCInstrDesc &MCID,
264 if (I->isInsideBundle()) {
265 MachineBasicBlock::instr_iterator MII = I;
266 return BuildMI(BB, MII, DL, MCID, DestReg);
269 MachineBasicBlock::iterator MII = I;
270 return BuildMI(BB, MII, DL, MCID, DestReg);
273 /// BuildMI - This version of the builder inserts the newly-built
274 /// instruction before the given position in the given MachineBasicBlock, and
275 /// does NOT take a destination register.
277 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
278 MachineBasicBlock::iterator I,
280 const MCInstrDesc &MCID) {
281 MachineFunction &MF = *BB.getParent();
282 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
284 return MachineInstrBuilder(MF, MI);
287 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
288 MachineBasicBlock::instr_iterator I,
290 const MCInstrDesc &MCID) {
291 MachineFunction &MF = *BB.getParent();
292 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
294 return MachineInstrBuilder(MF, MI);
297 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
300 const MCInstrDesc &MCID) {
301 if (I->isInsideBundle()) {
302 MachineBasicBlock::instr_iterator MII = I;
303 return BuildMI(BB, MII, DL, MCID);
306 MachineBasicBlock::iterator MII = I;
307 return BuildMI(BB, MII, DL, MCID);
310 /// BuildMI - This version of the builder inserts the newly-built
311 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
312 /// destination register.
314 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
316 const MCInstrDesc &MCID) {
317 return BuildMI(*BB, BB->end(), DL, MCID);
320 /// BuildMI - This version of the builder inserts the newly-built
321 /// instruction at the end of the given MachineBasicBlock, and sets up the first
322 /// operand as a destination virtual register.
324 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
326 const MCInstrDesc &MCID,
328 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
331 inline unsigned getDefRegState(bool B) {
332 return B ? RegState::Define : 0;
334 inline unsigned getImplRegState(bool B) {
335 return B ? RegState::Implicit : 0;
337 inline unsigned getKillRegState(bool B) {
338 return B ? RegState::Kill : 0;
340 inline unsigned getDeadRegState(bool B) {
341 return B ? RegState::Dead : 0;
343 inline unsigned getUndefRegState(bool B) {
344 return B ? RegState::Undef : 0;
346 inline unsigned getInternalReadRegState(bool B) {
347 return B ? RegState::InternalRead : 0;
349 inline unsigned getDebugRegState(bool B) {
350 return B ? RegState::Debug : 0;
354 /// Helper class for constructing bundles of MachineInstrs.
356 /// MIBundleBuilder can create a bundle from scratch by inserting new
357 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
358 /// existing MachineInstrs in a basic block.
359 class MIBundleBuilder {
360 MachineBasicBlock &MBB;
361 MachineBasicBlock::instr_iterator Begin;
362 MachineBasicBlock::instr_iterator End;
365 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
366 /// BB above the bundle or instruction at Pos.
367 MIBundleBuilder(MachineBasicBlock &BB,
368 MachineBasicBlock::iterator Pos)
369 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
371 /// Create a bundle from the sequence of instructions between B and E.
372 MIBundleBuilder(MachineBasicBlock &BB,
373 MachineBasicBlock::iterator B,
374 MachineBasicBlock::iterator E)
375 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
376 assert(B != E && "No instructions to bundle");
379 MachineInstr *MI = B;
381 MI->bundleWithPred();
385 /// Create an MIBundleBuilder representing an existing instruction or bundle
386 /// that has MI as its head.
387 explicit MIBundleBuilder(MachineInstr *MI)
388 : MBB(*MI->getParent()), Begin(MI) {
389 MachineBasicBlock::iterator I = MI;
391 End = I.getInstrIterator();
394 /// Return a reference to the basic block containing this bundle.
395 MachineBasicBlock &getMBB() const { return MBB; }
397 /// Return true if no instructions have been inserted in this bundle yet.
398 /// Empty bundles aren't representable in a MachineBasicBlock.
399 bool empty() const { return Begin == End; }
401 /// Return an iterator to the first bundled instruction.
402 MachineBasicBlock::instr_iterator begin() const { return Begin; }
404 /// Return an iterator beyond the last bundled instruction.
405 MachineBasicBlock::instr_iterator end() const { return End; }
407 /// Insert MI into this bundle before I which must point to an instruction in
408 /// the bundle, or end().
409 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
414 MI->bundleWithSucc();
419 MI->bundleWithPred();
422 // MI was inserted in the middle of the bundle, so its neighbors' flags are
423 // already fine. Update MI's bundle flags manually.
424 MI->setFlag(MachineInstr::BundledPred);
425 MI->setFlag(MachineInstr::BundledSucc);
429 /// Insert MI into MBB by prepending it to the instructions in the bundle.
430 /// MI will become the first instruction in the bundle.
431 MIBundleBuilder &prepend(MachineInstr *MI) {
432 return insert(begin(), MI);
435 /// Insert MI into MBB by appending it to the instructions in the bundle.
436 /// MI will become the last instruction in the bundle.
437 MIBundleBuilder &append(MachineInstr *MI) {
438 return insert(end(), MI);
442 } // End llvm namespace