FunctionLoweringInfo.cpp 20 KB

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