FunctionLoweringInfo.cpp 22 KB

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