]> granicus.if.org Git - clang/commitdiff
[MS] Mangle a unique ID into all MS inline asm labels
authorReid Kleckner <rnk@google.com>
Mon, 28 Nov 2016 20:52:19 +0000 (20:52 +0000)
committerReid Kleckner <rnk@google.com>
Mon, 28 Nov 2016 20:52:19 +0000 (20:52 +0000)
This solves PR23715 in a way that is compatible with LTO.

MSVC supports jumping to source-level labels and between inline asm
blocks, but we don't.

Also revert the old solution, r255201, which was to mark these calls as
noduplicate.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@288059 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Sema/Sema.h
lib/CodeGen/CGStmt.cpp
lib/Sema/Sema.cpp
lib/Sema/SemaStmtAsm.cpp
test/CodeGen/mozilla-ms-inline-asm.c
test/CodeGen/ms-inline-asm.c
test/CodeGen/ms-inline-asm.cpp

index 397c0347739a0980e9061766c5927de2fbbe479c..05031edbfe299bcb4eb5701ee570405dae39c7bd 100644 (file)
@@ -788,9 +788,6 @@ public:
   /// \brief will hold 'respondsToSelector:'
   Selector RespondsToSelectorSel;
 
-  /// \brief counter for internal MS Asm label names.
-  unsigned MSAsmLabelNameCounter;
-
   /// A flag to remember whether the implicit forms of operator new and delete
   /// have been declared.
   bool GlobalNewDeleteDeclared;
index 32233b19d6a01220887660c89e252784b0fe6df5..c68fc18aef967acc3f0328cca9f925dc3c7d4f43 100644 (file)
@@ -2103,15 +2103,6 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
   Result->addAttribute(llvm::AttributeSet::FunctionIndex,
                        llvm::Attribute::NoUnwind);
 
