From 158a7c33231e3072892331d889b8e8bcdae1a1c0 Mon Sep 17 00:00:00 2001 From: Michael Zolotukhin Date: Sat, 16 Jun 2018 18:57:31 +0000 Subject: CorrelatedValuePropagation: Preserve DT. 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 --- .../Scalar/CorrelatedValuePropagation.cpp | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp') diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp index c290f89c9f4..ea148b728a1 100644 --- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp +++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp @@ -82,6 +82,7 @@ namespace { AU.addRequired(); AU.addRequired(); AU.addPreserved(); + AU.addPreserved(); } }; @@ -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(); @@ -321,6 +322,10 @@ static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI) { // Analyse each switch case in turn. bool Changed = false; + DenseMap SuccessorsCount; + for (auto *Succ : successors(BB)) + SuccessorsCount[Succ]++; + for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) { ConstantInt *Case = CI->getCaseValue(); @@ -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(); @@ -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) { @@ -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; } @@ -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(Term), LVI); + BBChanged |= processSwitch(cast(Term), LVI, DT); break; case Instruction::Ret: { auto *RI = cast(Term); @@ -767,5 +779,6 @@ CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) { return PreservedAnalyses::all(); PreservedAnalyses PA; PA.preserve(); + PA.preserve(); return PA; } -- cgit v1.2.3