[llvm-commits] [llvm] r54396 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/sitofp.ll

Chris Lattner sabre at nondot.org
Tue Aug 5 22:13:06 PDT 2008


Author: lattner
Date: Wed Aug  6 00:13:06 2008
New Revision: 54396

URL: http://llvm.org/viewvc/llvm-project?rev=54396&view=rev
Log:
Zap sitofp/fptoui pairs.  In all cases when the sign difference 
matters, the result is undefined anyway.


Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/sitofp.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=54396&r1=54395&r2=54396&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Wed Aug  6 00:13:06 2008
@@ -7750,27 +7750,41 @@
 }
 
 Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) {
-  // fptoui(uitofp(X)) --> X  if the intermediate type has enough bits in its
-  // mantissa to accurately represent all values of X.  For example, do not
-  // do this with i64->float->i64.
-  if (UIToFPInst *SrcI = dyn_cast<UIToFPInst>(FI.getOperand(0)))
-    if (SrcI->getOperand(0)->getType() == FI.getType() &&
-        (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */
-                    SrcI->getType()->getFPMantissaWidth())
-      return ReplaceInstUsesWith(FI, SrcI->getOperand(0));
+  Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
+  if (OpI == 0)
+    return commonCastTransforms(FI);
+
+  // fptoui(uitofp(X)) --> X
+  // fptoui(sitofp(X)) --> X
+  // This is safe if the intermediate type has enough bits in its mantissa to
+  // accurately represent all values of X.  For example, do not do this with
+  // i64->float->i64.  This is also safe for sitofp case, because any negative
+  // 'X' value would cause an undefined result for the fptoui. 
+  if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
+      OpI->getOperand(0)->getType() == FI.getType() &&
+      (int)FI.getType()->getPrimitiveSizeInBits() < /*extra bit for sign */
+                    OpI->getType()->getFPMantissaWidth())
+    return ReplaceInstUsesWith(FI, OpI->getOperand(0));
 
   return commonCastTransforms(FI);
 }
 
 Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) {
-  // fptosi(sitofp(X)) --> X  if the intermediate type has enough bits in its
-  // mantissa to accurately represent all values of X.  For example, do not
-  // do this with i64->float->i64.
-  if (SIToFPInst *SrcI = dyn_cast<SIToFPInst>(FI.getOperand(0)))
-    if (SrcI->getOperand(0)->getType() == FI.getType() &&
-        (int)FI.getType()->getPrimitiveSizeInBits() <= 
-                    SrcI->getType()->getFPMantissaWidth())
-      return ReplaceInstUsesWith(FI, SrcI->getOperand(0));
+  Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0));
+  if (OpI == 0)
+    return commonCastTransforms(FI);
+  
+  // fptosi(sitofp(X)) --> X
+  // fptosi(uitofp(X)) --> X
+  // This is safe if the intermediate type has enough bits in its mantissa to
+  // accurately represent all values of X.  For example, do not do this with
+  // i64->float->i64.  This is also safe for sitofp case, because any negative
+  // 'X' value would cause an undefined result for the fptoui. 
+  if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) &&
+      OpI->getOperand(0)->getType() == FI.getType() &&
+      (int)FI.getType()->getPrimitiveSizeInBits() <= 
+                    OpI->getType()->getFPMantissaWidth())
+    return ReplaceInstUsesWith(FI, OpI->getOperand(0));
   
   return commonCastTransforms(FI);
 }

Modified: llvm/trunk/test/Transforms/InstCombine/sitofp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/sitofp.ll?rev=54396&r1=54395&r2=54396&view=diff

==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/sitofp.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/sitofp.ll Wed Aug  6 00:13:06 2008
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep sitofp
+; RUN: llvm-as < %s | opt -instcombine | llvm-dis | not grep itofp
 
 define i1 @test1(i8 %A) {
   %B = sitofp i8 %A to double
@@ -41,3 +41,15 @@
 	ret i32 %G
 }
 
+define i32 @test7(i32 %a) nounwind {
+	%b = sitofp i32 %a to double		; <double> [#uses=1]
+	%c = fptoui double %b to i32		; <i32> [#uses=1]
+	ret i32 %c
+}
+
+define i32 @test8(i32 %a) nounwind {
+	%b = uitofp i32 %a to double		; <double> [#uses=1]
+	%c = fptosi double %b to i32		; <i32> [#uses=1]
+	ret i32 %c
+}
+





More information about the llvm-commits mailing list