[PATCH] D149920: ms inline asm: recognize "jmp" as TargetLowering::C_Address

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 4 18:18:17 PDT 2023


MaskRay created this revision.
MaskRay added reviewers: alvinhochun, epastor, hans, pengfei, RKSimon, xiangzhangllvm.
Herald added a subscriber: hiraditya.
Herald added a project: All.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

`__asm call k` lowered to `call dword ptr ${0:P}` and is fixed by D149695 <https://reviews.llvm.org/D149695> to
lower to `call ${0:P}` instead. Both forms are recognized as having a
ConstraintType of TargetLowering::C_Memory and will switch to
TargetLowering::C_Address (by D133914 <https://reviews.llvm.org/D133914>), so that the final assembly is a direct
call instead of an indirect call.

Extend D133914 <https://reviews.llvm.org/D133914> code to cover the JMP instruction, so that
`jmp ${0:P}` will correctly lower to `jmp _k` instead of `jmp dword ptr [_k]`.

After this patch and D149579 <https://reviews.llvm.org/D149579>,

  int (*kptr)(int);
  ...
  __asm call kptr
  __asm jmp kptr

will correctly lower to indirect calls (the "Broken case" in
clang/test/CodeGen/ms-inline-asm-functions.c).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149920

Files:
  llvm/lib/Target/X86/X86ISelLowering.cpp
  llvm/test/CodeGen/X86/inline-asm-function-call-pic.ll


Index: llvm/test/CodeGen/X86/inline-asm-function-call-pic.ll
===================================================================
--- llvm/test/CodeGen/X86/inline-asm-function-call-pic.ll
+++ llvm/test/CodeGen/X86/inline-asm-function-call-pic.ll
@@ -15,6 +15,7 @@
 ;   __asm {
 ;           call static_func
 ;           call extern_func
+;           jmp extern_func
 ;           shr eax, 0
 ;           shr ebx, 0
 ;           shr ecx, 0
@@ -40,6 +41,7 @@
 ; CHECK-EMPTY:
 ; CHECK-NEXT:    calll static_func
 ; CHECK-NEXT:    calll extern_func at PLT
+; CHECK-NEXT:    jmp extern_func at PLT
 ; CHECK-NEXT:    shrl $0, %eax
 ; CHECK-NEXT:    shrl $0, %ebx
 ; CHECK-NEXT:    shrl $0, %ecx
@@ -52,7 +54,7 @@
 ; CHECK-NEXT:    #NO_APP
 entry:
   %call = tail call i32 @static_func()
-  tail call void asm sideeffect inteldialect "call dword ptr ${0:P}\0A\09call dword ptr ${1:P}\0A\09shr eax, $$0\0A\09shr ebx, $$0\0A\09shr ecx, $$0\0A\09shr edx, $$0\0A\09shr edi, $$0\0A\09shr esi, $$0\0A\09shr ebp, $$0\0A\09shr esp, $$0", "*m,*m,~{eax},~{ebp},~{ebx},~{ecx},~{edi},~{edx},~{flags},~{esi},~{esp},~{dirflag},~{fpsr},~{flags}"(ptr nonnull elementtype(i32 (...)) @static_func, ptr nonnull elementtype(i32 (...)) @extern_func) #0
+  tail call void asm sideeffect inteldialect "call ${0:P}\0A\09call ${1:P}\0A\09jmp ${1:P}\0A\09shr eax, $$0\0A\09shr ebx, $$0\0A\09shr ecx, $$0\0A\09shr edx, $$0\0A\09shr edi, $$0\0A\09shr esi, $$0\0A\09shr ebp, $$0\0A\09shr esp, $$0", "*m,*m,~{eax},~{ebp},~{ebx},~{ecx},~{edi},~{edx},~{flags},~{esi},~{esp},~{dirflag},~{fpsr},~{flags}"(ptr nonnull elementtype(i32 (...)) @static_func, ptr nonnull elementtype(i32 (...)) @extern_func) #0
   ret void
 }
 
Index: llvm/lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -33956,11 +33956,7 @@
 bool X86TargetLowering::isInlineAsmTargetBranch(
     const SmallVectorImpl<StringRef> &AsmStrs, unsigned OpNo) const {
   StringRef InstrStr = getInstrStrFromOpNo(AsmStrs, OpNo);
-
-  if (InstrStr.contains("call"))
-    return true;
-
-  return false;
+  return InstrStr.startswith("call") || InstrStr.startswith("jmp");
 }
 
 /// Provide custom lowering hooks for some operations.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149920.519712.patch
Type: text/x-patch
Size: 2286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230505/bfeffbb0/attachment.bin>


More information about the llvm-commits mailing list