-  if (isa<MSAsmStmt>(&S)) {
-    // If the assembly contains any labels, mark the call noduplicate to prevent
-    // defining the same ASM label twice (PR23715). This is pretty hacky, but it
-    // works.
-    if (AsmString.find("__MSASMLABEL_") != std::string::npos)
-      Result->addAttribute(llvm::AttributeSet::FunctionIndex,
-                           llvm::Attribute::NoDuplicate);
-  }
-
   // Attach readnone and readonly attributes.
   if (!HasSideEffect) {
     if (ReadNone)
index 250e31bc33a2319aad1bb9c7ca5fac41dcb3a13c..fd1a94e16d79a1c2b8bdb0f5441761352ffe91e7 100644 (file)
@@ -96,7 +96,6 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
     ValueWithBytesObjCTypeMethod(nullptr),
     NSArrayDecl(nullptr), ArrayWithObjectsMethod(nullptr),
     NSDictionaryDecl(nullptr), DictionaryWithObjectsMethod(nullptr),
-    MSAsmLabelNameCounter(0),
     GlobalNewDeleteDeclared(false),
     TUKind(TUKind),
     NumSFINAEErrors(0),
index b36abb46c4a6d174b18ea6881b0858507cdc6ece..f11e7aaa34fa0abd6915acfafe6740339d9eb173 100644 (file)
@@ -753,14 +753,12 @@ LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName,
     // Create an internal name for the label.  The name should not be a valid mangled
     // name, and should be unique.  We use a dot to make the name an invalid mangled
     // name.
-    OS << "__MSASMLABEL_." << MSAsmLabelNameCounter++ << "__";
-    for (auto it = ExternalLabelName.begin(); it != ExternalLabelName.end();
-         ++it) {
-      OS << *it;
-      if (*it == '$') {
-        // We escape '$' in asm strings by replacing it with "$$"
+    OS << "__MSASMLABEL_.{:uid}__";
+    for (char C : ExternalLabelName) {
+      OS << C;
+      // We escape '$' in asm strings by replacing it with "$$"
+      if (C == '$')
         OS << '$';
-      }
     }
     Label->setMSAsmLabel(OS.str());
   }
index b8b7a2d677dd7e98380ab2cc72fae4ab696bbdec..b15595632d2e052d5c127d57fd11969c5d17042c 100644 (file)
@@ -20,7 +20,7 @@ void invoke(void* that, unsigned methodIndex,
 // CHECK: call void asm sideeffect inteldialect
 // CHECK: mov edx,dword ptr $1
 // CHECK: test edx,edx
-// CHECK: jz {{[^_]*}}__MSASMLABEL_.0__noparams
+// CHECK: jz {{[^_]*}}__MSASMLABEL_.{:uid}__noparams
 //             ^ Can't use {{.*}} here because the matching is greedy.
 // CHECK: mov eax,edx
 // CHECK: shl eax,$$3
@@ -28,7 +28,7 @@ void invoke(void* that, unsigned methodIndex,
 // CHECK: mov ecx,esp
 // CHECK: push dword ptr $0
 // CHECK: call dword ptr $2
-// CHECK: {{.*}}__MSASMLABEL_.0__noparams:
+// CHECK: {{.*}}__MSASMLABEL_.{:uid}__noparams:
 // CHECK: mov ecx,dword ptr $3
 // CHECK: push ecx
 // CHECK: mov edx,[ecx]
index aa1a474582863e2c81bad4579efbe21d0a95b039..0e9271fe7209d568e285e99d232528f6a305e8f9 100644 (file)
@@ -249,7 +249,7 @@ void t23() {
   the_label:
   }
 // CHECK: t23
-// CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.0__the_label:", "~{dirflag},~{fpsr},~{flags}"()
+// CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.{:uid}__the_label:", "~{dirflag},~{fpsr},~{flags}"()
 }
 
 void t24_helper(void) {}
@@ -595,7 +595,7 @@ void label1() {
     jmp label
   }
   // CHECK-LABEL: define void @label1()
-  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.1__label:\0A\09jmp {{.*}}__MSASMLABEL_.1__label", "~{dirflag},~{fpsr},~{flags}"() [[ATTR1:#[0-9]+]]
+  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.{:uid}__label:\0A\09jmp {{.*}}__MSASMLABEL_.{:uid}__label", "~{dirflag},~{fpsr},~{flags}"() [[ATTR1:#[0-9]+]]
 }
 
 void label2() {
@@ -604,7 +604,7 @@ void label2() {
     label:
   }
   // CHECK-LABEL: define void @label2
-  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.2__label\0A\09{{.*}}__MSASMLABEL_.2__label:", "~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.{:uid}__label\0A\09{{.*}}__MSASMLABEL_.{:uid}__label:", "~{dirflag},~{fpsr},~{flags}"()
 }
 
 void label3() {
@@ -613,7 +613,7 @@ void label3() {
     mov eax, label
   }
   // CHECK-LABEL: define void @label3
-  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.3__label:\0A\09mov eax, {{.*}}__MSASMLABEL_.3__label", "~{eax},~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.{:uid}__label:\0A\09mov eax, {{.*}}__MSASMLABEL_.{:uid}__label", "~{eax},~{dirflag},~{fpsr},~{flags}"()
 }
 
 void label4() {
@@ -622,7 +622,7 @@ void label4() {
     mov eax, [label]
   }
   // CHECK-LABEL: define void @label4
-  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.4__label:\0A\09mov eax, {{.*}}__MSASMLABEL_.4__label", "~{eax},~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "{{.*}}__MSASMLABEL_.{:uid}__label:\0A\09mov eax, {{.*}}__MSASMLABEL_.{:uid}__label", "~{eax},~{dirflag},~{fpsr},~{flags}"()
 }
 
 void label5() {
@@ -631,7 +631,7 @@ void label5() {
     dollar_label$:
   }
   // CHECK-LABEL: define void @label5
-  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.5__dollar_label$$\0A\09{{.*}}__MSASMLABEL_.5__dollar_label$$:", "~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.{:uid}__dollar_label$$\0A\09{{.*}}__MSASMLABEL_.{:uid}__dollar_label$$:", "~{dirflag},~{fpsr},~{flags}"()
 }
 
 void label6(){
@@ -640,7 +640,7 @@ void label6(){
     label:
   }
   // CHECK-LABEL: define void @label6
-  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.6__label\0A\09{{.*}}__MSASMLABEL_.6__label:", "~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "jmp {{.*}}__MSASMLABEL_.{:uid}__label\0A\09{{.*}}__MSASMLABEL_.{:uid}__label:", "~{dirflag},~{fpsr},~{flags}"()
 }
 
 typedef union _LARGE_INTEGER {
@@ -662,4 +662,6 @@ int test_indirect_field(LARGE_INTEGER LargeInteger) {
 // CHECK: call i32 asm sideeffect inteldialect "mov eax, dword ptr $1",
 
 // MS ASM containing labels must not be duplicated (PR23715).
-// CHECK: attributes [[ATTR1]] = { {{.*}}noduplicate{{.*}} }
+// CHECK: attributes [[ATTR1]] = {
+// CHECK-NOT: noduplicate
+// CHECK-SAME: }{{$}}
index 123a0e3d6d3d43c45e5f093addc651a8dcc33416..584122fd93396f879425dd3fbc862add444ce52b 100644 (file)
@@ -126,14 +126,14 @@ void t7_using() {
 void t8() {
   __asm some_label:
   // CHECK-LABEL: define void @_Z2t8v()
-  // CHECK: call void asm sideeffect inteldialect "L__MSASMLABEL_.1__some_label:", "~{dirflag},~{fpsr},~{flags}"()
+  // CHECK: call void asm sideeffect inteldialect "L__MSASMLABEL_.{:uid}__some_label:", "~{dirflag},~{fpsr},~{flags}"()
   struct A {
     static void g() {
       __asm jmp some_label ; This should jump forwards
       __asm some_label:
       __asm nop
       // CHECK-LABEL: define internal void @_ZZ2t8vEN1A1gEv()
-      // CHECK: call void asm sideeffect inteldialect "jmp L__MSASMLABEL_.2__some_label\0A\09L__MSASMLABEL_.2__some_label:\0A\09nop", "~{dirflag},~{fpsr},~{flags}"()
+      // CHECK: call void asm sideeffect inteldialect "jmp L__MSASMLABEL_.{:uid}__some_label\0A\09L__MSASMLABEL_.{:uid}__some_label:\0A\09nop", "~{dirflag},~{fpsr},~{flags}"()
     }
   };
   A::g();