From 8ceab61c75b8f34c90e831c85a3420e3510b501a Mon Sep 17 00:00:00 2001 From: David Green Date: Mon, 21 May 2018 11:06:28 +0000 Subject: [PATCH] [CVP] Require DomTree for new Pass Manager We were previously using a DT in CVP through SimplifyQuery, but not requiring it in the new pass manager. Hence it would crash if DT was not already available. This now gets DT directly and plumbs it through to where it is used (instead of using it through SQ). llvm-svn: 332836 --- .../Scalar/CorrelatedValuePropagation.cpp | 22 +++++++------ .../phi-common-val.ll | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp index 15df482652c0..5cca790e907d 100644 --- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp +++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp @@ -136,7 +136,7 @@ static bool processSelect(SelectInst *S, LazyValueInfo *LVI) { /// --> /// %r = %x static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI, - const SimplifyQuery &SQ) { + DominatorTree *DT) { // Collect incoming constants and initialize possible common value. SmallVector, 4> IncomingConstants; Value *CommonValue = nullptr; @@ -159,7 +159,7 @@ static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI, // The common value must be valid in all incoming blocks. BasicBlock *ToBB = P->getParent(); if (auto *CommonInst = dyn_cast(CommonValue)) - if (!SQ.DT->dominates(CommonInst, ToBB)) + if (!DT->dominates(CommonInst, ToBB)) return false; // We have a phi with exactly 1 variable incoming value and 1 or more constant @@ -180,7 +180,7 @@ static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI, return true; } -static bool processPHI(PHINode *P, LazyValueInfo *LVI, +static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ) { bool Changed = false; @@ -242,7 +242,7 @@ static bool processPHI(PHINode *P, LazyValueInfo *LVI, } if (!Changed) - Changed = simplifyCommonValuePhi(P, LVI, SQ); + Changed = simplifyCommonValuePhi(P, LVI, DT); if (Changed) ++NumPhis; @@ -669,7 +669,8 @@ static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) { ConstantInt::getFalse(C->getContext()); } -static bool runImpl(Function &F, LazyValueInfo *LVI, const SimplifyQuery &SQ) { +static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT, + const SimplifyQuery &SQ) { bool FnChanged = false; // Visiting in a pre-order depth-first traversal causes us to simplify early // blocks before querying later blocks (which require us to analyze early @@ -685,7 +686,7 @@ static bool runImpl(Function &F, LazyValueInfo *LVI, const SimplifyQuery &SQ) { BBChanged |= processSelect(cast(II), LVI); break; case Instruction::PHI: - BBChanged |= processPHI(cast(II), LVI, SQ); + BBChanged |= processPHI(cast(II), LVI, DT, SQ); break; case Instruction::ICmp: case Instruction::FCmp: @@ -750,14 +751,17 @@ bool CorrelatedValuePropagation::runOnFunction(Function &F) { return false; LazyValueInfo *LVI = &getAnalysis().getLVI(); - return runImpl(F, LVI, getBestSimplifyQuery(*this, F)); + DominatorTree *DT = &getAnalysis().getDomTree(); + + return runImpl(F, LVI, DT, getBestSimplifyQuery(*this, F)); } PreservedAnalyses CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) { - LazyValueInfo *LVI = &AM.getResult(F); - bool Changed = runImpl(F, LVI, getBestSimplifyQuery(AM, F)); + DominatorTree *DT = &AM.getResult(F); + + bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F)); if (!Changed) return PreservedAnalyses::all(); diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll b/llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll index 3011fef9749a..b761fff0031e 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll @@ -1,5 +1,6 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt < %s -correlated-propagation -S | FileCheck %s +; RUN: opt < %s -passes="correlated-propagation" -S | FileCheck %s define i8* @simplify_phi_common_value_op0(i8* %ptr, i32* %b) { ; CHECK-LABEL: @simplify_phi_common_value_op0( @@ -92,3 +93,33 @@ return: ret i8 %r } +define i8* @simplify_phi_common_value_from_instruction(i8* %ptr_op, i32* %b, i32 %i) { +; CHECK-LABEL: @simplify_phi_common_value_from_instruction( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[PTR:%.*]] = getelementptr i8, i8* [[PTR_OP:%.*]], i32 [[I:%.*]] +; CHECK-NEXT: [[ISNULL:%.*]] = icmp eq i8* [[PTR]], null +; CHECK-NEXT: br i1 [[ISNULL]], label [[RETURN:%.*]], label [[ELSE:%.*]] +; CHECK: else: +; CHECK-NEXT: [[LB:%.*]] = load i32, i32* [[B:%.*]] +; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[LB]], 1 +; CHECK-NEXT: store i32 [[ADD]], i32* [[B]] +; CHECK-NEXT: br label [[RETURN]] +; CHECK: return: +; CHECK-NEXT: ret i8* [[PTR]] +; +entry: + %ptr = getelementptr i8, i8* %ptr_op, i32 %i + %isnull = icmp eq i8* %ptr, null + br i1 %isnull, label %return, label %else + +else: + %lb = load i32, i32* %b + %add = add nsw i32 %lb, 1 + store i32 %add, i32* %b + br label %return + +return: + %r = phi i8* [ %ptr, %else ], [ null, %entry ] + ret i8* %r +} +