Chris Lattner | b86bd2c | 2006-03-27 07:04:16 +0000 | [diff] [blame] | 1 | //===- README.txt - Notes for improving PowerPC-specific code gen ---------===// |
| 2 | |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3 | TODO: |
Nate Begeman | ef9531e | 2005-04-11 20:48:57 +0000 | [diff] [blame] | 4 | * gpr0 allocation |
Nate Begeman | 4a0de07 | 2004-10-26 04:10:53 +0000 | [diff] [blame] | 5 | * implement do-loop -> bdnz transform |
Nate Begeman | a6ed0aa | 2008-02-11 04:16:09 +0000 | [diff] [blame] | 6 | * lmw/stmw pass a la arm load store optimizer for prolog/epilog |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 7 | |
Nate Begeman | a63fee8 | 2006-02-03 05:17:06 +0000 | [diff] [blame] | 8 | ===-------------------------------------------------------------------------=== |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 9 | |
Chris Lattner | ddac706 | 2010-01-07 17:53:10 +0000 | [diff] [blame] | 10 | On PPC64, this: |
| 11 | |
| 12 | long f2 (long x) { return 0xfffffff000000000UL; } |
| 13 | long f3 (long x) { return 0x1ffffffffUL; } |
| 14 | |
| 15 | could compile into: |
| 16 | |
| 17 | _f2: |
| 18 | li r3,-1 |
| 19 | rldicr r3,r3,0,27 |
| 20 | blr |
| 21 | _f3: |
| 22 | li r3,-1 |
| 23 | rldicl r3,r3,0,31 |
| 24 | blr |
| 25 | |
| 26 | we produce: |
| 27 | |
| 28 | _f2: |
| 29 | lis r2, 4095 |
| 30 | ori r2, r2, 65535 |
| 31 | sldi r3, r2, 36 |
| 32 | blr |
| 33 | _f3: |
| 34 | li r2, 1 |
| 35 | sldi r2, r2, 32 |
| 36 | oris r2, r2, 65535 |
| 37 | ori r3, r2, 65535 |
| 38 | blr |
| 39 | |
| 40 | |
| 41 | ===-------------------------------------------------------------------------=== |
| 42 | |
Nate Begeman | a63fee8 | 2006-02-03 05:17:06 +0000 | [diff] [blame] | 43 | Support 'update' load/store instructions. These are cracked on the G5, but are |
| 44 | still a codesize win. |
| 45 | |
Chris Lattner | 26ddb50 | 2006-11-10 01:33:53 +0000 | [diff] [blame] | 46 | With preinc enabled, this: |
| 47 | |
| 48 | long *%test4(long *%X, long *%dest) { |
| 49 | %Y = getelementptr long* %X, int 4 |
| 50 | %A = load long* %Y |
| 51 | store long %A, long* %dest |
| 52 | ret long* %Y |
| 53 | } |
| 54 | |
| 55 | compiles to: |
| 56 | |
| 57 | _test4: |
| 58 | mr r2, r3 |
| 59 | lwzu r5, 32(r2) |
| 60 | lwz r3, 36(r3) |
| 61 | stw r5, 0(r4) |
| 62 | stw r3, 4(r4) |
| 63 | mr r3, r2 |
| 64 | blr |
| 65 | |
| 66 | with -sched=list-burr, I get: |
| 67 | |
| 68 | _test4: |
| 69 | lwz r2, 36(r3) |
| 70 | lwzu r5, 32(r3) |
| 71 | stw r2, 4(r4) |
| 72 | stw r5, 0(r4) |
| 73 | blr |
| 74 | |
Nate Begeman | a63fee8 | 2006-02-03 05:17:06 +0000 | [diff] [blame] | 75 | ===-------------------------------------------------------------------------=== |
| 76 | |
Chris Lattner | 6e11295 | 2006-11-07 18:30:21 +0000 | [diff] [blame] | 77 | We compile the hottest inner loop of viterbi to: |
| 78 | |
| 79 | li r6, 0 |
| 80 | b LBB1_84 ;bb432.i |
| 81 | LBB1_83: ;bb420.i |
| 82 | lbzx r8, r5, r7 |
| 83 | addi r6, r7, 1 |
| 84 | stbx r8, r4, r7 |
| 85 | LBB1_84: ;bb432.i |
| 86 | mr r7, r6 |
| 87 | cmplwi cr0, r7, 143 |
| 88 | bne cr0, LBB1_83 ;bb420.i |
| 89 | |
| 90 | The CBE manages to produce: |
| 91 | |
| 92 | li r0, 143 |
| 93 | mtctr r0 |
| 94 | loop: |
| 95 | lbzx r2, r2, r11 |
| 96 | stbx r0, r2, r9 |
| 97 | addi r2, r2, 1 |
| 98 | bdz later |
| 99 | b loop |
| 100 | |
| 101 | This could be much better (bdnz instead of bdz) but it still beats us. If we |
| 102 | produced this with bdnz, the loop would be a single dispatch group. |
| 103 | |
| 104 | ===-------------------------------------------------------------------------=== |
| 105 | |
Chris Lattner | 6a250ec | 2006-10-13 20:20:58 +0000 | [diff] [blame] | 106 | Compile: |
| 107 | |
| 108 | void foo(int *P) { |
| 109 | if (P) *P = 0; |
| 110 | } |
| 111 | |
| 112 | into: |
| 113 | |
| 114 | _foo: |
| 115 | cmpwi cr0,r3,0 |
| 116 | beqlr cr0 |
| 117 | li r0,0 |
| 118 | stw r0,0(r3) |
| 119 | blr |
| 120 | |
| 121 | This is effectively a simple form of predication. |
| 122 | |
| 123 | ===-------------------------------------------------------------------------=== |
| 124 | |
Chris Lattner | a3c4454 | 2005-08-24 18:15:24 +0000 | [diff] [blame] | 125 | Lump the constant pool for each function into ONE pic object, and reference |
| 126 | pieces of it as offsets from the start. For functions like this (contrived |
| 127 | to have lots of constants obviously): |
| 128 | |
| 129 | double X(double Y) { return (Y*1.23 + 4.512)*2.34 + 14.38; } |
| 130 | |
| 131 | We generate: |
| 132 | |
| 133 | _X: |
| 134 | lis r2, ha16(.CPI_X_0) |
| 135 | lfd f0, lo16(.CPI_X_0)(r2) |
| 136 | lis r2, ha16(.CPI_X_1) |
| 137 | lfd f2, lo16(.CPI_X_1)(r2) |
| 138 | fmadd f0, f1, f0, f2 |
| 139 | lis r2, ha16(.CPI_X_2) |
| 140 | lfd f1, lo16(.CPI_X_2)(r2) |
| 141 | lis r2, ha16(.CPI_X_3) |
| 142 | lfd f2, lo16(.CPI_X_3)(r2) |
| 143 | fmadd f1, f0, f1, f2 |
| 144 | blr |
| 145 | |
| 146 | It would be better to materialize .CPI_X into a register, then use immediates |
| 147 | off of the register to avoid the lis's. This is even more important in PIC |
| 148 | mode. |
| 149 | |
Chris Lattner | 39b248b | 2006-02-02 23:50:22 +0000 | [diff] [blame] | 150 | Note that this (and the static variable version) is discussed here for GCC: |
| 151 | http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html |
| 152 | |
Chris Lattner | aabd035 | 2007-08-23 15:16:03 +0000 | [diff] [blame] | 153 | Here's another example (the sgn function): |
| 154 | double testf(double a) { |
| 155 | return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0); |
| 156 | } |
| 157 | |
| 158 | it produces a BB like this: |
| 159 | LBB1_1: ; cond_true |
| 160 | lis r2, ha16(LCPI1_0) |
| 161 | lfs f0, lo16(LCPI1_0)(r2) |
| 162 | lis r2, ha16(LCPI1_1) |
| 163 | lis r3, ha16(LCPI1_2) |
| 164 | lfs f2, lo16(LCPI1_2)(r3) |
| 165 | lfs f3, lo16(LCPI1_1)(r2) |
| 166 | fsub f0, f0, f1 |
| 167 | fsel f1, f0, f2, f3 |
| 168 | blr |
| 169 | |
Chris Lattner | a3c4454 | 2005-08-24 18:15:24 +0000 | [diff] [blame] | 170 | ===-------------------------------------------------------------------------=== |
Nate Begeman | 92cce90 | 2005-09-06 15:30:48 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 33c1dab | 2006-02-03 06:22:11 +0000 | [diff] [blame] | 172 | PIC Code Gen IPO optimization: |
| 173 | |
| 174 | Squish small scalar globals together into a single global struct, allowing the |
| 175 | address of the struct to be CSE'd, avoiding PIC accesses (also reduces the size |
| 176 | of the GOT on targets with one). |
| 177 | |
| 178 | Note that this is discussed here for GCC: |
| 179 | http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html |
| 180 | |
| 181 | ===-------------------------------------------------------------------------=== |
| 182 | |
Nate Begeman | 92cce90 | 2005-09-06 15:30:48 +0000 | [diff] [blame] | 183 | Implement Newton-Rhapson method for improving estimate instructions to the |
| 184 | correct accuracy, and implementing divide as multiply by reciprocal when it has |
Dan Gohman | d2cb3d2 | 2009-07-24 00:30:09 +0000 | [diff] [blame] | 185 | more than one use. Itanium would want this too. |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 186 | |
| 187 | ===-------------------------------------------------------------------------=== |
| 188 | |
Chris Lattner | 62c08dd | 2005-12-08 07:13:28 +0000 | [diff] [blame] | 189 | Compile offsets from allocas: |
| 190 | |
| 191 | int *%test() { |
| 192 | %X = alloca { int, int } |
| 193 | %Y = getelementptr {int,int}* %X, int 0, uint 1 |
| 194 | ret int* %Y |
| 195 | } |
| 196 | |
| 197 | into a single add, not two: |
| 198 | |
| 199 | _test: |
| 200 | addi r2, r1, -8 |
| 201 | addi r3, r2, 4 |
| 202 | blr |
| 203 | |
| 204 | --> important for C++. |
| 205 | |
Chris Lattner | 39706e6 | 2005-12-22 17:19:28 +0000 | [diff] [blame] | 206 | ===-------------------------------------------------------------------------=== |
| 207 | |
Chris Lattner | 39706e6 | 2005-12-22 17:19:28 +0000 | [diff] [blame] | 208 | No loads or stores of the constants should be needed: |
| 209 | |
| 210 | struct foo { double X, Y; }; |
| 211 | void xxx(struct foo F); |
| 212 | void bar() { struct foo R = { 1.0, 2.0 }; xxx(R); } |
| 213 | |
Chris Lattner | 1db4b4f | 2006-01-16 17:53:00 +0000 | [diff] [blame] | 214 | ===-------------------------------------------------------------------------=== |
| 215 | |
Dale Johannesen | 7074fea | 2009-07-01 23:36:02 +0000 | [diff] [blame] | 216 | Darwin Stub removal: |
| 217 | |
| 218 | We still generate calls to foo$stub, and stubs, on Darwin. This is not |
Chris Lattner | c4b0b40 | 2009-07-02 01:24:34 +0000 | [diff] [blame] | 219 | necessary when building with the Leopard (10.5) or later linker, as stubs are |
| 220 | generated by ld when necessary. Parameterizing this based on the deployment |
| 221 | target (-mmacosx-version-min) is probably enough. x86-32 does this right, see |
| 222 | its logic. |
Dale Johannesen | 7074fea | 2009-07-01 23:36:02 +0000 | [diff] [blame] | 223 | |
| 224 | ===-------------------------------------------------------------------------=== |
| 225 | |
Chris Lattner | 98fbc2f | 2006-01-16 17:58:54 +0000 | [diff] [blame] | 226 | Darwin Stub LICM optimization: |
| 227 | |
| 228 | Loops like this: |
| 229 | |
| 230 | for (...) bar(); |
| 231 | |
| 232 | Have to go through an indirect stub if bar is external or linkonce. It would |
| 233 | be better to compile it as: |
| 234 | |
| 235 | fp = &bar; |
| 236 | for (...) fp(); |
| 237 | |
| 238 | which only computes the address of bar once (instead of each time through the |
| 239 | stub). This is Darwin specific and would have to be done in the code generator. |
| 240 | Probably not a win on x86. |
| 241 | |
| 242 | ===-------------------------------------------------------------------------=== |
| 243 | |
Chris Lattner | 98fbc2f | 2006-01-16 17:58:54 +0000 | [diff] [blame] | 244 | Simple IPO for argument passing, change: |
| 245 | void foo(int X, double Y, int Z) -> void foo(int X, int Z, double Y) |
| 246 | |
| 247 | the Darwin ABI specifies that any integer arguments in the first 32 bytes worth |
| 248 | of arguments get assigned to r3 through r10. That is, if you have a function |
| 249 | foo(int, double, int) you get r3, f1, r6, since the 64 bit double ate up the |
| 250 | argument bytes for r4 and r5. The trick then would be to shuffle the argument |
| 251 | order for functions we can internalize so that the maximum number of |
| 252 | integers/pointers get passed in regs before you see any of the fp arguments. |
| 253 | |
| 254 | Instead of implementing this, it would actually probably be easier to just |
| 255 | implement a PPC fastcc, where we could do whatever we wanted to the CC, |
| 256 | including having this work sanely. |
| 257 | |
| 258 | ===-------------------------------------------------------------------------=== |
| 259 | |
| 260 | Fix Darwin FP-In-Integer Registers ABI |
| 261 | |
| 262 | Darwin passes doubles in structures in integer registers, which is very very |
| 263 | bad. Add something like a BIT_CONVERT to LLVM, then do an i-p transformation |
| 264 | that percolates these things out of functions. |
| 265 | |
| 266 | Check out how horrible this is: |
| 267 | http://gcc.gnu.org/ml/gcc/2005-10/msg01036.html |
| 268 | |
| 269 | This is an extension of "interprocedural CC unmunging" that can't be done with |
| 270 | just fastcc. |
| 271 | |
| 272 | ===-------------------------------------------------------------------------=== |
| 273 | |
Chris Lattner | 56b6964 | 2006-01-31 02:55:28 +0000 | [diff] [blame] | 274 | Compile this: |
| 275 | |
Chris Lattner | 83e64ba | 2006-01-31 07:16:34 +0000 | [diff] [blame] | 276 | int foo(int a) { |
| 277 | int b = (a < 8); |
| 278 | if (b) { |
| 279 | return b * 3; // ignore the fact that this is always 3. |
| 280 | } else { |
| 281 | return 2; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | into something not this: |
| 286 | |
| 287 | _foo: |
| 288 | 1) cmpwi cr7, r3, 8 |
| 289 | mfcr r2, 1 |
| 290 | rlwinm r2, r2, 29, 31, 31 |
| 291 | 1) cmpwi cr0, r3, 7 |
| 292 | bgt cr0, LBB1_2 ; UnifiedReturnBlock |
| 293 | LBB1_1: ; then |
| 294 | rlwinm r2, r2, 0, 31, 31 |
| 295 | mulli r3, r2, 3 |
| 296 | blr |
| 297 | LBB1_2: ; UnifiedReturnBlock |
| 298 | li r3, 2 |
| 299 | blr |
| 300 | |
| 301 | In particular, the two compares (marked 1) could be shared by reversing one. |
| 302 | This could be done in the dag combiner, by swapping a BR_CC when a SETCC of the |
| 303 | same operands (but backwards) exists. In this case, this wouldn't save us |
| 304 | anything though, because the compares still wouldn't be shared. |
Chris Lattner | 0ddc180 | 2006-02-01 00:28:12 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 5a7efc9 | 2006-02-01 17:54:23 +0000 | [diff] [blame] | 306 | ===-------------------------------------------------------------------------=== |
| 307 | |
Chris Lattner | 275b884 | 2006-02-02 07:37:11 +0000 | [diff] [blame] | 308 | We should custom expand setcc instead of pretending that we have it. That |
| 309 | would allow us to expose the access of the crbit after the mfcr, allowing |
| 310 | that access to be trivially folded into other ops. A simple example: |
| 311 | |
| 312 | int foo(int a, int b) { return (a < b) << 4; } |
| 313 | |
| 314 | compiles into: |
| 315 | |
| 316 | _foo: |
| 317 | cmpw cr7, r3, r4 |
| 318 | mfcr r2, 1 |
| 319 | rlwinm r2, r2, 29, 31, 31 |
| 320 | slwi r3, r2, 4 |
| 321 | blr |
| 322 | |
Chris Lattner | d463f7f | 2006-02-03 01:49:49 +0000 | [diff] [blame] | 323 | ===-------------------------------------------------------------------------=== |
| 324 | |
Nate Begeman | a63fee8 | 2006-02-03 05:17:06 +0000 | [diff] [blame] | 325 | Fold add and sub with constant into non-extern, non-weak addresses so this: |
| 326 | |
| 327 | static int a; |
| 328 | void bar(int b) { a = b; } |
| 329 | void foo(unsigned char *c) { |
| 330 | *c = a; |
| 331 | } |
| 332 | |
| 333 | So that |
| 334 | |
| 335 | _foo: |
| 336 | lis r2, ha16(_a) |
| 337 | la r2, lo16(_a)(r2) |
| 338 | lbz r2, 3(r2) |
| 339 | stb r2, 0(r3) |
| 340 | blr |
| 341 | |
| 342 | Becomes |
| 343 | |
| 344 | _foo: |
| 345 | lis r2, ha16(_a+3) |
| 346 | lbz r2, lo16(_a+3)(r2) |
| 347 | stb r2, 0(r3) |
| 348 | blr |
Chris Lattner | 2138453 | 2006-02-05 05:27:35 +0000 | [diff] [blame] | 349 | |
| 350 | ===-------------------------------------------------------------------------=== |
| 351 | |
| 352 | We generate really bad code for this: |
| 353 | |
| 354 | int f(signed char *a, _Bool b, _Bool c) { |
| 355 | signed char t = 0; |
| 356 | if (b) t = *a; |
| 357 | if (c) *a = t; |
| 358 | } |
| 359 | |
Chris Lattner | 00d18f0 | 2006-03-01 06:36:20 +0000 | [diff] [blame] | 360 | ===-------------------------------------------------------------------------=== |
| 361 | |
| 362 | This: |
| 363 | int test(unsigned *P) { return *P >> 24; } |
| 364 | |
| 365 | Should compile to: |
| 366 | |
| 367 | _test: |
| 368 | lbz r3,0(r3) |
| 369 | blr |
| 370 | |
| 371 | not: |
| 372 | |
| 373 | _test: |
| 374 | lwz r2, 0(r3) |
| 375 | srwi r3, r2, 24 |
| 376 | blr |
| 377 | |
Chris Lattner | 5a63c47 | 2006-03-07 04:42:59 +0000 | [diff] [blame] | 378 | ===-------------------------------------------------------------------------=== |
| 379 | |
| 380 | On the G5, logical CR operations are more expensive in their three |
| 381 | address form: ops that read/write the same register are half as expensive as |
| 382 | those that read from two registers that are different from their destination. |
| 383 | |
| 384 | We should model this with two separate instructions. The isel should generate |
| 385 | the "two address" form of the instructions. When the register allocator |
| 386 | detects that it needs to insert a copy due to the two-addresness of the CR |
| 387 | logical op, it will invoke PPCInstrInfo::convertToThreeAddress. At this point |
| 388 | we can convert to the "three address" instruction, to save code space. |
| 389 | |
| 390 | This only matters when we start generating cr logical ops. |
| 391 | |
Chris Lattner | 49f398b | 2006-03-08 00:25:47 +0000 | [diff] [blame] | 392 | ===-------------------------------------------------------------------------=== |
| 393 | |
| 394 | We should compile these two functions to the same thing: |
| 395 | |
| 396 | #include <stdlib.h> |
| 397 | void f(int a, int b, int *P) { |
| 398 | *P = (a-b)>=0?(a-b):(b-a); |
| 399 | } |
| 400 | void g(int a, int b, int *P) { |
| 401 | *P = abs(a-b); |
| 402 | } |
| 403 | |
| 404 | Further, they should compile to something better than: |
| 405 | |
| 406 | _g: |
| 407 | subf r2, r4, r3 |
| 408 | subfic r3, r2, 0 |
| 409 | cmpwi cr0, r2, -1 |
| 410 | bgt cr0, LBB2_2 ; entry |
| 411 | LBB2_1: ; entry |
| 412 | mr r2, r3 |
| 413 | LBB2_2: ; entry |
| 414 | stw r2, 0(r5) |
| 415 | blr |
| 416 | |
| 417 | GCC produces: |
| 418 | |
| 419 | _g: |
| 420 | subf r4,r4,r3 |
| 421 | srawi r2,r4,31 |
| 422 | xor r0,r2,r4 |
| 423 | subf r0,r2,r0 |
| 424 | stw r0,0(r5) |
| 425 | blr |
| 426 | |
| 427 | ... which is much nicer. |
| 428 | |
| 429 | This theoretically may help improve twolf slightly (used in dimbox.c:142?). |
| 430 | |
| 431 | ===-------------------------------------------------------------------------=== |
| 432 | |
Chris Lattner | 3f6bfda | 2010-01-24 02:27:03 +0000 | [diff] [blame] | 433 | PR5945: This: |
| 434 | define i32 @clamp0g(i32 %a) { |
| 435 | entry: |
| 436 | %cmp = icmp slt i32 %a, 0 |
| 437 | %sel = select i1 %cmp, i32 0, i32 %a |
| 438 | ret i32 %sel |
| 439 | } |
| 440 | |
| 441 | Is compile to this with the PowerPC (32-bit) backend: |
| 442 | |
| 443 | _clamp0g: |
| 444 | cmpwi cr0, r3, 0 |
| 445 | li r2, 0 |
| 446 | blt cr0, LBB1_2 |
| 447 | ; BB#1: ; %entry |
| 448 | mr r2, r3 |
| 449 | LBB1_2: ; %entry |
| 450 | mr r3, r2 |
| 451 | blr |
| 452 | |
| 453 | This could be reduced to the much simpler: |
| 454 | |
| 455 | _clamp0g: |
| 456 | srawi r2, r3, 31 |
| 457 | andc r3, r3, r2 |
| 458 | blr |
| 459 | |
| 460 | ===-------------------------------------------------------------------------=== |
| 461 | |
Nate Begeman | 2df9928 | 2006-03-16 18:50:44 +0000 | [diff] [blame] | 462 | int foo(int N, int ***W, int **TK, int X) { |
| 463 | int t, i; |
| 464 | |
| 465 | for (t = 0; t < N; ++t) |
| 466 | for (i = 0; i < 4; ++i) |
| 467 | W[t / X][i][t % X] = TK[i][t]; |
| 468 | |
| 469 | return 5; |
| 470 | } |
| 471 | |
Chris Lattner | ed51169 | 2006-03-16 22:25:55 +0000 | [diff] [blame] | 472 | We generate relatively atrocious code for this loop compared to gcc. |
| 473 | |
Chris Lattner | ef040dd | 2006-03-21 00:47:09 +0000 | [diff] [blame] | 474 | We could also strength reduce the rem and the div: |
| 475 | http://www.lcs.mit.edu/pubs/pdf/MIT-LCS-TM-600.pdf |
| 476 | |
Chris Lattner | 28b1a0b | 2006-03-19 05:33:30 +0000 | [diff] [blame] | 477 | ===-------------------------------------------------------------------------=== |
Chris Lattner | ed51169 | 2006-03-16 22:25:55 +0000 | [diff] [blame] | 478 | |
Nate Begeman | c0a8b6d | 2006-03-21 18:58:20 +0000 | [diff] [blame] | 479 | float foo(float X) { return (int)(X); } |
| 480 | |
Chris Lattner | 9d86a9d | 2006-03-22 05:33:23 +0000 | [diff] [blame] | 481 | Currently produces: |
Nate Begeman | c0a8b6d | 2006-03-21 18:58:20 +0000 | [diff] [blame] | 482 | |
| 483 | _foo: |
Nate Begeman | c0a8b6d | 2006-03-21 18:58:20 +0000 | [diff] [blame] | 484 | fctiwz f0, f1 |
| 485 | stfd f0, -8(r1) |
Chris Lattner | 9d86a9d | 2006-03-22 05:33:23 +0000 | [diff] [blame] | 486 | lwz r2, -4(r1) |
| 487 | extsw r2, r2 |
| 488 | std r2, -16(r1) |
| 489 | lfd f0, -16(r1) |
| 490 | fcfid f0, f0 |
Nate Begeman | c0a8b6d | 2006-03-21 18:58:20 +0000 | [diff] [blame] | 491 | frsp f1, f0 |
| 492 | blr |
| 493 | |
Chris Lattner | 9d86a9d | 2006-03-22 05:33:23 +0000 | [diff] [blame] | 494 | We could use a target dag combine to turn the lwz/extsw into an lwa when the |
| 495 | lwz has a single use. Since LWA is cracked anyway, this would be a codesize |
| 496 | win only. |
Nate Begeman | c0a8b6d | 2006-03-21 18:58:20 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 716aefc | 2006-03-23 21:28:44 +0000 | [diff] [blame] | 498 | ===-------------------------------------------------------------------------=== |
| 499 | |
Chris Lattner | 057f09b | 2006-03-24 20:04:27 +0000 | [diff] [blame] | 500 | We generate ugly code for this: |
| 501 | |
| 502 | void func(unsigned int *ret, float dx, float dy, float dz, float dw) { |
| 503 | unsigned code = 0; |
| 504 | if(dx < -dw) code |= 1; |
| 505 | if(dx > dw) code |= 2; |
| 506 | if(dy < -dw) code |= 4; |
| 507 | if(dy > dw) code |= 8; |
| 508 | if(dz < -dw) code |= 16; |
| 509 | if(dz > dw) code |= 32; |
| 510 | *ret = code; |
| 511 | } |
| 512 | |
Chris Lattner | 420736d | 2006-03-25 06:47:10 +0000 | [diff] [blame] | 513 | ===-------------------------------------------------------------------------=== |
| 514 | |
Chris Lattner | ed93790 | 2006-04-13 16:48:00 +0000 | [diff] [blame] | 515 | Complete the signed i32 to FP conversion code using 64-bit registers |
| 516 | transformation, good for PI. See PPCISelLowering.cpp, this comment: |
Chris Lattner | 220d2b8 | 2006-04-02 07:20:00 +0000 | [diff] [blame] | 517 | |
Chris Lattner | ed93790 | 2006-04-13 16:48:00 +0000 | [diff] [blame] | 518 | // FIXME: disable this lowered code. This generates 64-bit register values, |
| 519 | // and we don't model the fact that the top part is clobbered by calls. We |
| 520 | // need to flag these together so that the value isn't live across a call. |
| 521 | //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
Chris Lattner | 220d2b8 | 2006-04-02 07:20:00 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 9d62fa4 | 2006-05-17 19:02:25 +0000 | [diff] [blame] | 523 | Also, if the registers are spilled to the stack, we have to ensure that all |
| 524 | 64-bits of them are save/restored, otherwise we will miscompile the code. It |
| 525 | sounds like we need to get the 64-bit register classes going. |
| 526 | |
Chris Lattner | 55c6325 | 2006-05-05 05:36:15 +0000 | [diff] [blame] | 527 | ===-------------------------------------------------------------------------=== |
| 528 | |
Nate Begeman | 908049b | 2007-01-29 21:21:22 +0000 | [diff] [blame] | 529 | %struct.B = type { i8, [3 x i8] } |
Nate Begeman | 7514620 | 2006-05-08 20:54:02 +0000 | [diff] [blame] | 530 | |
Nate Begeman | 908049b | 2007-01-29 21:21:22 +0000 | [diff] [blame] | 531 | define void @bar(%struct.B* %b) { |
Nate Begeman | 7514620 | 2006-05-08 20:54:02 +0000 | [diff] [blame] | 532 | entry: |
Nate Begeman | 908049b | 2007-01-29 21:21:22 +0000 | [diff] [blame] | 533 | %tmp = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1] |
| 534 | %tmp = load i32* %tmp ; <uint> [#uses=1] |
| 535 | %tmp3 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1] |
| 536 | %tmp4 = load i32* %tmp3 ; <uint> [#uses=1] |
| 537 | %tmp8 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=2] |
| 538 | %tmp9 = load i32* %tmp8 ; <uint> [#uses=1] |
| 539 | %tmp4.mask17 = shl i32 %tmp4, i8 1 ; <uint> [#uses=1] |
| 540 | %tmp1415 = and i32 %tmp4.mask17, 2147483648 ; <uint> [#uses=1] |
| 541 | %tmp.masked = and i32 %tmp, 2147483648 ; <uint> [#uses=1] |
| 542 | %tmp11 = or i32 %tmp1415, %tmp.masked ; <uint> [#uses=1] |
| 543 | %tmp12 = and i32 %tmp9, 2147483647 ; <uint> [#uses=1] |
| 544 | %tmp13 = or i32 %tmp12, %tmp11 ; <uint> [#uses=1] |
| 545 | store i32 %tmp13, i32* %tmp8 |
Chris Lattner | 55c6325 | 2006-05-05 05:36:15 +0000 | [diff] [blame] | 546 | ret void |
| 547 | } |
| 548 | |
| 549 | We emit: |
| 550 | |
| 551 | _foo: |
| 552 | lwz r2, 0(r3) |
Nate Begeman | 7514620 | 2006-05-08 20:54:02 +0000 | [diff] [blame] | 553 | slwi r4, r2, 1 |
| 554 | or r4, r4, r2 |
| 555 | rlwimi r2, r4, 0, 0, 0 |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 556 | stw r2, 0(r3) |
Chris Lattner | 55c6325 | 2006-05-05 05:36:15 +0000 | [diff] [blame] | 557 | blr |
| 558 | |
Nate Begeman | 7514620 | 2006-05-08 20:54:02 +0000 | [diff] [blame] | 559 | We could collapse a bunch of those ORs and ANDs and generate the following |
| 560 | equivalent code: |
Chris Lattner | 55c6325 | 2006-05-05 05:36:15 +0000 | [diff] [blame] | 561 | |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 562 | _foo: |
| 563 | lwz r2, 0(r3) |
Nate Begeman | d8624ed | 2006-05-08 19:09:24 +0000 | [diff] [blame] | 564 | rlwinm r4, r2, 1, 0, 0 |
Nate Begeman | 4667f2c | 2006-05-08 17:38:32 +0000 | [diff] [blame] | 565 | or r2, r2, r4 |
| 566 | stw r2, 0(r3) |
| 567 | blr |
Chris Lattner | 1eeedae | 2006-07-14 04:07:29 +0000 | [diff] [blame] | 568 | |
| 569 | ===-------------------------------------------------------------------------=== |
| 570 | |
Chris Lattner | f0613e1 | 2006-09-14 20:56:30 +0000 | [diff] [blame] | 571 | We compile: |
| 572 | |
| 573 | unsigned test6(unsigned x) { |
| 574 | return ((x & 0x00FF0000) >> 16) | ((x & 0x000000FF) << 16); |
| 575 | } |
| 576 | |
| 577 | into: |
| 578 | |
| 579 | _test6: |
| 580 | lis r2, 255 |
| 581 | rlwinm r3, r3, 16, 0, 31 |
| 582 | ori r2, r2, 255 |
| 583 | and r3, r3, r2 |
| 584 | blr |
| 585 | |
| 586 | GCC gets it down to: |
| 587 | |
| 588 | _test6: |
| 589 | rlwinm r0,r3,16,8,15 |
| 590 | rlwinm r3,r3,16,24,31 |
| 591 | or r3,r3,r0 |
| 592 | blr |
| 593 | |
Chris Lattner | afd7a08 | 2007-01-18 07:34:57 +0000 | [diff] [blame] | 594 | |
| 595 | ===-------------------------------------------------------------------------=== |
| 596 | |
| 597 | Consider a function like this: |
| 598 | |
| 599 | float foo(float X) { return X + 1234.4123f; } |
| 600 | |
| 601 | The FP constant ends up in the constant pool, so we need to get the LR register. |
| 602 | This ends up producing code like this: |
| 603 | |
| 604 | _foo: |
| 605 | .LBB_foo_0: ; entry |
| 606 | mflr r11 |
| 607 | *** stw r11, 8(r1) |
| 608 | bl "L00000$pb" |
| 609 | "L00000$pb": |
| 610 | mflr r2 |
| 611 | addis r2, r2, ha16(.CPI_foo_0-"L00000$pb") |
| 612 | lfs f0, lo16(.CPI_foo_0-"L00000$pb")(r2) |
| 613 | fadds f1, f1, f0 |
| 614 | *** lwz r11, 8(r1) |
| 615 | mtlr r11 |
| 616 | blr |
| 617 | |
| 618 | This is functional, but there is no reason to spill the LR register all the way |
| 619 | to the stack (the two marked instrs): spilling it to a GPR is quite enough. |
| 620 | |
| 621 | Implementing this will require some codegen improvements. Nate writes: |
| 622 | |
| 623 | "So basically what we need to support the "no stack frame save and restore" is a |
| 624 | generalization of the LR optimization to "callee-save regs". |
| 625 | |
| 626 | Currently, we have LR marked as a callee-save reg. The register allocator sees |
| 627 | that it's callee save, and spills it directly to the stack. |
| 628 | |
| 629 | Ideally, something like this would happen: |
| 630 | |
| 631 | LR would be in a separate register class from the GPRs. The class of LR would be |
| 632 | marked "unspillable". When the register allocator came across an unspillable |
| 633 | reg, it would ask "what is the best class to copy this into that I *can* spill" |
| 634 | If it gets a class back, which it will in this case (the gprs), it grabs a free |
| 635 | register of that class. If it is then later necessary to spill that reg, so be |
| 636 | it. |
| 637 | |
| 638 | ===-------------------------------------------------------------------------=== |
Chris Lattner | 95b9d6e | 2007-01-31 19:49:20 +0000 | [diff] [blame] | 639 | |
| 640 | We compile this: |
| 641 | int test(_Bool X) { |
| 642 | return X ? 524288 : 0; |
| 643 | } |
| 644 | |
| 645 | to: |
| 646 | _test: |
| 647 | cmplwi cr0, r3, 0 |
| 648 | lis r2, 8 |
| 649 | li r3, 0 |
| 650 | beq cr0, LBB1_2 ;entry |
| 651 | LBB1_1: ;entry |
| 652 | mr r3, r2 |
| 653 | LBB1_2: ;entry |
| 654 | blr |
| 655 | |
| 656 | instead of: |
| 657 | _test: |
| 658 | addic r2,r3,-1 |
| 659 | subfe r0,r2,r3 |
| 660 | slwi r3,r0,19 |
| 661 | blr |
| 662 | |
| 663 | This sort of thing occurs a lot due to globalopt. |
| 664 | |
| 665 | ===-------------------------------------------------------------------------=== |
Chris Lattner | 8abcfe1 | 2007-02-09 17:38:01 +0000 | [diff] [blame] | 666 | |
Chris Lattner | a9cf5b3 | 2010-01-23 18:42:37 +0000 | [diff] [blame] | 667 | We compile: |
| 668 | |
| 669 | define i32 @bar(i32 %x) nounwind readnone ssp { |
| 670 | entry: |
| 671 | %0 = icmp eq i32 %x, 0 ; <i1> [#uses=1] |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 672 | %neg = sext i1 %0 to i32 ; <i32> [#uses=1] |
Chris Lattner | a9cf5b3 | 2010-01-23 18:42:37 +0000 | [diff] [blame] | 673 | ret i32 %neg |
| 674 | } |
| 675 | |
| 676 | to: |
| 677 | |
| 678 | _bar: |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 679 | cntlzw r2, r3 |
| 680 | slwi r2, r2, 26 |
| 681 | srawi r3, r2, 31 |
Chris Lattner | a9cf5b3 | 2010-01-23 18:42:37 +0000 | [diff] [blame] | 682 | blr |
| 683 | |
Chris Lattner | abb992d | 2010-01-24 00:09:49 +0000 | [diff] [blame] | 684 | it would be better to produce: |
Chris Lattner | a9cf5b3 | 2010-01-23 18:42:37 +0000 | [diff] [blame] | 685 | |
| 686 | _bar: |
| 687 | addic r3,r3,-1 |
| 688 | subfe r3,r3,r3 |
| 689 | blr |
| 690 | |
| 691 | ===-------------------------------------------------------------------------=== |
| 692 | |
Chris Lattner | 8abcfe1 | 2007-02-09 17:38:01 +0000 | [diff] [blame] | 693 | We currently compile 32-bit bswap: |
| 694 | |
| 695 | declare i32 @llvm.bswap.i32(i32 %A) |
| 696 | define i32 @test(i32 %A) { |
| 697 | %B = call i32 @llvm.bswap.i32(i32 %A) |
| 698 | ret i32 %B |
| 699 | } |
| 700 | |
| 701 | to: |
| 702 | |
| 703 | _test: |
| 704 | rlwinm r2, r3, 24, 16, 23 |
| 705 | slwi r4, r3, 24 |
| 706 | rlwimi r2, r3, 8, 24, 31 |
| 707 | rlwimi r4, r3, 8, 8, 15 |
| 708 | rlwimi r4, r2, 0, 16, 31 |
| 709 | mr r3, r4 |
| 710 | blr |
| 711 | |
| 712 | it would be more efficient to produce: |
| 713 | |
| 714 | _foo: mr r0,r3 |
| 715 | rlwinm r3,r3,8,0xffffffff |
| 716 | rlwimi r3,r0,24,0,7 |
| 717 | rlwimi r3,r0,24,16,23 |
| 718 | blr |
| 719 | |
| 720 | ===-------------------------------------------------------------------------=== |
Chris Lattner | 013e051 | 2007-03-25 04:46:28 +0000 | [diff] [blame] | 721 | |
| 722 | test/CodeGen/PowerPC/2007-03-24-cntlzd.ll compiles to: |
| 723 | |
| 724 | __ZNK4llvm5APInt17countLeadingZerosEv: |
| 725 | ld r2, 0(r3) |
| 726 | cntlzd r2, r2 |
| 727 | or r2, r2, r2 <<-- silly. |
| 728 | addi r3, r2, -64 |
| 729 | blr |
| 730 | |
| 731 | The dead or is a 'truncate' from 64- to 32-bits. |
| 732 | |
| 733 | ===-------------------------------------------------------------------------=== |
Chris Lattner | fcb1e61 | 2007-03-31 07:06:25 +0000 | [diff] [blame] | 734 | |
| 735 | We generate horrible ppc code for this: |
| 736 | |
| 737 | #define N 2000000 |
| 738 | double a[N],c[N]; |
| 739 | void simpleloop() { |
| 740 | int j; |
| 741 | for (j=0; j<N; j++) |
| 742 | c[j] = a[j]; |
| 743 | } |
| 744 | |
| 745 | LBB1_1: ;bb |
| 746 | lfdx f0, r3, r4 |
| 747 | addi r5, r5, 1 ;; Extra IV for the exit value compare. |
| 748 | stfdx f0, r2, r4 |
| 749 | addi r4, r4, 8 |
| 750 | |
| 751 | xoris r6, r5, 30 ;; This is due to a large immediate. |
| 752 | cmplwi cr0, r6, 33920 |
| 753 | bne cr0, LBB1_1 |
| 754 | |
Chris Lattner | bf8ae84 | 2007-09-10 21:43:18 +0000 | [diff] [blame] | 755 | //===---------------------------------------------------------------------===// |
| 756 | |
| 757 | This: |
| 758 | #include <algorithm> |
| 759 | inline std::pair<unsigned, bool> full_add(unsigned a, unsigned b) |
| 760 | { return std::make_pair(a + b, a + b < a); } |
| 761 | bool no_overflow(unsigned a, unsigned b) |
| 762 | { return !full_add(a, b).second; } |
| 763 | |
| 764 | Should compile to: |
| 765 | |
| 766 | __Z11no_overflowjj: |
| 767 | add r4,r3,r4 |
| 768 | subfc r3,r3,r4 |
| 769 | li r3,0 |
| 770 | adde r3,r3,r3 |
| 771 | blr |
| 772 | |
| 773 | (or better) not: |
| 774 | |
| 775 | __Z11no_overflowjj: |
| 776 | add r2, r4, r3 |
| 777 | cmplw cr7, r2, r3 |
| 778 | mfcr r2 |
| 779 | rlwinm r2, r2, 29, 31, 31 |
| 780 | xori r3, r2, 1 |
| 781 | blr |
| 782 | |
| 783 | //===---------------------------------------------------------------------===// |
Chris Lattner | fcb1e61 | 2007-03-31 07:06:25 +0000 | [diff] [blame] | 784 | |
Chris Lattner | fe39edd | 2008-01-08 06:46:30 +0000 | [diff] [blame] | 785 | We compile some FP comparisons into an mfcr with two rlwinms and an or. For |
| 786 | example: |
| 787 | #include <math.h> |
| 788 | int test(double x, double y) { return islessequal(x, y);} |
| 789 | int test2(double x, double y) { return islessgreater(x, y);} |
| 790 | int test3(double x, double y) { return !islessequal(x, y);} |
| 791 | |
| 792 | Compiles into (all three are similar, but the bits differ): |
| 793 | |
| 794 | _test: |
| 795 | fcmpu cr7, f1, f2 |
| 796 | mfcr r2 |
| 797 | rlwinm r3, r2, 29, 31, 31 |
| 798 | rlwinm r2, r2, 31, 31, 31 |
| 799 | or r3, r2, r3 |
| 800 | blr |
| 801 | |
| 802 | GCC compiles this into: |
| 803 | |
| 804 | _test: |
| 805 | fcmpu cr7,f1,f2 |
| 806 | cror 30,28,30 |
| 807 | mfcr r3 |
| 808 | rlwinm r3,r3,31,1 |
| 809 | blr |
| 810 | |
| 811 | which is more efficient and can use mfocr. See PR642 for some more context. |
| 812 | |
| 813 | //===---------------------------------------------------------------------===// |
Chris Lattner | 150943c | 2008-03-02 19:27:34 +0000 | [diff] [blame] | 814 | |
| 815 | void foo(float *data, float d) { |
| 816 | long i; |
| 817 | for (i = 0; i < 8000; i++) |
| 818 | data[i] = d; |
| 819 | } |
| 820 | void foo2(float *data, float d) { |
| 821 | long i; |
| 822 | data--; |
| 823 | for (i = 0; i < 8000; i++) { |
| 824 | data[1] = d; |
| 825 | data++; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | These compile to: |
| 830 | |
| 831 | _foo: |
| 832 | li r2, 0 |
| 833 | LBB1_1: ; bb |
| 834 | addi r4, r2, 4 |
| 835 | stfsx f1, r3, r2 |
| 836 | cmplwi cr0, r4, 32000 |
| 837 | mr r2, r4 |
| 838 | bne cr0, LBB1_1 ; bb |
| 839 | blr |
| 840 | _foo2: |
| 841 | li r2, 0 |
| 842 | LBB2_1: ; bb |
| 843 | addi r4, r2, 4 |
| 844 | stfsx f1, r3, r2 |
| 845 | cmplwi cr0, r4, 32000 |
| 846 | mr r2, r4 |
| 847 | bne cr0, LBB2_1 ; bb |
| 848 | blr |
| 849 | |
| 850 | The 'mr' could be eliminated to folding the add into the cmp better. |
| 851 | |
| 852 | //===---------------------------------------------------------------------===// |
Dale Johannesen | a7647e6 | 2008-11-17 18:56:34 +0000 | [diff] [blame] | 853 | Codegen for the following (low-probability) case deteriorated considerably |
| 854 | when the correctness fixes for unordered comparisons went in (PR 642, 58871). |
| 855 | It should be possible to recover the code quality described in the comments. |
| 856 | |
| 857 | ; RUN: llvm-as < %s | llc -march=ppc32 | grep or | count 3 |
| 858 | ; This should produce one 'or' or 'cror' instruction per function. |
| 859 | |
| 860 | ; RUN: llvm-as < %s | llc -march=ppc32 | grep mfcr | count 3 |
| 861 | ; PR2964 |
| 862 | |
| 863 | define i32 @test(double %x, double %y) nounwind { |
| 864 | entry: |
| 865 | %tmp3 = fcmp ole double %x, %y ; <i1> [#uses=1] |
| 866 | %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1] |
| 867 | ret i32 %tmp345 |
| 868 | } |
| 869 | |
| 870 | define i32 @test2(double %x, double %y) nounwind { |
| 871 | entry: |
| 872 | %tmp3 = fcmp one double %x, %y ; <i1> [#uses=1] |
| 873 | %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1] |
| 874 | ret i32 %tmp345 |
| 875 | } |
| 876 | |
| 877 | define i32 @test3(double %x, double %y) nounwind { |
| 878 | entry: |
| 879 | %tmp3 = fcmp ugt double %x, %y ; <i1> [#uses=1] |
| 880 | %tmp34 = zext i1 %tmp3 to i32 ; <i32> [#uses=1] |
| 881 | ret i32 %tmp34 |
| 882 | } |
| 883 | //===----------------------------------------------------------------------===// |
| 884 | ; RUN: llvm-as < %s | llc -march=ppc32 | not grep fneg |
| 885 | |
| 886 | ; This could generate FSEL with appropriate flags (FSEL is not IEEE-safe, and |
| 887 | ; should not be generated except with -enable-finite-only-fp-math or the like). |
| 888 | ; With the correctness fixes for PR642 (58871) LowerSELECT_CC would need to |
| 889 | ; recognize a more elaborate tree than a simple SETxx. |
| 890 | |
| 891 | define double @test_FNEG_sel(double %A, double %B, double %C) { |
| 892 | %D = sub double -0.000000e+00, %A ; <double> [#uses=1] |
| 893 | %Cond = fcmp ugt double %D, -0.000000e+00 ; <i1> [#uses=1] |
| 894 | %E = select i1 %Cond, double %B, double %C ; <double> [#uses=1] |
| 895 | ret double %E |
| 896 | } |
| 897 | |
Dale Johannesen | 15ce1d7 | 2010-02-12 23:16:24 +0000 | [diff] [blame^] | 898 | //===----------------------------------------------------------------------===// |
| 899 | The save/restore sequence for CR in prolog/epilog is terrible: |
| 900 | - Each CR subreg is saved individually, rather than doing one save as a unit. |
| 901 | - On Darwin, the save is done after the decrement of SP, which means the offset |
| 902 | from SP of the save slot can be too big for a store instruction, which means we |
| 903 | need an additional register (currently hacked in 96015+96020; the solution there |
| 904 | is correct, but poor). |
| 905 | - On SVR4 the same thing can happen, and I don't think saving before the SP |
| 906 | decrement is safe on that target, as there is no red zone. This is currently |
| 907 | broken AFAIK, although it's not a target I can exercise. |
| 908 | The following demonstrates the problem: |
| 909 | extern void bar(char *p); |
| 910 | void foo() { |
| 911 | char x[100000]; |
| 912 | bar(x); |
| 913 | __asm__("" ::: "cr2"); |
| 914 | } |