FunctionLoweringInfo.cpp 22 KB

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