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