diff options
Diffstat (limited to 'llvm/lib/CodeGen/SafeStack.cpp')
-rw-r--r-- | llvm/lib/CodeGen/SafeStack.cpp | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp index 3179763..3680617 100644 --- a/llvm/lib/CodeGen/SafeStack.cpp +++ b/llvm/lib/CodeGen/SafeStack.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/BranchProbabilityInfo.h" +#include "llvm/Analysis/DomTreeUpdater.h" #include "llvm/Analysis/InlineCost.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/ScalarEvolution.h" @@ -130,6 +131,7 @@ class SafeStack { Function &F; const TargetLoweringBase &TL; const DataLayout &DL; + DomTreeUpdater *DTU; ScalarEvolution &SE; Type *StackPtrTy; @@ -207,8 +209,8 @@ class SafeStack { public: SafeStack(Function &F, const TargetLoweringBase &TL, const DataLayout &DL, - ScalarEvolution &SE) - : F(F), TL(TL), DL(DL), SE(SE), + DomTreeUpdater *DTU, ScalarEvolution &SE) + : F(F), TL(TL), DL(DL), DTU(DTU), SE(SE), StackPtrTy(Type::getInt8PtrTy(F.getContext())), IntPtrTy(DL.getIntPtrType(F.getContext())), Int32Ty(Type::getInt32Ty(F.getContext())), @@ -477,8 +479,7 @@ void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, Instruction &RI, .createBranchWeights(SuccessProb.getNumerator(), FailureProb.getNumerator()); Instruction *CheckTerm = - SplitBlockAndInsertIfThen(Cmp, &RI, - /* Unreachable */ true, Weights); + SplitBlockAndInsertIfThen(Cmp, &RI, /* Unreachable */ true, Weights, DTU); IRBuilder<> IRBFail(CheckTerm); // FIXME: respect -fsanitize-trap / -ftrap-function here? FunctionCallee StackChkFail = @@ -864,6 +865,7 @@ public: AU.addRequired<TargetPassConfig>(); AU.addRequired<TargetLibraryInfoWrapperPass>(); AU.addRequired<AssumptionCacheTracker>(); + AU.addPreserved<DominatorTreeWrapperPass>(); } bool runOnFunction(Function &F) override { @@ -893,15 +895,34 @@ public: // Compute DT and LI only for functions that have the attribute. // This is only useful because the legacy pass manager doesn't let us // compute analyzes lazily. - // In the backend pipeline, nothing preserves DT before SafeStack, so we - // would otherwise always compute it wastefully, even if there is no - // function with the safestack attribute. - DominatorTree DT(F); - LoopInfo LI(DT); - ScalarEvolution SE(F, TLI, ACT, DT, LI); + DominatorTree *DT; + bool ShouldPreserveDominatorTree; + Optional<DominatorTree> LazilyComputedDomTree; + + // Do we already have a DominatorTree avaliable from the previous pass? + // Note that we should *NOT* require it, to avoid the case where we end up + // not needing it, but the legacy PM would have computed it for us anyways. + if (auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>()) { + DT = &DTWP->getDomTree(); + ShouldPreserveDominatorTree = true; + } else { + // Otherwise, we need to compute it. + LazilyComputedDomTree.emplace(F); + DT = LazilyComputedDomTree.getPointer(); + ShouldPreserveDominatorTree = false; + } + + // Likewise, lazily compute loop info. + LoopInfo LI(*DT); + + DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); + + ScalarEvolution SE(F, TLI, ACT, *DT, LI); - return SafeStack(F, *TL, *DL, SE).run(); + return SafeStack(F, *TL, *DL, ShouldPreserveDominatorTree ? &DTU : nullptr, + SE) + .run(); } }; @@ -912,6 +933,7 @@ char SafeStackLegacyPass::ID = 0; INITIALIZE_PASS_BEGIN(SafeStackLegacyPass, DEBUG_TYPE, "Safe Stack instrumentation pass", false, false) INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) +INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) INITIALIZE_PASS_END(SafeStackLegacyPass, DEBUG_TYPE, "Safe Stack instrumentation pass", false, false) |