]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/CodeGen/MachineInstrBuilder.h
Merge ACPICA 20170303.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / CodeGen / MachineInstrBuilder.h
1 //===-- CodeGen/MachineInstBuilder.h - Simplify creation of MIs -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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:
12 //
13 //   M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
14 //           .addReg(argVal1)
15 //           .addReg(argVal2);
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20 #define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
21
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBundle.h"
24 #include "llvm/Support/ErrorHandling.h"
25
26 namespace llvm {
27
28 class MCInstrDesc;
29 class MDNode;
30
31 namespace RegState {
32   enum {
33     Define         = 0x2,
34     Implicit       = 0x4,
35     Kill           = 0x8,
36     Dead           = 0x10,
37     Undef          = 0x20,
38     EarlyClobber   = 0x40,
39     Debug          = 0x80,
40     InternalRead   = 0x100,
41     DefineNoRead   = Define | Undef,
42     ImplicitDefine = Implicit | Define,
43     ImplicitKill   = Implicit | Kill
44   };
45 }
46
47 class MachineInstrBuilder {
48   MachineFunction *MF;
49   MachineInstr *MI;
50 public:
51   MachineInstrBuilder() : MF(nullptr), MI(nullptr) {}
52
53   /// Create a MachineInstrBuilder for manipulating an existing instruction.
54   /// F must be the machine function that was used to allocate I.
55   MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
56   MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
57       : MF(&F), MI(&*I) {}
58
59   /// Allow automatic conversion to the machine instruction we are working on.
60   operator MachineInstr*() const { return MI; }
61   MachineInstr *operator->() const { return MI; }
62   operator MachineBasicBlock::iterator() const { return MI; }
63
64   /// If conversion operators fail, use this method to get the MachineInstr
65   /// explicitly.
66   MachineInstr *getInstr() const { return MI; }
67
68   /// Add a new virtual register operand.
69   const MachineInstrBuilder &addReg(unsigned RegNo, unsigned flags = 0,
70                                     unsigned SubReg = 0) const {
71     assert((flags & 0x1) == 0 &&
72            "Passing in 'true' to addReg is forbidden! Use enums instead.");
73     MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
74                                                flags & RegState::Define,
75                                                flags & RegState::Implicit,
76                                                flags & RegState::Kill,
77                                                flags & RegState::Dead,
78                                                flags & RegState::Undef,
79                                                flags & RegState::EarlyClobber,
80                                                SubReg,
81                                                flags & RegState::Debug,
82                                                flags & RegState::InternalRead));
83     return *this;
84   }
85
86   /// Add a virtual register definition operand.
87   const MachineInstrBuilder &addDef(unsigned RegNo, unsigned Flags = 0,
88                                     unsigned SubReg = 0) const {
89     return addReg(RegNo, Flags | RegState::Define, SubReg);
90   }
91
92   /// Add a virtual register use operand. It is an error for Flags to contain
93   /// `RegState::Define` when calling this function.
94   const MachineInstrBuilder &addUse(unsigned RegNo, unsigned Flags = 0,
95                                     unsigned SubReg = 0) const {
96     assert(!(Flags & RegState::Define) &&
97            "Misleading addUse defines register, use addReg instead.");
98     return addReg(RegNo, Flags, SubReg);
99   }
100
101   /// Add a new immediate operand.
102   const MachineInstrBuilder &addImm(int64_t Val) const {
103     MI->addOperand(*MF, MachineOperand::CreateImm(Val));
104     return *this;
105   }
106
107   const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
108     MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
109     return *this;
110   }
111
112   const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
113     MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
114     return *this;
115   }
116
117   const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
118                                     unsigned char TargetFlags = 0) const {
119     MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
120     return *this;
121   }
122
123   const MachineInstrBuilder &addFrameIndex(int Idx) const {
124     MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
125     return *this;
126   }
127
128   const MachineInstrBuilder &addConstantPoolIndex(unsigned Idx,
129                                                   int Offset = 0,
130                                           unsigned char TargetFlags = 0) const {
131     MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
132     return *this;
133   }
134
135   const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
136                                           unsigned char TargetFlags = 0) const {
137     MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
138                                                           TargetFlags));
139     return *this;
140   }
141
142   const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
143                                           unsigned char TargetFlags = 0) const {
144     MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
145     return *this;
146   }
147
148   const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
149                                               int64_t Offset = 0,
150                                           unsigned char TargetFlags = 0) const {
151     MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
152     return *this;
153   }
154
155   const MachineInstrBuilder &addExternalSymbol(const char *FnName,
156                                           unsigned char TargetFlags = 0) const {
157     MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
158     return *this;
159   }
160
161   const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
162                                              int64_t Offset = 0,
163                                           unsigned char TargetFlags = 0) const {
164     MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
165     return *this;
166   }
167
168   const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
169     MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
170     return *this;
171   }
172
173   const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
174     MI->addMemOperand(*MF, MMO);
175     return *this;
176   }
177
178   const MachineInstrBuilder &setMemRefs(MachineInstr::mmo_iterator b,
179                                         MachineInstr::mmo_iterator e) const {
180     MI->setMemRefs(b, e);
181     return *this;
182   }
183
184   const MachineInstrBuilder &setMemRefs(std::pair<MachineInstr::mmo_iterator,
185                                         unsigned> MemOperandsRef) const {
186     MI->setMemRefs(MemOperandsRef);
187     return *this;
188   }
189
190   const MachineInstrBuilder &addOperand(const MachineOperand &MO) const {
191     MI->addOperand(*MF, MO);
192     return *this;
193   }
194
195   const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
196     MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
197     assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
198                                : true) &&
199            "first MDNode argument of a DBG_VALUE not a variable");
200     return *this;
201   }
202
203   const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
204     MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
205     return *this;
206   }
207
208   const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
209     MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
210     return *this;
211   }
212
213   const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
214     MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
215     return *this;
216   }
217
218   const MachineInstrBuilder &addSym(MCSymbol *Sym,
219                                     unsigned char TargetFlags = 0) const {
220     MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
221     return *this;
222   }
223
224   const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
225     MI->setFlags(Flags);
226     return *this;
227   }
228
229   const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
230     MI->setFlag(Flag);
231     return *this;
232   }
233
234   // Add a displacement from an existing MachineOperand with an added offset.
235   const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
236                                      unsigned char TargetFlags = 0) const {
237     // If caller specifies new TargetFlags then use it, otherwise the
238     // default behavior is to copy the target flags from the existing
239     // MachineOperand. This means if the caller wants to clear the
240     // target flags it needs to do so explicitly.
241     if (0 == TargetFlags)
242       TargetFlags = Disp.getTargetFlags();
243
244     switch (Disp.getType()) {
245       default:
246         llvm_unreachable("Unhandled operand type in addDisp()");
247       case MachineOperand::MO_Immediate:
248         return addImm(Disp.getImm() + off);
249       case MachineOperand::MO_ConstantPoolIndex:
250         return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
251                                     TargetFlags);
252       case MachineOperand::MO_GlobalAddress:
253         return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
254                                 TargetFlags);
255     }
256   }
257
258   /// Copy all the implicit operands from OtherMI onto this one.
259   const MachineInstrBuilder &
260   copyImplicitOps(const MachineInstr &OtherMI) const {
261     MI->copyImplicitOps(*MF, OtherMI);
262     return *this;
263   }
264 };
265
266 /// Builder interface. Specify how to create the initial instruction itself.
267 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
268                                    const MCInstrDesc &MCID) {
269   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
270 }
271
272 /// This version of the builder sets up the first operand as a
273 /// destination virtual register.
274 inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
275                                    const MCInstrDesc &MCID, unsigned DestReg) {
276   return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
277            .addReg(DestReg, RegState::Define);
278 }
279
280 /// This version of the builder inserts the newly-built instruction before
281 /// the given position in the given MachineBasicBlock, and sets up the first
282 /// operand as a destination virtual register.
283 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
284                                    MachineBasicBlock::iterator I,
285                                    const DebugLoc &DL, const MCInstrDesc &MCID,
286                                    unsigned DestReg) {
287   MachineFunction &MF = *BB.getParent();
288   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
289   BB.insert(I, MI);
290   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
291 }
292
293 /// This version of the builder inserts the newly-built instruction before
294 /// the given position in the given MachineBasicBlock, and sets up the first
295 /// operand as a destination virtual register.
296 ///
297 /// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
298 /// added to the same bundle.
299 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
300                                    MachineBasicBlock::instr_iterator I,
301                                    const DebugLoc &DL, const MCInstrDesc &MCID,
302                                    unsigned DestReg) {
303   MachineFunction &MF = *BB.getParent();
304   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
305   BB.insert(I, MI);
306   return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
307 }
308
309 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
310                                    const DebugLoc &DL, const MCInstrDesc &MCID,
311                                    unsigned DestReg) {
312   // Calling the overload for instr_iterator is always correct.  However, the
313   // definition is not available in headers, so inline the check.
314   if (I.isInsideBundle())
315     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
316   return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
317 }
318
319 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
320                                    const DebugLoc &DL, const MCInstrDesc &MCID,
321                                    unsigned DestReg) {
322   return BuildMI(BB, *I, DL, MCID, DestReg);
323 }
324
325 /// This version of the builder inserts the newly-built instruction before the
326 /// given position in the given MachineBasicBlock, and does NOT take a
327 /// destination register.
328 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
329                                    MachineBasicBlock::iterator I,
330                                    const DebugLoc &DL,
331                                    const MCInstrDesc &MCID) {
332   MachineFunction &MF = *BB.getParent();
333   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
334   BB.insert(I, MI);
335   return MachineInstrBuilder(MF, MI);
336 }
337
338 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
339                                    MachineBasicBlock::instr_iterator I,
340                                    const DebugLoc &DL,
341                                    const MCInstrDesc &MCID) {
342   MachineFunction &MF = *BB.getParent();
343   MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
344   BB.insert(I, MI);
345   return MachineInstrBuilder(MF, MI);
346 }
347
348 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
349                                    const DebugLoc &DL,
350                                    const MCInstrDesc &MCID) {
351   // Calling the overload for instr_iterator is always correct.  However, the
352   // definition is not available in headers, so inline the check.
353   if (I.isInsideBundle())
354     return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
355   return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
356 }
357
358 inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
359                                    const DebugLoc &DL,
360                                    const MCInstrDesc &MCID) {
361   return BuildMI(BB, *I, DL, MCID);
362 }
363
364 /// This version of the builder inserts the newly-built instruction at the end
365 /// of the given MachineBasicBlock, and does NOT take a destination register.
366 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
367                                    const MCInstrDesc &MCID) {
368   return BuildMI(*BB, BB->end(), DL, MCID);
369 }
370
371 /// This version of the builder inserts the newly-built instruction at the
372 /// end of the given MachineBasicBlock, and sets up the first operand as a
373 /// destination virtual register.
374 inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
375                                    const MCInstrDesc &MCID, unsigned DestReg) {
376   return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
377 }
378
379 /// This version of the builder builds a DBG_VALUE intrinsic
380 /// for either a value in a register or a register-indirect+offset
381 /// address.  The convention is that a DBG_VALUE is indirect iff the
382 /// second operand is an immediate.
383 MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
384                             const MCInstrDesc &MCID, bool IsIndirect,
385                             unsigned Reg, unsigned Offset,
386                             const MDNode *Variable, const MDNode *Expr);
387
388 /// This version of the builder builds a DBG_VALUE intrinsic
389 /// for either a value in a register or a register-indirect+offset
390 /// address and inserts it at position I.
391 MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
392                             MachineBasicBlock::iterator I, const DebugLoc &DL,
393                             const MCInstrDesc &MCID, bool IsIndirect,
394                             unsigned Reg, unsigned Offset,
395                             const MDNode *Variable, const MDNode *Expr);
396
397 inline unsigned getDefRegState(bool B) {
398   return B ? RegState::Define : 0;
399 }
400 inline unsigned getImplRegState(bool B) {
401   return B ? RegState::Implicit : 0;
402 }
403 inline unsigned getKillRegState(bool B) {
404   return B ? RegState::Kill : 0;
405 }
406 inline unsigned getDeadRegState(bool B) {
407   return B ? RegState::Dead : 0;
408 }
409 inline unsigned getUndefRegState(bool B) {
410   return B ? RegState::Undef : 0;
411 }
412 inline unsigned getInternalReadRegState(bool B) {
413   return B ? RegState::InternalRead : 0;
414 }
415 inline unsigned getDebugRegState(bool B) {
416   return B ? RegState::Debug : 0;
417 }
418
419 /// Get all register state flags from machine operand \p RegOp.
420 inline unsigned getRegState(const MachineOperand &RegOp) {
421   assert(RegOp.isReg() && "Not a register operand");
422   return getDefRegState(RegOp.isDef())                    |
423          getImplRegState(RegOp.isImplicit())              |
424          getKillRegState(RegOp.isKill())                  |
425          getDeadRegState(RegOp.isDead())                  |
426          getUndefRegState(RegOp.isUndef())                |
427          getInternalReadRegState(RegOp.isInternalRead())  |
428          getDebugRegState(RegOp.isDebug());
429 }
430
431 /// Helper class for constructing bundles of MachineInstrs.
432 ///
433 /// MIBundleBuilder can create a bundle from scratch by inserting new
434 /// MachineInstrs one at a time, or it can create a bundle from a sequence of
435 /// existing MachineInstrs in a basic block.
436 class MIBundleBuilder {
437   MachineBasicBlock &MBB;
438   MachineBasicBlock::instr_iterator Begin;
439   MachineBasicBlock::instr_iterator End;
440
441 public:
442   /// Create an MIBundleBuilder that inserts instructions into a new bundle in
443   /// BB above the bundle or instruction at Pos.
444   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
445       : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
446
447   /// Create a bundle from the sequence of instructions between B and E.
448   MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
449                   MachineBasicBlock::iterator E)
450       : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
451     assert(B != E && "No instructions to bundle");
452     ++B;
453     while (B != E) {
454       MachineInstr &MI = *B;
455       ++B;
456       MI.bundleWithPred();
457     }
458   }
459
460   /// Create an MIBundleBuilder representing an existing instruction or bundle
461   /// that has MI as its head.
462   explicit MIBundleBuilder(MachineInstr *MI)
463       : MBB(*MI->getParent()), Begin(MI),
464         End(getBundleEnd(MI->getIterator())) {}
465
466   /// Return a reference to the basic block containing this bundle.
467   MachineBasicBlock &getMBB() const { return MBB; }
468
469   /// Return true if no instructions have been inserted in this bundle yet.
470   /// Empty bundles aren't representable in a MachineBasicBlock.
471   bool empty() const { return Begin == End; }
472
473   /// Return an iterator to the first bundled instruction.
474   MachineBasicBlock::instr_iterator begin() const { return Begin; }
475
476   /// Return an iterator beyond the last bundled instruction.
477   MachineBasicBlock::instr_iterator end() const { return End; }
478
479   /// Insert MI into this bundle before I which must point to an instruction in
480   /// the bundle, or end().
481   MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
482                           MachineInstr *MI) {
483     MBB.insert(I, MI);
484     if (I == Begin) {
485       if (!empty())
486         MI->bundleWithSucc();
487       Begin = MI->getIterator();
488       return *this;
489     }
490     if (I == End) {
491       MI->bundleWithPred();
492       return *this;
493     }
494     // MI was inserted in the middle of the bundle, so its neighbors' flags are
495     // already fine. Update MI's bundle flags manually.
496     MI->setFlag(MachineInstr::BundledPred);
497     MI->setFlag(MachineInstr::BundledSucc);
498     return *this;
499   }
500
501   /// Insert MI into MBB by prepending it to the instructions in the bundle.
502   /// MI will become the first instruction in the bundle.
503   MIBundleBuilder &prepend(MachineInstr *MI) {
504     return insert(begin(), MI);
505   }
506
507   /// Insert MI into MBB by appending it to the instructions in the bundle.
508   /// MI will become the last instruction in the bundle.
509   MIBundleBuilder &append(MachineInstr *MI) {
510     return insert(end(), MI);
511   }
512 };
513
514 } // End llvm namespace
515
516 #endif