diff options
| author | Joseph Tremoulet <jotrem@microsoft.com> | 2016-01-04 16:16:01 +0000 | 
|---|---|---|
| committer | Joseph Tremoulet <jotrem@microsoft.com> | 2016-01-04 16:16:01 +0000 | 
| commit | 52f729a613910f0b03420e1912d301c17e975a22 (patch) | |
| tree | 3c7b6af49f4605c02ced420e3495fb56f630297d /llvm/lib/CodeGen | |
| parent | 5e27146df780271143355b82ab05fb8baac88b95 (diff) | |
| download | bcm5719-llvm-52f729a613910f0b03420e1912d301c17e975a22.tar.gz bcm5719-llvm-52f729a613910f0b03420e1912d301c17e975a22.zip | |
[WinEH] Update CoreCLR EH state numbering
Summary:
Fix the CLR state numbering to generate correct tables, and update the lit
test to verify them.
The CLR numbering assigns one state number to each catchpad and
cleanuppad.
It also computes two tree-like relations over states:
 1) Each state has a "HandlerParentState", which is the state of the next
    outer handler enclosing this state's handler (same as nearest ancestor
    per the ParentPad linkage on EH pads, but skipping over catchswitches).
 2) Each state has a "TryParentState", which:
    a) for a catchpad that's not the last handler on its catchswitch, is
       the state of the next catchpad on that catchswitch.
    b) for all other pads, is the state of the pad whose try region is the
       next outer try region enclosing this state's try region.  The "try
       regions are not present as such in the IR, but will be inferred
       based on the placement of invokes and pads which reach each other
       by exceptional exits.
