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