Refactor and check "onlyReadsMemory" before optimizing builtins.
authorBob Wilson <bob.wilson@apple.com>
Fri, 3 Aug 2012 23:29:17 +0000 (23:29 +0000)
committerBob Wilson <bob.wilson@apple.com>
Fri, 3 Aug 2012 23:29:17 +0000 (23:29 +0000)
This patch is mostly just refactoring a bunch of copy-and-pasted code, but
it also adds a check that the call instructions are readnone or readonly.
That check was already present for sin, cos, sqrt, log2, and exp2 calls, but
it was missing for the rest of the builtins being handled in this code.

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

13 files changed:
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h
test/CodeGen/ARM/fabss.ll
test/CodeGen/ARM/fcopysign.ll
test/CodeGen/ARM/fparith.ll
test/CodeGen/ARM/vfp.ll
test/CodeGen/CellSPU/fcmp32.ll
test/CodeGen/CellSPU/fneg-fabs.ll
test/CodeGen/Hexagon/opt-fabs.ll
test/CodeGen/PowerPC/fabs.ll
test/CodeGen/PowerPC/fnabs.ll
test/CodeGen/X86/fabs.ll
test/CodeGen/X86/stack-align.ll

index a6c28587946ba98a226c26e1088c9bfe2b8c781f..ba5bd79722cef8764bbacb1e5c4bdd24f4cf01d7 100644 (file)
@@ -5511,6 +5511,22 @@ bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
   return false;
 }
 
+/// visitUnaryFloatCall - If a call instruction is a unary floating-point
+/// operation (as expected), translate it to an SDNode with the specified opcode
+/// and return true.
+bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
+                                              unsigned Opcode) {
+  // Sanity check that it really is a unary floating-point call.
+  if (I.getNumArgOperands() != 1 ||
+      !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
+      I.getType() != I.getArgOperand(0)->getType() ||
+      !I.onlyReadsMemory())
+    return false;
+
+  SDValue Tmp = getValue(I.getArgOperand(0));
+  setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(), Tmp.getValueType(), Tmp));
+  return true;
+}
 
 void SelectionDAGBuilder::visitCall(const CallInst &I) {
   // Handle inline assembly differently.
@@ -5553,7 +5569,8 @@ void SelectionDAGBuilder::visitCall(const CallInst &I) {
         if (I.getNumArgOperands() == 2 &&   // Basic sanity checks.
             I.getArgOperand(0)->getType()->isFloatingPointTy() &&
             I.getType() == I.getArgOperand(0)->getType() &&
-            I.getType() == I.getArgOperand(1)->getType()) {
+            I.getType() == I.getArgOperand(1)->getType() &&
+            I.onlyReadsMemory()) {
           SDValue LHS = getValue(I.getArgOperand(0));
           SDValue RHS = getValue(I.getArgOperand(1));
           setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
@@ -5564,139 +5581,68 @@ void SelectionDAGBuilder::visitCall(const CallInst &I) {
       case LibFunc::fabs:
       case LibFunc::fabsf:
       case LibFunc::fabsl:
-        if (I.getNumArgOperands() == 1 &&   // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FABS))
           return;
-        }
         break;
       case LibFunc::sin:
       case LibFunc::sinf:
       case LibFunc::sinl:
-        if (I.getNumArgOperands() == 1 &&   // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType() &&
-            I.onlyReadsMemory()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FSIN))
           return;
-        }
         break;
       case LibFunc::cos:
       case LibFunc::cosf:
       case LibFunc::cosl:
-        if (I.getNumArgOperands() == 1 &&   // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType() &&
-            I.onlyReadsMemory()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FCOS))
           return;
-        }
         break;
       case LibFunc::sqrt:
       case LibFunc::sqrtf:
       case LibFunc::sqrtl:
-        if (I.getNumArgOperands() == 1 &&   // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType() &&
-            I.onlyReadsMemory()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FSQRT))
           return;
-        }
         break;
       case LibFunc::floor:
       case LibFunc::floorf:
       case LibFunc::floorl:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FFLOOR, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FFLOOR))
           return;
-        }
         break;
       case LibFunc::nearbyint:
       case LibFunc::nearbyintf:
       case LibFunc::nearbyintl:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FNEARBYINT, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
           return;
-        }
         break;
       case LibFunc::ceil:
       case LibFunc::ceilf:
       case LibFunc::ceill:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FCEIL, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FCEIL))
           return;
-        }
         break;
       case LibFunc::rint:
       case LibFunc::rintf:
       case LibFunc::rintl:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FRINT, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FRINT))
           return;
-        }
         break;
       case LibFunc::trunc:
       case LibFunc::truncf:
       case LibFunc::truncl:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FTRUNC, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FTRUNC))
           return;
-        }
         break;
       case LibFunc::log2:
       case LibFunc::log2f:
       case LibFunc::log2l:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType() &&
-            I.onlyReadsMemory()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FLOG2, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FLOG2))
           return;
-        }
         break;
       case LibFunc::exp2:
       case LibFunc::exp2f:
       case LibFunc::exp2l:
