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