1 //===-- FunctionLoweringInfo.cpp ------------------------------------------===// 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 functions from LLVM IR into 10 // Machine IR. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/FunctionLoweringInfo.h" 15 #include "llvm/CodeGen/Analysis.h" 16 #include "llvm/CodeGen/MachineFrameInfo.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineInstrBuilder.h" 19 #include "llvm/CodeGen/MachineRegisterInfo.h" 20 #include "llvm/CodeGen/TargetFrameLowering.h" 21 #include "llvm/CodeGen/TargetInstrInfo.h" 22 #include "llvm/CodeGen/TargetLowering.h" 23 #include "llvm/CodeGen/TargetRegisterInfo.h" 24 #include "llvm/CodeGen/TargetSubtargetInfo.h" 25 #include "llvm/CodeGen/WasmEHFuncInfo.h" 26 #include "llvm/CodeGen/WinEHFuncInfo.h" 27 #include "llvm/IR/DataLayout.h" 28 #include "llvm/IR/DerivedTypes.h" 29 #include "llvm/IR/Function.h" 30 #include "llvm/IR/Instructions.h" 31 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/MathExtras.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include "llvm/Target/TargetOptions.h" 39 #include <algorithm> 40 using namespace llvm; 41 42 #define DEBUG_TYPE "function-lowering-info" 43 44 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by 45 /// PHI nodes or outside of the basic block that defines it, or used by a 46 /// switch or atomic instruction, which may expand to multiple basic blocks. 47 static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { 48 if (I->use_empty()) return false; 49 if (isa<PHINode>(I)) return true; 50 const BasicBlock *BB = I->getParent(); 51 for (const User *U : I->users()) 52 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) 53 return true; 54 55 return false; 56 } 57 58 static ISD::NodeType getPreferredExtendForValue(const Value *V) { 59 // For the users of the source value being used for compare instruction, if 60 // the number of signed predicate is greater than unsigned predicate, we 61 // prefer to use SIGN_EXTEND. 62 // 63 // With this optimization, we would be able to reduce some redundant sign or 64 // zero extension instruction, and eventually more machine CSE opportunities 65 // can be exposed. 66 ISD::NodeType ExtendKind = ISD::ANY_EXTEND; 67 unsigned NumOfSigned = 0, NumOfUnsigned = 0; 68 for (const User *U : V->users()) { 69 if (const auto *CI = dyn_cast<CmpInst>(U)) { 70 NumOfSigned += CI->isSigned(); 71 NumOfUnsigned += CI->isUnsigned(); 72 } 73 } 74 if (NumOfSigned > NumOfUnsigned) 75 ExtendKind = ISD::SIGN_EXTEND; 76 77 return ExtendKind; 78 } 79 80 void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, 81 SelectionDAG *DAG) { 82 Fn = &fn; 83 MF = &mf; 84 TLI = MF->getSubtarget().getTargetLowering(); 85 RegInfo = &MF->getRegInfo(); 86 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); 87 unsigned StackAlign = TFI->getStackAlignment(); 88 89 // Check whether the function can return without sret-demotion. 90 SmallVector<ISD::OutputArg, 4> Outs; 91 CallingConv::ID CC = Fn->getCallingConv(); 92 93 GetReturnInfo(CC, Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI, 94 mf.getDataLayout()); 95 CanLowerReturn = 96 TLI->CanLowerReturn(CC, *MF, Fn->isVarArg(), Outs, Fn->getContext()); 97 98 // If this personality uses funclets, we need to do a bit more work. 99 DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects; 100 EHPersonality Personality = classifyEHPersonality( 101 Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr); 102 if (isFuncletEHPersonality(Personality)) { 103 // Calculate state numbers if we haven't already. 104 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo(); 105 if (Personality == EHPersonality::MSVC_CXX) 106 calculateWinCXXEHStateNumbers(&fn, EHInfo); 107 else if (isAsynchronousEHPersonality(Personality)) 108 calculateSEHStateNumbers(&fn, EHInfo); 109 else if (Personality == EHPersonality::CoreCLR) 110 calculateClrEHStateNumbers(&fn, EHInfo); 111 112 // Map all BB references in the WinEH data to MBBs. 113 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { 114 for (WinEHHandlerType &H : TBME.HandlerArray) { 115 if (const AllocaInst *AI = H.CatchObj.Alloca) 116 CatchObjects.insert({AI, {}}).first->second.push_back( 117 &H.CatchObj.FrameIndex); 118 else 119 H.CatchObj.FrameIndex = INT_MAX; 120 } 121 } 122 } 123 if (Personality == EHPersonality::Wasm_CXX) { 124 WasmEHFuncInfo &EHInfo = *MF->getWasmEHFuncInfo(); 125 calculateWasmEHInfo(&fn, EHInfo); 126 } 127 128 // Initialize the mapping of values to registers. This is only set up for 129 // instruction values that are used outside of the block that defines 130 // them. 131 for (const BasicBlock &BB : *Fn) { 132 for (const Instruction &I : BB) { 133 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 134 Type *Ty = AI->getAllocatedType(); 135 unsigned Align = 136 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty), 137 AI->getAlignment()); 138 139 // Static allocas can be folded into the initial stack frame 140 // adjustment. For targets that don't realign the stack, don't 141 // do this if there is an extra alignment requirement. 142 if (AI->isStaticAlloca() && 143 (TFI->isStackRealignable() || (Align <= StackAlign))) { 144 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize()); 145 uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty); 146 147 TySize *= CUI->getZExtValue(); // Get total allocated size. 148 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. 149 int FrameIndex = INT_MAX; 150 auto Iter = CatchObjects.find(AI); 151 if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) { 152 FrameIndex = MF->getFrameInfo().CreateFixedObject( 153 TySize, 0, /*Immutable=*/false, /*isAliased=*/true); 154 MF->getFrameInfo().setObjectAlignment(FrameIndex, Align); 155 } else { 156 FrameIndex = 157 MF->getFrameInfo().CreateStackObject(TySize, Align, false, AI); 158 } 159 160 StaticAllocaMap[AI] = FrameIndex; 161 // Update the catch handler information. 162 if (Iter != CatchObjects.end()) { 163 for (int *CatchObjPtr : Iter->second) 164 *CatchObjPtr = FrameIndex; 165 } 166 } else { 167 // FIXME: Overaligned static allocas should be grouped into 168 // a single dynamic allocation instead of using a separate 169 // stack allocation for each one. 170 if (Align <= StackAlign) 171 Align = 0; 172 // Inform the Frame Information that we have variable-sized objects. 173 MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, AI); 174 } 175 } 176 177 // Look for inline asm that clobbers the SP register. 178 if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 179 ImmutableCallSite CS(&I); 180 if (isa<InlineAsm>(CS.getCalledValue())) { 181 unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); 182 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 183 std::vector<TargetLowering::AsmOperandInfo> Ops = 184 TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS); 185 for (TargetLowering::AsmOperandInfo &Op : Ops) { 186 if (Op.Type == InlineAsm::isClobber) { 187 // Clobbers don't have SDValue operands, hence SDValue(). 188 TLI->ComputeConstraintToUse(Op, SDValue(), DAG); 189 std::pair<unsigned, const TargetRegisterClass *> PhysReg = 190 TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode, 191 Op.ConstraintVT); 192 if (PhysReg.first == SP) 193 MF->getFrameInfo().setHasOpaqueSPAdjustment(true); 194 } 195 } 196 } 197 } 198 199 // Look for calls to the @llvm.va_start intrinsic. We can omit some 200 // prologue boilerplate for variadic functions that don't examine their 201 // arguments. 202 if (const auto *II = dyn_cast<IntrinsicInst>(&I)) { 203 if (II->getIntrinsicID() == Intrinsic::vastart) 204 MF->getFrameInfo().setHasVAStart(true); 205 } 206 207 // If we have a musttail call in a variadic function, we need to ensure we 208 // forward implicit register parameters. 209 if (const auto *CI = dyn_cast<CallInst>(&I)) { 210 if (CI->isMustTailCall() && Fn->isVarArg()) 211 MF->getFrameInfo().setHasMustTailInVarArgFunc(true); 212 } 213 214 // Mark values used outside their block as exported, by allocating 215 // a virtual register for them. 216 if (isUsedOutsideOfDefiningBlock(&I)) 217 if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I))) 218 InitializeRegForValue(&I); 219 220 // Decide the preferred extend type for a value. 221 PreferredExtendType[&I] = getPreferredExtendForValue(&I); 222 } 223 } 224 225 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This 226 // also creates the initial PHI MachineInstrs, though none of the input 227 // operands are populated. 228 for (const BasicBlock &BB : *Fn) { 229 // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks 230 // are really data, and no instructions can live here. 231 if (BB.isEHPad()) { 232 const Instruction *PadInst = BB.getFirstNonPHI(); 233 // If this is a non-landingpad EH pad, mark this function as using 234 // funclets. 235 // FIXME: SEH catchpads do not create EH scope/funclets, so we could avoid 236 // setting this in such cases in order to improve frame layout. 237 if (!isa<LandingPadInst>(PadInst)) { 238 MF->setHasEHScopes(true); 239 MF->setHasEHFunclets(true); 240 MF->getFrameInfo().setHasOpaqueSPAdjustment(true); 241 } 242 if (isa<CatchSwitchInst>(PadInst)) { 243 assert(&*BB.begin() == PadInst && 244 "WinEHPrepare failed to remove PHIs from imaginary BBs"); 245 continue; 246 } 247 if (isa<FuncletPadInst>(PadInst)) 248 assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs"); 249 } 250 251 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB); 252 MBBMap[&BB] = MBB; 253 MF->push_back(MBB); 254 255 // Transfer the address-taken flag. This is necessary because there could 256 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only 257 // the first one should be marked. 258 if (BB.hasAddressTaken()) 259 MBB->setHasAddressTaken(); 260 261 // Mark landing pad blocks. 262 if (BB.isEHPad()) 263 MBB->setIsEHPad(); 264 265 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as 266 // appropriate. 267 for (const PHINode &PN : BB.phis()) { 268 if (PN.use_empty()) 269 continue; 270 271 // Skip empty types 272 if (PN.getType()->isEmptyTy()) 273 continue; 274 275 DebugLoc DL = PN.getDebugLoc(); 276 unsigned PHIReg = ValueMap[&PN]; 277 assert(PHIReg && "PHI node does not have an assigned virtual register!"); 278 279 SmallVector<EVT, 4> ValueVTs; 280 ComputeValueVTs(*TLI, MF->getDataLayout(), PN.getType(), ValueVTs); 281 for (EVT VT : ValueVTs) { 282 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); 283 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 284 for (unsigned i = 0; i != NumRegisters; ++i) 285 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); 286 PHIReg += NumRegisters; 287 } 288 } 289 } 290 291 if (isFuncletEHPersonality(Personality)) { 292 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo(); 293 294 // Map all BB references in the WinEH data to MBBs. 295 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { 296 for (WinEHHandlerType &H : TBME.HandlerArray) { 297 if (H.Handler) 298 H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()]; 299 } 300 } 301 for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap) 302 if (UME.Cleanup) 303 UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()]; 304 for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) { 305 const auto *BB = UME.Handler.get<const BasicBlock *>(); 306 UME.Handler = MBBMap[BB]; 307 } 308 for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) { 309 const auto *BB = CME.Handler.get<const BasicBlock *>(); 310 CME.Handler = MBBMap[BB]; 311 } 312 } 313 314 else if (Personality == EHPersonality::Wasm_CXX) { 315 WasmEHFuncInfo &EHInfo = *MF->getWasmEHFuncInfo(); 316 // Map all BB references in the WinEH data to MBBs. 317 DenseMap<BBOrMBB, BBOrMBB> NewMap; 318 for (auto &KV : EHInfo.EHPadUnwindMap) { 319 const auto *Src = KV.first.get<const BasicBlock *>(); 320 const auto *Dst = KV.second.get<const BasicBlock *>(); 321 NewMap[MBBMap[Src]] = MBBMap[Dst]; 322 } 323 EHInfo.EHPadUnwindMap = std::move(NewMap); 324 NewMap.clear(); 325 for (auto &KV : EHInfo.ThrowUnwindMap) { 326 const auto *Src = KV.first.get<const BasicBlock *>(); 327 const auto *Dst = KV.second.get<const BasicBlock *>(); 328 NewMap[MBBMap[Src]] = MBBMap[Dst]; 329 } 330 EHInfo.ThrowUnwindMap = std::move(NewMap); 331 } 332 } 333 334 /// clear - Clear out all the function-specific state. This returns this 335 /// FunctionLoweringInfo to an empty state, ready to be used for a 336 /// different function. 337 void FunctionLoweringInfo::clear() { 338 MBBMap.clear(); 339 ValueMap.clear(); 340 VirtReg2Value.clear(); 341 StaticAllocaMap.clear(); 342 LiveOutRegInfo.clear(); 343 VisitedBBs.clear(); 344 ArgDbgValues.clear(); 345 ByValArgFrameIndexMap.clear(); 346 RegFixups.clear(); 347 RegsWithFixups.clear(); 348 StatepointStackSlots.clear(); 349 StatepointSpillMaps.clear(); 350 PreferredExtendType.clear(); 351 } 352 353 /// CreateReg - Allocate a single virtual register for the given type. 354 unsigned FunctionLoweringInfo::CreateReg(MVT VT) { 355 return RegInfo->createVirtualRegister( 356 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT)); 357 } 358 359 /// CreateRegs - Allocate the appropriate number of virtual registers of 360 /// the correctly promoted or expanded types. Assign these registers 361 /// consecutive vreg numbers and return the first assigned number. 362 /// 363 /// In the case that the given value has struct or array type, this function 364 /// will assign registers for each member or element. 365 /// 366 unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { 367 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); 368 369 SmallVector<EVT, 4> ValueVTs; 370 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); 371 372 unsigned FirstReg = 0; 373 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { 374 EVT ValueVT = ValueVTs[Value]; 375 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); 376 377 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); 378 for (unsigned i = 0; i != NumRegs; ++i) { 379 unsigned R = CreateReg(RegisterVT); 380 if (!FirstReg) FirstReg = R; 381 } 382 } 383 return FirstReg; 384 } 385 386 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the 387 /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If 388 /// the register's LiveOutInfo is for a smaller bit width, it is extended to 389 /// the larger bit width by zero extension. The bit width must be no smaller 390 /// than the LiveOutInfo's existing bit width. 391 const FunctionLoweringInfo::LiveOutInfo * 392 FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { 393 if (!LiveOutRegInfo.inBounds(Reg)) 394 return nullptr; 395 396 LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; 397 if (!LOI->IsValid) 398 return nullptr; 399 400 if (BitWidth > LOI->Known.getBitWidth()) { 401 LOI->NumSignBits = 1; 402 LOI->Known = LOI->Known.zextOrTrunc(BitWidth); 403 } 404 405 return LOI; 406 } 407 408 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination 409 /// register based on the LiveOutInfo of its operands. 410 void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { 411 Type *Ty = PN->getType(); 412 if (!Ty->isIntegerTy() || Ty->isVectorTy()) 413 return; 414 415 SmallVector<EVT, 1> ValueVTs; 416 ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); 417 assert(ValueVTs.size() == 1 && 418 "PHIs with non-vector integer types should have a single VT."); 419 EVT IntVT = ValueVTs[0]; 420 421 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) 422 return; 423 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); 424 unsigned BitWidth = IntVT.getSizeInBits(); 425 426 unsigned DestReg = ValueMap[PN]; 427 if (!TargetRegisterInfo::isVirtualRegister(DestReg)) 428 return; 429 LiveOutRegInfo.grow(DestReg); 430 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; 431 432 Value *V = PN->getIncomingValue(0); 433 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { 434 DestLOI.NumSignBits = 1; 435 DestLOI.Known = KnownBits(BitWidth); 436 return; 437 } 438 439 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 440 APInt Val = CI->getValue().zextOrTrunc(BitWidth); 441 DestLOI.NumSignBits = Val.getNumSignBits(); 442 DestLOI.Known.Zero = ~Val; 443 DestLOI.Known.One = Val; 444 } else { 445 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" 446 "CopyToReg node was created."); 447 unsigned SrcReg = ValueMap[V]; 448 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { 449 DestLOI.IsValid = false; 450 return; 451 } 452 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); 453 if (!SrcLOI) { 454 DestLOI.IsValid = false; 455 return; 456 } 457 DestLOI = *SrcLOI; 458 } 459 460 assert(DestLOI.Known.Zero.getBitWidth() == BitWidth && 461 DestLOI.Known.One.getBitWidth() == BitWidth && 462 "Masks should have the same bit width as the type."); 463 464 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { 465 Value *V = PN->getIncomingValue(i); 466 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { 467 DestLOI.NumSignBits = 1; 468 DestLOI.Known = KnownBits(BitWidth); 469 return; 470 } 471 472 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 473 APInt Val = CI->getValue().zextOrTrunc(BitWidth); 474 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); 475 DestLOI.Known.Zero &= ~Val; 476 DestLOI.Known.One &= Val; 477 continue; 478 } 479 480 assert(ValueMap.count(V) && "V should have been placed in ValueMap when " 481 "its CopyToReg node was created."); 482 unsigned SrcReg = ValueMap[V]; 483 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { 484 DestLOI.IsValid = false; 485 return; 486 } 487 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); 488 if (!SrcLOI) { 489 DestLOI.IsValid = false; 490 return; 491 } 492 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); 493 DestLOI.Known.Zero &= SrcLOI->Known.Zero; 494 DestLOI.Known.One &= SrcLOI->Known.One; 495 } 496 } 497 498 /// setArgumentFrameIndex - Record frame index for the byval 499 /// argument. This overrides previous frame index entry for this argument, 500 /// if any. 501 void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, 502 int FI) { 503 ByValArgFrameIndexMap[A] = FI; 504 } 505 506 /// getArgumentFrameIndex - Get frame index for the byval argument. 507 /// If the argument does not have any assigned frame index then 0 is 508 /// returned. 509 int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { 510 auto I = ByValArgFrameIndexMap.find(A); 511 if (I != ByValArgFrameIndexMap.end()) 512 return I->second; 513 LLVM_DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); 514 return INT_MAX; 515 } 516 517 unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg( 518 const Value *CPI, const TargetRegisterClass *RC) { 519 MachineRegisterInfo &MRI = MF->getRegInfo(); 520 auto I = CatchPadExceptionPointers.insert({CPI, 0}); 521 unsigned &VReg = I.first->second; 522 if (I.second) 523 VReg = MRI.createVirtualRegister(RC); 524 assert(VReg && "null vreg in exception pointer table!"); 525 return VReg; 526 } 527 528 unsigned 529 FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB, 530 const Value *Val) { 531 auto Key = std::make_pair(MBB, Val); 532 auto It = SwiftErrorVRegDefMap.find(Key); 533 // If this is the first use of this swifterror value in this basic block, 534 // create a new virtual register. 535 // After we processed all basic blocks we will satisfy this "upwards exposed 536 // use" by inserting a copy or phi at the beginning of this block. 537 if (It == SwiftErrorVRegDefMap.end()) { 538 auto &DL = MF->getDataLayout(); 539 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL)); 540 auto VReg = MF->getRegInfo().createVirtualRegister(RC); 541 SwiftErrorVRegDefMap[Key] = VReg; 542 SwiftErrorVRegUpwardsUse[Key] = VReg; 543 return VReg; 544 } else return It->second; 545 } 546 547 void FunctionLoweringInfo::setCurrentSwiftErrorVReg( 548 const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) { 549 SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg; 550 } 551 552 std::pair<unsigned, bool> 553 FunctionLoweringInfo::getOrCreateSwiftErrorVRegDefAt(const Instruction *I) { 554 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, true); 555 auto It = SwiftErrorVRegDefUses.find(Key); 556 if (It == SwiftErrorVRegDefUses.end()) { 557 auto &DL = MF->getDataLayout(); 558 const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL)); 559 unsigned VReg = MF->getRegInfo().createVirtualRegister(RC); 560 SwiftErrorVRegDefUses[Key] = VReg; 561 return std::make_pair(VReg, true); 562 } 563 return std::make_pair(It->second, false); 564 } 565 566 std::pair<unsigned, bool> 567 FunctionLoweringInfo::getOrCreateSwiftErrorVRegUseAt(const Instruction *I, const MachineBasicBlock *MBB, const Value *Val) { 568 auto Key = PointerIntPair<const Instruction *, 1, bool>(I, false); 569 auto It = SwiftErrorVRegDefUses.find(Key); 570 if (It == SwiftErrorVRegDefUses.end()) { 571 unsigned VReg = getOrCreateSwiftErrorVReg(MBB, Val); 572 SwiftErrorVRegDefUses[Key] = VReg; 573 return std::make_pair(VReg, true); 574 } 575 return std::make_pair(It->second, false); 576 } 577 578 const Value * 579 FunctionLoweringInfo::getValueFromVirtualReg(unsigned Vreg) { 580 if (VirtReg2Value.empty()) { 581 SmallVector<EVT, 4> ValueVTs; 582 for (auto &P : ValueMap) { 583 ValueVTs.clear(); 584 ComputeValueVTs(*TLI, Fn->getParent()->getDataLayout(), 585 P.first->getType(), ValueVTs); 586 unsigned Reg = P.second; 587 for (EVT VT : ValueVTs) { 588 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); 589 for (unsigned i = 0, e = NumRegisters; i != e; ++i) 590 VirtReg2Value[Reg++] = P.first; 591 } 592 } 593 } 594 return VirtReg2Value.lookup(Vreg); 595 } 596