pengfei created this revision.
pengfei added reviewers: skan, xiangzhangllvm, craig.topper, coby.
Herald added a subscriber: hiraditya.
pengfei requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.
D113096 <https://reviews.llvm.org/D113096> solved the "undefined reference to
xxx" issue by adding
constraint *m for the global var. But it has strong side effect due to
the symbol in the assembly being replace with constraint variable.
This leads to some lowering fails. https://godbolt.org/z/h3nWoerPe
This patch fix the problem by use the constraint *m as place holder
rather than real constraint. It has negligible effect for the existing
code generation.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D115225
Files:
clang/test/CodeGen/X86/ms_fmul.c
clang/test/CodeGen/ms-inline-asm-functions.c
clang/test/CodeGen/ms-inline-asm-static-variable.c
clang/test/CodeGen/ms-inline-asm-variables.c
llvm/include/llvm/MC/MCParser/MCParsedAsmOperand.h
llvm/lib/MC/MCParser/AsmParser.cpp
llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
llvm/lib/Target/X86/AsmParser/X86Operand.h
llvm/test/CodeGen/X86/ms-inline-asm-array.ll
Index: llvm/test/CodeGen/X86/ms-inline-asm-array.ll
===================================================================
--- llvm/test/CodeGen/X86/ms-inline-asm-array.ll
+++ llvm/test/CodeGen/X86/ms-inline-asm-array.ll
@@ -5,7 +5,7 @@
; CHECK: movl %edx, arr(,%rdx,4)
define dso_local i32 @main() #0 {
entry:
- call void asm sideeffect inteldialect "mov dword ptr $0[rdx * $$4],edx", "=*m,~{dirflag},~{fpsr},~{flags}"([10 x i32]* @arr) #1, !srcloc !4
+ call void asm sideeffect inteldialect "mov dword ptr arr[rdx * $$4],edx", "=*m,~{dirflag},~{fpsr},~{flags}"([10 x i32]* @arr) #1, !srcloc !4
ret i32 0
}
Index: llvm/lib/Target/X86/AsmParser/X86Operand.h
===================================================================
--- llvm/lib/Target/X86/AsmParser/X86Operand.h
+++ llvm/lib/Target/X86/AsmParser/X86Operand.h
@@ -285,6 +285,13 @@
bool isOffsetOfLocal() const override { return isImm() && Imm.LocalRef; }
+ bool isMemPlaceholder(const MCInstrDesc &Desc) const override {
+ // Add more restrictions to avoid the use of global symbols. This helps
+ // with reducing the code size.
+ return !Desc.isRematerializable() && !Desc.isCall() && isMem() &&
+ !Mem.BaseReg && !Mem.IndexReg;
+ }
+
bool needAddressOf() const override { return AddressOf; }
bool isMem() const override { return Kind == Memory; }
Index: llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
===================================================================
--- llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -2551,8 +2551,6 @@
StringRef ErrMsg;
unsigned BaseReg = SM.getBaseReg();
unsigned IndexReg = SM.getIndexReg();
- if (IndexReg && BaseReg == X86::RIP)
- BaseReg = 0;
unsigned Scale = SM.getScale();
if (!PtrInOperand)
Size = SM.getElementSize() << 3;
Index: llvm/lib/MC/MCParser/AsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmParser.cpp
+++ llvm/lib/MC/MCParser/AsmParser.cpp
@@ -6012,12 +6012,13 @@
bool isOutput = (i == 1) && Desc.mayStore();
SMLoc Start = SMLoc::getFromPointer(SymName.data());
+ int64_t Size = Operand.isMemPlaceholder(Desc) ? 0 : SymName.size();
if (isOutput) {
++InputIdx;
OutputDecls.push_back(OpDecl);
OutputDeclsAddressOf.push_back(Operand.needAddressOf());
OutputConstraints.push_back(("=" + Constraint).str());
- AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
+ AsmStrRewrites.emplace_back(AOK_Output, Start, Size);
} else {
InputDecls.push_back(OpDecl);
InputDeclsAddressOf.push_back(Operand.needAddressOf());
@@ -6025,7 +6026,7 @@
if (Desc.OpInfo[i - 1].isBranchTarget())
AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
else
- AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
+ AsmStrRewrites.emplace_back(AOK_Input, Start, Size);
}
}
@@ -6140,13 +6141,17 @@
OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
break;
case AOK_Input:
- OS << '$' << InputIdx++;
+ if (AR.Len)
+ OS << '$' << InputIdx;
+ ++InputIdx;
break;
case AOK_CallInput:
OS << "${" << InputIdx++ << ":P}";
break;
case AOK_Output:
- OS << '$' << OutputIdx++;
+ if (AR.Len)
+ OS << '$' << OutputIdx;
+ ++OutputIdx;
break;
case AOK_SizeDirective:
switch (AR.Val) {
Index: llvm/include/llvm/MC/MCParser/MCParsedAsmOperand.h
===================================================================
--- llvm/include/llvm/MC/MCParser/MCParsedAsmOperand.h
+++ llvm/include/llvm/MC/MCParser/MCParsedAsmOperand.h
@@ -10,6 +10,7 @@
#define LLVM_MC_MCPARSER_MCPARSEDASMOPERAND_H
#include "llvm/ADT/StringRef.h"
+#include "llvm/MC/MCInstrDesc.h"
#include "llvm/Support/SMLoc.h"
#include <string>
@@ -76,6 +77,10 @@
/// assembly.
virtual bool isOffsetOfLocal() const { return false; }
+ /// isMemPlaceholder - Do we need to ignore the constraint, rather than emit
+ /// code? Only valid when parsing MS-style inline assembly.
+ virtual bool isMemPlaceholder(const MCInstrDesc &Desc) const { return false; }
+
/// getOffsetOfLoc - Get the location of the offset operator.
virtual SMLoc getOffsetOfLoc() const { return SMLoc(); }
Index: clang/test/CodeGen/ms-inline-asm-variables.c
===================================================================
--- clang/test/CodeGen/ms-inline-asm-variables.c
+++ clang/test/CodeGen/ms-inline-asm-variables.c
@@ -3,19 +3,19 @@
int gVar;
void t1() {
- // CHECK: add eax, dword ptr ${{[0-9]}}[eax]
+ // CHECK: add eax, dword ptr gVar[eax]
__asm add eax, dword ptr gVar[eax]
- // CHECK: add dword ptr ${{[0-9]}}[eax], eax
+ // CHECK: add dword ptr gVar[eax], eax
__asm add dword ptr [eax+gVar], eax
- // CHECK: add ebx, dword ptr ${{[0-9]}}[ebx + $$270]
+ // CHECK: add ebx, dword ptr gVar[ebx + $$270]
__asm add ebx, dword ptr gVar[271 - 82 + 81 + ebx]
- // CHECK: add dword ptr ${{[0-9]}}[ebx + $$828], ebx
+ // CHECK: add dword ptr gVar[ebx + $$828], ebx
__asm add dword ptr [ebx + gVar + 828], ebx
- // CHECK: add ecx, dword ptr ${{[0-9]}}[ecx + ecx * $$4 + $$4590]
+ // CHECK: add ecx, dword ptr gVar[ecx + ecx * $$4 + $$4590]
__asm add ecx, dword ptr gVar[4590 + ecx + ecx*4]
- // CHECK: add dword ptr ${{[0-9]}}[ecx + ecx * $$8 + $$73], ecx
+ // CHECK: add dword ptr gVar[ecx + ecx * $$8 + $$73], ecx
__asm add dword ptr [gVar + ecx + 45 + 23 - 53 + 60 - 2 + ecx*8], ecx
- // CHECK: add ${{[0-9]}}[ecx + ebx + $$7], eax
+ // CHECK: add gVar[ecx + ebx + $$7], eax
__asm add 1 + 1 + 2 + 3[gVar + ecx + ebx], eax
}
Index: clang/test/CodeGen/ms-inline-asm-static-variable.c
===================================================================
--- clang/test/CodeGen/ms-inline-asm-static-variable.c
+++ clang/test/CodeGen/ms-inline-asm-static-variable.c
@@ -5,6 +5,6 @@
static int arr[10];
void t1() {
// CHECK: @arr = internal global [10 x i32]
- // CHECK: call void asm sideeffect inteldialect "mov dword ptr $0[edx * $$4],edx", "=*m,{{.*}}([10 x i32]* @arr)
+ // CHECK: call void asm sideeffect inteldialect "mov dword ptr arr[edx * $$4],edx", "=*m,{{.*}}([10 x i32]* @arr)
__asm mov dword ptr arr[edx*4],edx
}
Index: clang/test/CodeGen/ms-inline-asm-functions.c
===================================================================
--- clang/test/CodeGen/ms-inline-asm-functions.c
+++ clang/test/CodeGen/ms-inline-asm-functions.c
@@ -39,7 +39,7 @@
int baz() {
// CHECK-LABEL: _baz:
__asm mov eax, k;
- // CHECK: movl _k, %eax
+ // CHECK: movl k, %eax
__asm mov eax, kptr;
// CHECK: movl _kptr, %eax
}
Index: clang/test/CodeGen/X86/ms_fmul.c
===================================================================
--- clang/test/CodeGen/X86/ms_fmul.c
+++ clang/test/CodeGen/X86/ms_fmul.c
@@ -18,4 +18,4 @@
}}
// CHECK-LABEL: foo
-// CHECK: call void asm sideeffect inteldialect "fmul qword ptr $0[edx + $$240]\0A\09ret"
+// CHECK: call void asm sideeffect inteldialect "fmul qword ptr static_const_table[edx + $$240]\0A\09ret"
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits