From c671375e8eaff8e91c37ac7621c84a9373923ecd Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Mon, 3 Nov 2014 21:55:12 +0000 Subject: [PATCH] InstCombine: Remove infinite loop caused by FoldOpIntoPhi FoldOpIntoPhi could create an infinite loop if the PHI could potentially reach a BB it was considering inserting instructions into. The instructions it would insert would eventually lead to other combines firing which would, again, lead to FoldOpIntoPhi firing. The solution is to handicap FoldOpIntoPhi so that it doesn't attempt to insert instructions that the PHI might reach. This fixes PR21377. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221187 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstructionCombining.cpp | 7 +++- test/Transforms/InstCombine/fold-phi.ll | 37 +++++++++++-------- test/Transforms/InstCombine/pr12338.ll | 2 +- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index c32294f51fb..5a0b01eca5c 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -40,8 +40,10 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Analysis/AssumptionTracker.h" +#include "llvm/Analysis/CFG.h" #include "llvm/Analysis/ConstantFolding.h" #include "llvm/Analysis/InstructionSimplify.h" +#include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/MemoryBuiltins.h" #include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/CFG.h" @@ -794,13 +796,14 @@ Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { // If the incoming non-constant value is in I's block, we will remove one // instruction, but insert another equivalent one, leading to infinite // instcombine. - if (NonConstBB == I.getParent()) + if (isPotentiallyReachable(I.getParent(), NonConstBB, DT, + getAnalysisIfAvailable())) return nullptr; } // If there is exactly one non-constant value, we can insert a copy of the // operation in that block. However, if this is a critical edge, we would be - // inserting the computation one some other paths (e.g. inside a loop). Only + // inserting the computation on some other paths (e.g. inside a loop). Only // do this if the pred block is unconditionally branching into the phi block. if (NonConstBB != nullptr) { BranchInst *BI = dyn_cast(NonConstBB->getTerminator()); diff --git a/test/Transforms/InstCombine/fold-phi.ll b/test/Transforms/InstCombine/fold-phi.ll index bd01d58aa58..7954caf9754 100644 --- a/test/Transforms/InstCombine/fold-phi.ll +++ b/test/Transforms/InstCombine/fold-phi.ll @@ -17,23 +17,30 @@ end: ret float %add5 } -; CHECK: fold_phi -define float @fold_phi(float %a) nounwind { +; CHECK-LABEL: @pr21377( +define void @pr21377(i32, i32) { entry: - br label %for.body + br label %while.cond.i -for.body: -; CHECK: phi float -; CHECK-NEXT: br i1 undef - %sum.057 = phi float [ 0.000000e+00, %entry ], [ %add5, %bb0 ] - %add5 = fadd float %sum.057, 1.0 ;; Should be moved to the latch! - br i1 undef, label %bb0, label %end +while.cond.i: ; preds = %while.end.i, %entry + %g.0.i = phi i64 [ 0, %entry ], [ %phitmp5.i, %while.end.i ] + br i1 undef, label %fn2.exit, label %while.body.i -; CHECK: bb0: -bb0: -; CHECK: fadd float - br label %for.body +while.body.i: ; preds = %while.cond.i + %conv.i = zext i32 %0 to i64 + %phitmp3.i = or i64 %g.0.i, %conv.i + br label %while.cond3.i -end: - ret float %add5 +while.cond3.i: ; preds = %while.cond3.i, %while.body.i + %g.1.i = phi i64 [ %phitmp3.i, %while.body.i ], [ 0, %while.cond3.i ] + br i1 undef, label %while.end.i, label %while.cond3.i + +while.end.i: ; preds = %while.cond3.i + %conv.i.i = zext i32 %1 to i64 + %or7.i = or i64 %g.1.i, %conv.i.i + %phitmp5.i = and i64 %or7.i, 4294967295 + br label %while.cond.i + +fn2.exit: ; preds = %while.cond.i + ret void } diff --git a/test/Transforms/InstCombine/pr12338.ll b/test/Transforms/InstCombine/pr12338.ll index d34600f0fa5..614387a452c 100644 --- a/test/Transforms/InstCombine/pr12338.ll +++ b/test/Transforms/InstCombine/pr12338.ll @@ -6,7 +6,6 @@ entry: for.cond: %local = phi <1 x i32> [ , %entry ], [ %phi2, %cond.end47 ] -; CHECK: sub <1 x i32> , %local %phi3 = sub <1 x i32> zeroinitializer, %local br label %cond.end @@ -19,6 +18,7 @@ cond.end: cond.end47: %sum = add <1 x i32> %cond, +; CHECK: sub <1 x i32> , %cond %phi2 = sub <1 x i32> zeroinitializer, %sum br label %for.cond }