<div dir="ltr"><div>Does anyone know of a good test to estimate whether or not a pair of GlobalVariables could potentially be unified due to the "unnamed_addr" flag?  This is for an out-of-source AA project, and I need to err on the side of assuming unification is possible.  But for precision reasons, I'd really like to avoid false positives.</div><div><div><br></div></div><div>I'm having trouble understanding just how equivalent two GV's initalizers must be for the linker to be allowed to unify them.  Here's what <a href="http://llvm.org/docs/LangRef.html#global-variables">the docs</a> say:  </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span style="color:rgb(0,0,0);font-family:'Lucida Grande','Lucida Sans Unicode',Geneva,Verdana,sans-serif;font-size:14px;line-height:21px">Global variables can be marked with </span><tt class="" style="font-family:Consolas,'Deja Vu Sans Mono','Bitstream Vera Sans Mono',monospace;font-size:0.95em;color:rgb(0,0,0);line-height:21px"><span class="">unnamed_addr</span></tt><span style="color:rgb(0,0,0);font-family:'Lucida Grande','Lucida Sans Unicode',Geneva,Verdana,sans-serif;font-size:14px;line-height:21px"> which indicates that the address is not significant, only the content. Constants marked like this can be merged with other constants if they have the same initializer. Note that a constant with significant address </span><em style="color:rgb(0,0,0);font-family:'Lucida Grande','Lucida Sans Unicode',Geneva,Verdana,sans-serif;font-size:14px;line-height:21px">can</em><span style="color:rgb(0,0,0);font-family:'Lucida Grande','Lucida Sans Unicode',Geneva,Verdana,sans-serif;font-size:14px;line-height:21px"> be merged with a </span><tt class="" style="font-family:Consolas,'Deja Vu Sans Mono','Bitstream Vera Sans Mono',monospace;font-size:0.95em;color:rgb(0,0,0);line-height:21px"><span class="">unnamed_addr</span></tt><span style="color:rgb(0,0,0);font-family:'Lucida Grande','Lucida Sans Unicode',Geneva,Verdana,sans-serif;font-size:14px;line-height:21px"> constant, the result being a constant whose address is significant.</span></blockquote><div><br></div><div>From that wording, I'm having trouble figuring out just how similar two GVs' initializers must be before the linker is considered free to unify the GVs' storage.  I've got a few theories, but would appreciate any suggestions.  I'm hoping for an overall test which is both precise, and not too computationally intensive on a program with very many globals.</div><div><ul><li>Theory 1: At the LLVM C++ API level, GV1 and GV2 can only be unified if their initializer is the very same API object.  I.e., "GV1->getInitializer() == GV2->getInitializer()."  For this to be a sufficient test, I think there would need to be some strong promises by the C++ API implementation regarding using a single object to represent equal or equivalent initial values.<br></li></ul><ul><li>Theory 2: The linker requires that the initializers for GV1 and GV2 are <i>syntactically</i> equivalent compile-time constants, but their initializers might not be described using the same llvm::Constant object.  For example: <br></li></ul></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>@GV1 = private unnamed_addr constant [4 x i8] c"Foo\00", align 1<br></div></div><div>@GV2 = private constant [4 x i8] c"Foo\00", align 1<br></div><div><br></div></blockquote></blockquote><ul><li>Theory 3: Type-safe compile-time-constant semantic equivalence, but unlike Theory 2, allows for syntactically alternative representations.  All that matters is that two initializer objects are equivalently typed, equivalently shaped, and ultimately have equivalent constituent scalar values.</li></ul><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>@GV1 = unnamed_addr constant [4 x i32] zeroinitializer, align 16</div><div>@GV2 = constant [4 x i32] [i32 0, i32 0, i32 0, i32 0], align 16</div></div><div><br></div></blockquote></blockquote><div><ul><li>Theory 4: Arbitrary compile-time-constant bit-pattern equivalence.  For example:</li></ul></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>@X = constant i32 -1, align 4</div><div>@Y = unnamed_addr constant [4 x i8] c"\FF\FF\FF\FF", align 1</div></div><div><br></div></blockquote></blockquote><div><br></div><div>Note: I'm using LLVM's 3.7's C++ API.  The target program will ultimately be linked on modern x86-64 Linux system, <i>probably</i> using Gnu ld.  The program is compiled with clang or clang++, and in some cases I've used "llvm-link" to combine the target-program bitcode files into a single module.  My analysis only considers a single bitcode file in isolation.</div><div><br></div>Thanks,<div>Christian</div><div><br></div></div>