d6acc635a33d98a90c110e19f1bd9ef8b625fa3f
[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 instructions 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 TargetRegisterClass;
23 class LiveVariables;
24 class CalleeSavedInfo;
25 class SDNode;
26 class SelectionDAG;
27
28 template<class T> class SmallVectorImpl;
29
30
31 //---------------------------------------------------------------------------
32 ///
33 /// TargetInstrInfo - Interface to description of machine instructions
34 ///
35 class TargetInstrInfo {
36   const TargetInstrDesc *Descriptors; // Raw array to allow static init'n
37   unsigned NumOpcodes;                // Number of entries in the desc array
38
39   TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
40   void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
41 public:
42   TargetInstrInfo(const TargetInstrDesc *desc, unsigned NumOpcodes);
43   virtual ~TargetInstrInfo();
44
45   // Invariant opcodes: All instruction sets have these as their low opcodes.
46   enum { 
47     PHI = 0,
48     INLINEASM = 1,
49     LABEL = 2,
50     DECLARE = 3,
51     EXTRACT_SUBREG = 4,
52     INSERT_SUBREG = 5,
53     IMPLICIT_DEF = 6
54   };
55   
56   // Target independent implict values for use with subreg insert. All targets
57   // that support insert_subreg support IMPL_VAL_UNDEF. Support for the other 
58   // values is target dependent.
59   enum ImplictVal {
60     IMPL_VAL_UNDEF = 0,
61     IMPL_VAL_ZERO  = 1,
62     LAST_IMPL_VAL  = 3
63   };
64
65   unsigned getNumOpcodes() const { return NumOpcodes; }
66
67   /// get - Return the machine instruction descriptor that corresponds to the
68   /// specified instruction opcode.
69   ///
70   const TargetInstrDesc &get(unsigned Opcode) const {
71     assert(Opcode < NumOpcodes && "Invalid opcode!");
72     return Descriptors[Opcode];
73   }
74
75   /// isTriviallyReMaterializable - Return true if the instruction is trivially
76   /// rematerializable, meaning it has no side effects and requires no operands
77   /// that aren't always available.
78   bool isTriviallyReMaterializable(MachineInstr *MI) const {
79     return MI->getDesc().isRematerializable() &&
80            isReallyTriviallyReMaterializable(MI);
81   }
82
83 protected:
84   /// isReallyTriviallyReMaterializable - For instructions with opcodes for
85   /// which the M_REMATERIALIZABLE flag is set, this function tests whether the
86   /// instruction itself is actually trivially rematerializable, considering
87   /// its operands.  This is used for targets that have instructions that are
88   /// only trivially rematerializable for specific uses.  This predicate must
89   /// return false if the instruction has any side effects other than
90   /// producing a value, or if it requres any address registers that are not
91   /// always available.
92   virtual bool isReallyTriviallyReMaterializable(MachineInstr *MI) const {
93     return true;
94   }
95
96 public:
97   /// Return true if the instruction is a register to register move
98   /// and leave the source and dest operands in the passed parameters.
99   virtual bool isMoveInstr(const MachineInstr& MI,
100                            unsigned& sourceReg,
101                            unsigned& destReg) const {
102     return false;
103   }
104   
105   /// isLoadFromStackSlot - If the specified machine instruction is a direct
106   /// load from a stack slot, return the virtual or physical register number of
107   /// the destination along with the FrameIndex of the loaded stack slot.  If
108   /// not, return 0.  This predicate must return 0 if the instruction has
109   /// any side effects other than loading from the stack slot.
110   virtual unsigned isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
111     return 0;
112   }
113   
114   /// isStoreToStackSlot - If the specified machine instruction is a direct
115   /// store to a stack slot, return the virtual or physical register number of
116   /// the source reg along with the FrameIndex of the loaded stack slot.  If
117   /// not, return 0.  This predicate must return 0 if the instruction has
118   /// any side effects other than storing to the stack slot.
119   virtual unsigned isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
120     return 0;
121   }
122
123   /// isInvariantLoad - Return true if the specified instruction (which is
124   /// marked mayLoad) is loading from a location whose value is invariant across
125   /// the function.  For example, loading a value from the constant pool or from
126   /// from the argument area of a function if it does not change.  This should
127   /// only return true of *all* loads the instruction does are invariant (if it
128   /// does multiple loads).
129   virtual bool isInvariantLoad(MachineInstr *MI) const {
130     return false;
131   }
132   
133   /// convertToThreeAddress - This method must be implemented by targets that
134   /// set the M_CONVERTIBLE_TO_3_ADDR flag.  When this flag is set, the target
135   /// may be able to convert a two-address instruction into one or more true
136   /// three-address instructions on demand.  This allows the X86 target (for
137   /// example) to convert ADD and SHL instructions into LEA instructions if they
138   /// would require register copies due to two-addressness.
139   ///
140   /// This method returns a null pointer if the transformation cannot be
141   /// performed, otherwise it returns the last new instruction.
142   ///
143   virtual MachineInstr *
144   convertToThreeAddress(MachineFunction::iterator &MFI,
145                    MachineBasicBlock::iterator &MBBI, LiveVariables &LV) const {
146     return 0;
147   }
148
149   /// commuteInstruction - If a target has any instructions that are commutable,
150   /// but require converting to a different instruction or making non-trivial
151   /// changes to commute them, this method can overloaded to do this.  The
152   /// default implementation of this method simply swaps the first two operands
153   /// of MI and returns it.
154   ///
155   /// If a target wants to make more aggressive changes, they can construct and
156   /// return a new machine instruction.  If an instruction cannot commute, it
157   /// can also return null.
158   ///
159   virtual MachineInstr *commuteInstruction(MachineInstr *MI) const = 0;
160
161   /// CommuteChangesDestination - Return true if commuting the specified
162   /// instruction will also changes the destination operand. Also return the
163   /// current operand index of the would be new destination register by
164   /// reference. This can happen when the commutable instruction is also a
165   /// two-address instruction.
166   virtual bool CommuteChangesDestination(MachineInstr *MI,
167                                          unsigned &OpIdx) const = 0;
168
169   /// AnalyzeBranch - Analyze the branching code at the end of MBB, returning
170   /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
171   /// implemented for a target).  Upon success, this returns false and returns
172   /// with the following information in various cases:
173   ///
174   /// 1. If this block ends with no branches (it just falls through to its succ)
175   ///    just return false, leaving TBB/FBB null.
176   /// 2. If this block ends with only an unconditional branch, it sets TBB to be
177   ///    the destination block.
178   /// 3. If this block ends with an conditional branch and it falls through to
179   ///    an successor block, it sets TBB to be the branch destination block and a
180   ///    list of operands that evaluate the condition. These
181   ///    operands can be passed to other TargetInstrInfo methods to create new
182   ///    branches.
183   /// 4. If this block ends with an conditional branch and an unconditional
184   ///    block, it returns the 'true' destination in TBB, the 'false' destination
185   ///    in FBB, and a list of operands that evaluate the condition. These
186   ///    operands can be passed to other TargetInstrInfo methods to create new
187   ///    branches.
188   ///
189   /// Note that RemoveBranch and InsertBranch must be implemented to support
190   /// cases where this method returns success.
191   ///
192   virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
193                              MachineBasicBlock *&FBB,
194                              std::vector<MachineOperand> &Cond) const {
195     return true;
196   }
197   
198   /// RemoveBranch - Remove the branching code at the end of the specific MBB.
199   /// this is only invoked in cases where AnalyzeBranch returns success. It
200   /// returns the number of instructions that were removed.
201   virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
202     assert(0 && "Target didn't implement TargetInstrInfo::RemoveBranch!"); 
203     return 0;
204   }
205   
206   /// InsertBranch - Insert a branch into the end of the specified
207   /// MachineBasicBlock.  This operands to this method are the same as those
208   /// returned by AnalyzeBranch.  This is invoked in cases where AnalyzeBranch
209   /// returns success and when an unconditional branch (TBB is non-null, FBB is
210   /// null, Cond is empty) needs to be inserted. It returns the number of
211   /// instructions inserted.
212   virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
213                             MachineBasicBlock *FBB,
214                             const std::vector<MachineOperand> &Cond) const {
215     assert(0 && "Target didn't implement TargetInstrInfo::InsertBranch!"); 
216     return 0;
217   }
218   
219   /// copyRegToReg - Add a copy between a pair of registers
220   virtual void copyRegToReg(MachineBasicBlock &MBB,
221                             MachineBasicBlock::iterator MI,
222                             unsigned DestReg, unsigned SrcReg,
223                             const TargetRegisterClass *DestRC,
224                             const TargetRegisterClass *SrcRC) const {
225     assert(0 && "Target didn't implement TargetInstrInfo::copyRegToReg!");
226   }
227   
228   virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
229                                    MachineBasicBlock::iterator MI,
230                                    unsigned SrcReg, bool isKill, int FrameIndex,
231                                    const TargetRegisterClass *RC) const {
232     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToStackSlot!");
233   }
234
235   virtual void storeRegToAddr(MachineFunction &MF, unsigned SrcReg, bool isKill,
236                               SmallVectorImpl<MachineOperand> &Addr,
237                               const TargetRegisterClass *RC,
238                               SmallVectorImpl<MachineInstr*> &NewMIs) const {
239     assert(0 && "Target didn't implement TargetInstrInfo::storeRegToAddr!");
240   }
241
242   virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
243                                     MachineBasicBlock::iterator MI,
244                                     unsigned DestReg, int FrameIndex,
245                                     const TargetRegisterClass *RC) const {
246     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromStackSlot!");
247   }
248
249   virtual void loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
250                                SmallVectorImpl<MachineOperand> &Addr,
251                                const TargetRegisterClass *RC,
252                                SmallVectorImpl<MachineInstr*> &NewMIs) const {
253     assert(0 && "Target didn't implement TargetInstrInfo::loadRegFromAddr!");
254   }
255   
256   /// spillCalleeSavedRegisters - Issues instruction(s) to spill all callee
257   /// saved registers and returns true if it isn't possible / profitable to do
258   /// so by issuing a series of store instructions via
259   /// storeRegToStackSlot(). Returns false otherwise.
260   virtual bool spillCalleeSavedRegisters(MachineBasicBlock &MBB,
261                                          MachineBasicBlock::iterator MI,
262                                 const std::vector<CalleeSavedInfo> &CSI) const {
263     return false;
264   }
265
266   /// restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee
267   /// saved registers and returns true if it isn't possible / profitable to do
268   /// so by issuing a series of load instructions via loadRegToStackSlot().
269   /// Returns false otherwise.
270   virtual bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
271                                            MachineBasicBlock::iterator MI,
272                                 const std::vector<CalleeSavedInfo> &CSI) const {
273     return false;
274   }
275   
276   /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
277   /// slot into the specified machine instruction for the specified operand(s).
278   /// If this is possible, a new instruction is returned with the specified
279   /// operand folded, otherwise NULL is returned. The client is responsible for
280   /// removing the old instruction and adding the new one in the instruction
281   /// stream.
282   virtual MachineInstr* foldMemoryOperand(MachineFunction &MF,
283                                           MachineInstr* MI,
284                                           SmallVectorImpl<unsigned> &Ops,
285                                           int FrameIndex) const {
286     return 0;
287   }
288
289   /// foldMemoryOperand - Same as the previous version except it allows folding
290   /// of any load and store from / to any address, not just from a specific
291   /// stack slot.
292   virtual MachineInstr* foldMemoryOperand(MachineFunction &MF,
293                                           MachineInstr* MI,
294                                           SmallVectorImpl<unsigned> &Ops,
295                                           MachineInstr* LoadMI) const {
296     return 0;
297   }
298
299   /// canFoldMemoryOperand - Returns true if the specified load / store is
300   /// folding is possible.
301   virtual
302   bool canFoldMemoryOperand(MachineInstr *MI,
303                             SmallVectorImpl<unsigned> &Ops) const{
304     return false;
305   }
306
307   /// unfoldMemoryOperand - Separate a single instruction which folded a load or
308   /// a store or a load and a store into two or more instruction. If this is
309   /// possible, returns true as well as the new instructions by reference.
310   virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
311                                 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
312                                   SmallVectorImpl<MachineInstr*> &NewMIs) const{
313     return false;
314   }
315
316   virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
317                                    SmallVectorImpl<SDNode*> &NewNodes) const {
318     return false;
319   }
320
321   /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
322   /// instruction after load / store are unfolded from an instruction of the
323   /// specified opcode. It returns zero if the specified unfolding is not
324   /// possible.
325   virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
326                                       bool UnfoldLoad, bool UnfoldStore) const {
327     return 0;
328   }
329   
330   /// BlockHasNoFallThrough - Return true if the specified block does not
331   /// fall-through into its successor block.  This is primarily used when a
332   /// branch is unanalyzable.  It is useful for things like unconditional
333   /// indirect branches (jump tables).
334   virtual bool BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
335     return false;
336   }
337   
338   /// ReverseBranchCondition - Reverses the branch condition of the specified
339   /// condition list, returning false on success and true if it cannot be
340   /// reversed.
341   virtual bool ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
342     return true;
343   }
344   
345   /// insertNoop - Insert a noop into the instruction stream at the specified
346   /// point.
347   virtual void insertNoop(MachineBasicBlock &MBB, 
348                           MachineBasicBlock::iterator MI) const {
349     assert(0 && "Target didn't implement insertNoop!");
350     abort();
351   }
352
353   /// isPredicated - Returns true if the instruction is already predicated.
354   ///
355   virtual bool isPredicated(const MachineInstr *MI) const {
356     return false;
357   }
358
359   /// isUnpredicatedTerminator - Returns true if the instruction is a
360   /// terminator instruction that has not been predicated.
361   virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
362
363   /// PredicateInstruction - Convert the instruction into a predicated
364   /// instruction. It returns true if the operation was successful.
365   virtual
366   bool PredicateInstruction(MachineInstr *MI,
367                             const std::vector<MachineOperand> &Pred) const = 0;
368
369   /// SubsumesPredicate - Returns true if the first specified predicate
370   /// subsumes the second, e.g. GE subsumes GT.
371   virtual
372   bool SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
373                          const std::vector<MachineOperand> &Pred2) const {
374     return false;
375   }
376
377   /// DefinesPredicate - If the specified instruction defines any predicate
378   /// or condition code register(s) used for predication, returns true as well
379   /// as the definition predicate(s) by reference.
380   virtual bool DefinesPredicate(MachineInstr *MI,
381                                 std::vector<MachineOperand> &Pred) const {
382     return false;
383   }
384
385   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
386   /// values.
387   virtual const TargetRegisterClass *getPointerRegClass() const {
388     assert(0 && "Target didn't implement getPointerRegClass!");
389     abort();
390     return 0; // Must return a value in order to compile with VS 2005
391   }
392 };
393
394 /// TargetInstrInfoImpl - This is the default implementation of
395 /// TargetInstrInfo, which just provides a couple of default implementations
396 /// for various methods.  This separated out because it is implemented in
397 /// libcodegen, not in libtarget.
398 class TargetInstrInfoImpl : public TargetInstrInfo {
399 protected:
400   TargetInstrInfoImpl(const TargetInstrDesc *desc, unsigned NumOpcodes)
401   : TargetInstrInfo(desc, NumOpcodes) {}
402 public:
403   virtual MachineInstr *commuteInstruction(MachineInstr *MI) const;
404   virtual bool CommuteChangesDestination(MachineInstr *MI,
405                                          unsigned &OpIdx) const;
406   virtual bool PredicateInstruction(MachineInstr *MI,
407                               const std::vector<MachineOperand> &Pred) const;
408   
409 };
410
411 } // End llvm namespace
412
413 #endif