LLVM API Documentation
00001 //===-- llvm/Target/TargetLowering.h - Target Lowering Info -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file describes how to lower LLVM code to machine code. This has two 00011 // main components: 00012 // 00013 // 1. Which ValueTypes are natively supported by the target. 00014 // 2. Which operations are supported for supported ValueTypes. 00015 // 3. Cost thresholds for alternative implementations of certain operations. 00016 // 00017 // In addition it has a few other components, like information about FP 00018 // immediates. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #ifndef LLVM_TARGET_TARGETLOWERING_H 00023 #define LLVM_TARGET_TARGETLOWERING_H 00024 00025 #include "llvm/InlineAsm.h" 00026 #include "llvm/CodeGen/SelectionDAGNodes.h" 00027 #include "llvm/CodeGen/RuntimeLibcalls.h" 00028 #include "llvm/ADT/APFloat.h" 00029 #include "llvm/ADT/DenseMap.h" 00030 #include "llvm/ADT/SmallSet.h" 00031 #include "llvm/ADT/STLExtras.h" 00032 #include <climits> 00033 #include <map> 00034 #include <vector> 00035 00036 namespace llvm { 00037 class AllocaInst; 00038 class CallInst; 00039 class Function; 00040 class FastISel; 00041 class MachineBasicBlock; 00042 class MachineFunction; 00043 class MachineFrameInfo; 00044 class MachineInstr; 00045 class MachineModuleInfo; 00046 class SDNode; 00047 class SDValue; 00048 class SelectionDAG; 00049 class TargetData; 00050 class TargetMachine; 00051 class TargetRegisterClass; 00052 class TargetSubtarget; 00053 class Value; 00054 00055 //===----------------------------------------------------------------------===// 00056 /// TargetLowering - This class defines information used to lower LLVM code to 00057 /// legal SelectionDAG operators that the target instruction selector can accept 00058 /// natively. 00059 /// 00060 /// This class also defines callbacks that targets must implement to lower 00061 /// target-specific constructs to SelectionDAG operators. 00062 /// 00063 class TargetLowering { 00064 public: 00065 /// LegalizeAction - This enum indicates whether operations are valid for a 00066 /// target, and if not, what action should be used to make them valid. 00067 enum LegalizeAction { 00068 Legal, // The target natively supports this operation. 00069 Promote, // This operation should be executed in a larger type. 00070 Expand, // Try to expand this to other ops, otherwise use a libcall. 00071 Custom // Use the LowerOperation hook to implement custom lowering. 00072 }; 00073 00074 enum OutOfRangeShiftAmount { 00075 Undefined, // Oversized shift amounts are undefined (default). 00076 Mask, // Shift amounts are auto masked (anded) to value size. 00077 Extend // Oversized shift pulls in zeros or sign bits. 00078 }; 00079 00080 enum BooleanContent { // How the target represents true/false values. 00081 UndefinedBooleanContent, // Only bit 0 counts, the rest can hold garbage. 00082 ZeroOrOneBooleanContent, // All bits zero except for bit 0. 00083 ZeroOrNegativeOneBooleanContent // All bits equal to bit 0. 00084 }; 00085 00086 enum SchedPreference { 00087 SchedulingForLatency, // Scheduling for shortest total latency. 00088 SchedulingForRegPressure // Scheduling for lowest register pressure. 00089 }; 00090 00091 explicit TargetLowering(TargetMachine &TM); 00092 virtual ~TargetLowering(); 00093 00094 TargetMachine &getTargetMachine() const { return TM; } 00095 const TargetData *getTargetData() const { return TD; } 00096 00097 bool isBigEndian() const { return !IsLittleEndian; } 00098 bool isLittleEndian() const { return IsLittleEndian; } 00099 MVT getPointerTy() const { return PointerTy; } 00100 MVT getShiftAmountTy() const { return ShiftAmountTy; } 00101 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; } 00102 00103 /// usesGlobalOffsetTable - Return true if this target uses a GOT for PIC 00104 /// codegen. 00105 bool usesGlobalOffsetTable() const { return UsesGlobalOffsetTable; } 00106 00107 /// isSelectExpensive - Return true if the select operation is expensive for 00108 /// this target. 00109 bool isSelectExpensive() const { return SelectIsExpensive; } 00110 00111 /// isIntDivCheap() - Return true if integer divide is usually cheaper than 00112 /// a sequence of several shifts, adds, and multiplies for this target. 00113 bool isIntDivCheap() const { return IntDivIsCheap; } 00114 00115 /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of 00116 /// srl/add/sra. 00117 bool isPow2DivCheap() const { return Pow2DivIsCheap; } 00118 00119 /// getSetCCResultType - Return the ValueType of the result of SETCC 00120 /// operations. Also used to obtain the target's preferred type for 00121 /// the condition operand of SELECT and BRCOND nodes. In the case of 00122 /// BRCOND the argument passed is MVT::Other since there are no other 00123 /// operands to get a type hint from. 00124 virtual MVT getSetCCResultType(MVT VT) const; 00125 00126 /// getBooleanContents - For targets without i1 registers, this gives the 00127 /// nature of the high-bits of boolean values held in types wider than i1. 00128 /// "Boolean values" are special true/false values produced by nodes like 00129 /// SETCC and consumed (as the condition) by nodes like SELECT and BRCOND. 00130 /// Not to be confused with general values promoted from i1. 00131 BooleanContent getBooleanContents() const { return BooleanContents;} 00132 00133 /// getSchedulingPreference - Return target scheduling preference. 00134 SchedPreference getSchedulingPreference() const { 00135 return SchedPreferenceInfo; 00136 } 00137 00138 /// getRegClassFor - Return the register class that should be used for the 00139 /// specified value type. This may only be called on legal types. 00140 TargetRegisterClass *getRegClassFor(MVT VT) const { 00141 assert((unsigned)VT.getSimpleVT() < array_lengthof(RegClassForVT)); 00142 TargetRegisterClass *RC = RegClassForVT[VT.getSimpleVT()]; 00143 assert(RC && "This value type is not natively supported!"); 00144 return RC; 00145 } 00146 00147 /// isTypeLegal - Return true if the target has native support for the 00148 /// specified value type. This means that it has a register that directly 00149 /// holds it without promotions or expansions. 00150 bool isTypeLegal(MVT VT) const { 00151 assert(!VT.isSimple() || 00152 (unsigned)VT.getSimpleVT() < array_lengthof(RegClassForVT)); 00153 return VT.isSimple() && RegClassForVT[VT.getSimpleVT()] != 0; 00154 } 00155 00156 class ValueTypeActionImpl { 00157 /// ValueTypeActions - This is a bitvector that contains two bits for each 00158 /// value type, where the two bits correspond to the LegalizeAction enum. 00159 /// This can be queried with "getTypeAction(VT)". 00160 uint32_t ValueTypeActions[2]; 00161 public: 00162 ValueTypeActionImpl() { 00163 ValueTypeActions[0] = ValueTypeActions[1] = 0; 00164 } 00165 ValueTypeActionImpl(const ValueTypeActionImpl &RHS) { 00166 ValueTypeActions[0] = RHS.ValueTypeActions[0]; 00167 ValueTypeActions[1] = RHS.ValueTypeActions[1]; 00168 } 00169 00170 LegalizeAction getTypeAction(MVT VT) const { 00171 if (VT.isExtended()) { 00172 if (VT.isVector()) { 00173 return VT.isPow2VectorType() ? Expand : Promote; 00174 } 00175 if (VT.isInteger()) 00176 // First promote to a power-of-two size, then expand if necessary. 00177 return VT == VT.getRoundIntegerType() ? Expand : Promote; 00178 assert(0 && "Unsupported extended type!"); 00179 return Legal; 00180 } 00181 unsigned I = VT.getSimpleVT(); 00182 assert(I<4*array_lengthof(ValueTypeActions)*sizeof(ValueTypeActions[0])); 00183 return (LegalizeAction)((ValueTypeActions[I>>4] >> ((2*I) & 31)) & 3); 00184 } 00185 void setTypeAction(MVT VT, LegalizeAction Action) { 00186 unsigned I = VT.getSimpleVT(); 00187 assert(I<4*array_lengthof(ValueTypeActions)*sizeof(ValueTypeActions[0])); 00188 ValueTypeActions[I>>4] |= Action << ((I*2) & 31); 00189 } 00190 }; 00191 00192 const ValueTypeActionImpl &getValueTypeActions() const { 00193 return ValueTypeActions; 00194 } 00195 00196 /// getTypeAction - Return how we should legalize values of this type, either 00197 /// it is already legal (return 'Legal') or we need to promote it to a larger 00198 /// type (return 'Promote'), or we need to expand it into multiple registers 00199 /// of smaller integer type (return 'Expand'). 'Custom' is not an option. 00200 LegalizeAction getTypeAction(MVT VT) const { 00201 return ValueTypeActions.getTypeAction(VT); 00202 } 00203 00204 /// getTypeToTransformTo - For types supported by the target, this is an 00205 /// identity function. For types that must be promoted to larger types, this 00206 /// returns the larger type to promote to. For integer types that are larger 00207 /// than the largest integer register, this contains one step in the expansion 00208 /// to get to the smaller register. For illegal floating point types, this 00209 /// returns the integer type to transform to. 00210 MVT getTypeToTransformTo(MVT VT) const { 00211 if (VT.isSimple()) { 00212 assert((unsigned)VT.getSimpleVT() < array_lengthof(TransformToType)); 00213 MVT NVT = TransformToType[VT.getSimpleVT()]; 00214 assert(getTypeAction(NVT) != Promote && 00215 "Promote may not follow Expand or Promote"); 00216 return NVT; 00217 } 00218 00219 if (VT.isVector()) { 00220 MVT NVT = VT.getPow2VectorType(); 00221 if (NVT == VT) { 00222 // Vector length is a power of 2 - split to half the size. 00223 unsigned NumElts = VT.getVectorNumElements(); 00224 MVT EltVT = VT.getVectorElementType(); 00225 return (NumElts == 1) ? EltVT : MVT::getVectorVT(EltVT, NumElts / 2); 00226 } 00227 // Promote to a power of two size, avoiding multi-step promotion. 00228 return getTypeAction(NVT) == Promote ? getTypeToTransformTo(NVT) : NVT; 00229 } else if (VT.isInteger()) { 00230 MVT NVT = VT.getRoundIntegerType(); 00231 if (NVT == VT) 00232 // Size is a power of two - expand to half the size. 00233 return MVT::getIntegerVT(VT.getSizeInBits() / 2); 00234 else 00235 // Promote to a power of two size, avoiding multi-step promotion. 00236 return getTypeAction(NVT) == Promote ? getTypeToTransformTo(NVT) : NVT; 00237 } 00238 assert(0 && "Unsupported extended type!"); 00239 return MVT(); // Not reached 00240 } 00241 00242 /// getTypeToExpandTo - For types supported by the target, this is an 00243 /// identity function. For types that must be expanded (i.e. integer types 00244 /// that are larger than the largest integer register or illegal floating 00245 /// point types), this returns the largest legal type it will be expanded to. 00246 MVT getTypeToExpandTo(MVT VT) const { 00247 assert(!VT.isVector()); 00248 while (true) { 00249 switch (getTypeAction(VT)) { 00250 case Legal: 00251 return VT; 00252 case Expand: 00253 VT = getTypeToTransformTo(VT); 00254 break; 00255 default: 00256 assert(false && "Type is not legal nor is it to be expanded!"); 00257 return VT; 00258 } 00259 } 00260 return VT; 00261 } 00262 00263 /// getVectorTypeBreakdown - Vector types are broken down into some number of 00264 /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32 00265 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack. 00266 /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86. 00267 /// 00268 /// This method returns the number of registers needed, and the VT for each 00269 /// register. It also returns the VT and quantity of the intermediate values 00270 /// before they are promoted/expanded. 00271 /// 00272 unsigned getVectorTypeBreakdown(MVT VT, 00273 MVT &IntermediateVT, 00274 unsigned &NumIntermediates, 00275 MVT &RegisterVT) const; 00276 00277 /// getTgtMemIntrinsic: Given an intrinsic, checks if on the target the 00278 /// intrinsic will need to map to a MemIntrinsicNode (touches memory). If 00279 /// this is the case, it returns true and store the intrinsic 00280 /// information into the IntrinsicInfo that was passed to the function. 00281 typedef struct IntrinsicInfo { 00282 unsigned opc; // target opcode 00283 MVT memVT; // memory VT 00284 const Value* ptrVal; // value representing memory location 00285 int offset; // offset off of ptrVal 00286 unsigned align; // alignment 00287 bool vol; // is volatile? 00288 bool readMem; // reads memory? 00289 bool writeMem; // writes memory? 00290 } IntrinisicInfo; 00291 00292 virtual bool getTgtMemIntrinsic(IntrinsicInfo& Info, 00293 CallInst &I, unsigned Intrinsic) { 00294 return false; 00295 } 00296 00297 /// getWidenVectorType: given a vector type, returns the type to widen to 00298 /// (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself. 00299 /// If there is no vector type that we want to widen to, returns MVT::Other 00300 /// When and were to widen is target dependent based on the cost of 00301 /// scalarizing vs using the wider vector type. 00302 virtual MVT getWidenVectorType(MVT VT); 00303 00304 typedef std::vector<APFloat>::const_iterator legal_fpimm_iterator; 00305 legal_fpimm_iterator legal_fpimm_begin() const { 00306 return LegalFPImmediates.begin(); 00307 } 00308 legal_fpimm_iterator legal_fpimm_end() const { 00309 return LegalFPImmediates.end(); 00310 } 00311 00312 /// isShuffleMaskLegal - Targets can use this to indicate that they only 00313 /// support *some* VECTOR_SHUFFLE operations, those with specific masks. 00314 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values 00315 /// are assumed to be legal. 00316 virtual bool isShuffleMaskLegal(SDValue Mask, MVT VT) const { 00317 return true; 00318 } 00319 00320 /// isVectorClearMaskLegal - Similar to isShuffleMaskLegal. This is 00321 /// used by Targets can use this to indicate if there is a suitable 00322 /// VECTOR_SHUFFLE that can be used to replace a VAND with a constant 00323 /// pool entry. 00324 virtual bool isVectorClearMaskLegal(const std::vector<SDValue> &BVOps, 00325 MVT EVT, 00326 SelectionDAG &DAG) const { 00327 return false; 00328 } 00329 00330 /// getOperationAction - Return how this operation should be treated: either 00331 /// it is legal, needs to be promoted to a larger size, needs to be 00332 /// expanded to some other code sequence, or the target has a custom expander 00333 /// for it. 00334 LegalizeAction getOperationAction(unsigned Op, MVT VT) const { 00335 if (VT.isExtended()) return Expand; 00336 assert(Op < array_lengthof(OpActions) && 00337 (unsigned)VT.getSimpleVT() < sizeof(OpActions[0])*4 && 00338 "Table isn't big enough!"); 00339 return (LegalizeAction)((OpActions[Op] >> (2*VT.getSimpleVT())) & 3); 00340 } 00341 00342 /// isOperationLegal - Return true if the specified operation is legal on this 00343 /// target. 00344 bool isOperationLegal(unsigned Op, MVT VT) const { 00345 return (VT == MVT::Other || isTypeLegal(VT)) && 00346 (getOperationAction(Op, VT) == Legal || 00347 getOperationAction(Op, VT) == Custom); 00348 } 00349 00350 /// getLoadExtAction - Return how this load with extension should be treated: 00351 /// either it is legal, needs to be promoted to a larger size, needs to be 00352 /// expanded to some other code sequence, or the target has a custom expander 00353 /// for it. 00354 LegalizeAction getLoadExtAction(unsigned LType, MVT VT) const { 00355 assert(LType < array_lengthof(LoadExtActions) && 00356 (unsigned)VT.getSimpleVT() < sizeof(LoadExtActions[0])*4 && 00357 "Table isn't big enough!"); 00358 return (LegalizeAction)((LoadExtActions[LType] >> (2*VT.getSimpleVT())) & 3); 00359 } 00360 00361 /// isLoadExtLegal - Return true if the specified load with extension is legal 00362 /// on this target. 00363 bool isLoadExtLegal(unsigned LType, MVT VT) const { 00364 return VT.isSimple() && 00365 (getLoadExtAction(LType, VT) == Legal || 00366 getLoadExtAction(LType, VT) == Custom); 00367 } 00368 00369 /// getTruncStoreAction - Return how this store with truncation should be 00370 /// treated: either it is legal, needs to be promoted to a larger size, needs 00371 /// to be expanded to some other code sequence, or the target has a custom 00372 /// expander for it. 00373 LegalizeAction getTruncStoreAction(MVT ValVT, 00374 MVT MemVT) const { 00375 assert((unsigned)ValVT.getSimpleVT() < array_lengthof(TruncStoreActions) && 00376 (unsigned)MemVT.getSimpleVT() < sizeof(TruncStoreActions[0])*4 && 00377 "Table isn't big enough!"); 00378 return (LegalizeAction)((TruncStoreActions[ValVT.getSimpleVT()] >> 00379 (2*MemVT.getSimpleVT())) & 3); 00380 } 00381 00382 /// isTruncStoreLegal - Return true if the specified store with truncation is 00383 /// legal on this target. 00384 bool isTruncStoreLegal(MVT ValVT, MVT MemVT) const { 00385 return isTypeLegal(ValVT) && MemVT.isSimple() && 00386 (getTruncStoreAction(ValVT, MemVT) == Legal || 00387 getTruncStoreAction(ValVT, MemVT) == Custom); 00388 } 00389 00390 /// getIndexedLoadAction - Return how the indexed load should be treated: 00391 /// either it is legal, needs to be promoted to a larger size, needs to be 00392 /// expanded to some other code sequence, or the target has a custom expander 00393 /// for it. 00394 LegalizeAction 00395 getIndexedLoadAction(unsigned IdxMode, MVT VT) const { 00396 assert(IdxMode < array_lengthof(IndexedModeActions[0]) && 00397 (unsigned)VT.getSimpleVT() < sizeof(IndexedModeActions[0][0])*4 && 00398 "Table isn't big enough!"); 00399 return (LegalizeAction)((IndexedModeActions[0][IdxMode] >> 00400 (2*VT.getSimpleVT())) & 3); 00401 } 00402 00403 /// isIndexedLoadLegal - Return true if the specified indexed load is legal 00404 /// on this target. 00405 bool isIndexedLoadLegal(unsigned IdxMode, MVT VT) const { 00406 return VT.isSimple() && 00407 (getIndexedLoadAction(IdxMode, VT) == Legal || 00408 getIndexedLoadAction(IdxMode, VT) == Custom); 00409 } 00410 00411 /// getIndexedStoreAction - Return how the indexed store should be treated: 00412 /// either it is legal, needs to be promoted to a larger size, needs to be 00413 /// expanded to some other code sequence, or the target has a custom expander 00414 /// for it. 00415 LegalizeAction 00416 getIndexedStoreAction(unsigned IdxMode, MVT VT) const { 00417 assert(IdxMode < array_lengthof(IndexedModeActions[1]) && 00418 (unsigned)VT.getSimpleVT() < sizeof(IndexedModeActions[1][0])*4 && 00419 "Table isn't big enough!"); 00420 return (LegalizeAction)((IndexedModeActions[1][IdxMode] >> 00421 (2*VT.getSimpleVT())) & 3); 00422 } 00423 00424 /// isIndexedStoreLegal - Return true if the specified indexed load is legal 00425 /// on this target. 00426 bool isIndexedStoreLegal(unsigned IdxMode, MVT VT) const { 00427 return VT.isSimple() && 00428 (getIndexedStoreAction(IdxMode, VT) == Legal || 00429 getIndexedStoreAction(IdxMode, VT) == Custom); 00430 } 00431 00432 /// getConvertAction - Return how the conversion should be treated: 00433 /// either it is legal, needs to be promoted to a larger size, needs to be 00434 /// expanded to some other code sequence, or the target has a custom expander 00435 /// for it. 00436 LegalizeAction 00437 getConvertAction(MVT FromVT, MVT ToVT) const { 00438 assert((unsigned)FromVT.getSimpleVT() < array_lengthof(ConvertActions) && 00439 (unsigned)ToVT.getSimpleVT() < sizeof(ConvertActions[0])*4 && 00440 "Table isn't big enough!"); 00441 return (LegalizeAction)((ConvertActions[FromVT.getSimpleVT()] >> 00442 (2*ToVT.getSimpleVT())) & 3); 00443 } 00444 00445 /// isConvertLegal - Return true if the specified conversion is legal 00446 /// on this target. 00447 bool isConvertLegal(MVT FromVT, MVT ToVT) const { 00448 return isTypeLegal(FromVT) && isTypeLegal(ToVT) && 00449 (getConvertAction(FromVT, ToVT) == Legal || 00450 getConvertAction(FromVT, ToVT) == Custom); 00451 } 00452 00453 /// getCondCodeAction - Return how the condition code should be treated: 00454 /// either it is legal, needs to be expanded to some other code sequence, 00455 /// or the target has a custom expander for it. 00456 LegalizeAction 00457 getCondCodeAction(ISD::CondCode CC, MVT VT) const { 00458 assert((unsigned)CC < array_lengthof(CondCodeActions) && 00459 (unsigned)VT.getSimpleVT() < sizeof(CondCodeActions[0])*4 && 00460 "Table isn't big enough!"); 00461 LegalizeAction Action = (LegalizeAction) 00462 ((CondCodeActions[CC] >> (2*VT.getSimpleVT())) & 3); 00463 assert(Action != Promote && "Can't promote condition code!"); 00464 return Action; 00465 } 00466 00467 /// isCondCodeLegal - Return true if the specified condition code is legal 00468 /// on this target. 00469 bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const { 00470 return getCondCodeAction(CC, VT) == Legal || 00471 getCondCodeAction(CC, VT) == Custom; 00472 } 00473 00474 00475 /// getTypeToPromoteTo - If the action for this operation is to promote, this 00476 /// method returns the ValueType to promote to. 00477 MVT getTypeToPromoteTo(unsigned Op, MVT VT) const { 00478 assert(getOperationAction(Op, VT) == Promote && 00479 "This operation isn't promoted!"); 00480 00481 // See if this has an explicit type specified. 00482 std::map<std::pair<unsigned, MVT::SimpleValueType>, 00483 MVT::SimpleValueType>::const_iterator PTTI = 00484 PromoteToType.find(std::make_pair(Op, VT.getSimpleVT())); 00485 if (PTTI != PromoteToType.end()) return PTTI->second; 00486 00487 assert((VT.isInteger() || VT.isFloatingPoint()) && 00488 "Cannot autopromote this type, add it with AddPromotedToType."); 00489 00490 MVT NVT = VT; 00491 do { 00492 NVT = (MVT::SimpleValueType)(NVT.getSimpleVT()+1); 00493 assert(NVT.isInteger() == VT.isInteger() && NVT != MVT::isVoid && 00494 "Didn't find type to promote to!"); 00495 } while (!isTypeLegal(NVT) || 00496 getOperationAction(Op, NVT) == Promote); 00497 return NVT; 00498 } 00499 00500 /// getValueType - Return the MVT corresponding to this LLVM type. 00501 /// This is fixed by the LLVM operations except for the pointer size. If 00502 /// AllowUnknown is true, this will return MVT::Other for types with no MVT 00503 /// counterpart (e.g. structs), otherwise it will assert. 00504 MVT getValueType(const Type *Ty, bool AllowUnknown = false) const { 00505 MVT VT = MVT::getMVT(Ty, AllowUnknown); 00506 return VT == MVT::iPTR ? PointerTy : VT; 00507 } 00508 00509 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate 00510 /// function arguments in the caller parameter area. This is the actual 00511 /// alignment, not its logarithm. 00512 virtual unsigned getByValTypeAlignment(const Type *Ty) const; 00513 00514 /// getRegisterType - Return the type of registers that this ValueType will 00515 /// eventually require. 00516 MVT getRegisterType(MVT VT) const { 00517 if (VT.isSimple()) { 00518 assert((unsigned)VT.getSimpleVT() < array_lengthof(RegisterTypeForVT)); 00519 return RegisterTypeForVT[VT.getSimpleVT()]; 00520 } 00521 if (VT.isVector()) { 00522 MVT VT1, RegisterVT; 00523 unsigned NumIntermediates; 00524 (void)getVectorTypeBreakdown(VT, VT1, NumIntermediates, RegisterVT); 00525 return RegisterVT; 00526 } 00527 if (VT.isInteger()) { 00528 return getRegisterType(getTypeToTransformTo(VT)); 00529 } 00530 assert(0 && "Unsupported extended type!"); 00531 return MVT(); // Not reached 00532 } 00533 00534 /// getNumRegisters - Return the number of registers that this ValueType will 00535 /// eventually require. This is one for any types promoted to live in larger 00536 /// registers, but may be more than one for types (like i64) that are split 00537 /// into pieces. For types like i140, which are first promoted then expanded, 00538 /// it is the number of registers needed to hold all the bits of the original 00539 /// type. For an i140 on a 32 bit machine this means 5 registers. 00540 unsigned getNumRegisters(MVT VT) const { 00541 if (VT.isSimple()) { 00542 assert((unsigned)VT.getSimpleVT() < array_lengthof(NumRegistersForVT)); 00543 return NumRegistersForVT[VT.getSimpleVT()]; 00544 } 00545 if (VT.isVector()) { 00546 MVT VT1, VT2; 00547 unsigned NumIntermediates; 00548 return getVectorTypeBreakdown(VT, VT1, NumIntermediates, VT2); 00549 } 00550 if (VT.isInteger()) { 00551 unsigned BitWidth = VT.getSizeInBits(); 00552 unsigned RegWidth = getRegisterType(VT).getSizeInBits(); 00553 return (BitWidth + RegWidth - 1) / RegWidth; 00554 } 00555 assert(0 && "Unsupported extended type!"); 00556 return 0; // Not reached 00557 } 00558 00559 /// ShouldShrinkFPConstant - If true, then instruction selection should 00560 /// seek to shrink the FP constant of the specified type to a smaller type 00561 /// in order to save space and / or reduce runtime. 00562 virtual bool ShouldShrinkFPConstant(MVT VT) const { return true; } 00563 00564 /// hasTargetDAGCombine - If true, the target has custom DAG combine 00565 /// transformations that it can perform for the specified node. 00566 bool hasTargetDAGCombine(ISD::NodeType NT) const { 00567 assert(unsigned(NT >> 3) < array_lengthof(TargetDAGCombineArray)); 00568 return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7)); 00569 } 00570 00571 /// This function returns the maximum number of store operations permitted 00572 /// to replace a call to llvm.memset. The value is set by the target at the 00573 /// performance threshold for such a replacement. 00574 /// @brief Get maximum # of store operations permitted for llvm.memset 00575 unsigned getMaxStoresPerMemset() const { return maxStoresPerMemset; } 00576 00577 /// This function returns the maximum number of store operations permitted 00578 /// to replace a call to llvm.memcpy. The value is set by the target at the 00579 /// performance threshold for such a replacement. 00580 /// @brief Get maximum # of store operations permitted for llvm.memcpy 00581 unsigned getMaxStoresPerMemcpy() const { return maxStoresPerMemcpy; } 00582 00583 /// This function returns the maximum number of store operations permitted 00584 /// to replace a call to llvm.memmove. The value is set by the target at the 00585 /// performance threshold for such a replacement. 00586 /// @brief Get maximum # of store operations permitted for llvm.memmove 00587 unsigned getMaxStoresPerMemmove() const { return maxStoresPerMemmove; } 00588 00589 /// This function returns true if the target allows unaligned memory accesses. 00590 /// This is used, for example, in situations where an array copy/move/set is 00591 /// converted to a sequence of store operations. It's use helps to ensure that 00592 /// such replacements don't generate code that causes an alignment error 00593 /// (trap) on the target machine. 00594 /// @brief Determine if the target supports unaligned memory accesses. 00595 bool allowsUnalignedMemoryAccesses() const { 00596 return allowUnalignedMemoryAccesses; 00597 } 00598 00599 /// getOptimalMemOpType - Returns the target specific optimal type for load 00600 /// and store operations as a result of memset, memcpy, and memmove lowering. 00601 /// It returns MVT::iAny if SelectionDAG should be responsible for 00602 /// determining it. 00603 virtual MVT getOptimalMemOpType(uint64_t Size, unsigned Align, 00604 bool isSrcConst, bool isSrcStr) const { 00605 return MVT::iAny; 00606 } 00607 00608 /// usesUnderscoreSetJmp - Determine if we should use _setjmp or setjmp 00609 /// to implement llvm.setjmp. 00610 bool usesUnderscoreSetJmp() const { 00611 return UseUnderscoreSetJmp; 00612 } 00613 00614 /// usesUnderscoreLongJmp - Determine if we should use _longjmp or longjmp 00615 /// to implement llvm.longjmp. 00616 bool usesUnderscoreLongJmp() const { 00617 return UseUnderscoreLongJmp; 00618 } 00619 00620 /// getStackPointerRegisterToSaveRestore - If a physical register, this 00621 /// specifies the register that llvm.savestack/llvm.restorestack should save 00622 /// and restore. 00623 unsigned getStackPointerRegisterToSaveRestore() const { 00624 return StackPointerRegisterToSaveRestore; 00625 } 00626 00627 /// getExceptionAddressRegister - If a physical register, this returns 00628 /// the register that receives the exception address on entry to a landing 00629 /// pad. 00630 unsigned getExceptionAddressRegister() const { 00631 return ExceptionPointerRegister; 00632 } 00633 00634 /// getExceptionSelectorRegister - If a physical register, this returns 00635 /// the register that receives the exception typeid on entry to a landing 00636 /// pad. 00637 unsigned getExceptionSelectorRegister() const { 00638 return ExceptionSelectorRegister; 00639 } 00640 00641 /// getJumpBufSize - returns the target's jmp_buf size in bytes (if never 00642 /// set, the default is 200) 00643 unsigned getJumpBufSize() const { 00644 return JumpBufSize; 00645 } 00646 00647 /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes 00648 /// (if never set, the default is 0) 00649 unsigned getJumpBufAlignment() const { 00650 return JumpBufAlignment; 00651 } 00652 00653 /// getIfCvtBlockLimit - returns the target specific if-conversion block size 00654 /// limit. Any block whose size is greater should not be predicated. 00655 unsigned getIfCvtBlockSizeLimit() const { 00656 return IfCvtBlockSizeLimit; 00657 } 00658 00659 /// getIfCvtDupBlockLimit - returns the target specific size limit for a 00660 /// block to be considered for duplication. Any block whose size is greater 00661 /// should not be duplicated to facilitate its predication. 00662 unsigned getIfCvtDupBlockSizeLimit() const { 00663 return IfCvtDupBlockSizeLimit; 00664 } 00665 00666 /// getPrefLoopAlignment - return the preferred loop alignment. 00667 /// 00668 unsigned getPrefLoopAlignment() const { 00669 return PrefLoopAlignment; 00670 } 00671 00672 /// getPreIndexedAddressParts - returns true by value, base pointer and 00673 /// offset pointer and addressing mode by reference if the node's address 00674 /// can be legally represented as pre-indexed load / store address. 00675 virtual bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, 00676 SDValue &Offset, 00677 ISD::MemIndexedMode &AM, 00678 SelectionDAG &DAG) { 00679 return false; 00680 } 00681 00682 /// getPostIndexedAddressParts - returns true by value, base pointer and 00683 /// offset pointer and addressing mode by reference if this node can be 00684 /// combined with a load / store to form a post-indexed load / store. 00685 virtual bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, 00686 SDValue &Base, SDValue &Offset, 00687 ISD::MemIndexedMode &AM, 00688 SelectionDAG &DAG) { 00689 return false; 00690 } 00691 00692 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC 00693 /// jumptable. 00694 virtual SDValue getPICJumpTableRelocBase(SDValue Table, 00695 SelectionDAG &DAG) const; 00696 00697 /// isOffsetFoldingLegal - Return true if folding a constant offset 00698 /// with the given GlobalAddress is legal. It is frequently not legal in 00699 /// PIC relocation models. 00700 virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const; 00701 00702 //===--------------------------------------------------------------------===// 00703 // TargetLowering Optimization Methods 00704 // 00705 00706 /// TargetLoweringOpt - A convenience struct that encapsulates a DAG, and two 00707 /// SDValues for returning information from TargetLowering to its clients 00708 /// that want to combine 00709 struct TargetLoweringOpt { 00710 SelectionDAG &DAG; 00711 SDValue Old; 00712 SDValue New; 00713 00714 explicit TargetLoweringOpt(SelectionDAG &InDAG) : DAG(InDAG) {} 00715 00716 bool CombineTo(SDValue O, SDValue N) { 00717 Old = O; 00718 New = N; 00719 return true; 00720 } 00721 00722 /// ShrinkDemandedConstant - Check to see if the specified operand of the 00723 /// specified instruction is a constant integer. If so, check to see if 00724 /// there are any bits set in the constant that are not demanded. If so, 00725 /// shrink the constant and return true. 00726 bool ShrinkDemandedConstant(SDValue Op, const APInt &Demanded); 00727 }; 00728 00729 /// SimplifyDemandedBits - Look at Op. At this point, we know that only the 00730 /// DemandedMask bits of the result of Op are ever used downstream. If we can 00731 /// use this information to simplify Op, create a new simplified DAG node and 00732 /// return true, returning the original and new nodes in Old and New. 00733 /// Otherwise, analyze the expression and return a mask of KnownOne and 00734 /// KnownZero bits for the expression (used to simplify the caller). 00735 /// The KnownZero/One bits may only be accurate for those bits in the 00736 /// DemandedMask. 00737 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedMask, 00738 APInt &KnownZero, APInt &KnownOne, 00739 TargetLoweringOpt &TLO, unsigned Depth = 0) const; 00740 00741 /// computeMaskedBitsForTargetNode - Determine which of the bits specified in 00742 /// Mask are known to be either zero or one and return them in the 00743 /// KnownZero/KnownOne bitsets. 00744 virtual void computeMaskedBitsForTargetNode(const SDValue Op, 00745 const APInt &Mask, 00746 APInt &KnownZero, 00747 APInt &KnownOne, 00748 const SelectionDAG &DAG, 00749 unsigned Depth = 0) const; 00750 00751 /// ComputeNumSignBitsForTargetNode - This method can be implemented by 00752 /// targets that want to expose additional information about sign bits to the 00753 /// DAG Combiner. 00754 virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, 00755 unsigned Depth = 0) const; 00756 00757 struct