PTX: Fix conversion between predicates and value types
authorJustin Holewinski <justin.holewinski@gmail.com>
Mon, 20 Jun 2011 18:42:48 +0000 (18:42 +0000)
committerJustin Holewinski <justin.holewinski@gmail.com>
Mon, 20 Jun 2011 18:42:48 +0000 (18:42 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133454 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/PTX/PTXInstrInfo.td
test/CodeGen/PTX/cvt.ll
test/CodeGen/PTX/setp.ll

index d497283b72116a2c025997ae7f2d86a8a7750ba7..d240899c6adc15dfbebbbabb19753b70fca70993 100644 (file)
@@ -826,31 +826,35 @@ defm STs : PTX_ST_ALL<"st.shared", store_shared>;
 // TODO: Do something with st.param if/when it is needed.
 
 // Conversion to pred
-
+// PTX does not directly support converting to a predicate type, so we fake it
+// by performing a greater-than test between the value and zero.  This follows
+// the C convention that any non-zero value is equivalent to 'true'.
 def CVT_pred_u16
-  : InstPTX<(outs RegPred:$d), (ins RegI16:$a), "cvt.pred.u16\t$d, $a",
+  : InstPTX<(outs RegPred:$d), (ins RegI16:$a), "setp.gt.b16\t$d, $a, 0",
             [(set RegPred:$d, (trunc RegI16:$a))]>;
 
 def CVT_pred_u32
-  : InstPTX<(outs RegPred:$d), (ins RegI32:$a), "cvt.pred.u32\t$d, $a",
+  : InstPTX<(outs RegPred:$d), (ins RegI32:$a), "setp.gt.b32\t$d, $a, 0",
             [(set RegPred:$d, (trunc RegI32:$a))]>;
 
 def CVT_pred_u64
-  : InstPTX<(outs RegPred:$d), (ins RegI64:$a), "cvt.pred.u64\t$d, $a",
+  : InstPTX<(outs RegPred:$d), (ins RegI64:$a), "setp.gt.b64\t$d, $a, 0",
             [(set RegPred:$d, (trunc RegI64:$a))]>;
 
 def CVT_pred_f32
-  : InstPTX<(outs RegPred:$d), (ins RegF32:$a), "cvt.rzi.pred.f32\t$d, $a",
+  : InstPTX<(outs RegPred:$d), (ins RegF32:$a), "setp.gt.b32\t$d, $a, 0",
             [(set RegPred:$d, (fp_to_uint RegF32:$a))]>;
 
 def CVT_pred_f64
-  : InstPTX<(outs RegPred:$d), (ins RegF64:$a), "cvt.rzi.pred.f64\t$d, $a",
+  : InstPTX<(outs RegPred:$d), (ins RegF64:$a), "setp.gt.b64\t$d, $a, 0",
             [(set RegPred:$d, (fp_to_uint RegF64:$a))]>;
 
 // Conversion to u16
-
+// PTX does not directly support converting a predicate to a value, so we
+// use a select instruction to select either 0 or 1 (integer or fp) based
+// on the truth value of the predicate.
 def CVT_u16_pred
-  : InstPTX<(outs RegI16:$d), (ins RegPred:$a), "cvt.u16.pred\t$d, $a",
+  : InstPTX<(outs RegI16:$d), (ins RegPred:$a), "selp.u16\t$d, 1, 0, $a",
             [(set RegI16:$d, (zext RegPred:$a))]>;
 
 def CVT_u16_u32
@@ -872,7 +876,7 @@ def CVT_u16_f64
 // Conversion to u32
 
 def CVT_u32_pred
-  : InstPTX<(outs RegI32:$d), (ins RegPred:$a), "cvt.u32.pred\t$d, $a",
+  : InstPTX<(outs RegI32:$d), (ins RegPred:$a), "selp.u32\t$d, 1, 0, $a",
             [(set RegI32:$d, (zext RegPred:$a))]>;
 
 def CVT_u32_u16
@@ -894,7 +898,7 @@ def CVT_u32_f64
 // Conversion to u64
 
 def CVT_u64_pred
-  : InstPTX<(outs RegI64:$d), (ins RegPred:$a), "cvt.u64.pred\t$d, $a",
+  : InstPTX<(outs RegI64:$d), (ins RegPred:$a), "selp.u64\t$d, 1, 0, $a",
             [(set RegI64:$d, (zext RegPred:$a))]>;
 
 def CVT_u64_u16
@@ -916,7 +920,8 @@ def CVT_u64_f64
 // Conversion to f32
 
 def CVT_f32_pred
-  : InstPTX<(outs RegF32:$d), (ins RegPred:$a), "cvt.rn.f32.pred\t$d, $a",
+  : InstPTX<(outs RegF32:$d), (ins RegPred:$a),
+            "selp.f32\t$d, 0F3F800000, 0F00000000, $a",  // 1.0
             [(set RegF32:$d, (uint_to_fp RegPred:$a))]>;
 
 def CVT_f32_u16
@@ -938,7 +943,8 @@ def CVT_f32_f64
 // Conversion to f64
 
 def CVT_f64_pred
-  : InstPTX<(outs RegF64:$d), (ins RegPred:$a), "cvt.rn.f64.pred\t$d, $a",
+  : InstPTX<(outs RegF64:$d), (ins RegPred:$a), 
+            "selp.f64\t$d, 0D3F80000000000000, 0D0000000000000000, $a",  // 1.0
             [(set RegF64:$d, (uint_to_fp RegPred:$a))]>;
 
 def CVT_f64_u16
index bf18bd792112ee4edfc5cd45b460e514f580e409..f7233697c043e978fc3df1735629a130a7529012 100644 (file)
@@ -4,8 +4,10 @@
 ; (note: we convert back to i32 to return)
 
 define ptx_device i32 @cvt_pred_i16(i16 %x, i1 %y) {
-; CHECK: cvt.pred.u16 p0, rh1;
-; CHECK: ret;
+; CHECK: setp.gt.b16 p0, rh1, 0
+; CHECK-NEXT: and.pred p0, p0, p1;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
+; CHECK-NEXT: ret;
        %a = trunc i16 %x to i1
        %b = and i1 %a, %y
        %c = zext i1 %b to i32
@@ -13,8 +15,10 @@ define ptx_device i32 @cvt_pred_i16(i16 %x, i1 %y) {
 }
 
 define ptx_device i32 @cvt_pred_i32(i32 %x, i1 %y) {
-; CHECK: cvt.pred.u32 p0, r1;
-; CHECK: ret;
+; CHECK: setp.gt.b32 p0, r1, 0
+; CHECK-NEXT: and.pred p0, p0, p1;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
+; CHECK-NEXT: ret;
        %a = trunc i32 %x to i1
        %b = and i1 %a, %y
        %c = zext i1 %b to i32
@@ -22,8 +26,10 @@ define ptx_device i32 @cvt_pred_i32(i32 %x, i1 %y) {
 }
 
 define ptx_device i32 @cvt_pred_i64(i64 %x, i1 %y) {
-; CHECK: cvt.pred.u64 p0, rd1;
-; CHECK: ret;
+; CHECK: setp.gt.b64 p0, rd1, 0
+; CHECK-NEXT: and.pred p0, p0, p1;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
+; CHECK-NEXT: ret;
        %a = trunc i64 %x to i1
        %b = and i1 %a, %y
        %c = zext i1 %b to i32
@@ -31,8 +37,10 @@ define ptx_device i32 @cvt_pred_i64(i64 %x, i1 %y) {
 }
 
 define ptx_device i32 @cvt_pred_f32(float %x, i1 %y) {
-; CHECK: cvt.rzi.pred.f32 p0, r1;
-; CHECK: ret;
+; CHECK: setp.gt.b32 p0, r1, 0
+; CHECK-NEXT: and.pred p0, p0, p1;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
+; CHECK-NEXT: ret;
        %a = fptoui float %x to i1
        %b = and i1 %a, %y
        %c = zext i1 %b to i32
@@ -40,8 +48,10 @@ define ptx_device i32 @cvt_pred_f32(float %x, i1 %y) {
 }
 
 define ptx_device i32 @cvt_pred_f64(double %x, i1 %y) {
-; CHECK: cvt.rzi.pred.f64 p0, rd1;
-; CHECK: ret;
+; CHECK: setp.gt.b64 p0, rd1, 0
+; CHECK-NEXT: and.pred p0, p0, p1;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
+; CHECK-NEXT: ret;
        %a = fptoui double %x to i1
        %b = and i1 %a, %y
        %c = zext i1 %b to i32
@@ -51,36 +61,36 @@ define ptx_device i32 @cvt_pred_f64(double %x, i1 %y) {
 ; i16
 
 define ptx_device i16 @cvt_i16_preds(i1 %x) {
-; CHECK: cvt.u16.pred rh0, p1;
-; CHECK: ret;
+; CHECK: selp.u16 rh0, 1, 0, p1;
+; CHECK-NEXT: ret;
        %a = zext i1 %x to i16
        ret i16 %a
 }
 
 define ptx_device i16 @cvt_i16_i32(i32 %x) {
 ; CHECK: cvt.u16.u32 rh0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = trunc i32 %x to i16
        ret i16 %a
 }
 
 define ptx_device i16 @cvt_i16_i64(i64 %x) {
 ; CHECK: cvt.u16.u64 rh0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = trunc i64 %x to i16
        ret i16 %a
 }
 
 define ptx_device i16 @cvt_i16_f32(float %x) {
 ; CHECK: cvt.rzi.u16.f32 rh0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptoui float %x to i16
        ret i16 %a
 }
 
 define ptx_device i16 @cvt_i16_f64(double %x) {
 ; CHECK: cvt.rzi.u16.f64 rh0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptoui double %x to i16
        ret i16 %a
 }
@@ -88,36 +98,36 @@ define ptx_device i16 @cvt_i16_f64(double %x) {
 ; i32
 
 define ptx_device i32 @cvt_i32_preds(i1 %x) {
-; CHECK: cvt.u32.pred r0, p1;
-; CHECK: ret;
+; CHECK: selp.u32 r0, 1, 0, p1;
+; CHECK-NEXT: ret;
        %a = zext i1 %x to i32
        ret i32 %a
 }
 
 define ptx_device i32 @cvt_i32_i16(i16 %x) {
 ; CHECK: cvt.u32.u16 r0, rh1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = zext i16 %x to i32
        ret i32 %a
 }
 
 define ptx_device i32 @cvt_i32_i64(i64 %x) {
 ; CHECK: cvt.u32.u64 r0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = trunc i64 %x to i32
        ret i32 %a
 }
 
 define ptx_device i32 @cvt_i32_f32(float %x) {
 ; CHECK: cvt.rzi.u32.f32 r0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptoui float %x to i32
        ret i32 %a
 }
 
 define ptx_device i32 @cvt_i32_f64(double %x) {
 ; CHECK: cvt.rzi.u32.f64 r0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptoui double %x to i32
        ret i32 %a
 }
@@ -125,29 +135,29 @@ define ptx_device i32 @cvt_i32_f64(double %x) {
 ; i64
 
 define ptx_device i64 @cvt_i64_preds(i1 %x) {
-; CHECK: cvt.u64.pred rd0, p1;
-; CHECK: ret;
+; CHECK: selp.u64 rd0, 1, 0, p1;
+; CHECK-NEXT: ret;
        %a = zext i1 %x to i64
        ret i64 %a
 }
 
 define ptx_device i64 @cvt_i64_i16(i16 %x) {
 ; CHECK: cvt.u64.u16 rd0, rh1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = zext i16 %x to i64
        ret i64 %a
 }
 
 define ptx_device i64 @cvt_i64_i32(i32 %x) {
 ; CHECK: cvt.u64.u32 rd0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = zext i32 %x to i64
        ret i64 %a
 }
 
 define ptx_device i64 @cvt_i64_f32(float %x) {
 ; CHECK: cvt.rzi.u64.f32 rd0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptoui float %x to i64
        ret i64 %a
 }
@@ -162,36 +172,36 @@ define ptx_device i64 @cvt_i64_f64(double %x) {
 ; f32
 
 define ptx_device float @cvt_f32_preds(i1 %x) {
-; CHECK: cvt.rn.f32.pred r0, p1;
-; CHECK: ret;
+; CHECK: selp.f32 r0, 0F3F800000, 0F00000000, p1;
+; CHECK-NEXT: ret;
        %a = uitofp i1 %x to float
        ret float %a
 }
 
 define ptx_device float @cvt_f32_i16(i16 %x) {
 ; CHECK: cvt.rn.f32.u16 r0, rh1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i16 %x to float
        ret float %a
 }
 
 define ptx_device float @cvt_f32_i32(i32 %x) {
 ; CHECK: cvt.rn.f32.u32 r0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i32 %x to float
        ret float %a
 }
 
 define ptx_device float @cvt_f32_i64(i64 %x) {
 ; CHECK: cvt.rn.f32.u64 r0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i64 %x to float
        ret float %a
 }
 
 define ptx_device float @cvt_f32_f64(double %x) {
 ; CHECK: cvt.rn.f32.f64 r0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fptrunc double %x to float
        ret float %a
 }
@@ -199,36 +209,36 @@ define ptx_device float @cvt_f32_f64(double %x) {
 ; f64
 
 define ptx_device double @cvt_f64_preds(i1 %x) {
-; CHECK: cvt.rn.f64.pred rd0, p1;
-; CHECK: ret;
+; CHECK: selp.f64 rd0, 0D3F80000000000000, 0D0000000000000000, p1;
+; CHECK-NEXT: ret;
        %a = uitofp i1 %x to double
        ret double %a
 }
 
 define ptx_device double @cvt_f64_i16(i16 %x) {
 ; CHECK: cvt.rn.f64.u16 rd0, rh1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i16 %x to double
        ret double %a
 }
 
 define ptx_device double @cvt_f64_i32(i32 %x) {
 ; CHECK: cvt.rn.f64.u32 rd0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i32 %x to double
        ret double %a
 }
 
 define ptx_device double @cvt_f64_i64(i64 %x) {
 ; CHECK: cvt.rn.f64.u64 rd0, rd1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = uitofp i64 %x to double
        ret double %a
 }
 
 define ptx_device double @cvt_f64_f32(float %x) {
 ; CHECK: cvt.f64.f32 rd0, r1;
-; CHECK: ret;
+; CHECK-NEXT: ret;
        %a = fpext float %x to double
        ret double %a
 }
index 5836122049e678b756a79e53431436ee512d18dc..a5d7484268ce9eeffb6cf37db5f7f5da6dabfc16 100644 (file)
@@ -2,7 +2,7 @@
 
 define ptx_device i32 @test_setp_eq_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.eq.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp eq i32 %x, %y
        %z = zext i1 %p to i32
@@ -11,7 +11,7 @@ define ptx_device i32 @test_setp_eq_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_ne_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.ne.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ne i32 %x, %y
        %z = zext i1 %p to i32
@@ -20,7 +20,7 @@ define ptx_device i32 @test_setp_ne_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_lt_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.lt.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ult i32 %x, %y
        %z = zext i1 %p to i32
@@ -29,7 +29,7 @@ define ptx_device i32 @test_setp_lt_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_le_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.le.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ule i32 %x, %y
        %z = zext i1 %p to i32
@@ -38,7 +38,7 @@ define ptx_device i32 @test_setp_le_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_gt_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.gt.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ugt i32 %x, %y
        %z = zext i1 %p to i32
@@ -47,7 +47,7 @@ define ptx_device i32 @test_setp_gt_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_ge_u32_rr(i32 %x, i32 %y) {
 ; CHECK: setp.ge.u32 p0, r1, r2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp uge i32 %x, %y
        %z = zext i1 %p to i32
@@ -56,7 +56,7 @@ define ptx_device i32 @test_setp_ge_u32_rr(i32 %x, i32 %y) {
 
 define ptx_device i32 @test_setp_eq_u32_ri(i32 %x) {
 ; CHECK: setp.eq.u32 p0, r1, 1;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp eq i32 %x, 1
        %z = zext i1 %p to i32
@@ -65,7 +65,7 @@ define ptx_device i32 @test_setp_eq_u32_ri(i32 %x) {
 
 define ptx_device i32 @test_setp_ne_u32_ri(i32 %x) {
 ; CHECK: setp.ne.u32 p0, r1, 1;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ne i32 %x, 1
        %z = zext i1 %p to i32
@@ -74,7 +74,7 @@ define ptx_device i32 @test_setp_ne_u32_ri(i32 %x) {
 
 define ptx_device i32 @test_setp_lt_u32_ri(i32 %x) {
 ; CHECK: setp.eq.u32 p0, r1, 0;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ult i32 %x, 1
        %z = zext i1 %p to i32
@@ -83,7 +83,7 @@ define ptx_device i32 @test_setp_lt_u32_ri(i32 %x) {
 
 define ptx_device i32 @test_setp_le_u32_ri(i32 %x) {
 ; CHECK: setp.lt.u32 p0, r1, 2;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ule i32 %x, 1
        %z = zext i1 %p to i32
@@ -92,7 +92,7 @@ define ptx_device i32 @test_setp_le_u32_ri(i32 %x) {
 
 define ptx_device i32 @test_setp_gt_u32_ri(i32 %x) {
 ; CHECK: setp.gt.u32 p0, r1, 1;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp ugt i32 %x, 1
        %z = zext i1 %p to i32
@@ -101,7 +101,7 @@ define ptx_device i32 @test_setp_gt_u32_ri(i32 %x) {
 
 define ptx_device i32 @test_setp_ge_u32_ri(i32 %x) {
 ; CHECK: setp.ne.u32 p0, r1, 0;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %p = icmp uge i32 %x, 1
        %z = zext i1 %p to i32
@@ -111,7 +111,7 @@ define ptx_device i32 @test_setp_ge_u32_ri(i32 %x) {
 define ptx_device i32 @test_setp_4_op_format_1(i32 %x, i32 %y, i32 %u, i32 %v) {
 ; CHECK: setp.gt.u32 p0, r3, r4;
 ; CHECK-NEXT: setp.eq.and.u32 p0, r1, r2, p0;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %c = icmp eq i32 %x, %y
        %d = icmp ugt i32 %u, %v
@@ -121,9 +121,9 @@ define ptx_device i32 @test_setp_4_op_format_1(i32 %x, i32 %y, i32 %u, i32 %v) {
 }
 
 define ptx_device i32 @test_setp_4_op_format_2(i32 %x, i32 %y, i32 %w) {
-; CHECK: cvt.pred.u32 p0, r3;
+; CHECK: setp.gt.b32 p0, r3, 0;
 ; CHECK-NEXT: setp.eq.and.u32 p0, r1, r2, !p0;
-; CHECK-NEXT: cvt.u32.pred r0, p0;
+; CHECK-NEXT: selp.u32 r0, 1, 0, p0;
 ; CHECK-NEXT: ret;
        %c = trunc i32 %w to i1
        %d = icmp eq i32 %x, %y