[PATCH] Attach readonly and readnone attributes to inline-asm instructions
Akira Hatanaka
ahatanak at gmail.com
Thu Jun 18 12:55:07 PDT 2015
Currently, clang/llvm handles inline-asm instructions in a very conservative manner, choosing not to eliminate the instructions or hoisting them out of a loop even when it's safe to do so. This patch makes changes to clang to attach a readonly or readnone attribute to an inline-asm instruction, which enables passes such as LICM and EarlyCSE to move or optimize away the instruction.
The patch is based on the patch Chad proposed in 2012:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20120430/057131.html
If this patch is approved, I plan to add another test case to check CSE and LICM can remove an inline-asm or hoist it out of a loop if readonly or readnone is attached.
http://reviews.llvm.org/D10546
Files:
lib/CodeGen/CGStmt.cpp
test/CodeGen/asm-attrs.c
Index: lib/CodeGen/CGStmt.cpp
===================================================================
--- lib/CodeGen/CGStmt.cpp
+++ lib/CodeGen/CGStmt.cpp
@@ -1843,9 +1843,18 @@
std::vector<llvm::Value*> InOutArgs;
std::vector<llvm::Type*> InOutArgTypes;
+ // An inline asm can be marked readonly if it isn't volatile, it doesn't
+ // clobber memory, and it doesn't have any output memory constraints. It
+ // can be marked readnone if it doesn't have any input or output memory
+ // constraints.
+ bool ReadOnly = !S.isVolatile(), ReadNone = !S.isVolatile();
+
for (unsigned i = 0, e = S.getNumOutputs(); i != e; i++) {
TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
+ if (Info.allowsMemory())
+ ReadOnly = ReadNone = false;
+
// Simplify the output constraint.
std::string OutputConstraint(S.getOutputConstraint(i));
OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
@@ -1950,6 +1959,9 @@
TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
+ if (Info.allowsMemory())
+ ReadNone = false;
+
if (!Constraints.empty())
Constraints += ',';
@@ -2014,7 +2026,9 @@
for (unsigned i = 0, e = S.getNumClobbers(); i != e; i++) {
StringRef Clobber = S.getClobber(i);
- if (Clobber != "memory" && Clobber != "cc")
+ if (Clobber == "memory")
+ ReadOnly = ReadNone = false;
+ else if (Clobber != "cc")
Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
if (!Constraints.empty())
@@ -2054,6 +2068,14 @@
Result->addAttribute(llvm::AttributeSet::FunctionIndex,
llvm::Attribute::NoUnwind);
+ // Attach readnone and readonly attributes.
+ if (ReadNone)
+ Result->addAttribute(llvm::AttributeSet::FunctionIndex,
+ llvm::Attribute::ReadNone);
+ else if (ReadOnly)
+ Result->addAttribute(llvm::AttributeSet::FunctionIndex,
+ llvm::Attribute::ReadOnly);
+
// Slap the source location of the inline asm into a !srcloc metadata on the
// call.
if (const GCCAsmStmt *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S)) {
Index: test/CodeGen/asm-attrs.c
===================================================================
--- /dev/null
+++ test/CodeGen/asm-attrs.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -triple armv7-apple-darwin -emit-llvm %s -o - | FileCheck %s
+
+// CHECK: call i32 asm "foo0", {{.*}} [[READNONE:#[0-9]+]]
+// CHECK: call i32 asm "foo1", {{.*}} [[READNONE]]
+// CHECK: call i32 asm "foo2", {{.*}} [[NOATTRS:#[0-9]+]]
+// CHECK: call i32 asm sideeffect "foo3", {{.*}} [[NOATTRS]]
+// CHECK: call i32 asm "foo4", {{.*}} [[READONLY:#[0-9]+]]
+// CHECK: call i32 asm "foo5", {{.*}} [[READONLY]]
+// CHECK: call i32 asm "foo6", {{.*}} [[NOATTRS]]
+
+// CHECK: attributes [[READNONE]] = { nounwind readnone }
+// CHECK: attributes [[NOATTRS]] = { nounwind }
+// CHECK: attributes [[READONLY]] = { nounwind readonly }
+
+int g0, g1;
+
+void test_attrs(int a) {
+ __asm__ ("foo0" : "=r"(g1) : "r"(a));
+ __asm__ ("foo1" : "=r"(g1) : "r"(a) : "cc");
+ __asm__ ("foo2" : "=r"(g1) : "r"(a) : "memory");
+ __asm__ volatile("foo3" : "=r"(g1) : "r"(a));
+ __asm__ ("foo4" : "=r"(g1) : "r"(a), "m"(g0));
+ __asm__ ("foo5" : "=r"(g1) : "r"(a), "Q"(g0));
+ __asm__ ("foo6" : "=r"(g1), "=m"(g0) : "r"(a));
+}
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10546.27952.patch
Type: text/x-patch
Size: 3335 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150618/760886d4/attachment.bin>
More information about the cfe-commits
mailing list