1 //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the target machine instruction set to the code generator.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
15 #define LLVM_TARGET_TARGETINSTRINFO_H
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/CodeGen/DFAPacketizer.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineCombinerPattern.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
27 class InstrItineraryData;
30 class MachineMemOperand;
31 class MachineRegisterInfo;
35 class MCSymbolRefExpr;
37 class ScheduleHazardRecognizer;
40 class TargetRegisterClass;
41 class TargetRegisterInfo;
42 class BranchProbability;
43 class TargetSubtargetInfo;
45 template<class T> class SmallVectorImpl;
48 //---------------------------------------------------------------------------
50 /// TargetInstrInfo - Interface to description of machine instruction set
52 class TargetInstrInfo : public MCInstrInfo {
53 TargetInstrInfo(const TargetInstrInfo &) LLVM_DELETED_FUNCTION;
54 void operator=(const TargetInstrInfo &) LLVM_DELETED_FUNCTION;
56 TargetInstrInfo(int CFSetupOpcode = -1, int CFDestroyOpcode = -1)
57 : CallFrameSetupOpcode(CFSetupOpcode),
58 CallFrameDestroyOpcode(CFDestroyOpcode) {
61 virtual ~TargetInstrInfo();
63 /// getRegClass - Givem a machine instruction descriptor, returns the register
64 /// class constraint for OpNum, or NULL.
65 const TargetRegisterClass *getRegClass(const MCInstrDesc &TID,
67 const TargetRegisterInfo *TRI,
68 const MachineFunction &MF) const;
70 /// isTriviallyReMaterializable - Return true if the instruction is trivially
71 /// rematerializable, meaning it has no side effects and requires no operands
72 /// that aren't always available.
73 bool isTriviallyReMaterializable(const MachineInstr *MI,
74 AliasAnalysis *AA = nullptr) const {
75 return MI->getOpcode() == TargetOpcode::IMPLICIT_DEF ||
76 (MI->getDesc().isRematerializable() &&
77 (isReallyTriviallyReMaterializable(MI, AA) ||
78 isReallyTriviallyReMaterializableGeneric(MI, AA)));
82 /// isReallyTriviallyReMaterializable - For instructions with opcodes for
83 /// which the M_REMATERIALIZABLE flag is set, this hook lets the target
84 /// specify whether the instruction is actually trivially rematerializable,
85 /// taking into consideration its operands. This predicate must return false
86 /// if the instruction has any side effects other than producing a value, or
87 /// if it requres any address registers that are not always available.
88 virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
89 AliasAnalysis *AA) const {
94 /// isReallyTriviallyReMaterializableGeneric - For instructions with opcodes
95 /// for which the M_REMATERIALIZABLE flag is set and the target hook
96 /// isReallyTriviallyReMaterializable returns false, this function does
97 /// target-independent tests to determine if the instruction is really
98 /// trivially rematerializable.
99 bool isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
100 AliasAnalysis *AA) const;
103 /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
104 /// frame setup/destroy instructions if they exist (-1 otherwise). Some
105 /// targets use pseudo instructions in order to abstract away the difference
106 /// between operating with a frame pointer and operating without, through the
107 /// use of these two instructions.
109 int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
110 int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
112 /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
113 /// extension instruction. That is, it's like a copy where it's legal for the
114 /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
115 /// true, then it's expected the pre-extension value is available as a subreg
116 /// of the result register. This also returns the sub-register index in
118 virtual bool isCoalescableExtInstr(const MachineInstr &MI,
119 unsigned &SrcReg, unsigned &DstReg,
120 unsigned &SubIdx) const {
124 /// isLoadFromStackSlot - If the specified machine instruction is a direct
125 /// load from a stack slot, return the virtual or physical register number of
126 /// the destination along with the FrameIndex of the loaded stack slot. If
127 /// not, return 0. This predicate must return 0 if the instruction has
128 /// any side effects other than loading from the stack slot.
129 virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
130 int &FrameIndex) const {
134 /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
135 /// stack locations as well. This uses a heuristic so it isn't
136 /// reliable for correctness.
137 virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
138 int &FrameIndex) const {
142 /// hasLoadFromStackSlot - If the specified machine instruction has
143 /// a load from a stack slot, return true along with the FrameIndex
144 /// of the loaded stack slot and the machine mem operand containing
145 /// the reference. If not, return false. Unlike
146 /// isLoadFromStackSlot, this returns true for any instructions that
147 /// loads from the stack. This is just a hint, as some cases may be
149 virtual bool hasLoadFromStackSlot(const MachineInstr *MI,
150 const MachineMemOperand *&MMO,
151 int &FrameIndex) const;
153 /// isStoreToStackSlot - If the specified machine instruction is a direct
154 /// store to a stack slot, return the virtual or physical register number of
155 /// the source reg along with the FrameIndex of the loaded stack slot. If
156 /// not, return 0. This predicate must return 0 if the instruction has
157 /// any side effects other than storing to the stack slot.
158 virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
159 int &FrameIndex) const {
163 /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
164 /// stack locations as well. This uses a heuristic so it isn't
165 /// reliable for correctness.
166 virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
167 int &FrameIndex) const {
171 /// hasStoreToStackSlot - If the specified machine instruction has a
172 /// store to a stack slot, return true along with the FrameIndex of
173 /// the loaded stack slot and the machine mem operand containing the
174 /// reference. If not, return false. Unlike isStoreToStackSlot,
175 /// this returns true for any instructions that stores to the
176 /// stack. This is just a hint, as some cases may be missed.
177 virtual bool hasStoreToStackSlot(const MachineInstr *MI,
178 const MachineMemOperand *&MMO,
179 int &FrameIndex) const;
181 /// isStackSlotCopy - Return true if the specified machine instruction
182 /// is a copy of one stack slot to another and has no other effect.
183 /// Provide the identity of the two frame indices.
184 virtual bool isStackSlotCopy(const MachineInstr *MI, int &DestFrameIndex,
185 int &SrcFrameIndex) const {
189 /// Compute the size in bytes and offset within a stack slot of a spilled
190 /// register or subregister.
192 /// \param [out] Size in bytes of the spilled value.
193 /// \param [out] Offset in bytes within the stack slot.
194 /// \returns true if both Size and Offset are successfully computed.
196 /// Not all subregisters have computable spill slots. For example,
197 /// subregisters registers may not be byte-sized, and a pair of discontiguous
198 /// subregisters has no single offset.
200 /// Targets with nontrivial bigendian implementations may need to override
201 /// this, particularly to support spilled vector registers.
202 virtual bool getStackSlotRange(const TargetRegisterClass *RC, unsigned SubIdx,
203 unsigned &Size, unsigned &Offset,
204 const TargetMachine *TM) const;
206 /// isAsCheapAsAMove - Return true if the instruction is as cheap as a move
209 /// Targets for different archs need to override this, and different
210 /// micro-architectures can also be finely tuned inside.
211 virtual bool isAsCheapAsAMove(const MachineInstr *MI) const {
212 return MI->isAsCheapAsAMove();
215 /// reMaterialize - Re-issue the specified 'original' instruction at the
216 /// specific location targeting a new destination register.
217 /// The register in Orig->getOperand(0).getReg() will be substituted by
218 /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
220 virtual void reMaterialize(MachineBasicBlock &MBB,
221 MachineBasicBlock::iterator MI,
222 unsigned DestReg, unsigned SubIdx,
223 const MachineInstr *Orig,
224 const TargetRegisterInfo &TRI) const;
226 /// duplicate - Create a duplicate of the Orig instruction in MF. This is like
227 /// MachineFunction::CloneMachineInstr(), but the target may update operands
228 /// that are required to be unique.
230 /// The instruction must be duplicable as indicated by isNotDuplicable().
231 virtual MachineInstr *duplicate(MachineInstr *Orig,
232 MachineFunction &MF) const;
234 /// convertToThreeAddress - This method must be implemented by targets that
235 /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
236 /// may be able to convert a two-address instruction into one or more true
237 /// three-address instructions on demand. This allows the X86 target (for
238 /// example) to convert ADD and SHL instructions into LEA instructions if they
239 /// would require register copies due to two-addressness.
241 /// This method returns a null pointer if the transformation cannot be
242 /// performed, otherwise it returns the last new instruction.
244 virtual MachineInstr *
245 convertToThreeAddress(MachineFunction::iterator &MFI,
246 MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
250 /// commuteInstruction - If a target has any instructions that are
251 /// commutable but require converting to different instructions or making
252 /// non-trivial changes to commute them, this method can overloaded to do
253 /// that. The default implementation simply swaps the commutable operands.
254 /// If NewMI is false, MI is modified in place and returned; otherwise, a
255 /// new machine instruction is created and returned. Do not call this
256 /// method for a non-commutable instruction, but there may be some cases
257 /// where this method fails and returns null.
258 virtual MachineInstr *commuteInstruction(MachineInstr *MI,
259 bool NewMI = false) const;
261 /// findCommutedOpIndices - If specified MI is commutable, return the two
262 /// operand indices that would swap value. Return false if the instruction
263 /// is not in a form which this routine understands.
264 virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
265 unsigned &SrcOpIdx2) const;
267 /// A pair composed of a register and a sub-register index.
268 /// Used to give some type checking when modeling Reg:SubReg.
269 struct RegSubRegPair {
272 RegSubRegPair(unsigned Reg = 0, unsigned SubReg = 0)
273 : Reg(Reg), SubReg(SubReg) {}
275 /// A pair composed of a pair of a register and a sub-register index,
276 /// and another sub-register index.
277 /// Used to give some type checking when modeling Reg:SubReg1, SubReg2.
278 struct RegSubRegPairAndIdx : RegSubRegPair {
280 RegSubRegPairAndIdx(unsigned Reg = 0, unsigned SubReg = 0,
282 : RegSubRegPair(Reg, SubReg), SubIdx(SubIdx) {}
285 /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
287 /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
288 /// the list is modeled as <Reg:SubReg, SubIdx>.
289 /// E.g., REG_SEQUENCE vreg1:sub1, sub0, vreg2, sub1 would produce
291 /// - vreg1:sub1, sub0
292 /// - vreg2<:0>, sub1
294 /// \returns true if it is possible to build such an input sequence
295 /// with the pair \p MI, \p DefIdx. False otherwise.
297 /// \pre MI.isRegSequence() or MI.isRegSequenceLike().
299 /// \note The generic implementation does not provide any support for
300 /// MI.isRegSequenceLike(). In other words, one has to override
301 /// getRegSequenceLikeInputs for target specific instructions.
303 getRegSequenceInputs(const MachineInstr &MI, unsigned DefIdx,
304 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const;
306 /// produceSameValue - Return true if two machine instructions would produce
307 /// identical values. By default, this is only true when the two instructions
308 /// are deemed identical except for defs. If this function is called when the
309 /// IR is still in SSA form, the caller can pass the MachineRegisterInfo for
310 /// aggressive checks.
311 virtual bool produceSameValue(const MachineInstr *MI0,
312 const MachineInstr *MI1,
313 const MachineRegisterInfo *MRI = nullptr) const;
315 /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
316 /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
317 /// implemented for a target). Upon success, this returns false and returns
318 /// with the following information in various cases:
320 /// 1. If this block ends with no branches (it just falls through to its succ)
321 /// just return false, leaving TBB/FBB null.
322 /// 2. If this block ends with only an unconditional branch, it sets TBB to be
323 /// the destination block.
324 /// 3. If this block ends with a conditional branch and it falls through to a
325 /// successor block, it sets TBB to be the branch destination block and a
326 /// list of operands that evaluate the condition. These operands can be
327 /// passed to other TargetInstrInfo methods to create new branches.
328 /// 4. If this block ends with a conditional branch followed by an
329 /// unconditional branch, it returns the 'true' destination in TBB, the
330 /// 'false' destination in FBB, and a list of operands that evaluate the
331 /// condition. These operands can be passed to other TargetInstrInfo
332 /// methods to create new branches.
334 /// Note that RemoveBranch and InsertBranch must be implemented to support
335 /// cases where this method returns success.
337 /// If AllowModify is true, then this routine is allowed to modify the basic
338 /// block (e.g. delete instructions after the unconditional branch).
340 virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
341 MachineBasicBlock *&FBB,
342 SmallVectorImpl<MachineOperand> &Cond,
343 bool AllowModify = false) const {
347 /// RemoveBranch - Remove the branching code at the end of the specific MBB.
348 /// This is only invoked in cases where AnalyzeBranch returns success. It
349 /// returns the number of instructions that were removed.
350 virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
351 llvm_unreachable("Target didn't implement TargetInstrInfo::RemoveBranch!");
354 /// InsertBranch - Insert branch code into the end of the specified
355 /// MachineBasicBlock. The operands to this method are the same as those
356 /// returned by AnalyzeBranch. This is only invoked in cases where
357 /// AnalyzeBranch returns success. It returns the number of instructions
360 /// It is also invoked by tail merging to add unconditional branches in
361 /// cases where AnalyzeBranch doesn't apply because there was no original
362 /// branch to analyze. At least this much must be implemented, else tail
363 /// merging needs to be disabled.
364 virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
365 MachineBasicBlock *FBB,
366 const SmallVectorImpl<MachineOperand> &Cond,
368 llvm_unreachable("Target didn't implement TargetInstrInfo::InsertBranch!");
371 /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
372 /// after it, replacing it with an unconditional branch to NewDest. This is
373 /// used by the tail merging pass.
374 virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
375 MachineBasicBlock *NewDest) const;
377 /// getUnconditionalBranch - Get an instruction that performs an unconditional
378 /// branch to the given symbol.
380 getUnconditionalBranch(MCInst &MI,
381 const MCSymbolRefExpr *BranchTarget) const {
382 llvm_unreachable("Target didn't implement "
383 "TargetInstrInfo::getUnconditionalBranch!");
386 /// getTrap - Get a machine trap instruction
387 virtual void getTrap(MCInst &MI) const {
388 llvm_unreachable("Target didn't implement TargetInstrInfo::getTrap!");
391 /// isLegalToSplitMBBAt - Return true if it's legal to split the given basic
392 /// block at the specified instruction (i.e. instruction would be the start
393 /// of a new basic block).
394 virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
395 MachineBasicBlock::iterator MBBI) const {
399 /// isProfitableToIfCvt - Return true if it's profitable to predicate
400 /// instructions with accumulated instruction latency of "NumCycles"
401 /// of the specified basic block, where the probability of the instructions
402 /// being executed is given by Probability, and Confidence is a measure
403 /// of our confidence that it will be properly predicted.
405 bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
406 unsigned ExtraPredCycles,
407 const BranchProbability &Probability) const {
411 /// isProfitableToIfCvt - Second variant of isProfitableToIfCvt, this one
412 /// checks for the case where two basic blocks from true and false path
413 /// of a if-then-else (diamond) are predicated on mutally exclusive
414 /// predicates, where the probability of the true path being taken is given
415 /// by Probability, and Confidence is a measure of our confidence that it
416 /// will be properly predicted.
418 isProfitableToIfCvt(MachineBasicBlock &TMBB,
419 unsigned NumTCycles, unsigned ExtraTCycles,
420 MachineBasicBlock &FMBB,
421 unsigned NumFCycles, unsigned ExtraFCycles,
422 const BranchProbability &Probability) const {
426 /// isProfitableToDupForIfCvt - Return true if it's profitable for
427 /// if-converter to duplicate instructions of specified accumulated
428 /// instruction latencies in the specified MBB to enable if-conversion.
429 /// The probability of the instructions being executed is given by
430 /// Probability, and Confidence is a measure of our confidence that it
431 /// will be properly predicted.
433 isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
434 const BranchProbability &Probability) const {
438 /// isProfitableToUnpredicate - Return true if it's profitable to unpredicate
439 /// one side of a 'diamond', i.e. two sides of if-else predicated on mutually
440 /// exclusive predicates.
448 /// This may be profitable is conditional instructions are always executed.
449 virtual bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
450 MachineBasicBlock &FMBB) const {
454 /// canInsertSelect - Return true if it is possible to insert a select
455 /// instruction that chooses between TrueReg and FalseReg based on the
456 /// condition code in Cond.
458 /// When successful, also return the latency in cycles from TrueReg,
459 /// FalseReg, and Cond to the destination register. In most cases, a select
460 /// instruction will be 1 cycle, so CondCycles = TrueCycles = FalseCycles = 1
462 /// Some x86 implementations have 2-cycle cmov instructions.
464 /// @param MBB Block where select instruction would be inserted.
465 /// @param Cond Condition returned by AnalyzeBranch.
466 /// @param TrueReg Virtual register to select when Cond is true.
467 /// @param FalseReg Virtual register to select when Cond is false.
468 /// @param CondCycles Latency from Cond+Branch to select output.
469 /// @param TrueCycles Latency from TrueReg to select output.
470 /// @param FalseCycles Latency from FalseReg to select output.
471 virtual bool canInsertSelect(const MachineBasicBlock &MBB,
472 const SmallVectorImpl<MachineOperand> &Cond,
473 unsigned TrueReg, unsigned FalseReg,
475 int &TrueCycles, int &FalseCycles) const {
479 /// insertSelect - Insert a select instruction into MBB before I that will
480 /// copy TrueReg to DstReg when Cond is true, and FalseReg to DstReg when
483 /// This function can only be called after canInsertSelect() returned true.
484 /// The condition in Cond comes from AnalyzeBranch, and it can be assumed
485 /// that the same flags or registers required by Cond are available at the
488 /// @param MBB Block where select instruction should be inserted.
489 /// @param I Insertion point.
490 /// @param DL Source location for debugging.
491 /// @param DstReg Virtual register to be defined by select instruction.
492 /// @param Cond Condition as computed by AnalyzeBranch.
493 /// @param TrueReg Virtual register to copy when Cond is true.
494 /// @param FalseReg Virtual register to copy when Cons is false.
495 virtual void insertSelect(MachineBasicBlock &MBB,
496 MachineBasicBlock::iterator I, DebugLoc DL,
498 const SmallVectorImpl<MachineOperand> &Cond,
499 unsigned TrueReg, unsigned FalseReg) const {
500 llvm_unreachable("Target didn't implement TargetInstrInfo::insertSelect!");
503 /// analyzeSelect - Analyze the given select instruction, returning true if
504 /// it cannot be understood. It is assumed that MI->isSelect() is true.
506 /// When successful, return the controlling condition and the operands that
507 /// determine the true and false result values.
509 /// Result = SELECT Cond, TrueOp, FalseOp
511 /// Some targets can optimize select instructions, for example by predicating
512 /// the instruction defining one of the operands. Such targets should set
515 /// @param MI Select instruction to analyze.
516 /// @param Cond Condition controlling the select.
517 /// @param TrueOp Operand number of the value selected when Cond is true.
518 /// @param FalseOp Operand number of the value selected when Cond is false.
519 /// @param Optimizable Returned as true if MI is optimizable.
520 /// @returns False on success.
521 virtual bool analyzeSelect(const MachineInstr *MI,
522 SmallVectorImpl<MachineOperand> &Cond,
523 unsigned &TrueOp, unsigned &FalseOp,
524 bool &Optimizable) const {
525 assert(MI && MI->getDesc().isSelect() && "MI must be a select instruction");
529 /// optimizeSelect - Given a select instruction that was understood by
530 /// analyzeSelect and returned Optimizable = true, attempt to optimize MI by
531 /// merging it with one of its operands. Returns NULL on failure.
533 /// When successful, returns the new select instruction. The client is
534 /// responsible for deleting MI.
536 /// If both sides of the select can be optimized, PreferFalse is used to pick
539 /// @param MI Optimizable select instruction.
540 /// @param PreferFalse Try to optimize FalseOp instead of TrueOp.
541 /// @returns Optimized instruction or NULL.
542 virtual MachineInstr *optimizeSelect(MachineInstr *MI,
543 bool PreferFalse = false) const {
544 // This function must be implemented if Optimizable is ever set.
545 llvm_unreachable("Target must implement TargetInstrInfo::optimizeSelect!");
548 /// copyPhysReg - Emit instructions to copy a pair of physical registers.
550 /// This function should support copies within any legal register class as
551 /// well as any cross-class copies created during instruction selection.
553 /// The source and destination registers may overlap, which may require a
554 /// careful implementation when multiple copy instructions are required for
555 /// large registers. See for example the ARM target.
556 virtual void copyPhysReg(MachineBasicBlock &MBB,
557 MachineBasicBlock::iterator MI, DebugLoc DL,
558 unsigned DestReg, unsigned SrcReg,
559 bool KillSrc) const {
560 llvm_unreachable("Target didn't implement TargetInstrInfo::copyPhysReg!");
563 /// storeRegToStackSlot - Store the specified register of the given register
564 /// class to the specified stack frame index. The store instruction is to be
565 /// added to the given machine basic block before the specified machine
566 /// instruction. If isKill is true, the register operand is the last use and
567 /// must be marked kill.
568 virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
569 MachineBasicBlock::iterator MI,
570 unsigned SrcReg, bool isKill, int FrameIndex,
571 const TargetRegisterClass *RC,
572 const TargetRegisterInfo *TRI) const {
573 llvm_unreachable("Target didn't implement "
574 "TargetInstrInfo::storeRegToStackSlot!");
577 /// loadRegFromStackSlot - Load the specified register of the given register
578 /// class from the specified stack frame index. The load instruction is to be
579 /// added to the given machine basic block before the specified machine
581 virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
582 MachineBasicBlock::iterator MI,
583 unsigned DestReg, int FrameIndex,
584 const TargetRegisterClass *RC,
585 const TargetRegisterInfo *TRI) const {
586 llvm_unreachable("Target didn't implement "
587 "TargetInstrInfo::loadRegFromStackSlot!");
590 /// expandPostRAPseudo - This function is called for all pseudo instructions
591 /// that remain after register allocation. Many pseudo instructions are
592 /// created to help register allocation. This is the place to convert them
593 /// into real instructions. The target can edit MI in place, or it can insert
594 /// new instructions and erase MI. The function should return true if
595 /// anything was changed.
596 virtual bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
600 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
601 /// slot into the specified machine instruction for the specified operand(s).
602 /// If this is possible, a new instruction is returned with the specified
603 /// operand folded, otherwise NULL is returned.
604 /// The new instruction is inserted before MI, and the client is responsible
605 /// for removing the old instruction.
606 MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
607 const SmallVectorImpl<unsigned> &Ops,
608 int FrameIndex) const;
610 /// foldMemoryOperand - Same as the previous version except it allows folding
611 /// of any load and store from / to any address, not just from a specific
613 MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
614 const SmallVectorImpl<unsigned> &Ops,
615 MachineInstr* LoadMI) const;
617 /// hasPattern - return true when there is potentially a faster code sequence
618 /// for an instruction chain ending in \p Root. All potential pattern are
619 /// returned in the \p Pattern vector. Pattern should be sorted in priority
620 /// order since the pattern evaluator stops checking as soon as it finds a
622 /// \param Root - Instruction that could be combined with one of its operands
623 /// \param Pattern - Vector of possible combination pattern
625 virtual bool hasPattern(
627 SmallVectorImpl<MachineCombinerPattern::MC_PATTERN> &Pattern) const {
631 /// genAlternativeCodeSequence - when hasPattern() finds a pattern this
632 /// function generates the instructions that could replace the original code
633 /// sequence. The client has to decide whether the actual replacementment is
634 /// beneficial or not.
635 /// \param Root - Instruction that could be combined with one of its operands
636 /// \param P - Combination pattern for Root
637 /// \param InsInstrs - Vector of new instructions that implement P
638 /// \param DelInstrs - Old instructions, including Root, that could be replaced
640 /// \param InstrIdxForVirtReg - map of virtual register to instruction in
641 /// InsInstr that defines it
642 virtual void genAlternativeCodeSequence(
643 MachineInstr &Root, MachineCombinerPattern::MC_PATTERN P,
644 SmallVectorImpl<MachineInstr *> &InsInstrs,
645 SmallVectorImpl<MachineInstr *> &DelInstrs,
646 DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
650 /// useMachineCombiner - return true when a target supports MachineCombiner
651 virtual bool useMachineCombiner(void) const { return false; }
654 /// foldMemoryOperandImpl - Target-dependent implementation for
655 /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
656 /// take care of adding a MachineMemOperand to the newly created instruction.
657 virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
659 const SmallVectorImpl<unsigned> &Ops,
660 int FrameIndex) const {
664 /// foldMemoryOperandImpl - Target-dependent implementation for
665 /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
666 /// take care of adding a MachineMemOperand to the newly created instruction.
667 virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
669 const SmallVectorImpl<unsigned> &Ops,
670 MachineInstr* LoadMI) const {
674 /// \brief Target-dependent implementation of getRegSequenceInputs.
676 /// \returns true if it is possible to build the equivalent
677 /// REG_SEQUENCE inputs with the pair \p MI, \p DefIdx. False otherwise.
679 /// \pre MI.isRegSequenceLike().
681 /// \see TargetInstrInfo::getRegSequenceInputs.
682 virtual bool getRegSequenceLikeInputs(
683 const MachineInstr &MI, unsigned DefIdx,
684 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
689 /// canFoldMemoryOperand - Returns true for the specified load / store if
690 /// folding is possible.
692 bool canFoldMemoryOperand(const MachineInstr *MI,
693 const SmallVectorImpl<unsigned> &Ops) const;
695 /// unfoldMemoryOperand - Separate a single instruction which folded a load or
696 /// a store or a load and a store into two or more instruction. If this is
697 /// possible, returns true as well as the new instructions by reference.
698 virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
699 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
700 SmallVectorImpl<MachineInstr*> &NewMIs) const{
704 virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
705 SmallVectorImpl<SDNode*> &NewNodes) const {
709 /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
710 /// instruction after load / store are unfolded from an instruction of the
711 /// specified opcode. It returns zero if the specified unfolding is not
712 /// possible. If LoadRegIndex is non-null, it is filled in with the operand
713 /// index of the operand which will hold the register holding the loaded
715 virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
716 bool UnfoldLoad, bool UnfoldStore,
717 unsigned *LoadRegIndex = nullptr) const {
721 /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
722 /// to determine if two loads are loading from the same base address. It
723 /// should only return true if the base pointers are the same and the
724 /// only differences between the two addresses are the offset. It also returns
725 /// the offsets by reference.
726 virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
727 int64_t &Offset1, int64_t &Offset2) const {
731 /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
732 /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
733 /// be scheduled togther. On some targets if two loads are loading from
734 /// addresses in the same cache line, it's better if they are scheduled
735 /// together. This function takes two integers that represent the load offsets
736 /// from the common base address. It returns true if it decides it's desirable
737 /// to schedule the two loads together. "NumLoads" is the number of loads that
738 /// have already been scheduled after Load1.
739 virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
740 int64_t Offset1, int64_t Offset2,
741 unsigned NumLoads) const {
745 /// \brief Get the base register and byte offset of a load/store instr.
746 virtual bool getLdStBaseRegImmOfs(MachineInstr *LdSt,
747 unsigned &BaseReg, unsigned &Offset,
748 const TargetRegisterInfo *TRI) const {
752 virtual bool enableClusterLoads() const { return false; }
754 virtual bool shouldClusterLoads(MachineInstr *FirstLdSt,
755 MachineInstr *SecondLdSt,
756 unsigned NumLoads) const {
760 /// \brief Can this target fuse the given instructions if they are scheduled
762 virtual bool shouldScheduleAdjacent(MachineInstr* First,
763 MachineInstr *Second) const {
767 /// ReverseBranchCondition - Reverses the branch condition of the specified
768 /// condition list, returning false on success and true if it cannot be
771 bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
775 /// insertNoop - Insert a noop into the instruction stream at the specified
777 virtual void insertNoop(MachineBasicBlock &MBB,
778 MachineBasicBlock::iterator MI) const;
781 /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
782 virtual void getNoopForMachoTarget(MCInst &NopInst) const {
783 // Default to just using 'nop' string.
787 /// isPredicated - Returns true if the instruction is already predicated.
789 virtual bool isPredicated(const MachineInstr *MI) const {
793 /// isUnpredicatedTerminator - Returns true if the instruction is a
794 /// terminator instruction that has not been predicated.
795 virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
797 /// PredicateInstruction - Convert the instruction into a predicated
798 /// instruction. It returns true if the operation was successful.
800 bool PredicateInstruction(MachineInstr *MI,
801 const SmallVectorImpl<MachineOperand> &Pred) const;
803 /// SubsumesPredicate - Returns true if the first specified predicate
804 /// subsumes the second, e.g. GE subsumes GT.
806 bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
807 const SmallVectorImpl<MachineOperand> &Pred2) const {
811 /// DefinesPredicate - If the specified instruction defines any predicate
812 /// or condition code register(s) used for predication, returns true as well
813 /// as the definition predicate(s) by reference.
814 virtual bool DefinesPredicate(MachineInstr *MI,
815 std::vector<MachineOperand> &Pred) const {
819 /// isPredicable - Return true if the specified instruction can be predicated.
820 /// By default, this returns true for every instruction with a
821 /// PredicateOperand.
822 virtual bool isPredicable(MachineInstr *MI) const {
823 return MI->getDesc().isPredicable();
826 /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
827 /// instruction that defines the specified register class.
828 virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
832 /// isSchedulingBoundary - Test if the given instruction should be
833 /// considered a scheduling boundary. This primarily includes labels and
835 virtual bool isSchedulingBoundary(const MachineInstr *MI,
836 const MachineBasicBlock *MBB,
837 const MachineFunction &MF) const;
839 /// Measure the specified inline asm to determine an approximation of its
841 virtual unsigned getInlineAsmLength(const char *Str,
842 const MCAsmInfo &MAI) const;
844 /// CreateTargetHazardRecognizer - Allocate and return a hazard recognizer to
845 /// use for this target when scheduling the machine instructions before
846 /// register allocation.
847 virtual ScheduleHazardRecognizer*
848 CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
849 const ScheduleDAG *DAG) const;
851 /// CreateTargetMIHazardRecognizer - Allocate and return a hazard recognizer
852 /// to use for this target when scheduling the machine instructions before
853 /// register allocation.
854 virtual ScheduleHazardRecognizer*
855 CreateTargetMIHazardRecognizer(const InstrItineraryData*,
856 const ScheduleDAG *DAG) const;
858 /// CreateTargetPostRAHazardRecognizer - Allocate and return a hazard
859 /// recognizer to use for this target when scheduling the machine instructions
860 /// after register allocation.
861 virtual ScheduleHazardRecognizer*
862 CreateTargetPostRAHazardRecognizer(const InstrItineraryData*,
863 const ScheduleDAG *DAG) const;
865 /// Provide a global flag for disabling the PreRA hazard recognizer that
866 /// targets may choose to honor.
867 bool usePreRAHazardRecognizer() const;
869 /// analyzeCompare - For a comparison instruction, return the source registers
870 /// in SrcReg and SrcReg2 if having two register operands, and the value it
871 /// compares against in CmpValue. Return true if the comparison instruction
873 virtual bool analyzeCompare(const MachineInstr *MI,
874 unsigned &SrcReg, unsigned &SrcReg2,
875 int &Mask, int &Value) const {
879 /// optimizeCompareInstr - See if the comparison instruction can be converted
880 /// into something more efficient. E.g., on ARM most instructions can set the
881 /// flags register, obviating the need for a separate CMP.
882 virtual bool optimizeCompareInstr(MachineInstr *CmpInstr,
883 unsigned SrcReg, unsigned SrcReg2,
885 const MachineRegisterInfo *MRI) const {
889 /// optimizeLoadInstr - Try to remove the load by folding it to a register
890 /// operand at the use. We fold the load instructions if and only if the
891 /// def and use are in the same BB. We only look at one load and see
892 /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
893 /// defined by the load we are trying to fold. DefMI returns the machine
894 /// instruction that defines FoldAsLoadDefReg, and the function returns
895 /// the machine instruction generated due to folding.
896 virtual MachineInstr* optimizeLoadInstr(MachineInstr *MI,
897 const MachineRegisterInfo *MRI,
898 unsigned &FoldAsLoadDefReg,
899 MachineInstr *&DefMI) const {
903 /// FoldImmediate - 'Reg' is known to be defined by a move immediate
904 /// instruction, try to fold the immediate into the use instruction.
905 /// If MRI->hasOneNonDBGUse(Reg) is true, and this function returns true,
906 /// then the caller may assume that DefMI has been erased from its parent
907 /// block. The caller may assume that it will not be erased by this
908 /// function otherwise.
909 virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
910 unsigned Reg, MachineRegisterInfo *MRI) const {
914 /// getNumMicroOps - Return the number of u-operations the given machine
915 /// instruction will be decoded to on the target cpu. The itinerary's
916 /// IssueWidth is the number of microops that can be dispatched each
917 /// cycle. An instruction with zero microops takes no dispatch resources.
918 virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
919 const MachineInstr *MI) const;
921 /// isZeroCost - Return true for pseudo instructions that don't consume any
922 /// machine resources in their current form. These are common cases that the
923 /// scheduler should consider free, rather than conservatively handling them
924 /// as instructions with no itinerary.
925 bool isZeroCost(unsigned Opcode) const {
926 return Opcode <= TargetOpcode::COPY;
929 virtual int getOperandLatency(const InstrItineraryData *ItinData,
930 SDNode *DefNode, unsigned DefIdx,
931 SDNode *UseNode, unsigned UseIdx) const;
933 /// getOperandLatency - Compute and return the use operand latency of a given
934 /// pair of def and use.
935 /// In most cases, the static scheduling itinerary was enough to determine the
936 /// operand latency. But it may not be possible for instructions with variable
937 /// number of defs / uses.
939 /// This is a raw interface to the itinerary that may be directly overriden by
940 /// a target. Use computeOperandLatency to get the best estimate of latency.
941 virtual int getOperandLatency(const InstrItineraryData *ItinData,
942 const MachineInstr *DefMI, unsigned DefIdx,
943 const MachineInstr *UseMI,
944 unsigned UseIdx) const;
946 /// computeOperandLatency - Compute and return the latency of the given data
947 /// dependent def and use when the operand indices are already known.
948 unsigned computeOperandLatency(const InstrItineraryData *ItinData,
949 const MachineInstr *DefMI, unsigned DefIdx,
950 const MachineInstr *UseMI, unsigned UseIdx)
953 /// getInstrLatency - Compute the instruction latency of a given instruction.
954 /// If the instruction has higher cost when predicated, it's returned via
956 virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
957 const MachineInstr *MI,
958 unsigned *PredCost = nullptr) const;
960 virtual unsigned getPredicationCost(const MachineInstr *MI) const;
962 virtual int getInstrLatency(const InstrItineraryData *ItinData,
965 /// Return the default expected latency for a def based on it's opcode.
966 unsigned defaultDefLatency(const MCSchedModel *SchedModel,
967 const MachineInstr *DefMI) const;
969 int computeDefOperandLatency(const InstrItineraryData *ItinData,
970 const MachineInstr *DefMI) const;
972 /// isHighLatencyDef - Return true if this opcode has high latency to its
974 virtual bool isHighLatencyDef(int opc) const { return false; }
976 /// hasHighOperandLatency - Compute operand latency between a def of 'Reg'
977 /// and an use in the current loop, return true if the target considered
978 /// it 'high'. This is used by optimization passes such as machine LICM to
979 /// determine whether it makes sense to hoist an instruction out even in
980 /// high register pressure situation.
982 bool hasHighOperandLatency(const InstrItineraryData *ItinData,
983 const MachineRegisterInfo *MRI,
984 const MachineInstr *DefMI, unsigned DefIdx,
985 const MachineInstr *UseMI, unsigned UseIdx) const {
989 /// hasLowDefLatency - Compute operand latency of a def of 'Reg', return true
990 /// if the target considered it 'low'.
992 bool hasLowDefLatency(const InstrItineraryData *ItinData,
993 const MachineInstr *DefMI, unsigned DefIdx) const;
995 /// verifyInstruction - Perform target specific instruction verification.
997 bool verifyInstruction(const MachineInstr *MI, StringRef &ErrInfo) const {
1001 /// getExecutionDomain - Return the current execution domain and bit mask of
1002 /// possible domains for instruction.
1004 /// Some micro-architectures have multiple execution domains, and multiple
1005 /// opcodes that perform the same operation in different domains. For
1006 /// example, the x86 architecture provides the por, orps, and orpd
1007 /// instructions that all do the same thing. There is a latency penalty if a
1008 /// register is written in one domain and read in another.
1010 /// This function returns a pair (domain, mask) containing the execution
1011 /// domain of MI, and a bit mask of possible domains. The setExecutionDomain
1012 /// function can be used to change the opcode to one of the domains in the
1013 /// bit mask. Instructions whose execution domain can't be changed should
1014 /// return a 0 mask.
1016 /// The execution domain numbers don't have any special meaning except domain
1017 /// 0 is used for instructions that are not associated with any interesting
1018 /// execution domain.
1020 virtual std::pair<uint16_t, uint16_t>
1021 getExecutionDomain(const MachineInstr *MI) const {
1022 return std::make_pair(0, 0);
1025 /// setExecutionDomain - Change the opcode of MI to execute in Domain.
1027 /// The bit (1 << Domain) must be set in the mask returned from
1028 /// getExecutionDomain(MI).
1030 virtual void setExecutionDomain(MachineInstr *MI, unsigned Domain) const {}
1033 /// getPartialRegUpdateClearance - Returns the preferred minimum clearance
1034 /// before an instruction with an unwanted partial register update.
1036 /// Some instructions only write part of a register, and implicitly need to
1037 /// read the other parts of the register. This may cause unwanted stalls
1038 /// preventing otherwise unrelated instructions from executing in parallel in
1039 /// an out-of-order CPU.
1041 /// For example, the x86 instruction cvtsi2ss writes its result to bits
1042 /// [31:0] of the destination xmm register. Bits [127:32] are unaffected, so
1043 /// the instruction needs to wait for the old value of the register to become
1046 /// addps %xmm1, %xmm0
1047 /// movaps %xmm0, (%rax)
1048 /// cvtsi2ss %rbx, %xmm0
1050 /// In the code above, the cvtsi2ss instruction needs to wait for the addps
1051 /// instruction before it can issue, even though the high bits of %xmm0
1052 /// probably aren't needed.
1054 /// This hook returns the preferred clearance before MI, measured in
1055 /// instructions. Other defs of MI's operand OpNum are avoided in the last N
1056 /// instructions before MI. It should only return a positive value for
1057 /// unwanted dependencies. If the old bits of the defined register have
1058 /// useful values, or if MI is determined to otherwise read the dependency,
1059 /// the hook should return 0.
1061 /// The unwanted dependency may be handled by:
1063 /// 1. Allocating the same register for an MI def and use. That makes the
1064 /// unwanted dependency identical to a required dependency.
1066 /// 2. Allocating a register for the def that has no defs in the previous N
1069 /// 3. Calling breakPartialRegDependency() with the same arguments. This
1070 /// allows the target to insert a dependency breaking instruction.
1073 getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
1074 const TargetRegisterInfo *TRI) const {
1075 // The default implementation returns 0 for no partial register dependency.
1079 /// \brief Return the minimum clearance before an instruction that reads an
1080 /// unused register.
1082 /// For example, AVX instructions may copy part of an register operand into
1083 /// the unused high bits of the destination register.
1085 /// vcvtsi2sdq %rax, %xmm0<undef>, %xmm14
1087 /// In the code above, vcvtsi2sdq copies %xmm0[127:64] into %xmm14 creating a
1088 /// false dependence on any previous write to %xmm0.
1090 /// This hook works similarly to getPartialRegUpdateClearance, except that it
1091 /// does not take an operand index. Instead sets \p OpNum to the index of the
1092 /// unused register.
1093 virtual unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
1094 const TargetRegisterInfo *TRI) const {
1095 // The default implementation returns 0 for no undef register dependency.
1099 /// breakPartialRegDependency - Insert a dependency-breaking instruction
1100 /// before MI to eliminate an unwanted dependency on OpNum.
1102 /// If it wasn't possible to avoid a def in the last N instructions before MI
1103 /// (see getPartialRegUpdateClearance), this hook will be called to break the
1104 /// unwanted dependency.
1106 /// On x86, an xorps instruction can be used as a dependency breaker:
1108 /// addps %xmm1, %xmm0
1109 /// movaps %xmm0, (%rax)
1110 /// xorps %xmm0, %xmm0
1111 /// cvtsi2ss %rbx, %xmm0
1113 /// An <imp-kill> operand should be added to MI if an instruction was
1114 /// inserted. This ties the instructions together in the post-ra scheduler.
1117 breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
1118 const TargetRegisterInfo *TRI) const {}
1120 /// Create machine specific model for scheduling.
1121 virtual DFAPacketizer*
1122 CreateTargetScheduleState(const TargetMachine*, const ScheduleDAG*) const {
1127 int CallFrameSetupOpcode, CallFrameDestroyOpcode;
1130 } // End llvm namespace