]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
x86: Add COND_VZEROUPPER that can replace vzeroupper if no `ret`
authorNoah Goldstein <goldstein.w.n@gmail.com>
Tue, 7 Jun 2022 04:11:28 +0000 (21:11 -0700)
committerNoah Goldstein <goldstein.w.n@gmail.com>
Tue, 7 Jun 2022 20:08:28 +0000 (13:08 -0700)
The RTM vzeroupper mitigation has no way of replacing inline
vzeroupper not before a return.

This can be useful when hoisting a vzeroupper to save code size
for example:

```
L(foo):
cmpl %eax, %edx
jz L(bar)
tzcntl %eax, %eax
addq %rdi, %rax
VZEROUPPER_RETURN

L(bar):
xorl %eax, %eax
VZEROUPPER_RETURN
```

Can become:

```
L(foo):
COND_VZEROUPPER
cmpl %eax, %edx
jz L(bar)
tzcntl %eax, %eax
addq %rdi, %rax
ret

L(bar):
xorl %eax, %eax
ret
```

This code does not change any existing functionality.

There is no difference in the objdump of libc.so before and after this
patch.
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
sysdeps/x86_64/multiarch/avx-rtm-vecs.h
sysdeps/x86_64/sysdep.h

index 3f531dd47fceefe9f40ac986f60b55946adb86ee..6ca9f5e6bae7ba726c7ce959f72a29d34b4542f8 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef _AVX_RTM_VECS_H
 #define _AVX_RTM_VECS_H                        1
 
+#define COND_VZEROUPPER                        COND_VZEROUPPER_XTEST
 #define ZERO_UPPER_VEC_REGISTERS_RETURN        \
        ZERO_UPPER_VEC_REGISTERS_RETURN_XTEST
 
index f14d50786d2df06e0ee4febf40966031679fd76b..4f512d5566b797fa2a690cc13646888804c69df1 100644 (file)
@@ -106,6 +106,24 @@ lose:                                                                            \
        vzeroupper;                                             \
        ret
 
+/* Can be used to replace vzeroupper that is not directly before a
+   return.  This is useful when hoisting a vzeroupper from multiple
+   return paths to decrease the total number of vzerouppers and code
+   size.  */
+#define COND_VZEROUPPER_XTEST                                                  \
+    xtest;                                                     \
+    jz 1f;                                                     \
+    vzeroall;                                                  \
+    jmp 2f;                                                    \
+1:                                                     \
+    vzeroupper;                                                        \
+2:
+
+/* In RTM define this as COND_VZEROUPPER_XTEST.  */
+#ifndef COND_VZEROUPPER
+# define COND_VZEROUPPER vzeroupper
+#endif
+
 /* Zero upper vector registers and return.  */
 #ifndef ZERO_UPPER_VEC_REGISTERS_RETURN
 # define ZERO_UPPER_VEC_REGISTERS_RETURN \