1 //===-- llvm/Target/InstrInfo.h - Target Instruction Information --*-C++-*-==//
3 // This file describes the target machine instructions to the code generator.
5 //===---------------------------------------------------------------------===//
7 #ifndef LLVM_TARGET_MACHINEINSTRINFO_H
8 #define LLVM_TARGET_MACHINEINSTRINFO_H
10 #include "Support/DataTypes.h"
13 class MachineInstrDescriptor;
20 class MachineCodeForInstruction;
22 //---------------------------------------------------------------------------
23 // Data types used to define information about a single machine instruction
24 //---------------------------------------------------------------------------
26 typedef int MachineOpCode;
27 typedef unsigned InstrSchedClass;
29 const MachineOpCode INVALID_MACHINE_OPCODE = -1;
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.
36 // FIXME: This should be a property of the target so that more than one target
37 // at a time can be active...
39 extern const MachineInstrDescriptor *TargetInstrDescriptors;
42 //---------------------------------------------------------------------------
43 // struct MachineInstrDescriptor:
44 // Predefined information about each machine instruction.
45 // Designed to initialized statically.
47 // class MachineInstructionInfo
48 // Interface to description of machine instructions
50 //---------------------------------------------------------------------------
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;
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
83 class MachineInstrInfo {
85 const TargetMachine& target;
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
92 MachineInstrInfo(const MachineInstrInfo &); // DO NOT IMPLEMENT
93 void operator=(const MachineInstrInfo &); // DO NOT IMPLEMENT
95 MachineInstrInfo(const TargetMachine& tgt,
96 const MachineInstrDescriptor *desc, unsigned descSize,
97 unsigned numRealOpCodes);
98 virtual ~MachineInstrInfo();
100 unsigned getNumRealOpCodes() const { return numRealOpCodes; }
101 unsigned getNumTotalOpCodes() const { return descSize; }
103 const MachineInstrDescriptor& getDescriptor(MachineOpCode opCode) const {
104 assert(opCode >= 0 && opCode < (int)descSize);
108 int getNumOperands(MachineOpCode opCode) const {
109 return getDescriptor(opCode).numOperands;
112 int getResultPos(MachineOpCode opCode) const {
113 return getDescriptor(opCode).resultPos;
116 unsigned getNumDelaySlots(MachineOpCode opCode) const {
117 return getDescriptor(opCode).numDelaySlots;
120 InstrSchedClass getSchedClass(MachineOpCode opCode) const {
121 return getDescriptor(opCode).schedClass;
125 // Query instruction class flags according to the machine-independent
126 // flags listed above.
128 unsigned getIClass(MachineOpCode opCode) const {
129 return getDescriptor(opCode).iclass;
131 bool isNop(MachineOpCode opCode) const {
132 return getDescriptor(opCode).iclass & M_NOP_FLAG;
134 bool isBranch(MachineOpCode opCode) const {
135 return getDescriptor(opCode).iclass & M_BRANCH_FLAG;
137 bool isCall(MachineOpCode opCode) const {
138 return getDescriptor(opCode).iclass & M_CALL_FLAG;
140 bool isReturn(MachineOpCode opCode) const {
141 return getDescriptor(opCode).iclass & M_RET_FLAG;
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;
148 bool isArith(MachineOpCode opCode) const {
149 return getDescriptor(opCode).iclass & M_ARITH_FLAG;
151 bool isCCInstr(MachineOpCode opCode) const {
152 return getDescriptor(opCode).iclass & M_CC_FLAG;
154 bool isLogical(MachineOpCode opCode) const {
155 return getDescriptor(opCode).iclass & M_LOGICAL_FLAG;
157 bool isIntInstr(MachineOpCode opCode) const {
158 return getDescriptor(opCode).iclass & M_INT_FLAG;
160 bool isFloatInstr(MachineOpCode opCode) const {
161 return getDescriptor(opCode).iclass & M_FLOAT_FLAG;
163 bool isConditional(MachineOpCode opCode) const {
164 return getDescriptor(opCode).iclass & M_CONDL_FLAG;
166 bool isLoad(MachineOpCode opCode) const {
167 return getDescriptor(opCode).iclass & M_LOAD_FLAG;
169 bool isPrefetch(MachineOpCode opCode) const {
170 return getDescriptor(opCode).iclass & M_PREFETCH_FLAG;
172 bool isLoadOrPrefetch(MachineOpCode opCode) const {
173 return getDescriptor(opCode).iclass & M_LOAD_FLAG
174 || getDescriptor(opCode).iclass & M_PREFETCH_FLAG;
176 bool isStore(MachineOpCode opCode) const {
177 return getDescriptor(opCode).iclass & M_STORE_FLAG;
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;
184 bool isDummyPhiInstr(const MachineOpCode opCode) const {
185 return getDescriptor(opCode).iclass & M_DUMMY_PHI_FLAG;
187 bool isPseudoInstr(const MachineOpCode opCode) const {
188 return getDescriptor(opCode).iclass & M_PSEUDO_FLAG;
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.
196 virtual bool hasOperandInterlock(MachineOpCode opCode) const {
200 virtual bool hasResultInterlock(MachineOpCode opCode) const {
205 // Latencies for individual instructions and instruction pairs
207 virtual int minLatency(MachineOpCode opCode) const {
208 return getDescriptor(opCode).latency;
211 virtual int maxLatency(MachineOpCode opCode) const {
212 return getDescriptor(opCode).latency;
216 // Which operand holds an immediate constant? Returns -1 if none
218 virtual int getImmedConstantPos(MachineOpCode opCode) const {
219 return -1; // immediate position is machine specific, so say -1 == "none"
222 // Check if the specified constant fits in the immediate field
223 // of this machine instruction
225 virtual bool constantFitsInImmedField(MachineOpCode opCode,
226 int64_t intValue) const;
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.
234 virtual uint64_t maxImmedConstant(MachineOpCode opCode,
235 bool &isSignExtended) const {
236 isSignExtended = getDescriptor(opCode).immedIsSignExtended;
237 return getDescriptor(opCode).maxImmedConst;
240 //-------------------------------------------------------------------------
241 // Queries about representation of LLVM quantities (e.g., constants)
242 //-------------------------------------------------------------------------
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.
249 virtual bool ConstantTypeMustBeLoaded(const Constant* CV) const;
251 // Test if this constant may not fit in the immediate field of the
252 // machine instructions (probably) generated for this instruction.
254 virtual bool ConstantMayNotFitInImmedField(const Constant* CV,
255 const Instruction* I) const {
256 return true; // safe but very conservative
259 //-------------------------------------------------------------------------
260 // Code generation support for creating individual machine instructions
261 //-------------------------------------------------------------------------
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
266 virtual MachineOpCode getNOPOpCode() const = 0;
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).
276 virtual void CreateCodeToLoadConst(const TargetMachine& target,
280 std::vector<MachineInstr*>& mvec,
281 MachineCodeForInstruction& mcfi) const=0;
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.
290 virtual void CreateCodeToCopyIntToFloat(const TargetMachine& target,
294 std::vector<MachineInstr*>& mvec,
295 MachineCodeForInstruction& mcfi)const=0;
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.
303 virtual void CreateCodeToCopyFloatToInt(const TargetMachine& target,
307 std::vector<MachineInstr*>& mvec,
308 MachineCodeForInstruction& mcfi)const=0;
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.
315 virtual void CreateCopyInstructionsByType(const TargetMachine& target,
319 std::vector<MachineInstr*>& mvec,
320 MachineCodeForInstruction& mcfi)const=0;
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.
328 virtual void CreateSignExtensionInstructions(const TargetMachine& target,
333 std::vector<MachineInstr*>& mvec,
334 MachineCodeForInstruction& mcfi) const=0;
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.
342 virtual void CreateZeroExtensionInstructions(const TargetMachine& target,
346 unsigned srcSizeInBits,
347 std::vector<MachineInstr*>& mvec,
348 MachineCodeForInstruction& mcfi) const=0;