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) {}
49 explicit MachineInstrBuilder(MachineInstr *mi) : MF(0), MI(mi) {}
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(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(MachineOperand::CreateImm(Val));
88 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
89 MI->addOperand(MachineOperand::CreateCImm(Val));
93 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
94 MI->addOperand(MachineOperand::CreateFPImm(Val));
98 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
99 unsigned char TargetFlags = 0) const {
100 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
104 const MachineInstrBuilder &addFrameIndex(int Idx) const {
105 MI->addOperand(MachineOperand::CreateFI(Idx));
109 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
111 unsigned char TargetFlags = 0) const {
112 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
116 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
117 unsigned char TargetFlags = 0) const {
118 MI->addOperand(MachineOperand::CreateTargetIndex(Idx, Offset, TargetFlags));
122 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
123 unsigned char TargetFlags = 0) const {
124 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
128 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
130 unsigned char TargetFlags = 0) const {
131 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
135 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
136 unsigned char TargetFlags = 0) const {
137 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
141 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
142 MI->addOperand(MachineOperand::CreateRegMask(Mask));
146 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
147 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
151 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
152 MachineInstr::mmo_iterator e) const {
153 MI->setMemRefs(b, e);
158 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
163 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
164 MI->addOperand(MachineOperand::CreateMetadata(MD));
168 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
169 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
173 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
178 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
183 // Add a displacement from an existing MachineOperand with an added offset.
184 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
185 unsigned char TargetFlags = 0) const {
186 switch (Disp.getType()) {
188 llvm_unreachable("Unhandled operand type in addDisp()");
189 case MachineOperand::MO_Immediate:
190 return addImm(Disp.getImm() + off);
191 case MachineOperand::MO_GlobalAddress: {
192 // If caller specifies new TargetFlags then use it, otherwise the
193 // default behavior is to copy the target flags from the existing
194 // MachineOperand. This means if the caller wants to clear the
195 // target flags it needs to do so explicitly.
197 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
199 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
200 Disp.getTargetFlags());
206 /// BuildMI - Builder interface. Specify how to create the initial instruction
209 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
211 const MCInstrDesc &MCID) {
212 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
215 /// BuildMI - This version of the builder sets up the first operand as a
216 /// destination virtual register.
218 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
220 const MCInstrDesc &MCID,
222 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
223 .addReg(DestReg, RegState::Define);
226 /// BuildMI - This version of the builder inserts the newly-built
227 /// instruction before the given position in the given MachineBasicBlock, and
228 /// sets up the first operand as a destination virtual register.
230 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
231 MachineBasicBlock::iterator I,
233 const MCInstrDesc &MCID,
235 MachineFunction &MF = *BB.getParent();
236 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
238 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
241 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
242 MachineBasicBlock::instr_iterator I,
244 const MCInstrDesc &MCID,
246 MachineFunction &MF = *BB.getParent();
247 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
249 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
252 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
255 const MCInstrDesc &MCID,
257 if (I->isInsideBundle()) {
258 MachineBasicBlock::instr_iterator MII = I;
259 return BuildMI(BB, MII, DL, MCID, DestReg);
262 MachineBasicBlock::iterator MII = I;
263 return BuildMI(BB, MII, DL, MCID, DestReg);
266 /// BuildMI - This version of the builder inserts the newly-built
267 /// instruction before the given position in the given MachineBasicBlock, and
268 /// does NOT take a destination register.
270 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
271 MachineBasicBlock::iterator I,
273 const MCInstrDesc &MCID) {
274 MachineFunction &MF = *BB.getParent();
275 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
277 return MachineInstrBuilder(MF, MI);
280 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
281 MachineBasicBlock::instr_iterator I,
283 const MCInstrDesc &MCID) {
284 MachineFunction &MF = *BB.getParent();
285 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
287 return MachineInstrBuilder(MF, MI);
290 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
293 const MCInstrDesc &MCID) {
294 if (I->isInsideBundle()) {
295 MachineBasicBlock::instr_iterator MII = I;
296 return BuildMI(BB, MII, DL, MCID);
299 MachineBasicBlock::iterator MII = I;
300 return BuildMI(BB, MII, DL, MCID);
303 /// BuildMI - This version of the builder inserts the newly-built
304 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
305 /// destination register.
307 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
309 const MCInstrDesc &MCID) {
310 return BuildMI(*BB, BB->end(), DL, MCID);
313 /// BuildMI - This version of the builder inserts the newly-built
314 /// instruction at the end of the given MachineBasicBlock, and sets up the first
315 /// operand as a destination virtual register.
317 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
319 const MCInstrDesc &MCID,
321 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
324 inline unsigned getDefRegState(bool B) {
325 return B ? RegState::Define : 0;
327 inline unsigned getImplRegState(bool B) {
328 return B ? RegState::Implicit : 0;
330 inline unsigned getKillRegState(bool B) {
331 return B ? RegState::Kill : 0;
333 inline unsigned getDeadRegState(bool B) {
334 return B ? RegState::Dead : 0;
336 inline unsigned getUndefRegState(bool B) {
337 return B ? RegState::Undef : 0;
339 inline unsigned getInternalReadRegState(bool B) {
340 return B ? RegState::InternalRead : 0;
344 /// Helper class for constructing bundles of MachineInstrs.
346 /// MIBundleBuilder can create a bundle from scratch by inserting new
347 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
348 /// existing MachineInstrs in a basic block.
349 class MIBundleBuilder {
350 MachineBasicBlock &MBB;
351 MachineBasicBlock::instr_iterator Begin;
352 MachineBasicBlock::instr_iterator End;
355 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
356 /// BB above the bundle or instruction at Pos.
357 MIBundleBuilder(MachineBasicBlock &BB,
358 MachineBasicBlock::iterator Pos)
359 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
361 /// Create a bundle from the sequence of instructions between B and E.
362 MIBundleBuilder(MachineBasicBlock &BB,
363 MachineBasicBlock::iterator B,
364 MachineBasicBlock::iterator E)
365 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
366 assert(B != E && "No instructions to bundle");
369 MachineInstr *MI = B;
371 MI->bundleWithPred();
375 /// Create an MIBundleBuilder representing an existing instruction or bundle
376 /// that has MI as its head.
377 explicit MIBundleBuilder(MachineInstr *MI)
378 : MBB(*MI->getParent()), Begin(MI) {
379 MachineBasicBlock::iterator I = MI;
381 End = I.getInstrIterator();
384 /// Return a reference to the basic block containing this bundle.
385 MachineBasicBlock &getMBB() const { return MBB; }
387 /// Return true if no instructions have been inserted in this bundle yet.
388 /// Empty bundles aren't representable in a MachineBasicBlock.
389 bool empty() const { return Begin == End; }
391 /// Return an iterator to the first bundled instruction.
392 MachineBasicBlock::instr_iterator begin() const { return Begin; }
394 /// Return an iterator beyond the last bundled instruction.
395 MachineBasicBlock::instr_iterator end() const { return End; }
397 /// Insert MI into this bundle before I which must point to an instruction in
398 /// the bundle, or end().
399 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
404 MI->bundleWithSucc();
409 MI->bundleWithPred();
412 // MI was inserted in the middle of the bundle, so its neighbors' flags are
413 // already fine. Update MI's bundle flags manually.
414 MI->setFlag(MachineInstr::BundledPred);
415 MI->setFlag(MachineInstr::BundledSucc);
419 /// Insert MI into MBB by prepending it to the instructions in the bundle.
420 /// MI will become the first instruction in the bundle.
421 MIBundleBuilder &prepend(MachineInstr *MI) {
422 return insert(begin(), MI);
425 /// Insert MI into MBB by appending it to the instructions in the bundle.
426 /// MI will become the last instruction in the bundle.
427 MIBundleBuilder &append(MachineInstr *MI) {
428 return insert(end(), MI);
432 } // End llvm namespace