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