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/CodeGen/MachineInstrBundle.h"
22 #include "llvm/Support/ErrorHandling.h"
39 DefineNoRead = Define | Undef,
40 ImplicitDefine = Implicit | Define,
41 ImplicitKill = Implicit | Kill
45 class MachineInstrBuilder {
49 MachineInstrBuilder() : MF(0), MI(0) {}
51 /// Create a MachineInstrBuilder for manipulating an existing instruction.
52 /// F must be the machine function that was used to allocate I.
53 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
55 /// Allow automatic conversion to the machine instruction we are working on.
57 operator MachineInstr*() const { return MI; }
58 MachineInstr *operator->() const { return MI; }
59 operator MachineBasicBlock::iterator() const { return MI; }
61 /// addReg - Add a new virtual register operand...
64 MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
65 unsigned SubReg = 0) const {
66 assert((flags & 0x1) == 0 &&
67 "Passing in 'true' to addReg is forbidden! Use enums instead.");
68 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
69 flags & RegState::Define,
70 flags & RegState::Implicit,
71 flags & RegState::Kill,
72 flags & RegState::Dead,
73 flags & RegState::Undef,
74 flags & RegState::EarlyClobber,
76 flags & RegState::Debug,
77 flags & RegState::InternalRead));
81 /// addImm - Add a new immediate operand.
83 const MachineInstrBuilder &addImm(int64_t Val) const {
84 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
88 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
89 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
93 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
94 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
98 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
99 unsigned char TargetFlags = 0) const {
100 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
104 const MachineInstrBuilder &addFrameIndex(int Idx) const {
105 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
109 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
111 unsigned char TargetFlags = 0) const {
112 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
116 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
117 unsigned char TargetFlags = 0) const {
118 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
123 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
124 unsigned char TargetFlags = 0) const {
125 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
129 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
131 unsigned char TargetFlags = 0) const {
132 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
136 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
137 unsigned char TargetFlags = 0) const {
138 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
142 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
144 unsigned char TargetFlags = 0) const {
145 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
149 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
150 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
154 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
155 MI->addMemOperand(*MF, MMO);
159 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
160 MachineInstr::mmo_iterator e) const {
161 MI->setMemRefs(b, e);
166 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
167 MI->addOperand(*MF, MO);
171 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
172 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
176 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
177 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym));
181 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
186 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
191 // Add a displacement from an existing MachineOperand with an added offset.
192 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
193 unsigned char TargetFlags = 0) const {
194 switch (Disp.getType()) {
196 llvm_unreachable("Unhandled operand type in addDisp()");
197 case MachineOperand::MO_Immediate:
198 return addImm(Disp.getImm() + off);
199 case MachineOperand::MO_GlobalAddress: {
200 // If caller specifies new TargetFlags then use it, otherwise the
201 // default behavior is to copy the target flags from the existing
202 // MachineOperand. This means if the caller wants to clear the
203 // target flags it needs to do so explicitly.
205 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
207 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
208 Disp.getTargetFlags());
213 /// Copy all the implicit operands from OtherMI onto this one.
214 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) {
215 MI->copyImplicitOps(*MF, OtherMI);
220 /// BuildMI - Builder interface. Specify how to create the initial instruction
223 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
225 const MCInstrDesc &MCID) {
226 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
229 /// BuildMI - This version of the builder sets up the first operand as a
230 /// destination virtual register.
232 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
234 const MCInstrDesc &MCID,
236 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
237 .addReg(DestReg, RegState::Define);
240 /// BuildMI - This version of the builder inserts the newly-built
241 /// instruction before the given position in the given MachineBasicBlock, and
242 /// sets up the first operand as a destination virtual register.
244 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
245 MachineBasicBlock::iterator I,
247 const MCInstrDesc &MCID,
249 MachineFunction &MF = *BB.getParent();
250 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
252 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
255 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
256 MachineBasicBlock::instr_iterator I,
258 const MCInstrDesc &MCID,
260 MachineFunction &MF = *BB.getParent();
261 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
263 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
266 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
269 const MCInstrDesc &MCID,
271 if (I->isInsideBundle()) {
272 MachineBasicBlock::instr_iterator MII = I;
273 return BuildMI(BB, MII, DL, MCID, DestReg);
276 MachineBasicBlock::iterator MII = I;
277 return BuildMI(BB, MII, DL, MCID, DestReg);
280 /// BuildMI - This version of the builder inserts the newly-built
281 /// instruction before the given position in the given MachineBasicBlock, and
282 /// does NOT take a destination register.
284 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
285 MachineBasicBlock::iterator I,
287 const MCInstrDesc &MCID) {
288 MachineFunction &MF = *BB.getParent();
289 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
291 return MachineInstrBuilder(MF, MI);
294 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
295 MachineBasicBlock::instr_iterator I,
297 const MCInstrDesc &MCID) {
298 MachineFunction &MF = *BB.getParent();
299 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
301 return MachineInstrBuilder(MF, MI);
304 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
307 const MCInstrDesc &MCID) {
308 if (I->isInsideBundle()) {
309 MachineBasicBlock::instr_iterator MII = I;
310 return BuildMI(BB, MII, DL, MCID);
313 MachineBasicBlock::iterator MII = I;
314 return BuildMI(BB, MII, DL, MCID);
317 /// BuildMI - This version of the builder inserts the newly-built
318 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
319 /// destination register.
321 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
323 const MCInstrDesc &MCID) {
324 return BuildMI(*BB, BB->end(), DL, MCID);
327 /// BuildMI - This version of the builder inserts the newly-built
328 /// instruction at the end of the given MachineBasicBlock, and sets up the first
329 /// operand as a destination virtual register.
331 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
333 const MCInstrDesc &MCID,
335 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
338 inline unsigned getDefRegState(bool B) {
339 return B ? RegState::Define : 0;
341 inline unsigned getImplRegState(bool B) {
342 return B ? RegState::Implicit : 0;
344 inline unsigned getKillRegState(bool B) {
345 return B ? RegState::Kill : 0;
347 inline unsigned getDeadRegState(bool B) {
348 return B ? RegState::Dead : 0;
350 inline unsigned getUndefRegState(bool B) {
351 return B ? RegState::Undef : 0;
353 inline unsigned getInternalReadRegState(bool B) {
354 return B ? RegState::InternalRead : 0;
356 inline unsigned getDebugRegState(bool B) {
357 return B ? RegState::Debug : 0;
361 /// Helper class for constructing bundles of MachineInstrs.
363 /// MIBundleBuilder can create a bundle from scratch by inserting new
364 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
365 /// existing MachineInstrs in a basic block.
366 class MIBundleBuilder {
367 MachineBasicBlock &MBB;
368 MachineBasicBlock::instr_iterator Begin;
369 MachineBasicBlock::instr_iterator End;
372 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
373 /// BB above the bundle or instruction at Pos.
374 MIBundleBuilder(MachineBasicBlock &BB,
375 MachineBasicBlock::iterator Pos)
376 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
378 /// Create a bundle from the sequence of instructions between B and E.
379 MIBundleBuilder(MachineBasicBlock &BB,
380 MachineBasicBlock::iterator B,
381 MachineBasicBlock::iterator E)
382 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
383 assert(B != E && "No instructions to bundle");
386 MachineInstr *MI = B;
388 MI->bundleWithPred();
392 /// Create an MIBundleBuilder representing an existing instruction or bundle
393 /// that has MI as its head.
394 explicit MIBundleBuilder(MachineInstr *MI)
395 : MBB(*MI->getParent()), Begin(MI), End(getBundleEnd(MI)) {}
397 /// Return a reference to the basic block containing this bundle.
398 MachineBasicBlock &getMBB() const { return MBB; }
400 /// Return true if no instructions have been inserted in this bundle yet.
401 /// Empty bundles aren't representable in a MachineBasicBlock.
402 bool empty() const { return Begin == End; }
404 /// Return an iterator to the first bundled instruction.
405 MachineBasicBlock::instr_iterator begin() const { return Begin; }
407 /// Return an iterator beyond the last bundled instruction.
408 MachineBasicBlock::instr_iterator end() const { return End; }
410 /// Insert MI into this bundle before I which must point to an instruction in
411 /// the bundle, or end().
412 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
417 MI->bundleWithSucc();
422 MI->bundleWithPred();
425 // MI was inserted in the middle of the bundle, so its neighbors' flags are
426 // already fine. Update MI's bundle flags manually.
427 MI->setFlag(MachineInstr::BundledPred);
428 MI->setFlag(MachineInstr::BundledSucc);
432 /// Insert MI into MBB by prepending it to the instructions in the bundle.
433 /// MI will become the first instruction in the bundle.
434 MIBundleBuilder &prepend(MachineInstr *MI) {
435 return insert(begin(), MI);
438 /// Insert MI into MBB by appending it to the instructions in the bundle.
439 /// MI will become the last instruction in the bundle.
440 MIBundleBuilder &append(MachineInstr *MI) {
441 return insert(end(), MI);
445 } // End llvm namespace