blob: 20f4898b2a4378dde9ccff7dafcd9de1bccc86ef [file] [log] [blame]
Chris Lattnerd1aaee02006-02-03 06:21:43 +00001Target Independent Opportunities:
2
Chris Lattner3cbd1602006-09-28 06:01:17 +00003//===---------------------------------------------------------------------===//
4
Chris Lattner6dc22332006-11-14 01:57:53 +00005With the recent changes to make the implicit def/use set explicit in
6machineinstrs, we should change the target descriptions for 'call' instructions
7so that the .td files don't list all the call-clobbered registers as implicit
8defs. Instead, these should be added by the code generator (e.g. on the dag).
9
10This has a number of uses:
11
121. PPC32/64 and X86 32/64 can avoid having multiple copies of call instructions
13 for their different impdef sets.
142. Targets with multiple calling convs (e.g. x86) which have different clobber
15 sets don't need copies of call instructions.
163. 'Interprocedural register allocation' can be done to reduce the clobber sets
17 of calls.
18
19//===---------------------------------------------------------------------===//
20
Nate Begemanbb01d4f2006-03-17 01:40:33 +000021Make the PPC branch selector target independant
22
23//===---------------------------------------------------------------------===//
Chris Lattnerd1aaee02006-02-03 06:21:43 +000024
25Get the C front-end to expand hypot(x,y) -> llvm.sqrt(x*x+y*y) when errno and
26precision don't matter (ffastmath). Misc/mandel will like this. :)
27
Chris Lattnerd1aaee02006-02-03 06:21:43 +000028//===---------------------------------------------------------------------===//
29
30Solve this DAG isel folding deficiency:
31
32int X, Y;
33
34void fn1(void)
35{
36 X = X | (Y << 3);
37}
38
39compiles to
40
41fn1:
42 movl Y, %eax
43 shll $3, %eax
44 orl X, %eax
45 movl %eax, X
46 ret
47
48The problem is the store's chain operand is not the load X but rather
49a TokenFactor of the load X and load Y, which prevents the folding.
50
51There are two ways to fix this:
52
531. The dag combiner can start using alias analysis to realize that y/x
54 don't alias, making the store to X not dependent on the load from Y.
552. The generated isel could be made smarter in the case it can't
56 disambiguate the pointers.
57
58Number 1 is the preferred solution.
59
Evan Cheng60f49512006-03-13 23:19:10 +000060This has been "fixed" by a TableGen hack. But that is a short term workaround
61which will be removed once the proper fix is made.
62
Chris Lattnerd1aaee02006-02-03 06:21:43 +000063//===---------------------------------------------------------------------===//
64
Chris Lattnere43e5c02006-03-04 01:19:34 +000065On targets with expensive 64-bit multiply, we could LSR this:
66
67for (i = ...; ++i) {
68 x = 1ULL << i;
69
70into:
71 long long tmp = 1;
72 for (i = ...; ++i, tmp+=tmp)
73 x = tmp;
74
75This would be a win on ppc32, but not x86 or ppc64.
76
Chris Lattnerc9a318d2006-03-04 08:44:51 +000077//===---------------------------------------------------------------------===//
Chris Lattner5032c322006-03-05 20:00:08 +000078
79Shrink: (setlt (loadi32 P), 0) -> (setlt (loadi8 Phi), 0)
80
81//===---------------------------------------------------------------------===//
Chris Lattnerbccb0e02006-03-07 02:46:26 +000082
Chris Lattner003f6332006-03-11 20:17:08 +000083Reassociate should turn: X*X*X*X -> t=(X*X) (t*t) to eliminate a multiply.
84
85//===---------------------------------------------------------------------===//
86
Chris Lattner4e56b682006-03-11 20:20:40 +000087Interesting? testcase for add/shift/mul reassoc:
88
89int bar(int x, int y) {
90 return x*x*x+y+x*x*x*x*x*y*y*y*y;
91}
92int foo(int z, int n) {
93 return bar(z, n) + bar(2*z, 2*n);
94}
95
Chris Lattner2cca31f2007-05-05 22:29:06 +000096Reassociate should handle the example in GCC PR16157.
97
Chris Lattner4e56b682006-03-11 20:20:40 +000098//===---------------------------------------------------------------------===//
99
Chris Lattnerf1362992006-03-09 20:13:21 +0000100These two functions should generate the same code on big-endian systems:
101
102int g(int *j,int *l) { return memcmp(j,l,4); }
103int h(int *j, int *l) { return *j - *l; }
104
105this could be done in SelectionDAGISel.cpp, along with other special cases,
106for 1,2,4,8 bytes.
107
108//===---------------------------------------------------------------------===//
109
Chris Lattnere24cf9d2006-03-22 07:33:46 +0000110It would be nice to revert this patch:
111http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20060213/031986.html
112
113And teach the dag combiner enough to simplify the code expanded before
114legalize. It seems plausible that this knowledge would let it simplify other
115stuff too.
116
Chris Lattner0affd762006-03-24 19:59:17 +0000117//===---------------------------------------------------------------------===//
118
Reid Spencer09575ba2007-02-15 03:39:18 +0000119For vector types, TargetData.cpp::getTypeInfo() returns alignment that is equal
Evan Chengdc1161c2006-03-31 22:35:14 +0000120to the type size. It works but can be overly conservative as the alignment of
Reid Spencer09575ba2007-02-15 03:39:18 +0000121specific vector types are target dependent.
Chris Lattner0baebb12006-04-01 04:08:29 +0000122
123//===---------------------------------------------------------------------===//
124
125We should add 'unaligned load/store' nodes, and produce them from code like
126this:
127
128v4sf example(float *P) {
129 return (v4sf){P[0], P[1], P[2], P[3] };
130}
131
132//===---------------------------------------------------------------------===//
133
Reid Spencer09575ba2007-02-15 03:39:18 +0000134We should constant fold vector type casts at the LLVM level, regardless of the
Chris Lattner7a29cf32006-04-02 01:47:20 +0000135cast. Currently we cannot fold some casts because we don't have TargetData
136information in the constant folder, so we don't know the endianness of the
137target!
138
139//===---------------------------------------------------------------------===//
Chris Lattnerd1c3a062006-04-20 18:49:28 +0000140
Chris Lattner4cda95b2006-05-18 18:26:13 +0000141Add support for conditional increments, and other related patterns. Instead
142of:
143
144 movl 136(%esp), %eax
145 cmpl $0, %eax
146 je LBB16_2 #cond_next
147LBB16_1: #cond_true
148 incl _foo
149LBB16_2: #cond_next
150
151emit:
152 movl _foo, %eax
153 cmpl $1, %edi
154 sbbl $-1, %eax
155 movl %eax, _foo
156
157//===---------------------------------------------------------------------===//
Chris Lattner240f8462006-05-19 20:45:08 +0000158
159Combine: a = sin(x), b = cos(x) into a,b = sincos(x).
160
161Expand these to calls of sin/cos and stores:
162 double sincos(double x, double *sin, double *cos);
163 float sincosf(float x, float *sin, float *cos);
164 long double sincosl(long double x, long double *sin, long double *cos);
165
166Doing so could allow SROA of the destination pointers. See also:
167http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17687
168
169//===---------------------------------------------------------------------===//
Chris Lattner29d7bde2006-05-19 21:01:38 +0000170
171Scalar Repl cannot currently promote this testcase to 'ret long cst':
172
Chris Lattner2cca31f2007-05-05 22:29:06 +0000173 %struct.X = type { i32, i32 }
Chris Lattner29d7bde2006-05-19 21:01:38 +0000174 %struct.Y = type { %struct.X }
Chris Lattner2cca31f2007-05-05 22:29:06 +0000175
176define i64 @bar() {
177 %retval = alloca %struct.Y, align 8
178 %tmp12 = getelementptr %struct.Y* %retval, i32 0, i32 0, i32 0
179 store i32 0, i32* %tmp12
180 %tmp15 = getelementptr %struct.Y* %retval, i32 0, i32 0, i32 1
181 store i32 1, i32* %tmp15
182 %retval.upgrd.1 = bitcast %struct.Y* %retval to i64*
183 %retval.upgrd.2 = load i64* %retval.upgrd.1
184 ret i64 %retval.upgrd.2
Chris Lattner29d7bde2006-05-19 21:01:38 +0000185}
186
187it should be extended to do so.
188
189//===---------------------------------------------------------------------===//
Chris Lattner80b0a702006-05-21 03:57:07 +0000190
Chris Lattnerfeeb9c72006-12-11 00:44:03 +0000191-scalarrepl should promote this to be a vector scalar.
192
193 %struct..0anon = type { <4 x float> }
194
Chris Lattner2cca31f2007-05-05 22:29:06 +0000195define void @test1(<4 x float> %V, float* %P) {
Chris Lattnerfeeb9c72006-12-11 00:44:03 +0000196 %u = alloca %struct..0anon, align 16
Chris Lattner2cca31f2007-05-05 22:29:06 +0000197 %tmp = getelementptr %struct..0anon* %u, i32 0, i32 0
Chris Lattnerfeeb9c72006-12-11 00:44:03 +0000198 store <4 x float> %V, <4 x float>* %tmp
199 %tmp1 = bitcast %struct..0anon* %u to [4 x float]*
Chris Lattner2cca31f2007-05-05 22:29:06 +0000200 %tmp.upgrd.1 = getelementptr [4 x float]* %tmp1, i32 0, i32 1
201 %tmp.upgrd.2 = load float* %tmp.upgrd.1
202 %tmp3 = mul float %tmp.upgrd.2, 2.000000e+00
Chris Lattnerfeeb9c72006-12-11 00:44:03 +0000203 store float %tmp3, float* %P
204 ret void
205}
206
207//===---------------------------------------------------------------------===//
208
Chris Lattner80b0a702006-05-21 03:57:07 +0000209Turn this into a single byte store with no load (the other 3 bytes are
210unmodified):
211
212void %test(uint* %P) {
213 %tmp = load uint* %P
214 %tmp14 = or uint %tmp, 3305111552
215 %tmp15 = and uint %tmp14, 3321888767
216 store uint %tmp15, uint* %P
217 ret void
218}
219
Chris Lattnera5d458722006-05-30 21:29:15 +0000220//===---------------------------------------------------------------------===//
221
222dag/inst combine "clz(x)>>5 -> x==0" for 32-bit x.
223
224Compile:
225
226int bar(int x)
227{
228 int t = __builtin_clz(x);
229 return -(t>>5);
230}
231
232to:
233
234_bar: addic r3,r3,-1
235 subfe r3,r3,r3
236 blr
237
Chris Lattnerc9dc3752006-09-15 20:31:36 +0000238//===---------------------------------------------------------------------===//
239
240Legalize should lower ctlz like this:
241 ctlz(x) = popcnt((x-1) & ~x)
242
243on targets that have popcnt but not ctlz. itanium, what else?
Chris Lattnera5d458722006-05-30 21:29:15 +0000244
Chris Lattnerf7e34782006-09-16 23:57:51 +0000245//===---------------------------------------------------------------------===//
246
247quantum_sigma_x in 462.libquantum contains the following loop:
248
249 for(i=0; i<reg->size; i++)
250 {
251 /* Flip the target bit of each basis state */
252 reg->node[i].state ^= ((MAX_UNSIGNED) 1 << target);
253 }
254
255Where MAX_UNSIGNED/state is a 64-bit int. On a 32-bit platform it would be just
256so cool to turn it into something like:
257
Chris Lattner4a13d3b2006-09-18 04:54:35 +0000258 long long Res = ((MAX_UNSIGNED) 1 << target);
Chris Lattnerf7e34782006-09-16 23:57:51 +0000259 if (target < 32) {
260 for(i=0; i<reg->size; i++)
Chris Lattner4a13d3b2006-09-18 04:54:35 +0000261 reg->node[i].state ^= Res & 0xFFFFFFFFULL;
Chris Lattnerf7e34782006-09-16 23:57:51 +0000262 } else {
263 for(i=0; i<reg->size; i++)
Chris Lattner4a13d3b2006-09-18 04:54:35 +0000264 reg->node[i].state ^= Res & 0xFFFFFFFF00000000ULL
Chris Lattnerf7e34782006-09-16 23:57:51 +0000265 }
266
267... which would only do one 32-bit XOR per loop iteration instead of two.
268
269It would also be nice to recognize the reg->size doesn't alias reg->node[i], but
270alas...
271
272//===---------------------------------------------------------------------===//
Chris Lattnerf11327d2006-09-25 17:12:14 +0000273
274This isn't recognized as bswap by instcombine:
275
276unsigned int swap_32(unsigned int v) {
277 v = ((v & 0x00ff00ffU) << 8) | ((v & 0xff00ff00U) >> 8);
278 v = ((v & 0x0000ffffU) << 16) | ((v & 0xffff0000U) >> 16);
279 return v;
280}
281
Chris Lattner4d475f62006-12-08 02:01:32 +0000282Nor is this (yes, it really is bswap):
283
284unsigned long reverse(unsigned v) {
285 unsigned t;
286 t = v ^ ((v << 16) | (v >> 16));
287 t &= ~0xff0000;
288 v = (v << 24) | (v >> 8);
289 return v ^ (t >> 8);
290}
291
Chris Lattnerf11327d2006-09-25 17:12:14 +0000292//===---------------------------------------------------------------------===//
293
294These should turn into single 16-bit (unaligned?) loads on little/big endian
295processors.
296
297unsigned short read_16_le(const unsigned char *adr) {
298 return adr[0] | (adr[1] << 8);
299}
300unsigned short read_16_be(const unsigned char *adr) {
301 return (adr[0] << 8) | adr[1];
302}
303
304//===---------------------------------------------------------------------===//
Chris Lattnerf0540032006-10-24 16:12:47 +0000305
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000306-instcombine should handle this transform:
Reid Spencer266e42b2006-12-23 06:05:41 +0000307 icmp pred (sdiv X / C1 ), C2
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000308when X, C1, and C2 are unsigned. Similarly for udiv and signed operands.
309
310Currently InstCombine avoids this transform but will do it when the signs of
311the operands and the sign of the divide match. See the FIXME in
312InstructionCombining.cpp in the visitSetCondInst method after the switch case
313for Instruction::UDiv (around line 4447) for more details.
314
315The SingleSource/Benchmarks/Shootout-C++/hash and hash2 tests have examples of
316this construct.
Chris Lattner20483732006-11-03 22:27:39 +0000317
318//===---------------------------------------------------------------------===//
319
320Instcombine misses several of these cases (see the testcase in the patch):
321http://gcc.gnu.org/ml/gcc-patches/2006-10/msg01519.html
322
Reid Spencer7e80b0b2006-10-26 06:15:43 +0000323//===---------------------------------------------------------------------===//
Chris Lattner4e03cb12006-11-10 00:23:26 +0000324
325viterbi speeds up *significantly* if the various "history" related copy loops
326are turned into memcpy calls at the source level. We need a "loops to memcpy"
327pass.
328
329//===---------------------------------------------------------------------===//
Nick Lewycky0df2ada2006-11-13 00:23:28 +0000330
Chris Lattner89e58132007-01-16 06:39:48 +0000331Consider:
332
333typedef unsigned U32;
334typedef unsigned long long U64;
335int test (U32 *inst, U64 *regs) {
336 U64 effective_addr2;
337 U32 temp = *inst;
338 int r1 = (temp >> 20) & 0xf;
339 int b2 = (temp >> 16) & 0xf;
340 effective_addr2 = temp & 0xfff;
341 if (b2) effective_addr2 += regs[b2];
342 b2 = (temp >> 12) & 0xf;
343 if (b2) effective_addr2 += regs[b2];
344 effective_addr2 &= regs[4];
345 if ((effective_addr2 & 3) == 0)
346 return 1;
347 return 0;
348}
349
350Note that only the low 2 bits of effective_addr2 are used. On 32-bit systems,
351we don't eliminate the computation of the top half of effective_addr2 because
352we don't have whole-function selection dags. On x86, this means we use one
353extra register for the function when effective_addr2 is declared as U64 than
354when it is declared U32.
355
356//===---------------------------------------------------------------------===//
357
Chris Lattner09a32e42007-02-13 21:44:43 +0000358Promote for i32 bswap can use i64 bswap + shr. Useful on targets with 64-bit
359regs and bswap, like itanium.
360
361//===---------------------------------------------------------------------===//
Chris Lattner43cab752007-03-24 06:01:32 +0000362
363LSR should know what GPR types a target has. This code:
364
365volatile short X, Y; // globals
366
367void foo(int N) {
368 int i;
369 for (i = 0; i < N; i++) { X = i; Y = i*4; }
370}
371
372produces two identical IV's (after promotion) on PPC/ARM:
373
374LBB1_1: @bb.preheader
375 mov r3, #0
376 mov r2, r3
377 mov r1, r3
378LBB1_2: @bb
379 ldr r12, LCPI1_0
380 ldr r12, [r12]
381 strh r2, [r12]
382 ldr r12, LCPI1_1
383 ldr r12, [r12]
384 strh r3, [r12]
385 add r1, r1, #1 <- [0,+,1]
386 add r3, r3, #4
387 add r2, r2, #1 <- [0,+,1]
388 cmp r1, r0
389 bne LBB1_2 @bb
390
391
392//===---------------------------------------------------------------------===//
393
Chris Lattner2cca31f2007-05-05 22:29:06 +0000394Tail call elim should be more aggressive, checking to see if the call is
395followed by an uncond branch to an exit block.
396
397; This testcase is due to tail-duplication not wanting to copy the return
398; instruction into the terminating blocks because there was other code
399; optimized out of the function after the taildup happened.
400;RUN: llvm-upgrade < %s | llvm-as | opt -tailcallelim | llvm-dis | not grep call
401
402int %t4(int %a) {
403entry:
404 %tmp.1 = and int %a, 1
405 %tmp.2 = cast int %tmp.1 to bool
406 br bool %tmp.2, label %then.0, label %else.0
407
408then.0:
409 %tmp.5 = add int %a, -1
410 %tmp.3 = call int %t4( int %tmp.5 )
411 br label %return
412
413else.0:
414 %tmp.7 = setne int %a, 0
415 br bool %tmp.7, label %then.1, label %return
416
417then.1:
418 %tmp.11 = add int %a, -2
419 %tmp.9 = call int %t4( int %tmp.11 )
420 br label %return
421
422return:
423 %result.0 = phi int [ 0, %else.0 ], [ %tmp.3, %then.0 ],
424 [ %tmp.9, %then.1 ]
425 ret int %result.0
426}