Scrap a huge layer of cruft out of this interface.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Support/DataTypes.h"
18 #include <vector>
19 #include <cassert>
20
21 namespace llvm {
22
23 class MachineInstr;
24 class TargetMachine;
25 class Value;
26 class Type;
27 class Instruction;
28 class Constant;
29 class Function;
30 class MachineCodeForInstruction;
31
32 //---------------------------------------------------------------------------
33 // Data types used to define information about a single machine instruction
34 //---------------------------------------------------------------------------
35
36 typedef short MachineOpCode;
37 typedef unsigned InstrSchedClass;
38
39 //---------------------------------------------------------------------------
40 // struct TargetInstrDescriptor:
41 //      Predefined information about each machine instruction.
42 //      Designed to initialized statically.
43 //
44
45 const unsigned M_NOP_FLAG               = 1 << 0;
46 const unsigned M_BRANCH_FLAG            = 1 << 1;
47 const unsigned M_CALL_FLAG              = 1 << 2;
48 const unsigned M_RET_FLAG               = 1 << 3;
49 const unsigned M_CC_FLAG                = 1 << 6;
50 const unsigned M_LOAD_FLAG              = 1 << 10;
51 const unsigned M_STORE_FLAG             = 1 << 12;
52 const unsigned M_DUMMY_PHI_FLAG = 1 << 13;
53 const unsigned M_PSEUDO_FLAG           = 1 << 14;       // Pseudo instruction
54 // 3-addr instructions which really work like 2-addr ones, eg. X86 add/sub
55 const unsigned M_2_ADDR_FLAG           = 1 << 15;
56
57 // M_TERMINATOR_FLAG - Is this instruction part of the terminator for a basic
58 // block?  Typically this is things like return and branch instructions.
59 // Various passes use this to insert code into the bottom of a basic block, but
60 // before control flow occurs.
61 const unsigned M_TERMINATOR_FLAG       = 1 << 16;
62
63 struct TargetInstrDescriptor {
64   const char *    Name;          // Assembly language mnemonic for the opcode.
65   int             numOperands;   // Number of args; -1 if variable #args
66   int             resultPos;     // Position of the result; -1 if no result
67   unsigned        maxImmedConst; // Largest +ve constant in IMMED field or 0.
68   bool            immedIsSignExtended; // Is IMMED field sign-extended? If so,
69                                  //   smallest -ve value is -(maxImmedConst+1).
70   unsigned        numDelaySlots; // Number of delay slots after instruction
71   unsigned        latency;       // Latency in machine cycles
72   InstrSchedClass schedClass;    // enum  identifying instr sched class
73   unsigned        Flags;         // flags identifying machine instr class
74   unsigned        TSFlags;       // Target Specific Flag values
75   const unsigned *ImplicitUses;  // Registers implicitly read by this instr
76   const unsigned *ImplicitDefs;  // Registers implicitly defined by this instr
77 };
78
79
80 //---------------------------------------------------------------------------
81 /// 
82 /// TargetInstrInfo - Interface to description of machine instructions
83 /// 
84 class TargetInstrInfo {
85   const TargetInstrDescriptor* desc;    // raw array to allow static init'n
86   unsigned descSize;                    // number of entries in the desc array
87   unsigned numRealOpCodes;              // number of non-dummy op codes
88   
89   TargetInstrInfo(const TargetInstrInfo &);  // DO NOT IMPLEMENT
90   void operator=(const TargetInstrInfo &);   // DO NOT IMPLEMENT
91 public:
92   TargetInstrInfo(const TargetInstrDescriptor *desc, unsigned descSize,
93                   unsigned numRealOpCodes);
94   virtual ~TargetInstrInfo();
95
96   // Invariant: All instruction sets use opcode #0 as the PHI instruction
97   enum { PHI = 0 };
98   
99   unsigned getNumRealOpCodes()  const { return numRealOpCodes; }
100   unsigned getNumTotalOpCodes() const { return descSize; }
101   
102   /// get - Return the machine instruction descriptor that corresponds to the
103   /// specified instruction opcode.
104   ///
105   const TargetInstrDescriptor& get(MachineOpCode opCode) const {
106     assert(opCode >= 0 && opCode < (int)descSize);
107     return desc[opCode];
108   }
109
110   const char *getName(MachineOpCode opCode) const {
111     return get(opCode).Name;
112   }
113   
114   int getNumOperands(MachineOpCode opCode) const {
115     return get(opCode).numOperands;
116   }
117
118
119   InstrSchedClass getSchedClass(MachineOpCode opCode) const {
120     return get(opCode).schedClass;
121   }
122
123   const unsigned *getImplicitUses(MachineOpCode opCode) const {
124     return get(opCode).ImplicitUses;
125   }
126
127   const unsigned *getImplicitDefs(MachineOpCode opCode) const {
128     return get(opCode).ImplicitDefs;
129   }
130
131
132   //
133   // Query instruction class flags according to the machine-independent
134   // flags listed above.
135   // 
136   bool isReturn(MachineOpCode opCode) const {
137     return get(opCode).Flags & M_RET_FLAG;
138   }
139
140   bool isPseudoInstr(MachineOpCode opCode) const {
141     return get(opCode).Flags & M_PSEUDO_FLAG;
142   }
143   bool isTwoAddrInstr(MachineOpCode opCode) const {
144     return get(opCode).Flags & M_2_ADDR_FLAG;
145   }
146   bool isTerminatorInstr(unsigned Opcode) const {
147     return get(Opcode).Flags & M_TERMINATOR_FLAG;
148   }
149
150   //
151   // Return true if the instruction is a register to register move and
152   // leave the source and dest operands in the passed parameters.
153   //
154   virtual bool isMoveInstr(const MachineInstr& MI,
155                            unsigned& sourceReg,
156                            unsigned& destReg) const {
157     return false;
158   }
159
160
161
162
163   //-------------------------------------------------------------------------
164   // Code generation support for creating individual machine instructions
165   //
166   // WARNING: These methods are Sparc specific
167   //
168   // DO NOT USE ANY OF THESE METHODS THEY ARE DEPRECATED!
169   //
170   //-------------------------------------------------------------------------
171
172   int getResultPos(MachineOpCode opCode) const {
173     return get(opCode).resultPos;
174   }
175   unsigned getNumDelaySlots(MachineOpCode opCode) const {
176     return get(opCode).numDelaySlots;
177   }
178   bool isCCInstr(MachineOpCode opCode) const {
179     return get(opCode).Flags & M_CC_FLAG;
180   }
181   bool isNop(MachineOpCode opCode) const {
182     return get(opCode).Flags & M_NOP_FLAG;
183   }
184   bool isBranch(MachineOpCode opCode) const {
185     return get(opCode).Flags & M_BRANCH_FLAG;
186   }
187   bool isCall(MachineOpCode opCode) const {
188     return get(opCode).Flags & M_CALL_FLAG;
189   }
190   bool isLoad(MachineOpCode opCode) const {
191     return get(opCode).Flags & M_LOAD_FLAG;
192   }
193   bool isStore(MachineOpCode opCode) const {
194     return get(opCode).Flags & M_STORE_FLAG;
195   }
196   bool isDummyPhiInstr(MachineOpCode opCode) const {
197     return get(opCode).Flags & M_DUMMY_PHI_FLAG;
198   }
199   // Check if an instruction can be issued before its operands are ready,
200   // or if a subsequent instruction that uses its result can be issued
201   // before the results are ready.
202   // Default to true since most instructions on many architectures allow this.
203   // 
204   virtual bool hasOperandInterlock(MachineOpCode opCode) const {
205     return true;
206   }  
207   virtual bool hasResultInterlock(MachineOpCode opCode) const {
208     return true;
209   }
210   
211   // 
212   // Latencies for individual instructions and instruction pairs
213   // 
214   virtual int minLatency(MachineOpCode opCode) const {
215     return get(opCode).latency;
216   }
217   
218   virtual int maxLatency(MachineOpCode opCode) const {
219     return get(opCode).latency;
220   }
221
222   //
223   // Which operand holds an immediate constant?  Returns -1 if none
224   // 
225   virtual int getImmedConstantPos(MachineOpCode opCode) const {
226     return -1; // immediate position is machine specific, so say -1 == "none"
227   }
228   
229   // Check if the specified constant fits in the immediate field
230   // of this machine instruction
231   // 
232   virtual bool constantFitsInImmedField(MachineOpCode opCode,
233                                         int64_t intValue) const;
234   
235   // Return the largest positive constant that can be held in the IMMED field
236   // of this machine instruction.
237   // isSignExtended is set to true if the value is sign-extended before use
238   // (this is true for all immediate fields in SPARC instructions).
239   // Return 0 if the instruction has no IMMED field.
240   // 
241   virtual uint64_t maxImmedConstant(MachineOpCode opCode,
242                                     bool &isSignExtended) const {
243     isSignExtended = get(opCode).immedIsSignExtended;
244     return get(opCode).maxImmedConst;
245   }
246
247   //-------------------------------------------------------------------------
248   // Queries about representation of LLVM quantities (e.g., constants)
249   //-------------------------------------------------------------------------
250
251   /// ConstantTypeMustBeLoaded - Test if this type of constant must be loaded
252   /// from memory into a register, i.e., cannot be set bitwise in register and
253   /// cannot use immediate fields of instructions.  Note that this only makes
254   /// sense for primitive types.
255   ///
256   virtual bool ConstantTypeMustBeLoaded(const Constant* CV) const;
257
258   // Test if this constant may not fit in the immediate field of the
259   // machine instructions (probably) generated for this instruction.
260   // 
261   virtual bool ConstantMayNotFitInImmedField(const Constant* CV,
262                                              const Instruction* I) const {
263     return true;                        // safe but very conservative
264   }
265
266   // Get certain common op codes for the current target.  this and all the
267   // Create* methods below should be moved to a machine code generation class
268   // 
269   virtual MachineOpCode getNOPOpCode() const { abort(); }
270
271   // Get the value of an integral constant in the form that must
272   // be put into the machine register.  The specified constant is interpreted
273   // as (i.e., converted if necessary to) the specified destination type.  The
274   // result is always returned as an uint64_t, since the representation of
275   // int64_t and uint64_t are identical.  The argument can be any known const.
276   // 
277   // isValidConstant is set to true if a valid constant was found.
278   // 
279   virtual uint64_t ConvertConstantToIntType(const TargetMachine &target,
280                                             const Value *V,
281                                             const Type *destType,
282                                             bool  &isValidConstant) const {
283     abort();
284   }
285
286   // Create an instruction sequence to put the constant `val' into
287   // the virtual register `dest'.  `val' may be a Constant or a
288   // GlobalValue, viz., the constant address of a global variable or function.
289   // The generated instructions are returned in `mvec'.
290   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
291   // Symbolic constants or constants that must be accessed from memory
292   // are added to the constant pool via MachineFunction::get(F).
293   // 
294   virtual void  CreateCodeToLoadConst(const TargetMachine& target,
295                                       Function* F,
296                                       Value* val,
297                                       Instruction* dest,
298                                       std::vector<MachineInstr*>& mvec,
299                                       MachineCodeForInstruction& mcfi) const {
300     abort();
301   }
302   
303   // Create an instruction sequence to copy an integer value `val'
304   // to a floating point value `dest' by copying to memory and back.
305   // val must be an integral type.  dest must be a Float or Double.
306   // The generated instructions are returned in `mvec'.
307   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
308   // Any stack space required is allocated via mcff.
309   // 
310   virtual void CreateCodeToCopyIntToFloat(const TargetMachine& target,
311                                           Function* F,
312                                           Value* val,
313                                           Instruction* dest,
314                                           std::vector<MachineInstr*>& mvec,
315                                           MachineCodeForInstruction& MI) const {
316     abort();
317   }
318
319   // Similarly, create an instruction sequence to copy an FP value
320   // `val' to an integer value `dest' by copying to memory and back.
321   // The generated instructions are returned in `mvec'.
322   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
323   // Any stack space required is allocated via mcff.
324   // 
325   virtual void CreateCodeToCopyFloatToInt(const TargetMachine& target,
326                                           Function* F,
327                                           Value* val,
328                                           Instruction* dest,
329                                           std::vector<MachineInstr*>& mvec,
330                                           MachineCodeForInstruction& MI) const {
331     abort();
332   }
333   
334   // Create instruction(s) to copy src to dest, for arbitrary types
335   // The generated instructions are returned in `mvec'.
336   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
337   // Any stack space required is allocated via mcff.
338   // 
339   virtual void CreateCopyInstructionsByType(const TargetMachine& target,
340                                             Function* F,
341                                             Value* src,
342                                             Instruction* dest,
343                                             std::vector<MachineInstr*>& mvec,
344                                           MachineCodeForInstruction& MI) const {
345     abort();
346   }
347
348   // Create instruction sequence to produce a sign-extended register value
349   // from an arbitrary sized value (sized in bits, not bytes).
350   // The generated instructions are appended to `mvec'.
351   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
352   // Any stack space required is allocated via mcff.
353   // 
354   virtual void CreateSignExtensionInstructions(const TargetMachine& target,
355                                        Function* F,
356                                        Value* srcVal,
357                                        Value* destVal,
358                                        unsigned numLowBits,
359                                        std::vector<MachineInstr*>& mvec,
360                                        MachineCodeForInstruction& MI) const {
361     abort();
362   }
363
364   // Create instruction sequence to produce a zero-extended register value
365   // from an arbitrary sized value (sized in bits, not bytes).
366   // The generated instructions are appended to `mvec'.
367   // Any temp. registers (TmpInstruction) created are recorded in mcfi.
368   // Any stack space required is allocated via mcff.
369   // 
370   virtual void CreateZeroExtensionInstructions(const TargetMachine& target,
371                                        Function* F,
372                                        Value* srcVal,
373                                        Value* destVal,
374                                        unsigned srcSizeInBits,
375                                        std::vector<MachineInstr*>& mvec,
376                                        MachineCodeForInstruction& mcfi) const {
377     abort();
378   }
379 };
380
381 } // End llvm namespace
382
383 #endif