<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:12.727272033691406px">Hello,</span><div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">
GlobalOpt has an interesting special-case optimization for globals that are only accessed within "main". These globals are replaced by allocas within the "main" function (and the GV itself is deleted). The full condition for this happening is:</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><div>  // If this is a first class global and has only one accessing function</div>
<div>  // and this function is main (which we know is not recursive we can make</div><div>  // this global a local variable) we replace the global with a local alloca</div><div>  // in this function.</div><div>  //</div><div>
  // NOTE: It doesn't make sense to promote non single-value types since we</div><div>  // are just replacing static memory to stack memory.</div><div>  //</div><div>  // If the global is in different address space, don't bring it to stack.</div>
<div>  if (!GS.HasMultipleAccessingFunctions &&</div><div>      GS.AccessingFunction && !GS.HasNonInstructionUser &&</div><div>      GV->getType()->getElementType()->isSingleValueType() &&</div>
<div>      GS.AccessingFunction->getName() == "main" &&</div><div>      GS.AccessingFunction->hasExternalLinkage() &&</div><div>      GV->getType()->getAddressSpace() == 0) {</div></div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">From today's discussion on IRC, there appear to be two problems with this approach:</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">1) The hard-coding of "main" to mean "entry point to the code" that only dynamically runs once.</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px">2) Assuming that "main" cannot be recursive (in the general sense).</div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">
<br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">(1) is a problem for non-traditional compilation flows such as simply JIT of freestanding code where "main" is not the entry point; another case is PNaCl, where "main" is not the entry point ("_start" is), and others where parts of the runtime environment are included in the IR together with the user code. This is not the only place where the name "main" is hard-coded within the LLVM code base, but it's a good example.</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">(2) is a problem because the C standard, unlike the C++ standard, says nothing about "main" not being recursive. C++11 says in 3.6.1: "The function main shall not be used within a program.". C does not appear to mention such a restriction, which may make the optimization invalid for C.</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">A number of possible solutions were raised: some sort of function attribute that marks an entry point, module-level entry point, documenting that LLVM assumes that the entry point is always renamed to "main", etc. These mostly address (1) but not (2).</div>
<div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">Any thoughts and suggestions are welcome.</div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">
<br></div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px">Eli</div><div style="font-family:arial,sans-serif;font-size:12.727272033691406px"><br></div></div>