blob: e139f03310206e11edf0b99f5fb2de442355579a [file] [log] [blame]
Chris Lattner1171ff42005-10-23 19:52:42 +00001//===---------------------------------------------------------------------===//
2// Random ideas for the X86 backend.
3//===---------------------------------------------------------------------===//
4
5Add a MUL2U and MUL2S nodes to represent a multiply that returns both the
6Hi and Lo parts (combination of MUL and MULH[SU] into one node). Add this to
7X86, & make the dag combiner produce it when needed. This will eliminate one
8imul from the code generated for:
9
10long long test(long long X, long long Y) { return X*Y; }
11
12by using the EAX result from the mul. We should add a similar node for
13DIVREM.
14
Chris Lattner865874c2005-12-02 00:11:20 +000015another case is:
16
17long long test(int X, int Y) { return (long long)X*Y; }
18
19... which should only be one imul instruction.
20
Chris Lattner1171ff42005-10-23 19:52:42 +000021//===---------------------------------------------------------------------===//
22
23This should be one DIV/IDIV instruction, not a libcall:
24
25unsigned test(unsigned long long X, unsigned Y) {
26 return X/Y;
27}
28
29This can be done trivially with a custom legalizer. What about overflow
30though? http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
31
32//===---------------------------------------------------------------------===//
33
Chris Lattner1171ff42005-10-23 19:52:42 +000034Some targets (e.g. athlons) prefer freep to fstp ST(0):
35http://gcc.gnu.org/ml/gcc-patches/2004-04/msg00659.html
36
37//===---------------------------------------------------------------------===//
38
Evan Chenga3195e82006-01-12 22:54:21 +000039This should use fiadd on chips where it is profitable:
Chris Lattner1171ff42005-10-23 19:52:42 +000040double foo(double P, int *I) { return P+*I; }
41
42//===---------------------------------------------------------------------===//
43
44The FP stackifier needs to be global. Also, it should handle simple permutates
45to reduce number of shuffle instructions, e.g. turning:
46
47fld P -> fld Q
48fld Q fld P
49fxch
50
51or:
52
53fxch -> fucomi
54fucomi jl X
55jg X
56
Chris Lattner1db4b4f2006-01-16 17:53:00 +000057Ideas:
58http://gcc.gnu.org/ml/gcc-patches/2004-11/msg02410.html
59
60
Chris Lattner1171ff42005-10-23 19:52:42 +000061//===---------------------------------------------------------------------===//
62
63Improvements to the multiply -> shift/add algorithm:
64http://gcc.gnu.org/ml/gcc-patches/2004-08/msg01590.html
65
66//===---------------------------------------------------------------------===//
67
68Improve code like this (occurs fairly frequently, e.g. in LLVM):
69long long foo(int x) { return 1LL << x; }
70
71http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01109.html
72http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01128.html
73http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01136.html
74
75Another useful one would be ~0ULL >> X and ~0ULL << X.
76
Chris Lattnerffff6172005-10-23 21:44:59 +000077//===---------------------------------------------------------------------===//
78
Chris Lattner1e4ed932005-11-28 04:52:39 +000079Compile this:
80_Bool f(_Bool a) { return a!=1; }
81
82into:
83 movzbl %dil, %eax
84 xorl $1, %eax
85 ret
Evan Cheng8dee8cc2005-12-17 01:25:19 +000086
87//===---------------------------------------------------------------------===//
88
89Some isel ideas:
90
911. Dynamic programming based approach when compile time if not an
92 issue.
932. Code duplication (addressing mode) during isel.
943. Other ideas from "Register-Sensitive Selection, Duplication, and
95 Sequencing of Instructions".
96
97//===---------------------------------------------------------------------===//
98
99Should we promote i16 to i32 to avoid partial register update stalls?
Evan Cheng98abbfb2005-12-17 06:54:43 +0000100
101//===---------------------------------------------------------------------===//
102
103Leave any_extend as pseudo instruction and hint to register
104allocator. Delay codegen until post register allocation.
Evan Chenga3195e82006-01-12 22:54:21 +0000105
106//===---------------------------------------------------------------------===//
107
108Add a target specific hook to DAG combiner to handle SINT_TO_FP and
109FP_TO_SINT when the source operand is already in memory.
110
111//===---------------------------------------------------------------------===//
112
113Check if load folding would add a cycle in the dag.
Evan Chenge08c2702006-01-13 01:20:42 +0000114
115//===---------------------------------------------------------------------===//
116
117Model X86 EFLAGS as a real register to avoid redudant cmp / test. e.g.
118
119 cmpl $1, %eax
120 setg %al
121 testb %al, %al # unnecessary
122 jne .BB7
Chris Lattner1db4b4f2006-01-16 17:53:00 +0000123
124//===---------------------------------------------------------------------===//
125
126Count leading zeros and count trailing zeros:
127
128int clz(int X) { return __builtin_clz(X); }
129int ctz(int X) { return __builtin_ctz(X); }
130
131$ gcc t.c -S -o - -O3 -fomit-frame-pointer -masm=intel
132clz:
133 bsr %eax, DWORD PTR [%esp+4]
134 xor %eax, 31
135 ret
136ctz:
137 bsf %eax, DWORD PTR [%esp+4]
138 ret
139
140however, check that these are defined for 0 and 32. Our intrinsics are, GCC's
141aren't.
142
143//===---------------------------------------------------------------------===//
144
145Use push/pop instructions in prolog/epilog sequences instead of stores off
146ESP (certain code size win, perf win on some [which?] processors).
147
148//===---------------------------------------------------------------------===//
149
150Only use inc/neg/not instructions on processors where they are faster than
151add/sub/xor. They are slower on the P4 due to only updating some processor
152flags.
153
154//===---------------------------------------------------------------------===//
155
156Open code rint,floor,ceil,trunc:
157http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02006.html
158http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02011.html
159
160//===---------------------------------------------------------------------===//
161
162Combine: a = sin(x), b = cos(x) into a,b = sincos(x).
163
Evan Chenge826a012006-01-27 22:11:01 +0000164//===---------------------------------------------------------------------===//
165
Chris Lattnerb638cd82006-01-29 09:08:15 +0000166The instruction selector sometimes misses folding a load into a compare. The
167pattern is written as (cmp reg, (load p)). Because the compare isn't
168commutative, it is not matched with the load on both sides. The dag combiner
169should be made smart enough to cannonicalize the load into the RHS of a compare
170when it can invert the result of the compare for free.
171
Chris Lattner6a284562006-01-29 09:14:47 +0000172//===---------------------------------------------------------------------===//
173
Chris Lattner5164a312006-01-29 09:42:20 +0000174LSR should be turned on for the X86 backend and tuned to take advantage of its
175addressing modes.
176
Chris Lattnerc7097af2006-01-29 09:46:06 +0000177//===---------------------------------------------------------------------===//
178
179When compiled with unsafemath enabled, "main" should enable SSE DAZ mode and
180other fast SSE modes.
Chris Lattnerbdde4652006-01-31 00:20:38 +0000181
182//===---------------------------------------------------------------------===//
183
Chris Lattner594086d2006-01-31 00:45:37 +0000184Think about doing i64 math in SSE regs.
185
Chris Lattner8e38ae62006-01-31 02:10:06 +0000186//===---------------------------------------------------------------------===//
187
188The DAG Isel doesn't fold the loads into the adds in this testcase. The
189pattern selector does. This is because the chain value of the load gets
190selected first, and the loads aren't checking to see if they are only used by
191and add.
192
193.ll:
194
195int %test(int* %x, int* %y, int* %z) {
196 %X = load int* %x
197 %Y = load int* %y
198 %Z = load int* %z
199 %a = add int %X, %Y
200 %b = add int %a, %Z
201 ret int %b
202}
203
204dag isel:
205
206_test:
207 movl 4(%esp), %eax
208 movl (%eax), %eax
209 movl 8(%esp), %ecx
210 movl (%ecx), %ecx
211 addl %ecx, %eax
212 movl 12(%esp), %ecx
213 movl (%ecx), %ecx
214 addl %ecx, %eax
215 ret
216
217pattern isel:
218
219_test:
220 movl 12(%esp), %ecx
221 movl 4(%esp), %edx
222 movl 8(%esp), %eax
223 movl (%eax), %eax
224 addl (%edx), %eax
225 addl (%ecx), %eax
226 ret
227
228This is bad for register pressure, though the dag isel is producing a
229better schedule. :)
Chris Lattner3e1d5e52006-02-01 01:44:25 +0000230
231//===---------------------------------------------------------------------===//
232
233This testcase should have no SSE instructions in it, and only one load from
234a constant pool:
235
236double %test3(bool %B) {
237 %C = select bool %B, double 123.412, double 523.01123123
238 ret double %C
239}
240
241Currently, the select is being lowered, which prevents the dag combiner from
242turning 'select (load CPI1), (load CPI2)' -> 'load (select CPI1, CPI2)'
243
244The pattern isel got this one right.
245
Chris Lattner1f7c6302006-02-01 06:40:32 +0000246//===---------------------------------------------------------------------===//
247
Chris Lattner3e2b94a2006-02-01 21:44:48 +0000248We need to lower switch statements to tablejumps when appropriate instead of
249always into binary branch trees.
Chris Lattner4d7db402006-02-01 23:38:08 +0000250
251//===---------------------------------------------------------------------===//
252
253SSE doesn't have [mem] op= reg instructions. If we have an SSE instruction
254like this:
255
256 X += y
257
258and the register allocator decides to spill X, it is cheaper to emit this as:
259
260Y += [xslot]
261store Y -> [xslot]
262
263than as:
264
265tmp = [xslot]
266tmp += y
267store tmp -> [xslot]
268
269..and this uses one fewer register (so this should be done at load folding
270time, not at spiller time). *Note* however that this can only be done
271if Y is dead. Here's a testcase:
272
273%.str_3 = external global [15 x sbyte] ; <[15 x sbyte]*> [#uses=0]
274implementation ; Functions:
275declare void %printf(int, ...)
276void %main() {
277build_tree.exit:
278 br label %no_exit.i7
279no_exit.i7: ; preds = %no_exit.i7, %build_tree.exit
280 %tmp.0.1.0.i9 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.34.i18, %no_exit.i7 ] ; <double> [#uses=1]
281 %tmp.0.0.0.i10 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.28.i16, %no_exit.i7 ] ; <double> [#uses=1]
282 %tmp.28.i16 = add double %tmp.0.0.0.i10, 0.000000e+00
283 %tmp.34.i18 = add double %tmp.0.1.0.i9, 0.000000e+00
284 br bool false, label %Compute_Tree.exit23, label %no_exit.i7
285Compute_Tree.exit23: ; preds = %no_exit.i7
286 tail call void (int, ...)* %printf( int 0 )
287 store double %tmp.34.i18, double* null
288 ret void
289}
290
291We currently emit:
292
293.BBmain_1:
294 xorpd %XMM1, %XMM1
295 addsd %XMM0, %XMM1
296*** movsd %XMM2, QWORD PTR [%ESP + 8]
297*** addsd %XMM2, %XMM1
298*** movsd QWORD PTR [%ESP + 8], %XMM2
299 jmp .BBmain_1 # no_exit.i7
300
301This is a bugpoint reduced testcase, which is why the testcase doesn't make
302much sense (e.g. its an infinite loop). :)
303
Evan Cheng8b6e4e62006-02-02 02:40:17 +0000304//===---------------------------------------------------------------------===//
305
306None of the FPStack instructions are handled in
307X86RegisterInfo::foldMemoryOperand, which prevents the spiller from
308folding spill code into the instructions.
Chris Lattner9acddcd2006-02-02 19:16:34 +0000309
310//===---------------------------------------------------------------------===//
311
312In many cases, LLVM generates code like this:
313
314_test:
315 movl 8(%esp), %eax
316 cmpl %eax, 4(%esp)
317 setl %al
318 movzbl %al, %eax
319 ret
320
321on some processors (which ones?), it is more efficient to do this:
322
323_test:
324 movl 8(%esp), %ebx
325 xor %eax, %eax
326 cmpl %ebx, 4(%esp)
327 setl %al
328 ret
329
330Doing this correctly is tricky though, as the xor clobbers the flags.
331
Chris Lattnerd395d092006-02-02 19:43:28 +0000332//===---------------------------------------------------------------------===//
333
334We should generate 'test' instead of 'cmp' in various cases, e.g.:
335
336bool %test(int %X) {
337 %Y = shl int %X, ubyte 1
338 %C = seteq int %Y, 0
339 ret bool %C
340}
341bool %test(int %X) {
342 %Y = and int %X, 8
343 %C = seteq int %Y, 0
344 ret bool %C
345}
346
347This may just be a matter of using 'test' to write bigger patterns for X86cmp.
348
349//===---------------------------------------------------------------------===//
350
351Evaluate whether using movapd for SSE reg-reg moves is faster than using
352movsd/movss for them. It may eliminate false partial register dependences by
353writing the whole result register.
354
355//===---------------------------------------------------------------------===//
356
357SSE should implement 'select_cc' using 'emulated conditional moves' that use
358pcmp/pand/pandn/por to do a selection instead of a conditional branch:
359
360double %X(double %Y, double %Z, double %A, double %B) {
361 %C = setlt double %A, %B
362 %z = add double %Z, 0.0 ;; select operand is not a load
363 %D = select bool %C, double %Y, double %z
364 ret double %D
365}
366
367We currently emit:
368
369_X:
370 subl $12, %esp
371 xorpd %xmm0, %xmm0
372 addsd 24(%esp), %xmm0
373 movsd 32(%esp), %xmm1
374 movsd 16(%esp), %xmm2
375 ucomisd 40(%esp), %xmm1
376 jb LBB_X_2
377LBB_X_1:
378 movsd %xmm0, %xmm2
379LBB_X_2:
380 movsd %xmm2, (%esp)
381 fldl (%esp)
382 addl $12, %esp
383 ret
Chris Lattner9acddcd2006-02-02 19:16:34 +0000384
Evan Cheng183fff92006-02-07 08:35:44 +0000385//===---------------------------------------------------------------------===//
386
387The x86 backend currently supports dynamic-no-pic. Need to add asm
388printer support for static and PIC.