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 on x86-32.
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 should implement 'select_cc' using 'emulated conditional moves' that use
40 pcmp/pand/pandn/por to do a selection instead of a conditional branch:
42 double %X(double %Y, double %Z, double %A, double %B) {
43 %C = setlt double %A, %B
44 %z = fadd double %Z, 0.0 ;; select operand is not a load
45 %D = select bool %C, double %Y, double %z
57 ucomisd 40(%esp), %xmm1
67 //===---------------------------------------------------------------------===//
69 It's not clear whether we should use pxor or xorps / xorpd to clear XMM
70 registers. The choice may depend on subtarget information. We should do some
71 more experiments on different x86 machines.
73 //===---------------------------------------------------------------------===//
75 Lower memcpy / memset to a series of SSE 128 bit move instructions when it's
78 //===---------------------------------------------------------------------===//
81 if (copysign(1.0, x) == copysign(1.0, y))
86 //===---------------------------------------------------------------------===//
88 Use movhps to update upper 64-bits of a v4sf value. Also movlps on lower half
91 //===---------------------------------------------------------------------===//
93 Better codegen for vector_shuffles like this { x, 0, 0, 0 } or { x, 0, x, 0}.
94 Perhaps use pxor / xorp* to clear a XMM register first?
96 //===---------------------------------------------------------------------===//
98 How to decide when to use the "floating point version" of logical ops? Here are
101 movaps LCPI5_5, %xmm2
104 mulps 8656(%ecx), %xmm3
105 addps 8672(%ecx), %xmm3
111 movaps LCPI5_5, %xmm1
114 mulps 8656(%ecx), %xmm3
115 addps 8672(%ecx), %xmm3
119 movaps %xmm3, 112(%esp)
122 Due to some minor source change, the later case ended up using orps and movaps
123 instead of por and movdqa. Does it matter?
125 //===---------------------------------------------------------------------===//
127 X86RegisterInfo::copyRegToReg() returns X86::MOVAPSrr for VR128. Is it possible
128 to choose between movaps, movapd, and movdqa based on types of source and
131 How about andps, andpd, and pand? Do we really care about the type of the packed
132 elements? If not, why not always use the "ps" variants which are likely to be
135 //===---------------------------------------------------------------------===//
137 External test Nurbs exposed some problems. Look for
138 __ZN15Nurbs_SSE_Cubic17TessellateSurfaceE, bb cond_next140. This is what icc
141 movaps (%edx), %xmm2 #59.21
142 movaps (%edx), %xmm5 #60.21
143 movaps (%edx), %xmm4 #61.21
144 movaps (%edx), %xmm3 #62.21
145 movl 40(%ecx), %ebp #69.49
146 shufps $0, %xmm2, %xmm5 #60.21
147 movl 100(%esp), %ebx #69.20
148 movl (%ebx), %edi #69.20
149 imull %ebp, %edi #69.49
150 addl (%eax), %edi #70.33
151 shufps $85, %xmm2, %xmm4 #61.21
152 shufps $170, %xmm2, %xmm3 #62.21
153 shufps $255, %xmm2, %xmm2 #63.21
154 lea (%ebp,%ebp,2), %ebx #69.49
156 lea -3(%edi,%ebx), %ebx #70.33
158 addl 32(%ecx), %ebx #68.37
159 testb $15, %bl #91.13
160 jne L_B1.24 # Prob 5% #91.13
162 This is the llvm code after instruction scheduling:
164 cond_next140 (0xa910740, LLVM BB @0xa90beb0):
165 %reg1078 = MOV32ri -3
166 %reg1079 = ADD32rm %reg1078, %reg1068, 1, %NOREG, 0
167 %reg1037 = MOV32rm %reg1024, 1, %NOREG, 40
168 %reg1080 = IMUL32rr %reg1079, %reg1037
169 %reg1081 = MOV32rm %reg1058, 1, %NOREG, 0
170 %reg1038 = LEA32r %reg1081, 1, %reg1080, -3
171 %reg1036 = MOV32rm %reg1024, 1, %NOREG, 32
172 %reg1082 = SHL32ri %reg1038, 4
173 %reg1039 = ADD32rr %reg1036, %reg1082
174 %reg1083 = MOVAPSrm %reg1059, 1, %NOREG, 0
175 %reg1034 = SHUFPSrr %reg1083, %reg1083, 170
176 %reg1032 = SHUFPSrr %reg1083, %reg1083, 0
177 %reg1035 = SHUFPSrr %reg1083, %reg1083, 255
178 %reg1033 = SHUFPSrr %reg1083, %reg1083, 85
179 %reg1040 = MOV32rr %reg1039
180 %reg1084 = AND32ri8 %reg1039, 15
182 JE mbb<cond_next204,0xa914d30>
184 Still ok. After register allocation:
186 cond_next140 (0xa910740, LLVM BB @0xa90beb0):
188 %EDX = MOV32rm <fi#3>, 1, %NOREG, 0
189 ADD32rm %EAX<def&use>, %EDX, 1, %NOREG, 0
190 %EDX = MOV32rm <fi#7>, 1, %NOREG, 0
191 %EDX = MOV32rm %EDX, 1, %NOREG, 40
192 IMUL32rr %EAX<def&use>, %EDX
193 %ESI = MOV32rm <fi#5>, 1, %NOREG, 0
194 %ESI = MOV32rm %ESI, 1, %NOREG, 0
195 MOV32mr <fi#4>, 1, %NOREG, 0, %ESI
196 %EAX = LEA32r %ESI, 1, %EAX, -3
197 %ESI = MOV32rm <fi#7>, 1, %NOREG, 0
198 %ESI = MOV32rm %ESI, 1, %NOREG, 32
200 SHL32ri %EDI<def&use>, 4
201 ADD32rr %EDI<def&use>, %ESI
202 %XMM0 = MOVAPSrm %ECX, 1, %NOREG, 0
203 %XMM1 = MOVAPSrr %XMM0
204 SHUFPSrr %XMM1<def&use>, %XMM1, 170
205 %XMM2 = MOVAPSrr %XMM0
206 SHUFPSrr %XMM2<def&use>, %XMM2, 0
207 %XMM3 = MOVAPSrr %XMM0
208 SHUFPSrr %XMM3<def&use>, %XMM3, 255
209 SHUFPSrr %XMM0<def&use>, %XMM0, 85
211 AND32ri8 %EBX<def&use>, 15
213 JE mbb<cond_next204,0xa914d30>
215 This looks really bad. The problem is shufps is a destructive opcode. Since it
216 appears as operand two in more than one shufps ops. It resulted in a number of
217 copies. Note icc also suffers from the same problem. Either the instruction
218 selector should select pshufd or The register allocator can made the two-address
219 to three-address transformation.
221 It also exposes some other problems. See MOV32ri -3 and the spills.
223 //===---------------------------------------------------------------------===//
225 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25500
227 LLVM is producing bad code.
229 LBB_main_4: # cond_true44
240 jne LBB_main_4 # cond_true44
242 There are two problems. 1) No need to two loop induction variables. We can
243 compare against 262144 * 16. 2) Known register coalescer issue. We should
244 be able eliminate one of the movaps:
246 addps %xmm2, %xmm1 <=== Commute!
249 movaps %xmm1, %xmm1 <=== Eliminate!
256 jne LBB_main_4 # cond_true44
258 //===---------------------------------------------------------------------===//
262 __m128 test(float a) {
263 return _mm_set_ps(0.0, 0.0, 0.0, a*a);
274 Because mulss doesn't modify the top 3 elements, the top elements of
275 xmm1 are already zero'd. We could compile this to:
281 //===---------------------------------------------------------------------===//
283 Here's a sick and twisted idea. Consider code like this:
285 __m128 test(__m128 a) {
286 float b = *(float*)&A;
288 return _mm_set_ps(0.0, 0.0, 0.0, b);
291 This might compile to this code:
293 movaps c(%esp), %xmm1
298 Now consider if the ... code caused xmm1 to get spilled. This might produce
301 movaps c(%esp), %xmm1
302 movaps %xmm1, c2(%esp)
306 movaps c2(%esp), %xmm1
310 However, since the reload is only used by these instructions, we could
311 "fold" it into the uses, producing something like this:
313 movaps c(%esp), %xmm1
314 movaps %xmm1, c2(%esp)
317 movss c2(%esp), %xmm0
320 ... saving two instructions.
322 The basic idea is that a reload from a spill slot, can, if only one 4-byte
323 chunk is used, bring in 3 zeros the one element instead of 4 elements.
324 This can be used to simplify a variety of shuffle operations, where the
325 elements are fixed zeros.
327 //===---------------------------------------------------------------------===//
329 __m128d test1( __m128d A, __m128d B) {
330 return _mm_shuffle_pd(A, B, 0x3);
335 shufpd $3, %xmm1, %xmm0
337 Perhaps it's better to use unpckhpd instead?
339 unpckhpd %xmm1, %xmm0
341 Don't know if unpckhpd is faster. But it is shorter.
343 //===---------------------------------------------------------------------===//
345 This code generates ugly code, probably due to costs being off or something:
347 define void @test(float* %P, <4 x float>* %P2 ) {
348 %xFloat0.688 = load float* %P
349 %tmp = load <4 x float>* %P2
350 %inFloat3.713 = insertelement <4 x float> %tmp, float 0.0, i32 3
351 store <4 x float> %inFloat3.713, <4 x float>* %P2
362 shufps $50, %xmm1, %xmm2
363 shufps $132, %xmm2, %xmm0
367 Would it be better to generate:
373 pinsrw $6, %eax, %xmm0
374 pinsrw $7, %eax, %xmm0
380 //===---------------------------------------------------------------------===//
382 Some useful information in the Apple Altivec / SSE Migration Guide:
384 http://developer.apple.com/documentation/Performance/Conceptual/
385 Accelerate_sse_migration/index.html
387 e.g. SSE select using and, andnot, or. Various SSE compare translations.
389 //===---------------------------------------------------------------------===//
391 Add hooks to commute some CMPP operations.
393 //===---------------------------------------------------------------------===//
395 Apply the same transformation that merged four float into a single 128-bit load
396 to loads from constant pool.
398 //===---------------------------------------------------------------------===//
400 Floating point max / min are commutable when -enable-unsafe-fp-path is
401 specified. We should turn int_x86_sse_max_ss and X86ISD::FMIN etc. into other
402 nodes which are selected to max / min instructions that are marked commutable.
404 //===---------------------------------------------------------------------===//
406 We should materialize vector constants like "all ones" and "signbit" with
409 cmpeqps xmm1, xmm1 ; xmm1 = all-ones
412 cmpeqps xmm1, xmm1 ; xmm1 = all-ones
413 psrlq xmm1, 31 ; xmm1 = all 100000000000...
415 instead of using a load from the constant pool. The later is important for
416 ABS/NEG/copysign etc.
418 //===---------------------------------------------------------------------===//
422 #include <xmmintrin.h>
424 void x(unsigned short n) {
425 a = _mm_slli_epi32 (a, n);
428 a = _mm_slli_epi32 (a, n);
431 compile to ( -O3 -static -fomit-frame-pointer):
446 "y" looks good, but "x" does silly movzwl stuff around into a GPR. It seems
447 like movd would be sufficient in both cases as the value is already zero
448 extended in the 32-bit stack slot IIRC. For signed short, it should also be
449 save, as a really-signed value would be undefined for pslld.
452 //===---------------------------------------------------------------------===//
455 int t1(double d) { return signbit(d); }
457 This currently compiles to:
459 movsd 16(%esp), %xmm0
466 We should use movmskp{s|d} instead.
468 //===---------------------------------------------------------------------===//
470 CodeGen/X86/vec_align.ll tests whether we can turn 4 scalar loads into a single
471 (aligned) vector load. This functionality has a couple of problems.
473 1. The code to infer alignment from loads of globals is in the X86 backend,
474 not the dag combiner. This is because dagcombine2 needs to be able to see
475 through the X86ISD::Wrapper node, which DAGCombine can't really do.
476 2. The code for turning 4 x load into a single vector load is target
477 independent and should be moved to the dag combiner.
478 3. The code for turning 4 x load into a vector load can only handle a direct
479 load from a global or a direct load from the stack. It should be generalized
480 to handle any load from P, P+4, P+8, P+12, where P can be anything.
481 4. The alignment inference code cannot handle loads from globals in non-static
482 mode because it doesn't look through the extra dyld stub load. If you try
483 vec_align.ll without -relocation-model=static, you'll see what I mean.
485 //===---------------------------------------------------------------------===//
487 We should lower store(fneg(load p), q) into an integer load+xor+store, which
488 eliminates a constant pool load. For example, consider:
490 define i64 @ccosf(float %z.0, float %z.1) nounwind readonly {
492 %tmp6 = fsub float -0.000000e+00, %z.1 ; <float> [#uses=1]
493 %tmp20 = tail call i64 @ccoshf( float %tmp6, float %z.0 ) nounwind readonly
497 This currently compiles to:
499 LCPI1_0: # <4 x float>
500 .long 2147483648 # float -0
501 .long 2147483648 # float -0
502 .long 2147483648 # float -0
503 .long 2147483648 # float -0
506 movss 16(%esp), %xmm0
508 movss 20(%esp), %xmm0
515 Note the load into xmm0, then xor (to negate), then store. In PIC mode,
516 this code computes the pic base and does two loads to do the constant pool
517 load, so the improvement is much bigger.
519 The tricky part about this xform is that the argument load/store isn't exposed
520 until post-legalize, and at that point, the fneg has been custom expanded into
521 an X86 fxor. This means that we need to handle this case in the x86 backend
522 instead of in target independent code.
524 //===---------------------------------------------------------------------===//
526 Non-SSE4 insert into 16 x i8 is atrociously bad.
528 //===---------------------------------------------------------------------===//
530 <2 x i64> extract is substantially worse than <2 x f64>, even if the destination
533 //===---------------------------------------------------------------------===//
535 SSE4 extract-to-mem ops aren't being pattern matched because of the AssertZext
536 sitting between the truncate and the extract.
538 //===---------------------------------------------------------------------===//
540 INSERTPS can match any insert (extract, imm1), imm2 for 4 x float, and insert
541 any number of 0.0 simultaneously. Currently we only use it for simple
544 See comments in LowerINSERT_VECTOR_ELT_SSE4.
546 //===---------------------------------------------------------------------===//
548 On a random note, SSE2 should declare insert/extract of 2 x f64 as legal, not
549 Custom. All combinations of insert/extract reg-reg, reg-mem, and mem-reg are
550 legal, it'll just take a few extra patterns written in the .td file.
552 Note: this is not a code quality issue; the custom lowered code happens to be
553 right, but we shouldn't have to custom lower anything. This is probably related
554 to <2 x i64> ops being so bad.
556 //===---------------------------------------------------------------------===//
558 'select' on vectors and scalars could be a whole lot better. We currently
559 lower them to conditional branches. On x86-64 for example, we compile this:
561 double test(double a, double b, double c, double d) { return a<b ? c : d; }
583 For unpredictable branches, the later is much more efficient. This should
584 just be a matter of having scalar sse map to SELECT_CC and custom expanding
587 //===---------------------------------------------------------------------===//
589 LLVM currently generates stack realignment code, when it is not necessary
590 needed. The problem is that we need to know about stack alignment too early,
593 At that point we don't know, whether there will be vector spill, or not.
594 Stack realignment logic is overly conservative here, but otherwise we can
595 produce unaligned loads/stores.
597 Fixing this will require some huge RA changes.
600 #include <emmintrin.h>
602 typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
604 static const vSInt16 a = {- 22725, - 12873, - 22725, - 12873, - 22725, - 12873,
607 vSInt16 madd(vSInt16 b)
609 return _mm_madd_epi16(a, b);
612 Generated code (x86-32, linux):
617 movaps .LCPI1_0, %xmm1
623 //===---------------------------------------------------------------------===//
626 #include <emmintrin.h>
627 __m128 foo2 (float x) {
628 return _mm_set_ps (0, 0, x, 0);
631 In x86-32 mode, we generate this spiffy code:
635 pshufd $81, %xmm0, %xmm0
638 in x86-64 mode, we generate this code, which could be better:
643 pshufd $81, %xmm1, %xmm0
646 In sse4 mode, we could use insertps to make both better.
648 Here's another testcase that could use insertps [mem]:
650 #include <xmmintrin.h>
652 __m128 foo1 (float x1, float x4) {
653 return _mm_set_ps (x2, x1, x3, x4);
656 gcc mainline compiles it to:
659 insertps $0x10, x2(%rip), %xmm0
660 insertps $0x10, x3(%rip), %xmm1
666 //===---------------------------------------------------------------------===//
668 We compile vector multiply-by-constant into poor code:
670 define <4 x i32> @f(<4 x i32> %i) nounwind {
671 %A = mul <4 x i32> %i, < i32 10, i32 10, i32 10, i32 10 >
675 On targets without SSE4.1, this compiles into:
677 LCPI1_0: ## <4 x i32>
686 pshufd $3, %xmm0, %xmm1
688 imull LCPI1_0+12, %eax
690 pshufd $1, %xmm0, %xmm2
692 imull LCPI1_0+4, %eax
694 punpckldq %xmm1, %xmm2
700 imull LCPI1_0+8, %eax
702 punpckldq %xmm0, %xmm1
704 punpckldq %xmm2, %xmm0
707 It would be better to synthesize integer vector multiplication by constants
708 using shifts and adds, pslld and paddd here. And even on targets with SSE4.1,
709 simple cases such as multiplication by powers of two would be better as
710 vector shifts than as multiplications.
712 //===---------------------------------------------------------------------===//
719 return _mm_set_epi8 (1, 0, 0, 0, 0, 0, 0, 0, 0, x, 0, 1, 0, 0, 0, 0);
725 pinsrw $2, %eax, %xmm0
727 pinsrw $3, %eax, %xmm0
729 pinsrw $7, %eax, %xmm0
735 movzbl 16(%esp), %eax
737 pinsrw $3, %eax, %xmm0
752 With SSE4, it should be
753 movdqa .LC0(%rip), %xmm0
754 pinsrb $6, %edi, %xmm0
756 //===---------------------------------------------------------------------===//
758 We should transform a shuffle of two vectors of constants into a single vector
759 of constants. Also, insertelement of a constant into a vector of constants
760 should also result in a vector of constants. e.g. 2008-06-25-VecISelBug.ll.
762 We compiled it to something horrible:
766 .long 1065353216 ## float 1
770 LCPI1_0: ## <4 x float>
772 .long 1065353216 ## float 1
774 .long 1065353216 ## float 1
780 movhps LCPI1_0, %xmm0
783 shufps $2, %xmm1, %xmm2
784 shufps $132, %xmm2, %xmm0
787 //===---------------------------------------------------------------------===//
792 float foo(unsigned char x) {
796 compiles to (x86-32):
798 define float @foo(i8 zeroext %x) nounwind {
799 %tmp12 = uitofp i8 %x to float ; <float> [#uses=1]
814 We should be able to use:
815 cvtsi2ss 8($esp), %xmm0
816 since we know the stack slot is already zext'd.
818 //===---------------------------------------------------------------------===//
820 Consider using movlps instead of movsd to implement (scalar_to_vector (loadf64))
821 when code size is critical. movlps is slower than movsd on core2 but it's one
824 //===---------------------------------------------------------------------===//
826 We should use a dynamic programming based approach to tell when using FPStack
827 operations is cheaper than SSE. SciMark montecarlo contains code like this
830 double MonteCarlo_num_flops(int Num_samples) {
831 return ((double) Num_samples)* 4.0;
834 In fpstack mode, this compiles into:
837 .long 1082130432 ## float 4.000000e+00
838 _MonteCarlo_num_flops:
847 in SSE mode, it compiles into significantly slower code:
849 _MonteCarlo_num_flops:
851 cvtsi2sd 16(%esp), %xmm0
858 There are also other cases in scimark where using fpstack is better, it is
859 cheaper to do fld1 than load from a constant pool for example, so
860 "load, add 1.0, store" is better done in the fp stack, etc.
862 //===---------------------------------------------------------------------===//
864 The X86 backend should be able to if-convert SSE comparisons like "ucomisd" to
865 "cmpsd". For example, this code:
867 double d1(double x) { return x == x ? x : x + x; }
879 Also, the 'ret's should be shared. This is PR6032.
881 //===---------------------------------------------------------------------===//
883 These should compile into the same code (PR6214): Perhaps instcombine should
884 canonicalize the former into the later?
886 define float @foo(float %x) nounwind {
887 %t = bitcast float %x to i32
888 %s = and i32 %t, 2147483647
889 %d = bitcast i32 %s to float
893 declare float @fabsf(float %n)
894 define float @bar(float %x) nounwind {
895 %d = call float @fabsf(float %x)
899 //===---------------------------------------------------------------------===//
901 This IR (from PR6194):
903 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
904 target triple = "x86_64-apple-darwin10.0.0"
906 %0 = type { double, double }
907 %struct.float3 = type { float, float, float }
909 define void @test(%0, %struct.float3* nocapture %res) nounwind noinline ssp {
911 %tmp18 = extractvalue %0 %0, 0 ; <double> [#uses=1]
912 %tmp19 = bitcast double %tmp18 to i64 ; <i64> [#uses=1]
913 %tmp20 = zext i64 %tmp19 to i128 ; <i128> [#uses=1]
914 %tmp10 = lshr i128 %tmp20, 32 ; <i128> [#uses=1]
915 %tmp11 = trunc i128 %tmp10 to i32 ; <i32> [#uses=1]
916 %tmp12 = bitcast i32 %tmp11 to float ; <float> [#uses=1]
917 %tmp5 = getelementptr inbounds %struct.float3* %res, i64 0, i32 1 ; <float*> [#uses=1]
918 store float %tmp12, float* %tmp5
930 This would be better kept in the SSE unit by treating XMM0 as a 4xfloat and
931 doing a shuffle from v[1] to v[0] then a float store.
933 //===---------------------------------------------------------------------===//