1 //===- llvm/CodeGen/TargetLowering.h - Target Lowering Info -----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 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_CODEGEN_TARGETLOWERING_H 23 #define LLVM_CODEGEN_TARGETLOWERING_H 24 25 #include "llvm/ADT/APInt.h" 26 #include "llvm/ADT/ArrayRef.h" 27 #include "llvm/ADT/DenseMap.h" 28 #include "llvm/ADT/SmallVector.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/CodeGen/DAGCombine.h" 31 #include "llvm/CodeGen/ISDOpcodes.h" 32 #include "llvm/CodeGen/LowLevelTypeUtils.h" 33 #include "llvm/CodeGen/MachineRegisterInfo.h" 34 #include "llvm/CodeGen/RuntimeLibcalls.h" 35 #include "llvm/CodeGen/SelectionDAG.h" 36 #include "llvm/CodeGen/SelectionDAGNodes.h" 37 #include "llvm/CodeGen/TargetCallingConv.h" 38 #include "llvm/CodeGen/ValueTypes.h" 39 #include "llvm/CodeGenTypes/MachineValueType.h" 40 #include "llvm/IR/Attributes.h" 41 #include "llvm/IR/CallingConv.h" 42 #include "llvm/IR/DataLayout.h" 43 #include "llvm/IR/DerivedTypes.h" 44 #include "llvm/IR/Function.h" 45 #include "llvm/IR/InlineAsm.h" 46 #include "llvm/IR/Instruction.h" 47 #include "llvm/IR/Instructions.h" 48 #include "llvm/IR/Type.h" 49 #include "llvm/Support/Alignment.h" 50 #include "llvm/Support/AtomicOrdering.h" 51 #include "llvm/Support/Casting.h" 52 #include "llvm/Support/ErrorHandling.h" 53 #include <algorithm> 54 #include <cassert> 55 #include <climits> 56 #include <cstdint> 57 #include <iterator> 58 #include <map> 59 #include <string> 60 #include <utility> 61 #include <vector> 62 63 namespace llvm { 64 65 class AssumptionCache; 66 class CCState; 67 class CCValAssign; 68 enum class ComplexDeinterleavingOperation; 69 enum class ComplexDeinterleavingRotation; 70 class Constant; 71 class FastISel; 72 class FunctionLoweringInfo; 73 class GlobalValue; 74 class Loop; 75 class GISelKnownBits; 76 class IntrinsicInst; 77 class IRBuilderBase; 78 struct KnownBits; 79 class LLVMContext; 80 class MachineBasicBlock; 81 class MachineFunction; 82 class MachineInstr; 83 class MachineJumpTableInfo; 84 class MachineLoop; 85 class MachineRegisterInfo; 86 class MCContext; 87 class MCExpr; 88 class Module; 89 class ProfileSummaryInfo; 90 class TargetLibraryInfo; 91 class TargetMachine; 92 class TargetRegisterClass; 93 class TargetRegisterInfo; 94 class TargetTransformInfo; 95 class Value; 96 97 namespace Sched { 98 99 enum Preference { 100 None, // No preference 101 Source, // Follow source order. 102 RegPressure, // Scheduling for lowest register pressure. 103 Hybrid, // Scheduling for both latency and register pressure. 104 ILP, // Scheduling for ILP in low register pressure mode. 105 VLIW, // Scheduling for VLIW targets. 106 Fast, // Fast suboptimal list scheduling 107 Linearize // Linearize DAG, no scheduling 108 }; 109 110 } // end namespace Sched 111 112 // MemOp models a memory operation, either memset or memcpy/memmove. 113 struct MemOp { 114 private: 115 // Shared 116 uint64_t Size; 117 bool DstAlignCanChange; // true if destination alignment can satisfy any 118 // constraint. 119 Align DstAlign; // Specified alignment of the memory operation. 120 121 bool AllowOverlap; 122 // memset only 123 bool IsMemset; // If setthis memory operation is a memset. 124 bool ZeroMemset; // If set clears out memory with zeros. 125 // memcpy only 126 bool MemcpyStrSrc; // Indicates whether the memcpy source is an in-register 127 // constant so it does not need to be loaded. 128 Align SrcAlign; // Inferred alignment of the source or default value if the 129 // memory operation does not need to load the value. 130 public: 131 static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, 132 Align SrcAlign, bool IsVolatile, 133 bool MemcpyStrSrc = false) { 134 MemOp Op; 135 Op.Size = Size; 136 Op.DstAlignCanChange = DstAlignCanChange; 137 Op.DstAlign = DstAlign; 138 Op.AllowOverlap = !IsVolatile; 139 Op.IsMemset = false; 140 Op.ZeroMemset = false; 141 Op.MemcpyStrSrc = MemcpyStrSrc; 142 Op.SrcAlign = SrcAlign; 143 return Op; 144 } 145 SetMemOp146 static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, 147 bool IsZeroMemset, bool IsVolatile) { 148 MemOp Op; 149 Op.Size = Size; 150 Op.DstAlignCanChange = DstAlignCanChange; 151 Op.DstAlign = DstAlign; 152 Op.AllowOverlap = !IsVolatile; 153 Op.IsMemset = true; 154 Op.ZeroMemset = IsZeroMemset; 155 Op.MemcpyStrSrc = false; 156 return Op; 157 } 158 sizeMemOp159 uint64_t size() const { return Size; } getDstAlignMemOp160 Align getDstAlign() const { 161 assert(!DstAlignCanChange); 162 return DstAlign; 163 } isFixedDstAlignMemOp164 bool isFixedDstAlign() const { return !DstAlignCanChange; } allowOverlapMemOp165 bool allowOverlap() const { return AllowOverlap; } isMemsetMemOp166 bool isMemset() const { return IsMemset; } isMemcpyMemOp167 bool isMemcpy() const { return !IsMemset; } isMemcpyWithFixedDstAlignMemOp168 bool isMemcpyWithFixedDstAlign() const { 169 return isMemcpy() && !DstAlignCanChange; 170 } isZeroMemsetMemOp171 bool isZeroMemset() const { return isMemset() && ZeroMemset; } isMemcpyStrSrcMemOp172 bool isMemcpyStrSrc() const { 173 assert(isMemcpy() && "Must be a memcpy"); 174 return MemcpyStrSrc; 175 } getSrcAlignMemOp176 Align getSrcAlign() const { 177 assert(isMemcpy() && "Must be a memcpy"); 178 return SrcAlign; 179 } isSrcAlignedMemOp180 bool isSrcAligned(Align AlignCheck) const { 181 return isMemset() || llvm::isAligned(AlignCheck, SrcAlign.value()); 182 } isDstAlignedMemOp183 bool isDstAligned(Align AlignCheck) const { 184 return DstAlignCanChange || llvm::isAligned(AlignCheck, DstAlign.value()); 185 } isAlignedMemOp186 bool isAligned(Align AlignCheck) const { 187 return isSrcAligned(AlignCheck) && isDstAligned(AlignCheck); 188 } 189 }; 190 191 /// This base class for TargetLowering contains the SelectionDAG-independent 192 /// parts that can be used from the rest of CodeGen. 193 class TargetLoweringBase { 194 public: 195 /// This enum indicates whether operations are valid for a target, and if not, 196 /// what action should be used to make them valid. 197 enum LegalizeAction : uint8_t { 198 Legal, // The target natively supports this operation. 199 Promote, // This operation should be executed in a larger type. 200 Expand, // Try to expand this to other ops, otherwise use a libcall. 201 LibCall, // Don't try to expand this to other ops, always use a libcall. 202 Custom // Use the LowerOperation hook to implement custom lowering. 203 }; 204 205 /// This enum indicates whether a types are legal for a target, and if not, 206 /// what action should be used to make them valid. 207 enum LegalizeTypeAction : uint8_t { 208 TypeLegal, // The target natively supports this type. 209 TypePromoteInteger, // Replace this integer with a larger one. 210 TypeExpandInteger, // Split this integer into two of half the size. 211 TypeSoftenFloat, // Convert this float to a same size integer type. 212 TypeExpandFloat, // Split this float into two of half the size. 213 TypeScalarizeVector, // Replace this one-element vector with its element. 214 TypeSplitVector, // Split this vector into two of half the size. 215 TypeWidenVector, // This vector should be widened into a larger vector. 216 TypePromoteFloat, // Replace this float with a larger one. 217 TypeSoftPromoteHalf, // Soften half to i16 and use float to do arithmetic. 218 TypeScalarizeScalableVector, // This action is explicitly left unimplemented. 219 // While it is theoretically possible to 220 // legalize operations on scalable types with a 221 // loop that handles the vscale * #lanes of the 222 // vector, this is non-trivial at SelectionDAG 223 // level and these types are better to be 224 // widened or promoted. 225 }; 226 227 /// LegalizeKind holds the legalization kind that needs to happen to EVT 228 /// in order to type-legalize it. 229 using LegalizeKind = std::pair<LegalizeTypeAction, EVT>; 230 231 /// Enum that describes how the target represents true/false values. 232 enum BooleanContent { 233 UndefinedBooleanContent, // Only bit 0 counts, the rest can hold garbage. 234 ZeroOrOneBooleanContent, // All bits zero except for bit 0. 235 ZeroOrNegativeOneBooleanContent // All bits equal to bit 0. 236 }; 237 238 /// Enum that describes what type of support for selects the target has. 239 enum SelectSupportKind { 240 ScalarValSelect, // The target supports scalar selects (ex: cmov). 241 ScalarCondVectorVal, // The target supports selects with a scalar condition 242 // and vector values (ex: cmov). 243 VectorMaskSelect // The target supports vector selects with a vector 244 // mask (ex: x86 blends). 245 }; 246 247 /// Enum that specifies what an atomic load/AtomicRMWInst is expanded 248 /// to, if at all. Exists because different targets have different levels of 249 /// support for these atomic instructions, and also have different options 250 /// w.r.t. what they should expand to. 251 enum class AtomicExpansionKind { 252 None, // Don't expand the instruction. 253 CastToInteger, // Cast the atomic instruction to another type, e.g. from 254 // floating-point to integer type. 255 LLSC, // Expand the instruction into loadlinked/storeconditional; used 256 // by ARM/AArch64. 257 LLOnly, // Expand the (load) instruction into just a load-linked, which has 258 // greater atomic guarantees than a normal load. 259 CmpXChg, // Expand the instruction into cmpxchg; used by at least X86. 260 MaskedIntrinsic, // Use a target-specific intrinsic for the LL/SC loop. 261 BitTestIntrinsic, // Use a target-specific intrinsic for special bit 262 // operations; used by X86. 263 CmpArithIntrinsic,// Use a target-specific intrinsic for special compare 264 // operations; used by X86. 265 Expand, // Generic expansion in terms of other atomic operations. 266 267 // Rewrite to a non-atomic form for use in a known non-preemptible 268 // environment. 269 NotAtomic 270 }; 271 272 /// Enum that specifies when a multiplication should be expanded. 273 enum class MulExpansionKind { 274 Always, // Always expand the instruction. 275 OnlyLegalOrCustom, // Only expand when the resulting instructions are legal 276 // or custom. 277 }; 278 279 /// Enum that specifies when a float negation is beneficial. 280 enum class NegatibleCost { 281 Cheaper = 0, // Negated expression is cheaper. 282 Neutral = 1, // Negated expression has the same cost. 283 Expensive = 2 // Negated expression is more expensive. 284 }; 285 286 /// Enum of different potentially desirable ways to fold (and/or (setcc ...), 287 /// (setcc ...)). 288 enum AndOrSETCCFoldKind : uint8_t { 289 None = 0, // No fold is preferable. 290 AddAnd = 1, // Fold with `Add` op and `And` op is preferable. 291 NotAnd = 2, // Fold with `Not` op and `And` op is preferable. 292 ABS = 4, // Fold with `llvm.abs` op is preferable. 293 }; 294 295 class ArgListEntry { 296 public: 297 Value *Val = nullptr; 298 SDValue Node = SDValue(); 299 Type *Ty = nullptr; 300 bool IsSExt : 1; 301 bool IsZExt : 1; 302 bool IsInReg : 1; 303 bool IsSRet : 1; 304 bool IsNest : 1; 305 bool IsByVal : 1; 306 bool IsByRef : 1; 307 bool IsInAlloca : 1; 308 bool IsPreallocated : 1; 309 bool IsReturned : 1; 310 bool IsSwiftSelf : 1; 311 bool IsSwiftAsync : 1; 312 bool IsSwiftError : 1; 313 bool IsCFGuardTarget : 1; 314 MaybeAlign Alignment = std::nullopt; 315 Type *IndirectType = nullptr; 316 ArgListEntry()317 ArgListEntry() 318 : IsSExt(false), IsZExt(false), IsInReg(false), IsSRet(false), 319 IsNest(false), IsByVal(false), IsByRef(false), IsInAlloca(false), 320 IsPreallocated(false), IsReturned(false), IsSwiftSelf(false), 321 IsSwiftAsync(false), IsSwiftError(false), IsCFGuardTarget(false) {} 322 323 void setAttributes(const CallBase *Call, unsigned ArgIdx); 324 }; 325 using ArgListTy = std::vector<ArgListEntry>; 326 markLibCallAttributes(MachineFunction * MF,unsigned CC,ArgListTy & Args)327 virtual void markLibCallAttributes(MachineFunction *MF, unsigned CC, 328 ArgListTy &Args) const {}; 329 getExtendForContent(BooleanContent Content)330 static ISD::NodeType getExtendForContent(BooleanContent Content) { 331 switch (Content) { 332 case UndefinedBooleanContent: 333 // Extend by adding rubbish bits. 334 return ISD::ANY_EXTEND; 335 case ZeroOrOneBooleanContent: 336 // Extend by adding zero bits. 337 return ISD::ZERO_EXTEND; 338 case ZeroOrNegativeOneBooleanContent: 339 // Extend by copying the sign bit. 340 return ISD::SIGN_EXTEND; 341 } 342 llvm_unreachable("Invalid content kind"); 343 } 344 345 explicit TargetLoweringBase(const TargetMachine &TM); 346 TargetLoweringBase(const TargetLoweringBase &) = delete; 347 TargetLoweringBase &operator=(const TargetLoweringBase &) = delete; 348 virtual ~TargetLoweringBase() = default; 349 350 /// Return true if the target support strict float operation isStrictFPEnabled()351 bool isStrictFPEnabled() const { 352 return IsStrictFPEnabled; 353 } 354 355 protected: 356 /// Initialize all of the actions to default values. 357 void initActions(); 358 359 public: getTargetMachine()360 const TargetMachine &getTargetMachine() const { return TM; } 361 useSoftFloat()362 virtual bool useSoftFloat() const { return false; } 363 364 /// Return the pointer type for the given address space, defaults to 365 /// the pointer type from the data layout. 366 /// FIXME: The default needs to be removed once all the code is updated. 367 virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS = 0) const { 368 return MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); 369 } 370 371 /// Return the in-memory pointer type for the given address space, defaults to 372 /// the pointer type from the data layout. 373 /// FIXME: The default needs to be removed once all the code is updated. 374 virtual MVT getPointerMemTy(const DataLayout &DL, uint32_t AS = 0) const { 375 return MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); 376 } 377 378 /// Return the type for frame index, which is determined by 379 /// the alloca address space specified through the data layout. getFrameIndexTy(const DataLayout & DL)380 MVT getFrameIndexTy(const DataLayout &DL) const { 381 return getPointerTy(DL, DL.getAllocaAddrSpace()); 382 } 383 384 /// Return the type for code pointers, which is determined by the program 385 /// address space specified through the data layout. getProgramPointerTy(const DataLayout & DL)386 MVT getProgramPointerTy(const DataLayout &DL) const { 387 return getPointerTy(DL, DL.getProgramAddressSpace()); 388 } 389 390 /// Return the type for operands of fence. 391 /// TODO: Let fence operands be of i32 type and remove this. getFenceOperandTy(const DataLayout & DL)392 virtual MVT getFenceOperandTy(const DataLayout &DL) const { 393 return getPointerTy(DL); 394 } 395 396 /// Return the type to use for a scalar shift opcode, given the shifted amount 397 /// type. Targets should return a legal type if the input type is legal. 398 /// Targets can return a type that is too small if the input type is illegal. 399 virtual MVT getScalarShiftAmountTy(const DataLayout &, EVT) const; 400 401 /// Returns the type for the shift amount of a shift opcode. For vectors, 402 /// returns the input type. For scalars, behavior depends on \p LegalTypes. If 403 /// \p LegalTypes is true, calls getScalarShiftAmountTy, otherwise uses 404 /// pointer type. If getScalarShiftAmountTy or pointer type cannot represent 405 /// all possible shift amounts, returns MVT::i32. In general, \p LegalTypes 406 /// should be set to true for calls during type legalization and after type 407 /// legalization has been completed. 408 EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL, 409 bool LegalTypes = true) const; 410 411 /// Return the preferred type to use for a shift opcode, given the shifted 412 /// amount type is \p ShiftValueTy. 413 LLVM_READONLY getPreferredShiftAmountTy(LLT ShiftValueTy)414 virtual LLT getPreferredShiftAmountTy(LLT ShiftValueTy) const { 415 return ShiftValueTy; 416 } 417 418 /// Returns the type to be used for the index operand of: 419 /// ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT, 420 /// ISD::INSERT_SUBVECTOR, and ISD::EXTRACT_SUBVECTOR getVectorIdxTy(const DataLayout & DL)421 virtual MVT getVectorIdxTy(const DataLayout &DL) const { 422 return getPointerTy(DL); 423 } 424 425 /// Returns the type to be used for the EVL/AVL operand of VP nodes: 426 /// ISD::VP_ADD, ISD::VP_SUB, etc. It must be a legal scalar integer type, 427 /// and must be at least as large as i32. The EVL is implicitly zero-extended 428 /// to any larger type. getVPExplicitVectorLengthTy()429 virtual MVT getVPExplicitVectorLengthTy() const { return MVT::i32; } 430 431 /// This callback is used to inspect load/store instructions and add 432 /// target-specific MachineMemOperand flags to them. The default 433 /// implementation does nothing. getTargetMMOFlags(const Instruction & I)434 virtual MachineMemOperand::Flags getTargetMMOFlags(const Instruction &I) const { 435 return MachineMemOperand::MONone; 436 } 437 438 /// This callback is used to inspect load/store SDNode. 439 /// The default implementation does nothing. 440 virtual MachineMemOperand::Flags getTargetMMOFlags(const MemSDNode & Node)441 getTargetMMOFlags(const MemSDNode &Node) const { 442 return MachineMemOperand::MONone; 443 } 444 445 MachineMemOperand::Flags 446 getLoadMemOperandFlags(const LoadInst &LI, const DataLayout &DL, 447 AssumptionCache *AC = nullptr, 448 const TargetLibraryInfo *LibInfo = nullptr) const; 449 MachineMemOperand::Flags getStoreMemOperandFlags(const StoreInst &SI, 450 const DataLayout &DL) const; 451 MachineMemOperand::Flags getAtomicMemOperandFlags(const Instruction &AI, 452 const DataLayout &DL) const; 453 isSelectSupported(SelectSupportKind)454 virtual bool isSelectSupported(SelectSupportKind /*kind*/) const { 455 return true; 456 } 457 458 /// Return true if the @llvm.get.active.lane.mask intrinsic should be expanded 459 /// using generic code in SelectionDAGBuilder. shouldExpandGetActiveLaneMask(EVT VT,EVT OpVT)460 virtual bool shouldExpandGetActiveLaneMask(EVT VT, EVT OpVT) const { 461 return true; 462 } 463 shouldExpandGetVectorLength(EVT CountVT,unsigned VF,bool IsScalable)464 virtual bool shouldExpandGetVectorLength(EVT CountVT, unsigned VF, 465 bool IsScalable) const { 466 return true; 467 } 468 469 /// Return true if the @llvm.experimental.cttz.elts intrinsic should be 470 /// expanded using generic code in SelectionDAGBuilder. shouldExpandCttzElements(EVT VT)471 virtual bool shouldExpandCttzElements(EVT VT) const { return true; } 472 473 /// Return the minimum number of bits required to hold the maximum possible 474 /// number of trailing zero vector elements. 475 unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC, 476 bool ZeroIsPoison, 477 const ConstantRange *VScaleRange) const; 478 479 // Return true if op(vecreduce(x), vecreduce(y)) should be reassociated to 480 // vecreduce(op(x, y)) for the reduction opcode RedOpc. shouldReassociateReduction(unsigned RedOpc,EVT VT)481 virtual bool shouldReassociateReduction(unsigned RedOpc, EVT VT) const { 482 return true; 483 } 484 485 /// Return true if it is profitable to convert a select of FP constants into 486 /// a constant pool load whose address depends on the select condition. The 487 /// parameter may be used to differentiate a select with FP compare from 488 /// integer compare. reduceSelectOfFPConstantLoads(EVT CmpOpVT)489 virtual bool reduceSelectOfFPConstantLoads(EVT CmpOpVT) const { 490 return true; 491 } 492 493 /// Return true if multiple condition registers are available. hasMultipleConditionRegisters()494 bool hasMultipleConditionRegisters() const { 495 return HasMultipleConditionRegisters; 496 } 497 498 /// Return true if the target has BitExtract instructions. hasExtractBitsInsn()499 bool hasExtractBitsInsn() const { return HasExtractBitsInsn; } 500 501 /// Return the preferred vector type legalization action. 502 virtual TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT)503 getPreferredVectorAction(MVT VT) const { 504 // The default action for one element vectors is to scalarize 505 if (VT.getVectorElementCount().isScalar()) 506 return TypeScalarizeVector; 507 // The default action for an odd-width vector is to widen. 508 if (!VT.isPow2VectorType()) 509 return TypeWidenVector; 510 // The default action for other vectors is to promote 511 return TypePromoteInteger; 512 } 513 514 // Return true if the half type should be promoted using soft promotion rules 515 // where each operation is promoted to f32 individually, then converted to 516 // fp16. The default behavior is to promote chains of operations, keeping 517 // intermediate results in f32 precision and range. softPromoteHalfType()518 virtual bool softPromoteHalfType() const { return false; } 519 520 // Return true if, for soft-promoted half, the half type should be passed 521 // passed to and returned from functions as f32. The default behavior is to 522 // pass as i16. If soft-promoted half is not used, this function is ignored 523 // and values are always passed and returned as f32. useFPRegsForHalfType()524 virtual bool useFPRegsForHalfType() const { return false; } 525 526 // There are two general methods for expanding a BUILD_VECTOR node: 527 // 1. Use SCALAR_TO_VECTOR on the defined scalar values and then shuffle 528 // them together. 529 // 2. Build the vector on the stack and then load it. 530 // If this function returns true, then method (1) will be used, subject to 531 // the constraint that all of the necessary shuffles are legal (as determined 532 // by isShuffleMaskLegal). If this function returns false, then method (2) is 533 // always used. The vector type, and the number of defined values, are 534 // provided. 535 virtual bool shouldExpandBuildVectorWithShuffles(EVT,unsigned DefinedValues)536 shouldExpandBuildVectorWithShuffles(EVT /* VT */, 537 unsigned DefinedValues) const { 538 return DefinedValues < 3; 539 } 540 541 /// Return true if integer divide is usually cheaper than a sequence of 542 /// several shifts, adds, and multiplies for this target. 543 /// The definition of "cheaper" may depend on whether we're optimizing 544 /// for speed or for size. isIntDivCheap(EVT VT,AttributeList Attr)545 virtual bool isIntDivCheap(EVT VT, AttributeList Attr) const { return false; } 546 547 /// Return true if the target can handle a standalone remainder operation. hasStandaloneRem(EVT VT)548 virtual bool hasStandaloneRem(EVT VT) const { 549 return true; 550 } 551 552 /// Return true if SQRT(X) shouldn't be replaced with X*RSQRT(X). isFsqrtCheap(SDValue X,SelectionDAG & DAG)553 virtual bool isFsqrtCheap(SDValue X, SelectionDAG &DAG) const { 554 // Default behavior is to replace SQRT(X) with X*RSQRT(X). 555 return false; 556 } 557 558 /// Reciprocal estimate status values used by the functions below. 559 enum ReciprocalEstimate : int { 560 Unspecified = -1, 561 Disabled = 0, 562 Enabled = 1 563 }; 564 565 /// Return a ReciprocalEstimate enum value for a square root of the given type 566 /// based on the function's attributes. If the operation is not overridden by 567 /// the function's attributes, "Unspecified" is returned and target defaults 568 /// are expected to be used for instruction selection. 569 int getRecipEstimateSqrtEnabled(EVT VT, MachineFunction &MF) const; 570 571 /// Return a ReciprocalEstimate enum value for a division of the given type 572 /// based on the function's attributes. If the operation is not overridden by 573 /// the function's attributes, "Unspecified" is returned and target defaults 574 /// are expected to be used for instruction selection. 575 int getRecipEstimateDivEnabled(EVT VT, MachineFunction &MF) const; 576 577 /// Return the refinement step count for a square root of the given type based 578 /// on the function's attributes. If the operation is not overridden by 579 /// the function's attributes, "Unspecified" is returned and target defaults 580 /// are expected to be used for instruction selection. 581 int getSqrtRefinementSteps(EVT VT, MachineFunction &MF) const; 582 583 /// Return the refinement step count for a division of the given type based 584 /// on the function's attributes. If the operation is not overridden by 585 /// the function's attributes, "Unspecified" is returned and target defaults 586 /// are expected to be used for instruction selection. 587 int getDivRefinementSteps(EVT VT, MachineFunction &MF) const; 588 589 /// Returns true if target has indicated at least one type should be bypassed. isSlowDivBypassed()590 bool isSlowDivBypassed() const { return !BypassSlowDivWidths.empty(); } 591 592 /// Returns map of slow types for division or remainder with corresponding 593 /// fast types getBypassSlowDivWidths()594 const DenseMap<unsigned int, unsigned int> &getBypassSlowDivWidths() const { 595 return BypassSlowDivWidths; 596 } 597 598 /// Return true only if vscale must be a power of two. isVScaleKnownToBeAPowerOfTwo()599 virtual bool isVScaleKnownToBeAPowerOfTwo() const { return false; } 600 601 /// Return true if Flow Control is an expensive operation that should be 602 /// avoided. isJumpExpensive()603 bool isJumpExpensive() const { return JumpIsExpensive; } 604 605 // Costs parameters used by 606 // SelectionDAGBuilder::shouldKeepJumpConditionsTogether. 607 // shouldKeepJumpConditionsTogether will use these parameter value to 608 // determine if two conditions in the form `br (and/or cond1, cond2)` should 609 // be split into two branches or left as one. 610 // 611 // BaseCost is the cost threshold (in latency). If the estimated latency of 612 // computing both `cond1` and `cond2` is below the cost of just computing 613 // `cond1` + BaseCost, the two conditions will be kept together. Otherwise 614 // they will be split. 615 // 616 // LikelyBias increases BaseCost if branch probability info indicates that it 617 // is likely that both `cond1` and `cond2` will be computed. 618 // 619 // UnlikelyBias decreases BaseCost if branch probability info indicates that 620 // it is likely that both `cond1` and `cond2` will be computed. 621 // 622 // Set any field to -1 to make it ignored (setting BaseCost to -1 results in 623 // `shouldKeepJumpConditionsTogether` always returning false). 624 struct CondMergingParams { 625 int BaseCost; 626 int LikelyBias; 627 int UnlikelyBias; 628 }; 629 // Return params for deciding if we should keep two branch conditions merged 630 // or split them into two separate branches. 631 // Arg0: The binary op joining the two conditions (and/or). 632 // Arg1: The first condition (cond1) 633 // Arg2: The second condition (cond2) 634 virtual CondMergingParams getJumpConditionMergingParams(Instruction::BinaryOps,const Value *,const Value *)635 getJumpConditionMergingParams(Instruction::BinaryOps, const Value *, 636 const Value *) const { 637 // -1 will always result in splitting. 638 return {-1, -1, -1}; 639 } 640 641 /// Return true if selects are only cheaper than branches if the branch is 642 /// unlikely to be predicted right. isPredictableSelectExpensive()643 bool isPredictableSelectExpensive() const { 644 return PredictableSelectIsExpensive; 645 } 646 fallBackToDAGISel(const Instruction & Inst)647 virtual bool fallBackToDAGISel(const Instruction &Inst) const { 648 return false; 649 } 650 651 /// Return true if the following transform is beneficial: 652 /// fold (conv (load x)) -> (load (conv*)x) 653 /// On architectures that don't natively support some vector loads 654 /// efficiently, casting the load to a smaller vector of larger types and 655 /// loading is more efficient, however, this can be undone by optimizations in 656 /// dag combiner. 657 virtual bool isLoadBitCastBeneficial(EVT LoadVT, EVT BitcastVT, 658 const SelectionDAG &DAG, 659 const MachineMemOperand &MMO) const; 660 661 /// Return true if the following transform is beneficial: 662 /// (store (y (conv x)), y*)) -> (store x, (x*)) isStoreBitCastBeneficial(EVT StoreVT,EVT BitcastVT,const SelectionDAG & DAG,const MachineMemOperand & MMO)663 virtual bool isStoreBitCastBeneficial(EVT StoreVT, EVT BitcastVT, 664 const SelectionDAG &DAG, 665 const MachineMemOperand &MMO) const { 666 // Default to the same logic as loads. 667 return isLoadBitCastBeneficial(StoreVT, BitcastVT, DAG, MMO); 668 } 669 670 /// Return true if it is expected to be cheaper to do a store of vector 671 /// constant with the given size and type for the address space than to 672 /// store the individual scalar element constants. storeOfVectorConstantIsCheap(bool IsZero,EVT MemVT,unsigned NumElem,unsigned AddrSpace)673 virtual bool storeOfVectorConstantIsCheap(bool IsZero, EVT MemVT, 674 unsigned NumElem, 675 unsigned AddrSpace) const { 676 return IsZero; 677 } 678 679 /// Allow store merging for the specified type after legalization in addition 680 /// to before legalization. This may transform stores that do not exist 681 /// earlier (for example, stores created from intrinsics). mergeStoresAfterLegalization(EVT MemVT)682 virtual bool mergeStoresAfterLegalization(EVT MemVT) const { 683 return true; 684 } 685 686 /// Returns if it's reasonable to merge stores to MemVT size. canMergeStoresTo(unsigned AS,EVT MemVT,const MachineFunction & MF)687 virtual bool canMergeStoresTo(unsigned AS, EVT MemVT, 688 const MachineFunction &MF) const { 689 return true; 690 } 691 692 /// Return true if it is cheap to speculate a call to intrinsic cttz. isCheapToSpeculateCttz(Type * Ty)693 virtual bool isCheapToSpeculateCttz(Type *Ty) const { 694 return false; 695 } 696 697 /// Return true if it is cheap to speculate a call to intrinsic ctlz. isCheapToSpeculateCtlz(Type * Ty)698 virtual bool isCheapToSpeculateCtlz(Type *Ty) const { 699 return false; 700 } 701 702 /// Return true if ctlz instruction is fast. isCtlzFast()703 virtual bool isCtlzFast() const { 704 return false; 705 } 706 707 /// Return true if ctpop instruction is fast. isCtpopFast(EVT VT)708 virtual bool isCtpopFast(EVT VT) const { 709 return isOperationLegal(ISD::CTPOP, VT); 710 } 711 712 /// Return the maximum number of "x & (x - 1)" operations that can be done 713 /// instead of deferring to a custom CTPOP. getCustomCtpopCost(EVT VT,ISD::CondCode Cond)714 virtual unsigned getCustomCtpopCost(EVT VT, ISD::CondCode Cond) const { 715 return 1; 716 } 717 718 /// Return true if instruction generated for equality comparison is folded 719 /// with instruction generated for signed comparison. isEqualityCmpFoldedWithSignedCmp()720 virtual bool isEqualityCmpFoldedWithSignedCmp() const { return true; } 721 722 /// Return true if the heuristic to prefer icmp eq zero should be used in code 723 /// gen prepare. preferZeroCompareBranch()724 virtual bool preferZeroCompareBranch() const { return false; } 725 726 /// Return true if it is cheaper to split the store of a merged int val 727 /// from a pair of smaller values into multiple stores. isMultiStoresCheaperThanBitsMerge(EVT LTy,EVT HTy)728 virtual bool isMultiStoresCheaperThanBitsMerge(EVT LTy, EVT HTy) const { 729 return false; 730 } 731 732 /// Return if the target supports combining a 733 /// chain like: 734 /// \code 735 /// %andResult = and %val1, #mask 736 /// %icmpResult = icmp %andResult, 0 737 /// \endcode 738 /// into a single machine instruction of a form like: 739 /// \code 740 /// cc = test %register, #mask 741 /// \endcode isMaskAndCmp0FoldingBeneficial(const Instruction & AndI)742 virtual bool isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const { 743 return false; 744 } 745 746 /// Return true if it is valid to merge the TargetMMOFlags in two SDNodes. 747 virtual bool areTwoSDNodeTargetMMOFlagsMergeable(const MemSDNode & NodeX,const MemSDNode & NodeY)748 areTwoSDNodeTargetMMOFlagsMergeable(const MemSDNode &NodeX, 749 const MemSDNode &NodeY) const { 750 return true; 751 } 752 753 /// Use bitwise logic to make pairs of compares more efficient. For example: 754 /// and (seteq A, B), (seteq C, D) --> seteq (or (xor A, B), (xor C, D)), 0 755 /// This should be true when it takes more than one instruction to lower 756 /// setcc (cmp+set on x86 scalar), when bitwise ops are faster than logic on 757 /// condition bits (crand on PowerPC), and/or when reducing cmp+br is a win. convertSetCCLogicToBitwiseLogic(EVT VT)758 virtual bool convertSetCCLogicToBitwiseLogic(EVT VT) const { 759 return false; 760 } 761 762 /// Return the preferred operand type if the target has a quick way to compare 763 /// integer values of the given size. Assume that any legal integer type can 764 /// be compared efficiently. Targets may override this to allow illegal wide 765 /// types to return a vector type if there is support to compare that type. hasFastEqualityCompare(unsigned NumBits)766 virtual MVT hasFastEqualityCompare(unsigned NumBits) const { 767 MVT VT = MVT::getIntegerVT(NumBits); 768 return isTypeLegal(VT) ? VT : MVT::INVALID_SIMPLE_VALUE_TYPE; 769 } 770 771 /// Return true if the target should transform: 772 /// (X & Y) == Y ---> (~X & Y) == 0 773 /// (X & Y) != Y ---> (~X & Y) != 0 774 /// 775 /// This may be profitable if the target has a bitwise and-not operation that 776 /// sets comparison flags. A target may want to limit the transformation based 777 /// on the type of Y or if Y is a constant. 778 /// 779 /// Note that the transform will not occur if Y is known to be a power-of-2 780 /// because a mask and compare of a single bit can be handled by inverting the 781 /// predicate, for example: 782 /// (X & 8) == 8 ---> (X & 8) != 0 hasAndNotCompare(SDValue Y)783 virtual bool hasAndNotCompare(SDValue Y) const { 784 return false; 785 } 786 787 /// Return true if the target has a bitwise and-not operation: 788 /// X = ~A & B 789 /// This can be used to simplify select or other instructions. hasAndNot(SDValue X)790 virtual bool hasAndNot(SDValue X) const { 791 // If the target has the more complex version of this operation, assume that 792 // it has this operation too. 793 return hasAndNotCompare(X); 794 } 795 796 /// Return true if the target has a bit-test instruction: 797 /// (X & (1 << Y)) ==/!= 0 798 /// This knowledge can be used to prevent breaking the pattern, 799 /// or creating it if it could be recognized. hasBitTest(SDValue X,SDValue Y)800 virtual bool hasBitTest(SDValue X, SDValue Y) const { return false; } 801 802 /// There are two ways to clear extreme bits (either low or high): 803 /// Mask: x & (-1 << y) (the instcombine canonical form) 804 /// Shifts: x >> y << y 805 /// Return true if the variant with 2 variable shifts is preferred. 806 /// Return false if there is no preference. shouldFoldMaskToVariableShiftPair(SDValue X)807 virtual bool shouldFoldMaskToVariableShiftPair(SDValue X) const { 808 // By default, let's assume that no one prefers shifts. 809 return false; 810 } 811 812 /// Return true if it is profitable to fold a pair of shifts into a mask. 813 /// This is usually true on most targets. But some targets, like Thumb1, 814 /// have immediate shift instructions, but no immediate "and" instruction; 815 /// this makes the fold unprofitable. shouldFoldConstantShiftPairToMask(const SDNode * N,CombineLevel Level)816 virtual bool shouldFoldConstantShiftPairToMask(const SDNode *N, 817 CombineLevel Level) const { 818 return true; 819 } 820 821 /// Should we tranform the IR-optimal check for whether given truncation 822 /// down into KeptBits would be truncating or not: 823 /// (add %x, (1 << (KeptBits-1))) srccond (1 << KeptBits) 824 /// Into it's more traditional form: 825 /// ((%x << C) a>> C) dstcond %x 826 /// Return true if we should transform. 827 /// Return false if there is no preference. shouldTransformSignedTruncationCheck(EVT XVT,unsigned KeptBits)828 virtual bool shouldTransformSignedTruncationCheck(EVT XVT, 829 unsigned KeptBits) const { 830 // By default, let's assume that no one prefers shifts. 831 return false; 832 } 833 834 /// Given the pattern 835 /// (X & (C l>>/<< Y)) ==/!= 0 836 /// return true if it should be transformed into: 837 /// ((X <</l>> Y) & C) ==/!= 0 838 /// WARNING: if 'X' is a constant, the fold may deadlock! 839 /// FIXME: we could avoid passing XC, but we can't use isConstOrConstSplat() 840 /// here because it can end up being not linked in. shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd(SDValue X,ConstantSDNode * XC,ConstantSDNode * CC,SDValue Y,unsigned OldShiftOpcode,unsigned NewShiftOpcode,SelectionDAG & DAG)841 virtual bool shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd( 842 SDValue X, ConstantSDNode *XC, ConstantSDNode *CC, SDValue Y, 843 unsigned OldShiftOpcode, unsigned NewShiftOpcode, 844 SelectionDAG &DAG) const { 845 if (hasBitTest(X, Y)) { 846 // One interesting pattern that we'd want to form is 'bit test': 847 // ((1 << Y) & C) ==/!= 0 848 // But we also need to be careful not to try to reverse that fold. 849 850 // Is this '1 << Y' ? 851 if (OldShiftOpcode == ISD::SHL && CC->isOne()) 852 return false; // Keep the 'bit test' pattern. 853 854 // Will it be '1 << Y' after the transform ? 855 if (XC && NewShiftOpcode == ISD::SHL && XC->isOne()) 856 return true; // Do form the 'bit test' pattern. 857 } 858 859 // If 'X' is a constant, and we transform, then we will immediately 860 // try to undo the fold, thus causing endless combine loop. 861 // So by default, let's assume everyone prefers the fold 862 // iff 'X' is not a constant. 863 return !XC; 864 } 865 866 // Return true if its desirable to perform the following transform: 867 // (fmul C, (uitofp Pow2)) 868 // -> (bitcast_to_FP (add (bitcast_to_INT C), Log2(Pow2) << mantissa)) 869 // (fdiv C, (uitofp Pow2)) 870 // -> (bitcast_to_FP (sub (bitcast_to_INT C), Log2(Pow2) << mantissa)) 871 // 872 // This is only queried after we have verified the transform will be bitwise 873 // equals. 874 // 875 // SDNode *N : The FDiv/FMul node we want to transform. 876 // SDValue FPConst: The Float constant operand in `N`. 877 // SDValue IntPow2: The Integer power of 2 operand in `N`. optimizeFMulOrFDivAsShiftAddBitcast(SDNode * N,SDValue FPConst,SDValue IntPow2)878 virtual bool optimizeFMulOrFDivAsShiftAddBitcast(SDNode *N, SDValue FPConst, 879 SDValue IntPow2) const { 880 // Default to avoiding fdiv which is often very expensive. 881 return N->getOpcode() == ISD::FDIV; 882 } 883 884 // Given: 885 // (icmp eq/ne (and X, C0), (shift X, C1)) 886 // or 887 // (icmp eq/ne X, (rotate X, CPow2)) 888 889 // If C0 is a mask or shifted mask and the shift amt (C1) isolates the 890 // remaining bits (i.e something like `(x64 & UINT32_MAX) == (x64 >> 32)`) 891 // Do we prefer the shift to be shift-right, shift-left, or rotate. 892 // Note: Its only valid to convert the rotate version to the shift version iff 893 // the shift-amt (`C1`) is a power of 2 (including 0). 894 // If ShiftOpc (current Opcode) is returned, do nothing. preferedOpcodeForCmpEqPiecesOfOperand(EVT VT,unsigned ShiftOpc,bool MayTransformRotate,const APInt & ShiftOrRotateAmt,const std::optional<APInt> & AndMask)895 virtual unsigned preferedOpcodeForCmpEqPiecesOfOperand( 896 EVT VT, unsigned ShiftOpc, bool MayTransformRotate, 897 const APInt &ShiftOrRotateAmt, 898 const std::optional<APInt> &AndMask) const { 899 return ShiftOpc; 900 } 901 902 /// These two forms are equivalent: 903 /// sub %y, (xor %x, -1) 904 /// add (add %x, 1), %y 905 /// The variant with two add's is IR-canonical. 906 /// Some targets may prefer one to the other. preferIncOfAddToSubOfNot(EVT VT)907 virtual bool preferIncOfAddToSubOfNot(EVT VT) const { 908 // By default, let's assume that everyone prefers the form with two add's. 909 return true; 910 } 911 912 // By default prefer folding (abs (sub nsw x, y)) -> abds(x, y). Some targets 913 // may want to avoid this to prevent loss of sub_nsw pattern. preferABDSToABSWithNSW(EVT VT)914 virtual bool preferABDSToABSWithNSW(EVT VT) const { 915 return true; 916 } 917 918 // Return true if the target wants to transform Op(Splat(X)) -> Splat(Op(X)) preferScalarizeSplat(SDNode * N)919 virtual bool preferScalarizeSplat(SDNode *N) const { return true; } 920 921 // Return true if the target wants to transform: 922 // (TruncVT truncate(sext_in_reg(VT X, ExtVT)) 923 // -> (TruncVT sext_in_reg(truncate(VT X), ExtVT)) 924 // Some targets might prefer pre-sextinreg to improve truncation/saturation. preferSextInRegOfTruncate(EVT TruncVT,EVT VT,EVT ExtVT)925 virtual bool preferSextInRegOfTruncate(EVT TruncVT, EVT VT, EVT ExtVT) const { 926 return true; 927 } 928 929 /// Return true if the target wants to use the optimization that 930 /// turns ext(promotableInst1(...(promotableInstN(load)))) into 931 /// promotedInst1(...(promotedInstN(ext(load)))). enableExtLdPromotion()932 bool enableExtLdPromotion() const { return EnableExtLdPromotion; } 933 934 /// Return true if the target can combine store(extractelement VectorTy, 935 /// Idx). 936 /// \p Cost[out] gives the cost of that transformation when this is true. canCombineStoreAndExtract(Type * VectorTy,Value * Idx,unsigned & Cost)937 virtual bool canCombineStoreAndExtract(Type *VectorTy, Value *Idx, 938 unsigned &Cost) const { 939 return false; 940 } 941 942 /// Return true if the target shall perform extract vector element and store 943 /// given that the vector is known to be splat of constant. 944 /// \p Index[out] gives the index of the vector element to be extracted when 945 /// this is true. shallExtractConstSplatVectorElementToStore(Type * VectorTy,unsigned ElemSizeInBits,unsigned & Index)946 virtual bool shallExtractConstSplatVectorElementToStore( 947 Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const { 948 return false; 949 } 950 951 /// Return true if inserting a scalar into a variable element of an undef 952 /// vector is more efficiently handled by splatting the scalar instead. shouldSplatInsEltVarIndex(EVT)953 virtual bool shouldSplatInsEltVarIndex(EVT) const { 954 return false; 955 } 956 957 /// Return true if target always benefits from combining into FMA for a 958 /// given value type. This must typically return false on targets where FMA 959 /// takes more cycles to execute than FADD. enableAggressiveFMAFusion(EVT VT)960 virtual bool enableAggressiveFMAFusion(EVT VT) const { return false; } 961 962 /// Return true if target always benefits from combining into FMA for a 963 /// given value type. This must typically return false on targets where FMA 964 /// takes more cycles to execute than FADD. enableAggressiveFMAFusion(LLT Ty)965 virtual bool enableAggressiveFMAFusion(LLT Ty) const { return false; } 966 967 /// Return the ValueType of the result of SETCC operations. 968 virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, 969 EVT VT) const; 970 971 /// Return the ValueType for comparison libcalls. Comparison libcalls include 972 /// floating point comparison calls, and Ordered/Unordered check calls on 973 /// floating point numbers. 974 virtual 975 MVT::SimpleValueType getCmpLibcallReturnType() const; 976 977 /// For targets without i1 registers, this gives the nature of the high-bits 978 /// of boolean values held in types wider than i1. 979 /// 980 /// "Boolean values" are special true/false values produced by nodes like 981 /// SETCC and consumed (as the condition) by nodes like SELECT and BRCOND. 982 /// Not to be confused with general values promoted from i1. Some cpus 983 /// distinguish between vectors of boolean and scalars; the isVec parameter 984 /// selects between the two kinds. For example on X86 a scalar boolean should 985 /// be zero extended from i1, while the elements of a vector of booleans 986 /// should be sign extended from i1. 987 /// 988 /// Some cpus also treat floating point types the same way as they treat 989 /// vectors instead of the way they treat scalars. getBooleanContents(bool isVec,bool isFloat)990 BooleanContent getBooleanContents(bool isVec, bool isFloat) const { 991 if (isVec) 992 return BooleanVectorContents; 993 return isFloat ? BooleanFloatContents : BooleanContents; 994 } 995 getBooleanContents(EVT Type)996 BooleanContent getBooleanContents(EVT Type) const { 997 return getBooleanContents(Type.isVector(), Type.isFloatingPoint()); 998 } 999 1000 /// Promote the given target boolean to a target boolean of the given type. 1001 /// A target boolean is an integer value, not necessarily of type i1, the bits 1002 /// of which conform to getBooleanContents. 1003 /// 1004 /// ValVT is the type of values that produced the boolean. promoteTargetBoolean(SelectionDAG & DAG,SDValue Bool,EVT ValVT)1005 SDValue promoteTargetBoolean(SelectionDAG &DAG, SDValue Bool, 1006 EVT ValVT) const { 1007 SDLoc dl(Bool); 1008 EVT BoolVT = 1009 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ValVT); 1010 ISD::NodeType ExtendCode = getExtendForContent(getBooleanContents(ValVT)); 1011 return DAG.getNode(ExtendCode, dl, BoolVT, Bool); 1012 } 1013 1014 /// Return target scheduling preference. getSchedulingPreference()1015 Sched::Preference getSchedulingPreference() const { 1016 return SchedPreferenceInfo; 1017 } 1018 1019 /// Some scheduler, e.g. hybrid, can switch to different scheduling heuristics 1020 /// for different nodes. This function returns the preference (or none) for 1021 /// the given node. getSchedulingPreference(SDNode *)1022 virtual Sched::Preference getSchedulingPreference(SDNode *) const { 1023 return Sched::None; 1024 } 1025 1026 /// Return the register class that should be used for the specified value 1027 /// type. 1028 virtual const TargetRegisterClass *getRegClassFor(MVT VT, bool isDivergent = false) const { 1029 (void)isDivergent; 1030 const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy]; 1031 assert(RC && "This value type is not natively supported!"); 1032 return RC; 1033 } 1034 1035 /// Allows target to decide about the register class of the 1036 /// specific value that is live outside the defining block. 1037 /// Returns true if the value needs uniform register class. requiresUniformRegister(MachineFunction & MF,const Value *)1038 virtual bool requiresUniformRegister(MachineFunction &MF, 1039 const Value *) const { 1040 return false; 1041 } 1042 1043 /// Return the 'representative' register class for the specified value 1044 /// type. 1045 /// 1046 /// The 'representative' register class is the largest legal super-reg 1047 /// register class for the register class of the value type. For example, on 1048 /// i386 the rep register class for i8, i16, and i32 are GR32; while the rep 1049 /// register class is GR64 on x86_64. getRepRegClassFor(MVT VT)1050 virtual const TargetRegisterClass *getRepRegClassFor(MVT VT) const { 1051 const TargetRegisterClass *RC = RepRegClassForVT[VT.SimpleTy]; 1052 return RC; 1053 } 1054 1055 /// Return the cost of the 'representative' register class for the specified 1056 /// value type. getRepRegClassCostFor(MVT VT)1057 virtual uint8_t getRepRegClassCostFor(MVT VT) const { 1058 return RepRegClassCostForVT[VT.SimpleTy]; 1059 } 1060 1061 /// Return the preferred strategy to legalize tihs SHIFT instruction, with 1062 /// \p ExpansionFactor being the recursion depth - how many expansion needed. 1063 enum class ShiftLegalizationStrategy { 1064 ExpandToParts, 1065 ExpandThroughStack, 1066 LowerToLibcall 1067 }; 1068 virtual ShiftLegalizationStrategy preferredShiftLegalizationStrategy(SelectionDAG & DAG,SDNode * N,unsigned ExpansionFactor)1069 preferredShiftLegalizationStrategy(SelectionDAG &DAG, SDNode *N, 1070 unsigned ExpansionFactor) const { 1071 if (ExpansionFactor == 1) 1072 return ShiftLegalizationStrategy::ExpandToParts; 1073 return ShiftLegalizationStrategy::ExpandThroughStack; 1074 } 1075 1076 /// Return true if the target has native support for the specified value type. 1077 /// This means that it has a register that directly holds it without 1078 /// promotions or expansions. isTypeLegal(EVT VT)1079 bool isTypeLegal(EVT VT) const { 1080 assert(!VT.isSimple() || 1081 (unsigned)VT.getSimpleVT().SimpleTy < std::size(RegClassForVT)); 1082 return VT.isSimple() && RegClassForVT[VT.getSimpleVT().SimpleTy] != nullptr; 1083 } 1084 1085 class ValueTypeActionImpl { 1086 /// ValueTypeActions - For each value type, keep a LegalizeTypeAction enum 1087 /// that indicates how instruction selection should deal with the type. 1088 LegalizeTypeAction ValueTypeActions[MVT::VALUETYPE_SIZE]; 1089 1090 public: ValueTypeActionImpl()1091 ValueTypeActionImpl() { 1092 std::fill(std::begin(ValueTypeActions), std::end(ValueTypeActions), 1093 TypeLegal); 1094 } 1095 getTypeAction(MVT VT)1096 LegalizeTypeAction getTypeAction(MVT VT) const { 1097 return ValueTypeActions[VT.SimpleTy]; 1098 } 1099 setTypeAction(MVT VT,LegalizeTypeAction Action)1100 void setTypeAction(MVT VT, LegalizeTypeAction Action) { 1101 ValueTypeActions[VT.SimpleTy] = Action; 1102 } 1103 }; 1104 getValueTypeActions()1105 const ValueTypeActionImpl &getValueTypeActions() const { 1106 return ValueTypeActions; 1107 } 1108 1109 /// Return pair that represents the legalization kind (first) that needs to 1110 /// happen to EVT (second) in order to type-legalize it. 1111 /// 1112 /// First: how we should legalize values of this type, either it is already 1113 /// legal (return 'Legal') or we need to promote it to a larger type (return 1114 /// 'Promote'), or we need to expand it into multiple registers of smaller 1115 /// integer type (return 'Expand'). 'Custom' is not an option. 1116 /// 1117 /// Second: for types supported by the target, this is an identity function. 1118 /// For types that must be promoted to larger types, this returns the larger 1119 /// type to promote to. For integer types that are larger than the largest 1120 /// integer register, this contains one step in the expansion to get to the 1121 /// smaller register. For illegal floating point types, this returns the 1122 /// integer type to transform to. 1123 LegalizeKind getTypeConversion(LLVMContext &Context, EVT VT) const; 1124 1125 /// Return how we should legalize values of this type, either it is already 1126 /// legal (return 'Legal') or we need to promote it to a larger type (return 1127 /// 'Promote'), or we need to expand it into multiple registers of smaller 1128 /// integer type (return 'Expand'). 'Custom' is not an option. getTypeAction(LLVMContext & Context,EVT VT)1129 LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const { 1130 return getTypeConversion(Context, VT).first; 1131 } getTypeAction(MVT VT)1132 LegalizeTypeAction getTypeAction(MVT VT) const { 1133 return ValueTypeActions.getTypeAction(VT); 1134 } 1135 1136 /// For types supported by the target, this is an identity function. For 1137 /// types that must be promoted to larger types, this returns the larger type 1138 /// to promote to. For integer types that are larger than the largest integer 1139 /// register, this contains one step in the expansion to get to the smaller 1140 /// register. For illegal floating point types, this returns the integer type 1141 /// to transform to. getTypeToTransformTo(LLVMContext & Context,EVT VT)1142 virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const { 1143 return getTypeConversion(Context, VT).second; 1144 } 1145 1146 /// For types supported by the target, this is an identity function. For 1147 /// types that must be expanded (i.e. integer types that are larger than the 1148 /// largest integer register or illegal floating point types), this returns 1149 /// the largest legal type it will be expanded to. getTypeToExpandTo(LLVMContext & Context,EVT VT)1150 EVT getTypeToExpandTo(LLVMContext &Context, EVT VT) const { 1151 assert(!VT.isVector()); 1152 while (true) { 1153 switch (getTypeAction(Context, VT)) { 1154 case TypeLegal: 1155 return VT; 1156 case TypeExpandInteger: 1157 VT = getTypeToTransformTo(Context, VT); 1158 break; 1159 default: 1160 llvm_unreachable("Type is not legal nor is it to be expanded!"); 1161 } 1162 } 1163 } 1164 1165 /// Vector types are broken down into some number of legal first class types. 1166 /// For example, EVT::v8f32 maps to 2 EVT::v4f32 with Altivec or SSE1, or 8 1167 /// promoted EVT::f64 values with the X86 FP stack. Similarly, EVT::v2i64 1168 /// turns into 4 EVT::i32 values with both PPC and X86. 1169 /// 1170 /// This method returns the number of registers needed, and the VT for each 1171 /// register. It also returns the VT and quantity of the intermediate values 1172 /// before they are promoted/expanded. 1173 unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, 1174 EVT &IntermediateVT, 1175 unsigned &NumIntermediates, 1176 MVT &RegisterVT) const; 1177 1178 /// Certain targets such as MIPS require that some types such as vectors are 1179 /// always broken down into scalars in some contexts. This occurs even if the 1180 /// vector type is legal. getVectorTypeBreakdownForCallingConv(LLVMContext & Context,CallingConv::ID CC,EVT VT,EVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT)1181 virtual unsigned getVectorTypeBreakdownForCallingConv( 1182 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, 1183 unsigned &NumIntermediates, MVT &RegisterVT) const { 1184 return getVectorTypeBreakdown(Context, VT, IntermediateVT, NumIntermediates, 1185 RegisterVT); 1186 } 1187 1188 struct IntrinsicInfo { 1189 unsigned opc = 0; // target opcode 1190 EVT memVT; // memory VT 1191 1192 // value representing memory location 1193 PointerUnion<const Value *, const PseudoSourceValue *> ptrVal; 1194 1195 // Fallback address space for use if ptrVal is nullptr. std::nullopt means 1196 // unknown address space. 1197 std::optional<unsigned> fallbackAddressSpace; 1198 1199 int offset = 0; // offset off of ptrVal 1200 uint64_t size = 0; // the size of the memory location 1201 // (taken from memVT if zero) 1202 MaybeAlign align = Align(1); // alignment 1203 1204 MachineMemOperand::Flags flags = MachineMemOperand::MONone; 1205 IntrinsicInfo() = default; 1206 }; 1207 1208 /// Given an intrinsic, checks if on the target the intrinsic will need to map 1209 /// to a MemIntrinsicNode (touches memory). If this is the case, it returns 1210 /// true and store the intrinsic information into the IntrinsicInfo that was 1211 /// passed to the function. getTgtMemIntrinsic(IntrinsicInfo &,const CallInst &,MachineFunction &,unsigned)1212 virtual bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, 1213 MachineFunction &, 1214 unsigned /*Intrinsic*/) const { 1215 return false; 1216 } 1217 1218 /// Returns true if the target can instruction select the specified FP 1219 /// immediate natively. If false, the legalizer will materialize the FP 1220 /// immediate as a load from a constant pool. 1221 virtual bool isFPImmLegal(const APFloat & /*Imm*/, EVT /*VT*/, 1222 bool ForCodeSize = false) const { 1223 return false; 1224 } 1225 1226 /// Targets can use this to indicate that they only support *some* 1227 /// VECTOR_SHUFFLE operations, those with specific masks. By default, if a 1228 /// target supports the VECTOR_SHUFFLE node, all mask values are assumed to be 1229 /// legal. isShuffleMaskLegal(ArrayRef<int>,EVT)1230 virtual bool isShuffleMaskLegal(ArrayRef<int> /*Mask*/, EVT /*VT*/) const { 1231 return true; 1232 } 1233 1234 /// Returns true if the operation can trap for the value type. 1235 /// 1236 /// VT must be a legal type. By default, we optimistically assume most 1237 /// operations don't trap except for integer divide and remainder. 1238 virtual bool canOpTrap(unsigned Op, EVT VT) const; 1239 1240 /// Similar to isShuffleMaskLegal. Targets can use this to indicate if there 1241 /// is a suitable VECTOR_SHUFFLE that can be used to replace a VAND with a 1242 /// constant pool entry. isVectorClearMaskLegal(ArrayRef<int>,EVT)1243 virtual bool isVectorClearMaskLegal(ArrayRef<int> /*Mask*/, 1244 EVT /*VT*/) const { 1245 return false; 1246 } 1247 1248 /// How to legalize this custom operation? getCustomOperationAction(SDNode & Op)1249 virtual LegalizeAction getCustomOperationAction(SDNode &Op) const { 1250 return Legal; 1251 } 1252 1253 /// Return how this operation should be treated: either it is legal, needs to 1254 /// be promoted to a larger size, needs to be expanded to some other code 1255 /// sequence, or the target has a custom expander for it. getOperationAction(unsigned Op,EVT VT)1256 LegalizeAction getOperationAction(unsigned Op, EVT VT) const { 1257 if (VT.isExtended()) return Expand; 1258 // If a target-specific SDNode requires legalization, require the target 1259 // to provide custom legalization for it. 1260 if (Op >= std::size(OpActions[0])) 1261 return Custom; 1262 return OpActions[(unsigned)VT.getSimpleVT().SimpleTy][Op]; 1263 } 1264 1265 /// Custom method defined by each target to indicate if an operation which 1266 /// may require a scale is supported natively by the target. 1267 /// If not, the operation is illegal. isSupportedFixedPointOperation(unsigned Op,EVT VT,unsigned Scale)1268 virtual bool isSupportedFixedPointOperation(unsigned Op, EVT VT, 1269 unsigned Scale) const { 1270 return false; 1271 } 1272 1273 /// Some fixed point operations may be natively supported by the target but 1274 /// only for specific scales. This method allows for checking 1275 /// if the width is supported by the target for a given operation that may 1276 /// depend on scale. getFixedPointOperationAction(unsigned Op,EVT VT,unsigned Scale)1277 LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, 1278 unsigned Scale) const { 1279 auto Action = getOperationAction(Op, VT); 1280 if (Action != Legal) 1281 return Action; 1282 1283 // This operation is supported in this type but may only work on specific 1284 // scales. 1285 bool Supported; 1286 switch (Op) { 1287 default: 1288 llvm_unreachable("Unexpected fixed point operation."); 1289 case ISD::SMULFIX: 1290 case ISD::SMULFIXSAT: 1291 case ISD::UMULFIX: 1292 case ISD::UMULFIXSAT: 1293 case ISD::SDIVFIX: 1294 case ISD::SDIVFIXSAT: 1295 case ISD::UDIVFIX: 1296 case ISD::UDIVFIXSAT: 1297 Supported = isSupportedFixedPointOperation(Op, VT, Scale); 1298 break; 1299 } 1300 1301 return Supported ? Action : Expand; 1302 } 1303 1304 // If Op is a strict floating-point operation, return the result 1305 // of getOperationAction for the equivalent non-strict operation. getStrictFPOperationAction(unsigned Op,EVT VT)1306 LegalizeAction getStrictFPOperationAction(unsigned Op, EVT VT) const { 1307 unsigned EqOpc; 1308 switch (Op) { 1309 default: llvm_unreachable("Unexpected FP pseudo-opcode"); 1310 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 1311 case ISD::STRICT_##DAGN: EqOpc = ISD::DAGN; break; 1312 #define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 1313 case ISD::STRICT_##DAGN: EqOpc = ISD::SETCC; break; 1314 #include "llvm/IR/ConstrainedOps.def" 1315 } 1316 1317 return getOperationAction(EqOpc, VT); 1318 } 1319 1320 /// Return true if the specified operation is legal on this target or can be 1321 /// made legal with custom lowering. This is used to help guide high-level 1322 /// lowering decisions. LegalOnly is an optional convenience for code paths 1323 /// traversed pre and post legalisation. 1324 bool isOperationLegalOrCustom(unsigned Op, EVT VT, 1325 bool LegalOnly = false) const { 1326 if (LegalOnly) 1327 return isOperationLegal(Op, VT); 1328 1329 return (VT == MVT::Other || isTypeLegal(VT)) && 1330 (getOperationAction(Op, VT) == Legal || 1331 getOperationAction(Op, VT) == Custom); 1332 } 1333 1334 /// Return true if the specified operation is legal on this target or can be 1335 /// made legal using promotion. This is used to help guide high-level lowering 1336 /// decisions. LegalOnly is an optional convenience for code paths traversed 1337 /// pre and post legalisation. 1338 bool isOperationLegalOrPromote(unsigned Op, EVT VT, 1339 bool LegalOnly = false) const { 1340 if (LegalOnly) 1341 return isOperationLegal(Op, VT); 1342 1343 return (VT == MVT::Other || isTypeLegal(VT)) && 1344 (getOperationAction(Op, VT) == Legal || 1345 getOperationAction(Op, VT) == Promote); 1346 } 1347 1348 /// Return true if the specified operation is legal on this target or can be 1349 /// made legal with custom lowering or using promotion. This is used to help 1350 /// guide high-level lowering decisions. LegalOnly is an optional convenience 1351 /// for code paths traversed pre and post legalisation. 1352 bool isOperationLegalOrCustomOrPromote(unsigned Op, EVT VT, 1353 bool LegalOnly = false) const { 1354 if (LegalOnly) 1355 return isOperationLegal(Op, VT); 1356 1357 return (VT == MVT::Other || isTypeLegal(VT)) && 1358 (getOperationAction(Op, VT) == Legal || 1359 getOperationAction(Op, VT) == Custom || 1360 getOperationAction(Op, VT) == Promote); 1361 } 1362 1363 /// Return true if the operation uses custom lowering, regardless of whether 1364 /// the type is legal or not. isOperationCustom(unsigned Op,EVT VT)1365 bool isOperationCustom(unsigned Op, EVT VT) const { 1366 return getOperationAction(Op, VT) == Custom; 1367 } 1368 1369 /// Return true if lowering to a jump table is allowed. areJTsAllowed(const Function * Fn)1370 virtual bool areJTsAllowed(const Function *Fn) const { 1371 if (Fn->getFnAttribute("no-jump-tables").getValueAsBool()) 1372 return false; 1373 1374 return isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || 1375 isOperationLegalOrCustom(ISD::BRIND, MVT::Other); 1376 } 1377 1378 /// Check whether the range [Low,High] fits in a machine word. rangeFitsInWord(const APInt & Low,const APInt & High,const DataLayout & DL)1379 bool rangeFitsInWord(const APInt &Low, const APInt &High, 1380 const DataLayout &DL) const { 1381 // FIXME: Using the pointer type doesn't seem ideal. 1382 uint64_t BW = DL.getIndexSizeInBits(0u); 1383 uint64_t Range = (High - Low).getLimitedValue(UINT64_MAX - 1) + 1; 1384 return Range <= BW; 1385 } 1386 1387 /// Return true if lowering to a jump table is suitable for a set of case 1388 /// clusters which may contain \p NumCases cases, \p Range range of values. 1389 virtual bool isSuitableForJumpTable(const SwitchInst *SI, uint64_t NumCases, 1390 uint64_t Range, ProfileSummaryInfo *PSI, 1391 BlockFrequencyInfo *BFI) const; 1392 1393 /// Returns preferred type for switch condition. 1394 virtual MVT getPreferredSwitchConditionType(LLVMContext &Context, 1395 EVT ConditionVT) const; 1396 1397 /// Return true if lowering to a bit test is suitable for a set of case 1398 /// clusters which contains \p NumDests unique destinations, \p Low and 1399 /// \p High as its lowest and highest case values, and expects \p NumCmps 1400 /// case value comparisons. Check if the number of destinations, comparison 1401 /// metric, and range are all suitable. isSuitableForBitTests(unsigned NumDests,unsigned NumCmps,const APInt & Low,const APInt & High,const DataLayout & DL)1402 bool isSuitableForBitTests(unsigned NumDests, unsigned NumCmps, 1403 const APInt &Low, const APInt &High, 1404 const DataLayout &DL) const { 1405 // FIXME: I don't think NumCmps is the correct metric: a single case and a 1406 // range of cases both require only one branch to lower. Just looking at the 1407 // number of clusters and destinations should be enough to decide whether to 1408 // build bit tests. 1409 1410 // To lower a range with bit tests, the range must fit the bitwidth of a 1411 // machine word. 1412 if (!rangeFitsInWord(Low, High, DL)) 1413 return false; 1414 1415 // Decide whether it's profitable to lower this range with bit tests. Each 1416 // destination requires a bit test and branch, and there is an overall range 1417 // check branch. For a small number of clusters, separate comparisons might 1418 // be cheaper, and for many destinations, splitting the range might be 1419 // better. 1420 return (NumDests == 1 && NumCmps >= 3) || (NumDests == 2 && NumCmps >= 5) || 1421 (NumDests == 3 && NumCmps >= 6); 1422 } 1423 1424 /// Return true if the specified operation is illegal on this target or 1425 /// unlikely to be made legal with custom lowering. This is used to help guide 1426 /// high-level lowering decisions. isOperationExpand(unsigned Op,EVT VT)1427 bool isOperationExpand(unsigned Op, EVT VT) const { 1428 return (!isTypeLegal(VT) || getOperationAction(Op, VT) == Expand); 1429 } 1430 1431 /// Return true if the specified operation is legal on this target. isOperationLegal(unsigned Op,EVT VT)1432 bool isOperationLegal(unsigned Op, EVT VT) const { 1433 return (VT == MVT::Other || isTypeLegal(VT)) && 1434 getOperationAction(Op, VT) == Legal; 1435 } 1436 1437 /// Return how this load with extension should be treated: either it is legal, 1438 /// needs to be promoted to a larger size, needs to be expanded to some other 1439 /// code sequence, or the target has a custom expander for it. getLoadExtAction(unsigned ExtType,EVT ValVT,EVT MemVT)1440 LegalizeAction getLoadExtAction(unsigned ExtType, EVT ValVT, 1441 EVT MemVT) const { 1442 if (ValVT.isExtended() || MemVT.isExtended()) return Expand; 1443 unsigned ValI = (unsigned) ValVT.getSimpleVT().SimpleTy; 1444 unsigned MemI = (unsigned) MemVT.getSimpleVT().SimpleTy; 1445 assert(ExtType < ISD::LAST_LOADEXT_TYPE && ValI < MVT::VALUETYPE_SIZE && 1446 MemI < MVT::VALUETYPE_SIZE && "Table isn't big enough!"); 1447 unsigned Shift = 4 * ExtType; 1448 return (LegalizeAction)((LoadExtActions[ValI][MemI] >> Shift) & 0xf); 1449 } 1450 1451 /// Return true if the specified load with extension is legal on this target. isLoadExtLegal(unsigned ExtType,EVT ValVT,EVT MemVT)1452 bool isLoadExtLegal(unsigned ExtType, EVT ValVT, EVT MemVT) const { 1453 return getLoadExtAction(ExtType, ValVT, MemVT) == Legal; 1454 } 1455 1456 /// Return true if the specified load with extension is legal or custom 1457 /// on this target. isLoadExtLegalOrCustom(unsigned ExtType,EVT ValVT,EVT MemVT)1458 bool isLoadExtLegalOrCustom(unsigned ExtType, EVT ValVT, EVT MemVT) const { 1459 return getLoadExtAction(ExtType, ValVT, MemVT) == Legal || 1460 getLoadExtAction(ExtType, ValVT, MemVT) == Custom; 1461 } 1462 1463 /// Same as getLoadExtAction, but for atomic loads. getAtomicLoadExtAction(unsigned ExtType,EVT ValVT,EVT MemVT)1464 LegalizeAction getAtomicLoadExtAction(unsigned ExtType, EVT ValVT, 1465 EVT MemVT) const { 1466 if (ValVT.isExtended() || MemVT.isExtended()) return Expand; 1467 unsigned ValI = (unsigned)ValVT.getSimpleVT().SimpleTy; 1468 unsigned MemI = (unsigned)MemVT.getSimpleVT().SimpleTy; 1469 assert(ExtType < ISD::LAST_LOADEXT_TYPE && ValI < MVT::VALUETYPE_SIZE && 1470 MemI < MVT::VALUETYPE_SIZE && "Table isn't big enough!"); 1471 unsigned Shift = 4 * ExtType; 1472 LegalizeAction Action = 1473 (LegalizeAction)((AtomicLoadExtActions[ValI][MemI] >> Shift) & 0xf); 1474 assert((Action == Legal || Action == Expand) && 1475 "Unsupported atomic load extension action."); 1476 return Action; 1477 } 1478 1479 /// Return true if the specified atomic load with extension is legal on 1480 /// this target. isAtomicLoadExtLegal(unsigned ExtType,EVT ValVT,EVT MemVT)1481 bool isAtomicLoadExtLegal(unsigned ExtType, EVT ValVT, EVT MemVT) const { 1482 return getAtomicLoadExtAction(ExtType, ValVT, MemVT) == Legal; 1483 } 1484 1485 /// Return how this store with truncation should be treated: either it is 1486 /// legal, needs to be promoted to a larger size, needs to be expanded to some 1487 /// other code sequence, or the target has a custom expander for it. getTruncStoreAction(EVT ValVT,EVT MemVT)1488 LegalizeAction getTruncStoreAction(EVT ValVT, EVT MemVT) const { 1489 if (ValVT.isExtended() || MemVT.isExtended()) return Expand; 1490 unsigned ValI = (unsigned) ValVT.getSimpleVT().SimpleTy; 1491 unsigned MemI = (unsigned) MemVT.getSimpleVT().SimpleTy; 1492 assert(ValI < MVT::VALUETYPE_SIZE && MemI < MVT::VALUETYPE_SIZE && 1493 "Table isn't big enough!"); 1494 return TruncStoreActions[ValI][MemI]; 1495 } 1496 1497 /// Return true if the specified store with truncation is legal on this 1498 /// target. isTruncStoreLegal(EVT ValVT,EVT MemVT)1499 bool isTruncStoreLegal(EVT ValVT, EVT MemVT) const { 1500 return isTypeLegal(ValVT) && getTruncStoreAction(ValVT, MemVT) == Legal; 1501 } 1502 1503 /// Return true if the specified store with truncation has solution on this 1504 /// target. isTruncStoreLegalOrCustom(EVT ValVT,EVT MemVT)1505 bool isTruncStoreLegalOrCustom(EVT ValVT, EVT MemVT) const { 1506 return isTypeLegal(ValVT) && 1507 (getTruncStoreAction(ValVT, MemVT) == Legal || 1508 getTruncStoreAction(ValVT, MemVT) == Custom); 1509 } 1510 canCombineTruncStore(EVT ValVT,EVT MemVT,bool LegalOnly)1511 virtual bool canCombineTruncStore(EVT ValVT, EVT MemVT, 1512 bool LegalOnly) const { 1513 if (LegalOnly) 1514 return isTruncStoreLegal(ValVT, MemVT); 1515 1516 return isTruncStoreLegalOrCustom(ValVT, MemVT); 1517 } 1518 1519 /// Return how the indexed load should be treated: either it is legal, needs 1520 /// to be promoted to a larger size, needs to be expanded to some other code 1521 /// sequence, or the target has a custom expander for it. getIndexedLoadAction(unsigned IdxMode,MVT VT)1522 LegalizeAction getIndexedLoadAction(unsigned IdxMode, MVT VT) const { 1523 return getIndexedModeAction(IdxMode, VT, IMAB_Load); 1524 } 1525 1526 /// Return true if the specified indexed load is legal on this target. isIndexedLoadLegal(unsigned IdxMode,EVT VT)1527 bool isIndexedLoadLegal(unsigned IdxMode, EVT VT) const { 1528 return VT.isSimple() && 1529 (getIndexedLoadAction(IdxMode, VT.getSimpleVT()) == Legal || 1530 getIndexedLoadAction(IdxMode, VT.getSimpleVT()) == Custom); 1531 } 1532 1533 /// Return how the indexed store should be treated: either it is legal, needs 1534 /// to be promoted to a larger size, needs to be expanded to some other code 1535 /// sequence, or the target has a custom expander for it. getIndexedStoreAction(unsigned IdxMode,MVT VT)1536 LegalizeAction getIndexedStoreAction(unsigned IdxMode, MVT VT) const { 1537 return getIndexedModeAction(IdxMode, VT, IMAB_Store); 1538 } 1539 1540 /// Return true if the specified indexed load is legal on this target. isIndexedStoreLegal(unsigned IdxMode,EVT VT)1541 bool isIndexedStoreLegal(unsigned IdxMode, EVT VT) const { 1542 return VT.isSimple() && 1543 (getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Legal || 1544 getIndexedStoreAction(IdxMode, VT.getSimpleVT()) == Custom); 1545 } 1546 1547 /// Return how the indexed load should be treated: either it is legal, needs 1548 /// to be promoted to a larger size, needs to be expanded to some other code 1549 /// sequence, or the target has a custom expander for it. getIndexedMaskedLoadAction(unsigned IdxMode,MVT VT)1550 LegalizeAction getIndexedMaskedLoadAction(unsigned IdxMode, MVT VT) const { 1551 return getIndexedModeAction(IdxMode, VT, IMAB_MaskedLoad); 1552 } 1553 1554 /// Return true if the specified indexed load is legal on this target. isIndexedMaskedLoadLegal(unsigned IdxMode,EVT VT)1555 bool isIndexedMaskedLoadLegal(unsigned IdxMode, EVT VT) const { 1556 return VT.isSimple() && 1557 (getIndexedMaskedLoadAction(IdxMode, VT.getSimpleVT()) == Legal || 1558 getIndexedMaskedLoadAction(IdxMode, VT.getSimpleVT()) == Custom); 1559 } 1560 1561 /// Return how the indexed store should be treated: either it is legal, needs 1562 /// to be promoted to a larger size, needs to be expanded to some other code 1563 /// sequence, or the target has a custom expander for it. getIndexedMaskedStoreAction(unsigned IdxMode,MVT VT)1564 LegalizeAction getIndexedMaskedStoreAction(unsigned IdxMode, MVT VT) const { 1565 return getIndexedModeAction(IdxMode, VT, IMAB_MaskedStore); 1566 } 1567 1568 /// Return true if the specified indexed load is legal on this target. isIndexedMaskedStoreLegal(unsigned IdxMode,EVT VT)1569 bool isIndexedMaskedStoreLegal(unsigned IdxMode, EVT VT) const { 1570 return VT.isSimple() && 1571 (getIndexedMaskedStoreAction(IdxMode, VT.getSimpleVT()) == Legal || 1572 getIndexedMaskedStoreAction(IdxMode, VT.getSimpleVT()) == Custom); 1573 } 1574 1575 /// Returns true if the index type for a masked gather/scatter requires 1576 /// extending shouldExtendGSIndex(EVT VT,EVT & EltTy)1577 virtual bool shouldExtendGSIndex(EVT VT, EVT &EltTy) const { return false; } 1578 1579 // Returns true if Extend can be folded into the index of a masked gathers/scatters 1580 // on this target. shouldRemoveExtendFromGSIndex(SDValue Extend,EVT DataVT)1581 virtual bool shouldRemoveExtendFromGSIndex(SDValue Extend, EVT DataVT) const { 1582 return false; 1583 } 1584 1585 // Return true if the target supports a scatter/gather instruction with 1586 // indices which are scaled by the particular value. Note that all targets 1587 // must by definition support scale of 1. isLegalScaleForGatherScatter(uint64_t Scale,uint64_t ElemSize)1588 virtual bool isLegalScaleForGatherScatter(uint64_t Scale, 1589 uint64_t ElemSize) const { 1590 // MGATHER/MSCATTER are only required to support scaling by one or by the 1591 // element size. 1592 if (Scale != ElemSize && Scale != 1) 1593 return false; 1594 return true; 1595 } 1596 1597 /// Return how the condition code should be treated: either it is legal, needs 1598 /// to be expanded to some other code sequence, or the target has a custom 1599 /// expander for it. 1600 LegalizeAction getCondCodeAction(ISD::CondCode CC,MVT VT)1601 getCondCodeAction(ISD::CondCode CC, MVT VT) const { 1602 assert((unsigned)CC < std::size(CondCodeActions) && 1603 ((unsigned)VT.SimpleTy >> 3) < std::size(CondCodeActions[0]) && 1604 "Table isn't big enough!"); 1605 // See setCondCodeAction for how this is encoded. 1606 uint32_t Shift = 4 * (VT.SimpleTy & 0x7); 1607 uint32_t Value = CondCodeActions[CC][VT.SimpleTy >> 3]; 1608 LegalizeAction Action = (LegalizeAction) ((Value >> Shift) & 0xF); 1609 assert(Action != Promote && "Can't promote condition code!"); 1610 return Action; 1611 } 1612 1613 /// Return true if the specified condition code is legal on this target. isCondCodeLegal(ISD::CondCode CC,MVT VT)1614 bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const { 1615 return getCondCodeAction(CC, VT) == Legal; 1616 } 1617 1618 /// Return true if the specified condition code is legal or custom on this 1619 /// target. isCondCodeLegalOrCustom(ISD::CondCode CC,MVT VT)1620 bool isCondCodeLegalOrCustom(ISD::CondCode CC, MVT VT) const { 1621 return getCondCodeAction(CC, VT) == Legal || 1622 getCondCodeAction(CC, VT) == Custom; 1623 } 1624 1625 /// If the action for this operation is to promote, this method returns the 1626 /// ValueType to promote to. getTypeToPromoteTo(unsigned Op,MVT VT)1627 MVT getTypeToPromoteTo(unsigned Op, MVT VT) const { 1628 assert(getOperationAction(Op, VT) == Promote && 1629 "This operation isn't promoted!"); 1630 1631 // See if this has an explicit type specified. 1632 std::map<std::pair<unsigned, MVT::SimpleValueType>, 1633 MVT::SimpleValueType>::const_iterator PTTI = 1634 PromoteToType.find(std::make_pair(Op, VT.SimpleTy)); 1635 if (PTTI != PromoteToType.end()) return PTTI->second; 1636 1637 assert((VT.isInteger() || VT.isFloatingPoint()) && 1638 "Cannot autopromote this type, add it with AddPromotedToType."); 1639 1640 uint64_t VTBits = VT.getScalarSizeInBits(); 1641 MVT NVT = VT; 1642 do { 1643 NVT = (MVT::SimpleValueType)(NVT.SimpleTy+1); 1644 assert(NVT.isInteger() == VT.isInteger() && NVT != MVT::isVoid && 1645 "Didn't find type to promote to!"); 1646 } while (VTBits >= NVT.getScalarSizeInBits() || !isTypeLegal(NVT) || 1647 getOperationAction(Op, NVT) == Promote); 1648 return NVT; 1649 } 1650 1651 virtual EVT getAsmOperandValueType(const DataLayout &DL, Type *Ty, 1652 bool AllowUnknown = false) const { 1653 return getValueType(DL, Ty, AllowUnknown); 1654 } 1655 1656 /// Return the EVT corresponding to this LLVM type. This is fixed by the LLVM 1657 /// operations except for the pointer size. If AllowUnknown is true, this 1658 /// will return MVT::Other for types with no EVT counterpart (e.g. structs), 1659 /// otherwise it will assert. 1660 EVT getValueType(const DataLayout &DL, Type *Ty, 1661 bool AllowUnknown = false) const { 1662 // Lower scalar pointers to native pointer types. 1663 if (auto *PTy = dyn_cast<PointerType>(Ty)) 1664 return getPointerTy(DL, PTy->getAddressSpace()); 1665 1666 if (auto *VTy = dyn_cast<VectorType>(Ty)) { 1667 Type *EltTy = VTy->getElementType(); 1668 // Lower vectors of pointers to native pointer types. 1669 if (auto *PTy = dyn_cast<PointerType>(EltTy)) { 1670 EVT PointerTy(getPointerTy(DL, PTy->getAddressSpace())); 1671 EltTy = PointerTy.getTypeForEVT(Ty->getContext()); 1672 } 1673 return EVT::getVectorVT(Ty->getContext(), EVT::getEVT(EltTy, false), 1674 VTy->getElementCount()); 1675 } 1676 1677 return EVT::getEVT(Ty, AllowUnknown); 1678 } 1679 1680 EVT getMemValueType(const DataLayout &DL, Type *Ty, 1681 bool AllowUnknown = false) const { 1682 // Lower scalar pointers to native pointer types. 1683 if (auto *PTy = dyn_cast<PointerType>(Ty)) 1684 return getPointerMemTy(DL, PTy->getAddressSpace()); 1685 1686 if (auto *VTy = dyn_cast<VectorType>(Ty)) { 1687 Type *EltTy = VTy->getElementType(); 1688 if (auto *PTy = dyn_cast<PointerType>(EltTy)) { 1689 EVT PointerTy(getPointerMemTy(DL, PTy->getAddressSpace())); 1690 EltTy = PointerTy.getTypeForEVT(Ty->getContext()); 1691 } 1692 return EVT::getVectorVT(Ty->getContext(), EVT::getEVT(EltTy, false), 1693 VTy->getElementCount()); 1694 } 1695 1696 return getValueType(DL, Ty, AllowUnknown); 1697 } 1698 1699 1700 /// Return the MVT corresponding to this LLVM type. See getValueType. 1701 MVT getSimpleValueType(const DataLayout &DL, Type *Ty, 1702 bool AllowUnknown = false) const { 1703 return getValueType(DL, Ty, AllowUnknown).getSimpleVT(); 1704 } 1705 1706 /// Return the desired alignment for ByVal or InAlloca aggregate function 1707 /// arguments in the caller parameter area. This is the actual alignment, not 1708 /// its logarithm. 1709 virtual uint64_t getByValTypeAlignment(Type *Ty, const DataLayout &DL) const; 1710 1711 /// Return the type of registers that this ValueType will eventually require. getRegisterType(MVT VT)1712 MVT getRegisterType(MVT VT) const { 1713 assert((unsigned)VT.SimpleTy < std::size(RegisterTypeForVT)); 1714 return RegisterTypeForVT[VT.SimpleTy]; 1715 } 1716 1717 /// Return the type of registers that this ValueType will eventually require. getRegisterType(LLVMContext & Context,EVT VT)1718 MVT getRegisterType(LLVMContext &Context, EVT VT) const { 1719 if (VT.isSimple()) 1720 return getRegisterType(VT.getSimpleVT()); 1721 if (VT.isVector()) { 1722 EVT VT1; 1723 MVT RegisterVT; 1724 unsigned NumIntermediates; 1725 (void)getVectorTypeBreakdown(Context, VT, VT1, 1726 NumIntermediates, RegisterVT); 1727 return RegisterVT; 1728 } 1729 if (VT.isInteger()) { 1730 return getRegisterType(Context, getTypeToTransformTo(Context, VT)); 1731 } 1732 llvm_unreachable("Unsupported extended type!"); 1733 } 1734 1735 /// Return the number of registers that this ValueType will eventually 1736 /// require. 1737 /// 1738 /// This is one for any types promoted to live in larger registers, but may be 1739 /// more than one for types (like i64) that are split into pieces. For types 1740 /// like i140, which are first promoted then expanded, it is the number of 1741 /// registers needed to hold all the bits of the original type. For an i140 1742 /// on a 32 bit machine this means 5 registers. 1743 /// 1744 /// RegisterVT may be passed as a way to override the default settings, for 1745 /// instance with i128 inline assembly operands on SystemZ. 1746 virtual unsigned 1747 getNumRegisters(LLVMContext &Context, EVT VT, 1748 std::optional<MVT> RegisterVT = std::nullopt) const { 1749 if (VT.isSimple()) { 1750 assert((unsigned)VT.getSimpleVT().SimpleTy < 1751 std::size(NumRegistersForVT)); 1752 return NumRegistersForVT[VT.getSimpleVT().SimpleTy]; 1753 } 1754 if (VT.isVector()) { 1755 EVT VT1; 1756 MVT VT2; 1757 unsigned NumIntermediates; 1758 return getVectorTypeBreakdown(Context, VT, VT1, NumIntermediates, VT2); 1759 } 1760 if (VT.isInteger()) { 1761 unsigned BitWidth = VT.getSizeInBits(); 1762 unsigned RegWidth = getRegisterType(Context, VT).getSizeInBits(); 1763 return (BitWidth + RegWidth - 1) / RegWidth; 1764 } 1765 llvm_unreachable("Unsupported extended type!"); 1766 } 1767 1768 /// Certain combinations of ABIs, Targets and features require that types 1769 /// are legal for some operations and not for other operations. 1770 /// For MIPS all vector types must be passed through the integer register set. getRegisterTypeForCallingConv(LLVMContext & Context,CallingConv::ID CC,EVT VT)1771 virtual MVT getRegisterTypeForCallingConv(LLVMContext &Context, 1772 CallingConv::ID CC, EVT VT) const { 1773 return getRegisterType(Context, VT); 1774 } 1775 1776 /// Certain targets require unusual breakdowns of certain types. For MIPS, 1777 /// this occurs when a vector type is used, as vector are passed through the 1778 /// integer register set. getNumRegistersForCallingConv(LLVMContext & Context,CallingConv::ID CC,EVT VT)1779 virtual unsigned getNumRegistersForCallingConv(LLVMContext &Context, 1780 CallingConv::ID CC, 1781 EVT VT) const { 1782 return getNumRegisters(Context, VT); 1783 } 1784 1785 /// Certain targets have context sensitive alignment requirements, where one 1786 /// type has the alignment requirement of another type. getABIAlignmentForCallingConv(Type * ArgTy,const DataLayout & DL)1787 virtual Align getABIAlignmentForCallingConv(Type *ArgTy, 1788 const DataLayout &DL) const { 1789 return DL.getABITypeAlign(ArgTy); 1790 } 1791 1792 /// If true, then instruction selection should seek to shrink the FP constant 1793 /// of the specified type to a smaller type in order to save space and / or 1794 /// reduce runtime. ShouldShrinkFPConstant(EVT)1795 virtual bool ShouldShrinkFPConstant(EVT) const { return true; } 1796 1797 /// Return true if it is profitable to reduce a load to a smaller type. 1798 /// Example: (i16 (trunc (i32 (load x))) -> i16 load x shouldReduceLoadWidth(SDNode * Load,ISD::LoadExtType ExtTy,EVT NewVT)1799 virtual bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtTy, 1800 EVT NewVT) const { 1801 // By default, assume that it is cheaper to extract a subvector from a wide 1802 // vector load rather than creating multiple narrow vector loads. 1803 if (NewVT.isVector() && !Load->hasOneUse()) 1804 return false; 1805 1806 return true; 1807 } 1808 1809 /// Return true (the default) if it is profitable to remove a sext_inreg(x) 1810 /// where the sext is redundant, and use x directly. shouldRemoveRedundantExtend(SDValue Op)1811 virtual bool shouldRemoveRedundantExtend(SDValue Op) const { return true; } 1812 1813 /// Indicates if any padding is guaranteed to go at the most significant bits 1814 /// when storing the type to memory and the type size isn't equal to the store 1815 /// size. isPaddedAtMostSignificantBitsWhenStored(EVT VT)1816 bool isPaddedAtMostSignificantBitsWhenStored(EVT VT) const { 1817 return VT.isScalarInteger() && !VT.isByteSized(); 1818 } 1819 1820 /// When splitting a value of the specified type into parts, does the Lo 1821 /// or Hi part come first? This usually follows the endianness, except 1822 /// for ppcf128, where the Hi part always comes first. hasBigEndianPartOrdering(EVT VT,const DataLayout & DL)1823 bool hasBigEndianPartOrdering(EVT VT, const DataLayout &DL) const { 1824 return DL.isBigEndian() || VT == MVT::ppcf128; 1825 } 1826 1827 /// If true, the target has custom DAG combine transformations that it can 1828 /// perform for the specified node. hasTargetDAGCombine(ISD::NodeType NT)1829 bool hasTargetDAGCombine(ISD::NodeType NT) const { 1830 assert(unsigned(NT >> 3) < std::size(TargetDAGCombineArray)); 1831 return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7)); 1832 } 1833 getGatherAllAliasesMaxDepth()1834 unsigned getGatherAllAliasesMaxDepth() const { 1835 return GatherAllAliasesMaxDepth; 1836 } 1837 1838 /// Returns the size of the platform's va_list object. getVaListSizeInBits(const DataLayout & DL)1839 virtual unsigned getVaListSizeInBits(const DataLayout &DL) const { 1840 return getPointerTy(DL).getSizeInBits(); 1841 } 1842 1843 /// Get maximum # of store operations permitted for llvm.memset 1844 /// 1845 /// This function returns the maximum number of store operations permitted 1846 /// to replace a call to llvm.memset. The value is set by the target at the 1847 /// performance threshold for such a replacement. If OptSize is true, 1848 /// return the limit for functions that have OptSize attribute. getMaxStoresPerMemset(bool OptSize)1849 unsigned getMaxStoresPerMemset(bool OptSize) const { 1850 return OptSize ? MaxStoresPerMemsetOptSize : MaxStoresPerMemset; 1851 } 1852 1853 /// Get maximum # of store operations permitted for llvm.memcpy 1854 /// 1855 /// This function returns the maximum number of store operations permitted 1856 /// to replace a call to llvm.memcpy. The value is set by the target at the 1857 /// performance threshold for such a replacement. If OptSize is true, 1858 /// return the limit for functions that have OptSize attribute. getMaxStoresPerMemcpy(bool OptSize)1859 unsigned getMaxStoresPerMemcpy(bool OptSize) const { 1860 return OptSize ? MaxStoresPerMemcpyOptSize : MaxStoresPerMemcpy; 1861 } 1862 1863 /// \brief Get maximum # of store operations to be glued together 1864 /// 1865 /// This function returns the maximum number of store operations permitted 1866 /// to glue together during lowering of llvm.memcpy. The value is set by 1867 // the target at the performance threshold for such a replacement. getMaxGluedStoresPerMemcpy()1868 virtual unsigned getMaxGluedStoresPerMemcpy() const { 1869 return MaxGluedStoresPerMemcpy; 1870 } 1871 1872 /// Get maximum # of load operations permitted for memcmp 1873 /// 1874 /// This function returns the maximum number of load operations permitted 1875 /// to replace a call to memcmp. The value is set by the target at the 1876 /// performance threshold for such a replacement. If OptSize is true, 1877 /// return the limit for functions that have OptSize attribute. getMaxExpandSizeMemcmp(bool OptSize)1878 unsigned getMaxExpandSizeMemcmp(bool OptSize) const { 1879 return OptSize ? MaxLoadsPerMemcmpOptSize : MaxLoadsPerMemcmp; 1880 } 1881 1882 /// Get maximum # of store operations permitted for llvm.memmove 1883 /// 1884 /// This function returns the maximum number of store operations permitted 1885 /// to replace a call to llvm.memmove. The value is set by the target at the 1886 /// performance threshold for such a replacement. If OptSize is true, 1887 /// return the limit for functions that have OptSize attribute. getMaxStoresPerMemmove(bool OptSize)1888 unsigned getMaxStoresPerMemmove(bool OptSize) const { 1889 return OptSize ? MaxStoresPerMemmoveOptSize : MaxStoresPerMemmove; 1890 } 1891 1892 /// Determine if the target supports unaligned memory accesses. 1893 /// 1894 /// This function returns true if the target allows unaligned memory accesses 1895 /// of the specified type in the given address space. If true, it also returns 1896 /// a relative speed of the unaligned memory access in the last argument by 1897 /// reference. The higher the speed number the faster the operation comparing 1898 /// to a number returned by another such call. This is used, for example, in 1899 /// situations where an array copy/move/set is converted to a sequence of 1900 /// store operations. Its use helps to ensure that such replacements don't 1901 /// generate code that causes an alignment error (trap) on the target machine. 1902 virtual bool allowsMisalignedMemoryAccesses( 1903 EVT, unsigned AddrSpace = 0, Align Alignment = Align(1), 1904 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 1905 unsigned * /*Fast*/ = nullptr) const { 1906 return false; 1907 } 1908 1909 /// LLT handling variant. 1910 virtual bool allowsMisalignedMemoryAccesses( 1911 LLT, unsigned AddrSpace = 0, Align Alignment = Align(1), 1912 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 1913 unsigned * /*Fast*/ = nullptr) const { 1914 return false; 1915 } 1916 1917 /// This function returns true if the memory access is aligned or if the 1918 /// target allows this specific unaligned memory access. If the access is 1919 /// allowed, the optional final parameter returns a relative speed of the 1920 /// access (as defined by the target). 1921 bool allowsMemoryAccessForAlignment( 1922 LLVMContext &Context, const DataLayout &DL, EVT VT, 1923 unsigned AddrSpace = 0, Align Alignment = Align(1), 1924 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 1925 unsigned *Fast = nullptr) const; 1926 1927 /// Return true if the memory access of this type is aligned or if the target 1928 /// allows this specific unaligned access for the given MachineMemOperand. 1929 /// If the access is allowed, the optional final parameter returns a relative 1930 /// speed of the access (as defined by the target). 1931 bool allowsMemoryAccessForAlignment(LLVMContext &Context, 1932 const DataLayout &DL, EVT VT, 1933 const MachineMemOperand &MMO, 1934 unsigned *Fast = nullptr) const; 1935 1936 /// Return true if the target supports a memory access of this type for the 1937 /// given address space and alignment. If the access is allowed, the optional 1938 /// final parameter returns the relative speed of the access (as defined by 1939 /// the target). 1940 virtual bool 1941 allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT, 1942 unsigned AddrSpace = 0, Align Alignment = Align(1), 1943 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 1944 unsigned *Fast = nullptr) const; 1945 1946 /// Return true if the target supports a memory access of this type for the 1947 /// given MachineMemOperand. If the access is allowed, the optional 1948 /// final parameter returns the relative access speed (as defined by the 1949 /// target). 1950 bool allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT, 1951 const MachineMemOperand &MMO, 1952 unsigned *Fast = nullptr) const; 1953 1954 /// LLT handling variant. 1955 bool allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, LLT Ty, 1956 const MachineMemOperand &MMO, 1957 unsigned *Fast = nullptr) const; 1958 1959 /// Returns the target specific optimal type for load and store operations as 1960 /// a result of memset, memcpy, and memmove lowering. 1961 /// It returns EVT::Other if the type should be determined using generic 1962 /// target-independent logic. 1963 virtual EVT getOptimalMemOpType(const MemOp & Op,const AttributeList &)1964 getOptimalMemOpType(const MemOp &Op, 1965 const AttributeList & /*FuncAttributes*/) const { 1966 return MVT::Other; 1967 } 1968 1969 /// LLT returning variant. 1970 virtual LLT getOptimalMemOpLLT(const MemOp & Op,const AttributeList &)1971 getOptimalMemOpLLT(const MemOp &Op, 1972 const AttributeList & /*FuncAttributes*/) const { 1973 return LLT(); 1974 } 1975 1976 /// Returns true if it's safe to use load / store of the specified type to 1977 /// expand memcpy / memset inline. 1978 /// 1979 /// This is mostly true for all types except for some special cases. For 1980 /// example, on X86 targets without SSE2 f64 load / store are done with fldl / 1981 /// fstpl which also does type conversion. Note the specified type doesn't 1982 /// have to be legal as the hook is used before type legalization. isSafeMemOpType(MVT)1983 virtual bool isSafeMemOpType(MVT /*VT*/) const { return true; } 1984 1985 /// Return lower limit for number of blocks in a jump table. 1986 virtual unsigned getMinimumJumpTableEntries() const; 1987 1988 /// Return lower limit of the density in a jump table. 1989 unsigned getMinimumJumpTableDensity(bool OptForSize) const; 1990 1991 /// Return upper limit for number of entries in a jump table. 1992 /// Zero if no limit. 1993 unsigned getMaximumJumpTableSize() const; 1994 1995 virtual bool isJumpTableRelative() const; 1996 1997 /// If a physical register, this specifies the register that 1998 /// llvm.savestack/llvm.restorestack should save and restore. getStackPointerRegisterToSaveRestore()1999 Register getStackPointerRegisterToSaveRestore() const { 2000 return StackPointerRegisterToSaveRestore; 2001 } 2002 2003 /// If a physical register, this returns the register that receives the 2004 /// exception address on entry to an EH pad. 2005 virtual Register getExceptionPointerRegister(const Constant * PersonalityFn)2006 getExceptionPointerRegister(const Constant *PersonalityFn) const { 2007 return Register(); 2008 } 2009 2010 /// If a physical register, this returns the register that receives the 2011 /// exception typeid on entry to a landing pad. 2012 virtual Register getExceptionSelectorRegister(const Constant * PersonalityFn)2013 getExceptionSelectorRegister(const Constant *PersonalityFn) const { 2014 return Register(); 2015 } 2016 needsFixedCatchObjects()2017 virtual bool needsFixedCatchObjects() const { 2018 report_fatal_error("Funclet EH is not implemented for this target"); 2019 } 2020 2021 /// Return the minimum stack alignment of an argument. getMinStackArgumentAlignment()2022 Align getMinStackArgumentAlignment() const { 2023 return MinStackArgumentAlignment; 2024 } 2025 2026 /// Return the minimum function alignment. getMinFunctionAlignment()2027 Align getMinFunctionAlignment() const { return MinFunctionAlignment; } 2028 2029 /// Return the preferred function alignment. getPrefFunctionAlignment()2030 Align getPrefFunctionAlignment() const { return PrefFunctionAlignment; } 2031 2032 /// Return the preferred loop alignment. 2033 virtual Align getPrefLoopAlignment(MachineLoop *ML = nullptr) const; 2034 2035 /// Return the maximum amount of bytes allowed to be emitted when padding for 2036 /// alignment 2037 virtual unsigned 2038 getMaxPermittedBytesForAlignment(MachineBasicBlock *MBB) const; 2039 2040 /// Should loops be aligned even when the function is marked OptSize (but not 2041 /// MinSize). alignLoopsWithOptSize()2042 virtual bool alignLoopsWithOptSize() const { return false; } 2043 2044 /// If the target has a standard location for the stack protector guard, 2045 /// returns the address of that location. Otherwise, returns nullptr. 2046 /// DEPRECATED: please override useLoadStackGuardNode and customize 2047 /// LOAD_STACK_GUARD, or customize \@llvm.stackguard(). 2048 virtual Value *getIRStackGuard(IRBuilderBase &IRB) const; 2049 2050 /// Inserts necessary declarations for SSP (stack protection) purpose. 2051 /// Should be used only when getIRStackGuard returns nullptr. 2052 virtual void insertSSPDeclarations(Module &M) const; 2053 2054 /// Return the variable that's previously inserted by insertSSPDeclarations, 2055 /// if any, otherwise return nullptr. Should be used only when 2056 /// getIRStackGuard returns nullptr. 2057 virtual Value *getSDagStackGuard(const Module &M) const; 2058 2059 /// If this function returns true, stack protection checks should XOR the 2060 /// frame pointer (or whichever pointer is used to address locals) into the 2061 /// stack guard value before checking it. getIRStackGuard must return nullptr 2062 /// if this returns true. useStackGuardXorFP()2063 virtual bool useStackGuardXorFP() const { return false; } 2064 2065 /// If the target has a standard stack protection check function that 2066 /// performs validation and error handling, returns the function. Otherwise, 2067 /// returns nullptr. Must be previously inserted by insertSSPDeclarations. 2068 /// Should be used only when getIRStackGuard returns nullptr. 2069 virtual Function *getSSPStackGuardCheck(const Module &M) const; 2070 2071 protected: 2072 Value *getDefaultSafeStackPointerLocation(IRBuilderBase &IRB, 2073 bool UseTLS) const; 2074 2075 public: 2076 /// Returns the target-specific address of the unsafe stack pointer. 2077 virtual Value *getSafeStackPointerLocation(IRBuilderBase &IRB) const; 2078 2079 /// Returns the name of the symbol used to emit stack probes or the empty 2080 /// string if not applicable. hasStackProbeSymbol(const MachineFunction & MF)2081 virtual bool hasStackProbeSymbol(const MachineFunction &MF) const { return false; } 2082 hasInlineStackProbe(const MachineFunction & MF)2083 virtual bool hasInlineStackProbe(const MachineFunction &MF) const { return false; } 2084 getStackProbeSymbolName(const MachineFunction & MF)2085 virtual StringRef getStackProbeSymbolName(const MachineFunction &MF) const { 2086 return ""; 2087 } 2088 2089 /// Returns true if a cast from SrcAS to DestAS is "cheap", such that e.g. we 2090 /// are happy to sink it into basic blocks. A cast may be free, but not 2091 /// necessarily a no-op. e.g. a free truncate from a 64-bit to 32-bit pointer. 2092 virtual bool isFreeAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const; 2093 2094 /// Return true if the pointer arguments to CI should be aligned by aligning 2095 /// the object whose address is being passed. If so then MinSize is set to the 2096 /// minimum size the object must be to be aligned and PrefAlign is set to the 2097 /// preferred alignment. shouldAlignPointerArgs(CallInst *,unsigned &,Align &)2098 virtual bool shouldAlignPointerArgs(CallInst * /*CI*/, unsigned & /*MinSize*/, 2099 Align & /*PrefAlign*/) const { 2100 return false; 2101 } 2102 2103 //===--------------------------------------------------------------------===// 2104 /// \name Helpers for TargetTransformInfo implementations 2105 /// @{ 2106 2107 /// Get the ISD node that corresponds to the Instruction class opcode. 2108 int InstructionOpcodeToISD(unsigned Opcode) const; 2109 2110 /// @} 2111 2112 //===--------------------------------------------------------------------===// 2113 /// \name Helpers for atomic expansion. 2114 /// @{ 2115 2116 /// Returns the maximum atomic operation size (in bits) supported by 2117 /// the backend. Atomic operations greater than this size (as well 2118 /// as ones that are not naturally aligned), will be expanded by 2119 /// AtomicExpandPass into an __atomic_* library call. getMaxAtomicSizeInBitsSupported()2120 unsigned getMaxAtomicSizeInBitsSupported() const { 2121 return MaxAtomicSizeInBitsSupported; 2122 } 2123 2124 /// Returns the size in bits of the maximum div/rem the backend supports. 2125 /// Larger operations will be expanded by ExpandLargeDivRem. getMaxDivRemBitWidthSupported()2126 unsigned getMaxDivRemBitWidthSupported() const { 2127 return MaxDivRemBitWidthSupported; 2128 } 2129 2130 /// Returns the size in bits of the maximum larget fp convert the backend 2131 /// supports. Larger operations will be expanded by ExpandLargeFPConvert. getMaxLargeFPConvertBitWidthSupported()2132 unsigned getMaxLargeFPConvertBitWidthSupported() const { 2133 return MaxLargeFPConvertBitWidthSupported; 2134 } 2135 2136 /// Returns the size of the smallest cmpxchg or ll/sc instruction 2137 /// the backend supports. Any smaller operations are widened in 2138 /// AtomicExpandPass. 2139 /// 2140 /// Note that *unlike* operations above the maximum size, atomic ops 2141 /// are still natively supported below the minimum; they just 2142 /// require a more complex expansion. getMinCmpXchgSizeInBits()2143 unsigned getMinCmpXchgSizeInBits() const { return MinCmpXchgSizeInBits; } 2144 2145 /// Whether the target supports unaligned atomic operations. supportsUnalignedAtomics()2146 bool supportsUnalignedAtomics() const { return SupportsUnalignedAtomics; } 2147 2148 /// Whether AtomicExpandPass should automatically insert fences and reduce 2149 /// ordering for this atomic. This should be true for most architectures with 2150 /// weak memory ordering. Defaults to false. shouldInsertFencesForAtomic(const Instruction * I)2151 virtual bool shouldInsertFencesForAtomic(const Instruction *I) const { 2152 return false; 2153 } 2154 2155 /// Whether AtomicExpandPass should automatically insert a trailing fence 2156 /// without reducing the ordering for this atomic. Defaults to false. 2157 virtual bool shouldInsertTrailingFenceForAtomicStore(const Instruction * I)2158 shouldInsertTrailingFenceForAtomicStore(const Instruction *I) const { 2159 return false; 2160 } 2161 2162 /// Perform a load-linked operation on Addr, returning a "Value *" with the 2163 /// corresponding pointee type. This may entail some non-trivial operations to 2164 /// truncate or reconstruct types that will be illegal in the backend. See 2165 /// ARMISelLowering for an example implementation. emitLoadLinked(IRBuilderBase & Builder,Type * ValueTy,Value * Addr,AtomicOrdering Ord)2166 virtual Value *emitLoadLinked(IRBuilderBase &Builder, Type *ValueTy, 2167 Value *Addr, AtomicOrdering Ord) const { 2168 llvm_unreachable("Load linked unimplemented on this target"); 2169 } 2170 2171 /// Perform a store-conditional operation to Addr. Return the status of the 2172 /// store. This should be 0 if the store succeeded, non-zero otherwise. emitStoreConditional(IRBuilderBase & Builder,Value * Val,Value * Addr,AtomicOrdering Ord)2173 virtual Value *emitStoreConditional(IRBuilderBase &Builder, Value *Val, 2174 Value *Addr, AtomicOrdering Ord) const { 2175 llvm_unreachable("Store conditional unimplemented on this target"); 2176 } 2177 2178 /// Perform a masked atomicrmw using a target-specific intrinsic. This 2179 /// represents the core LL/SC loop which will be lowered at a late stage by 2180 /// the backend. The target-specific intrinsic returns the loaded value and 2181 /// is not responsible for masking and shifting the result. emitMaskedAtomicRMWIntrinsic(IRBuilderBase & Builder,AtomicRMWInst * AI,Value * AlignedAddr,Value * Incr,Value * Mask,Value * ShiftAmt,AtomicOrdering Ord)2182 virtual Value *emitMaskedAtomicRMWIntrinsic(IRBuilderBase &Builder, 2183 AtomicRMWInst *AI, 2184 Value *AlignedAddr, Value *Incr, 2185 Value *Mask, Value *ShiftAmt, 2186 AtomicOrdering Ord) const { 2187 llvm_unreachable("Masked atomicrmw expansion unimplemented on this target"); 2188 } 2189 2190 /// Perform a atomicrmw expansion using a target-specific way. This is 2191 /// expected to be called when masked atomicrmw and bit test atomicrmw don't 2192 /// work, and the target supports another way to lower atomicrmw. emitExpandAtomicRMW(AtomicRMWInst * AI)2193 virtual void emitExpandAtomicRMW(AtomicRMWInst *AI) const { 2194 llvm_unreachable( 2195 "Generic atomicrmw expansion unimplemented on this target"); 2196 } 2197 2198 /// Perform a bit test atomicrmw using a target-specific intrinsic. This 2199 /// represents the combined bit test intrinsic which will be lowered at a late 2200 /// stage by the backend. emitBitTestAtomicRMWIntrinsic(AtomicRMWInst * AI)2201 virtual void emitBitTestAtomicRMWIntrinsic(AtomicRMWInst *AI) const { 2202 llvm_unreachable( 2203 "Bit test atomicrmw expansion unimplemented on this target"); 2204 } 2205 2206 /// Perform a atomicrmw which the result is only used by comparison, using a 2207 /// target-specific intrinsic. This represents the combined atomic and compare 2208 /// intrinsic which will be lowered at a late stage by the backend. emitCmpArithAtomicRMWIntrinsic(AtomicRMWInst * AI)2209 virtual void emitCmpArithAtomicRMWIntrinsic(AtomicRMWInst *AI) const { 2210 llvm_unreachable( 2211 "Compare arith atomicrmw expansion unimplemented on this target"); 2212 } 2213 2214 /// Perform a masked cmpxchg using a target-specific intrinsic. This 2215 /// represents the core LL/SC loop which will be lowered at a late stage by 2216 /// the backend. The target-specific intrinsic returns the loaded value and 2217 /// is not responsible for masking and shifting the result. emitMaskedAtomicCmpXchgIntrinsic(IRBuilderBase & Builder,AtomicCmpXchgInst * CI,Value * AlignedAddr,Value * CmpVal,Value * NewVal,Value * Mask,AtomicOrdering Ord)2218 virtual Value *emitMaskedAtomicCmpXchgIntrinsic( 2219 IRBuilderBase &Builder, AtomicCmpXchgInst *CI, Value *AlignedAddr, 2220 Value *CmpVal, Value *NewVal, Value *Mask, AtomicOrdering Ord) const { 2221 llvm_unreachable("Masked cmpxchg expansion unimplemented on this target"); 2222 } 2223 2224 //===--------------------------------------------------------------------===// 2225 /// \name KCFI check lowering. 2226 /// @{ 2227 EmitKCFICheck(MachineBasicBlock & MBB,MachineBasicBlock::instr_iterator & MBBI,const TargetInstrInfo * TII)2228 virtual MachineInstr *EmitKCFICheck(MachineBasicBlock &MBB, 2229 MachineBasicBlock::instr_iterator &MBBI, 2230 const TargetInstrInfo *TII) const { 2231 llvm_unreachable("KCFI is not supported on this target"); 2232 } 2233 2234 /// @} 2235 2236 /// Inserts in the IR a target-specific intrinsic specifying a fence. 2237 /// It is called by AtomicExpandPass before expanding an 2238 /// AtomicRMW/AtomicCmpXchg/AtomicStore/AtomicLoad 2239 /// if shouldInsertFencesForAtomic returns true. 2240 /// 2241 /// Inst is the original atomic instruction, prior to other expansions that 2242 /// may be performed. 2243 /// 2244 /// This function should either return a nullptr, or a pointer to an IR-level 2245 /// Instruction*. Even complex fence sequences can be represented by a 2246 /// single Instruction* through an intrinsic to be lowered later. 2247 /// 2248 /// The default implementation emits an IR fence before any release (or 2249 /// stronger) operation that stores, and after any acquire (or stronger) 2250 /// operation. This is generally a correct implementation, but backends may 2251 /// override if they wish to use alternative schemes (e.g. the PowerPC 2252 /// standard ABI uses a fence before a seq_cst load instead of after a 2253 /// seq_cst store). 2254 /// @{ 2255 virtual Instruction *emitLeadingFence(IRBuilderBase &Builder, 2256 Instruction *Inst, 2257 AtomicOrdering Ord) const; 2258 2259 virtual Instruction *emitTrailingFence(IRBuilderBase &Builder, 2260 Instruction *Inst, 2261 AtomicOrdering Ord) const; 2262 /// @} 2263 2264 // Emits code that executes when the comparison result in the ll/sc 2265 // expansion of a cmpxchg instruction is such that the store-conditional will 2266 // not execute. This makes it possible to balance out the load-linked with 2267 // a dedicated instruction, if desired. 2268 // E.g., on ARM, if ldrex isn't followed by strex, the exclusive monitor would 2269 // be unnecessarily held, except if clrex, inserted by this hook, is executed. emitAtomicCmpXchgNoStoreLLBalance(IRBuilderBase & Builder)2270 virtual void emitAtomicCmpXchgNoStoreLLBalance(IRBuilderBase &Builder) const {} 2271 2272 /// Returns true if arguments should be sign-extended in lib calls. shouldSignExtendTypeInLibCall(EVT Type,bool IsSigned)2273 virtual bool shouldSignExtendTypeInLibCall(EVT Type, bool IsSigned) const { 2274 return IsSigned; 2275 } 2276 2277 /// Returns true if arguments should be extended in lib calls. shouldExtendTypeInLibCall(EVT Type)2278 virtual bool shouldExtendTypeInLibCall(EVT Type) const { 2279 return true; 2280 } 2281 2282 /// Returns how the given (atomic) load should be expanded by the 2283 /// IR-level AtomicExpand pass. shouldExpandAtomicLoadInIR(LoadInst * LI)2284 virtual AtomicExpansionKind shouldExpandAtomicLoadInIR(LoadInst *LI) const { 2285 return AtomicExpansionKind::None; 2286 } 2287 2288 /// Returns how the given (atomic) load should be cast by the IR-level 2289 /// AtomicExpand pass. shouldCastAtomicLoadInIR(LoadInst * LI)2290 virtual AtomicExpansionKind shouldCastAtomicLoadInIR(LoadInst *LI) const { 2291 if (LI->getType()->isFloatingPointTy()) 2292 return AtomicExpansionKind::CastToInteger; 2293 return AtomicExpansionKind::None; 2294 } 2295 2296 /// Returns how the given (atomic) store should be expanded by the IR-level 2297 /// AtomicExpand pass into. For instance AtomicExpansionKind::Expand will try 2298 /// to use an atomicrmw xchg. shouldExpandAtomicStoreInIR(StoreInst * SI)2299 virtual AtomicExpansionKind shouldExpandAtomicStoreInIR(StoreInst *SI) const { 2300 return AtomicExpansionKind::None; 2301 } 2302 2303 /// Returns how the given (atomic) store should be cast by the IR-level 2304 /// AtomicExpand pass into. For instance AtomicExpansionKind::CastToInteger 2305 /// will try to cast the operands to integer values. shouldCastAtomicStoreInIR(StoreInst * SI)2306 virtual AtomicExpansionKind shouldCastAtomicStoreInIR(StoreInst *SI) const { 2307 if (SI->getValueOperand()->getType()->isFloatingPointTy()) 2308 return AtomicExpansionKind::CastToInteger; 2309 return AtomicExpansionKind::None; 2310 } 2311 2312 /// Returns how the given atomic cmpxchg should be expanded by the IR-level 2313 /// AtomicExpand pass. 2314 virtual AtomicExpansionKind shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst * AI)2315 shouldExpandAtomicCmpXchgInIR(AtomicCmpXchgInst *AI) const { 2316 return AtomicExpansionKind::None; 2317 } 2318 2319 /// Returns how the IR-level AtomicExpand pass should expand the given 2320 /// AtomicRMW, if at all. Default is to never expand. shouldExpandAtomicRMWInIR(AtomicRMWInst * RMW)2321 virtual AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *RMW) const { 2322 return RMW->isFloatingPointOperation() ? 2323 AtomicExpansionKind::CmpXChg : AtomicExpansionKind::None; 2324 } 2325 2326 /// Returns how the given atomic atomicrmw should be cast by the IR-level 2327 /// AtomicExpand pass. 2328 virtual AtomicExpansionKind shouldCastAtomicRMWIInIR(AtomicRMWInst * RMWI)2329 shouldCastAtomicRMWIInIR(AtomicRMWInst *RMWI) const { 2330 if (RMWI->getOperation() == AtomicRMWInst::Xchg && 2331 (RMWI->getValOperand()->getType()->isFloatingPointTy() || 2332 RMWI->getValOperand()->getType()->isPointerTy())) 2333 return AtomicExpansionKind::CastToInteger; 2334 2335 return AtomicExpansionKind::None; 2336 } 2337 2338 /// On some platforms, an AtomicRMW that never actually modifies the value 2339 /// (such as fetch_add of 0) can be turned into a fence followed by an 2340 /// atomic load. This may sound useless, but it makes it possible for the 2341 /// processor to keep the cacheline shared, dramatically improving 2342 /// performance. And such idempotent RMWs are useful for implementing some 2343 /// kinds of locks, see for example (justification + benchmarks): 2344 /// http://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf 2345 /// This method tries doing that transformation, returning the atomic load if 2346 /// it succeeds, and nullptr otherwise. 2347 /// If shouldExpandAtomicLoadInIR returns true on that load, it will undergo 2348 /// another round of expansion. 2349 virtual LoadInst * lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst * RMWI)2350 lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *RMWI) const { 2351 return nullptr; 2352 } 2353 2354 /// Returns how the platform's atomic operations are extended (ZERO_EXTEND, 2355 /// SIGN_EXTEND, or ANY_EXTEND). getExtendForAtomicOps()2356 virtual ISD::NodeType getExtendForAtomicOps() const { 2357 return ISD::ZERO_EXTEND; 2358 } 2359 2360 /// Returns how the platform's atomic compare and swap expects its comparison 2361 /// value to be extended (ZERO_EXTEND, SIGN_EXTEND, or ANY_EXTEND). This is 2362 /// separate from getExtendForAtomicOps, which is concerned with the 2363 /// sign-extension of the instruction's output, whereas here we are concerned 2364 /// with the sign-extension of the input. For targets with compare-and-swap 2365 /// instructions (or sub-word comparisons in their LL/SC loop expansions), 2366 /// the input can be ANY_EXTEND, but the output will still have a specific 2367 /// extension. getExtendForAtomicCmpSwapArg()2368 virtual ISD::NodeType getExtendForAtomicCmpSwapArg() const { 2369 return ISD::ANY_EXTEND; 2370 } 2371 2372 /// @} 2373 2374 /// Returns true if we should normalize 2375 /// select(N0&N1, X, Y) => select(N0, select(N1, X, Y), Y) and 2376 /// select(N0|N1, X, Y) => select(N0, select(N1, X, Y, Y)) if it is likely 2377 /// that it saves us from materializing N0 and N1 in an integer register. 2378 /// Targets that are able to perform and/or on flags should return false here. shouldNormalizeToSelectSequence(LLVMContext & Context,EVT VT)2379 virtual bool shouldNormalizeToSelectSequence(LLVMContext &Context, 2380 EVT VT) const { 2381 // If a target has multiple condition registers, then it likely has logical 2382 // operations on those registers. 2383 if (hasMultipleConditionRegisters()) 2384 return false; 2385 // Only do the transform if the value won't be split into multiple 2386 // registers. 2387 LegalizeTypeAction Action = getTypeAction(Context, VT); 2388 return Action != TypeExpandInteger && Action != TypeExpandFloat && 2389 Action != TypeSplitVector; 2390 } 2391 isProfitableToCombineMinNumMaxNum(EVT VT)2392 virtual bool isProfitableToCombineMinNumMaxNum(EVT VT) const { return true; } 2393 2394 /// Return true if a select of constants (select Cond, C1, C2) should be 2395 /// transformed into simple math ops with the condition value. For example: 2396 /// select Cond, C1, C1-1 --> add (zext Cond), C1-1 convertSelectOfConstantsToMath(EVT VT)2397 virtual bool convertSelectOfConstantsToMath(EVT VT) const { 2398 return false; 2399 } 2400 2401 /// Return true if it is profitable to transform an integer 2402 /// multiplication-by-constant into simpler operations like shifts and adds. 2403 /// This may be true if the target does not directly support the 2404 /// multiplication operation for the specified type or the sequence of simpler 2405 /// ops is faster than the multiply. decomposeMulByConstant(LLVMContext & Context,EVT VT,SDValue C)2406 virtual bool decomposeMulByConstant(LLVMContext &Context, 2407 EVT VT, SDValue C) const { 2408 return false; 2409 } 2410 2411 /// Return true if it may be profitable to transform 2412 /// (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2). 2413 /// This may not be true if c1 and c2 can be represented as immediates but 2414 /// c1*c2 cannot, for example. 2415 /// The target should check if c1, c2 and c1*c2 can be represented as 2416 /// immediates, or have to be materialized into registers. If it is not sure 2417 /// about some cases, a default true can be returned to let the DAGCombiner 2418 /// decide. 2419 /// AddNode is (add x, c1), and ConstNode is c2. isMulAddWithConstProfitable(SDValue AddNode,SDValue ConstNode)2420 virtual bool isMulAddWithConstProfitable(SDValue AddNode, 2421 SDValue ConstNode) const { 2422 return true; 2423 } 2424 2425 /// Return true if it is more correct/profitable to use strict FP_TO_INT 2426 /// conversion operations - canonicalizing the FP source value instead of 2427 /// converting all cases and then selecting based on value. 2428 /// This may be true if the target throws exceptions for out of bounds 2429 /// conversions or has fast FP CMOV. shouldUseStrictFP_TO_INT(EVT FpVT,EVT IntVT,bool IsSigned)2430 virtual bool shouldUseStrictFP_TO_INT(EVT FpVT, EVT IntVT, 2431 bool IsSigned) const { 2432 return false; 2433 } 2434 2435 /// Return true if it is beneficial to expand an @llvm.powi.* intrinsic. 2436 /// If not optimizing for size, expanding @llvm.powi.* intrinsics is always 2437 /// considered beneficial. 2438 /// If optimizing for size, expansion is only considered beneficial for upto 2439 /// 5 multiplies and a divide (if the exponent is negative). isBeneficialToExpandPowI(int64_t Exponent,bool OptForSize)2440 bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const { 2441 if (Exponent < 0) 2442 Exponent = -Exponent; 2443 uint64_t E = static_cast<uint64_t>(Exponent); 2444 return !OptForSize || (llvm::popcount(E) + Log2_64(E) < 7); 2445 } 2446 2447 //===--------------------------------------------------------------------===// 2448 // TargetLowering Configuration Methods - These methods should be invoked by 2449 // the derived class constructor to configure this object for the target. 2450 // 2451 protected: 2452 /// Specify how the target extends the result of integer and floating point 2453 /// boolean values from i1 to a wider type. See getBooleanContents. setBooleanContents(BooleanContent Ty)2454 void setBooleanContents(BooleanContent Ty) { 2455 BooleanContents = Ty; 2456 BooleanFloatContents = Ty; 2457 } 2458 2459 /// Specify how the target extends the result of integer and floating point 2460 /// boolean values from i1 to a wider type. See getBooleanContents. setBooleanContents(BooleanContent IntTy,BooleanContent FloatTy)2461 void setBooleanContents(BooleanContent IntTy, BooleanContent FloatTy) { 2462 BooleanContents = IntTy; 2463 BooleanFloatContents = FloatTy; 2464 } 2465 2466 /// Specify how the target extends the result of a vector boolean value from a 2467 /// vector of i1 to a wider type. See getBooleanContents. setBooleanVectorContents(BooleanContent Ty)2468 void setBooleanVectorContents(BooleanContent Ty) { 2469 BooleanVectorContents = Ty; 2470 } 2471 2472 /// Specify the target scheduling preference. setSchedulingPreference(Sched::Preference Pref)2473 void setSchedulingPreference(Sched::Preference Pref) { 2474 SchedPreferenceInfo = Pref; 2475 } 2476 2477 /// Indicate the minimum number of blocks to generate jump tables. 2478 void setMinimumJumpTableEntries(unsigned Val); 2479 2480 /// Indicate the maximum number of entries in jump tables. 2481 /// Set to zero to generate unlimited jump tables. 2482 void setMaximumJumpTableSize(unsigned); 2483 2484 /// If set to a physical register, this specifies the register that 2485 /// llvm.savestack/llvm.restorestack should save and restore. setStackPointerRegisterToSaveRestore(Register R)2486 void setStackPointerRegisterToSaveRestore(Register R) { 2487 StackPointerRegisterToSaveRestore = R; 2488 } 2489 2490 /// Tells the code generator that the target has multiple (allocatable) 2491 /// condition registers that can be used to store the results of comparisons 2492 /// for use by selects and conditional branches. With multiple condition 2493 /// registers, the code generator will not aggressively sink comparisons into 2494 /// the blocks of their users. 2495 void setHasMultipleConditionRegisters(bool hasManyRegs = true) { 2496 HasMultipleConditionRegisters = hasManyRegs; 2497 } 2498 2499 /// Tells the code generator that the target has BitExtract instructions. 2500 /// The code generator will aggressively sink "shift"s into the blocks of 2501 /// their users if the users will generate "and" instructions which can be 2502 /// combined with "shift" to BitExtract instructions. 2503 void setHasExtractBitsInsn(bool hasExtractInsn = true) { 2504 HasExtractBitsInsn = hasExtractInsn; 2505 } 2506 2507 /// Tells the code generator not to expand logic operations on comparison 2508 /// predicates into separate sequences that increase the amount of flow 2509 /// control. 2510 void setJumpIsExpensive(bool isExpensive = true); 2511 2512 /// Tells the code generator which bitwidths to bypass. addBypassSlowDiv(unsigned int SlowBitWidth,unsigned int FastBitWidth)2513 void addBypassSlowDiv(unsigned int SlowBitWidth, unsigned int FastBitWidth) { 2514 BypassSlowDivWidths[SlowBitWidth] = FastBitWidth; 2515 } 2516 2517 /// Add the specified register class as an available regclass for the 2518 /// specified value type. This indicates the selector can handle values of 2519 /// that class natively. addRegisterClass(MVT VT,const TargetRegisterClass * RC)2520 void addRegisterClass(MVT VT, const TargetRegisterClass *RC) { 2521 assert((unsigned)VT.SimpleTy < std::size(RegClassForVT)); 2522 RegClassForVT[VT.SimpleTy] = RC; 2523 } 2524 2525 /// Return the largest legal super-reg register class of the register class 2526 /// for the specified type and its associated "cost". 2527 virtual std::pair<const TargetRegisterClass *, uint8_t> 2528 findRepresentativeClass(const TargetRegisterInfo *TRI, MVT VT) const; 2529 2530 /// Once all of the register classes are added, this allows us to compute 2531 /// derived properties we expose. 2532 void computeRegisterProperties(const TargetRegisterInfo *TRI); 2533 2534 /// Indicate that the specified operation does not work with the specified 2535 /// type and indicate what to do about it. Note that VT may refer to either 2536 /// the type of a result or that of an operand of Op. setOperationAction(unsigned Op,MVT VT,LegalizeAction Action)2537 void setOperationAction(unsigned Op, MVT VT, LegalizeAction Action) { 2538 assert(Op < std::size(OpActions[0]) && "Table isn't big enough!"); 2539 OpActions[(unsigned)VT.SimpleTy][Op] = Action; 2540 } setOperationAction(ArrayRef<unsigned> Ops,MVT VT,LegalizeAction Action)2541 void setOperationAction(ArrayRef<unsigned> Ops, MVT VT, 2542 LegalizeAction Action) { 2543 for (auto Op : Ops) 2544 setOperationAction(Op, VT, Action); 2545 } setOperationAction(ArrayRef<unsigned> Ops,ArrayRef<MVT> VTs,LegalizeAction Action)2546 void setOperationAction(ArrayRef<unsigned> Ops, ArrayRef<MVT> VTs, 2547 LegalizeAction Action) { 2548 for (auto VT : VTs) 2549 setOperationAction(Ops, VT, Action); 2550 } 2551 2552 /// Indicate that the specified load with extension does not work with the 2553 /// specified type and indicate what to do about it. setLoadExtAction(unsigned ExtType,MVT ValVT,MVT MemVT,LegalizeAction Action)2554 void setLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, 2555 LegalizeAction Action) { 2556 assert(ExtType < ISD::LAST_LOADEXT_TYPE && ValVT.isValid() && 2557 MemVT.isValid() && "Table isn't big enough!"); 2558 assert((unsigned)Action < 0x10 && "too many bits for bitfield array"); 2559 unsigned Shift = 4 * ExtType; 2560 LoadExtActions[ValVT.SimpleTy][MemVT.SimpleTy] &= ~((uint16_t)0xF << Shift); 2561 LoadExtActions[ValVT.SimpleTy][MemVT.SimpleTy] |= (uint16_t)Action << Shift; 2562 } setLoadExtAction(ArrayRef<unsigned> ExtTypes,MVT ValVT,MVT MemVT,LegalizeAction Action)2563 void setLoadExtAction(ArrayRef<unsigned> ExtTypes, MVT ValVT, MVT MemVT, 2564 LegalizeAction Action) { 2565 for (auto ExtType : ExtTypes) 2566 setLoadExtAction(ExtType, ValVT, MemVT, Action); 2567 } setLoadExtAction(ArrayRef<unsigned> ExtTypes,MVT ValVT,ArrayRef<MVT> MemVTs,LegalizeAction Action)2568 void setLoadExtAction(ArrayRef<unsigned> ExtTypes, MVT ValVT, 2569 ArrayRef<MVT> MemVTs, LegalizeAction Action) { 2570 for (auto MemVT : MemVTs) 2571 setLoadExtAction(ExtTypes, ValVT, MemVT, Action); 2572 } 2573 2574 /// Let target indicate that an extending atomic load of the specified type 2575 /// is legal. setAtomicLoadExtAction(unsigned ExtType,MVT ValVT,MVT MemVT,LegalizeAction Action)2576 void setAtomicLoadExtAction(unsigned ExtType, MVT ValVT, MVT MemVT, 2577 LegalizeAction Action) { 2578 assert(ExtType < ISD::LAST_LOADEXT_TYPE && ValVT.isValid() && 2579 MemVT.isValid() && "Table isn't big enough!"); 2580 assert((unsigned)Action < 0x10 && "too many bits for bitfield array"); 2581 unsigned Shift = 4 * ExtType; 2582 AtomicLoadExtActions[ValVT.SimpleTy][MemVT.SimpleTy] &= 2583 ~((uint16_t)0xF << Shift); 2584 AtomicLoadExtActions[ValVT.SimpleTy][MemVT.SimpleTy] |= 2585 ((uint16_t)Action << Shift); 2586 } setAtomicLoadExtAction(ArrayRef<unsigned> ExtTypes,MVT ValVT,MVT MemVT,LegalizeAction Action)2587 void setAtomicLoadExtAction(ArrayRef<unsigned> ExtTypes, MVT ValVT, MVT MemVT, 2588 LegalizeAction Action) { 2589 for (auto ExtType : ExtTypes) 2590 setAtomicLoadExtAction(ExtType, ValVT, MemVT, Action); 2591 } setAtomicLoadExtAction(ArrayRef<unsigned> ExtTypes,MVT ValVT,ArrayRef<MVT> MemVTs,LegalizeAction Action)2592 void setAtomicLoadExtAction(ArrayRef<unsigned> ExtTypes, MVT ValVT, 2593 ArrayRef<MVT> MemVTs, LegalizeAction Action) { 2594 for (auto MemVT : MemVTs) 2595 setAtomicLoadExtAction(ExtTypes, ValVT, MemVT, Action); 2596 } 2597 2598 /// Indicate that the specified truncating store does not work with the 2599 /// specified type and indicate what to do about it. setTruncStoreAction(MVT ValVT,MVT MemVT,LegalizeAction Action)2600 void setTruncStoreAction(MVT ValVT, MVT MemVT, LegalizeAction Action) { 2601 assert(ValVT.isValid() && MemVT.isValid() && "Table isn't big enough!"); 2602 TruncStoreActions[(unsigned)ValVT.SimpleTy][MemVT.SimpleTy] = Action; 2603 } 2604 2605 /// Indicate that the specified indexed load does or does not work with the 2606 /// specified type and indicate what to do abort it. 2607 /// 2608 /// NOTE: All indexed mode loads are initialized to Expand in 2609 /// TargetLowering.cpp setIndexedLoadAction(ArrayRef<unsigned> IdxModes,MVT VT,LegalizeAction Action)2610 void setIndexedLoadAction(ArrayRef<unsigned> IdxModes, MVT VT, 2611 LegalizeAction Action) { 2612 for (auto IdxMode : IdxModes) 2613 setIndexedModeAction(IdxMode, VT, IMAB_Load, Action); 2614 } 2615 setIndexedLoadAction(ArrayRef<unsigned> IdxModes,ArrayRef<MVT> VTs,LegalizeAction Action)2616 void setIndexedLoadAction(ArrayRef<unsigned> IdxModes, ArrayRef<MVT> VTs, 2617 LegalizeAction Action) { 2618 for (auto VT : VTs) 2619 setIndexedLoadAction(IdxModes, VT, Action); 2620 } 2621 2622 /// Indicate that the specified indexed store does or does not work with the 2623 /// specified type and indicate what to do about it. 2624 /// 2625 /// NOTE: All indexed mode stores are initialized to Expand in 2626 /// TargetLowering.cpp setIndexedStoreAction(ArrayRef<unsigned> IdxModes,MVT VT,LegalizeAction Action)2627 void setIndexedStoreAction(ArrayRef<unsigned> IdxModes, MVT VT, 2628 LegalizeAction Action) { 2629 for (auto IdxMode : IdxModes) 2630 setIndexedModeAction(IdxMode, VT, IMAB_Store, Action); 2631 } 2632 setIndexedStoreAction(ArrayRef<unsigned> IdxModes,ArrayRef<MVT> VTs,LegalizeAction Action)2633 void setIndexedStoreAction(ArrayRef<unsigned> IdxModes, ArrayRef<MVT> VTs, 2634 LegalizeAction Action) { 2635 for (auto VT : VTs) 2636 setIndexedStoreAction(IdxModes, VT, Action); 2637 } 2638 2639 /// Indicate that the specified indexed masked load does or does not work with 2640 /// the specified type and indicate what to do about it. 2641 /// 2642 /// NOTE: All indexed mode masked loads are initialized to Expand in 2643 /// TargetLowering.cpp setIndexedMaskedLoadAction(unsigned IdxMode,MVT VT,LegalizeAction Action)2644 void setIndexedMaskedLoadAction(unsigned IdxMode, MVT VT, 2645 LegalizeAction Action) { 2646 setIndexedModeAction(IdxMode, VT, IMAB_MaskedLoad, Action); 2647 } 2648 2649 /// Indicate that the specified indexed masked store does or does not work 2650 /// with the specified type and indicate what to do about it. 2651 /// 2652 /// NOTE: All indexed mode masked stores are initialized to Expand in 2653 /// TargetLowering.cpp setIndexedMaskedStoreAction(unsigned IdxMode,MVT VT,LegalizeAction Action)2654 void setIndexedMaskedStoreAction(unsigned IdxMode, MVT VT, 2655 LegalizeAction Action) { 2656 setIndexedModeAction(IdxMode, VT, IMAB_MaskedStore, Action); 2657 } 2658 2659 /// Indicate that the specified condition code is or isn't supported on the 2660 /// target and indicate what to do about it. setCondCodeAction(ArrayRef<ISD::CondCode> CCs,MVT VT,LegalizeAction Action)2661 void setCondCodeAction(ArrayRef<ISD::CondCode> CCs, MVT VT, 2662 LegalizeAction Action) { 2663 for (auto CC : CCs) { 2664 assert(VT.isValid() && (unsigned)CC < std::size(CondCodeActions) && 2665 "Table isn't big enough!"); 2666 assert((unsigned)Action < 0x10 && "too many bits for bitfield array"); 2667 /// The lower 3 bits of the SimpleTy index into Nth 4bit set from the 2668 /// 32-bit value and the upper 29 bits index into the second dimension of 2669 /// the array to select what 32-bit value to use. 2670 uint32_t Shift = 4 * (VT.SimpleTy & 0x7); 2671 CondCodeActions[CC][VT.SimpleTy >> 3] &= ~((uint32_t)0xF << Shift); 2672 CondCodeActions[CC][VT.SimpleTy >> 3] |= (uint32_t)Action << Shift; 2673 } 2674 } setCondCodeAction(ArrayRef<ISD::CondCode> CCs,ArrayRef<MVT> VTs,LegalizeAction Action)2675 void setCondCodeAction(ArrayRef<ISD::CondCode> CCs, ArrayRef<MVT> VTs, 2676 LegalizeAction Action) { 2677 for (auto VT : VTs) 2678 setCondCodeAction(CCs, VT, Action); 2679 } 2680 2681 /// If Opc/OrigVT is specified as being promoted, the promotion code defaults 2682 /// to trying a larger integer/fp until it can find one that works. If that 2683 /// default is insufficient, this method can be used by the target to override 2684 /// the default. AddPromotedToType(unsigned Opc,MVT OrigVT,MVT DestVT)2685 void AddPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT) { 2686 PromoteToType[std::make_pair(Opc, OrigVT.SimpleTy)] = DestVT.SimpleTy; 2687 } 2688 2689 /// Convenience method to set an operation to Promote and specify the type 2690 /// in a single call. setOperationPromotedToType(unsigned Opc,MVT OrigVT,MVT DestVT)2691 void setOperationPromotedToType(unsigned Opc, MVT OrigVT, MVT DestVT) { 2692 setOperationAction(Opc, OrigVT, Promote); 2693 AddPromotedToType(Opc, OrigVT, DestVT); 2694 } setOperationPromotedToType(ArrayRef<unsigned> Ops,MVT OrigVT,MVT DestVT)2695 void setOperationPromotedToType(ArrayRef<unsigned> Ops, MVT OrigVT, 2696 MVT DestVT) { 2697 for (auto Op : Ops) { 2698 setOperationAction(Op, OrigVT, Promote); 2699 AddPromotedToType(Op, OrigVT, DestVT); 2700 } 2701 } 2702 2703 /// Targets should invoke this method for each target independent node that 2704 /// they want to provide a custom DAG combiner for by implementing the 2705 /// PerformDAGCombine virtual method. setTargetDAGCombine(ArrayRef<ISD::NodeType> NTs)2706 void setTargetDAGCombine(ArrayRef<ISD::NodeType> NTs) { 2707 for (auto NT : NTs) { 2708 assert(unsigned(NT >> 3) < std::size(TargetDAGCombineArray)); 2709 TargetDAGCombineArray[NT >> 3] |= 1 << (NT & 7); 2710 } 2711 } 2712 2713 /// Set the target's minimum function alignment. setMinFunctionAlignment(Align Alignment)2714 void setMinFunctionAlignment(Align Alignment) { 2715 MinFunctionAlignment = Alignment; 2716 } 2717 2718 /// Set the target's preferred function alignment. This should be set if 2719 /// there is a performance benefit to higher-than-minimum alignment setPrefFunctionAlignment(Align Alignment)2720 void setPrefFunctionAlignment(Align Alignment) { 2721 PrefFunctionAlignment = Alignment; 2722 } 2723 2724 /// Set the target's preferred loop alignment. Default alignment is one, it 2725 /// means the target does not care about loop alignment. The target may also 2726 /// override getPrefLoopAlignment to provide per-loop values. setPrefLoopAlignment(Align Alignment)2727 void setPrefLoopAlignment(Align Alignment) { PrefLoopAlignment = Alignment; } setMaxBytesForAlignment(unsigned MaxBytes)2728 void setMaxBytesForAlignment(unsigned MaxBytes) { 2729 MaxBytesForAlignment = MaxBytes; 2730 } 2731 2732 /// Set the minimum stack alignment of an argument. setMinStackArgumentAlignment(Align Alignment)2733 void setMinStackArgumentAlignment(Align Alignment) { 2734 MinStackArgumentAlignment = Alignment; 2735 } 2736 2737 /// Set the maximum atomic operation size supported by the 2738 /// backend. Atomic operations greater than this size (as well as 2739 /// ones that are not naturally aligned), will be expanded by 2740 /// AtomicExpandPass into an __atomic_* library call. setMaxAtomicSizeInBitsSupported(unsigned SizeInBits)2741 void setMaxAtomicSizeInBitsSupported(unsigned SizeInBits) { 2742 MaxAtomicSizeInBitsSupported = SizeInBits; 2743 } 2744 2745 /// Set the size in bits of the maximum div/rem the backend supports. 2746 /// Larger operations will be expanded by ExpandLargeDivRem. setMaxDivRemBitWidthSupported(unsigned SizeInBits)2747 void setMaxDivRemBitWidthSupported(unsigned SizeInBits) { 2748 MaxDivRemBitWidthSupported = SizeInBits; 2749 } 2750 2751 /// Set the size in bits of the maximum fp convert the backend supports. 2752 /// Larger operations will be expanded by ExpandLargeFPConvert. setMaxLargeFPConvertBitWidthSupported(unsigned SizeInBits)2753 void setMaxLargeFPConvertBitWidthSupported(unsigned SizeInBits) { 2754 MaxLargeFPConvertBitWidthSupported = SizeInBits; 2755 } 2756 2757 /// Sets the minimum cmpxchg or ll/sc size supported by the backend. setMinCmpXchgSizeInBits(unsigned SizeInBits)2758 void setMinCmpXchgSizeInBits(unsigned SizeInBits) { 2759 MinCmpXchgSizeInBits = SizeInBits; 2760 } 2761 2762 /// Sets whether unaligned atomic operations are supported. setSupportsUnalignedAtomics(bool UnalignedSupported)2763 void setSupportsUnalignedAtomics(bool UnalignedSupported) { 2764 SupportsUnalignedAtomics = UnalignedSupported; 2765 } 2766 2767 public: 2768 //===--------------------------------------------------------------------===// 2769 // Addressing mode description hooks (used by LSR etc). 2770 // 2771 2772 /// CodeGenPrepare sinks address calculations into the same BB as Load/Store 2773 /// instructions reading the address. This allows as much computation as 2774 /// possible to be done in the address mode for that operand. This hook lets 2775 /// targets also pass back when this should be done on intrinsics which 2776 /// load/store. getAddrModeArguments(IntrinsicInst *,SmallVectorImpl<Value * > &,Type * &)2777 virtual bool getAddrModeArguments(IntrinsicInst * /*I*/, 2778 SmallVectorImpl<Value*> &/*Ops*/, 2779 Type *&/*AccessTy*/) const { 2780 return false; 2781 } 2782 2783 /// This represents an addressing mode of: 2784 /// BaseGV + BaseOffs + BaseReg + Scale*ScaleReg + ScalableOffset*vscale 2785 /// If BaseGV is null, there is no BaseGV. 2786 /// If BaseOffs is zero, there is no base offset. 2787 /// If HasBaseReg is false, there is no base register. 2788 /// If Scale is zero, there is no ScaleReg. Scale of 1 indicates a reg with 2789 /// no scale. 2790 /// If ScalableOffset is zero, there is no scalable offset. 2791 struct AddrMode { 2792 GlobalValue *BaseGV = nullptr; 2793 int64_t BaseOffs = 0; 2794 bool HasBaseReg = false; 2795 int64_t Scale = 0; 2796 int64_t ScalableOffset = 0; 2797 AddrMode() = default; 2798 }; 2799 2800 /// Return true if the addressing mode represented by AM is legal for this 2801 /// target, for a load/store of the specified type. 2802 /// 2803 /// The type may be VoidTy, in which case only return true if the addressing 2804 /// mode is legal for a load/store of any legal type. TODO: Handle 2805 /// pre/postinc as well. 2806 /// 2807 /// If the address space cannot be determined, it will be -1. 2808 /// 2809 /// TODO: Remove default argument 2810 virtual bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, 2811 Type *Ty, unsigned AddrSpace, 2812 Instruction *I = nullptr) const; 2813 2814 /// Returns true if the targets addressing mode can target thread local 2815 /// storage (TLS). addressingModeSupportsTLS(const GlobalValue &)2816 virtual bool addressingModeSupportsTLS(const GlobalValue &) const { 2817 return false; 2818 } 2819 2820 /// Return the prefered common base offset. getPreferredLargeGEPBaseOffset(int64_t MinOffset,int64_t MaxOffset)2821 virtual int64_t getPreferredLargeGEPBaseOffset(int64_t MinOffset, 2822 int64_t MaxOffset) const { 2823 return 0; 2824 } 2825 2826 /// Return true if the specified immediate is legal icmp immediate, that is 2827 /// the target has icmp instructions which can compare a register against the 2828 /// immediate without having to materialize the immediate into a register. isLegalICmpImmediate(int64_t)2829 virtual bool isLegalICmpImmediate(int64_t) const { 2830 return true; 2831 } 2832 2833 /// Return true if the specified immediate is legal add immediate, that is the 2834 /// target has add instructions which can add a register with the immediate 2835 /// without having to materialize the immediate into a register. isLegalAddImmediate(int64_t)2836 virtual bool isLegalAddImmediate(int64_t) const { 2837 return true; 2838 } 2839 2840 /// Return true if adding the specified scalable immediate is legal, that is 2841 /// the target has add instructions which can add a register with the 2842 /// immediate (multiplied by vscale) without having to materialize the 2843 /// immediate into a register. isLegalAddScalableImmediate(int64_t)2844 virtual bool isLegalAddScalableImmediate(int64_t) const { return false; } 2845 2846 /// Return true if the specified immediate is legal for the value input of a 2847 /// store instruction. isLegalStoreImmediate(int64_t Value)2848 virtual bool isLegalStoreImmediate(int64_t Value) const { 2849 // Default implementation assumes that at least 0 works since it is likely 2850 // that a zero register exists or a zero immediate is allowed. 2851 return Value == 0; 2852 } 2853 2854 /// Return true if it's significantly cheaper to shift a vector by a uniform 2855 /// scalar than by an amount which will vary across each lane. On x86 before 2856 /// AVX2 for example, there is a "psllw" instruction for the former case, but 2857 /// no simple instruction for a general "a << b" operation on vectors. 2858 /// This should also apply to lowering for vector funnel shifts (rotates). isVectorShiftByScalarCheap(Type * Ty)2859 virtual bool isVectorShiftByScalarCheap(Type *Ty) const { 2860 return false; 2861 } 2862 2863 /// Given a shuffle vector SVI representing a vector splat, return a new 2864 /// scalar type of size equal to SVI's scalar type if the new type is more 2865 /// profitable. Returns nullptr otherwise. For example under MVE float splats 2866 /// are converted to integer to prevent the need to move from SPR to GPR 2867 /// registers. shouldConvertSplatType(ShuffleVectorInst * SVI)2868 virtual Type* shouldConvertSplatType(ShuffleVectorInst* SVI) const { 2869 return nullptr; 2870 } 2871 2872 /// Given a set in interconnected phis of type 'From' that are loaded/stored 2873 /// or bitcast to type 'To', return true if the set should be converted to 2874 /// 'To'. shouldConvertPhiType(Type * From,Type * To)2875 virtual bool shouldConvertPhiType(Type *From, Type *To) const { 2876 return (From->isIntegerTy() || From->isFloatingPointTy()) && 2877 (To->isIntegerTy() || To->isFloatingPointTy()); 2878 } 2879 2880 /// Returns true if the opcode is a commutative binary operation. isCommutativeBinOp(unsigned Opcode)2881 virtual bool isCommutativeBinOp(unsigned Opcode) const { 2882 // FIXME: This should get its info from the td file. 2883 switch (Opcode) { 2884 case ISD::ADD: 2885 case ISD::SMIN: 2886 case ISD::SMAX: 2887 case ISD::UMIN: 2888 case ISD::UMAX: 2889 case ISD::MUL: 2890 case ISD::MULHU: 2891 case ISD::MULHS: 2892 case ISD::SMUL_LOHI: 2893 case ISD::UMUL_LOHI: 2894 case ISD::FADD: 2895 case ISD::FMUL: 2896 case ISD::AND: 2897 case ISD::OR: 2898 case ISD::XOR: 2899 case ISD::SADDO: 2900 case ISD::UADDO: 2901 case ISD::ADDC: 2902 case ISD::ADDE: 2903 case ISD::SADDSAT: 2904 case ISD::UADDSAT: 2905 case ISD::FMINNUM: 2906 case ISD::FMAXNUM: 2907 case ISD::FMINNUM_IEEE: 2908 case ISD::FMAXNUM_IEEE: 2909 case ISD::FMINIMUM: 2910 case ISD::FMAXIMUM: 2911 case ISD::AVGFLOORS: 2912 case ISD::AVGFLOORU: 2913 case ISD::AVGCEILS: 2914 case ISD::AVGCEILU: 2915 case ISD::ABDS: 2916 case ISD::ABDU: 2917 return true; 2918 default: return false; 2919 } 2920 } 2921 2922 /// Return true if the node is a math/logic binary operator. isBinOp(unsigned Opcode)2923 virtual bool isBinOp(unsigned Opcode) const { 2924 // A commutative binop must be a binop. 2925 if (isCommutativeBinOp(Opcode)) 2926 return true; 2927 // These are non-commutative binops. 2928 switch (Opcode) { 2929 case ISD::SUB: 2930 case ISD::SHL: 2931 case ISD::SRL: 2932 case ISD::SRA: 2933 case ISD::ROTL: 2934 case ISD::ROTR: 2935 case ISD::SDIV: 2936 case ISD::UDIV: 2937 case ISD::SREM: 2938 case ISD::UREM: 2939 case ISD::SSUBSAT: 2940 case ISD::USUBSAT: 2941 case ISD::FSUB: 2942 case ISD::FDIV: 2943 case ISD::FREM: 2944 return true; 2945 default: 2946 return false; 2947 } 2948 } 2949 2950 /// Return true if it's free to truncate a value of type FromTy to type 2951 /// ToTy. e.g. On x86 it's free to truncate a i32 value in register EAX to i16 2952 /// by referencing its sub-register AX. 2953 /// Targets must return false when FromTy <= ToTy. isTruncateFree(Type * FromTy,Type * ToTy)2954 virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const { 2955 return false; 2956 } 2957 2958 /// Return true if a truncation from FromTy to ToTy is permitted when deciding 2959 /// whether a call is in tail position. Typically this means that both results 2960 /// would be assigned to the same register or stack slot, but it could mean 2961 /// the target performs adequate checks of its own before proceeding with the 2962 /// tail call. Targets must return false when FromTy <= ToTy. allowTruncateForTailCall(Type * FromTy,Type * ToTy)2963 virtual bool allowTruncateForTailCall(Type *FromTy, Type *ToTy) const { 2964 return false; 2965 } 2966 isTruncateFree(EVT FromVT,EVT ToVT)2967 virtual bool isTruncateFree(EVT FromVT, EVT ToVT) const { return false; } isTruncateFree(LLT FromTy,LLT ToTy,const DataLayout & DL,LLVMContext & Ctx)2968 virtual bool isTruncateFree(LLT FromTy, LLT ToTy, const DataLayout &DL, 2969 LLVMContext &Ctx) const { 2970 return isTruncateFree(getApproximateEVTForLLT(FromTy, DL, Ctx), 2971 getApproximateEVTForLLT(ToTy, DL, Ctx)); 2972 } 2973 2974 /// Return true if truncating the specific node Val to type VT2 is free. isTruncateFree(SDValue Val,EVT VT2)2975 virtual bool isTruncateFree(SDValue Val, EVT VT2) const { 2976 // Fallback to type matching. 2977 return isTruncateFree(Val.getValueType(), VT2); 2978 } 2979 isProfitableToHoist(Instruction * I)2980 virtual bool isProfitableToHoist(Instruction *I) const { return true; } 2981 2982 /// Return true if the extension represented by \p I is free. 2983 /// Unlikely the is[Z|FP]ExtFree family which is based on types, 2984 /// this method can use the context provided by \p I to decide 2985 /// whether or not \p I is free. 2986 /// This method extends the behavior of the is[Z|FP]ExtFree family. 2987 /// In other words, if is[Z|FP]Free returns true, then this method 2988 /// returns true as well. The converse is not true. 2989 /// The target can perform the adequate checks by overriding isExtFreeImpl. 2990 /// \pre \p I must be a sign, zero, or fp extension. isExtFree(const Instruction * I)2991 bool isExtFree(const Instruction *I) const { 2992 switch (I->getOpcode()) { 2993 case Instruction::FPExt: 2994 if (isFPExtFree(EVT::getEVT(I->getType()), 2995 EVT::getEVT(I->getOperand(0)->getType()))) 2996 return true; 2997 break; 2998 case Instruction::ZExt: 2999 if (isZExtFree(I->getOperand(0)->getType(), I->getType())) 3000 return true; 3001 break; 3002 case Instruction::SExt: 3003 break; 3004 default: 3005 llvm_unreachable("Instruction is not an extension"); 3006 } 3007 return isExtFreeImpl(I); 3008 } 3009 3010 /// Return true if \p Load and \p Ext can form an ExtLoad. 3011 /// For example, in AArch64 3012 /// %L = load i8, i8* %ptr 3013 /// %E = zext i8 %L to i32 3014 /// can be lowered into one load instruction 3015 /// ldrb w0, [x0] isExtLoad(const LoadInst * Load,const Instruction * Ext,const DataLayout & DL)3016 bool isExtLoad(const LoadInst *Load, const Instruction *Ext, 3017 const DataLayout &DL) const { 3018 EVT VT = getValueType(DL, Ext->getType()); 3019 EVT LoadVT = getValueType(DL, Load->getType()); 3020 3021 // If the load has other users and the truncate is not free, the ext 3022 // probably isn't free. 3023 if (!Load->hasOneUse() && (isTypeLegal(LoadVT) || !isTypeLegal(VT)) && 3024 !isTruncateFree(Ext->getType(), Load->getType())) 3025 return false; 3026 3027 // Check whether the target supports casts folded into loads. 3028 unsigned LType; 3029 if (isa<ZExtInst>(Ext)) 3030 LType = ISD::ZEXTLOAD; 3031 else { 3032 assert(isa<SExtInst>(Ext) && "Unexpected ext type!"); 3033 LType = ISD::SEXTLOAD; 3034 } 3035 3036 return isLoadExtLegal(LType, VT, LoadVT); 3037 } 3038 3039 /// Return true if any actual instruction that defines a value of type FromTy 3040 /// implicitly zero-extends the value to ToTy in the result register. 3041 /// 3042 /// The function should return true when it is likely that the truncate can 3043 /// be freely folded with an instruction defining a value of FromTy. If 3044 /// the defining instruction is unknown (because you're looking at a 3045 /// function argument, PHI, etc.) then the target may require an 3046 /// explicit truncate, which is not necessarily free, but this function 3047 /// does not deal with those cases. 3048 /// Targets must return false when FromTy >= ToTy. isZExtFree(Type * FromTy,Type * ToTy)3049 virtual bool isZExtFree(Type *FromTy, Type *ToTy) const { 3050 return false; 3051 } 3052 isZExtFree(EVT FromTy,EVT ToTy)3053 virtual bool isZExtFree(EVT FromTy, EVT ToTy) const { return false; } isZExtFree(LLT FromTy,LLT ToTy,const DataLayout & DL,LLVMContext & Ctx)3054 virtual bool isZExtFree(LLT FromTy, LLT ToTy, const DataLayout &DL, 3055 LLVMContext &Ctx) const { 3056 return isZExtFree(getApproximateEVTForLLT(FromTy, DL, Ctx), 3057 getApproximateEVTForLLT(ToTy, DL, Ctx)); 3058 } 3059 3060 /// Return true if zero-extending the specific node Val to type VT2 is free 3061 /// (either because it's implicitly zero-extended such as ARM ldrb / ldrh or 3062 /// because it's folded such as X86 zero-extending loads). isZExtFree(SDValue Val,EVT VT2)3063 virtual bool isZExtFree(SDValue Val, EVT VT2) const { 3064 return isZExtFree(Val.getValueType(), VT2); 3065 } 3066 3067 /// Return true if sign-extension from FromTy to ToTy is cheaper than 3068 /// zero-extension. isSExtCheaperThanZExt(EVT FromTy,EVT ToTy)3069 virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const { 3070 return false; 3071 } 3072 3073 /// Return true if this constant should be sign extended when promoting to 3074 /// a larger type. signExtendConstant(const ConstantInt * C)3075 virtual bool signExtendConstant(const ConstantInt *C) const { return false; } 3076 3077 /// Return true if sinking I's operands to the same basic block as I is 3078 /// profitable, e.g. because the operands can be folded into a target 3079 /// instruction during instruction selection. After calling the function 3080 /// \p Ops contains the Uses to sink ordered by dominance (dominating users 3081 /// come first). shouldSinkOperands(Instruction * I,SmallVectorImpl<Use * > & Ops)3082 virtual bool shouldSinkOperands(Instruction *I, 3083 SmallVectorImpl<Use *> &Ops) const { 3084 return false; 3085 } 3086 3087 /// Try to optimize extending or truncating conversion instructions (like 3088 /// zext, trunc, fptoui, uitofp) for the target. 3089 virtual bool optimizeExtendOrTruncateConversion(Instruction * I,Loop * L,const TargetTransformInfo & TTI)3090 optimizeExtendOrTruncateConversion(Instruction *I, Loop *L, 3091 const TargetTransformInfo &TTI) const { 3092 return false; 3093 } 3094 3095 /// Return true if the target supplies and combines to a paired load 3096 /// two loaded values of type LoadedType next to each other in memory. 3097 /// RequiredAlignment gives the minimal alignment constraints that must be met 3098 /// to be able to select this paired load. 3099 /// 3100 /// This information is *not* used to generate actual paired loads, but it is 3101 /// used to generate a sequence of loads that is easier to combine into a 3102 /// paired load. 3103 /// For instance, something like this: 3104 /// a = load i64* addr 3105 /// b = trunc i64 a to i32 3106 /// c = lshr i64 a, 32 3107 /// d = trunc i64 c to i32 3108 /// will be optimized into: 3109 /// b = load i32* addr1 3110 /// d = load i32* addr2 3111 /// Where addr1 = addr2 +/- sizeof(i32). 3112 /// 3113 /// In other words, unless the target performs a post-isel load combining, 3114 /// this information should not be provided because it will generate more 3115 /// loads. hasPairedLoad(EVT,Align &)3116 virtual bool hasPairedLoad(EVT /*LoadedType*/, 3117 Align & /*RequiredAlignment*/) const { 3118 return false; 3119 } 3120 3121 /// Return true if the target has a vector blend instruction. hasVectorBlend()3122 virtual bool hasVectorBlend() const { return false; } 3123 3124 /// Get the maximum supported factor for interleaved memory accesses. 3125 /// Default to be the minimum interleave factor: 2. getMaxSupportedInterleaveFactor()3126 virtual unsigned getMaxSupportedInterleaveFactor() const { return 2; } 3127 3128 /// Lower an interleaved load to target specific intrinsics. Return 3129 /// true on success. 3130 /// 3131 /// \p LI is the vector load instruction. 3132 /// \p Shuffles is the shufflevector list to DE-interleave the loaded vector. 3133 /// \p Indices is the corresponding indices for each shufflevector. 3134 /// \p Factor is the interleave factor. lowerInterleavedLoad(LoadInst * LI,ArrayRef<ShuffleVectorInst * > Shuffles,ArrayRef<unsigned> Indices,unsigned Factor)3135 virtual bool lowerInterleavedLoad(LoadInst *LI, 3136 ArrayRef<ShuffleVectorInst *> Shuffles, 3137 ArrayRef<unsigned> Indices, 3138 unsigned Factor) const { 3139 return false; 3140 } 3141 3142 /// Lower an interleaved store to target specific intrinsics. Return 3143 /// true on success. 3144 /// 3145 /// \p SI is the vector store instruction. 3146 /// \p SVI is the shufflevector to RE-interleave the stored vector. 3147 /// \p Factor is the interleave factor. lowerInterleavedStore(StoreInst * SI,ShuffleVectorInst * SVI,unsigned Factor)3148 virtual bool lowerInterleavedStore(StoreInst *SI, ShuffleVectorInst *SVI, 3149 unsigned Factor) const { 3150 return false; 3151 } 3152 3153 /// Lower a deinterleave intrinsic to a target specific load intrinsic. 3154 /// Return true on success. Currently only supports 3155 /// llvm.vector.deinterleave2 3156 /// 3157 /// \p DI is the deinterleave intrinsic. 3158 /// \p LI is the accompanying load instruction lowerDeinterleaveIntrinsicToLoad(IntrinsicInst * DI,LoadInst * LI)3159 virtual bool lowerDeinterleaveIntrinsicToLoad(IntrinsicInst *DI, 3160 LoadInst *LI) const { 3161 return false; 3162 } 3163 3164 /// Lower an interleave intrinsic to a target specific store intrinsic. 3165 /// Return true on success. Currently only supports 3166 /// llvm.vector.interleave2 3167 /// 3168 /// \p II is the interleave intrinsic. 3169 /// \p SI is the accompanying store instruction lowerInterleaveIntrinsicToStore(IntrinsicInst * II,StoreInst * SI)3170 virtual bool lowerInterleaveIntrinsicToStore(IntrinsicInst *II, 3171 StoreInst *SI) const { 3172 return false; 3173 } 3174 3175 /// Return true if an fpext operation is free (for instance, because 3176 /// single-precision floating-point numbers are implicitly extended to 3177 /// double-precision). isFPExtFree(EVT DestVT,EVT SrcVT)3178 virtual bool isFPExtFree(EVT DestVT, EVT SrcVT) const { 3179 assert(SrcVT.isFloatingPoint() && DestVT.isFloatingPoint() && 3180 "invalid fpext types"); 3181 return false; 3182 } 3183 3184 /// Return true if an fpext operation input to an \p Opcode operation is free 3185 /// (for instance, because half-precision floating-point numbers are 3186 /// implicitly extended to float-precision) for an FMA instruction. isFPExtFoldable(const MachineInstr & MI,unsigned Opcode,LLT DestTy,LLT SrcTy)3187 virtual bool isFPExtFoldable(const MachineInstr &MI, unsigned Opcode, 3188 LLT DestTy, LLT SrcTy) const { 3189 return false; 3190 } 3191 3192 /// Return true if an fpext operation input to an \p Opcode operation is free 3193 /// (for instance, because half-precision floating-point numbers are 3194 /// implicitly extended to float-precision) for an FMA instruction. isFPExtFoldable(const SelectionDAG & DAG,unsigned Opcode,EVT DestVT,EVT SrcVT)3195 virtual bool isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode, 3196 EVT DestVT, EVT SrcVT) const { 3197 assert(DestVT.isFloatingPoint() && SrcVT.isFloatingPoint() && 3198 "invalid fpext types"); 3199 return isFPExtFree(DestVT, SrcVT); 3200 } 3201 3202 /// Return true if folding a vector load into ExtVal (a sign, zero, or any 3203 /// extend node) is profitable. isVectorLoadExtDesirable(SDValue ExtVal)3204 virtual bool isVectorLoadExtDesirable(SDValue ExtVal) const { return false; } 3205 3206 /// Return true if an fneg operation is free to the point where it is never 3207 /// worthwhile to replace it with a bitwise operation. isFNegFree(EVT VT)3208 virtual bool isFNegFree(EVT VT) const { 3209 assert(VT.isFloatingPoint()); 3210 return false; 3211 } 3212 3213 /// Return true if an fabs operation is free to the point where it is never 3214 /// worthwhile to replace it with a bitwise operation. isFAbsFree(EVT VT)3215 virtual bool isFAbsFree(EVT VT) const { 3216 assert(VT.isFloatingPoint()); 3217 return false; 3218 } 3219 3220 /// Return true if an FMA operation is faster than a pair of fmul and fadd 3221 /// instructions. fmuladd intrinsics will be expanded to FMAs when this method 3222 /// returns true, otherwise fmuladd is expanded to fmul + fadd. 3223 /// 3224 /// NOTE: This may be called before legalization on types for which FMAs are 3225 /// not legal, but should return true if those types will eventually legalize 3226 /// to types that support FMAs. After legalization, it will only be called on 3227 /// types that support FMAs (via Legal or Custom actions) isFMAFasterThanFMulAndFAdd(const MachineFunction & MF,EVT)3228 virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, 3229 EVT) const { 3230 return false; 3231 } 3232 3233 /// Return true if an FMA operation is faster than a pair of fmul and fadd 3234 /// instructions. fmuladd intrinsics will be expanded to FMAs when this method 3235 /// returns true, otherwise fmuladd is expanded to fmul + fadd. 3236 /// 3237 /// NOTE: This may be called before legalization on types for which FMAs are 3238 /// not legal, but should return true if those types will eventually legalize 3239 /// to types that support FMAs. After legalization, it will only be called on 3240 /// types that support FMAs (via Legal or Custom actions) isFMAFasterThanFMulAndFAdd(const MachineFunction & MF,LLT)3241 virtual bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, 3242 LLT) const { 3243 return false; 3244 } 3245 3246 /// IR version isFMAFasterThanFMulAndFAdd(const Function & F,Type *)3247 virtual bool isFMAFasterThanFMulAndFAdd(const Function &F, Type *) const { 3248 return false; 3249 } 3250 3251 /// Returns true if \p MI can be combined with another instruction to 3252 /// form TargetOpcode::G_FMAD. \p N may be an TargetOpcode::G_FADD, 3253 /// TargetOpcode::G_FSUB, or an TargetOpcode::G_FMUL which will be 3254 /// distributed into an fadd/fsub. isFMADLegal(const MachineInstr & MI,LLT Ty)3255 virtual bool isFMADLegal(const MachineInstr &MI, LLT Ty) const { 3256 assert((MI.getOpcode() == TargetOpcode::G_FADD || 3257 MI.getOpcode() == TargetOpcode::G_FSUB || 3258 MI.getOpcode() == TargetOpcode::G_FMUL) && 3259 "unexpected node in FMAD forming combine"); 3260 switch (Ty.getScalarSizeInBits()) { 3261 case 16: 3262 return isOperationLegal(TargetOpcode::G_FMAD, MVT::f16); 3263 case 32: 3264 return isOperationLegal(TargetOpcode::G_FMAD, MVT::f32); 3265 case 64: 3266 return isOperationLegal(TargetOpcode::G_FMAD, MVT::f64); 3267 default: 3268 break; 3269 } 3270 3271 return false; 3272 } 3273 3274 /// Returns true if be combined with to form an ISD::FMAD. \p N may be an 3275 /// ISD::FADD, ISD::FSUB, or an ISD::FMUL which will be distributed into an 3276 /// fadd/fsub. isFMADLegal(const SelectionDAG & DAG,const SDNode * N)3277 virtual bool isFMADLegal(const SelectionDAG &DAG, const SDNode *N) const { 3278 assert((N->getOpcode() == ISD::FADD || N->getOpcode() == ISD::FSUB || 3279 N->getOpcode() == ISD::FMUL) && 3280 "unexpected node in FMAD forming combine"); 3281 return isOperationLegal(ISD::FMAD, N->getValueType(0)); 3282 } 3283 3284 // Return true when the decision to generate FMA's (or FMS, FMLA etc) rather 3285 // than FMUL and ADD is delegated to the machine combiner. generateFMAsInMachineCombiner(EVT VT,CodeGenOptLevel OptLevel)3286 virtual bool generateFMAsInMachineCombiner(EVT VT, 3287 CodeGenOptLevel OptLevel) const { 3288 return false; 3289 } 3290 3291 /// Return true if it's profitable to narrow operations of type SrcVT to 3292 /// DestVT. e.g. on x86, it's profitable to narrow from i32 to i8 but not from 3293 /// i32 to i16. isNarrowingProfitable(EVT SrcVT,EVT DestVT)3294 virtual bool isNarrowingProfitable(EVT SrcVT, EVT DestVT) const { 3295 return false; 3296 } 3297 3298 /// Return true if pulling a binary operation into a select with an identity 3299 /// constant is profitable. This is the inverse of an IR transform. 3300 /// Example: X + (Cond ? Y : 0) --> Cond ? (X + Y) : X shouldFoldSelectWithIdentityConstant(unsigned BinOpcode,EVT VT)3301 virtual bool shouldFoldSelectWithIdentityConstant(unsigned BinOpcode, 3302 EVT VT) const { 3303 return false; 3304 } 3305 3306 /// Return true if it is beneficial to convert a load of a constant to 3307 /// just the constant itself. 3308 /// On some targets it might be more efficient to use a combination of 3309 /// arithmetic instructions to materialize the constant instead of loading it 3310 /// from a constant pool. shouldConvertConstantLoadToIntImm(const APInt & Imm,Type * Ty)3311 virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, 3312 Type *Ty) const { 3313 return false; 3314 } 3315 3316 /// Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type 3317 /// from this source type with this index. This is needed because 3318 /// EXTRACT_SUBVECTOR usually has custom lowering that depends on the index of 3319 /// the first element, and only the target knows which lowering is cheap. isExtractSubvectorCheap(EVT ResVT,EVT SrcVT,unsigned Index)3320 virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, 3321 unsigned Index) const { 3322 return false; 3323 } 3324 3325 /// Try to convert an extract element of a vector binary operation into an 3326 /// extract element followed by a scalar operation. shouldScalarizeBinop(SDValue VecOp)3327 virtual bool shouldScalarizeBinop(SDValue VecOp) const { 3328 return false; 3329 } 3330 3331 /// Return true if extraction of a scalar element from the given vector type 3332 /// at the given index is cheap. For example, if scalar operations occur on 3333 /// the same register file as vector operations, then an extract element may 3334 /// be a sub-register rename rather than an actual instruction. isExtractVecEltCheap(EVT VT,unsigned Index)3335 virtual bool isExtractVecEltCheap(EVT VT, unsigned Index) const { 3336 return false; 3337 } 3338 3339 /// Try to convert math with an overflow comparison into the corresponding DAG 3340 /// node operation. Targets may want to override this independently of whether 3341 /// the operation is legal/custom for the given type because it may obscure 3342 /// matching of other patterns. shouldFormOverflowOp(unsigned Opcode,EVT VT,bool MathUsed)3343 virtual bool shouldFormOverflowOp(unsigned Opcode, EVT VT, 3344 bool MathUsed) const { 3345 // TODO: The default logic is inherited from code in CodeGenPrepare. 3346 // The opcode should not make a difference by default? 3347 if (Opcode != ISD::UADDO) 3348 return false; 3349 3350 // Allow the transform as long as we have an integer type that is not 3351 // obviously illegal and unsupported and if the math result is used 3352 // besides the overflow check. On some targets (e.g. SPARC), it is 3353 // not profitable to form on overflow op if the math result has no 3354 // concrete users. 3355 if (VT.isVector()) 3356 return false; 3357 return MathUsed && (VT.isSimple() || !isOperationExpand(Opcode, VT)); 3358 } 3359 3360 // Return true if it is profitable to use a scalar input to a BUILD_VECTOR 3361 // even if the vector itself has multiple uses. aggressivelyPreferBuildVectorSources(EVT VecVT)3362 virtual bool aggressivelyPreferBuildVectorSources(EVT VecVT) const { 3363 return false; 3364 } 3365 3366 // Return true if CodeGenPrepare should consider splitting large offset of a 3367 // GEP to make the GEP fit into the addressing mode and can be sunk into the 3368 // same blocks of its users. shouldConsiderGEPOffsetSplit()3369 virtual bool shouldConsiderGEPOffsetSplit() const { return false; } 3370 3371 /// Return true if creating a shift of the type by the given 3372 /// amount is not profitable. shouldAvoidTransformToShift(EVT VT,unsigned Amount)3373 virtual bool shouldAvoidTransformToShift(EVT VT, unsigned Amount) const { 3374 return false; 3375 } 3376 3377 // Should we fold (select_cc seteq (and x, y), 0, 0, A) -> (and (sra (shl x)) 3378 // A) where y has a single bit set? shouldFoldSelectWithSingleBitTest(EVT VT,const APInt & AndMask)3379 virtual bool shouldFoldSelectWithSingleBitTest(EVT VT, 3380 const APInt &AndMask) const { 3381 unsigned ShCt = AndMask.getBitWidth() - 1; 3382 return !shouldAvoidTransformToShift(VT, ShCt); 3383 } 3384 3385 /// Does this target require the clearing of high-order bits in a register 3386 /// passed to the fp16 to fp conversion library function. shouldKeepZExtForFP16Conv()3387 virtual bool shouldKeepZExtForFP16Conv() const { return false; } 3388 3389 /// Should we generate fp_to_si_sat and fp_to_ui_sat from type FPVT to type VT 3390 /// from min(max(fptoi)) saturation patterns. shouldConvertFpToSat(unsigned Op,EVT FPVT,EVT VT)3391 virtual bool shouldConvertFpToSat(unsigned Op, EVT FPVT, EVT VT) const { 3392 return isOperationLegalOrCustom(Op, VT); 3393 } 3394 3395 /// Does this target support complex deinterleaving isComplexDeinterleavingSupported()3396 virtual bool isComplexDeinterleavingSupported() const { return false; } 3397 3398 /// Does this target support complex deinterleaving with the given operation 3399 /// and type isComplexDeinterleavingOperationSupported(ComplexDeinterleavingOperation Operation,Type * Ty)3400 virtual bool isComplexDeinterleavingOperationSupported( 3401 ComplexDeinterleavingOperation Operation, Type *Ty) const { 3402 return false; 3403 } 3404 3405 /// Create the IR node for the given complex deinterleaving operation. 3406 /// If one cannot be created using all the given inputs, nullptr should be 3407 /// returned. 3408 virtual Value *createComplexDeinterleavingIR( 3409 IRBuilderBase &B, ComplexDeinterleavingOperation OperationType, 3410 ComplexDeinterleavingRotation Rotation, Value *InputA, Value *InputB, 3411 Value *Accumulator = nullptr) const { 3412 return nullptr; 3413 } 3414 3415 //===--------------------------------------------------------------------===// 3416 // Runtime Library hooks 3417 // 3418 3419 /// Rename the default libcall routine name for the specified libcall. setLibcallName(RTLIB::Libcall Call,const char * Name)3420 void setLibcallName(RTLIB::Libcall Call, const char *Name) { 3421 LibcallRoutineNames[Call] = Name; 3422 } setLibcallName(ArrayRef<RTLIB::Libcall> Calls,const char * Name)3423 void setLibcallName(ArrayRef<RTLIB::Libcall> Calls, const char *Name) { 3424 for (auto Call : Calls) 3425 setLibcallName(Call, Name); 3426 } 3427 3428 /// Get the libcall routine name for the specified libcall. getLibcallName(RTLIB::Libcall Call)3429 const char *getLibcallName(RTLIB::Libcall Call) const { 3430 return LibcallRoutineNames[Call]; 3431 } 3432 3433 /// Override the default CondCode to be used to test the result of the 3434 /// comparison libcall against zero. setCmpLibcallCC(RTLIB::Libcall Call,ISD::CondCode CC)3435 void setCmpLibcallCC(RTLIB::Libcall Call, ISD::CondCode CC) { 3436 CmpLibcallCCs[Call] = CC; 3437 } 3438 3439 /// Get the CondCode that's to be used to test the result of the comparison 3440 /// libcall against zero. getCmpLibcallCC(RTLIB::Libcall Call)3441 ISD::CondCode getCmpLibcallCC(RTLIB::Libcall Call) const { 3442 return CmpLibcallCCs[Call]; 3443 } 3444 3445 /// Set the CallingConv that should be used for the specified libcall. setLibcallCallingConv(RTLIB::Libcall Call,CallingConv::ID CC)3446 void setLibcallCallingConv(RTLIB::Libcall Call, CallingConv::ID CC) { 3447 LibcallCallingConvs[Call] = CC; 3448 } 3449 3450 /// Get the CallingConv that should be used for the specified libcall. getLibcallCallingConv(RTLIB::Libcall Call)3451 CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const { 3452 return LibcallCallingConvs[Call]; 3453 } 3454 3455 /// Execute target specific actions to finalize target lowering. 3456 /// This is used to set extra flags in MachineFrameInformation and freezing 3457 /// the set of reserved registers. 3458 /// The default implementation just freezes the set of reserved registers. 3459 virtual void finalizeLowering(MachineFunction &MF) const; 3460 3461 //===----------------------------------------------------------------------===// 3462 // GlobalISel Hooks 3463 //===----------------------------------------------------------------------===// 3464 /// Check whether or not \p MI needs to be moved close to its uses. 3465 virtual bool shouldLocalize(const MachineInstr &MI, const TargetTransformInfo *TTI) const; 3466 3467 3468 private: 3469 const TargetMachine &TM; 3470 3471 /// Tells the code generator that the target has multiple (allocatable) 3472 /// condition registers that can be used to store the results of comparisons 3473 /// for use by selects and conditional branches. With multiple condition 3474 /// registers, the code generator will not aggressively sink comparisons into 3475 /// the blocks of their users. 3476 bool HasMultipleConditionRegisters; 3477 3478 /// Tells the code generator that the target has BitExtract instructions. 3479 /// The code generator will aggressively sink "shift"s into the blocks of 3480 /// their users if the users will generate "and" instructions which can be 3481 /// combined with "shift" to BitExtract instructions. 3482 bool HasExtractBitsInsn; 3483 3484 /// Tells the code generator to bypass slow divide or remainder 3485 /// instructions. For example, BypassSlowDivWidths[32,8] tells the code 3486 /// generator to bypass 32-bit integer div/rem with an 8-bit unsigned integer 3487 /// div/rem when the operands are positive and less than 256. 3488 DenseMap <unsigned int, unsigned int> BypassSlowDivWidths; 3489 3490 /// Tells the code generator that it shouldn't generate extra flow control 3491 /// instructions and should attempt to combine flow control instructions via 3492 /// predication. 3493 bool JumpIsExpensive; 3494 3495 /// Information about the contents of the high-bits in boolean values held in 3496 /// a type wider than i1. See getBooleanContents. 3497 BooleanContent BooleanContents; 3498 3499 /// Information about the contents of the high-bits in boolean values held in 3500 /// a type wider than i1. See getBooleanContents. 3501 BooleanContent BooleanFloatContents; 3502 3503 /// Information about the contents of the high-bits in boolean vector values 3504 /// when the element type is wider than i1. See getBooleanContents. 3505 BooleanContent BooleanVectorContents; 3506 3507 /// The target scheduling preference: shortest possible total cycles or lowest 3508 /// register usage. 3509 Sched::Preference SchedPreferenceInfo; 3510 3511 /// The minimum alignment that any argument on the stack needs to have. 3512 Align MinStackArgumentAlignment; 3513 3514 /// The minimum function alignment (used when optimizing for size, and to 3515 /// prevent explicitly provided alignment from leading to incorrect code). 3516 Align MinFunctionAlignment; 3517 3518 /// The preferred function alignment (used when alignment unspecified and 3519 /// optimizing for speed). 3520 Align PrefFunctionAlignment; 3521 3522 /// The preferred loop alignment (in log2 bot in bytes). 3523 Align PrefLoopAlignment; 3524 /// The maximum amount of bytes permitted to be emitted for alignment. 3525 unsigned MaxBytesForAlignment; 3526 3527 /// Size in bits of the maximum atomics size the backend supports. 3528 /// Accesses larger than this will be expanded by AtomicExpandPass. 3529 unsigned MaxAtomicSizeInBitsSupported; 3530 3531 /// Size in bits of the maximum div/rem size the backend supports. 3532 /// Larger operations will be expanded by ExpandLargeDivRem. 3533 unsigned MaxDivRemBitWidthSupported; 3534 3535 /// Size in bits of the maximum larget fp convert size the backend 3536 /// supports. Larger operations will be expanded by ExpandLargeFPConvert. 3537 unsigned MaxLargeFPConvertBitWidthSupported; 3538 3539 /// Size in bits of the minimum cmpxchg or ll/sc operation the 3540 /// backend supports. 3541 unsigned MinCmpXchgSizeInBits; 3542 3543 /// This indicates if the target supports unaligned atomic operations. 3544 bool SupportsUnalignedAtomics; 3545 3546 /// If set to a physical register, this specifies the register that 3547 /// llvm.savestack/llvm.restorestack should save and restore. 3548 Register StackPointerRegisterToSaveRestore; 3549 3550 /// This indicates the default register class to use for each ValueType the 3551 /// target supports natively. 3552 const TargetRegisterClass *RegClassForVT[MVT::VALUETYPE_SIZE]; 3553 uint16_t NumRegistersForVT[MVT::VALUETYPE_SIZE]; 3554 MVT RegisterTypeForVT[MVT::VALUETYPE_SIZE]; 3555 3556 /// This indicates the "representative" register class to use for each 3557 /// ValueType the target supports natively. This information is used by the 3558 /// scheduler to track register pressure. By default, the representative 3559 /// register class is the largest legal super-reg register class of the 3560 /// register class of the specified type. e.g. On x86, i8, i16, and i32's 3561 /// representative class would be GR32. 3562 const TargetRegisterClass *RepRegClassForVT[MVT::VALUETYPE_SIZE] = {0}; 3563 3564 /// This indicates the "cost" of the "representative" register class for each 3565 /// ValueType. The cost is used by the scheduler to approximate register 3566 /// pressure. 3567 uint8_t RepRegClassCostForVT[MVT::VALUETYPE_SIZE]; 3568 3569 /// For any value types we are promoting or expanding, this contains the value 3570 /// type that we are changing to. For Expanded types, this contains one step 3571 /// of the expand (e.g. i64 -> i32), even if there are multiple steps required 3572 /// (e.g. i64 -> i16). For types natively supported by the system, this holds 3573 /// the same type (e.g. i32 -> i32). 3574 MVT TransformToType[MVT::VALUETYPE_SIZE]; 3575 3576 /// For each operation and each value type, keep a LegalizeAction that 3577 /// indicates how instruction selection should deal with the operation. Most 3578 /// operations are Legal (aka, supported natively by the target), but 3579 /// operations that are not should be described. Note that operations on 3580 /// non-legal value types are not described here. 3581 LegalizeAction OpActions[MVT::VALUETYPE_SIZE][ISD::BUILTIN_OP_END]; 3582 3583 /// For each load extension type and each value type, keep a LegalizeAction 3584 /// that indicates how instruction selection should deal with a load of a 3585 /// specific value type and extension type. Uses 4-bits to store the action 3586 /// for each of the 4 load ext types. 3587 uint16_t LoadExtActions[MVT::VALUETYPE_SIZE][MVT::VALUETYPE_SIZE]; 3588 3589 /// Similar to LoadExtActions, but for atomic loads. Only Legal or Expand 3590 /// (default) values are supported. 3591 uint16_t AtomicLoadExtActions[MVT::VALUETYPE_SIZE][MVT::VALUETYPE_SIZE]; 3592 3593 /// For each value type pair keep a LegalizeAction that indicates whether a 3594 /// truncating store of a specific value type and truncating type is legal. 3595 LegalizeAction TruncStoreActions[MVT::VALUETYPE_SIZE][MVT::VALUETYPE_SIZE]; 3596 3597 /// For each indexed mode and each value type, keep a quad of LegalizeAction 3598 /// that indicates how instruction selection should deal with the load / 3599 /// store / maskedload / maskedstore. 3600 /// 3601 /// The first dimension is the value_type for the reference. The second 3602 /// dimension represents the various modes for load store. 3603 uint16_t IndexedModeActions[MVT::VALUETYPE_SIZE][ISD::LAST_INDEXED_MODE]; 3604 3605 /// For each condition code (ISD::CondCode) keep a LegalizeAction that 3606 /// indicates how instruction selection should deal with the condition code. 3607 /// 3608 /// Because each CC action takes up 4 bits, we need to have the array size be 3609 /// large enough to fit all of the value types. This can be done by rounding 3610 /// up the MVT::VALUETYPE_SIZE value to the next multiple of 8. 3611 uint32_t CondCodeActions[ISD::SETCC_INVALID][(MVT::VALUETYPE_SIZE + 7) / 8]; 3612 3613 ValueTypeActionImpl ValueTypeActions; 3614 3615 private: 3616 /// Targets can specify ISD nodes that they would like PerformDAGCombine 3617 /// callbacks for by calling setTargetDAGCombine(), which sets a bit in this 3618 /// array. 3619 unsigned char 3620 TargetDAGCombineArray[(ISD::BUILTIN_OP_END+CHAR_BIT-1)/CHAR_BIT]; 3621 3622 /// For operations that must be promoted to a specific type, this holds the 3623 /// destination type. This map should be sparse, so don't hold it as an 3624 /// array. 3625 /// 3626 /// Targets add entries to this map with AddPromotedToType(..), clients access 3627 /// this with getTypeToPromoteTo(..). 3628 std::map<std::pair<unsigned, MVT::SimpleValueType>, MVT::SimpleValueType> 3629 PromoteToType; 3630 3631 /// Stores the name each libcall. 3632 const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL + 1]; 3633 3634 /// The ISD::CondCode that should be used to test the result of each of the 3635 /// comparison libcall against zero. 3636 ISD::CondCode CmpLibcallCCs[RTLIB::UNKNOWN_LIBCALL]; 3637 3638 /// Stores the CallingConv that should be used for each libcall. 3639 CallingConv::ID LibcallCallingConvs[RTLIB::UNKNOWN_LIBCALL]; 3640 3641 /// Set default libcall names and calling conventions. 3642 void InitLibcalls(const Triple &TT); 3643 3644 /// The bits of IndexedModeActions used to store the legalisation actions 3645 /// We store the data as | ML | MS | L | S | each taking 4 bits. 3646 enum IndexedModeActionsBits { 3647 IMAB_Store = 0, 3648 IMAB_Load = 4, 3649 IMAB_MaskedStore = 8, 3650 IMAB_MaskedLoad = 12 3651 }; 3652 setIndexedModeAction(unsigned IdxMode,MVT VT,unsigned Shift,LegalizeAction Action)3653 void setIndexedModeAction(unsigned IdxMode, MVT VT, unsigned Shift, 3654 LegalizeAction Action) { 3655 assert(VT.isValid() && IdxMode < ISD::LAST_INDEXED_MODE && 3656 (unsigned)Action < 0xf && "Table isn't big enough!"); 3657 unsigned Ty = (unsigned)VT.SimpleTy; 3658 IndexedModeActions[Ty][IdxMode] &= ~(0xf << Shift); 3659 IndexedModeActions[Ty][IdxMode] |= ((uint16_t)Action) << Shift; 3660 } 3661 getIndexedModeAction(unsigned IdxMode,MVT VT,unsigned Shift)3662 LegalizeAction getIndexedModeAction(unsigned IdxMode, MVT VT, 3663 unsigned Shift) const { 3664 assert(IdxMode < ISD::LAST_INDEXED_MODE && VT.isValid() && 3665 "Table isn't big enough!"); 3666 unsigned Ty = (unsigned)VT.SimpleTy; 3667 return (LegalizeAction)((IndexedModeActions[Ty][IdxMode] >> Shift) & 0xf); 3668 } 3669 3670 protected: 3671 /// Return true if the extension represented by \p I is free. 3672 /// \pre \p I is a sign, zero, or fp extension and 3673 /// is[Z|FP]ExtFree of the related types is not true. isExtFreeImpl(const Instruction * I)3674 virtual bool isExtFreeImpl(const Instruction *I) const { return false; } 3675 3676 /// Depth that GatherAllAliases should continue looking for chain 3677 /// dependencies when trying to find a more preferable chain. As an 3678 /// approximation, this should be more than the number of consecutive stores 3679 /// expected to be merged. 3680 unsigned GatherAllAliasesMaxDepth; 3681 3682 /// \brief Specify maximum number of store instructions per memset call. 3683 /// 3684 /// When lowering \@llvm.memset this field specifies the maximum number of 3685 /// store operations that may be substituted for the call to memset. Targets 3686 /// must set this value based on the cost threshold for that target. Targets 3687 /// should assume that the memset will be done using as many of the largest 3688 /// store operations first, followed by smaller ones, if necessary, per 3689 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine 3690 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte 3691 /// store. This only applies to setting a constant array of a constant size. 3692 unsigned MaxStoresPerMemset; 3693 /// Likewise for functions with the OptSize attribute. 3694 unsigned MaxStoresPerMemsetOptSize; 3695 3696 /// \brief Specify maximum number of store instructions per memcpy call. 3697 /// 3698 /// When lowering \@llvm.memcpy this field specifies the maximum number of 3699 /// store operations that may be substituted for a call to memcpy. Targets 3700 /// must set this value based on the cost threshold for that target. Targets 3701 /// should assume that the memcpy will be done using as many of the largest 3702 /// store operations first, followed by smaller ones, if necessary, per 3703 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine 3704 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store 3705 /// and one 1-byte store. This only applies to copying a constant array of 3706 /// constant size. 3707 unsigned MaxStoresPerMemcpy; 3708 /// Likewise for functions with the OptSize attribute. 3709 unsigned MaxStoresPerMemcpyOptSize; 3710 /// \brief Specify max number of store instructions to glue in inlined memcpy. 3711 /// 3712 /// When memcpy is inlined based on MaxStoresPerMemcpy, specify maximum number 3713 /// of store instructions to keep together. This helps in pairing and 3714 // vectorization later on. 3715 unsigned MaxGluedStoresPerMemcpy = 0; 3716 3717 /// \brief Specify maximum number of load instructions per memcmp call. 3718 /// 3719 /// When lowering \@llvm.memcmp this field specifies the maximum number of 3720 /// pairs of load operations that may be substituted for a call to memcmp. 3721 /// Targets must set this value based on the cost threshold for that target. 3722 /// Targets should assume that the memcmp will be done using as many of the 3723 /// largest load operations first, followed by smaller ones, if necessary, per 3724 /// alignment restrictions. For example, loading 7 bytes on a 32-bit machine 3725 /// with 32-bit alignment would result in one 4-byte load, a one 2-byte load 3726 /// and one 1-byte load. This only applies to copying a constant array of 3727 /// constant size. 3728 unsigned MaxLoadsPerMemcmp; 3729 /// Likewise for functions with the OptSize attribute. 3730 unsigned MaxLoadsPerMemcmpOptSize; 3731 3732 /// \brief Specify maximum number of store instructions per memmove call. 3733 /// 3734 /// When lowering \@llvm.memmove this field specifies the maximum number of 3735 /// store instructions that may be substituted for a call to memmove. Targets 3736 /// must set this value based on the cost threshold for that target. Targets 3737 /// should assume that the memmove will be done using as many of the largest 3738 /// store operations first, followed by smaller ones, if necessary, per 3739 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine 3740 /// with 8-bit alignment would result in nine 1-byte stores. This only 3741 /// applies to copying a constant array of constant size. 3742 unsigned MaxStoresPerMemmove; 3743 /// Likewise for functions with the OptSize attribute. 3744 unsigned MaxStoresPerMemmoveOptSize; 3745 3746 /// Tells the code generator that select is more expensive than a branch if 3747 /// the branch is usually predicted right. 3748 bool PredictableSelectIsExpensive; 3749 3750 /// \see enableExtLdPromotion. 3751 bool EnableExtLdPromotion; 3752 3753 /// Return true if the value types that can be represented by the specified 3754 /// register class are all legal. 3755 bool isLegalRC(const TargetRegisterInfo &TRI, 3756 const TargetRegisterClass &RC) const; 3757 3758 /// Replace/modify any TargetFrameIndex operands with a targte-dependent 3759 /// sequence of memory operands that is recognized by PrologEpilogInserter. 3760 MachineBasicBlock *emitPatchPoint(MachineInstr &MI, 3761 MachineBasicBlock *MBB) const; 3762 3763 bool IsStrictFPEnabled; 3764 }; 3765 3766 /// This class defines information used to lower LLVM code to legal SelectionDAG 3767 /// operators that the target instruction selector can accept natively. 3768 /// 3769 /// This class also defines callbacks that targets must implement to lower 3770 /// target-specific constructs to SelectionDAG operators. 3771 class TargetLowering : public TargetLoweringBase { 3772 public: 3773 struct DAGCombinerInfo; 3774 struct MakeLibCallOptions; 3775 3776 TargetLowering(const TargetLowering &) = delete; 3777 TargetLowering &operator=(const TargetLowering &) = delete; 3778 3779 explicit TargetLowering(const TargetMachine &TM); 3780 3781 bool isPositionIndependent() const; 3782 isSDNodeSourceOfDivergence(const SDNode * N,FunctionLoweringInfo * FLI,UniformityInfo * UA)3783 virtual bool isSDNodeSourceOfDivergence(const SDNode *N, 3784 FunctionLoweringInfo *FLI, 3785 UniformityInfo *UA) const { 3786 return false; 3787 } 3788 3789 // Lets target to control the following reassociation of operands: (op (op x, 3790 // c1), y) -> (op (op x, y), c1) where N0 is (op x, c1) and N1 is y. By 3791 // default consider profitable any case where N0 has single use. This 3792 // behavior reflects the condition replaced by this target hook call in the 3793 // DAGCombiner. Any particular target can implement its own heuristic to 3794 // restrict common combiner. isReassocProfitable(SelectionDAG & DAG,SDValue N0,SDValue N1)3795 virtual bool isReassocProfitable(SelectionDAG &DAG, SDValue N0, 3796 SDValue N1) const { 3797 return N0.hasOneUse(); 3798 } 3799 3800 // Lets target to control the following reassociation of operands: (op (op x, 3801 // c1), y) -> (op (op x, y), c1) where N0 is (op x, c1) and N1 is y. By 3802 // default consider profitable any case where N0 has single use. This 3803 // behavior reflects the condition replaced by this target hook call in the 3804 // combiner. Any particular target can implement its own heuristic to 3805 // restrict common combiner. isReassocProfitable(MachineRegisterInfo & MRI,Register N0,Register N1)3806 virtual bool isReassocProfitable(MachineRegisterInfo &MRI, Register N0, 3807 Register N1) const { 3808 return MRI.hasOneNonDBGUse(N0); 3809 } 3810 isSDNodeAlwaysUniform(const SDNode * N)3811 virtual bool isSDNodeAlwaysUniform(const SDNode * N) const { 3812 return false; 3813 } 3814 3815 /// Returns true by value, base pointer and offset pointer and addressing mode 3816 /// by reference if the node's address can be legally represented as 3817 /// pre-indexed load / store address. getPreIndexedAddressParts(SDNode *,SDValue &,SDValue &,ISD::MemIndexedMode &,SelectionDAG &)3818 virtual bool getPreIndexedAddressParts(SDNode * /*N*/, SDValue &/*Base*/, 3819 SDValue &/*Offset*/, 3820 ISD::MemIndexedMode &/*AM*/, 3821 SelectionDAG &/*DAG*/) const { 3822 return false; 3823 } 3824 3825 /// Returns true by value, base pointer and offset pointer and addressing mode 3826 /// by reference if this node can be combined with a load / store to form a 3827 /// post-indexed load / store. getPostIndexedAddressParts(SDNode *,SDNode *,SDValue &,SDValue &,ISD::MemIndexedMode &,SelectionDAG &)3828 virtual bool getPostIndexedAddressParts(SDNode * /*N*/, SDNode * /*Op*/, 3829 SDValue &/*Base*/, 3830 SDValue &/*Offset*/, 3831 ISD::MemIndexedMode &/*AM*/, 3832 SelectionDAG &/*DAG*/) const { 3833 return false; 3834 } 3835 3836 /// Returns true if the specified base+offset is a legal indexed addressing 3837 /// mode for this target. \p MI is the load or store instruction that is being 3838 /// considered for transformation. isIndexingLegal(MachineInstr & MI,Register Base,Register Offset,bool IsPre,MachineRegisterInfo & MRI)3839 virtual bool isIndexingLegal(MachineInstr &MI, Register Base, Register Offset, 3840 bool IsPre, MachineRegisterInfo &MRI) const { 3841 return false; 3842 } 3843 3844 /// Return the entry encoding for a jump table in the current function. The 3845 /// returned value is a member of the MachineJumpTableInfo::JTEntryKind enum. 3846 virtual unsigned getJumpTableEncoding() const; 3847 3848 virtual const MCExpr * LowerCustomJumpTableEntry(const MachineJumpTableInfo *,const MachineBasicBlock *,unsigned,MCContext &)3849 LowerCustomJumpTableEntry(const MachineJumpTableInfo * /*MJTI*/, 3850 const MachineBasicBlock * /*MBB*/, unsigned /*uid*/, 3851 MCContext &/*Ctx*/) const { 3852 llvm_unreachable("Need to implement this hook if target has custom JTIs"); 3853 } 3854 3855 /// Returns relocation base for the given PIC jumptable. 3856 virtual SDValue getPICJumpTableRelocBase(SDValue Table, 3857 SelectionDAG &DAG) const; 3858 3859 /// This returns the relocation base for the given PIC jumptable, the same as 3860 /// getPICJumpTableRelocBase, but as an MCExpr. 3861 virtual const MCExpr * 3862 getPICJumpTableRelocBaseExpr(const MachineFunction *MF, 3863 unsigned JTI, MCContext &Ctx) const; 3864 3865 /// Return true if folding a constant offset with the given GlobalAddress is 3866 /// legal. It is frequently not legal in PIC relocation models. 3867 virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const; 3868 3869 /// On x86, return true if the operand with index OpNo is a CALL or JUMP 3870 /// instruction, which can use either a memory constraint or an address 3871 /// constraint. -fasm-blocks "__asm call foo" lowers to 3872 /// call void asm sideeffect inteldialect "call ${0:P}", "*m..." 3873 /// 3874 /// This function is used by a hack to choose the address constraint, 3875 /// lowering to a direct call. 3876 virtual bool isInlineAsmTargetBranch(const SmallVectorImpl<StringRef> & AsmStrs,unsigned OpNo)3877 isInlineAsmTargetBranch(const SmallVectorImpl<StringRef> &AsmStrs, 3878 unsigned OpNo) const { 3879 return false; 3880 } 3881 3882 bool isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, 3883 SDValue &Chain) const; 3884 3885 void softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, 3886 SDValue &NewRHS, ISD::CondCode &CCCode, 3887 const SDLoc &DL, const SDValue OldLHS, 3888 const SDValue OldRHS) const; 3889 3890 void softenSetCCOperands(SelectionDAG &DAG, EVT VT, SDValue &NewLHS, 3891 SDValue &NewRHS, ISD::CondCode &CCCode, 3892 const SDLoc &DL, const SDValue OldLHS, 3893 const SDValue OldRHS, SDValue &Chain, 3894 bool IsSignaling = false) const; 3895 3896 /// Returns a pair of (return value, chain). 3897 /// It is an error to pass RTLIB::UNKNOWN_LIBCALL as \p LC. 3898 std::pair<SDValue, SDValue> makeLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, 3899 EVT RetVT, ArrayRef<SDValue> Ops, 3900 MakeLibCallOptions CallOptions, 3901 const SDLoc &dl, 3902 SDValue Chain = SDValue()) const; 3903 3904 /// Check whether parameters to a call that are passed in callee saved 3905 /// registers are the same as from the calling function. This needs to be 3906 /// checked for tail call eligibility. 3907 bool parametersInCSRMatch(const MachineRegisterInfo &MRI, 3908 const uint32_t *CallerPreservedMask, 3909 const SmallVectorImpl<CCValAssign> &ArgLocs, 3910 const SmallVectorImpl<SDValue> &OutVals) const; 3911 3912 //===--------------------------------------------------------------------===// 3913 // TargetLowering Optimization Methods 3914 // 3915 3916 /// A convenience struct that encapsulates a DAG, and two SDValues for 3917 /// returning information from TargetLowering to its clients that want to 3918 /// combine. 3919 struct TargetLoweringOpt { 3920 SelectionDAG &DAG; 3921 bool LegalTys; 3922 bool LegalOps; 3923 SDValue Old; 3924 SDValue New; 3925 TargetLoweringOptTargetLoweringOpt3926 explicit TargetLoweringOpt(SelectionDAG &InDAG, 3927 bool LT, bool LO) : 3928 DAG(InDAG), LegalTys(LT), LegalOps(LO) {} 3929 LegalTypesTargetLoweringOpt3930 bool LegalTypes() const { return LegalTys; } LegalOperationsTargetLoweringOpt3931 bool LegalOperations() const { return LegalOps; } 3932 CombineToTargetLoweringOpt3933 bool CombineTo(SDValue O, SDValue N) { 3934 Old = O; 3935 New = N; 3936 return true; 3937 } 3938 }; 3939 3940 /// Determines the optimal series of memory ops to replace the memset / memcpy. 3941 /// Return true if the number of memory ops is below the threshold (Limit). 3942 /// Note that this is always the case when Limit is ~0. 3943 /// It returns the types of the sequence of memory ops to perform 3944 /// memset / memcpy by reference. 3945 virtual bool 3946 findOptimalMemOpLowering(std::vector<EVT> &MemOps, unsigned Limit, 3947 const MemOp &Op, unsigned DstAS, unsigned SrcAS, 3948 const AttributeList &FuncAttributes) const; 3949 3950 /// Check to see if the specified operand of the specified instruction is a 3951 /// constant integer. If so, check to see if there are any bits set in the 3952 /// constant that are not demanded. If so, shrink the constant and return 3953 /// true. 3954 bool ShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits, 3955 const APInt &DemandedElts, 3956 TargetLoweringOpt &TLO) const; 3957 3958 /// Helper wrapper around ShrinkDemandedConstant, demanding all elements. 3959 bool ShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits, 3960 TargetLoweringOpt &TLO) const; 3961 3962 // Target hook to do target-specific const optimization, which is called by 3963 // ShrinkDemandedConstant. This function should return true if the target 3964 // doesn't want ShrinkDemandedConstant to further optimize the constant. targetShrinkDemandedConstant(SDValue Op,const APInt & DemandedBits,const APInt & DemandedElts,TargetLoweringOpt & TLO)3965 virtual bool targetShrinkDemandedConstant(SDValue Op, 3966 const APInt &DemandedBits, 3967 const APInt &DemandedElts, 3968 TargetLoweringOpt &TLO) const { 3969 return false; 3970 } 3971 3972 /// Convert x+y to (VT)((SmallVT)x+(SmallVT)y) if the casts are free. 3973 /// This uses isTruncateFree/isZExtFree and ANY_EXTEND for the widening cast, 3974 /// but it could be generalized for targets with other types of implicit 3975 /// widening casts. 3976 bool ShrinkDemandedOp(SDValue Op, unsigned BitWidth, 3977 const APInt &DemandedBits, 3978 TargetLoweringOpt &TLO) const; 3979 3980 /// Look at Op. At this point, we know that only the DemandedBits bits of the 3981 /// result of Op are ever used downstream. If we can use this information to 3982 /// simplify Op, create a new simplified DAG node and return true, returning 3983 /// the original and new nodes in Old and New. Otherwise, analyze the 3984 /// expression and return a mask of KnownOne and KnownZero bits for the 3985 /// expression (used to simplify the caller). The KnownZero/One bits may only 3986 /// be accurate for those bits in the Demanded masks. 3987 /// \p AssumeSingleUse When this parameter is true, this function will 3988 /// attempt to simplify \p Op even if there are multiple uses. 3989 /// Callers are responsible for correctly updating the DAG based on the 3990 /// results of this function, because simply replacing TLO.Old 3991 /// with TLO.New will be incorrect when this parameter is true and TLO.Old 3992 /// has multiple uses. 3993 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 3994 const APInt &DemandedElts, KnownBits &Known, 3995 TargetLoweringOpt &TLO, unsigned Depth = 0, 3996 bool AssumeSingleUse = false) const; 3997 3998 /// Helper wrapper around SimplifyDemandedBits, demanding all elements. 3999 /// Adds Op back to the worklist upon success. 4000 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 4001 KnownBits &Known, TargetLoweringOpt &TLO, 4002 unsigned Depth = 0, 4003 bool AssumeSingleUse = false) const; 4004 4005 /// Helper wrapper around SimplifyDemandedBits. 4006 /// Adds Op back to the worklist upon success. 4007 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 4008 DAGCombinerInfo &DCI) const; 4009 4010 /// Helper wrapper around SimplifyDemandedBits. 4011 /// Adds Op back to the worklist upon success. 4012 bool SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits, 4013 const APInt &DemandedElts, 4014 DAGCombinerInfo &DCI) const; 4015 4016 /// More limited version of SimplifyDemandedBits that can be used to "look 4017 /// through" ops that don't contribute to the DemandedBits/DemandedElts - 4018 /// bitwise ops etc. 4019 SDValue SimplifyMultipleUseDemandedBits(SDValue Op, const APInt &DemandedBits, 4020 const APInt &DemandedElts, 4021 SelectionDAG &DAG, 4022 unsigned Depth = 0) const; 4023 4024 /// Helper wrapper around SimplifyMultipleUseDemandedBits, demanding all 4025 /// elements. 4026 SDValue SimplifyMultipleUseDemandedBits(SDValue Op, const APInt &DemandedBits, 4027 SelectionDAG &DAG, 4028 unsigned Depth = 0) const; 4029 4030 /// Helper wrapper around SimplifyMultipleUseDemandedBits, demanding all 4031 /// bits from only some vector elements. 4032 SDValue SimplifyMultipleUseDemandedVectorElts(SDValue Op, 4033 const APInt &DemandedElts, 4034 SelectionDAG &DAG, 4035 unsigned Depth = 0) const; 4036 4037 /// Look at Vector Op. At this point, we know that only the DemandedElts 4038 /// elements of the result of Op are ever used downstream. If we can use 4039 /// this information to simplify Op, create a new simplified DAG node and 4040 /// return true, storing the original and new nodes in TLO. 4041 /// Otherwise, analyze the expression and return a mask of KnownUndef and 4042 /// KnownZero elements for the expression (used to simplify the caller). 4043 /// The KnownUndef/Zero elements may only be accurate for those bits 4044 /// in the DemandedMask. 4045 /// \p AssumeSingleUse When this parameter is true, this function will 4046 /// attempt to simplify \p Op even if there are multiple uses. 4047 /// Callers are responsible for correctly updating the DAG based on the 4048 /// results of this function, because simply replacing TLO.Old 4049 /// with TLO.New will be incorrect when this parameter is true and TLO.Old 4050 /// has multiple uses. 4051 bool SimplifyDemandedVectorElts(SDValue Op, const APInt &DemandedEltMask, 4052 APInt &KnownUndef, APInt &KnownZero, 4053 TargetLoweringOpt &TLO, unsigned Depth = 0, 4054 bool AssumeSingleUse = false) const; 4055 4056 /// Helper wrapper around SimplifyDemandedVectorElts. 4057 /// Adds Op back to the worklist upon success. 4058 bool SimplifyDemandedVectorElts(SDValue Op, const APInt &DemandedElts, 4059 DAGCombinerInfo &DCI) const; 4060 4061 /// Return true if the target supports simplifying demanded vector elements by 4062 /// converting them to undefs. 4063 virtual bool shouldSimplifyDemandedVectorElts(SDValue Op,const TargetLoweringOpt & TLO)4064 shouldSimplifyDemandedVectorElts(SDValue Op, 4065 const TargetLoweringOpt &TLO) const { 4066 return true; 4067 } 4068 4069 /// Determine which of the bits specified in Mask are known to be either zero 4070 /// or one and return them in the KnownZero/KnownOne bitsets. The DemandedElts 4071 /// argument allows us to only collect the known bits that are shared by the 4072 /// requested vector elements. 4073 virtual void computeKnownBitsForTargetNode(const SDValue Op, 4074 KnownBits &Known, 4075 const APInt &DemandedElts, 4076 const SelectionDAG &DAG, 4077 unsigned Depth = 0) const; 4078 4079 /// Determine which of the bits specified in Mask are known to be either zero 4080 /// or one and return them in the KnownZero/KnownOne bitsets. The DemandedElts 4081 /// argument allows us to only collect the known bits that are shared by the 4082 /// requested vector elements. This is for GISel. 4083 virtual void computeKnownBitsForTargetInstr(GISelKnownBits &Analysis, 4084 Register R, KnownBits &Known, 4085 const APInt &DemandedElts, 4086 const MachineRegisterInfo &MRI, 4087 unsigned Depth = 0) const; 4088 4089 /// Determine the known alignment for the pointer value \p R. This is can 4090 /// typically be inferred from the number of low known 0 bits. However, for a 4091 /// pointer with a non-integral address space, the alignment value may be 4092 /// independent from the known low bits. 4093 virtual Align computeKnownAlignForTargetInstr(GISelKnownBits &Analysis, 4094 Register R, 4095 const MachineRegisterInfo &MRI, 4096 unsigned Depth = 0) const; 4097 4098 /// Determine which of the bits of FrameIndex \p FIOp are known to be 0. 4099 /// Default implementation computes low bits based on alignment 4100 /// information. This should preserve known bits passed into it. 4101 virtual void computeKnownBitsForFrameIndex(int FIOp, 4102 KnownBits &Known, 4103 const MachineFunction &MF) const; 4104 4105 /// This method can be implemented by targets that want to expose additional 4106 /// information about sign bits to the DAG Combiner. The DemandedElts 4107 /// argument allows us to only collect the minimum sign bits that are shared 4108 /// by the requested vector elements. 4109 virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, 4110 const APInt &DemandedElts, 4111 const SelectionDAG &DAG, 4112 unsigned Depth = 0) const; 4113 4114 /// This method can be implemented by targets that want to expose additional 4115 /// information about sign bits to GlobalISel combiners. The DemandedElts 4116 /// argument allows us to only collect the minimum sign bits that are shared 4117 /// by the requested vector elements. 4118 virtual unsigned computeNumSignBitsForTargetInstr(GISelKnownBits &Analysis, 4119 Register R, 4120 const APInt &DemandedElts, 4121 const MachineRegisterInfo &MRI, 4122 unsigned Depth = 0) const; 4123 4124 /// Attempt to simplify any target nodes based on the demanded vector 4125 /// elements, returning true on success. Otherwise, analyze the expression and 4126 /// return a mask of KnownUndef and KnownZero elements for the expression 4127 /// (used to simplify the caller). The KnownUndef/Zero elements may only be 4128 /// accurate for those bits in the DemandedMask. 4129 virtual bool SimplifyDemandedVectorEltsForTargetNode( 4130 SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, 4131 APInt &KnownZero, TargetLoweringOpt &TLO, unsigned Depth = 0) const; 4132 4133 /// Attempt to simplify any target nodes based on the demanded bits/elts, 4134 /// returning true on success. Otherwise, analyze the 4135 /// expression and return a mask of KnownOne and KnownZero bits for the 4136 /// expression (used to simplify the caller). The KnownZero/One bits may only 4137 /// be accurate for those bits in the Demanded masks. 4138 virtual bool SimplifyDemandedBitsForTargetNode(SDValue Op, 4139 const APInt &DemandedBits, 4140 const APInt &DemandedElts, 4141 KnownBits &Known, 4142 TargetLoweringOpt &TLO, 4143 unsigned Depth = 0) const; 4144 4145 /// More limited version of SimplifyDemandedBits that can be used to "look 4146 /// through" ops that don't contribute to the DemandedBits/DemandedElts - 4147 /// bitwise ops etc. 4148 virtual SDValue SimplifyMultipleUseDemandedBitsForTargetNode( 4149 SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, 4150 SelectionDAG &DAG, unsigned Depth) const; 4151 4152 /// Return true if this function can prove that \p Op is never poison 4153 /// and, if \p PoisonOnly is false, does not have undef bits. The DemandedElts 4154 /// argument limits the check to the requested vector elements. 4155 virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode( 4156 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, 4157 bool PoisonOnly, unsigned Depth) const; 4158 4159 /// Return true if Op can create undef or poison from non-undef & non-poison 4160 /// operands. The DemandedElts argument limits the check to the requested 4161 /// vector elements. 4162 virtual bool 4163 canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, 4164 const SelectionDAG &DAG, bool PoisonOnly, 4165 bool ConsiderFlags, unsigned Depth) const; 4166 4167 /// Tries to build a legal vector shuffle using the provided parameters 4168 /// or equivalent variations. The Mask argument maybe be modified as the 4169 /// function tries different variations. 4170 /// Returns an empty SDValue if the operation fails. 4171 SDValue buildLegalVectorShuffle(EVT VT, const SDLoc &DL, SDValue N0, 4172 SDValue N1, MutableArrayRef<int> Mask, 4173 SelectionDAG &DAG) const; 4174 4175 /// This method returns the constant pool value that will be loaded by LD. 4176 /// NOTE: You must check for implicit extensions of the constant by LD. 4177 virtual const Constant *getTargetConstantFromLoad(LoadSDNode *LD) const; 4178 4179 /// If \p SNaN is false, \returns true if \p Op is known to never be any 4180 /// NaN. If \p sNaN is true, returns if \p Op is known to never be a signaling 4181 /// NaN. 4182 virtual bool isKnownNeverNaNForTargetNode(SDValue Op, 4183 const SelectionDAG &DAG, 4184 bool SNaN = false, 4185 unsigned Depth = 0) const; 4186 4187 /// Return true if vector \p Op has the same value across all \p DemandedElts, 4188 /// indicating any elements which may be undef in the output \p UndefElts. 4189 virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, 4190 APInt &UndefElts, 4191 const SelectionDAG &DAG, 4192 unsigned Depth = 0) const; 4193 4194 /// Returns true if the given Opc is considered a canonical constant for the 4195 /// target, which should not be transformed back into a BUILD_VECTOR. isTargetCanonicalConstantNode(SDValue Op)4196 virtual bool isTargetCanonicalConstantNode(SDValue Op) const { 4197 return Op.getOpcode() == ISD::SPLAT_VECTOR || 4198 Op.getOpcode() == ISD::SPLAT_VECTOR_PARTS; 4199 } 4200 4201 struct DAGCombinerInfo { 4202 void *DC; // The DAG Combiner object. 4203 CombineLevel Level; 4204 bool CalledByLegalizer; 4205 4206 public: 4207 SelectionDAG &DAG; 4208 DAGCombinerInfoDAGCombinerInfo4209 DAGCombinerInfo(SelectionDAG &dag, CombineLevel level, bool cl, void *dc) 4210 : DC(dc), Level(level), CalledByLegalizer(cl), DAG(dag) {} 4211 isBeforeLegalizeDAGCombinerInfo4212 bool isBeforeLegalize() const { return Level == BeforeLegalizeTypes; } isBeforeLegalizeOpsDAGCombinerInfo4213 bool isBeforeLegalizeOps() const { return Level < AfterLegalizeVectorOps; } isAfterLegalizeDAGDAGCombinerInfo4214 bool isAfterLegalizeDAG() const { return Level >= AfterLegalizeDAG; } getDAGCombineLevelDAGCombinerInfo4215 CombineLevel getDAGCombineLevel() { return Level; } isCalledByLegalizerDAGCombinerInfo4216 bool isCalledByLegalizer() const { return CalledByLegalizer; } 4217 4218 void AddToWorklist(SDNode *N); 4219 SDValue CombineTo(SDNode *N, ArrayRef<SDValue> To, bool AddTo = true); 4220 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true); 4221 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo = true); 4222 4223 bool recursivelyDeleteUnusedNodes(SDNode *N); 4224 4225 void CommitTargetLoweringOpt(const TargetLoweringOpt &TLO); 4226 }; 4227 4228 /// Return if the N is a constant or constant vector equal to the true value 4229 /// from getBooleanContents(). 4230 bool isConstTrueVal(SDValue N) const; 4231 4232 /// Return if the N is a constant or constant vector equal to the false value 4233 /// from getBooleanContents(). 4234 bool isConstFalseVal(SDValue N) const; 4235 4236 /// Return if \p N is a True value when extended to \p VT. 4237 bool isExtendedTrueVal(const ConstantSDNode *N, EVT VT, bool SExt) const; 4238 4239 /// Try to simplify a setcc built with the specified operands and cc. If it is 4240 /// unable to simplify it, return a null SDValue. 4241 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, 4242 bool foldBooleans, DAGCombinerInfo &DCI, 4243 const SDLoc &dl) const; 4244 4245 // For targets which wrap address, unwrap for analysis. unwrapAddress(SDValue N)4246 virtual SDValue unwrapAddress(SDValue N) const { return N; } 4247 4248 /// Returns true (and the GlobalValue and the offset) if the node is a 4249 /// GlobalAddress + offset. 4250 virtual bool 4251 isGAPlusOffset(SDNode *N, const GlobalValue* &GA, int64_t &Offset) const; 4252 4253 /// This method will be invoked for all target nodes and for any 4254 /// target-independent nodes that the target has registered with invoke it 4255 /// for. 4256 /// 4257 /// The semantics are as follows: 4258 /// Return Value: 4259 /// SDValue.Val == 0 - No change was made 4260 /// SDValue.Val == N - N was replaced, is dead, and is already handled. 4261 /// otherwise - N should be replaced by the returned Operand. 4262 /// 4263 /// In addition, methods provided by DAGCombinerInfo may be used to perform 4264 /// more complex transformations. 4265 /// 4266 virtual SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 4267 4268 /// Return true if it is profitable to move this shift by a constant amount 4269 /// through its operand, adjusting any immediate operands as necessary to 4270 /// preserve semantics. This transformation may not be desirable if it 4271 /// disrupts a particularly auspicious target-specific tree (e.g. bitfield 4272 /// extraction in AArch64). By default, it returns true. 4273 /// 4274 /// @param N the shift node 4275 /// @param Level the current DAGCombine legalization level. isDesirableToCommuteWithShift(const SDNode * N,CombineLevel Level)4276 virtual bool isDesirableToCommuteWithShift(const SDNode *N, 4277 CombineLevel Level) const { 4278 return true; 4279 } 4280 4281 /// GlobalISel - return true if it is profitable to move this shift by a 4282 /// constant amount through its operand, adjusting any immediate operands as 4283 /// necessary to preserve semantics. This transformation may not be desirable 4284 /// if it disrupts a particularly auspicious target-specific tree (e.g. 4285 /// bitfield extraction in AArch64). By default, it returns true. 4286 /// 4287 /// @param MI the shift instruction 4288 /// @param IsAfterLegal true if running after legalization. isDesirableToCommuteWithShift(const MachineInstr & MI,bool IsAfterLegal)4289 virtual bool isDesirableToCommuteWithShift(const MachineInstr &MI, 4290 bool IsAfterLegal) const { 4291 return true; 4292 } 4293 4294 /// GlobalISel - return true if it's profitable to perform the combine: 4295 /// shl ([sza]ext x), y => zext (shl x, y) isDesirableToPullExtFromShl(const MachineInstr & MI)4296 virtual bool isDesirableToPullExtFromShl(const MachineInstr &MI) const { 4297 return true; 4298 } 4299 4300 // Return AndOrSETCCFoldKind::{AddAnd, ABS} if its desirable to try and 4301 // optimize LogicOp(SETCC0, SETCC1). An example (what is implemented as of 4302 // writing this) is: 4303 // With C as a power of 2 and C != 0 and C != INT_MIN: 4304 // AddAnd: 4305 // (icmp eq A, C) | (icmp eq A, -C) 4306 // -> (icmp eq and(add(A, C), ~(C + C)), 0) 4307 // (icmp ne A, C) & (icmp ne A, -C)w 4308 // -> (icmp ne and(add(A, C), ~(C + C)), 0) 4309 // ABS: 4310 // (icmp eq A, C) | (icmp eq A, -C) 4311 // -> (icmp eq Abs(A), C) 4312 // (icmp ne A, C) & (icmp ne A, -C)w 4313 // -> (icmp ne Abs(A), C) 4314 // 4315 // @param LogicOp the logic op 4316 // @param SETCC0 the first of the SETCC nodes 4317 // @param SETCC0 the second of the SETCC nodes isDesirableToCombineLogicOpOfSETCC(const SDNode * LogicOp,const SDNode * SETCC0,const SDNode * SETCC1)4318 virtual AndOrSETCCFoldKind isDesirableToCombineLogicOpOfSETCC( 4319 const SDNode *LogicOp, const SDNode *SETCC0, const SDNode *SETCC1) const { 4320 return AndOrSETCCFoldKind::None; 4321 } 4322 4323 /// Return true if it is profitable to combine an XOR of a logical shift 4324 /// to create a logical shift of NOT. This transformation may not be desirable 4325 /// if it disrupts a particularly auspicious target-specific tree (e.g. 4326 /// BIC on ARM/AArch64). By default, it returns true. isDesirableToCommuteXorWithShift(const SDNode * N)4327 virtual bool isDesirableToCommuteXorWithShift(const SDNode *N) const { 4328 return true; 4329 } 4330 4331 /// Return true if the target has native support for the specified value type 4332 /// and it is 'desirable' to use the type for the given node type. e.g. On x86 4333 /// i16 is legal, but undesirable since i16 instruction encodings are longer 4334 /// and some i16 instructions are slow. isTypeDesirableForOp(unsigned,EVT VT)4335 virtual bool isTypeDesirableForOp(unsigned /*Opc*/, EVT VT) const { 4336 // By default, assume all legal types are desirable. 4337 return isTypeLegal(VT); 4338 } 4339 4340 /// Return true if it is profitable for dag combiner to transform a floating 4341 /// point op of specified opcode to a equivalent op of an integer 4342 /// type. e.g. f32 load -> i32 load can be profitable on ARM. isDesirableToTransformToIntegerOp(unsigned,EVT)4343 virtual bool isDesirableToTransformToIntegerOp(unsigned /*Opc*/, 4344 EVT /*VT*/) const { 4345 return false; 4346 } 4347 4348 /// This method query the target whether it is beneficial for dag combiner to 4349 /// promote the specified node. If true, it should return the desired 4350 /// promotion type by reference. IsDesirableToPromoteOp(SDValue,EVT &)4351 virtual bool IsDesirableToPromoteOp(SDValue /*Op*/, EVT &/*PVT*/) const { 4352 return false; 4353 } 4354 4355 /// Return true if the target supports swifterror attribute. It optimizes 4356 /// loads and stores to reading and writing a specific register. supportSwiftError()4357 virtual bool supportSwiftError() const { 4358 return false; 4359 } 4360 4361 /// Return true if the target supports that a subset of CSRs for the given 4362 /// machine function is handled explicitly via copies. supportSplitCSR(MachineFunction * MF)4363 virtual bool supportSplitCSR(MachineFunction *MF) const { 4364 return false; 4365 } 4366 4367 /// Return true if the target supports kcfi operand bundles. supportKCFIBundles()4368 virtual bool supportKCFIBundles() const { return false; } 4369 4370 /// Perform necessary initialization to handle a subset of CSRs explicitly 4371 /// via copies. This function is called at the beginning of instruction 4372 /// selection. initializeSplitCSR(MachineBasicBlock * Entry)4373 virtual void initializeSplitCSR(MachineBasicBlock *Entry) const { 4374 llvm_unreachable("Not Implemented"); 4375 } 4376 4377 /// Insert explicit copies in entry and exit blocks. We copy a subset of 4378 /// CSRs to virtual registers in the entry block, and copy them back to 4379 /// physical registers in the exit blocks. This function is called at the end 4380 /// of instruction selection. insertCopiesSplitCSR(MachineBasicBlock * Entry,const SmallVectorImpl<MachineBasicBlock * > & Exits)4381 virtual void insertCopiesSplitCSR( 4382 MachineBasicBlock *Entry, 4383 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 4384 llvm_unreachable("Not Implemented"); 4385 } 4386 4387 /// Return the newly negated expression if the cost is not expensive and 4388 /// set the cost in \p Cost to indicate that if it is cheaper or neutral to 4389 /// do the negation. 4390 virtual SDValue getNegatedExpression(SDValue Op, SelectionDAG &DAG, 4391 bool LegalOps, bool OptForSize, 4392 NegatibleCost &Cost, 4393 unsigned Depth = 0) const; 4394 4395 SDValue getCheaperOrNeutralNegatedExpression( 4396 SDValue Op, SelectionDAG &DAG, bool LegalOps, bool OptForSize, 4397 const NegatibleCost CostThreshold = NegatibleCost::Neutral, 4398 unsigned Depth = 0) const { 4399 NegatibleCost Cost = NegatibleCost::Expensive; 4400 SDValue Neg = 4401 getNegatedExpression(Op, DAG, LegalOps, OptForSize, Cost, Depth); 4402 if (!Neg) 4403 return SDValue(); 4404 4405 if (Cost <= CostThreshold) 4406 return Neg; 4407 4408 // Remove the new created node to avoid the side effect to the DAG. 4409 if (Neg->use_empty()) 4410 DAG.RemoveDeadNode(Neg.getNode()); 4411 return SDValue(); 4412 } 4413 4414 /// This is the helper function to return the newly negated expression only 4415 /// when the cost is cheaper. 4416 SDValue getCheaperNegatedExpression(SDValue Op, SelectionDAG &DAG, 4417 bool LegalOps, bool OptForSize, 4418 unsigned Depth = 0) const { 4419 return getCheaperOrNeutralNegatedExpression(Op, DAG, LegalOps, OptForSize, 4420 NegatibleCost::Cheaper, Depth); 4421 } 4422 4423 /// This is the helper function to return the newly negated expression if 4424 /// the cost is not expensive. 4425 SDValue getNegatedExpression(SDValue Op, SelectionDAG &DAG, bool LegalOps, 4426 bool OptForSize, unsigned Depth = 0) const { 4427 NegatibleCost Cost = NegatibleCost::Expensive; 4428 return getNegatedExpression(Op, DAG, LegalOps, OptForSize, Cost, Depth); 4429 } 4430 4431 //===--------------------------------------------------------------------===// 4432 // Lowering methods - These methods must be implemented by targets so that 4433 // the SelectionDAGBuilder code knows how to lower these. 4434 // 4435 4436 /// Target-specific splitting of values into parts that fit a register 4437 /// storing a legal type splitValueIntoRegisterParts(SelectionDAG & DAG,const SDLoc & DL,SDValue Val,SDValue * Parts,unsigned NumParts,MVT PartVT,std::optional<CallingConv::ID> CC)4438 virtual bool splitValueIntoRegisterParts( 4439 SelectionDAG & DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, 4440 unsigned NumParts, MVT PartVT, std::optional<CallingConv::ID> CC) const { 4441 return false; 4442 } 4443 4444 /// Allows the target to handle physreg-carried dependency 4445 /// in target-specific way. Used from the ScheduleDAGSDNodes to decide whether 4446 /// to add the edge to the dependency graph. 4447 /// Def - input: Selection DAG node defininfg physical register 4448 /// User - input: Selection DAG node using physical register 4449 /// Op - input: Number of User operand 4450 /// PhysReg - inout: set to the physical register if the edge is 4451 /// necessary, unchanged otherwise 4452 /// Cost - inout: physical register copy cost. 4453 /// Returns 'true' is the edge is necessary, 'false' otherwise checkForPhysRegDependency(SDNode * Def,SDNode * User,unsigned Op,const TargetRegisterInfo * TRI,const TargetInstrInfo * TII,unsigned & PhysReg,int & Cost)4454 virtual bool checkForPhysRegDependency(SDNode *Def, SDNode *User, unsigned Op, 4455 const TargetRegisterInfo *TRI, 4456 const TargetInstrInfo *TII, 4457 unsigned &PhysReg, int &Cost) const { 4458 return false; 4459 } 4460 4461 /// Target-specific combining of register parts into its original value 4462 virtual SDValue joinRegisterPartsIntoValue(SelectionDAG & DAG,const SDLoc & DL,const SDValue * Parts,unsigned NumParts,MVT PartVT,EVT ValueVT,std::optional<CallingConv::ID> CC)4463 joinRegisterPartsIntoValue(SelectionDAG &DAG, const SDLoc &DL, 4464 const SDValue *Parts, unsigned NumParts, 4465 MVT PartVT, EVT ValueVT, 4466 std::optional<CallingConv::ID> CC) const { 4467 return SDValue(); 4468 } 4469 4470 /// This hook must be implemented to lower the incoming (formal) arguments, 4471 /// described by the Ins array, into the specified DAG. The implementation 4472 /// should fill in the InVals array with legal-type argument values, and 4473 /// return the resulting token chain value. LowerFormalArguments(SDValue,CallingConv::ID,bool,const SmallVectorImpl<ISD::InputArg> &,const SDLoc &,SelectionDAG &,SmallVectorImpl<SDValue> &)4474 virtual SDValue LowerFormalArguments( 4475 SDValue /*Chain*/, CallingConv::ID /*CallConv*/, bool /*isVarArg*/, 4476 const SmallVectorImpl<ISD::InputArg> & /*Ins*/, const SDLoc & /*dl*/, 4477 SelectionDAG & /*DAG*/, SmallVectorImpl<SDValue> & /*InVals*/) const { 4478 llvm_unreachable("Not Implemented"); 4479 } 4480 4481 /// This structure contains all information that is necessary for lowering 4482 /// calls. It is passed to TLI::LowerCallTo when the SelectionDAG builder 4483 /// needs to lower a call, and targets will see this struct in their LowerCall 4484 /// implementation. 4485 struct CallLoweringInfo { 4486 SDValue Chain; 4487 Type *RetTy = nullptr; 4488 bool RetSExt : 1; 4489 bool RetZExt : 1; 4490 bool IsVarArg : 1; 4491 bool IsInReg : 1; 4492 bool DoesNotReturn : 1; 4493 bool IsReturnValueUsed : 1; 4494 bool IsConvergent : 1; 4495 bool IsPatchPoint : 1; 4496 bool IsPreallocated : 1; 4497 bool NoMerge : 1; 4498 4499 // IsTailCall should be modified by implementations of 4500 // TargetLowering::LowerCall that perform tail call conversions. 4501 bool IsTailCall = false; 4502 4503 // Is Call lowering done post SelectionDAG type legalization. 4504 bool IsPostTypeLegalization = false; 4505 4506 unsigned NumFixedArgs = -1; 4507 CallingConv::ID CallConv = CallingConv::C; 4508 SDValue Callee; 4509 ArgListTy Args; 4510 SelectionDAG &DAG; 4511 SDLoc DL; 4512 const CallBase *CB = nullptr; 4513 SmallVector<ISD::OutputArg, 32> Outs; 4514 SmallVector<SDValue, 32> OutVals; 4515 SmallVector<ISD::InputArg, 32> Ins; 4516 SmallVector<SDValue, 4> InVals; 4517 const ConstantInt *CFIType = nullptr; 4518 SDValue ConvergenceControlToken; 4519 CallLoweringInfoCallLoweringInfo4520 CallLoweringInfo(SelectionDAG &DAG) 4521 : RetSExt(false), RetZExt(false), IsVarArg(false), IsInReg(false), 4522 DoesNotReturn(false), IsReturnValueUsed(true), IsConvergent(false), 4523 IsPatchPoint(false), IsPreallocated(false), NoMerge(false), 4524 DAG(DAG) {} 4525 setDebugLocCallLoweringInfo4526 CallLoweringInfo &setDebugLoc(const SDLoc &dl) { 4527 DL = dl; 4528 return *this; 4529 } 4530 setChainCallLoweringInfo4531 CallLoweringInfo &setChain(SDValue InChain) { 4532 Chain = InChain; 4533 return *this; 4534 } 4535 4536 // setCallee with target/module-specific attributes setLibCalleeCallLoweringInfo4537 CallLoweringInfo &setLibCallee(CallingConv::ID CC, Type *ResultType, 4538 SDValue Target, ArgListTy &&ArgsList) { 4539 RetTy = ResultType; 4540 Callee = Target; 4541 CallConv = CC; 4542 NumFixedArgs = ArgsList.size(); 4543 Args = std::move(ArgsList); 4544 4545 DAG.getTargetLoweringInfo().markLibCallAttributes( 4546 &(DAG.getMachineFunction()), CC, Args); 4547 return *this; 4548 } 4549 4550 CallLoweringInfo &setCallee(CallingConv::ID CC, Type *ResultType, 4551 SDValue Target, ArgListTy &&ArgsList, 4552 AttributeSet ResultAttrs = {}) { 4553 RetTy = ResultType; 4554 IsInReg = ResultAttrs.hasAttribute(Attribute::InReg); 4555 RetSExt = ResultAttrs.hasAttribute(Attribute::SExt); 4556 RetZExt = ResultAttrs.hasAttribute(Attribute::ZExt); 4557 NoMerge = ResultAttrs.hasAttribute(Attribute::NoMerge); 4558 4559 Callee = Target; 4560 CallConv = CC; 4561 NumFixedArgs = ArgsList.size(); 4562 Args = std::move(ArgsList); 4563 return *this; 4564 } 4565 setCalleeCallLoweringInfo4566 CallLoweringInfo &setCallee(Type *ResultType, FunctionType *FTy, 4567 SDValue Target, ArgListTy &&ArgsList, 4568 const CallBase &Call) { 4569 RetTy = ResultType; 4570 4571 IsInReg = Call.hasRetAttr(Attribute::InReg); 4572 DoesNotReturn = 4573 Call.doesNotReturn() || 4574 (!isa<InvokeInst>(Call) && isa<UnreachableInst>(Call.getNextNode())); 4575 IsVarArg = FTy->isVarArg(); 4576 IsReturnValueUsed = !Call.use_empty(); 4577 RetSExt = Call.hasRetAttr(Attribute::SExt); 4578 RetZExt = Call.hasRetAttr(Attribute::ZExt); 4579 NoMerge = Call.hasFnAttr(Attribute::NoMerge); 4580 4581 Callee = Target; 4582 4583 CallConv = Call.getCallingConv(); 4584 NumFixedArgs = FTy->getNumParams(); 4585 Args = std::move(ArgsList); 4586 4587 CB = &Call; 4588 4589 return *this; 4590 } 4591 4592 CallLoweringInfo &setInRegister(bool Value = true) { 4593 IsInReg = Value; 4594 return *this; 4595 } 4596 4597 CallLoweringInfo &setNoReturn(bool Value = true) { 4598 DoesNotReturn = Value; 4599 return *this; 4600 } 4601 4602 CallLoweringInfo &setVarArg(bool Value = true) { 4603 IsVarArg = Value; 4604 return *this; 4605 } 4606 4607 CallLoweringInfo &setTailCall(bool Value = true) { 4608 IsTailCall = Value; 4609 return *this; 4610 } 4611 4612 CallLoweringInfo &setDiscardResult(bool Value = true) { 4613 IsReturnValueUsed = !Value; 4614 return *this; 4615 } 4616 4617 CallLoweringInfo &setConvergent(bool Value = true) { 4618 IsConvergent = Value; 4619 return *this; 4620 } 4621 4622 CallLoweringInfo &setSExtResult(bool Value = true) { 4623 RetSExt = Value; 4624 return *this; 4625 } 4626 4627 CallLoweringInfo &setZExtResult(bool Value = true) { 4628 RetZExt = Value; 4629 return *this; 4630 } 4631 4632 CallLoweringInfo &setIsPatchPoint(bool Value = true) { 4633 IsPatchPoint = Value; 4634 return *this; 4635 } 4636 4637 CallLoweringInfo &setIsPreallocated(bool Value = true) { 4638 IsPreallocated = Value; 4639 return *this; 4640 } 4641 4642 CallLoweringInfo &setIsPostTypeLegalization(bool Value=true) { 4643 IsPostTypeLegalization = Value; 4644 return *this; 4645 } 4646 setCFITypeCallLoweringInfo4647 CallLoweringInfo &setCFIType(const ConstantInt *Type) { 4648 CFIType = Type; 4649 return *this; 4650 } 4651 setConvergenceControlTokenCallLoweringInfo4652 CallLoweringInfo &setConvergenceControlToken(SDValue Token) { 4653 ConvergenceControlToken = Token; 4654 return *this; 4655 } 4656 getArgsCallLoweringInfo4657 ArgListTy &getArgs() { 4658 return Args; 4659 } 4660 }; 4661 4662 /// This structure is used to pass arguments to makeLibCall function. 4663 struct MakeLibCallOptions { 4664 // By passing type list before soften to makeLibCall, the target hook 4665 // shouldExtendTypeInLibCall can get the original type before soften. 4666 ArrayRef<EVT> OpsVTBeforeSoften; 4667 EVT RetVTBeforeSoften; 4668 bool IsSExt : 1; 4669 bool DoesNotReturn : 1; 4670 bool IsReturnValueUsed : 1; 4671 bool IsPostTypeLegalization : 1; 4672 bool IsSoften : 1; 4673 MakeLibCallOptionsMakeLibCallOptions4674 MakeLibCallOptions() 4675 : IsSExt(false), DoesNotReturn(false), IsReturnValueUsed(true), 4676 IsPostTypeLegalization(false), IsSoften(false) {} 4677 4678 MakeLibCallOptions &setSExt(bool Value = true) { 4679 IsSExt = Value; 4680 return *this; 4681 } 4682 4683 MakeLibCallOptions &setNoReturn(bool Value = true) { 4684 DoesNotReturn = Value; 4685 return *this; 4686 } 4687 4688 MakeLibCallOptions &setDiscardResult(bool Value = true) { 4689 IsReturnValueUsed = !Value; 4690 return *this; 4691 } 4692 4693 MakeLibCallOptions &setIsPostTypeLegalization(bool Value = true) { 4694 IsPostTypeLegalization = Value; 4695 return *this; 4696 } 4697 4698 MakeLibCallOptions &setTypeListBeforeSoften(ArrayRef<EVT> OpsVT, EVT RetVT, 4699 bool Value = true) { 4700 OpsVTBeforeSoften = OpsVT; 4701 RetVTBeforeSoften = RetVT; 4702 IsSoften = Value; 4703 return *this; 4704 } 4705 }; 4706 4707 /// This function lowers an abstract call to a function into an actual call. 4708 /// This returns a pair of operands. The first element is the return value 4709 /// for the function (if RetTy is not VoidTy). The second element is the 4710 /// outgoing token chain. It calls LowerCall to do the actual lowering. 4711 std::pair<SDValue, SDValue> LowerCallTo(CallLoweringInfo &CLI) const; 4712 4713 /// This hook must be implemented to lower calls into the specified 4714 /// DAG. The outgoing arguments to the call are described by the Outs array, 4715 /// and the values to be returned by the call are described by the Ins 4716 /// array. The implementation should fill in the InVals array with legal-type 4717 /// return values from the call, and return the resulting token chain value. 4718 virtual SDValue LowerCall(CallLoweringInfo &,SmallVectorImpl<SDValue> &)4719 LowerCall(CallLoweringInfo &/*CLI*/, 4720 SmallVectorImpl<SDValue> &/*InVals*/) const { 4721 llvm_unreachable("Not Implemented"); 4722 } 4723 4724 /// Target-specific cleanup for formal ByVal parameters. HandleByVal(CCState *,unsigned &,Align)4725 virtual void HandleByVal(CCState *, unsigned &, Align) const {} 4726 4727 /// This hook should be implemented to check whether the return values 4728 /// described by the Outs array can fit into the return registers. If false 4729 /// is returned, an sret-demotion is performed. CanLowerReturn(CallingConv::ID,MachineFunction &,bool,const SmallVectorImpl<ISD::OutputArg> &,LLVMContext &)4730 virtual bool CanLowerReturn(CallingConv::ID /*CallConv*/, 4731 MachineFunction &/*MF*/, bool /*isVarArg*/, 4732 const SmallVectorImpl<ISD::OutputArg> &/*Outs*/, 4733 LLVMContext &/*Context*/) const 4734 { 4735 // Return true by default to get preexisting behavior. 4736 return true; 4737 } 4738 4739 /// This hook must be implemented to lower outgoing return values, described 4740 /// by the Outs array, into the specified DAG. The implementation should 4741 /// return the resulting token chain value. LowerReturn(SDValue,CallingConv::ID,bool,const SmallVectorImpl<ISD::OutputArg> &,const SmallVectorImpl<SDValue> &,const SDLoc &,SelectionDAG &)4742 virtual SDValue LowerReturn(SDValue /*Chain*/, CallingConv::ID /*CallConv*/, 4743 bool /*isVarArg*/, 4744 const SmallVectorImpl<ISD::OutputArg> & /*Outs*/, 4745 const SmallVectorImpl<SDValue> & /*OutVals*/, 4746 const SDLoc & /*dl*/, 4747 SelectionDAG & /*DAG*/) const { 4748 llvm_unreachable("Not Implemented"); 4749 } 4750 4751 /// Return true if result of the specified node is used by a return node 4752 /// only. It also compute and return the input chain for the tail call. 4753 /// 4754 /// This is used to determine whether it is possible to codegen a libcall as 4755 /// tail call at legalization time. isUsedByReturnOnly(SDNode *,SDValue &)4756 virtual bool isUsedByReturnOnly(SDNode *, SDValue &/*Chain*/) const { 4757 return false; 4758 } 4759 4760 /// Return true if the target may be able emit the call instruction as a tail 4761 /// call. This is used by optimization passes to determine if it's profitable 4762 /// to duplicate return instructions to enable tailcall optimization. mayBeEmittedAsTailCall(const CallInst *)4763 virtual bool mayBeEmittedAsTailCall(const CallInst *) const { 4764 return false; 4765 } 4766 4767 /// Return the builtin name for the __builtin___clear_cache intrinsic 4768 /// Default is to invoke the clear cache library call getClearCacheBuiltinName()4769 virtual const char * getClearCacheBuiltinName() const { 4770 return "__clear_cache"; 4771 } 4772 4773 /// Return the register ID of the name passed in. Used by named register 4774 /// global variables extension. There is no target-independent behaviour 4775 /// so the default action is to bail. getRegisterByName(const char * RegName,LLT Ty,const MachineFunction & MF)4776 virtual Register getRegisterByName(const char* RegName, LLT Ty, 4777 const MachineFunction &MF) const { 4778 report_fatal_error("Named registers not implemented for this target"); 4779 } 4780 4781 /// Return the type that should be used to zero or sign extend a 4782 /// zeroext/signext integer return value. FIXME: Some C calling conventions 4783 /// require the return type to be promoted, but this is not true all the time, 4784 /// e.g. i1/i8/i16 on x86/x86_64. It is also not necessary for non-C calling 4785 /// conventions. The frontend should handle this and include all of the 4786 /// necessary information. getTypeForExtReturn(LLVMContext & Context,EVT VT,ISD::NodeType)4787 virtual EVT getTypeForExtReturn(LLVMContext &Context, EVT VT, 4788 ISD::NodeType /*ExtendKind*/) const { 4789 EVT MinVT = getRegisterType(MVT::i32); 4790 return VT.bitsLT(MinVT) ? MinVT : VT; 4791 } 4792 4793 /// For some targets, an LLVM struct type must be broken down into multiple 4794 /// simple types, but the calling convention specifies that the entire struct 4795 /// must be passed in a block of consecutive registers. 4796 virtual bool functionArgumentNeedsConsecutiveRegisters(Type * Ty,CallingConv::ID CallConv,bool isVarArg,const DataLayout & DL)4797 functionArgumentNeedsConsecutiveRegisters(Type *Ty, CallingConv::ID CallConv, 4798 bool isVarArg, 4799 const DataLayout &DL) const { 4800 return false; 4801 } 4802 4803 /// For most targets, an LLVM type must be broken down into multiple 4804 /// smaller types. Usually the halves are ordered according to the endianness 4805 /// but for some platform that would break. So this method will default to 4806 /// matching the endianness but can be overridden. 4807 virtual bool shouldSplitFunctionArgumentsAsLittleEndian(const DataLayout & DL)4808 shouldSplitFunctionArgumentsAsLittleEndian(const DataLayout &DL) const { 4809 return DL.isLittleEndian(); 4810 } 4811 4812 /// Returns a 0 terminated array of registers that can be safely used as 4813 /// scratch registers. getScratchRegisters(CallingConv::ID CC)4814 virtual const MCPhysReg *getScratchRegisters(CallingConv::ID CC) const { 4815 return nullptr; 4816 } 4817 4818 /// Returns a 0 terminated array of rounding control registers that can be 4819 /// attached into strict FP call. getRoundingControlRegisters()4820 virtual ArrayRef<MCPhysReg> getRoundingControlRegisters() const { 4821 return ArrayRef<MCPhysReg>(); 4822 } 4823 4824 /// This callback is used to prepare for a volatile or atomic load. 4825 /// It takes a chain node as input and returns the chain for the load itself. 4826 /// 4827 /// Having a callback like this is necessary for targets like SystemZ, 4828 /// which allows a CPU to reuse the result of a previous load indefinitely, 4829 /// even if a cache-coherent store is performed by another CPU. The default 4830 /// implementation does nothing. prepareVolatileOrAtomicLoad(SDValue Chain,const SDLoc & DL,SelectionDAG & DAG)4831 virtual SDValue prepareVolatileOrAtomicLoad(SDValue Chain, const SDLoc &DL, 4832 SelectionDAG &DAG) const { 4833 return Chain; 4834 } 4835 4836 /// This callback is invoked by the type legalizer to legalize nodes with an 4837 /// illegal operand type but legal result types. It replaces the 4838 /// LowerOperation callback in the type Legalizer. The reason we can not do 4839 /// away with LowerOperation entirely is that LegalizeDAG isn't yet ready to 4840 /// use this callback. 4841 /// 4842 /// TODO: Consider merging with ReplaceNodeResults. 4843 /// 4844 /// The target places new result values for the node in Results (their number 4845 /// and types must exactly match those of the original return values of 4846 /// the node), or leaves Results empty, which indicates that the node is not 4847 /// to be custom lowered after all. 4848 /// The default implementation calls LowerOperation. 4849 virtual void LowerOperationWrapper(SDNode *N, 4850 SmallVectorImpl<SDValue> &Results, 4851 SelectionDAG &DAG) const; 4852 4853 /// This callback is invoked for operations that are unsupported by the 4854 /// target, which are registered to use 'custom' lowering, and whose defined 4855 /// values are all legal. If the target has no operations that require custom 4856 /// lowering, it need not implement this. The default implementation of this 4857 /// aborts. 4858 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const; 4859 4860 /// This callback is invoked when a node result type is illegal for the 4861 /// target, and the operation was registered to use 'custom' lowering for that 4862 /// result type. The target places new result values for the node in Results 4863 /// (their number and types must exactly match those of the original return 4864 /// values of the node), or leaves Results empty, which indicates that the 4865 /// node is not to be custom lowered after all. 4866 /// 4867 /// If the target has no operations that require custom lowering, it need not 4868 /// implement this. The default implementation aborts. ReplaceNodeResults(SDNode *,SmallVectorImpl<SDValue> &,SelectionDAG &)4869 virtual void ReplaceNodeResults(SDNode * /*N*/, 4870 SmallVectorImpl<SDValue> &/*Results*/, 4871 SelectionDAG &/*DAG*/) const { 4872 llvm_unreachable("ReplaceNodeResults not implemented for this target!"); 4873 } 4874 4875 /// This method returns the name of a target specific DAG node. 4876 virtual const char *getTargetNodeName(unsigned Opcode) const; 4877 4878 /// This method returns a target specific FastISel object, or null if the 4879 /// target does not support "fast" ISel. createFastISel(FunctionLoweringInfo &,const TargetLibraryInfo *)4880 virtual FastISel *createFastISel(FunctionLoweringInfo &, 4881 const TargetLibraryInfo *) const { 4882 return nullptr; 4883 } 4884 4885 bool verifyReturnAddressArgumentIsConstant(SDValue Op, 4886 SelectionDAG &DAG) const; 4887 4888 #ifndef NDEBUG 4889 /// Check the given SDNode. Aborts if it is invalid. verifyTargetSDNode(const SDNode * N)4890 virtual void verifyTargetSDNode(const SDNode *N) const {}; 4891 #endif 4892 4893 //===--------------------------------------------------------------------===// 4894 // Inline Asm Support hooks 4895 // 4896 4897 /// This hook allows the target to expand an inline asm call to be explicit 4898 /// llvm code if it wants to. This is useful for turning simple inline asms 4899 /// into LLVM intrinsics, which gives the compiler more information about the 4900 /// behavior of the code. ExpandInlineAsm(CallInst *)4901 virtual bool ExpandInlineAsm(CallInst *) const { 4902 return false; 4903 } 4904 4905 enum ConstraintType { 4906 C_Register, // Constraint represents specific register(s). 4907 C_RegisterClass, // Constraint represents any of register(s) in class. 4908 C_Memory, // Memory constraint. 4909 C_Address, // Address constraint. 4910 C_Immediate, // Requires an immediate. 4911 C_Other, // Something else. 4912 C_Unknown // Unsupported constraint. 4913 }; 4914 4915 enum ConstraintWeight { 4916 // Generic weights. 4917 CW_Invalid = -1, // No match. 4918 CW_Okay = 0, // Acceptable. 4919 CW_Good = 1, // Good weight. 4920 CW_Better = 2, // Better weight. 4921 CW_Best = 3, // Best weight. 4922 4923 // Well-known weights. 4924 CW_SpecificReg = CW_Okay, // Specific register operands. 4925 CW_Register = CW_Good, // Register operands. 4926 CW_Memory = CW_Better, // Memory operands. 4927 CW_Constant = CW_Best, // Constant operand. 4928 CW_Default = CW_Okay // Default or don't know type. 4929 }; 4930 4931 /// This contains information for each constraint that we are lowering. 4932 struct AsmOperandInfo : public InlineAsm::ConstraintInfo { 4933 /// This contains the actual string for the code, like "m". TargetLowering 4934 /// picks the 'best' code from ConstraintInfo::Codes that most closely 4935 /// matches the operand. 4936 std::string ConstraintCode; 4937 4938 /// Information about the constraint code, e.g. Register, RegisterClass, 4939 /// Memory, Other, Unknown. 4940 TargetLowering::ConstraintType ConstraintType = TargetLowering::C_Unknown; 4941 4942 /// If this is the result output operand or a clobber, this is null, 4943 /// otherwise it is the incoming operand to the CallInst. This gets 4944 /// modified as the asm is processed. 4945 Value *CallOperandVal = nullptr; 4946 4947 /// The ValueType for the operand value. 4948 MVT ConstraintVT = MVT::Other; 4949 4950 /// Copy constructor for copying from a ConstraintInfo. AsmOperandInfoAsmOperandInfo4951 AsmOperandInfo(InlineAsm::ConstraintInfo Info) 4952 : InlineAsm::ConstraintInfo(std::move(Info)) {} 4953 4954 /// Return true of this is an input operand that is a matching constraint 4955 /// like "4". 4956 bool isMatchingInputConstraint() const; 4957 4958 /// If this is an input matching constraint, this method returns the output 4959 /// operand it matches. 4960 unsigned getMatchedOperand() const; 4961 }; 4962 4963 using AsmOperandInfoVector = std::vector<AsmOperandInfo>; 4964 4965 /// Split up the constraint string from the inline assembly value into the 4966 /// specific constraints and their prefixes, and also tie in the associated 4967 /// operand values. If this returns an empty vector, and if the constraint 4968 /// string itself isn't empty, there was an error parsing. 4969 virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, 4970 const TargetRegisterInfo *TRI, 4971 const CallBase &Call) const; 4972 4973 /// Examine constraint type and operand type and determine a weight value. 4974 /// The operand object must already have been set up with the operand type. 4975 virtual ConstraintWeight getMultipleConstraintMatchWeight( 4976 AsmOperandInfo &info, int maIndex) const; 4977 4978 /// Examine constraint string and operand type and determine a weight value. 4979 /// The operand object must already have been set up with the operand type. 4980 virtual ConstraintWeight getSingleConstraintMatchWeight( 4981 AsmOperandInfo &info, const char *constraint) const; 4982 4983 /// Determines the constraint code and constraint type to use for the specific 4984 /// AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType. 4985 /// If the actual operand being passed in is available, it can be passed in as 4986 /// Op, otherwise an empty SDValue can be passed. 4987 virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, 4988 SDValue Op, 4989 SelectionDAG *DAG = nullptr) const; 4990 4991 /// Given a constraint, return the type of constraint it is for this target. 4992 virtual ConstraintType getConstraintType(StringRef Constraint) const; 4993 4994 using ConstraintPair = std::pair<StringRef, TargetLowering::ConstraintType>; 4995 using ConstraintGroup = SmallVector<ConstraintPair>; 4996 /// Given an OpInfo with list of constraints codes as strings, return a 4997 /// sorted Vector of pairs of constraint codes and their types in priority of 4998 /// what we'd prefer to lower them as. This may contain immediates that 4999 /// cannot be lowered, but it is meant to be a machine agnostic order of 5000 /// preferences. 5001 ConstraintGroup getConstraintPreferences(AsmOperandInfo &OpInfo) const; 5002 5003 /// Given a physical register constraint (e.g. {edx}), return the register 5004 /// number and the register class for the register. 5005 /// 5006 /// Given a register class constraint, like 'r', if this corresponds directly 5007 /// to an LLVM register class, return a register of 0 and the register class 5008 /// pointer. 5009 /// 5010 /// This should only be used for C_Register constraints. On error, this 5011 /// returns a register number of 0 and a null register class pointer. 5012 virtual std::pair<unsigned, const TargetRegisterClass *> 5013 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 5014 StringRef Constraint, MVT VT) const; 5015 5016 virtual InlineAsm::ConstraintCode getInlineAsmMemConstraint(StringRef ConstraintCode)5017 getInlineAsmMemConstraint(StringRef ConstraintCode) const { 5018 if (ConstraintCode == "m") 5019 return InlineAsm::ConstraintCode::m; 5020 if (ConstraintCode == "o") 5021 return InlineAsm::ConstraintCode::o; 5022 if (ConstraintCode == "X") 5023 return InlineAsm::ConstraintCode::X; 5024 if (ConstraintCode == "p") 5025 return InlineAsm::ConstraintCode::p; 5026 return InlineAsm::ConstraintCode::Unknown; 5027 } 5028 5029 /// Try to replace an X constraint, which matches anything, with another that 5030 /// has more specific requirements based on the type of the corresponding 5031 /// operand. This returns null if there is no replacement to make. 5032 virtual const char *LowerXConstraint(EVT ConstraintVT) const; 5033 5034 /// Lower the specified operand into the Ops vector. If it is invalid, don't 5035 /// add anything to Ops. 5036 virtual void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint, 5037 std::vector<SDValue> &Ops, 5038 SelectionDAG &DAG) const; 5039 5040 // Lower custom output constraints. If invalid, return SDValue(). 5041 virtual SDValue LowerAsmOutputForConstraint(SDValue &Chain, SDValue &Glue, 5042 const SDLoc &DL, 5043 const AsmOperandInfo &OpInfo, 5044 SelectionDAG &DAG) const; 5045 5046 // Targets may override this function to collect operands from the CallInst 5047 // and for example, lower them into the SelectionDAG operands. 5048 virtual void CollectTargetIntrinsicOperands(const CallInst &I, 5049 SmallVectorImpl<SDValue> &Ops, 5050 SelectionDAG &DAG) const; 5051 5052 //===--------------------------------------------------------------------===// 5053 // Div utility functions 5054 // 5055 5056 SDValue BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization, 5057 SmallVectorImpl<SDNode *> &Created) const; 5058 SDValue BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization, 5059 SmallVectorImpl<SDNode *> &Created) const; 5060 // Build sdiv by power-of-2 with conditional move instructions 5061 SDValue buildSDIVPow2WithCMov(SDNode *N, const APInt &Divisor, 5062 SelectionDAG &DAG, 5063 SmallVectorImpl<SDNode *> &Created) const; 5064 5065 /// Targets may override this function to provide custom SDIV lowering for 5066 /// power-of-2 denominators. If the target returns an empty SDValue, LLVM 5067 /// assumes SDIV is expensive and replaces it with a series of other integer 5068 /// operations. 5069 virtual SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, 5070 SelectionDAG &DAG, 5071 SmallVectorImpl<SDNode *> &Created) const; 5072 5073 /// Targets may override this function to provide custom SREM lowering for 5074 /// power-of-2 denominators. If the target returns an empty SDValue, LLVM 5075 /// assumes SREM is expensive and replaces it with a series of other integer 5076 /// operations. 5077 virtual SDValue BuildSREMPow2(SDNode *N, const APInt &Divisor, 5078 SelectionDAG &DAG, 5079 SmallVectorImpl<SDNode *> &Created) const; 5080 5081 /// Indicate whether this target prefers to combine FDIVs with the same 5082 /// divisor. If the transform should never be done, return zero. If the 5083 /// transform should be done, return the minimum number of divisor uses 5084 /// that must exist. combineRepeatedFPDivisors()5085 virtual unsigned combineRepeatedFPDivisors() const { 5086 return 0; 5087 } 5088 5089 /// Hooks for building estimates in place of slower divisions and square 5090 /// roots. 5091 5092 /// Return either a square root or its reciprocal estimate value for the input 5093 /// operand. 5094 /// \p Enabled is a ReciprocalEstimate enum with value either 'Unspecified' or 5095 /// 'Enabled' as set by a potential default override attribute. 5096 /// If \p RefinementSteps is 'Unspecified', the number of Newton-Raphson 5097 /// refinement iterations required to generate a sufficient (though not 5098 /// necessarily IEEE-754 compliant) estimate is returned in that parameter. 5099 /// The boolean UseOneConstNR output is used to select a Newton-Raphson 5100 /// algorithm implementation that uses either one or two constants. 5101 /// The boolean Reciprocal is used to select whether the estimate is for the 5102 /// square root of the input operand or the reciprocal of its square root. 5103 /// A target may choose to implement its own refinement within this function. 5104 /// If that's true, then return '0' as the number of RefinementSteps to avoid 5105 /// any further refinement of the estimate. 5106 /// An empty SDValue return means no estimate sequence can be created. getSqrtEstimate(SDValue Operand,SelectionDAG & DAG,int Enabled,int & RefinementSteps,bool & UseOneConstNR,bool Reciprocal)5107 virtual SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, 5108 int Enabled, int &RefinementSteps, 5109 bool &UseOneConstNR, bool Reciprocal) const { 5110 return SDValue(); 5111 } 5112 5113 /// Try to convert the fminnum/fmaxnum to a compare/select sequence. This is 5114 /// required for correctness since InstCombine might have canonicalized a 5115 /// fcmp+select sequence to a FMINNUM/FMAXNUM intrinsic. If we were to fall 5116 /// through to the default expansion/soften to libcall, we might introduce a 5117 /// link-time dependency on libm into a file that originally did not have one. 5118 SDValue createSelectForFMINNUM_FMAXNUM(SDNode *Node, SelectionDAG &DAG) const; 5119 5120 /// Return a reciprocal estimate value for the input operand. 5121 /// \p Enabled is a ReciprocalEstimate enum with value either 'Unspecified' or 5122 /// 'Enabled' as set by a potential default override attribute. 5123 /// If \p RefinementSteps is 'Unspecified', the number of Newton-Raphson 5124 /// refinement iterations required to generate a sufficient (though not 5125 /// necessarily IEEE-754 compliant) estimate is returned in that parameter. 5126 /// A target may choose to implement its own refinement within this function. 5127 /// If that's true, then return '0' as the number of RefinementSteps to avoid 5128 /// any further refinement of the estimate. 5129 /// An empty SDValue return means no estimate sequence can be created. getRecipEstimate(SDValue Operand,SelectionDAG & DAG,int Enabled,int & RefinementSteps)5130 virtual SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, 5131 int Enabled, int &RefinementSteps) const { 5132 return SDValue(); 5133 } 5134 5135 /// Return a target-dependent comparison result if the input operand is 5136 /// suitable for use with a square root estimate calculation. For example, the 5137 /// comparison may check if the operand is NAN, INF, zero, normal, etc. The 5138 /// result should be used as the condition operand for a select or branch. 5139 virtual SDValue getSqrtInputTest(SDValue Operand, SelectionDAG &DAG, 5140 const DenormalMode &Mode) const; 5141 5142 /// Return a target-dependent result if the input operand is not suitable for 5143 /// use with a square root estimate calculation. getSqrtResultForDenormInput(SDValue Operand,SelectionDAG & DAG)5144 virtual SDValue getSqrtResultForDenormInput(SDValue Operand, 5145 SelectionDAG &DAG) const { 5146 return DAG.getConstantFP(0.0, SDLoc(Operand), Operand.getValueType()); 5147 } 5148 5149 //===--------------------------------------------------------------------===// 5150 // Legalization utility functions 5151 // 5152 5153 /// Expand a MUL or [US]MUL_LOHI of n-bit values into two or four nodes, 5154 /// respectively, each computing an n/2-bit part of the result. 5155 /// \param Result A vector that will be filled with the parts of the result 5156 /// in little-endian order. 5157 /// \param LL Low bits of the LHS of the MUL. You can use this parameter 5158 /// if you want to control how low bits are extracted from the LHS. 5159 /// \param LH High bits of the LHS of the MUL. See LL for meaning. 5160 /// \param RL Low bits of the RHS of the MUL. See LL for meaning 5161 /// \param RH High bits of the RHS of the MUL. See LL for meaning. 5162 /// \returns true if the node has been expanded, false if it has not 5163 bool expandMUL_LOHI(unsigned Opcode, EVT VT, const SDLoc &dl, SDValue LHS, 5164 SDValue RHS, SmallVectorImpl<SDValue> &Result, EVT HiLoVT, 5165 SelectionDAG &DAG, MulExpansionKind Kind, 5166 SDValue LL = SDValue(), SDValue LH = SDValue(), 5167 SDValue RL = SDValue(), SDValue RH = SDValue()) const; 5168 5169 /// Expand a MUL into two nodes. One that computes the high bits of 5170 /// the result and one that computes the low bits. 5171 /// \param HiLoVT The value type to use for the Lo and Hi nodes. 5172 /// \param LL Low bits of the LHS of the MUL. You can use this parameter 5173 /// if you want to control how low bits are extracted from the LHS. 5174 /// \param LH High bits of the LHS of the MUL. See LL for meaning. 5175 /// \param RL Low bits of the RHS of the MUL. See LL for meaning 5176 /// \param RH High bits of the RHS of the MUL. See LL for meaning. 5177 /// \returns true if the node has been expanded. false if it has not 5178 bool expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT, 5179 SelectionDAG &DAG, MulExpansionKind Kind, 5180 SDValue LL = SDValue(), SDValue LH = SDValue(), 5181 SDValue RL = SDValue(), SDValue RH = SDValue()) const; 5182 5183 /// Attempt to expand an n-bit div/rem/divrem by constant using a n/2-bit 5184 /// urem by constant and other arithmetic ops. The n/2-bit urem by constant 5185 /// will be expanded by DAGCombiner. This is not possible for all constant 5186 /// divisors. 5187 /// \param N Node to expand 5188 /// \param Result A vector that will be filled with the lo and high parts of 5189 /// the results. For *DIVREM, this will be the quotient parts followed 5190 /// by the remainder parts. 5191 /// \param HiLoVT The value type to use for the Lo and Hi parts. Should be 5192 /// half of VT. 5193 /// \param LL Low bits of the LHS of the operation. You can use this 5194 /// parameter if you want to control how low bits are extracted from 5195 /// the LHS. 5196 /// \param LH High bits of the LHS of the operation. See LL for meaning. 5197 /// \returns true if the node has been expanded, false if it has not. 5198 bool expandDIVREMByConstant(SDNode *N, SmallVectorImpl<SDValue> &Result, 5199 EVT HiLoVT, SelectionDAG &DAG, 5200 SDValue LL = SDValue(), 5201 SDValue LH = SDValue()) const; 5202 5203 /// Expand funnel shift. 5204 /// \param N Node to expand 5205 /// \returns The expansion if successful, SDValue() otherwise 5206 SDValue expandFunnelShift(SDNode *N, SelectionDAG &DAG) const; 5207 5208 /// Expand rotations. 5209 /// \param N Node to expand 5210 /// \param AllowVectorOps expand vector rotate, this should only be performed 5211 /// if the legalization is happening outside of LegalizeVectorOps 5212 /// \returns The expansion if successful, SDValue() otherwise 5213 SDValue expandROT(SDNode *N, bool AllowVectorOps, SelectionDAG &DAG) const; 5214 5215 /// Expand shift-by-parts. 5216 /// \param N Node to expand 5217 /// \param Lo lower-output-part after conversion 5218 /// \param Hi upper-output-part after conversion 5219 void expandShiftParts(SDNode *N, SDValue &Lo, SDValue &Hi, 5220 SelectionDAG &DAG) const; 5221 5222 /// Expand float(f32) to SINT(i64) conversion 5223 /// \param N Node to expand 5224 /// \param Result output after conversion 5225 /// \returns True, if the expansion was successful, false otherwise 5226 bool expandFP_TO_SINT(SDNode *N, SDValue &Result, SelectionDAG &DAG) const; 5227 5228 /// Expand float to UINT conversion 5229 /// \param N Node to expand 5230 /// \param Result output after conversion 5231 /// \param Chain output chain after conversion 5232 /// \returns True, if the expansion was successful, false otherwise 5233 bool expandFP_TO_UINT(SDNode *N, SDValue &Result, SDValue &Chain, 5234 SelectionDAG &DAG) const; 5235 5236 /// Expand UINT(i64) to double(f64) conversion 5237 /// \param N Node to expand 5238 /// \param Result output after conversion 5239 /// \param Chain output chain after conversion 5240 /// \returns True, if the expansion was successful, false otherwise 5241 bool expandUINT_TO_FP(SDNode *N, SDValue &Result, SDValue &Chain, 5242 SelectionDAG &DAG) const; 5243 5244 /// Expand fminnum/fmaxnum into fminnum_ieee/fmaxnum_ieee with quieted inputs. 5245 SDValue expandFMINNUM_FMAXNUM(SDNode *N, SelectionDAG &DAG) const; 5246 5247 /// Expand fminimum/fmaximum into multiple comparison with selects. 5248 SDValue expandFMINIMUM_FMAXIMUM(SDNode *N, SelectionDAG &DAG) const; 5249 5250 /// Expand FP_TO_[US]INT_SAT into FP_TO_[US]INT and selects or min/max. 5251 /// \param N Node to expand 5252 /// \returns The expansion result 5253 SDValue expandFP_TO_INT_SAT(SDNode *N, SelectionDAG &DAG) const; 5254 5255 /// Truncate Op to ResultVT. If the result is exact, leave it alone. If it is 5256 /// not exact, force the result to be odd. 5257 /// \param ResultVT The type of result. 5258 /// \param Op The value to round. 5259 /// \returns The expansion result 5260 SDValue expandRoundInexactToOdd(EVT ResultVT, SDValue Op, const SDLoc &DL, 5261 SelectionDAG &DAG) const; 5262 5263 /// Expand round(fp) to fp conversion 5264 /// \param N Node to expand 5265 /// \returns The expansion result 5266 SDValue expandFP_ROUND(SDNode *Node, SelectionDAG &DAG) const; 5267 5268 /// Expand check for floating point class. 5269 /// \param ResultVT The type of intrinsic call result. 5270 /// \param Op The tested value. 5271 /// \param Test The test to perform. 5272 /// \param Flags The optimization flags. 5273 /// \returns The expansion result or SDValue() if it fails. 5274 SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, 5275 SDNodeFlags Flags, const SDLoc &DL, 5276 SelectionDAG &DAG) const; 5277 5278 /// Expand CTPOP nodes. Expands vector/scalar CTPOP nodes, 5279 /// vector nodes can only succeed if all operations are legal/custom. 5280 /// \param N Node to expand 5281 /// \returns The expansion result or SDValue() if it fails. 5282 SDValue expandCTPOP(SDNode *N, SelectionDAG &DAG) const; 5283 5284 /// Expand VP_CTPOP nodes. 5285 /// \returns The expansion result or SDValue() if it fails. 5286 SDValue expandVPCTPOP(SDNode *N, SelectionDAG &DAG) const; 5287 5288 /// Expand CTLZ/CTLZ_ZERO_UNDEF nodes. Expands vector/scalar CTLZ nodes, 5289 /// vector nodes can only succeed if all operations are legal/custom. 5290 /// \param N Node to expand 5291 /// \returns The expansion result or SDValue() if it fails. 5292 SDValue expandCTLZ(SDNode *N, SelectionDAG &DAG) const; 5293 5294 /// Expand VP_CTLZ/VP_CTLZ_ZERO_UNDEF nodes. 5295 /// \param N Node to expand 5296 /// \returns The expansion result or SDValue() if it fails. 5297 SDValue expandVPCTLZ(SDNode *N, SelectionDAG &DAG) const; 5298 5299 /// Expand CTTZ via Table Lookup. 5300 /// \param N Node to expand 5301 /// \returns The expansion result or SDValue() if it fails. 5302 SDValue CTTZTableLookup(SDNode *N, SelectionDAG &DAG, const SDLoc &DL, EVT VT, 5303 SDValue Op, unsigned NumBitsPerElt) const; 5304 5305 /// Expand CTTZ/CTTZ_ZERO_UNDEF nodes. Expands vector/scalar CTTZ nodes, 5306 /// vector nodes can only succeed if all operations are legal/custom. 5307 /// \param N Node to expand 5308 /// \returns The expansion result or SDValue() if it fails. 5309 SDValue expandCTTZ(SDNode *N, SelectionDAG &DAG) const; 5310 5311 /// Expand VP_CTTZ/VP_CTTZ_ZERO_UNDEF nodes. 5312 /// \param N Node to expand 5313 /// \returns The expansion result or SDValue() if it fails. 5314 SDValue expandVPCTTZ(SDNode *N, SelectionDAG &DAG) const; 5315 5316 /// Expand VP_CTTZ_ELTS/VP_CTTZ_ELTS_ZERO_UNDEF nodes. 5317 /// \param N Node to expand 5318 /// \returns The expansion result or SDValue() if it fails. 5319 SDValue expandVPCTTZElements(SDNode *N, SelectionDAG &DAG) const; 5320 5321 /// Expand ABS nodes. Expands vector/scalar ABS nodes, 5322 /// vector nodes can only succeed if all operations are legal/custom. 5323 /// (ABS x) -> (XOR (ADD x, (SRA x, type_size)), (SRA x, type_size)) 5324 /// \param N Node to expand 5325 /// \param IsNegative indicate negated abs 5326 /// \returns The expansion result or SDValue() if it fails. 5327 SDValue expandABS(SDNode *N, SelectionDAG &DAG, 5328 bool IsNegative = false) const; 5329 5330 /// Expand ABDS/ABDU nodes. Expands vector/scalar ABDS/ABDU nodes. 5331 /// \param N Node to expand 5332 /// \returns The expansion result or SDValue() if it fails. 5333 SDValue expandABD(SDNode *N, SelectionDAG &DAG) const; 5334 5335 /// Expand BSWAP nodes. Expands scalar/vector BSWAP nodes with i16/i32/i64 5336 /// scalar types. Returns SDValue() if expand fails. 5337 /// \param N Node to expand 5338 /// \returns The expansion result or SDValue() if it fails. 5339 SDValue expandBSWAP(SDNode *N, SelectionDAG &DAG) const; 5340 5341 /// Expand VP_BSWAP nodes. Expands VP_BSWAP nodes with 5342 /// i16/i32/i64 scalar types. Returns SDValue() if expand fails. \param N Node 5343 /// to expand \returns The expansion result or SDValue() if it fails. 5344 SDValue expandVPBSWAP(SDNode *N, SelectionDAG &DAG) const; 5345 5346 /// Expand BITREVERSE nodes. Expands scalar/vector BITREVERSE nodes. 5347 /// Returns SDValue() if expand fails. 5348 /// \param N Node to expand 5349 /// \returns The expansion result or SDValue() if it fails. 5350 SDValue expandBITREVERSE(SDNode *N, SelectionDAG &DAG) const; 5351 5352 /// Expand VP_BITREVERSE nodes. Expands VP_BITREVERSE nodes with 5353 /// i8/i16/i32/i64 scalar types. \param N Node to expand \returns The 5354 /// expansion result or SDValue() if it fails. 5355 SDValue expandVPBITREVERSE(SDNode *N, SelectionDAG &DAG) const; 5356 5357 /// Turn load of vector type into a load of the individual elements. 5358 /// \param LD load to expand 5359 /// \returns BUILD_VECTOR and TokenFactor nodes. 5360 std::pair<SDValue, SDValue> scalarizeVectorLoad(LoadSDNode *LD, 5361 SelectionDAG &DAG) const; 5362 5363 // Turn a store of a vector type into stores of the individual elements. 5364 /// \param ST Store with a vector value type 5365 /// \returns TokenFactor of the individual store chains. 5366 SDValue scalarizeVectorStore(StoreSDNode *ST, SelectionDAG &DAG) const; 5367 5368 /// Expands an unaligned load to 2 half-size loads for an integer, and 5369 /// possibly more for vectors. 5370 std::pair<SDValue, SDValue> expandUnalignedLoad(LoadSDNode *LD, 5371 SelectionDAG &DAG) const; 5372 5373 /// Expands an unaligned store to 2 half-size stores for integer values, and 5374 /// possibly more for vectors. 5375 SDValue expandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG) const; 5376 5377 /// Increments memory address \p Addr according to the type of the value 5378 /// \p DataVT that should be stored. If the data is stored in compressed 5379 /// form, the memory address should be incremented according to the number of 5380 /// the stored elements. This number is equal to the number of '1's bits 5381 /// in the \p Mask. 5382 /// \p DataVT is a vector type. \p Mask is a vector value. 5383 /// \p DataVT and \p Mask have the same number of vector elements. 5384 SDValue IncrementMemoryAddress(SDValue Addr, SDValue Mask, const SDLoc &DL, 5385 EVT DataVT, SelectionDAG &DAG, 5386 bool IsCompressedMemory) const; 5387 5388 /// Get a pointer to vector element \p Idx located in memory for a vector of 5389 /// type \p VecVT starting at a base address of \p VecPtr. If \p Idx is out of 5390 /// bounds the returned pointer is unspecified, but will be within the vector 5391 /// bounds. 5392 SDValue getVectorElementPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, 5393 SDValue Index) const; 5394 5395 /// Get a pointer to a sub-vector of type \p SubVecVT at index \p Idx located 5396 /// in memory for a vector of type \p VecVT starting at a base address of 5397 /// \p VecPtr. If \p Idx plus the size of \p SubVecVT is out of bounds the 5398 /// returned pointer is unspecified, but the value returned will be such that 5399 /// the entire subvector would be within the vector bounds. 5400 SDValue getVectorSubVecPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, 5401 EVT SubVecVT, SDValue Index) const; 5402 5403 /// Method for building the DAG expansion of ISD::[US][MIN|MAX]. This 5404 /// method accepts integers as its arguments. 5405 SDValue expandIntMINMAX(SDNode *Node, SelectionDAG &DAG) const; 5406 5407 /// Method for building the DAG expansion of ISD::[US][ADD|SUB]SAT. This 5408 /// method accepts integers as its arguments. 5409 SDValue expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const; 5410 5411 /// Method for building the DAG expansion of ISD::[US]SHLSAT. This 5412 /// method accepts integers as its arguments. 5413 SDValue expandShlSat(SDNode *Node, SelectionDAG &DAG) const; 5414 5415 /// Method for building the DAG expansion of ISD::[U|S]MULFIX[SAT]. This 5416 /// method accepts integers as its arguments. 5417 SDValue expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const; 5418 5419 /// Method for building the DAG expansion of ISD::[US]DIVFIX[SAT]. This 5420 /// method accepts integers as its arguments. 5421 /// Note: This method may fail if the division could not be performed 5422 /// within the type. Clients must retry with a wider type if this happens. 5423 SDValue expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, 5424 SDValue LHS, SDValue RHS, 5425 unsigned Scale, SelectionDAG &DAG) const; 5426 5427 /// Method for building the DAG expansion of ISD::U(ADD|SUB)O. Expansion 5428 /// always suceeds and populates the Result and Overflow arguments. 5429 void expandUADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, 5430 SelectionDAG &DAG) const; 5431 5432 /// Method for building the DAG expansion of ISD::S(ADD|SUB)O. Expansion 5433 /// always suceeds and populates the Result and Overflow arguments. 5434 void expandSADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, 5435 SelectionDAG &DAG) const; 5436 5437 /// Method for building the DAG expansion of ISD::[US]MULO. Returns whether 5438 /// expansion was successful and populates the Result and Overflow arguments. 5439 bool expandMULO(SDNode *Node, SDValue &Result, SDValue &Overflow, 5440 SelectionDAG &DAG) const; 5441 5442 /// forceExpandWideMUL - Unconditionally expand a MUL into either a libcall or 5443 /// brute force via a wide multiplication. The expansion works by 5444 /// attempting to do a multiplication on a wider type twice the size of the 5445 /// original operands. LL and LH represent the lower and upper halves of the 5446 /// first operand. RL and RH represent the lower and upper halves of the 5447 /// second operand. The upper and lower halves of the result are stored in Lo 5448 /// and Hi. 5449 void forceExpandWideMUL(SelectionDAG &DAG, const SDLoc &dl, bool Signed, 5450 EVT WideVT, const SDValue LL, const SDValue LH, 5451 const SDValue RL, const SDValue RH, SDValue &Lo, 5452 SDValue &Hi) const; 5453 5454 /// Same as above, but creates the upper halves of each operand by 5455 /// sign/zero-extending the operands. 5456 void forceExpandWideMUL(SelectionDAG &DAG, const SDLoc &dl, bool Signed, 5457 const SDValue LHS, const SDValue RHS, SDValue &Lo, 5458 SDValue &Hi) const; 5459 5460 /// Expand a VECREDUCE_* into an explicit calculation. If Count is specified, 5461 /// only the first Count elements of the vector are used. 5462 SDValue expandVecReduce(SDNode *Node, SelectionDAG &DAG) const; 5463 5464 /// Expand a VECREDUCE_SEQ_* into an explicit ordered calculation. 5465 SDValue expandVecReduceSeq(SDNode *Node, SelectionDAG &DAG) const; 5466 5467 /// Expand an SREM or UREM using SDIV/UDIV or SDIVREM/UDIVREM, if legal. 5468 /// Returns true if the expansion was successful. 5469 bool expandREM(SDNode *Node, SDValue &Result, SelectionDAG &DAG) const; 5470 5471 /// Method for building the DAG expansion of ISD::VECTOR_SPLICE. This 5472 /// method accepts vectors as its arguments. 5473 SDValue expandVectorSplice(SDNode *Node, SelectionDAG &DAG) const; 5474 5475 /// Legalize a SETCC or VP_SETCC with given LHS and RHS and condition code CC 5476 /// on the current target. A VP_SETCC will additionally be given a Mask 5477 /// and/or EVL not equal to SDValue(). 5478 /// 5479 /// If the SETCC has been legalized using AND / OR, then the legalized node 5480 /// will be stored in LHS. RHS and CC will be set to SDValue(). NeedInvert 5481 /// will be set to false. This will also hold if the VP_SETCC has been 5482 /// legalized using VP_AND / VP_OR. 5483 /// 5484 /// If the SETCC / VP_SETCC has been legalized by using 5485 /// getSetCCSwappedOperands(), then the values of LHS and RHS will be 5486 /// swapped, CC will be set to the new condition, and NeedInvert will be set 5487 /// to false. 5488 /// 5489 /// If the SETCC / VP_SETCC has been legalized using the inverse condcode, 5490 /// then LHS and RHS will be unchanged, CC will set to the inverted condcode, 5491 /// and NeedInvert will be set to true. The caller must invert the result of 5492 /// the SETCC with SelectionDAG::getLogicalNOT() or take equivalent action to 5493 /// swap the effect of a true/false result. 5494 /// 5495 /// \returns true if the SETCC / VP_SETCC has been legalized, false if it 5496 /// hasn't. 5497 bool LegalizeSetCCCondCode(SelectionDAG &DAG, EVT VT, SDValue &LHS, 5498 SDValue &RHS, SDValue &CC, SDValue Mask, 5499 SDValue EVL, bool &NeedInvert, const SDLoc &dl, 5500 SDValue &Chain, bool IsSignaling = false) const; 5501 5502 //===--------------------------------------------------------------------===// 5503 // Instruction Emitting Hooks 5504 // 5505 5506 /// This method should be implemented by targets that mark instructions with 5507 /// the 'usesCustomInserter' flag. These instructions are special in various 5508 /// ways, which require special support to insert. The specified MachineInstr 5509 /// is created but not inserted into any basic blocks, and this method is 5510 /// called to expand it into a sequence of instructions, potentially also 5511 /// creating new basic blocks and control flow. 5512 /// As long as the returned basic block is different (i.e., we created a new 5513 /// one), the custom inserter is free to modify the rest of \p MBB. 5514 virtual MachineBasicBlock * 5515 EmitInstrWithCustomInserter(MachineInstr &MI, MachineBasicBlock *MBB) const; 5516 5517 /// This method should be implemented by targets that mark instructions with 5518 /// the 'hasPostISelHook' flag. These instructions must be adjusted after 5519 /// instruction selection by target hooks. e.g. To fill in optional defs for 5520 /// ARM 's' setting instructions. 5521 virtual void AdjustInstrPostInstrSelection(MachineInstr &MI, 5522 SDNode *Node) const; 5523 5524 /// If this function returns true, SelectionDAGBuilder emits a 5525 /// LOAD_STACK_GUARD node when it is lowering Intrinsic::stackprotector. useLoadStackGuardNode()5526 virtual bool useLoadStackGuardNode() const { 5527 return false; 5528 } 5529 emitStackGuardXorFP(SelectionDAG & DAG,SDValue Val,const SDLoc & DL)5530 virtual SDValue emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, 5531 const SDLoc &DL) const { 5532 llvm_unreachable("not implemented for this target"); 5533 } 5534 5535 /// Lower TLS global address SDNode for target independent emulated TLS model. 5536 virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA, 5537 SelectionDAG &DAG) const; 5538 5539 /// Expands target specific indirect branch for the case of JumpTable 5540 /// expansion. 5541 virtual SDValue expandIndirectJTBranch(const SDLoc &dl, SDValue Value, 5542 SDValue Addr, int JTI, 5543 SelectionDAG &DAG) const; 5544 5545 // seteq(x, 0) -> truncate(srl(ctlz(zext(x)), log2(#bits))) 5546 // If we're comparing for equality to zero and isCtlzFast is true, expose the 5547 // fact that this can be implemented as a ctlz/srl pair, so that the dag 5548 // combiner can fold the new nodes. 5549 SDValue lowerCmpEqZeroToCtlzSrl(SDValue Op, SelectionDAG &DAG) const; 5550 5551 // Return true if `X & Y eq/ne 0` is preferable to `X & Y ne/eq Y` isXAndYEqZeroPreferableToXAndYEqY(ISD::CondCode,EVT)5552 virtual bool isXAndYEqZeroPreferableToXAndYEqY(ISD::CondCode, EVT) const { 5553 return true; 5554 } 5555 5556 private: 5557 SDValue foldSetCCWithAnd(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, 5558 const SDLoc &DL, DAGCombinerInfo &DCI) const; 5559 SDValue foldSetCCWithBinOp(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond, 5560 const SDLoc &DL, DAGCombinerInfo &DCI) const; 5561 5562 SDValue optimizeSetCCOfSignedTruncationCheck(EVT SCCVT, SDValue N0, 5563 SDValue N1, ISD::CondCode Cond, 5564 DAGCombinerInfo &DCI, 5565 const SDLoc &DL) const; 5566 5567 // (X & (C l>>/<< Y)) ==/!= 0 --> ((X <</l>> Y) & C) ==/!= 0 5568 SDValue optimizeSetCCByHoistingAndByConstFromLogicalShift( 5569 EVT SCCVT, SDValue N0, SDValue N1C, ISD::CondCode Cond, 5570 DAGCombinerInfo &DCI, const SDLoc &DL) const; 5571 5572 SDValue prepareUREMEqFold(EVT SETCCVT, SDValue REMNode, 5573 SDValue CompTargetNode, ISD::CondCode Cond, 5574 DAGCombinerInfo &DCI, const SDLoc &DL, 5575 SmallVectorImpl<SDNode *> &Created) const; 5576 SDValue buildUREMEqFold(EVT SETCCVT, SDValue REMNode, SDValue CompTargetNode, 5577 ISD::CondCode Cond, DAGCombinerInfo &DCI, 5578 const SDLoc &DL) const; 5579 5580 SDValue prepareSREMEqFold(EVT SETCCVT, SDValue REMNode, 5581 SDValue CompTargetNode, ISD::CondCode Cond, 5582 DAGCombinerInfo &DCI, const SDLoc &DL, 5583 SmallVectorImpl<SDNode *> &Created) const; 5584 SDValue buildSREMEqFold(EVT SETCCVT, SDValue REMNode, SDValue CompTargetNode, 5585 ISD::CondCode Cond, DAGCombinerInfo &DCI, 5586 const SDLoc &DL) const; 5587 }; 5588 5589 /// Given an LLVM IR type and return type attributes, compute the return value 5590 /// EVTs and flags, and optionally also the offsets, if the return value is 5591 /// being lowered to memory. 5592 void GetReturnInfo(CallingConv::ID CC, Type *ReturnType, AttributeList attr, 5593 SmallVectorImpl<ISD::OutputArg> &Outs, 5594 const TargetLowering &TLI, const DataLayout &DL); 5595 5596 } // end namespace llvm 5597 5598 #endif // LLVM_CODEGEN_TARGETLOWERING_H 5599