Skip to content

Commit be08e45

Browse files
committed
[X86][MS-InlineAsm] Add constraint *m for memory access w/ global var
Constraint `*m` should be used when the address of a variable is passed as a value. And the constraint is missing for MS inline assembly when sth is written to the address of the variable. The missing would cause FE delete the definition of the static varible, and then result in "undefined reference to xxx" issue. Reviewed By: xiangzhangllvm Differential Revision: https://reviews.llvm.org/D113096
1 parent 0bce3e3 commit be08e45

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

clang/test/CodeGen/X86/ms_fmul.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ void __attribute__ ((naked)) foo(void)
1818
}}
1919

2020
// CHECK-LABEL: foo
21-
// CHECK: call void asm sideeffect inteldialect "fmul qword ptr static_const_table[edx + $$240]\0A\09ret"
21+
// CHECK: call void asm sideeffect inteldialect "fmul qword ptr $0[edx + $$240]\0A\09ret"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: x86-registered-target
2+
// Check the constraint "*m" of operand arr and the definition of arr is not removed by FE
3+
// RUN: %clang_cc1 %s -fasm-blocks -triple i386-apple-darwin10 -emit-llvm -o - | FileCheck %s
4+
5+
static int arr[10];
6+
void t1() {
7+
// CHECK: @arr = internal global [10 x i32]
8+
// CHECK: call void asm sideeffect inteldialect "mov dword ptr $0[edx * $$4],edx", "=*m,{{.*}}([10 x i32]* @arr)
9+
__asm mov dword ptr arr[edx*4],edx
10+
}

clang/test/CodeGen/ms-inline-asm-variables.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
int gVar;
55
void t1() {
6-
// CHECK: add eax, dword ptr gVar[eax]
6+
// CHECK: add eax, dword ptr ${{[0-9]}}[eax]
77
__asm add eax, dword ptr gVar[eax]
8-
// CHECK: add dword ptr gVar[eax], eax
8+
// CHECK: add dword ptr ${{[0-9]}}[eax], eax
99
__asm add dword ptr [eax+gVar], eax
10-
// CHECK: add ebx, dword ptr gVar[ebx + $$270]
10+
// CHECK: add ebx, dword ptr ${{[0-9]}}[ebx + $$270]
1111
__asm add ebx, dword ptr gVar[271 - 82 + 81 + ebx]
12-
// CHECK: add dword ptr gVar[ebx + $$828], ebx
12+
// CHECK: add dword ptr ${{[0-9]}}[ebx + $$828], ebx
1313
__asm add dword ptr [ebx + gVar + 828], ebx
14-
// CHECK: add ecx, dword ptr gVar[ecx + ecx * $$4 + $$4590]
14+
// CHECK: add ecx, dword ptr ${{[0-9]}}[ecx + ecx * $$4 + $$4590]
1515
__asm add ecx, dword ptr gVar[4590 + ecx + ecx*4]
16-
// CHECK: add dword ptr gVar[ecx + ecx * $$8 + $$73], ecx
16+
// CHECK: add dword ptr ${{[0-9]}}[ecx + ecx * $$8 + $$73], ecx
1717
__asm add dword ptr [gVar + ecx + 45 + 23 - 53 + 60 - 2 + ecx*8], ecx
18-
// CHECK: add gVar[ecx + ebx + $$7], eax
18+
// CHECK: add ${{[0-9]}}[ecx + ebx + $$7], eax
1919
__asm add 1 + 1 + 2 + 3[gVar + ecx + ebx], eax
2020
}
2121

@@ -32,4 +32,3 @@ void t2() {
3232
// CHECK: mov ${{[0-9]}}[ebx + $$47], eax
3333
__asm mov 5 + 8 + 13 + 21[lVar + ebx], eax
3434
}
35-

llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,8 +1758,8 @@ bool X86AsmParser::CreateMemForMSInlineAsm(
17581758
// It is widely common for MS InlineAsm to use a global variable and one/two
17591759
// registers in a mmory expression, and though unaccessible via rip/eip.
17601760
if (IsGlobalLV && (BaseReg || IndexReg)) {
1761-
Operands.push_back(
1762-
X86Operand::CreateMem(getPointerWidth(), Disp, Start, End, Size));
1761+
Operands.push_back(X86Operand::CreateMem(getPointerWidth(), Disp, Start,
1762+
End, Size, Identifier, Decl));
17631763
return false;
17641764
}
17651765
// Otherwise, we set the base register to a non-zero value
@@ -2551,6 +2551,8 @@ bool X86AsmParser::ParseIntelOperand(OperandVector &Operands) {
25512551
StringRef ErrMsg;
25522552
unsigned BaseReg = SM.getBaseReg();
25532553
unsigned IndexReg = SM.getIndexReg();
2554+
if (IndexReg && BaseReg == X86::RIP)
2555+
BaseReg = 0;
25542556
unsigned Scale = SM.getScale();
25552557
if (!PtrInOperand)
25562558
Size = SM.getElementSize() << 3;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llc < %s -mcpu=x86-64 | FileCheck %s
2+
3+
@arr = internal global [10 x i32] zeroinitializer, align 16
4+
5+
; CHECK: movl %edx, arr(,%rdx,4)
6+
define dso_local i32 @main() #0 {
7+
entry:
8+
call void asm sideeffect inteldialect "mov dword ptr $0[rdx * $$4],edx", "=*m,~{dirflag},~{fpsr},~{flags}"([10 x i32]* @arr) #1, !srcloc !4
9+
ret i32 0
10+
}
11+
12+
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
13+
attributes #1 = { nounwind }
14+
15+
!llvm.module.flags = !{!0, !1, !2}
16+
!llvm.ident = !{!3}
17+
18+
!0 = !{i32 1, !"wchar_size", i32 4}
19+
!1 = !{i32 7, !"uwtable", i32 1}
20+
!2 = !{i32 7, !"frame-pointer", i32 2}
21+
!3 = !{!"clang"}
22+
!4 = !{i64 63}

0 commit comments

Comments
 (0)