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