-        if (I.getNumArgOperands() == 1 && // Basic sanity checks.
-            I.getArgOperand(0)->getType()->isFloatingPointTy() &&
-            I.getType() == I.getArgOperand(0)->getType() &&
-            I.onlyReadsMemory()) {
-          SDValue Tmp = getValue(I.getArgOperand(0));
-          setValue(&I, DAG.getNode(ISD::FEXP2, getCurDebugLoc(),
-                                   Tmp.getValueType(), Tmp));
+        if (visitUnaryFloatCall(I, ISD::FEXP2))
           return;
-        }
         break;
       case LibFunc::memcmp:
         if (visitMemCmpCall(I))
index d0fde6f01d6d87f3a3863c7d21b1938eddd56e97..40900023140eec874976142d453f31aba468b965 100644 (file)
@@ -520,6 +520,7 @@ private:
   void visitPHI(const PHINode &I);
   void visitCall(const CallInst &I);
   bool visitMemCmpCall(const CallInst &I);
+  bool visitUnaryFloatCall(const CallInst &I, unsigned Opcode);
   void visitAtomicLoad(const LoadInst &I);
   void visitAtomicStore(const StoreInst &I);
 
index 45c322dce8b969c61790b856859b84b5c8f4c970..5c0db88407a3c855e2184d782797b9af0185d17c 100644 (file)
@@ -6,7 +6,7 @@
 define float @test(float %a, float %b) {
 entry:
         %dum = fadd float %a, %b
-       %0 = tail call float @fabsf(float %dum)
+       %0 = tail call float @fabsf(float %dum) readnone
         %dum1 = fadd float %0, %b
        ret float %dum1
 }
index 27fa2b093d8984cbb74cc2f12b560b6d20f38396..5511d24cb2807b0362a43d9c881445396422edfe 100644 (file)
@@ -11,7 +11,7 @@ entry:
 ; HARD: test1:
 ; HARD: vmov.i32 [[REG1:(d[0-9]+)]], #0x80000000
 ; HARD: vbsl [[REG1]], d
-  %0 = tail call float @copysignf(float %x, float %y) nounwind
+  %0 = tail call float @copysignf(float %x, float %y) nounwind readnone
   ret float %0
 }
 
@@ -25,7 +25,7 @@ entry:
 ; HARD: vmov.i32 [[REG2:(d[0-9]+)]], #0x80000000
 ; HARD: vshl.i64 [[REG2]], [[REG2]], #32
 ; HARD: vbsl [[REG2]], d1, d0
-  %0 = tail call double @copysign(double %x, double %y) nounwind
+  %0 = tail call double @copysign(double %x, double %y) nounwind readnone
   ret double %0
 }
 
@@ -36,7 +36,7 @@ entry:
 ; SOFT: vshl.i64 [[REG3]], [[REG3]], #32
 ; SOFT: vbsl [[REG3]],
   %0 = fmul double %x, %y
-  %1 = tail call double @copysign(double %0, double %z) nounwind
+  %1 = tail call double @copysign(double %0, double %z) nounwind readnone
   ret double %1
 }
 
