blob: f5e50fc808a8d87ebe3547e0c5d3eaa2a5c20df6 [file] [log] [blame]
Chris Lattnerb86bd2c2006-03-27 07:04:16 +00001//===- README.txt - Notes for improving PowerPC-specific code gen ---------===//
2
Nate Begemanb64af912004-08-10 20:42:36 +00003TODO:
Nate Begemanef9531e2005-04-11 20:48:57 +00004* gpr0 allocation
Nate Begeman4a0de072004-10-26 04:10:53 +00005* implement do-loop -> bdnz transform
Nate Begemana6ed0aa2008-02-11 04:16:09 +00006* lmw/stmw pass a la arm load store optimizer for prolog/epilog
Nate Begeman50fb3c42005-12-24 01:00:15 +00007
Nate Begemana63fee82006-02-03 05:17:06 +00008===-------------------------------------------------------------------------===
Nate Begeman50fb3c42005-12-24 01:00:15 +00009
Nate Begemana63fee82006-02-03 05:17:06 +000010Support 'update' load/store instructions. These are cracked on the G5, but are
11still a codesize win.
12
Chris Lattner26ddb502006-11-10 01:33:53 +000013With preinc enabled, this:
14
15long *%test4(long *%X, long *%dest) {
16 %Y = getelementptr long* %X, int 4
17 %A = load long* %Y
18 store long %A, long* %dest
19 ret long* %Y
20}
21
22compiles to:
23
24_test4:
25 mr r2, r3
26 lwzu r5, 32(r2)
27 lwz r3, 36(r3)
28 stw r5, 0(r4)
29 stw r3, 4(r4)
30 mr r3, r2
31 blr
32
33with -sched=list-burr, I get:
34
35_test4:
36 lwz r2, 36(r3)
37 lwzu r5, 32(r3)
38 stw r2, 4(r4)
39 stw r5, 0(r4)
40 blr
41
Nate Begemana63fee82006-02-03 05:17:06 +000042===-------------------------------------------------------------------------===
43
Chris Lattner6e112952006-11-07 18:30:21 +000044We compile the hottest inner loop of viterbi to:
45
46 li r6, 0
47 b LBB1_84 ;bb432.i
48LBB1_83: ;bb420.i
49 lbzx r8, r5, r7
50 addi r6, r7, 1
51 stbx r8, r4, r7
52LBB1_84: ;bb432.i
53 mr r7, r6
54 cmplwi cr0, r7, 143
55 bne cr0, LBB1_83 ;bb420.i
56
57The CBE manages to produce:
58
59 li r0, 143
60 mtctr r0
61loop:
62 lbzx r2, r2, r11
63 stbx r0, r2, r9
64 addi r2, r2, 1
65 bdz later
66 b loop
67
68This could be much better (bdnz instead of bdz) but it still beats us. If we
69produced this with bdnz, the loop would be a single dispatch group.
70
71===-------------------------------------------------------------------------===
72
Chris Lattner6a250ec2006-10-13 20:20:58 +000073Compile:
74
75void foo(int *P) {
76 if (P) *P = 0;
77}
78
79into:
80
81_foo:
82 cmpwi cr0,r3,0
83 beqlr cr0
84 li r0,0
85 stw r0,0(r3)
86 blr
87
88This is effectively a simple form of predication.
89
90===-------------------------------------------------------------------------===
91
Chris Lattnera3c44542005-08-24 18:15:24 +000092Lump the constant pool for each function into ONE pic object, and reference
93pieces of it as offsets from the start. For functions like this (contrived
94to have lots of constants obviously):
95
96double X(double Y) { return (Y*1.23 + 4.512)*2.34 + 14.38; }
97
98We generate:
99
100_X:
101 lis r2, ha16(.CPI_X_0)
102 lfd f0, lo16(.CPI_X_0)(r2)
103 lis r2, ha16(.CPI_X_1)
104 lfd f2, lo16(.CPI_X_1)(r2)
105 fmadd f0, f1, f0, f2
106 lis r2, ha16(.CPI_X_2)
107 lfd f1, lo16(.CPI_X_2)(r2)
108 lis r2, ha16(.CPI_X_3)
109 lfd f2, lo16(.CPI_X_3)(r2)
110 fmadd f1, f0, f1, f2
111 blr
112
113It would be better to materialize .CPI_X into a register, then use immediates
114off of the register to avoid the lis's. This is even more important in PIC
115mode.
116
Chris Lattner39b248b2006-02-02 23:50:22 +0000117Note that this (and the static variable version) is discussed here for GCC:
118http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html
119
Chris Lattneraabd0352007-08-23 15:16:03 +0000120Here's another example (the sgn function):
121double testf(double a) {
122 return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0);
123}
124
125it produces a BB like this:
126LBB1_1: ; cond_true
127 lis r2, ha16(LCPI1_0)
128 lfs f0, lo16(LCPI1_0)(r2)
129 lis r2, ha16(LCPI1_1)
130 lis r3, ha16(LCPI1_2)
131 lfs f2, lo16(LCPI1_2)(r3)
132 lfs f3, lo16(LCPI1_1)(r2)
133 fsub f0, f0, f1
134 fsel f1, f0, f2, f3
135 blr
136
Chris Lattnera3c44542005-08-24 18:15:24 +0000137===-------------------------------------------------------------------------===
Nate Begeman92cce902005-09-06 15:30:48 +0000138
Chris Lattner33c1dab2006-02-03 06:22:11 +0000139PIC Code Gen IPO optimization:
140
141Squish small scalar globals together into a single global struct, allowing the
142address of the struct to be CSE'd, avoiding PIC accesses (also reduces the size
143of the GOT on targets with one).
144
145Note that this is discussed here for GCC:
146http://gcc.gnu.org/ml/gcc-patches/2006-02/msg00133.html
147
148===-------------------------------------------------------------------------===
149
Nate Begeman92cce902005-09-06 15:30:48 +0000150Implement Newton-Rhapson method for improving estimate instructions to the
151correct accuracy, and implementing divide as multiply by reciprocal when it has
Dan Gohmand2cb3d22009-07-24 00:30:09 +0000152more than one use. Itanium would want this too.
Nate Begeman21e463b2005-10-16 05:39:50 +0000153
154===-------------------------------------------------------------------------===
155
Chris Lattner62c08dd2005-12-08 07:13:28 +0000156Compile offsets from allocas:
157
158int *%test() {
159 %X = alloca { int, int }
160 %Y = getelementptr {int,int}* %X, int 0, uint 1
161 ret int* %Y
162}
163
164into a single add, not two:
165
166_test:
167 addi r2, r1, -8
168 addi r3, r2, 4
169 blr
170
171--> important for C++.
172
Chris Lattner39706e62005-12-22 17:19:28 +0000173===-------------------------------------------------------------------------===
174
Chris Lattner39706e62005-12-22 17:19:28 +0000175No loads or stores of the constants should be needed:
176
177struct foo { double X, Y; };
178void xxx(struct foo F);
179void bar() { struct foo R = { 1.0, 2.0 }; xxx(R); }
180
Chris Lattner1db4b4f2006-01-16 17:53:00 +0000181===-------------------------------------------------------------------------===
182
Dale Johannesen7074fea2009-07-01 23:36:02 +0000183Darwin Stub removal:
184
185We still generate calls to foo$stub, and stubs, on Darwin. This is not
Chris Lattnerc4b0b402009-07-02 01:24:34 +0000186necessary when building with the Leopard (10.5) or later linker, as stubs are
187generated by ld when necessary. Parameterizing this based on the deployment
188target (-mmacosx-version-min) is probably enough. x86-32 does this right, see
189its logic.
Dale Johannesen7074fea2009-07-01 23:36:02 +0000190
191===-------------------------------------------------------------------------===
192
Chris Lattner98fbc2f2006-01-16 17:58:54 +0000193Darwin Stub LICM optimization:
194
195Loops like this:
196
197 for (...) bar();
198
199Have to go through an indirect stub if bar is external or linkonce. It would
200be better to compile it as:
201
202 fp = &bar;
203 for (...) fp();
204
205which only computes the address of bar once (instead of each time through the
206stub). This is Darwin specific and would have to be done in the code generator.
207Probably not a win on x86.
208
209===-------------------------------------------------------------------------===
210
Chris Lattner98fbc2f2006-01-16 17:58:54 +0000211Simple IPO for argument passing, change:
212 void foo(int X, double Y, int Z) -> void foo(int X, int Z, double Y)
213
214the Darwin ABI specifies that any integer arguments in the first 32 bytes worth
215of arguments get assigned to r3 through r10. That is, if you have a function
216foo(int, double, int) you get r3, f1, r6, since the 64 bit double ate up the
217argument bytes for r4 and r5. The trick then would be to shuffle the argument
218order for functions we can internalize so that the maximum number of
219integers/pointers get passed in regs before you see any of the fp arguments.
220
221Instead of implementing this, it would actually probably be easier to just
222implement a PPC fastcc, where we could do whatever we wanted to the CC,
223including having this work sanely.
224
225===-------------------------------------------------------------------------===
226
227Fix Darwin FP-In-Integer Registers ABI
228
229Darwin passes doubles in structures in integer registers, which is very very
230bad. Add something like a BIT_CONVERT to LLVM, then do an i-p transformation
231that percolates these things out of functions.
232
233Check out how horrible this is:
234http://gcc.gnu.org/ml/gcc/2005-10/msg01036.html
235
236This is an extension of "interprocedural CC unmunging" that can't be done with
237just fastcc.
238
239===-------------------------------------------------------------------------===
240
Chris Lattner56b69642006-01-31 02:55:28 +0000241Compile this:
242
Chris Lattner83e64ba2006-01-31 07:16:34 +0000243int foo(int a) {
244 int b = (a < 8);
245 if (b) {
246 return b * 3; // ignore the fact that this is always 3.
247 } else {
248 return 2;
249 }
250}
251
252into something not this:
253
254_foo:
2551) cmpwi cr7, r3, 8
256 mfcr r2, 1
257 rlwinm r2, r2, 29, 31, 31
2581) cmpwi cr0, r3, 7
259 bgt cr0, LBB1_2 ; UnifiedReturnBlock
260LBB1_1: ; then
261 rlwinm r2, r2, 0, 31, 31
262 mulli r3, r2, 3
263 blr
264LBB1_2: ; UnifiedReturnBlock
265 li r3, 2
266 blr
267
268In particular, the two compares (marked 1) could be shared by reversing one.
269This could be done in the dag combiner, by swapping a BR_CC when a SETCC of the
270same operands (but backwards) exists. In this case, this wouldn't save us
271anything though, because the compares still wouldn't be shared.
Chris Lattner0ddc1802006-02-01 00:28:12 +0000272
Chris Lattner5a7efc92006-02-01 17:54:23 +0000273===-------------------------------------------------------------------------===
274
Chris Lattner275b8842006-02-02 07:37:11 +0000275We should custom expand setcc instead of pretending that we have it. That
276would allow us to expose the access of the crbit after the mfcr, allowing
277that access to be trivially folded into other ops. A simple example:
278
279int foo(int a, int b) { return (a < b) << 4; }
280
281compiles into:
282
283_foo:
284 cmpw cr7, r3, r4
285 mfcr r2, 1
286 rlwinm r2, r2, 29, 31, 31
287 slwi r3, r2, 4
288 blr
289
Chris Lattnerd463f7f2006-02-03 01:49:49 +0000290===-------------------------------------------------------------------------===
291
Nate Begemana63fee82006-02-03 05:17:06 +0000292Fold add and sub with constant into non-extern, non-weak addresses so this:
293
294static int a;
295void bar(int b) { a = b; }
296void foo(unsigned char *c) {
297 *c = a;
298}
299
300So that
301
302_foo:
303 lis r2, ha16(_a)
304 la r2, lo16(_a)(r2)
305 lbz r2, 3(r2)
306 stb r2, 0(r3)
307 blr
308
309Becomes
310
311_foo:
312 lis r2, ha16(_a+3)
313 lbz r2, lo16(_a+3)(r2)
314 stb r2, 0(r3)
315 blr
Chris Lattner21384532006-02-05 05:27:35 +0000316
317===-------------------------------------------------------------------------===
318
319We generate really bad code for this:
320
321int f(signed char *a, _Bool b, _Bool c) {
322 signed char t = 0;
323 if (b) t = *a;
324 if (c) *a = t;
325}
326
Chris Lattner00d18f02006-03-01 06:36:20 +0000327===-------------------------------------------------------------------------===
328
329This:
330int test(unsigned *P) { return *P >> 24; }
331
332Should compile to:
333
334_test:
335 lbz r3,0(r3)
336 blr
337
338not:
339
340_test:
341 lwz r2, 0(r3)
342 srwi r3, r2, 24
343 blr
344
Chris Lattner5a63c472006-03-07 04:42:59 +0000345===-------------------------------------------------------------------------===
346
347On the G5, logical CR operations are more expensive in their three
348address form: ops that read/write the same register are half as expensive as
349those that read from two registers that are different from their destination.
350
351We should model this with two separate instructions. The isel should generate
352the "two address" form of the instructions. When the register allocator
353detects that it needs to insert a copy due to the two-addresness of the CR
354logical op, it will invoke PPCInstrInfo::convertToThreeAddress. At this point
355we can convert to the "three address" instruction, to save code space.
356
357This only matters when we start generating cr logical ops.
358
Chris Lattner49f398b2006-03-08 00:25:47 +0000359===-------------------------------------------------------------------------===
360
361We should compile these two functions to the same thing:
362
363#include <stdlib.h>
364void f(int a, int b, int *P) {
365 *P = (a-b)>=0?(a-b):(b-a);
366}
367void g(int a, int b, int *P) {
368 *P = abs(a-b);
369}
370
371Further, they should compile to something better than:
372
373_g:
374 subf r2, r4, r3
375 subfic r3, r2, 0
376 cmpwi cr0, r2, -1
377 bgt cr0, LBB2_2 ; entry
378LBB2_1: ; entry
379 mr r2, r3
380LBB2_2: ; entry
381 stw r2, 0(r5)
382 blr
383
384GCC produces:
385
386_g:
387 subf r4,r4,r3
388 srawi r2,r4,31
389 xor r0,r2,r4
390 subf r0,r2,r0
391 stw r0,0(r5)
392 blr
393
394... which is much nicer.
395
396This theoretically may help improve twolf slightly (used in dimbox.c:142?).
397
398===-------------------------------------------------------------------------===
399
Nate Begeman2df99282006-03-16 18:50:44 +0000400int foo(int N, int ***W, int **TK, int X) {
401 int t, i;
402
403 for (t = 0; t < N; ++t)
404 for (i = 0; i < 4; ++i)
405 W[t / X][i][t % X] = TK[i][t];
406
407 return 5;
408}
409
Chris Lattnered511692006-03-16 22:25:55 +0000410We generate relatively atrocious code for this loop compared to gcc.
411
Chris Lattneref040dd2006-03-21 00:47:09 +0000412We could also strength reduce the rem and the div:
413http://www.lcs.mit.edu/pubs/pdf/MIT-LCS-TM-600.pdf
414
Chris Lattner28b1a0b2006-03-19 05:33:30 +0000415===-------------------------------------------------------------------------===
Chris Lattnered511692006-03-16 22:25:55 +0000416
Nate Begemanc0a8b6d2006-03-21 18:58:20 +0000417float foo(float X) { return (int)(X); }
418
Chris Lattner9d86a9d2006-03-22 05:33:23 +0000419Currently produces:
Nate Begemanc0a8b6d2006-03-21 18:58:20 +0000420
421_foo:
Nate Begemanc0a8b6d2006-03-21 18:58:20 +0000422 fctiwz f0, f1
423 stfd f0, -8(r1)
Chris Lattner9d86a9d2006-03-22 05:33:23 +0000424 lwz r2, -4(r1)
425 extsw r2, r2
426 std r2, -16(r1)
427 lfd f0, -16(r1)
428 fcfid f0, f0
Nate Begemanc0a8b6d2006-03-21 18:58:20 +0000429 frsp f1, f0
430 blr
431
Chris Lattner9d86a9d2006-03-22 05:33:23 +0000432We could use a target dag combine to turn the lwz/extsw into an lwa when the
433lwz has a single use. Since LWA is cracked anyway, this would be a codesize
434win only.
Nate Begemanc0a8b6d2006-03-21 18:58:20 +0000435
Chris Lattner716aefc2006-03-23 21:28:44 +0000436===-------------------------------------------------------------------------===
437
Chris Lattner057f09b2006-03-24 20:04:27 +0000438We generate ugly code for this:
439
440void func(unsigned int *ret, float dx, float dy, float dz, float dw) {
441 unsigned code = 0;
442 if(dx < -dw) code |= 1;
443 if(dx > dw) code |= 2;
444 if(dy < -dw) code |= 4;
445 if(dy > dw) code |= 8;
446 if(dz < -dw) code |= 16;
447 if(dz > dw) code |= 32;
448 *ret = code;
449}
450
Chris Lattner420736d2006-03-25 06:47:10 +0000451===-------------------------------------------------------------------------===
452
Chris Lattnered937902006-04-13 16:48:00 +0000453Complete the signed i32 to FP conversion code using 64-bit registers
454transformation, good for PI. See PPCISelLowering.cpp, this comment:
Chris Lattner220d2b82006-04-02 07:20:00 +0000455
Chris Lattnered937902006-04-13 16:48:00 +0000456 // FIXME: disable this lowered code. This generates 64-bit register values,
457 // and we don't model the fact that the top part is clobbered by calls. We
458 // need to flag these together so that the value isn't live across a call.
459 //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattner220d2b82006-04-02 07:20:00 +0000460
Chris Lattner9d62fa42006-05-17 19:02:25 +0000461Also, if the registers are spilled to the stack, we have to ensure that all
46264-bits of them are save/restored, otherwise we will miscompile the code. It
463sounds like we need to get the 64-bit register classes going.
464
Chris Lattner55c63252006-05-05 05:36:15 +0000465===-------------------------------------------------------------------------===
466
Nate Begeman908049b2007-01-29 21:21:22 +0000467%struct.B = type { i8, [3 x i8] }
Nate Begeman75146202006-05-08 20:54:02 +0000468
Nate Begeman908049b2007-01-29 21:21:22 +0000469define void @bar(%struct.B* %b) {
Nate Begeman75146202006-05-08 20:54:02 +0000470entry:
Nate Begeman908049b2007-01-29 21:21:22 +0000471 %tmp = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1]
472 %tmp = load i32* %tmp ; <uint> [#uses=1]
473 %tmp3 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=1]
474 %tmp4 = load i32* %tmp3 ; <uint> [#uses=1]
475 %tmp8 = bitcast %struct.B* %b to i32* ; <uint*> [#uses=2]
476 %tmp9 = load i32* %tmp8 ; <uint> [#uses=1]
477 %tmp4.mask17 = shl i32 %tmp4, i8 1 ; <uint> [#uses=1]
478 %tmp1415 = and i32 %tmp4.mask17, 2147483648 ; <uint> [#uses=1]
479 %tmp.masked = and i32 %tmp, 2147483648 ; <uint> [#uses=1]
480 %tmp11 = or i32 %tmp1415, %tmp.masked ; <uint> [#uses=1]
481 %tmp12 = and i32 %tmp9, 2147483647 ; <uint> [#uses=1]
482 %tmp13 = or i32 %tmp12, %tmp11 ; <uint> [#uses=1]
483 store i32 %tmp13, i32* %tmp8
Chris Lattner55c63252006-05-05 05:36:15 +0000484 ret void
485}
486
487We emit:
488
489_foo:
490 lwz r2, 0(r3)
Nate Begeman75146202006-05-08 20:54:02 +0000491 slwi r4, r2, 1
492 or r4, r4, r2
493 rlwimi r2, r4, 0, 0, 0
Nate Begeman4667f2c2006-05-08 17:38:32 +0000494 stw r2, 0(r3)
Chris Lattner55c63252006-05-05 05:36:15 +0000495 blr
496
Nate Begeman75146202006-05-08 20:54:02 +0000497We could collapse a bunch of those ORs and ANDs and generate the following
498equivalent code:
Chris Lattner55c63252006-05-05 05:36:15 +0000499
Nate Begeman4667f2c2006-05-08 17:38:32 +0000500_foo:
501 lwz r2, 0(r3)
Nate Begemand8624ed2006-05-08 19:09:24 +0000502 rlwinm r4, r2, 1, 0, 0
Nate Begeman4667f2c2006-05-08 17:38:32 +0000503 or r2, r2, r4
504 stw r2, 0(r3)
505 blr
Chris Lattner1eeedae2006-07-14 04:07:29 +0000506
507===-------------------------------------------------------------------------===
508
Chris Lattnerf0613e12006-09-14 20:56:30 +0000509We compile:
510
511unsigned test6(unsigned x) {
512 return ((x & 0x00FF0000) >> 16) | ((x & 0x000000FF) << 16);
513}
514
515into:
516
517_test6:
518 lis r2, 255
519 rlwinm r3, r3, 16, 0, 31
520 ori r2, r2, 255
521 and r3, r3, r2
522 blr
523
524GCC gets it down to:
525
526_test6:
527 rlwinm r0,r3,16,8,15
528 rlwinm r3,r3,16,24,31
529 or r3,r3,r0
530 blr
531
Chris Lattnerafd7a082007-01-18 07:34:57 +0000532
533===-------------------------------------------------------------------------===
534
535Consider a function like this:
536
537float foo(float X) { return X + 1234.4123f; }
538
539The FP constant ends up in the constant pool, so we need to get the LR register.
540 This ends up producing code like this:
541
542_foo:
543.LBB_foo_0: ; entry
544 mflr r11
545*** stw r11, 8(r1)
546 bl "L00000$pb"
547"L00000$pb":
548 mflr r2
549 addis r2, r2, ha16(.CPI_foo_0-"L00000$pb")
550 lfs f0, lo16(.CPI_foo_0-"L00000$pb")(r2)
551 fadds f1, f1, f0
552*** lwz r11, 8(r1)
553 mtlr r11
554 blr
555
556This is functional, but there is no reason to spill the LR register all the way
557to the stack (the two marked instrs): spilling it to a GPR is quite enough.
558
559Implementing this will require some codegen improvements. Nate writes:
560
561"So basically what we need to support the "no stack frame save and restore" is a
562generalization of the LR optimization to "callee-save regs".
563
564Currently, we have LR marked as a callee-save reg. The register allocator sees
565that it's callee save, and spills it directly to the stack.
566
567Ideally, something like this would happen:
568
569LR would be in a separate register class from the GPRs. The class of LR would be
570marked "unspillable". When the register allocator came across an unspillable
571reg, it would ask "what is the best class to copy this into that I *can* spill"
572If it gets a class back, which it will in this case (the gprs), it grabs a free
573register of that class. If it is then later necessary to spill that reg, so be
574it.
575
576===-------------------------------------------------------------------------===
Chris Lattner95b9d6e2007-01-31 19:49:20 +0000577
578We compile this:
579int test(_Bool X) {
580 return X ? 524288 : 0;
581}
582
583to:
584_test:
585 cmplwi cr0, r3, 0
586 lis r2, 8
587 li r3, 0
588 beq cr0, LBB1_2 ;entry
589LBB1_1: ;entry
590 mr r3, r2
591LBB1_2: ;entry
592 blr
593
594instead of:
595_test:
596 addic r2,r3,-1
597 subfe r0,r2,r3
598 slwi r3,r0,19
599 blr
600
601This sort of thing occurs a lot due to globalopt.
602
603===-------------------------------------------------------------------------===
Chris Lattner8abcfe12007-02-09 17:38:01 +0000604
605We currently compile 32-bit bswap:
606
607declare i32 @llvm.bswap.i32(i32 %A)
608define i32 @test(i32 %A) {
609 %B = call i32 @llvm.bswap.i32(i32 %A)
610 ret i32 %B
611}
612
613to:
614
615_test:
616 rlwinm r2, r3, 24, 16, 23
617 slwi r4, r3, 24
618 rlwimi r2, r3, 8, 24, 31
619 rlwimi r4, r3, 8, 8, 15
620 rlwimi r4, r2, 0, 16, 31
621 mr r3, r4
622 blr
623
624it would be more efficient to produce:
625
626_foo: mr r0,r3
627 rlwinm r3,r3,8,0xffffffff
628 rlwimi r3,r0,24,0,7
629 rlwimi r3,r0,24,16,23
630 blr
631
632===-------------------------------------------------------------------------===
Chris Lattner013e0512007-03-25 04:46:28 +0000633
634test/CodeGen/PowerPC/2007-03-24-cntlzd.ll compiles to:
635
636__ZNK4llvm5APInt17countLeadingZerosEv:
637 ld r2, 0(r3)
638 cntlzd r2, r2
639 or r2, r2, r2 <<-- silly.
640 addi r3, r2, -64
641 blr
642
643The dead or is a 'truncate' from 64- to 32-bits.
644
645===-------------------------------------------------------------------------===
Chris Lattnerfcb1e612007-03-31 07:06:25 +0000646
647We generate horrible ppc code for this:
648
649#define N 2000000
650double a[N],c[N];
651void simpleloop() {
652 int j;
653 for (j=0; j<N; j++)
654 c[j] = a[j];
655}
656
657LBB1_1: ;bb
658 lfdx f0, r3, r4
659 addi r5, r5, 1 ;; Extra IV for the exit value compare.
660 stfdx f0, r2, r4
661 addi r4, r4, 8
662
663 xoris r6, r5, 30 ;; This is due to a large immediate.
664 cmplwi cr0, r6, 33920
665 bne cr0, LBB1_1
666
Chris Lattnerbf8ae842007-09-10 21:43:18 +0000667//===---------------------------------------------------------------------===//
668
669This:
670 #include <algorithm>
671 inline std::pair<unsigned, bool> full_add(unsigned a, unsigned b)
672 { return std::make_pair(a + b, a + b < a); }
673 bool no_overflow(unsigned a, unsigned b)
674 { return !full_add(a, b).second; }
675
676Should compile to:
677
678__Z11no_overflowjj:
679 add r4,r3,r4
680 subfc r3,r3,r4
681 li r3,0
682 adde r3,r3,r3
683 blr
684
685(or better) not:
686
687__Z11no_overflowjj:
688 add r2, r4, r3
689 cmplw cr7, r2, r3
690 mfcr r2
691 rlwinm r2, r2, 29, 31, 31
692 xori r3, r2, 1
693 blr
694
695//===---------------------------------------------------------------------===//
Chris Lattnerfcb1e612007-03-31 07:06:25 +0000696
Chris Lattnerfe39edd2008-01-08 06:46:30 +0000697We compile some FP comparisons into an mfcr with two rlwinms and an or. For
698example:
699#include <math.h>
700int test(double x, double y) { return islessequal(x, y);}
701int test2(double x, double y) { return islessgreater(x, y);}
702int test3(double x, double y) { return !islessequal(x, y);}
703
704Compiles into (all three are similar, but the bits differ):
705
706_test:
707 fcmpu cr7, f1, f2
708 mfcr r2
709 rlwinm r3, r2, 29, 31, 31
710 rlwinm r2, r2, 31, 31, 31
711 or r3, r2, r3
712 blr
713
714GCC compiles this into:
715
716 _test:
717 fcmpu cr7,f1,f2
718 cror 30,28,30
719 mfcr r3
720 rlwinm r3,r3,31,1
721 blr
722
723which is more efficient and can use mfocr. See PR642 for some more context.
724
725//===---------------------------------------------------------------------===//
Chris Lattner150943c2008-03-02 19:27:34 +0000726
727void foo(float *data, float d) {
728 long i;
729 for (i = 0; i < 8000; i++)
730 data[i] = d;
731}
732void foo2(float *data, float d) {
733 long i;
734 data--;
735 for (i = 0; i < 8000; i++) {
736 data[1] = d;
737 data++;
738 }
739}
740
741These compile to:
742
743_foo:
744 li r2, 0
745LBB1_1: ; bb
746 addi r4, r2, 4
747 stfsx f1, r3, r2
748 cmplwi cr0, r4, 32000
749 mr r2, r4
750 bne cr0, LBB1_1 ; bb
751 blr
752_foo2:
753 li r2, 0
754LBB2_1: ; bb
755 addi r4, r2, 4
756 stfsx f1, r3, r2
757 cmplwi cr0, r4, 32000
758 mr r2, r4
759 bne cr0, LBB2_1 ; bb
760 blr
761
762The 'mr' could be eliminated to folding the add into the cmp better.
763
764//===---------------------------------------------------------------------===//
Dale Johannesena7647e62008-11-17 18:56:34 +0000765Codegen for the following (low-probability) case deteriorated considerably
766when the correctness fixes for unordered comparisons went in (PR 642, 58871).
767It should be possible to recover the code quality described in the comments.
768
769; RUN: llvm-as < %s | llc -march=ppc32 | grep or | count 3
770; This should produce one 'or' or 'cror' instruction per function.
771
772; RUN: llvm-as < %s | llc -march=ppc32 | grep mfcr | count 3
773; PR2964
774
775define i32 @test(double %x, double %y) nounwind {
776entry:
777 %tmp3 = fcmp ole double %x, %y ; <i1> [#uses=1]
778 %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
779 ret i32 %tmp345
780}
781
782define i32 @test2(double %x, double %y) nounwind {
783entry:
784 %tmp3 = fcmp one double %x, %y ; <i1> [#uses=1]
785 %tmp345 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
786 ret i32 %tmp345
787}
788
789define i32 @test3(double %x, double %y) nounwind {
790entry:
791 %tmp3 = fcmp ugt double %x, %y ; <i1> [#uses=1]
792 %tmp34 = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
793 ret i32 %tmp34
794}
795//===----------------------------------------------------------------------===//
796; RUN: llvm-as < %s | llc -march=ppc32 | not grep fneg
797
798; This could generate FSEL with appropriate flags (FSEL is not IEEE-safe, and
799; should not be generated except with -enable-finite-only-fp-math or the like).
800; With the correctness fixes for PR642 (58871) LowerSELECT_CC would need to
801; recognize a more elaborate tree than a simple SETxx.
802
803define double @test_FNEG_sel(double %A, double %B, double %C) {
804 %D = sub double -0.000000e+00, %A ; <double> [#uses=1]
805 %Cond = fcmp ugt double %D, -0.000000e+00 ; <i1> [#uses=1]
806 %E = select i1 %Cond, double %B, double %C ; <double> [#uses=1]
807 ret double %E
808}
809