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