Various bits of framework needed for precise machine-level selection
[oota-llvm.git] / include / llvm / Target / TargetInstrInfo.h
1 //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the target machine instruction set to the code generator.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_TARGETINSTRINFO_H
15 #define LLVM_TARGET_TARGETINSTRINFO_H
16
17 #include "llvm/Target/TargetInstrDesc.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19
20 namespace llvm {
21
22 class InstrItineraryData;
23 class LiveVariables;
24 class MCAsmInfo;
25 class MachineMemOperand;
26 class MachineRegisterInfo;
27 class MDNode;
28 class MCInst;
29 class SDNode;
30 class ScheduleHazardRecognizer;
31 class SelectionDAG;
32 class ScheduleDAG;
33 class TargetRegisterClass;
34 class TargetRegisterInfo;
35
36 template<class T> class SmallVectorImpl;
37
38
39 //---------------------------------------------------------------------------
40 ///
41 /// TargetInstrInfo - Interface to description of machine instruction set
42 ///
43 class TargetInstrInfo {
44   const TargetInstrDesc *Descriptors; // Raw array to allow static init'n
45   unsigned NumOpcodes;                // Number of entries in the desc array
46
47   TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
48   void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
49 public:
50   TargetInstrInfo(const TargetInstrDesc *desc, unsigned NumOpcodes);
51   virtual ~TargetInstrInfo();
52
53   unsigned getNumOpcodes() const { return NumOpcodes; }
54
55   /// get - Return the machine instruction descriptor that corresponds to the
56   /// specified instruction opcode.
57   ///
58   const TargetInstrDesc &get(unsigned Opcode) const {
59     assert(Opcode < NumOpcodes && "Invalid opcode!");
60     return Descriptors[Opcode];
61   }
62
63   /// isTriviallyReMaterializable - Return true if the instruction is trivially
64   /// rematerializable, meaning it has no side effects and requires no operands
65   /// that aren't always available.
66   bool isTriviallyReMaterializable(const MachineInstr *MI,
67                                    AliasAnalysis *AA = 0) const {
68     return MI->getOpcode() == TargetOpcode::IMPLICIT_DEF ||
69            (MI->getDesc().isRematerializable() &&
70             (isReallyTriviallyReMaterializable(MI, AA) ||
71              isReallyTriviallyReMaterializableGeneric(MI, AA)));
72   }
73
74 protected:
75   /// isReallyTriviallyReMaterializable - For instructions with opcodes for
76   /// which the M_REMATERIALIZABLE flag is set, this hook lets the target
77   /// specify whether the instruction is actually trivially rematerializable,
78   /// taking into consideration its operands. This predicate must return false
79   /// if the instruction has any side effects other than producing a value, or
80   /// if it requres any address registers that are not always available.
81   virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
82                                                  AliasAnalysis *AA) const {
83     return false;
84   }
85
86 private:
87   /// isReallyTriviallyReMaterializableGeneric - For instructions with opcodes
88   /// for which the M_REMATERIALIZABLE flag is set and the target hook
89   /// isReallyTriviallyReMaterializable returns false, this function does
90   /// target-independent tests to determine if the instruction is really
91   /// trivially rematerializable.
92   bool isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
93                                                 AliasAnalysis *AA) const;
94
95 public:
96   /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
97   /// extension instruction. That is, it's like a copy where it's legal for the
98   /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
99   /// true, then it's expected the pre-extension value is available as a subreg
100   /// of the result register. This also returns the sub-register index in
101   /// SubIdx.
102   virtual bool isCoalescableExtInstr(const MachineInstr &MI,
103                                      unsigned &SrcReg, unsigned &DstReg,
104                                      unsigned &SubIdx) const {
105     return false;
106   }
107
108   /// isLoadFromStackSlot - If the specified machine instruction is a direct
109   /// load from a stack slot, return the virtual or physical register number of
110   /// the destination along with the FrameIndex of the loaded stack slot.  If
111   /// not, return 0.  This predicate must return 0 if the instruction has
112   /// any side effects other than loading from the stack slot.
113   virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
114                                        int &FrameIndex) const {
115     return 0;
116   }
117
118   /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
119   /// stack locations as well.  This uses a heuristic so it isn't
120   /// reliable for correctness.
121   virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
122                                              int &FrameIndex) const {
123     return 0;
124   }
125
126   /// hasLoadFromStackSlot - If the specified machine instruction has
127   /// a load from a stack slot, return true along with the FrameIndex
128   /// of the loaded stack slot and the machine mem operand containing
129   /// the reference.  If not, return false.  Unlike
130   /// isLoadFromStackSlot, this returns true for any instructions that
131   /// loads from the stack.  This is just a hint, as some cases may be
132   /// missed.
133   virtual bool hasLoadFromStackSlot(const MachineInstr *MI,
134                                     const MachineMemOperand *&MMO,
135                                     int &FrameIndex) const {
136     return 0;
137   }
138
139   /// isStoreToStackSlot - If the specified machine instruction is a direct
140   /// store to a stack slot, return the virtual or physical register number of
141   /// the source reg along with the FrameIndex of the loaded stack slot.  If
142   /// not, return 0.  This predicate must return 0 if the instruction has
143   /// any side effects other than storing to the stack slot.
144   virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
145                                       int &FrameIndex) const {
146     return 0;
147   }
148
149   /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
150   /// stack locations as well.  This uses a heuristic so it isn't
151   /// reliable for correctness.
152   virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
153                                             int &FrameIndex) const {
154     return 0;
155   }
156
157   /// hasStoreToStackSlot - If the specified machine instruction has a
158   /// store to a stack slot, return true along with the FrameIndex of
159   /// the loaded stack slot and the machine mem operand containing the
160   /// reference.  If not, return false.  Unlike isStoreToStackSlot,
161   /// this returns true for any instructions that stores to the
162   /// stack.  This is just a hint, as some cases may be missed.
163   virtual bool hasStoreToStackSlot(const MachineInstr *MI,
164                                    const MachineMemOperand *&MMO,
165                                    int &FrameIndex) const {
166     return 0;
167   }
168
169   /// reMaterialize - Re-issue the specified 'original' instruction at the
170   /// specific location targeting a new destination register.
171   /// The register in Orig->getOperand(0).getReg() will be substituted by
172   /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
173   /// SubIdx.
174   virtual void reMaterialize(MachineBasicBlock &MBB,
175                              MachineBasicBlock::iterator MI,
176                              unsigned DestReg, unsigned SubIdx,
177                              const MachineInstr *Orig,
178                              const TargetRegisterInfo &TRI) const = 0;
179
180   /// scheduleTwoAddrSource - Schedule the copy / re-mat of the source of the
181   /// two-addrss instruction inserted by two-address pass.
182   virtual void scheduleTwoAddrSource(MachineInstr *SrcMI,
183                                      MachineInstr *UseMI,
184                                      const TargetRegisterInfo &TRI) const {
185     // Do nothing.
186   }
187
188   /// duplicate - Create a duplicate of the Orig instruction in MF. This is like
189   /// MachineFunction::CloneMachineInstr(), but the target may update operands
190   /// that are required to be unique.
191   ///
192   /// The instruction must be duplicable as indicated by isNotDuplicable().
193   virtual MachineInstr *duplicate(MachineInstr *Orig,
194                                   MachineFunction &MF) const = 0;
195
196   /// convertToThreeAddress - This method must be implemented by targets that
197   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
198   /// may be able to convert a two-address instruction into one or more true
199   /// three-address instructions on demand.  This allows the X86 target (for
200   /// example) to convert ADD and SHL instructions into LEA instructions if they
201   /// would require register copies due to two-addressness.
202   ///
203   /// This method returns a null pointer if the transformation cannot be
204   /// performed, otherwise it returns the last new instruction.
205   ///
206   virtual MachineInstr *
207   convertToThreeAddress(MachineFunction::iterator &MFI,
208                    MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
209     return 0;
210   }
211
212   /// commuteInstruction - If a target has any instructions that are
213   /// commutable but require converting to different instructions or making
214   /// non-trivial changes to commute them, this method can overloaded to do
215   /// that.  The default implementation simply swaps the commutable operands.
216   /// If NewMI is false, MI is modified in place and returned; otherwise, a
217   /// new machine instruction is created and returned.  Do not call this
218   /// method for a non-commutable instruction, but there may be some cases
219   /// where this method fails and returns null.
220   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
221                                            bool NewMI = false) const = 0;
222
223   /// findCommutedOpIndices - If specified MI is commutable, return the two
224   /// operand indices that would swap value. Return false if the instruction
225   /// is not in a form which this routine understands.
226   virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
227                                      unsigned &SrcOpIdx2) const = 0;
228
229   /// produceSameValue - Return true if two machine instructions would produce
230   /// identical values. By default, this is only true when the two instructions
231   /// are deemed identical except for defs.
232   virtual bool produceSameValue(const MachineInstr *MI0,
233                                 const MachineInstr *MI1) const = 0;
234
235   /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
236   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
237   /// implemented for a target).  Upon success, this returns false and returns
238   /// with the following information in various cases:
239   ///
240   /// 1. If this block ends with no branches (it just falls through to its succ)
241   ///    just return false, leaving TBB/FBB null.
242   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
243   ///    the destination block.
244   /// 3. If this block ends with a conditional branch and it falls through to a
245   ///    successor block, it sets TBB to be the branch destination block and a
246   ///    list of operands that evaluate the condition. These operands can be
247   ///    passed to other TargetInstrInfo methods to create new branches.
248   /// 4. If this block ends with a conditional branch followed by an
249   ///    unconditional branch, it returns the 'true' destination in TBB, the
250   ///    'false' destination in FBB, and a list of operands that evaluate the
251   ///    condition.  These operands can be passed to other TargetInstrInfo
252   ///    methods to create new branches.
253   ///
254   /// Note that RemoveBranch and InsertBranch must be implemented to support
255   /// cases where this method returns success.
256   ///
257   /// If AllowModify is true, then this routine is allowed to modify the basic
258   /// block (e.g. delete instructions after the unconditional branch).
259   ///
260   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
261                              MachineBasicBlock *&FBB,
262                              SmallVectorImpl<MachineOperand> &Cond,
263                              bool AllowModify = false) const {
264     return true;
265   }
266
267   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
268   /// This is only invoked in cases where AnalyzeBranch returns success. It
269   /// returns the number of instructions that were removed.
270   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
271     assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!");
272     return 0;
273   }
274
275   /// InsertBranch - Insert branch code into the end of the specified
276   /// MachineBasicBlock.  The operands to this method are the same as those
277   /// returned by AnalyzeBranch.  This is only invoked in cases where
278   /// AnalyzeBranch returns success. It returns the number of instructions
279   /// inserted.
280   ///
281   /// It is also invoked by tail merging to add unconditional branches in
282   /// cases where AnalyzeBranch doesn't apply because there was no original
283   /// branch to analyze.  At least this much must be implemented, else tail
284   /// merging needs to be disabled.
285   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
286                                 MachineBasicBlock *FBB,
287                                 const SmallVectorImpl<MachineOperand> &Cond,
288                                 DebugLoc DL) const {
289     assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!");
290     return 0;
291   }
292
293   /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
294   /// after it, replacing it with an unconditional branch to NewDest. This is
295   /// used by the tail merging pass.
296   virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
297                                        MachineBasicBlock *NewDest) const = 0;
298
299   /// isLegalToSplitMBBAt - Return true if it's legal to split the given basic
300   /// block at the specified instruction (i.e. instruction would be the start
301   /// of a new basic block).
302   virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
303                                    MachineBasicBlock::iterator MBBI) const {
304     return true;
305   }
306
307   /// isProfitableToIfCvt - Return true if it's profitable to predicate
308   /// instructions with accumulated instruction latency of "NumCycles"
309   /// of the specified basic block, where the probability of the instructions
310   /// being executed is given by Probability, and Confidence is a measure
311   /// of our confidence that it will be properly predicted.
312   virtual
313   bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
314                            unsigned ExtraPredCycles,
315                            float Probability, float Confidence) const {
316     return false;
317   }
318
319   /// isProfitableToIfCvt - Second variant of isProfitableToIfCvt, this one
320   /// checks for the case where two basic blocks from true and false path
321   /// of a if-then-else (diamond) are predicated on mutally exclusive
322   /// predicates, where the probability of the true path being taken is given
323   /// by Probability, and Confidence is a measure of our confidence that it
324   /// will be properly predicted.
325   virtual bool
326   isProfitableToIfCvt(MachineBasicBlock &TMBB,
327                       unsigned NumTCycles, unsigned ExtraTCycles,
328                       MachineBasicBlock &FMBB,
329                       unsigned NumFCycles, unsigned ExtraFCycles,
330                       float Probability, float Confidence) const {
331     return false;
332   }
333
334   /// isProfitableToDupForIfCvt - Return true if it's profitable for
335   /// if-converter to duplicate instructions of specified accumulated
336   /// instruction latencies in the specified MBB to enable if-conversion.
337   /// The probability of the instructions being executed is given by
338   /// Probability, and Confidence is a measure of our confidence that it
339   /// will be properly predicted.
340   virtual bool
341   isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCyles,
342                             float Probability, float Confidence) const {
343     return false;
344   }
345
346   /// copyPhysReg - Emit instructions to copy a pair of physical registers.
347   virtual void copyPhysReg(MachineBasicBlock &MBB,
348                            MachineBasicBlock::iterator MI, DebugLoc DL,
349                            unsigned DestReg, unsigned SrcReg,
350                            bool KillSrc) const {
351     assert(0 && "Target didn't implement TargetInstrInfo::copyPhysReg!");
352   }
353
354   /// storeRegToStackSlot - Store the specified register of the given register
355   /// class to the specified stack frame index. The store instruction is to be
356   /// added to the given machine basic block before the specified machine
357   /// instruction. If isKill is true, the register operand is the last use and
358   /// must be marked kill.
359   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
360                                    MachineBasicBlock::iterator MI,
361                                    unsigned SrcReg, bool isKill, int FrameIndex,
362                                    const TargetRegisterClass *RC,
363                                    const TargetRegisterInfo *TRI) const {
364   assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
365   }
366
367   /// loadRegFromStackSlot - Load the specified register of the given register
368   /// class from the specified stack frame index. The load instruction is to be
369   /// added to the given machine basic block before the specified machine
370   /// instruction.
371   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
372                                     MachineBasicBlock::iterator MI,
373                                     unsigned DestReg, int FrameIndex,
374                                     const TargetRegisterClass *RC,
375                                     const TargetRegisterInfo *TRI) const {
376   assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
377   }
378
379   /// emitFrameIndexDebugValue - Emit a target-dependent form of
380   /// DBG_VALUE encoding the address of a frame index.  Addresses would
381   /// normally be lowered the same way as other addresses on the target,
382   /// e.g. in load instructions.  For targets that do not support this
383   /// the debug info is simply lost.
384   /// If you add this for a target you should handle this DBG_VALUE in the
385   /// target-specific AsmPrinter code as well; you will probably get invalid
386   /// assembly output if you don't.
387   virtual MachineInstr *emitFrameIndexDebugValue(MachineFunction &MF,
388                                                  int FrameIx,
389                                                  uint64_t Offset,
390                                                  const MDNode *MDPtr,
391                                                  DebugLoc dl) const {
392     return 0;
393   }
394
395   /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
396   /// slot into the specified machine instruction for the specified operand(s).
397   /// If this is possible, a new instruction is returned with the specified
398   /// operand folded, otherwise NULL is returned.
399   /// The new instruction is inserted before MI, and the client is responsible
400   /// for removing the old instruction.
401   MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
402                                   const SmallVectorImpl<unsigned> &Ops,
403                                   int FrameIndex) const;
404
405   /// foldMemoryOperand - Same as the previous version except it allows folding
406   /// of any load and store from / to any address, not just from a specific
407   /// stack slot.
408   MachineInstr* foldMemoryOperand(MachineBasicBlock::iterator MI,
409                                   const SmallVectorImpl<unsigned> &Ops,
410                                   MachineInstr* LoadMI) const;
411
412 protected:
413   /// foldMemoryOperandImpl - Target-dependent implementation for
414   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
415   /// take care of adding a MachineMemOperand to the newly created instruction.
416   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
417                                           MachineInstr* MI,
418                                           const SmallVectorImpl<unsigned> &Ops,
419                                           int FrameIndex) const {
420     return 0;
421   }
422
423   /// foldMemoryOperandImpl - Target-dependent implementation for
424   /// foldMemoryOperand. Target-independent code in foldMemoryOperand will
425   /// take care of adding a MachineMemOperand to the newly created instruction.
426   virtual MachineInstr* foldMemoryOperandImpl(MachineFunction &MF,
427                                               MachineInstr* MI,
428                                           const SmallVectorImpl<unsigned> &Ops,
429                                               MachineInstr* LoadMI) const {
430     return 0;
431   }
432
433 public:
434   /// canFoldMemoryOperand - Returns true for the specified load / store if
435   /// folding is possible.
436   virtual
437   bool canFoldMemoryOperand(const MachineInstr *MI,
438                             const SmallVectorImpl<unsigned> &Ops) const =0;
439
440   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
441   /// a store or a load and a store into two or more instruction. If this is
442   /// possible, returns true as well as the new instructions by reference.
443   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
444                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
445                                  SmallVectorImpl<MachineInstr*> &NewMIs) const{
446     return false;
447   }
448
449   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
450                                    SmallVectorImpl<SDNode*> &NewNodes) const {
451     return false;
452   }
453
454   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
455   /// instruction after load / store are unfolded from an instruction of the
456   /// specified opcode. It returns zero if the specified unfolding is not
457   /// possible. If LoadRegIndex is non-null, it is filled in with the operand
458   /// index of the operand which will hold the register holding the loaded
459   /// value.
460   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
461                                       bool UnfoldLoad, bool UnfoldStore,
462                                       unsigned *LoadRegIndex = 0) const {
463     return 0;
464   }
465
466   /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
467   /// to determine if two loads are loading from the same base address. It
468   /// should only return true if the base pointers are the same and the
469   /// only differences between the two addresses are the offset. It also returns
470   /// the offsets by reference.
471   virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
472                                     int64_t &Offset1, int64_t &Offset2) const {
473     return false;
474   }
475
476   /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
477   /// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
478   /// be scheduled togther. On some targets if two loads are loading from
479   /// addresses in the same cache line, it's better if they are scheduled
480   /// together. This function takes two integers that represent the load offsets
481   /// from the common base address. It returns true if it decides it's desirable
482   /// to schedule the two loads together. "NumLoads" is the number of loads that
483   /// have already been scheduled after Load1.
484   virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
485                                        int64_t Offset1, int64_t Offset2,
486                                        unsigned NumLoads) const {
487     return false;
488   }
489
490   /// ReverseBranchCondition - Reverses the branch condition of the specified
491   /// condition list, returning false on success and true if it cannot be
492   /// reversed.
493   virtual
494   bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
495     return true;
496   }
497
498   /// insertNoop - Insert a noop into the instruction stream at the specified
499   /// point.
500   virtual void insertNoop(MachineBasicBlock &MBB,
501                           MachineBasicBlock::iterator MI) const;
502
503
504   /// getNoopForMachoTarget - Return the noop instruction to use for a noop.
505   virtual void getNoopForMachoTarget(MCInst &NopInst) const {
506     // Default to just using 'nop' string.
507   }
508
509
510   /// isPredicated - Returns true if the instruction is already predicated.
511   ///
512   virtual bool isPredicated(const MachineInstr *MI) const {
513     return false;
514   }
515
516   /// isUnpredicatedTerminator - Returns true if the instruction is a
517   /// terminator instruction that has not been predicated.
518   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
519
520   /// PredicateInstruction - Convert the instruction into a predicated
521   /// instruction. It returns true if the operation was successful.
522   virtual
523   bool PredicateInstruction(MachineInstr *MI,
524                         const SmallVectorImpl<MachineOperand> &Pred) const = 0;
525
526   /// SubsumesPredicate - Returns true if the first specified predicate
527   /// subsumes the second, e.g. GE subsumes GT.
528   virtual
529   bool SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
530                          const SmallVectorImpl<MachineOperand> &Pred2) const {
531     return false;
532   }
533
534   /// DefinesPredicate - If the specified instruction defines any predicate
535   /// or condition code register(s) used for predication, returns true as well
536   /// as the definition predicate(s) by reference.
537   virtual bool DefinesPredicate(MachineInstr *MI,
538                                 std::vector<MachineOperand> &Pred) const {
539     return false;
540   }
541
542   /// isPredicable - Return true if the specified instruction can be predicated.
543   /// By default, this returns true for every instruction with a
544   /// PredicateOperand.
545   virtual bool isPredicable(MachineInstr *MI) const {
546     return MI->getDesc().isPredicable();
547   }
548
549   /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
550   /// instruction that defines the specified register class.
551   virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
552     return true;
553   }
554
555   /// isSchedulingBoundary - Test if the given instruction should be
556   /// considered a scheduling boundary. This primarily includes labels and
557   /// terminators.
558   virtual bool isSchedulingBoundary(const MachineInstr *MI,
559                                     const MachineBasicBlock *MBB,
560                                     const MachineFunction &MF) const = 0;
561
562   /// Measure the specified inline asm to determine an approximation of its
563   /// length.
564   virtual unsigned getInlineAsmLength(const char *Str,
565                                       const MCAsmInfo &MAI) const;
566
567   /// CreateTargetPreRAHazardRecognizer - Allocate and return a hazard
568   /// recognizer to use for this target when scheduling the machine instructions
569   /// before register allocation.
570   virtual ScheduleHazardRecognizer*
571   CreateTargetHazardRecognizer(const TargetMachine *TM,
572                                const ScheduleDAG *DAG) const = 0;
573
574   /// CreateTargetPostRAHazardRecognizer - Allocate and return a hazard
575   /// recognizer to use for this target when scheduling the machine instructions
576   /// after register allocation.
577   virtual ScheduleHazardRecognizer*
578   CreateTargetPostRAHazardRecognizer(const InstrItineraryData*,
579                                      const ScheduleDAG *DAG) const = 0;
580
581   /// AnalyzeCompare - For a comparison instruction, return the source register
582   /// in SrcReg and the value it compares against in CmpValue. Return true if
583   /// the comparison instruction can be analyzed.
584   virtual bool AnalyzeCompare(const MachineInstr *MI,
585                               unsigned &SrcReg, int &Mask, int &Value) const {
586     return false;
587   }
588
589   /// OptimizeCompareInstr - See if the comparison instruction can be converted
590   /// into something more efficient. E.g., on ARM most instructions can set the
591   /// flags register, obviating the need for a separate CMP.
592   virtual bool OptimizeCompareInstr(MachineInstr *CmpInstr,
593                                     unsigned SrcReg, int Mask, int Value,
594                                     const MachineRegisterInfo *MRI) const {
595     return false;
596   }
597
598   /// FoldImmediate - 'Reg' is known to be defined by a move immediate
599   /// instruction, try to fold the immediate into the use instruction.
600   virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
601                              unsigned Reg, MachineRegisterInfo *MRI) const {
602     return false;
603   }
604
605   /// getNumMicroOps - Return the number of u-operations the given machine
606   /// instruction will be decoded to on the target cpu.
607   virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
608                                   const MachineInstr *MI) const;
609
610   /// getOperandLatency - Compute and return the use operand latency of a given
611   /// pair of def and use.
612   /// In most cases, the static scheduling itinerary was enough to determine the
613   /// operand latency. But it may not be possible for instructions with variable
614   /// number of defs / uses.
615   virtual int getOperandLatency(const InstrItineraryData *ItinData,
616                               const MachineInstr *DefMI, unsigned DefIdx,
617                               const MachineInstr *UseMI, unsigned UseIdx) const;
618
619   virtual int getOperandLatency(const InstrItineraryData *ItinData,
620                                 SDNode *DefNode, unsigned DefIdx,
621                                 SDNode *UseNode, unsigned UseIdx) const;
622
623   /// getInstrLatency - Compute the instruction latency of a given instruction.
624   /// If the instruction has higher cost when predicated, it's returned via
625   /// PredCost.
626   virtual int getInstrLatency(const InstrItineraryData *ItinData,
627                               const MachineInstr *MI,
628                               unsigned *PredCost = 0) const;
629
630   virtual int getInstrLatency(const InstrItineraryData *ItinData,
631                               SDNode *Node) const;
632
633   /// hasHighOperandLatency - Compute operand latency between a def of 'Reg'
634   /// and an use in the current loop, return true if the target considered
635   /// it 'high'. This is used by optimization passes such as machine LICM to
636   /// determine whether it makes sense to hoist an instruction out even in
637   /// high register pressure situation.
638   virtual
639   bool hasHighOperandLatency(const InstrItineraryData *ItinData,
640                              const MachineRegisterInfo *MRI,
641                              const MachineInstr *DefMI, unsigned DefIdx,
642                              const MachineInstr *UseMI, unsigned UseIdx) const {
643     return false;
644   }
645
646   /// hasLowDefLatency - Compute operand latency of a def of 'Reg', return true
647   /// if the target considered it 'low'.
648   virtual
649   bool hasLowDefLatency(const InstrItineraryData *ItinData,
650                         const MachineInstr *DefMI, unsigned DefIdx) const;
651 };
652
653 /// TargetInstrInfoImpl - This is the default implementation of
654 /// TargetInstrInfo, which just provides a couple of default implementations
655 /// for various methods.  This separated out because it is implemented in
656 /// libcodegen, not in libtarget.
657 class TargetInstrInfoImpl : public TargetInstrInfo {
658 protected:
659   TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
660   : TargetInstrInfo(desc, NumOpcodes) {}
661 public:
662   virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
663                                        MachineBasicBlock *NewDest) const;
664   virtual MachineInstr *commuteInstruction(MachineInstr *MI,
665                                            bool NewMI = false) const;
666   virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
667                                      unsigned &SrcOpIdx2) const;
668   virtual bool canFoldMemoryOperand(const MachineInstr *MI,
669                                     const SmallVectorImpl<unsigned> &Ops) const;
670   virtual bool PredicateInstruction(MachineInstr *MI,
671                             const SmallVectorImpl<MachineOperand> &Pred) const;
672   virtual void reMaterialize(MachineBasicBlock &MBB,
673                              MachineBasicBlock::iterator MI,
674                              unsigned DestReg, unsigned SubReg,
675                              const MachineInstr *Orig,
676                              const TargetRegisterInfo &TRI) const;
677   virtual MachineInstr *duplicate(MachineInstr *Orig,
678                                   MachineFunction &MF) const;
679   virtual bool produceSameValue(const MachineInstr *MI0,
680                                 const MachineInstr *MI1) const;
681   virtual bool isSchedulingBoundary(const MachineInstr *MI,
682                                     const MachineBasicBlock *MBB,
683                                     const MachineFunction &MF) const;
684
685   virtual ScheduleHazardRecognizer *
686   CreateTargetHazardRecognizer(const TargetMachine*, const ScheduleDAG*) const;
687
688   virtual ScheduleHazardRecognizer *
689   CreateTargetPostRAHazardRecognizer(const InstrItineraryData*,
690                                      const ScheduleDAG*) const;
691 };
692
693 } // End llvm namespace
694
695 #endif