FunctionLoweringInfo.cpp 22 KB

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