Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 9 | // |
Rafael Espindola | 4253bd8 | 2012-10-26 18:47:48 +0000 | [diff] [blame] | 10 | // This pass loops over all of the functions and variables in the input module. |
| 11 | // If the function or variable is not in the list of external names given to |
| 12 | // the pass it is marked as internal. |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 13 | // |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 14 | // This transformation would not be legal in a regular compilation, but it gets |
| 15 | // extra information from the linker about what is safe. |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 16 | // |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 17 | // For example: Internalizing a function with external linkage. Only if we are |
| 18 | // told it is only used from within this module, it is safe to do it. |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 19 | // |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO/Internalize.h" |
Chris Lattner | 99a53f6 | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
Mehdi Amini | c87d7d0 | 2016-02-10 23:24:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
| 31 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 35 | #include <fstream> |
| 36 | #include <set> |
Chris Lattner | f52e03c | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "internalize" |
| 40 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 41 | STATISTIC(NumAliases, "Number of aliases internalized"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 42 | STATISTIC(NumFunctions, "Number of functions internalized"); |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 43 | STATISTIC(NumGlobals, "Number of global vars internalized"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 44 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 45 | // APIFile - A file which contains a list of symbols that should not be marked |
| 46 | // external. |
| 47 | static cl::opt<std::string> |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 48 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
| 49 | cl::desc("A file containing list of symbol names to preserve")); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 50 | |
| 51 | // APIList - A list of symbols that should not be marked internal. |
| 52 | static cl::list<std::string> |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 53 | APIList("internalize-public-api-list", cl::value_desc("list"), |
| 54 | cl::desc("A list of symbol names to preserve"), cl::CommaSeparated); |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 55 | |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 56 | namespace { |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 57 | |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 58 | // Helper to load an API list to preserve from file and expose it as a functor |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 59 | // for internalization. |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 60 | class PreserveAPIList { |
| 61 | public: |
| 62 | PreserveAPIList() { |
| 63 | if (!APIFile.empty()) |
| 64 | LoadFile(APIFile); |
| 65 | ExternalNames.insert(APIList.begin(), APIList.end()); |
| 66 | } |
| 67 | |
| 68 | bool operator()(const GlobalValue &GV) { |
| 69 | return ExternalNames.count(GV.getName()); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | // Contains the set of symbols loaded from file |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 74 | StringSet<> ExternalNames; |
Mehdi Amini | c87d7d0 | 2016-02-10 23:24:31 +0000 | [diff] [blame] | 75 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 76 | void LoadFile(StringRef Filename) { |
| 77 | // Load the APIFile... |
| 78 | std::ifstream In(Filename.data()); |
| 79 | if (!In.good()) { |
| 80 | errs() << "WARNING: Internalize couldn't load file '" << Filename |
| 81 | << "'! Continuing as if it's empty.\n"; |
| 82 | return; // Just continue as if the file were empty |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 83 | } |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 84 | while (In) { |
| 85 | std::string Symbol; |
| 86 | In >> Symbol; |
| 87 | if (!Symbol.empty()) |
| 88 | ExternalNames.insert(Symbol); |
| 89 | } |
| 90 | } |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 91 | }; |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 92 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 93 | // Internalization exposed as a pass |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 94 | class InternalizePass : public ModulePass { |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 95 | // Client supplied callback to control wheter a symbol must be preserved. |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 96 | std::function<bool(const GlobalValue &)> MustPreserveGV; |
| 97 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 98 | public: |
| 99 | static char ID; // Pass identification, replacement for typeid |
| 100 | |
| 101 | InternalizePass() : ModulePass(ID), MustPreserveGV(PreserveAPIList()) {} |
| 102 | |
| 103 | InternalizePass(std::function<bool(const GlobalValue &)> MustPreserveGV) |
| 104 | : ModulePass(ID), MustPreserveGV(std::move(MustPreserveGV)) { |
| 105 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
| 106 | } |
| 107 | |
| 108 | bool runOnModule(Module &M) override { |
| 109 | CallGraphWrapperPass *CGPass = |
| 110 | getAnalysisIfAvailable<CallGraphWrapperPass>(); |
| 111 | CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr; |
| 112 | return internalizeModule(M, MustPreserveGV, CG); |
| 113 | } |
| 114 | |
| 115 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 116 | AU.setPreservesCFG(); |
| 117 | AU.addPreserved<CallGraphWrapperPass>(); |
| 118 | } |
| 119 | }; |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 120 | |
| 121 | // Helper class to perform internalization. |
| 122 | class Internalizer { |
| 123 | // Client supplied callback to control wheter a symbol must be preserved. |
| 124 | const std::function<bool(const GlobalValue &)> &MustPreserveGV; |
| 125 | |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 126 | // Set of symbols private to the compiler that this pass should not touch. |
| 127 | StringSet<> AlwaysPreserved; |
| 128 | |
| 129 | // Return false if we're allowed to internalize this GV. |
| 130 | bool ShouldPreserveGV(const GlobalValue &GV) { |
| 131 | // Function must be defined here |
| 132 | if (GV.isDeclaration()) |
| 133 | return true; |
| 134 | |
| 135 | // Available externally is really just a "declaration with a body". |
| 136 | if (GV.hasAvailableExternallyLinkage()) |
| 137 | return true; |
| 138 | |
| 139 | // Assume that dllexported symbols are referenced elsewhere |
| 140 | if (GV.hasDLLExportStorageClass()) |
| 141 | return true; |
| 142 | |
| 143 | // Already local, has nothing to do. |
| 144 | if (GV.hasLocalLinkage()) |
| 145 | return false; |
| 146 | |
| 147 | // Check some special cases |
| 148 | if (AlwaysPreserved.count(GV.getName())) |
| 149 | return true; |
| 150 | |
| 151 | return MustPreserveGV(GV); |
| 152 | } |
| 153 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 154 | bool maybeInternalize(GlobalValue &GV, |
| 155 | const std::set<const Comdat *> &ExternalComdats); |
| 156 | void checkComdatVisibility(GlobalValue &GV, |
| 157 | std::set<const Comdat *> &ExternalComdats); |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 158 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 159 | public: |
| 160 | Internalizer(const std::function<bool(const GlobalValue &)> &MustPreserveGV) |
| 161 | : MustPreserveGV(MustPreserveGV) {} |
Nuno Lopes | 5093ab4 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 162 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 163 | /// Run the internalizer on \p TheModule, returns true if any changes was |
| 164 | /// made. |
| 165 | /// |
| 166 | /// If the CallGraph \p CG is supplied, it will be updated when |
| 167 | /// internalizing a function (by removing any edge from the "external node") |
| 168 | bool internalizeModule(Module &TheModule, CallGraph *CG = nullptr); |
| 169 | }; |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 170 | |
| 171 | // Internalize GV if it is possible to do so, i.e. it is not externally visible |
| 172 | // and is not a member of an externally visible comdat. |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 173 | bool Internalizer::maybeInternalize( |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 174 | GlobalValue &GV, const std::set<const Comdat *> &ExternalComdats) { |
| 175 | if (Comdat *C = GV.getComdat()) { |
| 176 | if (ExternalComdats.count(C)) |
| 177 | return false; |
| 178 | |
| 179 | // If a comdat is not externally visible we can drop it. |
| 180 | if (auto GO = dyn_cast<GlobalObject>(&GV)) |
| 181 | GO->setComdat(nullptr); |
| 182 | |
| 183 | if (GV.hasLocalLinkage()) |
| 184 | return false; |
| 185 | } else { |
| 186 | if (GV.hasLocalLinkage()) |
| 187 | return false; |
| 188 | |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 189 | if (ShouldPreserveGV(GV)) |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
| 193 | GV.setVisibility(GlobalValue::DefaultVisibility); |
| 194 | GV.setLinkage(GlobalValue::InternalLinkage); |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 195 | return true; |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 198 | // If GV is part of a comdat and is externally visible, keep track of its |
| 199 | // comdat so that we don't internalize any of its members. |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 200 | void Internalizer::checkComdatVisibility( |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 201 | GlobalValue &GV, std::set<const Comdat *> &ExternalComdats) { |
| 202 | Comdat *C = GV.getComdat(); |
| 203 | if (!C) |
| 204 | return; |
| 205 | |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 206 | if (ShouldPreserveGV(GV)) |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 207 | ExternalComdats.insert(C); |
| 208 | } |
| 209 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 210 | bool Internalizer::internalizeModule(Module &M, CallGraph *CG) { |
Mehdi Amini | 59269a8 | 2016-04-13 05:25:16 +0000 | [diff] [blame] | 211 | bool Changed = false; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 212 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 213 | |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 214 | SmallPtrSet<GlobalValue *, 8> Used; |
| 215 | collectUsedGlobalVariables(M, Used, false); |
| 216 | |
Peter Collingbourne | 9b0fe61 | 2015-07-16 17:42:21 +0000 | [diff] [blame] | 217 | // Collect comdat visiblity information for the module. |
| 218 | std::set<const Comdat *> ExternalComdats; |
| 219 | if (!M.getComdatSymbolTable().empty()) { |
| 220 | for (Function &F : M) |
| 221 | checkComdatVisibility(F, ExternalComdats); |
| 222 | for (GlobalVariable &GV : M.globals()) |
| 223 | checkComdatVisibility(GV, ExternalComdats); |
| 224 | for (GlobalAlias &GA : M.aliases()) |
| 225 | checkComdatVisibility(GA, ExternalComdats); |
| 226 | } |
| 227 | |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 228 | // We must assume that globals in llvm.used have a reference that not even |
| 229 | // the linker can see, so we don't internalize them. |
| 230 | // For llvm.compiler.used the situation is a bit fuzzy. The assembler and |
| 231 | // linker can drop those symbols. If this pass is running as part of LTO, |
| 232 | // one might think that it could just drop llvm.compiler.used. The problem |
| 233 | // is that even in LTO llvm doesn't see every reference. For example, |
| 234 | // we don't see references from function local inline assembly. To be |
| 235 | // conservative, we internalize symbols in llvm.compiler.used, but we |
| 236 | // keep llvm.compiler.used so that the symbol is not deleted by llvm. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 237 | for (GlobalValue *V : Used) { |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 238 | AlwaysPreserved.insert(V->getName()); |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 241 | // Mark all functions not in the api as internal. |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 242 | for (Function &I : M) { |
| 243 | if (!maybeInternalize(I, ExternalComdats)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 244 | continue; |
Mehdi Amini | 59269a8 | 2016-04-13 05:25:16 +0000 | [diff] [blame] | 245 | Changed = true; |
Bill Wendling | 2865be7 | 2013-08-30 21:07:33 +0000 | [diff] [blame] | 246 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 247 | if (ExternalNode) |
| 248 | // Remove a callgraph edge from the external node to this function. |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 249 | ExternalNode->removeOneAbstractEdgeTo((*CG)[&I]); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 250 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 251 | ++NumFunctions; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 252 | DEBUG(dbgs() << "Internalizing func " << I.getName() << "\n"); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 253 | } |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 255 | // Never internalize the llvm.used symbol. It is used to implement |
| 256 | // attribute((used)). |
Chris Lattner | 58f9bb2 | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 257 | // FIXME: Shouldn't this just filter on llvm.metadata section?? |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 258 | AlwaysPreserved.insert("llvm.used"); |
| 259 | AlwaysPreserved.insert("llvm.compiler.used"); |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 260 | |
Jim Laskey | c56315c | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 261 | // Never internalize anchors used by the machine module info, else the info |
| 262 | // won't find them. (see MachineModuleInfo.) |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 263 | AlwaysPreserved.insert("llvm.global_ctors"); |
| 264 | AlwaysPreserved.insert("llvm.global_dtors"); |
| 265 | AlwaysPreserved.insert("llvm.global.annotations"); |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 266 | |
Bill Wendling | 585583c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 267 | // Never internalize symbols code-gen inserts. |
Bill Wendling | e1eaecd | 2013-08-12 20:09:37 +0000 | [diff] [blame] | 268 | // FIXME: We should probably add this (and the __stack_chk_guard) via some |
| 269 | // type of call-back in CodeGen. |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 270 | AlwaysPreserved.insert("__stack_chk_fail"); |
| 271 | AlwaysPreserved.insert("__stack_chk_guard"); |
Bill Wendling | 585583c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 272 | |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 273 | // Mark all global variables with initializers that are not in the api as |
| 274 | // internal as well. |
Mehdi Amini | 3949b9e | 2016-04-13 05:25:12 +0000 | [diff] [blame] | 275 | for (auto &GV : M.globals()) { |
| 276 | if (!maybeInternalize(GV, ExternalComdats)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 277 | continue; |
Mehdi Amini | 59269a8 | 2016-04-13 05:25:16 +0000 | [diff] [blame] | 278 | Changed = true; |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 279 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 280 | ++NumGlobals; |
Mehdi Amini | 3949b9e | 2016-04-13 05:25:12 +0000 | [diff] [blame] | 281 | DEBUG(dbgs() << "Internalized gvar " << GV.getName() << "\n"); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 282 | } |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 283 | |
Duncan Sands | 582c53d | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 284 | // Mark all aliases that are not in the api as internal as well. |
Mehdi Amini | 3949b9e | 2016-04-13 05:25:12 +0000 | [diff] [blame] | 285 | for (auto &GA : M.aliases()) { |
| 286 | if (!maybeInternalize(GA, ExternalComdats)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 287 | continue; |
Mehdi Amini | 59269a8 | 2016-04-13 05:25:16 +0000 | [diff] [blame] | 288 | Changed = true; |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 289 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 290 | ++NumAliases; |
Mehdi Amini | 3949b9e | 2016-04-13 05:25:12 +0000 | [diff] [blame] | 291 | DEBUG(dbgs() << "Internalized alias " << GA.getName() << "\n"); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 292 | } |
Duncan Sands | 582c53d | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 293 | |
Mehdi Amini | 59269a8 | 2016-04-13 05:25:16 +0000 | [diff] [blame] | 294 | return Changed; |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Mehdi Amini | 1059383 | 2016-04-13 06:32:29 +0000 | [diff] [blame^] | 297 | } // end anonymous namespace |
| 298 | |
| 299 | char InternalizePass::ID = 0; |
| 300 | INITIALIZE_PASS(InternalizePass, "internalize", "Internalize Global Symbols", |
| 301 | false, false) |
| 302 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 303 | /// Public API below |
| 304 | |
| 305 | bool llvm::internalizeModule( |
| 306 | Module &TheModule, |
| 307 | const std::function<bool(const GlobalValue &)> &MustPreserveGV, |
| 308 | CallGraph *CG) { |
| 309 | return Internalizer(MustPreserveGV).internalizeModule(TheModule, CG); |
Devang Patel | 839d926 | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 310 | } |
Mehdi Amini | c87d7d0 | 2016-02-10 23:24:31 +0000 | [diff] [blame] | 311 | |
Mehdi Amini | 24d3414 | 2016-04-13 05:25:08 +0000 | [diff] [blame] | 312 | ModulePass *llvm::createInternalizePass() { return new InternalizePass(); } |
| 313 | |
Mehdi Amini | 4078709 | 2016-04-13 04:20:32 +0000 | [diff] [blame] | 314 | ModulePass *llvm::createInternalizePass( |
| 315 | std::function<bool(const GlobalValue &)> MustPreserveGV) { |
| 316 | return new InternalizePass(std::move(MustPreserveGV)); |
Mehdi Amini | c87d7d0 | 2016-02-10 23:24:31 +0000 | [diff] [blame] | 317 | } |