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