FunctionLoweringInfo.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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/ADT/PostOrderIterator.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/MachineModuleInfo.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/WinEHFuncInfo.h"
  23. #include "llvm/IR/DataLayout.h"
  24. #include "llvm/IR/DebugInfo.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/Function.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/IntrinsicInst.h"
  29. #include "llvm/IR/LLVMContext.h"
  30. #include "llvm/IR/Module.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include "llvm/Support/MathExtras.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include "llvm/Target/TargetFrameLowering.h"
  36. #include "llvm/Target/TargetInstrInfo.h"
  37. #include "llvm/Target/TargetLowering.h"
  38. #include "llvm/Target/TargetOptions.h"
  39. #include "llvm/Target/TargetRegisterInfo.h"
  40. #include "llvm/Target/TargetSubtargetInfo.h"
  41. #include <algorithm>
  42. using namespace llvm;
  43. #define DEBUG_TYPE "function-lowering-info"
  44. /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
  45. /// PHI nodes or outside of the basic block that defines it, or used by a
  46. /// switch or atomic instruction, which may expand to multiple basic blocks.
  47. static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
  48. if (I->use_empty()) return false;
  49. if (isa<PHINode>(I)) return true;
  50. const BasicBlock *BB = I->getParent();
  51. for (const User *U : I->users())
  52. if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
  53. return true;
  54. return false;
  55. }
  56. static ISD::NodeType getPreferredExtendForValue(const Value *V) {
  57. // For the users of the source value being used for compare instruction, if
  58. // the number of signed predicate is greater than unsigned predicate, we
  59. // prefer to use SIGN_EXTEND.
  60. //
  61. // With this optimization, we would be able to reduce some redundant sign or
  62. // zero extension instruction, and eventually more machine CSE opportunities
  63. // can be exposed.
  64. ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
  65. unsigned NumOfSigned = 0, NumOfUnsigned = 0;
  66. for (const User *U : V->users()) {
  67. if (const auto *CI = dyn_cast<CmpInst>(U)) {
  68. NumOfSigned += CI->isSigned();
  69. NumOfUnsigned += CI->isUnsigned();
  70. }
  71. }
  72. if (NumOfSigned > NumOfUnsigned)
  73. ExtendKind = ISD::SIGN_EXTEND;
  74. return ExtendKind;
  75. }
  76. void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
  77. SelectionDAG *DAG) {
  78. Fn = &fn;
  79. MF = &mf;
  80. TLI = MF->getSubtarget().getTargetLowering();
  81. RegInfo = &MF->getRegInfo();
  82. MachineModuleInfo &MMI = MF->getMMI();
  83. // Check whether the function can return without sret-demotion.
  84. SmallVector<ISD::OutputArg, 4> Outs;
  85. GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
  86. mf.getDataLayout());
  87. CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
  88. Fn->isVarArg(), Outs, Fn->getContext());
  89. // Initialize the mapping of values to registers. This is only set up for
  90. // instruction values that are used outside of the block that defines
  91. // them.
  92. Function::const_iterator BB = Fn->begin(), EB = Fn->end();
  93. for (; BB != EB; ++BB)
  94. for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
  95. I != E; ++I) {
  96. if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
  97. // Static allocas can be folded into the initial stack frame adjustment.
  98. if (AI->isStaticAlloca()) {
  99. const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
  100. Type *Ty = AI->getAllocatedType();
  101. uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty);
  102. unsigned Align =
  103. std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
  104. AI->getAlignment());
  105. TySize *= CUI->getZExtValue(); // Get total allocated size.
  106. if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
  107. StaticAllocaMap[AI] =
  108. MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
  109. } else {
  110. unsigned Align =
  111. std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(
  112. AI->getAllocatedType()),
  113. AI->getAlignment());
  114. unsigned StackAlign =
  115. MF->getSubtarget().getFrameLowering()->getStackAlignment();
  116. if (Align <= StackAlign)
  117. Align = 0;
  118. // Inform the Frame Information that we have variable-sized objects.
  119. MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
  120. }
  121. }
  122. // Look for inline asm that clobbers the SP register.
  123. if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
  124. ImmutableCallSite CS(I);
  125. if (isa<InlineAsm>(CS.getCalledValue())) {
  126. unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
  127. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  128. std::vector<TargetLowering::AsmOperandInfo> Ops =
  129. TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS);
  130. for (size_t I = 0, E = Ops.size(); I != E; ++I) {
  131. TargetLowering::AsmOperandInfo &Op = Ops[I];
  132. if (Op.Type == InlineAsm::isClobber) {
  133. // Clobbers don't have SDValue operands, hence SDValue().
  134. TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
  135. std::pair<unsigned, const TargetRegisterClass *> PhysReg =
  136. TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode,
  137. Op.ConstraintVT);
  138. if (PhysReg.first == SP)
  139. MF->getFrameInfo()->setHasOpaqueSPAdjustment(true);
  140. }
  141. }
  142. }
  143. }
  144. // Look for calls to the @llvm.va_start intrinsic. We can omit some
  145. // prologue boilerplate for variadic functions that don't examine their
  146. // arguments.
  147. if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
  148. if (II->getIntrinsicID() == Intrinsic::vastart)
  149. MF->getFrameInfo()->setHasVAStart(true);
  150. }
  151. // If we have a musttail call in a variadic funciton, we need to ensure we
  152. // forward implicit register parameters.
  153. if (const auto *CI = dyn_cast<CallInst>(I)) {
  154. if (CI->isMustTailCall() && Fn->isVarArg())
  155. MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
  156. }
  157. // Mark values used outside their block as exported, by allocating
  158. // a virtual register for them.
  159. if (isUsedOutsideOfDefiningBlock(I))
  160. if (!isa<AllocaInst>(I) ||
  161. !StaticAllocaMap.count(cast<AllocaInst>(I)))
  162. InitializeRegForValue(I);
  163. // Collect llvm.dbg.declare information. This is done now instead of
  164. // during the initial isel pass through the IR so that it is done
  165. // in a predictable order.
  166. if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
  167. assert(DI->getVariable() && "Missing variable");
  168. assert(DI->getDebugLoc() && "Missing location");
  169. if (MMI.hasDebugInfo()) {
  170. // Don't handle byval struct arguments or VLAs, for example.
  171. // Non-byval arguments are handled here (they refer to the stack
  172. // temporary alloca at this point).
  173. const Value *Address = DI->getAddress();
  174. if (Address) {
  175. if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
  176. Address = BCI->getOperand(0);
  177. if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
  178. DenseMap<const AllocaInst *, int>::iterator SI =
  179. StaticAllocaMap.find(AI);
  180. if (SI != StaticAllocaMap.end()) { // Check for VLAs.
  181. int FI = SI->second;
  182. MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(),
  183. FI, DI->getDebugLoc());
  184. }
  185. }
  186. }
  187. }
  188. }
  189. // Decide the preferred extend type for a value.
  190. PreferredExtendType[I] = getPreferredExtendForValue(I);
  191. }
  192. // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
  193. // also creates the initial PHI MachineInstrs, though none of the input
  194. // operands are populated.
  195. for (BB = Fn->begin(); BB != EB; ++BB) {
  196. MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
  197. MBBMap[BB] = MBB;
  198. MF->push_back(MBB);
  199. // Transfer the address-taken flag. This is necessary because there could
  200. // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
  201. // the first one should be marked.
  202. if (BB->hasAddressTaken())
  203. MBB->setHasAddressTaken();
  204. // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
  205. // appropriate.
  206. for (BasicBlock::const_iterator I = BB->begin();
  207. const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  208. if (PN->use_empty()) continue;
  209. // Skip empty types
  210. if (PN->getType()->isEmptyTy())
  211. continue;
  212. DebugLoc DL = PN->getDebugLoc();
  213. unsigned PHIReg = ValueMap[PN];
  214. assert(PHIReg && "PHI node does not have an assigned virtual register!");
  215. SmallVector<EVT, 4> ValueVTs;
  216. ComputeValueVTs(*TLI, MF->getDataLayout(), PN->getType(), ValueVTs);
  217. for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
  218. EVT VT = ValueVTs[vti];
  219. unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
  220. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  221. for (unsigned i = 0; i != NumRegisters; ++i)
  222. BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
  223. PHIReg += NumRegisters;
  224. }
  225. }
  226. }
  227. // Mark landing pad blocks.
  228. SmallVector<const LandingPadInst *, 4> LPads;
  229. for (BB = Fn->begin(); BB != EB; ++BB) {
  230. if (BB->isEHPad())
  231. MBBMap[BB]->setIsEHPad();
  232. const Instruction *FNP = BB->getFirstNonPHI();
  233. if (const auto *LPI = dyn_cast<LandingPadInst>(FNP))
  234. LPads.push_back(LPI);
  235. }
  236. // If this is an MSVC EH personality, we need to do a bit more work.
  237. if (!Fn->hasPersonalityFn())
  238. return;
  239. EHPersonality Personality = classifyEHPersonality(Fn->getPersonalityFn());
  240. if (!isMSVCEHPersonality(Personality))
  241. return;
  242. if (Personality == EHPersonality::MSVC_Win64SEH ||
  243. Personality == EHPersonality::MSVC_X86SEH) {
  244. addSEHHandlersForLPads(LPads);
  245. }
  246. WinEHFuncInfo &EHInfo = MMI.getWinEHFuncInfo(&fn);
  247. if (Personality == EHPersonality::MSVC_CXX) {
  248. // Calculate state numbers and then map from funclet BBs to MBBs.
  249. const Function *WinEHParentFn = MMI.getWinEHParent(&fn);
  250. calculateWinCXXEHStateNumbers(WinEHParentFn, EHInfo);
  251. for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap)
  252. for (WinEHHandlerType &H : TBME.HandlerArray)
  253. if (const auto *BB = dyn_cast<BasicBlock>(H.Handler))
  254. H.HandlerMBB = MBBMap[BB];
  255. }
  256. // Copy the state numbers to LandingPadInfo for the current function, which
  257. // could be a handler or the parent. This should happen for 32-bit SEH and
  258. // C++ EH.
  259. if (Personality == EHPersonality::MSVC_CXX ||
  260. Personality == EHPersonality::MSVC_X86SEH) {
  261. for (const LandingPadInst *LP : LPads) {
  262. MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
  263. MMI.addWinEHState(LPadMBB, EHInfo.EHPadStateMap[LP]);
  264. }
  265. }
  266. }
  267. void FunctionLoweringInfo::addSEHHandlersForLPads(
  268. ArrayRef<const LandingPadInst *> LPads) {
  269. MachineModuleInfo &MMI = MF->getMMI();
  270. // Iterate over all landing pads with llvm.eh.actions calls.
  271. for (const LandingPadInst *LP : LPads) {
  272. const IntrinsicInst *ActionsCall =
  273. dyn_cast<IntrinsicInst>(LP->getNextNode());
  274. if (!ActionsCall ||
  275. ActionsCall->getIntrinsicID() != Intrinsic::eh_actions)
  276. continue;
  277. // Parse the llvm.eh.actions call we found.
  278. MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
  279. SmallVector<std::unique_ptr<ActionHandler>, 4> Actions;
  280. parseEHActions(ActionsCall, Actions);
  281. // Iterate EH actions from most to least precedence, which means
  282. // iterating in reverse.
  283. for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) {
  284. ActionHandler *Action = I->get();
  285. if (auto *CH = dyn_cast<CatchHandler>(Action)) {
  286. const auto *Filter =
  287. dyn_cast<Function>(CH->getSelector()->stripPointerCasts());
  288. assert((Filter || CH->getSelector()->isNullValue()) &&
  289. "expected function or catch-all");
  290. const auto *RecoverBA =
  291. cast<BlockAddress>(CH->getHandlerBlockOrFunc());
  292. MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA);
  293. } else {
  294. assert(isa<CleanupHandler>(Action));
  295. const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc());
  296. MMI.addSEHCleanupHandler(LPadMBB, Fini);
  297. }
  298. }
  299. }
  300. }
  301. /// clear - Clear out all the function-specific state. This returns this
  302. /// FunctionLoweringInfo to an empty state, ready to be used for a
  303. /// different function.
  304. void FunctionLoweringInfo::clear() {
  305. assert(CatchInfoFound.size() == CatchInfoLost.size() &&
  306. "Not all catch info was assigned to a landing pad!");
  307. MBBMap.clear();
  308. ValueMap.clear();
  309. StaticAllocaMap.clear();
  310. #ifndef NDEBUG
  311. CatchInfoLost.clear();
  312. CatchInfoFound.clear();
  313. #endif
  314. LiveOutRegInfo.clear();
  315. VisitedBBs.clear();
  316. ArgDbgValues.clear();
  317. ByValArgFrameIndexMap.clear();
  318. RegFixups.clear();
  319. StatepointStackSlots.clear();
  320. StatepointRelocatedValues.clear();
  321. PreferredExtendType.clear();
  322. }
  323. /// CreateReg - Allocate a single virtual register for the given type.
  324. unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
  325. return RegInfo->createVirtualRegister(
  326. MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
  327. }
  328. /// CreateRegs - Allocate the appropriate number of virtual registers of
  329. /// the correctly promoted or expanded types. Assign these registers
  330. /// consecutive vreg numbers and return the first assigned number.
  331. ///
  332. /// In the case that the given value has struct or array type, this function
  333. /// will assign registers for each member or element.
  334. ///
  335. unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
  336. const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
  337. SmallVector<EVT, 4> ValueVTs;
  338. ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
  339. unsigned FirstReg = 0;
  340. for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
  341. EVT ValueVT = ValueVTs[Value];
  342. MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
  343. unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
  344. for (unsigned i = 0; i != NumRegs; ++i) {
  345. unsigned R = CreateReg(RegisterVT);
  346. if (!FirstReg) FirstReg = R;
  347. }
  348. }
  349. return FirstReg;
  350. }
  351. /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
  352. /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
  353. /// the register's LiveOutInfo is for a smaller bit width, it is extended to
  354. /// the larger bit width by zero extension. The bit width must be no smaller
  355. /// than the LiveOutInfo's existing bit width.
  356. const FunctionLoweringInfo::LiveOutInfo *
  357. FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
  358. if (!LiveOutRegInfo.inBounds(Reg))
  359. return nullptr;
  360. LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
  361. if (!LOI->IsValid)
  362. return nullptr;
  363. if (BitWidth > LOI->KnownZero.getBitWidth()) {
  364. LOI->NumSignBits = 1;
  365. LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
  366. LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
  367. }
  368. return LOI;
  369. }
  370. /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
  371. /// register based on the LiveOutInfo of its operands.
  372. void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
  373. Type *Ty = PN->getType();
  374. if (!Ty->isIntegerTy() || Ty->isVectorTy())
  375. return;
  376. SmallVector<EVT, 1> ValueVTs;
  377. ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs);
  378. assert(ValueVTs.size() == 1 &&
  379. "PHIs with non-vector integer types should have a single VT.");
  380. EVT IntVT = ValueVTs[0];
  381. if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
  382. return;
  383. IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
  384. unsigned BitWidth = IntVT.getSizeInBits();
  385. unsigned DestReg = ValueMap[PN];
  386. if (!TargetRegisterInfo::isVirtualRegister(DestReg))
  387. return;
  388. LiveOutRegInfo.grow(DestReg);
  389. LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
  390. Value *V = PN->getIncomingValue(0);
  391. if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
  392. DestLOI.NumSignBits = 1;
  393. APInt Zero(BitWidth, 0);
  394. DestLOI.KnownZero = Zero;
  395. DestLOI.KnownOne = Zero;
  396. return;
  397. }
  398. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  399. APInt Val = CI->getValue().zextOrTrunc(BitWidth);
  400. DestLOI.NumSignBits = Val.getNumSignBits();
  401. DestLOI.KnownZero = ~Val;
  402. DestLOI.KnownOne = Val;
  403. } else {
  404. assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
  405. "CopyToReg node was created.");
  406. unsigned SrcReg = ValueMap[V];
  407. if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
  408. DestLOI.IsValid = false;
  409. return;
  410. }
  411. const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
  412. if (!SrcLOI) {
  413. DestLOI.IsValid = false;
  414. return;
  415. }
  416. DestLOI = *SrcLOI;
  417. }
  418. assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
  419. DestLOI.KnownOne.getBitWidth() == BitWidth &&
  420. "Masks should have the same bit width as the type.");
  421. for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
  422. Value *V = PN->getIncomingValue(i);
  423. if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
  424. DestLOI.NumSignBits = 1;
  425. APInt Zero(BitWidth, 0);
  426. DestLOI.KnownZero = Zero;
  427. DestLOI.KnownOne = Zero;
  428. return;
  429. }
  430. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  431. APInt Val = CI->getValue().zextOrTrunc(BitWidth);
  432. DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
  433. DestLOI.KnownZero &= ~Val;
  434. DestLOI.KnownOne &= Val;
  435. continue;
  436. }
  437. assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
  438. "its CopyToReg node was created.");
  439. unsigned SrcReg = ValueMap[V];
  440. if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
  441. DestLOI.IsValid = false;
  442. return;
  443. }
  444. const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
  445. if (!SrcLOI) {
  446. DestLOI.IsValid = false;
  447. return;
  448. }
  449. DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
  450. DestLOI.KnownZero &= SrcLOI->KnownZero;
  451. DestLOI.KnownOne &= SrcLOI->KnownOne;
  452. }
  453. }
  454. /// setArgumentFrameIndex - Record frame index for the byval
  455. /// argument. This overrides previous frame index entry for this argument,
  456. /// if any.
  457. void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
  458. int FI) {
  459. ByValArgFrameIndexMap[A] = FI;
  460. }
  461. /// getArgumentFrameIndex - Get frame index for the byval argument.
  462. /// If the argument does not have any assigned frame index then 0 is
  463. /// returned.
  464. int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
  465. DenseMap<const Argument *, int>::iterator I =
  466. ByValArgFrameIndexMap.find(A);
  467. if (I != ByValArgFrameIndexMap.end())
  468. return I->second;
  469. DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
  470. return 0;
  471. }
  472. /// ComputeUsesVAFloatArgument - Determine if any floating-point values are
  473. /// being passed to this variadic function, and set the MachineModuleInfo's
  474. /// usesVAFloatArgument flag if so. This flag is used to emit an undefined
  475. /// reference to _fltused on Windows, which will link in MSVCRT's
  476. /// floating-point support.
  477. void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
  478. MachineModuleInfo *MMI)
  479. {
  480. FunctionType *FT = cast<FunctionType>(
  481. I.getCalledValue()->getType()->getContainedType(0));
  482. if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
  483. for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
  484. Type* T = I.getArgOperand(i)->getType();
  485. for (auto i : post_order(T)) {
  486. if (i->isFloatingPointTy()) {
  487. MMI->setUsesVAFloatArgument(true);
  488. return;
  489. }
  490. }
  491. }
  492. }
  493. }
  494. /// AddLandingPadInfo - Extract the exception handling information from the
  495. /// landingpad instruction and add them to the specified machine module info.
  496. void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
  497. MachineBasicBlock *MBB) {
  498. if (const auto *PF = dyn_cast<Function>(
  499. I.getParent()->getParent()->getPersonalityFn()->stripPointerCasts()))
  500. MMI.addPersonality(PF);
  501. if (I.isCleanup())
  502. MMI.addCleanup(MBB);
  503. // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
  504. // but we need to do it this way because of how the DWARF EH emitter
  505. // processes the clauses.
  506. for (unsigned i = I.getNumClauses(); i != 0; --i) {
  507. Value *Val = I.getClause(i - 1);
  508. if (I.isCatch(i - 1)) {
  509. MMI.addCatchTypeInfo(MBB,
  510. dyn_cast<GlobalValue>(Val->stripPointerCasts()));
  511. } else {
  512. // Add filters in a list.
  513. Constant *CVal = cast<Constant>(Val);
  514. SmallVector<const GlobalValue*, 4> FilterList;
  515. for (User::op_iterator
  516. II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
  517. FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
  518. MMI.addFilterTypeInfo(MBB, FilterList);
  519. }
  520. }
  521. }