-static bool
-allPredsCameFromLandingPad(BasicBlock *BB,
- SmallSet<BasicBlock *, 4> &VisitedBlocks) {
- VisitedBlocks.insert(BB);
- if (BB->isLandingPad())
- return true;
- // If we find a block with no predecessors, the search failed.
- if (pred_empty(BB))
- return false;
- for (BasicBlock *Pred : predecessors(BB)) {
- if (VisitedBlocks.count(Pred))
- continue;
- if (!allPredsCameFromLandingPad(Pred, VisitedBlocks))
- return false;
- }
- return true;
-}
-
-static bool
-allSuccessorsReachEndCatch(BasicBlock *BB, BasicBlock::iterator InstBegin,
- IntrinsicInst **SecondBeginCatch,
- SmallSet<BasicBlock *, 4> &VisitedBlocks) {
- VisitedBlocks.insert(BB);
- for (BasicBlock::iterator I = InstBegin, E = BB->end(); I != E; ++I) {
- IntrinsicInst *IC = dyn_cast<IntrinsicInst>(I);
- if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch)
- return true;
- // If we find another begincatch while looking for an endcatch,
- // that's also an error.
- if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch) {
- *SecondBeginCatch = IC;
- return false;
- }
- }
-
- // If we reach a block with no successors while searching, the
- // search has failed.
- if (succ_empty(BB))
- return false;
- // Otherwise, search all of the successors.
- for (BasicBlock *Succ : successors(BB)) {
- if (VisitedBlocks.count(Succ))
- continue;
- if (!allSuccessorsReachEndCatch(Succ, Succ->begin(), SecondBeginCatch,
- VisitedBlocks))
- return false;
- }
- return true;
-}
-
-void Lint::visitEHBeginCatch(IntrinsicInst *II) {
- // The checks in this function make a potentially dubious assumption about
- // the CFG, namely that any block involved in a catch is only used for the
- // catch. This will very likely be true of IR generated by a front end,
- // but it may cease to be true, for example, if the IR is run through a
- // pass which combines similar blocks.
- //
- // In general, if we encounter a block the isn't dominated by the catch
- // block while we are searching the catch block's successors for a call
- // to end catch intrinsic, then it is possible that it will be legal for
- // a path through this block to never reach a call to llvm.eh.endcatch.
- // An analogous statement could be made about our search for a landing
- // pad among the catch block's predecessors.
- //
- // What is actually required is that no path is possible at runtime that
- // reaches a call to llvm.eh.begincatch without having previously visited
- // a landingpad instruction and that no path is possible at runtime that
- // calls llvm.eh.begincatch and does not subsequently call llvm.eh.endcatch
- // (mentally adjusting for the fact that in reality these calls will be
- // removed before code generation).
- //
- // Because this is a lint check, we take a pessimistic approach and warn if
- // the control flow is potentially incorrect.
-
- SmallSet<BasicBlock *, 4> VisitedBlocks;
- BasicBlock *CatchBB = II->getParent();
-
- // The begin catch must occur in a landing pad block or all paths
- // to it must have come from a landing pad.
- Assert(allPredsCameFromLandingPad(CatchBB, VisitedBlocks),
- "llvm.eh.begincatch may be reachable without passing a landingpad",
- II);
-
- // Reset the visited block list.
- VisitedBlocks.clear();
-
- IntrinsicInst *SecondBeginCatch = nullptr;
-
- // This has to be called before it is asserted. Otherwise, the first assert
- // below can never be hit.
- bool EndCatchFound = allSuccessorsReachEndCatch(
- CatchBB, std::next(static_cast<BasicBlock::iterator>(II)),
- &SecondBeginCatch, VisitedBlocks);
- Assert(
- SecondBeginCatch == nullptr,
- "llvm.eh.begincatch may be called a second time before llvm.eh.endcatch",
- II, SecondBeginCatch);
- Assert(EndCatchFound,
- "Some paths from llvm.eh.begincatch may not reach llvm.eh.endcatch",
- II);
-}
-
-static bool allPredCameFromBeginCatch(
- BasicBlock *BB, BasicBlock::reverse_iterator InstRbegin,
- IntrinsicInst **SecondEndCatch, SmallSet<BasicBlock *, 4> &VisitedBlocks) {
- VisitedBlocks.insert(BB);
- // Look for a begincatch in this block.
- for (BasicBlock::reverse_iterator RI = InstRbegin, RE = BB->rend(); RI != RE;
- ++RI) {
- IntrinsicInst *IC = dyn_cast<IntrinsicInst>(&*RI);
- if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch)
- return true;
- // If we find another end catch before we find a begin catch, that's
- // an error.
- if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch) {
- *SecondEndCatch = IC;
- return false;
- }
- // If we encounter a landingpad instruction, the search failed.
- if (isa<LandingPadInst>(*RI))
- return false;
- }
- // If while searching we find a block with no predeccesors,
- // the search failed.
- if (pred_empty(BB))
- return false;
- // Search any predecessors we haven't seen before.
- for (BasicBlock *Pred : predecessors(BB)) {
- if (VisitedBlocks.count(Pred))
- continue;
- if (!allPredCameFromBeginCatch(Pred, Pred->rbegin(), SecondEndCatch,
- VisitedBlocks))
- return false;
- }
- return true;
-}
-
-void Lint::visitEHEndCatch(IntrinsicInst *II) {
- // The check in this function makes a potentially dubious assumption about
- // the CFG, namely that any block involved in a catch is only used for the
- // catch. This will very likely be true of IR generated by a front end,
- // but it may cease to be true, for example, if the IR is run through a
- // pass which combines similar blocks.
- //
- // In general, if we encounter a block the isn't post-dominated by the
- // end catch block while we are searching the end catch block's predecessors
- // for a call to the begin catch intrinsic, then it is possible that it will
- // be legal for a path to reach the end catch block without ever having
- // called llvm.eh.begincatch.
- //
- // What is actually required is that no path is possible at runtime that
- // reaches a call to llvm.eh.endcatch without having previously visited
- // a call to llvm.eh.begincatch (mentally adjusting for the fact that in
- // reality these calls will be removed before code generation).
- //
- // Because this is a lint check, we take a pessimistic approach and warn if
- // the control flow is potentially incorrect.
-
- BasicBlock *EndCatchBB = II->getParent();
-
- // Alls paths to the end catch call must pass through a begin catch call.
-
- // If llvm.eh.begincatch wasn't called in the current block, we'll use this
- // lambda to recursively look for it in predecessors.
- SmallSet<BasicBlock *, 4> VisitedBlocks;
- IntrinsicInst *SecondEndCatch = nullptr;
-
- // This has to be called before it is asserted. Otherwise, the first assert
- // below can never be hit.
- bool BeginCatchFound =
- allPredCameFromBeginCatch(EndCatchBB, BasicBlock::reverse_iterator(II),
- &SecondEndCatch, VisitedBlocks);
- Assert(
- SecondEndCatch == nullptr,
- "llvm.eh.endcatch may be called a second time after llvm.eh.begincatch",
- II, SecondEndCatch);
- Assert(BeginCatchFound,
- "llvm.eh.endcatch may be reachable without passing llvm.eh.begincatch",
- II);
-}
-