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 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "internalize" |
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" |
| 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" |
Rafael Espindola | 3d7fc25 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/ModuleUtils.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 | |
Duncan Sands | 582c53d | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 38 | STATISTIC(NumAliases , "Number of aliases internalized"); |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 39 | STATISTIC(NumFunctions, "Number of functions internalized"); |
| 40 | STATISTIC(NumGlobals , "Number of global vars internalized"); |
| 41 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | // APIFile - A file which contains a list of symbols that should not be marked |
| 43 | // external. |
| 44 | static cl::opt<std::string> |
| 45 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
| 46 | cl::desc("A file containing list of symbol names to preserve")); |
| 47 | |
| 48 | // APIList - A list of symbols that should not be marked internal. |
| 49 | static cl::list<std::string> |
| 50 | APIList("internalize-public-api-list", cl::value_desc("list"), |
| 51 | cl::desc("A list of symbol names to preserve"), |
| 52 | cl::CommaSeparated); |
| 53 | |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 54 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 55 | class InternalizePass : public ModulePass { |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 56 | std::set<std::string> ExternalNames; |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 57 | bool OnlyHidden; |
Chris Lattner | 44457bb | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 58 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 59 | static char ID; // Pass identification, replacement for typeid |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 60 | explicit InternalizePass(bool OnlyHidden = false); |
| 61 | explicit InternalizePass(ArrayRef<const char *> ExportList, bool OnlyHidden); |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 62 | void LoadFile(const char *Filename); |
| 63 | virtual bool runOnModule(Module &M); |
Nuno Lopes | 5093ab4 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 64 | |
| 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 66 | AU.setPreservesCFG(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 67 | AU.addPreserved<CallGraphWrapperPass>(); |
Nuno Lopes | 5093ab4 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 47cc366 | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 69 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 70 | } // end anonymous namespace |
| 71 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 72 | char InternalizePass::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS(InternalizePass, "internalize", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 74 | "Internalize Global Symbols", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 75 | |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 76 | InternalizePass::InternalizePass(bool OnlyHidden) |
| 77 | : ModulePass(ID), OnlyHidden(OnlyHidden) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 78 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 79 | if (!APIFile.empty()) // If a filename is specified, use it. |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 80 | LoadFile(APIFile.c_str()); |
Rafael Espindola | b832d49 | 2013-09-04 18:53:21 +0000 | [diff] [blame] | 81 | ExternalNames.insert(APIList.begin(), APIList.end()); |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 84 | InternalizePass::InternalizePass(ArrayRef<const char *> ExportList, |
| 85 | bool OnlyHidden) |
| 86 | : ModulePass(ID), OnlyHidden(OnlyHidden) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 87 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Rafael Espindola | b7c0b4a | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 88 | for(ArrayRef<const char *>::const_iterator itr = ExportList.begin(); |
| 89 | itr != ExportList.end(); itr++) { |
Devang Patel | 839d926 | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 90 | ExternalNames.insert(*itr); |
| 91 | } |
| 92 | } |
| 93 | |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 94 | void InternalizePass::LoadFile(const char *Filename) { |
| 95 | // Load the APIFile... |
| 96 | std::ifstream In(Filename); |
| 97 | if (!In.good()) { |
Chris Lattner | 317dbbc | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 98 | errs() << "WARNING: Internalize couldn't load file '" << Filename |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 99 | << "'! Continuing as if it's empty.\n"; |
| 100 | return; // Just continue as if the file were empty |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 101 | } |
| 102 | while (In) { |
| 103 | std::string Symbol; |
| 104 | In >> Symbol; |
| 105 | if (!Symbol.empty()) |
| 106 | ExternalNames.insert(Symbol); |
| 107 | } |
| 108 | } |
| 109 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 110 | static bool shouldInternalize(const GlobalValue &GV, |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 111 | const std::set<std::string> &ExternalNames, |
| 112 | bool OnlyHidden) { |
| 113 | if (OnlyHidden && !GV.hasHiddenVisibility()) |
| 114 | return false; |
| 115 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 116 | // Function must be defined here |
| 117 | if (GV.isDeclaration()) |
| 118 | return false; |
| 119 | |
| 120 | // Available externally is really just a "declaration with a body". |
| 121 | if (GV.hasAvailableExternallyLinkage()) |
| 122 | return false; |
| 123 | |
Yunzhong Gao | 9163e8b | 2013-12-03 18:05:14 +0000 | [diff] [blame] | 124 | // Assume that dllexported symbols are referenced elsewhere |
Nico Rieck | 7157bb7 | 2014-01-14 15:22:47 +0000 | [diff] [blame] | 125 | if (GV.hasDLLExportStorageClass()) |
Yunzhong Gao | 9163e8b | 2013-12-03 18:05:14 +0000 | [diff] [blame] | 126 | return false; |
| 127 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 128 | // Already has internal linkage |
| 129 | if (GV.hasLocalLinkage()) |
| 130 | return false; |
| 131 | |
| 132 | // Marked to keep external? |
| 133 | if (ExternalNames.count(GV.getName())) |
| 134 | return false; |
| 135 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 136 | return true; |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 139 | bool InternalizePass::runOnModule(Module &M) { |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 140 | CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>(); |
| 141 | CallGraph *CG = CGPass ? &CGPass->getCallGraph() : 0; |
Duncan Sands | 3a813a5 | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 142 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0; |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 143 | bool Changed = false; |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 144 | |
Rafael Espindola | 17600e2 | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 145 | SmallPtrSet<GlobalValue *, 8> Used; |
| 146 | collectUsedGlobalVariables(M, Used, false); |
| 147 | |
| 148 | // We must assume that globals in llvm.used have a reference that not even |
| 149 | // the linker can see, so we don't internalize them. |
| 150 | // For llvm.compiler.used the situation is a bit fuzzy. The assembler and |
| 151 | // linker can drop those symbols. If this pass is running as part of LTO, |
| 152 | // one might think that it could just drop llvm.compiler.used. The problem |
| 153 | // is that even in LTO llvm doesn't see every reference. For example, |
| 154 | // we don't see references from function local inline assembly. To be |
| 155 | // conservative, we internalize symbols in llvm.compiler.used, but we |
| 156 | // keep llvm.compiler.used so that the symbol is not deleted by llvm. |
| 157 | for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end(); |
| 158 | I != E; ++I) { |
| 159 | GlobalValue *V = *I; |
| 160 | ExternalNames.insert(V->getName()); |
| 161 | } |
| 162 | |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 163 | // Mark all functions not in the api as internal. |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 164 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 165 | if (!shouldInternalize(*I, ExternalNames, OnlyHidden)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 166 | continue; |
Bill Wendling | 2865be7 | 2013-08-30 21:07:33 +0000 | [diff] [blame] | 167 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 168 | I->setLinkage(GlobalValue::InternalLinkage); |
Bill Wendling | 2865be7 | 2013-08-30 21:07:33 +0000 | [diff] [blame] | 169 | |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 170 | if (ExternalNode) |
| 171 | // Remove a callgraph edge from the external node to this function. |
| 172 | ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
| 173 | |
| 174 | Changed = true; |
| 175 | ++NumFunctions; |
| 176 | DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n"); |
| 177 | } |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 179 | // Never internalize the llvm.used symbol. It is used to implement |
| 180 | // attribute((used)). |
Chris Lattner | 58f9bb2 | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 181 | // FIXME: Shouldn't this just filter on llvm.metadata section?? |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 182 | ExternalNames.insert("llvm.used"); |
Chris Lattner | 58f9bb2 | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 183 | ExternalNames.insert("llvm.compiler.used"); |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 184 | |
Jim Laskey | c56315c | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 185 | // Never internalize anchors used by the machine module info, else the info |
| 186 | // won't find them. (see MachineModuleInfo.) |
Chris Lattner | 34404e3 | 2007-06-06 20:51:41 +0000 | [diff] [blame] | 187 | ExternalNames.insert("llvm.global_ctors"); |
| 188 | ExternalNames.insert("llvm.global_dtors"); |
Tanya Lattner | 30f65fe | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 189 | ExternalNames.insert("llvm.global.annotations"); |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 190 | |
Bill Wendling | 585583c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 191 | // Never internalize symbols code-gen inserts. |
Bill Wendling | e1eaecd | 2013-08-12 20:09:37 +0000 | [diff] [blame] | 192 | // FIXME: We should probably add this (and the __stack_chk_guard) via some |
| 193 | // type of call-back in CodeGen. |
| 194 | ExternalNames.insert("__stack_chk_fail"); |
Bill Wendling | 585583c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 195 | ExternalNames.insert("__stack_chk_guard"); |
| 196 | |
Devang Patel | f2763e2 | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 197 | // Mark all global variables with initializers that are not in the api as |
| 198 | // internal as well. |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 199 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 200 | I != E; ++I) { |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 201 | if (!shouldInternalize(*I, ExternalNames, OnlyHidden)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 202 | continue; |
| 203 | |
| 204 | I->setLinkage(GlobalValue::InternalLinkage); |
| 205 | Changed = true; |
| 206 | ++NumGlobals; |
| 207 | DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n"); |
| 208 | } |
Duncan Sands | d24b93f | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 209 | |
Duncan Sands | 582c53d | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 210 | // Mark all aliases that are not in the api as internal as well. |
| 211 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 212 | I != E; ++I) { |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 213 | if (!shouldInternalize(*I, ExternalNames, OnlyHidden)) |
Rafael Espindola | 49a6c15 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 214 | continue; |
| 215 | |
| 216 | I->setLinkage(GlobalValue::InternalLinkage); |
| 217 | Changed = true; |
| 218 | ++NumAliases; |
| 219 | DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n"); |
| 220 | } |
Duncan Sands | 582c53d | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 8cdc773 | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 222 | return Changed; |
| 223 | } |
| 224 | |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 225 | ModulePass *llvm::createInternalizePass(bool OnlyHidden) { |
| 226 | return new InternalizePass(OnlyHidden); |
Chris Lattner | 1b94c00 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 227 | } |
Devang Patel | 839d926 | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 228 | |
Duncan P. N. Exon Smith | 93be7c4 | 2014-01-14 18:52:17 +0000 | [diff] [blame^] | 229 | ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList, |
| 230 | bool OnlyHidden) { |
| 231 | return new InternalizePass(ExportList, OnlyHidden); |
| 232 | } |
| 233 | |
| 234 | ModulePass *llvm::createInternalizePass(const char *SingleExport) { |
| 235 | return createInternalizePass(ArrayRef<const char *>(SingleExport)); |
Devang Patel | 839d926 | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 236 | } |