[llvm] r222265 - InstCombine: Fix another infinite loop caused by visitFPTrunc

Steven Wu stevenwu at apple.com
Thu Dec 11 14:13:29 PST 2014


Hi David

Here is another infinite loop fall out from this patch. Here is a test case:
 define float @test(float %arg) {
 entry:
   %0 = fpext float %arg to double
   %1 = frem double %0, 0xBAD
   %2 = fptrunc double %1 to float
   ret float %2
 }

The test case has exactly the same pattern as the instruction generated from instcombine which suggests probably it shouldn’t generate a truncation of same type? Maybe even a smaller type?
Here is a patch. Let me know what you think.

diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp b/lib/Transforms/InstCombine/InstCombineCasts.cpp
index aba77bb..357bf24 100644
--- a/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1269,16 +1269,19 @@ 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.
-        if (LHSWidth < SrcWidth)
-          LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
-        else if (RHSWidth <= SrcWidth)
-          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<Instruction>(ExactResult))
-            RI->copyFastMathFlags(OpI);
-          return CastInst::CreateFPCast(ExactResult, CI.getType());
+        if (SrcWidth != OpWidth) {
+          if (LHSWidth < SrcWidth)
+            LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
+          else if (RHSWidth <= SrcWidth)
+            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<Instruction>(ExactResult))
+              RI->copyFastMathFlags(OpI);
+            return CastInst::CreateFPCast(ExactResult, CI.getType());
+          }
         }
+        break;
     }
 
Thanks

Steven
> On Nov 18, 2014, at 2:06 PM, David Majnemer <david.majnemer at gmail.com> wrote:
> 
> majnemer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141211/075c718a/attachment.html>


More information about the llvm-commits mailing list