Tanya Lattner | 2393a24 | 2004-11-06 23:08:26 +0000 | [diff] [blame] | 1 | ; RUN: llvm-as -f %s -o - | llc |
| 2 | |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 3 | %AConst = constant int 123 |
| 4 | |
Vikram S. Adve | f289911 | 2002-10-13 00:44:00 +0000 | [diff] [blame] | 5 | %Domain = type { sbyte*, int, int*, int, int, int*, %Domain* } |
| 6 | |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 7 | implementation |
| 8 | |
| 9 | ; Test setting values of different constants in registers. |
| 10 | ; |
| 11 | void "testConsts"(int %N, float %X) |
| 12 | begin |
| 13 | ; <label>:0 |
| 14 | %a = add int %N, 1 ; 1 should be put in immed field |
Brian Gaeke | 3d55eda | 2003-11-22 06:18:35 +0000 | [diff] [blame] | 15 | %i = add int %N, 12345678 ; constant has to be loaded |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 16 | %b = add short 4, 3 ; one of the operands shd be immed |
| 17 | %c = add float %X, 0.0 ; will this be optimzzed? |
| 18 | %d = add float %X, 3.1415 ; constant has to be loaded |
| 19 | %f = add uint 4294967295, 10 ; result shd be 9 (not in immed fld) |
| 20 | %g = add ushort 20, 65535 ; result shd be 19 (65536 in immed fld) |
Brian Gaeke | 3d55eda | 2003-11-22 06:18:35 +0000 | [diff] [blame] | 21 | %j = add ushort 65535, 30 ; result shd be 29 (not in immed fld) |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 22 | %h = add ubyte 40, 255 ; result shd be 39 (255 in immed fld) |
Brian Gaeke | 3d55eda | 2003-11-22 06:18:35 +0000 | [diff] [blame] | 23 | %k = add ubyte 255, 50 ; result shd be 49 (not in immed fld) |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 24 | |
| 25 | ret void |
| 26 | end |
| 27 | |
| 28 | ; A SetCC whose result is used should produce instructions to |
| 29 | ; compute the boolean value in a register. One whose result |
| 30 | ; is unused will only generate the condition code but not |
| 31 | ; the boolean result. |
| 32 | ; |
| 33 | void "unusedBool"(int * %x, int * %y) |
| 34 | begin |
| 35 | ; <label>:0 ; [#uses=0] |
| 36 | seteq int * %x, %y ; <bool>:0 [#uses=1] |
Chris Lattner | d022471 | 2002-08-14 19:29:38 +0000 | [diff] [blame] | 37 | xor bool %0, true ; <bool>:1 [#uses=0] |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 38 | setne int * %x, %y ; <bool>:2 [#uses=0] |
| 39 | ret void |
| 40 | end |
| 41 | |
| 42 | ; A constant argument to a Phi produces a Cast instruction in the |
| 43 | ; corresponding predecessor basic block. This checks a few things: |
| 44 | ; -- phi arguments coming from the bottom of the same basic block |
| 45 | ; (they should not be forward substituted in the machine code!) |
| 46 | ; -- code generation for casts of various types |
| 47 | ; -- use of immediate fields for integral constants of different sizes |
| 48 | ; -- branch on a constant condition |
| 49 | ; |
| 50 | void "mergeConstants"(int * %x, int * %y) |
| 51 | begin |
| 52 | ; <label>:0 |
| 53 | br label %Top |
| 54 | Top: |
| 55 | phi int [ 0, %0 ], [ 1, %Top ], [ 524288, %Next ] |
| 56 | phi float [ 0.0, %0 ], [ 1.0, %Top ], [ 2.0, %Next ] |
| 57 | phi double [ 0.5, %0 ], [ 1.5, %Top ], [ 2.5, %Next ] |
| 58 | phi bool [ true, %0 ], [ false,%Top ], [ true, %Next ] |
| 59 | br bool true, label %Top, label %Next |
| 60 | Next: |
| 61 | br label %Top |
| 62 | end |
| 63 | |
| 64 | |
| 65 | |
| 66 | ; A constant argument to a cast used only once should be forward substituted |
| 67 | ; and loaded where needed, which happens is: |
| 68 | ; -- User of cast has no immediate field |
| 69 | ; -- User of cast has immediate field but constant is too large to fit |
| 70 | ; or constant is not resolved until later (e.g., global address) |
| 71 | ; -- User of cast uses it as a call arg. or return value so it is an implicit |
| 72 | ; use but has to be loaded into a virtual register so that the reg. |
| 73 | ; allocator can allocate the appropriate phys. reg. for it |
| 74 | ; |
| 75 | int* "castconst"(float) |
| 76 | begin |
| 77 | ; <label>:0 |
| 78 | %castbig = cast ulong 99999999 to int |
| 79 | %castsmall = cast ulong 1 to int |
| 80 | %usebig = add int %castbig, %castsmall |
| 81 | |
| 82 | %castglob = cast int* %AConst to long* |
| 83 | %dummyl = load long* %castglob |
| 84 | |
| 85 | %castnull = cast ulong 0 to int* |
| 86 | ret int* %castnull |
| 87 | end |
| 88 | |
| 89 | |
| 90 | |
| 91 | ; Test branch-on-comparison-with-zero, in two ways: |
| 92 | ; 1. can be folded |
| 93 | ; 2. cannot be folded because result of comparison is used twice |
| 94 | ; |
Chris Lattner | 92ae901 | 2002-08-01 20:48:21 +0000 | [diff] [blame] | 95 | void "testbool"(int %A, int %B) { |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 96 | br label %Top |
| 97 | Top: |
Chris Lattner | 92ae901 | 2002-08-01 20:48:21 +0000 | [diff] [blame] | 98 | %D = add int %A, %B |
| 99 | %E = sub int %D, -4 |
| 100 | %C = setle int %E, 0 |
| 101 | br bool %C, label %retlbl, label %loop |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 102 | |
| 103 | loop: |
Chris Lattner | 92ae901 | 2002-08-01 20:48:21 +0000 | [diff] [blame] | 104 | %F = add int %A, %B |
| 105 | %G = sub int %D, -4 |
| 106 | %D = setle int %G, 0 |
Chris Lattner | d022471 | 2002-08-14 19:29:38 +0000 | [diff] [blame] | 107 | %E = xor bool %D, true |
Chris Lattner | 92ae901 | 2002-08-01 20:48:21 +0000 | [diff] [blame] | 108 | br bool %E, label %loop, label %Top |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 109 | |
| 110 | retlbl: |
| 111 | ret void |
| 112 | end |
| 113 | |
| 114 | |
Vikram S. Adve | ddd5751 | 2002-08-22 03:05:13 +0000 | [diff] [blame] | 115 | ;; Test use of a boolean result in cast operations. |
| 116 | ;; Requires converting a condition code result into a 0/1 value in a reg. |
| 117 | ;; |
| 118 | implementation |
| 119 | |
| 120 | int %castbool(int %A, int %B) { |
| 121 | bb0: ; [#uses=0] |
| 122 | %cond213 = setlt int %A, %B ; <bool> [#uses=1] |
| 123 | %cast110 = cast bool %cond213 to ubyte ; <ubyte> [#uses=1] |
| 124 | %cast109 = cast ubyte %cast110 to int ; <int> [#uses=1] |
| 125 | ret int %cast109 |
| 126 | } |
| 127 | |
| 128 | |
| 129 | ;; Test use of a boolean result in arithmetic and logical operations. |
| 130 | ;; Requires converting a condition code result into a 0/1 value in a reg. |
| 131 | ;; |
| 132 | bool %boolexpr(bool %b, int %N) { |
| 133 | %b2 = setge int %N, 0 |
| 134 | %b3 = and bool %b, %b2 |
| 135 | ret bool %b3 |
| 136 | } |
| 137 | |
| 138 | |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 139 | ; Test branch on floating point comparison |
| 140 | ; |
| 141 | void "testfloatbool"(float %x, float %y) ; Def %0, %1 - float |
| 142 | begin |
| 143 | ; <label>:0 |
| 144 | br label %Top |
| 145 | Top: |
| 146 | %p = add float %x, %y ; Def 2 - float |
| 147 | %z = sub float %x, %y ; Def 3 - float |
| 148 | %b = setle float %p, %z ; Def 0 - bool |
Chris Lattner | d022471 | 2002-08-14 19:29:38 +0000 | [diff] [blame] | 149 | %c = xor bool %b, true ; Def 1 - bool |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 150 | br bool %b, label %Top, label %goon |
| 151 | goon: |
| 152 | ret void |
| 153 | end |
| 154 | |
| 155 | |
| 156 | ; Test cases where an LLVM instruction requires no machine |
| 157 | ; instructions (e.g., cast int* to long). But there are 2 cases: |
| 158 | ; 1. If the result register has only a single use and the use is in the |
| 159 | ; same basic block, the operand will be copy-propagated during |
| 160 | ; instruction selection. |
| 161 | ; 2. If the result register has multiple uses or is in a different |
| 162 | ; basic block, it cannot (or will not) be copy propagated during |
| 163 | ; instruction selection. It will generate a |
| 164 | ; copy instruction (add-with-0), but this copy should get coalesced |
| 165 | ; away by the register allocator. |
| 166 | ; |
| 167 | int "checkForward"(int %N, int* %A) |
| 168 | begin |
| 169 | |
| 170 | bb2: ;;<label> |
| 171 | %reg114 = shl int %N, ubyte 2 ;; |
Vikram S. Adve | ba3b3e8 | 2002-09-20 00:57:37 +0000 | [diff] [blame] | 172 | %cast115 = cast int %reg114 to long ;; reg114 will be propagated |
| 173 | %cast116 = cast int* %A to long ;; %A will be propagated |
| 174 | %reg116 = add long %cast116, %cast115 ;; |
| 175 | %castPtr = cast long %reg116 to int* ;; %A will be propagated |
| 176 | %reg118 = load int* %castPtr ;; |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 177 | %cast117 = cast int %reg118 to long ;; reg118 will be copied 'cos |
| 178 | %reg159 = add long 1234567, %cast117 ;; cast117 has 2 uses, here |
| 179 | %reg160 = add long 7654321, %cast117 ;; and here. |
Chris Lattner | 92ae901 | 2002-08-01 20:48:21 +0000 | [diff] [blame] | 180 | ret int 0 |
Vikram S. Adve | a7a1c7e | 2002-07-10 21:54:05 +0000 | [diff] [blame] | 181 | end |
Vikram S. Adve | 0f63e22 | 2002-08-15 14:09:56 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | ; Test case for unary NOT operation constructed from XOR. |
| 185 | ; |
| 186 | void "checkNot"(bool %b, int %i) |
| 187 | begin |
| 188 | %notB = xor bool %b, true |
| 189 | %notI = xor int %i, -1 |
| 190 | %F = setge int %notI, 100 |
| 191 | %J = add int %i, %i |
| 192 | %andNotB = and bool %F, %notB ;; should get folded with notB |
| 193 | %andNotI = and int %J, %notI ;; should get folded with notI |
| 194 | |
| 195 | %notB2 = xor bool true, %b ;; should become XNOR |
| 196 | %notI2 = xor int -1, %i ;; should become XNOR |
| 197 | |
| 198 | ret void |
| 199 | end |
Vikram S. Adve | f289911 | 2002-10-13 00:44:00 +0000 | [diff] [blame] | 200 | |
| 201 | |
| 202 | ; Test case for folding getelementptr into a load/store |
| 203 | ; |
| 204 | int "checkFoldGEP"(%Domain* %D, long %idx) |
| 205 | begin |
Chris Lattner | 1fbaa0a | 2003-01-15 18:06:37 +0000 | [diff] [blame] | 206 | %reg841 = getelementptr %Domain* %D, long 0, ubyte 1 |
Vikram S. Adve | f289911 | 2002-10-13 00:44:00 +0000 | [diff] [blame] | 207 | %reg820 = load int* %reg841 |
| 208 | ret int %reg820 |
| 209 | end |