Delay diagnosing asm constraints that require immediates until after inlining
authorBill Wendling <isanbard@gmail.com>
Tue, 6 Aug 2019 22:41:22 +0000 (22:41 +0000)
committerBill Wendling <isanbard@gmail.com>
Tue, 6 Aug 2019 22:41:22 +0000 (22:41 +0000)
Summary:
An inline asm call may result in an immediate input value after inlining.
Therefore, don't emit a diagnostic here if the input isn't an immediate.

Reviewers: joerg, eli.friedman, rsmith

Subscribers: asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, s.egerton, krytarowski, mgorny, riccibruno, eraman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D60943

llvm-svn: 368104

clang/lib/CodeGen/CGStmt.cpp
clang/lib/Sema/SemaStmtAsm.cpp
clang/test/CodeGen/pr41027.c [new file with mode: 0644]
clang/test/Sema/inline-asm-validate-riscv.c
clang/test/Sema/inline-asm-validate-x86.c
clang/test/Sema/pr41027.c [deleted file]

index dd0dea5b94a03d221da444ccf506c1e917ff2091..40ab79509f98384acbb6c37305af11efdff6c609 100644 (file)
@@ -1846,11 +1846,9 @@ llvm::Value* CodeGenFunction::EmitAsmInput(
       InputExpr->EvaluateAsRValue(EVResult, getContext(), true);
 
       llvm::APSInt IntResult;
-      if (!EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
-                                           getContext()))
-        llvm_unreachable("Invalid immediate constant!");
-
-      return llvm::ConstantInt::get(getLLVMContext(), IntResult);
+      if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
+                                          getContext()))
+        return llvm::ConstantInt::get(getLLVMContext(), IntResult);
     }
 
     Expr::EvalResult Result;
