cd0a3edb2bd4b76e36f4f699608e05cf35c712ee
[oota-llvm.git] / include / llvm / Target / TargetInstrInfo.h
1 //===-- llvm/Target/InstrInfo.h - Target Instruction Information --*-C++-*-==//
2 //
3 // This file describes the target machine instructions to the code generator.
4 //
5 //===---------------------------------------------------------------------===//
6
7 #ifndef LLVM_TARGET_MACHINEINSTRINFO_H
8 #define LLVM_TARGET_MACHINEINSTRINFO_H
9
10 #include "Support/DataTypes.h"
11 #include <vector>
12
13 class MachineInstrDescriptor;
14 class MachineInstr;
15 class TargetMachine;
16 class Value;
17 class Instruction;
18 class Constant;
19 class Function;
20 class MachineCodeForInstruction;
21
22 //---------------------------------------------------------------------------
23 // Data types used to define information about a single machine instruction
24 //---------------------------------------------------------------------------
25
26 typedef int MachineOpCode;
27 typedef unsigned InstrSchedClass;
28
29 const MachineOpCode INVALID_MACHINE_OPCODE = -1;
30
31
32 // Global variable holding an array of descriptors for machine instructions.
33 // The actual object needs to be created separately for each target machine.
34 // This variable is initialized and reset by class MachineInstrInfo.
35 // 
36 // FIXME: This should be a property of the target so that more than one target
37 // at a time can be active...
38 //
39 extern const MachineInstrDescriptor *TargetInstrDescriptors;
40
41
42 //---------------------------------------------------------------------------
43 // struct MachineInstrDescriptor:
44 //      Predefined information about each machine instruction.
45 //      Designed to initialized statically.
46 // 
47 // class MachineInstructionInfo
48 //      Interface to description of machine instructions
49 // 
50 //---------------------------------------------------------------------------
51
52 const unsigned  M_NOP_FLAG              = 1 << 0;
53 const unsigned  M_BRANCH_FLAG           = 1 << 1;
54 const unsigned  M_CALL_FLAG             = 1 << 2;
55 const unsigned  M_RET_FLAG              = 1 << 3;
56 const unsigned  M_ARITH_FLAG            = 1 << 4;
57 const unsigned  M_CC_FLAG               = 1 << 6;
58 const unsigned  M_LOGICAL_FLAG          = 1 << 6;
59 const unsigned  M_INT_FLAG              = 1 << 7;
60 const unsigned  M_FLOAT_FLAG            = 1 << 8;
61 const unsigned  M_CONDL_FLAG            = 1 << 9;
62 const unsigned  M_LOAD_FLAG             = 1 << 10;
63 const unsigned  M_PREFETCH_FLAG         = 1 << 11;
64 const unsigned  M_STORE_FLAG            = 1 << 12;
65 const unsigned  M_DUMMY_PHI_FLAG        = 1 << 13;
66 const unsigned  M_PSEUDO_FLAG           = 1 << 14;
67
68
69 struct MachineInstrDescriptor {
70   const char *    opCodeString;  // Assembly language mnemonic for the opcode.
71   int             numOperands;   // Number of args; -1 if variable #args
72   int             resultPos;     // Position of the result; -1 if no result
73   unsigned        maxImmedConst; // Largest +ve constant in IMMMED field or 0.
74   bool            immedIsSignExtended; // Is IMMED field sign-extended? If so,
75                                  //   smallest -ve value is -(maxImmedConst+1).
76   unsigned        numDelaySlots; // Number of delay slots after instruction
77   unsigned        latency;       // Latency in machine cycles
78   InstrSchedClass schedClass;    // enum  identifying instr sched class
79   unsigned        iclass;        // flags identifying machine instr class
80 };
81
82
83 class MachineInstrInfo {
84 public:
85   const TargetMachine& target;
86
87 protected:
88   const MachineInstrDescriptor* desc;   // raw array to allow static init'n
89   unsigned descSize;            // number of entries in the desc array
90   unsigned numRealOpCodes;              // number of non-dummy op codes
91   
92   MachineInstrInfo(const MachineInstrInfo &); // DO NOT IMPLEMENT
93   void operator=(const MachineInstrInfo &);   // DO NOT IMPLEMENT
94 public:
95   MachineInstrInfo(const TargetMachine& tgt,
96                    const MachineInstrDescriptor *desc, unsigned descSize,
97                    unsigned numRealOpCodes);
98   virtual ~MachineInstrInfo();
99   
100   unsigned getNumRealOpCodes()  const { return numRealOpCodes; }
101   unsigned getNumTotalOpCodes() const { return descSize; }
102   
103   const MachineInstrDescriptor& getDescriptor(MachineOpCode opCode) const {
104     assert(opCode >= 0 && opCode < (int)descSize);
105     return desc[opCode];
106   }
107   
108   int getNumOperands(MachineOpCode opCode) const {
109     return getDescriptor(opCode).numOperands;
110   }
111   
112   int getResultPos(MachineOpCode opCode) const {
113     return getDescriptor(opCode).resultPos;
114   }
115   
116   unsigned getNumDelaySlots(MachineOpCode opCode) const {
117     return getDescriptor(opCode).numDelaySlots;
118   }
119   
120   InstrSchedClass getSchedClass(MachineOpCode opCode) const {
121     return getDescriptor(opCode).schedClass;
122   }
123   
124   //
125   // Query instruction class flags according to the machine-independent
126   // flags listed above.
127   // 
128   unsigned getIClass(MachineOpCode opCode) const {
129     return getDescriptor(opCode).iclass;
130   }
131   bool isNop(MachineOpCode opCode) const {
132     return getDescriptor(opCode).iclass & M_NOP_FLAG;
133   }
134   bool isBranch(MachineOpCode opCode) const {
135     return getDescriptor(opCode).iclass & M_BRANCH_FLAG;
136   }
137   bool isCall(MachineOpCode opCode) const {
138     return getDescriptor(opCode).iclass & M_CALL_FLAG;
139   }
140   bool isReturn(MachineOpCode opCode) const {
141     return getDescriptor(opCode).iclass & M_RET_FLAG;
142   }
143   bool isControlFlow(MachineOpCode opCode) const {
144     return getDescriptor(opCode).iclass & M_BRANCH_FLAG
145         || getDescriptor(opCode).iclass & M_CALL_FLAG
146         || getDescriptor(opCode).iclass & M_RET_FLAG;
147   }
148   bool isArith(MachineOpCode opCode) const {
149     return getDescriptor(opCode).iclass & M_ARITH_FLAG;
150   }
151   bool isCCInstr(MachineOpCode opCode) const {
152     return getDescriptor(opCode).iclass & M_CC_FLAG;
153   }
154   bool isLogical(MachineOpCode opCode) const {
155     return getDescriptor(opCode).iclass & M_LOGICAL_FLAG;
156   }
157   bool isIntInstr(MachineOpCode opCode) const {
158     return getDescriptor(opCode).iclass & M_INT_FLAG;
159   }
160   bool isFloatInstr(MachineOpCode opCode) const {
161     return getDescriptor(opCode).iclass & M_FLOAT_FLAG;
162   }
163   bool isConditional(MachineOpCode opCode) const { 
164     return getDescriptor(opCode).iclass & M_CONDL_FLAG;
165   }
166   bool isLoad(MachineOpCode opCode) const {
167     return getDescriptor(opCode).iclass & M_LOAD_FLAG;
168   }
169   bool isPrefetch(MachineOpCode opCode) const {
170     return getDescriptor(opCode).iclass & M_PREFETCH_FLAG;
171   }
172   bool isLoadOrPrefetch(MachineOpCode opCode) const {
173     return getDescriptor(opCode).iclass & M_LOAD_FLAG
174         || getDescriptor(opCode).iclass & M_PREFETCH_FLAG;
175   }
176   bool isStore(MachineOpCode opCode) const {
177     return getDescriptor(opCode).iclass & M_STORE_FLAG;
178   }
179   bool isMemoryAccess(MachineOpCode opCode) const {
180     return getDescriptor(opCode).iclass & M_LOAD_FLAG
181         || getDescriptor(opCode).iclass & M_PREFETCH_FLAG
182         || getDescriptor(opCode).iclass & M_STORE_FLAG;
183   }
184   bool isDummyPhiInstr(const MachineOpCode opCode) const {
185     return getDescriptor(opCode).iclass & M_DUMMY_PHI_FLAG;
186   }
187   bool isPseudoInstr(const MachineOpCode opCode) const {
188     return getDescriptor(opCode).iclass & M_PSEUDO_FLAG;
189   }
190
191   // Check if an instruction can be issued before its operands are ready,
192   // or if a subsequent instruction that uses its result can be issued
193   // before the results are ready.
194   // Default to true since most instructions on many architectures allow this.
195   // 
196   virtual bool hasOperandInterlock(MachineOpCode opCode) const {
197     return true;
198   }
199   
200   virtual bool hasResultInterlock(MachineOpCode opCode) const {
201     return true;
202   }
203   
204   // 
205   // Latencies for individual instructions and instruction pairs
206   // 
207   virtual int minLatency(MachineOpCode opCode) const {
208     return getDescriptor(opCode).latency;
209   }
210   
211   virtual int maxLatency(MachineOpCode opCode) const {
212     return getDescriptor(opCode).latency;
213   }
214
215   //
216   // Which operand holds an immediate constant?  Returns -1 if none
217   // 
218   virtual int getImmedConstantPos(MachineOpCode opCode) const {
219     return -1; // immediate position is machine specific, so say -1 == "none"
220   }
221   
222   // Check if the specified constant fits in the immediate field
223   // of this machine instruction
224   // 
225   virtual bool constantFitsInImmedField(MachineOpCode opCode,
226                                         int64_t intValue) const;
227   
228   // Return the largest +ve constant that can be held in the IMMMED field
229   // of this machine instruction.
230   // isSignExtended is set to true if the value is sign-extended before use
231   // (this is true for all immediate fields in SPARC instructions).
232   // Return 0 if the instruction has no IMMED field.
233   // 
234   virtual uint64_t maxImmedConstant(MachineOpCode opCode,
235                                     bool &isSignExtended) const {
236     isSignExtended = getDescriptor(opCode).immedIsSignExtended;
237     return getDescriptor(opCode).maxImmedConst;
238   }
239
240   //-------------------------------------------------------------------------
241   // Queries about representation of LLVM quantities (e.g., constants)
242   //-------------------------------------------------------------------------
243
244   /// ConstantTypeMustBeLoaded - Test if this type of constant must be loaded
245   /// from memory into a register, i.e., cannot be set bitwise in register and
246   /// cannot use immediate fields of instructions.  Note that this only makes
247   /// sense for primitive types.
248   ///
249   virtual bool ConstantTypeMustBeLoaded(const Constant* CV) const;
250
251   // Test if this constant may not fit in the immediate field of the
252   // machine instructions (probably) generated for this instruction.
253   // 
254   virtual bool ConstantMayNotFitInImmedField(const Constant* CV,
255                                              const Instruction* I) const {
256     return true;                        // safe but very conservative
257   }
258
259   //-------------------------------------------------------------------------
260   // Code generation support for creating individual machine instructions
261   //-------------------------------------------------------------------------
262
263   // Get certain common op codes for the current target.  this and all the
264   // Create* methods below should be moved to a machine code generation class
265   // 
266   virtual MachineOpCode getNOPOpCode() const = 0;
267
268   // Create an instruction sequence to put the constant `val' into
269   // the virtual register `dest'.  `val' may be a Constant or a
270   // GlobalValue, viz., the constant address of a global variable or function.
271   // The generated instructions are returned in `mvec'.
272   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
273   // Symbolic constants or constants that must be accessed from memory
274   // are added to the constant pool via MachineFunction::get(F).
275   // 
276   virtual void  CreateCodeToLoadConst(const TargetMachine& target,
277                                       Function* F,
278                                       Value* val,
279                                       Instruction* dest,
280                                       std::vector<MachineInstr*>& mvec,
281                                       MachineCodeForInstruction& mcfi) const=0;
282   
283   // Create an instruction sequence to copy an integer value `val'
284   // to a floating point value `dest' by copying to memory and back.
285   // val must be an integral type.  dest must be a Float or Double.
286   // The generated instructions are returned in `mvec'.
287   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
288   // Any stack space required is allocated via mcff.
289   // 
290   virtual void  CreateCodeToCopyIntToFloat(const TargetMachine& target,
291                                        Function* F,
292                                        Value* val,
293                                        Instruction* dest,
294                                        std::vector<MachineInstr*>& mvec,
295                                        MachineCodeForInstruction& mcfi)const=0;
296
297   // Similarly, create an instruction sequence to copy an FP value
298   // `val' to an integer value `dest' by copying to memory and back.
299   // The generated instructions are returned in `mvec'.
300   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
301   // Any stack space required is allocated via mcff.
302   // 
303   virtual void  CreateCodeToCopyFloatToInt(const TargetMachine& target,
304                                        Function* F,
305                                        Value* val,
306                                        Instruction* dest,
307                                        std::vector<MachineInstr*>& mvec,
308                                        MachineCodeForInstruction& mcfi)const=0;
309   
310   // Create instruction(s) to copy src to dest, for arbitrary types
311   // The generated instructions are returned in `mvec'.
312   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
313   // Any stack space required is allocated via mcff.
314   // 
315   virtual void CreateCopyInstructionsByType(const TargetMachine& target,
316                                        Function* F,
317                                        Value* src,
318                                        Instruction* dest,
319                                        std::vector<MachineInstr*>& mvec,
320                                        MachineCodeForInstruction& mcfi)const=0;
321
322   // Create instruction sequence to produce a sign-extended register value
323   // from an arbitrary sized value (sized in bits, not bytes).
324   // The generated instructions are appended to `mvec'.
325   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
326   // Any stack space required is allocated via mcff.
327   // 
328   virtual void CreateSignExtensionInstructions(const TargetMachine& target,
329                                        Function* F,
330                                        Value* srcVal,
331                                        Value* destVal,
332                                        unsigned numLowBits,
333                                        std::vector<MachineInstr*>& mvec,
334                                        MachineCodeForInstruction& mcfi) const=0;
335
336   // Create instruction sequence to produce a zero-extended register value
337   // from an arbitrary sized value (sized in bits, not bytes).
338   // The generated instructions are appended to `mvec'.
339   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
340   // Any stack space required is allocated via mcff.
341   // 
342   virtual void CreateZeroExtensionInstructions(const TargetMachine& target,
343                                        Function* F,
344                                        Value* srcVal,
345                                        Value* destVal,
346                                        unsigned srcSizeInBits,
347                                        std::vector<MachineInstr*>& mvec,
348                                        MachineCodeForInstruction& mcfi) const=0;
349 };
350
351 #endif