index ce6d6b29e9d559017e964f6cb802535b74cd27bb..a8bae3b951998211bb76525b7aec002d0028e039 100644 (file)
@@ -84,7 +84,7 @@ define float @f11(float %a) {
 ;CHECK: f11:
 ;CHECK: bic
 entry:
-       %tmp1 = call float @fabsf( float %a )           ; <float> [#uses=1]
+       %tmp1 = call float @fabsf( float %a ) readnone  ; <float> [#uses=1]
        ret float %tmp1
 }
 
@@ -94,7 +94,7 @@ define double @f12(double %a) {
 ;CHECK: f12:
 ;CHECK: vabs.f64
 entry:
-       %tmp1 = call double @fabs( double %a )          ; <double> [#uses=1]
+       %tmp1 = call double @fabs( double %a ) readnone ; <double> [#uses=1]
        ret double %tmp1
 }
 
index 49a69827bc05325f1e3d738b39858a8d48c903e7..bf0f3680c13556a4e5709669b0b8f37804acb86d 100644 (file)
@@ -17,11 +17,11 @@ define void @test_abs(float* %P, double* %D) {
 ;CHECK: test_abs:
        %a = load float* %P             ; <float> [#uses=1]
 ;CHECK: vabs.f32
-       %b = call float @fabsf( float %a )              ; <float> [#uses=1]
+       %b = call float @fabsf( float %a ) readnone     ; <float> [#uses=1]
        store float %b, float* %P
        %A = load double* %D            ; <double> [#uses=1]
 ;CHECK: vabs.f64
-       %B = call double @fabs( double %A )             ; <double> [#uses=1]
+       %B = call double @fabs( double %A ) readnone    ; <double> [#uses=1]
        store double %B, double* %D
        ret void
 }
index c14fd7ba4a46a58479a85f37f6f1a8ea189dec82..b2a7317c129762c48ab010d7bfc0ebbb3dad8a58 100644 (file)
@@ -15,8 +15,8 @@ define i1 @fcmp_eq(float %arg1, float %arg2) {
 define i1 @fcmp_mag_eq(float %arg1, float %arg2) {
 ; CHECK: fcmeq
 ; CHECK: bi $lr
-        %1 = call float @fabsf(float %arg1)
-        %2 = call float @fabsf(float %arg2)
+        %1 = call float @fabsf(float %arg1) readnone
+        %2 = call float @fabsf(float %arg2) readnone
         %3 = fcmp oeq float %1, %2
         ret i1 %3
 }
index 1e5e3b34144059e88f97241d80dd25dfb32c4866..6e01906dae699949b188d282565e9325058035e6 100644 (file)
@@ -32,11 +32,11 @@ declare double @fabs(double)
 declare float @fabsf(float)
 
 define double @fabs_dp(double %X) {
-        %Y = call double @fabs( double %X )
+        %Y = call double @fabs( double %X ) readnone
         ret double %Y
 }
 
 define float @fabs_sp(float %X) {
-        %Y = call float @fabsf( float %X )
+        %Y = call float @fabsf( float %X ) readnone
         ret float %Y
 }
index 1cf0dd0cd994b4f2484e9275879dc019cdb19cd3..f1a66f4e65c7ffad53e297aad209554c1818bc7a 100644 (file)
@@ -8,7 +8,7 @@ entry:
   %x.addr = alloca float, align 4
   store float %x, float* %x.addr, align 4
   %0 = load float* %x.addr, align 4
-  %call = call float @fabsf(float %0)
+  %call = call float @fabsf(float %0) readnone
   ret float %call
 }
 
index 156e00b4e5772acef23ca1191fc37f971fb9a738..ddcce745084a2c74882590abb62a1f18f3f89e2b 100644 (file)
@@ -2,6 +2,6 @@
 
 define double @fabs(double %f) {
 entry:
-       %tmp2 = tail call double @fabs( double %f )             ; <double> [#uses=1]
+       %tmp2 = tail call double @fabs( double %f ) readnone    ; <double> [#uses=1]
        ret double %tmp2
 }
index bbd5c7159edc46e52110aa4ebfa91c4c0dd8ed5d..9fa2dcb2909c6b574d0b60e9cd7beca4a666843c 100644 (file)
@@ -3,7 +3,7 @@
 declare double @fabs(double)
 
 define double @test(double %X) {
-        %Y = call double @fabs( double %X )             ; <double> [#uses=1]
+        %Y = call double @fabs( double %X ) readnone     ; <double> [#uses=1]
         %Z = fsub double -0.000000e+00, %Y               ; <double> [#uses=1]
         ret double %Z
 }
index 07a8d91185b8f8123f0142f88b26a357893e6270..2074f04d405a4b332569db62281a894b5baa3964 100644 (file)
@@ -11,7 +11,7 @@ declare x86_fp80 @fabsl(x86_fp80)
 ; UNSAFE: test1:
 ; NOOPT:  test1:
 define float @test1(float %X) {
-        %Y = call float @fabsf(float %X)
+        %Y = call float @fabsf(float %X) readnone
         ret float %Y
 }
 ; CHECK:  {{^[ \t]+fabs$}}
@@ -42,7 +42,7 @@ define double @test2(double %X) {
 ; UNSAFE: test3:
 ; NOOPT:  test3:
 define x86_fp80 @test3(x86_fp80 %X) {
-        %Y = call x86_fp80 @fabsl(x86_fp80 %X)
+        %Y = call x86_fp80 @fabsl(x86_fp80 %X) readnone
         ret x86_fp80 %Y
 }
 ; CHECK:  {{^[ \t]+fabs$}}
index f6c13ec0adf72d781c3b684eab5dbe489f12fdc6..0ddb2378ef2f2fe8b016237a31c28b5916804f5a 100644 (file)
@@ -10,11 +10,11 @@ target triple = "i686-apple-darwin8"
 define void @test({ double, double }* byval  %z, double* %P) nounwind {
 entry:
        %tmp3 = load double* @G, align 16               ; <double> [#uses=1]
-       %tmp4 = tail call double @fabs( double %tmp3 )          ; <double> [#uses=1]
+       %tmp4 = tail call double @fabs( double %tmp3 ) readnone ; <double> [#uses=1]
         store volatile double %tmp4, double* %P
        %tmp = getelementptr { double, double }* %z, i32 0, i32 0               ; <double*> [#uses=1]
        %tmp1 = load volatile double* %tmp, align 8             ; <double> [#uses=1]
-       %tmp2 = tail call double @fabs( double %tmp1 )          ; <double> [#uses=1]
+       %tmp2 = tail call double @fabs( double %tmp1 ) readnone ; <double> [#uses=1]
     ; CHECK: andpd{{.*}}4(%esp), %xmm
        %tmp6 = fadd double %tmp4, %tmp2                ; <double> [#uses=1]
        store volatile double %tmp6, double* %P, align 8