add a crazy idea
[oota-llvm.git] / lib / Target / ARM / README.txt
1 //===---------------------------------------------------------------------===//
2 // Random ideas for the ARM backend.
3 //===---------------------------------------------------------------------===//
4
5 Reimplement 'select' in terms of 'SEL'.
6
7 * We would really like to support UXTAB16, but we need to prove that the
8   add doesn't need to overflow between the two 16-bit chunks.
9
10 * implement predication support
11 * Implement pre/post increment support.  (e.g. PR935)
12 * Coalesce stack slots!
13 * Implement smarter constant generation for binops with large immediates.
14
15 * Consider materializing FP constants like 0.0f and 1.0f using integer 
16   immediate instructions then copy to FPU.  Slower than load into FPU?
17
18 //===---------------------------------------------------------------------===//
19
20 Crazy idea:  Consider code that uses lots of 8-bit or 16-bit values.  By the
21 time regalloc happens, these values are now in a 32-bit register, usually with
22 the top-bits known to be sign or zero extended.  If spilled, we should be able
23 to spill these to a 8-bit or 16-bit stack slot, zero or sign extending as part
24 of the reload.
25
26 Doing this reduces the size of the stack frame (important for thumb etc), and
27 also increases the likelihood that we will be able to reload multiple values
28 from the stack with a single load.
29
30 //===---------------------------------------------------------------------===//
31
32 The constant island pass is in good shape.  Some cleanups might be desirable,
33 but there is unlikely to be much improvement in the generated code.
34
35 1.  There may be some advantage to trying to be smarter about the initial
36 placement, rather than putting everything at the end.
37
38 2.  The handling of 2-byte padding for Thumb is overly conservative.  There 
39 would be a small gain to keeping accurate track of the padding (which would
40 require aligning functions containing constant pools to 4-byte boundaries).
41
42 3.  There might be some compile-time efficiency to be had by representing
43 consecutive islands as a single block rather than multiple blocks.
44
45 4.  Use a priority queue to sort constant pool users in inverse order of
46     position so we always process the one closed to the end of functions
47     first. This may simply CreateNewWater.
48
49 //===---------------------------------------------------------------------===//
50
51 We need to start generating predicated instructions.  The .td files have a way
52 to express this now (see the PPC conditional return instruction), but the 
53 branch folding pass (or a new if-cvt pass) should start producing these, at
54 least in the trivial case.
55
56 Among the obvious wins, doing so can eliminate the need to custom expand 
57 copysign (i.e. we won't need to custom expand it to get the conditional
58 negate).
59
60 This allows us to eliminate one instruction from:
61
62 define i32 @_Z6slow4bii(i32 %x, i32 %y) {
63         %tmp = icmp sgt i32 %x, %y
64         %retval = select i1 %tmp, i32 %x, i32 %y
65         ret i32 %retval
66 }
67
68 __Z6slow4bii:
69         cmp r0, r1
70         movgt r1, r0
71         mov r0, r1
72         bx lr
73
74 //===---------------------------------------------------------------------===//
75
76 Implement long long "X-3" with instructions that fold the immediate in.  These
77 were disabled due to badness with the ARM carry flag on subtracts.
78
79 //===---------------------------------------------------------------------===//
80
81 We currently compile abs:
82 int foo(int p) { return p < 0 ? -p : p; }
83
84 into:
85
86 _foo:
87         rsb r1, r0, #0
88         cmn r0, #1
89         movgt r1, r0
90         mov r0, r1
91         bx lr
92
93 This is very, uh, literal.  This could be a 3 operation sequence:
94   t = (p sra 31); 
95   res = (p xor t)-t
96
97 Which would be better.  This occurs in png decode.
98
99 //===---------------------------------------------------------------------===//
100
101 More load / store optimizations:
102 1) Look past instructions without side-effects (not load, store, branch, etc.)
103    when forming the list of loads / stores to optimize.
104
105 2) Smarter register allocation?
106 We are probably missing some opportunities to use ldm / stm. Consider:
107
108 ldr r5, [r0]
109 ldr r4, [r0, #4]
110
111 This cannot be merged into a ldm. Perhaps we will need to do the transformation
112 before register allocation. Then teach the register allocator to allocate a
113 chunk of consecutive registers.
114
115 3) Better representation for block transfer? This is from Olden/power:
116
117         fldd d0, [r4]
118         fstd d0, [r4, #+32]
119         fldd d0, [r4, #+8]
120         fstd d0, [r4, #+40]
121         fldd d0, [r4, #+16]
122         fstd d0, [r4, #+48]
123         fldd d0, [r4, #+24]
124         fstd d0, [r4, #+56]
125
126 If we can spare the registers, it would be better to use fldm and fstm here.
127 Need major register allocator enhancement though.
128
129 4) Can we recognize the relative position of constantpool entries? i.e. Treat
130
131         ldr r0, LCPI17_3
132         ldr r1, LCPI17_4
133         ldr r2, LCPI17_5
134
135    as
136         ldr r0, LCPI17
137         ldr r1, LCPI17+4
138         ldr r2, LCPI17+8
139
140    Then the ldr's can be combined into a single ldm. See Olden/power.
141
142 Note for ARM v4 gcc uses ldmia to load a pair of 32-bit values to represent a
143 double 64-bit FP constant:
144
145         adr     r0, L6
146         ldmia   r0, {r0-r1}
147
148         .align 2
149 L6:
150         .long   -858993459
151         .long   1074318540
152
153 5) Can we make use of ldrd and strd? Instead of generating ldm / stm, use
154 ldrd/strd instead if there are only two destination registers that form an
155 odd/even pair. However, we probably would pay a penalty if the address is not
156 aligned on 8-byte boundary. This requires more information on load / store
157 nodes (and MI's?) then we currently carry.
158
159 6) struct copies appear to be done field by field 
160 instead of by words, at least sometimes:
161
162 struct foo { int x; short s; char c1; char c2; };
163 void cpy(struct foo*a, struct foo*b) { *a = *b; }
164
165 llvm code (-O2)
166         ldrb r3, [r1, #+6]
167         ldr r2, [r1]
168         ldrb r12, [r1, #+7]
169         ldrh r1, [r1, #+4]
170         str r2, [r0]
171         strh r1, [r0, #+4]
172         strb r3, [r0, #+6]
173         strb r12, [r0, #+7]
174 gcc code (-O2)
175         ldmia   r1, {r1-r2}
176         stmia   r0, {r1-r2}
177
178 In this benchmark poor handling of aggregate copies has shown up as
179 having a large effect on size, and possibly speed as well (we don't have
180 a good way to measure on ARM).
181
182 //===---------------------------------------------------------------------===//
183
184 * Consider this silly example:
185
186 double bar(double x) {  
187   double r = foo(3.1);
188   return x+r;
189 }
190
191 _bar:
192         sub sp, sp, #16
193         str r4, [sp, #+12]
194         str r5, [sp, #+8]
195         str lr, [sp, #+4]
196         mov r4, r0
197         mov r5, r1
198         ldr r0, LCPI2_0
199         bl _foo
200         fmsr f0, r0
201         fcvtsd d0, f0
202         fmdrr d1, r4, r5
203         faddd d0, d0, d1
204         fmrrd r0, r1, d0
205         ldr lr, [sp, #+4]
206         ldr r5, [sp, #+8]
207         ldr r4, [sp, #+12]
208         add sp, sp, #16
209         bx lr
210
211 Ignore the prologue and epilogue stuff for a second. Note 
212         mov r4, r0
213         mov r5, r1
214 the copys to callee-save registers and the fact they are only being used by the
215 fmdrr instruction. It would have been better had the fmdrr been scheduled
216 before the call and place the result in a callee-save DPR register. The two
217 mov ops would not have been necessary.
218
219 //===---------------------------------------------------------------------===//
220
221 Calling convention related stuff:
222
223 * gcc's parameter passing implementation is terrible and we suffer as a result:
224
225 e.g.
226 struct s {
227   double d1;
228   int s1;
229 };
230
231 void foo(struct s S) {
232   printf("%g, %d\n", S.d1, S.s1);
233 }
234
235 'S' is passed via registers r0, r1, r2. But gcc stores them to the stack, and
236 then reload them to r1, r2, and r3 before issuing the call (r0 contains the
237 address of the format string):
238
239         stmfd   sp!, {r7, lr}
240         add     r7, sp, #0
241         sub     sp, sp, #12
242         stmia   sp, {r0, r1, r2}
243         ldmia   sp, {r1-r2}
244         ldr     r0, L5
245         ldr     r3, [sp, #8]
246 L2:
247         add     r0, pc, r0
248         bl      L_printf$stub
249
250 Instead of a stmia, ldmia, and a ldr, wouldn't it be better to do three moves?
251
252 * Return an aggregate type is even worse:
253
254 e.g.
255 struct s foo(void) {
256   struct s S = {1.1, 2};
257   return S;
258 }
259
260         mov     ip, r0
261         ldr     r0, L5
262         sub     sp, sp, #12
263 L2:
264         add     r0, pc, r0
265         @ lr needed for prologue
266         ldmia   r0, {r0, r1, r2}
267         stmia   sp, {r0, r1, r2}
268         stmia   ip, {r0, r1, r2}
269         mov     r0, ip
270         add     sp, sp, #12
271         bx      lr
272
273 r0 (and later ip) is the hidden parameter from caller to store the value in. The
274 first ldmia loads the constants into r0, r1, r2. The last stmia stores r0, r1,
275 r2 into the address passed in. However, there is one additional stmia that
276 stores r0, r1, and r2 to some stack location. The store is dead.
277
278 The llvm-gcc generated code looks like this:
279
280 csretcc void %foo(%struct.s* %agg.result) {
281 entry:
282         %S = alloca %struct.s, align 4          ; <%struct.s*> [#uses=1]
283         %memtmp = alloca %struct.s              ; <%struct.s*> [#uses=1]
284         cast %struct.s* %S to sbyte*            ; <sbyte*>:0 [#uses=2]
285         call void %llvm.memcpy.i32( sbyte* %0, sbyte* cast ({ double, int }* %C.0.904 to sbyte*), uint 12, uint 4 )
286         cast %struct.s* %agg.result to sbyte*           ; <sbyte*>:1 [#uses=2]
287         call void %llvm.memcpy.i32( sbyte* %1, sbyte* %0, uint 12, uint 0 )
288         cast %struct.s* %memtmp to sbyte*               ; <sbyte*>:2 [#uses=1]
289         call void %llvm.memcpy.i32( sbyte* %2, sbyte* %1, uint 12, uint 0 )
290         ret void
291 }
292
293 llc ends up issuing two memcpy's (the first memcpy becomes 3 loads from
294 constantpool). Perhaps we should 1) fix llvm-gcc so the memcpy is translated
295 into a number of load and stores, or 2) custom lower memcpy (of small size) to
296 be ldmia / stmia. I think option 2 is better but the current register
297 allocator cannot allocate a chunk of registers at a time.
298
299 A feasible temporary solution is to use specific physical registers at the
300 lowering time for small (<= 4 words?) transfer size.
301
302 * ARM CSRet calling convention requires the hidden argument to be returned by
303 the callee.
304
305 //===---------------------------------------------------------------------===//
306
307 We can definitely do a better job on BB placements to eliminate some branches.
308 It's very common to see llvm generated assembly code that looks like this:
309
310 LBB3:
311  ...
312 LBB4:
313 ...
314   beq LBB3
315   b LBB2
316
317 If BB4 is the only predecessor of BB3, then we can emit BB3 after BB4. We can
318 then eliminate beq and and turn the unconditional branch to LBB2 to a bne.
319
320 See McCat/18-imp/ComputeBoundingBoxes for an example.
321
322 //===---------------------------------------------------------------------===//
323
324 Register scavenging is now implemented.  The example in the previous version
325 of this document produces optimal code at -O2.
326
327 //===---------------------------------------------------------------------===//
328
329 Pre-/post- indexed load / stores:
330
331 1) We should not make the pre/post- indexed load/store transform if the base ptr
332 is guaranteed to be live beyond the load/store. This can happen if the base
333 ptr is live out of the block we are performing the optimization. e.g.
334
335 mov r1, r2
336 ldr r3, [r1], #4
337 ...
338
339 vs.
340
341 ldr r3, [r2]
342 add r1, r2, #4
343 ...
344
345 In most cases, this is just a wasted optimization. However, sometimes it can
346 negatively impact the performance because two-address code is more restrictive
347 when it comes to scheduling.
348
349 Unfortunately, liveout information is currently unavailable during DAG combine
350 time.
351
352 2) Consider spliting a indexed load / store into a pair of add/sub + load/store
353    to solve #1 (in TwoAddressInstructionPass.cpp).
354
355 3) Enhance LSR to generate more opportunities for indexed ops.
356
357 4) Once we added support for multiple result patterns, write indexed loads
358    patterns instead of C++ instruction selection code.
359
360 5) Use FLDM / FSTM to emulate indexed FP load / store.
361
362 //===---------------------------------------------------------------------===//
363
364 We should add i64 support to take advantage of the 64-bit load / stores.
365 We can add a pseudo i64 register class containing pseudo registers that are
366 register pairs. All other ops (e.g. add, sub) would be expanded as usual.
367
368 We need to add pseudo instructions (i.e. gethi / getlo) to extract i32 registers
369 from the i64 register. These are single moves which can be eliminated if the
370 destination register is a sub-register of the source. We should implement proper
371 subreg support in the register allocator to coalesce these away.
372
373 There are other minor issues such as multiple instructions for a spill / restore
374 / move.
375
376 //===---------------------------------------------------------------------===//
377
378 Implement support for some more tricky ways to materialize immediates.  For
379 example, to get 0xffff8000, we can use:
380
381 mov r9, #&3f8000
382 sub r9, r9, #&400000
383
384 //===---------------------------------------------------------------------===//
385
386 We sometimes generate multiple add / sub instructions to update sp in prologue
387 and epilogue if the inc / dec value is too large to fit in a single immediate
388 operand. In some cases, perhaps it might be better to load the value from a
389 constantpool instead.
390
391 //===---------------------------------------------------------------------===//
392
393 GCC generates significantly better code for this function.
394
395 int foo(int StackPtr, unsigned char *Line, unsigned char *Stack, int LineLen) {
396     int i = 0;
397
398     if (StackPtr != 0) {
399        while (StackPtr != 0 && i < (((LineLen) < (32768))? (LineLen) : (32768)))
400           Line[i++] = Stack[--StackPtr];
401         if (LineLen > 32768)
402         {
403             while (StackPtr != 0 && i < LineLen)
404             {
405                 i++;
406                 --StackPtr;
407             }
408         }
409     }
410     return StackPtr;
411 }
412
413 //===---------------------------------------------------------------------===//
414
415 This should compile to the mlas instruction:
416 int mlas(int x, int y, int z) { return ((x * y + z) < 0) ? 7 : 13; }
417
418 //===---------------------------------------------------------------------===//
419
420 At some point, we should triage these to see if they still apply to us:
421
422 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19598
423 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18560
424 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27016
425
426 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11831
427 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11826
428 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11825
429 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11824
430 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11823
431 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11820
432 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10982
433
434 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10242
435 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9831
436 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9760
437 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9759
438 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9703
439 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9702
440 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9663
441
442 http://www.inf.u-szeged.hu/gcc-arm/
443 http://citeseer.ist.psu.edu/debus04linktime.html
444
445 //===---------------------------------------------------------------------===//
446
447 gcc generates smaller code for this function at -O2 or -Os:
448
449 void foo(signed char* p) {
450   if (*p == 3)
451      bar();
452    else if (*p == 4)
453     baz();
454   else if (*p == 5)
455     quux();
456 }
457
458 llvm decides it's a good idea to turn the repeated if...else into a
459 binary tree, as if it were a switch; the resulting code requires -1 
460 compare-and-branches when *p<=2 or *p==5, the same number if *p==4
461 or *p>6, and +1 if *p==3.  So it should be a speed win
462 (on balance).  However, the revised code is larger, with 4 conditional 
463 branches instead of 3.
464
465 More seriously, there is a byte->word extend before
466 each comparison, where there should be only one, and the condition codes
467 are not remembered when the same two values are compared twice.
468
469 //===---------------------------------------------------------------------===//
470
471 More register scavenging work:
472
473 1. Use the register scavenger to track frame index materialized into registers
474    (those that do not fit in addressing modes) to allow reuse in the same BB.
475 2. Finish scavenging for Thumb.
476 3. We know some spills and restores are unnecessary. The issue is once live
477    intervals are merged, they are not never split. So every def is spilled
478    and every use requires a restore if the register allocator decides the
479    resulting live interval is not assigned a physical register. It may be
480    possible (with the help of the scavenger) to turn some spill / restore
481    pairs into register copies.
482
483 //===---------------------------------------------------------------------===//
484
485 More LSR enhancements possible:
486
487 1. Teach LSR about pre- and post- indexed ops to allow iv increment be merged
488    in a load / store.
489 2. Allow iv reuse even when a type conversion is required. For example, i8
490    and i32 load / store addressing modes are identical.
491
492
493 //===---------------------------------------------------------------------===//
494
495 This:
496
497 int foo(int a, int b, int c, int d) {
498   long long acc = (long long)a * (long long)b;
499   acc += (long long)c * (long long)d;
500   return (int)(acc >> 32);
501 }
502
503 Should compile to use SMLAL (Signed Multiply Accumulate Long) which multiplies 
504 two signed 32-bit values to produce a 64-bit value, and accumulates this with 
505 a 64-bit value.
506
507 We currently get this with v6:
508
509 _foo:
510         mul r12, r1, r0
511         smmul r1, r1, r0
512         smmul r0, r3, r2
513         mul r3, r3, r2
514         adds r3, r3, r12
515         adc r0, r0, r1
516         bx lr
517
518 and this with v4:
519
520 _foo:
521         stmfd sp!, {r7, lr}
522         mov r7, sp
523         mul r12, r1, r0
524         smull r0, r1, r1, r0
525         smull lr, r0, r3, r2
526         mul r3, r3, r2
527         adds r3, r3, r12
528         adc r0, r0, r1
529         ldmfd sp!, {r7, pc}
530
531 This apparently occurs in real code.
532
533 //===---------------------------------------------------------------------===//