[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.
This commit is contained in:
Roman Lebedev 2021-01-27 17:13:10 +03:00
parent 4de3bdd65f
commit 51a25846c1
No known key found for this signature in database
GPG Key ID: 083C3EBB4A1689E0
2 changed files with 33 additions and 12 deletions

View File

@ -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;
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<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, 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)

View File

@ -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