summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Smith <richard-llvm@metafoo.co.uk>2012-02-17 07:31:37 +0000
committerRichard Smith <richard-llvm@metafoo.co.uk>2012-02-17 07:31:37 +0000
commit08a51446213bb26cb0eaaefeb91d4e0c23084759 (patch)
tree1fd2cb3dc0b5aa58ef12496f7b859233db934de6
parent2939e6e136a6a7b26367dd43cd3a858054228a4e (diff)
downloadbcm5719-llvm-08a51446213bb26cb0eaaefeb91d4e0c23084759.tar.gz
bcm5719-llvm-08a51446213bb26cb0eaaefeb91d4e0c23084759.zip
The clang half of r150794: after the construction of a global or static const
variable ends, if the variable has a trivial destructor and no mutable subobjects then emit an llvm.invariant.start call for it. globalopt knows to make the variable const when evaluating this. llvm-svn: 150798
-rw-r--r--clang/lib/CodeGen/CGDeclCXX.cpp18
-rw-r--r--clang/test/CodeGenCXX/init-invariant.cpp60
2 files changed, 77 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
index 189760821b7..75bb7dedd90 100644
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
@@ -101,6 +101,19 @@ static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
CGF.EmitCXXGlobalDtorRegistration(function, argument);
}
+/// Emit code to cause the variable at the given address to be considered as
+/// constant from this point onwards.
+static void EmitDeclInvariant(CodeGenFunction &CGF, llvm::Constant *Addr) {
+ // Grab the llvm.invariant.start intrinsic.
+ llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
+ llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID);
+
+ // Emit a call, with size -1 signifying the whole object.
+ llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, -1),
+ llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)};
+ CGF.Builder.CreateCall(InvariantStart, Args);
+}
+
void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
llvm::Constant *DeclPtr,
bool PerformInit) {
@@ -111,7 +124,10 @@ void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
if (!T->isReferenceType()) {
if (PerformInit)
EmitDeclInit(*this, D, DeclPtr);
- EmitDeclDestroy(*this, D, DeclPtr);
+ if (CGM.isTypeConstant(D.getType(), true))
+ EmitDeclInvariant(*this, DeclPtr);
+ else
+ EmitDeclDestroy(*this, D, DeclPtr);
return;
}
diff --git a/clang/test/CodeGenCXX/init-invariant.cpp b/clang/test/CodeGenCXX/init-invariant.cpp
new file mode 100644
index 00000000000..da17303b2d4
--- /dev/null
+++ b/clang/test/CodeGenCXX/init-invariant.cpp
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -triple i686-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+// Check that we add an llvm.invariant.start to mark when a global becomes
+// read-only. If globalopt can fold the initializer, it will then mark the
+// variable as constant.
+
+struct A {
+ A() : n(42) {}
+ int n;
+};
+
+// CHECK: @a = global {{.*}} zeroinitializer
+extern const A a = A();
+
+struct B {
+ B() : n(76) {}
+ mutable int n;
+};
+
+// CHECK: @b = global {{.*}} zeroinitializer
+extern const B b = B();
+
+struct C {
+ C() : n(81) {}
+ ~C();
+ int n;
+};
+
+// CHECK: @c = global {{.*}} zeroinitializer
+extern const C c = C();
+
+int f() { return 5; }
+// CHECK: @d = global i32 0
+extern const int d = f();
+
+void e() {
+ static const A a = A();
+}
+
+// CHECK: define internal void @__cxx_global_var_init
+// CHECK: call void @_ZN1AC1Ev({{.*}}* @a)
+// CHECK-NEXT: call {{.*}}@llvm.invariant.start(i64 -1, i8* bitcast ({{.*}} @a to i8*))
+
+// CHECK: define internal void @__cxx_global_var_init
+// CHECK: call void @_ZN1BC1Ev({{.*}}* @b)
+// CHECK-NOT: call {{.*}}@llvm.invariant.start(i64 -1, i8* bitcast ({{.*}} @b to i8*))
+
+// CHECK: define internal void @__cxx_global_var_init
+// CHECK: call void @_ZN1CC1Ev({{.*}}* @c)
+// CHECK-NOT: call {{.*}}@llvm.invariant.start(i64 -1, i8* bitcast ({{.*}} @c to i8*))
+
+// CHECK: define internal void @__cxx_global_var_init
+// CHECK: call i32 @_Z1fv(
+// CHECK: store {{.*}}, i32* @d
+// CHECK: call {{.*}}@llvm.invariant.start(i64 -1, i8* bitcast ({{.*}} @d to i8*))
+
+// CHECK: define void @_Z1ev(
+// CHECK: call void @_ZN1AC1Ev(%struct.A* @_ZZ1evE1a)
+// CHECK: call {{.*}}@llvm.invariant.start(i64 -1, i8* bitcast ({{.*}} @_ZZ1evE1a to i8*))
+// CHECK-NOT: llvm.invariant.end
OpenPOWER on IntegriCloud