From 51a25846c198cff00abad0936f975167357afa6f Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Wed, 27 Jan 2021 17:13:10 +0300 Subject: [PATCH] [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. --- llvm/lib/CodeGen/SafeStack.cpp | 44 +++++++++++++++++++++------- llvm/test/CodeGen/ARM/O3-pipeline.ll | 1 - 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/llvm/lib/CodeGen/SafeStack.cpp b/llvm/lib/CodeGen/SafeStack.cpp index 31797631c97b..368061740c4b 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(); AU.addRequired(); AU.addRequired(); + AU.addPreserved(); } 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 LazilyComputedDomTree; - return SafeStack(F, *TL, *DL, SE).run(); + // 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()) { + 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, 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) diff --git a/llvm/test/CodeGen/ARM/O3-pipeline.ll b/llvm/test/CodeGen/ARM/O3-pipeline.ll index b4f52797f461..8011ad797b0c 100644 --- a/llvm/test/CodeGen/ARM/O3-pipeline.ll +++ b/llvm/test/CodeGen/ARM/O3-pipeline.ll @@ -68,7 +68,6 @@ ; CHECK-NEXT: Safe Stack instrumentation pass ; CHECK-NEXT: Insert stack protectors ; CHECK-NEXT: Module Verifier -; CHECK-NEXT: Dominator Tree Construction ; CHECK-NEXT: Basic Alias Analysis (stateless AA impl) ; CHECK-NEXT: Function Alias Analysis Results ; CHECK-NEXT: Natural Loop Information