FunctionLoweringInfo.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/MachineModuleInfo.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/WinEHFuncInfo.h"
  22. #include "llvm/IR/DataLayout.h"
  23. #include "llvm/IR/DebugInfo.h"
  24. #include "llvm/IR/DerivedTypes.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/IntrinsicInst.h"
  28. #include "llvm/IR/LLVMContext.h"
  29. #include "llvm/IR/Module.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/MathExtras.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include "llvm/Target/TargetFrameLowering.h"
  35. #include "llvm/Target/TargetInstrInfo.h"
  36. #include "llvm/Target/TargetLowering.h"
  37. #include "llvm/Target/TargetOptions.h"
  38. #include "llvm/Target/TargetRegisterInfo.h"
  39. #include "llvm/Target/TargetSubtargetInfo.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. MachineModuleInfo &MMI = MF->getMMI();
  82. const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
  83. unsigned StackAlign = TFI->getStackAlignment();
  84. // Check whether the function can return without sret-demotion.
  85. SmallVector<ISD::OutputArg, 4> Outs;
  86. GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
  87. mf.getDataLayout());
  88. CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
  89. 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. // Initialize the mapping of values to registers. This is only set up for
  115. // instruction values that are used outside of the block that defines
  116. // them.
  117. Function::const_iterator BB = Fn->begin(), EB = Fn->end();
  118. for (; BB != EB; ++BB)
  119. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
  120. I != E; ++I) {
  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 (size_t I = 0, E = Ops.size(); I != E; ++I) {
  170. TargetLowering::AsmOperandInfo &Op = Ops[I];
  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. // Collect llvm.dbg.declare information. This is done now instead of
  202. // during the initial isel pass through the IR so that it is done
  203. // in a predictable order.
  204. if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
  205. assert(DI->getVariable() && "Missing variable");
  206. assert(DI->getDebugLoc() && "Missing location");
  207. if (MMI.hasDebugInfo()) {
  208. // Don't handle byval struct arguments or VLAs, for example.
  209. // Non-byval arguments are handled here (they refer to the stack
  210. // temporary alloca at this point).
  211. const Value *Address = DI->getAddress();
  212. if (Address) {
  213. if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
  214. Address = BCI->getOperand(0);
  215. if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
  216. DenseMap<const AllocaInst *, int>::iterator SI =
  217. StaticAllocaMap.find(AI);
  218. if (SI != StaticAllocaMap.end()) { // Check for VLAs.
  219. int FI = SI->second;
  220. MF->setVariableDbgInfo(DI->getVariable(), DI->getExpression(),
  221. FI, DI->getDebugLoc());
  222. }
  223. }
  224. }
  225. }
  226. }
  227. // Decide the preferred extend type for a value.
  228. PreferredExtendType[&*I] = getPreferredExtendForValue(&*I);
  229. }
  230. // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
  231. // also creates the initial PHI MachineInstrs, though none of the input
  232. // operands are populated.
  233. for (BB = Fn->begin(); BB != EB; ++BB) {
  234. // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
  235. // are really data, and no instructions can live here.
  236. if (BB->isEHPad()) {
  237. const Instruction *I = BB->getFirstNonPHI();
  238. // If this is a non-landingpad EH pad, mark this function as using
  239. // funclets.
  240. // FIXME: SEH catchpads do not create funclets, so we could avoid setting
  241. // this in such cases in order to improve frame layout.
  242. if (!isa<LandingPadInst>(I)) {
  243. MMI.setHasEHFunclets(true);
  244. MF->getFrameInfo().setHasOpaqueSPAdjustment(true);
  245. }
  246. if (isa<CatchSwitchInst>(I)) {
  247. assert(&*BB->begin() == I &&
  248. "WinEHPrepare failed to remove PHIs from imaginary BBs");
  249. continue;
  250. }
  251. if (isa<FuncletPadInst>(I))
  252. assert(&*BB->begin() == I && "WinEHPrepare failed to demote PHIs");
  253. }
  254. MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&*BB);
  255. MBBMap[&*BB] = MBB;
  256. MF->push_back(MBB);
  257. // Transfer the address-taken flag. This is necessary because there could
  258. // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
  259. // the first one should be marked.
  260. if (BB->hasAddressTaken())
  261. MBB->setHasAddressTaken();
  262. // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
  263. // appropriate.
  264. for (BasicBlock::const_iterator I = BB->begin();
  265. const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  266. if (PN->use_empty()) continue;
  267. // Skip empty types
  268. if (PN->getType()->isEmptyTy())
  269. continue;
  270. DebugLoc DL = PN->getDebugLoc();
  271. unsigned PHIReg = ValueMap[PN];
  272. assert(PHIReg && "PHI node does not have an assigned virtual register!");
  273. SmallVector<EVT, 4> ValueVTs;
  274. ComputeValueVTs(*TLI, MF->getDataLayout(), PN->getType(), ValueVTs);
  275. for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
  276. EVT VT = ValueVTs[vti];
  277. unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
  278. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  279. for (unsigned i = 0; i != NumRegisters; ++i)
  280. BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
  281. PHIReg += NumRegisters;
  282. }
  283. }
  284. }
  285. // Mark landing pad blocks.
  286. SmallVector<const LandingPadInst *, 4> LPads;
  287. for (BB = Fn->begin(); BB != EB; ++BB) {
  288. const Instruction *FNP = BB->getFirstNonPHI();
  289. if (BB->isEHPad() && MBBMap.count(&*BB))
  290. MBBMap[&*BB]->setIsEHPad();
  291. if (const auto *LPI = dyn_cast<LandingPadInst>(FNP))
  292. LPads.push_back(LPI);
  293. }
  294. if (!isFuncletEHPersonality(Personality))
  295. return;
  296. WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
  297. // Map all BB references in the WinEH data to MBBs.
  298. for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
  299. for (WinEHHandlerType &H : TBME.HandlerArray) {
  300. if (H.Handler)
  301. H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()];
  302. }
  303. }
  304. for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap)
  305. if (UME.Cleanup)
  306. UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()];
  307. for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) {
  308. const BasicBlock *BB = UME.Handler.get<const BasicBlock *>();
  309. UME.Handler = MBBMap[BB];
  310. }
  311. for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
  312. const BasicBlock *BB = CME.Handler.get<const BasicBlock *>();
  313. CME.Handler = MBBMap[BB];
  314. }
  315. }
  316. /// clear - Clear out all the function-specific state. This returns this
  317. /// FunctionLoweringInfo to an empty state, ready to be used for a
  318. /// different function.
  319. void FunctionLoweringInfo::clear() {
  320. MBBMap.clear();
  321. ValueMap.clear();
  322. StaticAllocaMap.clear();
  323. LiveOutRegInfo.clear();
  324. VisitedBBs.clear();
  325. ArgDbgValues.clear();
  326. ByValArgFrameIndexMap.clear();
  327. RegFixups.clear();
  328. StatepointStackSlots.clear();
  329. StatepointSpillMaps.clear();
  330. PreferredExtendType.clear();
  331. }
  332. /// CreateReg - Allocate a single virtual register for the given type.
  333. unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
  334. return RegInfo->createVirtualRegister(
  335. MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
  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. unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
  345. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  346. SmallVector<EVT, 4> ValueVTs;
  347. ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
  348. unsigned FirstReg = 0;
  349. for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
  350. EVT ValueVT = ValueVTs[Value];
  351. MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
  352. unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
  353. for (unsigned i = 0; i != NumRegs; ++i) {
  354. unsigned R = CreateReg(RegisterVT);
  355. if (!FirstReg) FirstReg = R;
  356. }
  357. }
  358. return FirstReg;
  359. }
  360. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  361. /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
  362. /// the register's LiveOutInfo is for a smaller bit width, it is extended to
  363. /// the larger bit width by zero extension. The bit width must be no smaller
  364. /// than the LiveOutInfo's existing bit width.
  365. const FunctionLoweringInfo::LiveOutInfo *
  366. FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
  367. if (!LiveOutRegInfo.inBounds(Reg))
  368. return nullptr;
  369. LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
  370. if (!LOI->IsValid)
  371. return nullptr;
  372. if (BitWidth > LOI->KnownZero.getBitWidth()) {
  373. LOI->NumSignBits = 1;
  374. LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
  375. LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
  376. }
  377. return LOI;
  378. }
  379. /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
  380. /// register based on the LiveOutInfo of its operands.
  381. void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
  382. Type *Ty = PN->getType();
  383. if (!Ty->isIntegerTy() || Ty->isVectorTy())
  384. return;
  385. SmallVector<EVT, 1> ValueVTs;
  386. ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
  387. assert(ValueVTs.size() == 1 &&
  388. "PHIs with non-vector integer types should have a single VT.");
  389. EVT IntVT = ValueVTs[0];
  390. if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
  391. return;
  392. IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
  393. unsigned BitWidth = IntVT.getSizeInBits();
  394. unsigned DestReg = ValueMap[PN];
  395. if (!TargetRegisterInfo::isVirtualRegister(DestReg))
  396. return;
  397. LiveOutRegInfo.grow(DestReg);
  398. LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
  399. Value *V = PN->getIncomingValue(0);
  400. if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
  401. DestLOI.NumSignBits = 1;
  402. APInt Zero(BitWidth, 0);
  403. DestLOI.KnownZero = Zero;
  404. DestLOI.KnownOne = Zero;
  405. return;
  406. }
  407. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  408. APInt Val = CI->getValue().zextOrTrunc(BitWidth);
  409. DestLOI.NumSignBits = Val.getNumSignBits();
  410. DestLOI.KnownZero = ~Val;
  411. DestLOI.KnownOne = Val;
  412. } else {
  413. assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
  414. "CopyToReg node was created.");
  415. unsigned SrcReg = ValueMap[V];
  416. if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
  417. DestLOI.IsValid = false;
  418. return;
  419. }
  420. const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
  421. if (!SrcLOI) {
  422. DestLOI.IsValid = false;
  423. return;
  424. }
  425. DestLOI = *SrcLOI;
  426. }
  427. assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
  428. DestLOI.KnownOne.getBitWidth() == BitWidth &&
  429. "Masks should have the same bit width as the type.");
  430. for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
  431. Value *V = PN->getIncomingValue(i);
  432. if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
  433. DestLOI.NumSignBits = 1;
  434. APInt Zero(BitWidth, 0);
  435. DestLOI.KnownZero = Zero;
  436. DestLOI.KnownOne = Zero;
  437. return;
  438. }
  439. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  440. APInt Val = CI->getValue().zextOrTrunc(BitWidth);
  441. DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
  442. DestLOI.KnownZero &= ~Val;
  443. DestLOI.KnownOne &= Val;
  444. continue;
  445. }
  446. assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
  447. "its CopyToReg node was created.");
  448. unsigned SrcReg = ValueMap[V];
  449. if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
  450. DestLOI.IsValid = false;
  451. return;
  452. }
  453. const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
  454. if (!SrcLOI) {
  455. DestLOI.IsValid = false;
  456. return;
  457. }
  458. DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
  459. DestLOI.KnownZero &= SrcLOI->KnownZero;
  460. DestLOI.KnownOne &= SrcLOI->KnownOne;
  461. }
  462. }
  463. /// setArgumentFrameIndex - Record frame index for the byval
  464. /// argument. This overrides previous frame index entry for this argument,
  465. /// if any.
  466. void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
  467. int FI) {
  468. ByValArgFrameIndexMap[A] = FI;
  469. }
  470. /// getArgumentFrameIndex - Get frame index for the byval argument.
  471. /// If the argument does not have any assigned frame index then 0 is
  472. /// returned.
  473. int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
  474. DenseMap<const Argument *, int>::iterator I =
  475. ByValArgFrameIndexMap.find(A);
  476. if (I != ByValArgFrameIndexMap.end())
  477. return I->second;
  478. DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
  479. return 0;
  480. }
  481. unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg(
  482. const Value *CPI, const TargetRegisterClass *RC) {
  483. MachineRegisterInfo &MRI = MF->getRegInfo();
  484. auto I = CatchPadExceptionPointers.insert({CPI, 0});
  485. unsigned &VReg = I.first->second;
  486. if (I.second)
  487. VReg = MRI.createVirtualRegister(RC);
  488. assert(VReg && "null vreg in exception pointer table!");
  489. return VReg;
  490. }
  491. unsigned
  492. FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB,
  493. const Value *Val) {
  494. auto Key = std::make_pair(MBB, Val);
  495. auto It = SwiftErrorVRegDefMap.find(Key);
  496. // If this is the first use of this swifterror value in this basic block,
  497. // create a new virtual register.
  498. // After we processed all basic blocks we will satisfy this "upwards exposed
  499. // use" by inserting a copy or phi at the beginning of this block.
  500. if (It == SwiftErrorVRegDefMap.end()) {
  501. auto &DL = MF->getDataLayout();
  502. const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL));
  503. auto VReg = MF->getRegInfo().createVirtualRegister(RC);
  504. SwiftErrorVRegDefMap[Key] = VReg;
  505. SwiftErrorVRegUpwardsUse[Key] = VReg;
  506. return VReg;
  507. } else return It->second;
  508. }
  509. void FunctionLoweringInfo::setCurrentSwiftErrorVReg(
  510. const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) {
  511. SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg;
  512. }