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());
212 /// Copy all the implicit operands from OtherMI onto this one.
213 const MachineInstrBuilder ©ImplicitOps(const MachineInstr *OtherMI) {
214 MI->copyImplicitOps(*MF, OtherMI);
219 /// BuildMI - Builder interface. Specify how to create the initial instruction
222 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
224 const MCInstrDesc &MCID) {
225 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
228 /// BuildMI - This version of the builder sets up the first operand as a
229 /// destination virtual register.
231 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
233 const MCInstrDesc &MCID,
235 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
236 .addReg(DestReg, RegState::Define);
239 /// BuildMI - This version of the builder inserts the newly-built
240 /// instruction before the given position in the given MachineBasicBlock, and
241 /// sets up the first operand as a destination virtual register.
243 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
244 MachineBasicBlock::iterator I,
246 const MCInstrDesc &MCID,
248 MachineFunction &MF = *BB.getParent();
249 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
251 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
254 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
255 MachineBasicBlock::instr_iterator I,
257 const MCInstrDesc &MCID,
259 MachineFunction &MF = *BB.getParent();
260 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
262 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
265 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
268 const MCInstrDesc &MCID,
270 if (I->isInsideBundle()) {
271 MachineBasicBlock::instr_iterator MII = I;
272 return BuildMI(BB, MII, DL, MCID, DestReg);
275 MachineBasicBlock::iterator MII = I;
276 return BuildMI(BB, MII, DL, MCID, DestReg);
279 /// BuildMI - This version of the builder inserts the newly-built
280 /// instruction before the given position in the given MachineBasicBlock, and
281 /// does NOT take a destination register.
283 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
284 MachineBasicBlock::iterator I,
286 const MCInstrDesc &MCID) {
287 MachineFunction &MF = *BB.getParent();
288 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
290 return MachineInstrBuilder(MF, MI);
293 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
294 MachineBasicBlock::instr_iterator I,
296 const MCInstrDesc &MCID) {
297 MachineFunction &MF = *BB.getParent();
298 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
300 return MachineInstrBuilder(MF, MI);
303 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
306 const MCInstrDesc &MCID) {
307 if (I->isInsideBundle()) {
308 MachineBasicBlock::instr_iterator MII = I;
309 return BuildMI(BB, MII, DL, MCID);
312 MachineBasicBlock::iterator MII = I;
313 return BuildMI(BB, MII, DL, MCID);
316 /// BuildMI - This version of the builder inserts the newly-built
317 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
318 /// destination register.
320 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
322 const MCInstrDesc &MCID) {
323 return BuildMI(*BB, BB->end(), DL, MCID);
326 /// BuildMI - This version of the builder inserts the newly-built
327 /// instruction at the end of the given MachineBasicBlock, and sets up the first
328 /// operand as a destination virtual register.
330 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
332 const MCInstrDesc &MCID,
334 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
337 inline unsigned getDefRegState(bool B) {
338 return B ? RegState::Define : 0;
340 inline unsigned getImplRegState(bool B) {
341 return B ? RegState::Implicit : 0;
343 inline unsigned getKillRegState(bool B) {
344 return B ? RegState::Kill : 0;
346 inline unsigned getDeadRegState(bool B) {
347 return B ? RegState::Dead : 0;
349 inline unsigned getUndefRegState(bool B) {
350 return B ? RegState::Undef : 0;
352 inline unsigned getInternalReadRegState(bool B) {
353 return B ? RegState::InternalRead : 0;
355 inline unsigned getDebugRegState(bool B) {
356 return B ? RegState::Debug : 0;
360 /// Helper class for constructing bundles of MachineInstrs.
362 /// MIBundleBuilder can create a bundle from scratch by inserting new
363 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
364 /// existing MachineInstrs in a basic block.
365 class MIBundleBuilder {
366 MachineBasicBlock &MBB;
367 MachineBasicBlock::instr_iterator Begin;
368 MachineBasicBlock::instr_iterator End;
371 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
372 /// BB above the bundle or instruction at Pos.
373 MIBundleBuilder(MachineBasicBlock &BB,
374 MachineBasicBlock::iterator Pos)
375 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
377 /// Create a bundle from the sequence of instructions between B and E.
378 MIBundleBuilder(MachineBasicBlock &BB,
379 MachineBasicBlock::iterator B,
380 MachineBasicBlock::iterator E)
381 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
382 assert(B != E && "No instructions to bundle");
385 MachineInstr *MI = B;
387 MI->bundleWithPred();
391 /// Create an MIBundleBuilder representing an existing instruction or bundle
392 /// that has MI as its head.
393 explicit MIBundleBuilder(MachineInstr *MI)
394 : MBB(*MI->getParent()), Begin(MI) {
395 MachineBasicBlock::iterator I = MI;
397 End = I.getInstrIterator();
400 /// Return a reference to the basic block containing this bundle.
401 MachineBasicBlock &getMBB() const { return MBB; }
403 /// Return true if no instructions have been inserted in this bundle yet.
404 /// Empty bundles aren't representable in a MachineBasicBlock.
405 bool empty() const { return Begin == End; }
407 /// Return an iterator to the first bundled instruction.
408 MachineBasicBlock::instr_iterator begin() const { return Begin; }
410 /// Return an iterator beyond the last bundled instruction.
411 MachineBasicBlock::instr_iterator end() const { return End; }
413 /// Insert MI into this bundle before I which must point to an instruction in
414 /// the bundle, or end().
415 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
420 MI->bundleWithSucc();
425 MI->bundleWithPred();
428 // MI was inserted in the middle of the bundle, so its neighbors' flags are
429 // already fine. Update MI's bundle flags manually.
430 MI->setFlag(MachineInstr::BundledPred);
431 MI->setFlag(MachineInstr::BundledSucc);
435 /// Insert MI into MBB by prepending it to the instructions in the bundle.
436 /// MI will become the first instruction in the bundle.
437 MIBundleBuilder &prepend(MachineInstr *MI) {
438 return insert(begin(), MI);
441 /// Insert MI into MBB by appending it to the instructions in the bundle.
442 /// MI will become the last instruction in the bundle.
443 MIBundleBuilder &append(MachineInstr *MI) {
444 return insert(end(), MI);
448 } // End llvm namespace