1 //===-- FunctionLoweringInfo.cpp ------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements routines for translating functions from LLVM IR into
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/FunctionLoweringInfo.h"
16 #include "llvm/ADT/PostOrderIterator.h"
17 #include "llvm/CodeGen/Analysis.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/WinEHFuncInfo.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/DebugInfo.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetFrameLowering.h"
37 #include "llvm/Target/TargetInstrInfo.h"
38 #include "llvm/Target/TargetLowering.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/Target/TargetRegisterInfo.h"
41 #include "llvm/Target/TargetSubtargetInfo.h"
45 #define DEBUG_TYPE "function-lowering-info"
47 /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
48 /// PHI nodes or outside of the basic block that defines it, or used by a
49 /// switch or atomic instruction, which may expand to multiple basic blocks.
50 static bool isUsedOutsideOfDefiningBlock(const Instruction *I) {
51 if (I->use_empty()) return false;
52 if (isa<PHINode>(I)) return true;
53 const BasicBlock *BB = I->getParent();
54 for (const User *U : I->users())
55 if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U))
61 static ISD::NodeType getPreferredExtendForValue(const Value *V) {
62 // For the users of the source value being used for compare instruction, if
63 // the number of signed predicate is greater than unsigned predicate, we
64 // prefer to use SIGN_EXTEND.
66 // With this optimization, we would be able to reduce some redundant sign or
67 // zero extension instruction, and eventually more machine CSE opportunities
69 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
70 unsigned NumOfSigned = 0, NumOfUnsigned = 0;
71 for (const User *U : V->users()) {
72 if (const auto *CI = dyn_cast<CmpInst>(U)) {
73 NumOfSigned += CI->isSigned();
74 NumOfUnsigned += CI->isUnsigned();
77 if (NumOfSigned > NumOfUnsigned)
78 ExtendKind = ISD::SIGN_EXTEND;
84 struct WinEHNumbering {
85 WinEHNumbering(WinEHFuncInfo &FuncInfo) : FuncInfo(FuncInfo),
86 CurrentBaseState(-1), NextState(0) {}
88 WinEHFuncInfo &FuncInfo;
92 SmallVector<std::unique_ptr<ActionHandler>, 4> HandlerStack;
93 SmallPtrSet<const Function *, 4> VisitedHandlers;
95 int currentEHNumber() const {
96 return HandlerStack.empty() ? CurrentBaseState : HandlerStack.back()->getEHState();
99 void createUnwindMapEntry(int ToState, ActionHandler *AH);
100 void createTryBlockMapEntry(int TryLow, int TryHigh,
101 ArrayRef<CatchHandler *> Handlers);
102 void processCallSite(MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
103 ImmutableCallSite CS);
104 void popUnmatchedActions(int FirstMismatch);
105 void calculateStateNumbers(const Function &F);
106 void findActionRootLPads(const Function &F);
110 void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
114 TLI = MF->getSubtarget().getTargetLowering();
115 RegInfo = &MF->getRegInfo();
116 MachineModuleInfo &MMI = MF->getMMI();
118 // Check whether the function can return without sret-demotion.
119 SmallVector<ISD::OutputArg, 4> Outs;
120 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI);
121 CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF,
122 Fn->isVarArg(), Outs, Fn->getContext());
124 // Initialize the mapping of values to registers. This is only set up for
125 // instruction values that are used outside of the block that defines
127 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
128 for (; BB != EB; ++BB)
129 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
131 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
132 // Static allocas can be folded into the initial stack frame adjustment.
133 if (AI->isStaticAlloca()) {
134 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
135 Type *Ty = AI->getAllocatedType();
136 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
138 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
141 TySize *= CUI->getZExtValue(); // Get total allocated size.
142 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
144 StaticAllocaMap[AI] =
145 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
148 unsigned Align = std::max(
149 (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
150 AI->getAllocatedType()),
152 unsigned StackAlign =
153 MF->getSubtarget().getFrameLowering()->getStackAlignment();
154 if (Align <= StackAlign)
156 // Inform the Frame Information that we have variable-sized objects.
157 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
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(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,
177 if (PhysReg.first == SP)
178 MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
184 // Look for calls to the @llvm.va_start intrinsic. We can omit some
185 // prologue boilerplate for variadic functions that don't examine their
187 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
188 if (II->getIntrinsicID() == Intrinsic::vastart)
189 MF->getFrameInfo()->setHasVAStart(true);
192 // If we have a musttail call in a variadic funciton, we need to ensure we
193 // forward implicit register parameters.
194 if (const auto *CI = dyn_cast<CallInst>(I)) {
195 if (CI->isMustTailCall() && Fn->isVarArg())
196 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
199 // Mark values used outside their block as exported, by allocating
200 // a virtual register for them.
201 if (isUsedOutsideOfDefiningBlock(I))
202 if (!isa<AllocaInst>(I) ||
203 !StaticAllocaMap.count(cast<AllocaInst>(I)))
204 InitializeRegForValue(I);
206 // Collect llvm.dbg.declare information. This is done now instead of
207 // during the initial isel pass through the IR so that it is done
208 // in a predictable order.
209 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
210 assert(DI->getVariable() && "Missing variable");
211 assert(DI->getDebugLoc() && "Missing location");
212 if (MMI.hasDebugInfo()) {
213 // Don't handle byval struct arguments or VLAs, for example.
214 // Non-byval arguments are handled here (they refer to the stack
215 // temporary alloca at this point).
216 const Value *Address = DI->getAddress();
218 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
219 Address = BCI->getOperand(0);
220 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
221 DenseMap<const AllocaInst *, int>::iterator SI =
222 StaticAllocaMap.find(AI);
223 if (SI != StaticAllocaMap.end()) { // Check for VLAs.
225 MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(),
226 FI, DI->getDebugLoc());
233 // Decide the preferred extend type for a value.
234 PreferredExtendType[I] = getPreferredExtendForValue(I);
237 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
238 // also creates the initial PHI MachineInstrs, though none of the input
239 // operands are populated.
240 for (BB = Fn->begin(); BB != EB; ++BB) {
241 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
245 // Transfer the address-taken flag. This is necessary because there could
246 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
247 // the first one should be marked.
248 if (BB->hasAddressTaken())
249 MBB->setHasAddressTaken();
251 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
253 for (BasicBlock::const_iterator I = BB->begin();
254 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
255 if (PN->use_empty()) continue;
258 if (PN->getType()->isEmptyTy())
261 DebugLoc DL = PN->getDebugLoc();
262 unsigned PHIReg = ValueMap[PN];
263 assert(PHIReg && "PHI node does not have an assigned virtual register!");
265 SmallVector<EVT, 4> ValueVTs;
266 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
267 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
268 EVT VT = ValueVTs[vti];
269 unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT);
270 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
271 for (unsigned i = 0; i != NumRegisters; ++i)
272 BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i);
273 PHIReg += NumRegisters;
278 // Mark landing pad blocks.
279 SmallVector<const LandingPadInst *, 4> LPads;
280 for (BB = Fn->begin(); BB != EB; ++BB) {
281 if (const auto *Invoke = dyn_cast<InvokeInst>(BB->getTerminator()))
282 MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad();
283 if (BB->isLandingPad())
284 LPads.push_back(BB->getLandingPadInst());
287 // If this is an MSVC EH personality, we need to do a bit more work.
288 EHPersonality Personality = EHPersonality::Unknown;
290 Personality = classifyEHPersonality(LPads.back()->getPersonalityFn());
291 if (!isMSVCEHPersonality(Personality))
294 WinEHFuncInfo *EHInfo = nullptr;
295 if (Personality == EHPersonality::MSVC_Win64SEH) {
296 addSEHHandlersForLPads(LPads);
297 } else if (Personality == EHPersonality::MSVC_CXX) {
298 const Function *WinEHParentFn = MMI.getWinEHParent(&fn);
299 EHInfo = &MMI.getWinEHFuncInfo(WinEHParentFn);
300 if (EHInfo->LandingPadStateMap.empty()) {
301 WinEHNumbering Num(*EHInfo);
302 Num.findActionRootLPads(*WinEHParentFn);
303 // The VisitedHandlers list is used by both findActionRootLPads and
304 // calculateStateNumbers, but both functions need to visit all handlers.
305 Num.VisitedHandlers.clear();
306 Num.calculateStateNumbers(*WinEHParentFn);
307 // Pop everything on the handler stack.
308 // It may be necessary to call this more than once because a handler can
309 // be pushed on the stack as a result of clearing the stack.
310 while (!Num.HandlerStack.empty())
311 Num.processCallSite(None, ImmutableCallSite());
314 // Copy the state numbers to LandingPadInfo for the current function, which
315 // could be a handler or the parent.
316 for (const LandingPadInst *LP : LPads) {
317 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
318 MMI.addWinEHState(LPadMBB, EHInfo->LandingPadStateMap[LP]);
323 void FunctionLoweringInfo::addSEHHandlersForLPads(
324 ArrayRef<const LandingPadInst *> LPads) {
325 MachineModuleInfo &MMI = MF->getMMI();
327 // Iterate over all landing pads with llvm.eh.actions calls.
328 for (const LandingPadInst *LP : LPads) {
329 const IntrinsicInst *ActionsCall =
330 dyn_cast<IntrinsicInst>(LP->getNextNode());
332 ActionsCall->getIntrinsicID() != Intrinsic::eh_actions)
335 // Parse the llvm.eh.actions call we found.
336 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
337 SmallVector<std::unique_ptr<ActionHandler>, 4> Actions;
338 parseEHActions(ActionsCall, Actions);
340 // Iterate EH actions from most to least precedence, which means
341 // iterating in reverse.
342 for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) {
343 ActionHandler *Action = I->get();
344 if (auto *CH = dyn_cast<CatchHandler>(Action)) {
346 dyn_cast<Function>(CH->getSelector()->stripPointerCasts());
347 assert((Filter || CH->getSelector()->isNullValue()) &&
348 "expected function or catch-all");
349 const auto *RecoverBA =
350 cast<BlockAddress>(CH->getHandlerBlockOrFunc());
351 MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA);
353 assert(isa<CleanupHandler>(Action));
354 const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc());
355 MMI.addSEHCleanupHandler(LPadMBB, Fini);
361 void WinEHNumbering::createUnwindMapEntry(int ToState, ActionHandler *AH) {
362 WinEHUnwindMapEntry UME;
363 UME.ToState = ToState;
364 if (auto *CH = dyn_cast_or_null<CleanupHandler>(AH))
365 UME.Cleanup = cast<Function>(CH->getHandlerBlockOrFunc());
367 UME.Cleanup = nullptr;
368 FuncInfo.UnwindMap.push_back(UME);
371 void WinEHNumbering::createTryBlockMapEntry(int TryLow, int TryHigh,
372 ArrayRef<CatchHandler *> Handlers) {
373 // See if we already have an entry for this set of handlers.
374 // This is using iterators rather than a range-based for loop because
375 // if we find the entry we're looking for we'll need the iterator to erase it.
376 int NumHandlers = Handlers.size();
377 auto I = FuncInfo.TryBlockMap.begin();
378 auto E = FuncInfo.TryBlockMap.end();
379 for ( ; I != E; ++I) {
381 if (Entry.HandlerArray.size() != (size_t)NumHandlers)
384 for (N = 0; N < NumHandlers; ++N) {
385 if (Entry.HandlerArray[N].Handler != Handlers[N]->getHandlerBlockOrFunc())
386 break; // breaks out of inner loop
388 // If all the handlers match, this is what we were looking for.
389 if (N == NumHandlers) {
394 // If we found an existing entry for this set of handlers, extend the range
395 // but move the entry to the end of the map vector. The order of entries
396 // in the map is critical to the way that the runtime finds handlers.
397 // FIXME: Depending on what has happened with block ordering, this may
398 // incorrectly combine entries that should remain separate.
400 // Copy the existing entry.
401 WinEHTryBlockMapEntry Entry = *I;
402 Entry.TryLow = std::min(TryLow, Entry.TryLow);
403 Entry.TryHigh = std::max(TryHigh, Entry.TryHigh);
404 assert(Entry.TryLow <= Entry.TryHigh);
405 // Erase the old entry and add this one to the back.
406 FuncInfo.TryBlockMap.erase(I);
407 FuncInfo.TryBlockMap.push_back(Entry);
411 // If we didn't find an entry, create a new one.
412 WinEHTryBlockMapEntry TBME;
413 TBME.TryLow = TryLow;
414 TBME.TryHigh = TryHigh;
415 assert(TBME.TryLow <= TBME.TryHigh);
416 for (CatchHandler *CH : Handlers) {
418 if (CH->getSelector()->isNullValue()) {
419 HT.Adjectives = 0x40;
420 HT.TypeDescriptor = nullptr;
422 auto *GV = cast<GlobalVariable>(CH->getSelector()->stripPointerCasts());
423 // Selectors are always pointers to GlobalVariables with 'struct' type.
424 // The struct has two fields, adjectives and a type descriptor.
425 auto *CS = cast<ConstantStruct>(GV->getInitializer());
427 cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue();
429 cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts());
431 HT.Handler = cast<Function>(CH->getHandlerBlockOrFunc());
432 HT.CatchObjRecoverIdx = CH->getExceptionVarIndex();
433 TBME.HandlerArray.push_back(HT);
435 FuncInfo.TryBlockMap.push_back(TBME);
438 static void print_name(const Value *V) {
441 DEBUG(dbgs() << "null");
445 if (const auto *F = dyn_cast<Function>(V))
446 DEBUG(dbgs() << F->getName());
452 void WinEHNumbering::processCallSite(
453 MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
454 ImmutableCallSite CS) {
455 DEBUG(dbgs() << "processCallSite (EH state = " << currentEHNumber()
457 print_name(CS ? CS.getCalledValue() : nullptr);
458 DEBUG(dbgs() << '\n');
460 DEBUG(dbgs() << "HandlerStack: \n");
461 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
462 DEBUG(dbgs() << " ");
463 print_name(HandlerStack[I]->getHandlerBlockOrFunc());
464 DEBUG(dbgs() << '\n');
466 DEBUG(dbgs() << "Actions: \n");
467 for (int I = 0, E = Actions.size(); I < E; ++I) {
468 DEBUG(dbgs() << " ");
469 print_name(Actions[I]->getHandlerBlockOrFunc());
470 DEBUG(dbgs() << '\n');
472 int FirstMismatch = 0;
473 for (int E = std::min(HandlerStack.size(), Actions.size()); FirstMismatch < E;
475 if (HandlerStack[FirstMismatch]->getHandlerBlockOrFunc() !=
476 Actions[FirstMismatch]->getHandlerBlockOrFunc())
480 // Remove unmatched actions from the stack and process their EH states.
481 popUnmatchedActions(FirstMismatch);
483 DEBUG(dbgs() << "Pushing actions for CallSite: ");
484 print_name(CS ? CS.getCalledValue() : nullptr);
485 DEBUG(dbgs() << '\n');
487 bool LastActionWasCatch = false;
488 const LandingPadInst *LastRootLPad = nullptr;
489 for (size_t I = FirstMismatch; I != Actions.size(); ++I) {
490 // We can reuse eh states when pushing two catches for the same invoke.
491 bool CurrActionIsCatch = isa<CatchHandler>(Actions[I].get());
492 auto *Handler = cast<Function>(Actions[I]->getHandlerBlockOrFunc());
493 // Various conditions can lead to a handler being popped from the
494 // stack and re-pushed later. That shouldn't create a new state.
495 // FIXME: Can code optimization lead to re-used handlers?
496 if (FuncInfo.HandlerEnclosedState.count(Handler)) {
497 // If we already assigned the state enclosed by this handler re-use it.
498 Actions[I]->setEHState(FuncInfo.HandlerEnclosedState[Handler]);
501 const LandingPadInst* RootLPad = FuncInfo.RootLPad[Handler];
502 if (CurrActionIsCatch && LastActionWasCatch && RootLPad == LastRootLPad) {
503 DEBUG(dbgs() << "setEHState for handler to " << currentEHNumber() << "\n");
504 Actions[I]->setEHState(currentEHNumber());
506 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber() << ", ");
507 print_name(Actions[I]->getHandlerBlockOrFunc());
508 DEBUG(dbgs() << ") with EH state " << NextState << "\n");
509 createUnwindMapEntry(currentEHNumber(), Actions[I].get());
510 DEBUG(dbgs() << "setEHState for handler to " << NextState << "\n");
511 Actions[I]->setEHState(NextState);
514 HandlerStack.push_back(std::move(Actions[I]));
515 LastActionWasCatch = CurrActionIsCatch;
516 LastRootLPad = RootLPad;
519 // This is used to defer numbering states for a handler until after the
520 // last time it appears in an invoke action list.
522 for (int I = 0, E = HandlerStack.size(); I < E; ++I) {
523 auto *Handler = cast<Function>(HandlerStack[I]->getHandlerBlockOrFunc());
524 if (FuncInfo.LastInvoke[Handler] != cast<InvokeInst>(CS.getInstruction()))
526 FuncInfo.LastInvokeVisited[Handler] = true;
527 DEBUG(dbgs() << "Last invoke of ");
529 DEBUG(dbgs() << " has been visited.\n");
533 DEBUG(dbgs() << "In EHState " << currentEHNumber() << " for CallSite: ");
534 print_name(CS ? CS.getCalledValue() : nullptr);
535 DEBUG(dbgs() << '\n');
538 void WinEHNumbering::popUnmatchedActions(int FirstMismatch) {
539 // Don't recurse while we are looping over the handler stack. Instead, defer
540 // the numbering of the catch handlers until we are done popping.
541 SmallVector<CatchHandler *, 4> PoppedCatches;
542 for (int I = HandlerStack.size() - 1; I >= FirstMismatch; --I) {
543 std::unique_ptr<ActionHandler> Handler = HandlerStack.pop_back_val();
544 if (isa<CatchHandler>(Handler.get()))
545 PoppedCatches.push_back(cast<CatchHandler>(Handler.release()));
548 int TryHigh = NextState - 1;
549 int LastTryLowIdx = 0;
550 for (int I = 0, E = PoppedCatches.size(); I != E; ++I) {
551 CatchHandler *CH = PoppedCatches[I];
552 DEBUG(dbgs() << "Popped handler with state " << CH->getEHState() << "\n");
553 if (I + 1 == E || CH->getEHState() != PoppedCatches[I + 1]->getEHState()) {
554 int TryLow = CH->getEHState();
556 makeArrayRef(&PoppedCatches[LastTryLowIdx], I - LastTryLowIdx + 1);
557 DEBUG(dbgs() << "createTryBlockMapEntry(" << TryLow << ", " << TryHigh);
558 for (size_t J = 0; J < Handlers.size(); ++J) {
559 DEBUG(dbgs() << ", ");
560 print_name(Handlers[J]->getHandlerBlockOrFunc());
562 DEBUG(dbgs() << ")\n");
563 createTryBlockMapEntry(TryLow, TryHigh, Handlers);
564 LastTryLowIdx = I + 1;
568 for (CatchHandler *CH : PoppedCatches) {
569 if (auto *F = dyn_cast<Function>(CH->getHandlerBlockOrFunc())) {
570 if (FuncInfo.LastInvokeVisited[F]) {
571 DEBUG(dbgs() << "Assigning base state " << NextState << " to ");
573 DEBUG(dbgs() << '\n');
574 FuncInfo.HandlerBaseState[F] = NextState;
575 DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber()
577 createUnwindMapEntry(currentEHNumber(), nullptr);
579 calculateStateNumbers(*F);
582 DEBUG(dbgs() << "Deferring handling of ");
584 DEBUG(dbgs() << " until last invoke visited.\n");
591 void WinEHNumbering::calculateStateNumbers(const Function &F) {
592 auto I = VisitedHandlers.insert(&F);
594 return; // We've already visited this handler, don't renumber it.
596 int OldBaseState = CurrentBaseState;
597 if (FuncInfo.HandlerBaseState.count(&F)) {
598 CurrentBaseState = FuncInfo.HandlerBaseState[&F];
601 size_t SavedHandlerStackSize = HandlerStack.size();
603 DEBUG(dbgs() << "Calculating state numbers for: " << F.getName() << '\n');
604 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
605 for (const BasicBlock &BB : F) {
606 for (const Instruction &I : BB) {
607 const auto *CI = dyn_cast<CallInst>(&I);
608 if (!CI || CI->doesNotThrow())
610 processCallSite(None, CI);
612 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
615 const LandingPadInst *LPI = II->getLandingPadInst();
616 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
619 assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
620 parseEHActions(ActionsCall, ActionList);
621 if (ActionList.empty())
623 processCallSite(ActionList, II);
625 FuncInfo.LandingPadStateMap[LPI] = currentEHNumber();
626 DEBUG(dbgs() << "Assigning state " << currentEHNumber()
627 << " to landing pad at " << LPI->getParent()->getName()
631 // Pop any actions that were pushed on the stack for this function.
632 popUnmatchedActions(SavedHandlerStackSize);
634 DEBUG(dbgs() << "Assigning max state " << NextState - 1
635 << " to " << F.getName() << '\n');
636 FuncInfo.CatchHandlerMaxState[&F] = NextState - 1;
638 CurrentBaseState = OldBaseState;
641 // This function follows the same basic traversal as calculateStateNumbers
642 // but it is necessary to identify the root landing pad associated
643 // with each action before we start assigning state numbers.
644 void WinEHNumbering::findActionRootLPads(const Function &F) {
645 auto I = VisitedHandlers.insert(&F);
647 return; // We've already visited this handler, don't revisit it.
649 SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
650 for (const BasicBlock &BB : F) {
651 const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
654 const LandingPadInst *LPI = II->getLandingPadInst();
655 auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
659 assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
660 parseEHActions(ActionsCall, ActionList);
661 if (ActionList.empty())
663 for (int I = 0, E = ActionList.size(); I < E; ++I) {
665 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc())) {
666 FuncInfo.LastInvoke[Handler] = II;
667 // Don't replace the root landing pad if we previously saw this
668 // handler in a different function.
669 if (FuncInfo.RootLPad.count(Handler) &&
670 FuncInfo.RootLPad[Handler]->getParent()->getParent() != &F)
672 DEBUG(dbgs() << "Setting root lpad for ");
674 DEBUG(dbgs() << " to " << LPI->getParent()->getName() << '\n');
675 FuncInfo.RootLPad[Handler] = LPI;
678 // Walk the actions again and look for nested handlers. This has to
679 // happen after all of the actions have been processed in the current
681 for (int I = 0, E = ActionList.size(); I < E; ++I)
683 = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc()))
684 findActionRootLPads(*Handler);
689 /// clear - Clear out all the function-specific state. This returns this
690 /// FunctionLoweringInfo to an empty state, ready to be used for a
691 /// different function.
692 void FunctionLoweringInfo::clear() {
693 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
694 "Not all catch info was assigned to a landing pad!");
698 StaticAllocaMap.clear();
700 CatchInfoLost.clear();
701 CatchInfoFound.clear();
703 LiveOutRegInfo.clear();
705 ArgDbgValues.clear();
706 ByValArgFrameIndexMap.clear();
708 StatepointStackSlots.clear();
709 StatepointRelocatedValues.clear();
710 PreferredExtendType.clear();
713 /// CreateReg - Allocate a single virtual register for the given type.
714 unsigned FunctionLoweringInfo::CreateReg(MVT VT) {
715 return RegInfo->createVirtualRegister(
716 MF->getSubtarget().getTargetLowering()->getRegClassFor(VT));
719 /// CreateRegs - Allocate the appropriate number of virtual registers of
720 /// the correctly promoted or expanded types. Assign these registers
721 /// consecutive vreg numbers and return the first assigned number.
723 /// In the case that the given value has struct or array type, this function
724 /// will assign registers for each member or element.
726 unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
727 const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
729 SmallVector<EVT, 4> ValueVTs;
730 ComputeValueVTs(*TLI, Ty, ValueVTs);
732 unsigned FirstReg = 0;
733 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
734 EVT ValueVT = ValueVTs[Value];
735 MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT);
737 unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT);
738 for (unsigned i = 0; i != NumRegs; ++i) {
739 unsigned R = CreateReg(RegisterVT);
740 if (!FirstReg) FirstReg = R;
746 /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the
747 /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If
748 /// the register's LiveOutInfo is for a smaller bit width, it is extended to
749 /// the larger bit width by zero extension. The bit width must be no smaller
750 /// than the LiveOutInfo's existing bit width.
751 const FunctionLoweringInfo::LiveOutInfo *
752 FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) {
753 if (!LiveOutRegInfo.inBounds(Reg))
756 LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
760 if (BitWidth > LOI->KnownZero.getBitWidth()) {
761 LOI->NumSignBits = 1;
762 LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
763 LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
769 /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination
770 /// register based on the LiveOutInfo of its operands.
771 void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) {
772 Type *Ty = PN->getType();
773 if (!Ty->isIntegerTy() || Ty->isVectorTy())
776 SmallVector<EVT, 1> ValueVTs;
777 ComputeValueVTs(*TLI, Ty, ValueVTs);
778 assert(ValueVTs.size() == 1 &&
779 "PHIs with non-vector integer types should have a single VT.");
780 EVT IntVT = ValueVTs[0];
782 if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
784 IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
785 unsigned BitWidth = IntVT.getSizeInBits();
787 unsigned DestReg = ValueMap[PN];
788 if (!TargetRegisterInfo::isVirtualRegister(DestReg))
790 LiveOutRegInfo.grow(DestReg);
791 LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
793 Value *V = PN->getIncomingValue(0);
794 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
795 DestLOI.NumSignBits = 1;
796 APInt Zero(BitWidth, 0);
797 DestLOI.KnownZero = Zero;
798 DestLOI.KnownOne = Zero;
802 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
803 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
804 DestLOI.NumSignBits = Val.getNumSignBits();
805 DestLOI.KnownZero = ~Val;
806 DestLOI.KnownOne = Val;
808 assert(ValueMap.count(V) && "V should have been placed in ValueMap when its"
809 "CopyToReg node was created.");
810 unsigned SrcReg = ValueMap[V];
811 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
812 DestLOI.IsValid = false;
815 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
817 DestLOI.IsValid = false;
823 assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
824 DestLOI.KnownOne.getBitWidth() == BitWidth &&
825 "Masks should have the same bit width as the type.");
827 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) {
828 Value *V = PN->getIncomingValue(i);
829 if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) {
830 DestLOI.NumSignBits = 1;
831 APInt Zero(BitWidth, 0);
832 DestLOI.KnownZero = Zero;
833 DestLOI.KnownOne = Zero;
837 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
838 APInt Val = CI->getValue().zextOrTrunc(BitWidth);
839 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits());
840 DestLOI.KnownZero &= ~Val;
841 DestLOI.KnownOne &= Val;
845 assert(ValueMap.count(V) && "V should have been placed in ValueMap when "
846 "its CopyToReg node was created.");
847 unsigned SrcReg = ValueMap[V];
848 if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) {
849 DestLOI.IsValid = false;
852 const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
854 DestLOI.IsValid = false;
857 DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
858 DestLOI.KnownZero &= SrcLOI->KnownZero;
859 DestLOI.KnownOne &= SrcLOI->KnownOne;
863 /// setArgumentFrameIndex - Record frame index for the byval
864 /// argument. This overrides previous frame index entry for this argument,
866 void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
868 ByValArgFrameIndexMap[A] = FI;
871 /// getArgumentFrameIndex - Get frame index for the byval argument.
872 /// If the argument does not have any assigned frame index then 0 is
874 int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
875 DenseMap<const Argument *, int>::iterator I =
876 ByValArgFrameIndexMap.find(A);
877 if (I != ByValArgFrameIndexMap.end())
879 DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
883 /// ComputeUsesVAFloatArgument - Determine if any floating-point values are
884 /// being passed to this variadic function, and set the MachineModuleInfo's
885 /// usesVAFloatArgument flag if so. This flag is used to emit an undefined
886 /// reference to _fltused on Windows, which will link in MSVCRT's
887 /// floating-point support.
888 void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
889 MachineModuleInfo *MMI)
891 FunctionType *FT = cast<FunctionType>(
892 I.getCalledValue()->getType()->getContainedType(0));
893 if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
894 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
895 Type* T = I.getArgOperand(i)->getType();
896 for (auto i : post_order(T)) {
897 if (i->isFloatingPointTy()) {
898 MMI->setUsesVAFloatArgument(true);
906 /// AddLandingPadInfo - Extract the exception handling information from the
907 /// landingpad instruction and add them to the specified machine module info.
908 void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
909 MachineBasicBlock *MBB) {
910 MMI.addPersonality(MBB,
911 cast<Function>(I.getPersonalityFn()->stripPointerCasts()));
916 // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
917 // but we need to do it this way because of how the DWARF EH emitter
918 // processes the clauses.
919 for (unsigned i = I.getNumClauses(); i != 0; --i) {
920 Value *Val = I.getClause(i - 1);
921 if (I.isCatch(i - 1)) {
922 MMI.addCatchTypeInfo(MBB,
923 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
925 // Add filters in a list.
926 Constant *CVal = cast<Constant>(Val);
927 SmallVector<const GlobalValue*, 4> FilterList;
928 for (User::op_iterator
929 II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II)
930 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
932 MMI.addFilterTypeInfo(MBB, FilterList);