[WinEH] C++ EH state numbering fixes
[oota-llvm.git] / lib / CodeGen / SelectionDAG / FunctionLoweringInfo.cpp
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
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"
42 #include <algorithm>
43 using namespace llvm;
44
45 #define DEBUG_TYPE "function-lowering-info"
46
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))
56       return true;
57
58   return false;
59 }
60
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.
65   //
66   // With this optimization, we would be able to reduce some redundant sign or
67   // zero extension instruction, and eventually more machine CSE opportunities
68   // can be exposed.
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();
75     }
76   }
77   if (NumOfSigned > NumOfUnsigned)
78     ExtendKind = ISD::SIGN_EXTEND;
79
80   return ExtendKind;
81 }
82
83 namespace {
84 struct WinEHNumbering {
85   WinEHNumbering(WinEHFuncInfo &FuncInfo) : FuncInfo(FuncInfo),
86       CurrentBaseState(-1), NextState(0) {}
87
88   WinEHFuncInfo &FuncInfo;
89   int CurrentBaseState;
90   int NextState;
91
92   SmallVector<std::unique_ptr<ActionHandler>, 4> HandlerStack;
93   SmallPtrSet<const Function *, 4> VisitedHandlers;
94
95   int currentEHNumber() const {
96     return HandlerStack.empty() ? CurrentBaseState : HandlerStack.back()->getEHState();
97   }
98
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);
107 };
108 }
109
110 void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
111                                SelectionDAG *DAG) {
112   Fn = &fn;
113   MF = &mf;
114   TLI = MF->getSubtarget().getTargetLowering();
115   RegInfo = &MF->getRegInfo();
116   MachineModuleInfo &MMI = MF->getMMI();
117
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());
123
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
126   // them.
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();
130          I != E; ++I) {
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);
137           unsigned Align =
138               std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
139                        AI->getAlignment());
140
141           TySize *= CUI->getZExtValue();   // Get total allocated size.
142           if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
143
144           StaticAllocaMap[AI] =
145             MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
146
147         } else {
148           unsigned Align = std::max(
149               (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
150                 AI->getAllocatedType()),
151               AI->getAlignment());
152           unsigned StackAlign =
153               MF->getSubtarget().getFrameLowering()->getStackAlignment();
154           if (Align <= StackAlign)
155             Align = 0;
156           // Inform the Frame Information that we have variable-sized objects.
157           MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
158         }
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(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()->setHasInlineAsmWithSPAdjust(true);
179             }
180           }
181         }
182       }
183
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
186       // arguments.
187       if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
188         if (II->getIntrinsicID() == Intrinsic::vastart)
189           MF->getFrameInfo()->setHasVAStart(true);
190       }
191
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);
197       }
198
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);
205
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();
217           if (Address) {
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.
224                 int FI = SI->second;
225                 MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(),
226                                        FI, DI->getDebugLoc());
227               }
228             }
229           }
230         }
231       }
232
233       // Decide the preferred extend type for a value.
234       PreferredExtendType[I] = getPreferredExtendForValue(I);
235     }
236
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);
242     MBBMap[BB] = MBB;
243     MF->push_back(MBB);
244
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();
250
251     // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
252     // appropriate.
253     for (BasicBlock::const_iterator I = BB->begin();
254          const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
255       if (PN->use_empty()) continue;
256
257       // Skip empty types
258       if (PN->getType()->isEmptyTy())
259         continue;
260
261       DebugLoc DL = PN->getDebugLoc();
262       unsigned PHIReg = ValueMap[PN];
263       assert(PHIReg && "PHI node does not have an assigned virtual register!");
264
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;
274       }
275     }
276   }
277
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());
285   }
286
287   // If this is an MSVC EH personality, we need to do a bit more work.
288   EHPersonality Personality = EHPersonality::Unknown;
289   if (!LPads.empty())
290     Personality = classifyEHPersonality(LPads.back()->getPersonalityFn());
291   if (!isMSVCEHPersonality(Personality))
292     return;
293
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());
312     }
313
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]);
319     }
320   }
321 }
322
323 void FunctionLoweringInfo::addSEHHandlersForLPads(
324     ArrayRef<const LandingPadInst *> LPads) {
325   MachineModuleInfo &MMI = MF->getMMI();
326
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());
331     if (!ActionsCall ||
332         ActionsCall->getIntrinsicID() != Intrinsic::eh_actions)
333       continue;
334
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);
339
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)) {
345         const auto *Filter =
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);
352       } else {
353         assert(isa<CleanupHandler>(Action));
354         const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc());
355         MMI.addSEHCleanupHandler(LPadMBB, Fini);
356       }
357     }
358   }
359 }
360
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());
366   else
367     UME.Cleanup = nullptr;
368   FuncInfo.UnwindMap.push_back(UME);
369 }
370
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) {
380     auto &Entry = *I;
381     if (Entry.HandlerArray.size() != NumHandlers)
382       continue;
383     int N;
384     for (N = 0; N < NumHandlers; ++N) {
385       if (Entry.HandlerArray[N].Handler != Handlers[N]->getHandlerBlockOrFunc())
386         break; // breaks out of inner loop
387     }
388     // If all the handlers match, this is what we were looking for.
389     if (N == NumHandlers) {
390       break;
391     }
392   }
393
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.
399   if (I != E) {
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);
408     return;
409   }
410
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) {
417     WinEHHandlerType HT;
418     if (CH->getSelector()->isNullValue()) {
419       HT.Adjectives = 0x40;
420       HT.TypeDescriptor = nullptr;
421     } else {
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());
426       HT.Adjectives =
427           cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue();
428       HT.TypeDescriptor =
429           cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts());
430     }
431     HT.Handler = cast<Function>(CH->getHandlerBlockOrFunc());
432     HT.CatchObjRecoverIdx = CH->getExceptionVarIndex();
433     TBME.HandlerArray.push_back(HT);
434   }
435   FuncInfo.TryBlockMap.push_back(TBME);
436 }
437
438 static void print_name(const Value *V) {
439 #ifndef NDEBUG
440   if (!V) {
441     DEBUG(dbgs() << "null");
442     return;
443   }
444
445   if (const auto *F = dyn_cast<Function>(V))
446     DEBUG(dbgs() << F->getName());
447   else
448     DEBUG(V->dump());
449 #endif
450 }
451
452 void WinEHNumbering::processCallSite(
453     MutableArrayRef<std::unique_ptr<ActionHandler>> Actions,
454     ImmutableCallSite CS) {
455   DEBUG(dbgs() << "processCallSite (EH state = " << currentEHNumber()
456                << ") for: ");
457   print_name(CS ? CS.getCalledValue() : nullptr);
458   DEBUG(dbgs() << '\n');
459
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');
465   }
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');
471   }
472   int FirstMismatch = 0;
473   for (int E = std::min(HandlerStack.size(), Actions.size()); FirstMismatch < E;
474        ++FirstMismatch) {
475     if (HandlerStack[FirstMismatch]->getHandlerBlockOrFunc() !=
476         Actions[FirstMismatch]->getHandlerBlockOrFunc())
477       break;
478   }
479
480   // Remove unmatched actions from the stack and process their EH states.
481   popUnmatchedActions(FirstMismatch);
482
483   DEBUG(dbgs() << "Pushing actions for CallSite: ");
484   print_name(CS ? CS.getCalledValue() : nullptr);
485   DEBUG(dbgs() << '\n');
486
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]);
499       continue;
500     }
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());
505     } else {
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);
512       NextState++;
513     }
514     HandlerStack.push_back(std::move(Actions[I]));
515     LastActionWasCatch = CurrActionIsCatch;
516     LastRootLPad = RootLPad;
517   }
518
519   // This is used to defer numbering states for a handler until after the
520   // last time it appears in an invoke action list.
521   if (CS.isInvoke()) {
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()))
525         continue;
526       FuncInfo.LastInvokeVisited[Handler] = true;
527       DEBUG(dbgs() << "Last invoke of ");
528       print_name(Handler);
529       DEBUG(dbgs() << " has been visited.\n");
530     }
531   }
532
533   DEBUG(dbgs() << "In EHState " << currentEHNumber() << " for CallSite: ");
534   print_name(CS ? CS.getCalledValue() : nullptr);
535   DEBUG(dbgs() << '\n');
536 }
537
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()));
546   }
547
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();
555       auto Handlers =
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());
561       }
562       DEBUG(dbgs() << ")\n");
563       createTryBlockMapEntry(TryLow, TryHigh, Handlers);
564       LastTryLowIdx = I + 1;
565     }
566   }
567
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 ");
572         print_name(F);
573         DEBUG(dbgs() << '\n');
574         FuncInfo.HandlerBaseState[F] = NextState;
575         DEBUG(dbgs() << "createUnwindMapEntry(" << currentEHNumber() 
576                      << ", null)\n");
577         createUnwindMapEntry(currentEHNumber(), nullptr);
578         ++NextState;
579         calculateStateNumbers(*F);
580       }
581       else {
582         DEBUG(dbgs() << "Deferring handling of ");
583         print_name(F);
584         DEBUG(dbgs() << " until last invoke visited.\n");
585       }
586     }
587     delete CH;
588   }
589 }
590
591 void WinEHNumbering::calculateStateNumbers(const Function &F) {
592   auto I = VisitedHandlers.insert(&F);
593   if (!I.second)
594     return; // We've already visited this handler, don't renumber it.
595
596   int OldBaseState = CurrentBaseState;
597   if (FuncInfo.HandlerBaseState.count(&F)) {
598     CurrentBaseState = FuncInfo.HandlerBaseState[&F];
599   }
600
601   size_t SavedHandlerStackSize = HandlerStack.size();
602
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())
609         continue;
610       processCallSite(None, CI);
611     }
612     const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
613     if (!II)
614       continue;
615     const LandingPadInst *LPI = II->getLandingPadInst();
616     auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
617     if (!ActionsCall)
618       continue;
619     assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
620     parseEHActions(ActionsCall, ActionList);
621     if (ActionList.empty())
622       continue;
623     processCallSite(ActionList, II);
624     ActionList.clear();
625     FuncInfo.LandingPadStateMap[LPI] = currentEHNumber();
626     DEBUG(dbgs() << "Assigning state " << currentEHNumber()
627                   << " to landing pad at " << LPI->getParent()->getName()
628                   << '\n');
629   }
630
631   // Pop any actions that were pushed on the stack for this function.
632   popUnmatchedActions(SavedHandlerStackSize);
633
634   DEBUG(dbgs() << "Assigning max state " << NextState - 1
635                << " to " << F.getName() << '\n');
636   FuncInfo.CatchHandlerMaxState[&F] = NextState - 1;
637
638   CurrentBaseState = OldBaseState;
639 }
640
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);
646   if (!I.second)
647     return; // We've already visited this handler, don't revisit it.
648
649   SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
650   for (const BasicBlock &BB : F) {
651     const auto *II = dyn_cast<InvokeInst>(BB.getTerminator());
652     if (!II)
653       continue;
654     const LandingPadInst *LPI = II->getLandingPadInst();
655     auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode());
656     if (!ActionsCall)
657       continue;
658
659     assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions);
660     parseEHActions(ActionsCall, ActionList);
661     if (ActionList.empty())
662       continue;
663     for (int I = 0, E = ActionList.size(); I < E; ++I) {
664       if (auto *Handler
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)
671           continue;
672         DEBUG(dbgs() << "Setting root lpad for ");
673         print_name(Handler);
674         DEBUG(dbgs() << " to " << LPI->getParent()->getName() << '\n');
675         FuncInfo.RootLPad[Handler] = LPI;
676       }
677     }
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
680     // function.
681     for (int I = 0, E = ActionList.size(); I < E; ++I)
682       if (auto *Handler
683               = dyn_cast<Function>(ActionList[I]->getHandlerBlockOrFunc()))
684         findActionRootLPads(*Handler);
685     ActionList.clear();
686   }
687 }
688
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!");
695
696   MBBMap.clear();
697   ValueMap.clear();
698   StaticAllocaMap.clear();
699 #ifndef NDEBUG
700   CatchInfoLost.clear();
701   CatchInfoFound.clear();
702 #endif
703   LiveOutRegInfo.clear();
704   VisitedBBs.clear();
705   ArgDbgValues.clear();
706   ByValArgFrameIndexMap.clear();
707   RegFixups.clear();
708   StatepointStackSlots.clear();
709   StatepointRelocatedValues.clear();
710   PreferredExtendType.clear();
711 }
712
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));
717 }
718
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.
722 ///
723 /// In the case that the given value has struct or array type, this function
724 /// will assign registers for each member or element.
725 ///
726 unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) {
727   const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
728
729   SmallVector<EVT, 4> ValueVTs;
730   ComputeValueVTs(*TLI, Ty, ValueVTs);
731
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);
736
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;
741     }
742   }
743   return FirstReg;
744 }
745
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))
754     return nullptr;
755
756   LiveOutInfo *LOI = &LiveOutRegInfo[Reg];
757   if (!LOI->IsValid)
758     return nullptr;
759
760   if (BitWidth > LOI->KnownZero.getBitWidth()) {
761     LOI->NumSignBits = 1;
762     LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth);
763     LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth);
764   }
765
766   return LOI;
767 }
768
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())
774     return;
775
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];
781
782   if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1)
783     return;
784   IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT);
785   unsigned BitWidth = IntVT.getSizeInBits();
786
787   unsigned DestReg = ValueMap[PN];
788   if (!TargetRegisterInfo::isVirtualRegister(DestReg))
789     return;
790   LiveOutRegInfo.grow(DestReg);
791   LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg];
792
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;
799     return;
800   }
801
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;
807   } else {
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;
813       return;
814     }
815     const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
816     if (!SrcLOI) {
817       DestLOI.IsValid = false;
818       return;
819     }
820     DestLOI = *SrcLOI;
821   }
822
823   assert(DestLOI.KnownZero.getBitWidth() == BitWidth &&
824          DestLOI.KnownOne.getBitWidth() == BitWidth &&
825          "Masks should have the same bit width as the type.");
826
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;
834       return;
835     }
836
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;
842       continue;
843     }
844
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;
850       return;
851     }
852     const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth);
853     if (!SrcLOI) {
854       DestLOI.IsValid = false;
855       return;
856     }
857     DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits);
858     DestLOI.KnownZero &= SrcLOI->KnownZero;
859     DestLOI.KnownOne &= SrcLOI->KnownOne;
860   }
861 }
862
863 /// setArgumentFrameIndex - Record frame index for the byval
864 /// argument. This overrides previous frame index entry for this argument,
865 /// if any.
866 void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A,
867                                                  int FI) {
868   ByValArgFrameIndexMap[A] = FI;
869 }
870
871 /// getArgumentFrameIndex - Get frame index for the byval argument.
872 /// If the argument does not have any assigned frame index then 0 is
873 /// returned.
874 int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) {
875   DenseMap<const Argument *, int>::iterator I =
876     ByValArgFrameIndexMap.find(A);
877   if (I != ByValArgFrameIndexMap.end())
878     return I->second;
879   DEBUG(dbgs() << "Argument does not have assigned frame index!\n");
880   return 0;
881 }
882
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)
890 {
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);
899           return;
900         }
901       }
902     }
903   }
904 }
905
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()));
912
913   if (I.isCleanup())
914     MMI.addCleanup(MBB);
915
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()));
924     } else {
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()));
931
932       MMI.addFilterTypeInfo(MBB, FilterList);
933     }
934   }
935 }