TargetLowering.h revision 7226158d7e3986e55b58214a749aa4eabb3fb6d5
1//===-- llvm/Target/TargetLowering.h - Target Lowering 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 how to lower LLVM code to machine code. This has two 11// main components: 12// 13// 1. Which ValueTypes are natively supported by the target. 14// 2. Which operations are supported for supported ValueTypes. 15// 3. Cost thresholds for alternative implementations of certain operations. 16// 17// In addition it has a few other components, like information about FP 18// immediates. 19// 20//===----------------------------------------------------------------------===// 21 22#ifndef LLVM_TARGET_TARGETLOWERING_H 23#define LLVM_TARGET_TARGETLOWERING_H 24 25#include "llvm/Type.h" 26#include "llvm/CodeGen/ValueTypes.h" 27#include <vector> 28 29namespace llvm { 30 class Value; 31 class Function; 32 class TargetMachine; 33 class TargetData; 34 class TargetRegisterClass; 35 class SDNode; 36 class SDOperand; 37 class SelectionDAG; 38 class MachineBasicBlock; 39 class MachineInstr; 40 41//===----------------------------------------------------------------------===// 42/// TargetLowering - This class defines information used to lower LLVM code to 43/// legal SelectionDAG operators that the target instruction selector can accept 44/// natively. 45/// 46/// This class also defines callbacks that targets must implement to lower 47/// target-specific constructs to SelectionDAG operators. 48/// 49class TargetLowering { 50public: 51 /// LegalizeAction - This enum indicates whether operations are valid for a 52 /// target, and if not, what action should be used to make them valid. 53 enum LegalizeAction { 54 Legal, // The target natively supports this operation. 55 Promote, // This operation should be executed in a larger type. 56 Expand, // Try to expand this to other ops, otherwise use a libcall. 57 Custom, // Use the LowerOperation hook to implement custom lowering. 58 }; 59 60 enum OutOfRangeShiftAmount { 61 Undefined, // Oversized shift amounts are undefined (default). 62 Mask, // Shift amounts are auto masked (anded) to value size. 63 Extend, // Oversized shift pulls in zeros or sign bits. 64 }; 65 66 enum SetCCResultValue { 67 UndefinedSetCCResult, // SetCC returns a garbage/unknown extend. 68 ZeroOrOneSetCCResult, // SetCC returns a zero extended result. 69 ZeroOrNegativeOneSetCCResult, // SetCC returns a sign extended result. 70 }; 71 72 TargetLowering(TargetMachine &TM); 73 virtual ~TargetLowering(); 74 75 TargetMachine &getTargetMachine() const { return TM; } 76 const TargetData &getTargetData() const { return TD; } 77 78 bool isLittleEndian() const { return IsLittleEndian; } 79 MVT::ValueType getPointerTy() const { return PointerTy; } 80 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; } 81 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; } 82 83 /// isSetCCExpensive - Return true if the setcc operation is expensive for 84 /// this target. 85 bool isSetCCExpensive() const { return SetCCIsExpensive; } 86 87 /// isIntDivCheap() - Return true if integer divide is usually cheaper than 88 /// a sequence of several shifts, adds, and multiplies for this target. 89 bool isIntDivCheap() const { return IntDivIsCheap; } 90 91 /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of 92 /// srl/add/sra. 93 bool isPow2DivCheap() const { return Pow2DivIsCheap; } 94 95 /// getSetCCResultTy - Return the ValueType of the result of setcc operations. 96 /// 97 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; } 98 99 /// getSetCCResultContents - For targets without boolean registers, this flag 100 /// returns information about the contents of the high-bits in the setcc 101 /// result register. 102 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;} 103 104 /// getRegClassFor - Return the register class that should be used for the 105 /// specified value type. This may only be called on legal types. 106 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const { 107 TargetRegisterClass *RC = RegClassForVT[VT]; 108 assert(RC && "This value type is not natively supported!"); 109 return RC; 110 } 111 112 /// isTypeLegal - Return true if the target has native support for the 113 /// specified value type. This means that it has a register that directly 114 /// holds it without promotions or expansions. 115 bool isTypeLegal(MVT::ValueType VT) const { 116 return RegClassForVT[VT] != 0; 117 } 118 119 /// getTypeAction - Return how we should legalize values of this type, either 120 /// it is already legal (return 'Legal') or we need to promote it to a larger 121 /// type (return 'Promote'), or we need to expand it into multiple registers 122 /// of smaller integer type (return 'Expand'). 'Custom' is not an option. 123 LegalizeAction getTypeAction(MVT::ValueType VT) const { 124 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3); 125 } 126 unsigned long long getValueTypeActions() const { return ValueTypeActions; } 127 128 /// getTypeToTransformTo - For types supported by the target, this is an 129 /// identity function. For types that must be promoted to larger types, this 130 /// returns the larger type to promote to. For types that are larger than the 131 /// largest integer register, this contains one step in the expansion to get 132 /// to the smaller register. 133 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const { 134 return TransformToType[VT]; 135 } 136 137 typedef std::vector<double>::const_iterator legal_fpimm_iterator; 138 legal_fpimm_iterator legal_fpimm_begin() const { 139 return LegalFPImmediates.begin(); 140 } 141 legal_fpimm_iterator legal_fpimm_end() const { 142 return LegalFPImmediates.end(); 143 } 144 145 /// getOperationAction - Return how this operation should be treated: either 146 /// it is legal, needs to be promoted to a larger size, needs to be 147 /// expanded to some other code sequence, or the target has a custom expander 148 /// for it. 149 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const { 150 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3); 151 } 152 153 /// isOperationLegal - Return true if the specified operation is legal on this 154 /// target. 155 bool isOperationLegal(unsigned Op, MVT::ValueType VT) const { 156 return getOperationAction(Op, VT) == Legal; 157 } 158 159 /// getTypeToPromoteTo - If the action for this operation is to promote, this 160 /// method returns the ValueType to promote to. 161 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const { 162 assert(getOperationAction(Op, VT) == Promote && 163 "This operation isn't promoted!"); 164 MVT::ValueType NVT = VT; 165 do { 166 NVT = (MVT::ValueType)(NVT+1); 167 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid && 168 "Didn't find type to promote to!"); 169 } while (!isTypeLegal(NVT) || 170 getOperationAction(Op, NVT) == Promote); 171 return NVT; 172 } 173 174 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type. 175 /// This is fixed by the LLVM operations except for the pointer size. 176 MVT::ValueType getValueType(const Type *Ty) const { 177 switch (Ty->getTypeID()) { 178 default: assert(0 && "Unknown type!"); 179 case Type::VoidTyID: return MVT::isVoid; 180 case Type::BoolTyID: return MVT::i1; 181 case Type::UByteTyID: 182 case Type::SByteTyID: return MVT::i8; 183 case Type::ShortTyID: 184 case Type::UShortTyID: return MVT::i16; 185 case Type::IntTyID: 186 case Type::UIntTyID: return MVT::i32; 187 case Type::LongTyID: 188 case Type::ULongTyID: return MVT::i64; 189 case Type::FloatTyID: return MVT::f32; 190 case Type::DoubleTyID: return MVT::f64; 191 case Type::PointerTyID: return PointerTy; 192 case Type::PackedTyID: return MVT::Vector; 193 } 194 } 195 196 /// getNumElements - Return the number of registers that this ValueType will 197 /// eventually require. This is always one for all non-integer types, is 198 /// one for any types promoted to live in larger registers, but may be more 199 /// than one for types (like i64) that are split into pieces. 200 unsigned getNumElements(MVT::ValueType VT) const { 201 return NumElementsForVT[VT]; 202 } 203 204 /// This function returns the maximum number of store operations permitted 205 /// to replace a call to llvm.memset. The value is set by the target at the 206 /// performance threshold for such a replacement. 207 /// @brief Get maximum # of store operations permitted for llvm.memset 208 unsigned getMaxStoresPerMemSet() const { return maxStoresPerMemSet; } 209 210 /// This function returns the maximum number of store operations permitted 211 /// to replace a call to llvm.memcpy. The value is set by the target at the 212 /// performance threshold for such a replacement. 213 /// @brief Get maximum # of store operations permitted for llvm.memcpy 214 unsigned getMaxStoresPerMemCpy() const { return maxStoresPerMemCpy; } 215 216 /// This function returns the maximum number of store operations permitted 217 /// to replace a call to llvm.memmove. The value is set by the target at the 218 /// performance threshold for such a replacement. 219 /// @brief Get maximum # of store operations permitted for llvm.memmove 220 unsigned getMaxStoresPerMemMove() const { return maxStoresPerMemMove; } 221 222 /// This function returns true if the target allows unaligned memory accesses. 223 /// This is used, for example, in situations where an array copy/move/set is 224 /// converted to a sequence of store operations. It's use helps to ensure that 225 /// such replacements don't generate code that causes an alignment error 226 /// (trap) on the target machine. 227 /// @brief Determine if the target supports unaligned memory accesses. 228 bool allowsUnalignedMemoryAccesses() const 229 { return allowUnalignedMemoryAccesses; } 230 231 /// usesUnderscoreSetJmpLongJmp - Determine if we should use _setjmp or setjmp 232 /// to implement llvm.setjmp. 233 bool usesUnderscoreSetJmpLongJmp() const { 234 return UseUnderscoreSetJmpLongJmp; 235 } 236 237 //===--------------------------------------------------------------------===// 238 // TargetLowering Configuration Methods - These methods should be invoked by 239 // the derived class constructor to configure this object for the target. 240 // 241 242protected: 243 244 /// setShiftAmountType - Describe the type that should be used for shift 245 /// amounts. This type defaults to the pointer type. 246 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; } 247 248 /// setSetCCResultType - Describe the type that shoudl be used as the result 249 /// of a setcc operation. This defaults to the pointer type. 250 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; } 251 252 /// setSetCCResultContents - Specify how the target extends the result of a 253 /// setcc operation in a register. 254 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; } 255 256 /// setShiftAmountFlavor - Describe how the target handles out of range shift 257 /// amounts. 258 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) { 259 ShiftAmtHandling = OORSA; 260 } 261 262 /// setUseUnderscoreSetJmpLongJmp - Indicate whether this target prefers to 263 /// use _setjmp and _longjmp to or implement llvm.setjmp/llvm.longjmp or 264 /// the non _ versions. Defaults to false. 265 void setUseUnderscoreSetJmpLongJmp(bool Val) { 266 UseUnderscoreSetJmpLongJmp = Val; 267 } 268 269 /// setSetCCIxExpensive - This is a short term hack for targets that codegen 270 /// setcc as a conditional branch. This encourages the code generator to fold 271 /// setcc operations into other operations if possible. 272 void setSetCCIsExpensive() { SetCCIsExpensive = true; } 273 274 /// setIntDivIsCheap - Tells the code generator that integer divide is 275 /// expensive, and if possible, should be replaced by an alternate sequence 276 /// of instructions not containing an integer divide. 277 void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; } 278 279 /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate 280 /// srl/add/sra for a signed divide by power of two, and let the target handle 281 /// it. 282 void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; } 283 284 /// addRegisterClass - Add the specified register class as an available 285 /// regclass for the specified value type. This indicates the selector can 286 /// handle values of that class natively. 287 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) { 288 AvailableRegClasses.push_back(std::make_pair(VT, RC)); 289 RegClassForVT[VT] = RC; 290 } 291 292 /// computeRegisterProperties - Once all of the register classes are added, 293 /// this allows us to compute derived properties we expose. 294 void computeRegisterProperties(); 295 296 /// setOperationAction - Indicate that the specified operation does not work 297 /// with the specified type and indicate what to do about it. 298 void setOperationAction(unsigned Op, MVT::ValueType VT, 299 LegalizeAction Action) { 300 assert(VT < 16 && Op < sizeof(OpActions)/sizeof(OpActions[0]) && 301 "Table isn't big enough!"); 302 OpActions[Op] |= Action << VT*2; 303 } 304 305 /// addLegalFPImmediate - Indicate that this target can instruction select 306 /// the specified FP immediate natively. 307 void addLegalFPImmediate(double Imm) { 308 LegalFPImmediates.push_back(Imm); 309 } 310 311public: 312 313 //===--------------------------------------------------------------------===// 314 // Lowering methods - These methods must be implemented by targets so that 315 // the SelectionDAGLowering code knows how to lower these. 316 // 317 318 /// LowerArguments - This hook must be implemented to indicate how we should 319 /// lower the arguments for the specified function, into the specified DAG. 320 virtual std::vector<SDOperand> 321 LowerArguments(Function &F, SelectionDAG &DAG) = 0; 322 323 /// LowerCallTo - This hook lowers an abstract call to a function into an 324 /// actual call. This returns a pair of operands. The first element is the 325 /// return value for the function (if RetTy is not VoidTy). The second 326 /// element is the outgoing token chain. 327 typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy; 328 virtual std::pair<SDOperand, SDOperand> 329 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, 330 unsigned CallingConv, bool isTailCall, SDOperand Callee, 331 ArgListTy &Args, SelectionDAG &DAG) = 0; 332 333 /// LowerReturnTo - This hook lowers a return instruction into the appropriate 334 /// legal ISD::RET node for the target's current ABI. This method is optional 335 /// and is intended for targets that need non-standard behavior. 336 virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op, 337 SelectionDAG &DAG); 338 339 /// LowerVAStart - This lowers the llvm.va_start intrinsic. If not 340 /// implemented, this method prints a message and aborts. This method should 341 /// return the modified chain value. Note that VAListPtr* correspond to the 342 /// llvm.va_start operand. 343 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP, 344 Value *VAListV, SelectionDAG &DAG); 345 346 /// LowerVAEnd - This lowers llvm.va_end and returns the resultant chain. If 347 /// not implemented, this defaults to a noop. 348 virtual SDOperand LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV, 349 SelectionDAG &DAG); 350 351 /// LowerVACopy - This lowers llvm.va_copy and returns the resultant chain. 352 /// If not implemented, this defaults to loading a pointer from the input and 353 /// storing it to the output. 354 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, 355 SDOperand DestP, Value *DestV, 356 SelectionDAG &DAG); 357 358 /// LowerVAArg - This lowers the vaarg instruction. If not implemented, this 359 /// prints a message and aborts. 360 virtual std::pair<SDOperand,SDOperand> 361 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, 362 const Type *ArgTy, SelectionDAG &DAG); 363 364 /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or 365 /// llvm.frameaddress (depending on the value of the first argument). The 366 /// return values are the result pointer and the resultant token chain. If 367 /// not implemented, both of these intrinsics will return null. 368 virtual std::pair<SDOperand, SDOperand> 369 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, 370 SelectionDAG &DAG); 371 372 /// LowerOperation - For operations that are unsupported by the target, and 373 /// which are registered to use 'custom' lowering. This callback is invoked. 374 /// If the target has no operations that require custom lowering, it need not 375 /// implement this. The default implementation of this aborts. 376 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG); 377 378 // getTargetNodeName() - This method returns the name of a target specific 379 // DAG node. 380 virtual const char *getTargetNodeName(unsigned Opcode) const; 381 382 //===--------------------------------------------------------------------===// 383 // Scheduler hooks 384 // 385 386 // InsertAtEndOfBasicBlock - This method should be implemented by targets that 387 // mark instructions with the 'usesCustomDAGSchedInserter' flag. These 388 // instructions are special in various ways, which require special support to 389 // insert. The specified MachineInstr is created but not inserted into any 390 // basic blocks, and the scheduler passes ownership of it to this method. 391 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI, 392 MachineBasicBlock *MBB); 393 394private: 395 TargetMachine &TM; 396 const TargetData &TD; 397 398 /// IsLittleEndian - True if this is a little endian target. 399 /// 400 bool IsLittleEndian; 401 402 /// PointerTy - The type to use for pointers, usually i32 or i64. 403 /// 404 MVT::ValueType PointerTy; 405 406 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever 407 /// PointerTy is. 408 MVT::ValueType ShiftAmountTy; 409 410 OutOfRangeShiftAmount ShiftAmtHandling; 411 412 /// SetCCIsExpensive - This is a short term hack for targets that codegen 413 /// setcc as a conditional branch. This encourages the code generator to fold 414 /// setcc operations into other operations if possible. 415 bool SetCCIsExpensive; 416 417 /// IntDivIsCheap - Tells the code generator not to expand integer divides by 418 /// constants into a sequence of muls, adds, and shifts. This is a hack until 419 /// a real cost model is in place. If we ever optimize for size, this will be 420 /// set to true unconditionally. 421 bool IntDivIsCheap; 422 423 /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate 424 /// srl/add/sra for a signed divide by power of two, and let the target handle 425 /// it. 426 bool Pow2DivIsCheap; 427 428 /// SetCCResultTy - The type that SetCC operations use. This defaults to the 429 /// PointerTy. 430 MVT::ValueType SetCCResultTy; 431 432 /// SetCCResultContents - Information about the contents of the high-bits in 433 /// the result of a setcc comparison operation. 434 SetCCResultValue SetCCResultContents; 435 436 /// UseUnderscoreSetJmpLongJmp - This target prefers to use _setjmp and 437 /// _longjmp to implement llvm.setjmp/llvm.longjmp. Defaults to false. 438 bool UseUnderscoreSetJmpLongJmp; 439 440 /// RegClassForVT - This indicates the default register class to use for 441 /// each ValueType the target supports natively. 442 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE]; 443 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE]; 444 445 /// ValueTypeActions - This is a bitvector that contains two bits for each 446 /// value type, where the two bits correspond to the LegalizeAction enum. 447 /// This can be queried with "getTypeAction(VT)". 448 unsigned long long ValueTypeActions; 449 450 /// TransformToType - For any value types we are promoting or expanding, this 451 /// contains the value type that we are changing to. For Expanded types, this 452 /// contains one step of the expand (e.g. i64 -> i32), even if there are 453 /// multiple steps required (e.g. i64 -> i16). For types natively supported 454 /// by the system, this holds the same type (e.g. i32 -> i32). 455 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE]; 456 457 /// OpActions - For each operation and each value type, keep a LegalizeAction 458 /// that indicates how instruction selection should deal with the operation. 459 /// Most operations are Legal (aka, supported natively by the target), but 460 /// operations that are not should be described. Note that operations on 461 /// non-legal value types are not described here. 462 unsigned OpActions[128]; 463 464 std::vector<double> LegalFPImmediates; 465 466 std::vector<std::pair<MVT::ValueType, 467 TargetRegisterClass*> > AvailableRegClasses; 468 469protected: 470 /// When lowering %llvm.memset this field specifies the maximum number of 471 /// store operations that may be substituted for the call to memset. Targets 472 /// must set this value based on the cost threshold for that target. Targets 473 /// should assume that the memset will be done using as many of the largest 474 /// store operations first, followed by smaller ones, if necessary, per 475 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine 476 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte 477 /// store. This only applies to setting a constant array of a constant size. 478 /// @brief Specify maximum number of store instructions per memset call. 479 unsigned maxStoresPerMemSet; 480 481 /// When lowering %llvm.memcpy this field specifies the maximum number of 482 /// store operations that may be substituted for a call to memcpy. Targets 483 /// must set this value based on the cost threshold for that target. Targets 484 /// should assume that the memcpy will be done using as many of the largest 485 /// store operations first, followed by smaller ones, if necessary, per 486 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine 487 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store 488 /// and one 1-byte store. This only applies to copying a constant array of 489 /// constant size. 490 /// @brief Specify maximum bytes of store instructions per memcpy call. 491 unsigned maxStoresPerMemCpy; 492 493 /// When lowering %llvm.memmove this field specifies the maximum number of 494 /// store instructions that may be substituted for a call to memmove. Targets 495 /// must set this value based on the cost threshold for that target. Targets 496 /// should assume that the memmove will be done using as many of the largest 497 /// store operations first, followed by smaller ones, if necessary, per 498 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine 499 /// with 8-bit alignment would result in nine 1-byte stores. This only 500 /// applies to copying a constant array of constant size. 501 /// @brief Specify maximum bytes of store instructions per memmove call. 502 unsigned maxStoresPerMemMove; 503 504 /// This field specifies whether the target machine permits unaligned memory 505 /// accesses. This is used, for example, to determine the size of store 506 /// operations when copying small arrays and other similar tasks. 507 /// @brief Indicate whether the target permits unaligned memory accesses. 508 bool allowUnalignedMemoryAccesses; 509}; 510} // end llvm namespace 511 512#endif 513