cosmetic changes from review of last patch. obvious
[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 The constant island pass has been much improved; all the todo items in the
21 previous version of this document have been addressed.  However, there are still
22 things that can be done:
23
24 1.  When there isn't an existing water, the current MBB is split right after 
25 the use.  It would be profitable to look farther forward, especially on Thumb,
26 where negative offsets won't work.
27 (Partially fixed:  it will put the island at the end of the block if that is 
28 in range.  If it is not in range things still work as above, which is poor on 
29 Thumb.)
30
31 2.  There may be some advantage to trying to be smarter about the initial
32 placement, rather than putting everything at the end.
33
34 3.  The handling of 2-byte padding for Thumb is overly conservative.  There 
35 would be a small gain to keeping accurate track of the padding (which would
36 require aligning functions containing constant pools to 4-byte boundaries).
37
38 //===---------------------------------------------------------------------===//
39
40 We need to start generating predicated instructions.  The .td files have a way
41 to express this now (see the PPC conditional return instruction), but the 
42 branch folding pass (or a new if-cvt pass) should start producing these, at
43 least in the trivial case.
44
45 Among the obvious wins, doing so can eliminate the need to custom expand 
46 copysign (i.e. we won't need to custom expand it to get the conditional
47 negate).
48
49 This allows us to eliminate one instruction from:
50
51 define i32 @_Z6slow4bii(i32 %x, i32 %y) {
52         %tmp = icmp sgt i32 %x, %y
53         %retval = select i1 %tmp, i32 %x, i32 %y
54         ret i32 %retval
55 }
56
57 __Z6slow4bii:
58         cmp r0, r1
59         movgt r1, r0
60         mov r0, r1
61         bx lr
62
63 //===---------------------------------------------------------------------===//
64
65 Implement long long "X-3" with instructions that fold the immediate in.  These
66 were disabled due to badness with the ARM carry flag on subtracts.
67
68 //===---------------------------------------------------------------------===//
69
70 We currently compile abs:
71 int foo(int p) { return p < 0 ? -p : p; }
72
73 into:
74
75 _foo:
76         rsb r1, r0, #0
77         cmn r0, #1
78         movgt r1, r0
79         mov r0, r1
80         bx lr
81
82 This is very, uh, literal.  This could be a 3 operation sequence:
83   t = (p sra 31); 
84   res = (p xor t)-t
85
86 Which would be better.  This occurs in png decode.
87
88 //===---------------------------------------------------------------------===//
89
90 More load / store optimizations:
91 1) Look past instructions without side-effects (not load, store, branch, etc.)
92    when forming the list of loads / stores to optimize.
93
94 2) Smarter register allocation?
95 We are probably missing some opportunities to use ldm / stm. Consider:
96
97 ldr r5, [r0]
98 ldr r4, [r0, #4]
99
100 This cannot be merged into a ldm. Perhaps we will need to do the transformation
101 before register allocation. Then teach the register allocator to allocate a
102 chunk of consecutive registers.
103
104 3) Better representation for block transfer? This is from Olden/power:
105
106         fldd d0, [r4]
107         fstd d0, [r4, #+32]
108         fldd d0, [r4, #+8]
109         fstd d0, [r4, #+40]
110         fldd d0, [r4, #+16]
111         fstd d0, [r4, #+48]
112         fldd d0, [r4, #+24]
113         fstd d0, [r4, #+56]
114
115 If we can spare the registers, it would be better to use fldm and fstm here.
116 Need major register allocator enhancement though.
117
118 4) Can we recognize the relative position of constantpool entries? i.e. Treat
119
120         ldr r0, LCPI17_3
121         ldr r1, LCPI17_4
122         ldr r2, LCPI17_5
123
124    as
125         ldr r0, LCPI17
126         ldr r1, LCPI17+4
127         ldr r2, LCPI17+8
128
129    Then the ldr's can be combined into a single ldm. See Olden/power.
130
131 Note for ARM v4 gcc uses ldmia to load a pair of 32-bit values to represent a
132 double 64-bit FP constant:
133
134         adr     r0, L6
135         ldmia   r0, {r0-r1}
136
137         .align 2
138 L6:
139         .long   -858993459
140         .long   1074318540
141
142 5) Can we make use of ldrd and strd? Instead of generating ldm / stm, use
143 ldrd/strd instead if there are only two destination registers that form an
144 odd/even pair. However, we probably would pay a penalty if the address is not
145 aligned on 8-byte boundary. This requires more information on load / store
146 nodes (and MI's?) then we currently carry.
147
148 //===---------------------------------------------------------------------===//
149
150 * Consider this silly example:
151
152 double bar(double x) {  
153   double r = foo(3.1);
154   return x+r;
155 }
156
157 _bar:
158         sub sp, sp, #16
159         str r4, [sp, #+12]
160         str r5, [sp, #+8]
161         str lr, [sp, #+4]
162         mov r4, r0
163         mov r5, r1
164         ldr r0, LCPI2_0
165         bl _foo
166         fmsr f0, r0
167         fcvtsd d0, f0
168         fmdrr d1, r4, r5
169         faddd d0, d0, d1
170         fmrrd r0, r1, d0
171         ldr lr, [sp, #+4]
172         ldr r5, [sp, #+8]
173         ldr r4, [sp, #+12]
174         add sp, sp, #16
175         bx lr
176
177 Ignore the prologue and epilogue stuff for a second. Note 
178         mov r4, r0
179         mov r5, r1
180 the copys to callee-save registers and the fact they are only being used by the
181 fmdrr instruction. It would have been better had the fmdrr been scheduled
182 before the call and place the result in a callee-save DPR register. The two
183 mov ops would not have been necessary.
184
185 //===---------------------------------------------------------------------===//
186
187 Calling convention related stuff:
188
189 * gcc's parameter passing implementation is terrible and we suffer as a result:
190
191 e.g.
192 struct s {
193   double d1;
194   int s1;
195 };
196
197 void foo(struct s S) {
198   printf("%g, %d\n", S.d1, S.s1);
199 }
200
201 'S' is passed via registers r0, r1, r2. But gcc stores them to the stack, and
202 then reload them to r1, r2, and r3 before issuing the call (r0 contains the
203 address of the format string):
204
205         stmfd   sp!, {r7, lr}
206         add     r7, sp, #0
207         sub     sp, sp, #12
208         stmia   sp, {r0, r1, r2}
209         ldmia   sp, {r1-r2}
210         ldr     r0, L5
211         ldr     r3, [sp, #8]
212 L2:
213         add     r0, pc, r0
214         bl      L_printf$stub
215
216 Instead of a stmia, ldmia, and a ldr, wouldn't it be better to do three moves?
217
218 * Return an aggregate type is even worse:
219
220 e.g.
221 struct s foo(void) {
222   struct s S = {1.1, 2};
223   return S;
224 }
225
226         mov     ip, r0
227         ldr     r0, L5
228         sub     sp, sp, #12
229 L2:
230         add     r0, pc, r0
231         @ lr needed for prologue
232         ldmia   r0, {r0, r1, r2}
233         stmia   sp, {r0, r1, r2}
234         stmia   ip, {r0, r1, r2}
235         mov     r0, ip
236         add     sp, sp, #12
237         bx      lr
238
239 r0 (and later ip) is the hidden parameter from caller to store the value in. The
240 first ldmia loads the constants into r0, r1, r2. The last stmia stores r0, r1,
241 r2 into the address passed in. However, there is one additional stmia that
242 stores r0, r1, and r2 to some stack location. The store is dead.
243
244 The llvm-gcc generated code looks like this:
245
246 csretcc void %foo(%struct.s* %agg.result) {
247 entry:
248         %S = alloca %struct.s, align 4          ; <%struct.s*> [#uses=1]
249         %memtmp = alloca %struct.s              ; <%struct.s*> [#uses=1]
250         cast %struct.s* %S to sbyte*            ; <sbyte*>:0 [#uses=2]
251         call void %llvm.memcpy.i32( sbyte* %0, sbyte* cast ({ double, int }* %C.0.904 to sbyte*), uint 12, uint 4 )
252         cast %struct.s* %agg.result to sbyte*           ; <sbyte*>:1 [#uses=2]
253         call void %llvm.memcpy.i32( sbyte* %1, sbyte* %0, uint 12, uint 0 )
254         cast %struct.s* %memtmp to sbyte*               ; <sbyte*>:2 [#uses=1]
255         call void %llvm.memcpy.i32( sbyte* %2, sbyte* %1, uint 12, uint 0 )
256         ret void
257 }
258
259 llc ends up issuing two memcpy's (the first memcpy becomes 3 loads from
260 constantpool). Perhaps we should 1) fix llvm-gcc so the memcpy is translated
261 into a number of load and stores, or 2) custom lower memcpy (of small size) to
262 be ldmia / stmia. I think option 2 is better but the current register
263 allocator cannot allocate a chunk of registers at a time.
264
265 A feasible temporary solution is to use specific physical registers at the
266 lowering time for small (<= 4 words?) transfer size.
267
268 * ARM CSRet calling convention requires the hidden argument to be returned by
269 the callee.
270
271 //===---------------------------------------------------------------------===//
272
273 We can definitely do a better job on BB placements to eliminate some branches.
274 It's very common to see llvm generated assembly code that looks like this:
275
276 LBB3:
277  ...
278 LBB4:
279 ...
280   beq LBB3
281   b LBB2
282
283 If BB4 is the only predecessor of BB3, then we can emit BB3 after BB4. We can
284 then eliminate beq and and turn the unconditional branch to LBB2 to a bne.
285
286 See McCat/18-imp/ComputeBoundingBoxes for an example.
287
288 //===---------------------------------------------------------------------===//
289
290 We need register scavenging.  Currently, the 'ip' register is reserved in case
291 frame indexes are too big.  This means that we generate extra code for stuff 
292 like this:
293
294 void foo(unsigned x, unsigned y, unsigned z, unsigned *a, unsigned *b, unsigned *c) { 
295    short Rconst = (short) (16384.0f * 1.40200 + 0.5 );
296    *a = x * Rconst;
297    *b = y * Rconst;
298    *c = z * Rconst;
299 }
300
301 we compile it to:
302
303 _foo:
304 ***     stmfd sp!, {r4, r7}
305 ***     add r7, sp, #4
306         mov r4, #186
307         orr r4, r4, #89, 24 @ 22784
308         mul r0, r0, r4
309         str r0, [r3]
310         mul r0, r1, r4
311         ldr r1, [sp, #+8]
312         str r0, [r1]
313         mul r0, r2, r4
314         ldr r1, [sp, #+12]
315         str r0, [r1]
316 ***     sub sp, r7, #4
317 ***     ldmfd sp!, {r4, r7}
318         bx lr
319
320 GCC produces:
321
322 _foo:
323         ldr     ip, L4
324         mul     r0, ip, r0
325         mul     r1, ip, r1
326         str     r0, [r3, #0]
327         ldr     r3, [sp, #0]
328         mul     r2, ip, r2
329         str     r1, [r3, #0]
330         ldr     r3, [sp, #4]
331         str     r2, [r3, #0]
332         bx      lr
333 L4:
334         .long   22970
335
336 This is apparently all because we couldn't use ip here.
337
338 //===---------------------------------------------------------------------===//
339
340 Pre-/post- indexed load / stores:
341
342 1) We should not make the pre/post- indexed load/store transform if the base ptr
343 is guaranteed to be live beyond the load/store. This can happen if the base
344 ptr is live out of the block we are performing the optimization. e.g.
345
346 mov r1, r2
347 ldr r3, [r1], #4
348 ...
349
350 vs.
351
352 ldr r3, [r2]
353 add r1, r2, #4
354 ...
355
356 In most cases, this is just a wasted optimization. However, sometimes it can
357 negatively impact the performance because two-address code is more restrictive
358 when it comes to scheduling.
359
360 Unfortunately, liveout information is currently unavailable during DAG combine
361 time.
362
363 2) Consider spliting a indexed load / store into a pair of add/sub + load/store
364    to solve #1 (in TwoAddressInstructionPass.cpp).
365
366 3) Enhance LSR to generate more opportunities for indexed ops.
367
368 4) Once we added support for multiple result patterns, write indexed loads
369    patterns instead of C++ instruction selection code.
370
371 5) Use FLDM / FSTM to emulate indexed FP load / store.
372
373 //===---------------------------------------------------------------------===//
374
375 We should add i64 support to take advantage of the 64-bit load / stores.
376 We can add a pseudo i64 register class containing pseudo registers that are
377 register pairs. All other ops (e.g. add, sub) would be expanded as usual.
378
379 We need to add pseudo instructions (i.e. gethi / getlo) to extract i32 registers
380 from the i64 register. These are single moves which can be eliminated if the
381 destination register is a sub-register of the source. We should implement proper
382 subreg support in the register allocator to coalesce these away.
383
384 There are other minor issues such as multiple instructions for a spill / restore
385 / move.
386
387 //===---------------------------------------------------------------------===//
388
389 Implement support for some more tricky ways to materialize immediates.  For
390 example, to get 0xffff8000, we can use:
391
392 mov r9, #&3f8000
393 sub r9, r9, #&400000
394
395 //===---------------------------------------------------------------------===//
396
397 We sometimes generate multiple add / sub instructions to update sp in prologue
398 and epilogue if the inc / dec value is too large to fit in a single immediate
399 operand. In some cases, perhaps it might be better to load the value from a
400 constantpool instead.
401
402 //===---------------------------------------------------------------------===//
403
404 GCC generates significantly better code for this function.
405
406 int foo(int StackPtr, unsigned char *Line, unsigned char *Stack, int LineLen) {
407     int i = 0;
408
409     if (StackPtr != 0) {
410        while (StackPtr != 0 && i < (((LineLen) < (32768))? (LineLen) : (32768)))
411           Line[i++] = Stack[--StackPtr];
412         if (LineLen > 32768)
413         {
414             while (StackPtr != 0 && i < LineLen)
415             {
416                 i++;
417                 --StackPtr;
418             }
419         }
420     }
421     return StackPtr;
422 }
423
424 //===---------------------------------------------------------------------===//
425
426 This should compile to the mlas instruction:
427 int mlas(int x, int y, int z) { return ((x * y + z) < 0) ? 7 : 13; }
428
429 //===---------------------------------------------------------------------===//
430
431 At some point, we should triage these to see if they still apply to us:
432
433 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19598
434 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18560
435 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27016
436
437 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11831
438 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11826
439 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11825
440 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11824
441 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11823
442 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11820
443 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10982
444
445 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10242
446 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9831
447 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9760
448 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9759
449 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9703
450 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9702
451 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9663
452
453 http://www.inf.u-szeged.hu/gcc-arm/
454 http://citeseer.ist.psu.edu/debus04linktime.html
455
456 //===---------------------------------------------------------------------===//