From 643bef9333fddccab13d91e342e8ea7c6009c985 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 18 Nov 2014 22:06:45 +0000 Subject: [PATCH] InstCombine: Fix another infinite loop caused by visitFPTrunc We would attempt to replace an frem's operand with the same operand. This would cause InstCombine to think real work was done, causing InstCombine to enter an infinite loop. This fixes the second part of PR21576. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222265 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineCasts.cpp | 9 ++++----- test/Transforms/InstCombine/fpcast.ll | 10 ++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp index a0570f7ebd4..aba77bb4462 100644 --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -1269,13 +1269,12 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { // type of OpI doesn't enter into things at all. We simply evaluate // in whichever source type is larger, then convert to the // destination type. - Value *NewLHS = LHSOrig, *NewRHS = RHSOrig; if (LHSWidth < SrcWidth) - NewLHS = Builder->CreateFPExt(NewLHS, RHSOrig->getType()); + LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType()); else if (RHSWidth <= SrcWidth) - NewRHS = Builder->CreateFPExt(NewRHS, LHSOrig->getType()); - if (NewLHS != LHSOrig || NewRHS != RHSOrig) { - Value *ExactResult = Builder->CreateFRem(NewLHS, NewRHS); + RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType()); + if (LHSOrig != OpI->getOperand(0) || RHSOrig != OpI->getOperand(1)) { + Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig); if (Instruction *RI = dyn_cast(ExactResult)) RI->copyFastMathFlags(OpI); return CastInst::CreateFPCast(ExactResult, CI.getType()); diff --git a/test/Transforms/InstCombine/fpcast.ll b/test/Transforms/InstCombine/fpcast.ll index c4c8578198b..ac034028b22 100644 --- a/test/Transforms/InstCombine/fpcast.ll +++ b/test/Transforms/InstCombine/fpcast.ll @@ -63,3 +63,13 @@ define <1 x float> @test6(<1 x double> %V) { ; CHECK-NEXT: %[[trunc:.*]] = fptrunc <1 x double> %[[frem]] to <1 x float> ; CHECK-NEXT: ret <1 x float> %trunc } + +define float @test7(double %V) { + %frem = frem double %V, 1.000000e+00 + %trunc = fptrunc double %frem to float + ret float %trunc +; CHECK-LABEL: @test7 +; CHECK-NEXT: %[[frem:.*]] = frem double %V, 1.000000e+00 +; CHECK-NEXT: %[[trunc:.*]] = fptrunc double %frem to float +; CHECK-NEXT: ret float %trunc +} -- 2.11.0