diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2021-01-27 17:13:10 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2021-01-27 18:32:35 +0300 |
commit | 51a25846c198cff00abad0936f975167357afa6f (patch) | |
tree | 090831097b9df97c7cb22da831d0314660442187 /llvm/lib/CodeGen/SafeStack.cpp | |
parent | 4de3bdd65fb1d8efe6e100b343b076e7ea9840c8 (diff) | |
download | llvm-51a25846c198cff00abad0936f975167357afa6f.zip llvm-51a25846c198cff00abad0936f975167357afa6f.tar.gz llvm-51a25846c198cff00abad0936f975167357afa6f.tar.bz2 |
[CodeGen] SafeStack: preserve DominatorTree if it is avaliable
While this is mostly NFC right now, because only ARM happens
to run this pass with DomTree available before it,
and required after it, more backends will be affected once
the SimplifyCFG's switch for domtree preservation is flipped,
and DwarfEHPrepare also preserves the domtree.
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) |