Skip to content

Commit

Permalink
CorrelatedValuePropagation: Preserve DT.
Browse files Browse the repository at this point in the history
Summary:
We only modify CFG in a couple of places, and we can preserve DT there
with a little effort.

Reviewers: davide, vsk

Subscribers: hiraditya, llvm-commits

Differential Revision: https://reviews.llvm.org/D48059

llvm-svn: 334895
  • Loading branch information
Michael Zolotukhin committed Jun 16, 2018
1 parent b6b7a3b commit 158a7c3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
23 changes: 18 additions & 5 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Expand Up @@ -82,6 +82,7 @@ namespace {
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LazyValueInfoWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addPreserved<DominatorTreeWrapperPass>();
}
};

Expand Down Expand Up @@ -306,7 +307,7 @@ static bool processCmp(CmpInst *C, LazyValueInfo *LVI) {
/// that cannot fire no matter what the incoming edge can safely be removed. If
/// a case fires on every incoming edge then the entire switch can be removed
/// and replaced with a branch to the case destination.
static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {
static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI, DominatorTree *DT) {
Value *Cond = SI->getCondition();
BasicBlock *BB = SI->getParent();

Expand All @@ -321,6 +322,10 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {

// Analyse each switch case in turn.
bool Changed = false;
DenseMap<BasicBlock*, int> SuccessorsCount;
for (auto *Succ : successors(BB))
SuccessorsCount[Succ]++;

for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
ConstantInt *Case = CI->getCaseValue();

Expand Down Expand Up @@ -355,7 +360,8 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {

if (State == LazyValueInfo::False) {
// This case never fires - remove it.
CI->getCaseSuccessor()->removePredecessor(BB);
BasicBlock *Succ = CI->getCaseSuccessor();
Succ->removePredecessor(BB);
CI = SI->removeCase(CI);
CE = SI->case_end();

Expand All @@ -365,6 +371,8 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {

++NumDeadCases;
Changed = true;
if (--SuccessorsCount[Succ] == 0)
DT->deleteEdge(BB, Succ);
continue;
}
if (State == LazyValueInfo::True) {
Expand All @@ -381,10 +389,14 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) {
++CI;
}

if (Changed)
if (Changed) {
// If the switch has been simplified to the point where it can be replaced
// by a branch then do so now.
ConstantFoldTerminator(BB);
DeferredDominance DDT(*DT);
ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
/*TLI = */ nullptr, &DDT);
DDT.flush();
}

return Changed;
}
Expand Down Expand Up @@ -722,7 +734,7 @@ static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT,
Instruction *Term = BB->getTerminator();
switch (Term->getOpcode()) {
case Instruction::Switch:
BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI);
BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
break;
case Instruction::Ret: {
auto *RI = cast<ReturnInst>(Term);
Expand Down Expand Up @@ -767,5 +779,6 @@ CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) {
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserve<GlobalsAA>();
PA.preserve<DominatorTreeAnalysis>();
return PA;
}
1 change: 0 additions & 1 deletion llvm/test/Other/opt-O2-pipeline.ll
Expand Up @@ -145,7 +145,6 @@
; CHECK-NEXT: Lazy Value Information Analysis
; CHECK-NEXT: Jump Threading
; CHECK-NEXT: Value Propagation
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
; CHECK-NEXT: Function Alias Analysis Results
; CHECK-NEXT: Memory Dependence Analysis
Expand Down
1 change: 0 additions & 1 deletion llvm/test/Other/opt-O3-pipeline.ll
Expand Up @@ -149,7 +149,6 @@
; CHECK-NEXT: Lazy Value Information Analysis
; CHECK-NEXT: Jump Threading
; CHECK-NEXT: Value Propagation
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
; CHECK-NEXT: Function Alias Analysis Results
; CHECK-NEXT: Memory Dependence Analysis
Expand Down
1 change: 0 additions & 1 deletion llvm/test/Other/opt-Os-pipeline.ll
Expand Up @@ -131,7 +131,6 @@
; CHECK-NEXT: Lazy Value Information Analysis
; CHECK-NEXT: Jump Threading
; CHECK-NEXT: Value Propagation
; CHECK-NEXT: Dominator Tree Construction
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
; CHECK-NEXT: Function Alias Analysis Results
; CHECK-NEXT: Memory Dependence Analysis
Expand Down

0 comments on commit 158a7c3

Please sign in to comment.