Home | History | Annotate | Download | only in Float2Int
      1 ; RUN: opt < %s -float2int -S | FileCheck %s
      2 ; RUN: opt < %s -passes='float2int' -S | FileCheck %s
      3 
      4 ;
      5 ; Positive tests
      6 ;
      7 
      8 ; CHECK-LABEL: @simple1
      9 ; CHECK:  %1 = zext i8 %a to i32
     10 ; CHECK:  %2 = add i32 %1, 1
     11 ; CHECK:  %3 = trunc i32 %2 to i16
     12 ; CHECK:  ret i16 %3
     13 define i16 @simple1(i8 %a) {
     14   %1 = uitofp i8 %a to float
     15   %2 = fadd float %1, 1.0
     16   %3 = fptoui float %2 to i16
     17   ret i16 %3
     18 }
     19 
     20 ; CHECK-LABEL: @simple2
     21 ; CHECK:  %1 = zext i8 %a to i32
     22 ; CHECK:  %2 = sub i32 %1, 1
     23 ; CHECK:  %3 = trunc i32 %2 to i8
     24 ; CHECK:  ret i8 %3
     25 define i8 @simple2(i8 %a) {
     26   %1 = uitofp i8 %a to float
     27   %2 = fsub float %1, 1.0
     28   %3 = fptoui float %2 to i8
     29   ret i8 %3
     30 }
     31 
     32 ; CHECK-LABEL: @simple3
     33 ; CHECK:  %1 = zext i8 %a to i32
     34 ; CHECK:  %2 = sub i32 %1, 1
     35 ; CHECK:  ret i32 %2
     36 define i32 @simple3(i8 %a) {
     37   %1 = uitofp i8 %a to float
     38   %2 = fsub float %1, 1.0
     39   %3 = fptoui float %2 to i32
     40   ret i32 %3
     41 }
     42 
     43 ; CHECK-LABEL: @cmp
     44 ; CHECK:  %1 = zext i8 %a to i32
     45 ; CHECK:  %2 = zext i8 %b to i32
     46 ; CHECK:  %3 = icmp slt i32 %1, %2
     47 ; CHECK:  ret i1 %3
     48 define i1 @cmp(i8 %a, i8 %b) {
     49   %1 = uitofp i8 %a to float
     50   %2 = uitofp i8 %b to float
     51   %3 = fcmp ult float %1, %2
     52   ret i1 %3
     53 }
     54 
     55 ; CHECK-LABEL: @simple4
     56 ; CHECK:  %1 = zext i32 %a to i64
     57 ; CHECK:  %2 = add i64 %1, 1
     58 ; CHECK:  %3 = trunc i64 %2 to i32
     59 ; CHECK:  ret i32 %3
     60 define i32 @simple4(i32 %a) {
     61   %1 = uitofp i32 %a to double
     62   %2 = fadd double %1, 1.0
     63   %3 = fptoui double %2 to i32
     64   ret i32 %3
     65 }
     66 
     67 ; CHECK-LABEL: @simple5
     68 ; CHECK:  %1 = zext i8 %a to i32
     69 ; CHECK:  %2 = zext i8 %b to i32
     70 ; CHECK:  %3 = add i32 %1, 1
     71 ; CHECK:  %4 = mul i32 %3, %2
     72 ; CHECK:  ret i32 %4
     73 define i32 @simple5(i8 %a, i8 %b) {
     74   %1 = uitofp i8 %a to float
     75   %2 = uitofp i8 %b to float
     76   %3 = fadd float %1, 1.0
     77   %4 = fmul float %3, %2
     78   %5 = fptoui float %4 to i32
     79   ret i32 %5
     80 }
     81 
     82 ; The two chains don't interact - failure of one shouldn't
     83 ; cause failure of the other.
     84 
     85 ; CHECK-LABEL: @multi1
     86 ; CHECK:  %1 = zext i8 %a to i32
     87 ; CHECK:  %2 = zext i8 %b to i32
     88 ; CHECK:  %fc = uitofp i8 %c to float
     89 ; CHECK:  %x1 = add i32 %1, %2
     90 ; CHECK:  %z = fadd float %fc, %d
     91 ; CHECK:  %w = fptoui float %z to i32
     92 ; CHECK:  %r = add i32 %x1, %w
     93 ; CHECK:  ret i32 %r
     94 define i32 @multi1(i8 %a, i8 %b, i8 %c, float %d) {
     95   %fa = uitofp i8 %a to float
     96   %fb = uitofp i8 %b to float
     97   %fc = uitofp i8 %c to float
     98   %x = fadd float %fa, %fb
     99   %y = fptoui float %x to i32
    100   %z = fadd float %fc, %d
    101   %w = fptoui float %z to i32
    102   %r = add i32 %y, %w
    103   ret i32 %r
    104 }
    105 
    106 ; CHECK-LABEL: @simple_negzero
    107 ; CHECK:  %1 = zext i8 %a to i32
    108 ; CHECK:  %2 = add i32 %1, 0
    109 ; CHECK:  %3 = trunc i32 %2 to i16
    110 ; CHECK:  ret i16 %3
    111 define i16 @simple_negzero(i8 %a) {
    112   %1 = uitofp i8 %a to float
    113   %2 = fadd fast float %1, -0.0
    114   %3 = fptoui float %2 to i16
    115   ret i16 %3
    116 }
    117 
    118 ; CHECK-LABEL: @simple_negative
    119 ; CHECK: %1 = sext i8 %call to i32
    120 ; CHECK: %mul1 = mul i32 %1, -3
    121 ; CHECK: %2 = trunc i32 %mul1 to i8
    122 ; CHECK: %conv3 = sext i8 %2 to i32
    123 ; CHECK: ret i32 %conv3
    124 define i32 @simple_negative(i8 %call) {
    125   %conv1 = sitofp i8 %call to float
    126   %mul = fmul float %conv1, -3.000000e+00
    127   %conv2 = fptosi float %mul to i8
    128   %conv3 = sext i8 %conv2 to i32
    129   ret i32 %conv3
    130 }
    131 
    132 ;
    133 ; Negative tests
    134 ;
    135 
    136 ; CHECK-LABEL: @neg_multi1
    137 ; CHECK:  %fa = uitofp i8 %a to float
    138 ; CHECK:  %fc = uitofp i8 %c to float
    139 ; CHECK:  %x = fadd float %fa, %fc
    140 ; CHECK:  %y = fptoui float %x to i32
    141 ; CHECK:  %z = fadd float %fc, %d
    142 ; CHECK:  %w = fptoui float %z to i32
    143 ; CHECK:  %r = add i32 %y, %w
    144 ; CHECK:  ret i32 %r
    145 ; The two chains intersect, which means because one fails, no
    146 ; transform can occur.
    147 define i32 @neg_multi1(i8 %a, i8 %b, i8 %c, float %d) {
    148   %fa = uitofp i8 %a to float
    149   %fc = uitofp i8 %c to float
    150   %x = fadd float %fa, %fc
    151   %y = fptoui float %x to i32
    152   %z = fadd float %fc, %d
    153   %w = fptoui float %z to i32
    154   %r = add i32 %y, %w
    155   ret i32 %r
    156 }
    157 
    158 ; CHECK-LABEL: @neg_muld
    159 ; CHECK:  %fa = uitofp i32 %a to double
    160 ; CHECK:  %fb = uitofp i32 %b to double
    161 ; CHECK:  %mul = fmul double %fa, %fb
    162 ; CHECK:  %r = fptoui double %mul to i64
    163 ; CHECK:  ret i64 %r
    164 ; The i32 * i32 = i64, which has 64 bits, which is greater than the 52 bits
    165 ; that can be exactly represented in a double.
    166 define i64 @neg_muld(i32 %a, i32 %b) {
    167   %fa = uitofp i32 %a to double
    168   %fb = uitofp i32 %b to double
    169   %mul = fmul double %fa, %fb
    170   %r = fptoui double %mul to i64
    171   ret i64 %r
    172 }
    173 
    174 ; CHECK-LABEL: @neg_mulf
    175 ; CHECK:  %fa = uitofp i16 %a to float
    176 ; CHECK:  %fb = uitofp i16 %b to float
    177 ; CHECK:  %mul = fmul float %fa, %fb
    178 ; CHECK:  %r = fptoui float %mul to i32
    179 ; CHECK:  ret i32 %r
    180 ; The i16 * i16 = i32, which can't be represented in a float, but can in a
    181 ; double. This should fail, as the written code uses floats, not doubles so
    182 ; the original result may be inaccurate.
    183 define i32 @neg_mulf(i16 %a, i16 %b) {
    184   %fa = uitofp i16 %a to float
    185   %fb = uitofp i16 %b to float
    186   %mul = fmul float %fa, %fb
    187   %r = fptoui float %mul to i32
    188   ret i32 %r
    189 }
    190 
    191 ; CHECK-LABEL: @neg_cmp
    192 ; CHECK:  %1 = uitofp i8 %a to float
    193 ; CHECK:  %2 = uitofp i8 %b to float
    194 ; CHECK:  %3 = fcmp false float %1, %2
    195 ; CHECK:  ret i1 %3
    196 ; "false" doesn't have an icmp equivalent.
    197 define i1 @neg_cmp(i8 %a, i8 %b) {
    198   %1 = uitofp i8 %a to float
    199   %2 = uitofp i8 %b to float
    200   %3 = fcmp false float %1, %2
    201   ret i1 %3
    202 }
    203 
    204 ; CHECK-LABEL: @neg_div
    205 ; CHECK:  %1 = uitofp i8 %a to float
    206 ; CHECK:  %2 = fdiv float %1, 1.0
    207 ; CHECK:  %3 = fptoui float %2 to i16
    208 ; CHECK:  ret i16 %3
    209 ; Division isn't a supported operator.
    210 define i16 @neg_div(i8 %a) {
    211   %1 = uitofp i8 %a to float
    212   %2 = fdiv float %1, 1.0
    213   %3 = fptoui float %2 to i16
    214   ret i16 %3
    215 }
    216 
    217 ; CHECK-LABEL: @neg_remainder
    218 ; CHECK:  %1 = uitofp i8 %a to float
    219 ; CHECK:  %2 = fadd float %1, 1.2
    220 ; CHECK:  %3 = fptoui float %2 to i16
    221 ; CHECK:  ret i16 %3
    222 ; 1.2 is not an integer.
    223 define i16 @neg_remainder(i8 %a) {
    224   %1 = uitofp i8 %a to float
    225   %2 = fadd float %1, 1.25
    226   %3 = fptoui float %2 to i16
    227   ret i16 %3
    228 }
    229 
    230 ; CHECK-LABEL: @neg_toolarge
    231 ; CHECK:  %1 = uitofp i80 %a to fp128
    232 ; CHECK:  %2 = fadd fp128 %1, %1
    233 ; CHECK:  %3 = fptoui fp128 %2 to i80
    234 ; CHECK:  ret i80 %3
    235 ; i80 > i64, which is the largest bitwidth handleable by default.
    236 define i80 @neg_toolarge(i80 %a) {
    237   %1 = uitofp i80 %a to fp128
    238   %2 = fadd fp128 %1, %1
    239   %3 = fptoui fp128 %2 to i80
    240   ret i80 %3
    241 }
    242 
    243 ; CHECK-LABEL: @neg_calluser
    244 ; CHECK: sitofp
    245 ; CHECK: fcmp
    246 ; The sequence %1..%3 cannot be converted because %4 uses %2.
    247 define i32 @neg_calluser(i32 %value) {
    248   %1 = sitofp i32 %value to double
    249   %2 = fadd double %1, 1.0
    250   %3 = fcmp olt double %2, 0.000000e+00
    251   %4 = tail call double @g(double %2)
    252   %5 = fptosi double %4 to i32
    253   %6 = zext i1 %3 to i32
    254   %7 = add i32 %6, %5
    255   ret i32 %7
    256 }
    257 declare double @g(double)
    258 
    259 ; CHECK-LABEL: @neg_vector
    260 ; CHECK:  %1 = uitofp <4 x i8> %a to <4 x float>
    261 ; CHECK:  %2 = fptoui <4 x float> %1 to <4 x i16>
    262 ; CHECK:  ret <4 x i16> %2
    263 define <4 x i16> @neg_vector(<4 x i8> %a) {
    264   %1 = uitofp <4 x i8> %a to <4 x float>
    265   %2 = fptoui <4 x float> %1 to <4 x i16>
    266   ret <4 x i16> %2
    267 }
    268