Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 1 | ; Tests to make sure elimination of casts is working correctly |
Chris Lattner | 23405aa | 2006-04-12 18:07:41 +0000 | [diff] [blame] | 2 | ; RUN: llvm-as < %s | opt -instcombine -disable-output && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 3 | ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | grep '%c' | notcast |
Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 4 | |
Chris Lattner | 0ca4418 | 2004-02-22 05:24:09 +0000 | [diff] [blame] | 5 | %inbuf = external global [32832 x ubyte] |
| 6 | |
Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 7 | implementation |
| 8 | |
Chris Lattner | c7f9637 | 2002-08-02 18:48:09 +0000 | [diff] [blame] | 9 | int %test1(int %A) { |
Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 10 | %c1 = cast int %A to uint |
| 11 | %c2 = cast uint %c1 to int |
| 12 | ret int %c2 |
Chris Lattner | c7f9637 | 2002-08-02 18:48:09 +0000 | [diff] [blame] | 13 | } |
Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 14 | |
Chris Lattner | c7f9637 | 2002-08-02 18:48:09 +0000 | [diff] [blame] | 15 | ulong %test2(ubyte %A) { |
Chris Lattner | fa49f81 | 2002-05-02 17:01:39 +0000 | [diff] [blame] | 16 | %c1 = cast ubyte %A to ushort |
| 17 | %c2 = cast ushort %c1 to uint |
| 18 | %Ret = cast uint %c2 to ulong |
| 19 | ret ulong %Ret |
Chris Lattner | c7f9637 | 2002-08-02 18:48:09 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | ulong %test3(ulong %A) { ; This function should just use bitwise AND |
| 23 | %c1 = cast ulong %A to ubyte |
| 24 | %c2 = cast ubyte %c1 to ulong |
| 25 | ret ulong %c2 |
| 26 | } |
Chris Lattner | 8decbcb | 2002-08-15 16:13:29 +0000 | [diff] [blame] | 27 | |
| 28 | uint %test4(int %A, int %B) { |
| 29 | %COND = setlt int %A, %B |
| 30 | %c = cast bool %COND to ubyte ; Booleans are unsigned integrals |
| 31 | %result = cast ubyte %c to uint ; for the cast elim purpose |
| 32 | ret uint %result |
| 33 | } |
| 34 | |
| 35 | int %test5(bool %B) { |
| 36 | %c = cast bool %B to ubyte ; This cast should get folded into |
| 37 | %result = cast ubyte %c to int ; this cast |
| 38 | ret int %result |
| 39 | } |
| 40 | |
| 41 | int %test6(ulong %A) { |
| 42 | %c1 = cast ulong %A to uint |
| 43 | %res = cast uint %c1 to int |
| 44 | ret int %res |
| 45 | } |
| 46 | |
| 47 | long %test7(bool %A) { |
| 48 | %c1 = cast bool %A to int |
| 49 | %res = cast int %c1 to long |
| 50 | ret long %res |
| 51 | } |
| 52 | |
| 53 | long %test8(sbyte %A) { |
| 54 | %c1 = cast sbyte %A to ulong |
| 55 | %res = cast ulong %c1 to long |
| 56 | ret long %res |
| 57 | } |
| 58 | |
Chris Lattner | a30b70f | 2002-09-23 23:39:17 +0000 | [diff] [blame] | 59 | short %test9(short %A) { |
| 60 | %c1 = cast short %A to int |
| 61 | %c2 = cast int %c1 to short |
| 62 | ret short %c2 |
| 63 | } |
| 64 | |
| 65 | short %test10(short %A) { |
| 66 | %c1 = cast short %A to uint |
| 67 | %c2 = cast uint %c1 to short |
| 68 | ret short %c2 |
| 69 | } |
Chris Lattner | 8186a94 | 2003-06-23 21:48:26 +0000 | [diff] [blame] | 70 | |
Chris Lattner | d1c5ea1 | 2003-10-07 22:53:46 +0000 | [diff] [blame] | 71 | declare void %varargs(int, ...) |
| 72 | |
| 73 | void %test11(int* %P) { |
| 74 | %c = cast int* %P to short* |
| 75 | call void(int, ...)* %varargs(int 5, short* %c) |
| 76 | ret void |
| 77 | } |
Chris Lattner | fc424c3 | 2003-11-02 05:56:58 +0000 | [diff] [blame] | 78 | |
| 79 | int* %test12() { |
| 80 | %p = malloc [4 x sbyte] |
| 81 | %c = cast [4 x sbyte]* %p to int* |
| 82 | ret int* %c |
| 83 | } |
Chris Lattner | 0ca4418 | 2004-02-22 05:24:09 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 0ca4418 | 2004-02-22 05:24:09 +0000 | [diff] [blame] | 85 | ubyte *%test13(long %A) { |
| 86 | %c = getelementptr [0 x ubyte]* cast ([32832 x ubyte]* %inbuf to [0 x ubyte]*), long 0, long %A |
| 87 | ret ubyte* %c |
| 88 | } |
Chris Lattner | 3c4de7f | 2004-02-23 07:16:03 +0000 | [diff] [blame] | 89 | |
| 90 | bool %test14(sbyte %A) { |
Chris Lattner | 2e13cea | 2004-05-25 04:28:43 +0000 | [diff] [blame] | 91 | %c = cast sbyte %A to ubyte |
| 92 | %X = setlt ubyte %c, 128 ; setge %A, 0 |
Chris Lattner | 3c4de7f | 2004-02-23 07:16:03 +0000 | [diff] [blame] | 93 | ret bool %X |
| 94 | } |
| 95 | |
| 96 | bool %test15(ubyte %A) { |
Chris Lattner | 2e13cea | 2004-05-25 04:28:43 +0000 | [diff] [blame] | 97 | %c = cast ubyte %A to sbyte |
| 98 | %X = setlt sbyte %c, 0 ; setgt %A, 127 |
Chris Lattner | 3c4de7f | 2004-02-23 07:16:03 +0000 | [diff] [blame] | 99 | ret bool %X |
| 100 | } |
| 101 | |
Chris Lattner | 2e13cea | 2004-05-25 04:28:43 +0000 | [diff] [blame] | 102 | bool %test16(int* %P) { |
| 103 | %c = cast int* %P to bool ;; setne P, null |
| 104 | ret bool %c |
| 105 | } |
Chris Lattner | 6c08892 | 2004-07-20 00:57:21 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 6c08892 | 2004-07-20 00:57:21 +0000 | [diff] [blame] | 107 | short %test17(bool %tmp3) { |
| 108 | %c = cast bool %tmp3 to int |
| 109 | %t86 = cast int %c to short |
| 110 | ret short %t86 |
| 111 | } |
| 112 | |
| 113 | short %test18(sbyte %tmp3) { |
| 114 | %c = cast sbyte %tmp3 to int |
| 115 | %t86 = cast int %c to short |
| 116 | ret short %t86 |
| 117 | } |
| 118 | |
Chris Lattner | b5ddea5 | 2004-09-29 03:18:20 +0000 | [diff] [blame] | 119 | bool %test19(int %X) { |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 120 | %c = cast int %X to long |
| 121 | %Z = setlt long %c, 12345 |
Chris Lattner | b5ddea5 | 2004-09-29 03:18:20 +0000 | [diff] [blame] | 122 | ret bool %Z |
| 123 | } |
| 124 | |
| 125 | bool %test20(bool %B) { |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 126 | %c = cast bool %B to int |
| 127 | %D = setlt int %c, -1 |
Chris Lattner | b5ddea5 | 2004-09-29 03:18:20 +0000 | [diff] [blame] | 128 | ret bool %D ;; false |
| 129 | } |
Chris Lattner | 296fcba | 2005-01-01 16:13:43 +0000 | [diff] [blame] | 130 | |
| 131 | uint %test21(uint %X) { |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 132 | %c1 = cast uint %X to sbyte |
| 133 | %c2 = cast sbyte %c1 to uint ;; sext -> zext -> and -> nop |
| 134 | %RV = and uint %c2, 255 |
Chris Lattner | 296fcba | 2005-01-01 16:13:43 +0000 | [diff] [blame] | 135 | ret uint %RV |
| 136 | } |
| 137 | |
| 138 | uint %test22(uint %X) { |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 139 | %c1 = cast uint %X to sbyte |
| 140 | %c2 = cast sbyte %c1 to uint ;; sext -> zext -> and -> nop |
| 141 | %RV = shl uint %c2, ubyte 24 |
Chris Lattner | 296fcba | 2005-01-01 16:13:43 +0000 | [diff] [blame] | 142 | ret uint %RV |
| 143 | } |
| 144 | |
| 145 | int %test23(int %X) { |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 146 | %c1 = cast int %X to ushort ;; Turn into an AND even though X |
| 147 | %c2 = cast ushort %c1 to int ;; and Z are signed. |
| 148 | ret int %c2 |
Chris Lattner | 296fcba | 2005-01-01 16:13:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | bool %test24(bool %C) { |
| 152 | %X = select bool %C, uint 14, uint 1234 |
Chris Lattner | 2a73a0b | 2005-01-31 05:17:31 +0000 | [diff] [blame] | 153 | %c = cast uint %X to bool ;; Fold cast into select |
| 154 | ret bool %c |
Chris Lattner | 296fcba | 2005-01-01 16:13:43 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 0f99119 | 2005-01-31 05:51:18 +0000 | [diff] [blame] | 157 | void %test25(int** %P) { |
| 158 | %c = cast int** %P to float** |
| 159 | store float* null, float** %c ;; Fold cast into null |
| 160 | ret void |
| 161 | } |
| 162 | |
Chris Lattner | 6b8800d | 2006-01-19 07:39:20 +0000 | [diff] [blame] | 163 | int %test26(float %F) { |
| 164 | %c = cast float %F to double ;; no need to cast from float->double. |
| 165 | %D = cast double %c to int |
| 166 | ret int %D |
| 167 | } |
Chris Lattner | 23405aa | 2006-04-12 18:07:41 +0000 | [diff] [blame] | 168 | |
| 169 | [4 x float]* %test27([9 x [4 x float]]* %A) { |
| 170 | %c = cast [9 x [4 x float]]* %A to [4 x float]* |
| 171 | ret [4 x float]* %c |
| 172 | } |
| 173 | |
| 174 | float* %test28([4 x float]* %A) { |
| 175 | %c = cast [4 x float]* %A to float* |
| 176 | ret float* %c |
| 177 | } |
| 178 | |
Chris Lattner | a0771b8 | 2006-05-05 06:38:40 +0000 | [diff] [blame] | 179 | uint %test29(uint %c1, uint %c2) { |
| 180 | %tmp1 = cast uint %c1 to ubyte |
| 181 | %tmp4.mask = cast uint %c2 to ubyte |
| 182 | %tmp = or ubyte %tmp4.mask, %tmp1 |
| 183 | %tmp10 = cast ubyte %tmp to uint |
| 184 | ret uint %tmp10 |
| 185 | } |
| 186 | |
Chris Lattner | 5250bae | 2006-05-13 02:00:07 +0000 | [diff] [blame] | 187 | uint %test30(uint %c1) { |
| 188 | %c2 = cast uint %c1 to ubyte |
| 189 | %c3 = xor ubyte %c2, 1 |
| 190 | %c4 = cast ubyte %c3 to uint |
| 191 | ret uint %c4 |
| 192 | } |
| 193 | |
Chris Lattner | e04deb9 | 2006-09-18 05:25:10 +0000 | [diff] [blame] | 194 | bool %test31(ulong %A) { |
| 195 | %B = cast ulong %A to int |
| 196 | %C = and int %B, 42 |
| 197 | %D = seteq int %C, 10 |
| 198 | ret bool %D |
| 199 | } |
Chris Lattner | 5250bae | 2006-05-13 02:00:07 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 537cf96 | 2006-10-01 19:39:45 +0000 | [diff] [blame] | 201 | |
| 202 | void %test32(double** %tmp) { |
| 203 | %tmp8 = malloc [16 x sbyte] |
| 204 | %tmp8 = cast [16 x sbyte]* %tmp8 to double* |
| 205 | store double* %tmp8, double** %tmp |
| 206 | ret void |
| 207 | } |
| 208 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 209 | uint %test33(uint %c1) { |
| 210 | %x = bitcast uint %c1 to float |
| 211 | %y = bitcast float %x to uint |
| 212 | ret uint %y |
| 213 | } |