From: Daniel Berlin Date: Sat, 18 Mar 2017 15:41:36 +0000 (+0000) Subject: NewGVN: Fix PHI evaluation bug exposed by new verifier. We were checking whether... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=174f2f4bda045981a41e75183db17ea4e7049ae5;p=llvm NewGVN: Fix PHI evaluation bug exposed by new verifier. We were checking whether the incoming block was reachable instead of whether the specific edge was reachable git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298187 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/NewGVN.cpp b/lib/Transforms/Scalar/NewGVN.cpp index a637160b8e6..0fa9b1d714a 100644 --- a/lib/Transforms/Scalar/NewGVN.cpp +++ b/lib/Transforms/Scalar/NewGVN.cpp @@ -444,7 +444,7 @@ PHIExpression *NewGVN::createPHIExpression(Instruction *I) { // Filter out unreachable phi operands. auto Filtered = make_filter_range(PN->operands(), [&](const Use &U) { - return ReachableBlocks.count(PN->getIncomingBlock(U)); + return ReachableEdges.count({PN->getIncomingBlock(U), PHIBlock}); }); std::transform(Filtered.begin(), Filtered.end(), op_inserter(E), @@ -1740,10 +1740,11 @@ void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) { // If all the arguments are the same, the MemoryPhi has the same value as the // argument. // Filter out unreachable blocks and self phis from our operands. + const BasicBlock *PHIBlock = MP->getBlock(); auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) { return lookupMemoryAccessEquiv(cast(U)) != MP && !isMemoryAccessTop(cast(U)) && - ReachableBlocks.count(MP->getIncomingBlock(U)); + ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock}); }); // If all that is left is nothing, our memoryphi is undef. We keep it as // InitialClass. Note: The only case this should happen is if we have at @@ -1822,7 +1823,7 @@ bool NewGVN::singleReachablePHIPath(const MemoryAccess *First, } else { auto *MP = cast(First); auto ReachableOperandPred = [&](const Use &U) { - return ReachableBlocks.count(MP->getIncomingBlock(U)); + return ReachableEdges.count({MP->getIncomingBlock(U), MP->getBlock()}); }; auto FilteredPhiArgs = make_filter_range(MP->operands(), ReachableOperandPred); @@ -1879,7 +1880,8 @@ void NewGVN::verifyMemoryCongruency() const { // We can only sanely verify that MemoryDefs in the operand list all have // the same class. auto ReachableOperandPred = [&](const Use &U) { - return ReachableBlocks.count(FirstMP->getIncomingBlock(U)) && + return ReachableEdges.count( + {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) && isa(U); }; diff --git a/test/Transforms/NewGVN/phi-edge-handling.ll b/test/Transforms/NewGVN/phi-edge-handling.ll new file mode 100644 index 00000000000..6451006a694 --- /dev/null +++ b/test/Transforms/NewGVN/phi-edge-handling.ll @@ -0,0 +1,60 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -newgvn -S | FileCheck %s + + +;; Block 6 is reachable, but edge 6->4 is not +;; This means the phi value is undef, not 0 +; Function Attrs: ssp uwtable +define i16 @hoge() local_unnamed_addr #0 align 2 { +; CHECK-LABEL: @hoge( +; CHECK-NEXT: bb: +; CHECK-NEXT: switch i8 undef, label [[BB7:%.*]] [ +; CHECK-NEXT: i8 0, label [[BB1:%.*]] +; CHECK-NEXT: i8 12, label [[BB2:%.*]] +; CHECK-NEXT: ] +; CHECK: bb1: +; CHECK-NEXT: br label [[BB6:%.*]] +; CHECK: bb2: +; CHECK-NEXT: br label [[BB4:%.*]] +; CHECK: bb3: +; CHECK-NEXT: unreachable +; CHECK: bb4: +; CHECK-NEXT: ret i16 undef +; CHECK: bb6: +; CHECK-NEXT: br i1 true, label [[BB3:%.*]], label [[BB4]], !llvm.loop !1 +; CHECK: bb7: +; CHECK-NEXT: unreachable +; +bb: + switch i8 undef, label %bb7 [ + i8 0, label %bb1 + i8 12, label %bb2 + ] + +bb1: ; preds = %bb + br label %bb6 + +bb2: ; preds = %bb + br label %bb4 + +bb3: ; preds = %bb6 + unreachable + +bb4: ; preds = %bb6, %bb2 + %tmp = phi i16 [ 0, %bb6 ], [ undef, %bb2 ] + ret i16 %tmp + +bb6: ; preds = %bb4 + br i1 true, label %bb3, label %bb4, !llvm.loop !1 + +bb7: ; preds = %bb + unreachable +} + +attributes #0 = { ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } + +!llvm.ident = !{!0} + +!0 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git a8b933d4d1d133594fdaed35ee5814514b738f6d) (/Users/dannyb/sources/llvm-clean fc630a9b5613f544c07a8f16abcc173793df62cf)"} +!1 = distinct !{!1, !2} +!2 = !{!"llvm.loop.unroll.disable"}