According to https://learn.microsoft.com/ru-ru/cpp/build/x64-software-conventions?view=vs-2017 - xmm6:xmm15 are non-volatile. But my program doesn't crash if I don't preserve xmm6, xmm7. I don't call OS from assembly. Do I need to preserve registers in this case ? I am running under Windows 7.
Breaking the ABI does not guarantee a fault, just like UB in C might happen to work. e.g. perhaps a caller saves/restores (on entry/exit) the XMM reg you destroy, but didn't care about their values across the call to your function. e.g. maybe it wanted to keep an FP value in a register across a printf
call, not across the call to your function.
Or perhaps nothing uses them, and main
and the CRT startup code don't care.
The way ABI guarantees work is that if you follow them, you guarantee no problems, not the other way around.
IDK if there's a calling-convention "checker" wrapper function that verifies that all call-preserved regs are correctly preserved, and that you didn't step on any stack space outside the shadow space and (if any) your stack args. Probably someone's written something like that. e.g. Writing a thunk to verify SysV ABI compliance
It would be great to avoid saving these registers because it affects performance (not much but still).
If you're compiling your C with GCC or clang, you could declare your asm function's prototype as using the x86-64 System V ABI where all of xmm0..15 are call-clobbered (and arg-passing uses different registers), using a GCC function attribute
__attribute__((sysv_abi))
extern "C" int myfunc(void);
Then the caller will have to save/restore all of xmm6..15 because it has to assume the callee destroyed them all.
So do this for a function high enough up the call tree that this overhead is amortized over many function calls.
(Or better, use intrinsics so your use of XMM regs can inline and optimize away the call/ret overhead as well as the save/restore XMM reg overhead. If save/restore or call overhead matters, the solution is more inlining so functions aren't so small.)
See Work around windows calling convention preserving xmm registers? for that. Beware that ICC is buggy and fails to save/restore XMM6..15 around a call to a System V ABI function, and GCC doesn't properly support AVX on Windows (stack alignment problems).