• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/CodeGen/TargetInstrInfo.h - Instruction Info --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file describes the target machine instruction set to the code generator.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
14 #define LLVM_TARGET_TARGETINSTRINFO_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseMapInfo.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/CodeGen/MIRFormatter.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineCombinerPattern.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineOperand.h"
27 #include "llvm/CodeGen/MachineOutliner.h"
28 #include "llvm/CodeGen/VirtRegMap.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/Support/BranchProbability.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include <cassert>
33 #include <cstddef>
34 #include <cstdint>
35 #include <utility>
36 #include <vector>
37 
38 namespace llvm {
39 
40 class AAResults;
41 class DFAPacketizer;
42 class InstrItineraryData;
43 class LiveIntervals;
44 class LiveVariables;
45 class MachineLoop;
46 class MachineMemOperand;
47 class MachineRegisterInfo;
48 class MCAsmInfo;
49 class MCInst;
50 struct MCSchedModel;
51 class Module;
52 class ScheduleDAG;
53 class ScheduleDAGMI;
54 class ScheduleHazardRecognizer;
55 class SDNode;
56 class SelectionDAG;
57 class RegScavenger;
58 class TargetRegisterClass;
59 class TargetRegisterInfo;
60 class TargetSchedModel;
61 class TargetSubtargetInfo;
62 
63 template <class T> class SmallVectorImpl;
64 
65 using ParamLoadedValue = std::pair<MachineOperand, DIExpression*>;
66 
67 struct DestSourcePair {
68   const MachineOperand *Destination;
69   const MachineOperand *Source;
70 
DestSourcePairDestSourcePair71   DestSourcePair(const MachineOperand &Dest, const MachineOperand &Src)
72       : Destination(&Dest), Source(&Src) {}
73 };
74 
75 /// Used to describe a register and immediate addition.
76 struct RegImmPair {
77   Register Reg;
78   int64_t Imm;
79 
RegImmPairRegImmPair80   RegImmPair(Register Reg, int64_t Imm) : Reg(Reg), Imm(Imm) {}
81 };
82 
83 /// Used to describe addressing mode similar to ExtAddrMode in CodeGenPrepare.
84 /// It holds the register values, the scale value and the displacement.
85 struct ExtAddrMode {
86   Register BaseReg;
87   Register ScaledReg;
88   int64_t Scale;
89   int64_t Displacement;
90 };
91 
92 //---------------------------------------------------------------------------
93 ///
94 /// TargetInstrInfo - Interface to description of machine instruction set
95 ///
96 class TargetInstrInfo : public MCInstrInfo {
97 public:
98   TargetInstrInfo(unsigned CFSetupOpcode = ~0u, unsigned CFDestroyOpcode = ~0u,
99                   unsigned CatchRetOpcode = ~0u, unsigned ReturnOpcode = ~0u)
CallFrameSetupOpcode(CFSetupOpcode)100       : CallFrameSetupOpcode(CFSetupOpcode),
101         CallFrameDestroyOpcode(CFDestroyOpcode), CatchRetOpcode(CatchRetOpcode),
102         ReturnOpcode(ReturnOpcode) {}
103   TargetInstrInfo(const TargetInstrInfo &) = delete;
104   TargetInstrInfo &operator=(const TargetInstrInfo &) = delete;
105   virtual ~TargetInstrInfo();
106 
isGenericOpcode(unsigned Opc)107   static bool isGenericOpcode(unsigned Opc) {
108     return Opc <= TargetOpcode::GENERIC_OP_END;
109   }
110 
111   /// Given a machine instruction descriptor, returns the register
112   /// class constraint for OpNum, or NULL.
113   virtual
114   const TargetRegisterClass *getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
115                                          const TargetRegisterInfo *TRI,
116                                          const MachineFunction &MF) const;
117 
118   /// Return true if the instruction is trivially rematerializable, meaning it
119   /// has no side effects and requires no operands that aren't always available.
120   /// This means the only allowed uses are constants and unallocatable physical
121   /// registers so that the instructions result is independent of the place
122   /// in the function.
123   bool isTriviallyReMaterializable(const MachineInstr &MI,
124                                    AAResults *AA = nullptr) const {
125     return MI.getOpcode() == TargetOpcode::IMPLICIT_DEF ||
126            (MI.getDesc().isRematerializable() &&
127             (isReallyTriviallyReMaterializable(MI, AA) ||
128              isReallyTriviallyReMaterializableGeneric(MI, AA)));
129   }
130 
131 protected:
132   /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
133   /// set, this hook lets the target specify whether the instruction is actually
134   /// trivially rematerializable, taking into consideration its operands. This
135   /// predicate must return false if the instruction has any side effects other
136   /// than producing a value, or if it requres any address registers that are
137   /// not always available.
138   /// Requirements must be check as stated in isTriviallyReMaterializable() .
isReallyTriviallyReMaterializable(const MachineInstr & MI,AAResults * AA)139   virtual bool isReallyTriviallyReMaterializable(const MachineInstr &MI,
140                                                  AAResults *AA) const {
141     return false;
142   }
143 
144   /// This method commutes the operands of the given machine instruction MI.
145   /// The operands to be commuted are specified by their indices OpIdx1 and
146   /// OpIdx2.
147   ///
148   /// If a target has any instructions that are commutable but require
149   /// converting to different instructions or making non-trivial changes
150   /// to commute them, this method can be overloaded to do that.
151   /// The default implementation simply swaps the commutable operands.
152   ///
153   /// If NewMI is false, MI is modified in place and returned; otherwise, a
154   /// new machine instruction is created and returned.
155   ///
156   /// Do not call this method for a non-commutable instruction.
157   /// Even though the instruction is commutable, the method may still
158   /// fail to commute the operands, null pointer is returned in such cases.
159   virtual MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
160                                                unsigned OpIdx1,
161                                                unsigned OpIdx2) const;
162 
163   /// Assigns the (CommutableOpIdx1, CommutableOpIdx2) pair of commutable
164   /// operand indices to (ResultIdx1, ResultIdx2).
165   /// One or both input values of the pair: (ResultIdx1, ResultIdx2) may be
166   /// predefined to some indices or be undefined (designated by the special
167   /// value 'CommuteAnyOperandIndex').
168   /// The predefined result indices cannot be re-defined.
169   /// The function returns true iff after the result pair redefinition
170   /// the fixed result pair is equal to or equivalent to the source pair of
171   /// indices: (CommutableOpIdx1, CommutableOpIdx2). It is assumed here that
172   /// the pairs (x,y) and (y,x) are equivalent.
173   static bool fixCommutedOpIndices(unsigned &ResultIdx1, unsigned &ResultIdx2,
174                                    unsigned CommutableOpIdx1,
175                                    unsigned CommutableOpIdx2);
176 
177 private:
178   /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
179   /// set and the target hook isReallyTriviallyReMaterializable returns false,
180   /// this function does target-independent tests to determine if the
181   /// instruction is really trivially rematerializable.
182   bool isReallyTriviallyReMaterializableGeneric(const MachineInstr &MI,
183                                                 AAResults *AA) const;
184 
185 public:
186   /// These methods return the opcode of the frame setup/destroy instructions
187   /// if they exist (-1 otherwise).  Some targets use pseudo instructions in
188   /// order to abstract away the difference between operating with a frame
189   /// pointer and operating without, through the use of these two instructions.
190   ///
getCallFrameSetupOpcode()191   unsigned getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
getCallFrameDestroyOpcode()192   unsigned getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
193 
194   /// Returns true if the argument is a frame pseudo instruction.
isFrameInstr(const MachineInstr & I)195   bool isFrameInstr(const MachineInstr &I) const {
196     return I.getOpcode() == getCallFrameSetupOpcode() ||
197            I.getOpcode() == getCallFrameDestroyOpcode();
198   }
199 
200   /// Returns true if the argument is a frame setup pseudo instruction.
isFrameSetup(const MachineInstr & I)201   bool isFrameSetup(const MachineInstr &I) const {
202     return I.getOpcode() == getCallFrameSetupOpcode();
203   }
204 
205   /// Returns size of the frame associated with the given frame instruction.
206   /// For frame setup instruction this is frame that is set up space set up
207   /// after the instruction. For frame destroy instruction this is the frame
208   /// freed by the caller.
209   /// Note, in some cases a call frame (or a part of it) may be prepared prior
210   /// to the frame setup instruction. It occurs in the calls that involve
211   /// inalloca arguments. This function reports only the size of the frame part
212   /// that is set up between the frame setup and destroy pseudo instructions.
getFrameSize(const MachineInstr & I)213   int64_t getFrameSize(const MachineInstr &I) const {
214     assert(isFrameInstr(I) && "Not a frame instruction");
215     assert(I.getOperand(0).getImm() >= 0);
216     return I.getOperand(0).getImm();
217   }
218 
219   /// Returns the total frame size, which is made up of the space set up inside
220   /// the pair of frame start-stop instructions and the space that is set up
221   /// prior to the pair.
getFrameTotalSize(const MachineInstr & I)222   int64_t getFrameTotalSize(const MachineInstr &I) const {
223     if (isFrameSetup(I)) {
224       assert(I.getOperand(1).getImm() >= 0 &&
225              "Frame size must not be negative");
226       return getFrameSize(I) + I.getOperand(1).getImm();
227     }
228     return getFrameSize(I);
229   }
230 
getCatchReturnOpcode()231   unsigned getCatchReturnOpcode() const { return CatchRetOpcode; }
getReturnOpcode()232   unsigned getReturnOpcode() const { return ReturnOpcode; }
233 
234   /// Returns the actual stack pointer adjustment made by an instruction
235   /// as part of a call sequence. By default, only call frame setup/destroy
236   /// instructions adjust the stack, but targets may want to override this
237   /// to enable more fine-grained adjustment, or adjust by a different value.
238   virtual int getSPAdjust(const MachineInstr &MI) const;
239 
240   /// Return true if the instruction is a "coalescable" extension instruction.
241   /// That is, it's like a copy where it's legal for the source to overlap the
242   /// destination. e.g. X86::MOVSX64rr32. If this returns true, then it's
243   /// expected the pre-extension value is available as a subreg of the result
244   /// register. This also returns the sub-register index in SubIdx.
isCoalescableExtInstr(const MachineInstr & MI,Register & SrcReg,Register & DstReg,unsigned & SubIdx)245   virtual bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg,
246                                      Register &DstReg, unsigned &SubIdx) const {
247     return false;
248   }
249 
250   /// If the specified machine instruction is a direct
251   /// load from a stack slot, return the virtual or physical register number of
252   /// the destination along with the FrameIndex of the loaded stack slot.  If
253   /// not, return 0.  This predicate must return 0 if the instruction has
254   /// any side effects other than loading from the stack slot.
isLoadFromStackSlot(const MachineInstr & MI,int & FrameIndex)255   virtual unsigned isLoadFromStackSlot(const MachineInstr &MI,
256                                        int &FrameIndex) const {
257     return 0;
258   }
259 
260   /// Optional extension of isLoadFromStackSlot that returns the number of
261   /// bytes loaded from the stack. This must be implemented if a backend
262   /// supports partial stack slot spills/loads to further disambiguate
263   /// what the load does.
isLoadFromStackSlot(const MachineInstr & MI,int & FrameIndex,unsigned & MemBytes)264   virtual unsigned isLoadFromStackSlot(const MachineInstr &MI,
265                                        int &FrameIndex,
266                                        unsigned &MemBytes) const {
267     MemBytes = 0;
268     return isLoadFromStackSlot(MI, FrameIndex);
269   }
270 
271   /// Check for post-frame ptr elimination stack locations as well.
272   /// This uses a heuristic so it isn't reliable for correctness.
isLoadFromStackSlotPostFE(const MachineInstr & MI,int & FrameIndex)273   virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI,
274                                              int &FrameIndex) const {
275     return 0;
276   }
277 
278   /// If the specified machine instruction has a load from a stack slot,
279   /// return true along with the FrameIndices of the loaded stack slot and the
280   /// machine mem operands containing the reference.
281   /// If not, return false.  Unlike isLoadFromStackSlot, this returns true for
282   /// any instructions that loads from the stack.  This is just a hint, as some
283   /// cases may be missed.
284   virtual bool hasLoadFromStackSlot(
285       const MachineInstr &MI,
286       SmallVectorImpl<const MachineMemOperand *> &Accesses) const;
287 
288   /// If the specified machine instruction is a direct
289   /// store to a stack slot, return the virtual or physical register number of
290   /// the source reg along with the FrameIndex of the loaded stack slot.  If
291   /// not, return 0.  This predicate must return 0 if the instruction has
292   /// any side effects other than storing to the stack slot.
isStoreToStackSlot(const MachineInstr & MI,int & FrameIndex)293   virtual unsigned isStoreToStackSlot(const MachineInstr &MI,
294                                       int &FrameIndex) const {
295     return 0;
296   }
297 
298   /// Optional extension of isStoreToStackSlot that returns the number of
299   /// bytes stored to the stack. This must be implemented if a backend
300   /// supports partial stack slot spills/loads to further disambiguate
301   /// what the store does.
isStoreToStackSlot(const MachineInstr & MI,int & FrameIndex,unsigned & MemBytes)302   virtual unsigned isStoreToStackSlot(const MachineInstr &MI,
303                                       int &FrameIndex,
304                                       unsigned &MemBytes) const {
305     MemBytes = 0;
306     return isStoreToStackSlot(MI, FrameIndex);
307   }
308 
309   /// Check for post-frame ptr elimination stack locations as well.
310   /// This uses a heuristic, so it isn't reliable for correctness.
isStoreToStackSlotPostFE(const MachineInstr & MI,int & FrameIndex)311   virtual unsigned isStoreToStackSlotPostFE(const MachineInstr &MI,
312                                             int &FrameIndex) const {
313     return 0;
314   }
315 
316   /// If the specified machine instruction has a store to a stack slot,
317   /// return true along with the FrameIndices of the loaded stack slot and the
318   /// machine mem operands containing the reference.
319   /// If not, return false.  Unlike isStoreToStackSlot,
320   /// this returns true for any instructions that stores to the
321   /// stack.  This is just a hint, as some cases may be missed.
322   virtual bool hasStoreToStackSlot(
323       const MachineInstr &MI,
324       SmallVectorImpl<const MachineMemOperand *> &Accesses) const;
325 
326   /// Return true if the specified machine instruction
327   /// is a copy of one stack slot to another and has no other effect.
328   /// Provide the identity of the two frame indices.
isStackSlotCopy(const MachineInstr & MI,int & DestFrameIndex,int & SrcFrameIndex)329   virtual bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex,
330                                int &SrcFrameIndex) const {
331     return false;
332   }
333 
334   /// Compute the size in bytes and offset within a stack slot of a spilled
335   /// register or subregister.
336   ///
337   /// \param [out] Size in bytes of the spilled value.
338   /// \param [out] Offset in bytes within the stack slot.
339   /// \returns true if both Size and Offset are successfully computed.
340   ///
341   /// Not all subregisters have computable spill slots. For example,
342   /// subregisters registers may not be byte-sized, and a pair of discontiguous
343   /// subregisters has no single offset.
344   ///
345   /// Targets with nontrivial bigendian implementations may need to override
346   /// this, particularly to support spilled vector registers.
347   virtual bool getStackSlotRange(const TargetRegisterClass *RC, unsigned SubIdx,
348                                  unsigned &Size, unsigned &Offset,
349                                  const MachineFunction &MF) const;
350 
351   /// Returns the size in bytes of the specified MachineInstr, or ~0U
352   /// when this function is not implemented by a target.
getInstSizeInBytes(const MachineInstr & MI)353   virtual unsigned getInstSizeInBytes(const MachineInstr &MI) const {
354     return ~0U;
355   }
356 
357   /// Return true if the instruction is as cheap as a move instruction.
358   ///
359   /// Targets for different archs need to override this, and different
360   /// micro-architectures can also be finely tuned inside.
isAsCheapAsAMove(const MachineInstr & MI)361   virtual bool isAsCheapAsAMove(const MachineInstr &MI) const {
362     return MI.isAsCheapAsAMove();
363   }
364 
365   /// Return true if the instruction should be sunk by MachineSink.
366   ///
367   /// MachineSink determines on its own whether the instruction is safe to sink;
368   /// this gives the target a hook to override the default behavior with regards
369   /// to which instructions should be sunk.
shouldSink(const MachineInstr & MI)370   virtual bool shouldSink(const MachineInstr &MI) const { return true; }
371 
372   /// Re-issue the specified 'original' instruction at the
373   /// specific location targeting a new destination register.
374   /// The register in Orig->getOperand(0).getReg() will be substituted by
375   /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
376   /// SubIdx.
377   virtual void reMaterialize(MachineBasicBlock &MBB,
378                              MachineBasicBlock::iterator MI, Register DestReg,
379                              unsigned SubIdx, const MachineInstr &Orig,
380                              const TargetRegisterInfo &TRI) const;
381 
382   /// Clones instruction or the whole instruction bundle \p Orig and
383   /// insert into \p MBB before \p InsertBefore. The target may update operands
384   /// that are required to be unique.
385   ///
386   /// \p Orig must not return true for MachineInstr::isNotDuplicable().
387   virtual MachineInstr &duplicate(MachineBasicBlock &MBB,
388                                   MachineBasicBlock::iterator InsertBefore,
389                                   const MachineInstr &Orig) const;
390 
391   /// This method must be implemented by targets that
392   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
393   /// may be able to convert a two-address instruction into one or more true
394   /// three-address instructions on demand.  This allows the X86 target (for
395   /// example) to convert ADD and SHL instructions into LEA instructions if they
396   /// would require register copies due to two-addressness.
397   ///
398   /// This method returns a null pointer if the transformation cannot be
399   /// performed, otherwise it returns the last new instruction.
400   ///
convertToThreeAddress(MachineFunction::iterator & MFI,MachineInstr & MI,LiveVariables * LV)401   virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
402                                               MachineInstr &MI,
403                                               LiveVariables *LV) const {
404     return nullptr;
405   }
406 
407   // This constant can be used as an input value of operand index passed to
408   // the method findCommutedOpIndices() to tell the method that the
409   // corresponding operand index is not pre-defined and that the method
410   // can pick any commutable operand.
411   static const unsigned CommuteAnyOperandIndex = ~0U;
412 
413   /// This method commutes the operands of the given machine instruction MI.
414   ///
415   /// The operands to be commuted are specified by their indices OpIdx1 and
416   /// OpIdx2. OpIdx1 and OpIdx2 arguments may be set to a special value
417   /// 'CommuteAnyOperandIndex', which means that the method is free to choose
418   /// any arbitrarily chosen commutable operand. If both arguments are set to
419   /// 'CommuteAnyOperandIndex' then the method looks for 2 different commutable
420   /// operands; then commutes them if such operands could be found.
421   ///
422   /// If NewMI is false, MI is modified in place and returned; otherwise, a
423   /// new machine instruction is created and returned.
424   ///
425   /// Do not call this method for a non-commutable instruction or
426   /// for non-commuable operands.
427   /// Even though the instruction is commutable, the method may still
428   /// fail to commute the operands, null pointer is returned in such cases.
429   MachineInstr *
430   commuteInstruction(MachineInstr &MI, bool NewMI = false,
431                      unsigned OpIdx1 = CommuteAnyOperandIndex,
432                      unsigned OpIdx2 = CommuteAnyOperandIndex) const;
433 
434   /// Returns true iff the routine could find two commutable operands in the
435   /// given machine instruction.
436   /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments.
437   /// If any of the INPUT values is set to the special value
438   /// 'CommuteAnyOperandIndex' then the method arbitrarily picks a commutable
439   /// operand, then returns its index in the corresponding argument.
440   /// If both of INPUT values are set to 'CommuteAnyOperandIndex' then method
441   /// looks for 2 commutable operands.
442   /// If INPUT values refer to some operands of MI, then the method simply
443   /// returns true if the corresponding operands are commutable and returns
444   /// false otherwise.
445   ///
446   /// For example, calling this method this way:
447   ///     unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
448   ///     findCommutedOpIndices(MI, Op1, Op2);
449   /// can be interpreted as a query asking to find an operand that would be
450   /// commutable with the operand#1.
451   virtual bool findCommutedOpIndices(const MachineInstr &MI,
452                                      unsigned &SrcOpIdx1,
453                                      unsigned &SrcOpIdx2) const;
454 
455   /// A pair composed of a register and a sub-register index.
456   /// Used to give some type checking when modeling Reg:SubReg.
457   struct RegSubRegPair {
458     Register Reg;
459     unsigned SubReg;
460 
461     RegSubRegPair(Register Reg = Register(), unsigned SubReg = 0)
RegRegSubRegPair462         : Reg(Reg), SubReg(SubReg) {}
463 
464     bool operator==(const RegSubRegPair& P) const {
465       return Reg == P.Reg && SubReg == P.SubReg;
466     }
467     bool operator!=(const RegSubRegPair& P) const {
468       return !(*this == P);
469     }
470   };
471 
472   /// A pair composed of a pair of a register and a sub-register index,
473   /// and another sub-register index.
474   /// Used to give some type checking when modeling Reg:SubReg1, SubReg2.
475   struct RegSubRegPairAndIdx : RegSubRegPair {
476     unsigned SubIdx;
477 
478     RegSubRegPairAndIdx(Register Reg = Register(), unsigned SubReg = 0,
479                         unsigned SubIdx = 0)
RegSubRegPairRegSubRegPairAndIdx480         : RegSubRegPair(Reg, SubReg), SubIdx(SubIdx) {}
481   };
482 
483   /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
484   /// and \p DefIdx.
485   /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
486   /// the list is modeled as <Reg:SubReg, SubIdx>. Operands with the undef
487   /// flag are not added to this list.
488   /// E.g., REG_SEQUENCE %1:sub1, sub0, %2, sub1 would produce
489   /// two elements:
490   /// - %1:sub1, sub0
491   /// - %2<:0>, sub1
492   ///
493   /// \returns true if it is possible to build such an input sequence
494   /// with the pair \p MI, \p DefIdx. False otherwise.
495   ///
496   /// \pre MI.isRegSequence() or MI.isRegSequenceLike().
497   ///
498   /// \note The generic implementation does not provide any support for
499   /// MI.isRegSequenceLike(). In other words, one has to override
500   /// getRegSequenceLikeInputs for target specific instructions.
501   bool
502   getRegSequenceInputs(const MachineInstr &MI, unsigned DefIdx,
503                        SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const;
504 
505   /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
506   /// and \p DefIdx.
507   /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
508   /// E.g., EXTRACT_SUBREG %1:sub1, sub0, sub1 would produce:
509   /// - %1:sub1, sub0
510   ///
511   /// \returns true if it is possible to build such an input sequence
512   /// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
513   /// False otherwise.
514   ///
515   /// \pre MI.isExtractSubreg() or MI.isExtractSubregLike().
516   ///
517   /// \note The generic implementation does not provide any support for
518   /// MI.isExtractSubregLike(). In other words, one has to override
519   /// getExtractSubregLikeInputs for target specific instructions.
520   bool getExtractSubregInputs(const MachineInstr &MI, unsigned DefIdx,
521                               RegSubRegPairAndIdx &InputReg) const;
522 
523   /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
524   /// and \p DefIdx.
525   /// \p [out] BaseReg and \p [out] InsertedReg contain
526   /// the equivalent inputs of INSERT_SUBREG.
527   /// E.g., INSERT_SUBREG %0:sub0, %1:sub1, sub3 would produce:
528   /// - BaseReg: %0:sub0
529   /// - InsertedReg: %1:sub1, sub3
530   ///
531   /// \returns true if it is possible to build such an input sequence
532   /// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
533   /// False otherwise.
534   ///
535   /// \pre MI.isInsertSubreg() or MI.isInsertSubregLike().
536   ///
537   /// \note The generic implementation does not provide any support for
538   /// MI.isInsertSubregLike(). In other words, one has to override
539   /// getInsertSubregLikeInputs for target specific instructions.
540   bool getInsertSubregInputs(const MachineInstr &MI, unsigned DefIdx,
541                              RegSubRegPair &BaseReg,
542                              RegSubRegPairAndIdx &InsertedReg) const;
543 
544   /// Return true if two machine instructions would produce identical values.
545   /// By default, this is only true when the two instructions
546   /// are deemed identical except for defs. If this function is called when the
547   /// IR is still in SSA form, the caller can pass the MachineRegisterInfo for
548   /// aggressive checks.
549   virtual bool produceSameValue(const MachineInstr &MI0,
550                                 const MachineInstr &MI1,
551                                 const MachineRegisterInfo *MRI = nullptr) const;
552 
553   /// \returns true if a branch from an instruction with opcode \p BranchOpc
554   ///  bytes is capable of jumping to a position \p BrOffset bytes away.
isBranchOffsetInRange(unsigned BranchOpc,int64_t BrOffset)555   virtual bool isBranchOffsetInRange(unsigned BranchOpc,
556                                      int64_t BrOffset) const {
557     llvm_unreachable("target did not implement");
558   }
559 
560   /// \returns The block that branch instruction \p MI jumps to.
getBranchDestBlock(const MachineInstr & MI)561   virtual MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const {
562     llvm_unreachable("target did not implement");
563   }
564 
565   /// Insert an unconditional indirect branch at the end of \p MBB to \p
566   /// NewDestBB.  \p BrOffset indicates the offset of \p NewDestBB relative to
567   /// the offset of the position to insert the new branch.
568   ///
569   /// \returns The number of bytes added to the block.
570   virtual unsigned insertIndirectBranch(MachineBasicBlock &MBB,
571                                         MachineBasicBlock &NewDestBB,
572                                         const DebugLoc &DL,
573                                         int64_t BrOffset = 0,
574                                         RegScavenger *RS = nullptr) const {
575     llvm_unreachable("target did not implement");
576   }
577 
578   /// Analyze the branching code at the end of MBB, returning
579   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
580   /// implemented for a target).  Upon success, this returns false and returns
581   /// with the following information in various cases:
582   ///
583   /// 1. If this block ends with no branches (it just falls through to its succ)
584   ///    just return false, leaving TBB/FBB null.
585   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
586   ///    the destination block.
587   /// 3. If this block ends with a conditional branch and it falls through to a
588   ///    successor block, it sets TBB to be the branch destination block and a
589   ///    list of operands that evaluate the condition. These operands can be
590   ///    passed to other TargetInstrInfo methods to create new branches.
591   /// 4. If this block ends with a conditional branch followed by an
592   ///    unconditional branch, it returns the 'true' destination in TBB, the
593   ///    'false' destination in FBB, and a list of operands that evaluate the
594   ///    condition.  These operands can be passed to other TargetInstrInfo
595   ///    methods to create new branches.
596   ///
597   /// Note that removeBranch and insertBranch must be implemented to support
598   /// cases where this method returns success.
599   ///
600   /// If AllowModify is true, then this routine is allowed to modify the basic
601   /// block (e.g. delete instructions after the unconditional branch).
602   ///
603   /// The CFG information in MBB.Predecessors and MBB.Successors must be valid
604   /// before calling this function.
605   virtual bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
606                              MachineBasicBlock *&FBB,
607                              SmallVectorImpl<MachineOperand> &Cond,
608                              bool AllowModify = false) const {
609     return true;
610   }
611 
612   /// Represents a predicate at the MachineFunction level.  The control flow a
613   /// MachineBranchPredicate represents is:
614   ///
615   ///  Reg = LHS `Predicate` RHS         == ConditionDef
616   ///  if Reg then goto TrueDest else goto FalseDest
617   ///
618   struct MachineBranchPredicate {
619     enum ComparePredicate {
620       PRED_EQ,     // True if two values are equal
621       PRED_NE,     // True if two values are not equal
622       PRED_INVALID // Sentinel value
623     };
624 
625     ComparePredicate Predicate = PRED_INVALID;
626     MachineOperand LHS = MachineOperand::CreateImm(0);
627     MachineOperand RHS = MachineOperand::CreateImm(0);
628     MachineBasicBlock *TrueDest = nullptr;
629     MachineBasicBlock *FalseDest = nullptr;
630     MachineInstr *ConditionDef = nullptr;
631 
632     /// SingleUseCondition is true if ConditionDef is dead except for the
633     /// branch(es) at the end of the basic block.
634     ///
635     bool SingleUseCondition = false;
636 
637     explicit MachineBranchPredicate() = default;
638   };
639 
640   /// Analyze the branching code at the end of MBB and parse it into the
641   /// MachineBranchPredicate structure if possible.  Returns false on success
642   /// and true on failure.
643   ///
644   /// If AllowModify is true, then this routine is allowed to modify the basic
645   /// block (e.g. delete instructions after the unconditional branch).
646   ///
647   virtual bool analyzeBranchPredicate(MachineBasicBlock &MBB,
648                                       MachineBranchPredicate &MBP,
649                                       bool AllowModify = false) const {
650     return true;
651   }
652 
653   /// Remove the branching code at the end of the specific MBB.
654   /// This is only invoked in cases where analyzeBranch returns success. It
655   /// returns the number of instructions that were removed.
656   /// If \p BytesRemoved is non-null, report the change in code size from the
657   /// removed instructions.
658   virtual unsigned removeBranch(MachineBasicBlock &MBB,
659                                 int *BytesRemoved = nullptr) const {
660     llvm_unreachable("Target didn't implement TargetInstrInfo::removeBranch!");
661   }
662 
663   /// Insert branch code into the end of the specified MachineBasicBlock. The
664   /// operands to this method are the same as those returned by analyzeBranch.
665   /// This is only invoked in cases where analyzeBranch returns success. It
666   /// returns the number of instructions inserted. If \p BytesAdded is non-null,
667   /// report the change in code size from the added instructions.
668   ///
669   /// It is also invoked by tail merging to add unconditional branches in
670   /// cases where analyzeBranch doesn't apply because there was no original
671   /// branch to analyze.  At least this much must be implemented, else tail
672   /// merging needs to be disabled.
673   ///
674   /// The CFG information in MBB.Predecessors and MBB.Successors must be valid
675   /// before calling this function.
676   virtual unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
677                                 MachineBasicBlock *FBB,
678                                 ArrayRef<MachineOperand> Cond,
679                                 const DebugLoc &DL,
680                                 int *BytesAdded = nullptr) const {
681     llvm_unreachable("Target didn't implement TargetInstrInfo::insertBranch!");
682   }
683 
684   unsigned insertUnconditionalBranch(MachineBasicBlock &MBB,
685                                      MachineBasicBlock *DestBB,
686                                      const DebugLoc &DL,
687                                      int *BytesAdded = nullptr) const {
688     return insertBranch(MBB, DestBB, nullptr, ArrayRef<MachineOperand>(), DL,
689                         BytesAdded);
690   }
691 
692   /// Object returned by analyzeLoopForPipelining. Allows software pipelining
693   /// implementations to query attributes of the loop being pipelined and to
694   /// apply target-specific updates to the loop once pipelining is complete.
695   class PipelinerLoopInfo {
696   public:
697     virtual ~PipelinerLoopInfo();
698     /// Return true if the given instruction should not be pipelined and should
699     /// be ignored. An example could be a loop comparison, or induction variable
700     /// update with no users being pipelined.
701     virtual bool shouldIgnoreForPipelining(const MachineInstr *MI) const = 0;
702 
703     /// Create a condition to determine if the trip count of the loop is greater
704     /// than TC.
705     ///
706     /// If the trip count is statically known to be greater than TC, return
707     /// true. If the trip count is statically known to be not greater than TC,
708     /// return false. Otherwise return nullopt and fill out Cond with the test
709     /// condition.
710     virtual Optional<bool>
711     createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
712                                     SmallVectorImpl<MachineOperand> &Cond) = 0;
713 
714     /// Modify the loop such that the trip count is
715     /// OriginalTC + TripCountAdjust.
716     virtual void adjustTripCount(int TripCountAdjust) = 0;
717 
718     /// Called when the loop's preheader has been modified to NewPreheader.
719     virtual void setPreheader(MachineBasicBlock *NewPreheader) = 0;
720 
721     /// Called when the loop is being removed. Any instructions in the preheader
722     /// should be removed.
723     ///
724     /// Once this function is called, no other functions on this object are
725     /// valid; the loop has been removed.
726     virtual void disposed() = 0;
727   };
728 
729   /// Analyze loop L, which must be a single-basic-block loop, and if the
730   /// conditions can be understood enough produce a PipelinerLoopInfo object.
731   virtual std::unique_ptr<PipelinerLoopInfo>
analyzeLoopForPipelining(MachineBasicBlock * LoopBB)732   analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
733     return nullptr;
734   }
735 
736   /// Analyze the loop code, return true if it cannot be understood. Upon
737   /// success, this function returns false and returns information about the
738   /// induction variable and compare instruction used at the end.
analyzeLoop(MachineLoop & L,MachineInstr * & IndVarInst,MachineInstr * & CmpInst)739   virtual bool analyzeLoop(MachineLoop &L, MachineInstr *&IndVarInst,
740                            MachineInstr *&CmpInst) const {
741     return true;
742   }
743 
744   /// Generate code to reduce the loop iteration by one and check if the loop
745   /// is finished.  Return the value/register of the new loop count.  We need
746   /// this function when peeling off one or more iterations of a loop. This
747   /// function assumes the nth iteration is peeled first.
reduceLoopCount(MachineBasicBlock & MBB,MachineBasicBlock & PreHeader,MachineInstr * IndVar,MachineInstr & Cmp,SmallVectorImpl<MachineOperand> & Cond,SmallVectorImpl<MachineInstr * > & PrevInsts,unsigned Iter,unsigned MaxIter)748   virtual unsigned reduceLoopCount(MachineBasicBlock &MBB,
749                                    MachineBasicBlock &PreHeader,
750                                    MachineInstr *IndVar, MachineInstr &Cmp,
751                                    SmallVectorImpl<MachineOperand> &Cond,
752                                    SmallVectorImpl<MachineInstr *> &PrevInsts,
753                                    unsigned Iter, unsigned MaxIter) const {
754     llvm_unreachable("Target didn't implement ReduceLoopCount");
755   }
756 
757   /// Delete the instruction OldInst and everything after it, replacing it with
758   /// an unconditional branch to NewDest. This is used by the tail merging pass.
759   virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
760                                        MachineBasicBlock *NewDest) const;
761 
762   /// Return true if it's legal to split the given basic
763   /// block at the specified instruction (i.e. instruction would be the start
764   /// of a new basic block).
isLegalToSplitMBBAt(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI)765   virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
766                                    MachineBasicBlock::iterator MBBI) const {
767     return true;
768   }
769 
770   /// Return true if it's profitable to predicate
771   /// instructions with accumulated instruction latency of "NumCycles"
772   /// of the specified basic block, where the probability of the instructions
773   /// being executed is given by Probability, and Confidence is a measure
774   /// of our confidence that it will be properly predicted.
isProfitableToIfCvt(MachineBasicBlock & MBB,unsigned NumCycles,unsigned ExtraPredCycles,BranchProbability Probability)775   virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
776                                    unsigned ExtraPredCycles,
777                                    BranchProbability Probability) const {
778     return false;
779   }
780 
781   /// Second variant of isProfitableToIfCvt. This one
782   /// checks for the case where two basic blocks from true and false path
783   /// of a if-then-else (diamond) are predicated on mutually exclusive
784   /// predicates, where the probability of the true path being taken is given
785   /// by Probability, and Confidence is a measure of our confidence that it
786   /// will be properly predicted.
isProfitableToIfCvt(MachineBasicBlock & TMBB,unsigned NumTCycles,unsigned ExtraTCycles,MachineBasicBlock & FMBB,unsigned NumFCycles,unsigned ExtraFCycles,BranchProbability Probability)787   virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTCycles,
788                                    unsigned ExtraTCycles,
789                                    MachineBasicBlock &FMBB, unsigned NumFCycles,
790                                    unsigned ExtraFCycles,
791                                    BranchProbability Probability) const {
792     return false;
793   }
794 
795   /// Return true if it's profitable for if-converter to duplicate instructions
796   /// of specified accumulated instruction latencies in the specified MBB to
797   /// enable if-conversion.
798   /// The probability of the instructions being executed is given by
799   /// Probability, and Confidence is a measure of our confidence that it
800   /// will be properly predicted.
isProfitableToDupForIfCvt(MachineBasicBlock & MBB,unsigned NumCycles,BranchProbability Probability)801   virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
802                                          unsigned NumCycles,
803                                          BranchProbability Probability) const {
804     return false;
805   }
806 
807   /// Return the increase in code size needed to predicate a contiguous run of
808   /// NumInsts instructions.
extraSizeToPredicateInstructions(const MachineFunction & MF,unsigned NumInsts)809   virtual unsigned extraSizeToPredicateInstructions(const MachineFunction &MF,
810                                                     unsigned NumInsts) const {
811     return 0;
812   }
813 
814   /// Return an estimate for the code size reduction (in bytes) which will be
815   /// caused by removing the given branch instruction during if-conversion.
predictBranchSizeForIfCvt(MachineInstr & MI)816   virtual unsigned predictBranchSizeForIfCvt(MachineInstr &MI) const {
817     return getInstSizeInBytes(MI);
818   }
819 
820   /// Return true if it's profitable to unpredicate
821   /// one side of a 'diamond', i.e. two sides of if-else predicated on mutually
822   /// exclusive predicates.
823   /// e.g.
824   ///   subeq  r0, r1, #1
825   ///   addne  r0, r1, #1
826   /// =>
827   ///   sub    r0, r1, #1
828   ///   addne  r0, r1, #1
829   ///
830   /// This may be profitable is conditional instructions are always executed.
isProfitableToUnpredicate(MachineBasicBlock & TMBB,MachineBasicBlock & FMBB)831   virtual bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
832                                          MachineBasicBlock &FMBB) const {
833     return false;
834   }
835 
836   /// Return true if it is possible to insert a select
837   /// instruction that chooses between TrueReg and FalseReg based on the
838   /// condition code in Cond.
839   ///
840   /// When successful, also return the latency in cycles from TrueReg,
841   /// FalseReg, and Cond to the destination register. In most cases, a select
842   /// instruction will be 1 cycle, so CondCycles = TrueCycles = FalseCycles = 1
843   ///
844   /// Some x86 implementations have 2-cycle cmov instructions.
845   ///
846   /// @param MBB         Block where select instruction would be inserted.
847   /// @param Cond        Condition returned by analyzeBranch.
848   /// @param DstReg      Virtual dest register that the result should write to.
849   /// @param TrueReg     Virtual register to select when Cond is true.
850   /// @param FalseReg    Virtual register to select when Cond is false.
851   /// @param CondCycles  Latency from Cond+Branch to select output.
852   /// @param TrueCycles  Latency from TrueReg to select output.
853   /// @param FalseCycles Latency from FalseReg to select output.
canInsertSelect(const MachineBasicBlock & MBB,ArrayRef<MachineOperand> Cond,Register DstReg,Register TrueReg,Register FalseReg,int & CondCycles,int & TrueCycles,int & FalseCycles)854   virtual bool canInsertSelect(const MachineBasicBlock &MBB,
855                                ArrayRef<MachineOperand> Cond, Register DstReg,
856                                Register TrueReg, Register FalseReg,
857                                int &CondCycles, int &TrueCycles,
858                                int &FalseCycles) const {
859     return false;
860   }
861 
862   /// Insert a select instruction into MBB before I that will copy TrueReg to
863   /// DstReg when Cond is true, and FalseReg to DstReg when Cond is false.
864   ///
865   /// This function can only be called after canInsertSelect() returned true.
866   /// The condition in Cond comes from analyzeBranch, and it can be assumed
867   /// that the same flags or registers required by Cond are available at the
868   /// insertion point.
869   ///
870   /// @param MBB      Block where select instruction should be inserted.
871   /// @param I        Insertion point.
872   /// @param DL       Source location for debugging.
873   /// @param DstReg   Virtual register to be defined by select instruction.
874   /// @param Cond     Condition as computed by analyzeBranch.
875   /// @param TrueReg  Virtual register to copy when Cond is true.
876   /// @param FalseReg Virtual register to copy when Cons is false.
insertSelect(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,const DebugLoc & DL,Register DstReg,ArrayRef<MachineOperand> Cond,Register TrueReg,Register FalseReg)877   virtual void insertSelect(MachineBasicBlock &MBB,
878                             MachineBasicBlock::iterator I, const DebugLoc &DL,
879                             Register DstReg, ArrayRef<MachineOperand> Cond,
880                             Register TrueReg, Register FalseReg) const {
881     llvm_unreachable("Target didn't implement TargetInstrInfo::insertSelect!");
882   }
883 
884   /// Analyze the given select instruction, returning true if
885   /// it cannot be understood. It is assumed that MI->isSelect() is true.
886   ///
887   /// When successful, return the controlling condition and the operands that
888   /// determine the true and false result values.
889   ///
890   ///   Result = SELECT Cond, TrueOp, FalseOp
891   ///
892   /// Some targets can optimize select instructions, for example by predicating
893   /// the instruction defining one of the operands. Such targets should set
894   /// Optimizable.
895   ///
896   /// @param         MI Select instruction to analyze.
897   /// @param Cond    Condition controlling the select.
898   /// @param TrueOp  Operand number of the value selected when Cond is true.
899   /// @param FalseOp Operand number of the value selected when Cond is false.
900   /// @param Optimizable Returned as true if MI is optimizable.
901   /// @returns False on success.
analyzeSelect(const MachineInstr & MI,SmallVectorImpl<MachineOperand> & Cond,unsigned & TrueOp,unsigned & FalseOp,bool & Optimizable)902   virtual bool analyzeSelect(const MachineInstr &MI,
903                              SmallVectorImpl<MachineOperand> &Cond,
904                              unsigned &TrueOp, unsigned &FalseOp,
905                              bool &Optimizable) const {
906     assert(MI.getDesc().isSelect() && "MI must be a select instruction");
907     return true;
908   }
909 
910   /// Given a select instruction that was understood by
911   /// analyzeSelect and returned Optimizable = true, attempt to optimize MI by
912   /// merging it with one of its operands. Returns NULL on failure.
913   ///
914   /// When successful, returns the new select instruction. The client is
915   /// responsible for deleting MI.
916   ///
917   /// If both sides of the select can be optimized, PreferFalse is used to pick
918   /// a side.
919   ///
920   /// @param MI          Optimizable select instruction.
921   /// @param NewMIs     Set that record all MIs in the basic block up to \p
922   /// MI. Has to be updated with any newly created MI or deleted ones.
923   /// @param PreferFalse Try to optimize FalseOp instead of TrueOp.
924   /// @returns Optimized instruction or NULL.
925   virtual MachineInstr *optimizeSelect(MachineInstr &MI,
926                                        SmallPtrSetImpl<MachineInstr *> &NewMIs,
927                                        bool PreferFalse = false) const {
928     // This function must be implemented if Optimizable is ever set.
929     llvm_unreachable("Target must implement TargetInstrInfo::optimizeSelect!");
930   }
931 
932   /// Emit instructions to copy a pair of physical registers.
933   ///
934   /// This function should support copies within any legal register class as
935   /// well as any cross-class copies created during instruction selection.
936   ///
937   /// The source and destination registers may overlap, which may require a
938   /// careful implementation when multiple copy instructions are required for
939   /// large registers. See for example the ARM target.
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,const DebugLoc & DL,MCRegister DestReg,MCRegister SrcReg,bool KillSrc)940   virtual void copyPhysReg(MachineBasicBlock &MBB,
941                            MachineBasicBlock::iterator MI, const DebugLoc &DL,
942                            MCRegister DestReg, MCRegister SrcReg,
943                            bool KillSrc) const {
944     llvm_unreachable("Target didn't implement TargetInstrInfo::copyPhysReg!");
945   }
946 
947 protected:
948   /// Target-dependent implementation for IsCopyInstr.
949   /// If the specific machine instruction is a instruction that moves/copies
950   /// value from one register to another register return destination and source
951   /// registers as machine operands.
952   virtual Optional<DestSourcePair>
isCopyInstrImpl(const MachineInstr & MI)953   isCopyInstrImpl(const MachineInstr &MI) const {
954     return None;
955   }
956 
957 public:
958   /// If the specific machine instruction is a instruction that moves/copies
959   /// value from one register to another register return destination and source
960   /// registers as machine operands.
961   /// For COPY-instruction the method naturally returns destination and source
962   /// registers as machine operands, for all other instructions the method calls
963   /// target-dependent implementation.
isCopyInstr(const MachineInstr & MI)964   Optional<DestSourcePair> isCopyInstr(const MachineInstr &MI) const {
965     if (MI.isCopy()) {
966       return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
967     }
968     return isCopyInstrImpl(MI);
969   }
970 
971   /// If the specific machine instruction is an instruction that adds an
972   /// immediate value and a physical register, and stores the result in
973   /// the given physical register \c Reg, return a pair of the source
974   /// register and the offset which has been added.
isAddImmediate(const MachineInstr & MI,Register Reg)975   virtual Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
976                                               Register Reg) const {
977     return None;
978   }
979 
980   /// Returns true if MI is an instruction that defines Reg to have a constant
981   /// value and the value is recorded in ImmVal. The ImmVal is a result that
982   /// should be interpreted as modulo size of Reg.
getConstValDefinedInReg(const MachineInstr & MI,const Register Reg,int64_t & ImmVal)983   virtual bool getConstValDefinedInReg(const MachineInstr &MI,
984                                        const Register Reg,
985                                        int64_t &ImmVal) const {
986     return false;
987   }
988 
989   /// Store the specified register of the given register class to the specified
990   /// stack frame index. The store instruction is to be added to the given
991   /// machine basic block before the specified machine instruction. If isKill
992   /// is true, the register operand is the last use and must be marked kill.
storeRegToStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,Register SrcReg,bool isKill,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI)993   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
994                                    MachineBasicBlock::iterator MI,
995                                    Register SrcReg, bool isKill, int FrameIndex,
996                                    const TargetRegisterClass *RC,
997                                    const TargetRegisterInfo *TRI) const {
998     llvm_unreachable("Target didn't implement "
999                      "TargetInstrInfo::storeRegToStackSlot!");
1000   }
1001 
1002   /// Load the specified register of the given register class from the specified
1003   /// stack frame index. The load instruction is to be added to the given
1004   /// machine basic block before the specified machine instruction.
loadRegFromStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MI,Register DestReg,int FrameIndex,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI)1005   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
1006                                     MachineBasicBlock::iterator MI,
1007                                     Register DestReg, int FrameIndex,
1008                                     const TargetRegisterClass *RC,
1009                                     const TargetRegisterInfo *TRI) const {
1010     llvm_unreachable("Target didn't implement "
1011                      "TargetInstrInfo::loadRegFromStackSlot!");
1012   }
1013 
1014   /// This function is called for all pseudo instructions
1015   /// that remain after register allocation. Many pseudo instructions are
1016   /// created to help register allocation. This is the place to convert them
1017   /// into real instructions. The target can edit MI in place, or it can insert
1018   /// new instructions and erase MI. The function should return true if
1019   /// anything was changed.
expandPostRAPseudo(MachineInstr & MI)1020   virtual bool expandPostRAPseudo(MachineInstr &MI) const { return false; }
1021 
1022   /// Check whether the target can fold a load that feeds a subreg operand
1023   /// (or a subreg operand that feeds a store).
1024   /// For example, X86 may want to return true if it can fold
1025   /// movl (%esp), %eax
1026   /// subb, %al, ...
1027   /// Into:
1028   /// subb (%esp), ...
1029   ///
1030   /// Ideally, we'd like the target implementation of foldMemoryOperand() to
1031   /// reject subregs - but since this behavior used to be enforced in the
1032   /// target-independent code, moving this responsibility to the targets
1033   /// has the potential of causing nasty silent breakage in out-of-tree targets.
isSubregFoldable()1034   virtual bool isSubregFoldable() const { return false; }
1035 
1036   /// Attempt to fold a load or store of the specified stack
1037   /// slot into the specified machine instruction for the specified operand(s).
1038   /// If this is possible, a new instruction is returned with the specified
1039   /// operand folded, otherwise NULL is returned.
1040   /// The new instruction is inserted before MI, and the client is responsible
1041   /// for removing the old instruction.
1042   /// If VRM is passed, the assigned physregs can be inspected by target to
1043   /// decide on using an opcode (note that those assignments can still change).
1044   MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
1045                                   int FI,
1046                                   LiveIntervals *LIS = nullptr,
1047                                   VirtRegMap *VRM = nullptr) const;
1048 
1049   /// Same as the previous version except it allows folding of any load and
1050   /// store from / to any address, not just from a specific stack slot.
1051   MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
1052                                   MachineInstr &LoadMI,
1053                                   LiveIntervals *LIS = nullptr) const;
1054 
1055   /// Return true when there is potentially a faster code sequence
1056   /// for an instruction chain ending in \p Root. All potential patterns are
1057   /// returned in the \p Pattern vector. Pattern should be sorted in priority
1058   /// order since the pattern evaluator stops checking as soon as it finds a
1059   /// faster sequence.
1060   /// \param Root - Instruction that could be combined with one of its operands
1061   /// \param Patterns - Vector of possible combination patterns
1062   virtual bool getMachineCombinerPatterns(
1063       MachineInstr &Root,
1064       SmallVectorImpl<MachineCombinerPattern> &Patterns) const;
1065 
1066   /// Return true when a code sequence can improve throughput. It
1067   /// should be called only for instructions in loops.
1068   /// \param Pattern - combiner pattern
1069   virtual bool isThroughputPattern(MachineCombinerPattern Pattern) const;
1070 
1071   /// Return true if the input \P Inst is part of a chain of dependent ops
1072   /// that are suitable for reassociation, otherwise return false.
1073   /// If the instruction's operands must be commuted to have a previous
1074   /// instruction of the same type define the first source operand, \P Commuted
1075   /// will be set to true.
1076   bool isReassociationCandidate(const MachineInstr &Inst, bool &Commuted) const;
1077 
1078   /// Return true when \P Inst is both associative and commutative.
isAssociativeAndCommutative(const MachineInstr & Inst)1079   virtual bool isAssociativeAndCommutative(const MachineInstr &Inst) const {
1080     return false;
1081   }
1082 
1083   /// Return true when \P Inst has reassociable operands in the same \P MBB.
1084   virtual bool hasReassociableOperands(const MachineInstr &Inst,
1085                                        const MachineBasicBlock *MBB) const;
1086 
1087   /// Return true when \P Inst has reassociable sibling.
1088   bool hasReassociableSibling(const MachineInstr &Inst, bool &Commuted) const;
1089 
1090   /// When getMachineCombinerPatterns() finds patterns, this function generates
1091   /// the instructions that could replace the original code sequence. The client
1092   /// has to decide whether the actual replacement is beneficial or not.
1093   /// \param Root - Instruction that could be combined with one of its operands
1094   /// \param Pattern - Combination pattern for Root
1095   /// \param InsInstrs - Vector of new instructions that implement P
1096   /// \param DelInstrs - Old instructions, including Root, that could be
1097   /// replaced by InsInstr
1098   /// \param InstIdxForVirtReg - map of virtual register to instruction in
1099   /// InsInstr that defines it
1100   virtual void genAlternativeCodeSequence(
1101       MachineInstr &Root, MachineCombinerPattern Pattern,
1102       SmallVectorImpl<MachineInstr *> &InsInstrs,
1103       SmallVectorImpl<MachineInstr *> &DelInstrs,
1104       DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const;
1105 
1106   /// Attempt to reassociate \P Root and \P Prev according to \P Pattern to
1107   /// reduce critical path length.
1108   void reassociateOps(MachineInstr &Root, MachineInstr &Prev,
1109                       MachineCombinerPattern Pattern,
1110                       SmallVectorImpl<MachineInstr *> &InsInstrs,
1111                       SmallVectorImpl<MachineInstr *> &DelInstrs,
1112                       DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const;
1113 
1114   /// The limit on resource length extension we accept in MachineCombiner Pass.
getExtendResourceLenLimit()1115   virtual int getExtendResourceLenLimit() const { return 0; }
1116 
1117   /// This is an architecture-specific helper function of reassociateOps.
1118   /// Set special operand attributes for new instructions after reassociation.
setSpecialOperandAttr(MachineInstr & OldMI1,MachineInstr & OldMI2,MachineInstr & NewMI1,MachineInstr & NewMI2)1119   virtual void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
1120                                      MachineInstr &NewMI1,
1121                                      MachineInstr &NewMI2) const {}
1122 
setSpecialOperandAttr(MachineInstr & MI,uint16_t Flags)1123   virtual void setSpecialOperandAttr(MachineInstr &MI, uint16_t Flags) const {}
1124 
1125   /// Return true when a target supports MachineCombiner.
useMachineCombiner()1126   virtual bool useMachineCombiner() const { return false; }
1127 
1128   /// Return true if the given SDNode can be copied during scheduling
1129   /// even if it has glue.
canCopyGluedNodeDuringSchedule(SDNode * N)1130   virtual bool canCopyGluedNodeDuringSchedule(SDNode *N) const { return false; }
1131 
1132 protected:
1133   /// Target-dependent implementation for foldMemoryOperand.
1134   /// Target-independent code in foldMemoryOperand will
1135   /// take care of adding a MachineMemOperand to the newly created instruction.
1136   /// The instruction and any auxiliary instructions necessary will be inserted
1137   /// at InsertPt.
1138   virtual MachineInstr *
1139   foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
1140                         ArrayRef<unsigned> Ops,
1141                         MachineBasicBlock::iterator InsertPt, int FrameIndex,
1142                         LiveIntervals *LIS = nullptr,
1143                         VirtRegMap *VRM = nullptr) const {
1144     return nullptr;
1145   }
1146 
1147   /// Target-dependent implementation for foldMemoryOperand.
1148   /// Target-independent code in foldMemoryOperand will
1149   /// take care of adding a MachineMemOperand to the newly created instruction.
1150   /// The instruction and any auxiliary instructions necessary will be inserted
1151   /// at InsertPt.
1152   virtual MachineInstr *foldMemoryOperandImpl(
1153       MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1154       MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
1155       LiveIntervals *LIS = nullptr) const {
1156     return nullptr;
1157   }
1158 
1159   /// Target-dependent implementation of getRegSequenceInputs.
1160   ///
1161   /// \returns true if it is possible to build the equivalent
1162   /// REG_SEQUENCE inputs with the pair \p MI, \p DefIdx. False otherwise.
1163   ///
1164   /// \pre MI.isRegSequenceLike().
1165   ///
1166   /// \see TargetInstrInfo::getRegSequenceInputs.
getRegSequenceLikeInputs(const MachineInstr & MI,unsigned DefIdx,SmallVectorImpl<RegSubRegPairAndIdx> & InputRegs)1167   virtual bool getRegSequenceLikeInputs(
1168       const MachineInstr &MI, unsigned DefIdx,
1169       SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
1170     return false;
1171   }
1172 
1173   /// Target-dependent implementation of getExtractSubregInputs.
1174   ///
1175   /// \returns true if it is possible to build the equivalent
1176   /// EXTRACT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
1177   ///
1178   /// \pre MI.isExtractSubregLike().
1179   ///
1180   /// \see TargetInstrInfo::getExtractSubregInputs.
getExtractSubregLikeInputs(const MachineInstr & MI,unsigned DefIdx,RegSubRegPairAndIdx & InputReg)1181   virtual bool getExtractSubregLikeInputs(const MachineInstr &MI,
1182                                           unsigned DefIdx,
1183                                           RegSubRegPairAndIdx &InputReg) const {
1184     return false;
1185   }
1186 
1187   /// Target-dependent implementation of getInsertSubregInputs.
1188   ///
1189   /// \returns true if it is possible to build the equivalent
1190   /// INSERT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
1191   ///
1192   /// \pre MI.isInsertSubregLike().
1193   ///
1194   /// \see TargetInstrInfo::getInsertSubregInputs.
1195   virtual bool
getInsertSubregLikeInputs(const MachineInstr & MI,unsigned DefIdx,RegSubRegPair & BaseReg,RegSubRegPairAndIdx & InsertedReg)1196   getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
1197                             RegSubRegPair &BaseReg,
1198                             RegSubRegPairAndIdx &InsertedReg) const {
1199     return false;
1200   }
1201 
1202 public:
1203   /// getAddressSpaceForPseudoSourceKind - Given the kind of memory
1204   /// (e.g. stack) the target returns the corresponding address space.
1205   virtual unsigned
getAddressSpaceForPseudoSourceKind(unsigned Kind)1206   getAddressSpaceForPseudoSourceKind(unsigned Kind) const {
1207     return 0;
1208   }
1209 
1210   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
1211   /// a store or a load and a store into two or more instruction. If this is
1212   /// possible, returns true as well as the new instructions by reference.
1213   virtual bool
unfoldMemoryOperand(MachineFunction & MF,MachineInstr & MI,unsigned Reg,bool UnfoldLoad,bool UnfoldStore,SmallVectorImpl<MachineInstr * > & NewMIs)1214   unfoldMemoryOperand(MachineFunction &MF, MachineInstr &MI, unsigned Reg,
1215                       bool UnfoldLoad, bool UnfoldStore,
1216                       SmallVectorImpl<MachineInstr *> &NewMIs) const {
1217     return false;
1218   }
1219 
unfoldMemoryOperand(SelectionDAG & DAG,SDNode * N,SmallVectorImpl<SDNode * > & NewNodes)1220   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
1221                                    SmallVectorImpl<SDNode *> &NewNodes) const {
1222     return false;
1223   }
1224 
1225   /// Returns the opcode of the would be new
1226   /// instruction after load / store are unfolded from an instruction of the
1227   /// specified opcode. It returns zero if the specified unfolding is not
1228   /// possible. If LoadRegIndex is non-null, it is filled in with the operand
1229   /// index of the operand which will hold the register holding the loaded
1230   /// value.
1231   virtual unsigned
1232   getOpcodeAfterMemoryUnfold(unsigned Opc, bool UnfoldLoad, bool UnfoldStore,
1233                              unsigned *LoadRegIndex = nullptr) const {
1234     return 0;
1235   }
1236 
1237   /// This is used by the pre-regalloc scheduler to determine if two loads are
1238   /// loading from the same base address. It should only return true if the base
1239   /// pointers are the same and the only differences between the two addresses
1240   /// are the offset. It also returns the offsets by reference.
areLoadsFromSameBasePtr(SDNode * Load1,SDNode * Load2,int64_t & Offset1,int64_t & Offset2)1241   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1242                                        int64_t &Offset1,
1243                                        int64_t &Offset2) const {
1244     return false;
1245   }
1246 
1247   /// This is a used by the pre-regalloc scheduler to determine (in conjunction
1248   /// with areLoadsFromSameBasePtr) if two loads should be scheduled together.
1249   /// On some targets if two loads are loading from
1250   /// addresses in the same cache line, it's better if they are scheduled
1251   /// together. This function takes two integers that represent the load offsets
1252   /// from the common base address. It returns true if it decides it's desirable
1253   /// to schedule the two loads together. "NumLoads" is the number of loads that
1254   /// have already been scheduled after Load1.
shouldScheduleLoadsNear(SDNode * Load1,SDNode * Load2,int64_t Offset1,int64_t Offset2,unsigned NumLoads)1255   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1256                                        int64_t Offset1, int64_t Offset2,
1257                                        unsigned NumLoads) const {
1258     return false;
1259   }
1260 
1261   /// Get the base operand and byte offset of an instruction that reads/writes
1262   /// memory. This is a convenience function for callers that are only prepared
1263   /// to handle a single base operand.
1264   bool getMemOperandWithOffset(const MachineInstr &MI,
1265                                const MachineOperand *&BaseOp, int64_t &Offset,
1266                                bool &OffsetIsScalable,
1267                                const TargetRegisterInfo *TRI) const;
1268 
1269   /// Get the base operands and byte offset of an instruction that reads/writes
1270   /// memory.
1271   /// It returns false if MI does not read/write memory.
1272   /// It returns false if no base operands and offset was found.
1273   /// It is not guaranteed to always recognize base operands and offsets in all
1274   /// cases.
getMemOperandsWithOffsetWidth(const MachineInstr & MI,SmallVectorImpl<const MachineOperand * > & BaseOps,int64_t & Offset,bool & OffsetIsScalable,unsigned & Width,const TargetRegisterInfo * TRI)1275   virtual bool getMemOperandsWithOffsetWidth(
1276       const MachineInstr &MI, SmallVectorImpl<const MachineOperand *> &BaseOps,
1277       int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
1278       const TargetRegisterInfo *TRI) const {
1279     return false;
1280   }
1281 
1282   /// Return true if the instruction contains a base register and offset. If
1283   /// true, the function also sets the operand position in the instruction
1284   /// for the base register and offset.
getBaseAndOffsetPosition(const MachineInstr & MI,unsigned & BasePos,unsigned & OffsetPos)1285   virtual bool getBaseAndOffsetPosition(const MachineInstr &MI,
1286                                         unsigned &BasePos,
1287                                         unsigned &OffsetPos) const {
1288     return false;
1289   }
1290 
1291   /// Target dependent implementation to get the values constituting the address
1292   /// MachineInstr that is accessing memory. These values are returned as a
1293   /// struct ExtAddrMode which contains all relevant information to make up the
1294   /// address.
1295   virtual Optional<ExtAddrMode>
getAddrModeFromMemoryOp(const MachineInstr & MemI,const TargetRegisterInfo * TRI)1296   getAddrModeFromMemoryOp(const MachineInstr &MemI,
1297                           const TargetRegisterInfo *TRI) const {
1298     return None;
1299   }
1300 
1301   /// Returns true if MI's Def is NullValueReg, and the MI
1302   /// does not change the Zero value. i.e. cases such as rax = shr rax, X where
1303   /// NullValueReg = rax. Note that if the NullValueReg is non-zero, this
1304   /// function can return true even if becomes zero. Specifically cases such as
1305   /// NullValueReg = shl NullValueReg, 63.
preservesZeroValueInReg(const MachineInstr * MI,const Register NullValueReg,const TargetRegisterInfo * TRI)1306   virtual bool preservesZeroValueInReg(const MachineInstr *MI,
1307                                        const Register NullValueReg,
1308                                        const TargetRegisterInfo *TRI) const {
1309     return false;
1310   }
1311 
1312   /// If the instruction is an increment of a constant value, return the amount.
getIncrementValue(const MachineInstr & MI,int & Value)1313   virtual bool getIncrementValue(const MachineInstr &MI, int &Value) const {
1314     return false;
1315   }
1316 
1317   /// Returns true if the two given memory operations should be scheduled
1318   /// adjacent. Note that you have to add:
1319   ///   DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
1320   /// or
1321   ///   DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
1322   /// to TargetPassConfig::createMachineScheduler() to have an effect.
1323   ///
1324   /// \p BaseOps1 and \p BaseOps2 are memory operands of two memory operations.
1325   /// \p NumLoads is the number of loads that will be in the cluster if this
1326   /// hook returns true.
1327   /// \p NumBytes is the number of bytes that will be loaded from all the
1328   /// clustered loads if this hook returns true.
shouldClusterMemOps(ArrayRef<const MachineOperand * > BaseOps1,ArrayRef<const MachineOperand * > BaseOps2,unsigned NumLoads,unsigned NumBytes)1329   virtual bool shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1,
1330                                    ArrayRef<const MachineOperand *> BaseOps2,
1331                                    unsigned NumLoads, unsigned NumBytes) const {
1332     llvm_unreachable("target did not implement shouldClusterMemOps()");
1333   }
1334 
1335   /// Reverses the branch condition of the specified condition list,
1336   /// returning false on success and true if it cannot be reversed.
1337   virtual bool
reverseBranchCondition(SmallVectorImpl<MachineOperand> & Cond)1338   reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
1339     return true;
1340   }
1341 
1342   /// Insert a noop into the instruction stream at the specified point.
1343   virtual void insertNoop(MachineBasicBlock &MBB,
1344                           MachineBasicBlock::iterator MI) const;
1345 
1346   /// Insert noops into the instruction stream at the specified point.
1347   virtual void insertNoops(MachineBasicBlock &MBB,
1348                            MachineBasicBlock::iterator MI,
1349                            unsigned Quantity) const;
1350 
1351   /// Return the noop instruction to use for a noop.
1352   virtual void getNoop(MCInst &NopInst) const;
1353 
1354   /// Return true for post-incremented instructions.
isPostIncrement(const MachineInstr & MI)1355   virtual bool isPostIncrement(const MachineInstr &MI) const { return false; }
1356 
1357   /// Returns true if the instruction is already predicated.
isPredicated(const MachineInstr & MI)1358   virtual bool isPredicated(const MachineInstr &MI) const { return false; }
1359 
1360   // Returns a MIRPrinter comment for this machine operand.
1361   virtual std::string
1362   createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op,
1363                           unsigned OpIdx, const TargetRegisterInfo *TRI) const;
1364 
1365   /// Returns true if the instruction is a
1366   /// terminator instruction that has not been predicated.
1367   bool isUnpredicatedTerminator(const MachineInstr &MI) const;
1368 
1369   /// Returns true if MI is an unconditional tail call.
isUnconditionalTailCall(const MachineInstr & MI)1370   virtual bool isUnconditionalTailCall(const MachineInstr &MI) const {
1371     return false;
1372   }
1373 
1374   /// Returns true if the tail call can be made conditional on BranchCond.
canMakeTailCallConditional(SmallVectorImpl<MachineOperand> & Cond,const MachineInstr & TailCall)1375   virtual bool canMakeTailCallConditional(SmallVectorImpl<MachineOperand> &Cond,
1376                                           const MachineInstr &TailCall) const {
1377     return false;
1378   }
1379 
1380   /// Replace the conditional branch in MBB with a conditional tail call.
replaceBranchWithTailCall(MachineBasicBlock & MBB,SmallVectorImpl<MachineOperand> & Cond,const MachineInstr & TailCall)1381   virtual void replaceBranchWithTailCall(MachineBasicBlock &MBB,
1382                                          SmallVectorImpl<MachineOperand> &Cond,
1383                                          const MachineInstr &TailCall) const {
1384     llvm_unreachable("Target didn't implement replaceBranchWithTailCall!");
1385   }
1386 
1387   /// Convert the instruction into a predicated instruction.
1388   /// It returns true if the operation was successful.
1389   virtual bool PredicateInstruction(MachineInstr &MI,
1390                                     ArrayRef<MachineOperand> Pred) const;
1391 
1392   /// Returns true if the first specified predicate
1393   /// subsumes the second, e.g. GE subsumes GT.
SubsumesPredicate(ArrayRef<MachineOperand> Pred1,ArrayRef<MachineOperand> Pred2)1394   virtual bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
1395                                  ArrayRef<MachineOperand> Pred2) const {
1396     return false;
1397   }
1398 
1399   /// If the specified instruction defines any predicate
1400   /// or condition code register(s) used for predication, returns true as well
1401   /// as the definition predicate(s) by reference.
1402   /// SkipDead should be set to false at any point that dead
1403   /// predicate instructions should be considered as being defined.
1404   /// A dead predicate instruction is one that is guaranteed to be removed
1405   /// after a call to PredicateInstruction.
ClobbersPredicate(MachineInstr & MI,std::vector<MachineOperand> & Pred,bool SkipDead)1406   virtual bool ClobbersPredicate(MachineInstr &MI,
1407                                  std::vector<MachineOperand> &Pred,
1408                                  bool SkipDead) const {
1409     return false;
1410   }
1411 
1412   /// Return true if the specified instruction can be predicated.
1413   /// By default, this returns true for every instruction with a
1414   /// PredicateOperand.
isPredicable(const MachineInstr & MI)1415   virtual bool isPredicable(const MachineInstr &MI) const {
1416     return MI.getDesc().isPredicable();
1417   }
1418 
1419   /// Return true if it's safe to move a machine
1420   /// instruction that defines the specified register class.
isSafeToMoveRegClassDefs(const TargetRegisterClass * RC)1421   virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
1422     return true;
1423   }
1424 
1425   /// Test if the given instruction should be considered a scheduling boundary.
1426   /// This primarily includes labels and terminators.
1427   virtual bool isSchedulingBoundary(const MachineInstr &MI,
1428                                     const MachineBasicBlock *MBB,
1429                                     const MachineFunction &MF) const;
1430 
1431   /// Measure the specified inline asm to determine an approximation of its
1432   /// length.
1433   virtual unsigned getInlineAsmLength(
1434     const char *Str, const MCAsmInfo &MAI,
1435     const TargetSubtargetInfo *STI = nullptr) const;
1436 
1437   /// Allocate and return a hazard recognizer to use for this target when
1438   /// scheduling the machine instructions before register allocation.
1439   virtual ScheduleHazardRecognizer *
1440   CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
1441                                const ScheduleDAG *DAG) const;
1442 
1443   /// Allocate and return a hazard recognizer to use for this target when
1444   /// scheduling the machine instructions before register allocation.
1445   virtual ScheduleHazardRecognizer *
1446   CreateTargetMIHazardRecognizer(const InstrItineraryData *,
1447                                  const ScheduleDAGMI *DAG) const;
1448 
1449   /// Allocate and return a hazard recognizer to use for this target when
1450   /// scheduling the machine instructions after register allocation.
1451   virtual ScheduleHazardRecognizer *
1452   CreateTargetPostRAHazardRecognizer(const InstrItineraryData *,
1453                                      const ScheduleDAG *DAG) const;
1454 
1455   /// Allocate and return a hazard recognizer to use for by non-scheduling
1456   /// passes.
1457   virtual ScheduleHazardRecognizer *
CreateTargetPostRAHazardRecognizer(const MachineFunction & MF)1458   CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const {
1459     return nullptr;
1460   }
1461 
1462   /// Provide a global flag for disabling the PreRA hazard recognizer that
1463   /// targets may choose to honor.
1464   bool usePreRAHazardRecognizer() const;
1465 
1466   /// For a comparison instruction, return the source registers
1467   /// in SrcReg and SrcReg2 if having two register operands, and the value it
1468   /// compares against in CmpValue. Return true if the comparison instruction
1469   /// can be analyzed.
analyzeCompare(const MachineInstr & MI,Register & SrcReg,Register & SrcReg2,int & Mask,int & Value)1470   virtual bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
1471                               Register &SrcReg2, int &Mask, int &Value) const {
1472     return false;
1473   }
1474 
1475   /// See if the comparison instruction can be converted
1476   /// into something more efficient. E.g., on ARM most instructions can set the
1477   /// flags register, obviating the need for a separate CMP.
optimizeCompareInstr(MachineInstr & CmpInstr,Register SrcReg,Register SrcReg2,int Mask,int Value,const MachineRegisterInfo * MRI)1478   virtual bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
1479                                     Register SrcReg2, int Mask, int Value,
1480                                     const MachineRegisterInfo *MRI) const {
1481     return false;
1482   }
optimizeCondBranch(MachineInstr & MI)1483   virtual bool optimizeCondBranch(MachineInstr &MI) const { return false; }
1484 
1485   /// Try to remove the load by folding it to a register operand at the use.
1486   /// We fold the load instructions if and only if the
1487   /// def and use are in the same BB. We only look at one load and see
1488   /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
1489   /// defined by the load we are trying to fold. DefMI returns the machine
1490   /// instruction that defines FoldAsLoadDefReg, and the function returns
1491   /// the machine instruction generated due to folding.
optimizeLoadInstr(MachineInstr & MI,const MachineRegisterInfo * MRI,Register & FoldAsLoadDefReg,MachineInstr * & DefMI)1492   virtual MachineInstr *optimizeLoadInstr(MachineInstr &MI,
1493                                           const MachineRegisterInfo *MRI,
1494                                           Register &FoldAsLoadDefReg,
1495                                           MachineInstr *&DefMI) const {
1496     return nullptr;
1497   }
1498 
1499   /// 'Reg' is known to be defined by a move immediate instruction,
1500   /// try to fold the immediate into the use instruction.
1501   /// If MRI->hasOneNonDBGUse(Reg) is true, and this function returns true,
1502   /// then the caller may assume that DefMI has been erased from its parent
1503   /// block. The caller may assume that it will not be erased by this
1504   /// function otherwise.
FoldImmediate(MachineInstr & UseMI,MachineInstr & DefMI,Register Reg,MachineRegisterInfo * MRI)1505   virtual bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
1506                              Register Reg, MachineRegisterInfo *MRI) const {
1507     return false;
1508   }
1509 
1510   /// Return the number of u-operations the given machine
1511   /// instruction will be decoded to on the target cpu. The itinerary's
1512   /// IssueWidth is the number of microops that can be dispatched each
1513   /// cycle. An instruction with zero microops takes no dispatch resources.
1514   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
1515                                   const MachineInstr &MI) const;
1516 
1517   /// Return true for pseudo instructions that don't consume any
1518   /// machine resources in their current form. These are common cases that the
1519   /// scheduler should consider free, rather than conservatively handling them
1520   /// as instructions with no itinerary.
isZeroCost(unsigned Opcode)1521   bool isZeroCost(unsigned Opcode) const {
1522     return Opcode <= TargetOpcode::COPY;
1523   }
1524 
1525   virtual int getOperandLatency(const InstrItineraryData *ItinData,
1526                                 SDNode *DefNode, unsigned DefIdx,
1527                                 SDNode *UseNode, unsigned UseIdx) const;
1528 
1529   /// Compute and return the use operand latency of a given pair of def and use.
1530   /// In most cases, the static scheduling itinerary was enough to determine the
1531   /// operand latency. But it may not be possible for instructions with variable
1532   /// number of defs / uses.
1533   ///
1534   /// This is a raw interface to the itinerary that may be directly overridden
1535   /// by a target. Use computeOperandLatency to get the best estimate of
1536   /// latency.
1537   virtual int getOperandLatency(const InstrItineraryData *ItinData,
1538                                 const MachineInstr &DefMI, unsigned DefIdx,
1539                                 const MachineInstr &UseMI,
1540                                 unsigned UseIdx) const;
1541 
1542   /// Compute the instruction latency of a given instruction.
1543   /// If the instruction has higher cost when predicated, it's returned via
1544   /// PredCost.
1545   virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
1546                                    const MachineInstr &MI,
1547                                    unsigned *PredCost = nullptr) const;
1548 
1549   virtual unsigned getPredicationCost(const MachineInstr &MI) const;
1550 
1551   virtual int getInstrLatency(const InstrItineraryData *ItinData,
1552                               SDNode *Node) const;
1553 
1554   /// Return the default expected latency for a def based on its opcode.
1555   unsigned defaultDefLatency(const MCSchedModel &SchedModel,
1556                              const MachineInstr &DefMI) const;
1557 
1558   int computeDefOperandLatency(const InstrItineraryData *ItinData,
1559                                const MachineInstr &DefMI) const;
1560 
1561   /// Return true if this opcode has high latency to its result.
isHighLatencyDef(int opc)1562   virtual bool isHighLatencyDef(int opc) const { return false; }
1563 
1564   /// Compute operand latency between a def of 'Reg'
1565   /// and a use in the current loop. Return true if the target considered
1566   /// it 'high'. This is used by optimization passes such as machine LICM to
1567   /// determine whether it makes sense to hoist an instruction out even in a
1568   /// high register pressure situation.
hasHighOperandLatency(const TargetSchedModel & SchedModel,const MachineRegisterInfo * MRI,const MachineInstr & DefMI,unsigned DefIdx,const MachineInstr & UseMI,unsigned UseIdx)1569   virtual bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
1570                                      const MachineRegisterInfo *MRI,
1571                                      const MachineInstr &DefMI, unsigned DefIdx,
1572                                      const MachineInstr &UseMI,
1573                                      unsigned UseIdx) const {
1574     return false;
1575   }
1576 
1577   /// Compute operand latency of a def of 'Reg'. Return true
1578   /// if the target considered it 'low'.
1579   virtual bool hasLowDefLatency(const TargetSchedModel &SchedModel,
1580                                 const MachineInstr &DefMI,
1581                                 unsigned DefIdx) const;
1582 
1583   /// Perform target-specific instruction verification.
verifyInstruction(const MachineInstr & MI,StringRef & ErrInfo)1584   virtual bool verifyInstruction(const MachineInstr &MI,
1585                                  StringRef &ErrInfo) const {
1586     return true;
1587   }
1588 
1589   /// Return the current execution domain and bit mask of
1590   /// possible domains for instruction.
1591   ///
1592   /// Some micro-architectures have multiple execution domains, and multiple
1593   /// opcodes that perform the same operation in different domains.  For
1594   /// example, the x86 architecture provides the por, orps, and orpd
1595   /// instructions that all do the same thing.  There is a latency penalty if a
1596   /// register is written in one domain and read in another.
1597   ///
1598   /// This function returns a pair (domain, mask) containing the execution
1599   /// domain of MI, and a bit mask of possible domains.  The setExecutionDomain
1600   /// function can be used to change the opcode to one of the domains in the
1601   /// bit mask.  Instructions whose execution domain can't be changed should
1602   /// return a 0 mask.
1603   ///
1604   /// The execution domain numbers don't have any special meaning except domain
1605   /// 0 is used for instructions that are not associated with any interesting
1606   /// execution domain.
1607   ///
1608   virtual std::pair<uint16_t, uint16_t>
getExecutionDomain(const MachineInstr & MI)1609   getExecutionDomain(const MachineInstr &MI) const {
1610     return std::make_pair(0, 0);
1611   }
1612 
1613   /// Change the opcode of MI to execute in Domain.
1614   ///
1615   /// The bit (1 << Domain) must be set in the mask returned from
1616   /// getExecutionDomain(MI).
setExecutionDomain(MachineInstr & MI,unsigned Domain)1617   virtual void setExecutionDomain(MachineInstr &MI, unsigned Domain) const {}
1618 
1619   /// Returns the preferred minimum clearance
1620   /// before an instruction with an unwanted partial register update.
1621   ///
1622   /// Some instructions only write part of a register, and implicitly need to
1623   /// read the other parts of the register.  This may cause unwanted stalls
1624   /// preventing otherwise unrelated instructions from executing in parallel in
1625   /// an out-of-order CPU.
1626   ///
1627   /// For example, the x86 instruction cvtsi2ss writes its result to bits
1628   /// [31:0] of the destination xmm register. Bits [127:32] are unaffected, so
1629   /// the instruction needs to wait for the old value of the register to become
1630   /// available:
1631   ///
1632   ///   addps %xmm1, %xmm0
1633   ///   movaps %xmm0, (%rax)
1634   ///   cvtsi2ss %rbx, %xmm0
1635   ///
1636   /// In the code above, the cvtsi2ss instruction needs to wait for the addps
1637   /// instruction before it can issue, even though the high bits of %xmm0
1638   /// probably aren't needed.
1639   ///
1640   /// This hook returns the preferred clearance before MI, measured in
1641   /// instructions.  Other defs of MI's operand OpNum are avoided in the last N
1642   /// instructions before MI.  It should only return a positive value for
1643   /// unwanted dependencies.  If the old bits of the defined register have
1644   /// useful values, or if MI is determined to otherwise read the dependency,
1645   /// the hook should return 0.
1646   ///
1647   /// The unwanted dependency may be handled by:
1648   ///
1649   /// 1. Allocating the same register for an MI def and use.  That makes the
1650   ///    unwanted dependency identical to a required dependency.
1651   ///
1652   /// 2. Allocating a register for the def that has no defs in the previous N
1653   ///    instructions.
1654   ///
1655   /// 3. Calling breakPartialRegDependency() with the same arguments.  This
1656   ///    allows the target to insert a dependency breaking instruction.
1657   ///
1658   virtual unsigned
getPartialRegUpdateClearance(const MachineInstr & MI,unsigned OpNum,const TargetRegisterInfo * TRI)1659   getPartialRegUpdateClearance(const MachineInstr &MI, unsigned OpNum,
1660                                const TargetRegisterInfo *TRI) const {
1661     // The default implementation returns 0 for no partial register dependency.
1662     return 0;
1663   }
1664 
1665   /// Return the minimum clearance before an instruction that reads an
1666   /// unused register.
1667   ///
1668   /// For example, AVX instructions may copy part of a register operand into
1669   /// the unused high bits of the destination register.
1670   ///
1671   /// vcvtsi2sdq %rax, undef %xmm0, %xmm14
1672   ///
1673   /// In the code above, vcvtsi2sdq copies %xmm0[127:64] into %xmm14 creating a
1674   /// false dependence on any previous write to %xmm0.
1675   ///
1676   /// This hook works similarly to getPartialRegUpdateClearance, except that it
1677   /// does not take an operand index. Instead sets \p OpNum to the index of the
1678   /// unused register.
getUndefRegClearance(const MachineInstr & MI,unsigned OpNum,const TargetRegisterInfo * TRI)1679   virtual unsigned getUndefRegClearance(const MachineInstr &MI, unsigned OpNum,
1680                                         const TargetRegisterInfo *TRI) const {
1681     // The default implementation returns 0 for no undef register dependency.
1682     return 0;
1683   }
1684 
1685   /// Insert a dependency-breaking instruction
1686   /// before MI to eliminate an unwanted dependency on OpNum.
1687   ///
1688   /// If it wasn't possible to avoid a def in the last N instructions before MI
1689   /// (see getPartialRegUpdateClearance), this hook will be called to break the
1690   /// unwanted dependency.
1691   ///
1692   /// On x86, an xorps instruction can be used as a dependency breaker:
1693   ///
1694   ///   addps %xmm1, %xmm0
1695   ///   movaps %xmm0, (%rax)
1696   ///   xorps %xmm0, %xmm0
1697   ///   cvtsi2ss %rbx, %xmm0
1698   ///
1699   /// An <imp-kill> operand should be added to MI if an instruction was
1700   /// inserted.  This ties the instructions together in the post-ra scheduler.
1701   ///
breakPartialRegDependency(MachineInstr & MI,unsigned OpNum,const TargetRegisterInfo * TRI)1702   virtual void breakPartialRegDependency(MachineInstr &MI, unsigned OpNum,
1703                                          const TargetRegisterInfo *TRI) const {}
1704 
1705   /// Create machine specific model for scheduling.
1706   virtual DFAPacketizer *
CreateTargetScheduleState(const TargetSubtargetInfo &)1707   CreateTargetScheduleState(const TargetSubtargetInfo &) const {
1708     return nullptr;
1709   }
1710 
1711   /// Sometimes, it is possible for the target
1712   /// to tell, even without aliasing information, that two MIs access different
1713   /// memory addresses. This function returns true if two MIs access different
1714   /// memory addresses and false otherwise.
1715   ///
1716   /// Assumes any physical registers used to compute addresses have the same
1717   /// value for both instructions. (This is the most useful assumption for
1718   /// post-RA scheduling.)
1719   ///
1720   /// See also MachineInstr::mayAlias, which is implemented on top of this
1721   /// function.
1722   virtual bool
areMemAccessesTriviallyDisjoint(const MachineInstr & MIa,const MachineInstr & MIb)1723   areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
1724                                   const MachineInstr &MIb) const {
1725     assert(MIa.mayLoadOrStore() &&
1726            "MIa must load from or modify a memory location");
1727     assert(MIb.mayLoadOrStore() &&
1728            "MIb must load from or modify a memory location");
1729     return false;
1730   }
1731 
1732   /// Return the value to use for the MachineCSE's LookAheadLimit,
1733   /// which is a heuristic used for CSE'ing phys reg defs.
getMachineCSELookAheadLimit()1734   virtual unsigned getMachineCSELookAheadLimit() const {
1735     // The default lookahead is small to prevent unprofitable quadratic
1736     // behavior.
1737     return 5;
1738   }
1739 
1740   /// Return the maximal number of alias checks on memory operands. For
1741   /// instructions with more than one memory operands, the alias check on a
1742   /// single MachineInstr pair has quadratic overhead and results in
1743   /// unacceptable performance in the worst case. The limit here is to clamp
1744   /// that maximal checks performed. Usually, that's the product of memory
1745   /// operand numbers from that pair of MachineInstr to be checked. For
1746   /// instance, with two MachineInstrs with 4 and 5 memory operands
1747   /// correspondingly, a total of 20 checks are required. With this limit set to
1748   /// 16, their alias check is skipped. We choose to limit the product instead
1749   /// of the individual instruction as targets may have special MachineInstrs
1750   /// with a considerably high number of memory operands, such as `ldm` in ARM.
1751   /// Setting this limit per MachineInstr would result in either too high
1752   /// overhead or too rigid restriction.
getMemOperandAACheckLimit()1753   virtual unsigned getMemOperandAACheckLimit() const { return 16; }
1754 
1755   /// Return an array that contains the ids of the target indices (used for the
1756   /// TargetIndex machine operand) and their names.
1757   ///
1758   /// MIR Serialization is able to serialize only the target indices that are
1759   /// defined by this method.
1760   virtual ArrayRef<std::pair<int, const char *>>
getSerializableTargetIndices()1761   getSerializableTargetIndices() const {
1762     return None;
1763   }
1764 
1765   /// Decompose the machine operand's target flags into two values - the direct
1766   /// target flag value and any of bit flags that are applied.
1767   virtual std::pair<unsigned, unsigned>
decomposeMachineOperandsTargetFlags(unsigned)1768   decomposeMachineOperandsTargetFlags(unsigned /*TF*/) const {
1769     return std::make_pair(0u, 0u);
1770   }
1771 
1772   /// Return an array that contains the direct target flag values and their
1773   /// names.
1774   ///
1775   /// MIR Serialization is able to serialize only the target flags that are
1776   /// defined by this method.
1777   virtual ArrayRef<std::pair<unsigned, const char *>>
getSerializableDirectMachineOperandTargetFlags()1778   getSerializableDirectMachineOperandTargetFlags() const {
1779     return None;
1780   }
1781 
1782   /// Return an array that contains the bitmask target flag values and their
1783   /// names.
1784   ///
1785   /// MIR Serialization is able to serialize only the target flags that are
1786   /// defined by this method.
1787   virtual ArrayRef<std::pair<unsigned, const char *>>
getSerializableBitmaskMachineOperandTargetFlags()1788   getSerializableBitmaskMachineOperandTargetFlags() const {
1789     return None;
1790   }
1791 
1792   /// Return an array that contains the MMO target flag values and their
1793   /// names.
1794   ///
1795   /// MIR Serialization is able to serialize only the MMO target flags that are
1796   /// defined by this method.
1797   virtual ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
getSerializableMachineMemOperandTargetFlags()1798   getSerializableMachineMemOperandTargetFlags() const {
1799     return None;
1800   }
1801 
1802   /// Determines whether \p Inst is a tail call instruction. Override this
1803   /// method on targets that do not properly set MCID::Return and MCID::Call on
1804   /// tail call instructions."
isTailCall(const MachineInstr & Inst)1805   virtual bool isTailCall(const MachineInstr &Inst) const {
1806     return Inst.isReturn() && Inst.isCall();
1807   }
1808 
1809   /// True if the instruction is bound to the top of its basic block and no
1810   /// other instructions shall be inserted before it. This can be implemented
1811   /// to prevent register allocator to insert spills before such instructions.
isBasicBlockPrologue(const MachineInstr & MI)1812   virtual bool isBasicBlockPrologue(const MachineInstr &MI) const {
1813     return false;
1814   }
1815 
1816   /// During PHI eleimination lets target to make necessary checks and
1817   /// insert the copy to the PHI destination register in a target specific
1818   /// manner.
createPHIDestinationCopy(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsPt,const DebugLoc & DL,Register Src,Register Dst)1819   virtual MachineInstr *createPHIDestinationCopy(
1820       MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt,
1821       const DebugLoc &DL, Register Src, Register Dst) const {
1822     return BuildMI(MBB, InsPt, DL, get(TargetOpcode::COPY), Dst)
1823         .addReg(Src);
1824   }
1825 
1826   /// During PHI eleimination lets target to make necessary checks and
1827   /// insert the copy to the PHI destination register in a target specific
1828   /// manner.
createPHISourceCopy(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsPt,const DebugLoc & DL,Register Src,unsigned SrcSubReg,Register Dst)1829   virtual MachineInstr *createPHISourceCopy(MachineBasicBlock &MBB,
1830                                             MachineBasicBlock::iterator InsPt,
1831                                             const DebugLoc &DL, Register Src,
1832                                             unsigned SrcSubReg,
1833                                             Register Dst) const {
1834     return BuildMI(MBB, InsPt, DL, get(TargetOpcode::COPY), Dst)
1835         .addReg(Src, 0, SrcSubReg);
1836   }
1837 
1838   /// Returns a \p outliner::OutlinedFunction struct containing target-specific
1839   /// information for a set of outlining candidates.
getOutliningCandidateInfo(std::vector<outliner::Candidate> & RepeatedSequenceLocs)1840   virtual outliner::OutlinedFunction getOutliningCandidateInfo(
1841       std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
1842     llvm_unreachable(
1843         "Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
1844   }
1845 
1846   /// Returns how or if \p MI should be outlined.
1847   virtual outliner::InstrType
getOutliningType(MachineBasicBlock::iterator & MIT,unsigned Flags)1848   getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const {
1849     llvm_unreachable(
1850         "Target didn't implement TargetInstrInfo::getOutliningType!");
1851   }
1852 
1853   /// Optional target hook that returns true if \p MBB is safe to outline from,
1854   /// and returns any target-specific information in \p Flags.
isMBBSafeToOutlineFrom(MachineBasicBlock & MBB,unsigned & Flags)1855   virtual bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
1856                                       unsigned &Flags) const {
1857     return true;
1858   }
1859 
1860   /// Insert a custom frame for outlined functions.
buildOutlinedFrame(MachineBasicBlock & MBB,MachineFunction & MF,const outliner::OutlinedFunction & OF)1861   virtual void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
1862                                   const outliner::OutlinedFunction &OF) const {
1863     llvm_unreachable(
1864         "Target didn't implement TargetInstrInfo::buildOutlinedFrame!");
1865   }
1866 
1867   /// Insert a call to an outlined function into the program.
1868   /// Returns an iterator to the spot where we inserted the call. This must be
1869   /// implemented by the target.
1870   virtual MachineBasicBlock::iterator
insertOutlinedCall(Module & M,MachineBasicBlock & MBB,MachineBasicBlock::iterator & It,MachineFunction & MF,const outliner::Candidate & C)1871   insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
1872                      MachineBasicBlock::iterator &It, MachineFunction &MF,
1873                      const outliner::Candidate &C) const {
1874     llvm_unreachable(
1875         "Target didn't implement TargetInstrInfo::insertOutlinedCall!");
1876   }
1877 
1878   /// Return true if the function can safely be outlined from.
1879   /// A function \p MF is considered safe for outlining if an outlined function
1880   /// produced from instructions in F will produce a program which produces the
1881   /// same output for any set of given inputs.
isFunctionSafeToOutlineFrom(MachineFunction & MF,bool OutlineFromLinkOnceODRs)1882   virtual bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
1883                                            bool OutlineFromLinkOnceODRs) const {
1884     llvm_unreachable("Target didn't implement "
1885                      "TargetInstrInfo::isFunctionSafeToOutlineFrom!");
1886   }
1887 
1888   /// Return true if the function should be outlined from by default.
shouldOutlineFromFunctionByDefault(MachineFunction & MF)1889   virtual bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const {
1890     return false;
1891   }
1892 
1893   /// Produce the expression describing the \p MI loading a value into
1894   /// the physical register \p Reg. This hook should only be used with
1895   /// \p MIs belonging to VReg-less functions.
1896   virtual Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
1897                                                          Register Reg) const;
1898 
1899   /// Return MIR formatter to format/parse MIR operands.  Target can override
1900   /// this virtual function and return target specific MIR formatter.
getMIRFormatter()1901   virtual const MIRFormatter *getMIRFormatter() const {
1902     if (!Formatter.get())
1903       Formatter = std::make_unique<MIRFormatter>();
1904     return Formatter.get();
1905   }
1906 
1907 private:
1908   mutable std::unique_ptr<MIRFormatter> Formatter;
1909   unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;
1910   unsigned CatchRetOpcode;
1911   unsigned ReturnOpcode;
1912 };
1913 
1914 /// Provide DenseMapInfo for TargetInstrInfo::RegSubRegPair.
1915 template <> struct DenseMapInfo<TargetInstrInfo::RegSubRegPair> {
1916   using RegInfo = DenseMapInfo<unsigned>;
1917 
1918   static inline TargetInstrInfo::RegSubRegPair getEmptyKey() {
1919     return TargetInstrInfo::RegSubRegPair(RegInfo::getEmptyKey(),
1920                                           RegInfo::getEmptyKey());
1921   }
1922 
1923   static inline TargetInstrInfo::RegSubRegPair getTombstoneKey() {
1924     return TargetInstrInfo::RegSubRegPair(RegInfo::getTombstoneKey(),
1925                                           RegInfo::getTombstoneKey());
1926   }
1927 
1928   /// Reuse getHashValue implementation from
1929   /// std::pair<unsigned, unsigned>.
1930   static unsigned getHashValue(const TargetInstrInfo::RegSubRegPair &Val) {
1931     std::pair<unsigned, unsigned> PairVal = std::make_pair(Val.Reg, Val.SubReg);
1932     return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
1933   }
1934 
1935   static bool isEqual(const TargetInstrInfo::RegSubRegPair &LHS,
1936                       const TargetInstrInfo::RegSubRegPair &RHS) {
1937     return RegInfo::isEqual(LHS.Reg, RHS.Reg) &&
1938            RegInfo::isEqual(LHS.SubReg, RHS.SubReg);
1939   }
1940 };
1941 
1942 } // end namespace llvm
1943 
1944 #endif // LLVM_TARGET_TARGETINSTRINFO_H
1945