<div dir="ltr">I'm working on a whole program optimizer that uses LLVM as a library, and one of the things I want to do is eliminate dead global functions and variables even when they are not local to a module. (This understandably doesn't happen by default because the optimizer has to assume it could be compiling a library rather than a program.)<br><br>I've actually written a function to do this, but then I came across InternalizePass which seems like it could allow my code to be discarded because it could flag globals as internal (in the presence of a main function, according to the documentation) which would allow the existing dead global elimination optimization pass to do the job.<br><br>It seems that InternalizePass is, again understandably, not enabled by default even when you select optimization level 3. How do you turn it on? I tried setting PrepareForLTO but that doesn't seem to do anything. Here's what I have so far:<br><br>    legacy::FunctionPassManager FPM(&M);<br>    legacy::PassManager MPM;<br><br>    PassManagerBuilder Builder;<br>    Builder.OptLevel = 3;<br>    Builder.PrepareForLTO = true;<br>    Builder.VerifyInput = true;<br>    Builder.VerifyOutput = true;<br>    Builder.populateFunctionPassManager(FPM);<br>    Builder.populateModulePassManager(MPM);<br><br>    FPM.doInitialization();<br>    for (Function &F : M)<br>      FPM.run(F);<br>    FPM.doFinalization();<br>    MPM.run(M);<br><br></div>