Catchswitches do not get their own states, but each gets mapped to the
state of its first catchpad.
Table generation requires each state's "unwind dest" state to have a lower
state number than the given state.
Since HandlerParentState can be computed as a function of a pad's
ParentPad, and TryParentState can be computed as a function of its unwind
dest and the TryParentStates of its children, the CLR state numbering
algorithm first computes HandlerParentState in a top-down pass, then
computes TryParentState in a bottom-up pass.
Also reword some comments/names in the CLR EH table generation to make the
distinction between the different kinds of "parent" clear.
Reviewers: rnk, andrew.w.kaylor, majnemer
Subscribers: AndyAyers, llvm-commits
Differential Revision: http://reviews.llvm.org/D15325
llvm-svn: 256760
Diffstat (limited to 'llvm/lib/CodeGen')
| -rw-r--r-- | llvm/lib/CodeGen/AsmPrinter/WinException.cpp | 43 | ||||
| -rw-r--r-- | llvm/lib/CodeGen/WinEHPrepare.cpp | 242 | 
2 files changed, 203 insertions, 82 deletions
| diff --git a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp index 48b7104f24c..4da5b580fcd 100644 --- a/llvm/lib/CodeGen/AsmPrinter/WinException.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/WinException.cpp @@ -976,32 +976,32 @@ void WinException::emitExceptHandlerTable(const MachineFunction *MF) {    }  } -static int getRank(const WinEHFuncInfo &FuncInfo, int State) { +static int getTryRank(const WinEHFuncInfo &FuncInfo, int State) {    int Rank = 0;    while (State != -1) {      ++Rank; -    State = FuncInfo.ClrEHUnwindMap[State].Parent; +    State = FuncInfo.ClrEHUnwindMap[State].TryParentState;    }    return Rank;  } -static int getAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) { -  int LeftRank = getRank(FuncInfo, Left); -  int RightRank = getRank(FuncInfo, Right); +static int getTryAncestor(const WinEHFuncInfo &FuncInfo, int Left, int Right) { +  int LeftRank = getTryRank(FuncInfo, Left); +  int RightRank = getTryRank(FuncInfo, Right);    while (LeftRank < RightRank) { -    Right = FuncInfo.ClrEHUnwindMap[Right].Parent; +    Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;      --RightRank;    }    while (RightRank < LeftRank) { -    Left = FuncInfo.ClrEHUnwindMap[Left].Parent; +    Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState;      --LeftRank;    }    while (Left != Right) { -    Left = FuncInfo.ClrEHUnwindMap[Left].Parent; -    Right = FuncInfo.ClrEHUnwindMap[Right].Parent; +    Left = FuncInfo.ClrEHUnwindMap[Left].TryParentState; +    Right = FuncInfo.ClrEHUnwindMap[Right].TryParentState;    }    return Left; @@ -1035,9 +1035,9 @@ void WinException::emitCLRExceptionTable(const MachineFunction *MF) {          FuncInfo.ClrEHUnwindMap[State].Handler.get<MachineBasicBlock *>();      HandlerStates[HandlerBlock] = State;      // Use this loop through all handlers to verify our assumption (used in -    // the MinEnclosingState computation) that ancestors have lower state -    // numbers than their descendants. -    assert(FuncInfo.ClrEHUnwindMap[State].Parent < State && +    // the MinEnclosingState computation) that enclosing funclets have lower +    // state numbers than their enclosed funclets. +    assert(FuncInfo.ClrEHUnwindMap[State].HandlerParentState < State &&             "ill-formed state numbering");    }    // Map the main function to the NullState. @@ -1070,7 +1070,6 @@ void WinException::emitCLRExceptionTable(const MachineFunction *MF) {    SmallVector<int, 4> MinClauseMap((size_t)NumStates, NumStates);    // Visit the root function and each funclet. -    for (MachineFunction::const_iterator FuncletStart = MF->begin(),                                         FuncletEnd = MF->begin(),                                         End = MF->end(); @@ -1100,17 +1099,18 @@ void WinException::emitCLRExceptionTable(const MachineFunction *MF) {      for (const auto &StateChange :           InvokeStateChangeIterator::range(FuncInfo, FuncletStart, FuncletEnd)) {        // Close any try regions we're not still under -      int AncestorState = -          getAncestor(FuncInfo, CurrentState, StateChange.NewState); -      while (CurrentState != AncestorState) { -        assert(CurrentState != NullState && "Failed to find ancestor!"); +      int StillPendingState = +          getTryAncestor(FuncInfo, CurrentState, StateChange.NewState); +      while (CurrentState != StillPendingState) { +        assert(CurrentState != NullState && +               "Failed to find still-pending state!");          // Close the pending clause          Clauses.push_back({CurrentStartLabel, StateChange.PreviousEndLabel,                             CurrentState, FuncletState}); -        // Now the parent handler is current -        CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].Parent; +        // Now the next-outer try region is current +        CurrentState = FuncInfo.ClrEHUnwindMap[CurrentState].TryParentState;          // Pop the new start label from the handler stack if we've exited all -        // descendants of the corresponding handler. +        // inner try regions of the corresponding try region.          if (HandlerStack.back().second == CurrentState)            CurrentStartLabel = HandlerStack.pop_back_val().first;        } @@ -1121,7 +1121,8 @@ void WinException::emitCLRExceptionTable(const MachineFunction *MF) {          // it.          for (int EnteredState = StateChange.NewState;               EnteredState != CurrentState; -             EnteredState = FuncInfo.ClrEHUnwindMap[EnteredState].Parent) { +             EnteredState = +                 FuncInfo.ClrEHUnwindMap[EnteredState].TryParentState) {            int &MinEnclosingState = MinClauseMap[EnteredState];            if (FuncletState < MinEnclosingState)              MinEnclosingState = FuncletState; diff --git a/llvm/lib/CodeGen/WinEHPrepare.cpp b/llvm/lib/CodeGen/WinEHPrepare.cpp index 27043b577e2..2426c27d43d 100644 --- a/llvm/lib/CodeGen/WinEHPrepare.cpp +++ b/llvm/lib/CodeGen/WinEHPrepare.cpp @@ -17,7 +17,9 @@  //===----------------------------------------------------------------------===//  #include "llvm/CodeGen/Passes.h" +#include "llvm/ADT/DenseMap.h"  #include "llvm/ADT/MapVector.h" +#include "llvm/ADT/STLExtras.h"  #include "llvm/Analysis/CFG.h"  #include "llvm/Analysis/EHPersonalities.h"  #include "llvm/CodeGen/MachineBasicBlock.h" @@ -436,11 +438,12 @@ void llvm::calculateWinCXXEHStateNumbers(const Function *Fn,    calculateStateNumbersForInvokes(Fn, FuncInfo);  } -static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int ParentState, -                           ClrHandlerType HandlerType, uint32_t TypeToken, -                           const BasicBlock *Handler) { +static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState, +                           int TryParentState, ClrHandlerType HandlerType, +                           uint32_t TypeToken, const BasicBlock *Handler) {    ClrEHUnwindMapEntry Entry; -  Entry.Parent = ParentState; +  Entry.HandlerParentState = HandlerParentState; +  Entry.TryParentState = TryParentState;    Entry.Handler = Handler;    Entry.HandlerType = HandlerType;    Entry.TypeToken = TypeToken; @@ -454,82 +457,199 @@ void llvm::calculateClrEHStateNumbers(const Function *Fn,    if (!FuncInfo.EHPadStateMap.empty())      return; +  // This numbering assigns one state number to each catchpad and cleanuppad. +  // It also computes two tree-like relations over states: +  // 1) Each state has a "HandlerParentState", which is the state of the next +  //    outer handler enclosing this state's handler (same as nearest ancestor +  //    per the ParentPad linkage on EH pads, but skipping over catchswitches). +  // 2) Each state has a "TryParentState", which: +  //    a) for a catchpad that's not the last handler on its catchswitch, is +  //       the state of the next catchpad on that catchswitch +  //    b) for all other pads, is the state of the pad whose try region is the +  //       next outer try region enclosing this state's try region.  The "try +  //       regions are not present as such in the IR, but will be inferred +  //       based on the placement of invokes and pads which reach each other +  //       by exceptional exits +  // Catchswitches do not get their own states, but each gets mapped to the +  // state of its first catchpad. + +  // Step one: walk down from outermost to innermost funclets, assigning each +  // catchpad and cleanuppad a state number.  Add an entry to the +  // ClrEHUnwindMap for each state, recording its HandlerParentState and +  // handler attributes.  Record the TryParentState as well for each catchpad +  // that's not the last on its catchswitch, but initialize all other entries' +  // TryParentStates to a sentinel -1 value that the next pass will update. + +  // Seed a worklist with pads that have no parent.    SmallVector<std::pair<const Instruction *, int>, 8> Worklist; - -  // Each pad needs to be able to refer to its parent, so scan the function -  // looking for top-level handlers and seed the worklist with them.    for (const BasicBlock &BB : *Fn) { -    if (!BB.isEHPad()) -      continue; -    if (BB.isLandingPad()) -      report_fatal_error("CoreCLR EH cannot use landingpads");      const Instruction *FirstNonPHI = BB.getFirstNonPHI(); -    if (!isTopLevelPadForMSVC(FirstNonPHI)) +    const Value *ParentPad; +    if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI)) +      ParentPad = CPI->getParentPad(); +    else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI)) +      ParentPad = CSI->getParentPad(); +    else        continue; -    // queue this with sentinel parent state -1 to mean unwind to caller. -    Worklist.emplace_back(FirstNonPHI, -1); +    if (isa<ConstantTokenNone>(ParentPad)) +      Worklist.emplace_back(FirstNonPHI, -1);    } +  // Use the worklist to visit all pads, from outer to inner.  Record +  // HandlerParentState for all pads.  Record TryParentState only for catchpads +  // that aren't the last on their catchswitch (setting all other entries' +  // TryParentStates to an initial value of -1).  This loop is also responsible +  // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and +  // catchswitches.    while (!Worklist.empty()) {      const Instruction *Pad; -    int ParentState; -    std::tie(Pad, ParentState) = Worklist.pop_back_val(); - -    Value *ParentPad; -    int PredState; -    if (const CleanupPadInst *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { -      // A cleanup can have multiple exits; don't re-process after the first. -      if (FuncInfo.EHPadStateMap.count(Cleanup)) -        continue; -      // CoreCLR personality uses arity to distinguish faults from finallies. -      const BasicBlock *PadBlock = Cleanup->getParent(); +    int HandlerParentState; +    std::tie(Pad, HandlerParentState) = Worklist.pop_back_val(); + +    if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { +      // Create the entry for this cleanup with the appropriate handler +      // properties.  Finaly and fault handlers are distinguished by arity.        ClrHandlerType HandlerType = -          (Cleanup->getNumOperands() ? ClrHandlerType::Fault -                                     : ClrHandlerType::Finally); -      int NewState = -          addClrEHHandler(FuncInfo, ParentState, HandlerType, 0, PadBlock); -      FuncInfo.EHPadStateMap[Cleanup] = NewState; -      // Propagate the new state to all preds of the cleanup -      ParentPad = Cleanup->getParentPad(); -      PredState = NewState; -    } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) { -      SmallVector<const CatchPadInst *, 1> Handlers; -      for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { -        const auto *Catch = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI()); -        Handlers.push_back(Catch); -      } -      FuncInfo.EHPadStateMap[CatchSwitch] = ParentState; -      int NewState = ParentState; -      for (auto HandlerI = Handlers.rbegin(), HandlerE = Handlers.rend(); -           HandlerI != HandlerE; ++HandlerI) { -        const CatchPadInst *Catch = *HandlerI; -        const BasicBlock *PadBlock = Catch->getParent(); +          (Cleanup->getNumArgOperands() ? ClrHandlerType::Fault +                                        : ClrHandlerType::Finally); +      int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1, +                                         HandlerType, 0, Pad->getParent()); +      // Queue any child EH pads on the worklist. +      for (const User *U : Cleanup->users()) +        if (const auto *I = dyn_cast<Instruction>(U)) +          if (I->isEHPad()) +            Worklist.emplace_back(I, CleanupState); +      // Remember this pad's state. +      FuncInfo.EHPadStateMap[Cleanup] = CleanupState; +    } else { +      // Walk the handlers of this catchswitch in reverse order since all but +      // the last need to set the following one as its TryParentState. +      const auto *CatchSwitch = cast<CatchSwitchInst>(Pad); +      int CatchState = -1, FollowerState = -1; +      SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers()); +      for (auto CBI = CatchBlocks.rbegin(), CBE = CatchBlocks.rend(); +           CBI != CBE; ++CBI, FollowerState = CatchState) { +        const BasicBlock *CatchBlock = *CBI; +        // Create the entry for this catch with the appropriate handler +        // properties. +        const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHI());          uint32_t TypeToken = static_cast<uint32_t>(              cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue()); -        NewState = addClrEHHandler(FuncInfo, NewState, ClrHandlerType::Catch, -                                   TypeToken, PadBlock); -        FuncInfo.EHPadStateMap[Catch] = NewState; +        CatchState = +            addClrEHHandler(FuncInfo, HandlerParentState, FollowerState, +                            ClrHandlerType::Catch, TypeToken, CatchBlock); +        // Queue any child EH pads on the worklist. +        for (const User *U : Catch->users()) +          if (const auto *I = dyn_cast<Instruction>(U)) +            if (I->isEHPad()) +              Worklist.emplace_back(I, CatchState); +        // Remember this catch's state. +        FuncInfo.EHPadStateMap[Catch] = CatchState;        } -      for (const auto *CatchPad : Handlers) { -        for (const User *U : CatchPad->users()) { -          const auto *UserI = cast<Instruction>(U); -          if (UserI->isEHPad()) -            Worklist.emplace_back(UserI, ParentState); +      // Associate the catchswitch with the state of its first catch. +      assert(CatchSwitch->getNumHandlers()); +      FuncInfo.EHPadStateMap[CatchSwitch] = CatchState; +    } +  } + +  // Step two: record the TryParentState of each state.  For cleanuppads that +  // don't have cleanuprets, we may need to infer this from their child pads, +  // so visit pads in descendant-most to ancestor-most order. +  for (auto Entry = FuncInfo.ClrEHUnwindMap.rbegin(), +            End = FuncInfo.ClrEHUnwindMap.rend(); +       Entry != End; ++Entry) { +    const Instruction *Pad = +        Entry->Handler.get<const BasicBlock *>()->getFirstNonPHI(); +    // For most pads, the TryParentState is the state associated with the +    // unwind dest of exceptional exits from it. +    const BasicBlock *UnwindDest; +    if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) { +      // If a catch is not the last in its catchswitch, its TryParentState is +      // the state associated with the next catch in the switch, even though +      // that's not the unwind dest of exceptions escaping the catch.  Those +      // cases were already assigned a TryParentState in the first pass, so +      // skip them. +      if (Entry->TryParentState != -1) +        continue; +      // Otherwise, get the unwind dest from the catchswitch. +      UnwindDest = Catch->getCatchSwitch()->getUnwindDest(); +    } else { +      const auto *Cleanup = cast<CleanupPadInst>(Pad); +      UnwindDest = nullptr; +      for (const User *U : Cleanup->users()) { +        if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) { +          // Common and unambiguous case -- cleanupret indicates cleanup's +          // unwind dest. +          UnwindDest = CleanupRet->getUnwindDest(); +          break; +        } + +        // Get an unwind dest for the user +        const BasicBlock *UserUnwindDest = nullptr; +        if (auto *Invoke = dyn_cast<InvokeInst>(U)) { +          UserUnwindDest = Invoke->getUnwindDest(); +        } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) { +          UserUnwindDest = CatchSwitch->getUnwindDest(); +        } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) { +          int UserState = FuncInfo.EHPadStateMap[ChildCleanup]; +          int UserUnwindState = +              FuncInfo.ClrEHUnwindMap[UserState].TryParentState; +          if (UserUnwindState != -1) +            UserUnwindDest = FuncInfo.ClrEHUnwindMap[UserUnwindState] +                                 .Handler.get<const BasicBlock *>();          } + +        // Not having an unwind dest for this user might indicate that it +        // doesn't unwind, so can't be taken as proof that the cleanup itself +        // may unwind to caller (see e.g. SimplifyUnreachable and +        // RemoveUnwindEdge). +        if (!UserUnwindDest) +          continue; + +        // Now we have an unwind dest for the user, but we need to see if it +        // unwinds all the way out of the cleanup or if it stays within it. +        const Instruction *UserUnwindPad = UserUnwindDest->getFirstNonPHI(); +        const Value *UserUnwindParent; +        if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad)) +          UserUnwindParent = CSI->getParentPad(); +        else +          UserUnwindParent = +              cast<CleanupPadInst>(UserUnwindPad)->getParentPad(); + +        // The unwind stays within the cleanup iff it targets a child of the +        // cleanup. +        if (UserUnwindParent == Cleanup) +          continue; + +        // This unwind exits the cleanup, so its dest is the cleanup's dest. +        UnwindDest = UserUnwindDest; +        break;        } -      PredState = NewState; -      ParentPad = CatchSwitch->getParentPad(); -    } else { -      llvm_unreachable("Unexpected EH pad");      } -    // Queue all predecessors with the given state -    for (const BasicBlock *Pred : predecessors(Pad->getParent())) { -      if ((Pred = getEHPadFromPredecessor(Pred, ParentPad))) -        Worklist.emplace_back(Pred->getFirstNonPHI(), PredState); +    // Record the state of the unwind dest as the TryParentState. +    int UnwindDestState; + +    // If UnwindDest is null at this point, either the pad in question can +    // be exited by unwind to caller, or it cannot be exited by unwind.  In +    // either case, reporting such cases as unwinding to caller is correct. +    // This can lead to EH tables that "look strange" -- if this pad's is in +    // a parent funclet which has other children that do unwind to an enclosing +    // pad, the try region for this pad will be missing the "duplicate" EH +    // clause entries that you'd expect to see covering the whole parent.  That +    // should be benign, since the unwind never actually happens.  If it were +    // an issue, we could add a subsequent pass that pushes unwind dests down +    // from parents that have them to children that appear to unwind to caller. +    if (!UnwindDest) { +      UnwindDestState = -1; +    } else { +      UnwindDestState = FuncInfo.EHPadStateMap[UnwindDest->getFirstNonPHI()];      } + +    Entry->TryParentState = UnwindDestState;    } +  // Step three: transfer information from pads to invokes.    calculateStateNumbersForInvokes(Fn, FuncInfo);  } | 

