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