index b123a739a7ab1ef18550088db3d5f004f5b4a04f..9b051e02d1275a28faf6f8d08a7a3ce1bff887fb 100644 (file)
@@ -383,25 +383,19 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
     } else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) {
       if (!InputExpr->isValueDependent()) {
         Expr::EvalResult EVResult;
-        if (!InputExpr->EvaluateAsRValue(EVResult, Context, true))
-          return StmtError(
-              Diag(InputExpr->getBeginLoc(), diag::err_asm_immediate_expected)
-              << Info.getConstraintStr() << InputExpr->getSourceRange());
-
-        // For compatibility with GCC, we also allow pointers that would be
-        // integral constant expressions if they were cast to int.
-        llvm::APSInt IntResult;
-        if (!EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
-                                             Context))
-          return StmtError(
-              Diag(InputExpr->getBeginLoc(), diag::err_asm_immediate_expected)
-              << Info.getConstraintStr() << InputExpr->getSourceRange());
-
-        if (!Info.isValidAsmImmediate(IntResult))
-          return StmtError(Diag(InputExpr->getBeginLoc(),
-                                diag::err_invalid_asm_value_for_constraint)
-                           << IntResult.toString(10) << Info.getConstraintStr()
-                           << InputExpr->getSourceRange());
+        if (InputExpr->EvaluateAsRValue(EVResult, Context, true)) {
+          // For compatibility with GCC, we also allow pointers that would be
+          // integral constant expressions if they were cast to int.
+          llvm::APSInt IntResult;
+          if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
+                                               Context))
+            if (!Info.isValidAsmImmediate(IntResult))
+              return StmtError(Diag(InputExpr->getBeginLoc(),
+                                    diag::err_invalid_asm_value_for_constraint)
+                               << IntResult.toString(10)
+                               << Info.getConstraintStr()
+                               << InputExpr->getSourceRange());
+        }
       }
 
     } else {
diff --git a/clang/test/CodeGen/pr41027.c b/clang/test/CodeGen/pr41027.c
new file mode 100644 (file)
index 0000000..b60fe7d
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -S -O2 -o - %s | FileCheck %s
+
+// CHECK-LABEL: f:
+// CHECK:         movl $1, %eax
+// CHECK-NEXT:    #APP
+// CHECK-NEXT:    outl %eax, $1
+// CHECK-NEXT:    #NO_APP
+
+static inline void pr41027(unsigned a, unsigned b) {
+  if (__builtin_constant_p(a)) {
+    __asm__ volatile("outl %0,%w1" : : "a"(b), "n"(a));
+  } else {
+    __asm__ volatile("outl %0,%w1" : : "a"(b), "d"(a));
+  }
+}
+
+void f(unsigned port) {
+  pr41027(1, 1);
+}
index cba08c865287e4f500ce0ba802f87831c7ea2780..744f73e23cf26f8be473055201ce77dde81e4d68 100644 (file)
@@ -4,7 +4,6 @@
 void I(int i) {
   static const int BelowMin = -2049;
   static const int AboveMax = 2048;
-  asm volatile ("" :: "I"(i)); // expected-error{{constraint 'I' expects an integer constant expression}}
   asm volatile ("" :: "I"(BelowMin)); // expected-error{{value '-2049' out of range for constraint 'I'}}
   asm volatile ("" :: "I"(AboveMax)); // expected-error{{value '2048' out of range for constraint 'I'}}
 }
@@ -12,7 +11,6 @@ void I(int i) {
 void J(int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 1;
-  asm volatile ("" :: "J"(j)); // expected-error{{constraint 'J' expects an integer constant expression}}
   asm volatile ("" :: "J"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'J'}}
   asm volatile ("" :: "J"(AboveMax)); // expected-error{{value '1' out of range for constraint 'J'}}
 }
@@ -20,7 +18,6 @@ void J(int j) {
 void K(int k) {
   static const int BelowMin = -1;
   static const int AboveMax = 32;
-  asm volatile ("" :: "K"(k)); // expected-error{{constraint 'K' expects an integer constant expression}}
   asm volatile ("" :: "K"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'K'}}
   asm volatile ("" :: "K"(AboveMax)); // expected-error{{value '32' out of range for constraint 'K'}}
 }
index c6fa2e1b4fa8365ac9dbe31a815ae75956d035f0..87b60a0955301a52493b15c1997266d0698eeaf6 100644 (file)
@@ -4,9 +4,6 @@
 void I(int i, int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 32;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "I"(j)); // expected-error{{constraint 'I' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "I"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'I'}}
@@ -21,9 +18,6 @@ void I(int i, int j) {
 void J(int i, int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 64;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "J"(j)); // expected-error{{constraint 'J' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "J"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'J'}}
@@ -38,9 +32,6 @@ void J(int i, int j) {
 void K(int i, int j) {
   static const int BelowMin = -129;
   static const int AboveMax = 128;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "K"(j)); // expected-error{{constraint 'K' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "K"(BelowMin)); // expected-error{{value '-129' out of range for constraint 'K'}}
@@ -60,9 +51,6 @@ void L(int i, int j) {
   static const int Valid1 = 0xff;
   static const int Valid2 = 0xffff;
   static const int Valid3 = 0xffffffff;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "L"(j)); // expected-error{{constraint 'L' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "L"(Invalid1)); // expected-error{{value '1' out of range for constraint 'L'}}
@@ -89,9 +77,6 @@ void L(int i, int j) {
 void M(int i, int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 4;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "M"(j)); // expected-error{{constraint 'M' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "M"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'M'}}
@@ -106,9 +91,6 @@ void M(int i, int j) {
 void N(int i, int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 256;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "N"(j)); // expected-error{{constraint 'N' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "N"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'N'}}
@@ -123,9 +105,6 @@ void N(int i, int j) {
 void O(int i, int j) {
   static const int BelowMin = -1;
   static const int AboveMax = 128;
-  __asm__("xorl %0,%2"
-          : "=r"(i)
-          : "0"(i), "O"(j)); // expected-error{{constraint 'O' expects an integer constant expression}}
   __asm__("xorl %0,%2"
           : "=r"(i)
           : "0"(i), "O"(BelowMin)); // expected-error{{value '-1' out of range for constraint 'O'}}
@@ -146,10 +125,6 @@ void pr40890(void) {
   __asm__ __volatile__("\n#define S_A abcd%0\n" : : "n"(&((struct s*)0)->a));
   // This offset-from-null pointer can be used as an integer constant expression.
   __asm__ __volatile__("\n#define S_B abcd%0\n" : : "n"(&((struct s*)0)->b));
-  // This pointer cannot be used as an integer constant expression.
-  __asm__ __volatile__("\n#define GLOBAL_A abcd%0\n" : : "n"(&s.a)); // expected-error{{constraint 'n' expects an integer constant expression}}
-  // Floating-point is also not okay.
-  __asm__ __volatile__("\n#define PI abcd%0\n" : : "n"(3.14f)); // expected-error{{constraint 'n' expects an integer constant expression}}
 #ifdef AMD64
   // This arbitrary pointer is fine.
   __asm__ __volatile__("\n#define BEEF abcd%0\n" : : "n"((int*)0xdeadbeeeeeef));
diff --git a/clang/test/Sema/pr41027.c b/clang/test/Sema/pr41027.c
deleted file mode 100644 (file)
index 94ace64..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-// RUN: %clang_cc1 -triple x86_64 -fsyntax-only %s
-// XFAIL: *
-
-inline void pr41027(unsigned a, unsigned b) {
-  if (__builtin_constant_p(a)) {
-    __asm__ volatile("outl %0,%w1" : : "a"(b), "n"(a));
-  } else {
-    __asm__ volatile("outl %0,%w1" : : "a"(b), "d"(a));
-  }
-}