1 //===- SelectionDAGBuilder.cpp - Selection-DAG building -------------------===// 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 // This implements routines for translating from LLVM IR into SelectionDAG IR. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "SelectionDAGBuilder.h" 14 #include "SDNodeDbgValue.h" 15 #include "llvm/ADT/APFloat.h" 16 #include "llvm/ADT/APInt.h" 17 #include "llvm/ADT/BitVector.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/SmallSet.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/Analysis/AliasAnalysis.h" 24 #include "llvm/Analysis/BranchProbabilityInfo.h" 25 #include "llvm/Analysis/ConstantFolding.h" 26 #include "llvm/Analysis/Loads.h" 27 #include "llvm/Analysis/MemoryLocation.h" 28 #include "llvm/Analysis/TargetLibraryInfo.h" 29 #include "llvm/Analysis/ValueTracking.h" 30 #include "llvm/Analysis/VectorUtils.h" 31 #include "llvm/CodeGen/Analysis.h" 32 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h" 33 #include "llvm/CodeGen/CodeGenCommonISel.h" 34 #include "llvm/CodeGen/FunctionLoweringInfo.h" 35 #include "llvm/CodeGen/GCMetadata.h" 36 #include "llvm/CodeGen/ISDOpcodes.h" 37 #include "llvm/CodeGen/MachineBasicBlock.h" 38 #include "llvm/CodeGen/MachineFrameInfo.h" 39 #include "llvm/CodeGen/MachineFunction.h" 40 #include "llvm/CodeGen/MachineInstrBuilder.h" 41 #include "llvm/CodeGen/MachineInstrBundleIterator.h" 42 #include "llvm/CodeGen/MachineMemOperand.h" 43 #include "llvm/CodeGen/MachineModuleInfo.h" 44 #include "llvm/CodeGen/MachineOperand.h" 45 #include "llvm/CodeGen/MachineRegisterInfo.h" 46 #include "llvm/CodeGen/RuntimeLibcalls.h" 47 #include "llvm/CodeGen/SelectionDAG.h" 48 #include "llvm/CodeGen/SelectionDAGTargetInfo.h" 49 #include "llvm/CodeGen/StackMaps.h" 50 #include "llvm/CodeGen/SwiftErrorValueTracking.h" 51 #include "llvm/CodeGen/TargetFrameLowering.h" 52 #include "llvm/CodeGen/TargetInstrInfo.h" 53 #include "llvm/CodeGen/TargetOpcodes.h" 54 #include "llvm/CodeGen/TargetRegisterInfo.h" 55 #include "llvm/CodeGen/TargetSubtargetInfo.h" 56 #include "llvm/CodeGen/WinEHFuncInfo.h" 57 #include "llvm/IR/Argument.h" 58 #include "llvm/IR/Attributes.h" 59 #include "llvm/IR/BasicBlock.h" 60 #include "llvm/IR/CFG.h" 61 #include "llvm/IR/CallingConv.h" 62 #include "llvm/IR/Constant.h" 63 #include "llvm/IR/ConstantRange.h" 64 #include "llvm/IR/Constants.h" 65 #include "llvm/IR/DataLayout.h" 66 #include "llvm/IR/DebugInfo.h" 67 #include "llvm/IR/DebugInfoMetadata.h" 68 #include "llvm/IR/DerivedTypes.h" 69 #include "llvm/IR/DiagnosticInfo.h" 70 #include "llvm/IR/EHPersonalities.h" 71 #include "llvm/IR/Function.h" 72 #include "llvm/IR/GetElementPtrTypeIterator.h" 73 #include "llvm/IR/InlineAsm.h" 74 #include "llvm/IR/InstrTypes.h" 75 #include "llvm/IR/Instructions.h" 76 #include "llvm/IR/IntrinsicInst.h" 77 #include "llvm/IR/Intrinsics.h" 78 #include "llvm/IR/IntrinsicsAArch64.h" 79 #include "llvm/IR/IntrinsicsAMDGPU.h" 80 #include "llvm/IR/IntrinsicsWebAssembly.h" 81 #include "llvm/IR/LLVMContext.h" 82 #include "llvm/IR/Metadata.h" 83 #include "llvm/IR/Module.h" 84 #include "llvm/IR/Operator.h" 85 #include "llvm/IR/PatternMatch.h" 86 #include "llvm/IR/Statepoint.h" 87 #include "llvm/IR/Type.h" 88 #include "llvm/IR/User.h" 89 #include "llvm/IR/Value.h" 90 #include "llvm/MC/MCContext.h" 91 #include "llvm/Support/AtomicOrdering.h" 92 #include "llvm/Support/Casting.h" 93 #include "llvm/Support/CommandLine.h" 94 #include "llvm/Support/Compiler.h" 95 #include "llvm/Support/Debug.h" 96 #include "llvm/Support/MathExtras.h" 97 #include "llvm/Support/raw_ostream.h" 98 #include "llvm/Target/TargetIntrinsicInfo.h" 99 #include "llvm/Target/TargetMachine.h" 100 #include "llvm/Target/TargetOptions.h" 101 #include "llvm/TargetParser/Triple.h" 102 #include "llvm/Transforms/Utils/Local.h" 103 #include <cstddef> 104 #include <iterator> 105 #include <limits> 106 #include <optional> 107 #include <tuple> 108 109 using namespace llvm; 110 using namespace PatternMatch; 111 using namespace SwitchCG; 112 113 #define DEBUG_TYPE "isel" 114 115 /// LimitFloatPrecision - Generate low-precision inline sequences for 116 /// some float libcalls (6, 8 or 12 bits). 117 static unsigned LimitFloatPrecision; 118 119 static cl::opt<bool> 120 InsertAssertAlign("insert-assert-align", cl::init(true), 121 cl::desc("Insert the experimental `assertalign` node."), 122 cl::ReallyHidden); 123 124 static cl::opt<unsigned, true> 125 LimitFPPrecision("limit-float-precision", 126 cl::desc("Generate low-precision inline sequences " 127 "for some float libcalls"), 128 cl::location(LimitFloatPrecision), cl::Hidden, 129 cl::init(0)); 130 131 static cl::opt<unsigned> SwitchPeelThreshold( 132 "switch-peel-threshold", cl::Hidden, cl::init(66), 133 cl::desc("Set the case probability threshold for peeling the case from a " 134 "switch statement. A value greater than 100 will void this " 135 "optimization")); 136 137 // Limit the width of DAG chains. This is important in general to prevent 138 // DAG-based analysis from blowing up. For example, alias analysis and 139 // load clustering may not complete in reasonable time. It is difficult to 140 // recognize and avoid this situation within each individual analysis, and 141 // future analyses are likely to have the same behavior. Limiting DAG width is 142 // the safe approach and will be especially important with global DAGs. 143 // 144 // MaxParallelChains default is arbitrarily high to avoid affecting 145 // optimization, but could be lowered to improve compile time. Any ld-ld-st-st 146 // sequence over this should have been converted to llvm.memcpy by the 147 // frontend. It is easy to induce this behavior with .ll code such as: 148 // %buffer = alloca [4096 x i8] 149 // %data = load [4096 x i8]* %argPtr 150 // store [4096 x i8] %data, [4096 x i8]* %buffer 151 static const unsigned MaxParallelChains = 64; 152 153 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, 154 const SDValue *Parts, unsigned NumParts, 155 MVT PartVT, EVT ValueVT, const Value *V, 156 std::optional<CallingConv::ID> CC); 157 158 /// getCopyFromParts - Create a value that contains the specified legal parts 159 /// combined into the value they represent. If the parts combine to a type 160 /// larger than ValueVT then AssertOp can be used to specify whether the extra 161 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT 162 /// (ISD::AssertSext). 163 static SDValue 164 getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, 165 unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V, 166 std::optional<CallingConv::ID> CC = std::nullopt, 167 std::optional<ISD::NodeType> AssertOp = std::nullopt) { 168 // Let the target assemble the parts if it wants to 169 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 170 if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts, 171 PartVT, ValueVT, CC)) 172 return Val; 173 174 if (ValueVT.isVector()) 175 return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V, 176 CC); 177 178 assert(NumParts > 0 && "No parts to assemble!"); 179 SDValue Val = Parts[0]; 180 181 if (NumParts > 1) { 182 // Assemble the value from multiple parts. 183 if (ValueVT.isInteger()) { 184 unsigned PartBits = PartVT.getSizeInBits(); 185 unsigned ValueBits = ValueVT.getSizeInBits(); 186 187 // Assemble the power of 2 part. 188 unsigned RoundParts = llvm::bit_floor(NumParts); 189 unsigned RoundBits = PartBits * RoundParts; 190 EVT RoundVT = RoundBits == ValueBits ? 191 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits); 192 SDValue Lo, Hi; 193 194 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2); 195 196 if (RoundParts > 2) { 197 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, 198 PartVT, HalfVT, V); 199 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, 200 RoundParts / 2, PartVT, HalfVT, V); 201 } else { 202 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]); 203 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]); 204 } 205 206 if (DAG.getDataLayout().isBigEndian()) 207 std::swap(Lo, Hi); 208 209 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi); 210 211 if (RoundParts < NumParts) { 212 // Assemble the trailing non-power-of-2 part. 213 unsigned OddParts = NumParts - RoundParts; 214 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits); 215 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT, 216 OddVT, V, CC); 217 218 // Combine the round and odd parts. 219 Lo = Val; 220 if (DAG.getDataLayout().isBigEndian()) 221 std::swap(Lo, Hi); 222 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); 223 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi); 224 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi, 225 DAG.getConstant(Lo.getValueSizeInBits(), DL, 226 TLI.getShiftAmountTy( 227 TotalVT, DAG.getDataLayout()))); 228 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo); 229 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi); 230 } 231 } else if (PartVT.isFloatingPoint()) { 232 // FP split into multiple FP parts (for ppcf128) 233 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 && 234 "Unexpected split"); 235 SDValue Lo, Hi; 236 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]); 237 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]); 238 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout())) 239 std::swap(Lo, Hi); 240 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi); 241 } else { 242 // FP split into integer parts (soft fp) 243 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() && 244 !PartVT.isVector() && "Unexpected split"); 245 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); 246 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V, CC); 247 } 248 } 249 250 // There is now one part, held in Val. Correct it to match ValueVT. 251 // PartEVT is the type of the register class that holds the value. 252 // ValueVT is the type of the inline asm operation. 253 EVT PartEVT = Val.getValueType(); 254 255 if (PartEVT == ValueVT) 256 return Val; 257 258 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() && 259 ValueVT.bitsLT(PartEVT)) { 260 // For an FP value in an integer part, we need to truncate to the right 261 // width first. 262 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); 263 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val); 264 } 265 266 // Handle types that have the same size. 267 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits()) 268 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 269 270 // Handle types with different sizes. 271 if (PartEVT.isInteger() && ValueVT.isInteger()) { 272 if (ValueVT.bitsLT(PartEVT)) { 273 // For a truncate, see if we have any information to 274 // indicate whether the truncated bits will always be 275 // zero or sign-extension. 276 if (AssertOp) 277 Val = DAG.getNode(*AssertOp, DL, PartEVT, Val, 278 DAG.getValueType(ValueVT)); 279 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); 280 } 281 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); 282 } 283 284 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { 285 // FP_ROUND's are always exact here. 286 if (ValueVT.bitsLT(Val.getValueType())) 287 return DAG.getNode( 288 ISD::FP_ROUND, DL, ValueVT, Val, 289 DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()))); 290 291 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val); 292 } 293 294 // Handle MMX to a narrower integer type by bitcasting MMX to integer and 295 // then truncating. 296 if (PartEVT == MVT::x86mmx && ValueVT.isInteger() && 297 ValueVT.bitsLT(PartEVT)) { 298 Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val); 299 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); 300 } 301 302 report_fatal_error("Unknown mismatch in getCopyFromParts!"); 303 } 304 305 static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V, 306 const Twine &ErrMsg) { 307 const Instruction *I = dyn_cast_or_null<Instruction>(V); 308 if (!V) 309 return Ctx.emitError(ErrMsg); 310 311 const char *AsmError = ", possible invalid constraint for vector type"; 312 if (const CallInst *CI = dyn_cast<CallInst>(I)) 313 if (CI->isInlineAsm()) 314 return Ctx.emitError(I, ErrMsg + AsmError); 315 316 return Ctx.emitError(I, ErrMsg); 317 } 318 319 /// getCopyFromPartsVector - Create a value that contains the specified legal 320 /// parts combined into the value they represent. If the parts combine to a 321 /// type larger than ValueVT then AssertOp can be used to specify whether the 322 /// extra bits are known to be zero (ISD::AssertZext) or sign extended from 323 /// ValueVT (ISD::AssertSext). 324 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL, 325 const SDValue *Parts, unsigned NumParts, 326 MVT PartVT, EVT ValueVT, const Value *V, 327 std::optional<CallingConv::ID> CallConv) { 328 assert(ValueVT.isVector() && "Not a vector value"); 329 assert(NumParts > 0 && "No parts to assemble!"); 330 const bool IsABIRegCopy = CallConv.has_value(); 331 332 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 333 SDValue Val = Parts[0]; 334 335 // Handle a multi-element vector. 336 if (NumParts > 1) { 337 EVT IntermediateVT; 338 MVT RegisterVT; 339 unsigned NumIntermediates; 340 unsigned NumRegs; 341 342 if (IsABIRegCopy) { 343 NumRegs = TLI.getVectorTypeBreakdownForCallingConv( 344 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, 345 NumIntermediates, RegisterVT); 346 } else { 347 NumRegs = 348 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, 349 NumIntermediates, RegisterVT); 350 } 351 352 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); 353 NumParts = NumRegs; // Silence a compiler warning. 354 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); 355 assert(RegisterVT.getSizeInBits() == 356 Parts[0].getSimpleValueType().getSizeInBits() && 357 "Part type sizes don't match!"); 358 359 // Assemble the parts into intermediate operands. 360 SmallVector<SDValue, 8> Ops(NumIntermediates); 361 if (NumIntermediates == NumParts) { 362 // If the register was not expanded, truncate or copy the value, 363 // as appropriate. 364 for (unsigned i = 0; i != NumParts; ++i) 365 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, 366 PartVT, IntermediateVT, V, CallConv); 367 } else if (NumParts > 0) { 368 // If the intermediate type was expanded, build the intermediate 369 // operands from the parts. 370 assert(NumParts % NumIntermediates == 0 && 371 "Must expand into a divisible number of parts!"); 372 unsigned Factor = NumParts / NumIntermediates; 373 for (unsigned i = 0; i != NumIntermediates; ++i) 374 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, 375 PartVT, IntermediateVT, V, CallConv); 376 } 377 378 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the 379 // intermediate operands. 380 EVT BuiltVectorTy = 381 IntermediateVT.isVector() 382 ? EVT::getVectorVT( 383 *DAG.getContext(), IntermediateVT.getScalarType(), 384 IntermediateVT.getVectorElementCount() * NumParts) 385 : EVT::getVectorVT(*DAG.getContext(), 386 IntermediateVT.getScalarType(), 387 NumIntermediates); 388 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS 389 : ISD::BUILD_VECTOR, 390 DL, BuiltVectorTy, Ops); 391 } 392 393 // There is now one part, held in Val. Correct it to match ValueVT. 394 EVT PartEVT = Val.getValueType(); 395 396 if (PartEVT == ValueVT) 397 return Val; 398 399 if (PartEVT.isVector()) { 400 // Vector/Vector bitcast. 401 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) 402 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 403 404 // If the parts vector has more elements than the value vector, then we 405 // have a vector widening case (e.g. <2 x float> -> <4 x float>). 406 // Extract the elements we want. 407 if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) { 408 assert((PartEVT.getVectorElementCount().getKnownMinValue() > 409 ValueVT.getVectorElementCount().getKnownMinValue()) && 410 (PartEVT.getVectorElementCount().isScalable() == 411 ValueVT.getVectorElementCount().isScalable()) && 412 "Cannot narrow, it would be a lossy transformation"); 413 PartEVT = 414 EVT::getVectorVT(*DAG.getContext(), PartEVT.getVectorElementType(), 415 ValueVT.getVectorElementCount()); 416 Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val, 417 DAG.getVectorIdxConstant(0, DL)); 418 if (PartEVT == ValueVT) 419 return Val; 420 if (PartEVT.isInteger() && ValueVT.isFloatingPoint()) 421 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 422 423 // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>). 424 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) 425 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 426 } 427 428 // Promoted vector extract 429 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT); 430 } 431 432 // Trivial bitcast if the types are the same size and the destination 433 // vector type is legal. 434 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() && 435 TLI.isTypeLegal(ValueVT)) 436 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 437 438 if (ValueVT.getVectorNumElements() != 1) { 439 // Certain ABIs require that vectors are passed as integers. For vectors 440 // are the same size, this is an obvious bitcast. 441 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) { 442 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 443 } else if (ValueVT.bitsLT(PartEVT)) { 444 const uint64_t ValueSize = ValueVT.getFixedSizeInBits(); 445 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize); 446 // Drop the extra bits. 447 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val); 448 return DAG.getBitcast(ValueVT, Val); 449 } 450 451 diagnosePossiblyInvalidConstraint( 452 *DAG.getContext(), V, "non-trivial scalar-to-vector conversion"); 453 return DAG.getUNDEF(ValueVT); 454 } 455 456 // Handle cases such as i8 -> <1 x i1> 457 EVT ValueSVT = ValueVT.getVectorElementType(); 458 if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) { 459 unsigned ValueSize = ValueSVT.getSizeInBits(); 460 if (ValueSize == PartEVT.getSizeInBits()) { 461 Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val); 462 } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) { 463 // It's possible a scalar floating point type gets softened to integer and 464 // then promoted to a larger integer. If PartEVT is the larger integer 465 // we need to truncate it and then bitcast to the FP type. 466 assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types"); 467 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize); 468 Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val); 469 Val = DAG.getBitcast(ValueSVT, Val); 470 } else { 471 Val = ValueVT.isFloatingPoint() 472 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT) 473 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT); 474 } 475 } 476 477 return DAG.getBuildVector(ValueVT, DL, Val); 478 } 479 480 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl, 481 SDValue Val, SDValue *Parts, unsigned NumParts, 482 MVT PartVT, const Value *V, 483 std::optional<CallingConv::ID> CallConv); 484 485 /// getCopyToParts - Create a series of nodes that contain the specified value 486 /// split into legal parts. If the parts contain more bits than Val, then, for 487 /// integers, ExtendKind can be used to specify how to generate the extra bits. 488 static void 489 getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts, 490 unsigned NumParts, MVT PartVT, const Value *V, 491 std::optional<CallingConv::ID> CallConv = std::nullopt, 492 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) { 493 // Let the target split the parts if it wants to 494 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 495 if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT, 496 CallConv)) 497 return; 498 EVT ValueVT = Val.getValueType(); 499 500 // Handle the vector case separately. 501 if (ValueVT.isVector()) 502 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V, 503 CallConv); 504 505 unsigned OrigNumParts = NumParts; 506 assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) && 507 "Copying to an illegal type!"); 508 509 if (NumParts == 0) 510 return; 511 512 assert(!ValueVT.isVector() && "Vector case handled elsewhere"); 513 EVT PartEVT = PartVT; 514 if (PartEVT == ValueVT) { 515 assert(NumParts == 1 && "No-op copy with multiple parts!"); 516 Parts[0] = Val; 517 return; 518 } 519 520 unsigned PartBits = PartVT.getSizeInBits(); 521 if (NumParts * PartBits > ValueVT.getSizeInBits()) { 522 // If the parts cover more bits than the value has, promote the value. 523 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) { 524 assert(NumParts == 1 && "Do not know what to promote to!"); 525 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val); 526 } else { 527 if (ValueVT.isFloatingPoint()) { 528 // FP values need to be bitcast, then extended if they are being put 529 // into a larger container. 530 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()); 531 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val); 532 } 533 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && 534 ValueVT.isInteger() && 535 "Unknown mismatch!"); 536 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); 537 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val); 538 if (PartVT == MVT::x86mmx) 539 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); 540 } 541 } else if (PartBits == ValueVT.getSizeInBits()) { 542 // Different types of the same size. 543 assert(NumParts == 1 && PartEVT != ValueVT); 544 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); 545 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) { 546 // If the parts cover less bits than value has, truncate the value. 547 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) && 548 ValueVT.isInteger() && 549 "Unknown mismatch!"); 550 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); 551 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); 552 if (PartVT == MVT::x86mmx) 553 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); 554 } 555 556 // The value may have changed - recompute ValueVT. 557 ValueVT = Val.getValueType(); 558 assert(NumParts * PartBits == ValueVT.getSizeInBits() && 559 "Failed to tile the value with PartVT!"); 560 561 if (NumParts == 1) { 562 if (PartEVT != ValueVT) { 563 diagnosePossiblyInvalidConstraint(*DAG.getContext(), V, 564 "scalar-to-vector conversion failed"); 565 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); 566 } 567 568 Parts[0] = Val; 569 return; 570 } 571 572 // Expand the value into multiple parts. 573 if (NumParts & (NumParts - 1)) { 574 // The number of parts is not a power of 2. Split off and copy the tail. 575 assert(PartVT.isInteger() && ValueVT.isInteger() && 576 "Do not know what to expand to!"); 577 unsigned RoundParts = llvm::bit_floor(NumParts); 578 unsigned RoundBits = RoundParts * PartBits; 579 unsigned OddParts = NumParts - RoundParts; 580 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val, 581 DAG.getShiftAmountConstant(RoundBits, ValueVT, DL)); 582 583 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V, 584 CallConv); 585 586 if (DAG.getDataLayout().isBigEndian()) 587 // The odd parts were reversed by getCopyToParts - unreverse them. 588 std::reverse(Parts + RoundParts, Parts + NumParts); 589 590 NumParts = RoundParts; 591 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits); 592 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val); 593 } 594 595 // The number of parts is a power of 2. Repeatedly bisect the value using 596 // EXTRACT_ELEMENT. 597 Parts[0] = DAG.getNode(ISD::BITCAST, DL, 598 EVT::getIntegerVT(*DAG.getContext(), 599 ValueVT.getSizeInBits()), 600 Val); 601 602 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) { 603 for (unsigned i = 0; i < NumParts; i += StepSize) { 604 unsigned ThisBits = StepSize * PartBits / 2; 605 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits); 606 SDValue &Part0 = Parts[i]; 607 SDValue &Part1 = Parts[i+StepSize/2]; 608 609 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, 610 ThisVT, Part0, DAG.getIntPtrConstant(1, DL)); 611 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, 612 ThisVT, Part0, DAG.getIntPtrConstant(0, DL)); 613 614 if (ThisBits == PartBits && ThisVT != PartVT) { 615 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0); 616 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1); 617 } 618 } 619 } 620 621 if (DAG.getDataLayout().isBigEndian()) 622 std::reverse(Parts, Parts + OrigNumParts); 623 } 624 625 static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val, 626 const SDLoc &DL, EVT PartVT) { 627 if (!PartVT.isVector()) 628 return SDValue(); 629 630 EVT ValueVT = Val.getValueType(); 631 EVT PartEVT = PartVT.getVectorElementType(); 632 EVT ValueEVT = ValueVT.getVectorElementType(); 633 ElementCount PartNumElts = PartVT.getVectorElementCount(); 634 ElementCount ValueNumElts = ValueVT.getVectorElementCount(); 635 636 // We only support widening vectors with equivalent element types and 637 // fixed/scalable properties. If a target needs to widen a fixed-length type 638 // to a scalable one, it should be possible to use INSERT_SUBVECTOR below. 639 if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) || 640 PartNumElts.isScalable() != ValueNumElts.isScalable()) 641 return SDValue(); 642 643 // Have a try for bf16 because some targets share its ABI with fp16. 644 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) { 645 assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) && 646 "Cannot widen to illegal type"); 647 Val = DAG.getNode(ISD::BITCAST, DL, 648 ValueVT.changeVectorElementType(MVT::f16), Val); 649 } else if (PartEVT != ValueEVT) { 650 return SDValue(); 651 } 652 653 // Widening a scalable vector to another scalable vector is done by inserting 654 // the vector into a larger undef one. 655 if (PartNumElts.isScalable()) 656 return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT), 657 Val, DAG.getVectorIdxConstant(0, DL)); 658 659 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in 660 // undef elements. 661 SmallVector<SDValue, 16> Ops; 662 DAG.ExtractVectorElements(Val, Ops); 663 SDValue EltUndef = DAG.getUNDEF(PartEVT); 664 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef); 665 666 // FIXME: Use CONCAT for 2x -> 4x. 667 return DAG.getBuildVector(PartVT, DL, Ops); 668 } 669 670 /// getCopyToPartsVector - Create a series of nodes that contain the specified 671 /// value split into legal parts. 672 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL, 673 SDValue Val, SDValue *Parts, unsigned NumParts, 674 MVT PartVT, const Value *V, 675 std::optional<CallingConv::ID> CallConv) { 676 EVT ValueVT = Val.getValueType(); 677 assert(ValueVT.isVector() && "Not a vector"); 678 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 679 const bool IsABIRegCopy = CallConv.has_value(); 680 681 if (NumParts == 1) { 682 EVT PartEVT = PartVT; 683 if (PartEVT == ValueVT) { 684 // Nothing to do. 685 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) { 686 // Bitconvert vector->vector case. 687 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val); 688 } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) { 689 Val = Widened; 690 } else if (PartVT.isVector() && 691 PartEVT.getVectorElementType().bitsGE( 692 ValueVT.getVectorElementType()) && 693 PartEVT.getVectorElementCount() == 694 ValueVT.getVectorElementCount()) { 695 696 // Promoted vector extract 697 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT); 698 } else if (PartEVT.isVector() && 699 PartEVT.getVectorElementType() != 700 ValueVT.getVectorElementType() && 701 TLI.getTypeAction(*DAG.getContext(), ValueVT) == 702 TargetLowering::TypeWidenVector) { 703 // Combination of widening and promotion. 704 EVT WidenVT = 705 EVT::getVectorVT(*DAG.getContext(), ValueVT.getVectorElementType(), 706 PartVT.getVectorElementCount()); 707 SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT); 708 Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT); 709 } else { 710 // Don't extract an integer from a float vector. This can happen if the 711 // FP type gets softened to integer and then promoted. The promotion 712 // prevents it from being picked up by the earlier bitcast case. 713 if (ValueVT.getVectorElementCount().isScalar() && 714 (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) { 715 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val, 716 DAG.getVectorIdxConstant(0, DL)); 717 } else { 718 uint64_t ValueSize = ValueVT.getFixedSizeInBits(); 719 assert(PartVT.getFixedSizeInBits() > ValueSize && 720 "lossy conversion of vector to scalar type"); 721 EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize); 722 Val = DAG.getBitcast(IntermediateType, Val); 723 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT); 724 } 725 } 726 727 assert(Val.getValueType() == PartVT && "Unexpected vector part value type"); 728 Parts[0] = Val; 729 return; 730 } 731 732 // Handle a multi-element vector. 733 EVT IntermediateVT; 734 MVT RegisterVT; 735 unsigned NumIntermediates; 736 unsigned NumRegs; 737 if (IsABIRegCopy) { 738 NumRegs = TLI.getVectorTypeBreakdownForCallingConv( 739 *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates, 740 RegisterVT); 741 } else { 742 NumRegs = 743 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT, 744 NumIntermediates, RegisterVT); 745 } 746 747 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!"); 748 NumParts = NumRegs; // Silence a compiler warning. 749 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!"); 750 751 assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() && 752 "Mixing scalable and fixed vectors when copying in parts"); 753 754 std::optional<ElementCount> DestEltCnt; 755 756 if (IntermediateVT.isVector()) 757 DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates; 758 else 759 DestEltCnt = ElementCount::getFixed(NumIntermediates); 760 761 EVT BuiltVectorTy = EVT::getVectorVT( 762 *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt); 763 764 if (ValueVT == BuiltVectorTy) { 765 // Nothing to do. 766 } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) { 767 // Bitconvert vector->vector case. 768 Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val); 769 } else { 770 if (BuiltVectorTy.getVectorElementType().bitsGT( 771 ValueVT.getVectorElementType())) { 772 // Integer promotion. 773 ValueVT = EVT::getVectorVT(*DAG.getContext(), 774 BuiltVectorTy.getVectorElementType(), 775 ValueVT.getVectorElementCount()); 776 Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val); 777 } 778 779 if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) { 780 Val = Widened; 781 } 782 } 783 784 assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type"); 785 786 // Split the vector into intermediate operands. 787 SmallVector<SDValue, 8> Ops(NumIntermediates); 788 for (unsigned i = 0; i != NumIntermediates; ++i) { 789 if (IntermediateVT.isVector()) { 790 // This does something sensible for scalable vectors - see the 791 // definition of EXTRACT_SUBVECTOR for further details. 792 unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements(); 793 Ops[i] = 794 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val, 795 DAG.getVectorIdxConstant(i * IntermediateNumElts, DL)); 796 } else { 797 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val, 798 DAG.getVectorIdxConstant(i, DL)); 799 } 800 } 801 802 // Split the intermediate operands into legal parts. 803 if (NumParts == NumIntermediates) { 804 // If the register was not expanded, promote or copy the value, 805 // as appropriate. 806 for (unsigned i = 0; i != NumParts; ++i) 807 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv); 808 } else if (NumParts > 0) { 809 // If the intermediate type was expanded, split each the value into 810 // legal parts. 811 assert(NumIntermediates != 0 && "division by zero"); 812 assert(NumParts % NumIntermediates == 0 && 813 "Must expand into a divisible number of parts!"); 814 unsigned Factor = NumParts / NumIntermediates; 815 for (unsigned i = 0; i != NumIntermediates; ++i) 816 getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V, 817 CallConv); 818 } 819 } 820 821 RegsForValue::RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt, 822 EVT valuevt, std::optional<CallingConv::ID> CC) 823 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs), 824 RegCount(1, regs.size()), CallConv(CC) {} 825 826 RegsForValue::RegsForValue(LLVMContext &Context, const TargetLowering &TLI, 827 const DataLayout &DL, unsigned Reg, Type *Ty, 828 std::optional<CallingConv::ID> CC) { 829 ComputeValueVTs(TLI, DL, Ty, ValueVTs); 830 831 CallConv = CC; 832 833 for (EVT ValueVT : ValueVTs) { 834 unsigned NumRegs = 835 isABIMangled() 836 ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT) 837 : TLI.getNumRegisters(Context, ValueVT); 838 MVT RegisterVT = 839 isABIMangled() 840 ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT) 841 : TLI.getRegisterType(Context, ValueVT); 842 for (unsigned i = 0; i != NumRegs; ++i) 843 Regs.push_back(Reg + i); 844 RegVTs.push_back(RegisterVT); 845 RegCount.push_back(NumRegs); 846 Reg += NumRegs; 847 } 848 } 849 850 SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, 851 FunctionLoweringInfo &FuncInfo, 852 const SDLoc &dl, SDValue &Chain, 853 SDValue *Glue, const Value *V) const { 854 // A Value with type {} or [0 x %t] needs no registers. 855 if (ValueVTs.empty()) 856 return SDValue(); 857 858 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 859 860 // Assemble the legal parts into the final values. 861 SmallVector<SDValue, 4> Values(ValueVTs.size()); 862 SmallVector<SDValue, 8> Parts; 863 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { 864 // Copy the legal parts from the registers. 865 EVT ValueVT = ValueVTs[Value]; 866 unsigned NumRegs = RegCount[Value]; 867 MVT RegisterVT = isABIMangled() 868 ? TLI.getRegisterTypeForCallingConv( 869 *DAG.getContext(), *CallConv, RegVTs[Value]) 870 : RegVTs[Value]; 871 872 Parts.resize(NumRegs); 873 for (unsigned i = 0; i != NumRegs; ++i) { 874 SDValue P; 875 if (!Glue) { 876 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT); 877 } else { 878 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue); 879 *Glue = P.getValue(2); 880 } 881 882 Chain = P.getValue(1); 883 Parts[i] = P; 884 885 // If the source register was virtual and if we know something about it, 886 // add an assert node. 887 if (!Register::isVirtualRegister(Regs[Part + i]) || 888 !RegisterVT.isInteger()) 889 continue; 890 891 const FunctionLoweringInfo::LiveOutInfo *LOI = 892 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]); 893 if (!LOI) 894 continue; 895 896 unsigned RegSize = RegisterVT.getScalarSizeInBits(); 897 unsigned NumSignBits = LOI->NumSignBits; 898 unsigned NumZeroBits = LOI->Known.countMinLeadingZeros(); 899 900 if (NumZeroBits == RegSize) { 901 // The current value is a zero. 902 // Explicitly express that as it would be easier for 903 // optimizations to kick in. 904 Parts[i] = DAG.getConstant(0, dl, RegisterVT); 905 continue; 906 } 907 908 // FIXME: We capture more information than the dag can represent. For 909 // now, just use the tightest assertzext/assertsext possible. 910 bool isSExt; 911 EVT FromVT(MVT::Other); 912 if (NumZeroBits) { 913 FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits); 914 isSExt = false; 915 } else if (NumSignBits > 1) { 916 FromVT = 917 EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1); 918 isSExt = true; 919 } else { 920 continue; 921 } 922 // Add an assertion node. 923 assert(FromVT != MVT::Other); 924 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl, 925 RegisterVT, P, DAG.getValueType(FromVT)); 926 } 927 928 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs, 929 RegisterVT, ValueVT, V, CallConv); 930 Part += NumRegs; 931 Parts.clear(); 932 } 933 934 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values); 935 } 936 937 void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, 938 const SDLoc &dl, SDValue &Chain, SDValue *Glue, 939 const Value *V, 940 ISD::NodeType PreferredExtendType) const { 941 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 942 ISD::NodeType ExtendKind = PreferredExtendType; 943 944 // Get the list of the values's legal parts. 945 unsigned NumRegs = Regs.size(); 946 SmallVector<SDValue, 8> Parts(NumRegs); 947 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) { 948 unsigned NumParts = RegCount[Value]; 949 950 MVT RegisterVT = isABIMangled() 951 ? TLI.getRegisterTypeForCallingConv( 952 *DAG.getContext(), *CallConv, RegVTs[Value]) 953 : RegVTs[Value]; 954 955 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT)) 956 ExtendKind = ISD::ZERO_EXTEND; 957 958 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part], 959 NumParts, RegisterVT, V, CallConv, ExtendKind); 960 Part += NumParts; 961 } 962 963 // Copy the parts into the registers. 964 SmallVector<SDValue, 8> Chains(NumRegs); 965 for (unsigned i = 0; i != NumRegs; ++i) { 966 SDValue Part; 967 if (!Glue) { 968 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]); 969 } else { 970 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue); 971 *Glue = Part.getValue(1); 972 } 973 974 Chains[i] = Part.getValue(0); 975 } 976 977 if (NumRegs == 1 || Glue) 978 // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is 979 // flagged to it. That is the CopyToReg nodes and the user are considered 980 // a single scheduling unit. If we create a TokenFactor and return it as 981 // chain, then the TokenFactor is both a predecessor (operand) of the 982 // user as well as a successor (the TF operands are flagged to the user). 983 // c1, f1 = CopyToReg 984 // c2, f2 = CopyToReg 985 // c3 = TokenFactor c1, c2 986 // ... 987 // = op c3, ..., f2 988 Chain = Chains[NumRegs-1]; 989 else 990 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains); 991 } 992 993 void RegsForValue::AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching, 994 unsigned MatchingIdx, const SDLoc &dl, 995 SelectionDAG &DAG, 996 std::vector<SDValue> &Ops) const { 997 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 998 999 InlineAsm::Flag Flag(Code, Regs.size()); 1000 if (HasMatching) 1001 Flag.setMatchingOp(MatchingIdx); 1002 else if (!Regs.empty() && Register::isVirtualRegister(Regs.front())) { 1003 // Put the register class of the virtual registers in the flag word. That 1004 // way, later passes can recompute register class constraints for inline 1005 // assembly as well as normal instructions. 1006 // Don't do this for tied operands that can use the regclass information 1007 // from the def. 1008 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 1009 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front()); 1010 Flag.setRegClass(RC->getID()); 1011 } 1012 1013 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32); 1014 Ops.push_back(Res); 1015 1016 if (Code == InlineAsm::Kind::Clobber) { 1017 // Clobbers should always have a 1:1 mapping with registers, and may 1018 // reference registers that have illegal (e.g. vector) types. Hence, we 1019 // shouldn't try to apply any sort of splitting logic to them. 1020 assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() && 1021 "No 1:1 mapping from clobbers to regs?"); 1022 Register SP = TLI.getStackPointerRegisterToSaveRestore(); 1023 (void)SP; 1024 for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) { 1025 Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I])); 1026 assert( 1027 (Regs[I] != SP || 1028 DAG.getMachineFunction().getFrameInfo().hasOpaqueSPAdjustment()) && 1029 "If we clobbered the stack pointer, MFI should know about it."); 1030 } 1031 return; 1032 } 1033 1034 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) { 1035 MVT RegisterVT = RegVTs[Value]; 1036 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value], 1037 RegisterVT); 1038 for (unsigned i = 0; i != NumRegs; ++i) { 1039 assert(Reg < Regs.size() && "Mismatch in # registers expected"); 1040 unsigned TheReg = Regs[Reg++]; 1041 Ops.push_back(DAG.getRegister(TheReg, RegisterVT)); 1042 } 1043 } 1044 } 1045 1046 SmallVector<std::pair<unsigned, TypeSize>, 4> 1047 RegsForValue::getRegsAndSizes() const { 1048 SmallVector<std::pair<unsigned, TypeSize>, 4> OutVec; 1049 unsigned I = 0; 1050 for (auto CountAndVT : zip_first(RegCount, RegVTs)) { 1051 unsigned RegCount = std::get<0>(CountAndVT); 1052 MVT RegisterVT = std::get<1>(CountAndVT); 1053 TypeSize RegisterSize = RegisterVT.getSizeInBits(); 1054 for (unsigned E = I + RegCount; I != E; ++I) 1055 OutVec.push_back(std::make_pair(Regs[I], RegisterSize)); 1056 } 1057 return OutVec; 1058 } 1059 1060 void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis *aa, 1061 AssumptionCache *ac, 1062 const TargetLibraryInfo *li) { 1063 AA = aa; 1064 AC = ac; 1065 GFI = gfi; 1066 LibInfo = li; 1067 Context = DAG.getContext(); 1068 LPadToCallSiteMap.clear(); 1069 SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout()); 1070 AssignmentTrackingEnabled = isAssignmentTrackingEnabled( 1071 *DAG.getMachineFunction().getFunction().getParent()); 1072 } 1073 1074 void SelectionDAGBuilder::clear() { 1075 NodeMap.clear(); 1076 UnusedArgNodeMap.clear(); 1077 PendingLoads.clear(); 1078 PendingExports.clear(); 1079 PendingConstrainedFP.clear(); 1080 PendingConstrainedFPStrict.clear(); 1081 CurInst = nullptr; 1082 HasTailCall = false; 1083 SDNodeOrder = LowestSDNodeOrder; 1084 StatepointLowering.clear(); 1085 } 1086 1087 void SelectionDAGBuilder::clearDanglingDebugInfo() { 1088 DanglingDebugInfoMap.clear(); 1089 } 1090 1091 // Update DAG root to include dependencies on Pending chains. 1092 SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) { 1093 SDValue Root = DAG.getRoot(); 1094 1095 if (Pending.empty()) 1096 return Root; 1097 1098 // Add current root to PendingChains, unless we already indirectly 1099 // depend on it. 1100 if (Root.getOpcode() != ISD::EntryToken) { 1101 unsigned i = 0, e = Pending.size(); 1102 for (; i != e; ++i) { 1103 assert(Pending[i].getNode()->getNumOperands() > 1); 1104 if (Pending[i].getNode()->getOperand(0) == Root) 1105 break; // Don't add the root if we already indirectly depend on it. 1106 } 1107 1108 if (i == e) 1109 Pending.push_back(Root); 1110 } 1111 1112 if (Pending.size() == 1) 1113 Root = Pending[0]; 1114 else 1115 Root = DAG.getTokenFactor(getCurSDLoc(), Pending); 1116 1117 DAG.setRoot(Root); 1118 Pending.clear(); 1119 return Root; 1120 } 1121 1122 SDValue SelectionDAGBuilder::getMemoryRoot() { 1123 return updateRoot(PendingLoads); 1124 } 1125 1126 SDValue SelectionDAGBuilder::getRoot() { 1127 // Chain up all pending constrained intrinsics together with all 1128 // pending loads, by simply appending them to PendingLoads and 1129 // then calling getMemoryRoot(). 1130 PendingLoads.reserve(PendingLoads.size() + 1131 PendingConstrainedFP.size() + 1132 PendingConstrainedFPStrict.size()); 1133 PendingLoads.append(PendingConstrainedFP.begin(), 1134 PendingConstrainedFP.end()); 1135 PendingLoads.append(PendingConstrainedFPStrict.begin(), 1136 PendingConstrainedFPStrict.end()); 1137 PendingConstrainedFP.clear(); 1138 PendingConstrainedFPStrict.clear(); 1139 return getMemoryRoot(); 1140 } 1141 1142 SDValue SelectionDAGBuilder::getControlRoot() { 1143 // We need to emit pending fpexcept.strict constrained intrinsics, 1144 // so append them to the PendingExports list. 1145 PendingExports.append(PendingConstrainedFPStrict.begin(), 1146 PendingConstrainedFPStrict.end()); 1147 PendingConstrainedFPStrict.clear(); 1148 return updateRoot(PendingExports); 1149 } 1150 1151 void SelectionDAGBuilder::handleDebugDeclare(Value *Address, 1152 DILocalVariable *Variable, 1153 DIExpression *Expression, 1154 DebugLoc DL) { 1155 assert(Variable && "Missing variable"); 1156 1157 // Check if address has undef value. 1158 if (!Address || isa<UndefValue>(Address) || 1159 (Address->use_empty() && !isa<Argument>(Address))) { 1160 LLVM_DEBUG( 1161 dbgs() 1162 << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n"); 1163 return; 1164 } 1165 1166 bool IsParameter = Variable->isParameter() || isa<Argument>(Address); 1167 1168 SDValue &N = NodeMap[Address]; 1169 if (!N.getNode() && isa<Argument>(Address)) 1170 // Check unused arguments map. 1171 N = UnusedArgNodeMap[Address]; 1172 SDDbgValue *SDV; 1173 if (N.getNode()) { 1174 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) 1175 Address = BCI->getOperand(0); 1176 // Parameters are handled specially. 1177 auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode()); 1178 if (IsParameter && FINode) { 1179 // Byval parameter. We have a frame index at this point. 1180 SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(), 1181 /*IsIndirect*/ true, DL, SDNodeOrder); 1182 } else if (isa<Argument>(Address)) { 1183 // Address is an argument, so try to emit its dbg value using 1184 // virtual register info from the FuncInfo.ValueMap. 1185 EmitFuncArgumentDbgValue(Address, Variable, Expression, DL, 1186 FuncArgumentDbgValueKind::Declare, N); 1187 return; 1188 } else { 1189 SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(), 1190 true, DL, SDNodeOrder); 1191 } 1192 DAG.AddDbgValue(SDV, IsParameter); 1193 } else { 1194 // If Address is an argument then try to emit its dbg value using 1195 // virtual register info from the FuncInfo.ValueMap. 1196 if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL, 1197 FuncArgumentDbgValueKind::Declare, N)) { 1198 LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info" 1199 << " (could not emit func-arg dbg_value)\n"); 1200 } 1201 } 1202 return; 1203 } 1204 1205 void SelectionDAGBuilder::visitDbgInfo(const Instruction &I) { 1206 // Add SDDbgValue nodes for any var locs here. Do so before updating 1207 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}. 1208 if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) { 1209 // Add SDDbgValue nodes for any var locs here. Do so before updating 1210 // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}. 1211 for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I); 1212 It != End; ++It) { 1213 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID); 1214 dropDanglingDebugInfo(Var, It->Expr); 1215 if (It->Values.isKillLocation(It->Expr)) { 1216 handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder); 1217 continue; 1218 } 1219 SmallVector<Value *> Values(It->Values.location_ops()); 1220 if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder, 1221 It->Values.hasArgList())) { 1222 SmallVector<Value *, 4> Vals; 1223 for (Value *V : It->Values.location_ops()) 1224 Vals.push_back(V); 1225 addDanglingDebugInfo(Vals, 1226 FnVarLocs->getDILocalVariable(It->VariableID), 1227 It->Expr, Vals.size() > 1, It->DL, SDNodeOrder); 1228 } 1229 } 1230 } 1231 1232 // Is there is any debug-info attached to this instruction, in the form of 1233 // DPValue non-instruction debug-info records. 1234 for (DPValue &DPV : I.getDbgValueRange()) { 1235 DILocalVariable *Variable = DPV.getVariable(); 1236 DIExpression *Expression = DPV.getExpression(); 1237 dropDanglingDebugInfo(Variable, Expression); 1238 1239 if (DPV.getType() == DPValue::LocationType::Declare) { 1240 if (FuncInfo.PreprocessedDPVDeclares.contains(&DPV)) 1241 continue; 1242 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DPV 1243 << "\n"); 1244 handleDebugDeclare(DPV.getVariableLocationOp(0), Variable, Expression, 1245 DPV.getDebugLoc()); 1246 continue; 1247 } 1248 1249 // A DPValue with no locations is a kill location. 1250 SmallVector<Value *, 4> Values(DPV.location_ops()); 1251 if (Values.empty()) { 1252 handleKillDebugValue(Variable, Expression, DPV.getDebugLoc(), 1253 SDNodeOrder); 1254 continue; 1255 } 1256 1257 // A DPValue with an undef or absent location is also a kill location. 1258 if (llvm::any_of(Values, 1259 [](Value *V) { return !V || isa<UndefValue>(V); })) { 1260 handleKillDebugValue(Variable, Expression, DPV.getDebugLoc(), 1261 SDNodeOrder); 1262 continue; 1263 } 1264 1265 bool IsVariadic = DPV.hasArgList(); 1266 if (!handleDebugValue(Values, Variable, Expression, DPV.getDebugLoc(), 1267 SDNodeOrder, IsVariadic)) { 1268 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic, 1269 DPV.getDebugLoc(), SDNodeOrder); 1270 } 1271 } 1272 } 1273 1274 void SelectionDAGBuilder::visit(const Instruction &I) { 1275 visitDbgInfo(I); 1276 1277 // Set up outgoing PHI node register values before emitting the terminator. 1278 if (I.isTerminator()) { 1279 HandlePHINodesInSuccessorBlocks(I.getParent()); 1280 } 1281 1282 // Increase the SDNodeOrder if dealing with a non-debug instruction. 1283 if (!isa<DbgInfoIntrinsic>(I)) 1284 ++SDNodeOrder; 1285 1286 CurInst = &I; 1287 1288 // Set inserted listener only if required. 1289 bool NodeInserted = false; 1290 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener; 1291 MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections); 1292 if (PCSectionsMD) { 1293 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>( 1294 DAG, [&](SDNode *) { NodeInserted = true; }); 1295 } 1296 1297 visit(I.getOpcode(), I); 1298 1299 if (!I.isTerminator() && !HasTailCall && 1300 !isa<GCStatepointInst>(I)) // statepoints handle their exports internally 1301 CopyToExportRegsIfNeeded(&I); 1302 1303 // Handle metadata. 1304 if (PCSectionsMD) { 1305 auto It = NodeMap.find(&I); 1306 if (It != NodeMap.end()) { 1307 DAG.addPCSections(It->second.getNode(), PCSectionsMD); 1308 } else if (NodeInserted) { 1309 // This should not happen; if it does, don't let it go unnoticed so we can 1310 // fix it. Relevant visit*() function is probably missing a setValue(). 1311 errs() << "warning: loosing !pcsections metadata [" 1312 << I.getModule()->getName() << "]\n"; 1313 LLVM_DEBUG(I.dump()); 1314 assert(false); 1315 } 1316 } 1317 1318 CurInst = nullptr; 1319 } 1320 1321 void SelectionDAGBuilder::visitPHI(const PHINode &) { 1322 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!"); 1323 } 1324 1325 void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) { 1326 // Note: this doesn't use InstVisitor, because it has to work with 1327 // ConstantExpr's in addition to instructions. 1328 switch (Opcode) { 1329 default: llvm_unreachable("Unknown instruction type encountered!"); 1330 // Build the switch statement using the Instruction.def file. 1331 #define HANDLE_INST(NUM, OPCODE, CLASS) \ 1332 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break; 1333 #include "llvm/IR/Instruction.def" 1334 } 1335 } 1336 1337 static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG, 1338 DILocalVariable *Variable, 1339 DebugLoc DL, unsigned Order, 1340 SmallVectorImpl<Value *> &Values, 1341 DIExpression *Expression) { 1342 // For variadic dbg_values we will now insert an undef. 1343 // FIXME: We can potentially recover these! 1344 SmallVector<SDDbgOperand, 2> Locs; 1345 for (const Value *V : Values) { 1346 auto *Undef = UndefValue::get(V->getType()); 1347 Locs.push_back(SDDbgOperand::fromConst(Undef)); 1348 } 1349 SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {}, 1350 /*IsIndirect=*/false, DL, Order, 1351 /*IsVariadic=*/true); 1352 DAG.AddDbgValue(SDV, /*isParameter=*/false); 1353 return true; 1354 } 1355 1356 void SelectionDAGBuilder::addDanglingDebugInfo(SmallVectorImpl<Value *> &Values, 1357 DILocalVariable *Var, 1358 DIExpression *Expr, 1359 bool IsVariadic, DebugLoc DL, 1360 unsigned Order) { 1361 if (IsVariadic) { 1362 handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr); 1363 return; 1364 } 1365 // TODO: Dangling debug info will eventually either be resolved or produce 1366 // an Undef DBG_VALUE. However in the resolution case, a gap may appear 1367 // between the original dbg.value location and its resolved DBG_VALUE, 1368 // which we should ideally fill with an extra Undef DBG_VALUE. 1369 assert(Values.size() == 1); 1370 DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order); 1371 } 1372 1373 void SelectionDAGBuilder::dropDanglingDebugInfo(const DILocalVariable *Variable, 1374 const DIExpression *Expr) { 1375 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) { 1376 DIVariable *DanglingVariable = DDI.getVariable(); 1377 DIExpression *DanglingExpr = DDI.getExpression(); 1378 if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) { 1379 LLVM_DEBUG(dbgs() << "Dropping dangling debug info for " 1380 << printDDI(nullptr, DDI) << "\n"); 1381 return true; 1382 } 1383 return false; 1384 }; 1385 1386 for (auto &DDIMI : DanglingDebugInfoMap) { 1387 DanglingDebugInfoVector &DDIV = DDIMI.second; 1388 1389 // If debug info is to be dropped, run it through final checks to see 1390 // whether it can be salvaged. 1391 for (auto &DDI : DDIV) 1392 if (isMatchingDbgValue(DDI)) 1393 salvageUnresolvedDbgValue(DDIMI.first, DDI); 1394 1395 erase_if(DDIV, isMatchingDbgValue); 1396 } 1397 } 1398 1399 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V, 1400 // generate the debug data structures now that we've seen its definition. 1401 void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V, 1402 SDValue Val) { 1403 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V); 1404 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end()) 1405 return; 1406 1407 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second; 1408 for (auto &DDI : DDIV) { 1409 DebugLoc DL = DDI.getDebugLoc(); 1410 unsigned ValSDNodeOrder = Val.getNode()->getIROrder(); 1411 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder(); 1412 DILocalVariable *Variable = DDI.getVariable(); 1413 DIExpression *Expr = DDI.getExpression(); 1414 assert(Variable->isValidLocationForIntrinsic(DL) && 1415 "Expected inlined-at fields to agree"); 1416 SDDbgValue *SDV; 1417 if (Val.getNode()) { 1418 // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a 1419 // FuncArgumentDbgValue (it would be hoisted to the function entry, and if 1420 // we couldn't resolve it directly when examining the DbgValue intrinsic 1421 // in the first place we should not be more successful here). Unless we 1422 // have some test case that prove this to be correct we should avoid 1423 // calling EmitFuncArgumentDbgValue here. 1424 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL, 1425 FuncArgumentDbgValueKind::Value, Val)) { 1426 LLVM_DEBUG(dbgs() << "Resolve dangling debug info for " 1427 << printDDI(V, DDI) << "\n"); 1428 LLVM_DEBUG(dbgs() << " By mapping to:\n "; Val.dump()); 1429 // Increase the SDNodeOrder for the DbgValue here to make sure it is 1430 // inserted after the definition of Val when emitting the instructions 1431 // after ISel. An alternative could be to teach 1432 // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly. 1433 LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs() 1434 << "changing SDNodeOrder from " << DbgSDNodeOrder << " to " 1435 << ValSDNodeOrder << "\n"); 1436 SDV = getDbgValue(Val, Variable, Expr, DL, 1437 std::max(DbgSDNodeOrder, ValSDNodeOrder)); 1438 DAG.AddDbgValue(SDV, false); 1439 } else 1440 LLVM_DEBUG(dbgs() << "Resolved dangling debug info for " 1441 << printDDI(V, DDI) 1442 << " in EmitFuncArgumentDbgValue\n"); 1443 } else { 1444 LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI) 1445 << "\n"); 1446 auto Undef = UndefValue::get(V->getType()); 1447 auto SDV = 1448 DAG.getConstantDbgValue(Variable, Expr, Undef, DL, DbgSDNodeOrder); 1449 DAG.AddDbgValue(SDV, false); 1450 } 1451 } 1452 DDIV.clear(); 1453 } 1454 1455 void SelectionDAGBuilder::salvageUnresolvedDbgValue(const Value *V, 1456 DanglingDebugInfo &DDI) { 1457 // TODO: For the variadic implementation, instead of only checking the fail 1458 // state of `handleDebugValue`, we need know specifically which values were 1459 // invalid, so that we attempt to salvage only those values when processing 1460 // a DIArgList. 1461 const Value *OrigV = V; 1462 DILocalVariable *Var = DDI.getVariable(); 1463 DIExpression *Expr = DDI.getExpression(); 1464 DebugLoc DL = DDI.getDebugLoc(); 1465 unsigned SDOrder = DDI.getSDNodeOrder(); 1466 1467 // Currently we consider only dbg.value intrinsics -- we tell the salvager 1468 // that DW_OP_stack_value is desired. 1469 bool StackValue = true; 1470 1471 // Can this Value can be encoded without any further work? 1472 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) 1473 return; 1474 1475 // Attempt to salvage back through as many instructions as possible. Bail if 1476 // a non-instruction is seen, such as a constant expression or global 1477 // variable. FIXME: Further work could recover those too. 1478 while (isa<Instruction>(V)) { 1479 const Instruction &VAsInst = *cast<const Instruction>(V); 1480 // Temporary "0", awaiting real implementation. 1481 SmallVector<uint64_t, 16> Ops; 1482 SmallVector<Value *, 4> AdditionalValues; 1483 V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst), 1484 Expr->getNumLocationOperands(), Ops, 1485 AdditionalValues); 1486 // If we cannot salvage any further, and haven't yet found a suitable debug 1487 // expression, bail out. 1488 if (!V) 1489 break; 1490 1491 // TODO: If AdditionalValues isn't empty, then the salvage can only be 1492 // represented with a DBG_VALUE_LIST, so we give up. When we have support 1493 // here for variadic dbg_values, remove that condition. 1494 if (!AdditionalValues.empty()) 1495 break; 1496 1497 // New value and expr now represent this debuginfo. 1498 Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue); 1499 1500 // Some kind of simplification occurred: check whether the operand of the 1501 // salvaged debug expression can be encoded in this DAG. 1502 if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) { 1503 LLVM_DEBUG( 1504 dbgs() << "Salvaged debug location info for:\n " << *Var << "\n" 1505 << *OrigV << "\nBy stripping back to:\n " << *V << "\n"); 1506 return; 1507 } 1508 } 1509 1510 // This was the final opportunity to salvage this debug information, and it 1511 // couldn't be done. Place an undef DBG_VALUE at this location to terminate 1512 // any earlier variable location. 1513 assert(OrigV && "V shouldn't be null"); 1514 auto *Undef = UndefValue::get(OrigV->getType()); 1515 auto *SDV = DAG.getConstantDbgValue(Var, Expr, Undef, DL, SDNodeOrder); 1516 DAG.AddDbgValue(SDV, false); 1517 LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n " 1518 << printDDI(OrigV, DDI) << "\n"); 1519 } 1520 1521 void SelectionDAGBuilder::handleKillDebugValue(DILocalVariable *Var, 1522 DIExpression *Expr, 1523 DebugLoc DbgLoc, 1524 unsigned Order) { 1525 Value *Poison = PoisonValue::get(Type::getInt1Ty(*Context)); 1526 DIExpression *NewExpr = 1527 const_cast<DIExpression *>(DIExpression::convertToUndefExpression(Expr)); 1528 handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order, 1529 /*IsVariadic*/ false); 1530 } 1531 1532 bool SelectionDAGBuilder::handleDebugValue(ArrayRef<const Value *> Values, 1533 DILocalVariable *Var, 1534 DIExpression *Expr, DebugLoc DbgLoc, 1535 unsigned Order, bool IsVariadic) { 1536 if (Values.empty()) 1537 return true; 1538 SmallVector<SDDbgOperand> LocationOps; 1539 SmallVector<SDNode *> Dependencies; 1540 for (const Value *V : Values) { 1541 // Constant value. 1542 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V) || 1543 isa<ConstantPointerNull>(V)) { 1544 LocationOps.emplace_back(SDDbgOperand::fromConst(V)); 1545 continue; 1546 } 1547 1548 // Look through IntToPtr constants. 1549 if (auto *CE = dyn_cast<ConstantExpr>(V)) 1550 if (CE->getOpcode() == Instruction::IntToPtr) { 1551 LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0))); 1552 continue; 1553 } 1554 1555 // If the Value is a frame index, we can create a FrameIndex debug value 1556 // without relying on the DAG at all. 1557 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 1558 auto SI = FuncInfo.StaticAllocaMap.find(AI); 1559 if (SI != FuncInfo.StaticAllocaMap.end()) { 1560 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second)); 1561 continue; 1562 } 1563 } 1564 1565 // Do not use getValue() in here; we don't want to generate code at 1566 // this point if it hasn't been done yet. 1567 SDValue N = NodeMap[V]; 1568 if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map. 1569 N = UnusedArgNodeMap[V]; 1570 if (N.getNode()) { 1571 // Only emit func arg dbg value for non-variadic dbg.values for now. 1572 if (!IsVariadic && 1573 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc, 1574 FuncArgumentDbgValueKind::Value, N)) 1575 return true; 1576 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) { 1577 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can 1578 // describe stack slot locations. 1579 // 1580 // Consider "int x = 0; int *px = &x;". There are two kinds of 1581 // interesting debug values here after optimization: 1582 // 1583 // dbg.value(i32* %px, !"int *px", !DIExpression()), and 1584 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref)) 1585 // 1586 // Both describe the direct values of their associated variables. 1587 Dependencies.push_back(N.getNode()); 1588 LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex())); 1589 continue; 1590 } 1591 LocationOps.emplace_back( 1592 SDDbgOperand::fromNode(N.getNode(), N.getResNo())); 1593 continue; 1594 } 1595 1596 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 1597 // Special rules apply for the first dbg.values of parameter variables in a 1598 // function. Identify them by the fact they reference Argument Values, that 1599 // they're parameters, and they are parameters of the current function. We 1600 // need to let them dangle until they get an SDNode. 1601 bool IsParamOfFunc = 1602 isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt(); 1603 if (IsParamOfFunc) 1604 return false; 1605 1606 // The value is not used in this block yet (or it would have an SDNode). 1607 // We still want the value to appear for the user if possible -- if it has 1608 // an associated VReg, we can refer to that instead. 1609 auto VMI = FuncInfo.ValueMap.find(V); 1610 if (VMI != FuncInfo.ValueMap.end()) { 1611 unsigned Reg = VMI->second; 1612 // If this is a PHI node, it may be split up into several MI PHI nodes 1613 // (in FunctionLoweringInfo::set). 1614 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, 1615 V->getType(), std::nullopt); 1616 if (RFV.occupiesMultipleRegs()) { 1617 // FIXME: We could potentially support variadic dbg_values here. 1618 if (IsVariadic) 1619 return false; 1620 unsigned Offset = 0; 1621 unsigned BitsToDescribe = 0; 1622 if (auto VarSize = Var->getSizeInBits()) 1623 BitsToDescribe = *VarSize; 1624 if (auto Fragment = Expr->getFragmentInfo()) 1625 BitsToDescribe = Fragment->SizeInBits; 1626 for (const auto &RegAndSize : RFV.getRegsAndSizes()) { 1627 // Bail out if all bits are described already. 1628 if (Offset >= BitsToDescribe) 1629 break; 1630 // TODO: handle scalable vectors. 1631 unsigned RegisterSize = RegAndSize.second; 1632 unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe) 1633 ? BitsToDescribe - Offset 1634 : RegisterSize; 1635 auto FragmentExpr = DIExpression::createFragmentExpression( 1636 Expr, Offset, FragmentSize); 1637 if (!FragmentExpr) 1638 continue; 1639 SDDbgValue *SDV = DAG.getVRegDbgValue( 1640 Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, SDNodeOrder); 1641 DAG.AddDbgValue(SDV, false); 1642 Offset += RegisterSize; 1643 } 1644 return true; 1645 } 1646 // We can use simple vreg locations for variadic dbg_values as well. 1647 LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg)); 1648 continue; 1649 } 1650 // We failed to create a SDDbgOperand for V. 1651 return false; 1652 } 1653 1654 // We have created a SDDbgOperand for each Value in Values. 1655 // Should use Order instead of SDNodeOrder? 1656 assert(!LocationOps.empty()); 1657 SDDbgValue *SDV = DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies, 1658 /*IsIndirect=*/false, DbgLoc, 1659 SDNodeOrder, IsVariadic); 1660 DAG.AddDbgValue(SDV, /*isParameter=*/false); 1661 return true; 1662 } 1663 1664 void SelectionDAGBuilder::resolveOrClearDbgInfo() { 1665 // Try to fixup any remaining dangling debug info -- and drop it if we can't. 1666 for (auto &Pair : DanglingDebugInfoMap) 1667 for (auto &DDI : Pair.second) 1668 salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI); 1669 clearDanglingDebugInfo(); 1670 } 1671 1672 /// getCopyFromRegs - If there was virtual register allocated for the value V 1673 /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise. 1674 SDValue SelectionDAGBuilder::getCopyFromRegs(const Value *V, Type *Ty) { 1675 DenseMap<const Value *, Register>::iterator It = FuncInfo.ValueMap.find(V); 1676 SDValue Result; 1677 1678 if (It != FuncInfo.ValueMap.end()) { 1679 Register InReg = It->second; 1680 1681 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(), 1682 DAG.getDataLayout(), InReg, Ty, 1683 std::nullopt); // This is not an ABI copy. 1684 SDValue Chain = DAG.getEntryNode(); 1685 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, 1686 V); 1687 resolveDanglingDebugInfo(V, Result); 1688 } 1689 1690 return Result; 1691 } 1692 1693 /// getValue - Return an SDValue for the given Value. 1694 SDValue SelectionDAGBuilder::getValue(const Value *V) { 1695 // If we already have an SDValue for this value, use it. It's important 1696 // to do this first, so that we don't create a CopyFromReg if we already 1697 // have a regular SDValue. 1698 SDValue &N = NodeMap[V]; 1699 if (N.getNode()) return N; 1700 1701 // If there's a virtual register allocated and initialized for this 1702 // value, use it. 1703 if (SDValue copyFromReg = getCopyFromRegs(V, V->getType())) 1704 return copyFromReg; 1705 1706 // Otherwise create a new SDValue and remember it. 1707 SDValue Val = getValueImpl(V); 1708 NodeMap[V] = Val; 1709 resolveDanglingDebugInfo(V, Val); 1710 return Val; 1711 } 1712 1713 /// getNonRegisterValue - Return an SDValue for the given Value, but 1714 /// don't look in FuncInfo.ValueMap for a virtual register. 1715 SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) { 1716 // If we already have an SDValue for this value, use it. 1717 SDValue &N = NodeMap[V]; 1718 if (N.getNode()) { 1719 if (isIntOrFPConstant(N)) { 1720 // Remove the debug location from the node as the node is about to be used 1721 // in a location which may differ from the original debug location. This 1722 // is relevant to Constant and ConstantFP nodes because they can appear 1723 // as constant expressions inside PHI nodes. 1724 N->setDebugLoc(DebugLoc()); 1725 } 1726 return N; 1727 } 1728 1729 // Otherwise create a new SDValue and remember it. 1730 SDValue Val = getValueImpl(V); 1731 NodeMap[V] = Val; 1732 resolveDanglingDebugInfo(V, Val); 1733 return Val; 1734 } 1735 1736 /// getValueImpl - Helper function for getValue and getNonRegisterValue. 1737 /// Create an SDValue for the given value. 1738 SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { 1739 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 1740 1741 if (const Constant *C = dyn_cast<Constant>(V)) { 1742 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true); 1743 1744 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C)) 1745 return DAG.getConstant(*CI, getCurSDLoc(), VT); 1746 1747 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) 1748 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT); 1749 1750 if (isa<ConstantPointerNull>(C)) { 1751 unsigned AS = V->getType()->getPointerAddressSpace(); 1752 return DAG.getConstant(0, getCurSDLoc(), 1753 TLI.getPointerTy(DAG.getDataLayout(), AS)); 1754 } 1755 1756 if (match(C, m_VScale())) 1757 return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1)); 1758 1759 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) 1760 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT); 1761 1762 if (isa<UndefValue>(C) && !V->getType()->isAggregateType()) 1763 return DAG.getUNDEF(VT); 1764 1765 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 1766 visit(CE->getOpcode(), *CE); 1767 SDValue N1 = NodeMap[V]; 1768 assert(N1.getNode() && "visit didn't populate the NodeMap!"); 1769 return N1; 1770 } 1771 1772 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) { 1773 SmallVector<SDValue, 4> Constants; 1774 for (const Use &U : C->operands()) { 1775 SDNode *Val = getValue(U).getNode(); 1776 // If the operand is an empty aggregate, there are no values. 1777 if (!Val) continue; 1778 // Add each leaf value from the operand to the Constants list 1779 // to form a flattened list of all the values. 1780 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) 1781 Constants.push_back(SDValue(Val, i)); 1782 } 1783 1784 return DAG.getMergeValues(Constants, getCurSDLoc()); 1785 } 1786 1787 if (const ConstantDataSequential *CDS = 1788 dyn_cast<ConstantDataSequential>(C)) { 1789 SmallVector<SDValue, 4> Ops; 1790 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { 1791 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); 1792 // Add each leaf value from the operand to the Constants list 1793 // to form a flattened list of all the values. 1794 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i) 1795 Ops.push_back(SDValue(Val, i)); 1796 } 1797 1798 if (isa<ArrayType>(CDS->getType())) 1799 return DAG.getMergeValues(Ops, getCurSDLoc()); 1800 return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops); 1801 } 1802 1803 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) { 1804 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) && 1805 "Unknown struct or array constant!"); 1806 1807 SmallVector<EVT, 4> ValueVTs; 1808 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs); 1809 unsigned NumElts = ValueVTs.size(); 1810 if (NumElts == 0) 1811 return SDValue(); // empty struct 1812 SmallVector<SDValue, 4> Constants(NumElts); 1813 for (unsigned i = 0; i != NumElts; ++i) { 1814 EVT EltVT = ValueVTs[i]; 1815 if (isa<UndefValue>(C)) 1816 Constants[i] = DAG.getUNDEF(EltVT); 1817 else if (EltVT.isFloatingPoint()) 1818 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT); 1819 else 1820 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT); 1821 } 1822 1823 return DAG.getMergeValues(Constants, getCurSDLoc()); 1824 } 1825 1826 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) 1827 return DAG.getBlockAddress(BA, VT); 1828 1829 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) 1830 return getValue(Equiv->getGlobalValue()); 1831 1832 if (const auto *NC = dyn_cast<NoCFIValue>(C)) 1833 return getValue(NC->getGlobalValue()); 1834 1835 if (VT == MVT::aarch64svcount) { 1836 assert(C->isNullValue() && "Can only zero this target type!"); 1837 return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, 1838 DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1)); 1839 } 1840 1841 VectorType *VecTy = cast<VectorType>(V->getType()); 1842 1843 // Now that we know the number and type of the elements, get that number of 1844 // elements into the Ops array based on what kind of constant it is. 1845 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) { 1846 SmallVector<SDValue, 16> Ops; 1847 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements(); 1848 for (unsigned i = 0; i != NumElements; ++i) 1849 Ops.push_back(getValue(CV->getOperand(i))); 1850 1851 return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops); 1852 } 1853 1854 if (isa<ConstantAggregateZero>(C)) { 1855 EVT EltVT = 1856 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType()); 1857 1858 SDValue Op; 1859 if (EltVT.isFloatingPoint()) 1860 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT); 1861 else 1862 Op = DAG.getConstant(0, getCurSDLoc(), EltVT); 1863 1864 return NodeMap[V] = DAG.getSplat(VT, getCurSDLoc(), Op); 1865 } 1866 1867 llvm_unreachable("Unknown vector constant"); 1868 } 1869 1870 // If this is a static alloca, generate it as the frameindex instead of 1871 // computation. 1872 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 1873 DenseMap<const AllocaInst*, int>::iterator SI = 1874 FuncInfo.StaticAllocaMap.find(AI); 1875 if (SI != FuncInfo.StaticAllocaMap.end()) 1876 return DAG.getFrameIndex( 1877 SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType())); 1878 } 1879 1880 // If this is an instruction which fast-isel has deferred, select it now. 1881 if (const Instruction *Inst = dyn_cast<Instruction>(V)) { 1882 Register InReg = FuncInfo.InitializeRegForValue(Inst); 1883 1884 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg, 1885 Inst->getType(), std::nullopt); 1886 SDValue Chain = DAG.getEntryNode(); 1887 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V); 1888 } 1889 1890 if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V)) 1891 return DAG.getMDNode(cast<MDNode>(MD->getMetadata())); 1892 1893 if (const auto *BB = dyn_cast<BasicBlock>(V)) 1894 return DAG.getBasicBlock(FuncInfo.MBBMap[BB]); 1895 1896 llvm_unreachable("Can't get register for value!"); 1897 } 1898 1899 void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) { 1900 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn()); 1901 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX; 1902 bool IsCoreCLR = Pers == EHPersonality::CoreCLR; 1903 bool IsSEH = isAsynchronousEHPersonality(Pers); 1904 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB; 1905 if (!IsSEH) 1906 CatchPadMBB->setIsEHScopeEntry(); 1907 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues. 1908 if (IsMSVCCXX || IsCoreCLR) 1909 CatchPadMBB->setIsEHFuncletEntry(); 1910 } 1911 1912 void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) { 1913 // Update machine-CFG edge. 1914 MachineBasicBlock *TargetMBB = FuncInfo.MBBMap[I.getSuccessor()]; 1915 FuncInfo.MBB->addSuccessor(TargetMBB); 1916 TargetMBB->setIsEHCatchretTarget(true); 1917 DAG.getMachineFunction().setHasEHCatchret(true); 1918 1919 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn()); 1920 bool IsSEH = isAsynchronousEHPersonality(Pers); 1921 if (IsSEH) { 1922 // If this is not a fall-through branch or optimizations are switched off, 1923 // emit the branch. 1924 if (TargetMBB != NextBlock(FuncInfo.MBB) || 1925 TM.getOptLevel() == CodeGenOptLevel::None) 1926 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, 1927 getControlRoot(), DAG.getBasicBlock(TargetMBB))); 1928 return; 1929 } 1930 1931 // Figure out the funclet membership for the catchret's successor. 1932 // This will be used by the FuncletLayout pass to determine how to order the 1933 // BB's. 1934 // A 'catchret' returns to the outer scope's color. 1935 Value *ParentPad = I.getCatchSwitchParentPad(); 1936 const BasicBlock *SuccessorColor; 1937 if (isa<ConstantTokenNone>(ParentPad)) 1938 SuccessorColor = &FuncInfo.Fn->getEntryBlock(); 1939 else 1940 SuccessorColor = cast<Instruction>(ParentPad)->getParent(); 1941 assert(SuccessorColor && "No parent funclet for catchret!"); 1942 MachineBasicBlock *SuccessorColorMBB = FuncInfo.MBBMap[SuccessorColor]; 1943 assert(SuccessorColorMBB && "No MBB for SuccessorColor!"); 1944 1945 // Create the terminator node. 1946 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other, 1947 getControlRoot(), DAG.getBasicBlock(TargetMBB), 1948 DAG.getBasicBlock(SuccessorColorMBB)); 1949 DAG.setRoot(Ret); 1950 } 1951 1952 void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) { 1953 // Don't emit any special code for the cleanuppad instruction. It just marks 1954 // the start of an EH scope/funclet. 1955 FuncInfo.MBB->setIsEHScopeEntry(); 1956 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn()); 1957 if (Pers != EHPersonality::Wasm_CXX) { 1958 FuncInfo.MBB->setIsEHFuncletEntry(); 1959 FuncInfo.MBB->setIsCleanupFuncletEntry(); 1960 } 1961 } 1962 1963 // In wasm EH, even though a catchpad may not catch an exception if a tag does 1964 // not match, it is OK to add only the first unwind destination catchpad to the 1965 // successors, because there will be at least one invoke instruction within the 1966 // catch scope that points to the next unwind destination, if one exists, so 1967 // CFGSort cannot mess up with BB sorting order. 1968 // (All catchpads with 'catch (type)' clauses have a 'llvm.rethrow' intrinsic 1969 // call within them, and catchpads only consisting of 'catch (...)' have a 1970 // '__cxa_end_catch' call within them, both of which generate invokes in case 1971 // the next unwind destination exists, i.e., the next unwind destination is not 1972 // the caller.) 1973 // 1974 // Having at most one EH pad successor is also simpler and helps later 1975 // transformations. 1976 // 1977 // For example, 1978 // current: 1979 // invoke void @foo to ... unwind label %catch.dispatch 1980 // catch.dispatch: 1981 // %0 = catchswitch within ... [label %catch.start] unwind label %next 1982 // catch.start: 1983 // ... 1984 // ... in this BB or some other child BB dominated by this BB there will be an 1985 // invoke that points to 'next' BB as an unwind destination 1986 // 1987 // next: ; We don't need to add this to 'current' BB's successor 1988 // ... 1989 static void findWasmUnwindDestinations( 1990 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, 1991 BranchProbability Prob, 1992 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>> 1993 &UnwindDests) { 1994 while (EHPadBB) { 1995 const Instruction *Pad = EHPadBB->getFirstNonPHI(); 1996 if (isa<CleanupPadInst>(Pad)) { 1997 // Stop on cleanup pads. 1998 UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob); 1999 UnwindDests.back().first->setIsEHScopeEntry(); 2000 break; 2001 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) { 2002 // Add the catchpad handlers to the possible destinations. We don't 2003 // continue to the unwind destination of the catchswitch for wasm. 2004 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { 2005 UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob); 2006 UnwindDests.back().first->setIsEHScopeEntry(); 2007 } 2008 break; 2009 } else { 2010 continue; 2011 } 2012 } 2013 } 2014 2015 /// When an invoke or a cleanupret unwinds to the next EH pad, there are 2016 /// many places it could ultimately go. In the IR, we have a single unwind 2017 /// destination, but in the machine CFG, we enumerate all the possible blocks. 2018 /// This function skips over imaginary basic blocks that hold catchswitch 2019 /// instructions, and finds all the "real" machine 2020 /// basic block destinations. As those destinations may not be successors of 2021 /// EHPadBB, here we also calculate the edge probability to those destinations. 2022 /// The passed-in Prob is the edge probability to EHPadBB. 2023 static void findUnwindDestinations( 2024 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB, 2025 BranchProbability Prob, 2026 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>> 2027 &UnwindDests) { 2028 EHPersonality Personality = 2029 classifyEHPersonality(FuncInfo.Fn->getPersonalityFn()); 2030 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX; 2031 bool IsCoreCLR = Personality == EHPersonality::CoreCLR; 2032 bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX; 2033 bool IsSEH = isAsynchronousEHPersonality(Personality); 2034 2035 if (IsWasmCXX) { 2036 findWasmUnwindDestinations(FuncInfo, EHPadBB, Prob, UnwindDests); 2037 assert(UnwindDests.size() <= 1 && 2038 "There should be at most one unwind destination for wasm"); 2039 return; 2040 } 2041 2042 while (EHPadBB) { 2043 const Instruction *Pad = EHPadBB->getFirstNonPHI(); 2044 BasicBlock *NewEHPadBB = nullptr; 2045 if (isa<LandingPadInst>(Pad)) { 2046 // Stop on landingpads. They are not funclets. 2047 UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob); 2048 break; 2049 } else if (isa<CleanupPadInst>(Pad)) { 2050 // Stop on cleanup pads. Cleanups are always funclet entries for all known 2051 // personalities. 2052 UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob); 2053 UnwindDests.back().first->setIsEHScopeEntry(); 2054 UnwindDests.back().first->setIsEHFuncletEntry(); 2055 break; 2056 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) { 2057 // Add the catchpad handlers to the possible destinations. 2058 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { 2059 UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob); 2060 // For MSVC++ and the CLR, catchblocks are funclets and need prologues. 2061 if (IsMSVCCXX || IsCoreCLR) 2062 UnwindDests.back().first->setIsEHFuncletEntry(); 2063 if (!IsSEH) 2064 UnwindDests.back().first->setIsEHScopeEntry(); 2065 } 2066 NewEHPadBB = CatchSwitch->getUnwindDest(); 2067 } else { 2068 continue; 2069 } 2070 2071 BranchProbabilityInfo *BPI = FuncInfo.BPI; 2072 if (BPI && NewEHPadBB) 2073 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB); 2074 EHPadBB = NewEHPadBB; 2075 } 2076 } 2077 2078 void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) { 2079 // Update successor info. 2080 SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests; 2081 auto UnwindDest = I.getUnwindDest(); 2082 BranchProbabilityInfo *BPI = FuncInfo.BPI; 2083 BranchProbability UnwindDestProb = 2084 (BPI && UnwindDest) 2085 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest) 2086 : BranchProbability::getZero(); 2087 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests); 2088 for (auto &UnwindDest : UnwindDests) { 2089 UnwindDest.first->setIsEHPad(); 2090 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second); 2091 } 2092 FuncInfo.MBB->normalizeSuccProbs(); 2093 2094 // Create the terminator node. 2095 SDValue Ret = 2096 DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other, getControlRoot()); 2097 DAG.setRoot(Ret); 2098 } 2099 2100 void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) { 2101 report_fatal_error("visitCatchSwitch not yet implemented!"); 2102 } 2103 2104 void SelectionDAGBuilder::visitRet(const ReturnInst &I) { 2105 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2106 auto &DL = DAG.getDataLayout(); 2107 SDValue Chain = getControlRoot(); 2108 SmallVector<ISD::OutputArg, 8> Outs; 2109 SmallVector<SDValue, 8> OutVals; 2110 2111 // Calls to @llvm.experimental.deoptimize don't generate a return value, so 2112 // lower 2113 // 2114 // %val = call <ty> @llvm.experimental.deoptimize() 2115 // ret <ty> %val 2116 // 2117 // differently. 2118 if (I.getParent()->getTerminatingDeoptimizeCall()) { 2119 LowerDeoptimizingReturn(); 2120 return; 2121 } 2122 2123 if (!FuncInfo.CanLowerReturn) { 2124 unsigned DemoteReg = FuncInfo.DemoteRegister; 2125 const Function *F = I.getParent()->getParent(); 2126 2127 // Emit a store of the return value through the virtual register. 2128 // Leave Outs empty so that LowerReturn won't try to load return 2129 // registers the usual way. 2130 SmallVector<EVT, 1> PtrValueVTs; 2131 ComputeValueVTs(TLI, DL, 2132 PointerType::get(F->getContext(), 2133 DAG.getDataLayout().getAllocaAddrSpace()), 2134 PtrValueVTs); 2135 2136 SDValue RetPtr = 2137 DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVTs[0]); 2138 SDValue RetOp = getValue(I.getOperand(0)); 2139 2140 SmallVector<EVT, 4> ValueVTs, MemVTs; 2141 SmallVector<uint64_t, 4> Offsets; 2142 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs, 2143 &Offsets, 0); 2144 unsigned NumValues = ValueVTs.size(); 2145 2146 SmallVector<SDValue, 4> Chains(NumValues); 2147 Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType()); 2148 for (unsigned i = 0; i != NumValues; ++i) { 2149 // An aggregate return value cannot wrap around the address space, so 2150 // offsets to its parts don't wrap either. 2151 SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr, 2152 TypeSize::getFixed(Offsets[i])); 2153 2154 SDValue Val = RetOp.getValue(RetOp.getResNo() + i); 2155 if (MemVTs[i] != ValueVTs[i]) 2156 Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]); 2157 Chains[i] = DAG.getStore( 2158 Chain, getCurSDLoc(), Val, 2159 // FIXME: better loc info would be nice. 2160 Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), 2161 commonAlignment(BaseAlign, Offsets[i])); 2162 } 2163 2164 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), 2165 MVT::Other, Chains); 2166 } else if (I.getNumOperands() != 0) { 2167 SmallVector<EVT, 4> ValueVTs; 2168 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs); 2169 unsigned NumValues = ValueVTs.size(); 2170 if (NumValues) { 2171 SDValue RetOp = getValue(I.getOperand(0)); 2172 2173 const Function *F = I.getParent()->getParent(); 2174 2175 bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters( 2176 I.getOperand(0)->getType(), F->getCallingConv(), 2177 /*IsVarArg*/ false, DL); 2178 2179 ISD::NodeType ExtendKind = ISD::ANY_EXTEND; 2180 if (F->getAttributes().hasRetAttr(Attribute::SExt)) 2181 ExtendKind = ISD::SIGN_EXTEND; 2182 else if (F->getAttributes().hasRetAttr(Attribute::ZExt)) 2183 ExtendKind = ISD::ZERO_EXTEND; 2184 2185 LLVMContext &Context = F->getContext(); 2186 bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg); 2187 2188 for (unsigned j = 0; j != NumValues; ++j) { 2189 EVT VT = ValueVTs[j]; 2190 2191 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) 2192 VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind); 2193 2194 CallingConv::ID CC = F->getCallingConv(); 2195 2196 unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT); 2197 MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT); 2198 SmallVector<SDValue, 4> Parts(NumParts); 2199 getCopyToParts(DAG, getCurSDLoc(), 2200 SDValue(RetOp.getNode(), RetOp.getResNo() + j), 2201 &Parts[0], NumParts, PartVT, &I, CC, ExtendKind); 2202 2203 // 'inreg' on function refers to return value 2204 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); 2205 if (RetInReg) 2206 Flags.setInReg(); 2207 2208 if (I.getOperand(0)->getType()->isPointerTy()) { 2209 Flags.setPointer(); 2210 Flags.setPointerAddrSpace( 2211 cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace()); 2212 } 2213 2214 if (NeedsRegBlock) { 2215 Flags.setInConsecutiveRegs(); 2216 if (j == NumValues - 1) 2217 Flags.setInConsecutiveRegsLast(); 2218 } 2219 2220 // Propagate extension type if any 2221 if (ExtendKind == ISD::SIGN_EXTEND) 2222 Flags.setSExt(); 2223 else if (ExtendKind == ISD::ZERO_EXTEND) 2224 Flags.setZExt(); 2225 2226 for (unsigned i = 0; i < NumParts; ++i) { 2227 Outs.push_back(ISD::OutputArg(Flags, 2228 Parts[i].getValueType().getSimpleVT(), 2229 VT, /*isfixed=*/true, 0, 0)); 2230 OutVals.push_back(Parts[i]); 2231 } 2232 } 2233 } 2234 } 2235 2236 // Push in swifterror virtual register as the last element of Outs. This makes 2237 // sure swifterror virtual register will be returned in the swifterror 2238 // physical register. 2239 const Function *F = I.getParent()->getParent(); 2240 if (TLI.supportSwiftError() && 2241 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) { 2242 assert(SwiftError.getFunctionArg() && "Need a swift error argument"); 2243 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy(); 2244 Flags.setSwiftError(); 2245 Outs.push_back(ISD::OutputArg( 2246 Flags, /*vt=*/TLI.getPointerTy(DL), /*argvt=*/EVT(TLI.getPointerTy(DL)), 2247 /*isfixed=*/true, /*origidx=*/1, /*partOffs=*/0)); 2248 // Create SDNode for the swifterror virtual register. 2249 OutVals.push_back( 2250 DAG.getRegister(SwiftError.getOrCreateVRegUseAt( 2251 &I, FuncInfo.MBB, SwiftError.getFunctionArg()), 2252 EVT(TLI.getPointerTy(DL)))); 2253 } 2254 2255 bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg(); 2256 CallingConv::ID CallConv = 2257 DAG.getMachineFunction().getFunction().getCallingConv(); 2258 Chain = DAG.getTargetLoweringInfo().LowerReturn( 2259 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG); 2260 2261 // Verify that the target's LowerReturn behaved as expected. 2262 assert(Chain.getNode() && Chain.getValueType() == MVT::Other && 2263 "LowerReturn didn't return a valid chain!"); 2264 2265 // Update the DAG with the new chain value resulting from return lowering. 2266 DAG.setRoot(Chain); 2267 } 2268 2269 /// CopyToExportRegsIfNeeded - If the given value has virtual registers 2270 /// created for it, emit nodes to copy the value into the virtual 2271 /// registers. 2272 void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) { 2273 // Skip empty types 2274 if (V->getType()->isEmptyTy()) 2275 return; 2276 2277 DenseMap<const Value *, Register>::iterator VMI = FuncInfo.ValueMap.find(V); 2278 if (VMI != FuncInfo.ValueMap.end()) { 2279 assert((!V->use_empty() || isa<CallBrInst>(V)) && 2280 "Unused value assigned virtual registers!"); 2281 CopyValueToVirtualRegister(V, VMI->second); 2282 } 2283 } 2284 2285 /// ExportFromCurrentBlock - If this condition isn't known to be exported from 2286 /// the current basic block, add it to ValueMap now so that we'll get a 2287 /// CopyTo/FromReg. 2288 void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) { 2289 // No need to export constants. 2290 if (!isa<Instruction>(V) && !isa<Argument>(V)) return; 2291 2292 // Already exported? 2293 if (FuncInfo.isExportedInst(V)) return; 2294 2295 Register Reg = FuncInfo.InitializeRegForValue(V); 2296 CopyValueToVirtualRegister(V, Reg); 2297 } 2298 2299 bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V, 2300 const BasicBlock *FromBB) { 2301 // The operands of the setcc have to be in this block. We don't know 2302 // how to export them from some other block. 2303 if (const Instruction *VI = dyn_cast<Instruction>(V)) { 2304 // Can export from current BB. 2305 if (VI->getParent() == FromBB) 2306 return true; 2307 2308 // Is already exported, noop. 2309 return FuncInfo.isExportedInst(V); 2310 } 2311 2312 // If this is an argument, we can export it if the BB is the entry block or 2313 // if it is already exported. 2314 if (isa<Argument>(V)) { 2315 if (FromBB->isEntryBlock()) 2316 return true; 2317 2318 // Otherwise, can only export this if it is already exported. 2319 return FuncInfo.isExportedInst(V); 2320 } 2321 2322 // Otherwise, constants can always be exported. 2323 return true; 2324 } 2325 2326 /// Return branch probability calculated by BranchProbabilityInfo for IR blocks. 2327 BranchProbability 2328 SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src, 2329 const MachineBasicBlock *Dst) const { 2330 BranchProbabilityInfo *BPI = FuncInfo.BPI; 2331 const BasicBlock *SrcBB = Src->getBasicBlock(); 2332 const BasicBlock *DstBB = Dst->getBasicBlock(); 2333 if (!BPI) { 2334 // If BPI is not available, set the default probability as 1 / N, where N is 2335 // the number of successors. 2336 auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1); 2337 return BranchProbability(1, SuccSize); 2338 } 2339 return BPI->getEdgeProbability(SrcBB, DstBB); 2340 } 2341 2342 void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src, 2343 MachineBasicBlock *Dst, 2344 BranchProbability Prob) { 2345 if (!FuncInfo.BPI) 2346 Src->addSuccessorWithoutProb(Dst); 2347 else { 2348 if (Prob.isUnknown()) 2349 Prob = getEdgeProbability(Src, Dst); 2350 Src->addSuccessor(Dst, Prob); 2351 } 2352 } 2353 2354 static bool InBlock(const Value *V, const BasicBlock *BB) { 2355 if (const Instruction *I = dyn_cast<Instruction>(V)) 2356 return I->getParent() == BB; 2357 return true; 2358 } 2359 2360 /// EmitBranchForMergedCondition - Helper method for FindMergedConditions. 2361 /// This function emits a branch and is used at the leaves of an OR or an 2362 /// AND operator tree. 2363 void 2364 SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond, 2365 MachineBasicBlock *TBB, 2366 MachineBasicBlock *FBB, 2367 MachineBasicBlock *CurBB, 2368 MachineBasicBlock *SwitchBB, 2369 BranchProbability TProb, 2370 BranchProbability FProb, 2371 bool InvertCond) { 2372 const BasicBlock *BB = CurBB->getBasicBlock(); 2373 2374 // If the leaf of the tree is a comparison, merge the condition into 2375 // the caseblock. 2376 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) { 2377 // The operands of the cmp have to be in this block. We don't know 2378 // how to export them from some other block. If this is the first block 2379 // of the sequence, no exporting is needed. 2380 if (CurBB == SwitchBB || 2381 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) && 2382 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) { 2383 ISD::CondCode Condition; 2384 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) { 2385 ICmpInst::Predicate Pred = 2386 InvertCond ? IC->getInversePredicate() : IC->getPredicate(); 2387 Condition = getICmpCondCode(Pred); 2388 } else { 2389 const FCmpInst *FC = cast<FCmpInst>(Cond); 2390 FCmpInst::Predicate Pred = 2391 InvertCond ? FC->getInversePredicate() : FC->getPredicate(); 2392 Condition = getFCmpCondCode(Pred); 2393 if (TM.Options.NoNaNsFPMath) 2394 Condition = getFCmpCodeWithoutNaN(Condition); 2395 } 2396 2397 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr, 2398 TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb); 2399 SL->SwitchCases.push_back(CB); 2400 return; 2401 } 2402 } 2403 2404 // Create a CaseBlock record representing this branch. 2405 ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ; 2406 CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()), 2407 nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb); 2408 SL->SwitchCases.push_back(CB); 2409 } 2410 2411 void SelectionDAGBuilder::FindMergedConditions(const Value *Cond, 2412 MachineBasicBlock *TBB, 2413 MachineBasicBlock *FBB, 2414 MachineBasicBlock *CurBB, 2415 MachineBasicBlock *SwitchBB, 2416 Instruction::BinaryOps Opc, 2417 BranchProbability TProb, 2418 BranchProbability FProb, 2419 bool InvertCond) { 2420 // Skip over not part of the tree and remember to invert op and operands at 2421 // next level. 2422 Value *NotCond; 2423 if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) && 2424 InBlock(NotCond, CurBB->getBasicBlock())) { 2425 FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb, 2426 !InvertCond); 2427 return; 2428 } 2429 2430 const Instruction *BOp = dyn_cast<Instruction>(Cond); 2431 const Value *BOpOp0, *BOpOp1; 2432 // Compute the effective opcode for Cond, taking into account whether it needs 2433 // to be inverted, e.g. 2434 // and (not (or A, B)), C 2435 // gets lowered as 2436 // and (and (not A, not B), C) 2437 Instruction::BinaryOps BOpc = (Instruction::BinaryOps)0; 2438 if (BOp) { 2439 BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1))) 2440 ? Instruction::And 2441 : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1))) 2442 ? Instruction::Or 2443 : (Instruction::BinaryOps)0); 2444 if (InvertCond) { 2445 if (BOpc == Instruction::And) 2446 BOpc = Instruction::Or; 2447 else if (BOpc == Instruction::Or) 2448 BOpc = Instruction::And; 2449 } 2450 } 2451 2452 // If this node is not part of the or/and tree, emit it as a branch. 2453 // Note that all nodes in the tree should have same opcode. 2454 bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse(); 2455 if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() || 2456 !InBlock(BOpOp0, CurBB->getBasicBlock()) || 2457 !InBlock(BOpOp1, CurBB->getBasicBlock())) { 2458 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB, 2459 TProb, FProb, InvertCond); 2460 return; 2461 } 2462 2463 // Create TmpBB after CurBB. 2464 MachineFunction::iterator BBI(CurBB); 2465 MachineFunction &MF = DAG.getMachineFunction(); 2466 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock()); 2467 CurBB->getParent()->insert(++BBI, TmpBB); 2468 2469 if (Opc == Instruction::Or) { 2470 // Codegen X | Y as: 2471 // BB1: 2472 // jmp_if_X TBB 2473 // jmp TmpBB 2474 // TmpBB: 2475 // jmp_if_Y TBB 2476 // jmp FBB 2477 // 2478 2479 // We have flexibility in setting Prob for BB1 and Prob for TmpBB. 2480 // The requirement is that 2481 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB) 2482 // = TrueProb for original BB. 2483 // Assuming the original probabilities are A and B, one choice is to set 2484 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to 2485 // A/(1+B) and 2B/(1+B). This choice assumes that 2486 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB. 2487 // Another choice is to assume TrueProb for BB1 equals to TrueProb for 2488 // TmpBB, but the math is more complicated. 2489 2490 auto NewTrueProb = TProb / 2; 2491 auto NewFalseProb = TProb / 2 + FProb; 2492 // Emit the LHS condition. 2493 FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb, 2494 NewFalseProb, InvertCond); 2495 2496 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B). 2497 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb}; 2498 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end()); 2499 // Emit the RHS condition into TmpBB. 2500 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0], 2501 Probs[1], InvertCond); 2502 } else { 2503 assert(Opc == Instruction::And && "Unknown merge op!"); 2504 // Codegen X & Y as: 2505 // BB1: 2506 // jmp_if_X TmpBB 2507 // jmp FBB 2508 // TmpBB: 2509 // jmp_if_Y TBB 2510 // jmp FBB 2511 // 2512 // This requires creation of TmpBB after CurBB. 2513 2514 // We have flexibility in setting Prob for BB1 and Prob for TmpBB. 2515 // The requirement is that 2516 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB) 2517 // = FalseProb for original BB. 2518 // Assuming the original probabilities are A and B, one choice is to set 2519 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to 2520 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 == 2521 // TrueProb for BB1 * FalseProb for TmpBB. 2522 2523 auto NewTrueProb = TProb + FProb / 2; 2524 auto NewFalseProb = FProb / 2; 2525 // Emit the LHS condition. 2526 FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb, 2527 NewFalseProb, InvertCond); 2528 2529 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A). 2530 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2}; 2531 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end()); 2532 // Emit the RHS condition into TmpBB. 2533 FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0], 2534 Probs[1], InvertCond); 2535 } 2536 } 2537 2538 /// If the set of cases should be emitted as a series of branches, return true. 2539 /// If we should emit this as a bunch of and/or'd together conditions, return 2540 /// false. 2541 bool 2542 SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) { 2543 if (Cases.size() != 2) return true; 2544 2545 // If this is two comparisons of the same values or'd or and'd together, they 2546 // will get folded into a single comparison, so don't emit two blocks. 2547 if ((Cases[0].CmpLHS == Cases[1].CmpLHS && 2548 Cases[0].CmpRHS == Cases[1].CmpRHS) || 2549 (Cases[0].CmpRHS == Cases[1].CmpLHS && 2550 Cases[0].CmpLHS == Cases[1].CmpRHS)) { 2551 return false; 2552 } 2553 2554 // Handle: (X != null) | (Y != null) --> (X|Y) != 0 2555 // Handle: (X == null) & (Y == null) --> (X|Y) == 0 2556 if (Cases[0].CmpRHS == Cases[1].CmpRHS && 2557 Cases[0].CC == Cases[1].CC && 2558 isa<Constant>(Cases[0].CmpRHS) && 2559 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) { 2560 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB) 2561 return false; 2562 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB) 2563 return false; 2564 } 2565 2566 return true; 2567 } 2568 2569 void SelectionDAGBuilder::visitBr(const BranchInst &I) { 2570 MachineBasicBlock *BrMBB = FuncInfo.MBB; 2571 2572 // Update machine-CFG edges. 2573 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)]; 2574 2575 if (I.isUnconditional()) { 2576 // Update machine-CFG edges. 2577 BrMBB->addSuccessor(Succ0MBB); 2578 2579 // If this is not a fall-through branch or optimizations are switched off, 2580 // emit the branch. 2581 if (Succ0MBB != NextBlock(BrMBB) || 2582 TM.getOptLevel() == CodeGenOptLevel::None) { 2583 auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, 2584 getControlRoot(), DAG.getBasicBlock(Succ0MBB)); 2585 setValue(&I, Br); 2586 DAG.setRoot(Br); 2587 } 2588 2589 return; 2590 } 2591 2592 // If this condition is one of the special cases we handle, do special stuff 2593 // now. 2594 const Value *CondVal = I.getCondition(); 2595 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)]; 2596 2597 // If this is a series of conditions that are or'd or and'd together, emit 2598 // this as a sequence of branches instead of setcc's with and/or operations. 2599 // As long as jumps are not expensive (exceptions for multi-use logic ops, 2600 // unpredictable branches, and vector extracts because those jumps are likely 2601 // expensive for any target), this should improve performance. 2602 // For example, instead of something like: 2603 // cmp A, B 2604 // C = seteq 2605 // cmp D, E 2606 // F = setle 2607 // or C, F 2608 // jnz foo 2609 // Emit: 2610 // cmp A, B 2611 // je foo 2612 // cmp D, E 2613 // jle foo 2614 const Instruction *BOp = dyn_cast<Instruction>(CondVal); 2615 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp && 2616 BOp->hasOneUse() && !I.hasMetadata(LLVMContext::MD_unpredictable)) { 2617 Value *Vec; 2618 const Value *BOp0, *BOp1; 2619 Instruction::BinaryOps Opcode = (Instruction::BinaryOps)0; 2620 if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1)))) 2621 Opcode = Instruction::And; 2622 else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1)))) 2623 Opcode = Instruction::Or; 2624 2625 if (Opcode && !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) && 2626 match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value())))) { 2627 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode, 2628 getEdgeProbability(BrMBB, Succ0MBB), 2629 getEdgeProbability(BrMBB, Succ1MBB), 2630 /*InvertCond=*/false); 2631 // If the compares in later blocks need to use values not currently 2632 // exported from this block, export them now. This block should always 2633 // be the first entry. 2634 assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!"); 2635 2636 // Allow some cases to be rejected. 2637 if (ShouldEmitAsBranches(SL->SwitchCases)) { 2638 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) { 2639 ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS); 2640 ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS); 2641 } 2642 2643 // Emit the branch for this block. 2644 visitSwitchCase(SL->SwitchCases[0], BrMBB); 2645 SL->SwitchCases.erase(SL->SwitchCases.begin()); 2646 return; 2647 } 2648 2649 // Okay, we decided not to do this, remove any inserted MBB's and clear 2650 // SwitchCases. 2651 for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) 2652 FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB); 2653 2654 SL->SwitchCases.clear(); 2655 } 2656 } 2657 2658 // Create a CaseBlock record representing this branch. 2659 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()), 2660 nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc()); 2661 2662 // Use visitSwitchCase to actually insert the fast branch sequence for this 2663 // cond branch. 2664 visitSwitchCase(CB, BrMBB); 2665 } 2666 2667 /// visitSwitchCase - Emits the necessary code to represent a single node in 2668 /// the binary search tree resulting from lowering a switch instruction. 2669 void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB, 2670 MachineBasicBlock *SwitchBB) { 2671 SDValue Cond; 2672 SDValue CondLHS = getValue(CB.CmpLHS); 2673 SDLoc dl = CB.DL; 2674 2675 if (CB.CC == ISD::SETTRUE) { 2676 // Branch or fall through to TrueBB. 2677 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb); 2678 SwitchBB->normalizeSuccProbs(); 2679 if (CB.TrueBB != NextBlock(SwitchBB)) { 2680 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(), 2681 DAG.getBasicBlock(CB.TrueBB))); 2682 } 2683 return; 2684 } 2685 2686 auto &TLI = DAG.getTargetLoweringInfo(); 2687 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType()); 2688 2689 // Build the setcc now. 2690 if (!CB.CmpMHS) { 2691 // Fold "(X == true)" to X and "(X == false)" to !X to 2692 // handle common cases produced by branch lowering. 2693 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) && 2694 CB.CC == ISD::SETEQ) 2695 Cond = CondLHS; 2696 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) && 2697 CB.CC == ISD::SETEQ) { 2698 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType()); 2699 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True); 2700 } else { 2701 SDValue CondRHS = getValue(CB.CmpRHS); 2702 2703 // If a pointer's DAG type is larger than its memory type then the DAG 2704 // values are zero-extended. This breaks signed comparisons so truncate 2705 // back to the underlying type before doing the compare. 2706 if (CondLHS.getValueType() != MemVT) { 2707 CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT); 2708 CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT); 2709 } 2710 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC); 2711 } 2712 } else { 2713 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now"); 2714 2715 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue(); 2716 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue(); 2717 2718 SDValue CmpOp = getValue(CB.CmpMHS); 2719 EVT VT = CmpOp.getValueType(); 2720 2721 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) { 2722 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT), 2723 ISD::SETLE); 2724 } else { 2725 SDValue SUB = DAG.getNode(ISD::SUB, dl, 2726 VT, CmpOp, DAG.getConstant(Low, dl, VT)); 2727 Cond = DAG.getSetCC(dl, MVT::i1, SUB, 2728 DAG.getConstant(High-Low, dl, VT), ISD::SETULE); 2729 } 2730 } 2731 2732 // Update successor info 2733 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb); 2734 // TrueBB and FalseBB are always different unless the incoming IR is 2735 // degenerate. This only happens when running llc on weird IR. 2736 if (CB.TrueBB != CB.FalseBB) 2737 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb); 2738 SwitchBB->normalizeSuccProbs(); 2739 2740 // If the lhs block is the next block, invert the condition so that we can 2741 // fall through to the lhs instead of the rhs block. 2742 if (CB.TrueBB == NextBlock(SwitchBB)) { 2743 std::swap(CB.TrueBB, CB.FalseBB); 2744 SDValue True = DAG.getConstant(1, dl, Cond.getValueType()); 2745 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True); 2746 } 2747 2748 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, 2749 MVT::Other, getControlRoot(), Cond, 2750 DAG.getBasicBlock(CB.TrueBB)); 2751 2752 setValue(CurInst, BrCond); 2753 2754 // Insert the false branch. Do this even if it's a fall through branch, 2755 // this makes it easier to do DAG optimizations which require inverting 2756 // the branch condition. 2757 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, 2758 DAG.getBasicBlock(CB.FalseBB)); 2759 2760 DAG.setRoot(BrCond); 2761 } 2762 2763 /// visitJumpTable - Emit JumpTable node in the current MBB 2764 void SelectionDAGBuilder::visitJumpTable(SwitchCG::JumpTable &JT) { 2765 // Emit the code for the jump table 2766 assert(JT.SL && "Should set SDLoc for SelectionDAG!"); 2767 assert(JT.Reg != -1U && "Should lower JT Header first!"); 2768 EVT PTy = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); 2769 SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy); 2770 SDValue Table = DAG.getJumpTable(JT.JTI, PTy); 2771 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other, 2772 Index.getValue(1), Table, Index); 2773 DAG.setRoot(BrJumpTable); 2774 } 2775 2776 /// visitJumpTableHeader - This function emits necessary code to produce index 2777 /// in the JumpTable from switch case. 2778 void SelectionDAGBuilder::visitJumpTableHeader(SwitchCG::JumpTable &JT, 2779 JumpTableHeader &JTH, 2780 MachineBasicBlock *SwitchBB) { 2781 assert(JT.SL && "Should set SDLoc for SelectionDAG!"); 2782 const SDLoc &dl = *JT.SL; 2783 2784 // Subtract the lowest switch case value from the value being switched on. 2785 SDValue SwitchOp = getValue(JTH.SValue); 2786 EVT VT = SwitchOp.getValueType(); 2787 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp, 2788 DAG.getConstant(JTH.First, dl, VT)); 2789 2790 // The SDNode we just created, which holds the value being switched on minus 2791 // the smallest case value, needs to be copied to a virtual register so it 2792 // can be used as an index into the jump table in a subsequent basic block. 2793 // This value may be smaller or larger than the target's pointer type, and 2794 // therefore require extension or truncating. 2795 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2796 SwitchOp = DAG.getZExtOrTrunc(Sub, dl, TLI.getPointerTy(DAG.getDataLayout())); 2797 2798 unsigned JumpTableReg = 2799 FuncInfo.CreateReg(TLI.getPointerTy(DAG.getDataLayout())); 2800 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, 2801 JumpTableReg, SwitchOp); 2802 JT.Reg = JumpTableReg; 2803 2804 if (!JTH.FallthroughUnreachable) { 2805 // Emit the range check for the jump table, and branch to the default block 2806 // for the switch statement if the value being switched on exceeds the 2807 // largest case in the switch. 2808 SDValue CMP = DAG.getSetCC( 2809 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), 2810 Sub.getValueType()), 2811 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT); 2812 2813 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, 2814 MVT::Other, CopyTo, CMP, 2815 DAG.getBasicBlock(JT.Default)); 2816 2817 // Avoid emitting unnecessary branches to the next block. 2818 if (JT.MBB != NextBlock(SwitchBB)) 2819 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond, 2820 DAG.getBasicBlock(JT.MBB)); 2821 2822 DAG.setRoot(BrCond); 2823 } else { 2824 // Avoid emitting unnecessary branches to the next block. 2825 if (JT.MBB != NextBlock(SwitchBB)) 2826 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo, 2827 DAG.getBasicBlock(JT.MBB))); 2828 else 2829 DAG.setRoot(CopyTo); 2830 } 2831 } 2832 2833 /// Create a LOAD_STACK_GUARD node, and let it carry the target specific global 2834 /// variable if there exists one. 2835 static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL, 2836 SDValue &Chain) { 2837 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2838 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout()); 2839 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout()); 2840 MachineFunction &MF = DAG.getMachineFunction(); 2841 Value *Global = TLI.getSDagStackGuard(*MF.getFunction().getParent()); 2842 MachineSDNode *Node = 2843 DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain); 2844 if (Global) { 2845 MachinePointerInfo MPInfo(Global); 2846 auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant | 2847 MachineMemOperand::MODereferenceable; 2848 MachineMemOperand *MemRef = MF.getMachineMemOperand( 2849 MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy)); 2850 DAG.setNodeMemRefs(Node, {MemRef}); 2851 } 2852 if (PtrTy != PtrMemTy) 2853 return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy); 2854 return SDValue(Node, 0); 2855 } 2856 2857 /// Codegen a new tail for a stack protector check ParentMBB which has had its 2858 /// tail spliced into a stack protector check success bb. 2859 /// 2860 /// For a high level explanation of how this fits into the stack protector 2861 /// generation see the comment on the declaration of class 2862 /// StackProtectorDescriptor. 2863 void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD, 2864 MachineBasicBlock *ParentBB) { 2865 2866 // First create the loads to the guard/stack slot for the comparison. 2867 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2868 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout()); 2869 EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout()); 2870 2871 MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo(); 2872 int FI = MFI.getStackProtectorIndex(); 2873 2874 SDValue Guard; 2875 SDLoc dl = getCurSDLoc(); 2876 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy); 2877 const Module &M = *ParentBB->getParent()->getFunction().getParent(); 2878 Align Align = 2879 DAG.getDataLayout().getPrefTypeAlign(PointerType::get(M.getContext(), 0)); 2880 2881 // Generate code to load the content of the guard slot. 2882 SDValue GuardVal = DAG.getLoad( 2883 PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr, 2884 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align, 2885 MachineMemOperand::MOVolatile); 2886 2887 if (TLI.useStackGuardXorFP()) 2888 GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl); 2889 2890 // Retrieve guard check function, nullptr if instrumentation is inlined. 2891 if (const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M)) { 2892 // The target provides a guard check function to validate the guard value. 2893 // Generate a call to that function with the content of the guard slot as 2894 // argument. 2895 FunctionType *FnTy = GuardCheckFn->getFunctionType(); 2896 assert(FnTy->getNumParams() == 1 && "Invalid function signature"); 2897 2898 TargetLowering::ArgListTy Args; 2899 TargetLowering::ArgListEntry Entry; 2900 Entry.Node = GuardVal; 2901 Entry.Ty = FnTy->getParamType(0); 2902 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg)) 2903 Entry.IsInReg = true; 2904 Args.push_back(Entry); 2905 2906 TargetLowering::CallLoweringInfo CLI(DAG); 2907 CLI.setDebugLoc(getCurSDLoc()) 2908 .setChain(DAG.getEntryNode()) 2909 .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(), 2910 getValue(GuardCheckFn), std::move(Args)); 2911 2912 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); 2913 DAG.setRoot(Result.second); 2914 return; 2915 } 2916 2917 // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD. 2918 // Otherwise, emit a volatile load to retrieve the stack guard value. 2919 SDValue Chain = DAG.getEntryNode(); 2920 if (TLI.useLoadStackGuardNode()) { 2921 Guard = getLoadStackGuard(DAG, dl, Chain); 2922 } else { 2923 const Value *IRGuard = TLI.getSDagStackGuard(M); 2924 SDValue GuardPtr = getValue(IRGuard); 2925 2926 Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr, 2927 MachinePointerInfo(IRGuard, 0), Align, 2928 MachineMemOperand::MOVolatile); 2929 } 2930 2931 // Perform the comparison via a getsetcc. 2932 SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(DAG.getDataLayout(), 2933 *DAG.getContext(), 2934 Guard.getValueType()), 2935 Guard, GuardVal, ISD::SETNE); 2936 2937 // If the guard/stackslot do not equal, branch to failure MBB. 2938 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl, 2939 MVT::Other, GuardVal.getOperand(0), 2940 Cmp, DAG.getBasicBlock(SPD.getFailureMBB())); 2941 // Otherwise branch to success MBB. 2942 SDValue Br = DAG.getNode(ISD::BR, dl, 2943 MVT::Other, BrCond, 2944 DAG.getBasicBlock(SPD.getSuccessMBB())); 2945 2946 DAG.setRoot(Br); 2947 } 2948 2949 /// Codegen the failure basic block for a stack protector check. 2950 /// 2951 /// A failure stack protector machine basic block consists simply of a call to 2952 /// __stack_chk_fail(). 2953 /// 2954 /// For a high level explanation of how this fits into the stack protector 2955 /// generation see the comment on the declaration of class 2956 /// StackProtectorDescriptor. 2957 void 2958 SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) { 2959 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2960 TargetLowering::MakeLibCallOptions CallOptions; 2961 CallOptions.setDiscardResult(true); 2962 SDValue Chain = 2963 TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid, 2964 std::nullopt, CallOptions, getCurSDLoc()) 2965 .second; 2966 // On PS4/PS5, the "return address" must still be within the calling 2967 // function, even if it's at the very end, so emit an explicit TRAP here. 2968 // Passing 'true' for doesNotReturn above won't generate the trap for us. 2969 if (TM.getTargetTriple().isPS()) 2970 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain); 2971 // WebAssembly needs an unreachable instruction after a non-returning call, 2972 // because the function return type can be different from __stack_chk_fail's 2973 // return type (void). 2974 if (TM.getTargetTriple().isWasm()) 2975 Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain); 2976 2977 DAG.setRoot(Chain); 2978 } 2979 2980 /// visitBitTestHeader - This function emits necessary code to produce value 2981 /// suitable for "bit tests" 2982 void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B, 2983 MachineBasicBlock *SwitchBB) { 2984 SDLoc dl = getCurSDLoc(); 2985 2986 // Subtract the minimum value. 2987 SDValue SwitchOp = getValue(B.SValue); 2988 EVT VT = SwitchOp.getValueType(); 2989 SDValue RangeSub = 2990 DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT)); 2991 2992 // Determine the type of the test operands. 2993 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 2994 bool UsePtrType = false; 2995 if (!TLI.isTypeLegal(VT)) { 2996 UsePtrType = true; 2997 } else { 2998 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i) 2999 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) { 3000 // Switch table case range are encoded into series of masks. 3001 // Just use pointer type, it's guaranteed to fit. 3002 UsePtrType = true; 3003 break; 3004 } 3005 } 3006 SDValue Sub = RangeSub; 3007 if (UsePtrType) { 3008 VT = TLI.getPointerTy(DAG.getDataLayout()); 3009 Sub = DAG.getZExtOrTrunc(Sub, dl, VT); 3010 } 3011 3012 B.RegVT = VT.getSimpleVT(); 3013 B.Reg = FuncInfo.CreateReg(B.RegVT); 3014 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub); 3015 3016 MachineBasicBlock* MBB = B.Cases[0].ThisBB; 3017 3018 if (!B.FallthroughUnreachable) 3019 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb); 3020 addSuccessorWithProb(SwitchBB, MBB, B.Prob); 3021 SwitchBB->normalizeSuccProbs(); 3022 3023 SDValue Root = CopyTo; 3024 if (!B.FallthroughUnreachable) { 3025 // Conditional branch to the default block. 3026 SDValue RangeCmp = DAG.getSetCC(dl, 3027 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), 3028 RangeSub.getValueType()), 3029 RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()), 3030 ISD::SETUGT); 3031 3032 Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp, 3033 DAG.getBasicBlock(B.Default)); 3034 } 3035 3036 // Avoid emitting unnecessary branches to the next block. 3037 if (MBB != NextBlock(SwitchBB)) 3038 Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB)); 3039 3040 DAG.setRoot(Root); 3041 } 3042 3043 /// visitBitTestCase - this function produces one "bit test" 3044 void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB, 3045 MachineBasicBlock* NextMBB, 3046 BranchProbability BranchProbToNext, 3047 unsigned Reg, 3048 BitTestCase &B, 3049 MachineBasicBlock *SwitchBB) { 3050 SDLoc dl = getCurSDLoc(); 3051 MVT VT = BB.RegVT; 3052 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT); 3053 SDValue Cmp; 3054 unsigned PopCount = llvm::popcount(B.Mask); 3055 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3056 if (PopCount == 1) { 3057 // Testing for a single bit; just compare the shift count with what it 3058 // would need to be to shift a 1 bit in that position. 3059 Cmp = DAG.getSetCC( 3060 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT), 3061 ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT), 3062 ISD::SETEQ); 3063 } else if (PopCount == BB.Range) { 3064 // There is only one zero bit in the range, test for it directly. 3065 Cmp = DAG.getSetCC( 3066 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT), 3067 ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE); 3068 } else { 3069 // Make desired shift 3070 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT, 3071 DAG.getConstant(1, dl, VT), ShiftOp); 3072 3073 // Emit bit tests and jumps 3074 SDValue AndOp = DAG.getNode(ISD::AND, dl, 3075 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT)); 3076 Cmp = DAG.getSetCC( 3077 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT), 3078 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE); 3079 } 3080 3081 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb. 3082 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb); 3083 // The branch probability from SwitchBB to NextMBB is BranchProbToNext. 3084 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext); 3085 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is 3086 // one as they are relative probabilities (and thus work more like weights), 3087 // and hence we need to normalize them to let the sum of them become one. 3088 SwitchBB->normalizeSuccProbs(); 3089 3090 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl, 3091 MVT::Other, getControlRoot(), 3092 Cmp, DAG.getBasicBlock(B.TargetBB)); 3093 3094 // Avoid emitting unnecessary branches to the next block. 3095 if (NextMBB != NextBlock(SwitchBB)) 3096 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd, 3097 DAG.getBasicBlock(NextMBB)); 3098 3099 DAG.setRoot(BrAnd); 3100 } 3101 3102 void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) { 3103 MachineBasicBlock *InvokeMBB = FuncInfo.MBB; 3104 3105 // Retrieve successors. Look through artificial IR level blocks like 3106 // catchswitch for successors. 3107 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)]; 3108 const BasicBlock *EHPadBB = I.getSuccessor(1); 3109 MachineBasicBlock *EHPadMBB = FuncInfo.MBBMap[EHPadBB]; 3110 3111 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't 3112 // have to do anything here to lower funclet bundles. 3113 assert(!I.hasOperandBundlesOtherThan( 3114 {LLVMContext::OB_deopt, LLVMContext::OB_gc_transition, 3115 LLVMContext::OB_gc_live, LLVMContext::OB_funclet, 3116 LLVMContext::OB_cfguardtarget, 3117 LLVMContext::OB_clang_arc_attachedcall}) && 3118 "Cannot lower invokes with arbitrary operand bundles yet!"); 3119 3120 const Value *Callee(I.getCalledOperand()); 3121 const Function *Fn = dyn_cast<Function>(Callee); 3122 if (isa<InlineAsm>(Callee)) 3123 visitInlineAsm(I, EHPadBB); 3124 else if (Fn && Fn->isIntrinsic()) { 3125 switch (Fn->getIntrinsicID()) { 3126 default: 3127 llvm_unreachable("Cannot invoke this intrinsic"); 3128 case Intrinsic::donothing: 3129 // Ignore invokes to @llvm.donothing: jump directly to the next BB. 3130 case Intrinsic::seh_try_begin: 3131 case Intrinsic::seh_scope_begin: 3132 case Intrinsic::seh_try_end: 3133 case Intrinsic::seh_scope_end: 3134 if (EHPadMBB) 3135 // a block referenced by EH table 3136 // so dtor-funclet not removed by opts 3137 EHPadMBB->setMachineBlockAddressTaken(); 3138 break; 3139 case Intrinsic::experimental_patchpoint_void: 3140 case Intrinsic::experimental_patchpoint_i64: 3141 visitPatchpoint(I, EHPadBB); 3142 break; 3143 case Intrinsic::experimental_gc_statepoint: 3144 LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB); 3145 break; 3146 case Intrinsic::wasm_rethrow: { 3147 // This is usually done in visitTargetIntrinsic, but this intrinsic is 3148 // special because it can be invoked, so we manually lower it to a DAG 3149 // node here. 3150 SmallVector<SDValue, 8> Ops; 3151 Ops.push_back(getRoot()); // inchain 3152 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3153 Ops.push_back( 3154 DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(), 3155 TLI.getPointerTy(DAG.getDataLayout()))); 3156 SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain 3157 DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops)); 3158 break; 3159 } 3160 } 3161 } else if (I.countOperandBundlesOfType(LLVMContext::OB_deopt)) { 3162 // Currently we do not lower any intrinsic calls with deopt operand bundles. 3163 // Eventually we will support lowering the @llvm.experimental.deoptimize 3164 // intrinsic, and right now there are no plans to support other intrinsics 3165 // with deopt state. 3166 LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB); 3167 } else { 3168 LowerCallTo(I, getValue(Callee), false, false, EHPadBB); 3169 } 3170 3171 // If the value of the invoke is used outside of its defining block, make it 3172 // available as a virtual register. 3173 // We already took care of the exported value for the statepoint instruction 3174 // during call to the LowerStatepoint. 3175 if (!isa<GCStatepointInst>(I)) { 3176 CopyToExportRegsIfNeeded(&I); 3177 } 3178 3179 SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests; 3180 BranchProbabilityInfo *BPI = FuncInfo.BPI; 3181 BranchProbability EHPadBBProb = 3182 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB) 3183 : BranchProbability::getZero(); 3184 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests); 3185 3186 // Update successor info. 3187 addSuccessorWithProb(InvokeMBB, Return); 3188 for (auto &UnwindDest : UnwindDests) { 3189 UnwindDest.first->setIsEHPad(); 3190 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second); 3191 } 3192 InvokeMBB->normalizeSuccProbs(); 3193 3194 // Drop into normal successor. 3195 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(), 3196 DAG.getBasicBlock(Return))); 3197 } 3198 3199 void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) { 3200 MachineBasicBlock *CallBrMBB = FuncInfo.MBB; 3201 3202 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't 3203 // have to do anything here to lower funclet bundles. 3204 assert(!I.hasOperandBundlesOtherThan( 3205 {LLVMContext::OB_deopt, LLVMContext::OB_funclet}) && 3206 "Cannot lower callbrs with arbitrary operand bundles yet!"); 3207 3208 assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr"); 3209 visitInlineAsm(I); 3210 CopyToExportRegsIfNeeded(&I); 3211 3212 // Retrieve successors. 3213 SmallPtrSet<BasicBlock *, 8> Dests; 3214 Dests.insert(I.getDefaultDest()); 3215 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getDefaultDest()]; 3216 3217 // Update successor info. 3218 addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne()); 3219 for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) { 3220 BasicBlock *Dest = I.getIndirectDest(i); 3221 MachineBasicBlock *Target = FuncInfo.MBBMap[Dest]; 3222 Target->setIsInlineAsmBrIndirectTarget(); 3223 Target->setMachineBlockAddressTaken(); 3224 Target->setLabelMustBeEmitted(); 3225 // Don't add duplicate machine successors. 3226 if (Dests.insert(Dest).second) 3227 addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero()); 3228 } 3229 CallBrMBB->normalizeSuccProbs(); 3230 3231 // Drop into default successor. 3232 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), 3233 MVT::Other, getControlRoot(), 3234 DAG.getBasicBlock(Return))); 3235 } 3236 3237 void SelectionDAGBuilder::visitResume(const ResumeInst &RI) { 3238 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!"); 3239 } 3240 3241 void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) { 3242 assert(FuncInfo.MBB->isEHPad() && 3243 "Call to landingpad not in landing pad!"); 3244 3245 // If there aren't registers to copy the values into (e.g., during SjLj 3246 // exceptions), then don't bother to create these DAG nodes. 3247 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3248 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn(); 3249 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 && 3250 TLI.getExceptionSelectorRegister(PersonalityFn) == 0) 3251 return; 3252 3253 // If landingpad's return type is token type, we don't create DAG nodes 3254 // for its exception pointer and selector value. The extraction of exception 3255 // pointer or selector value from token type landingpads is not currently 3256 // supported. 3257 if (LP.getType()->isTokenTy()) 3258 return; 3259 3260 SmallVector<EVT, 2> ValueVTs; 3261 SDLoc dl = getCurSDLoc(); 3262 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs); 3263 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported"); 3264 3265 // Get the two live-in registers as SDValues. The physregs have already been 3266 // copied into virtual registers. 3267 SDValue Ops[2]; 3268 if (FuncInfo.ExceptionPointerVirtReg) { 3269 Ops[0] = DAG.getZExtOrTrunc( 3270 DAG.getCopyFromReg(DAG.getEntryNode(), dl, 3271 FuncInfo.ExceptionPointerVirtReg, 3272 TLI.getPointerTy(DAG.getDataLayout())), 3273 dl, ValueVTs[0]); 3274 } else { 3275 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())); 3276 } 3277 Ops[1] = DAG.getZExtOrTrunc( 3278 DAG.getCopyFromReg(DAG.getEntryNode(), dl, 3279 FuncInfo.ExceptionSelectorVirtReg, 3280 TLI.getPointerTy(DAG.getDataLayout())), 3281 dl, ValueVTs[1]); 3282 3283 // Merge into one. 3284 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, 3285 DAG.getVTList(ValueVTs), Ops); 3286 setValue(&LP, Res); 3287 } 3288 3289 void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First, 3290 MachineBasicBlock *Last) { 3291 // Update JTCases. 3292 for (JumpTableBlock &JTB : SL->JTCases) 3293 if (JTB.first.HeaderBB == First) 3294 JTB.first.HeaderBB = Last; 3295 3296 // Update BitTestCases. 3297 for (BitTestBlock &BTB : SL->BitTestCases) 3298 if (BTB.Parent == First) 3299 BTB.Parent = Last; 3300 } 3301 3302 void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) { 3303 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB; 3304 3305 // Update machine-CFG edges with unique successors. 3306 SmallSet<BasicBlock*, 32> Done; 3307 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) { 3308 BasicBlock *BB = I.getSuccessor(i); 3309 bool Inserted = Done.insert(BB).second; 3310 if (!Inserted) 3311 continue; 3312 3313 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB]; 3314 addSuccessorWithProb(IndirectBrMBB, Succ); 3315 } 3316 IndirectBrMBB->normalizeSuccProbs(); 3317 3318 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(), 3319 MVT::Other, getControlRoot(), 3320 getValue(I.getAddress()))); 3321 } 3322 3323 void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) { 3324 if (!DAG.getTarget().Options.TrapUnreachable) 3325 return; 3326 3327 // We may be able to ignore unreachable behind a noreturn call. 3328 if (DAG.getTarget().Options.NoTrapAfterNoreturn) { 3329 if (const CallInst *Call = dyn_cast_or_null<CallInst>(I.getPrevNode())) { 3330 if (Call->doesNotReturn()) 3331 return; 3332 } 3333 } 3334 3335 DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot())); 3336 } 3337 3338 void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) { 3339 SDNodeFlags Flags; 3340 if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) 3341 Flags.copyFMF(*FPOp); 3342 3343 SDValue Op = getValue(I.getOperand(0)); 3344 SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(), 3345 Op, Flags); 3346 setValue(&I, UnNodeValue); 3347 } 3348 3349 void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) { 3350 SDNodeFlags Flags; 3351 if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) { 3352 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap()); 3353 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap()); 3354 } 3355 if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I)) 3356 Flags.setExact(ExactOp->isExact()); 3357 if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I)) 3358 Flags.setDisjoint(DisjointOp->isDisjoint()); 3359 if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) 3360 Flags.copyFMF(*FPOp); 3361 3362 SDValue Op1 = getValue(I.getOperand(0)); 3363 SDValue Op2 = getValue(I.getOperand(1)); 3364 SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), 3365 Op1, Op2, Flags); 3366 setValue(&I, BinNodeValue); 3367 } 3368 3369 void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) { 3370 SDValue Op1 = getValue(I.getOperand(0)); 3371 SDValue Op2 = getValue(I.getOperand(1)); 3372 3373 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy( 3374 Op1.getValueType(), DAG.getDataLayout()); 3375 3376 // Coerce the shift amount to the right type if we can. This exposes the 3377 // truncate or zext to optimization early. 3378 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) { 3379 assert(ShiftTy.getSizeInBits() >= Log2_32_Ceil(Op1.getValueSizeInBits()) && 3380 "Unexpected shift type"); 3381 Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy); 3382 } 3383 3384 bool nuw = false; 3385 bool nsw = false; 3386 bool exact = false; 3387 3388 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) { 3389 3390 if (const OverflowingBinaryOperator *OFBinOp = 3391 dyn_cast<const OverflowingBinaryOperator>(&I)) { 3392 nuw = OFBinOp->hasNoUnsignedWrap(); 3393 nsw = OFBinOp->hasNoSignedWrap(); 3394 } 3395 if (const PossiblyExactOperator *ExactOp = 3396 dyn_cast<const PossiblyExactOperator>(&I)) 3397 exact = ExactOp->isExact(); 3398 } 3399 SDNodeFlags Flags; 3400 Flags.setExact(exact); 3401 Flags.setNoSignedWrap(nsw); 3402 Flags.setNoUnsignedWrap(nuw); 3403 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2, 3404 Flags); 3405 setValue(&I, Res); 3406 } 3407 3408 void SelectionDAGBuilder::visitSDiv(const User &I) { 3409 SDValue Op1 = getValue(I.getOperand(0)); 3410 SDValue Op2 = getValue(I.getOperand(1)); 3411 3412 SDNodeFlags Flags; 3413 Flags.setExact(isa<PossiblyExactOperator>(&I) && 3414 cast<PossiblyExactOperator>(&I)->isExact()); 3415 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1, 3416 Op2, Flags)); 3417 } 3418 3419 void SelectionDAGBuilder::visitICmp(const User &I) { 3420 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE; 3421 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I)) 3422 predicate = IC->getPredicate(); 3423 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I)) 3424 predicate = ICmpInst::Predicate(IC->getPredicate()); 3425 SDValue Op1 = getValue(I.getOperand(0)); 3426 SDValue Op2 = getValue(I.getOperand(1)); 3427 ISD::CondCode Opcode = getICmpCondCode(predicate); 3428 3429 auto &TLI = DAG.getTargetLoweringInfo(); 3430 EVT MemVT = 3431 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType()); 3432 3433 // If a pointer's DAG type is larger than its memory type then the DAG values 3434 // are zero-extended. This breaks signed comparisons so truncate back to the 3435 // underlying type before doing the compare. 3436 if (Op1.getValueType() != MemVT) { 3437 Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT); 3438 Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT); 3439 } 3440 3441 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3442 I.getType()); 3443 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode)); 3444 } 3445 3446 void SelectionDAGBuilder::visitFCmp(const User &I) { 3447 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE; 3448 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I)) 3449 predicate = FC->getPredicate(); 3450 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I)) 3451 predicate = FCmpInst::Predicate(FC->getPredicate()); 3452 SDValue Op1 = getValue(I.getOperand(0)); 3453 SDValue Op2 = getValue(I.getOperand(1)); 3454 3455 ISD::CondCode Condition = getFCmpCondCode(predicate); 3456 auto *FPMO = cast<FPMathOperator>(&I); 3457 if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath) 3458 Condition = getFCmpCodeWithoutNaN(Condition); 3459 3460 SDNodeFlags Flags; 3461 Flags.copyFMF(*FPMO); 3462 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags); 3463 3464 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3465 I.getType()); 3466 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition)); 3467 } 3468 3469 // Check if the condition of the select has one use or two users that are both 3470 // selects with the same condition. 3471 static bool hasOnlySelectUsers(const Value *Cond) { 3472 return llvm::all_of(Cond->users(), [](const Value *V) { 3473 return isa<SelectInst>(V); 3474 }); 3475 } 3476 3477 void SelectionDAGBuilder::visitSelect(const User &I) { 3478 SmallVector<EVT, 4> ValueVTs; 3479 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(), 3480 ValueVTs); 3481 unsigned NumValues = ValueVTs.size(); 3482 if (NumValues == 0) return; 3483 3484 SmallVector<SDValue, 4> Values(NumValues); 3485 SDValue Cond = getValue(I.getOperand(0)); 3486 SDValue LHSVal = getValue(I.getOperand(1)); 3487 SDValue RHSVal = getValue(I.getOperand(2)); 3488 SmallVector<SDValue, 1> BaseOps(1, Cond); 3489 ISD::NodeType OpCode = 3490 Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT; 3491 3492 bool IsUnaryAbs = false; 3493 bool Negate = false; 3494 3495 SDNodeFlags Flags; 3496 if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) 3497 Flags.copyFMF(*FPOp); 3498 3499 Flags.setUnpredictable( 3500 cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable)); 3501 3502 // Min/max matching is only viable if all output VTs are the same. 3503 if (all_equal(ValueVTs)) { 3504 EVT VT = ValueVTs[0]; 3505 LLVMContext &Ctx = *DAG.getContext(); 3506 auto &TLI = DAG.getTargetLoweringInfo(); 3507 3508 // We care about the legality of the operation after it has been type 3509 // legalized. 3510 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal) 3511 VT = TLI.getTypeToTransformTo(Ctx, VT); 3512 3513 // If the vselect is legal, assume we want to leave this as a vector setcc + 3514 // vselect. Otherwise, if this is going to be scalarized, we want to see if 3515 // min/max is legal on the scalar type. 3516 bool UseScalarMinMax = VT.isVector() && 3517 !TLI.isOperationLegalOrCustom(ISD::VSELECT, VT); 3518 3519 // ValueTracking's select pattern matching does not account for -0.0, 3520 // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that 3521 // -0.0 is less than +0.0. 3522 Value *LHS, *RHS; 3523 auto SPR = matchSelectPattern(const_cast<User*>(&I), LHS, RHS); 3524 ISD::NodeType Opc = ISD::DELETED_NODE; 3525 switch (SPR.Flavor) { 3526 case SPF_UMAX: Opc = ISD::UMAX; break; 3527 case SPF_UMIN: Opc = ISD::UMIN; break; 3528 case SPF_SMAX: Opc = ISD::SMAX; break; 3529 case SPF_SMIN: Opc = ISD::SMIN; break; 3530 case SPF_FMINNUM: 3531 switch (SPR.NaNBehavior) { 3532 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?"); 3533 case SPNB_RETURNS_NAN: break; 3534 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break; 3535 case SPNB_RETURNS_ANY: 3536 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) || 3537 (UseScalarMinMax && 3538 TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType()))) 3539 Opc = ISD::FMINNUM; 3540 break; 3541 } 3542 break; 3543 case SPF_FMAXNUM: 3544 switch (SPR.NaNBehavior) { 3545 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?"); 3546 case SPNB_RETURNS_NAN: break; 3547 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break; 3548 case SPNB_RETURNS_ANY: 3549 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) || 3550 (UseScalarMinMax && 3551 TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType()))) 3552 Opc = ISD::FMAXNUM; 3553 break; 3554 } 3555 break; 3556 case SPF_NABS: 3557 Negate = true; 3558 [[fallthrough]]; 3559 case SPF_ABS: 3560 IsUnaryAbs = true; 3561 Opc = ISD::ABS; 3562 break; 3563 default: break; 3564 } 3565 3566 if (!IsUnaryAbs && Opc != ISD::DELETED_NODE && 3567 (TLI.isOperationLegalOrCustomOrPromote(Opc, VT) || 3568 (UseScalarMinMax && 3569 TLI.isOperationLegalOrCustom(Opc, VT.getScalarType()))) && 3570 // If the underlying comparison instruction is used by any other 3571 // instruction, the consumed instructions won't be destroyed, so it is 3572 // not profitable to convert to a min/max. 3573 hasOnlySelectUsers(cast<SelectInst>(I).getCondition())) { 3574 OpCode = Opc; 3575 LHSVal = getValue(LHS); 3576 RHSVal = getValue(RHS); 3577 BaseOps.clear(); 3578 } 3579 3580 if (IsUnaryAbs) { 3581 OpCode = Opc; 3582 LHSVal = getValue(LHS); 3583 BaseOps.clear(); 3584 } 3585 } 3586 3587 if (IsUnaryAbs) { 3588 for (unsigned i = 0; i != NumValues; ++i) { 3589 SDLoc dl = getCurSDLoc(); 3590 EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i); 3591 Values[i] = 3592 DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i)); 3593 if (Negate) 3594 Values[i] = DAG.getNegative(Values[i], dl, VT); 3595 } 3596 } else { 3597 for (unsigned i = 0; i != NumValues; ++i) { 3598 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end()); 3599 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i)); 3600 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i)); 3601 Values[i] = DAG.getNode( 3602 OpCode, getCurSDLoc(), 3603 LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags); 3604 } 3605 } 3606 3607 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 3608 DAG.getVTList(ValueVTs), Values)); 3609 } 3610 3611 void SelectionDAGBuilder::visitTrunc(const User &I) { 3612 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest). 3613 SDValue N = getValue(I.getOperand(0)); 3614 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3615 I.getType()); 3616 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N)); 3617 } 3618 3619 void SelectionDAGBuilder::visitZExt(const User &I) { 3620 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest). 3621 // ZExt also can't be a cast to bool for same reason. So, nothing much to do 3622 SDValue N = getValue(I.getOperand(0)); 3623 auto &TLI = DAG.getTargetLoweringInfo(); 3624 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 3625 3626 SDNodeFlags Flags; 3627 if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I)) 3628 Flags.setNonNeg(PNI->hasNonNeg()); 3629 3630 // Eagerly use nonneg information to canonicalize towards sign_extend if 3631 // that is the target's preference. 3632 // TODO: Let the target do this later. 3633 if (Flags.hasNonNeg() && 3634 TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) { 3635 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N)); 3636 return; 3637 } 3638 3639 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags)); 3640 } 3641 3642 void SelectionDAGBuilder::visitSExt(const User &I) { 3643 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest). 3644 // SExt also can't be a cast to bool for same reason. So, nothing much to do 3645 SDValue N = getValue(I.getOperand(0)); 3646 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3647 I.getType()); 3648 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N)); 3649 } 3650 3651 void SelectionDAGBuilder::visitFPTrunc(const User &I) { 3652 // FPTrunc is never a no-op cast, no need to check 3653 SDValue N = getValue(I.getOperand(0)); 3654 SDLoc dl = getCurSDLoc(); 3655 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3656 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 3657 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N, 3658 DAG.getTargetConstant( 3659 0, dl, TLI.getPointerTy(DAG.getDataLayout())))); 3660 } 3661 3662 void SelectionDAGBuilder::visitFPExt(const User &I) { 3663 // FPExt is never a no-op cast, no need to check 3664 SDValue N = getValue(I.getOperand(0)); 3665 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3666 I.getType()); 3667 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N)); 3668 } 3669 3670 void SelectionDAGBuilder::visitFPToUI(const User &I) { 3671 // FPToUI is never a no-op cast, no need to check 3672 SDValue N = getValue(I.getOperand(0)); 3673 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3674 I.getType()); 3675 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N)); 3676 } 3677 3678 void SelectionDAGBuilder::visitFPToSI(const User &I) { 3679 // FPToSI is never a no-op cast, no need to check 3680 SDValue N = getValue(I.getOperand(0)); 3681 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3682 I.getType()); 3683 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N)); 3684 } 3685 3686 void SelectionDAGBuilder::visitUIToFP(const User &I) { 3687 // UIToFP is never a no-op cast, no need to check 3688 SDValue N = getValue(I.getOperand(0)); 3689 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3690 I.getType()); 3691 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N)); 3692 } 3693 3694 void SelectionDAGBuilder::visitSIToFP(const User &I) { 3695 // SIToFP is never a no-op cast, no need to check 3696 SDValue N = getValue(I.getOperand(0)); 3697 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3698 I.getType()); 3699 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N)); 3700 } 3701 3702 void SelectionDAGBuilder::visitPtrToInt(const User &I) { 3703 // What to do depends on the size of the integer and the size of the pointer. 3704 // We can either truncate, zero extend, or no-op, accordingly. 3705 SDValue N = getValue(I.getOperand(0)); 3706 auto &TLI = DAG.getTargetLoweringInfo(); 3707 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3708 I.getType()); 3709 EVT PtrMemVT = 3710 TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType()); 3711 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT); 3712 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT); 3713 setValue(&I, N); 3714 } 3715 3716 void SelectionDAGBuilder::visitIntToPtr(const User &I) { 3717 // What to do depends on the size of the integer and the size of the pointer. 3718 // We can either truncate, zero extend, or no-op, accordingly. 3719 SDValue N = getValue(I.getOperand(0)); 3720 auto &TLI = DAG.getTargetLoweringInfo(); 3721 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 3722 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType()); 3723 N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT); 3724 N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT); 3725 setValue(&I, N); 3726 } 3727 3728 void SelectionDAGBuilder::visitBitCast(const User &I) { 3729 SDValue N = getValue(I.getOperand(0)); 3730 SDLoc dl = getCurSDLoc(); 3731 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 3732 I.getType()); 3733 3734 // BitCast assures us that source and destination are the same size so this is 3735 // either a BITCAST or a no-op. 3736 if (DestVT != N.getValueType()) 3737 setValue(&I, DAG.getNode(ISD::BITCAST, dl, 3738 DestVT, N)); // convert types. 3739 // Check if the original LLVM IR Operand was a ConstantInt, because getValue() 3740 // might fold any kind of constant expression to an integer constant and that 3741 // is not what we are looking for. Only recognize a bitcast of a genuine 3742 // constant integer as an opaque constant. 3743 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0))) 3744 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false, 3745 /*isOpaque*/true)); 3746 else 3747 setValue(&I, N); // noop cast. 3748 } 3749 3750 void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) { 3751 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3752 const Value *SV = I.getOperand(0); 3753 SDValue N = getValue(SV); 3754 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 3755 3756 unsigned SrcAS = SV->getType()->getPointerAddressSpace(); 3757 unsigned DestAS = I.getType()->getPointerAddressSpace(); 3758 3759 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS)) 3760 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS); 3761 3762 setValue(&I, N); 3763 } 3764 3765 void SelectionDAGBuilder::visitInsertElement(const User &I) { 3766 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3767 SDValue InVec = getValue(I.getOperand(0)); 3768 SDValue InVal = getValue(I.getOperand(1)); 3769 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(), 3770 TLI.getVectorIdxTy(DAG.getDataLayout())); 3771 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(), 3772 TLI.getValueType(DAG.getDataLayout(), I.getType()), 3773 InVec, InVal, InIdx)); 3774 } 3775 3776 void SelectionDAGBuilder::visitExtractElement(const User &I) { 3777 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3778 SDValue InVec = getValue(I.getOperand(0)); 3779 SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(), 3780 TLI.getVectorIdxTy(DAG.getDataLayout())); 3781 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(), 3782 TLI.getValueType(DAG.getDataLayout(), I.getType()), 3783 InVec, InIdx)); 3784 } 3785 3786 void SelectionDAGBuilder::visitShuffleVector(const User &I) { 3787 SDValue Src1 = getValue(I.getOperand(0)); 3788 SDValue Src2 = getValue(I.getOperand(1)); 3789 ArrayRef<int> Mask; 3790 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I)) 3791 Mask = SVI->getShuffleMask(); 3792 else 3793 Mask = cast<ConstantExpr>(I).getShuffleMask(); 3794 SDLoc DL = getCurSDLoc(); 3795 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3796 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 3797 EVT SrcVT = Src1.getValueType(); 3798 3799 if (all_of(Mask, [](int Elem) { return Elem == 0; }) && 3800 VT.isScalableVector()) { 3801 // Canonical splat form of first element of first input vector. 3802 SDValue FirstElt = 3803 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1, 3804 DAG.getVectorIdxConstant(0, DL)); 3805 setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt)); 3806 return; 3807 } 3808 3809 // For now, we only handle splats for scalable vectors. 3810 // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation 3811 // for targets that support a SPLAT_VECTOR for non-scalable vector types. 3812 assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle"); 3813 3814 unsigned SrcNumElts = SrcVT.getVectorNumElements(); 3815 unsigned MaskNumElts = Mask.size(); 3816 3817 if (SrcNumElts == MaskNumElts) { 3818 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask)); 3819 return; 3820 } 3821 3822 // Normalize the shuffle vector since mask and vector length don't match. 3823 if (SrcNumElts < MaskNumElts) { 3824 // Mask is longer than the source vectors. We can use concatenate vector to 3825 // make the mask and vectors lengths match. 3826 3827 if (MaskNumElts % SrcNumElts == 0) { 3828 // Mask length is a multiple of the source vector length. 3829 // Check if the shuffle is some kind of concatenation of the input 3830 // vectors. 3831 unsigned NumConcat = MaskNumElts / SrcNumElts; 3832 bool IsConcat = true; 3833 SmallVector<int, 8> ConcatSrcs(NumConcat, -1); 3834 for (unsigned i = 0; i != MaskNumElts; ++i) { 3835 int Idx = Mask[i]; 3836 if (Idx < 0) 3837 continue; 3838 // Ensure the indices in each SrcVT sized piece are sequential and that 3839 // the same source is used for the whole piece. 3840 if ((Idx % SrcNumElts != (i % SrcNumElts)) || 3841 (ConcatSrcs[i / SrcNumElts] >= 0 && 3842 ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) { 3843 IsConcat = false; 3844 break; 3845 } 3846 // Remember which source this index came from. 3847 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts; 3848 } 3849 3850 // The shuffle is concatenating multiple vectors together. Just emit 3851 // a CONCAT_VECTORS operation. 3852 if (IsConcat) { 3853 SmallVector<SDValue, 8> ConcatOps; 3854 for (auto Src : ConcatSrcs) { 3855 if (Src < 0) 3856 ConcatOps.push_back(DAG.getUNDEF(SrcVT)); 3857 else if (Src == 0) 3858 ConcatOps.push_back(Src1); 3859 else 3860 ConcatOps.push_back(Src2); 3861 } 3862 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps)); 3863 return; 3864 } 3865 } 3866 3867 unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts); 3868 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts; 3869 EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), 3870 PaddedMaskNumElts); 3871 3872 // Pad both vectors with undefs to make them the same length as the mask. 3873 SDValue UndefVal = DAG.getUNDEF(SrcVT); 3874 3875 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal); 3876 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal); 3877 MOps1[0] = Src1; 3878 MOps2[0] = Src2; 3879 3880 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1); 3881 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2); 3882 3883 // Readjust mask for new input vector length. 3884 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1); 3885 for (unsigned i = 0; i != MaskNumElts; ++i) { 3886 int Idx = Mask[i]; 3887 if (Idx >= (int)SrcNumElts) 3888 Idx -= SrcNumElts - PaddedMaskNumElts; 3889 MappedOps[i] = Idx; 3890 } 3891 3892 SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps); 3893 3894 // If the concatenated vector was padded, extract a subvector with the 3895 // correct number of elements. 3896 if (MaskNumElts != PaddedMaskNumElts) 3897 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result, 3898 DAG.getVectorIdxConstant(0, DL)); 3899 3900 setValue(&I, Result); 3901 return; 3902 } 3903 3904 if (SrcNumElts > MaskNumElts) { 3905 // Analyze the access pattern of the vector to see if we can extract 3906 // two subvectors and do the shuffle. 3907 int StartIdx[2] = { -1, -1 }; // StartIdx to extract from 3908 bool CanExtract = true; 3909 for (int Idx : Mask) { 3910 unsigned Input = 0; 3911 if (Idx < 0) 3912 continue; 3913 3914 if (Idx >= (int)SrcNumElts) { 3915 Input = 1; 3916 Idx -= SrcNumElts; 3917 } 3918 3919 // If all the indices come from the same MaskNumElts sized portion of 3920 // the sources we can use extract. Also make sure the extract wouldn't 3921 // extract past the end of the source. 3922 int NewStartIdx = alignDown(Idx, MaskNumElts); 3923 if (NewStartIdx + MaskNumElts > SrcNumElts || 3924 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx)) 3925 CanExtract = false; 3926 // Make sure we always update StartIdx as we use it to track if all 3927 // elements are undef. 3928 StartIdx[Input] = NewStartIdx; 3929 } 3930 3931 if (StartIdx[0] < 0 && StartIdx[1] < 0) { 3932 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used. 3933 return; 3934 } 3935 if (CanExtract) { 3936 // Extract appropriate subvector and generate a vector shuffle 3937 for (unsigned Input = 0; Input < 2; ++Input) { 3938 SDValue &Src = Input == 0 ? Src1 : Src2; 3939 if (StartIdx[Input] < 0) 3940 Src = DAG.getUNDEF(VT); 3941 else { 3942 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src, 3943 DAG.getVectorIdxConstant(StartIdx[Input], DL)); 3944 } 3945 } 3946 3947 // Calculate new mask. 3948 SmallVector<int, 8> MappedOps(Mask); 3949 for (int &Idx : MappedOps) { 3950 if (Idx >= (int)SrcNumElts) 3951 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts; 3952 else if (Idx >= 0) 3953 Idx -= StartIdx[0]; 3954 } 3955 3956 setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps)); 3957 return; 3958 } 3959 } 3960 3961 // We can't use either concat vectors or extract subvectors so fall back to 3962 // replacing the shuffle with extract and build vector. 3963 // to insert and build vector. 3964 EVT EltVT = VT.getVectorElementType(); 3965 SmallVector<SDValue,8> Ops; 3966 for (int Idx : Mask) { 3967 SDValue Res; 3968 3969 if (Idx < 0) { 3970 Res = DAG.getUNDEF(EltVT); 3971 } else { 3972 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2; 3973 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts; 3974 3975 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src, 3976 DAG.getVectorIdxConstant(Idx, DL)); 3977 } 3978 3979 Ops.push_back(Res); 3980 } 3981 3982 setValue(&I, DAG.getBuildVector(VT, DL, Ops)); 3983 } 3984 3985 void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) { 3986 ArrayRef<unsigned> Indices = I.getIndices(); 3987 const Value *Op0 = I.getOperand(0); 3988 const Value *Op1 = I.getOperand(1); 3989 Type *AggTy = I.getType(); 3990 Type *ValTy = Op1->getType(); 3991 bool IntoUndef = isa<UndefValue>(Op0); 3992 bool FromUndef = isa<UndefValue>(Op1); 3993 3994 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices); 3995 3996 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 3997 SmallVector<EVT, 4> AggValueVTs; 3998 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs); 3999 SmallVector<EVT, 4> ValValueVTs; 4000 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs); 4001 4002 unsigned NumAggValues = AggValueVTs.size(); 4003 unsigned NumValValues = ValValueVTs.size(); 4004 SmallVector<SDValue, 4> Values(NumAggValues); 4005 4006 // Ignore an insertvalue that produces an empty object 4007 if (!NumAggValues) { 4008 setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); 4009 return; 4010 } 4011 4012 SDValue Agg = getValue(Op0); 4013 unsigned i = 0; 4014 // Copy the beginning value(s) from the original aggregate. 4015 for (; i != LinearIndex; ++i) 4016 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : 4017 SDValue(Agg.getNode(), Agg.getResNo() + i); 4018 // Copy values from the inserted value(s). 4019 if (NumValValues) { 4020 SDValue Val = getValue(Op1); 4021 for (; i != LinearIndex + NumValValues; ++i) 4022 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) : 4023 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex); 4024 } 4025 // Copy remaining value(s) from the original aggregate. 4026 for (; i != NumAggValues; ++i) 4027 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) : 4028 SDValue(Agg.getNode(), Agg.getResNo() + i); 4029 4030 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 4031 DAG.getVTList(AggValueVTs), Values)); 4032 } 4033 4034 void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) { 4035 ArrayRef<unsigned> Indices = I.getIndices(); 4036 const Value *Op0 = I.getOperand(0); 4037 Type *AggTy = Op0->getType(); 4038 Type *ValTy = I.getType(); 4039 bool OutOfUndef = isa<UndefValue>(Op0); 4040 4041 unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices); 4042 4043 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4044 SmallVector<EVT, 4> ValValueVTs; 4045 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs); 4046 4047 unsigned NumValValues = ValValueVTs.size(); 4048 4049 // Ignore a extractvalue that produces an empty object 4050 if (!NumValValues) { 4051 setValue(&I, DAG.getUNDEF(MVT(MVT::Other))); 4052 return; 4053 } 4054 4055 SmallVector<SDValue, 4> Values(NumValValues); 4056 4057 SDValue Agg = getValue(Op0); 4058 // Copy out the selected value(s). 4059 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i) 4060 Values[i - LinearIndex] = 4061 OutOfUndef ? 4062 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) : 4063 SDValue(Agg.getNode(), Agg.getResNo() + i); 4064 4065 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 4066 DAG.getVTList(ValValueVTs), Values)); 4067 } 4068 4069 void SelectionDAGBuilder::visitGetElementPtr(const User &I) { 4070 Value *Op0 = I.getOperand(0); 4071 // Note that the pointer operand may be a vector of pointers. Take the scalar 4072 // element which holds a pointer. 4073 unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace(); 4074 SDValue N = getValue(Op0); 4075 SDLoc dl = getCurSDLoc(); 4076 auto &TLI = DAG.getTargetLoweringInfo(); 4077 4078 // Normalize Vector GEP - all scalar operands should be converted to the 4079 // splat vector. 4080 bool IsVectorGEP = I.getType()->isVectorTy(); 4081 ElementCount VectorElementCount = 4082 IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount() 4083 : ElementCount::getFixed(0); 4084 4085 if (IsVectorGEP && !N.getValueType().isVector()) { 4086 LLVMContext &Context = *DAG.getContext(); 4087 EVT VT = EVT::getVectorVT(Context, N.getValueType(), VectorElementCount); 4088 N = DAG.getSplat(VT, dl, N); 4089 } 4090 4091 for (gep_type_iterator GTI = gep_type_begin(&I), E = gep_type_end(&I); 4092 GTI != E; ++GTI) { 4093 const Value *Idx = GTI.getOperand(); 4094 if (StructType *StTy = GTI.getStructTypeOrNull()) { 4095 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue(); 4096 if (Field) { 4097 // N = N + Offset 4098 uint64_t Offset = 4099 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field); 4100 4101 // In an inbounds GEP with an offset that is nonnegative even when 4102 // interpreted as signed, assume there is no unsigned overflow. 4103 SDNodeFlags Flags; 4104 if (int64_t(Offset) >= 0 && cast<GEPOperator>(I).isInBounds()) 4105 Flags.setNoUnsignedWrap(true); 4106 4107 N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, 4108 DAG.getConstant(Offset, dl, N.getValueType()), Flags); 4109 } 4110 } else { 4111 // IdxSize is the width of the arithmetic according to IR semantics. 4112 // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth 4113 // (and fix up the result later). 4114 unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS); 4115 MVT IdxTy = MVT::getIntegerVT(IdxSize); 4116 TypeSize ElementSize = 4117 GTI.getSequentialElementStride(DAG.getDataLayout()); 4118 // We intentionally mask away the high bits here; ElementSize may not 4119 // fit in IdxTy. 4120 APInt ElementMul(IdxSize, ElementSize.getKnownMinValue()); 4121 bool ElementScalable = ElementSize.isScalable(); 4122 4123 // If this is a scalar constant or a splat vector of constants, 4124 // handle it quickly. 4125 const auto *C = dyn_cast<Constant>(Idx); 4126 if (C && isa<VectorType>(C->getType())) 4127 C = C->getSplatValue(); 4128 4129 const auto *CI = dyn_cast_or_null<ConstantInt>(C); 4130 if (CI && CI->isZero()) 4131 continue; 4132 if (CI && !ElementScalable) { 4133 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize); 4134 LLVMContext &Context = *DAG.getContext(); 4135 SDValue OffsVal; 4136 if (IsVectorGEP) 4137 OffsVal = DAG.getConstant( 4138 Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount)); 4139 else 4140 OffsVal = DAG.getConstant(Offs, dl, IdxTy); 4141 4142 // In an inbounds GEP with an offset that is nonnegative even when 4143 // interpreted as signed, assume there is no unsigned overflow. 4144 SDNodeFlags Flags; 4145 if (Offs.isNonNegative() && cast<GEPOperator>(I).isInBounds()) 4146 Flags.setNoUnsignedWrap(true); 4147 4148 OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType()); 4149 4150 N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, OffsVal, Flags); 4151 continue; 4152 } 4153 4154 // N = N + Idx * ElementMul; 4155 SDValue IdxN = getValue(Idx); 4156 4157 if (!IdxN.getValueType().isVector() && IsVectorGEP) { 4158 EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(), 4159 VectorElementCount); 4160 IdxN = DAG.getSplat(VT, dl, IdxN); 4161 } 4162 4163 // If the index is smaller or larger than intptr_t, truncate or extend 4164 // it. 4165 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType()); 4166 4167 if (ElementScalable) { 4168 EVT VScaleTy = N.getValueType().getScalarType(); 4169 SDValue VScale = DAG.getNode( 4170 ISD::VSCALE, dl, VScaleTy, 4171 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy)); 4172 if (IsVectorGEP) 4173 VScale = DAG.getSplatVector(N.getValueType(), dl, VScale); 4174 IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale); 4175 } else { 4176 // If this is a multiply by a power of two, turn it into a shl 4177 // immediately. This is a very common case. 4178 if (ElementMul != 1) { 4179 if (ElementMul.isPowerOf2()) { 4180 unsigned Amt = ElementMul.logBase2(); 4181 IdxN = DAG.getNode(ISD::SHL, dl, 4182 N.getValueType(), IdxN, 4183 DAG.getConstant(Amt, dl, IdxN.getValueType())); 4184 } else { 4185 SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl, 4186 IdxN.getValueType()); 4187 IdxN = DAG.getNode(ISD::MUL, dl, 4188 N.getValueType(), IdxN, Scale); 4189 } 4190 } 4191 } 4192 4193 N = DAG.getNode(ISD::ADD, dl, 4194 N.getValueType(), N, IdxN); 4195 } 4196 } 4197 4198 MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS); 4199 MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS); 4200 if (IsVectorGEP) { 4201 PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount); 4202 PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount); 4203 } 4204 4205 if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds()) 4206 N = DAG.getPtrExtendInReg(N, dl, PtrMemTy); 4207 4208 setValue(&I, N); 4209 } 4210 4211 void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) { 4212 // If this is a fixed sized alloca in the entry block of the function, 4213 // allocate it statically on the stack. 4214 if (FuncInfo.StaticAllocaMap.count(&I)) 4215 return; // getValue will auto-populate this. 4216 4217 SDLoc dl = getCurSDLoc(); 4218 Type *Ty = I.getAllocatedType(); 4219 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4220 auto &DL = DAG.getDataLayout(); 4221 TypeSize TySize = DL.getTypeAllocSize(Ty); 4222 MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign()); 4223 4224 SDValue AllocSize = getValue(I.getArraySize()); 4225 4226 EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace()); 4227 if (AllocSize.getValueType() != IntPtr) 4228 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr); 4229 4230 if (TySize.isScalable()) 4231 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize, 4232 DAG.getVScale(dl, IntPtr, 4233 APInt(IntPtr.getScalarSizeInBits(), 4234 TySize.getKnownMinValue()))); 4235 else { 4236 SDValue TySizeValue = 4237 DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64)); 4238 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize, 4239 DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr)); 4240 } 4241 4242 // Handle alignment. If the requested alignment is less than or equal to 4243 // the stack alignment, ignore it. If the size is greater than or equal to 4244 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node. 4245 Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign(); 4246 if (*Alignment <= StackAlign) 4247 Alignment = std::nullopt; 4248 4249 const uint64_t StackAlignMask = StackAlign.value() - 1U; 4250 // Round the size of the allocation up to the stack alignment size 4251 // by add SA-1 to the size. This doesn't overflow because we're computing 4252 // an address inside an alloca. 4253 SDNodeFlags Flags; 4254 Flags.setNoUnsignedWrap(true); 4255 AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize, 4256 DAG.getConstant(StackAlignMask, dl, IntPtr), Flags); 4257 4258 // Mask out the low bits for alignment purposes. 4259 AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize, 4260 DAG.getConstant(~StackAlignMask, dl, IntPtr)); 4261 4262 SDValue Ops[] = { 4263 getRoot(), AllocSize, 4264 DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)}; 4265 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other); 4266 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops); 4267 setValue(&I, DSA); 4268 DAG.setRoot(DSA.getValue(1)); 4269 4270 assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects()); 4271 } 4272 4273 static const MDNode *getRangeMetadata(const Instruction &I) { 4274 // If !noundef is not present, then !range violation results in a poison 4275 // value rather than immediate undefined behavior. In theory, transferring 4276 // these annotations to SDAG is fine, but in practice there are key SDAG 4277 // transforms that are known not to be poison-safe, such as folding logical 4278 // and/or to bitwise and/or. For now, only transfer !range if !noundef is 4279 // also present. 4280 if (!I.hasMetadata(LLVMContext::MD_noundef)) 4281 return nullptr; 4282 return I.getMetadata(LLVMContext::MD_range); 4283 } 4284 4285 void SelectionDAGBuilder::visitLoad(const LoadInst &I) { 4286 if (I.isAtomic()) 4287 return visitAtomicLoad(I); 4288 4289 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4290 const Value *SV = I.getOperand(0); 4291 if (TLI.supportSwiftError()) { 4292 // Swifterror values can come from either a function parameter with 4293 // swifterror attribute or an alloca with swifterror attribute. 4294 if (const Argument *Arg = dyn_cast<Argument>(SV)) { 4295 if (Arg->hasSwiftErrorAttr()) 4296 return visitLoadFromSwiftError(I); 4297 } 4298 4299 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) { 4300 if (Alloca->isSwiftError()) 4301 return visitLoadFromSwiftError(I); 4302 } 4303 } 4304 4305 SDValue Ptr = getValue(SV); 4306 4307 Type *Ty = I.getType(); 4308 SmallVector<EVT, 4> ValueVTs, MemVTs; 4309 SmallVector<TypeSize, 4> Offsets; 4310 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets, 0); 4311 unsigned NumValues = ValueVTs.size(); 4312 if (NumValues == 0) 4313 return; 4314 4315 Align Alignment = I.getAlign(); 4316 AAMDNodes AAInfo = I.getAAMetadata(); 4317 const MDNode *Ranges = getRangeMetadata(I); 4318 bool isVolatile = I.isVolatile(); 4319 MachineMemOperand::Flags MMOFlags = 4320 TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo); 4321 4322 SDValue Root; 4323 bool ConstantMemory = false; 4324 if (isVolatile) 4325 // Serialize volatile loads with other side effects. 4326 Root = getRoot(); 4327 else if (NumValues > MaxParallelChains) 4328 Root = getMemoryRoot(); 4329 else if (AA && 4330 AA->pointsToConstantMemory(MemoryLocation( 4331 SV, 4332 LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)), 4333 AAInfo))) { 4334 // Do not serialize (non-volatile) loads of constant memory with anything. 4335 Root = DAG.getEntryNode(); 4336 ConstantMemory = true; 4337 MMOFlags |= MachineMemOperand::MOInvariant; 4338 } else { 4339 // Do not serialize non-volatile loads against each other. 4340 Root = DAG.getRoot(); 4341 } 4342 4343 SDLoc dl = getCurSDLoc(); 4344 4345 if (isVolatile) 4346 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG); 4347 4348 SmallVector<SDValue, 4> Values(NumValues); 4349 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues)); 4350 4351 unsigned ChainI = 0; 4352 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { 4353 // Serializing loads here may result in excessive register pressure, and 4354 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling 4355 // could recover a bit by hoisting nodes upward in the chain by recognizing 4356 // they are side-effect free or do not alias. The optimizer should really 4357 // avoid this case by converting large object/array copies to llvm.memcpy 4358 // (MaxParallelChains should always remain as failsafe). 4359 if (ChainI == MaxParallelChains) { 4360 assert(PendingLoads.empty() && "PendingLoads must be serialized first"); 4361 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 4362 ArrayRef(Chains.data(), ChainI)); 4363 Root = Chain; 4364 ChainI = 0; 4365 } 4366 4367 // TODO: MachinePointerInfo only supports a fixed length offset. 4368 MachinePointerInfo PtrInfo = 4369 !Offsets[i].isScalable() || Offsets[i].isZero() 4370 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue()) 4371 : MachinePointerInfo(); 4372 4373 SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]); 4374 SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment, 4375 MMOFlags, AAInfo, Ranges); 4376 Chains[ChainI] = L.getValue(1); 4377 4378 if (MemVTs[i] != ValueVTs[i]) 4379 L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]); 4380 4381 Values[i] = L; 4382 } 4383 4384 if (!ConstantMemory) { 4385 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 4386 ArrayRef(Chains.data(), ChainI)); 4387 if (isVolatile) 4388 DAG.setRoot(Chain); 4389 else 4390 PendingLoads.push_back(Chain); 4391 } 4392 4393 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl, 4394 DAG.getVTList(ValueVTs), Values)); 4395 } 4396 4397 void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) { 4398 assert(DAG.getTargetLoweringInfo().supportSwiftError() && 4399 "call visitStoreToSwiftError when backend supports swifterror"); 4400 4401 SmallVector<EVT, 4> ValueVTs; 4402 SmallVector<uint64_t, 4> Offsets; 4403 const Value *SrcV = I.getOperand(0); 4404 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), 4405 SrcV->getType(), ValueVTs, &Offsets, 0); 4406 assert(ValueVTs.size() == 1 && Offsets[0] == 0 && 4407 "expect a single EVT for swifterror"); 4408 4409 SDValue Src = getValue(SrcV); 4410 // Create a virtual register, then update the virtual register. 4411 Register VReg = 4412 SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand()); 4413 // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue 4414 // Chain can be getRoot or getControlRoot. 4415 SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg, 4416 SDValue(Src.getNode(), Src.getResNo())); 4417 DAG.setRoot(CopyNode); 4418 } 4419 4420 void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) { 4421 assert(DAG.getTargetLoweringInfo().supportSwiftError() && 4422 "call visitLoadFromSwiftError when backend supports swifterror"); 4423 4424 assert(!I.isVolatile() && 4425 !I.hasMetadata(LLVMContext::MD_nontemporal) && 4426 !I.hasMetadata(LLVMContext::MD_invariant_load) && 4427 "Support volatile, non temporal, invariant for load_from_swift_error"); 4428 4429 const Value *SV = I.getOperand(0); 4430 Type *Ty = I.getType(); 4431 assert( 4432 (!AA || 4433 !AA->pointsToConstantMemory(MemoryLocation( 4434 SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)), 4435 I.getAAMetadata()))) && 4436 "load_from_swift_error should not be constant memory"); 4437 4438 SmallVector<EVT, 4> ValueVTs; 4439 SmallVector<uint64_t, 4> Offsets; 4440 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty, 4441 ValueVTs, &Offsets, 0); 4442 assert(ValueVTs.size() == 1 && Offsets[0] == 0 && 4443 "expect a single EVT for swifterror"); 4444 4445 // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT 4446 SDValue L = DAG.getCopyFromReg( 4447 getRoot(), getCurSDLoc(), 4448 SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]); 4449 4450 setValue(&I, L); 4451 } 4452 4453 void SelectionDAGBuilder::visitStore(const StoreInst &I) { 4454 if (I.isAtomic()) 4455 return visitAtomicStore(I); 4456 4457 const Value *SrcV = I.getOperand(0); 4458 const Value *PtrV = I.getOperand(1); 4459 4460 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4461 if (TLI.supportSwiftError()) { 4462 // Swifterror values can come from either a function parameter with 4463 // swifterror attribute or an alloca with swifterror attribute. 4464 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) { 4465 if (Arg->hasSwiftErrorAttr()) 4466 return visitStoreToSwiftError(I); 4467 } 4468 4469 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) { 4470 if (Alloca->isSwiftError()) 4471 return visitStoreToSwiftError(I); 4472 } 4473 } 4474 4475 SmallVector<EVT, 4> ValueVTs, MemVTs; 4476 SmallVector<TypeSize, 4> Offsets; 4477 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), 4478 SrcV->getType(), ValueVTs, &MemVTs, &Offsets, 0); 4479 unsigned NumValues = ValueVTs.size(); 4480 if (NumValues == 0) 4481 return; 4482 4483 // Get the lowered operands. Note that we do this after 4484 // checking if NumResults is zero, because with zero results 4485 // the operands won't have values in the map. 4486 SDValue Src = getValue(SrcV); 4487 SDValue Ptr = getValue(PtrV); 4488 4489 SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot(); 4490 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues)); 4491 SDLoc dl = getCurSDLoc(); 4492 Align Alignment = I.getAlign(); 4493 AAMDNodes AAInfo = I.getAAMetadata(); 4494 4495 auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout()); 4496 4497 unsigned ChainI = 0; 4498 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) { 4499 // See visitLoad comments. 4500 if (ChainI == MaxParallelChains) { 4501 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 4502 ArrayRef(Chains.data(), ChainI)); 4503 Root = Chain; 4504 ChainI = 0; 4505 } 4506 4507 // TODO: MachinePointerInfo only supports a fixed length offset. 4508 MachinePointerInfo PtrInfo = 4509 !Offsets[i].isScalable() || Offsets[i].isZero() 4510 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue()) 4511 : MachinePointerInfo(); 4512 4513 SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]); 4514 SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i); 4515 if (MemVTs[i] != ValueVTs[i]) 4516 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]); 4517 SDValue St = 4518 DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo); 4519 Chains[ChainI] = St; 4520 } 4521 4522 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 4523 ArrayRef(Chains.data(), ChainI)); 4524 setValue(&I, StoreNode); 4525 DAG.setRoot(StoreNode); 4526 } 4527 4528 void SelectionDAGBuilder::visitMaskedStore(const CallInst &I, 4529 bool IsCompressing) { 4530 SDLoc sdl = getCurSDLoc(); 4531 4532 auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0, 4533 MaybeAlign &Alignment) { 4534 // llvm.masked.store.*(Src0, Ptr, alignment, Mask) 4535 Src0 = I.getArgOperand(0); 4536 Ptr = I.getArgOperand(1); 4537 Alignment = cast<ConstantInt>(I.getArgOperand(2))->getMaybeAlignValue(); 4538 Mask = I.getArgOperand(3); 4539 }; 4540 auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0, 4541 MaybeAlign &Alignment) { 4542 // llvm.masked.compressstore.*(Src0, Ptr, Mask) 4543 Src0 = I.getArgOperand(0); 4544 Ptr = I.getArgOperand(1); 4545 Mask = I.getArgOperand(2); 4546 Alignment = std::nullopt; 4547 }; 4548 4549 Value *PtrOperand, *MaskOperand, *Src0Operand; 4550 MaybeAlign Alignment; 4551 if (IsCompressing) 4552 getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment); 4553 else 4554 getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment); 4555 4556 SDValue Ptr = getValue(PtrOperand); 4557 SDValue Src0 = getValue(Src0Operand); 4558 SDValue Mask = getValue(MaskOperand); 4559 SDValue Offset = DAG.getUNDEF(Ptr.getValueType()); 4560 4561 EVT VT = Src0.getValueType(); 4562 if (!Alignment) 4563 Alignment = DAG.getEVTAlign(VT); 4564 4565 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 4566 MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore, 4567 MemoryLocation::UnknownSize, *Alignment, I.getAAMetadata()); 4568 SDValue StoreNode = 4569 DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask, VT, MMO, 4570 ISD::UNINDEXED, false /* Truncating */, IsCompressing); 4571 DAG.setRoot(StoreNode); 4572 setValue(&I, StoreNode); 4573 } 4574 4575 // Get a uniform base for the Gather/Scatter intrinsic. 4576 // The first argument of the Gather/Scatter intrinsic is a vector of pointers. 4577 // We try to represent it as a base pointer + vector of indices. 4578 // Usually, the vector of pointers comes from a 'getelementptr' instruction. 4579 // The first operand of the GEP may be a single pointer or a vector of pointers 4580 // Example: 4581 // %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind 4582 // or 4583 // %gep.ptr = getelementptr i32, i32* %ptr, <8 x i32> %ind 4584 // %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, .. 4585 // 4586 // When the first GEP operand is a single pointer - it is the uniform base we 4587 // are looking for. If first operand of the GEP is a splat vector - we 4588 // extract the splat value and use it as a uniform base. 4589 // In all other cases the function returns 'false'. 4590 static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index, 4591 ISD::MemIndexType &IndexType, SDValue &Scale, 4592 SelectionDAGBuilder *SDB, const BasicBlock *CurBB, 4593 uint64_t ElemSize) { 4594 SelectionDAG& DAG = SDB->DAG; 4595 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4596 const DataLayout &DL = DAG.getDataLayout(); 4597 4598 assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type"); 4599 4600 // Handle splat constant pointer. 4601 if (auto *C = dyn_cast<Constant>(Ptr)) { 4602 C = C->getSplatValue(); 4603 if (!C) 4604 return false; 4605 4606 Base = SDB->getValue(C); 4607 4608 ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount(); 4609 EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts); 4610 Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT); 4611 IndexType = ISD::SIGNED_SCALED; 4612 Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL)); 4613 return true; 4614 } 4615 4616 const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr); 4617 if (!GEP || GEP->getParent() != CurBB) 4618 return false; 4619 4620 if (GEP->getNumOperands() != 2) 4621 return false; 4622 4623 const Value *BasePtr = GEP->getPointerOperand(); 4624 const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1); 4625 4626 // Make sure the base is scalar and the index is a vector. 4627 if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy()) 4628 return false; 4629 4630 TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType()); 4631 if (ScaleVal.isScalable()) 4632 return false; 4633 4634 // Target may not support the required addressing mode. 4635 if (ScaleVal != 1 && 4636 !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize)) 4637 return false; 4638 4639 Base = SDB->getValue(BasePtr); 4640 Index = SDB->getValue(IndexVal); 4641 IndexType = ISD::SIGNED_SCALED; 4642 4643 Scale = 4644 DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL)); 4645 return true; 4646 } 4647 4648 void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) { 4649 SDLoc sdl = getCurSDLoc(); 4650 4651 // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask) 4652 const Value *Ptr = I.getArgOperand(1); 4653 SDValue Src0 = getValue(I.getArgOperand(0)); 4654 SDValue Mask = getValue(I.getArgOperand(3)); 4655 EVT VT = Src0.getValueType(); 4656 Align Alignment = cast<ConstantInt>(I.getArgOperand(2)) 4657 ->getMaybeAlignValue() 4658 .value_or(DAG.getEVTAlign(VT.getScalarType())); 4659 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4660 4661 SDValue Base; 4662 SDValue Index; 4663 ISD::MemIndexType IndexType; 4664 SDValue Scale; 4665 bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this, 4666 I.getParent(), VT.getScalarStoreSize()); 4667 4668 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace(); 4669 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 4670 MachinePointerInfo(AS), MachineMemOperand::MOStore, 4671 // TODO: Make MachineMemOperands aware of scalable 4672 // vectors. 4673 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata()); 4674 if (!UniformBase) { 4675 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())); 4676 Index = getValue(Ptr); 4677 IndexType = ISD::SIGNED_SCALED; 4678 Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout())); 4679 } 4680 4681 EVT IdxVT = Index.getValueType(); 4682 EVT EltTy = IdxVT.getVectorElementType(); 4683 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) { 4684 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy); 4685 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index); 4686 } 4687 4688 SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale }; 4689 SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl, 4690 Ops, MMO, IndexType, false); 4691 DAG.setRoot(Scatter); 4692 setValue(&I, Scatter); 4693 } 4694 4695 void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) { 4696 SDLoc sdl = getCurSDLoc(); 4697 4698 auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0, 4699 MaybeAlign &Alignment) { 4700 // @llvm.masked.load.*(Ptr, alignment, Mask, Src0) 4701 Ptr = I.getArgOperand(0); 4702 Alignment = cast<ConstantInt>(I.getArgOperand(1))->getMaybeAlignValue(); 4703 Mask = I.getArgOperand(2); 4704 Src0 = I.getArgOperand(3); 4705 }; 4706 auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0, 4707 MaybeAlign &Alignment) { 4708 // @llvm.masked.expandload.*(Ptr, Mask, Src0) 4709 Ptr = I.getArgOperand(0); 4710 Alignment = std::nullopt; 4711 Mask = I.getArgOperand(1); 4712 Src0 = I.getArgOperand(2); 4713 }; 4714 4715 Value *PtrOperand, *MaskOperand, *Src0Operand; 4716 MaybeAlign Alignment; 4717 if (IsExpanding) 4718 getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment); 4719 else 4720 getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment); 4721 4722 SDValue Ptr = getValue(PtrOperand); 4723 SDValue Src0 = getValue(Src0Operand); 4724 SDValue Mask = getValue(MaskOperand); 4725 SDValue Offset = DAG.getUNDEF(Ptr.getValueType()); 4726 4727 EVT VT = Src0.getValueType(); 4728 if (!Alignment) 4729 Alignment = DAG.getEVTAlign(VT); 4730 4731 AAMDNodes AAInfo = I.getAAMetadata(); 4732 const MDNode *Ranges = getRangeMetadata(I); 4733 4734 // Do not serialize masked loads of constant memory with anything. 4735 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo); 4736 bool AddToChain = !AA || !AA->pointsToConstantMemory(ML); 4737 4738 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode(); 4739 4740 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 4741 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad, 4742 MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges); 4743 4744 SDValue Load = 4745 DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO, 4746 ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding); 4747 if (AddToChain) 4748 PendingLoads.push_back(Load.getValue(1)); 4749 setValue(&I, Load); 4750 } 4751 4752 void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) { 4753 SDLoc sdl = getCurSDLoc(); 4754 4755 // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0) 4756 const Value *Ptr = I.getArgOperand(0); 4757 SDValue Src0 = getValue(I.getArgOperand(3)); 4758 SDValue Mask = getValue(I.getArgOperand(2)); 4759 4760 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4761 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 4762 Align Alignment = cast<ConstantInt>(I.getArgOperand(1)) 4763 ->getMaybeAlignValue() 4764 .value_or(DAG.getEVTAlign(VT.getScalarType())); 4765 4766 const MDNode *Ranges = getRangeMetadata(I); 4767 4768 SDValue Root = DAG.getRoot(); 4769 SDValue Base; 4770 SDValue Index; 4771 ISD::MemIndexType IndexType; 4772 SDValue Scale; 4773 bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this, 4774 I.getParent(), VT.getScalarStoreSize()); 4775 unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace(); 4776 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 4777 MachinePointerInfo(AS), MachineMemOperand::MOLoad, 4778 // TODO: Make MachineMemOperands aware of scalable 4779 // vectors. 4780 MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges); 4781 4782 if (!UniformBase) { 4783 Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())); 4784 Index = getValue(Ptr); 4785 IndexType = ISD::SIGNED_SCALED; 4786 Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout())); 4787 } 4788 4789 EVT IdxVT = Index.getValueType(); 4790 EVT EltTy = IdxVT.getVectorElementType(); 4791 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) { 4792 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy); 4793 Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index); 4794 } 4795 4796 SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale }; 4797 SDValue Gather = DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl, 4798 Ops, MMO, IndexType, ISD::NON_EXTLOAD); 4799 4800 PendingLoads.push_back(Gather.getValue(1)); 4801 setValue(&I, Gather); 4802 } 4803 4804 void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) { 4805 SDLoc dl = getCurSDLoc(); 4806 AtomicOrdering SuccessOrdering = I.getSuccessOrdering(); 4807 AtomicOrdering FailureOrdering = I.getFailureOrdering(); 4808 SyncScope::ID SSID = I.getSyncScopeID(); 4809 4810 SDValue InChain = getRoot(); 4811 4812 MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType(); 4813 SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other); 4814 4815 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4816 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout()); 4817 4818 MachineFunction &MF = DAG.getMachineFunction(); 4819 MachineMemOperand *MMO = MF.getMachineMemOperand( 4820 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(), 4821 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering, 4822 FailureOrdering); 4823 4824 SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, 4825 dl, MemVT, VTs, InChain, 4826 getValue(I.getPointerOperand()), 4827 getValue(I.getCompareOperand()), 4828 getValue(I.getNewValOperand()), MMO); 4829 4830 SDValue OutChain = L.getValue(2); 4831 4832 setValue(&I, L); 4833 DAG.setRoot(OutChain); 4834 } 4835 4836 void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) { 4837 SDLoc dl = getCurSDLoc(); 4838 ISD::NodeType NT; 4839 switch (I.getOperation()) { 4840 default: llvm_unreachable("Unknown atomicrmw operation"); 4841 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break; 4842 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break; 4843 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break; 4844 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break; 4845 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break; 4846 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break; 4847 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break; 4848 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break; 4849 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break; 4850 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break; 4851 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break; 4852 case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break; 4853 case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break; 4854 case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break; 4855 case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break; 4856 case AtomicRMWInst::UIncWrap: 4857 NT = ISD::ATOMIC_LOAD_UINC_WRAP; 4858 break; 4859 case AtomicRMWInst::UDecWrap: 4860 NT = ISD::ATOMIC_LOAD_UDEC_WRAP; 4861 break; 4862 } 4863 AtomicOrdering Ordering = I.getOrdering(); 4864 SyncScope::ID SSID = I.getSyncScopeID(); 4865 4866 SDValue InChain = getRoot(); 4867 4868 auto MemVT = getValue(I.getValOperand()).getSimpleValueType(); 4869 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4870 auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout()); 4871 4872 MachineFunction &MF = DAG.getMachineFunction(); 4873 MachineMemOperand *MMO = MF.getMachineMemOperand( 4874 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(), 4875 DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering); 4876 4877 SDValue L = 4878 DAG.getAtomic(NT, dl, MemVT, InChain, 4879 getValue(I.getPointerOperand()), getValue(I.getValOperand()), 4880 MMO); 4881 4882 SDValue OutChain = L.getValue(1); 4883 4884 setValue(&I, L); 4885 DAG.setRoot(OutChain); 4886 } 4887 4888 void SelectionDAGBuilder::visitFence(const FenceInst &I) { 4889 SDLoc dl = getCurSDLoc(); 4890 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4891 SDValue Ops[3]; 4892 Ops[0] = getRoot(); 4893 Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl, 4894 TLI.getFenceOperandTy(DAG.getDataLayout())); 4895 Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl, 4896 TLI.getFenceOperandTy(DAG.getDataLayout())); 4897 SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops); 4898 setValue(&I, N); 4899 DAG.setRoot(N); 4900 } 4901 4902 void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) { 4903 SDLoc dl = getCurSDLoc(); 4904 AtomicOrdering Order = I.getOrdering(); 4905 SyncScope::ID SSID = I.getSyncScopeID(); 4906 4907 SDValue InChain = getRoot(); 4908 4909 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4910 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 4911 EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType()); 4912 4913 if (!TLI.supportsUnalignedAtomics() && 4914 I.getAlign().value() < MemVT.getSizeInBits() / 8) 4915 report_fatal_error("Cannot generate unaligned atomic load"); 4916 4917 auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo); 4918 4919 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 4920 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(), 4921 I.getAlign(), AAMDNodes(), nullptr, SSID, Order); 4922 4923 InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG); 4924 4925 SDValue Ptr = getValue(I.getPointerOperand()); 4926 SDValue L = DAG.getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, MemVT, InChain, 4927 Ptr, MMO); 4928 4929 SDValue OutChain = L.getValue(1); 4930 if (MemVT != VT) 4931 L = DAG.getPtrExtOrTrunc(L, dl, VT); 4932 4933 setValue(&I, L); 4934 DAG.setRoot(OutChain); 4935 } 4936 4937 void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) { 4938 SDLoc dl = getCurSDLoc(); 4939 4940 AtomicOrdering Ordering = I.getOrdering(); 4941 SyncScope::ID SSID = I.getSyncScopeID(); 4942 4943 SDValue InChain = getRoot(); 4944 4945 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4946 EVT MemVT = 4947 TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType()); 4948 4949 if (!TLI.supportsUnalignedAtomics() && 4950 I.getAlign().value() < MemVT.getSizeInBits() / 8) 4951 report_fatal_error("Cannot generate unaligned atomic store"); 4952 4953 auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout()); 4954 4955 MachineFunction &MF = DAG.getMachineFunction(); 4956 MachineMemOperand *MMO = MF.getMachineMemOperand( 4957 MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(), 4958 I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering); 4959 4960 SDValue Val = getValue(I.getValueOperand()); 4961 if (Val.getValueType() != MemVT) 4962 Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT); 4963 SDValue Ptr = getValue(I.getPointerOperand()); 4964 4965 SDValue OutChain = 4966 DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO); 4967 4968 setValue(&I, OutChain); 4969 DAG.setRoot(OutChain); 4970 } 4971 4972 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC 4973 /// node. 4974 void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I, 4975 unsigned Intrinsic) { 4976 // Ignore the callsite's attributes. A specific call site may be marked with 4977 // readnone, but the lowering code will expect the chain based on the 4978 // definition. 4979 const Function *F = I.getCalledFunction(); 4980 bool HasChain = !F->doesNotAccessMemory(); 4981 bool OnlyLoad = HasChain && F->onlyReadsMemory(); 4982 4983 // Build the operand list. 4984 SmallVector<SDValue, 8> Ops; 4985 if (HasChain) { // If this intrinsic has side-effects, chainify it. 4986 if (OnlyLoad) { 4987 // We don't need to serialize loads against other loads. 4988 Ops.push_back(DAG.getRoot()); 4989 } else { 4990 Ops.push_back(getRoot()); 4991 } 4992 } 4993 4994 // Info is set by getTgtMemIntrinsic 4995 TargetLowering::IntrinsicInfo Info; 4996 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4997 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, 4998 DAG.getMachineFunction(), 4999 Intrinsic); 5000 5001 // Add the intrinsic ID as an integer operand if it's not a target intrinsic. 5002 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID || 5003 Info.opc == ISD::INTRINSIC_W_CHAIN) 5004 Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(), 5005 TLI.getPointerTy(DAG.getDataLayout()))); 5006 5007 // Add all operands of the call to the operand list. 5008 for (unsigned i = 0, e = I.arg_size(); i != e; ++i) { 5009 const Value *Arg = I.getArgOperand(i); 5010 if (!I.paramHasAttr(i, Attribute::ImmArg)) { 5011 Ops.push_back(getValue(Arg)); 5012 continue; 5013 } 5014 5015 // Use TargetConstant instead of a regular constant for immarg. 5016 EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true); 5017 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) { 5018 assert(CI->getBitWidth() <= 64 && 5019 "large intrinsic immediates not handled"); 5020 Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT)); 5021 } else { 5022 Ops.push_back( 5023 DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT)); 5024 } 5025 } 5026 5027 SmallVector<EVT, 4> ValueVTs; 5028 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs); 5029 5030 if (HasChain) 5031 ValueVTs.push_back(MVT::Other); 5032 5033 SDVTList VTs = DAG.getVTList(ValueVTs); 5034 5035 // Propagate fast-math-flags from IR to node(s). 5036 SDNodeFlags Flags; 5037 if (auto *FPMO = dyn_cast<FPMathOperator>(&I)) 5038 Flags.copyFMF(*FPMO); 5039 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags); 5040 5041 // Create the node. 5042 SDValue Result; 5043 // In some cases, custom collection of operands from CallInst I may be needed. 5044 TLI.CollectTargetIntrinsicOperands(I, Ops, DAG); 5045 if (IsTgtIntrinsic) { 5046 // This is target intrinsic that touches memory 5047 // 5048 // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic 5049 // didn't yield anything useful. 5050 MachinePointerInfo MPI; 5051 if (Info.ptrVal) 5052 MPI = MachinePointerInfo(Info.ptrVal, Info.offset); 5053 else if (Info.fallbackAddressSpace) 5054 MPI = MachinePointerInfo(*Info.fallbackAddressSpace); 5055 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, 5056 Info.memVT, MPI, Info.align, Info.flags, 5057 Info.size, I.getAAMetadata()); 5058 } else if (!HasChain) { 5059 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops); 5060 } else if (!I.getType()->isVoidTy()) { 5061 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops); 5062 } else { 5063 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops); 5064 } 5065 5066 if (HasChain) { 5067 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1); 5068 if (OnlyLoad) 5069 PendingLoads.push_back(Chain); 5070 else 5071 DAG.setRoot(Chain); 5072 } 5073 5074 if (!I.getType()->isVoidTy()) { 5075 if (!isa<VectorType>(I.getType())) 5076 Result = lowerRangeToAssertZExt(DAG, I, Result); 5077 5078 MaybeAlign Alignment = I.getRetAlign(); 5079 5080 // Insert `assertalign` node if there's an alignment. 5081 if (InsertAssertAlign && Alignment) { 5082 Result = 5083 DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne()); 5084 } 5085 5086 setValue(&I, Result); 5087 } 5088 } 5089 5090 /// GetSignificand - Get the significand and build it into a floating-point 5091 /// number with exponent of 1: 5092 /// 5093 /// Op = (Op & 0x007fffff) | 0x3f800000; 5094 /// 5095 /// where Op is the hexadecimal representation of floating point value. 5096 static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl) { 5097 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, 5098 DAG.getConstant(0x007fffff, dl, MVT::i32)); 5099 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1, 5100 DAG.getConstant(0x3f800000, dl, MVT::i32)); 5101 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2); 5102 } 5103 5104 /// GetExponent - Get the exponent: 5105 /// 5106 /// (float)(int)(((Op & 0x7f800000) >> 23) - 127); 5107 /// 5108 /// where Op is the hexadecimal representation of floating point value. 5109 static SDValue GetExponent(SelectionDAG &DAG, SDValue Op, 5110 const TargetLowering &TLI, const SDLoc &dl) { 5111 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op, 5112 DAG.getConstant(0x7f800000, dl, MVT::i32)); 5113 SDValue t1 = DAG.getNode( 5114 ISD::SRL, dl, MVT::i32, t0, 5115 DAG.getConstant(23, dl, 5116 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout()))); 5117 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1, 5118 DAG.getConstant(127, dl, MVT::i32)); 5119 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2); 5120 } 5121 5122 /// getF32Constant - Get 32-bit floating point constant. 5123 static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt, 5124 const SDLoc &dl) { 5125 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl, 5126 MVT::f32); 5127 } 5128 5129 static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl, 5130 SelectionDAG &DAG) { 5131 // TODO: What fast-math-flags should be set on the floating-point nodes? 5132 5133 // IntegerPartOfX = ((int32_t)(t0); 5134 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0); 5135 5136 // FractionalPartOfX = t0 - (float)IntegerPartOfX; 5137 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX); 5138 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1); 5139 5140 // IntegerPartOfX <<= 23; 5141 IntegerPartOfX = 5142 DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX, 5143 DAG.getConstant(23, dl, 5144 DAG.getTargetLoweringInfo().getShiftAmountTy( 5145 MVT::i32, DAG.getDataLayout()))); 5146 5147 SDValue TwoToFractionalPartOfX; 5148 if (LimitFloatPrecision <= 6) { 5149 // For floating-point precision of 6: 5150 // 5151 // TwoToFractionalPartOfX = 5152 // 0.997535578f + 5153 // (0.735607626f + 0.252464424f * x) * x; 5154 // 5155 // error 0.0144103317, which is 6 bits 5156 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5157 getF32Constant(DAG, 0x3e814304, dl)); 5158 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, 5159 getF32Constant(DAG, 0x3f3c50c8, dl)); 5160 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5161 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5162 getF32Constant(DAG, 0x3f7f5e7e, dl)); 5163 } else if (LimitFloatPrecision <= 12) { 5164 // For floating-point precision of 12: 5165 // 5166 // TwoToFractionalPartOfX = 5167 // 0.999892986f + 5168 // (0.696457318f + 5169 // (0.224338339f + 0.792043434e-1f * x) * x) * x; 5170 // 5171 // error 0.000107046256, which is 13 to 14 bits 5172 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5173 getF32Constant(DAG, 0x3da235e3, dl)); 5174 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, 5175 getF32Constant(DAG, 0x3e65b8f3, dl)); 5176 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5177 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5178 getF32Constant(DAG, 0x3f324b07, dl)); 5179 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5180 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, 5181 getF32Constant(DAG, 0x3f7ff8fd, dl)); 5182 } else { // LimitFloatPrecision <= 18 5183 // For floating-point precision of 18: 5184 // 5185 // TwoToFractionalPartOfX = 5186 // 0.999999982f + 5187 // (0.693148872f + 5188 // (0.240227044f + 5189 // (0.554906021e-1f + 5190 // (0.961591928e-2f + 5191 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x; 5192 // error 2.47208000*10^(-7), which is better than 18 bits 5193 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5194 getF32Constant(DAG, 0x3924b03e, dl)); 5195 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, 5196 getF32Constant(DAG, 0x3ab24b87, dl)); 5197 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5198 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5199 getF32Constant(DAG, 0x3c1d8c17, dl)); 5200 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5201 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, 5202 getF32Constant(DAG, 0x3d634a1d, dl)); 5203 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); 5204 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, 5205 getF32Constant(DAG, 0x3e75fe14, dl)); 5206 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); 5207 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10, 5208 getF32Constant(DAG, 0x3f317234, dl)); 5209 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X); 5210 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12, 5211 getF32Constant(DAG, 0x3f800000, dl)); 5212 } 5213 5214 // Add the exponent into the result in integer domain. 5215 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX); 5216 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, 5217 DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX)); 5218 } 5219 5220 /// expandExp - Lower an exp intrinsic. Handles the special sequences for 5221 /// limited-precision mode. 5222 static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, 5223 const TargetLowering &TLI, SDNodeFlags Flags) { 5224 if (Op.getValueType() == MVT::f32 && 5225 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { 5226 5227 // Put the exponent in the right bit position for later addition to the 5228 // final result: 5229 // 5230 // t0 = Op * log2(e) 5231 5232 // TODO: What fast-math-flags should be set here? 5233 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op, 5234 DAG.getConstantFP(numbers::log2ef, dl, MVT::f32)); 5235 return getLimitedPrecisionExp2(t0, dl, DAG); 5236 } 5237 5238 // No special expansion. 5239 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags); 5240 } 5241 5242 /// expandLog - Lower a log intrinsic. Handles the special sequences for 5243 /// limited-precision mode. 5244 static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, 5245 const TargetLowering &TLI, SDNodeFlags Flags) { 5246 // TODO: What fast-math-flags should be set on the floating-point nodes? 5247 5248 if (Op.getValueType() == MVT::f32 && 5249 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { 5250 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 5251 5252 // Scale the exponent by log(2). 5253 SDValue Exp = GetExponent(DAG, Op1, TLI, dl); 5254 SDValue LogOfExponent = 5255 DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, 5256 DAG.getConstantFP(numbers::ln2f, dl, MVT::f32)); 5257 5258 // Get the significand and build it into a floating-point number with 5259 // exponent of 1. 5260 SDValue X = GetSignificand(DAG, Op1, dl); 5261 5262 SDValue LogOfMantissa; 5263 if (LimitFloatPrecision <= 6) { 5264 // For floating-point precision of 6: 5265 // 5266 // LogofMantissa = 5267 // -1.1609546f + 5268 // (1.4034025f - 0.23903021f * x) * x; 5269 // 5270 // error 0.0034276066, which is better than 8 bits 5271 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5272 getF32Constant(DAG, 0xbe74c456, dl)); 5273 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5274 getF32Constant(DAG, 0x3fb3a2b1, dl)); 5275 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5276 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5277 getF32Constant(DAG, 0x3f949a29, dl)); 5278 } else if (LimitFloatPrecision <= 12) { 5279 // For floating-point precision of 12: 5280 // 5281 // LogOfMantissa = 5282 // -1.7417939f + 5283 // (2.8212026f + 5284 // (-1.4699568f + 5285 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x; 5286 // 5287 // error 0.000061011436, which is 14 bits 5288 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5289 getF32Constant(DAG, 0xbd67b6d6, dl)); 5290 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5291 getF32Constant(DAG, 0x3ee4f4b8, dl)); 5292 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5293 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5294 getF32Constant(DAG, 0x3fbc278b, dl)); 5295 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5296 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5297 getF32Constant(DAG, 0x40348e95, dl)); 5298 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5299 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, 5300 getF32Constant(DAG, 0x3fdef31a, dl)); 5301 } else { // LimitFloatPrecision <= 18 5302 // For floating-point precision of 18: 5303 // 5304 // LogOfMantissa = 5305 // -2.1072184f + 5306 // (4.2372794f + 5307 // (-3.7029485f + 5308 // (2.2781945f + 5309 // (-0.87823314f + 5310 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x; 5311 // 5312 // error 0.0000023660568, which is better than 18 bits 5313 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5314 getF32Constant(DAG, 0xbc91e5ac, dl)); 5315 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5316 getF32Constant(DAG, 0x3e4350aa, dl)); 5317 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5318 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5319 getF32Constant(DAG, 0x3f60d3e3, dl)); 5320 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5321 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5322 getF32Constant(DAG, 0x4011cdf0, dl)); 5323 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5324 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, 5325 getF32Constant(DAG, 0x406cfd1c, dl)); 5326 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); 5327 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, 5328 getF32Constant(DAG, 0x408797cb, dl)); 5329 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); 5330 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, 5331 getF32Constant(DAG, 0x4006dcab, dl)); 5332 } 5333 5334 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa); 5335 } 5336 5337 // No special expansion. 5338 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags); 5339 } 5340 5341 /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for 5342 /// limited-precision mode. 5343 static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, 5344 const TargetLowering &TLI, SDNodeFlags Flags) { 5345 // TODO: What fast-math-flags should be set on the floating-point nodes? 5346 5347 if (Op.getValueType() == MVT::f32 && 5348 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { 5349 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 5350 5351 // Get the exponent. 5352 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl); 5353 5354 // Get the significand and build it into a floating-point number with 5355 // exponent of 1. 5356 SDValue X = GetSignificand(DAG, Op1, dl); 5357 5358 // Different possible minimax approximations of significand in 5359 // floating-point for various degrees of accuracy over [1,2]. 5360 SDValue Log2ofMantissa; 5361 if (LimitFloatPrecision <= 6) { 5362 // For floating-point precision of 6: 5363 // 5364 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x; 5365 // 5366 // error 0.0049451742, which is more than 7 bits 5367 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5368 getF32Constant(DAG, 0xbeb08fe0, dl)); 5369 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5370 getF32Constant(DAG, 0x40019463, dl)); 5371 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5372 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5373 getF32Constant(DAG, 0x3fd6633d, dl)); 5374 } else if (LimitFloatPrecision <= 12) { 5375 // For floating-point precision of 12: 5376 // 5377 // Log2ofMantissa = 5378 // -2.51285454f + 5379 // (4.07009056f + 5380 // (-2.12067489f + 5381 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x; 5382 // 5383 // error 0.0000876136000, which is better than 13 bits 5384 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5385 getF32Constant(DAG, 0xbda7262e, dl)); 5386 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5387 getF32Constant(DAG, 0x3f25280b, dl)); 5388 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5389 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5390 getF32Constant(DAG, 0x4007b923, dl)); 5391 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5392 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5393 getF32Constant(DAG, 0x40823e2f, dl)); 5394 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5395 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, 5396 getF32Constant(DAG, 0x4020d29c, dl)); 5397 } else { // LimitFloatPrecision <= 18 5398 // For floating-point precision of 18: 5399 // 5400 // Log2ofMantissa = 5401 // -3.0400495f + 5402 // (6.1129976f + 5403 // (-5.3420409f + 5404 // (3.2865683f + 5405 // (-1.2669343f + 5406 // (0.27515199f - 5407 // 0.25691327e-1f * x) * x) * x) * x) * x) * x; 5408 // 5409 // error 0.0000018516, which is better than 18 bits 5410 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5411 getF32Constant(DAG, 0xbcd2769e, dl)); 5412 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5413 getF32Constant(DAG, 0x3e8ce0b9, dl)); 5414 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5415 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5416 getF32Constant(DAG, 0x3fa22ae7, dl)); 5417 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5418 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4, 5419 getF32Constant(DAG, 0x40525723, dl)); 5420 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5421 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6, 5422 getF32Constant(DAG, 0x40aaf200, dl)); 5423 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); 5424 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8, 5425 getF32Constant(DAG, 0x40c39dad, dl)); 5426 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X); 5427 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10, 5428 getF32Constant(DAG, 0x4042902c, dl)); 5429 } 5430 5431 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa); 5432 } 5433 5434 // No special expansion. 5435 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags); 5436 } 5437 5438 /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for 5439 /// limited-precision mode. 5440 static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, 5441 const TargetLowering &TLI, SDNodeFlags Flags) { 5442 // TODO: What fast-math-flags should be set on the floating-point nodes? 5443 5444 if (Op.getValueType() == MVT::f32 && 5445 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { 5446 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 5447 5448 // Scale the exponent by log10(2) [0.30102999f]. 5449 SDValue Exp = GetExponent(DAG, Op1, TLI, dl); 5450 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp, 5451 getF32Constant(DAG, 0x3e9a209a, dl)); 5452 5453 // Get the significand and build it into a floating-point number with 5454 // exponent of 1. 5455 SDValue X = GetSignificand(DAG, Op1, dl); 5456 5457 SDValue Log10ofMantissa; 5458 if (LimitFloatPrecision <= 6) { 5459 // For floating-point precision of 6: 5460 // 5461 // Log10ofMantissa = 5462 // -0.50419619f + 5463 // (0.60948995f - 0.10380950f * x) * x; 5464 // 5465 // error 0.0014886165, which is 6 bits 5466 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5467 getF32Constant(DAG, 0xbdd49a13, dl)); 5468 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0, 5469 getF32Constant(DAG, 0x3f1c0789, dl)); 5470 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5471 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2, 5472 getF32Constant(DAG, 0x3f011300, dl)); 5473 } else if (LimitFloatPrecision <= 12) { 5474 // For floating-point precision of 12: 5475 // 5476 // Log10ofMantissa = 5477 // -0.64831180f + 5478 // (0.91751397f + 5479 // (-0.31664806f + 0.47637168e-1f * x) * x) * x; 5480 // 5481 // error 0.00019228036, which is better than 12 bits 5482 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5483 getF32Constant(DAG, 0x3d431f31, dl)); 5484 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, 5485 getF32Constant(DAG, 0x3ea21fb2, dl)); 5486 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5487 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, 5488 getF32Constant(DAG, 0x3f6ae232, dl)); 5489 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5490 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, 5491 getF32Constant(DAG, 0x3f25f7c3, dl)); 5492 } else { // LimitFloatPrecision <= 18 5493 // For floating-point precision of 18: 5494 // 5495 // Log10ofMantissa = 5496 // -0.84299375f + 5497 // (1.5327582f + 5498 // (-1.0688956f + 5499 // (0.49102474f + 5500 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x; 5501 // 5502 // error 0.0000037995730, which is better than 18 bits 5503 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X, 5504 getF32Constant(DAG, 0x3c5d51ce, dl)); 5505 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, 5506 getF32Constant(DAG, 0x3e00685a, dl)); 5507 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X); 5508 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2, 5509 getF32Constant(DAG, 0x3efb6798, dl)); 5510 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X); 5511 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4, 5512 getF32Constant(DAG, 0x3f88d192, dl)); 5513 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X); 5514 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6, 5515 getF32Constant(DAG, 0x3fc4316c, dl)); 5516 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X); 5517 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8, 5518 getF32Constant(DAG, 0x3f57ce70, dl)); 5519 } 5520 5521 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa); 5522 } 5523 5524 // No special expansion. 5525 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags); 5526 } 5527 5528 /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for 5529 /// limited-precision mode. 5530 static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG, 5531 const TargetLowering &TLI, SDNodeFlags Flags) { 5532 if (Op.getValueType() == MVT::f32 && 5533 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) 5534 return getLimitedPrecisionExp2(Op, dl, DAG); 5535 5536 // No special expansion. 5537 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags); 5538 } 5539 5540 /// visitPow - Lower a pow intrinsic. Handles the special sequences for 5541 /// limited-precision mode with x == 10.0f. 5542 static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS, 5543 SelectionDAG &DAG, const TargetLowering &TLI, 5544 SDNodeFlags Flags) { 5545 bool IsExp10 = false; 5546 if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 && 5547 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) { 5548 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) { 5549 APFloat Ten(10.0f); 5550 IsExp10 = LHSC->isExactlyValue(Ten); 5551 } 5552 } 5553 5554 // TODO: What fast-math-flags should be set on the FMUL node? 5555 if (IsExp10) { 5556 // Put the exponent in the right bit position for later addition to the 5557 // final result: 5558 // 5559 // #define LOG2OF10 3.3219281f 5560 // t0 = Op * LOG2OF10; 5561 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS, 5562 getF32Constant(DAG, 0x40549a78, dl)); 5563 return getLimitedPrecisionExp2(t0, dl, DAG); 5564 } 5565 5566 // No special expansion. 5567 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags); 5568 } 5569 5570 /// ExpandPowI - Expand a llvm.powi intrinsic. 5571 static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS, 5572 SelectionDAG &DAG) { 5573 // If RHS is a constant, we can expand this out to a multiplication tree if 5574 // it's beneficial on the target, otherwise we end up lowering to a call to 5575 // __powidf2 (for example). 5576 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { 5577 unsigned Val = RHSC->getSExtValue(); 5578 5579 // powi(x, 0) -> 1.0 5580 if (Val == 0) 5581 return DAG.getConstantFP(1.0, DL, LHS.getValueType()); 5582 5583 if (DAG.getTargetLoweringInfo().isBeneficialToExpandPowI( 5584 Val, DAG.shouldOptForSize())) { 5585 // Get the exponent as a positive value. 5586 if ((int)Val < 0) 5587 Val = -Val; 5588 // We use the simple binary decomposition method to generate the multiply 5589 // sequence. There are more optimal ways to do this (for example, 5590 // powi(x,15) generates one more multiply than it should), but this has 5591 // the benefit of being both really simple and much better than a libcall. 5592 SDValue Res; // Logically starts equal to 1.0 5593 SDValue CurSquare = LHS; 5594 // TODO: Intrinsics should have fast-math-flags that propagate to these 5595 // nodes. 5596 while (Val) { 5597 if (Val & 1) { 5598 if (Res.getNode()) 5599 Res = 5600 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare); 5601 else 5602 Res = CurSquare; // 1.0*CurSquare. 5603 } 5604 5605 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(), 5606 CurSquare, CurSquare); 5607 Val >>= 1; 5608 } 5609 5610 // If the original was negative, invert the result, producing 1/(x*x*x). 5611 if (RHSC->getSExtValue() < 0) 5612 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(), 5613 DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res); 5614 return Res; 5615 } 5616 } 5617 5618 // Otherwise, expand to a libcall. 5619 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS); 5620 } 5621 5622 static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL, 5623 SDValue LHS, SDValue RHS, SDValue Scale, 5624 SelectionDAG &DAG, const TargetLowering &TLI) { 5625 EVT VT = LHS.getValueType(); 5626 bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT; 5627 bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT; 5628 LLVMContext &Ctx = *DAG.getContext(); 5629 5630 // If the type is legal but the operation isn't, this node might survive all 5631 // the way to operation legalization. If we end up there and we do not have 5632 // the ability to widen the type (if VT*2 is not legal), we cannot expand the 5633 // node. 5634 5635 // Coax the legalizer into expanding the node during type legalization instead 5636 // by bumping the size by one bit. This will force it to Promote, enabling the 5637 // early expansion and avoiding the need to expand later. 5638 5639 // We don't have to do this if Scale is 0; that can always be expanded, unless 5640 // it's a saturating signed operation. Those can experience true integer 5641 // division overflow, a case which we must avoid. 5642 5643 // FIXME: We wouldn't have to do this (or any of the early 5644 // expansion/promotion) if it was possible to expand a libcall of an 5645 // illegal type during operation legalization. But it's not, so things 5646 // get a bit hacky. 5647 unsigned ScaleInt = Scale->getAsZExtVal(); 5648 if ((ScaleInt > 0 || (Saturating && Signed)) && 5649 (TLI.isTypeLegal(VT) || 5650 (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) { 5651 TargetLowering::LegalizeAction Action = TLI.getFixedPointOperationAction( 5652 Opcode, VT, ScaleInt); 5653 if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) { 5654 EVT PromVT; 5655 if (VT.isScalarInteger()) 5656 PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1); 5657 else if (VT.isVector()) { 5658 PromVT = VT.getVectorElementType(); 5659 PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1); 5660 PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount()); 5661 } else 5662 llvm_unreachable("Wrong VT for DIVFIX?"); 5663 LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT); 5664 RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT); 5665 EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout()); 5666 // For saturating operations, we need to shift up the LHS to get the 5667 // proper saturation width, and then shift down again afterwards. 5668 if (Saturating) 5669 LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS, 5670 DAG.getConstant(1, DL, ShiftTy)); 5671 SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale); 5672 if (Saturating) 5673 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res, 5674 DAG.getConstant(1, DL, ShiftTy)); 5675 return DAG.getZExtOrTrunc(Res, DL, VT); 5676 } 5677 } 5678 5679 return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale); 5680 } 5681 5682 // getUnderlyingArgRegs - Find underlying registers used for a truncated, 5683 // bitcasted, or split argument. Returns a list of <Register, size in bits> 5684 static void 5685 getUnderlyingArgRegs(SmallVectorImpl<std::pair<unsigned, TypeSize>> &Regs, 5686 const SDValue &N) { 5687 switch (N.getOpcode()) { 5688 case ISD::CopyFromReg: { 5689 SDValue Op = N.getOperand(1); 5690 Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(), 5691 Op.getValueType().getSizeInBits()); 5692 return; 5693 } 5694 case ISD::BITCAST: 5695 case ISD::AssertZext: 5696 case ISD::AssertSext: 5697 case ISD::TRUNCATE: 5698 getUnderlyingArgRegs(Regs, N.getOperand(0)); 5699 return; 5700 case ISD::BUILD_PAIR: 5701 case ISD::BUILD_VECTOR: 5702 case ISD::CONCAT_VECTORS: 5703 for (SDValue Op : N->op_values()) 5704 getUnderlyingArgRegs(Regs, Op); 5705 return; 5706 default: 5707 return; 5708 } 5709 } 5710 5711 /// If the DbgValueInst is a dbg_value of a function argument, create the 5712 /// corresponding DBG_VALUE machine instruction for it now. At the end of 5713 /// instruction selection, they will be inserted to the entry BB. 5714 /// We don't currently support this for variadic dbg_values, as they shouldn't 5715 /// appear for function arguments or in the prologue. 5716 bool SelectionDAGBuilder::EmitFuncArgumentDbgValue( 5717 const Value *V, DILocalVariable *Variable, DIExpression *Expr, 5718 DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) { 5719 const Argument *Arg = dyn_cast<Argument>(V); 5720 if (!Arg) 5721 return false; 5722 5723 MachineFunction &MF = DAG.getMachineFunction(); 5724 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo(); 5725 5726 // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind 5727 // we've been asked to pursue. 5728 auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr, 5729 bool Indirect) { 5730 if (Reg.isVirtual() && MF.useDebugInstrRef()) { 5731 // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF 5732 // pointing at the VReg, which will be patched up later. 5733 auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF); 5734 SmallVector<MachineOperand, 1> MOs({MachineOperand::CreateReg( 5735 /* Reg */ Reg, /* isDef */ false, /* isImp */ false, 5736 /* isKill */ false, /* isDead */ false, 5737 /* isUndef */ false, /* isEarlyClobber */ false, 5738 /* SubReg */ 0, /* isDebug */ true)}); 5739 5740 auto *NewDIExpr = FragExpr; 5741 // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into 5742 // the DIExpression. 5743 if (Indirect) 5744 NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore); 5745 SmallVector<uint64_t, 2> Ops({dwarf::DW_OP_LLVM_arg, 0}); 5746 NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops); 5747 return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr); 5748 } else { 5749 // Create a completely standard DBG_VALUE. 5750 auto &Inst = TII->get(TargetOpcode::DBG_VALUE); 5751 return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr); 5752 } 5753 }; 5754 5755 if (Kind == FuncArgumentDbgValueKind::Value) { 5756 // ArgDbgValues are hoisted to the beginning of the entry block. So we 5757 // should only emit as ArgDbgValue if the dbg.value intrinsic is found in 5758 // the entry block. 5759 bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front(); 5760 if (!IsInEntryBlock) 5761 return false; 5762 5763 // ArgDbgValues are hoisted to the beginning of the entry block. So we 5764 // should only emit as ArgDbgValue if the dbg.value intrinsic describes a 5765 // variable that also is a param. 5766 // 5767 // Although, if we are at the top of the entry block already, we can still 5768 // emit using ArgDbgValue. This might catch some situations when the 5769 // dbg.value refers to an argument that isn't used in the entry block, so 5770 // any CopyToReg node would be optimized out and the only way to express 5771 // this DBG_VALUE is by using the physical reg (or FI) as done in this 5772 // method. ArgDbgValues are hoisted to the beginning of the entry block. So 5773 // we should only emit as ArgDbgValue if the Variable is an argument to the 5774 // current function, and the dbg.value intrinsic is found in the entry 5775 // block. 5776 bool VariableIsFunctionInputArg = Variable->isParameter() && 5777 !DL->getInlinedAt(); 5778 bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder; 5779 if (!IsInPrologue && !VariableIsFunctionInputArg) 5780 return false; 5781 5782 // Here we assume that a function argument on IR level only can be used to 5783 // describe one input parameter on source level. If we for example have 5784 // source code like this 5785 // 5786 // struct A { long x, y; }; 5787 // void foo(struct A a, long b) { 5788 // ... 5789 // b = a.x; 5790 // ... 5791 // } 5792 // 5793 // and IR like this 5794 // 5795 // define void @foo(i32 %a1, i32 %a2, i32 %b) { 5796 // entry: 5797 // call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment 5798 // call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment 5799 // call void @llvm.dbg.value(metadata i32 %b, "b", 5800 // ... 5801 // call void @llvm.dbg.value(metadata i32 %a1, "b" 5802 // ... 5803 // 5804 // then the last dbg.value is describing a parameter "b" using a value that 5805 // is an argument. But since we already has used %a1 to describe a parameter 5806 // we should not handle that last dbg.value here (that would result in an 5807 // incorrect hoisting of the DBG_VALUE to the function entry). 5808 // Notice that we allow one dbg.value per IR level argument, to accommodate 5809 // for the situation with fragments above. 5810 if (VariableIsFunctionInputArg) { 5811 unsigned ArgNo = Arg->getArgNo(); 5812 if (ArgNo >= FuncInfo.DescribedArgs.size()) 5813 FuncInfo.DescribedArgs.resize(ArgNo + 1, false); 5814 else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo)) 5815 return false; 5816 FuncInfo.DescribedArgs.set(ArgNo); 5817 } 5818 } 5819 5820 bool IsIndirect = false; 5821 std::optional<MachineOperand> Op; 5822 // Some arguments' frame index is recorded during argument lowering. 5823 int FI = FuncInfo.getArgumentFrameIndex(Arg); 5824 if (FI != std::numeric_limits<int>::max()) 5825 Op = MachineOperand::CreateFI(FI); 5826 5827 SmallVector<std::pair<unsigned, TypeSize>, 8> ArgRegsAndSizes; 5828 if (!Op && N.getNode()) { 5829 getUnderlyingArgRegs(ArgRegsAndSizes, N); 5830 Register Reg; 5831 if (ArgRegsAndSizes.size() == 1) 5832 Reg = ArgRegsAndSizes.front().first; 5833 5834 if (Reg && Reg.isVirtual()) { 5835 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 5836 Register PR = RegInfo.getLiveInPhysReg(Reg); 5837 if (PR) 5838 Reg = PR; 5839 } 5840 if (Reg) { 5841 Op = MachineOperand::CreateReg(Reg, false); 5842 IsIndirect = Kind != FuncArgumentDbgValueKind::Value; 5843 } 5844 } 5845 5846 if (!Op && N.getNode()) { 5847 // Check if frame index is available. 5848 SDValue LCandidate = peekThroughBitcasts(N); 5849 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode())) 5850 if (FrameIndexSDNode *FINode = 5851 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) 5852 Op = MachineOperand::CreateFI(FINode->getIndex()); 5853 } 5854 5855 if (!Op) { 5856 // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg 5857 auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<unsigned, TypeSize>> 5858 SplitRegs) { 5859 unsigned Offset = 0; 5860 for (const auto &RegAndSize : SplitRegs) { 5861 // If the expression is already a fragment, the current register 5862 // offset+size might extend beyond the fragment. In this case, only 5863 // the register bits that are inside the fragment are relevant. 5864 int RegFragmentSizeInBits = RegAndSize.second; 5865 if (auto ExprFragmentInfo = Expr->getFragmentInfo()) { 5866 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits; 5867 // The register is entirely outside the expression fragment, 5868 // so is irrelevant for debug info. 5869 if (Offset >= ExprFragmentSizeInBits) 5870 break; 5871 // The register is partially outside the expression fragment, only 5872 // the low bits within the fragment are relevant for debug info. 5873 if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) { 5874 RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset; 5875 } 5876 } 5877 5878 auto FragmentExpr = DIExpression::createFragmentExpression( 5879 Expr, Offset, RegFragmentSizeInBits); 5880 Offset += RegAndSize.second; 5881 // If a valid fragment expression cannot be created, the variable's 5882 // correct value cannot be determined and so it is set as Undef. 5883 if (!FragmentExpr) { 5884 SDDbgValue *SDV = DAG.getConstantDbgValue( 5885 Variable, Expr, UndefValue::get(V->getType()), DL, SDNodeOrder); 5886 DAG.AddDbgValue(SDV, false); 5887 continue; 5888 } 5889 MachineInstr *NewMI = 5890 MakeVRegDbgValue(RegAndSize.first, *FragmentExpr, 5891 Kind != FuncArgumentDbgValueKind::Value); 5892 FuncInfo.ArgDbgValues.push_back(NewMI); 5893 } 5894 }; 5895 5896 // Check if ValueMap has reg number. 5897 DenseMap<const Value *, Register>::const_iterator 5898 VMI = FuncInfo.ValueMap.find(V); 5899 if (VMI != FuncInfo.ValueMap.end()) { 5900 const auto &TLI = DAG.getTargetLoweringInfo(); 5901 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second, 5902 V->getType(), std::nullopt); 5903 if (RFV.occupiesMultipleRegs()) { 5904 splitMultiRegDbgValue(RFV.getRegsAndSizes()); 5905 return true; 5906 } 5907 5908 Op = MachineOperand::CreateReg(VMI->second, false); 5909 IsIndirect = Kind != FuncArgumentDbgValueKind::Value; 5910 } else if (ArgRegsAndSizes.size() > 1) { 5911 // This was split due to the calling convention, and no virtual register 5912 // mapping exists for the value. 5913 splitMultiRegDbgValue(ArgRegsAndSizes); 5914 return true; 5915 } 5916 } 5917 5918 if (!Op) 5919 return false; 5920 5921 assert(Variable->isValidLocationForIntrinsic(DL) && 5922 "Expected inlined-at fields to agree"); 5923 MachineInstr *NewMI = nullptr; 5924 5925 if (Op->isReg()) 5926 NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect); 5927 else 5928 NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op, 5929 Variable, Expr); 5930 5931 // Otherwise, use ArgDbgValues. 5932 FuncInfo.ArgDbgValues.push_back(NewMI); 5933 return true; 5934 } 5935 5936 /// Return the appropriate SDDbgValue based on N. 5937 SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N, 5938 DILocalVariable *Variable, 5939 DIExpression *Expr, 5940 const DebugLoc &dl, 5941 unsigned DbgSDNodeOrder) { 5942 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) { 5943 // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe 5944 // stack slot locations. 5945 // 5946 // Consider "int x = 0; int *px = &x;". There are two kinds of interesting 5947 // debug values here after optimization: 5948 // 5949 // dbg.value(i32* %px, !"int *px", !DIExpression()), and 5950 // dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref)) 5951 // 5952 // Both describe the direct values of their associated variables. 5953 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(), 5954 /*IsIndirect*/ false, dl, DbgSDNodeOrder); 5955 } 5956 return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(), 5957 /*IsIndirect*/ false, dl, DbgSDNodeOrder); 5958 } 5959 5960 static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) { 5961 switch (Intrinsic) { 5962 case Intrinsic::smul_fix: 5963 return ISD::SMULFIX; 5964 case Intrinsic::umul_fix: 5965 return ISD::UMULFIX; 5966 case Intrinsic::smul_fix_sat: 5967 return ISD::SMULFIXSAT; 5968 case Intrinsic::umul_fix_sat: 5969 return ISD::UMULFIXSAT; 5970 case Intrinsic::sdiv_fix: 5971 return ISD::SDIVFIX; 5972 case Intrinsic::udiv_fix: 5973 return ISD::UDIVFIX; 5974 case Intrinsic::sdiv_fix_sat: 5975 return ISD::SDIVFIXSAT; 5976 case Intrinsic::udiv_fix_sat: 5977 return ISD::UDIVFIXSAT; 5978 default: 5979 llvm_unreachable("Unhandled fixed point intrinsic"); 5980 } 5981 } 5982 5983 void SelectionDAGBuilder::lowerCallToExternalSymbol(const CallInst &I, 5984 const char *FunctionName) { 5985 assert(FunctionName && "FunctionName must not be nullptr"); 5986 SDValue Callee = DAG.getExternalSymbol( 5987 FunctionName, 5988 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout())); 5989 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall()); 5990 } 5991 5992 /// Given a @llvm.call.preallocated.setup, return the corresponding 5993 /// preallocated call. 5994 static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) { 5995 assert(cast<CallBase>(PreallocatedSetup) 5996 ->getCalledFunction() 5997 ->getIntrinsicID() == Intrinsic::call_preallocated_setup && 5998 "expected call_preallocated_setup Value"); 5999 for (const auto *U : PreallocatedSetup->users()) { 6000 auto *UseCall = cast<CallBase>(U); 6001 const Function *Fn = UseCall->getCalledFunction(); 6002 if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) { 6003 return UseCall; 6004 } 6005 } 6006 llvm_unreachable("expected corresponding call to preallocated setup/arg"); 6007 } 6008 6009 /// If DI is a debug value with an EntryValue expression, lower it using the 6010 /// corresponding physical register of the associated Argument value 6011 /// (guaranteed to exist by the verifier). 6012 bool SelectionDAGBuilder::visitEntryValueDbgValue(const DbgValueInst &DI) { 6013 DILocalVariable *Variable = DI.getVariable(); 6014 DIExpression *Expr = DI.getExpression(); 6015 if (!Expr->isEntryValue() || !hasSingleElement(DI.getValues())) 6016 return false; 6017 6018 // These properties are guaranteed by the verifier. 6019 Argument *Arg = cast<Argument>(DI.getValue(0)); 6020 assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync)); 6021 6022 auto ArgIt = FuncInfo.ValueMap.find(Arg); 6023 if (ArgIt == FuncInfo.ValueMap.end()) { 6024 LLVM_DEBUG( 6025 dbgs() << "Dropping dbg.value: expression is entry_value but " 6026 "couldn't find an associated register for the Argument\n"); 6027 return true; 6028 } 6029 Register ArgVReg = ArgIt->getSecond(); 6030 6031 for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins()) 6032 if (ArgVReg == VirtReg || ArgVReg == PhysReg) { 6033 SDDbgValue *SDV = 6034 DAG.getVRegDbgValue(Variable, Expr, PhysReg, false /*IsIndidrect*/, 6035 DI.getDebugLoc(), SDNodeOrder); 6036 DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/); 6037 return true; 6038 } 6039 LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but " 6040 "couldn't find a physical register\n"); 6041 return true; 6042 } 6043 6044 /// Lower the call to the specified intrinsic function. 6045 void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, 6046 unsigned Intrinsic) { 6047 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 6048 SDLoc sdl = getCurSDLoc(); 6049 DebugLoc dl = getCurDebugLoc(); 6050 SDValue Res; 6051 6052 SDNodeFlags Flags; 6053 if (auto *FPOp = dyn_cast<FPMathOperator>(&I)) 6054 Flags.copyFMF(*FPOp); 6055 6056 switch (Intrinsic) { 6057 default: 6058 // By default, turn this into a target intrinsic node. 6059 visitTargetIntrinsic(I, Intrinsic); 6060 return; 6061 case Intrinsic::vscale: { 6062 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6063 setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1))); 6064 return; 6065 } 6066 case Intrinsic::vastart: visitVAStart(I); return; 6067 case Intrinsic::vaend: visitVAEnd(I); return; 6068 case Intrinsic::vacopy: visitVACopy(I); return; 6069 case Intrinsic::returnaddress: 6070 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, 6071 TLI.getValueType(DAG.getDataLayout(), I.getType()), 6072 getValue(I.getArgOperand(0)))); 6073 return; 6074 case Intrinsic::addressofreturnaddress: 6075 setValue(&I, 6076 DAG.getNode(ISD::ADDROFRETURNADDR, sdl, 6077 TLI.getValueType(DAG.getDataLayout(), I.getType()))); 6078 return; 6079 case Intrinsic::sponentry: 6080 setValue(&I, 6081 DAG.getNode(ISD::SPONENTRY, sdl, 6082 TLI.getValueType(DAG.getDataLayout(), I.getType()))); 6083 return; 6084 case Intrinsic::frameaddress: 6085 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, 6086 TLI.getFrameIndexTy(DAG.getDataLayout()), 6087 getValue(I.getArgOperand(0)))); 6088 return; 6089 case Intrinsic::read_volatile_register: 6090 case Intrinsic::read_register: { 6091 Value *Reg = I.getArgOperand(0); 6092 SDValue Chain = getRoot(); 6093 SDValue RegName = 6094 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata())); 6095 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6096 Res = DAG.getNode(ISD::READ_REGISTER, sdl, 6097 DAG.getVTList(VT, MVT::Other), Chain, RegName); 6098 setValue(&I, Res); 6099 DAG.setRoot(Res.getValue(1)); 6100 return; 6101 } 6102 case Intrinsic::write_register: { 6103 Value *Reg = I.getArgOperand(0); 6104 Value *RegValue = I.getArgOperand(1); 6105 SDValue Chain = getRoot(); 6106 SDValue RegName = 6107 DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata())); 6108 DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain, 6109 RegName, getValue(RegValue))); 6110 return; 6111 } 6112 case Intrinsic::memcpy: { 6113 const auto &MCI = cast<MemCpyInst>(I); 6114 SDValue Op1 = getValue(I.getArgOperand(0)); 6115 SDValue Op2 = getValue(I.getArgOperand(1)); 6116 SDValue Op3 = getValue(I.getArgOperand(2)); 6117 // @llvm.memcpy defines 0 and 1 to both mean no alignment. 6118 Align DstAlign = MCI.getDestAlign().valueOrOne(); 6119 Align SrcAlign = MCI.getSourceAlign().valueOrOne(); 6120 Align Alignment = std::min(DstAlign, SrcAlign); 6121 bool isVol = MCI.isVolatile(); 6122 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6123 // FIXME: Support passing different dest/src alignments to the memcpy DAG 6124 // node. 6125 SDValue Root = isVol ? getRoot() : getMemoryRoot(); 6126 SDValue MC = DAG.getMemcpy( 6127 Root, sdl, Op1, Op2, Op3, Alignment, isVol, 6128 /* AlwaysInline */ false, isTC, MachinePointerInfo(I.getArgOperand(0)), 6129 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), AA); 6130 updateDAGForMaybeTailCall(MC); 6131 return; 6132 } 6133 case Intrinsic::memcpy_inline: { 6134 const auto &MCI = cast<MemCpyInlineInst>(I); 6135 SDValue Dst = getValue(I.getArgOperand(0)); 6136 SDValue Src = getValue(I.getArgOperand(1)); 6137 SDValue Size = getValue(I.getArgOperand(2)); 6138 assert(isa<ConstantSDNode>(Size) && "memcpy_inline needs constant size"); 6139 // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment. 6140 Align DstAlign = MCI.getDestAlign().valueOrOne(); 6141 Align SrcAlign = MCI.getSourceAlign().valueOrOne(); 6142 Align Alignment = std::min(DstAlign, SrcAlign); 6143 bool isVol = MCI.isVolatile(); 6144 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6145 // FIXME: Support passing different dest/src alignments to the memcpy DAG 6146 // node. 6147 SDValue MC = DAG.getMemcpy( 6148 getRoot(), sdl, Dst, Src, Size, Alignment, isVol, 6149 /* AlwaysInline */ true, isTC, MachinePointerInfo(I.getArgOperand(0)), 6150 MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), AA); 6151 updateDAGForMaybeTailCall(MC); 6152 return; 6153 } 6154 case Intrinsic::memset: { 6155 const auto &MSI = cast<MemSetInst>(I); 6156 SDValue Op1 = getValue(I.getArgOperand(0)); 6157 SDValue Op2 = getValue(I.getArgOperand(1)); 6158 SDValue Op3 = getValue(I.getArgOperand(2)); 6159 // @llvm.memset defines 0 and 1 to both mean no alignment. 6160 Align Alignment = MSI.getDestAlign().valueOrOne(); 6161 bool isVol = MSI.isVolatile(); 6162 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6163 SDValue Root = isVol ? getRoot() : getMemoryRoot(); 6164 SDValue MS = DAG.getMemset( 6165 Root, sdl, Op1, Op2, Op3, Alignment, isVol, /* AlwaysInline */ false, 6166 isTC, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata()); 6167 updateDAGForMaybeTailCall(MS); 6168 return; 6169 } 6170 case Intrinsic::memset_inline: { 6171 const auto &MSII = cast<MemSetInlineInst>(I); 6172 SDValue Dst = getValue(I.getArgOperand(0)); 6173 SDValue Value = getValue(I.getArgOperand(1)); 6174 SDValue Size = getValue(I.getArgOperand(2)); 6175 assert(isa<ConstantSDNode>(Size) && "memset_inline needs constant size"); 6176 // @llvm.memset defines 0 and 1 to both mean no alignment. 6177 Align DstAlign = MSII.getDestAlign().valueOrOne(); 6178 bool isVol = MSII.isVolatile(); 6179 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6180 SDValue Root = isVol ? getRoot() : getMemoryRoot(); 6181 SDValue MC = DAG.getMemset(Root, sdl, Dst, Value, Size, DstAlign, isVol, 6182 /* AlwaysInline */ true, isTC, 6183 MachinePointerInfo(I.getArgOperand(0)), 6184 I.getAAMetadata()); 6185 updateDAGForMaybeTailCall(MC); 6186 return; 6187 } 6188 case Intrinsic::memmove: { 6189 const auto &MMI = cast<MemMoveInst>(I); 6190 SDValue Op1 = getValue(I.getArgOperand(0)); 6191 SDValue Op2 = getValue(I.getArgOperand(1)); 6192 SDValue Op3 = getValue(I.getArgOperand(2)); 6193 // @llvm.memmove defines 0 and 1 to both mean no alignment. 6194 Align DstAlign = MMI.getDestAlign().valueOrOne(); 6195 Align SrcAlign = MMI.getSourceAlign().valueOrOne(); 6196 Align Alignment = std::min(DstAlign, SrcAlign); 6197 bool isVol = MMI.isVolatile(); 6198 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6199 // FIXME: Support passing different dest/src alignments to the memmove DAG 6200 // node. 6201 SDValue Root = isVol ? getRoot() : getMemoryRoot(); 6202 SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol, 6203 isTC, MachinePointerInfo(I.getArgOperand(0)), 6204 MachinePointerInfo(I.getArgOperand(1)), 6205 I.getAAMetadata(), AA); 6206 updateDAGForMaybeTailCall(MM); 6207 return; 6208 } 6209 case Intrinsic::memcpy_element_unordered_atomic: { 6210 const AtomicMemCpyInst &MI = cast<AtomicMemCpyInst>(I); 6211 SDValue Dst = getValue(MI.getRawDest()); 6212 SDValue Src = getValue(MI.getRawSource()); 6213 SDValue Length = getValue(MI.getLength()); 6214 6215 Type *LengthTy = MI.getLength()->getType(); 6216 unsigned ElemSz = MI.getElementSizeInBytes(); 6217 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6218 SDValue MC = 6219 DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz, 6220 isTC, MachinePointerInfo(MI.getRawDest()), 6221 MachinePointerInfo(MI.getRawSource())); 6222 updateDAGForMaybeTailCall(MC); 6223 return; 6224 } 6225 case Intrinsic::memmove_element_unordered_atomic: { 6226 auto &MI = cast<AtomicMemMoveInst>(I); 6227 SDValue Dst = getValue(MI.getRawDest()); 6228 SDValue Src = getValue(MI.getRawSource()); 6229 SDValue Length = getValue(MI.getLength()); 6230 6231 Type *LengthTy = MI.getLength()->getType(); 6232 unsigned ElemSz = MI.getElementSizeInBytes(); 6233 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6234 SDValue MC = 6235 DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz, 6236 isTC, MachinePointerInfo(MI.getRawDest()), 6237 MachinePointerInfo(MI.getRawSource())); 6238 updateDAGForMaybeTailCall(MC); 6239 return; 6240 } 6241 case Intrinsic::memset_element_unordered_atomic: { 6242 auto &MI = cast<AtomicMemSetInst>(I); 6243 SDValue Dst = getValue(MI.getRawDest()); 6244 SDValue Val = getValue(MI.getValue()); 6245 SDValue Length = getValue(MI.getLength()); 6246 6247 Type *LengthTy = MI.getLength()->getType(); 6248 unsigned ElemSz = MI.getElementSizeInBytes(); 6249 bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget()); 6250 SDValue MC = 6251 DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz, 6252 isTC, MachinePointerInfo(MI.getRawDest())); 6253 updateDAGForMaybeTailCall(MC); 6254 return; 6255 } 6256 case Intrinsic::call_preallocated_setup: { 6257 const CallBase *PreallocatedCall = FindPreallocatedCall(&I); 6258 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall); 6259 SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other, 6260 getRoot(), SrcValue); 6261 setValue(&I, Res); 6262 DAG.setRoot(Res); 6263 return; 6264 } 6265 case Intrinsic::call_preallocated_arg: { 6266 const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0)); 6267 SDValue SrcValue = DAG.getSrcValue(PreallocatedCall); 6268 SDValue Ops[3]; 6269 Ops[0] = getRoot(); 6270 Ops[1] = SrcValue; 6271 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl, 6272 MVT::i32); // arg index 6273 SDValue Res = DAG.getNode( 6274 ISD::PREALLOCATED_ARG, sdl, 6275 DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops); 6276 setValue(&I, Res); 6277 DAG.setRoot(Res.getValue(1)); 6278 return; 6279 } 6280 case Intrinsic::dbg_declare: { 6281 const auto &DI = cast<DbgDeclareInst>(I); 6282 // Debug intrinsics are handled separately in assignment tracking mode. 6283 // Some intrinsics are handled right after Argument lowering. 6284 if (AssignmentTrackingEnabled || 6285 FuncInfo.PreprocessedDbgDeclares.count(&DI)) 6286 return; 6287 LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DI << "\n"); 6288 DILocalVariable *Variable = DI.getVariable(); 6289 DIExpression *Expression = DI.getExpression(); 6290 dropDanglingDebugInfo(Variable, Expression); 6291 // Assume dbg.declare can not currently use DIArgList, i.e. 6292 // it is non-variadic. 6293 assert(!DI.hasArgList() && "Only dbg.value should currently use DIArgList"); 6294 handleDebugDeclare(DI.getVariableLocationOp(0), Variable, Expression, 6295 DI.getDebugLoc()); 6296 return; 6297 } 6298 case Intrinsic::dbg_label: { 6299 const DbgLabelInst &DI = cast<DbgLabelInst>(I); 6300 DILabel *Label = DI.getLabel(); 6301 assert(Label && "Missing label"); 6302 6303 SDDbgLabel *SDV; 6304 SDV = DAG.getDbgLabel(Label, dl, SDNodeOrder); 6305 DAG.AddDbgLabel(SDV); 6306 return; 6307 } 6308 case Intrinsic::dbg_assign: { 6309 // Debug intrinsics are handled seperately in assignment tracking mode. 6310 if (AssignmentTrackingEnabled) 6311 return; 6312 // If assignment tracking hasn't been enabled then fall through and treat 6313 // the dbg.assign as a dbg.value. 6314 [[fallthrough]]; 6315 } 6316 case Intrinsic::dbg_value: { 6317 // Debug intrinsics are handled seperately in assignment tracking mode. 6318 if (AssignmentTrackingEnabled) 6319 return; 6320 const DbgValueInst &DI = cast<DbgValueInst>(I); 6321 assert(DI.getVariable() && "Missing variable"); 6322 6323 DILocalVariable *Variable = DI.getVariable(); 6324 DIExpression *Expression = DI.getExpression(); 6325 dropDanglingDebugInfo(Variable, Expression); 6326 6327 if (visitEntryValueDbgValue(DI)) 6328 return; 6329 6330 if (DI.isKillLocation()) { 6331 handleKillDebugValue(Variable, Expression, DI.getDebugLoc(), SDNodeOrder); 6332 return; 6333 } 6334 6335 SmallVector<Value *, 4> Values(DI.getValues()); 6336 if (Values.empty()) 6337 return; 6338 6339 bool IsVariadic = DI.hasArgList(); 6340 if (!handleDebugValue(Values, Variable, Expression, DI.getDebugLoc(), 6341 SDNodeOrder, IsVariadic)) 6342 addDanglingDebugInfo(Values, Variable, Expression, IsVariadic, 6343 DI.getDebugLoc(), SDNodeOrder); 6344 return; 6345 } 6346 6347 case Intrinsic::eh_typeid_for: { 6348 // Find the type id for the given typeinfo. 6349 GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0)); 6350 unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV); 6351 Res = DAG.getConstant(TypeID, sdl, MVT::i32); 6352 setValue(&I, Res); 6353 return; 6354 } 6355 6356 case Intrinsic::eh_return_i32: 6357 case Intrinsic::eh_return_i64: 6358 DAG.getMachineFunction().setCallsEHReturn(true); 6359 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl, 6360 MVT::Other, 6361 getControlRoot(), 6362 getValue(I.getArgOperand(0)), 6363 getValue(I.getArgOperand(1)))); 6364 return; 6365 case Intrinsic::eh_unwind_init: 6366 DAG.getMachineFunction().setCallsUnwindInit(true); 6367 return; 6368 case Intrinsic::eh_dwarf_cfa: 6369 setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl, 6370 TLI.getPointerTy(DAG.getDataLayout()), 6371 getValue(I.getArgOperand(0)))); 6372 return; 6373 case Intrinsic::eh_sjlj_callsite: { 6374 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI(); 6375 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0)); 6376 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!"); 6377 6378 MMI.setCurrentCallSite(CI->getZExtValue()); 6379 return; 6380 } 6381 case Intrinsic::eh_sjlj_functioncontext: { 6382 // Get and store the index of the function context. 6383 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 6384 AllocaInst *FnCtx = 6385 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts()); 6386 int FI = FuncInfo.StaticAllocaMap[FnCtx]; 6387 MFI.setFunctionContextIndex(FI); 6388 return; 6389 } 6390 case Intrinsic::eh_sjlj_setjmp: { 6391 SDValue Ops[2]; 6392 Ops[0] = getRoot(); 6393 Ops[1] = getValue(I.getArgOperand(0)); 6394 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl, 6395 DAG.getVTList(MVT::i32, MVT::Other), Ops); 6396 setValue(&I, Op.getValue(0)); 6397 DAG.setRoot(Op.getValue(1)); 6398 return; 6399 } 6400 case Intrinsic::eh_sjlj_longjmp: 6401 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other, 6402 getRoot(), getValue(I.getArgOperand(0)))); 6403 return; 6404 case Intrinsic::eh_sjlj_setup_dispatch: 6405 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other, 6406 getRoot())); 6407 return; 6408 case Intrinsic::masked_gather: 6409 visitMaskedGather(I); 6410 return; 6411 case Intrinsic::masked_load: 6412 visitMaskedLoad(I); 6413 return; 6414 case Intrinsic::masked_scatter: 6415 visitMaskedScatter(I); 6416 return; 6417 case Intrinsic::masked_store: 6418 visitMaskedStore(I); 6419 return; 6420 case Intrinsic::masked_expandload: 6421 visitMaskedLoad(I, true /* IsExpanding */); 6422 return; 6423 case Intrinsic::masked_compressstore: 6424 visitMaskedStore(I, true /* IsCompressing */); 6425 return; 6426 case Intrinsic::powi: 6427 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)), 6428 getValue(I.getArgOperand(1)), DAG)); 6429 return; 6430 case Intrinsic::log: 6431 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags)); 6432 return; 6433 case Intrinsic::log2: 6434 setValue(&I, 6435 expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags)); 6436 return; 6437 case Intrinsic::log10: 6438 setValue(&I, 6439 expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags)); 6440 return; 6441 case Intrinsic::exp: 6442 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags)); 6443 return; 6444 case Intrinsic::exp2: 6445 setValue(&I, 6446 expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags)); 6447 return; 6448 case Intrinsic::pow: 6449 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)), 6450 getValue(I.getArgOperand(1)), DAG, TLI, Flags)); 6451 return; 6452 case Intrinsic::sqrt: 6453 case Intrinsic::fabs: 6454 case Intrinsic::sin: 6455 case Intrinsic::cos: 6456 case Intrinsic::exp10: 6457 case Intrinsic::floor: 6458 case Intrinsic::ceil: 6459 case Intrinsic::trunc: 6460 case Intrinsic::rint: 6461 case Intrinsic::nearbyint: 6462 case Intrinsic::round: 6463 case Intrinsic::roundeven: 6464 case Intrinsic::canonicalize: { 6465 unsigned Opcode; 6466 switch (Intrinsic) { 6467 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. 6468 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break; 6469 case Intrinsic::fabs: Opcode = ISD::FABS; break; 6470 case Intrinsic::sin: Opcode = ISD::FSIN; break; 6471 case Intrinsic::cos: Opcode = ISD::FCOS; break; 6472 case Intrinsic::exp10: Opcode = ISD::FEXP10; break; 6473 case Intrinsic::floor: Opcode = ISD::FFLOOR; break; 6474 case Intrinsic::ceil: Opcode = ISD::FCEIL; break; 6475 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break; 6476 case Intrinsic::rint: Opcode = ISD::FRINT; break; 6477 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break; 6478 case Intrinsic::round: Opcode = ISD::FROUND; break; 6479 case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break; 6480 case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break; 6481 } 6482 6483 setValue(&I, DAG.getNode(Opcode, sdl, 6484 getValue(I.getArgOperand(0)).getValueType(), 6485 getValue(I.getArgOperand(0)), Flags)); 6486 return; 6487 } 6488 case Intrinsic::lround: 6489 case Intrinsic::llround: 6490 case Intrinsic::lrint: 6491 case Intrinsic::llrint: { 6492 unsigned Opcode; 6493 switch (Intrinsic) { 6494 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. 6495 case Intrinsic::lround: Opcode = ISD::LROUND; break; 6496 case Intrinsic::llround: Opcode = ISD::LLROUND; break; 6497 case Intrinsic::lrint: Opcode = ISD::LRINT; break; 6498 case Intrinsic::llrint: Opcode = ISD::LLRINT; break; 6499 } 6500 6501 EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6502 setValue(&I, DAG.getNode(Opcode, sdl, RetVT, 6503 getValue(I.getArgOperand(0)))); 6504 return; 6505 } 6506 case Intrinsic::minnum: 6507 setValue(&I, DAG.getNode(ISD::FMINNUM, sdl, 6508 getValue(I.getArgOperand(0)).getValueType(), 6509 getValue(I.getArgOperand(0)), 6510 getValue(I.getArgOperand(1)), Flags)); 6511 return; 6512 case Intrinsic::maxnum: 6513 setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl, 6514 getValue(I.getArgOperand(0)).getValueType(), 6515 getValue(I.getArgOperand(0)), 6516 getValue(I.getArgOperand(1)), Flags)); 6517 return; 6518 case Intrinsic::minimum: 6519 setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl, 6520 getValue(I.getArgOperand(0)).getValueType(), 6521 getValue(I.getArgOperand(0)), 6522 getValue(I.getArgOperand(1)), Flags)); 6523 return; 6524 case Intrinsic::maximum: 6525 setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl, 6526 getValue(I.getArgOperand(0)).getValueType(), 6527 getValue(I.getArgOperand(0)), 6528 getValue(I.getArgOperand(1)), Flags)); 6529 return; 6530 case Intrinsic::copysign: 6531 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl, 6532 getValue(I.getArgOperand(0)).getValueType(), 6533 getValue(I.getArgOperand(0)), 6534 getValue(I.getArgOperand(1)), Flags)); 6535 return; 6536 case Intrinsic::ldexp: 6537 setValue(&I, DAG.getNode(ISD::FLDEXP, sdl, 6538 getValue(I.getArgOperand(0)).getValueType(), 6539 getValue(I.getArgOperand(0)), 6540 getValue(I.getArgOperand(1)), Flags)); 6541 return; 6542 case Intrinsic::frexp: { 6543 SmallVector<EVT, 2> ValueVTs; 6544 ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs); 6545 SDVTList VTs = DAG.getVTList(ValueVTs); 6546 setValue(&I, 6547 DAG.getNode(ISD::FFREXP, sdl, VTs, getValue(I.getArgOperand(0)))); 6548 return; 6549 } 6550 case Intrinsic::arithmetic_fence: { 6551 setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl, 6552 getValue(I.getArgOperand(0)).getValueType(), 6553 getValue(I.getArgOperand(0)), Flags)); 6554 return; 6555 } 6556 case Intrinsic::fma: 6557 setValue(&I, DAG.getNode( 6558 ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(), 6559 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), 6560 getValue(I.getArgOperand(2)), Flags)); 6561 return; 6562 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \ 6563 case Intrinsic::INTRINSIC: 6564 #include "llvm/IR/ConstrainedOps.def" 6565 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I)); 6566 return; 6567 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID: 6568 #include "llvm/IR/VPIntrinsics.def" 6569 visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I)); 6570 return; 6571 case Intrinsic::fptrunc_round: { 6572 // Get the last argument, the metadata and convert it to an integer in the 6573 // call 6574 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata(); 6575 std::optional<RoundingMode> RoundMode = 6576 convertStrToRoundingMode(cast<MDString>(MD)->getString()); 6577 6578 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6579 6580 // Propagate fast-math-flags from IR to node(s). 6581 SDNodeFlags Flags; 6582 Flags.copyFMF(*cast<FPMathOperator>(&I)); 6583 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags); 6584 6585 SDValue Result; 6586 Result = DAG.getNode( 6587 ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)), 6588 DAG.getTargetConstant((int)*RoundMode, sdl, 6589 TLI.getPointerTy(DAG.getDataLayout()))); 6590 setValue(&I, Result); 6591 6592 return; 6593 } 6594 case Intrinsic::fmuladd: { 6595 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6596 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && 6597 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) { 6598 setValue(&I, DAG.getNode(ISD::FMA, sdl, 6599 getValue(I.getArgOperand(0)).getValueType(), 6600 getValue(I.getArgOperand(0)), 6601 getValue(I.getArgOperand(1)), 6602 getValue(I.getArgOperand(2)), Flags)); 6603 } else { 6604 // TODO: Intrinsic calls should have fast-math-flags. 6605 SDValue Mul = DAG.getNode( 6606 ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(), 6607 getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags); 6608 SDValue Add = DAG.getNode(ISD::FADD, sdl, 6609 getValue(I.getArgOperand(0)).getValueType(), 6610 Mul, getValue(I.getArgOperand(2)), Flags); 6611 setValue(&I, Add); 6612 } 6613 return; 6614 } 6615 case Intrinsic::convert_to_fp16: 6616 setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16, 6617 DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16, 6618 getValue(I.getArgOperand(0)), 6619 DAG.getTargetConstant(0, sdl, 6620 MVT::i32)))); 6621 return; 6622 case Intrinsic::convert_from_fp16: 6623 setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl, 6624 TLI.getValueType(DAG.getDataLayout(), I.getType()), 6625 DAG.getNode(ISD::BITCAST, sdl, MVT::f16, 6626 getValue(I.getArgOperand(0))))); 6627 return; 6628 case Intrinsic::fptosi_sat: { 6629 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6630 setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT, 6631 getValue(I.getArgOperand(0)), 6632 DAG.getValueType(VT.getScalarType()))); 6633 return; 6634 } 6635 case Intrinsic::fptoui_sat: { 6636 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6637 setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT, 6638 getValue(I.getArgOperand(0)), 6639 DAG.getValueType(VT.getScalarType()))); 6640 return; 6641 } 6642 case Intrinsic::set_rounding: 6643 Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other, 6644 {getRoot(), getValue(I.getArgOperand(0))}); 6645 setValue(&I, Res); 6646 DAG.setRoot(Res.getValue(0)); 6647 return; 6648 case Intrinsic::is_fpclass: { 6649 const DataLayout DLayout = DAG.getDataLayout(); 6650 EVT DestVT = TLI.getValueType(DLayout, I.getType()); 6651 EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType()); 6652 FPClassTest Test = static_cast<FPClassTest>( 6653 cast<ConstantInt>(I.getArgOperand(1))->getZExtValue()); 6654 MachineFunction &MF = DAG.getMachineFunction(); 6655 const Function &F = MF.getFunction(); 6656 SDValue Op = getValue(I.getArgOperand(0)); 6657 SDNodeFlags Flags; 6658 Flags.setNoFPExcept( 6659 !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP)); 6660 // If ISD::IS_FPCLASS should be expanded, do it right now, because the 6661 // expansion can use illegal types. Making expansion early allows 6662 // legalizing these types prior to selection. 6663 if (!TLI.isOperationLegalOrCustom(ISD::IS_FPCLASS, ArgVT)) { 6664 SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG); 6665 setValue(&I, Result); 6666 return; 6667 } 6668 6669 SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32); 6670 SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags); 6671 setValue(&I, V); 6672 return; 6673 } 6674 case Intrinsic::get_fpenv: { 6675 const DataLayout DLayout = DAG.getDataLayout(); 6676 EVT EnvVT = TLI.getValueType(DLayout, I.getType()); 6677 Align TempAlign = DAG.getEVTAlign(EnvVT); 6678 SDValue Chain = getRoot(); 6679 // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node 6680 // and temporary storage in stack. 6681 if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) { 6682 Res = DAG.getNode( 6683 ISD::GET_FPENV, sdl, 6684 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()), 6685 MVT::Other), 6686 Chain); 6687 } else { 6688 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value()); 6689 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex(); 6690 auto MPI = 6691 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 6692 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 6693 MPI, MachineMemOperand::MOStore, MemoryLocation::UnknownSize, 6694 TempAlign); 6695 Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO); 6696 Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI); 6697 } 6698 setValue(&I, Res); 6699 DAG.setRoot(Res.getValue(1)); 6700 return; 6701 } 6702 case Intrinsic::set_fpenv: { 6703 const DataLayout DLayout = DAG.getDataLayout(); 6704 SDValue Env = getValue(I.getArgOperand(0)); 6705 EVT EnvVT = Env.getValueType(); 6706 Align TempAlign = DAG.getEVTAlign(EnvVT); 6707 SDValue Chain = getRoot(); 6708 // If SET_FPENV is custom or legal, use it. Otherwise use loading 6709 // environment from memory. 6710 if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) { 6711 Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env); 6712 } else { 6713 // Allocate space in stack, copy environment bits into it and use this 6714 // memory in SET_FPENV_MEM. 6715 SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value()); 6716 int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex(); 6717 auto MPI = 6718 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 6719 Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign, 6720 MachineMemOperand::MOStore); 6721 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 6722 MPI, MachineMemOperand::MOLoad, MemoryLocation::UnknownSize, 6723 TempAlign); 6724 Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO); 6725 } 6726 DAG.setRoot(Chain); 6727 return; 6728 } 6729 case Intrinsic::reset_fpenv: 6730 DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot())); 6731 return; 6732 case Intrinsic::get_fpmode: 6733 Res = DAG.getNode( 6734 ISD::GET_FPMODE, sdl, 6735 DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()), 6736 MVT::Other), 6737 DAG.getRoot()); 6738 setValue(&I, Res); 6739 DAG.setRoot(Res.getValue(1)); 6740 return; 6741 case Intrinsic::set_fpmode: 6742 Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()}, 6743 getValue(I.getArgOperand(0))); 6744 DAG.setRoot(Res); 6745 return; 6746 case Intrinsic::reset_fpmode: { 6747 Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot()); 6748 DAG.setRoot(Res); 6749 return; 6750 } 6751 case Intrinsic::pcmarker: { 6752 SDValue Tmp = getValue(I.getArgOperand(0)); 6753 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp)); 6754 return; 6755 } 6756 case Intrinsic::readcyclecounter: { 6757 SDValue Op = getRoot(); 6758 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl, 6759 DAG.getVTList(MVT::i64, MVT::Other), Op); 6760 setValue(&I, Res); 6761 DAG.setRoot(Res.getValue(1)); 6762 return; 6763 } 6764 case Intrinsic::bitreverse: 6765 setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl, 6766 getValue(I.getArgOperand(0)).getValueType(), 6767 getValue(I.getArgOperand(0)))); 6768 return; 6769 case Intrinsic::bswap: 6770 setValue(&I, DAG.getNode(ISD::BSWAP, sdl, 6771 getValue(I.getArgOperand(0)).getValueType(), 6772 getValue(I.getArgOperand(0)))); 6773 return; 6774 case Intrinsic::cttz: { 6775 SDValue Arg = getValue(I.getArgOperand(0)); 6776 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); 6777 EVT Ty = Arg.getValueType(); 6778 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF, 6779 sdl, Ty, Arg)); 6780 return; 6781 } 6782 case Intrinsic::ctlz: { 6783 SDValue Arg = getValue(I.getArgOperand(0)); 6784 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1)); 6785 EVT Ty = Arg.getValueType(); 6786 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF, 6787 sdl, Ty, Arg)); 6788 return; 6789 } 6790 case Intrinsic::ctpop: { 6791 SDValue Arg = getValue(I.getArgOperand(0)); 6792 EVT Ty = Arg.getValueType(); 6793 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg)); 6794 return; 6795 } 6796 case Intrinsic::fshl: 6797 case Intrinsic::fshr: { 6798 bool IsFSHL = Intrinsic == Intrinsic::fshl; 6799 SDValue X = getValue(I.getArgOperand(0)); 6800 SDValue Y = getValue(I.getArgOperand(1)); 6801 SDValue Z = getValue(I.getArgOperand(2)); 6802 EVT VT = X.getValueType(); 6803 6804 if (X == Y) { 6805 auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR; 6806 setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z)); 6807 } else { 6808 auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR; 6809 setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z)); 6810 } 6811 return; 6812 } 6813 case Intrinsic::sadd_sat: { 6814 SDValue Op1 = getValue(I.getArgOperand(0)); 6815 SDValue Op2 = getValue(I.getArgOperand(1)); 6816 setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2)); 6817 return; 6818 } 6819 case Intrinsic::uadd_sat: { 6820 SDValue Op1 = getValue(I.getArgOperand(0)); 6821 SDValue Op2 = getValue(I.getArgOperand(1)); 6822 setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2)); 6823 return; 6824 } 6825 case Intrinsic::ssub_sat: { 6826 SDValue Op1 = getValue(I.getArgOperand(0)); 6827 SDValue Op2 = getValue(I.getArgOperand(1)); 6828 setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2)); 6829 return; 6830 } 6831 case Intrinsic::usub_sat: { 6832 SDValue Op1 = getValue(I.getArgOperand(0)); 6833 SDValue Op2 = getValue(I.getArgOperand(1)); 6834 setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2)); 6835 return; 6836 } 6837 case Intrinsic::sshl_sat: { 6838 SDValue Op1 = getValue(I.getArgOperand(0)); 6839 SDValue Op2 = getValue(I.getArgOperand(1)); 6840 setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2)); 6841 return; 6842 } 6843 case Intrinsic::ushl_sat: { 6844 SDValue Op1 = getValue(I.getArgOperand(0)); 6845 SDValue Op2 = getValue(I.getArgOperand(1)); 6846 setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2)); 6847 return; 6848 } 6849 case Intrinsic::smul_fix: 6850 case Intrinsic::umul_fix: 6851 case Intrinsic::smul_fix_sat: 6852 case Intrinsic::umul_fix_sat: { 6853 SDValue Op1 = getValue(I.getArgOperand(0)); 6854 SDValue Op2 = getValue(I.getArgOperand(1)); 6855 SDValue Op3 = getValue(I.getArgOperand(2)); 6856 setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl, 6857 Op1.getValueType(), Op1, Op2, Op3)); 6858 return; 6859 } 6860 case Intrinsic::sdiv_fix: 6861 case Intrinsic::udiv_fix: 6862 case Intrinsic::sdiv_fix_sat: 6863 case Intrinsic::udiv_fix_sat: { 6864 SDValue Op1 = getValue(I.getArgOperand(0)); 6865 SDValue Op2 = getValue(I.getArgOperand(1)); 6866 SDValue Op3 = getValue(I.getArgOperand(2)); 6867 setValue(&I, expandDivFix(FixedPointIntrinsicToOpcode(Intrinsic), sdl, 6868 Op1, Op2, Op3, DAG, TLI)); 6869 return; 6870 } 6871 case Intrinsic::smax: { 6872 SDValue Op1 = getValue(I.getArgOperand(0)); 6873 SDValue Op2 = getValue(I.getArgOperand(1)); 6874 setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2)); 6875 return; 6876 } 6877 case Intrinsic::smin: { 6878 SDValue Op1 = getValue(I.getArgOperand(0)); 6879 SDValue Op2 = getValue(I.getArgOperand(1)); 6880 setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2)); 6881 return; 6882 } 6883 case Intrinsic::umax: { 6884 SDValue Op1 = getValue(I.getArgOperand(0)); 6885 SDValue Op2 = getValue(I.getArgOperand(1)); 6886 setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2)); 6887 return; 6888 } 6889 case Intrinsic::umin: { 6890 SDValue Op1 = getValue(I.getArgOperand(0)); 6891 SDValue Op2 = getValue(I.getArgOperand(1)); 6892 setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2)); 6893 return; 6894 } 6895 case Intrinsic::abs: { 6896 // TODO: Preserve "int min is poison" arg in SDAG? 6897 SDValue Op1 = getValue(I.getArgOperand(0)); 6898 setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1)); 6899 return; 6900 } 6901 case Intrinsic::stacksave: { 6902 SDValue Op = getRoot(); 6903 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6904 Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op); 6905 setValue(&I, Res); 6906 DAG.setRoot(Res.getValue(1)); 6907 return; 6908 } 6909 case Intrinsic::stackrestore: 6910 Res = getValue(I.getArgOperand(0)); 6911 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res)); 6912 return; 6913 case Intrinsic::get_dynamic_area_offset: { 6914 SDValue Op = getRoot(); 6915 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout()); 6916 EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6917 // Result type for @llvm.get.dynamic.area.offset should match PtrTy for 6918 // target. 6919 if (PtrTy.getFixedSizeInBits() < ResTy.getFixedSizeInBits()) 6920 report_fatal_error("Wrong result type for @llvm.get.dynamic.area.offset" 6921 " intrinsic!"); 6922 Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy), 6923 Op); 6924 DAG.setRoot(Op); 6925 setValue(&I, Res); 6926 return; 6927 } 6928 case Intrinsic::stackguard: { 6929 MachineFunction &MF = DAG.getMachineFunction(); 6930 const Module &M = *MF.getFunction().getParent(); 6931 SDValue Chain = getRoot(); 6932 if (TLI.useLoadStackGuardNode()) { 6933 Res = getLoadStackGuard(DAG, sdl, Chain); 6934 } else { 6935 EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType()); 6936 const Value *Global = TLI.getSDagStackGuard(M); 6937 Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType()); 6938 Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global), 6939 MachinePointerInfo(Global, 0), Align, 6940 MachineMemOperand::MOVolatile); 6941 } 6942 if (TLI.useStackGuardXorFP()) 6943 Res = TLI.emitStackGuardXorFP(DAG, Res, sdl); 6944 DAG.setRoot(Chain); 6945 setValue(&I, Res); 6946 return; 6947 } 6948 case Intrinsic::stackprotector: { 6949 // Emit code into the DAG to store the stack guard onto the stack. 6950 MachineFunction &MF = DAG.getMachineFunction(); 6951 MachineFrameInfo &MFI = MF.getFrameInfo(); 6952 SDValue Src, Chain = getRoot(); 6953 6954 if (TLI.useLoadStackGuardNode()) 6955 Src = getLoadStackGuard(DAG, sdl, Chain); 6956 else 6957 Src = getValue(I.getArgOperand(0)); // The guard's value. 6958 6959 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1)); 6960 6961 int FI = FuncInfo.StaticAllocaMap[Slot]; 6962 MFI.setStackProtectorIndex(FI); 6963 EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout()); 6964 6965 SDValue FIN = DAG.getFrameIndex(FI, PtrTy); 6966 6967 // Store the stack protector onto the stack. 6968 Res = DAG.getStore( 6969 Chain, sdl, Src, FIN, 6970 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 6971 MaybeAlign(), MachineMemOperand::MOVolatile); 6972 setValue(&I, Res); 6973 DAG.setRoot(Res); 6974 return; 6975 } 6976 case Intrinsic::objectsize: 6977 llvm_unreachable("llvm.objectsize.* should have been lowered already"); 6978 6979 case Intrinsic::is_constant: 6980 llvm_unreachable("llvm.is.constant.* should have been lowered already"); 6981 6982 case Intrinsic::annotation: 6983 case Intrinsic::ptr_annotation: 6984 case Intrinsic::launder_invariant_group: 6985 case Intrinsic::strip_invariant_group: 6986 // Drop the intrinsic, but forward the value 6987 setValue(&I, getValue(I.getOperand(0))); 6988 return; 6989 6990 case Intrinsic::assume: 6991 case Intrinsic::experimental_noalias_scope_decl: 6992 case Intrinsic::var_annotation: 6993 case Intrinsic::sideeffect: 6994 // Discard annotate attributes, noalias scope declarations, assumptions, and 6995 // artificial side-effects. 6996 return; 6997 6998 case Intrinsic::codeview_annotation: { 6999 // Emit a label associated with this metadata. 7000 MachineFunction &MF = DAG.getMachineFunction(); 7001 MCSymbol *Label = 7002 MF.getMMI().getContext().createTempSymbol("annotation", true); 7003 Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata(); 7004 MF.addCodeViewAnnotation(Label, cast<MDNode>(MD)); 7005 Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label); 7006 DAG.setRoot(Res); 7007 return; 7008 } 7009 7010 case Intrinsic::init_trampoline: { 7011 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts()); 7012 7013 SDValue Ops[6]; 7014 Ops[0] = getRoot(); 7015 Ops[1] = getValue(I.getArgOperand(0)); 7016 Ops[2] = getValue(I.getArgOperand(1)); 7017 Ops[3] = getValue(I.getArgOperand(2)); 7018 Ops[4] = DAG.getSrcValue(I.getArgOperand(0)); 7019 Ops[5] = DAG.getSrcValue(F); 7020 7021 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops); 7022 7023 DAG.setRoot(Res); 7024 return; 7025 } 7026 case Intrinsic::adjust_trampoline: 7027 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl, 7028 TLI.getPointerTy(DAG.getDataLayout()), 7029 getValue(I.getArgOperand(0)))); 7030 return; 7031 case Intrinsic::gcroot: { 7032 assert(DAG.getMachineFunction().getFunction().hasGC() && 7033 "only valid in functions with gc specified, enforced by Verifier"); 7034 assert(GFI && "implied by previous"); 7035 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts(); 7036 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1)); 7037 7038 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode()); 7039 GFI->addStackRoot(FI->getIndex(), TypeMap); 7040 return; 7041 } 7042 case Intrinsic::gcread: 7043 case Intrinsic::gcwrite: 7044 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!"); 7045 case Intrinsic::get_rounding: 7046 Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot()); 7047 setValue(&I, Res); 7048 DAG.setRoot(Res.getValue(1)); 7049 return; 7050 7051 case Intrinsic::expect: 7052 // Just replace __builtin_expect(exp, c) with EXP. 7053 setValue(&I, getValue(I.getArgOperand(0))); 7054 return; 7055 7056 case Intrinsic::ubsantrap: 7057 case Intrinsic::debugtrap: 7058 case Intrinsic::trap: { 7059 StringRef TrapFuncName = 7060 I.getAttributes().getFnAttr("trap-func-name").getValueAsString(); 7061 if (TrapFuncName.empty()) { 7062 switch (Intrinsic) { 7063 case Intrinsic::trap: 7064 DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot())); 7065 break; 7066 case Intrinsic::debugtrap: 7067 DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot())); 7068 break; 7069 case Intrinsic::ubsantrap: 7070 DAG.setRoot(DAG.getNode( 7071 ISD::UBSANTRAP, sdl, MVT::Other, getRoot(), 7072 DAG.getTargetConstant( 7073 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl, 7074 MVT::i32))); 7075 break; 7076 default: llvm_unreachable("unknown trap intrinsic"); 7077 } 7078 return; 7079 } 7080 TargetLowering::ArgListTy Args; 7081 if (Intrinsic == Intrinsic::ubsantrap) { 7082 Args.push_back(TargetLoweringBase::ArgListEntry()); 7083 Args[0].Val = I.getArgOperand(0); 7084 Args[0].Node = getValue(Args[0].Val); 7085 Args[0].Ty = Args[0].Val->getType(); 7086 } 7087 7088 TargetLowering::CallLoweringInfo CLI(DAG); 7089 CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee( 7090 CallingConv::C, I.getType(), 7091 DAG.getExternalSymbol(TrapFuncName.data(), 7092 TLI.getPointerTy(DAG.getDataLayout())), 7093 std::move(Args)); 7094 7095 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); 7096 DAG.setRoot(Result.second); 7097 return; 7098 } 7099 7100 case Intrinsic::uadd_with_overflow: 7101 case Intrinsic::sadd_with_overflow: 7102 case Intrinsic::usub_with_overflow: 7103 case Intrinsic::ssub_with_overflow: 7104 case Intrinsic::umul_with_overflow: 7105 case Intrinsic::smul_with_overflow: { 7106 ISD::NodeType Op; 7107 switch (Intrinsic) { 7108 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. 7109 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break; 7110 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break; 7111 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break; 7112 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break; 7113 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break; 7114 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break; 7115 } 7116 SDValue Op1 = getValue(I.getArgOperand(0)); 7117 SDValue Op2 = getValue(I.getArgOperand(1)); 7118 7119 EVT ResultVT = Op1.getValueType(); 7120 EVT OverflowVT = MVT::i1; 7121 if (ResultVT.isVector()) 7122 OverflowVT = EVT::getVectorVT( 7123 *Context, OverflowVT, ResultVT.getVectorElementCount()); 7124 7125 SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT); 7126 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2)); 7127 return; 7128 } 7129 case Intrinsic::prefetch: { 7130 SDValue Ops[5]; 7131 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); 7132 auto Flags = rw == 0 ? MachineMemOperand::MOLoad :MachineMemOperand::MOStore; 7133 Ops[0] = DAG.getRoot(); 7134 Ops[1] = getValue(I.getArgOperand(0)); 7135 Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl, 7136 MVT::i32); 7137 Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl, 7138 MVT::i32); 7139 Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl, 7140 MVT::i32); 7141 SDValue Result = DAG.getMemIntrinsicNode( 7142 ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops, 7143 EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)), 7144 /* align */ std::nullopt, Flags); 7145 7146 // Chain the prefetch in parallel with any pending loads, to stay out of 7147 // the way of later optimizations. 7148 PendingLoads.push_back(Result); 7149 Result = getRoot(); 7150 DAG.setRoot(Result); 7151 return; 7152 } 7153 case Intrinsic::lifetime_start: 7154 case Intrinsic::lifetime_end: { 7155 bool IsStart = (Intrinsic == Intrinsic::lifetime_start); 7156 // Stack coloring is not enabled in O0, discard region information. 7157 if (TM.getOptLevel() == CodeGenOptLevel::None) 7158 return; 7159 7160 const int64_t ObjectSize = 7161 cast<ConstantInt>(I.getArgOperand(0))->getSExtValue(); 7162 Value *const ObjectPtr = I.getArgOperand(1); 7163 SmallVector<const Value *, 4> Allocas; 7164 getUnderlyingObjects(ObjectPtr, Allocas); 7165 7166 for (const Value *Alloca : Allocas) { 7167 const AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(Alloca); 7168 7169 // Could not find an Alloca. 7170 if (!LifetimeObject) 7171 continue; 7172 7173 // First check that the Alloca is static, otherwise it won't have a 7174 // valid frame index. 7175 auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject); 7176 if (SI == FuncInfo.StaticAllocaMap.end()) 7177 return; 7178 7179 const int FrameIndex = SI->second; 7180 int64_t Offset; 7181 if (GetPointerBaseWithConstantOffset( 7182 ObjectPtr, Offset, DAG.getDataLayout()) != LifetimeObject) 7183 Offset = -1; // Cannot determine offset from alloca to lifetime object. 7184 Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex, ObjectSize, 7185 Offset); 7186 DAG.setRoot(Res); 7187 } 7188 return; 7189 } 7190 case Intrinsic::pseudoprobe: { 7191 auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(); 7192 auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue(); 7193 auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue(); 7194 Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr); 7195 DAG.setRoot(Res); 7196 return; 7197 } 7198 case Intrinsic::invariant_start: 7199 // Discard region information. 7200 setValue(&I, 7201 DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType()))); 7202 return; 7203 case Intrinsic::invariant_end: 7204 // Discard region information. 7205 return; 7206 case Intrinsic::clear_cache: 7207 /// FunctionName may be null. 7208 if (const char *FunctionName = TLI.getClearCacheBuiltinName()) 7209 lowerCallToExternalSymbol(I, FunctionName); 7210 return; 7211 case Intrinsic::donothing: 7212 case Intrinsic::seh_try_begin: 7213 case Intrinsic::seh_scope_begin: 7214 case Intrinsic::seh_try_end: 7215 case Intrinsic::seh_scope_end: 7216 // ignore 7217 return; 7218 case Intrinsic::experimental_stackmap: 7219 visitStackmap(I); 7220 return; 7221 case Intrinsic::experimental_patchpoint_void: 7222 case Intrinsic::experimental_patchpoint_i64: 7223 visitPatchpoint(I); 7224 return; 7225 case Intrinsic::experimental_gc_statepoint: 7226 LowerStatepoint(cast<GCStatepointInst>(I)); 7227 return; 7228 case Intrinsic::experimental_gc_result: 7229 visitGCResult(cast<GCResultInst>(I)); 7230 return; 7231 case Intrinsic::experimental_gc_relocate: 7232 visitGCRelocate(cast<GCRelocateInst>(I)); 7233 return; 7234 case Intrinsic::instrprof_cover: 7235 llvm_unreachable("instrprof failed to lower a cover"); 7236 case Intrinsic::instrprof_increment: 7237 llvm_unreachable("instrprof failed to lower an increment"); 7238 case Intrinsic::instrprof_timestamp: 7239 llvm_unreachable("instrprof failed to lower a timestamp"); 7240 case Intrinsic::instrprof_value_profile: 7241 llvm_unreachable("instrprof failed to lower a value profiling call"); 7242 case Intrinsic::instrprof_mcdc_parameters: 7243 llvm_unreachable("instrprof failed to lower mcdc parameters"); 7244 case Intrinsic::instrprof_mcdc_tvbitmap_update: 7245 llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update"); 7246 case Intrinsic::instrprof_mcdc_condbitmap_update: 7247 llvm_unreachable("instrprof failed to lower an mcdc condbitmap update"); 7248 case Intrinsic::localescape: { 7249 MachineFunction &MF = DAG.getMachineFunction(); 7250 const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo(); 7251 7252 // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission 7253 // is the same on all targets. 7254 for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) { 7255 Value *Arg = I.getArgOperand(Idx)->stripPointerCasts(); 7256 if (isa<ConstantPointerNull>(Arg)) 7257 continue; // Skip null pointers. They represent a hole in index space. 7258 AllocaInst *Slot = cast<AllocaInst>(Arg); 7259 assert(FuncInfo.StaticAllocaMap.count(Slot) && 7260 "can only escape static allocas"); 7261 int FI = FuncInfo.StaticAllocaMap[Slot]; 7262 MCSymbol *FrameAllocSym = 7263 MF.getMMI().getContext().getOrCreateFrameAllocSymbol( 7264 GlobalValue::dropLLVMManglingEscape(MF.getName()), Idx); 7265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl, 7266 TII->get(TargetOpcode::LOCAL_ESCAPE)) 7267 .addSym(FrameAllocSym) 7268 .addFrameIndex(FI); 7269 } 7270 7271 return; 7272 } 7273 7274 case Intrinsic::localrecover: { 7275 // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx) 7276 MachineFunction &MF = DAG.getMachineFunction(); 7277 7278 // Get the symbol that defines the frame offset. 7279 auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts()); 7280 auto *Idx = cast<ConstantInt>(I.getArgOperand(2)); 7281 unsigned IdxVal = 7282 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max())); 7283 MCSymbol *FrameAllocSym = 7284 MF.getMMI().getContext().getOrCreateFrameAllocSymbol( 7285 GlobalValue::dropLLVMManglingEscape(Fn->getName()), IdxVal); 7286 7287 Value *FP = I.getArgOperand(1); 7288 SDValue FPVal = getValue(FP); 7289 EVT PtrVT = FPVal.getValueType(); 7290 7291 // Create a MCSymbol for the label to avoid any target lowering 7292 // that would make this PC relative. 7293 SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT); 7294 SDValue OffsetVal = 7295 DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym); 7296 7297 // Add the offset to the FP. 7298 SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl); 7299 setValue(&I, Add); 7300 7301 return; 7302 } 7303 7304 case Intrinsic::eh_exceptionpointer: 7305 case Intrinsic::eh_exceptioncode: { 7306 // Get the exception pointer vreg, copy from it, and resize it to fit. 7307 const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0)); 7308 MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); 7309 const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT); 7310 unsigned VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC); 7311 SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT); 7312 if (Intrinsic == Intrinsic::eh_exceptioncode) 7313 N = DAG.getZExtOrTrunc(N, sdl, MVT::i32); 7314 setValue(&I, N); 7315 return; 7316 } 7317 case Intrinsic::xray_customevent: { 7318 // Here we want to make sure that the intrinsic behaves as if it has a 7319 // specific calling convention. 7320 const auto &Triple = DAG.getTarget().getTargetTriple(); 7321 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64) 7322 return; 7323 7324 SmallVector<SDValue, 8> Ops; 7325 7326 // We want to say that we always want the arguments in registers. 7327 SDValue LogEntryVal = getValue(I.getArgOperand(0)); 7328 SDValue StrSizeVal = getValue(I.getArgOperand(1)); 7329 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 7330 SDValue Chain = getRoot(); 7331 Ops.push_back(LogEntryVal); 7332 Ops.push_back(StrSizeVal); 7333 Ops.push_back(Chain); 7334 7335 // We need to enforce the calling convention for the callsite, so that 7336 // argument ordering is enforced correctly, and that register allocation can 7337 // see that some registers may be assumed clobbered and have to preserve 7338 // them across calls to the intrinsic. 7339 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL, 7340 sdl, NodeTys, Ops); 7341 SDValue patchableNode = SDValue(MN, 0); 7342 DAG.setRoot(patchableNode); 7343 setValue(&I, patchableNode); 7344 return; 7345 } 7346 case Intrinsic::xray_typedevent: { 7347 // Here we want to make sure that the intrinsic behaves as if it has a 7348 // specific calling convention. 7349 const auto &Triple = DAG.getTarget().getTargetTriple(); 7350 if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64) 7351 return; 7352 7353 SmallVector<SDValue, 8> Ops; 7354 7355 // We want to say that we always want the arguments in registers. 7356 // It's unclear to me how manipulating the selection DAG here forces callers 7357 // to provide arguments in registers instead of on the stack. 7358 SDValue LogTypeId = getValue(I.getArgOperand(0)); 7359 SDValue LogEntryVal = getValue(I.getArgOperand(1)); 7360 SDValue StrSizeVal = getValue(I.getArgOperand(2)); 7361 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 7362 SDValue Chain = getRoot(); 7363 Ops.push_back(LogTypeId); 7364 Ops.push_back(LogEntryVal); 7365 Ops.push_back(StrSizeVal); 7366 Ops.push_back(Chain); 7367 7368 // We need to enforce the calling convention for the callsite, so that 7369 // argument ordering is enforced correctly, and that register allocation can 7370 // see that some registers may be assumed clobbered and have to preserve 7371 // them across calls to the intrinsic. 7372 MachineSDNode *MN = DAG.getMachineNode( 7373 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops); 7374 SDValue patchableNode = SDValue(MN, 0); 7375 DAG.setRoot(patchableNode); 7376 setValue(&I, patchableNode); 7377 return; 7378 } 7379 case Intrinsic::experimental_deoptimize: 7380 LowerDeoptimizeCall(&I); 7381 return; 7382 case Intrinsic::experimental_stepvector: 7383 visitStepVector(I); 7384 return; 7385 case Intrinsic::vector_reduce_fadd: 7386 case Intrinsic::vector_reduce_fmul: 7387 case Intrinsic::vector_reduce_add: 7388 case Intrinsic::vector_reduce_mul: 7389 case Intrinsic::vector_reduce_and: 7390 case Intrinsic::vector_reduce_or: 7391 case Intrinsic::vector_reduce_xor: 7392 case Intrinsic::vector_reduce_smax: 7393 case Intrinsic::vector_reduce_smin: 7394 case Intrinsic::vector_reduce_umax: 7395 case Intrinsic::vector_reduce_umin: 7396 case Intrinsic::vector_reduce_fmax: 7397 case Intrinsic::vector_reduce_fmin: 7398 case Intrinsic::vector_reduce_fmaximum: 7399 case Intrinsic::vector_reduce_fminimum: 7400 visitVectorReduce(I, Intrinsic); 7401 return; 7402 7403 case Intrinsic::icall_branch_funnel: { 7404 SmallVector<SDValue, 16> Ops; 7405 Ops.push_back(getValue(I.getArgOperand(0))); 7406 7407 int64_t Offset; 7408 auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset( 7409 I.getArgOperand(1), Offset, DAG.getDataLayout())); 7410 if (!Base) 7411 report_fatal_error( 7412 "llvm.icall.branch.funnel operand must be a GlobalValue"); 7413 Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0)); 7414 7415 struct BranchFunnelTarget { 7416 int64_t Offset; 7417 SDValue Target; 7418 }; 7419 SmallVector<BranchFunnelTarget, 8> Targets; 7420 7421 for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) { 7422 auto *ElemBase = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset( 7423 I.getArgOperand(Op), Offset, DAG.getDataLayout())); 7424 if (ElemBase != Base) 7425 report_fatal_error("all llvm.icall.branch.funnel operands must refer " 7426 "to the same GlobalValue"); 7427 7428 SDValue Val = getValue(I.getArgOperand(Op + 1)); 7429 auto *GA = dyn_cast<GlobalAddressSDNode>(Val); 7430 if (!GA) 7431 report_fatal_error( 7432 "llvm.icall.branch.funnel operand must be a GlobalValue"); 7433 Targets.push_back({Offset, DAG.getTargetGlobalAddress( 7434 GA->getGlobal(), sdl, Val.getValueType(), 7435 GA->getOffset())}); 7436 } 7437 llvm::sort(Targets, 7438 [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) { 7439 return T1.Offset < T2.Offset; 7440 }); 7441 7442 for (auto &T : Targets) { 7443 Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32)); 7444 Ops.push_back(T.Target); 7445 } 7446 7447 Ops.push_back(DAG.getRoot()); // Chain 7448 SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl, 7449 MVT::Other, Ops), 7450 0); 7451 DAG.setRoot(N); 7452 setValue(&I, N); 7453 HasTailCall = true; 7454 return; 7455 } 7456 7457 case Intrinsic::wasm_landingpad_index: 7458 // Information this intrinsic contained has been transferred to 7459 // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely 7460 // delete it now. 7461 return; 7462 7463 case Intrinsic::aarch64_settag: 7464 case Intrinsic::aarch64_settag_zero: { 7465 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 7466 bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero; 7467 SDValue Val = TSI.EmitTargetCodeForSetTag( 7468 DAG, sdl, getRoot(), getValue(I.getArgOperand(0)), 7469 getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)), 7470 ZeroMemory); 7471 DAG.setRoot(Val); 7472 setValue(&I, Val); 7473 return; 7474 } 7475 case Intrinsic::amdgcn_cs_chain: { 7476 assert(I.arg_size() == 5 && "Additional args not supported yet"); 7477 assert(cast<ConstantInt>(I.getOperand(4))->isZero() && 7478 "Non-zero flags not supported yet"); 7479 7480 // At this point we don't care if it's amdgpu_cs_chain or 7481 // amdgpu_cs_chain_preserve. 7482 CallingConv::ID CC = CallingConv::AMDGPU_CS_Chain; 7483 7484 Type *RetTy = I.getType(); 7485 assert(RetTy->isVoidTy() && "Should not return"); 7486 7487 SDValue Callee = getValue(I.getOperand(0)); 7488 7489 // We only have 2 actual args: one for the SGPRs and one for the VGPRs. 7490 // We'll also tack the value of the EXEC mask at the end. 7491 TargetLowering::ArgListTy Args; 7492 Args.reserve(3); 7493 7494 for (unsigned Idx : {2, 3, 1}) { 7495 TargetLowering::ArgListEntry Arg; 7496 Arg.Node = getValue(I.getOperand(Idx)); 7497 Arg.Ty = I.getOperand(Idx)->getType(); 7498 Arg.setAttributes(&I, Idx); 7499 Args.push_back(Arg); 7500 } 7501 7502 assert(Args[0].IsInReg && "SGPR args should be marked inreg"); 7503 assert(!Args[1].IsInReg && "VGPR args should not be marked inreg"); 7504 Args[2].IsInReg = true; // EXEC should be inreg 7505 7506 TargetLowering::CallLoweringInfo CLI(DAG); 7507 CLI.setDebugLoc(getCurSDLoc()) 7508 .setChain(getRoot()) 7509 .setCallee(CC, RetTy, Callee, std::move(Args)) 7510 .setNoReturn(true) 7511 .setTailCall(true) 7512 .setConvergent(I.isConvergent()); 7513 CLI.CB = &I; 7514 std::pair<SDValue, SDValue> Result = 7515 lowerInvokable(CLI, /*EHPadBB*/ nullptr); 7516 (void)Result; 7517 assert(!Result.first.getNode() && !Result.second.getNode() && 7518 "Should've lowered as tail call"); 7519 7520 HasTailCall = true; 7521 return; 7522 } 7523 case Intrinsic::ptrmask: { 7524 SDValue Ptr = getValue(I.getOperand(0)); 7525 SDValue Mask = getValue(I.getOperand(1)); 7526 7527 EVT PtrVT = Ptr.getValueType(); 7528 assert(PtrVT == Mask.getValueType() && 7529 "Pointers with different index type are not supported by SDAG"); 7530 setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask)); 7531 return; 7532 } 7533 case Intrinsic::threadlocal_address: { 7534 setValue(&I, getValue(I.getOperand(0))); 7535 return; 7536 } 7537 case Intrinsic::get_active_lane_mask: { 7538 EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 7539 SDValue Index = getValue(I.getOperand(0)); 7540 EVT ElementVT = Index.getValueType(); 7541 7542 if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) { 7543 visitTargetIntrinsic(I, Intrinsic); 7544 return; 7545 } 7546 7547 SDValue TripCount = getValue(I.getOperand(1)); 7548 EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT, 7549 CCVT.getVectorElementCount()); 7550 7551 SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index); 7552 SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount); 7553 SDValue VectorStep = DAG.getStepVector(sdl, VecTy); 7554 SDValue VectorInduction = DAG.getNode( 7555 ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep); 7556 SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction, 7557 VectorTripCount, ISD::CondCode::SETULT); 7558 setValue(&I, SetCC); 7559 return; 7560 } 7561 case Intrinsic::experimental_get_vector_length: { 7562 assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 && 7563 "Expected positive VF"); 7564 unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue(); 7565 bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne(); 7566 7567 SDValue Count = getValue(I.getOperand(0)); 7568 EVT CountVT = Count.getValueType(); 7569 7570 if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) { 7571 visitTargetIntrinsic(I, Intrinsic); 7572 return; 7573 } 7574 7575 // Expand to a umin between the trip count and the maximum elements the type 7576 // can hold. 7577 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 7578 7579 // Extend the trip count to at least the result VT. 7580 if (CountVT.bitsLT(VT)) { 7581 Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count); 7582 CountVT = VT; 7583 } 7584 7585 SDValue MaxEVL = DAG.getElementCount(sdl, CountVT, 7586 ElementCount::get(VF, IsScalable)); 7587 7588 SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL); 7589 // Clip to the result type if needed. 7590 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin); 7591 7592 setValue(&I, Trunc); 7593 return; 7594 } 7595 case Intrinsic::experimental_cttz_elts: { 7596 auto DL = getCurSDLoc(); 7597 SDValue Op = getValue(I.getOperand(0)); 7598 EVT OpVT = Op.getValueType(); 7599 7600 if (!TLI.shouldExpandCttzElements(OpVT)) { 7601 visitTargetIntrinsic(I, Intrinsic); 7602 return; 7603 } 7604 7605 if (OpVT.getScalarType() != MVT::i1) { 7606 // Compare the input vector elements to zero & use to count trailing zeros 7607 SDValue AllZero = DAG.getConstant(0, DL, OpVT); 7608 OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, 7609 OpVT.getVectorElementCount()); 7610 Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE); 7611 } 7612 7613 // Find the smallest "sensible" element type to use for the expansion. 7614 ConstantRange CR( 7615 APInt(64, OpVT.getVectorElementCount().getKnownMinValue())); 7616 if (OpVT.isScalableVT()) 7617 CR = CR.umul_sat(getVScaleRange(I.getCaller(), 64)); 7618 7619 // If the zero-is-poison flag is set, we can assume the upper limit 7620 // of the result is VF-1. 7621 if (!cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero()) 7622 CR = CR.subtract(APInt(64, 1)); 7623 7624 unsigned EltWidth = I.getType()->getScalarSizeInBits(); 7625 EltWidth = std::min(EltWidth, (unsigned)CR.getActiveBits()); 7626 EltWidth = std::max(llvm::bit_ceil(EltWidth), (unsigned)8); 7627 7628 MVT NewEltTy = MVT::getIntegerVT(EltWidth); 7629 7630 // Create the new vector type & get the vector length 7631 EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy, 7632 OpVT.getVectorElementCount()); 7633 7634 SDValue VL = 7635 DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount()); 7636 7637 SDValue StepVec = DAG.getStepVector(DL, NewVT); 7638 SDValue SplatVL = DAG.getSplat(NewVT, DL, VL); 7639 SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec); 7640 SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op); 7641 SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext); 7642 SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And); 7643 SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max); 7644 7645 EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType()); 7646 SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy); 7647 7648 setValue(&I, Ret); 7649 return; 7650 } 7651 case Intrinsic::vector_insert: { 7652 SDValue Vec = getValue(I.getOperand(0)); 7653 SDValue SubVec = getValue(I.getOperand(1)); 7654 SDValue Index = getValue(I.getOperand(2)); 7655 7656 // The intrinsic's index type is i64, but the SDNode requires an index type 7657 // suitable for the target. Convert the index as required. 7658 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout()); 7659 if (Index.getValueType() != VectorIdxTy) 7660 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl); 7661 7662 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 7663 setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec, 7664 Index)); 7665 return; 7666 } 7667 case Intrinsic::vector_extract: { 7668 SDValue Vec = getValue(I.getOperand(0)); 7669 SDValue Index = getValue(I.getOperand(1)); 7670 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 7671 7672 // The intrinsic's index type is i64, but the SDNode requires an index type 7673 // suitable for the target. Convert the index as required. 7674 MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout()); 7675 if (Index.getValueType() != VectorIdxTy) 7676 Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl); 7677 7678 setValue(&I, 7679 DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index)); 7680 return; 7681 } 7682 case Intrinsic::experimental_vector_reverse: 7683 visitVectorReverse(I); 7684 return; 7685 case Intrinsic::experimental_vector_splice: 7686 visitVectorSplice(I); 7687 return; 7688 case Intrinsic::callbr_landingpad: 7689 visitCallBrLandingPad(I); 7690 return; 7691 case Intrinsic::experimental_vector_interleave2: 7692 visitVectorInterleave(I); 7693 return; 7694 case Intrinsic::experimental_vector_deinterleave2: 7695 visitVectorDeinterleave(I); 7696 return; 7697 } 7698 } 7699 7700 void SelectionDAGBuilder::visitConstrainedFPIntrinsic( 7701 const ConstrainedFPIntrinsic &FPI) { 7702 SDLoc sdl = getCurSDLoc(); 7703 7704 // We do not need to serialize constrained FP intrinsics against 7705 // each other or against (nonvolatile) loads, so they can be 7706 // chained like loads. 7707 SDValue Chain = DAG.getRoot(); 7708 SmallVector<SDValue, 4> Opers; 7709 Opers.push_back(Chain); 7710 if (FPI.isUnaryOp()) { 7711 Opers.push_back(getValue(FPI.getArgOperand(0))); 7712 } else if (FPI.isTernaryOp()) { 7713 Opers.push_back(getValue(FPI.getArgOperand(0))); 7714 Opers.push_back(getValue(FPI.getArgOperand(1))); 7715 Opers.push_back(getValue(FPI.getArgOperand(2))); 7716 } else { 7717 Opers.push_back(getValue(FPI.getArgOperand(0))); 7718 Opers.push_back(getValue(FPI.getArgOperand(1))); 7719 } 7720 7721 auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) { 7722 assert(Result.getNode()->getNumValues() == 2); 7723 7724 // Push node to the appropriate list so that future instructions can be 7725 // chained up correctly. 7726 SDValue OutChain = Result.getValue(1); 7727 switch (EB) { 7728 case fp::ExceptionBehavior::ebIgnore: 7729 // The only reason why ebIgnore nodes still need to be chained is that 7730 // they might depend on the current rounding mode, and therefore must 7731 // not be moved across instruction that may change that mode. 7732 [[fallthrough]]; 7733 case fp::ExceptionBehavior::ebMayTrap: 7734 // These must not be moved across calls or instructions that may change 7735 // floating-point exception masks. 7736 PendingConstrainedFP.push_back(OutChain); 7737 break; 7738 case fp::ExceptionBehavior::ebStrict: 7739 // These must not be moved across calls or instructions that may change 7740 // floating-point exception masks or read floating-point exception flags. 7741 // In addition, they cannot be optimized out even if unused. 7742 PendingConstrainedFPStrict.push_back(OutChain); 7743 break; 7744 } 7745 }; 7746 7747 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7748 EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType()); 7749 SDVTList VTs = DAG.getVTList(VT, MVT::Other); 7750 fp::ExceptionBehavior EB = *FPI.getExceptionBehavior(); 7751 7752 SDNodeFlags Flags; 7753 if (EB == fp::ExceptionBehavior::ebIgnore) 7754 Flags.setNoFPExcept(true); 7755 7756 if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI)) 7757 Flags.copyFMF(*FPOp); 7758 7759 unsigned Opcode; 7760 switch (FPI.getIntrinsicID()) { 7761 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. 7762 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \ 7763 case Intrinsic::INTRINSIC: \ 7764 Opcode = ISD::STRICT_##DAGN; \ 7765 break; 7766 #include "llvm/IR/ConstrainedOps.def" 7767 case Intrinsic::experimental_constrained_fmuladd: { 7768 Opcode = ISD::STRICT_FMA; 7769 // Break fmuladd into fmul and fadd. 7770 if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict || 7771 !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) { 7772 Opers.pop_back(); 7773 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags); 7774 pushOutChain(Mul, EB); 7775 Opcode = ISD::STRICT_FADD; 7776 Opers.clear(); 7777 Opers.push_back(Mul.getValue(1)); 7778 Opers.push_back(Mul.getValue(0)); 7779 Opers.push_back(getValue(FPI.getArgOperand(2))); 7780 } 7781 break; 7782 } 7783 } 7784 7785 // A few strict DAG nodes carry additional operands that are not 7786 // set up by the default code above. 7787 switch (Opcode) { 7788 default: break; 7789 case ISD::STRICT_FP_ROUND: 7790 Opers.push_back( 7791 DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()))); 7792 break; 7793 case ISD::STRICT_FSETCC: 7794 case ISD::STRICT_FSETCCS: { 7795 auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI); 7796 ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate()); 7797 if (TM.Options.NoNaNsFPMath) 7798 Condition = getFCmpCodeWithoutNaN(Condition); 7799 Opers.push_back(DAG.getCondCode(Condition)); 7800 break; 7801 } 7802 } 7803 7804 SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags); 7805 pushOutChain(Result, EB); 7806 7807 SDValue FPResult = Result.getValue(0); 7808 setValue(&FPI, FPResult); 7809 } 7810 7811 static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) { 7812 std::optional<unsigned> ResOPC; 7813 switch (VPIntrin.getIntrinsicID()) { 7814 case Intrinsic::vp_ctlz: { 7815 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne(); 7816 ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ; 7817 break; 7818 } 7819 case Intrinsic::vp_cttz: { 7820 bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne(); 7821 ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ; 7822 break; 7823 } 7824 #define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD) \ 7825 case Intrinsic::VPID: \ 7826 ResOPC = ISD::VPSD; \ 7827 break; 7828 #include "llvm/IR/VPIntrinsics.def" 7829 } 7830 7831 if (!ResOPC) 7832 llvm_unreachable( 7833 "Inconsistency: no SDNode available for this VPIntrinsic!"); 7834 7835 if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD || 7836 *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) { 7837 if (VPIntrin.getFastMathFlags().allowReassoc()) 7838 return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD 7839 : ISD::VP_REDUCE_FMUL; 7840 } 7841 7842 return *ResOPC; 7843 } 7844 7845 void SelectionDAGBuilder::visitVPLoad( 7846 const VPIntrinsic &VPIntrin, EVT VT, 7847 const SmallVectorImpl<SDValue> &OpValues) { 7848 SDLoc DL = getCurSDLoc(); 7849 Value *PtrOperand = VPIntrin.getArgOperand(0); 7850 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 7851 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 7852 const MDNode *Ranges = getRangeMetadata(VPIntrin); 7853 SDValue LD; 7854 // Do not serialize variable-length loads of constant memory with 7855 // anything. 7856 if (!Alignment) 7857 Alignment = DAG.getEVTAlign(VT); 7858 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo); 7859 bool AddToChain = !AA || !AA->pointsToConstantMemory(ML); 7860 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode(); 7861 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 7862 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad, 7863 MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges); 7864 LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2], 7865 MMO, false /*IsExpanding */); 7866 if (AddToChain) 7867 PendingLoads.push_back(LD.getValue(1)); 7868 setValue(&VPIntrin, LD); 7869 } 7870 7871 void SelectionDAGBuilder::visitVPGather( 7872 const VPIntrinsic &VPIntrin, EVT VT, 7873 const SmallVectorImpl<SDValue> &OpValues) { 7874 SDLoc DL = getCurSDLoc(); 7875 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7876 Value *PtrOperand = VPIntrin.getArgOperand(0); 7877 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 7878 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 7879 const MDNode *Ranges = getRangeMetadata(VPIntrin); 7880 SDValue LD; 7881 if (!Alignment) 7882 Alignment = DAG.getEVTAlign(VT.getScalarType()); 7883 unsigned AS = 7884 PtrOperand->getType()->getScalarType()->getPointerAddressSpace(); 7885 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 7886 MachinePointerInfo(AS), MachineMemOperand::MOLoad, 7887 MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges); 7888 SDValue Base, Index, Scale; 7889 ISD::MemIndexType IndexType; 7890 bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale, 7891 this, VPIntrin.getParent(), 7892 VT.getScalarStoreSize()); 7893 if (!UniformBase) { 7894 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout())); 7895 Index = getValue(PtrOperand); 7896 IndexType = ISD::SIGNED_SCALED; 7897 Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout())); 7898 } 7899 EVT IdxVT = Index.getValueType(); 7900 EVT EltTy = IdxVT.getVectorElementType(); 7901 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) { 7902 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy); 7903 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index); 7904 } 7905 LD = DAG.getGatherVP( 7906 DAG.getVTList(VT, MVT::Other), VT, DL, 7907 {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO, 7908 IndexType); 7909 PendingLoads.push_back(LD.getValue(1)); 7910 setValue(&VPIntrin, LD); 7911 } 7912 7913 void SelectionDAGBuilder::visitVPStore( 7914 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) { 7915 SDLoc DL = getCurSDLoc(); 7916 Value *PtrOperand = VPIntrin.getArgOperand(1); 7917 EVT VT = OpValues[0].getValueType(); 7918 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 7919 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 7920 SDValue ST; 7921 if (!Alignment) 7922 Alignment = DAG.getEVTAlign(VT); 7923 SDValue Ptr = OpValues[1]; 7924 SDValue Offset = DAG.getUNDEF(Ptr.getValueType()); 7925 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 7926 MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore, 7927 MemoryLocation::UnknownSize, *Alignment, AAInfo); 7928 ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset, 7929 OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED, 7930 /* IsTruncating */ false, /*IsCompressing*/ false); 7931 DAG.setRoot(ST); 7932 setValue(&VPIntrin, ST); 7933 } 7934 7935 void SelectionDAGBuilder::visitVPScatter( 7936 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) { 7937 SDLoc DL = getCurSDLoc(); 7938 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 7939 Value *PtrOperand = VPIntrin.getArgOperand(1); 7940 EVT VT = OpValues[0].getValueType(); 7941 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 7942 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 7943 SDValue ST; 7944 if (!Alignment) 7945 Alignment = DAG.getEVTAlign(VT.getScalarType()); 7946 unsigned AS = 7947 PtrOperand->getType()->getScalarType()->getPointerAddressSpace(); 7948 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 7949 MachinePointerInfo(AS), MachineMemOperand::MOStore, 7950 MemoryLocation::UnknownSize, *Alignment, AAInfo); 7951 SDValue Base, Index, Scale; 7952 ISD::MemIndexType IndexType; 7953 bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale, 7954 this, VPIntrin.getParent(), 7955 VT.getScalarStoreSize()); 7956 if (!UniformBase) { 7957 Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout())); 7958 Index = getValue(PtrOperand); 7959 IndexType = ISD::SIGNED_SCALED; 7960 Scale = 7961 DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout())); 7962 } 7963 EVT IdxVT = Index.getValueType(); 7964 EVT EltTy = IdxVT.getVectorElementType(); 7965 if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) { 7966 EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy); 7967 Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index); 7968 } 7969 ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL, 7970 {getMemoryRoot(), OpValues[0], Base, Index, Scale, 7971 OpValues[2], OpValues[3]}, 7972 MMO, IndexType); 7973 DAG.setRoot(ST); 7974 setValue(&VPIntrin, ST); 7975 } 7976 7977 void SelectionDAGBuilder::visitVPStridedLoad( 7978 const VPIntrinsic &VPIntrin, EVT VT, 7979 const SmallVectorImpl<SDValue> &OpValues) { 7980 SDLoc DL = getCurSDLoc(); 7981 Value *PtrOperand = VPIntrin.getArgOperand(0); 7982 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 7983 if (!Alignment) 7984 Alignment = DAG.getEVTAlign(VT.getScalarType()); 7985 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 7986 const MDNode *Ranges = getRangeMetadata(VPIntrin); 7987 MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo); 7988 bool AddToChain = !AA || !AA->pointsToConstantMemory(ML); 7989 SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode(); 7990 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 7991 MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad, 7992 MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges); 7993 7994 SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], 7995 OpValues[2], OpValues[3], MMO, 7996 false /*IsExpanding*/); 7997 7998 if (AddToChain) 7999 PendingLoads.push_back(LD.getValue(1)); 8000 setValue(&VPIntrin, LD); 8001 } 8002 8003 void SelectionDAGBuilder::visitVPStridedStore( 8004 const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) { 8005 SDLoc DL = getCurSDLoc(); 8006 Value *PtrOperand = VPIntrin.getArgOperand(1); 8007 EVT VT = OpValues[0].getValueType(); 8008 MaybeAlign Alignment = VPIntrin.getPointerAlignment(); 8009 if (!Alignment) 8010 Alignment = DAG.getEVTAlign(VT.getScalarType()); 8011 AAMDNodes AAInfo = VPIntrin.getAAMetadata(); 8012 MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand( 8013 MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore, 8014 MemoryLocation::UnknownSize, *Alignment, AAInfo); 8015 8016 SDValue ST = DAG.getStridedStoreVP( 8017 getMemoryRoot(), DL, OpValues[0], OpValues[1], 8018 DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3], 8019 OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false, 8020 /*IsCompressing*/ false); 8021 8022 DAG.setRoot(ST); 8023 setValue(&VPIntrin, ST); 8024 } 8025 8026 void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) { 8027 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8028 SDLoc DL = getCurSDLoc(); 8029 8030 ISD::CondCode Condition; 8031 CmpInst::Predicate CondCode = VPIntrin.getPredicate(); 8032 bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy(); 8033 if (IsFP) { 8034 // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan) 8035 // flags, but calls that don't return floating-point types can't be 8036 // FPMathOperators, like vp.fcmp. This affects constrained fcmp too. 8037 Condition = getFCmpCondCode(CondCode); 8038 if (TM.Options.NoNaNsFPMath) 8039 Condition = getFCmpCodeWithoutNaN(Condition); 8040 } else { 8041 Condition = getICmpCondCode(CondCode); 8042 } 8043 8044 SDValue Op1 = getValue(VPIntrin.getOperand(0)); 8045 SDValue Op2 = getValue(VPIntrin.getOperand(1)); 8046 // #2 is the condition code 8047 SDValue MaskOp = getValue(VPIntrin.getOperand(3)); 8048 SDValue EVL = getValue(VPIntrin.getOperand(4)); 8049 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy(); 8050 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) && 8051 "Unexpected target EVL type"); 8052 EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL); 8053 8054 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 8055 VPIntrin.getType()); 8056 setValue(&VPIntrin, 8057 DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL)); 8058 } 8059 8060 void SelectionDAGBuilder::visitVectorPredicationIntrinsic( 8061 const VPIntrinsic &VPIntrin) { 8062 SDLoc DL = getCurSDLoc(); 8063 unsigned Opcode = getISDForVPIntrinsic(VPIntrin); 8064 8065 auto IID = VPIntrin.getIntrinsicID(); 8066 8067 if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin)) 8068 return visitVPCmp(*CmpI); 8069 8070 SmallVector<EVT, 4> ValueVTs; 8071 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8072 ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs); 8073 SDVTList VTs = DAG.getVTList(ValueVTs); 8074 8075 auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID); 8076 8077 MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy(); 8078 assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) && 8079 "Unexpected target EVL type"); 8080 8081 // Request operands. 8082 SmallVector<SDValue, 7> OpValues; 8083 for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) { 8084 auto Op = getValue(VPIntrin.getArgOperand(I)); 8085 if (I == EVLParamPos) 8086 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op); 8087 OpValues.push_back(Op); 8088 } 8089 8090 switch (Opcode) { 8091 default: { 8092 SDNodeFlags SDFlags; 8093 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin)) 8094 SDFlags.copyFMF(*FPMO); 8095 SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags); 8096 setValue(&VPIntrin, Result); 8097 break; 8098 } 8099 case ISD::VP_LOAD: 8100 visitVPLoad(VPIntrin, ValueVTs[0], OpValues); 8101 break; 8102 case ISD::VP_GATHER: 8103 visitVPGather(VPIntrin, ValueVTs[0], OpValues); 8104 break; 8105 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: 8106 visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues); 8107 break; 8108 case ISD::VP_STORE: 8109 visitVPStore(VPIntrin, OpValues); 8110 break; 8111 case ISD::VP_SCATTER: 8112 visitVPScatter(VPIntrin, OpValues); 8113 break; 8114 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 8115 visitVPStridedStore(VPIntrin, OpValues); 8116 break; 8117 case ISD::VP_FMULADD: { 8118 assert(OpValues.size() == 5 && "Unexpected number of operands"); 8119 SDNodeFlags SDFlags; 8120 if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin)) 8121 SDFlags.copyFMF(*FPMO); 8122 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict && 8123 TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) { 8124 setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags)); 8125 } else { 8126 SDValue Mul = DAG.getNode( 8127 ISD::VP_FMUL, DL, VTs, 8128 {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags); 8129 SDValue Add = 8130 DAG.getNode(ISD::VP_FADD, DL, VTs, 8131 {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags); 8132 setValue(&VPIntrin, Add); 8133 } 8134 break; 8135 } 8136 case ISD::VP_IS_FPCLASS: { 8137 const DataLayout DLayout = DAG.getDataLayout(); 8138 EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType()); 8139 auto Constant = OpValues[1]->getAsZExtVal(); 8140 SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32); 8141 SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT, 8142 {OpValues[0], Check, OpValues[2], OpValues[3]}); 8143 setValue(&VPIntrin, V); 8144 return; 8145 } 8146 case ISD::VP_INTTOPTR: { 8147 SDValue N = OpValues[0]; 8148 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType()); 8149 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType()); 8150 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1], 8151 OpValues[2]); 8152 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1], 8153 OpValues[2]); 8154 setValue(&VPIntrin, N); 8155 break; 8156 } 8157 case ISD::VP_PTRTOINT: { 8158 SDValue N = OpValues[0]; 8159 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 8160 VPIntrin.getType()); 8161 EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), 8162 VPIntrin.getOperand(0)->getType()); 8163 N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1], 8164 OpValues[2]); 8165 N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1], 8166 OpValues[2]); 8167 setValue(&VPIntrin, N); 8168 break; 8169 } 8170 case ISD::VP_ABS: 8171 case ISD::VP_CTLZ: 8172 case ISD::VP_CTLZ_ZERO_UNDEF: 8173 case ISD::VP_CTTZ: 8174 case ISD::VP_CTTZ_ZERO_UNDEF: { 8175 SDValue Result = 8176 DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]}); 8177 setValue(&VPIntrin, Result); 8178 break; 8179 } 8180 } 8181 } 8182 8183 SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain, 8184 const BasicBlock *EHPadBB, 8185 MCSymbol *&BeginLabel) { 8186 MachineFunction &MF = DAG.getMachineFunction(); 8187 MachineModuleInfo &MMI = MF.getMMI(); 8188 8189 // Insert a label before the invoke call to mark the try range. This can be 8190 // used to detect deletion of the invoke via the MachineModuleInfo. 8191 BeginLabel = MMI.getContext().createTempSymbol(); 8192 8193 // For SjLj, keep track of which landing pads go with which invokes 8194 // so as to maintain the ordering of pads in the LSDA. 8195 unsigned CallSiteIndex = MMI.getCurrentCallSite(); 8196 if (CallSiteIndex) { 8197 MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex); 8198 LPadToCallSiteMap[FuncInfo.MBBMap[EHPadBB]].push_back(CallSiteIndex); 8199 8200 // Now that the call site is handled, stop tracking it. 8201 MMI.setCurrentCallSite(0); 8202 } 8203 8204 return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel); 8205 } 8206 8207 SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II, 8208 const BasicBlock *EHPadBB, 8209 MCSymbol *BeginLabel) { 8210 assert(BeginLabel && "BeginLabel should've been set"); 8211 8212 MachineFunction &MF = DAG.getMachineFunction(); 8213 MachineModuleInfo &MMI = MF.getMMI(); 8214 8215 // Insert a label at the end of the invoke call to mark the try range. This 8216 // can be used to detect deletion of the invoke via the MachineModuleInfo. 8217 MCSymbol *EndLabel = MMI.getContext().createTempSymbol(); 8218 Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel); 8219 8220 // Inform MachineModuleInfo of range. 8221 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn()); 8222 // There is a platform (e.g. wasm) that uses funclet style IR but does not 8223 // actually use outlined funclets and their LSDA info style. 8224 if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) { 8225 assert(II && "II should've been set"); 8226 WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo(); 8227 EHInfo->addIPToStateRange(II, BeginLabel, EndLabel); 8228 } else if (!isScopedEHPersonality(Pers)) { 8229 assert(EHPadBB); 8230 MF.addInvoke(FuncInfo.MBBMap[EHPadBB], BeginLabel, EndLabel); 8231 } 8232 8233 return Chain; 8234 } 8235 8236 std::pair<SDValue, SDValue> 8237 SelectionDAGBuilder::lowerInvokable(TargetLowering::CallLoweringInfo &CLI, 8238 const BasicBlock *EHPadBB) { 8239 MCSymbol *BeginLabel = nullptr; 8240 8241 if (EHPadBB) { 8242 // Both PendingLoads and PendingExports must be flushed here; 8243 // this call might not return. 8244 (void)getRoot(); 8245 DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel)); 8246 CLI.setChain(getRoot()); 8247 } 8248 8249 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8250 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); 8251 8252 assert((CLI.IsTailCall || Result.second.getNode()) && 8253 "Non-null chain expected with non-tail call!"); 8254 assert((Result.second.getNode() || !Result.first.getNode()) && 8255 "Null value expected with tail call!"); 8256 8257 if (!Result.second.getNode()) { 8258 // As a special case, a null chain means that a tail call has been emitted 8259 // and the DAG root is already updated. 8260 HasTailCall = true; 8261 8262 // Since there's no actual continuation from this block, nothing can be 8263 // relying on us setting vregs for them. 8264 PendingExports.clear(); 8265 } else { 8266 DAG.setRoot(Result.second); 8267 } 8268 8269 if (EHPadBB) { 8270 DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB, 8271 BeginLabel)); 8272 } 8273 8274 return Result; 8275 } 8276 8277 void SelectionDAGBuilder::LowerCallTo(const CallBase &CB, SDValue Callee, 8278 bool isTailCall, 8279 bool isMustTailCall, 8280 const BasicBlock *EHPadBB) { 8281 auto &DL = DAG.getDataLayout(); 8282 FunctionType *FTy = CB.getFunctionType(); 8283 Type *RetTy = CB.getType(); 8284 8285 TargetLowering::ArgListTy Args; 8286 Args.reserve(CB.arg_size()); 8287 8288 const Value *SwiftErrorVal = nullptr; 8289 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8290 8291 if (isTailCall) { 8292 // Avoid emitting tail calls in functions with the disable-tail-calls 8293 // attribute. 8294 auto *Caller = CB.getParent()->getParent(); 8295 if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() == 8296 "true" && !isMustTailCall) 8297 isTailCall = false; 8298 8299 // We can't tail call inside a function with a swifterror argument. Lowering 8300 // does not support this yet. It would have to move into the swifterror 8301 // register before the call. 8302 if (TLI.supportSwiftError() && 8303 Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) 8304 isTailCall = false; 8305 } 8306 8307 for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) { 8308 TargetLowering::ArgListEntry Entry; 8309 const Value *V = *I; 8310 8311 // Skip empty types 8312 if (V->getType()->isEmptyTy()) 8313 continue; 8314 8315 SDValue ArgNode = getValue(V); 8316 Entry.Node = ArgNode; Entry.Ty = V->getType(); 8317 8318 Entry.setAttributes(&CB, I - CB.arg_begin()); 8319 8320 // Use swifterror virtual register as input to the call. 8321 if (Entry.IsSwiftError && TLI.supportSwiftError()) { 8322 SwiftErrorVal = V; 8323 // We find the virtual register for the actual swifterror argument. 8324 // Instead of using the Value, we use the virtual register instead. 8325 Entry.Node = 8326 DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V), 8327 EVT(TLI.getPointerTy(DL))); 8328 } 8329 8330 Args.push_back(Entry); 8331 8332 // If we have an explicit sret argument that is an Instruction, (i.e., it 8333 // might point to function-local memory), we can't meaningfully tail-call. 8334 if (Entry.IsSRet && isa<Instruction>(V)) 8335 isTailCall = false; 8336 } 8337 8338 // If call site has a cfguardtarget operand bundle, create and add an 8339 // additional ArgListEntry. 8340 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) { 8341 TargetLowering::ArgListEntry Entry; 8342 Value *V = Bundle->Inputs[0]; 8343 SDValue ArgNode = getValue(V); 8344 Entry.Node = ArgNode; 8345 Entry.Ty = V->getType(); 8346 Entry.IsCFGuardTarget = true; 8347 Args.push_back(Entry); 8348 } 8349 8350 // Check if target-independent constraints permit a tail call here. 8351 // Target-dependent constraints are checked within TLI->LowerCallTo. 8352 if (isTailCall && !isInTailCallPosition(CB, DAG.getTarget())) 8353 isTailCall = false; 8354 8355 // Disable tail calls if there is an swifterror argument. Targets have not 8356 // been updated to support tail calls. 8357 if (TLI.supportSwiftError() && SwiftErrorVal) 8358 isTailCall = false; 8359 8360 ConstantInt *CFIType = nullptr; 8361 if (CB.isIndirectCall()) { 8362 if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) { 8363 if (!TLI.supportKCFIBundles()) 8364 report_fatal_error( 8365 "Target doesn't support calls with kcfi operand bundles."); 8366 CFIType = cast<ConstantInt>(Bundle->Inputs[0]); 8367 assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type"); 8368 } 8369 } 8370 8371 TargetLowering::CallLoweringInfo CLI(DAG); 8372 CLI.setDebugLoc(getCurSDLoc()) 8373 .setChain(getRoot()) 8374 .setCallee(RetTy, FTy, Callee, std::move(Args), CB) 8375 .setTailCall(isTailCall) 8376 .setConvergent(CB.isConvergent()) 8377 .setIsPreallocated( 8378 CB.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0) 8379 .setCFIType(CFIType); 8380 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB); 8381 8382 if (Result.first.getNode()) { 8383 Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first); 8384 setValue(&CB, Result.first); 8385 } 8386 8387 // The last element of CLI.InVals has the SDValue for swifterror return. 8388 // Here we copy it to a virtual register and update SwiftErrorMap for 8389 // book-keeping. 8390 if (SwiftErrorVal && TLI.supportSwiftError()) { 8391 // Get the last element of InVals. 8392 SDValue Src = CLI.InVals.back(); 8393 Register VReg = 8394 SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal); 8395 SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src); 8396 DAG.setRoot(CopyNode); 8397 } 8398 } 8399 8400 static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT, 8401 SelectionDAGBuilder &Builder) { 8402 // Check to see if this load can be trivially constant folded, e.g. if the 8403 // input is from a string literal. 8404 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) { 8405 // Cast pointer to the type we really want to load. 8406 Type *LoadTy = 8407 Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits()); 8408 if (LoadVT.isVector()) 8409 LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements()); 8410 8411 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput), 8412 PointerType::getUnqual(LoadTy)); 8413 8414 if (const Constant *LoadCst = 8415 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput), 8416 LoadTy, Builder.DAG.getDataLayout())) 8417 return Builder.getValue(LoadCst); 8418 } 8419 8420 // Otherwise, we have to emit the load. If the pointer is to unfoldable but 8421 // still constant memory, the input chain can be the entry node. 8422 SDValue Root; 8423 bool ConstantMemory = false; 8424 8425 // Do not serialize (non-volatile) loads of constant memory with anything. 8426 if (Builder.AA && Builder.AA->pointsToConstantMemory(PtrVal)) { 8427 Root = Builder.DAG.getEntryNode(); 8428 ConstantMemory = true; 8429 } else { 8430 // Do not serialize non-volatile loads against each other. 8431 Root = Builder.DAG.getRoot(); 8432 } 8433 8434 SDValue Ptr = Builder.getValue(PtrVal); 8435 SDValue LoadVal = 8436 Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr, 8437 MachinePointerInfo(PtrVal), Align(1)); 8438 8439 if (!ConstantMemory) 8440 Builder.PendingLoads.push_back(LoadVal.getValue(1)); 8441 return LoadVal; 8442 } 8443 8444 /// Record the value for an instruction that produces an integer result, 8445 /// converting the type where necessary. 8446 void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I, 8447 SDValue Value, 8448 bool IsSigned) { 8449 EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 8450 I.getType(), true); 8451 Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT); 8452 setValue(&I, Value); 8453 } 8454 8455 /// See if we can lower a memcmp/bcmp call into an optimized form. If so, return 8456 /// true and lower it. Otherwise return false, and it will be lowered like a 8457 /// normal call. 8458 /// The caller already checked that \p I calls the appropriate LibFunc with a 8459 /// correct prototype. 8460 bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) { 8461 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1); 8462 const Value *Size = I.getArgOperand(2); 8463 const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size)); 8464 if (CSize && CSize->getZExtValue() == 0) { 8465 EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(), 8466 I.getType(), true); 8467 setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT)); 8468 return true; 8469 } 8470 8471 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8472 std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp( 8473 DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS), 8474 getValue(Size), MachinePointerInfo(LHS), MachinePointerInfo(RHS)); 8475 if (Res.first.getNode()) { 8476 processIntegerCallValue(I, Res.first, true); 8477 PendingLoads.push_back(Res.second); 8478 return true; 8479 } 8480 8481 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0 8482 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0 8483 if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I)) 8484 return false; 8485 8486 // If the target has a fast compare for the given size, it will return a 8487 // preferred load type for that size. Require that the load VT is legal and 8488 // that the target supports unaligned loads of that type. Otherwise, return 8489 // INVALID. 8490 auto hasFastLoadsAndCompare = [&](unsigned NumBits) { 8491 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8492 MVT LVT = TLI.hasFastEqualityCompare(NumBits); 8493 if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) { 8494 // TODO: Handle 5 byte compare as 4-byte + 1 byte. 8495 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads. 8496 // TODO: Check alignment of src and dest ptrs. 8497 unsigned DstAS = LHS->getType()->getPointerAddressSpace(); 8498 unsigned SrcAS = RHS->getType()->getPointerAddressSpace(); 8499 if (!TLI.isTypeLegal(LVT) || 8500 !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) || 8501 !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS)) 8502 LVT = MVT::INVALID_SIMPLE_VALUE_TYPE; 8503 } 8504 8505 return LVT; 8506 }; 8507 8508 // This turns into unaligned loads. We only do this if the target natively 8509 // supports the MVT we'll be loading or if it is small enough (<= 4) that 8510 // we'll only produce a small number of byte loads. 8511 MVT LoadVT; 8512 unsigned NumBitsToCompare = CSize->getZExtValue() * 8; 8513 switch (NumBitsToCompare) { 8514 default: 8515 return false; 8516 case 16: 8517 LoadVT = MVT::i16; 8518 break; 8519 case 32: 8520 LoadVT = MVT::i32; 8521 break; 8522 case 64: 8523 case 128: 8524 case 256: 8525 LoadVT = hasFastLoadsAndCompare(NumBitsToCompare); 8526 break; 8527 } 8528 8529 if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE) 8530 return false; 8531 8532 SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this); 8533 SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this); 8534 8535 // Bitcast to a wide integer type if the loads are vectors. 8536 if (LoadVT.isVector()) { 8537 EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits()); 8538 LoadL = DAG.getBitcast(CmpVT, LoadL); 8539 LoadR = DAG.getBitcast(CmpVT, LoadR); 8540 } 8541 8542 SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE); 8543 processIntegerCallValue(I, Cmp, false); 8544 return true; 8545 } 8546 8547 /// See if we can lower a memchr call into an optimized form. If so, return 8548 /// true and lower it. Otherwise return false, and it will be lowered like a 8549 /// normal call. 8550 /// The caller already checked that \p I calls the appropriate LibFunc with a 8551 /// correct prototype. 8552 bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) { 8553 const Value *Src = I.getArgOperand(0); 8554 const Value *Char = I.getArgOperand(1); 8555 const Value *Length = I.getArgOperand(2); 8556 8557 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8558 std::pair<SDValue, SDValue> Res = 8559 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(), 8560 getValue(Src), getValue(Char), getValue(Length), 8561 MachinePointerInfo(Src)); 8562 if (Res.first.getNode()) { 8563 setValue(&I, Res.first); 8564 PendingLoads.push_back(Res.second); 8565 return true; 8566 } 8567 8568 return false; 8569 } 8570 8571 /// See if we can lower a mempcpy call into an optimized form. If so, return 8572 /// true and lower it. Otherwise return false, and it will be lowered like a 8573 /// normal call. 8574 /// The caller already checked that \p I calls the appropriate LibFunc with a 8575 /// correct prototype. 8576 bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) { 8577 SDValue Dst = getValue(I.getArgOperand(0)); 8578 SDValue Src = getValue(I.getArgOperand(1)); 8579 SDValue Size = getValue(I.getArgOperand(2)); 8580 8581 Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne(); 8582 Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne(); 8583 // DAG::getMemcpy needs Alignment to be defined. 8584 Align Alignment = std::min(DstAlign, SrcAlign); 8585 8586 SDLoc sdl = getCurSDLoc(); 8587 8588 // In the mempcpy context we need to pass in a false value for isTailCall 8589 // because the return pointer needs to be adjusted by the size of 8590 // the copied memory. 8591 SDValue Root = getMemoryRoot(); 8592 SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, false, false, 8593 /*isTailCall=*/false, 8594 MachinePointerInfo(I.getArgOperand(0)), 8595 MachinePointerInfo(I.getArgOperand(1)), 8596 I.getAAMetadata()); 8597 assert(MC.getNode() != nullptr && 8598 "** memcpy should not be lowered as TailCall in mempcpy context **"); 8599 DAG.setRoot(MC); 8600 8601 // Check if Size needs to be truncated or extended. 8602 Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType()); 8603 8604 // Adjust return pointer to point just past the last dst byte. 8605 SDValue DstPlusSize = DAG.getNode(ISD::ADD, sdl, Dst.getValueType(), 8606 Dst, Size); 8607 setValue(&I, DstPlusSize); 8608 return true; 8609 } 8610 8611 /// See if we can lower a strcpy call into an optimized form. If so, return 8612 /// true and lower it, otherwise return false and it will be lowered like a 8613 /// normal call. 8614 /// The caller already checked that \p I calls the appropriate LibFunc with a 8615 /// correct prototype. 8616 bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) { 8617 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); 8618 8619 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8620 std::pair<SDValue, SDValue> Res = 8621 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(), 8622 getValue(Arg0), getValue(Arg1), 8623 MachinePointerInfo(Arg0), 8624 MachinePointerInfo(Arg1), isStpcpy); 8625 if (Res.first.getNode()) { 8626 setValue(&I, Res.first); 8627 DAG.setRoot(Res.second); 8628 return true; 8629 } 8630 8631 return false; 8632 } 8633 8634 /// See if we can lower a strcmp call into an optimized form. If so, return 8635 /// true and lower it, otherwise return false and it will be lowered like a 8636 /// normal call. 8637 /// The caller already checked that \p I calls the appropriate LibFunc with a 8638 /// correct prototype. 8639 bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) { 8640 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); 8641 8642 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8643 std::pair<SDValue, SDValue> Res = 8644 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(), 8645 getValue(Arg0), getValue(Arg1), 8646 MachinePointerInfo(Arg0), 8647 MachinePointerInfo(Arg1)); 8648 if (Res.first.getNode()) { 8649 processIntegerCallValue(I, Res.first, true); 8650 PendingLoads.push_back(Res.second); 8651 return true; 8652 } 8653 8654 return false; 8655 } 8656 8657 /// See if we can lower a strlen call into an optimized form. If so, return 8658 /// true and lower it, otherwise return false and it will be lowered like a 8659 /// normal call. 8660 /// The caller already checked that \p I calls the appropriate LibFunc with a 8661 /// correct prototype. 8662 bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) { 8663 const Value *Arg0 = I.getArgOperand(0); 8664 8665 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8666 std::pair<SDValue, SDValue> Res = 8667 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(), 8668 getValue(Arg0), MachinePointerInfo(Arg0)); 8669 if (Res.first.getNode()) { 8670 processIntegerCallValue(I, Res.first, false); 8671 PendingLoads.push_back(Res.second); 8672 return true; 8673 } 8674 8675 return false; 8676 } 8677 8678 /// See if we can lower a strnlen call into an optimized form. If so, return 8679 /// true and lower it, otherwise return false and it will be lowered like a 8680 /// normal call. 8681 /// The caller already checked that \p I calls the appropriate LibFunc with a 8682 /// correct prototype. 8683 bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) { 8684 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1); 8685 8686 const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo(); 8687 std::pair<SDValue, SDValue> Res = 8688 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(), 8689 getValue(Arg0), getValue(Arg1), 8690 MachinePointerInfo(Arg0)); 8691 if (Res.first.getNode()) { 8692 processIntegerCallValue(I, Res.first, false); 8693 PendingLoads.push_back(Res.second); 8694 return true; 8695 } 8696 8697 return false; 8698 } 8699 8700 /// See if we can lower a unary floating-point operation into an SDNode with 8701 /// the specified Opcode. If so, return true and lower it, otherwise return 8702 /// false and it will be lowered like a normal call. 8703 /// The caller already checked that \p I calls the appropriate LibFunc with a 8704 /// correct prototype. 8705 bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I, 8706 unsigned Opcode) { 8707 // We already checked this call's prototype; verify it doesn't modify errno. 8708 if (!I.onlyReadsMemory()) 8709 return false; 8710 8711 SDNodeFlags Flags; 8712 Flags.copyFMF(cast<FPMathOperator>(I)); 8713 8714 SDValue Tmp = getValue(I.getArgOperand(0)); 8715 setValue(&I, 8716 DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags)); 8717 return true; 8718 } 8719 8720 /// See if we can lower a binary floating-point operation into an SDNode with 8721 /// the specified Opcode. If so, return true and lower it. Otherwise return 8722 /// false, and it will be lowered like a normal call. 8723 /// The caller already checked that \p I calls the appropriate LibFunc with a 8724 /// correct prototype. 8725 bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I, 8726 unsigned Opcode) { 8727 // We already checked this call's prototype; verify it doesn't modify errno. 8728 if (!I.onlyReadsMemory()) 8729 return false; 8730 8731 SDNodeFlags Flags; 8732 Flags.copyFMF(cast<FPMathOperator>(I)); 8733 8734 SDValue Tmp0 = getValue(I.getArgOperand(0)); 8735 SDValue Tmp1 = getValue(I.getArgOperand(1)); 8736 EVT VT = Tmp0.getValueType(); 8737 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags)); 8738 return true; 8739 } 8740 8741 void SelectionDAGBuilder::visitCall(const CallInst &I) { 8742 // Handle inline assembly differently. 8743 if (I.isInlineAsm()) { 8744 visitInlineAsm(I); 8745 return; 8746 } 8747 8748 diagnoseDontCall(I); 8749 8750 if (Function *F = I.getCalledFunction()) { 8751 if (F->isDeclaration()) { 8752 // Is this an LLVM intrinsic or a target-specific intrinsic? 8753 unsigned IID = F->getIntrinsicID(); 8754 if (!IID) 8755 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) 8756 IID = II->getIntrinsicID(F); 8757 8758 if (IID) { 8759 visitIntrinsicCall(I, IID); 8760 return; 8761 } 8762 } 8763 8764 // Check for well-known libc/libm calls. If the function is internal, it 8765 // can't be a library call. Don't do the check if marked as nobuiltin for 8766 // some reason or the call site requires strict floating point semantics. 8767 LibFunc Func; 8768 if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() && 8769 F->hasName() && LibInfo->getLibFunc(*F, Func) && 8770 LibInfo->hasOptimizedCodeGen(Func)) { 8771 switch (Func) { 8772 default: break; 8773 case LibFunc_bcmp: 8774 if (visitMemCmpBCmpCall(I)) 8775 return; 8776 break; 8777 case LibFunc_copysign: 8778 case LibFunc_copysignf: 8779 case LibFunc_copysignl: 8780 // We already checked this call's prototype; verify it doesn't modify 8781 // errno. 8782 if (I.onlyReadsMemory()) { 8783 SDValue LHS = getValue(I.getArgOperand(0)); 8784 SDValue RHS = getValue(I.getArgOperand(1)); 8785 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(), 8786 LHS.getValueType(), LHS, RHS)); 8787 return; 8788 } 8789 break; 8790 case LibFunc_fabs: 8791 case LibFunc_fabsf: 8792 case LibFunc_fabsl: 8793 if (visitUnaryFloatCall(I, ISD::FABS)) 8794 return; 8795 break; 8796 case LibFunc_fmin: 8797 case LibFunc_fminf: 8798 case LibFunc_fminl: 8799 if (visitBinaryFloatCall(I, ISD::FMINNUM)) 8800 return; 8801 break; 8802 case LibFunc_fmax: 8803 case LibFunc_fmaxf: 8804 case LibFunc_fmaxl: 8805 if (visitBinaryFloatCall(I, ISD::FMAXNUM)) 8806 return; 8807 break; 8808 case LibFunc_sin: 8809 case LibFunc_sinf: 8810 case LibFunc_sinl: 8811 if (visitUnaryFloatCall(I, ISD::FSIN)) 8812 return; 8813 break; 8814 case LibFunc_cos: 8815 case LibFunc_cosf: 8816 case LibFunc_cosl: 8817 if (visitUnaryFloatCall(I, ISD::FCOS)) 8818 return; 8819 break; 8820 case LibFunc_sqrt: 8821 case LibFunc_sqrtf: 8822 case LibFunc_sqrtl: 8823 case LibFunc_sqrt_finite: 8824 case LibFunc_sqrtf_finite: 8825 case LibFunc_sqrtl_finite: 8826 if (visitUnaryFloatCall(I, ISD::FSQRT)) 8827 return; 8828 break; 8829 case LibFunc_floor: 8830 case LibFunc_floorf: 8831 case LibFunc_floorl: 8832 if (visitUnaryFloatCall(I, ISD::FFLOOR)) 8833 return; 8834 break; 8835 case LibFunc_nearbyint: 8836 case LibFunc_nearbyintf: 8837 case LibFunc_nearbyintl: 8838 if (visitUnaryFloatCall(I, ISD::FNEARBYINT)) 8839 return; 8840 break; 8841 case LibFunc_ceil: 8842 case LibFunc_ceilf: 8843 case LibFunc_ceill: 8844 if (visitUnaryFloatCall(I, ISD::FCEIL)) 8845 return; 8846 break; 8847 case LibFunc_rint: 8848 case LibFunc_rintf: 8849 case LibFunc_rintl: 8850 if (visitUnaryFloatCall(I, ISD::FRINT)) 8851 return; 8852 break; 8853 case LibFunc_round: 8854 case LibFunc_roundf: 8855 case LibFunc_roundl: 8856 if (visitUnaryFloatCall(I, ISD::FROUND)) 8857 return; 8858 break; 8859 case LibFunc_trunc: 8860 case LibFunc_truncf: 8861 case LibFunc_truncl: 8862 if (visitUnaryFloatCall(I, ISD::FTRUNC)) 8863 return; 8864 break; 8865 case LibFunc_log2: 8866 case LibFunc_log2f: 8867 case LibFunc_log2l: 8868 if (visitUnaryFloatCall(I, ISD::FLOG2)) 8869 return; 8870 break; 8871 case LibFunc_exp2: 8872 case LibFunc_exp2f: 8873 case LibFunc_exp2l: 8874 if (visitUnaryFloatCall(I, ISD::FEXP2)) 8875 return; 8876 break; 8877 case LibFunc_exp10: 8878 case LibFunc_exp10f: 8879 case LibFunc_exp10l: 8880 if (visitUnaryFloatCall(I, ISD::FEXP10)) 8881 return; 8882 break; 8883 case LibFunc_ldexp: 8884 case LibFunc_ldexpf: 8885 case LibFunc_ldexpl: 8886 if (visitBinaryFloatCall(I, ISD::FLDEXP)) 8887 return; 8888 break; 8889 case LibFunc_memcmp: 8890 if (visitMemCmpBCmpCall(I)) 8891 return; 8892 break; 8893 case LibFunc_mempcpy: 8894 if (visitMemPCpyCall(I)) 8895 return; 8896 break; 8897 case LibFunc_memchr: 8898 if (visitMemChrCall(I)) 8899 return; 8900 break; 8901 case LibFunc_strcpy: 8902 if (visitStrCpyCall(I, false)) 8903 return; 8904 break; 8905 case LibFunc_stpcpy: 8906 if (visitStrCpyCall(I, true)) 8907 return; 8908 break; 8909 case LibFunc_strcmp: 8910 if (visitStrCmpCall(I)) 8911 return; 8912 break; 8913 case LibFunc_strlen: 8914 if (visitStrLenCall(I)) 8915 return; 8916 break; 8917 case LibFunc_strnlen: 8918 if (visitStrNLenCall(I)) 8919 return; 8920 break; 8921 } 8922 } 8923 } 8924 8925 // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't 8926 // have to do anything here to lower funclet bundles. 8927 // CFGuardTarget bundles are lowered in LowerCallTo. 8928 assert(!I.hasOperandBundlesOtherThan( 8929 {LLVMContext::OB_deopt, LLVMContext::OB_funclet, 8930 LLVMContext::OB_cfguardtarget, LLVMContext::OB_preallocated, 8931 LLVMContext::OB_clang_arc_attachedcall, LLVMContext::OB_kcfi}) && 8932 "Cannot lower calls with arbitrary operand bundles!"); 8933 8934 SDValue Callee = getValue(I.getCalledOperand()); 8935 8936 if (I.countOperandBundlesOfType(LLVMContext::OB_deopt)) 8937 LowerCallSiteWithDeoptBundle(&I, Callee, nullptr); 8938 else 8939 // Check if we can potentially perform a tail call. More detailed checking 8940 // is be done within LowerCallTo, after more information about the call is 8941 // known. 8942 LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall()); 8943 } 8944 8945 namespace { 8946 8947 /// AsmOperandInfo - This contains information for each constraint that we are 8948 /// lowering. 8949 class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo { 8950 public: 8951 /// CallOperand - If this is the result output operand or a clobber 8952 /// this is null, otherwise it is the incoming operand to the CallInst. 8953 /// This gets modified as the asm is processed. 8954 SDValue CallOperand; 8955 8956 /// AssignedRegs - If this is a register or register class operand, this 8957 /// contains the set of register corresponding to the operand. 8958 RegsForValue AssignedRegs; 8959 8960 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info) 8961 : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) { 8962 } 8963 8964 /// Whether or not this operand accesses memory 8965 bool hasMemory(const TargetLowering &TLI) const { 8966 // Indirect operand accesses access memory. 8967 if (isIndirect) 8968 return true; 8969 8970 for (const auto &Code : Codes) 8971 if (TLI.getConstraintType(Code) == TargetLowering::C_Memory) 8972 return true; 8973 8974 return false; 8975 } 8976 }; 8977 8978 8979 } // end anonymous namespace 8980 8981 /// Make sure that the output operand \p OpInfo and its corresponding input 8982 /// operand \p MatchingOpInfo have compatible constraint types (otherwise error 8983 /// out). 8984 static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo, 8985 SDISelAsmOperandInfo &MatchingOpInfo, 8986 SelectionDAG &DAG) { 8987 if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT) 8988 return; 8989 8990 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo(); 8991 const auto &TLI = DAG.getTargetLoweringInfo(); 8992 8993 std::pair<unsigned, const TargetRegisterClass *> MatchRC = 8994 TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode, 8995 OpInfo.ConstraintVT); 8996 std::pair<unsigned, const TargetRegisterClass *> InputRC = 8997 TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode, 8998 MatchingOpInfo.ConstraintVT); 8999 if ((OpInfo.ConstraintVT.isInteger() != 9000 MatchingOpInfo.ConstraintVT.isInteger()) || 9001 (MatchRC.second != InputRC.second)) { 9002 // FIXME: error out in a more elegant fashion 9003 report_fatal_error("Unsupported asm: input constraint" 9004 " with a matching output constraint of" 9005 " incompatible type!"); 9006 } 9007 MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT; 9008 } 9009 9010 /// Get a direct memory input to behave well as an indirect operand. 9011 /// This may introduce stores, hence the need for a \p Chain. 9012 /// \return The (possibly updated) chain. 9013 static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location, 9014 SDISelAsmOperandInfo &OpInfo, 9015 SelectionDAG &DAG) { 9016 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9017 9018 // If we don't have an indirect input, put it in the constpool if we can, 9019 // otherwise spill it to a stack slot. 9020 // TODO: This isn't quite right. We need to handle these according to 9021 // the addressing mode that the constraint wants. Also, this may take 9022 // an additional register for the computation and we don't want that 9023 // either. 9024 9025 // If the operand is a float, integer, or vector constant, spill to a 9026 // constant pool entry to get its address. 9027 const Value *OpVal = OpInfo.CallOperandVal; 9028 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) || 9029 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) { 9030 OpInfo.CallOperand = DAG.getConstantPool( 9031 cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout())); 9032 return Chain; 9033 } 9034 9035 // Otherwise, create a stack slot and emit a store to it before the asm. 9036 Type *Ty = OpVal->getType(); 9037 auto &DL = DAG.getDataLayout(); 9038 uint64_t TySize = DL.getTypeAllocSize(Ty); 9039 MachineFunction &MF = DAG.getMachineFunction(); 9040 int SSFI = MF.getFrameInfo().CreateStackObject( 9041 TySize, DL.getPrefTypeAlign(Ty), false); 9042 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL)); 9043 Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot, 9044 MachinePointerInfo::getFixedStack(MF, SSFI), 9045 TLI.getMemValueType(DL, Ty)); 9046 OpInfo.CallOperand = StackSlot; 9047 9048 return Chain; 9049 } 9050 9051 /// GetRegistersForValue - Assign registers (virtual or physical) for the 9052 /// specified operand. We prefer to assign virtual registers, to allow the 9053 /// register allocator to handle the assignment process. However, if the asm 9054 /// uses features that we can't model on machineinstrs, we have SDISel do the 9055 /// allocation. This produces generally horrible, but correct, code. 9056 /// 9057 /// OpInfo describes the operand 9058 /// RefOpInfo describes the matching operand if any, the operand otherwise 9059 static std::optional<unsigned> 9060 getRegistersForValue(SelectionDAG &DAG, const SDLoc &DL, 9061 SDISelAsmOperandInfo &OpInfo, 9062 SDISelAsmOperandInfo &RefOpInfo) { 9063 LLVMContext &Context = *DAG.getContext(); 9064 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9065 9066 MachineFunction &MF = DAG.getMachineFunction(); 9067 SmallVector<unsigned, 4> Regs; 9068 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 9069 9070 // No work to do for memory/address operands. 9071 if (OpInfo.ConstraintType == TargetLowering::C_Memory || 9072 OpInfo.ConstraintType == TargetLowering::C_Address) 9073 return std::nullopt; 9074 9075 // If this is a constraint for a single physreg, or a constraint for a 9076 // register class, find it. 9077 unsigned AssignedReg; 9078 const TargetRegisterClass *RC; 9079 std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint( 9080 &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT); 9081 // RC is unset only on failure. Return immediately. 9082 if (!RC) 9083 return std::nullopt; 9084 9085 // Get the actual register value type. This is important, because the user 9086 // may have asked for (e.g.) the AX register in i32 type. We need to 9087 // remember that AX is actually i16 to get the right extension. 9088 const MVT RegVT = *TRI.legalclasstypes_begin(*RC); 9089 9090 if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) { 9091 // If this is an FP operand in an integer register (or visa versa), or more 9092 // generally if the operand value disagrees with the register class we plan 9093 // to stick it in, fix the operand type. 9094 // 9095 // If this is an input value, the bitcast to the new type is done now. 9096 // Bitcast for output value is done at the end of visitInlineAsm(). 9097 if ((OpInfo.Type == InlineAsm::isOutput || 9098 OpInfo.Type == InlineAsm::isInput) && 9099 !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) { 9100 // Try to convert to the first EVT that the reg class contains. If the 9101 // types are identical size, use a bitcast to convert (e.g. two differing 9102 // vector types). Note: output bitcast is done at the end of 9103 // visitInlineAsm(). 9104 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) { 9105 // Exclude indirect inputs while they are unsupported because the code 9106 // to perform the load is missing and thus OpInfo.CallOperand still 9107 // refers to the input address rather than the pointed-to value. 9108 if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect) 9109 OpInfo.CallOperand = 9110 DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand); 9111 OpInfo.ConstraintVT = RegVT; 9112 // If the operand is an FP value and we want it in integer registers, 9113 // use the corresponding integer type. This turns an f64 value into 9114 // i64, which can be passed with two i32 values on a 32-bit machine. 9115 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) { 9116 MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits()); 9117 if (OpInfo.Type == InlineAsm::isInput) 9118 OpInfo.CallOperand = 9119 DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand); 9120 OpInfo.ConstraintVT = VT; 9121 } 9122 } 9123 } 9124 9125 // No need to allocate a matching input constraint since the constraint it's 9126 // matching to has already been allocated. 9127 if (OpInfo.isMatchingInputConstraint()) 9128 return std::nullopt; 9129 9130 EVT ValueVT = OpInfo.ConstraintVT; 9131 if (OpInfo.ConstraintVT == MVT::Other) 9132 ValueVT = RegVT; 9133 9134 // Initialize NumRegs. 9135 unsigned NumRegs = 1; 9136 if (OpInfo.ConstraintVT != MVT::Other) 9137 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT); 9138 9139 // If this is a constraint for a specific physical register, like {r17}, 9140 // assign it now. 9141 9142 // If this associated to a specific register, initialize iterator to correct 9143 // place. If virtual, make sure we have enough registers 9144 9145 // Initialize iterator if necessary 9146 TargetRegisterClass::iterator I = RC->begin(); 9147 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 9148 9149 // Do not check for single registers. 9150 if (AssignedReg) { 9151 I = std::find(I, RC->end(), AssignedReg); 9152 if (I == RC->end()) { 9153 // RC does not contain the selected register, which indicates a 9154 // mismatch between the register and the required type/bitwidth. 9155 return {AssignedReg}; 9156 } 9157 } 9158 9159 for (; NumRegs; --NumRegs, ++I) { 9160 assert(I != RC->end() && "Ran out of registers to allocate!"); 9161 Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC); 9162 Regs.push_back(R); 9163 } 9164 9165 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT); 9166 return std::nullopt; 9167 } 9168 9169 static unsigned 9170 findMatchingInlineAsmOperand(unsigned OperandNo, 9171 const std::vector<SDValue> &AsmNodeOperands) { 9172 // Scan until we find the definition we already emitted of this operand. 9173 unsigned CurOp = InlineAsm::Op_FirstOperand; 9174 for (; OperandNo; --OperandNo) { 9175 // Advance to the next operand. 9176 unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal(); 9177 const InlineAsm::Flag F(OpFlag); 9178 assert( 9179 (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) && 9180 "Skipped past definitions?"); 9181 CurOp += F.getNumOperandRegisters() + 1; 9182 } 9183 return CurOp; 9184 } 9185 9186 namespace { 9187 9188 class ExtraFlags { 9189 unsigned Flags = 0; 9190 9191 public: 9192 explicit ExtraFlags(const CallBase &Call) { 9193 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand()); 9194 if (IA->hasSideEffects()) 9195 Flags |= InlineAsm::Extra_HasSideEffects; 9196 if (IA->isAlignStack()) 9197 Flags |= InlineAsm::Extra_IsAlignStack; 9198 if (Call.isConvergent()) 9199 Flags |= InlineAsm::Extra_IsConvergent; 9200 Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect; 9201 } 9202 9203 void update(const TargetLowering::AsmOperandInfo &OpInfo) { 9204 // Ideally, we would only check against memory constraints. However, the 9205 // meaning of an Other constraint can be target-specific and we can't easily 9206 // reason about it. Therefore, be conservative and set MayLoad/MayStore 9207 // for Other constraints as well. 9208 if (OpInfo.ConstraintType == TargetLowering::C_Memory || 9209 OpInfo.ConstraintType == TargetLowering::C_Other) { 9210 if (OpInfo.Type == InlineAsm::isInput) 9211 Flags |= InlineAsm::Extra_MayLoad; 9212 else if (OpInfo.Type == InlineAsm::isOutput) 9213 Flags |= InlineAsm::Extra_MayStore; 9214 else if (OpInfo.Type == InlineAsm::isClobber) 9215 Flags |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore); 9216 } 9217 } 9218 9219 unsigned get() const { return Flags; } 9220 }; 9221 9222 } // end anonymous namespace 9223 9224 static bool isFunction(SDValue Op) { 9225 if (Op && Op.getOpcode() == ISD::GlobalAddress) { 9226 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) { 9227 auto Fn = dyn_cast_or_null<Function>(GA->getGlobal()); 9228 9229 // In normal "call dllimport func" instruction (non-inlineasm) it force 9230 // indirect access by specifing call opcode. And usually specially print 9231 // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can 9232 // not do in this way now. (In fact, this is similar with "Data Access" 9233 // action). So here we ignore dllimport function. 9234 if (Fn && !Fn->hasDLLImportStorageClass()) 9235 return true; 9236 } 9237 } 9238 return false; 9239 } 9240 9241 /// visitInlineAsm - Handle a call to an InlineAsm object. 9242 void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call, 9243 const BasicBlock *EHPadBB) { 9244 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand()); 9245 9246 /// ConstraintOperands - Information about all of the constraints. 9247 SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands; 9248 9249 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9250 TargetLowering::AsmOperandInfoVector TargetConstraints = TLI.ParseConstraints( 9251 DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call); 9252 9253 // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack, 9254 // AsmDialect, MayLoad, MayStore). 9255 bool HasSideEffect = IA->hasSideEffects(); 9256 ExtraFlags ExtraInfo(Call); 9257 9258 for (auto &T : TargetConstraints) { 9259 ConstraintOperands.push_back(SDISelAsmOperandInfo(T)); 9260 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back(); 9261 9262 if (OpInfo.CallOperandVal) 9263 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal); 9264 9265 if (!HasSideEffect) 9266 HasSideEffect = OpInfo.hasMemory(TLI); 9267 9268 // Determine if this InlineAsm MayLoad or MayStore based on the constraints. 9269 // FIXME: Could we compute this on OpInfo rather than T? 9270 9271 // Compute the constraint code and ConstraintType to use. 9272 TLI.ComputeConstraintToUse(T, SDValue()); 9273 9274 if (T.ConstraintType == TargetLowering::C_Immediate && 9275 OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand)) 9276 // We've delayed emitting a diagnostic like the "n" constraint because 9277 // inlining could cause an integer showing up. 9278 return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) + 9279 "' expects an integer constant " 9280 "expression"); 9281 9282 ExtraInfo.update(T); 9283 } 9284 9285 // We won't need to flush pending loads if this asm doesn't touch 9286 // memory and is nonvolatile. 9287 SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot(); 9288 9289 bool EmitEHLabels = isa<InvokeInst>(Call); 9290 if (EmitEHLabels) { 9291 assert(EHPadBB && "InvokeInst must have an EHPadBB"); 9292 } 9293 bool IsCallBr = isa<CallBrInst>(Call); 9294 9295 if (IsCallBr || EmitEHLabels) { 9296 // If this is a callbr or invoke we need to flush pending exports since 9297 // inlineasm_br and invoke are terminators. 9298 // We need to do this before nodes are glued to the inlineasm_br node. 9299 Chain = getControlRoot(); 9300 } 9301 9302 MCSymbol *BeginLabel = nullptr; 9303 if (EmitEHLabels) { 9304 Chain = lowerStartEH(Chain, EHPadBB, BeginLabel); 9305 } 9306 9307 int OpNo = -1; 9308 SmallVector<StringRef> AsmStrs; 9309 IA->collectAsmStrs(AsmStrs); 9310 9311 // Second pass over the constraints: compute which constraint option to use. 9312 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) { 9313 if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput) 9314 OpNo++; 9315 9316 // If this is an output operand with a matching input operand, look up the 9317 // matching input. If their types mismatch, e.g. one is an integer, the 9318 // other is floating point, or their sizes are different, flag it as an 9319 // error. 9320 if (OpInfo.hasMatchingInput()) { 9321 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput]; 9322 patchMatchingInput(OpInfo, Input, DAG); 9323 } 9324 9325 // Compute the constraint code and ConstraintType to use. 9326 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); 9327 9328 if ((OpInfo.ConstraintType == TargetLowering::C_Memory && 9329 OpInfo.Type == InlineAsm::isClobber) || 9330 OpInfo.ConstraintType == TargetLowering::C_Address) 9331 continue; 9332 9333 // In Linux PIC model, there are 4 cases about value/label addressing: 9334 // 9335 // 1: Function call or Label jmp inside the module. 9336 // 2: Data access (such as global variable, static variable) inside module. 9337 // 3: Function call or Label jmp outside the module. 9338 // 4: Data access (such as global variable) outside the module. 9339 // 9340 // Due to current llvm inline asm architecture designed to not "recognize" 9341 // the asm code, there are quite troubles for us to treat mem addressing 9342 // differently for same value/adress used in different instuctions. 9343 // For example, in pic model, call a func may in plt way or direclty 9344 // pc-related, but lea/mov a function adress may use got. 9345 // 9346 // Here we try to "recognize" function call for the case 1 and case 3 in 9347 // inline asm. And try to adjust the constraint for them. 9348 // 9349 // TODO: Due to current inline asm didn't encourage to jmp to the outsider 9350 // label, so here we don't handle jmp function label now, but we need to 9351 // enhance it (especilly in PIC model) if we meet meaningful requirements. 9352 if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) && 9353 TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) && 9354 TM.getCodeModel() != CodeModel::Large) { 9355 OpInfo.isIndirect = false; 9356 OpInfo.ConstraintType = TargetLowering::C_Address; 9357 } 9358 9359 // If this is a memory input, and if the operand is not indirect, do what we 9360 // need to provide an address for the memory input. 9361 if (OpInfo.ConstraintType == TargetLowering::C_Memory && 9362 !OpInfo.isIndirect) { 9363 assert((OpInfo.isMultipleAlternative || 9364 (OpInfo.Type == InlineAsm::isInput)) && 9365 "Can only indirectify direct input operands!"); 9366 9367 // Memory operands really want the address of the value. 9368 Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG); 9369 9370 // There is no longer a Value* corresponding to this operand. 9371 OpInfo.CallOperandVal = nullptr; 9372 9373 // It is now an indirect operand. 9374 OpInfo.isIndirect = true; 9375 } 9376 9377 } 9378 9379 // AsmNodeOperands - The operands for the ISD::INLINEASM node. 9380 std::vector<SDValue> AsmNodeOperands; 9381 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain 9382 AsmNodeOperands.push_back(DAG.getTargetExternalSymbol( 9383 IA->getAsmString().c_str(), TLI.getProgramPointerTy(DAG.getDataLayout()))); 9384 9385 // If we have a !srcloc metadata node associated with it, we want to attach 9386 // this to the ultimately generated inline asm machineinstr. To do this, we 9387 // pass in the third operand as this (potentially null) inline asm MDNode. 9388 const MDNode *SrcLoc = Call.getMetadata("srcloc"); 9389 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc)); 9390 9391 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore 9392 // bits as operand 3. 9393 AsmNodeOperands.push_back(DAG.getTargetConstant( 9394 ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout()))); 9395 9396 // Third pass: Loop over operands to prepare DAG-level operands.. As part of 9397 // this, assign virtual and physical registers for inputs and otput. 9398 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) { 9399 // Assign Registers. 9400 SDISelAsmOperandInfo &RefOpInfo = 9401 OpInfo.isMatchingInputConstraint() 9402 ? ConstraintOperands[OpInfo.getMatchedOperand()] 9403 : OpInfo; 9404 const auto RegError = 9405 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo); 9406 if (RegError) { 9407 const MachineFunction &MF = DAG.getMachineFunction(); 9408 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 9409 const char *RegName = TRI.getName(*RegError); 9410 emitInlineAsmError(Call, "register '" + Twine(RegName) + 9411 "' allocated for constraint '" + 9412 Twine(OpInfo.ConstraintCode) + 9413 "' does not match required type"); 9414 return; 9415 } 9416 9417 auto DetectWriteToReservedRegister = [&]() { 9418 const MachineFunction &MF = DAG.getMachineFunction(); 9419 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 9420 for (unsigned Reg : OpInfo.AssignedRegs.Regs) { 9421 if (Register::isPhysicalRegister(Reg) && 9422 TRI.isInlineAsmReadOnlyReg(MF, Reg)) { 9423 const char *RegName = TRI.getName(Reg); 9424 emitInlineAsmError(Call, "write to reserved register '" + 9425 Twine(RegName) + "'"); 9426 return true; 9427 } 9428 } 9429 return false; 9430 }; 9431 assert((OpInfo.ConstraintType != TargetLowering::C_Address || 9432 (OpInfo.Type == InlineAsm::isInput && 9433 !OpInfo.isMatchingInputConstraint())) && 9434 "Only address as input operand is allowed."); 9435 9436 switch (OpInfo.Type) { 9437 case InlineAsm::isOutput: 9438 if (OpInfo.ConstraintType == TargetLowering::C_Memory) { 9439 const InlineAsm::ConstraintCode ConstraintID = 9440 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode); 9441 assert(ConstraintID != InlineAsm::ConstraintCode::Unknown && 9442 "Failed to convert memory constraint code to constraint id."); 9443 9444 // Add information to the INLINEASM node to know about this output. 9445 InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1); 9446 OpFlags.setMemConstraint(ConstraintID); 9447 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(), 9448 MVT::i32)); 9449 AsmNodeOperands.push_back(OpInfo.CallOperand); 9450 } else { 9451 // Otherwise, this outputs to a register (directly for C_Register / 9452 // C_RegisterClass, and a target-defined fashion for 9453 // C_Immediate/C_Other). Find a register that we can use. 9454 if (OpInfo.AssignedRegs.Regs.empty()) { 9455 emitInlineAsmError( 9456 Call, "couldn't allocate output register for constraint '" + 9457 Twine(OpInfo.ConstraintCode) + "'"); 9458 return; 9459 } 9460 9461 if (DetectWriteToReservedRegister()) 9462 return; 9463 9464 // Add information to the INLINEASM node to know that this register is 9465 // set. 9466 OpInfo.AssignedRegs.AddInlineAsmOperands( 9467 OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber 9468 : InlineAsm::Kind::RegDef, 9469 false, 0, getCurSDLoc(), DAG, AsmNodeOperands); 9470 } 9471 break; 9472 9473 case InlineAsm::isInput: 9474 case InlineAsm::isLabel: { 9475 SDValue InOperandVal = OpInfo.CallOperand; 9476 9477 if (OpInfo.isMatchingInputConstraint()) { 9478 // If this is required to match an output register we have already set, 9479 // just use its register. 9480 auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(), 9481 AsmNodeOperands); 9482 InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal()); 9483 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) { 9484 if (OpInfo.isIndirect) { 9485 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c 9486 emitInlineAsmError(Call, "inline asm not supported yet: " 9487 "don't know how to handle tied " 9488 "indirect register inputs"); 9489 return; 9490 } 9491 9492 SmallVector<unsigned, 4> Regs; 9493 MachineFunction &MF = DAG.getMachineFunction(); 9494 MachineRegisterInfo &MRI = MF.getRegInfo(); 9495 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 9496 auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]); 9497 Register TiedReg = R->getReg(); 9498 MVT RegVT = R->getSimpleValueType(0); 9499 const TargetRegisterClass *RC = 9500 TiedReg.isVirtual() ? MRI.getRegClass(TiedReg) 9501 : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT) 9502 : TRI.getMinimalPhysRegClass(TiedReg); 9503 for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i) 9504 Regs.push_back(MRI.createVirtualRegister(RC)); 9505 9506 RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType()); 9507 9508 SDLoc dl = getCurSDLoc(); 9509 // Use the produced MatchedRegs object to 9510 MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call); 9511 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true, 9512 OpInfo.getMatchedOperand(), dl, DAG, 9513 AsmNodeOperands); 9514 break; 9515 } 9516 9517 assert(Flag.isMemKind() && "Unknown matching constraint!"); 9518 assert(Flag.getNumOperandRegisters() == 1 && 9519 "Unexpected number of operands"); 9520 // Add information to the INLINEASM node to know about this input. 9521 // See InlineAsm.h isUseOperandTiedToDef. 9522 Flag.clearMemConstraint(); 9523 Flag.setMatchingOp(OpInfo.getMatchedOperand()); 9524 AsmNodeOperands.push_back(DAG.getTargetConstant( 9525 Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout()))); 9526 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]); 9527 break; 9528 } 9529 9530 // Treat indirect 'X' constraint as memory. 9531 if (OpInfo.ConstraintType == TargetLowering::C_Other && 9532 OpInfo.isIndirect) 9533 OpInfo.ConstraintType = TargetLowering::C_Memory; 9534 9535 if (OpInfo.ConstraintType == TargetLowering::C_Immediate || 9536 OpInfo.ConstraintType == TargetLowering::C_Other) { 9537 std::vector<SDValue> Ops; 9538 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode, 9539 Ops, DAG); 9540 if (Ops.empty()) { 9541 if (OpInfo.ConstraintType == TargetLowering::C_Immediate) 9542 if (isa<ConstantSDNode>(InOperandVal)) { 9543 emitInlineAsmError(Call, "value out of range for constraint '" + 9544 Twine(OpInfo.ConstraintCode) + "'"); 9545 return; 9546 } 9547 9548 emitInlineAsmError(Call, 9549 "invalid operand for inline asm constraint '" + 9550 Twine(OpInfo.ConstraintCode) + "'"); 9551 return; 9552 } 9553 9554 // Add information to the INLINEASM node to know about this input. 9555 InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size()); 9556 AsmNodeOperands.push_back(DAG.getTargetConstant( 9557 ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout()))); 9558 llvm::append_range(AsmNodeOperands, Ops); 9559 break; 9560 } 9561 9562 if (OpInfo.ConstraintType == TargetLowering::C_Memory) { 9563 assert((OpInfo.isIndirect || 9564 OpInfo.ConstraintType != TargetLowering::C_Memory) && 9565 "Operand must be indirect to be a mem!"); 9566 assert(InOperandVal.getValueType() == 9567 TLI.getPointerTy(DAG.getDataLayout()) && 9568 "Memory operands expect pointer values"); 9569 9570 const InlineAsm::ConstraintCode ConstraintID = 9571 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode); 9572 assert(ConstraintID != InlineAsm::ConstraintCode::Unknown && 9573 "Failed to convert memory constraint code to constraint id."); 9574 9575 // Add information to the INLINEASM node to know about this input. 9576 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1); 9577 ResOpType.setMemConstraint(ConstraintID); 9578 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType, 9579 getCurSDLoc(), 9580 MVT::i32)); 9581 AsmNodeOperands.push_back(InOperandVal); 9582 break; 9583 } 9584 9585 if (OpInfo.ConstraintType == TargetLowering::C_Address) { 9586 const InlineAsm::ConstraintCode ConstraintID = 9587 TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode); 9588 assert(ConstraintID != InlineAsm::ConstraintCode::Unknown && 9589 "Failed to convert memory constraint code to constraint id."); 9590 9591 InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1); 9592 9593 SDValue AsmOp = InOperandVal; 9594 if (isFunction(InOperandVal)) { 9595 auto *GA = cast<GlobalAddressSDNode>(InOperandVal); 9596 ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1); 9597 AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(), 9598 InOperandVal.getValueType(), 9599 GA->getOffset()); 9600 } 9601 9602 // Add information to the INLINEASM node to know about this input. 9603 ResOpType.setMemConstraint(ConstraintID); 9604 9605 AsmNodeOperands.push_back( 9606 DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32)); 9607 9608 AsmNodeOperands.push_back(AsmOp); 9609 break; 9610 } 9611 9612 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass || 9613 OpInfo.ConstraintType == TargetLowering::C_Register) && 9614 "Unknown constraint type!"); 9615 9616 // TODO: Support this. 9617 if (OpInfo.isIndirect) { 9618 emitInlineAsmError( 9619 Call, "Don't know how to handle indirect register inputs yet " 9620 "for constraint '" + 9621 Twine(OpInfo.ConstraintCode) + "'"); 9622 return; 9623 } 9624 9625 // Copy the input into the appropriate registers. 9626 if (OpInfo.AssignedRegs.Regs.empty()) { 9627 emitInlineAsmError(Call, 9628 "couldn't allocate input reg for constraint '" + 9629 Twine(OpInfo.ConstraintCode) + "'"); 9630 return; 9631 } 9632 9633 if (DetectWriteToReservedRegister()) 9634 return; 9635 9636 SDLoc dl = getCurSDLoc(); 9637 9638 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, 9639 &Call); 9640 9641 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false, 9642 0, dl, DAG, AsmNodeOperands); 9643 break; 9644 } 9645 case InlineAsm::isClobber: 9646 // Add the clobbered value to the operand list, so that the register 9647 // allocator is aware that the physreg got clobbered. 9648 if (!OpInfo.AssignedRegs.Regs.empty()) 9649 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::Clobber, 9650 false, 0, getCurSDLoc(), DAG, 9651 AsmNodeOperands); 9652 break; 9653 } 9654 } 9655 9656 // Finish up input operands. Set the input chain and add the flag last. 9657 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain; 9658 if (Glue.getNode()) AsmNodeOperands.push_back(Glue); 9659 9660 unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM; 9661 Chain = DAG.getNode(ISDOpc, getCurSDLoc(), 9662 DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands); 9663 Glue = Chain.getValue(1); 9664 9665 // Do additional work to generate outputs. 9666 9667 SmallVector<EVT, 1> ResultVTs; 9668 SmallVector<SDValue, 1> ResultValues; 9669 SmallVector<SDValue, 8> OutChains; 9670 9671 llvm::Type *CallResultType = Call.getType(); 9672 ArrayRef<Type *> ResultTypes; 9673 if (StructType *StructResult = dyn_cast<StructType>(CallResultType)) 9674 ResultTypes = StructResult->elements(); 9675 else if (!CallResultType->isVoidTy()) 9676 ResultTypes = ArrayRef(CallResultType); 9677 9678 auto CurResultType = ResultTypes.begin(); 9679 auto handleRegAssign = [&](SDValue V) { 9680 assert(CurResultType != ResultTypes.end() && "Unexpected value"); 9681 assert((*CurResultType)->isSized() && "Unexpected unsized type"); 9682 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType); 9683 ++CurResultType; 9684 // If the type of the inline asm call site return value is different but has 9685 // same size as the type of the asm output bitcast it. One example of this 9686 // is for vectors with different width / number of elements. This can 9687 // happen for register classes that can contain multiple different value 9688 // types. The preg or vreg allocated may not have the same VT as was 9689 // expected. 9690 // 9691 // This can also happen for a return value that disagrees with the register 9692 // class it is put in, eg. a double in a general-purpose register on a 9693 // 32-bit machine. 9694 if (ResultVT != V.getValueType() && 9695 ResultVT.getSizeInBits() == V.getValueSizeInBits()) 9696 V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V); 9697 else if (ResultVT != V.getValueType() && ResultVT.isInteger() && 9698 V.getValueType().isInteger()) { 9699 // If a result value was tied to an input value, the computed result 9700 // may have a wider width than the expected result. Extract the 9701 // relevant portion. 9702 V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V); 9703 } 9704 assert(ResultVT == V.getValueType() && "Asm result value mismatch!"); 9705 ResultVTs.push_back(ResultVT); 9706 ResultValues.push_back(V); 9707 }; 9708 9709 // Deal with output operands. 9710 for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) { 9711 if (OpInfo.Type == InlineAsm::isOutput) { 9712 SDValue Val; 9713 // Skip trivial output operands. 9714 if (OpInfo.AssignedRegs.Regs.empty()) 9715 continue; 9716 9717 switch (OpInfo.ConstraintType) { 9718 case TargetLowering::C_Register: 9719 case TargetLowering::C_RegisterClass: 9720 Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), 9721 Chain, &Glue, &Call); 9722 break; 9723 case TargetLowering::C_Immediate: 9724 case TargetLowering::C_Other: 9725 Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(), 9726 OpInfo, DAG); 9727 break; 9728 case TargetLowering::C_Memory: 9729 break; // Already handled. 9730 case TargetLowering::C_Address: 9731 break; // Silence warning. 9732 case TargetLowering::C_Unknown: 9733 assert(false && "Unexpected unknown constraint"); 9734 } 9735 9736 // Indirect output manifest as stores. Record output chains. 9737 if (OpInfo.isIndirect) { 9738 const Value *Ptr = OpInfo.CallOperandVal; 9739 assert(Ptr && "Expected value CallOperandVal for indirect asm operand"); 9740 SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr), 9741 MachinePointerInfo(Ptr)); 9742 OutChains.push_back(Store); 9743 } else { 9744 // generate CopyFromRegs to associated registers. 9745 assert(!Call.getType()->isVoidTy() && "Bad inline asm!"); 9746 if (Val.getOpcode() == ISD::MERGE_VALUES) { 9747 for (const SDValue &V : Val->op_values()) 9748 handleRegAssign(V); 9749 } else 9750 handleRegAssign(Val); 9751 } 9752 } 9753 } 9754 9755 // Set results. 9756 if (!ResultValues.empty()) { 9757 assert(CurResultType == ResultTypes.end() && 9758 "Mismatch in number of ResultTypes"); 9759 assert(ResultValues.size() == ResultTypes.size() && 9760 "Mismatch in number of output operands in asm result"); 9761 9762 SDValue V = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 9763 DAG.getVTList(ResultVTs), ResultValues); 9764 setValue(&Call, V); 9765 } 9766 9767 // Collect store chains. 9768 if (!OutChains.empty()) 9769 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains); 9770 9771 if (EmitEHLabels) { 9772 Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel); 9773 } 9774 9775 // Only Update Root if inline assembly has a memory effect. 9776 if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr || 9777 EmitEHLabels) 9778 DAG.setRoot(Chain); 9779 } 9780 9781 void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call, 9782 const Twine &Message) { 9783 LLVMContext &Ctx = *DAG.getContext(); 9784 Ctx.emitError(&Call, Message); 9785 9786 // Make sure we leave the DAG in a valid state 9787 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9788 SmallVector<EVT, 1> ValueVTs; 9789 ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs); 9790 9791 if (ValueVTs.empty()) 9792 return; 9793 9794 SmallVector<SDValue, 1> Ops; 9795 for (unsigned i = 0, e = ValueVTs.size(); i != e; ++i) 9796 Ops.push_back(DAG.getUNDEF(ValueVTs[i])); 9797 9798 setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc())); 9799 } 9800 9801 void SelectionDAGBuilder::visitVAStart(const CallInst &I) { 9802 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(), 9803 MVT::Other, getRoot(), 9804 getValue(I.getArgOperand(0)), 9805 DAG.getSrcValue(I.getArgOperand(0)))); 9806 } 9807 9808 void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) { 9809 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 9810 const DataLayout &DL = DAG.getDataLayout(); 9811 SDValue V = DAG.getVAArg( 9812 TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(), 9813 getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)), 9814 DL.getABITypeAlign(I.getType()).value()); 9815 DAG.setRoot(V.getValue(1)); 9816 9817 if (I.getType()->isPointerTy()) 9818 V = DAG.getPtrExtOrTrunc( 9819 V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType())); 9820 setValue(&I, V); 9821 } 9822 9823 void SelectionDAGBuilder::visitVAEnd(const CallInst &I) { 9824 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(), 9825 MVT::Other, getRoot(), 9826 getValue(I.getArgOperand(0)), 9827 DAG.getSrcValue(I.getArgOperand(0)))); 9828 } 9829 9830 void SelectionDAGBuilder::visitVACopy(const CallInst &I) { 9831 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(), 9832 MVT::Other, getRoot(), 9833 getValue(I.getArgOperand(0)), 9834 getValue(I.getArgOperand(1)), 9835 DAG.getSrcValue(I.getArgOperand(0)), 9836 DAG.getSrcValue(I.getArgOperand(1)))); 9837 } 9838 9839 SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG, 9840 const Instruction &I, 9841 SDValue Op) { 9842 const MDNode *Range = getRangeMetadata(I); 9843 if (!Range) 9844 return Op; 9845 9846 ConstantRange CR = getConstantRangeFromMetadata(*Range); 9847 if (CR.isFullSet() || CR.isEmptySet() || CR.isUpperWrapped()) 9848 return Op; 9849 9850 APInt Lo = CR.getUnsignedMin(); 9851 if (!Lo.isMinValue()) 9852 return Op; 9853 9854 APInt Hi = CR.getUnsignedMax(); 9855 unsigned Bits = std::max(Hi.getActiveBits(), 9856 static_cast<unsigned>(IntegerType::MIN_INT_BITS)); 9857 9858 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits); 9859 9860 SDLoc SL = getCurSDLoc(); 9861 9862 SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op, 9863 DAG.getValueType(SmallVT)); 9864 unsigned NumVals = Op.getNode()->getNumValues(); 9865 if (NumVals == 1) 9866 return ZExt; 9867 9868 SmallVector<SDValue, 4> Ops; 9869 9870 Ops.push_back(ZExt); 9871 for (unsigned I = 1; I != NumVals; ++I) 9872 Ops.push_back(Op.getValue(I)); 9873 9874 return DAG.getMergeValues(Ops, SL); 9875 } 9876 9877 /// Populate a CallLowerinInfo (into \p CLI) based on the properties of 9878 /// the call being lowered. 9879 /// 9880 /// This is a helper for lowering intrinsics that follow a target calling 9881 /// convention or require stack pointer adjustment. Only a subset of the 9882 /// intrinsic's operands need to participate in the calling convention. 9883 void SelectionDAGBuilder::populateCallLoweringInfo( 9884 TargetLowering::CallLoweringInfo &CLI, const CallBase *Call, 9885 unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy, 9886 AttributeSet RetAttrs, bool IsPatchPoint) { 9887 TargetLowering::ArgListTy Args; 9888 Args.reserve(NumArgs); 9889 9890 // Populate the argument list. 9891 // Attributes for args start at offset 1, after the return attribute. 9892 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs; 9893 ArgI != ArgE; ++ArgI) { 9894 const Value *V = Call->getOperand(ArgI); 9895 9896 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic."); 9897 9898 TargetLowering::ArgListEntry Entry; 9899 Entry.Node = getValue(V); 9900 Entry.Ty = V->getType(); 9901 Entry.setAttributes(Call, ArgI); 9902 Args.push_back(Entry); 9903 } 9904 9905 CLI.setDebugLoc(getCurSDLoc()) 9906 .setChain(getRoot()) 9907 .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args), 9908 RetAttrs) 9909 .setDiscardResult(Call->use_empty()) 9910 .setIsPatchPoint(IsPatchPoint) 9911 .setIsPreallocated( 9912 Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0); 9913 } 9914 9915 /// Add a stack map intrinsic call's live variable operands to a stackmap 9916 /// or patchpoint target node's operand list. 9917 /// 9918 /// Constants are converted to TargetConstants purely as an optimization to 9919 /// avoid constant materialization and register allocation. 9920 /// 9921 /// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not 9922 /// generate addess computation nodes, and so FinalizeISel can convert the 9923 /// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids 9924 /// address materialization and register allocation, but may also be required 9925 /// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an 9926 /// alloca in the entry block, then the runtime may assume that the alloca's 9927 /// StackMap location can be read immediately after compilation and that the 9928 /// location is valid at any point during execution (this is similar to the 9929 /// assumption made by the llvm.gcroot intrinsic). If the alloca's location were 9930 /// only available in a register, then the runtime would need to trap when 9931 /// execution reaches the StackMap in order to read the alloca's location. 9932 static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx, 9933 const SDLoc &DL, SmallVectorImpl<SDValue> &Ops, 9934 SelectionDAGBuilder &Builder) { 9935 SelectionDAG &DAG = Builder.DAG; 9936 for (unsigned I = StartIdx; I < Call.arg_size(); I++) { 9937 SDValue Op = Builder.getValue(Call.getArgOperand(I)); 9938 9939 // Things on the stack are pointer-typed, meaning that they are already 9940 // legal and can be emitted directly to target nodes. 9941 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) { 9942 Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType())); 9943 } else { 9944 // Otherwise emit a target independent node to be legalised. 9945 Ops.push_back(Builder.getValue(Call.getArgOperand(I))); 9946 } 9947 } 9948 } 9949 9950 /// Lower llvm.experimental.stackmap. 9951 void SelectionDAGBuilder::visitStackmap(const CallInst &CI) { 9952 // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>, 9953 // [live variables...]) 9954 9955 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value."); 9956 9957 SDValue Chain, InGlue, Callee; 9958 SmallVector<SDValue, 32> Ops; 9959 9960 SDLoc DL = getCurSDLoc(); 9961 Callee = getValue(CI.getCalledOperand()); 9962 9963 // The stackmap intrinsic only records the live variables (the arguments 9964 // passed to it) and emits NOPS (if requested). Unlike the patchpoint 9965 // intrinsic, this won't be lowered to a function call. This means we don't 9966 // have to worry about calling conventions and target specific lowering code. 9967 // Instead we perform the call lowering right here. 9968 // 9969 // chain, flag = CALLSEQ_START(chain, 0, 0) 9970 // chain, flag = STACKMAP(id, nbytes, ..., chain, flag) 9971 // chain, flag = CALLSEQ_END(chain, 0, 0, flag) 9972 // 9973 Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL); 9974 InGlue = Chain.getValue(1); 9975 9976 // Add the STACKMAP operands, starting with DAG house-keeping. 9977 Ops.push_back(Chain); 9978 Ops.push_back(InGlue); 9979 9980 // Add the <id>, <numShadowBytes> operands. 9981 // 9982 // These do not require legalisation, and can be emitted directly to target 9983 // constant nodes. 9984 SDValue ID = getValue(CI.getArgOperand(0)); 9985 assert(ID.getValueType() == MVT::i64); 9986 SDValue IDConst = 9987 DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType()); 9988 Ops.push_back(IDConst); 9989 9990 SDValue Shad = getValue(CI.getArgOperand(1)); 9991 assert(Shad.getValueType() == MVT::i32); 9992 SDValue ShadConst = 9993 DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType()); 9994 Ops.push_back(ShadConst); 9995 9996 // Add the live variables. 9997 addStackMapLiveVars(CI, 2, DL, Ops, *this); 9998 9999 // Create the STACKMAP node. 10000 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 10001 Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops); 10002 InGlue = Chain.getValue(1); 10003 10004 Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL); 10005 10006 // Stackmaps don't generate values, so nothing goes into the NodeMap. 10007 10008 // Set the root to the target-lowered call chain. 10009 DAG.setRoot(Chain); 10010 10011 // Inform the Frame Information that we have a stackmap in this function. 10012 FuncInfo.MF->getFrameInfo().setHasStackMap(); 10013 } 10014 10015 /// Lower llvm.experimental.patchpoint directly to its target opcode. 10016 void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB, 10017 const BasicBlock *EHPadBB) { 10018 // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>, 10019 // i32 <numBytes>, 10020 // i8* <target>, 10021 // i32 <numArgs>, 10022 // [Args...], 10023 // [live variables...]) 10024 10025 CallingConv::ID CC = CB.getCallingConv(); 10026 bool IsAnyRegCC = CC == CallingConv::AnyReg; 10027 bool HasDef = !CB.getType()->isVoidTy(); 10028 SDLoc dl = getCurSDLoc(); 10029 SDValue Callee = getValue(CB.getArgOperand(PatchPointOpers::TargetPos)); 10030 10031 // Handle immediate and symbolic callees. 10032 if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee)) 10033 Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl, 10034 /*isTarget=*/true); 10035 else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee)) 10036 Callee = DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(), 10037 SDLoc(SymbolicCallee), 10038 SymbolicCallee->getValueType(0)); 10039 10040 // Get the real number of arguments participating in the call <numArgs> 10041 SDValue NArgVal = getValue(CB.getArgOperand(PatchPointOpers::NArgPos)); 10042 unsigned NumArgs = NArgVal->getAsZExtVal(); 10043 10044 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs> 10045 // Intrinsics include all meta-operands up to but not including CC. 10046 unsigned NumMetaOpers = PatchPointOpers::CCPos; 10047 assert(CB.arg_size() >= NumMetaOpers + NumArgs && 10048 "Not enough arguments provided to the patchpoint intrinsic"); 10049 10050 // For AnyRegCC the arguments are lowered later on manually. 10051 unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs; 10052 Type *ReturnTy = 10053 IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType(); 10054 10055 TargetLowering::CallLoweringInfo CLI(DAG); 10056 populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee, 10057 ReturnTy, CB.getAttributes().getRetAttrs(), true); 10058 std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB); 10059 10060 SDNode *CallEnd = Result.second.getNode(); 10061 if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg)) 10062 CallEnd = CallEnd->getOperand(0).getNode(); 10063 10064 /// Get a call instruction from the call sequence chain. 10065 /// Tail calls are not allowed. 10066 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END && 10067 "Expected a callseq node."); 10068 SDNode *Call = CallEnd->getOperand(0).getNode(); 10069 bool HasGlue = Call->getGluedNode(); 10070 10071 // Replace the target specific call node with the patchable intrinsic. 10072 SmallVector<SDValue, 8> Ops; 10073 10074 // Push the chain. 10075 Ops.push_back(*(Call->op_begin())); 10076 10077 // Optionally, push the glue (if any). 10078 if (HasGlue) 10079 Ops.push_back(*(Call->op_end() - 1)); 10080 10081 // Push the register mask info. 10082 if (HasGlue) 10083 Ops.push_back(*(Call->op_end() - 2)); 10084 else 10085 Ops.push_back(*(Call->op_end() - 1)); 10086 10087 // Add the <id> and <numBytes> constants. 10088 SDValue IDVal = getValue(CB.getArgOperand(PatchPointOpers::IDPos)); 10089 Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64)); 10090 SDValue NBytesVal = getValue(CB.getArgOperand(PatchPointOpers::NBytesPos)); 10091 Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32)); 10092 10093 // Add the callee. 10094 Ops.push_back(Callee); 10095 10096 // Adjust <numArgs> to account for any arguments that have been passed on the 10097 // stack instead. 10098 // Call Node: Chain, Target, {Args}, RegMask, [Glue] 10099 unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3); 10100 NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs; 10101 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32)); 10102 10103 // Add the calling convention 10104 Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32)); 10105 10106 // Add the arguments we omitted previously. The register allocator should 10107 // place these in any free register. 10108 if (IsAnyRegCC) 10109 for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) 10110 Ops.push_back(getValue(CB.getArgOperand(i))); 10111 10112 // Push the arguments from the call instruction. 10113 SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1; 10114 Ops.append(Call->op_begin() + 2, e); 10115 10116 // Push live variables for the stack map. 10117 addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this); 10118 10119 SDVTList NodeTys; 10120 if (IsAnyRegCC && HasDef) { 10121 // Create the return types based on the intrinsic definition 10122 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10123 SmallVector<EVT, 3> ValueVTs; 10124 ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs); 10125 assert(ValueVTs.size() == 1 && "Expected only one return value type."); 10126 10127 // There is always a chain and a glue type at the end 10128 ValueVTs.push_back(MVT::Other); 10129 ValueVTs.push_back(MVT::Glue); 10130 NodeTys = DAG.getVTList(ValueVTs); 10131 } else 10132 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 10133 10134 // Replace the target specific call node with a PATCHPOINT node. 10135 SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops); 10136 10137 // Update the NodeMap. 10138 if (HasDef) { 10139 if (IsAnyRegCC) 10140 setValue(&CB, SDValue(PPV.getNode(), 0)); 10141 else 10142 setValue(&CB, Result.first); 10143 } 10144 10145 // Fixup the consumers of the intrinsic. The chain and glue may be used in the 10146 // call sequence. Furthermore the location of the chain and glue can change 10147 // when the AnyReg calling convention is used and the intrinsic returns a 10148 // value. 10149 if (IsAnyRegCC && HasDef) { 10150 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)}; 10151 SDValue To[] = {PPV.getValue(1), PPV.getValue(2)}; 10152 DAG.ReplaceAllUsesOfValuesWith(From, To, 2); 10153 } else 10154 DAG.ReplaceAllUsesWith(Call, PPV.getNode()); 10155 DAG.DeleteNode(Call); 10156 10157 // Inform the Frame Information that we have a patchpoint in this function. 10158 FuncInfo.MF->getFrameInfo().setHasPatchPoint(); 10159 } 10160 10161 void SelectionDAGBuilder::visitVectorReduce(const CallInst &I, 10162 unsigned Intrinsic) { 10163 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10164 SDValue Op1 = getValue(I.getArgOperand(0)); 10165 SDValue Op2; 10166 if (I.arg_size() > 1) 10167 Op2 = getValue(I.getArgOperand(1)); 10168 SDLoc dl = getCurSDLoc(); 10169 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 10170 SDValue Res; 10171 SDNodeFlags SDFlags; 10172 if (auto *FPMO = dyn_cast<FPMathOperator>(&I)) 10173 SDFlags.copyFMF(*FPMO); 10174 10175 switch (Intrinsic) { 10176 case Intrinsic::vector_reduce_fadd: 10177 if (SDFlags.hasAllowReassociation()) 10178 Res = DAG.getNode(ISD::FADD, dl, VT, Op1, 10179 DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags), 10180 SDFlags); 10181 else 10182 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags); 10183 break; 10184 case Intrinsic::vector_reduce_fmul: 10185 if (SDFlags.hasAllowReassociation()) 10186 Res = DAG.getNode(ISD::FMUL, dl, VT, Op1, 10187 DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags), 10188 SDFlags); 10189 else 10190 Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags); 10191 break; 10192 case Intrinsic::vector_reduce_add: 10193 Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1); 10194 break; 10195 case Intrinsic::vector_reduce_mul: 10196 Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1); 10197 break; 10198 case Intrinsic::vector_reduce_and: 10199 Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1); 10200 break; 10201 case Intrinsic::vector_reduce_or: 10202 Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1); 10203 break; 10204 case Intrinsic::vector_reduce_xor: 10205 Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1); 10206 break; 10207 case Intrinsic::vector_reduce_smax: 10208 Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1); 10209 break; 10210 case Intrinsic::vector_reduce_smin: 10211 Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1); 10212 break; 10213 case Intrinsic::vector_reduce_umax: 10214 Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1); 10215 break; 10216 case Intrinsic::vector_reduce_umin: 10217 Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1); 10218 break; 10219 case Intrinsic::vector_reduce_fmax: 10220 Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags); 10221 break; 10222 case Intrinsic::vector_reduce_fmin: 10223 Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags); 10224 break; 10225 case Intrinsic::vector_reduce_fmaximum: 10226 Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags); 10227 break; 10228 case Intrinsic::vector_reduce_fminimum: 10229 Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags); 10230 break; 10231 default: 10232 llvm_unreachable("Unhandled vector reduce intrinsic"); 10233 } 10234 setValue(&I, Res); 10235 } 10236 10237 /// Returns an AttributeList representing the attributes applied to the return 10238 /// value of the given call. 10239 static AttributeList getReturnAttrs(TargetLowering::CallLoweringInfo &CLI) { 10240 SmallVector<Attribute::AttrKind, 2> Attrs; 10241 if (CLI.RetSExt) 10242 Attrs.push_back(Attribute::SExt); 10243 if (CLI.RetZExt) 10244 Attrs.push_back(Attribute::ZExt); 10245 if (CLI.IsInReg) 10246 Attrs.push_back(Attribute::InReg); 10247 10248 return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex, 10249 Attrs); 10250 } 10251 10252 /// TargetLowering::LowerCallTo - This is the default LowerCallTo 10253 /// implementation, which just calls LowerCall. 10254 /// FIXME: When all targets are 10255 /// migrated to using LowerCall, this hook should be integrated into SDISel. 10256 std::pair<SDValue, SDValue> 10257 TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const { 10258 // Handle the incoming return values from the call. 10259 CLI.Ins.clear(); 10260 Type *OrigRetTy = CLI.RetTy; 10261 SmallVector<EVT, 4> RetTys; 10262 SmallVector<uint64_t, 4> Offsets; 10263 auto &DL = CLI.DAG.getDataLayout(); 10264 ComputeValueVTs(*this, DL, CLI.RetTy, RetTys, &Offsets, 0); 10265 10266 if (CLI.IsPostTypeLegalization) { 10267 // If we are lowering a libcall after legalization, split the return type. 10268 SmallVector<EVT, 4> OldRetTys; 10269 SmallVector<uint64_t, 4> OldOffsets; 10270 RetTys.swap(OldRetTys); 10271 Offsets.swap(OldOffsets); 10272 10273 for (size_t i = 0, e = OldRetTys.size(); i != e; ++i) { 10274 EVT RetVT = OldRetTys[i]; 10275 uint64_t Offset = OldOffsets[i]; 10276 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), RetVT); 10277 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), RetVT); 10278 unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8; 10279 RetTys.append(NumRegs, RegisterVT); 10280 for (unsigned j = 0; j != NumRegs; ++j) 10281 Offsets.push_back(Offset + j * RegisterVTByteSZ); 10282 } 10283 } 10284 10285 SmallVector<ISD::OutputArg, 4> Outs; 10286 GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL); 10287 10288 bool CanLowerReturn = 10289 this->CanLowerReturn(CLI.CallConv, CLI.DAG.getMachineFunction(), 10290 CLI.IsVarArg, Outs, CLI.RetTy->getContext()); 10291 10292 SDValue DemoteStackSlot; 10293 int DemoteStackIdx = -100; 10294 if (!CanLowerReturn) { 10295 // FIXME: equivalent assert? 10296 // assert(!CS.hasInAllocaArgument() && 10297 // "sret demotion is incompatible with inalloca"); 10298 uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy); 10299 Align Alignment = DL.getPrefTypeAlign(CLI.RetTy); 10300 MachineFunction &MF = CLI.DAG.getMachineFunction(); 10301 DemoteStackIdx = 10302 MF.getFrameInfo().CreateStackObject(TySize, Alignment, false); 10303 Type *StackSlotPtrType = PointerType::get(CLI.RetTy, 10304 DL.getAllocaAddrSpace()); 10305 10306 DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL)); 10307 ArgListEntry Entry; 10308 Entry.Node = DemoteStackSlot; 10309 Entry.Ty = StackSlotPtrType; 10310 Entry.IsSExt = false; 10311 Entry.IsZExt = false; 10312 Entry.IsInReg = false; 10313 Entry.IsSRet = true; 10314 Entry.IsNest = false; 10315 Entry.IsByVal = false; 10316 Entry.IsByRef = false; 10317 Entry.IsReturned = false; 10318 Entry.IsSwiftSelf = false; 10319 Entry.IsSwiftAsync = false; 10320 Entry.IsSwiftError = false; 10321 Entry.IsCFGuardTarget = false; 10322 Entry.Alignment = Alignment; 10323 CLI.getArgs().insert(CLI.getArgs().begin(), Entry); 10324 CLI.NumFixedArgs += 1; 10325 CLI.getArgs()[0].IndirectType = CLI.RetTy; 10326 CLI.RetTy = Type::getVoidTy(CLI.RetTy->getContext()); 10327 10328 // sret demotion isn't compatible with tail-calls, since the sret argument 10329 // points into the callers stack frame. 10330 CLI.IsTailCall = false; 10331 } else { 10332 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters( 10333 CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL); 10334 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) { 10335 ISD::ArgFlagsTy Flags; 10336 if (NeedsRegBlock) { 10337 Flags.setInConsecutiveRegs(); 10338 if (I == RetTys.size() - 1) 10339 Flags.setInConsecutiveRegsLast(); 10340 } 10341 EVT VT = RetTys[I]; 10342 MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(), 10343 CLI.CallConv, VT); 10344 unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(), 10345 CLI.CallConv, VT); 10346 for (unsigned i = 0; i != NumRegs; ++i) { 10347 ISD::InputArg MyFlags; 10348 MyFlags.Flags = Flags; 10349 MyFlags.VT = RegisterVT; 10350 MyFlags.ArgVT = VT; 10351 MyFlags.Used = CLI.IsReturnValueUsed; 10352 if (CLI.RetTy->isPointerTy()) { 10353 MyFlags.Flags.setPointer(); 10354 MyFlags.Flags.setPointerAddrSpace( 10355 cast<PointerType>(CLI.RetTy)->getAddressSpace()); 10356 } 10357 if (CLI.RetSExt) 10358 MyFlags.Flags.setSExt(); 10359 if (CLI.RetZExt) 10360 MyFlags.Flags.setZExt(); 10361 if (CLI.IsInReg) 10362 MyFlags.Flags.setInReg(); 10363 CLI.Ins.push_back(MyFlags); 10364 } 10365 } 10366 } 10367 10368 // We push in swifterror return as the last element of CLI.Ins. 10369 ArgListTy &Args = CLI.getArgs(); 10370 if (supportSwiftError()) { 10371 for (const ArgListEntry &Arg : Args) { 10372 if (Arg.IsSwiftError) { 10373 ISD::InputArg MyFlags; 10374 MyFlags.VT = getPointerTy(DL); 10375 MyFlags.ArgVT = EVT(getPointerTy(DL)); 10376 MyFlags.Flags.setSwiftError(); 10377 CLI.Ins.push_back(MyFlags); 10378 } 10379 } 10380 } 10381 10382 // Handle all of the outgoing arguments. 10383 CLI.Outs.clear(); 10384 CLI.OutVals.clear(); 10385 for (unsigned i = 0, e = Args.size(); i != e; ++i) { 10386 SmallVector<EVT, 4> ValueVTs; 10387 ComputeValueVTs(*this, DL, Args[i].Ty, ValueVTs); 10388 // FIXME: Split arguments if CLI.IsPostTypeLegalization 10389 Type *FinalType = Args[i].Ty; 10390 if (Args[i].IsByVal) 10391 FinalType = Args[i].IndirectType; 10392 bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters( 10393 FinalType, CLI.CallConv, CLI.IsVarArg, DL); 10394 for (unsigned Value = 0, NumValues = ValueVTs.size(); Value != NumValues; 10395 ++Value) { 10396 EVT VT = ValueVTs[Value]; 10397 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext()); 10398 SDValue Op = SDValue(Args[i].Node.getNode(), 10399 Args[i].Node.getResNo() + Value); 10400 ISD::ArgFlagsTy Flags; 10401 10402 // Certain targets (such as MIPS), may have a different ABI alignment 10403 // for a type depending on the context. Give the target a chance to 10404 // specify the alignment it wants. 10405 const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL)); 10406 Flags.setOrigAlign(OriginalAlignment); 10407 10408 if (Args[i].Ty->isPointerTy()) { 10409 Flags.setPointer(); 10410 Flags.setPointerAddrSpace( 10411 cast<PointerType>(Args[i].Ty)->getAddressSpace()); 10412 } 10413 if (Args[i].IsZExt) 10414 Flags.setZExt(); 10415 if (Args[i].IsSExt) 10416 Flags.setSExt(); 10417 if (Args[i].IsInReg) { 10418 // If we are using vectorcall calling convention, a structure that is 10419 // passed InReg - is surely an HVA 10420 if (CLI.CallConv == CallingConv::X86_VectorCall && 10421 isa<StructType>(FinalType)) { 10422 // The first value of a structure is marked 10423 if (0 == Value) 10424 Flags.setHvaStart(); 10425 Flags.setHva(); 10426 } 10427 // Set InReg Flag 10428 Flags.setInReg(); 10429 } 10430 if (Args[i].IsSRet) 10431 Flags.setSRet(); 10432 if (Args[i].IsSwiftSelf) 10433 Flags.setSwiftSelf(); 10434 if (Args[i].IsSwiftAsync) 10435 Flags.setSwiftAsync(); 10436 if (Args[i].IsSwiftError) 10437 Flags.setSwiftError(); 10438 if (Args[i].IsCFGuardTarget) 10439 Flags.setCFGuardTarget(); 10440 if (Args[i].IsByVal) 10441 Flags.setByVal(); 10442 if (Args[i].IsByRef) 10443 Flags.setByRef(); 10444 if (Args[i].IsPreallocated) { 10445 Flags.setPreallocated(); 10446 // Set the byval flag for CCAssignFn callbacks that don't know about 10447 // preallocated. This way we can know how many bytes we should've 10448 // allocated and how many bytes a callee cleanup function will pop. If 10449 // we port preallocated to more targets, we'll have to add custom 10450 // preallocated handling in the various CC lowering callbacks. 10451 Flags.setByVal(); 10452 } 10453 if (Args[i].IsInAlloca) { 10454 Flags.setInAlloca(); 10455 // Set the byval flag for CCAssignFn callbacks that don't know about 10456 // inalloca. This way we can know how many bytes we should've allocated 10457 // and how many bytes a callee cleanup function will pop. If we port 10458 // inalloca to more targets, we'll have to add custom inalloca handling 10459 // in the various CC lowering callbacks. 10460 Flags.setByVal(); 10461 } 10462 Align MemAlign; 10463 if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) { 10464 unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType); 10465 Flags.setByValSize(FrameSize); 10466 10467 // info is not there but there are cases it cannot get right. 10468 if (auto MA = Args[i].Alignment) 10469 MemAlign = *MA; 10470 else 10471 MemAlign = Align(getByValTypeAlignment(Args[i].IndirectType, DL)); 10472 } else if (auto MA = Args[i].Alignment) { 10473 MemAlign = *MA; 10474 } else { 10475 MemAlign = OriginalAlignment; 10476 } 10477 Flags.setMemAlign(MemAlign); 10478 if (Args[i].IsNest) 10479 Flags.setNest(); 10480 if (NeedsRegBlock) 10481 Flags.setInConsecutiveRegs(); 10482 10483 MVT PartVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(), 10484 CLI.CallConv, VT); 10485 unsigned NumParts = getNumRegistersForCallingConv(CLI.RetTy->getContext(), 10486 CLI.CallConv, VT); 10487 SmallVector<SDValue, 4> Parts(NumParts); 10488 ISD::NodeType ExtendKind = ISD::ANY_EXTEND; 10489 10490 if (Args[i].IsSExt) 10491 ExtendKind = ISD::SIGN_EXTEND; 10492 else if (Args[i].IsZExt) 10493 ExtendKind = ISD::ZERO_EXTEND; 10494 10495 // Conservatively only handle 'returned' on non-vectors that can be lowered, 10496 // for now. 10497 if (Args[i].IsReturned && !Op.getValueType().isVector() && 10498 CanLowerReturn) { 10499 assert((CLI.RetTy == Args[i].Ty || 10500 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() && 10501 CLI.RetTy->getPointerAddressSpace() == 10502 Args[i].Ty->getPointerAddressSpace())) && 10503 RetTys.size() == NumValues && "unexpected use of 'returned'"); 10504 // Before passing 'returned' to the target lowering code, ensure that 10505 // either the register MVT and the actual EVT are the same size or that 10506 // the return value and argument are extended in the same way; in these 10507 // cases it's safe to pass the argument register value unchanged as the 10508 // return register value (although it's at the target's option whether 10509 // to do so) 10510 // TODO: allow code generation to take advantage of partially preserved 10511 // registers rather than clobbering the entire register when the 10512 // parameter extension method is not compatible with the return 10513 // extension method 10514 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) || 10515 (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt && 10516 CLI.RetZExt == Args[i].IsZExt)) 10517 Flags.setReturned(); 10518 } 10519 10520 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB, 10521 CLI.CallConv, ExtendKind); 10522 10523 for (unsigned j = 0; j != NumParts; ++j) { 10524 // if it isn't first piece, alignment must be 1 10525 // For scalable vectors the scalable part is currently handled 10526 // by individual targets, so we just use the known minimum size here. 10527 ISD::OutputArg MyFlags( 10528 Flags, Parts[j].getValueType().getSimpleVT(), VT, 10529 i < CLI.NumFixedArgs, i, 10530 j * Parts[j].getValueType().getStoreSize().getKnownMinValue()); 10531 if (NumParts > 1 && j == 0) 10532 MyFlags.Flags.setSplit(); 10533 else if (j != 0) { 10534 MyFlags.Flags.setOrigAlign(Align(1)); 10535 if (j == NumParts - 1) 10536 MyFlags.Flags.setSplitEnd(); 10537 } 10538 10539 CLI.Outs.push_back(MyFlags); 10540 CLI.OutVals.push_back(Parts[j]); 10541 } 10542 10543 if (NeedsRegBlock && Value == NumValues - 1) 10544 CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast(); 10545 } 10546 } 10547 10548 SmallVector<SDValue, 4> InVals; 10549 CLI.Chain = LowerCall(CLI, InVals); 10550 10551 // Update CLI.InVals to use outside of this function. 10552 CLI.InVals = InVals; 10553 10554 // Verify that the target's LowerCall behaved as expected. 10555 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other && 10556 "LowerCall didn't return a valid chain!"); 10557 assert((!CLI.IsTailCall || InVals.empty()) && 10558 "LowerCall emitted a return value for a tail call!"); 10559 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) && 10560 "LowerCall didn't emit the correct number of values!"); 10561 10562 // For a tail call, the return value is merely live-out and there aren't 10563 // any nodes in the DAG representing it. Return a special value to 10564 // indicate that a tail call has been emitted and no more Instructions 10565 // should be processed in the current block. 10566 if (CLI.IsTailCall) { 10567 CLI.DAG.setRoot(CLI.Chain); 10568 return std::make_pair(SDValue(), SDValue()); 10569 } 10570 10571 #ifndef NDEBUG 10572 for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) { 10573 assert(InVals[i].getNode() && "LowerCall emitted a null value!"); 10574 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() && 10575 "LowerCall emitted a value with the wrong type!"); 10576 } 10577 #endif 10578 10579 SmallVector<SDValue, 4> ReturnValues; 10580 if (!CanLowerReturn) { 10581 // The instruction result is the result of loading from the 10582 // hidden sret parameter. 10583 SmallVector<EVT, 1> PVTs; 10584 Type *PtrRetTy = 10585 PointerType::get(OrigRetTy->getContext(), DL.getAllocaAddrSpace()); 10586 10587 ComputeValueVTs(*this, DL, PtrRetTy, PVTs); 10588 assert(PVTs.size() == 1 && "Pointers should fit in one register"); 10589 EVT PtrVT = PVTs[0]; 10590 10591 unsigned NumValues = RetTys.size(); 10592 ReturnValues.resize(NumValues); 10593 SmallVector<SDValue, 4> Chains(NumValues); 10594 10595 // An aggregate return value cannot wrap around the address space, so 10596 // offsets to its parts don't wrap either. 10597 SDNodeFlags Flags; 10598 Flags.setNoUnsignedWrap(true); 10599 10600 MachineFunction &MF = CLI.DAG.getMachineFunction(); 10601 Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx); 10602 for (unsigned i = 0; i < NumValues; ++i) { 10603 SDValue Add = CLI.DAG.getNode(ISD::ADD, CLI.DL, PtrVT, DemoteStackSlot, 10604 CLI.DAG.getConstant(Offsets[i], CLI.DL, 10605 PtrVT), Flags); 10606 SDValue L = CLI.DAG.getLoad( 10607 RetTys[i], CLI.DL, CLI.Chain, Add, 10608 MachinePointerInfo::getFixedStack(CLI.DAG.getMachineFunction(), 10609 DemoteStackIdx, Offsets[i]), 10610 HiddenSRetAlign); 10611 ReturnValues[i] = L; 10612 Chains[i] = L.getValue(1); 10613 } 10614 10615 CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains); 10616 } else { 10617 // Collect the legal value parts into potentially illegal values 10618 // that correspond to the original function's return values. 10619 std::optional<ISD::NodeType> AssertOp; 10620 if (CLI.RetSExt) 10621 AssertOp = ISD::AssertSext; 10622 else if (CLI.RetZExt) 10623 AssertOp = ISD::AssertZext; 10624 unsigned CurReg = 0; 10625 for (EVT VT : RetTys) { 10626 MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(), 10627 CLI.CallConv, VT); 10628 unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(), 10629 CLI.CallConv, VT); 10630 10631 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg], 10632 NumRegs, RegisterVT, VT, nullptr, 10633 CLI.CallConv, AssertOp)); 10634 CurReg += NumRegs; 10635 } 10636 10637 // For a function returning void, there is no return value. We can't create 10638 // such a node, so we just return a null return value in that case. In 10639 // that case, nothing will actually look at the value. 10640 if (ReturnValues.empty()) 10641 return std::make_pair(SDValue(), CLI.Chain); 10642 } 10643 10644 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL, 10645 CLI.DAG.getVTList(RetTys), ReturnValues); 10646 return std::make_pair(Res, CLI.Chain); 10647 } 10648 10649 /// Places new result values for the node in Results (their number 10650 /// and types must exactly match those of the original return values of 10651 /// the node), or leaves Results empty, which indicates that the node is not 10652 /// to be custom lowered after all. 10653 void TargetLowering::LowerOperationWrapper(SDNode *N, 10654 SmallVectorImpl<SDValue> &Results, 10655 SelectionDAG &DAG) const { 10656 SDValue Res = LowerOperation(SDValue(N, 0), DAG); 10657 10658 if (!Res.getNode()) 10659 return; 10660 10661 // If the original node has one result, take the return value from 10662 // LowerOperation as is. It might not be result number 0. 10663 if (N->getNumValues() == 1) { 10664 Results.push_back(Res); 10665 return; 10666 } 10667 10668 // If the original node has multiple results, then the return node should 10669 // have the same number of results. 10670 assert((N->getNumValues() == Res->getNumValues()) && 10671 "Lowering returned the wrong number of results!"); 10672 10673 // Places new result values base on N result number. 10674 for (unsigned I = 0, E = N->getNumValues(); I != E; ++I) 10675 Results.push_back(Res.getValue(I)); 10676 } 10677 10678 SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 10679 llvm_unreachable("LowerOperation not implemented for this target!"); 10680 } 10681 10682 void SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, 10683 unsigned Reg, 10684 ISD::NodeType ExtendType) { 10685 SDValue Op = getNonRegisterValue(V); 10686 assert((Op.getOpcode() != ISD::CopyFromReg || 10687 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) && 10688 "Copy from a reg to the same reg!"); 10689 assert(!Register::isPhysicalRegister(Reg) && "Is a physreg"); 10690 10691 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 10692 // If this is an InlineAsm we have to match the registers required, not the 10693 // notional registers required by the type. 10694 10695 RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(), 10696 std::nullopt); // This is not an ABI copy. 10697 SDValue Chain = DAG.getEntryNode(); 10698 10699 if (ExtendType == ISD::ANY_EXTEND) { 10700 auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V); 10701 if (PreferredExtendIt != FuncInfo.PreferredExtendType.end()) 10702 ExtendType = PreferredExtendIt->second; 10703 } 10704 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType); 10705 PendingExports.push_back(Chain); 10706 } 10707 10708 #include "llvm/CodeGen/SelectionDAGISel.h" 10709 10710 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the 10711 /// entry block, return true. This includes arguments used by switches, since 10712 /// the switch may expand into multiple basic blocks. 10713 static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) { 10714 // With FastISel active, we may be splitting blocks, so force creation 10715 // of virtual registers for all non-dead arguments. 10716 if (FastISel) 10717 return A->use_empty(); 10718 10719 const BasicBlock &Entry = A->getParent()->front(); 10720 for (const User *U : A->users()) 10721 if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U)) 10722 return false; // Use not in entry block. 10723 10724 return true; 10725 } 10726 10727 using ArgCopyElisionMapTy = 10728 DenseMap<const Argument *, 10729 std::pair<const AllocaInst *, const StoreInst *>>; 10730 10731 /// Scan the entry block of the function in FuncInfo for arguments that look 10732 /// like copies into a local alloca. Record any copied arguments in 10733 /// ArgCopyElisionCandidates. 10734 static void 10735 findArgumentCopyElisionCandidates(const DataLayout &DL, 10736 FunctionLoweringInfo *FuncInfo, 10737 ArgCopyElisionMapTy &ArgCopyElisionCandidates) { 10738 // Record the state of every static alloca used in the entry block. Argument 10739 // allocas are all used in the entry block, so we need approximately as many 10740 // entries as we have arguments. 10741 enum StaticAllocaInfo { Unknown, Clobbered, Elidable }; 10742 SmallDenseMap<const AllocaInst *, StaticAllocaInfo, 8> StaticAllocas; 10743 unsigned NumArgs = FuncInfo->Fn->arg_size(); 10744 StaticAllocas.reserve(NumArgs * 2); 10745 10746 auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * { 10747 if (!V) 10748 return nullptr; 10749 V = V->stripPointerCasts(); 10750 const auto *AI = dyn_cast<AllocaInst>(V); 10751 if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI)) 10752 return nullptr; 10753 auto Iter = StaticAllocas.insert({AI, Unknown}); 10754 return &Iter.first->second; 10755 }; 10756 10757 // Look for stores of arguments to static allocas. Look through bitcasts and 10758 // GEPs to handle type coercions, as long as the alloca is fully initialized 10759 // by the store. Any non-store use of an alloca escapes it and any subsequent 10760 // unanalyzed store might write it. 10761 // FIXME: Handle structs initialized with multiple stores. 10762 for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) { 10763 // Look for stores, and handle non-store uses conservatively. 10764 const auto *SI = dyn_cast<StoreInst>(&I); 10765 if (!SI) { 10766 // We will look through cast uses, so ignore them completely. 10767 if (I.isCast()) 10768 continue; 10769 // Ignore debug info and pseudo op intrinsics, they don't escape or store 10770 // to allocas. 10771 if (I.isDebugOrPseudoInst()) 10772 continue; 10773 // This is an unknown instruction. Assume it escapes or writes to all 10774 // static alloca operands. 10775 for (const Use &U : I.operands()) { 10776 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U)) 10777 *Info = StaticAllocaInfo::Clobbered; 10778 } 10779 continue; 10780 } 10781 10782 // If the stored value is a static alloca, mark it as escaped. 10783 if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand())) 10784 *Info = StaticAllocaInfo::Clobbered; 10785 10786 // Check if the destination is a static alloca. 10787 const Value *Dst = SI->getPointerOperand()->stripPointerCasts(); 10788 StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst); 10789 if (!Info) 10790 continue; 10791 const AllocaInst *AI = cast<AllocaInst>(Dst); 10792 10793 // Skip allocas that have been initialized or clobbered. 10794 if (*Info != StaticAllocaInfo::Unknown) 10795 continue; 10796 10797 // Check if the stored value is an argument, and that this store fully 10798 // initializes the alloca. 10799 // If the argument type has padding bits we can't directly forward a pointer 10800 // as the upper bits may contain garbage. 10801 // Don't elide copies from the same argument twice. 10802 const Value *Val = SI->getValueOperand()->stripPointerCasts(); 10803 const auto *Arg = dyn_cast<Argument>(Val); 10804 if (!Arg || Arg->hasPassPointeeByValueCopyAttr() || 10805 Arg->getType()->isEmptyTy() || 10806 DL.getTypeStoreSize(Arg->getType()) != 10807 DL.getTypeAllocSize(AI->getAllocatedType()) || 10808 !DL.typeSizeEqualsStoreSize(Arg->getType()) || 10809 ArgCopyElisionCandidates.count(Arg)) { 10810 *Info = StaticAllocaInfo::Clobbered; 10811 continue; 10812 } 10813 10814 LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI 10815 << '\n'); 10816 10817 // Mark this alloca and store for argument copy elision. 10818 *Info = StaticAllocaInfo::Elidable; 10819 ArgCopyElisionCandidates.insert({Arg, {AI, SI}}); 10820 10821 // Stop scanning if we've seen all arguments. This will happen early in -O0 10822 // builds, which is useful, because -O0 builds have large entry blocks and 10823 // many allocas. 10824 if (ArgCopyElisionCandidates.size() == NumArgs) 10825 break; 10826 } 10827 } 10828 10829 /// Try to elide argument copies from memory into a local alloca. Succeeds if 10830 /// ArgVal is a load from a suitable fixed stack object. 10831 static void tryToElideArgumentCopy( 10832 FunctionLoweringInfo &FuncInfo, SmallVectorImpl<SDValue> &Chains, 10833 DenseMap<int, int> &ArgCopyElisionFrameIndexMap, 10834 SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs, 10835 ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg, 10836 ArrayRef<SDValue> ArgVals, bool &ArgHasUses) { 10837 // Check if this is a load from a fixed stack object. 10838 auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]); 10839 if (!LNode) 10840 return; 10841 auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()); 10842 if (!FINode) 10843 return; 10844 10845 // Check that the fixed stack object is the right size and alignment. 10846 // Look at the alignment that the user wrote on the alloca instead of looking 10847 // at the stack object. 10848 auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg); 10849 assert(ArgCopyIter != ArgCopyElisionCandidates.end()); 10850 const AllocaInst *AI = ArgCopyIter->second.first; 10851 int FixedIndex = FINode->getIndex(); 10852 int &AllocaIndex = FuncInfo.StaticAllocaMap[AI]; 10853 int OldIndex = AllocaIndex; 10854 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo(); 10855 if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) { 10856 LLVM_DEBUG( 10857 dbgs() << " argument copy elision failed due to bad fixed stack " 10858 "object size\n"); 10859 return; 10860 } 10861 Align RequiredAlignment = AI->getAlign(); 10862 if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) { 10863 LLVM_DEBUG(dbgs() << " argument copy elision failed: alignment of alloca " 10864 "greater than stack argument alignment (" 10865 << DebugStr(RequiredAlignment) << " vs " 10866 << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n"); 10867 return; 10868 } 10869 10870 // Perform the elision. Delete the old stack object and replace its only use 10871 // in the variable info map. Mark the stack object as mutable. 10872 LLVM_DEBUG({ 10873 dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n' 10874 << " Replacing frame index " << OldIndex << " with " << FixedIndex 10875 << '\n'; 10876 }); 10877 MFI.RemoveStackObject(OldIndex); 10878 MFI.setIsImmutableObjectIndex(FixedIndex, false); 10879 AllocaIndex = FixedIndex; 10880 ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex}); 10881 for (SDValue ArgVal : ArgVals) 10882 Chains.push_back(ArgVal.getValue(1)); 10883 10884 // Avoid emitting code for the store implementing the copy. 10885 const StoreInst *SI = ArgCopyIter->second.second; 10886 ElidedArgCopyInstrs.insert(SI); 10887 10888 // Check for uses of the argument again so that we can avoid exporting ArgVal 10889 // if it is't used by anything other than the store. 10890 for (const Value *U : Arg.users()) { 10891 if (U != SI) { 10892 ArgHasUses = true; 10893 break; 10894 } 10895 } 10896 } 10897 10898 void SelectionDAGISel::LowerArguments(const Function &F) { 10899 SelectionDAG &DAG = SDB->DAG; 10900 SDLoc dl = SDB->getCurSDLoc(); 10901 const DataLayout &DL = DAG.getDataLayout(); 10902 SmallVector<ISD::InputArg, 16> Ins; 10903 10904 // In Naked functions we aren't going to save any registers. 10905 if (F.hasFnAttribute(Attribute::Naked)) 10906 return; 10907 10908 if (!FuncInfo->CanLowerReturn) { 10909 // Put in an sret pointer parameter before all the other parameters. 10910 SmallVector<EVT, 1> ValueVTs; 10911 ComputeValueVTs(*TLI, DAG.getDataLayout(), 10912 PointerType::get(F.getContext(), 10913 DAG.getDataLayout().getAllocaAddrSpace()), 10914 ValueVTs); 10915 10916 // NOTE: Assuming that a pointer will never break down to more than one VT 10917 // or one register. 10918 ISD::ArgFlagsTy Flags; 10919 Flags.setSRet(); 10920 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]); 10921 ISD::InputArg RetArg(Flags, RegisterVT, ValueVTs[0], true, 10922 ISD::InputArg::NoArgIndex, 0); 10923 Ins.push_back(RetArg); 10924 } 10925 10926 // Look for stores of arguments to static allocas. Mark such arguments with a 10927 // flag to ask the target to give us the memory location of that argument if 10928 // available. 10929 ArgCopyElisionMapTy ArgCopyElisionCandidates; 10930 findArgumentCopyElisionCandidates(DL, FuncInfo.get(), 10931 ArgCopyElisionCandidates); 10932 10933 // Set up the incoming argument description vector. 10934 for (const Argument &Arg : F.args()) { 10935 unsigned ArgNo = Arg.getArgNo(); 10936 SmallVector<EVT, 4> ValueVTs; 10937 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs); 10938 bool isArgValueUsed = !Arg.use_empty(); 10939 unsigned PartBase = 0; 10940 Type *FinalType = Arg.getType(); 10941 if (Arg.hasAttribute(Attribute::ByVal)) 10942 FinalType = Arg.getParamByValType(); 10943 bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters( 10944 FinalType, F.getCallingConv(), F.isVarArg(), DL); 10945 for (unsigned Value = 0, NumValues = ValueVTs.size(); 10946 Value != NumValues; ++Value) { 10947 EVT VT = ValueVTs[Value]; 10948 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext()); 10949 ISD::ArgFlagsTy Flags; 10950 10951 10952 if (Arg.getType()->isPointerTy()) { 10953 Flags.setPointer(); 10954 Flags.setPointerAddrSpace( 10955 cast<PointerType>(Arg.getType())->getAddressSpace()); 10956 } 10957 if (Arg.hasAttribute(Attribute::ZExt)) 10958 Flags.setZExt(); 10959 if (Arg.hasAttribute(Attribute::SExt)) 10960 Flags.setSExt(); 10961 if (Arg.hasAttribute(Attribute::InReg)) { 10962 // If we are using vectorcall calling convention, a structure that is 10963 // passed InReg - is surely an HVA 10964 if (F.getCallingConv() == CallingConv::X86_VectorCall && 10965 isa<StructType>(Arg.getType())) { 10966 // The first value of a structure is marked 10967 if (0 == Value) 10968 Flags.setHvaStart(); 10969 Flags.setHva(); 10970 } 10971 // Set InReg Flag 10972 Flags.setInReg(); 10973 } 10974 if (Arg.hasAttribute(Attribute::StructRet)) 10975 Flags.setSRet(); 10976 if (Arg.hasAttribute(Attribute::SwiftSelf)) 10977 Flags.setSwiftSelf(); 10978 if (Arg.hasAttribute(Attribute::SwiftAsync)) 10979 Flags.setSwiftAsync(); 10980 if (Arg.hasAttribute(Attribute::SwiftError)) 10981 Flags.setSwiftError(); 10982 if (Arg.hasAttribute(Attribute::ByVal)) 10983 Flags.setByVal(); 10984 if (Arg.hasAttribute(Attribute::ByRef)) 10985 Flags.setByRef(); 10986 if (Arg.hasAttribute(Attribute::InAlloca)) { 10987 Flags.setInAlloca(); 10988 // Set the byval flag for CCAssignFn callbacks that don't know about 10989 // inalloca. This way we can know how many bytes we should've allocated 10990 // and how many bytes a callee cleanup function will pop. If we port 10991 // inalloca to more targets, we'll have to add custom inalloca handling 10992 // in the various CC lowering callbacks. 10993 Flags.setByVal(); 10994 } 10995 if (Arg.hasAttribute(Attribute::Preallocated)) { 10996 Flags.setPreallocated(); 10997 // Set the byval flag for CCAssignFn callbacks that don't know about 10998 // preallocated. This way we can know how many bytes we should've 10999 // allocated and how many bytes a callee cleanup function will pop. If 11000 // we port preallocated to more targets, we'll have to add custom 11001 // preallocated handling in the various CC lowering callbacks. 11002 Flags.setByVal(); 11003 } 11004 11005 // Certain targets (such as MIPS), may have a different ABI alignment 11006 // for a type depending on the context. Give the target a chance to 11007 // specify the alignment it wants. 11008 const Align OriginalAlignment( 11009 TLI->getABIAlignmentForCallingConv(ArgTy, DL)); 11010 Flags.setOrigAlign(OriginalAlignment); 11011 11012 Align MemAlign; 11013 Type *ArgMemTy = nullptr; 11014 if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() || 11015 Flags.isByRef()) { 11016 if (!ArgMemTy) 11017 ArgMemTy = Arg.getPointeeInMemoryValueType(); 11018 11019 uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy); 11020 11021 // For in-memory arguments, size and alignment should be passed from FE. 11022 // BE will guess if this info is not there but there are cases it cannot 11023 // get right. 11024 if (auto ParamAlign = Arg.getParamStackAlign()) 11025 MemAlign = *ParamAlign; 11026 else if ((ParamAlign = Arg.getParamAlign())) 11027 MemAlign = *ParamAlign; 11028 else 11029 MemAlign = Align(TLI->getByValTypeAlignment(ArgMemTy, DL)); 11030 if (Flags.isByRef()) 11031 Flags.setByRefSize(MemSize); 11032 else 11033 Flags.setByValSize(MemSize); 11034 } else if (auto ParamAlign = Arg.getParamStackAlign()) { 11035 MemAlign = *ParamAlign; 11036 } else { 11037 MemAlign = OriginalAlignment; 11038 } 11039 Flags.setMemAlign(MemAlign); 11040 11041 if (Arg.hasAttribute(Attribute::Nest)) 11042 Flags.setNest(); 11043 if (NeedsRegBlock) 11044 Flags.setInConsecutiveRegs(); 11045 if (ArgCopyElisionCandidates.count(&Arg)) 11046 Flags.setCopyElisionCandidate(); 11047 if (Arg.hasAttribute(Attribute::Returned)) 11048 Flags.setReturned(); 11049 11050 MVT RegisterVT = TLI->getRegisterTypeForCallingConv( 11051 *CurDAG->getContext(), F.getCallingConv(), VT); 11052 unsigned NumRegs = TLI->getNumRegistersForCallingConv( 11053 *CurDAG->getContext(), F.getCallingConv(), VT); 11054 for (unsigned i = 0; i != NumRegs; ++i) { 11055 // For scalable vectors, use the minimum size; individual targets 11056 // are responsible for handling scalable vector arguments and 11057 // return values. 11058 ISD::InputArg MyFlags( 11059 Flags, RegisterVT, VT, isArgValueUsed, ArgNo, 11060 PartBase + i * RegisterVT.getStoreSize().getKnownMinValue()); 11061 if (NumRegs > 1 && i == 0) 11062 MyFlags.Flags.setSplit(); 11063 // if it isn't first piece, alignment must be 1 11064 else if (i > 0) { 11065 MyFlags.Flags.setOrigAlign(Align(1)); 11066 if (i == NumRegs - 1) 11067 MyFlags.Flags.setSplitEnd(); 11068 } 11069 Ins.push_back(MyFlags); 11070 } 11071 if (NeedsRegBlock && Value == NumValues - 1) 11072 Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast(); 11073 PartBase += VT.getStoreSize().getKnownMinValue(); 11074 } 11075 } 11076 11077 // Call the target to set up the argument values. 11078 SmallVector<SDValue, 8> InVals; 11079 SDValue NewRoot = TLI->LowerFormalArguments( 11080 DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals); 11081 11082 // Verify that the target's LowerFormalArguments behaved as expected. 11083 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other && 11084 "LowerFormalArguments didn't return a valid chain!"); 11085 assert(InVals.size() == Ins.size() && 11086 "LowerFormalArguments didn't emit the correct number of values!"); 11087 LLVM_DEBUG({ 11088 for (unsigned i = 0, e = Ins.size(); i != e; ++i) { 11089 assert(InVals[i].getNode() && 11090 "LowerFormalArguments emitted a null value!"); 11091 assert(EVT(Ins[i].VT) == InVals[i].getValueType() && 11092 "LowerFormalArguments emitted a value with the wrong type!"); 11093 } 11094 }); 11095 11096 // Update the DAG with the new chain value resulting from argument lowering. 11097 DAG.setRoot(NewRoot); 11098 11099 // Set up the argument values. 11100 unsigned i = 0; 11101 if (!FuncInfo->CanLowerReturn) { 11102 // Create a virtual register for the sret pointer, and put in a copy 11103 // from the sret argument into it. 11104 SmallVector<EVT, 1> ValueVTs; 11105 ComputeValueVTs(*TLI, DAG.getDataLayout(), 11106 PointerType::get(F.getContext(), 11107 DAG.getDataLayout().getAllocaAddrSpace()), 11108 ValueVTs); 11109 MVT VT = ValueVTs[0].getSimpleVT(); 11110 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT); 11111 std::optional<ISD::NodeType> AssertOp; 11112 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, 11113 nullptr, F.getCallingConv(), AssertOp); 11114 11115 MachineFunction& MF = SDB->DAG.getMachineFunction(); 11116 MachineRegisterInfo& RegInfo = MF.getRegInfo(); 11117 Register SRetReg = 11118 RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT)); 11119 FuncInfo->DemoteRegister = SRetReg; 11120 NewRoot = 11121 SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue); 11122 DAG.setRoot(NewRoot); 11123 11124 // i indexes lowered arguments. Bump it past the hidden sret argument. 11125 ++i; 11126 } 11127 11128 SmallVector<SDValue, 4> Chains; 11129 DenseMap<int, int> ArgCopyElisionFrameIndexMap; 11130 for (const Argument &Arg : F.args()) { 11131 SmallVector<SDValue, 4> ArgValues; 11132 SmallVector<EVT, 4> ValueVTs; 11133 ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs); 11134 unsigned NumValues = ValueVTs.size(); 11135 if (NumValues == 0) 11136 continue; 11137 11138 bool ArgHasUses = !Arg.use_empty(); 11139 11140 // Elide the copying store if the target loaded this argument from a 11141 // suitable fixed stack object. 11142 if (Ins[i].Flags.isCopyElisionCandidate()) { 11143 unsigned NumParts = 0; 11144 for (EVT VT : ValueVTs) 11145 NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(), 11146 F.getCallingConv(), VT); 11147 11148 tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap, 11149 ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg, 11150 ArrayRef(&InVals[i], NumParts), ArgHasUses); 11151 } 11152 11153 // If this argument is unused then remember its value. It is used to generate 11154 // debugging information. 11155 bool isSwiftErrorArg = 11156 TLI->supportSwiftError() && 11157 Arg.hasAttribute(Attribute::SwiftError); 11158 if (!ArgHasUses && !isSwiftErrorArg) { 11159 SDB->setUnusedArgValue(&Arg, InVals[i]); 11160 11161 // Also remember any frame index for use in FastISel. 11162 if (FrameIndexSDNode *FI = 11163 dyn_cast<FrameIndexSDNode>(InVals[i].getNode())) 11164 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex()); 11165 } 11166 11167 for (unsigned Val = 0; Val != NumValues; ++Val) { 11168 EVT VT = ValueVTs[Val]; 11169 MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(), 11170 F.getCallingConv(), VT); 11171 unsigned NumParts = TLI->getNumRegistersForCallingConv( 11172 *CurDAG->getContext(), F.getCallingConv(), VT); 11173 11174 // Even an apparent 'unused' swifterror argument needs to be returned. So 11175 // we do generate a copy for it that can be used on return from the 11176 // function. 11177 if (ArgHasUses || isSwiftErrorArg) { 11178 std::optional<ISD::NodeType> AssertOp; 11179 if (Arg.hasAttribute(Attribute::SExt)) 11180 AssertOp = ISD::AssertSext; 11181 else if (Arg.hasAttribute(Attribute::ZExt)) 11182 AssertOp = ISD::AssertZext; 11183 11184 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts, 11185 PartVT, VT, nullptr, 11186 F.getCallingConv(), AssertOp)); 11187 } 11188 11189 i += NumParts; 11190 } 11191 11192 // We don't need to do anything else for unused arguments. 11193 if (ArgValues.empty()) 11194 continue; 11195 11196 // Note down frame index. 11197 if (FrameIndexSDNode *FI = 11198 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode())) 11199 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex()); 11200 11201 SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues), 11202 SDB->getCurSDLoc()); 11203 11204 SDB->setValue(&Arg, Res); 11205 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) { 11206 // We want to associate the argument with the frame index, among 11207 // involved operands, that correspond to the lowest address. The 11208 // getCopyFromParts function, called earlier, is swapping the order of 11209 // the operands to BUILD_PAIR depending on endianness. The result of 11210 // that swapping is that the least significant bits of the argument will 11211 // be in the first operand of the BUILD_PAIR node, and the most 11212 // significant bits will be in the second operand. 11213 unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0; 11214 if (LoadSDNode *LNode = 11215 dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode())) 11216 if (FrameIndexSDNode *FI = 11217 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) 11218 FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex()); 11219 } 11220 11221 // Analyses past this point are naive and don't expect an assertion. 11222 if (Res.getOpcode() == ISD::AssertZext) 11223 Res = Res.getOperand(0); 11224 11225 // Update the SwiftErrorVRegDefMap. 11226 if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) { 11227 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); 11228 if (Register::isVirtualRegister(Reg)) 11229 SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(), 11230 Reg); 11231 } 11232 11233 // If this argument is live outside of the entry block, insert a copy from 11234 // wherever we got it to the vreg that other BB's will reference it as. 11235 if (Res.getOpcode() == ISD::CopyFromReg) { 11236 // If we can, though, try to skip creating an unnecessary vreg. 11237 // FIXME: This isn't very clean... it would be nice to make this more 11238 // general. 11239 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg(); 11240 if (Register::isVirtualRegister(Reg)) { 11241 FuncInfo->ValueMap[&Arg] = Reg; 11242 continue; 11243 } 11244 } 11245 if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) { 11246 FuncInfo->InitializeRegForValue(&Arg); 11247 SDB->CopyToExportRegsIfNeeded(&Arg); 11248 } 11249 } 11250 11251 if (!Chains.empty()) { 11252 Chains.push_back(NewRoot); 11253 NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains); 11254 } 11255 11256 DAG.setRoot(NewRoot); 11257 11258 assert(i == InVals.size() && "Argument register count mismatch!"); 11259 11260 // If any argument copy elisions occurred and we have debug info, update the 11261 // stale frame indices used in the dbg.declare variable info table. 11262 if (!ArgCopyElisionFrameIndexMap.empty()) { 11263 for (MachineFunction::VariableDbgInfo &VI : 11264 MF->getInStackSlotVariableDbgInfo()) { 11265 auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot()); 11266 if (I != ArgCopyElisionFrameIndexMap.end()) 11267 VI.updateStackSlot(I->second); 11268 } 11269 } 11270 11271 // Finally, if the target has anything special to do, allow it to do so. 11272 emitFunctionEntryCode(); 11273 } 11274 11275 /// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to 11276 /// ensure constants are generated when needed. Remember the virtual registers 11277 /// that need to be added to the Machine PHI nodes as input. We cannot just 11278 /// directly add them, because expansion might result in multiple MBB's for one 11279 /// BB. As such, the start of the BB might correspond to a different MBB than 11280 /// the end. 11281 void 11282 SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) { 11283 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11284 11285 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled; 11286 11287 // Check PHI nodes in successors that expect a value to be available from this 11288 // block. 11289 for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) { 11290 if (!isa<PHINode>(SuccBB->begin())) continue; 11291 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB]; 11292 11293 // If this terminator has multiple identical successors (common for 11294 // switches), only handle each succ once. 11295 if (!SuccsHandled.insert(SuccMBB).second) 11296 continue; 11297 11298 MachineBasicBlock::iterator MBBI = SuccMBB->begin(); 11299 11300 // At this point we know that there is a 1-1 correspondence between LLVM PHI 11301 // nodes and Machine PHI nodes, but the incoming operands have not been 11302 // emitted yet. 11303 for (const PHINode &PN : SuccBB->phis()) { 11304 // Ignore dead phi's. 11305 if (PN.use_empty()) 11306 continue; 11307 11308 // Skip empty types 11309 if (PN.getType()->isEmptyTy()) 11310 continue; 11311 11312 unsigned Reg; 11313 const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB); 11314 11315 if (const auto *C = dyn_cast<Constant>(PHIOp)) { 11316 unsigned &RegOut = ConstantsOut[C]; 11317 if (RegOut == 0) { 11318 RegOut = FuncInfo.CreateRegs(C); 11319 // We need to zero/sign extend ConstantInt phi operands to match 11320 // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo. 11321 ISD::NodeType ExtendType = ISD::ANY_EXTEND; 11322 if (auto *CI = dyn_cast<ConstantInt>(C)) 11323 ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND 11324 : ISD::ZERO_EXTEND; 11325 CopyValueToVirtualRegister(C, RegOut, ExtendType); 11326 } 11327 Reg = RegOut; 11328 } else { 11329 DenseMap<const Value *, Register>::iterator I = 11330 FuncInfo.ValueMap.find(PHIOp); 11331 if (I != FuncInfo.ValueMap.end()) 11332 Reg = I->second; 11333 else { 11334 assert(isa<AllocaInst>(PHIOp) && 11335 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) && 11336 "Didn't codegen value into a register!??"); 11337 Reg = FuncInfo.CreateRegs(PHIOp); 11338 CopyValueToVirtualRegister(PHIOp, Reg); 11339 } 11340 } 11341 11342 // Remember that this register needs to added to the machine PHI node as 11343 // the input for this MBB. 11344 SmallVector<EVT, 4> ValueVTs; 11345 ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs); 11346 for (EVT VT : ValueVTs) { 11347 const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT); 11348 for (unsigned i = 0; i != NumRegisters; ++i) 11349 FuncInfo.PHINodesToUpdate.push_back( 11350 std::make_pair(&*MBBI++, Reg + i)); 11351 Reg += NumRegisters; 11352 } 11353 } 11354 } 11355 11356 ConstantsOut.clear(); 11357 } 11358 11359 MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) { 11360 MachineFunction::iterator I(MBB); 11361 if (++I == FuncInfo.MF->end()) 11362 return nullptr; 11363 return &*I; 11364 } 11365 11366 /// During lowering new call nodes can be created (such as memset, etc.). 11367 /// Those will become new roots of the current DAG, but complications arise 11368 /// when they are tail calls. In such cases, the call lowering will update 11369 /// the root, but the builder still needs to know that a tail call has been 11370 /// lowered in order to avoid generating an additional return. 11371 void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) { 11372 // If the node is null, we do have a tail call. 11373 if (MaybeTC.getNode() != nullptr) 11374 DAG.setRoot(MaybeTC); 11375 else 11376 HasTailCall = true; 11377 } 11378 11379 void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond, 11380 MachineBasicBlock *SwitchMBB, 11381 MachineBasicBlock *DefaultMBB) { 11382 MachineFunction *CurMF = FuncInfo.MF; 11383 MachineBasicBlock *NextMBB = nullptr; 11384 MachineFunction::iterator BBI(W.MBB); 11385 if (++BBI != FuncInfo.MF->end()) 11386 NextMBB = &*BBI; 11387 11388 unsigned Size = W.LastCluster - W.FirstCluster + 1; 11389 11390 BranchProbabilityInfo *BPI = FuncInfo.BPI; 11391 11392 if (Size == 2 && W.MBB == SwitchMBB) { 11393 // If any two of the cases has the same destination, and if one value 11394 // is the same as the other, but has one bit unset that the other has set, 11395 // use bit manipulation to do two compares at once. For example: 11396 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)" 11397 // TODO: This could be extended to merge any 2 cases in switches with 3 11398 // cases. 11399 // TODO: Handle cases where W.CaseBB != SwitchBB. 11400 CaseCluster &Small = *W.FirstCluster; 11401 CaseCluster &Big = *W.LastCluster; 11402 11403 if (Small.Low == Small.High && Big.Low == Big.High && 11404 Small.MBB == Big.MBB) { 11405 const APInt &SmallValue = Small.Low->getValue(); 11406 const APInt &BigValue = Big.Low->getValue(); 11407 11408 // Check that there is only one bit different. 11409 APInt CommonBit = BigValue ^ SmallValue; 11410 if (CommonBit.isPowerOf2()) { 11411 SDValue CondLHS = getValue(Cond); 11412 EVT VT = CondLHS.getValueType(); 11413 SDLoc DL = getCurSDLoc(); 11414 11415 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, 11416 DAG.getConstant(CommonBit, DL, VT)); 11417 SDValue Cond = DAG.getSetCC( 11418 DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT), 11419 ISD::SETEQ); 11420 11421 // Update successor info. 11422 // Both Small and Big will jump to Small.BB, so we sum up the 11423 // probabilities. 11424 addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob); 11425 if (BPI) 11426 addSuccessorWithProb( 11427 SwitchMBB, DefaultMBB, 11428 // The default destination is the first successor in IR. 11429 BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0)); 11430 else 11431 addSuccessorWithProb(SwitchMBB, DefaultMBB); 11432 11433 // Insert the true branch. 11434 SDValue BrCond = 11435 DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond, 11436 DAG.getBasicBlock(Small.MBB)); 11437 // Insert the false branch. 11438 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond, 11439 DAG.getBasicBlock(DefaultMBB)); 11440 11441 DAG.setRoot(BrCond); 11442 return; 11443 } 11444 } 11445 } 11446 11447 if (TM.getOptLevel() != CodeGenOptLevel::None) { 11448 // Here, we order cases by probability so the most likely case will be 11449 // checked first. However, two clusters can have the same probability in 11450 // which case their relative ordering is non-deterministic. So we use Low 11451 // as a tie-breaker as clusters are guaranteed to never overlap. 11452 llvm::sort(W.FirstCluster, W.LastCluster + 1, 11453 [](const CaseCluster &a, const CaseCluster &b) { 11454 return a.Prob != b.Prob ? 11455 a.Prob > b.Prob : 11456 a.Low->getValue().slt(b.Low->getValue()); 11457 }); 11458 11459 // Rearrange the case blocks so that the last one falls through if possible 11460 // without changing the order of probabilities. 11461 for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) { 11462 --I; 11463 if (I->Prob > W.LastCluster->Prob) 11464 break; 11465 if (I->Kind == CC_Range && I->MBB == NextMBB) { 11466 std::swap(*I, *W.LastCluster); 11467 break; 11468 } 11469 } 11470 } 11471 11472 // Compute total probability. 11473 BranchProbability DefaultProb = W.DefaultProb; 11474 BranchProbability UnhandledProbs = DefaultProb; 11475 for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I) 11476 UnhandledProbs += I->Prob; 11477 11478 MachineBasicBlock *CurMBB = W.MBB; 11479 for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) { 11480 bool FallthroughUnreachable = false; 11481 MachineBasicBlock *Fallthrough; 11482 if (I == W.LastCluster) { 11483 // For the last cluster, fall through to the default destination. 11484 Fallthrough = DefaultMBB; 11485 FallthroughUnreachable = isa<UnreachableInst>( 11486 DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg()); 11487 } else { 11488 Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock()); 11489 CurMF->insert(BBI, Fallthrough); 11490 // Put Cond in a virtual register to make it available from the new blocks. 11491 ExportFromCurrentBlock(Cond); 11492 } 11493 UnhandledProbs -= I->Prob; 11494 11495 switch (I->Kind) { 11496 case CC_JumpTable: { 11497 // FIXME: Optimize away range check based on pivot comparisons. 11498 JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first; 11499 SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second; 11500 11501 // The jump block hasn't been inserted yet; insert it here. 11502 MachineBasicBlock *JumpMBB = JT->MBB; 11503 CurMF->insert(BBI, JumpMBB); 11504 11505 auto JumpProb = I->Prob; 11506 auto FallthroughProb = UnhandledProbs; 11507 11508 // If the default statement is a target of the jump table, we evenly 11509 // distribute the default probability to successors of CurMBB. Also 11510 // update the probability on the edge from JumpMBB to Fallthrough. 11511 for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(), 11512 SE = JumpMBB->succ_end(); 11513 SI != SE; ++SI) { 11514 if (*SI == DefaultMBB) { 11515 JumpProb += DefaultProb / 2; 11516 FallthroughProb -= DefaultProb / 2; 11517 JumpMBB->setSuccProbability(SI, DefaultProb / 2); 11518 JumpMBB->normalizeSuccProbs(); 11519 break; 11520 } 11521 } 11522 11523 // If the default clause is unreachable, propagate that knowledge into 11524 // JTH->FallthroughUnreachable which will use it to suppress the range 11525 // check. 11526 // 11527 // However, don't do this if we're doing branch target enforcement, 11528 // because a table branch _without_ a range check can be a tempting JOP 11529 // gadget - out-of-bounds inputs that are impossible in correct 11530 // execution become possible again if an attacker can influence the 11531 // control flow. So if an attacker doesn't already have a BTI bypass 11532 // available, we don't want them to be able to get one out of this 11533 // table branch. 11534 if (FallthroughUnreachable) { 11535 Function &CurFunc = CurMF->getFunction(); 11536 bool HasBranchTargetEnforcement = false; 11537 if (CurFunc.hasFnAttribute("branch-target-enforcement")) { 11538 HasBranchTargetEnforcement = 11539 CurFunc.getFnAttribute("branch-target-enforcement") 11540 .getValueAsBool(); 11541 } else { 11542 HasBranchTargetEnforcement = 11543 CurMF->getMMI().getModule()->getModuleFlag( 11544 "branch-target-enforcement"); 11545 } 11546 if (!HasBranchTargetEnforcement) 11547 JTH->FallthroughUnreachable = true; 11548 } 11549 11550 if (!JTH->FallthroughUnreachable) 11551 addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb); 11552 addSuccessorWithProb(CurMBB, JumpMBB, JumpProb); 11553 CurMBB->normalizeSuccProbs(); 11554 11555 // The jump table header will be inserted in our current block, do the 11556 // range check, and fall through to our fallthrough block. 11557 JTH->HeaderBB = CurMBB; 11558 JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader. 11559 11560 // If we're in the right place, emit the jump table header right now. 11561 if (CurMBB == SwitchMBB) { 11562 visitJumpTableHeader(*JT, *JTH, SwitchMBB); 11563 JTH->Emitted = true; 11564 } 11565 break; 11566 } 11567 case CC_BitTests: { 11568 // FIXME: Optimize away range check based on pivot comparisons. 11569 BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex]; 11570 11571 // The bit test blocks haven't been inserted yet; insert them here. 11572 for (BitTestCase &BTC : BTB->Cases) 11573 CurMF->insert(BBI, BTC.ThisBB); 11574 11575 // Fill in fields of the BitTestBlock. 11576 BTB->Parent = CurMBB; 11577 BTB->Default = Fallthrough; 11578 11579 BTB->DefaultProb = UnhandledProbs; 11580 // If the cases in bit test don't form a contiguous range, we evenly 11581 // distribute the probability on the edge to Fallthrough to two 11582 // successors of CurMBB. 11583 if (!BTB->ContiguousRange) { 11584 BTB->Prob += DefaultProb / 2; 11585 BTB->DefaultProb -= DefaultProb / 2; 11586 } 11587 11588 if (FallthroughUnreachable) 11589 BTB->FallthroughUnreachable = true; 11590 11591 // If we're in the right place, emit the bit test header right now. 11592 if (CurMBB == SwitchMBB) { 11593 visitBitTestHeader(*BTB, SwitchMBB); 11594 BTB->Emitted = true; 11595 } 11596 break; 11597 } 11598 case CC_Range: { 11599 const Value *RHS, *LHS, *MHS; 11600 ISD::CondCode CC; 11601 if (I->Low == I->High) { 11602 // Check Cond == I->Low. 11603 CC = ISD::SETEQ; 11604 LHS = Cond; 11605 RHS=I->Low; 11606 MHS = nullptr; 11607 } else { 11608 // Check I->Low <= Cond <= I->High. 11609 CC = ISD::SETLE; 11610 LHS = I->Low; 11611 MHS = Cond; 11612 RHS = I->High; 11613 } 11614 11615 // If Fallthrough is unreachable, fold away the comparison. 11616 if (FallthroughUnreachable) 11617 CC = ISD::SETTRUE; 11618 11619 // The false probability is the sum of all unhandled cases. 11620 CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB, 11621 getCurSDLoc(), I->Prob, UnhandledProbs); 11622 11623 if (CurMBB == SwitchMBB) 11624 visitSwitchCase(CB, SwitchMBB); 11625 else 11626 SL->SwitchCases.push_back(CB); 11627 11628 break; 11629 } 11630 } 11631 CurMBB = Fallthrough; 11632 } 11633 } 11634 11635 void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList, 11636 const SwitchWorkListItem &W, 11637 Value *Cond, 11638 MachineBasicBlock *SwitchMBB) { 11639 assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) && 11640 "Clusters not sorted?"); 11641 assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!"); 11642 11643 auto [LastLeft, FirstRight, LeftProb, RightProb] = 11644 SL->computeSplitWorkItemInfo(W); 11645 11646 // Use the first element on the right as pivot since we will make less-than 11647 // comparisons against it. 11648 CaseClusterIt PivotCluster = FirstRight; 11649 assert(PivotCluster > W.FirstCluster); 11650 assert(PivotCluster <= W.LastCluster); 11651 11652 CaseClusterIt FirstLeft = W.FirstCluster; 11653 CaseClusterIt LastRight = W.LastCluster; 11654 11655 const ConstantInt *Pivot = PivotCluster->Low; 11656 11657 // New blocks will be inserted immediately after the current one. 11658 MachineFunction::iterator BBI(W.MBB); 11659 ++BBI; 11660 11661 // We will branch to the LHS if Value < Pivot. If LHS is a single cluster, 11662 // we can branch to its destination directly if it's squeezed exactly in 11663 // between the known lower bound and Pivot - 1. 11664 MachineBasicBlock *LeftMBB; 11665 if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range && 11666 FirstLeft->Low == W.GE && 11667 (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) { 11668 LeftMBB = FirstLeft->MBB; 11669 } else { 11670 LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock()); 11671 FuncInfo.MF->insert(BBI, LeftMBB); 11672 WorkList.push_back( 11673 {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2}); 11674 // Put Cond in a virtual register to make it available from the new blocks. 11675 ExportFromCurrentBlock(Cond); 11676 } 11677 11678 // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a 11679 // single cluster, RHS.Low == Pivot, and we can branch to its destination 11680 // directly if RHS.High equals the current upper bound. 11681 MachineBasicBlock *RightMBB; 11682 if (FirstRight == LastRight && FirstRight->Kind == CC_Range && 11683 W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) { 11684 RightMBB = FirstRight->MBB; 11685 } else { 11686 RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock()); 11687 FuncInfo.MF->insert(BBI, RightMBB); 11688 WorkList.push_back( 11689 {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2}); 11690 // Put Cond in a virtual register to make it available from the new blocks. 11691 ExportFromCurrentBlock(Cond); 11692 } 11693 11694 // Create the CaseBlock record that will be used to lower the branch. 11695 CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB, 11696 getCurSDLoc(), LeftProb, RightProb); 11697 11698 if (W.MBB == SwitchMBB) 11699 visitSwitchCase(CB, SwitchMBB); 11700 else 11701 SL->SwitchCases.push_back(CB); 11702 } 11703 11704 // Scale CaseProb after peeling a case with the probablity of PeeledCaseProb 11705 // from the swith statement. 11706 static BranchProbability scaleCaseProbality(BranchProbability CaseProb, 11707 BranchProbability PeeledCaseProb) { 11708 if (PeeledCaseProb == BranchProbability::getOne()) 11709 return BranchProbability::getZero(); 11710 BranchProbability SwitchProb = PeeledCaseProb.getCompl(); 11711 11712 uint32_t Numerator = CaseProb.getNumerator(); 11713 uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator()); 11714 return BranchProbability(Numerator, std::max(Numerator, Denominator)); 11715 } 11716 11717 // Try to peel the top probability case if it exceeds the threshold. 11718 // Return current MachineBasicBlock for the switch statement if the peeling 11719 // does not occur. 11720 // If the peeling is performed, return the newly created MachineBasicBlock 11721 // for the peeled switch statement. Also update Clusters to remove the peeled 11722 // case. PeeledCaseProb is the BranchProbability for the peeled case. 11723 MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster( 11724 const SwitchInst &SI, CaseClusterVector &Clusters, 11725 BranchProbability &PeeledCaseProb) { 11726 MachineBasicBlock *SwitchMBB = FuncInfo.MBB; 11727 // Don't perform if there is only one cluster or optimizing for size. 11728 if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 || 11729 TM.getOptLevel() == CodeGenOptLevel::None || 11730 SwitchMBB->getParent()->getFunction().hasMinSize()) 11731 return SwitchMBB; 11732 11733 BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100); 11734 unsigned PeeledCaseIndex = 0; 11735 bool SwitchPeeled = false; 11736 for (unsigned Index = 0; Index < Clusters.size(); ++Index) { 11737 CaseCluster &CC = Clusters[Index]; 11738 if (CC.Prob < TopCaseProb) 11739 continue; 11740 TopCaseProb = CC.Prob; 11741 PeeledCaseIndex = Index; 11742 SwitchPeeled = true; 11743 } 11744 if (!SwitchPeeled) 11745 return SwitchMBB; 11746 11747 LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: " 11748 << TopCaseProb << "\n"); 11749 11750 // Record the MBB for the peeled switch statement. 11751 MachineFunction::iterator BBI(SwitchMBB); 11752 ++BBI; 11753 MachineBasicBlock *PeeledSwitchMBB = 11754 FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock()); 11755 FuncInfo.MF->insert(BBI, PeeledSwitchMBB); 11756 11757 ExportFromCurrentBlock(SI.getCondition()); 11758 auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex; 11759 SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt, 11760 nullptr, nullptr, TopCaseProb.getCompl()}; 11761 lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB); 11762 11763 Clusters.erase(PeeledCaseIt); 11764 for (CaseCluster &CC : Clusters) { 11765 LLVM_DEBUG( 11766 dbgs() << "Scale the probablity for one cluster, before scaling: " 11767 << CC.Prob << "\n"); 11768 CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb); 11769 LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n"); 11770 } 11771 PeeledCaseProb = TopCaseProb; 11772 return PeeledSwitchMBB; 11773 } 11774 11775 void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) { 11776 // Extract cases from the switch. 11777 BranchProbabilityInfo *BPI = FuncInfo.BPI; 11778 CaseClusterVector Clusters; 11779 Clusters.reserve(SI.getNumCases()); 11780 for (auto I : SI.cases()) { 11781 MachineBasicBlock *Succ = FuncInfo.MBBMap[I.getCaseSuccessor()]; 11782 const ConstantInt *CaseVal = I.getCaseValue(); 11783 BranchProbability Prob = 11784 BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex()) 11785 : BranchProbability(1, SI.getNumCases() + 1); 11786 Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob)); 11787 } 11788 11789 MachineBasicBlock *DefaultMBB = FuncInfo.MBBMap[SI.getDefaultDest()]; 11790 11791 // Cluster adjacent cases with the same destination. We do this at all 11792 // optimization levels because it's cheap to do and will make codegen faster 11793 // if there are many clusters. 11794 sortAndRangeify(Clusters); 11795 11796 // The branch probablity of the peeled case. 11797 BranchProbability PeeledCaseProb = BranchProbability::getZero(); 11798 MachineBasicBlock *PeeledSwitchMBB = 11799 peelDominantCaseCluster(SI, Clusters, PeeledCaseProb); 11800 11801 // If there is only the default destination, jump there directly. 11802 MachineBasicBlock *SwitchMBB = FuncInfo.MBB; 11803 if (Clusters.empty()) { 11804 assert(PeeledSwitchMBB == SwitchMBB); 11805 SwitchMBB->addSuccessor(DefaultMBB); 11806 if (DefaultMBB != NextBlock(SwitchMBB)) { 11807 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, 11808 getControlRoot(), DAG.getBasicBlock(DefaultMBB))); 11809 } 11810 return; 11811 } 11812 11813 SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(), 11814 DAG.getBFI()); 11815 SL->findBitTestClusters(Clusters, &SI); 11816 11817 LLVM_DEBUG({ 11818 dbgs() << "Case clusters: "; 11819 for (const CaseCluster &C : Clusters) { 11820 if (C.Kind == CC_JumpTable) 11821 dbgs() << "JT:"; 11822 if (C.Kind == CC_BitTests) 11823 dbgs() << "BT:"; 11824 11825 C.Low->getValue().print(dbgs(), true); 11826 if (C.Low != C.High) { 11827 dbgs() << '-'; 11828 C.High->getValue().print(dbgs(), true); 11829 } 11830 dbgs() << ' '; 11831 } 11832 dbgs() << '\n'; 11833 }); 11834 11835 assert(!Clusters.empty()); 11836 SwitchWorkList WorkList; 11837 CaseClusterIt First = Clusters.begin(); 11838 CaseClusterIt Last = Clusters.end() - 1; 11839 auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB); 11840 // Scale the branchprobability for DefaultMBB if the peel occurs and 11841 // DefaultMBB is not replaced. 11842 if (PeeledCaseProb != BranchProbability::getZero() && 11843 DefaultMBB == FuncInfo.MBBMap[SI.getDefaultDest()]) 11844 DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb); 11845 WorkList.push_back( 11846 {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb}); 11847 11848 while (!WorkList.empty()) { 11849 SwitchWorkListItem W = WorkList.pop_back_val(); 11850 unsigned NumClusters = W.LastCluster - W.FirstCluster + 1; 11851 11852 if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None && 11853 !DefaultMBB->getParent()->getFunction().hasMinSize()) { 11854 // For optimized builds, lower large range as a balanced binary tree. 11855 splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB); 11856 continue; 11857 } 11858 11859 lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB); 11860 } 11861 } 11862 11863 void SelectionDAGBuilder::visitStepVector(const CallInst &I) { 11864 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11865 auto DL = getCurSDLoc(); 11866 EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 11867 setValue(&I, DAG.getStepVector(DL, ResultVT)); 11868 } 11869 11870 void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) { 11871 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11872 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 11873 11874 SDLoc DL = getCurSDLoc(); 11875 SDValue V = getValue(I.getOperand(0)); 11876 assert(VT == V.getValueType() && "Malformed vector.reverse!"); 11877 11878 if (VT.isScalableVector()) { 11879 setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V)); 11880 return; 11881 } 11882 11883 // Use VECTOR_SHUFFLE for the fixed-length vector 11884 // to maintain existing behavior. 11885 SmallVector<int, 8> Mask; 11886 unsigned NumElts = VT.getVectorMinNumElements(); 11887 for (unsigned i = 0; i != NumElts; ++i) 11888 Mask.push_back(NumElts - 1 - i); 11889 11890 setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask)); 11891 } 11892 11893 void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I) { 11894 auto DL = getCurSDLoc(); 11895 SDValue InVec = getValue(I.getOperand(0)); 11896 EVT OutVT = 11897 InVec.getValueType().getHalfNumVectorElementsVT(*DAG.getContext()); 11898 11899 unsigned OutNumElts = OutVT.getVectorMinNumElements(); 11900 11901 // ISD Node needs the input vectors split into two equal parts 11902 SDValue Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec, 11903 DAG.getVectorIdxConstant(0, DL)); 11904 SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec, 11905 DAG.getVectorIdxConstant(OutNumElts, DL)); 11906 11907 // Use VECTOR_SHUFFLE for fixed-length vectors to benefit from existing 11908 // legalisation and combines. 11909 if (OutVT.isFixedLengthVector()) { 11910 SDValue Even = DAG.getVectorShuffle(OutVT, DL, Lo, Hi, 11911 createStrideMask(0, 2, OutNumElts)); 11912 SDValue Odd = DAG.getVectorShuffle(OutVT, DL, Lo, Hi, 11913 createStrideMask(1, 2, OutNumElts)); 11914 SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc()); 11915 setValue(&I, Res); 11916 return; 11917 } 11918 11919 SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL, 11920 DAG.getVTList(OutVT, OutVT), Lo, Hi); 11921 setValue(&I, Res); 11922 } 11923 11924 void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I) { 11925 auto DL = getCurSDLoc(); 11926 EVT InVT = getValue(I.getOperand(0)).getValueType(); 11927 SDValue InVec0 = getValue(I.getOperand(0)); 11928 SDValue InVec1 = getValue(I.getOperand(1)); 11929 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11930 EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 11931 11932 // Use VECTOR_SHUFFLE for fixed-length vectors to benefit from existing 11933 // legalisation and combines. 11934 if (OutVT.isFixedLengthVector()) { 11935 unsigned NumElts = InVT.getVectorMinNumElements(); 11936 SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVec0, InVec1); 11937 setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT), 11938 createInterleaveMask(NumElts, 2))); 11939 return; 11940 } 11941 11942 SDValue Res = DAG.getNode(ISD::VECTOR_INTERLEAVE, DL, 11943 DAG.getVTList(InVT, InVT), InVec0, InVec1); 11944 Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Res.getValue(0), 11945 Res.getValue(1)); 11946 setValue(&I, Res); 11947 } 11948 11949 void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) { 11950 SmallVector<EVT, 4> ValueVTs; 11951 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(), 11952 ValueVTs); 11953 unsigned NumValues = ValueVTs.size(); 11954 if (NumValues == 0) return; 11955 11956 SmallVector<SDValue, 4> Values(NumValues); 11957 SDValue Op = getValue(I.getOperand(0)); 11958 11959 for (unsigned i = 0; i != NumValues; ++i) 11960 Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i], 11961 SDValue(Op.getNode(), Op.getResNo() + i)); 11962 11963 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 11964 DAG.getVTList(ValueVTs), Values)); 11965 } 11966 11967 void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) { 11968 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 11969 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType()); 11970 11971 SDLoc DL = getCurSDLoc(); 11972 SDValue V1 = getValue(I.getOperand(0)); 11973 SDValue V2 = getValue(I.getOperand(1)); 11974 int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue(); 11975 11976 // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node. 11977 if (VT.isScalableVector()) { 11978 MVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout()); 11979 setValue(&I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2, 11980 DAG.getConstant(Imm, DL, IdxVT))); 11981 return; 11982 } 11983 11984 unsigned NumElts = VT.getVectorNumElements(); 11985 11986 uint64_t Idx = (NumElts + Imm) % NumElts; 11987 11988 // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors. 11989 SmallVector<int, 8> Mask; 11990 for (unsigned i = 0; i < NumElts; ++i) 11991 Mask.push_back(Idx + i); 11992 setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask)); 11993 } 11994 11995 // Consider the following MIR after SelectionDAG, which produces output in 11996 // phyregs in the first case or virtregs in the second case. 11997 // 11998 // INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx 11999 // %5:gr32 = COPY $ebx 12000 // %6:gr32 = COPY $edx 12001 // %1:gr32 = COPY %6:gr32 12002 // %0:gr32 = COPY %5:gr32 12003 // 12004 // INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32 12005 // %1:gr32 = COPY %6:gr32 12006 // %0:gr32 = COPY %5:gr32 12007 // 12008 // Given %0, we'd like to return $ebx in the first case and %5 in the second. 12009 // Given %1, we'd like to return $edx in the first case and %6 in the second. 12010 // 12011 // If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap 12012 // to a single virtreg (such as %0). The remaining outputs monotonically 12013 // increase in virtreg number from there. If a callbr has no outputs, then it 12014 // should not have a corresponding callbr landingpad; in fact, the callbr 12015 // landingpad would not even be able to refer to such a callbr. 12016 static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg) { 12017 MachineInstr *MI = MRI.def_begin(Reg)->getParent(); 12018 // There is definitely at least one copy. 12019 assert(MI->getOpcode() == TargetOpcode::COPY && 12020 "start of copy chain MUST be COPY"); 12021 Reg = MI->getOperand(1).getReg(); 12022 MI = MRI.def_begin(Reg)->getParent(); 12023 // There may be an optional second copy. 12024 if (MI->getOpcode() == TargetOpcode::COPY) { 12025 assert(Reg.isVirtual() && "expected COPY of virtual register"); 12026 Reg = MI->getOperand(1).getReg(); 12027 assert(Reg.isPhysical() && "expected COPY of physical register"); 12028 MI = MRI.def_begin(Reg)->getParent(); 12029 } 12030 // The start of the chain must be an INLINEASM_BR. 12031 assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR && 12032 "end of copy chain MUST be INLINEASM_BR"); 12033 return Reg; 12034 } 12035 12036 // We must do this walk rather than the simpler 12037 // setValue(&I, getCopyFromRegs(CBR, CBR->getType())); 12038 // otherwise we will end up with copies of virtregs only valid along direct 12039 // edges. 12040 void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) { 12041 SmallVector<EVT, 8> ResultVTs; 12042 SmallVector<SDValue, 8> ResultValues; 12043 const auto *CBR = 12044 cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator()); 12045 12046 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 12047 const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo(); 12048 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 12049 12050 unsigned InitialDef = FuncInfo.ValueMap[CBR]; 12051 SDValue Chain = DAG.getRoot(); 12052 12053 // Re-parse the asm constraints string. 12054 TargetLowering::AsmOperandInfoVector TargetConstraints = 12055 TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR); 12056 for (auto &T : TargetConstraints) { 12057 SDISelAsmOperandInfo OpInfo(T); 12058 if (OpInfo.Type != InlineAsm::isOutput) 12059 continue; 12060 12061 // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the 12062 // individual constraint. 12063 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG); 12064 12065 switch (OpInfo.ConstraintType) { 12066 case TargetLowering::C_Register: 12067 case TargetLowering::C_RegisterClass: { 12068 // Fill in OpInfo.AssignedRegs.Regs. 12069 getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo); 12070 12071 // getRegistersForValue may produce 1 to many registers based on whether 12072 // the OpInfo.ConstraintVT is legal on the target or not. 12073 for (size_t i = 0, e = OpInfo.AssignedRegs.Regs.size(); i != e; ++i) { 12074 Register OriginalDef = FollowCopyChain(MRI, InitialDef++); 12075 if (Register::isPhysicalRegister(OriginalDef)) 12076 FuncInfo.MBB->addLiveIn(OriginalDef); 12077 // Update the assigned registers to use the original defs. 12078 OpInfo.AssignedRegs.Regs[i] = OriginalDef; 12079 } 12080 12081 SDValue V = OpInfo.AssignedRegs.getCopyFromRegs( 12082 DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR); 12083 ResultValues.push_back(V); 12084 ResultVTs.push_back(OpInfo.ConstraintVT); 12085 break; 12086 } 12087 case TargetLowering::C_Other: { 12088 SDValue Flag; 12089 SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(), 12090 OpInfo, DAG); 12091 ++InitialDef; 12092 ResultValues.push_back(V); 12093 ResultVTs.push_back(OpInfo.ConstraintVT); 12094 break; 12095 } 12096 default: 12097 break; 12098 } 12099 } 12100 SDValue V = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(), 12101 DAG.getVTList(ResultVTs), ResultValues); 12102 setValue(&I, V); 12103 } 12104