1 //===---------------------------------------------------------------------===//
2 // Random ideas for the X86 backend: SSE-specific stuff.
3 //===---------------------------------------------------------------------===//
5 - Consider eliminating the unaligned SSE load intrinsics, replacing them with
6 unaligned LLVM load instructions.
8 //===---------------------------------------------------------------------===//
10 Expand libm rounding functions inline: Significant speedups possible.
11 http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00909.html
13 //===---------------------------------------------------------------------===//
15 When compiled with unsafemath enabled, "main" should enable SSE DAZ mode and
18 //===---------------------------------------------------------------------===//
20 Think about doing i64 math in SSE regs.
22 //===---------------------------------------------------------------------===//
24 This testcase should have no SSE instructions in it, and only one load from
27 double %test3(bool %B) {
28 %C = select bool %B, double 123.412, double 523.01123123
32 Currently, the select is being lowered, which prevents the dag combiner from
33 turning 'select (load CPI1), (load CPI2)' -> 'load (select CPI1, CPI2)'
35 The pattern isel got this one right.
37 //===---------------------------------------------------------------------===//
39 SSE doesn't have [mem] op= reg instructions. If we have an SSE instruction
44 and the register allocator decides to spill X, it is cheaper to emit this as:
55 ..and this uses one fewer register (so this should be done at load folding
56 time, not at spiller time). *Note* however that this can only be done
57 if Y is dead. Here's a testcase:
59 %.str_3 = external global [15 x sbyte] ; <[15 x sbyte]*> [#uses=0]
60 implementation ; Functions:
61 declare void %printf(int, ...)
65 no_exit.i7: ; preds = %no_exit.i7, %build_tree.exit
66 %tmp.0.1.0.i9 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.34.i18, %no_exit.i7 ] ; <double> [#uses=1]
67 %tmp.0.0.0.i10 = phi double [ 0.000000e+00, %build_tree.exit ], [ %tmp.28.i16, %no_exit.i7 ] ; <double> [#uses=1]
68 %tmp.28.i16 = add double %tmp.0.0.0.i10, 0.000000e+00
69 %tmp.34.i18 = add double %tmp.0.1.0.i9, 0.000000e+00
70 br bool false, label %Compute_Tree.exit23, label %no_exit.i7
71 Compute_Tree.exit23: ; preds = %no_exit.i7
72 tail call void (int, ...)* %printf( int 0 )
73 store double %tmp.34.i18, double* null
82 *** movsd %XMM2, QWORD PTR [%ESP + 8]
83 *** addsd %XMM2, %XMM1
84 *** movsd QWORD PTR [%ESP + 8], %XMM2
85 jmp .BBmain_1 # no_exit.i7
87 This is a bugpoint reduced testcase, which is why the testcase doesn't make
88 much sense (e.g. its an infinite loop). :)
90 //===---------------------------------------------------------------------===//
92 SSE should implement 'select_cc' using 'emulated conditional moves' that use
93 pcmp/pand/pandn/por to do a selection instead of a conditional branch:
95 double %X(double %Y, double %Z, double %A, double %B) {
96 %C = setlt double %A, %B
97 %z = add double %Z, 0.0 ;; select operand is not a load
98 %D = select bool %C, double %Y, double %z
107 addsd 24(%esp), %xmm0
108 movsd 32(%esp), %xmm1
109 movsd 16(%esp), %xmm2
110 ucomisd 40(%esp), %xmm1
120 //===---------------------------------------------------------------------===//
122 It's not clear whether we should use pxor or xorps / xorpd to clear XMM
123 registers. The choice may depend on subtarget information. We should do some
124 more experiments on different x86 machines.
126 //===---------------------------------------------------------------------===//
128 Currently the x86 codegen isn't very good at mixing SSE and FPStack
131 unsigned int foo(double x) { return x; }
135 movsd 24(%esp), %xmm0
143 This will be solved when we go to a dynamic programming based isel.
145 //===---------------------------------------------------------------------===//
147 Lower memcpy / memset to a series of SSE 128 bit move instructions when it's
150 //===---------------------------------------------------------------------===//
152 Teach the coalescer to commute 2-addr instructions, allowing us to eliminate
153 the reg-reg copy in this example:
155 float foo(int *x, float *y, unsigned c) {
158 for (i = 0; i < c; i++) {
159 float xx = (float)x[i];
168 cvtsi2ss %XMM0, DWORD PTR [%EDX + 4*%ESI]
169 mulss %XMM0, DWORD PTR [%EAX + 4*%ESI]
173 **** movaps %XMM1, %XMM0
174 jb LBB_foo_3 # no_exit
176 //===---------------------------------------------------------------------===//
179 if (copysign(1.0, x) == copysign(1.0, y))
184 //===---------------------------------------------------------------------===//
186 Use movhps to update upper 64-bits of a v4sf value. Also movlps on lower half
189 //===---------------------------------------------------------------------===//
191 Better codegen for vector_shuffles like this { x, 0, 0, 0 } or { x, 0, x, 0}.
192 Perhaps use pxor / xorp* to clear a XMM register first?
194 //===---------------------------------------------------------------------===//
196 How to decide when to use the "floating point version" of logical ops? Here are
199 movaps LCPI5_5, %xmm2
202 mulps 8656(%ecx), %xmm3
203 addps 8672(%ecx), %xmm3
209 movaps LCPI5_5, %xmm1
212 mulps 8656(%ecx), %xmm3
213 addps 8672(%ecx), %xmm3
217 movaps %xmm3, 112(%esp)
220 Due to some minor source change, the later case ended up using orps and movaps
221 instead of por and movdqa. Does it matter?
223 //===---------------------------------------------------------------------===//
225 X86RegisterInfo::copyRegToReg() returns X86::MOVAPSrr for VR128. Is it possible
226 to choose between movaps, movapd, and movdqa based on types of source and
229 How about andps, andpd, and pand? Do we really care about the type of the packed
230 elements? If not, why not always use the "ps" variants which are likely to be
233 //===---------------------------------------------------------------------===//
235 External test Nurbs exposed some problems. Look for
236 __ZN15Nurbs_SSE_Cubic17TessellateSurfaceE, bb cond_next140. This is what icc
239 movaps (%edx), %xmm2 #59.21
240 movaps (%edx), %xmm5 #60.21
241 movaps (%edx), %xmm4 #61.21
242 movaps (%edx), %xmm3 #62.21
243 movl 40(%ecx), %ebp #69.49
244 shufps $0, %xmm2, %xmm5 #60.21
245 movl 100(%esp), %ebx #69.20
246 movl (%ebx), %edi #69.20
247 imull %ebp, %edi #69.49
248 addl (%eax), %edi #70.33
249 shufps $85, %xmm2, %xmm4 #61.21
250 shufps $170, %xmm2, %xmm3 #62.21
251 shufps $255, %xmm2, %xmm2 #63.21
252 lea (%ebp,%ebp,2), %ebx #69.49
254 lea -3(%edi,%ebx), %ebx #70.33
256 addl 32(%ecx), %ebx #68.37
257 testb $15, %bl #91.13
258 jne L_B1.24 # Prob 5% #91.13
260 This is the llvm code after instruction scheduling:
262 cond_next140 (0xa910740, LLVM BB @0xa90beb0):
263 %reg1078 = MOV32ri -3
264 %reg1079 = ADD32rm %reg1078, %reg1068, 1, %NOREG, 0
265 %reg1037 = MOV32rm %reg1024, 1, %NOREG, 40
266 %reg1080 = IMUL32rr %reg1079, %reg1037
267 %reg1081 = MOV32rm %reg1058, 1, %NOREG, 0
268 %reg1038 = LEA32r %reg1081, 1, %reg1080, -3
269 %reg1036 = MOV32rm %reg1024, 1, %NOREG, 32
270 %reg1082 = SHL32ri %reg1038, 4
271 %reg1039 = ADD32rr %reg1036, %reg1082
272 %reg1083 = MOVAPSrm %reg1059, 1, %NOREG, 0
273 %reg1034 = SHUFPSrr %reg1083, %reg1083, 170
274 %reg1032 = SHUFPSrr %reg1083, %reg1083, 0
275 %reg1035 = SHUFPSrr %reg1083, %reg1083, 255
276 %reg1033 = SHUFPSrr %reg1083, %reg1083, 85
277 %reg1040 = MOV32rr %reg1039
278 %reg1084 = AND32ri8 %reg1039, 15
280 JE mbb<cond_next204,0xa914d30>
282 Still ok. After register allocation:
284 cond_next140 (0xa910740, LLVM BB @0xa90beb0):
286 %EDX = MOV32rm <fi#3>, 1, %NOREG, 0
287 ADD32rm %EAX<def&use>, %EDX, 1, %NOREG, 0
288 %EDX = MOV32rm <fi#7>, 1, %NOREG, 0
289 %EDX = MOV32rm %EDX, 1, %NOREG, 40
290 IMUL32rr %EAX<def&use>, %EDX
291 %ESI = MOV32rm <fi#5>, 1, %NOREG, 0
292 %ESI = MOV32rm %ESI, 1, %NOREG, 0
293 MOV32mr <fi#4>, 1, %NOREG, 0, %ESI
294 %EAX = LEA32r %ESI, 1, %EAX, -3
295 %ESI = MOV32rm <fi#7>, 1, %NOREG, 0
296 %ESI = MOV32rm %ESI, 1, %NOREG, 32
298 SHL32ri %EDI<def&use>, 4
299 ADD32rr %EDI<def&use>, %ESI
300 %XMM0 = MOVAPSrm %ECX, 1, %NOREG, 0
301 %XMM1 = MOVAPSrr %XMM0
302 SHUFPSrr %XMM1<def&use>, %XMM1, 170
303 %XMM2 = MOVAPSrr %XMM0
304 SHUFPSrr %XMM2<def&use>, %XMM2, 0
305 %XMM3 = MOVAPSrr %XMM0
306 SHUFPSrr %XMM3<def&use>, %XMM3, 255
307 SHUFPSrr %XMM0<def&use>, %XMM0, 85
309 AND32ri8 %EBX<def&use>, 15
311 JE mbb<cond_next204,0xa914d30>
313 This looks really bad. The problem is shufps is a destructive opcode. Since it
314 appears as operand two in more than one shufps ops. It resulted in a number of
315 copies. Note icc also suffers from the same problem. Either the instruction
316 selector should select pshufd or The register allocator can made the two-address
317 to three-address transformation.
319 It also exposes some other problems. See MOV32ri -3 and the spills.
321 //===---------------------------------------------------------------------===//
323 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25500
325 LLVM is producing bad code.
327 LBB_main_4: # cond_true44
338 jne LBB_main_4 # cond_true44
340 There are two problems. 1) No need to two loop induction variables. We can
341 compare against 262144 * 16. 2) Known register coalescer issue. We should
342 be able eliminate one of the movaps:
344 addps %xmm2, %xmm1 <=== Commute!
347 movaps %xmm1, %xmm1 <=== Eliminate!
354 jne LBB_main_4 # cond_true44
356 //===---------------------------------------------------------------------===//
360 __m128 test(float a) {
361 return _mm_set_ps(0.0, 0.0, 0.0, a*a);
372 Because mulss doesn't modify the top 3 elements, the top elements of
373 xmm1 are already zero'd. We could compile this to:
379 //===---------------------------------------------------------------------===//
381 Here's a sick and twisted idea. Consider code like this:
383 __m128 test(__m128 a) {
384 float b = *(float*)&A;
386 return _mm_set_ps(0.0, 0.0, 0.0, b);
389 This might compile to this code:
391 movaps c(%esp), %xmm1
396 Now consider if the ... code caused xmm1 to get spilled. This might produce
399 movaps c(%esp), %xmm1
400 movaps %xmm1, c2(%esp)
404 movaps c2(%esp), %xmm1
408 However, since the reload is only used by these instructions, we could
409 "fold" it into the uses, producing something like this:
411 movaps c(%esp), %xmm1
412 movaps %xmm1, c2(%esp)
415 movss c2(%esp), %xmm0
418 ... saving two instructions.
420 The basic idea is that a reload from a spill slot, can, if only one 4-byte
421 chunk is used, bring in 3 zeros the the one element instead of 4 elements.
422 This can be used to simplify a variety of shuffle operations, where the
423 elements are fixed zeros.
425 //===---------------------------------------------------------------------===//
429 #include <emmintrin.h>
430 void test(__m128d *r, __m128d *A, double B) {
431 *r = _mm_loadl_pd(*A, &B);
437 movsd 24(%esp), %xmm0
449 movl 4(%esp), %edx #3.6
450 movl 8(%esp), %eax #3.6
451 movapd (%eax), %xmm0 #4.22
452 movlpd 12(%esp), %xmm0 #4.8
453 movapd %xmm0, (%edx) #4.3
456 So icc is smart enough to know that B is in memory so it doesn't load it and
457 store it back to stack.
459 This should be fixed by eliminating the llvm.x86.sse2.loadl.pd intrinsic,
460 lowering it to a load+insertelement instead. Already match the load+shuffle
461 as movlpd, so this should be easy. We already get optimal code for:
463 define void @test2(<2 x double>* %r, <2 x double>* %A, double %B) {
465 %tmp2 = load <2 x double>* %A, align 16
466 %tmp8 = insertelement <2 x double> %tmp2, double %B, i32 0
467 store <2 x double> %tmp8, <2 x double>* %r, align 16
471 //===---------------------------------------------------------------------===//
473 __m128d test1( __m128d A, __m128d B) {
474 return _mm_shuffle_pd(A, B, 0x3);
479 shufpd $3, %xmm1, %xmm0
481 Perhaps it's better to use unpckhpd instead?
483 unpckhpd %xmm1, %xmm0
485 Don't know if unpckhpd is faster. But it is shorter.
487 //===---------------------------------------------------------------------===//
489 This code generates ugly code, probably due to costs being off or something:
491 define void @test(float* %P, <4 x float>* %P2 ) {
492 %xFloat0.688 = load float* %P
493 %tmp = load <4 x float>* %P2
494 %inFloat3.713 = insertelement <4 x float> %tmp, float 0.0, i32 3
495 store <4 x float> %inFloat3.713, <4 x float>* %P2
506 shufps $50, %xmm1, %xmm2
507 shufps $132, %xmm2, %xmm0
511 Would it be better to generate:
517 pinsrw $6, %eax, %xmm0
518 pinsrw $7, %eax, %xmm0
524 //===---------------------------------------------------------------------===//
526 Some useful information in the Apple Altivec / SSE Migration Guide:
528 http://developer.apple.com/documentation/Performance/Conceptual/
529 Accelerate_sse_migration/index.html
531 e.g. SSE select using and, andnot, or. Various SSE compare translations.
533 //===---------------------------------------------------------------------===//
535 Add hooks to commute some CMPP operations.
537 //===---------------------------------------------------------------------===//
539 Apply the same transformation that merged four float into a single 128-bit load
540 to loads from constant pool.
542 //===---------------------------------------------------------------------===//
544 Floating point max / min are commutable when -enable-unsafe-fp-path is
545 specified. We should turn int_x86_sse_max_ss and X86ISD::FMIN etc. into other
546 nodes which are selected to max / min instructions that are marked commutable.
548 //===---------------------------------------------------------------------===//
550 We should compile this:
551 #include <xmmintrin.h>
557 void swizzle (const void *a, vector4_t * b, vector4_t * c) {
558 b->v = _mm_loadl_pi (b->v, (__m64 *) a);
559 c->v = _mm_loadl_pi (c->v, ((__m64 *) a) + 1);
570 movlps 8(%eax), %xmm0
584 movlps 8(%ecx), %xmm0
588 //===---------------------------------------------------------------------===//
590 These functions should produce the same code:
592 #include <emmintrin.h>
594 typedef long long __m128i __attribute__ ((__vector_size__ (16)));
596 int foo(__m128i* val) {
597 return __builtin_ia32_vec_ext_v4si(*val, 1);
599 int bar(__m128i* val) {
607 We currently produce (with -m64):
610 pshufd $1, (%rdi), %xmm0
617 //===---------------------------------------------------------------------===//
619 We should materialize vector constants like "all ones" and "signbit" with
622 cmpeqps xmm1, xmm1 ; xmm1 = all-ones
625 cmpeqps xmm1, xmm1 ; xmm1 = all-ones
626 psrlq xmm1, 31 ; xmm1 = all 100000000000...
628 instead of using a load from the constant pool. The later is important for
629 ABS/NEG/copysign etc.
631 //===---------------------------------------------------------------------===//
633 "converting 64-bit constant pool entry to 32-bit not necessarily beneficial"
634 http://llvm.org/PR1264
638 define double @foo(double %x) {
639 %y = mul double %x, 5.000000e-01
643 llc -march=x86-64 currently produces a 32-bit constant pool entry and this code:
645 cvtss2sd .LCPI1_0(%rip), %xmm1
648 instead of just using a 64-bit constant pool entry with this:
650 mulsd .LCPI1_0(%rip), %xmm0
652 This is due to the code in ExpandConstantFP in LegalizeDAG.cpp. It notices that
653 x86-64 indeed has an instruction to load a 32-bit float from memory and convert
654 it into a 64-bit float in a register, however it doesn't notice that this isn't
655 beneficial because it prevents the load from being folded into the multiply.
657 //===---------------------------------------------------------------------===//
661 #include <xmmintrin.h>
663 void x(unsigned short n) {
664 a = _mm_slli_epi32 (a, n);
667 a = _mm_slli_epi32 (a, n);
670 compile to ( -O3 -static -fomit-frame-pointer):
685 "y" looks good, but "x" does silly movzwl stuff around into a GPR. It seems
686 like movd would be sufficient in both cases as the value is already zero
687 extended in the 32-bit stack slot IIRC. For signed short, it should also be
688 save, as a really-signed value would be undefined for pslld.
691 //===---------------------------------------------------------------------===//
694 int t1(double d) { return signbit(d); }
696 This currently compiles to:
698 movsd 16(%esp), %xmm0
705 We should use movmskp{s|d} instead.
707 //===---------------------------------------------------------------------===//
709 CodeGen/X86/vec_align.ll tests whether we can turn 4 scalar loads into a single
710 (aligned) vector load. This functionality has a couple of problems.
712 1. The code to infer alignment from loads of globals is in the X86 backend,
713 not the dag combiner. This is because dagcombine2 needs to be able to see
714 through the X86ISD::Wrapper node, which DAGCombine can't really do.
715 2. The code for turning 4 x load into a single vector load is target
716 independent and should be moved to the dag combiner.
717 3. The code for turning 4 x load into a vector load can only handle a direct
718 load from a global or a direct load from the stack. It should be generalized
719 to handle any load from P, P+4, P+8, P+12, where P can be anything.
720 4. The alignment inference code cannot handle loads from globals in non-static
721 mode because it doesn't look through the extra dyld stub load. If you try
722 vec_align.ll without -relocation-model=static, you'll see what I mean.
724 //===---------------------------------------------------------------------===//
726 We should lower store(fneg(load p), q) into an integer load+xor+store, which
727 eliminates a constant pool load. For example, consider:
729 define i64 @ccosf(float %z.0, float %z.1) nounwind readonly {
731 %tmp6 = sub float -0.000000e+00, %z.1 ; <float> [#uses=1]
732 %tmp20 = tail call i64 @ccoshf( float %tmp6, float %z.0 ) nounwind readonly ; <i64> [#uses=1]
736 This currently compiles to:
738 LCPI1_0: # <4 x float>
739 .long 2147483648 # float -0
740 .long 2147483648 # float -0
741 .long 2147483648 # float -0
742 .long 2147483648 # float -0
745 movss 16(%esp), %xmm0
747 movss 20(%esp), %xmm0
754 Note the load into xmm0, then xor (to negate), then store. In PIC mode,
755 this code computes the pic base and does two loads to do the constant pool
756 load, so the improvement is much bigger.
758 The tricky part about this xform is that the argument load/store isn't exposed
759 until post-legalize, and at that point, the fneg has been custom expanded into
760 an X86 fxor. This means that we need to handle this case in the x86 backend
761 instead of in target independent code.
763 //===---------------------------------------------------------------------===//
765 Non-SSE4 insert into 16 x i8 is atrociously bad.
767 //===---------------------------------------------------------------------===//
769 <2 x i64> extract is substantially worse than <2 x f64>, even if the destination
772 //===---------------------------------------------------------------------===//
774 SSE4 extract-to-mem ops aren't being pattern matched because of the AssertZext
775 sitting between the truncate and the extract.
777 //===---------------------------------------------------------------------===//
779 INSERTPS can match any insert (extract, imm1), imm2 for 4 x float, and insert
780 any number of 0.0 simultaneously. Currently we only use it for simple
783 See comments in LowerINSERT_VECTOR_ELT_SSE4.
785 //===---------------------------------------------------------------------===//
787 On a random note, SSE2 should declare insert/extract of 2 x f64 as legal, not
788 Custom. All combinations of insert/extract reg-reg, reg-mem, and mem-reg are
789 legal, it'll just take a few extra patterns written in the .td file.
791 Note: this is not a code quality issue; the custom lowered code happens to be
792 right, but we shouldn't have to custom lower anything. This is probably related
793 to <2 x i64> ops being so bad.