[X86] Do not emit JCC to __x86_indirect_thunk
authorJoao Moreira <joao.moreira@intel.com>
Tue, 4 Oct 2022 21:02:18 +0000 (14:02 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Thu, 6 Oct 2022 18:09:24 +0000 (11:09 -0700)
commiteac3e5c3fb7c121158fc4ae5117e6acb7fb5220e
treeb8330ec1ee3b0aadae0edc03d5d985d814fdbd86
parentdc2b8fb965d535cf3c7149b0e66db28b330c4c90
[X86] Do not emit JCC to __x86_indirect_thunk

Clang may optimize conditional tailcall blocks with the following layout:

cmp <condition>
je  tailcall_target
ret

When retpoline is in place, indirect calls are converted into direct calls to a retpoline thunk. When these indirect calls are tail calls, they may be subject to the above described optimization (there is no indirect JCC, but since now the jump is direct it can be made conditional). The above layout is non-ideal for the Linux kernel scenario because the branches into thunks may be patched back into indirect branches during runtime depending on the underlying CPU features, what would not be feasible if the binary is emitted with the optimized layout above.

Thus, prevent clang from emitting this it if CodeModel is Kernel.

Feature request from the respective kernel mailing list: https://lore.kernel.org/llvm/Yv3uI%2FMoJVctmBCh@worktop.programming.kicks-ass.net/

Reviewed By: nickdesaulniers, pengfei

Differential Revision: https://reviews.llvm.org/D134915
llvm/lib/Target/X86/X86InstrInfo.cpp
llvm/test/CodeGen/X86/jcc-indirect-thunk-kernel.ll [new file with mode: 0644]