Index: include/llvm/Analysis/CFG.h =================================================================== --- include/llvm/Analysis/CFG.h +++ include/llvm/Analysis/CFG.h @@ -0,0 +1,58 @@ +//===-- Analysis/CFG.h - BasicBlock Analyses --------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This family of functions performs analyses on basic blocks, and instructions +// contained within basic blocks. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_CFG_H +#define LLVM_ANALYSIS_CFG_H + +#include "llvm/IR/BasicBlock.h" +#include "llvm/Support/CFG.h" + +namespace llvm { + +class BasicBlock; +class DominatorTree; +class Function; +class Instruction; +class LoopInfo; +class TerminatorInst; + +/// Analyze the specified function to find all of the loop backedges in the +/// function and return them. This is a relatively cheap (compared to +/// computing dominators and loop info) analysis. +/// +/// The output is added to Result, as pairs of edge info. +void FindFunctionBackedges(const Function &F, + SmallVectorImpl > &Result); + +/// Search for the specified successor of basic block BB and return its position +/// in the terminator instruction's list of successors. It is an error to call +/// this with a block that is not a successor. +unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ); + +/// Return true if the specified edge is a critical edge. Critical edges are +/// edges from a block with multiple successors to a block with multiple +/// predecessors. +/// +bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, + bool AllowIdenticalEdges = false); + +/// Determine whether there is a path from From to To within a single function. +/// Returns false only if we can prove that once 'From' has been executed then +/// 'To' can not be executed. Conservatively returns true. +bool isPotentiallyReachable(const Instruction *From, const Instruction *To, + DominatorTree *DT = 0, LoopInfo *LI = 0); + +} // End llvm namespace + +#endif Index: include/llvm/Transforms/Utils/BasicBlockUtils.h =================================================================== --- include/llvm/Transforms/Utils/BasicBlockUtils.h +++ include/llvm/Transforms/Utils/BasicBlockUtils.h @@ -70,28 +70,6 @@ // void ReplaceInstWithInst(Instruction *From, Instruction *To); -/// FindFunctionBackedges - Analyze the specified function to find all of the -/// loop backedges in the function and return them. This is a relatively cheap -/// (compared to computing dominators and loop info) analysis. -/// -/// The output is added to Result, as pairs of edge info. -void FindFunctionBackedges(const Function &F, - SmallVectorImpl > &Result); - - -/// GetSuccessorNumber - Search for the specified successor of basic block BB -/// and return its position in the terminator instruction's list of -/// successors. It is an error to call this with a block that is not a -/// successor. -unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ); - -/// isCriticalEdge - Return true if the specified edge is a critical edge. -/// Critical edges are edges from a block with multiple successors to a block -/// with multiple predecessors. -/// -bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, - bool AllowIdenticalEdges = false); - /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to /// split the critical edge. This will update DominatorTree and /// DominatorFrontier information if it is available, thus calling this pass Index: lib/Analysis/AliasAnalysis.cpp =================================================================== --- lib/Analysis/AliasAnalysis.cpp +++ lib/Analysis/AliasAnalysis.cpp @@ -26,6 +26,7 @@ #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/CaptureTracking.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/BasicBlock.h" @@ -361,24 +362,6 @@ } namespace { - // Conservatively return true. Return false, if there is a single path - // starting from "From" and the path does not reach "To". - static bool hasPath(const BasicBlock *From, const BasicBlock *To) { - const unsigned MaxCheck = 5; - const BasicBlock *Current = From; - for (unsigned I = 0; I < MaxCheck; I++) { - unsigned NumSuccs = Current->getTerminator()->getNumSuccessors(); - if (NumSuccs > 1) - return true; - if (NumSuccs == 0) - return false; - Current = Current->getTerminator()->getSuccessor(0); - if (Current == To) - return true; - } - return true; - } - /// Only find pointer captures which happen before the given instruction. Uses /// the dominator tree to determine whether one instruction is before another. /// Only support the case where the Value is defined in the same basic block @@ -400,7 +383,7 @@ // there is no need to explore the use if BeforeHere dominates use. // Check whether there is a path from I to BeforeHere. if (BeforeHere != I && DT->dominates(BeforeHere, I) && - !hasPath(BB, BeforeHere->getParent())) + !isPotentiallyReachable(I, BeforeHere, DT)) return false; return true; } @@ -412,7 +395,7 @@ if (BeforeHere != I && !DT->isReachableFromEntry(BB)) return false; if (BeforeHere != I && DT->dominates(BeforeHere, I) && - !hasPath(BB, BeforeHere->getParent())) + !isPotentiallyReachable(I, BeforeHere, DT)) return false; Captured = true; return true; Index: lib/Analysis/CFG.cpp =================================================================== --- lib/Analysis/CFG.cpp +++ lib/Analysis/CFG.cpp @@ -0,0 +1,214 @@ +//===-- CFG.cpp - BasicBlock analysis --------------------------------------==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This family of functions performs analyses on basic blocks, and instructions +// contained within basic blocks. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/CFG.h" + +#include "llvm/ADT/SmallSet.h" +#include "llvm/Analysis/Dominators.h" +#include "llvm/Analysis/LoopInfo.h" + +using namespace llvm; + +/// FindFunctionBackedges - Analyze the specified function to find all of the +/// loop backedges in the function and return them. This is a relatively cheap +/// (compared to computing dominators and loop info) analysis. +/// +/// The output is added to Result, as pairs of edge info. +void llvm::FindFunctionBackedges(const Function &F, + SmallVectorImpl > &Result) { + const BasicBlock *BB = &F.getEntryBlock(); + if (succ_begin(BB) == succ_end(BB)) + return; + + SmallPtrSet Visited; + SmallVector, 8> VisitStack; + SmallPtrSet InStack; + + Visited.insert(BB); + VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); + InStack.insert(BB); + do { + std::pair &Top = VisitStack.back(); + const BasicBlock *ParentBB = Top.first; + succ_const_iterator &I = Top.second; + + bool FoundNew = false; + while (I != succ_end(ParentBB)) { + BB = *I++; + if (Visited.insert(BB)) { + FoundNew = true; + break; + } + // Successor is in VisitStack, it's a back edge. + if (InStack.count(BB)) + Result.push_back(std::make_pair(ParentBB, BB)); + } + + if (FoundNew) { + // Go down one level if there is a unvisited successor. + InStack.insert(BB); + VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); + } else { + // Go up one level. + InStack.erase(VisitStack.pop_back_val().first); + } + } while (!VisitStack.empty()); +} + +/// GetSuccessorNumber - Search for the specified successor of basic block BB +/// and return its position in the terminator instruction's list of +/// successors. It is an error to call this with a block that is not a +/// successor. +unsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) { + TerminatorInst *Term = BB->getTerminator(); +#ifndef NDEBUG + unsigned e = Term->getNumSuccessors(); +#endif + for (unsigned i = 0; ; ++i) { + assert(i != e && "Didn't find edge?"); + if (Term->getSuccessor(i) == Succ) + return i; + } +} + +/// isCriticalEdge - Return true if the specified edge is a critical edge. +/// Critical edges are edges from a block with multiple successors to a block +/// with multiple predecessors. +bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, + bool AllowIdenticalEdges) { + assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!"); + if (TI->getNumSuccessors() == 1) return false; + + const BasicBlock *Dest = TI->getSuccessor(SuccNum); + const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest); + + // If there is more than one predecessor, this is a critical edge... + assert(I != E && "No preds, but we have an edge to the block?"); + const BasicBlock *FirstPred = *I; + ++I; // Skip one edge due to the incoming arc from TI. + if (!AllowIdenticalEdges) + return I != E; + + // If AllowIdenticalEdges is true, then we allow this edge to be considered + // non-critical iff all preds come from TI's block. + while (I != E) { + const BasicBlock *P = *I; + if (P != FirstPred) + return true; + // Note: leave this as is until no one ever compiles with either gcc 4.0.1 + // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207 + E = pred_end(P); + ++I; + } + return false; +} + +// LoopInfo contains a mapping from basic block to the innermost loop. Find +// the outermost loop in the loop nest that contains BB. +static const Loop *getOutermostLoop(LoopInfo *LI, const BasicBlock *BB) { + const Loop *L = LI->getLoopFor(BB); + if (L) { + while (const Loop *Parent = L->getParentLoop()) + L = Parent; + } + return L; +} + +// True if there is a loop which contains both BB1 and BB2. +static bool loopContainsBoth(LoopInfo *LI, + const BasicBlock *BB1, const BasicBlock *BB2) { + const Loop *L1 = getOutermostLoop(LI, BB1); + const Loop *L2 = getOutermostLoop(LI, BB2); + return L1 != NULL && L1 == L2; +} + +/// Determine whether there is a path from From to To within a single function. +/// Returns false only if we can prove that once 'From' has been executed then +/// 'To' can not be executed. Conservatively returns true. +bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B, + DominatorTree *DT, LoopInfo *LI) { + assert(A->getParent()->getParent() == B->getParent()->getParent() && + "This analysis is function-local!"); + + if (A->getParent() == B->getParent()) { + // The same block case is special because it's the only time we're looking + // within a single block to see which comes first. Once we start looking at + // multiple blocks, the first instruction of the block is reachable, so we + // only need to determine reachability between whole blocks. + + const BasicBlock *BB = A->getParent(); + // Can't be in a loop if it's the entry block -- the entry block may not + // have predecessors. + bool HasLoop = BB != &BB->getParent()->getEntryBlock(); + + // Can't be in a loop if LoopInfo doesn't know about it. + if (LI && HasLoop) { + HasLoop = LI->getLoopFor(BB) != 0; + } + if (HasLoop) return true; + + // Linear scan, start at 'A', see whether we hit 'B' or the end first. + for (BasicBlock::const_iterator BBI = A, BBE = BB->end(); BBI != BBE; ++BBI) + if (&*BBI == B) return true; + return false; + } + + if (A->getParent() == &A->getParent()->getParent()->getEntryBlock()) + return true; + if (B->getParent() == &A->getParent()->getParent()->getEntryBlock()) + return false; + + unsigned Limit = 32; + SmallSet EverQueued; + SmallVector WorkList; + WorkList.push_back(A->getParent()); + const BasicBlock *StopBB = B->getParent(); + EverQueued.insert(WorkList.back()); + + // When the stop block is unreachable, it's dominated from everywhere, + // regardless of whether there's a path between the two blocks. + if (DT && !DT->isReachableFromEntry(StopBB)) + DT = NULL; + + do { + const BasicBlock *BB = WorkList.pop_back_val(); + if (BB == StopBB) + return true; + if (DT && DT->dominates(BB, StopBB)) + return true; + if (LI && loopContainsBoth(LI, BB, StopBB)) + return true; + + if (!--Limit) return true; + + if (const Loop *Outer = LI ? getOutermostLoop(LI, BB) : 0) { + // All blocks in a single loop are reachable from all other blocks. From + // any of these blocks, we can skip directly to the exits of the loop, + // ignoring any other blocks inside the loop body. + SmallVector Exits; + Outer->getExitBlocks(Exits); + for (unsigned i = 0, e = Exits.size(); i != e; ++i) { + if (EverQueued.insert(Exits[i])) + WorkList.push_back(Exits[i]); + } + } else { + for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I != E; + ++I) { + if (EverQueued.insert(*I)) + WorkList.push_back(*I); + } + } + } while (!WorkList.empty()); + return false; +} Index: lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/BranchProbabilityInfo.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/CodeGen/FastISel.h" #include "llvm/CodeGen/FunctionLoweringInfo.h" Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/InstructionSimplify.h" Index: lib/Transforms/Scalar/JumpThreading.cpp =================================================================== --- lib/Transforms/Scalar/JumpThreading.cpp +++ lib/Transforms/Scalar/JumpThreading.cpp @@ -19,6 +19,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/LazyValueInfo.h" @@ -1614,5 +1615,3 @@ ++NumDupes; return true; } - - Index: lib/Transforms/Utils/BasicBlockUtils.cpp =================================================================== --- lib/Transforms/Utils/BasicBlockUtils.cpp +++ lib/Transforms/Utils/BasicBlockUtils.cpp @@ -14,6 +14,7 @@ #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/MemoryDependenceAnalysis.h" @@ -235,22 +236,6 @@ ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); } -/// GetSuccessorNumber - Search for the specified successor of basic block BB -/// and return its position in the terminator instruction's list of -/// successors. It is an error to call this with a block that is not a -/// successor. -unsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) { - TerminatorInst *Term = BB->getTerminator(); -#ifndef NDEBUG - unsigned e = Term->getNumSuccessors(); -#endif - for (unsigned i = 0; ; ++i) { - assert(i != e && "Didn't find edge?"); - if (Term->getSuccessor(i) == Succ) - return i; - } -} - /// SplitEdge - Split the edge connecting specified block. Pass P must /// not be NULL. BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { @@ -598,52 +583,6 @@ } } -/// FindFunctionBackedges - Analyze the specified function to find all of the -/// loop backedges in the function and return them. This is a relatively cheap -/// (compared to computing dominators and loop info) analysis. -/// -/// The output is added to Result, as pairs of edge info. -void llvm::FindFunctionBackedges(const Function &F, - SmallVectorImpl > &Result) { - const BasicBlock *BB = &F.getEntryBlock(); - if (succ_begin(BB) == succ_end(BB)) - return; - - SmallPtrSet Visited; - SmallVector, 8> VisitStack; - SmallPtrSet InStack; - - Visited.insert(BB); - VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); - InStack.insert(BB); - do { - std::pair &Top = VisitStack.back(); - const BasicBlock *ParentBB = Top.first; - succ_const_iterator &I = Top.second; - - bool FoundNew = false; - while (I != succ_end(ParentBB)) { - BB = *I++; - if (Visited.insert(BB)) { - FoundNew = true; - break; - } - // Successor is in VisitStack, it's a back edge. - if (InStack.count(BB)) - Result.push_back(std::make_pair(ParentBB, BB)); - } - - if (FoundNew) { - // Go down one level if there is a unvisited successor. - InStack.insert(BB); - VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); - } else { - // Go up one level. - InStack.erase(VisitStack.pop_back_val().first); - } - } while (!VisitStack.empty()); -} - /// FoldReturnIntoUncondBranch - This method duplicates the specified return /// instruction into a predecessor which ends in an unconditional branch. If /// the return instruction returns a value defined by a PHI, propagate the Index: lib/Transforms/Utils/BreakCriticalEdges.cpp =================================================================== --- lib/Transforms/Utils/BreakCriticalEdges.cpp +++ lib/Transforms/Utils/BreakCriticalEdges.cpp @@ -19,6 +19,7 @@ #include "llvm/Transforms/Scalar.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/ProfileInfo.h" @@ -84,39 +85,6 @@ // Implementation of the external critical edge manipulation functions //===----------------------------------------------------------------------===// -// isCriticalEdge - Return true if the specified edge is a critical edge. -// Critical edges are edges from a block with multiple successors to a block -// with multiple predecessors. -// -bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, - bool AllowIdenticalEdges) { - assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!"); - if (TI->getNumSuccessors() == 1) return false; - - const BasicBlock *Dest = TI->getSuccessor(SuccNum); - const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest); - - // If there is more than one predecessor, this is a critical edge... - assert(I != E && "No preds, but we have an edge to the block?"); - const BasicBlock *FirstPred = *I; - ++I; // Skip one edge due to the incoming arc from TI. - if (!AllowIdenticalEdges) - return I != E; - - // If AllowIdenticalEdges is true, then we allow this edge to be considered - // non-critical iff all preds come from TI's block. - while (I != E) { - const BasicBlock *P = *I; - if (P != FirstPred) - return true; - // Note: leave this as is until no one ever compiles with either gcc 4.0.1 - // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207 - E = pred_end(P); - ++I; - } - return false; -} - /// createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form /// may require new PHIs in the new exit block. This function inserts the /// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB Index: lib/Transforms/Utils/DemoteRegToStack.cpp =================================================================== --- lib/Transforms/Utils/DemoteRegToStack.cpp +++ lib/Transforms/Utils/DemoteRegToStack.cpp @@ -10,6 +10,7 @@ #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/Analysis/CFG.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Type.h" Index: unittests/Analysis/CFGTest.cpp =================================================================== --- unittests/Analysis/CFGTest.cpp +++ unittests/Analysis/CFGTest.cpp @@ -0,0 +1,347 @@ +//===- CFGTest.cpp - CFG tests --------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/CFG.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Analysis/Dominators.h" +#include "llvm/Analysis/LoopInfo.h" +#include "llvm/Assembly/Parser.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/InstIterator.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Pass.h" +#include "llvm/PassManager.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +// This fixture assists in running the isPotentiallyReachable utility four ways +// and ensuring it produces the correct answer each time. +class IsPotentiallyReachableTest : public testing::Test { +protected: + void ParseAssembly(const char *Assembly) { + M.reset(new Module("Module", getGlobalContext())); + + SMDiagnostic Error; + bool Parsed = ParseAssemblyString(Assembly, M.get(), + Error, M->getContext()) == M.get(); + + std::string errMsg; + raw_string_ostream os(errMsg); + Error.print("", os); + + if (!Parsed) { + // A failure here means that the test itself is buggy. + report_fatal_error(os.str().c_str()); + } + + Function *F = M->getFunction("test"); + if (F == NULL) + report_fatal_error("Test must have a function named @test"); + + A = B = NULL; + for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) { + if (I->hasName()) { + if (I->getName() == "A") + A = &*I; + else if (I->getName() == "B") + B = &*I; + } + } + if (A == NULL) + report_fatal_error("@test must have an instruction %A"); + if (B == NULL) + report_fatal_error("@test must have an instruction %B"); + } + + void ExpectPath(bool ExpectedResult) { + static char ID; + class IsPotentiallyReachableTestPass : public FunctionPass { + public: + IsPotentiallyReachableTestPass(bool ExpectedResult, + Instruction *A, Instruction *B) + : FunctionPass(ID), ExpectedResult(ExpectedResult), A(A), B(B) {} + + void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + AU.addRequired(); + AU.addRequired(); + } + + bool runOnFunction(Function &F) { + if (!F.hasName() || F.getName() != "test") + return false; + + LoopInfo *LI = &getAnalysis(); + DominatorTree *DT = &getAnalysis(); + EXPECT_EQ(isPotentiallyReachable(A, B, 0, 0), ExpectedResult); + EXPECT_EQ(isPotentiallyReachable(A, B, DT, 0), ExpectedResult); + EXPECT_EQ(isPotentiallyReachable(A, B, 0, LI), ExpectedResult); + EXPECT_EQ(isPotentiallyReachable(A, B, DT, LI), ExpectedResult); + return false; + } + bool ExpectedResult; + Instruction *A, *B; + }; + + IsPotentiallyReachableTestPass *P = + new IsPotentiallyReachableTestPass(ExpectedResult, A, B); + PassManager PM; + PM.add(P); + PM.run(*M); + } +private: + OwningPtr M; + Instruction *A, *B; +}; + +} + +TEST_F(IsPotentiallyReachableTest, SameBlockNoPath) { + ParseAssembly( + "define void @test() {\n" + "entry:\n" + " bitcast i8 undef to i8\n" + " %B = bitcast i8 undef to i8\n" + " bitcast i8 undef to i8\n" + " bitcast i8 undef to i8\n" + " %A = bitcast i8 undef to i8\n" + " ret void\n" + "}\n"); + ExpectPath(false); +} + +TEST_F(IsPotentiallyReachableTest, SameBlockPath) { + ParseAssembly( + "define void @test() {\n" + "entry:\n" + " %A = bitcast i8 undef to i8\n" + " bitcast i8 undef to i8\n" + " bitcast i8 undef to i8\n" + " %B = bitcast i8 undef to i8\n" + " ret void\n" + "}\n"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, StraightNoPath) { + ParseAssembly( + "define void @test() {\n" + "entry:\n" + " %B = bitcast i8 undef to i8\n" + " br label %exit\n" + "exit:\n" + " %A = bitcast i8 undef to i8\n" + " ret void\n" + "}"); + ExpectPath(false); +} + +TEST_F(IsPotentiallyReachableTest, StraightPath) { + ParseAssembly( + "define void @test() {\n" + "entry:\n" + " %A = bitcast i8 undef to i8\n" + " br label %exit\n" + "exit:\n" + " %B = bitcast i8 undef to i8\n" + " ret void\n" + "}"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, DestUnreachable) { + ParseAssembly( + "define void @test() {\n" + "entry:\n" + " br label %midblock\n" + "midblock:\n" + " %A = bitcast i8 undef to i8\n" + " ret void\n" + "unreachable:\n" + " %B = bitcast i8 undef to i8\n" + " br label %midblock\n" + "}"); + ExpectPath(false); +} + +TEST_F(IsPotentiallyReachableTest, BranchToReturn) { + ParseAssembly( + "define void @test(i1 %x) {\n" + "entry:\n" + " %A = bitcast i8 undef to i8\n" + " br i1 %x, label %block1, label %block2\n" + "block1:\n" + " ret void\n" + "block2:\n" + " %B = bitcast i8 undef to i8\n" + " ret void\n" + "}"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, SimpleLoop1) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %loop\n" + "loop:\n" + " %B = bitcast i8 undef to i8\n" + " %A = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %loop, label %exit\n" + "exit:\n" + " ret void\n" + "}"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, SimpleLoop2) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " %B = bitcast i8 undef to i8\n" + " br label %loop\n" + "loop:\n" + " %A = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %loop, label %exit\n" + "exit:\n" + " ret void\n" + "}"); + ExpectPath(false); +} + +TEST_F(IsPotentiallyReachableTest, SimpleLoop3) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %loop\n" + "loop:\n" + " %B = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %loop, label %exit\n" + "exit:\n" + " %A = bitcast i8 undef to i8\n" + " ret void\n" + "}"); + ExpectPath(false); +} + + +TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther1) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %loop1\n" + "loop1:\n" + " %A = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %loop1, label %loop1exit\n" + "loop1exit:\n" + " br label %loop2\n" + "loop2:\n" + " %B = bitcast i8 undef to i8\n" + " %y = call i1 @switch()\n" + " br i1 %x, label %loop2, label %loop2exit\n" + "loop2exit:" + " ret void\n" + "}"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOther2) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %loop1\n" + "loop1:\n" + " %B = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %loop1, label %loop1exit\n" + "loop1exit:\n" + " br label %loop2\n" + "loop2:\n" + " %A = bitcast i8 undef to i8\n" + " %y = call i1 @switch()\n" + " br i1 %x, label %loop2, label %loop2exit\n" + "loop2exit:" + " ret void\n" + "}"); + ExpectPath(false); +} + +TEST_F(IsPotentiallyReachableTest, OneLoopAfterTheOtherInsideAThirdLoop) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %outerloop3\n" + "outerloop3:\n" + " br label %innerloop1\n" + "innerloop1:\n" + " %B = bitcast i8 undef to i8\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %innerloop1, label %innerloop1exit\n" + "innerloop1exit:\n" + " br label %innerloop2\n" + "innerloop2:\n" + " %A = bitcast i8 undef to i8\n" + " %y = call i1 @switch()\n" + " br i1 %x, label %innerloop2, label %innerloop2exit\n" + "innerloop2exit:" + " ;; In outer loop3 now.\n" + " %z = call i1 @switch()\n" + " br i1 %z, label %outerloop3, label %exit\n" + "exit:\n" + " ret void\n" + "}"); + ExpectPath(true); +} + +TEST_F(IsPotentiallyReachableTest, BranchInsideLoop) { + ParseAssembly( + "declare i1 @switch()\n" + "\n" + "define void @test() {\n" + "entry:\n" + " br label %loop\n" + "loop:\n" + " %x = call i1 @switch()\n" + " br i1 %x, label %nextloopblock, label %exit\n" + "nextloopblock:\n" + " %y = call i1 @switch()\n" + " br i1 %y, label %left, label %right\n" + "left:\n" + " %A = bitcast i8 undef to i8\n" + " br label %loop\n" + "right:\n" + " %B = bitcast i8 undef to i8\n" + " br label %loop\n" + "exit:\n" + " ret void\n" + "}"); + ExpectPath(true); +}