]> granicus.if.org Git - llvm/commitdiff
[Float2Int] Add support for unary FNeg to Float2Int
authorCameron McInally <cameron.mcinally@nyu.edu>
Mon, 8 Jul 2019 14:46:07 +0000 (14:46 +0000)
committerCameron McInally <cameron.mcinally@nyu.edu>
Mon, 8 Jul 2019 14:46:07 +0000 (14:46 +0000)
Differential Revision: https://reviews.llvm.org/D63941

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@365324 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/Float2Int.cpp
test/Transforms/Float2Int/basic.ll

index cb0288a28f79d09dabbbea4dddc00120e114954f..a0935efa264be4abb8f38ee9bfdea419c4cc93f4 100644 (file)
@@ -200,6 +200,7 @@ void Float2IntPass::walkBackwards(const SmallPtrSetImpl<Instruction*> &Roots) {
       continue;
     }
 
+    case Instruction::FNeg:
     case Instruction::FAdd:
     case Instruction::FSub:
     case Instruction::FMul:
@@ -240,6 +241,15 @@ void Float2IntPass::walkForwards() {
     case Instruction::SIToFP:
       llvm_unreachable("Should have been handled in walkForwards!");
 
+    case Instruction::FNeg:
+      Op = [](ArrayRef<ConstantRange> Ops) {
+        assert(Ops.size() == 1 && "FNeg is a unary operator!");
+        unsigned Size = Ops[0].getBitWidth();
+        auto Zero = ConstantRange(APInt::getNullValue(Size));
+        return Zero.sub(Ops[0]);
+      };
+      break;
+
     case Instruction::FAdd:
     case Instruction::FSub:
     case Instruction::FMul:
@@ -466,6 +476,10 @@ Value *Float2IntPass::convert(Instruction *I, Type *ToTy) {
     NewV = IRB.CreateSExtOrTrunc(NewOperands[0], ToTy);
     break;
 
+  case Instruction::FNeg:
+    NewV = IRB.CreateNeg(NewOperands[0], I->getName());
+    break;
+
   case Instruction::FAdd:
   case Instruction::FSub:
   case Instruction::FMul:
index eb6c039f1cee5e084eae0346b477a935e9009492..e1eddc668ea69a5491bf5e775938d138a980a255 100644 (file)
@@ -80,12 +80,11 @@ define i32 @simple5(i8 %a, i8 %b) {
 }
 
 ; CHECK-LABEL: @simple6
-; CHECK:  %1 = uitofp i8 %a to float
-; CHECK:  %2 = uitofp i8 %b to float
-; CHECK:  %3 = fneg float %1
-; CHECK:  %4 = fmul float %3, %2
-; CHECK:  %5 = fptoui float %4 to i32
-; CHECK:  ret i32 %5
+; CHECK:  %1 = zext i8 %a to i32
+; CHECK:  %2 = zext i8 %b to i32
+; CHECK:  %3 = sub i32 0, %1
+; CHECK:  %4 = mul i32 %3, %2
+; CHECK:  ret i32 %4
 define i32 @simple6(i8 %a, i8 %b) {
   %1 = uitofp i8 %a to float
   %2 = uitofp i8 %b to float
@@ -145,6 +144,18 @@ define i32 @simple_negative(i8 %call) {
   ret i32 %conv3
 }
 
+; CHECK-LABEL: @simple_fneg
+; CHECK:  %1 = zext i8 %a to i32
+; CHECK:  %2 = sub i32 0, %1
+; CHECK:  %3 = trunc i32 %2 to i16
+; CHECK:  ret i16 %3
+define i16 @simple_fneg(i8 %a) {
+  %1 = uitofp i8 %a to float
+  %2 = fneg fast float %1
+  %3 = fptoui float %2 to i16
+  ret i16 %3
+}
+
 ;
 ; Negative tests
 ;