llc inserts vzeroupper before a call and spoils avx registers, which lives across the call
| Bugzilla Link | 17631 |
| Resolution | FIXED |
| Resolved on | Oct 23, 2013 13:35 |
| Version | trunk |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
| CC | @dbabokin,@darkbuck |
Extended Description
This bug reproduces on 32 bit both Linux and Windows. Though it seems to be more frequently appears in real life on Windows. Reproducer:
; ModuleID = 'a.bc' target datalayout = "e-p:32:32:32-S32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f128:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32" target triple = "i386-pc-win32"
%struct_type = type { [64 x <8 x float>], <8 x float> }
; Function Attrs: nounwind readnone declare i32 @llvm.x86.avx.movmsk.ps.256(<8 x float>) #0
; Function Attrs: nounwind define i32 @equal(<8 x i32> %A) #1 { allocas: %first_alloc = alloca [64 x <8 x i32>] %second_alloc = alloca %struct_type
%A1 = bitcast <8 x i32> %A to <8 x float> %A2 = call i32 @llvm.x86.avx.movmsk.ps.256(<8 x float> %A1) ret i32 %A2 }
; Function Attrs: nounwind define i32 @before_equal() #1 { allocas: %A = alloca <8 x i32> store <8 x i32> <i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1, i32 -1>, <8 x i32>* %A %A1 = load <8 x i32>* %A %A2 = call i32 @equal(<8 x i32> %A1) ret i32 %A2 }
attributes #0 = { nounwind readnone } attributes #1 = { nounwind "target-features"="+avx,+popcnt,+cmov" }
Compilation command:
llc a.ll -o a.s -mcpu=corei7-avx cat a.s
_equal:
BB#0: # %allocas
pushl %ebp
movl %esp, %ebp
andl $-32, %esp
movl $4160, %eax # imm = 0x1040
vzeroupper
calll __chkstk
vmovmskps %ymm0, %eax
movl %ebp, %esp
popl %ebp
vzeroupper
ret
.def _before_equal;
.scl 2;
.type 32;
.endef
.globl _before_equal
.align 16, 0x90
_before_equal: # @before_equal
BB#0: # %allocas
pushl %ebp
movl %esp, %ebp
andl $-32, %esp
subl $64, %esp
vpcmpeqd %xmm0, %xmm0, %xmm0
vinsertf128 $1, %xmm0, %ymm0, %ymm0
vmovaps %ymm0, (%esp)
calll _equal
movl %ebp, %esp
popl %ebp
vzeroupper
ret
ymm0 is initialized by all 1s (vpcmpeqd + vinsertf128). Inside _equal function it's assumed to be live across __chkstk call, but vzeroupper zeros upper half of the register. Right after the call ymm0 is used (vmovmskps), which yields incorrect value in eax (0xf instead of 0xff).
It's turned out to be an issue specific to Windows where a special helper needs inserting in function prologue. However, that helper is (and should be) simple, has minimal registers clobbered and won't use/def XMM/YMM/ZMM registers. The simple solution is to skip them during 'vzeroupper' insertion.
Fixed in r193261.