<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>r174355.<br><br>On Feb 4, 2013, at 11:52 PM, Michael Gottesman <<a href="mailto:mgottesman@apple.com">mgottesman@apple.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>You are correct. Let me fix that.<br><br>On Feb 4, 2013, at 11:28 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br><br></div><blockquote type="cite"><div>On Mon, Feb 4, 2013 at 10:53 PM, Michael Gottesman <span dir="ltr"><<a href="mailto:mgottesman@apple.com" target="_blank">mgottesman@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: mgottesman<br>
Date: Tue Feb  5 00:53:26 2013<br>
New Revision: 174345<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=174345&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=174345&view=rev</a><br>
Log:<br>
Add code to GlobalVariable.h so that global variables marked as<br>
externally_initialized return false for hasDefiniteInitializer and<br>
hasUniqueInitializer.<br>
<br>
<a href="rdar://12580965">rdar://12580965</a>.<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll<br>
Modified:<br>
    llvm/trunk/include/llvm/IR/GlobalVariable.h<br>
<br>
Modified: llvm/trunk/include/llvm/IR/GlobalVariable.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/GlobalVariable.h?rev=174345&r1=174344&r2=174345&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/GlobalVariable.h?rev=174345&r1=174344&r2=174345&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/include/llvm/IR/GlobalVariable.h (original)<br>
+++ llvm/trunk/include/llvm/IR/GlobalVariable.h Tue Feb  5 00:53:26 2013<br>
@@ -110,7 +110,10 @@ public:<br>
     return hasInitializer() &&<br>
       // The initializer of a global variable with weak linkage may change at<br>
       // link time.<br>
-      !mayBeOverridden();<br>
+      !mayBeOverridden() &&<br>
+      // The initializer of a global variable with the externally_initialized<br>
+      // marker may change at runtime before cxx initializers are evaluated.<br></blockquote><div><br></div><div>You mean "C++", not "cxx", right? (Or, more generally, before global constructors run -- __attribute__((constructor)) functions are handled by globalopt too.)</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+      !isExternallyInitialized();<br>
   }<br>
<br>
   /// hasUniqueInitializer - Whether the global variable has an initializer, and<br>
@@ -123,7 +126,11 @@ public:<br>
       // instead. It is wrong to modify the initializer of a global variable<br>
       // with *_odr linkage because then different instances of the global may<br>
       // have different initializers, breaking the One Definition Rule.<br>
-      !isWeakForLinker();<br>
+      !isWeakForLinker() &&<br>
+      // It is not safe to modify initializers of global variables with the<br>
+      // external_initializer marker since the value may be changed at runtime<br>
+      // before cxx initializers are evaluated.<br>
+      !isExternallyInitialized();<br>
   }<br>
<br>
   /// getInitializer - Return the initializer for this global variable.  It is<br>
<br>
Added: llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll?rev=174345&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll?rev=174345&view=auto</a><br>

==============================================================================<br>
--- llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll (added)<br>
+++ llvm/trunk/test/Transforms/GlobalOpt/externally-initialized-global-ctr.ll Tue Feb  5 00:53:26 2013<br>
@@ -0,0 +1,35 @@<br>
+; RUN: opt < %s -globalopt -S | FileCheck %s<br>
+; <a href="rdar://12580965">rdar://12580965</a>.<br>
+; ObjC++ test case.<br>
+<br>
+%struct.ButtonInitData = type { i8* }<br>
+<br>
+@_ZL14buttonInitData = internal global [1 x %struct.ButtonInitData] zeroinitializer, align 4<br>
+<br>
+@"\01L_OBJC_METH_VAR_NAME_40" = internal global [7 x i8] c"print:\00", section "__TEXT,__objc_methname,cstring_literals", align 1<br>
+@"\01L_OBJC_SELECTOR_REFERENCES_41" = internal externally_initialized  global i8* getelementptr inbounds ([7 x i8]* @"\01L_OBJC_METH_VAR_NAME_40", i32 0, i32 0), section "__DATA, __objc_selrefs, literal_pointers, no_dead_strip"<br>

+<br>
+@llvm.global_ctors = appending global [1 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }]<br>
+@llvm.used = appending global [2 x i8*] [i8* getelementptr inbounds ([7 x i8]* @"\01L_OBJC_METH_VAR_NAME_40", i32 0, i32 0),  i8* bitcast (i8** @"\01L_OBJC_SELECTOR_REFERENCES_41" to i8*)]<br>
+<br>
+define internal void @__cxx_global_var_init() section "__TEXT,__StaticInit,regular,pure_instructions" {<br>
+  %1 = load i8** @"\01L_OBJC_SELECTOR_REFERENCES_41", !invariant.load !2009<br>
+  store i8* %1, i8** getelementptr inbounds ([1 x %struct.ButtonInitData]* @_ZL14buttonInitData, i32 0, i32 0, i32 0), align 4<br>
+  ret void<br>
+}<br>
+<br>
+define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" {<br>
+  call void @__cxx_global_var_init()<br>
+  ret void<br>
+}<br>
+<br>
+declare void @test(i8*)<br>
+<br>
+define void @print() {<br>
+; CHECK: %1 = load i8** getelementptr inbounds ([1 x %struct.ButtonInitData]* @_ZL14buttonInitData, i32 0, i32 0, i32 0), align 4<br>
+  %1 = load i8** getelementptr inbounds ([1 x %struct.ButtonInitData]* @_ZL14buttonInitData, i32 0, i32 0, i32 0), align 4<br>
+  call void @test(i8* %1)<br>
+  ret void<br>
+}<br>
+<br>
+!2009 = metadata !{}<br>
\ No newline at end of file<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br>
</div></blockquote>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>llvm-commits mailing list</span><br><span><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a></span><br><span><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></span><br></div></blockquote></body></html>