blob: c8e359fced33490039f391d0f0990fcad3998256 [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
79Should support emission of the bswap instruction, probably by adding a new
80DAG node for byte swapping. Also useful on PPC which has byte-swapping loads.
81
Chris Lattner1e4ed932005-11-28 04:52:39 +000082//===---------------------------------------------------------------------===//
83
84Compile this:
85_Bool f(_Bool a) { return a!=1; }
86
87into:
88 movzbl %dil, %eax
89 xorl $1, %eax
90 ret
Evan Cheng8dee8cc2005-12-17 01:25:19 +000091
92//===---------------------------------------------------------------------===//
93
94Some isel ideas:
95
961. Dynamic programming based approach when compile time if not an
97 issue.
982. Code duplication (addressing mode) during isel.
993. Other ideas from "Register-Sensitive Selection, Duplication, and
100 Sequencing of Instructions".
101
102//===---------------------------------------------------------------------===//
103
104Should we promote i16 to i32 to avoid partial register update stalls?
Evan Cheng98abbfb2005-12-17 06:54:43 +0000105
106//===---------------------------------------------------------------------===//
107
108Leave any_extend as pseudo instruction and hint to register
109allocator. Delay codegen until post register allocation.
Evan Chenga3195e82006-01-12 22:54:21 +0000110
111//===---------------------------------------------------------------------===//
112
113Add a target specific hook to DAG combiner to handle SINT_TO_FP and
114FP_TO_SINT when the source operand is already in memory.
115
116//===---------------------------------------------------------------------===//
117
118Check if load folding would add a cycle in the dag.
Evan Chenge08c2702006-01-13 01:20:42 +0000119
120//===---------------------------------------------------------------------===//
121
122Model X86 EFLAGS as a real register to avoid redudant cmp / test. e.g.
123
124 cmpl $1, %eax
125 setg %al
126 testb %al, %al # unnecessary
127 jne .BB7
Chris Lattner1db4b4f2006-01-16 17:53:00 +0000128
129//===---------------------------------------------------------------------===//
130
131Count leading zeros and count trailing zeros:
132
133int clz(int X) { return __builtin_clz(X); }
134int ctz(int X) { return __builtin_ctz(X); }
135
136$ gcc t.c -S -o - -O3 -fomit-frame-pointer -masm=intel
137clz:
138 bsr %eax, DWORD PTR [%esp+4]
139 xor %eax, 31
140 ret
141ctz:
142 bsf %eax, DWORD PTR [%esp+4]
143 ret
144
145however, check that these are defined for 0 and 32. Our intrinsics are, GCC's
146aren't.
147
148//===---------------------------------------------------------------------===//
149
150Use push/pop instructions in prolog/epilog sequences instead of stores off
151ESP (certain code size win, perf win on some [which?] processors).
152
153//===---------------------------------------------------------------------===//
154
155Only use inc/neg/not instructions on processors where they are faster than
156add/sub/xor. They are slower on the P4 due to only updating some processor
157flags.
158
159//===---------------------------------------------------------------------===//
160
161Open code rint,floor,ceil,trunc:
162http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02006.html
163http://gcc.gnu.org/ml/gcc-patches/2004-08/msg02011.html
164
165//===---------------------------------------------------------------------===//
166
167Combine: a = sin(x), b = cos(x) into a,b = sincos(x).
168
Evan Chenge826a012006-01-27 22:11:01 +0000169//===---------------------------------------------------------------------===//
170
Reid Spencer2ce5b262006-01-29 06:48:25 +0000171For all targets, not just X86:
172When llvm.memcpy, llvm.memset, or llvm.memmove are lowered, they should be
173optimized to a few store instructions if the source is constant and the length
174is smallish (< 8). This will greatly help some tests like Shootout/strcat.c
175
176//===---------------------------------------------------------------------===//
177
Evan Chenge826a012006-01-27 22:11:01 +0000178Solve this DAG isel folding deficiency:
179
180int X, Y;
181
182void fn1(void)
183{
184 X = X | (Y << 3);
185}
186
187compiles to
188
189fn1:
190 movl Y, %eax
191 shll $3, %eax
192 orl X, %eax
193 movl %eax, X
194 ret
195
196The problem is the store's chain operand is not the load X but rather
Evan Chengd41e9e52006-01-27 22:54:32 +0000197a TokenFactor of the load X and load Y, which prevents the folding.
198
199There are two ways to fix this:
200
2011. The dag combiner can start using alias analysis to realize that y/x
202 don't alias, making the store to X not dependent on the load from Y.
2032. The generated isel could be made smarter in the case it can't
204 disambiguate the pointers.
205
206Number 1 is the preferred solution.
Chris Lattnerb638cd82006-01-29 09:08:15 +0000207
208//===---------------------------------------------------------------------===//
209
210The instruction selector sometimes misses folding a load into a compare. The
211pattern is written as (cmp reg, (load p)). Because the compare isn't
212commutative, it is not matched with the load on both sides. The dag combiner
213should be made smart enough to cannonicalize the load into the RHS of a compare
214when it can invert the result of the compare for free.
215
Chris Lattner6a284562006-01-29 09:14:47 +0000216//===---------------------------------------------------------------------===//
217
Chris Lattner5164a312006-01-29 09:42:20 +0000218LSR should be turned on for the X86 backend and tuned to take advantage of its
219addressing modes.
220
Chris Lattnerc7097af2006-01-29 09:46:06 +0000221//===---------------------------------------------------------------------===//
222
223When compiled with unsafemath enabled, "main" should enable SSE DAZ mode and
224other fast SSE modes.
Chris Lattnerbdde4652006-01-31 00:20:38 +0000225
226//===---------------------------------------------------------------------===//
227
Chris Lattner594086d2006-01-31 00:45:37 +0000228Think about doing i64 math in SSE regs.
229
Chris Lattner8e38ae62006-01-31 02:10:06 +0000230//===---------------------------------------------------------------------===//
231
232The DAG Isel doesn't fold the loads into the adds in this testcase. The
233pattern selector does. This is because the chain value of the load gets
234selected first, and the loads aren't checking to see if they are only used by
235and add.
236
237.ll:
238
239int %test(int* %x, int* %y, int* %z) {
240 %X = load int* %x
241 %Y = load int* %y
242 %Z = load int* %z
243 %a = add int %X, %Y
244 %b = add int %a, %Z
245 ret int %b
246}
247
248dag isel:
249
250_test:
251 movl 4(%esp), %eax
252 movl (%eax), %eax
253 movl 8(%esp), %ecx
254 movl (%ecx), %ecx
255 addl %ecx, %eax
256 movl 12(%esp), %ecx
257 movl (%ecx), %ecx
258 addl %ecx, %eax
259 ret
260
261pattern isel:
262
263_test:
264 movl 12(%esp), %ecx
265 movl 4(%esp), %edx
266 movl 8(%esp), %eax
267 movl (%eax), %eax
268 addl (%edx), %eax
269 addl (%ecx), %eax
270 ret
271
272This is bad for register pressure, though the dag isel is producing a
273better schedule. :)
Chris Lattner3e1d5e52006-02-01 01:44:25 +0000274
275//===---------------------------------------------------------------------===//
276
277This testcase should have no SSE instructions in it, and only one load from
278a constant pool:
279
280double %test3(bool %B) {
281 %C = select bool %B, double 123.412, double 523.01123123
282 ret double %C
283}
284
285Currently, the select is being lowered, which prevents the dag combiner from
286turning 'select (load CPI1), (load CPI2)' -> 'load (select CPI1, CPI2)'
287
288The pattern isel got this one right.
289
Chris Lattner1f7c6302006-02-01 06:40:32 +0000290//===---------------------------------------------------------------------===//
291
Chris Lattner3e2b94a2006-02-01 21:44:48 +0000292We need to lower switch statements to tablejumps when appropriate instead of
293always into binary branch trees.
Chris Lattner4d7db402006-02-01 23:38:08 +0000294
295//===---------------------------------------------------------------------===//
296
297SSE doesn't have [mem] op= reg instructions. If we have an SSE instruction
298like this:
299
300 X += y
301
302and the register allocator decides to spill X, it is cheaper to emit this as:
303
304Y += [xslot]
305store Y -> [xslot]
306
307than as:
308
309tmp = [xslot]
310tmp += y
311store tmp -> [xslot]
312
313..and this uses one fewer register (so this should be done at load folding
314time, not at spiller time). *Note* however that this can only be done
315if Y is dead. Here's a testcase:
316
317%.str_3 = external global [15 x sbyte] ; <[15 x sbyte]*> [#uses=0]
318implementation ; Functions:
319declare void %printf(int, ...)
320void %main() {
321build_tree.exit:
322 br label %no_exit.i7
323no_exit.i7: ; preds = %no_exit.i7, %build_tree.exit
324 %tmp.0.1.0.i9 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.34.i18, %no_exit.i7 ] ; <double> [#uses=1]
325 %tmp.0.0.0.i10 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.28.i16, %no_exit.i7 ] ; <double> [#uses=1]
326 %tmp.28.i16 = add double %tmp.0.0.0.i10, 0.000000e+00
327 %tmp.34.i18 = add double %tmp.0.1.0.i9, 0.000000e+00
328 br bool false, label %Compute_Tree.exit23, label %no_exit.i7
329Compute_Tree.exit23: ; preds = %no_exit.i7
330 tail call void (int, ...)* %printf( int 0 )
331 store double %tmp.34.i18, double* null
332 ret void
333}
334
335We currently emit:
336
337.BBmain_1:
338 xorpd %XMM1, %XMM1
339 addsd %XMM0, %XMM1
340*** movsd %XMM2, QWORD PTR [%ESP + 8]
341*** addsd %XMM2, %XMM1
342*** movsd QWORD PTR [%ESP + 8], %XMM2
343 jmp .BBmain_1 # no_exit.i7
344
345This is a bugpoint reduced testcase, which is why the testcase doesn't make
346much sense (e.g. its an infinite loop). :)
347
Evan Cheng8b6e4e62006-02-02 02:40:17 +0000348//===---------------------------------------------------------------------===//
349
350None of the FPStack instructions are handled in
351X86RegisterInfo::foldMemoryOperand, which prevents the spiller from
352folding spill code into the instructions.
Chris Lattner9acddcd2006-02-02 19:16:34 +0000353
354//===---------------------------------------------------------------------===//
355
356In many cases, LLVM generates code like this:
357
358_test:
359 movl 8(%esp), %eax
360 cmpl %eax, 4(%esp)
361 setl %al
362 movzbl %al, %eax
363 ret
364
365on some processors (which ones?), it is more efficient to do this:
366
367_test:
368 movl 8(%esp), %ebx
369 xor %eax, %eax
370 cmpl %ebx, 4(%esp)
371 setl %al
372 ret
373
374Doing this correctly is tricky though, as the xor clobbers the flags.
375
376