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(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(MachineOperand::CreateImm(Val));
87 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
88 MI->addOperand(MachineOperand::CreateCImm(Val));
92 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
93 MI->addOperand(MachineOperand::CreateFPImm(Val));
97 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
98 unsigned char TargetFlags = 0) const {
99 MI->addOperand(MachineOperand::CreateMBB(MBB, TargetFlags));
103 const MachineInstrBuilder &addFrameIndex(int Idx) const {
104 MI->addOperand(MachineOperand::CreateFI(Idx));
108 const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
110 unsigned char TargetFlags = 0) const {
111 MI->addOperand(MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
115 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
116 unsigned char TargetFlags = 0) const {
117 MI->addOperand(MachineOperand::CreateTargetIndex(Idx, Offset, TargetFlags));
121 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
122 unsigned char TargetFlags = 0) const {
123 MI->addOperand(MachineOperand::CreateJTI(Idx, TargetFlags));
127 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
129 unsigned char TargetFlags = 0) const {
130 MI->addOperand(MachineOperand::CreateGA(GV, Offset, TargetFlags));
134 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
135 unsigned char TargetFlags = 0) const {
136 MI->addOperand(MachineOperand::CreateES(FnName, TargetFlags));
140 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
141 MI->addOperand(MachineOperand::CreateRegMask(Mask));
145 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
146 MI->addMemOperand(*MI->getParent()->getParent(), MMO);
150 const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
151 MachineInstr::mmo_iterator e) const {
152 MI->setMemRefs(b, e);
157 const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
162 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
163 MI->addOperand(MachineOperand::CreateMetadata(MD));
167 const MachineInstrBuilder &addSym(MCSymbol *Sym) const {
168 MI->addOperand(MachineOperand::CreateMCSymbol(Sym));
172 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
177 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
182 // Add a displacement from an existing MachineOperand with an added offset.
183 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
184 unsigned char TargetFlags = 0) const {
185 switch (Disp.getType()) {
187 llvm_unreachable("Unhandled operand type in addDisp()");
188 case MachineOperand::MO_Immediate:
189 return addImm(Disp.getImm() + off);
190 case MachineOperand::MO_GlobalAddress: {
191 // If caller specifies new TargetFlags then use it, otherwise the
192 // default behavior is to copy the target flags from the existing
193 // MachineOperand. This means if the caller wants to clear the
194 // target flags it needs to do so explicitly.
196 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
198 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
199 Disp.getTargetFlags());
205 /// BuildMI - Builder interface. Specify how to create the initial instruction
208 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
210 const MCInstrDesc &MCID) {
211 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
214 /// BuildMI - This version of the builder sets up the first operand as a
215 /// destination virtual register.
217 inline MachineInstrBuilder BuildMI(MachineFunction &MF,
219 const MCInstrDesc &MCID,
221 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
222 .addReg(DestReg, RegState::Define);
225 /// BuildMI - This version of the builder inserts the newly-built
226 /// instruction before the given position in the given MachineBasicBlock, and
227 /// sets up the first operand as a destination virtual register.
229 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
230 MachineBasicBlock::iterator I,
232 const MCInstrDesc &MCID,
234 MachineFunction &MF = *BB.getParent();
235 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
237 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
240 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
241 MachineBasicBlock::instr_iterator I,
243 const MCInstrDesc &MCID,
245 MachineFunction &MF = *BB.getParent();
246 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
248 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
251 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
254 const MCInstrDesc &MCID,
256 if (I->isInsideBundle()) {
257 MachineBasicBlock::instr_iterator MII = I;
258 return BuildMI(BB, MII, DL, MCID, DestReg);
261 MachineBasicBlock::iterator MII = I;
262 return BuildMI(BB, MII, DL, MCID, DestReg);
265 /// BuildMI - This version of the builder inserts the newly-built
266 /// instruction before the given position in the given MachineBasicBlock, and
267 /// does NOT take a destination register.
269 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
270 MachineBasicBlock::iterator I,
272 const MCInstrDesc &MCID) {
273 MachineFunction &MF = *BB.getParent();
274 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
276 return MachineInstrBuilder(MF, MI);
279 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
280 MachineBasicBlock::instr_iterator I,
282 const MCInstrDesc &MCID) {
283 MachineFunction &MF = *BB.getParent();
284 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
286 return MachineInstrBuilder(MF, MI);
289 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
292 const MCInstrDesc &MCID) {
293 if (I->isInsideBundle()) {
294 MachineBasicBlock::instr_iterator MII = I;
295 return BuildMI(BB, MII, DL, MCID);
298 MachineBasicBlock::iterator MII = I;
299 return BuildMI(BB, MII, DL, MCID);
302 /// BuildMI - This version of the builder inserts the newly-built
303 /// instruction at the end of the given MachineBasicBlock, and does NOT take a
304 /// destination register.
306 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
308 const MCInstrDesc &MCID) {
309 return BuildMI(*BB, BB->end(), DL, MCID);
312 /// BuildMI - This version of the builder inserts the newly-built
313 /// instruction at the end of the given MachineBasicBlock, and sets up the first
314 /// operand as a destination virtual register.
316 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB,
318 const MCInstrDesc &MCID,
320 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
323 inline unsigned getDefRegState(bool B) {
324 return B ? RegState::Define : 0;
326 inline unsigned getImplRegState(bool B) {
327 return B ? RegState::Implicit : 0;
329 inline unsigned getKillRegState(bool B) {
330 return B ? RegState::Kill : 0;
332 inline unsigned getDeadRegState(bool B) {
333 return B ? RegState::Dead : 0;
335 inline unsigned getUndefRegState(bool B) {
336 return B ? RegState::Undef : 0;
338 inline unsigned getInternalReadRegState(bool B) {
339 return B ? RegState::InternalRead : 0;
343 /// Helper class for constructing bundles of MachineInstrs.
345 /// MIBundleBuilder can create a bundle from scratch by inserting new
346 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
347 /// existing MachineInstrs in a basic block.
348 class MIBundleBuilder {
349 MachineBasicBlock &MBB;
350 MachineBasicBlock::instr_iterator Begin;
351 MachineBasicBlock::instr_iterator End;
354 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
355 /// BB above the bundle or instruction at Pos.
356 MIBundleBuilder(MachineBasicBlock &BB,
357 MachineBasicBlock::iterator Pos)
358 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
360 /// Create a bundle from the sequence of instructions between B and E.
361 MIBundleBuilder(MachineBasicBlock &BB,
362 MachineBasicBlock::iterator B,
363 MachineBasicBlock::iterator E)
364 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
365 assert(B != E && "No instructions to bundle");
368 MachineInstr *MI = B;
370 MI->bundleWithPred();
374 /// Create an MIBundleBuilder representing an existing instruction or bundle
375 /// that has MI as its head.
376 explicit MIBundleBuilder(MachineInstr *MI)
377 : MBB(*MI->getParent()), Begin(MI) {
378 MachineBasicBlock::iterator I = MI;
380 End = I.getInstrIterator();
383 /// Return a reference to the basic block containing this bundle.
384 MachineBasicBlock &getMBB() const { return MBB; }
386 /// Return true if no instructions have been inserted in this bundle yet.
387 /// Empty bundles aren't representable in a MachineBasicBlock.
388 bool empty() const { return Begin == End; }
390 /// Return an iterator to the first bundled instruction.
391 MachineBasicBlock::instr_iterator begin() const { return Begin; }
393 /// Return an iterator beyond the last bundled instruction.
394 MachineBasicBlock::instr_iterator end() const { return End; }
396 /// Insert MI into this bundle before I which must point to an instruction in
397 /// the bundle, or end().
398 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
403 MI->bundleWithSucc();
408 MI->bundleWithPred();
411 // MI was inserted in the middle of the bundle, so its neighbors' flags are
412 // already fine. Update MI's bundle flags manually.
413 MI->setFlag(MachineInstr::BundledPred);
414 MI->setFlag(MachineInstr::BundledSucc);
418 /// Insert MI into MBB by prepending it to the instructions in the bundle.
419 /// MI will become the first instruction in the bundle.
420 MIBundleBuilder &prepend(MachineInstr *MI) {
421 return insert(begin(), MI);
424 /// Insert MI into MBB by appending it to the instructions in the bundle.
425 /// MI will become the last instruction in the bundle.
426 MIBundleBuilder &append(MachineInstr *MI) {
427 return insert(end(), MI);
431 } // End llvm namespace