Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 1 | //===-- Internalize.cpp - Mark functions internal -------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 9 | // |
Rafael Espindola | e5551ed | 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 | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 13 | // |
Rafael Espindola | 7e667c5 | 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 | 713cab0 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 16 | // |
Rafael Espindola | 7e667c5 | 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 | 713cab0 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 19 | // |
Chris Lattner | dbb1735 | 2002-04-28 05:43:27 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | 568ddab | 2002-07-24 17:12:05 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Rafael Espindola | 4ef7eaf | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 713cab0 | 2013-10-21 17:14:55 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Rafael Espindola | 4ef7eaf | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 33 | #include <fstream> |
| 34 | #include <set> |
Chris Lattner | 1e2385b | 2003-11-21 21:54:22 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 37 | #define DEBUG_TYPE "internalize" |
| 38 | |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 39 | STATISTIC(NumAliases , "Number of aliases internalized"); |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 40 | STATISTIC(NumFunctions, "Number of functions internalized"); |
| 41 | STATISTIC(NumGlobals , "Number of global vars internalized"); |
| 42 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 43 | // APIFile - A file which contains a list of symbols that should not be marked |
| 44 | // external. |
| 45 | static cl::opt<std::string> |
| 46 | APIFile("internalize-public-api-file", cl::value_desc("filename"), |
| 47 | cl::desc("A file containing list of symbol names to preserve")); |
| 48 | |
| 49 | // APIList - A list of symbols that should not be marked internal. |
| 50 | static cl::list<std::string> |
| 51 | APIList("internalize-public-api-list", cl::value_desc("list"), |
| 52 | cl::desc("A list of symbol names to preserve"), |
| 53 | cl::CommaSeparated); |
| 54 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 55 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 56 | class InternalizePass : public ModulePass { |
Chris Lattner | c7a2c7f | 2003-05-22 19:34:49 +0000 | [diff] [blame] | 57 | std::set<std::string> ExternalNames; |
| 58 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 59 | static char ID; // Pass identification, replacement for typeid |
Rafael Espindola | e5551ed | 2012-10-26 18:47:48 +0000 | [diff] [blame] | 60 | explicit InternalizePass(); |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 61 | explicit InternalizePass(ArrayRef<const char *> ExportList); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 62 | void LoadFile(const char *Filename); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 63 | bool runOnModule(Module &M) override; |
Nuno Lopes | 0483d01 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 64 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 65 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Nuno Lopes | 0483d01 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 66 | AU.setPreservesCFG(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 67 | AU.addPreserved<CallGraphWrapperPass>(); |
Nuno Lopes | 0483d01 | 2008-09-30 22:04:30 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 55e41ba | 2002-07-30 19:48:44 +0000 | [diff] [blame] | 69 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 70 | } // end anonymous namespace |
| 71 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 72 | char InternalizePass::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS(InternalizePass, "internalize", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 74 | "Internalize Global Symbols", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 75 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 76 | InternalizePass::InternalizePass() : ModulePass(ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 77 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 78 | if (!APIFile.empty()) // If a filename is specified, use it. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 79 | LoadFile(APIFile.c_str()); |
Rafael Espindola | 1d7df34 | 2013-09-04 18:53:21 +0000 | [diff] [blame] | 80 | ExternalNames.insert(APIList.begin(), APIList.end()); |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 83 | InternalizePass::InternalizePass(ArrayRef<const char *> ExportList) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 84 | : ModulePass(ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 85 | initializeInternalizePassPass(*PassRegistry::getPassRegistry()); |
Rafael Espindola | 775079c | 2013-09-04 20:08:46 +0000 | [diff] [blame] | 86 | for(ArrayRef<const char *>::const_iterator itr = ExportList.begin(); |
| 87 | itr != ExportList.end(); itr++) { |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 88 | ExternalNames.insert(*itr); |
| 89 | } |
| 90 | } |
| 91 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 92 | void InternalizePass::LoadFile(const char *Filename) { |
| 93 | // Load the APIFile... |
| 94 | std::ifstream In(Filename); |
| 95 | if (!In.good()) { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 96 | errs() << "WARNING: Internalize couldn't load file '" << Filename |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 97 | << "'! Continuing as if it's empty.\n"; |
| 98 | return; // Just continue as if the file were empty |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 99 | } |
| 100 | while (In) { |
| 101 | std::string Symbol; |
| 102 | In >> Symbol; |
| 103 | if (!Symbol.empty()) |
| 104 | ExternalNames.insert(Symbol); |
| 105 | } |
| 106 | } |
| 107 | |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 108 | static bool shouldInternalize(const GlobalValue &GV, |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 109 | const std::set<std::string> &ExternalNames) { |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 110 | // Function must be defined here |
| 111 | if (GV.isDeclaration()) |
| 112 | return false; |
| 113 | |
| 114 | // Available externally is really just a "declaration with a body". |
| 115 | if (GV.hasAvailableExternallyLinkage()) |
| 116 | return false; |
| 117 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 118 | // Assume that dllexported symbols are referenced elsewhere |
| 119 | if (GV.hasDLLExportStorageClass()) |
| 120 | return false; |
| 121 | |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 122 | // Already has internal linkage |
| 123 | if (GV.hasLocalLinkage()) |
| 124 | return false; |
| 125 | |
| 126 | // Marked to keep external? |
| 127 | if (ExternalNames.count(GV.getName())) |
| 128 | return false; |
| 129 | |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 130 | return true; |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 133 | bool InternalizePass::runOnModule(Module &M) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 134 | CallGraphWrapperPass *CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 135 | CallGraph *CG = CGPass ? &CGPass->getCallGraph() : nullptr; |
| 136 | CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr; |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 137 | bool Changed = false; |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 138 | |
Rafael Espindola | 4ef7eaf | 2013-07-25 03:23:25 +0000 | [diff] [blame] | 139 | SmallPtrSet<GlobalValue *, 8> Used; |
| 140 | collectUsedGlobalVariables(M, Used, false); |
| 141 | |
| 142 | // We must assume that globals in llvm.used have a reference that not even |
| 143 | // the linker can see, so we don't internalize them. |
| 144 | // For llvm.compiler.used the situation is a bit fuzzy. The assembler and |
| 145 | // linker can drop those symbols. If this pass is running as part of LTO, |
| 146 | // one might think that it could just drop llvm.compiler.used. The problem |
| 147 | // is that even in LTO llvm doesn't see every reference. For example, |
| 148 | // we don't see references from function local inline assembly. To be |
| 149 | // conservative, we internalize symbols in llvm.compiler.used, but we |
| 150 | // keep llvm.compiler.used so that the symbol is not deleted by llvm. |
| 151 | for (SmallPtrSet<GlobalValue *, 8>::iterator I = Used.begin(), E = Used.end(); |
| 152 | I != E; ++I) { |
| 153 | GlobalValue *V = *I; |
| 154 | ExternalNames.insert(V->getName()); |
| 155 | } |
| 156 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 157 | // Mark all functions not in the api as internal. |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 158 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 159 | if (!shouldInternalize(*I, ExternalNames)) |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 160 | continue; |
Bill Wendling | 86d4956 | 2013-08-30 21:07:33 +0000 | [diff] [blame] | 161 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 162 | I->setVisibility(GlobalValue::DefaultVisibility); |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 163 | I->setLinkage(GlobalValue::InternalLinkage); |
Bill Wendling | 86d4956 | 2013-08-30 21:07:33 +0000 | [diff] [blame] | 164 | |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 165 | if (ExternalNode) |
| 166 | // Remove a callgraph edge from the external node to this function. |
| 167 | ExternalNode->removeOneAbstractEdgeTo((*CG)[I]); |
| 168 | |
| 169 | Changed = true; |
| 170 | ++NumFunctions; |
| 171 | DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n"); |
| 172 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 174 | // Never internalize the llvm.used symbol. It is used to implement |
| 175 | // attribute((used)). |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 176 | // FIXME: Shouldn't this just filter on llvm.metadata section?? |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 177 | ExternalNames.insert("llvm.used"); |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 178 | ExternalNames.insert("llvm.compiler.used"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 179 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 180 | // Never internalize anchors used by the machine module info, else the info |
| 181 | // won't find them. (see MachineModuleInfo.) |
Chris Lattner | 4e2288b | 2007-06-06 20:51:41 +0000 | [diff] [blame] | 182 | ExternalNames.insert("llvm.global_ctors"); |
| 183 | ExternalNames.insert("llvm.global_dtors"); |
Tanya Lattner | 088b591 | 2007-10-03 17:05:40 +0000 | [diff] [blame] | 184 | ExternalNames.insert("llvm.global.annotations"); |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 185 | |
Bill Wendling | aab3c0c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 186 | // Never internalize symbols code-gen inserts. |
Bill Wendling | d275ff5 | 2013-08-12 20:09:37 +0000 | [diff] [blame] | 187 | // FIXME: We should probably add this (and the __stack_chk_guard) via some |
| 188 | // type of call-back in CodeGen. |
| 189 | ExternalNames.insert("__stack_chk_fail"); |
Bill Wendling | aab3c0c | 2012-04-13 01:06:27 +0000 | [diff] [blame] | 190 | ExternalNames.insert("__stack_chk_guard"); |
| 191 | |
Devang Patel | ef3682a | 2008-05-14 20:01:01 +0000 | [diff] [blame] | 192 | // Mark all global variables with initializers that are not in the api as |
| 193 | // internal as well. |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 194 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 195 | I != E; ++I) { |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 196 | if (!shouldInternalize(*I, ExternalNames)) |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 197 | continue; |
| 198 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 199 | I->setVisibility(GlobalValue::DefaultVisibility); |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 200 | I->setLinkage(GlobalValue::InternalLinkage); |
| 201 | Changed = true; |
| 202 | ++NumGlobals; |
| 203 | DEBUG(dbgs() << "Internalized gvar " << I->getName() << "\n"); |
| 204 | } |
Duncan Sands | 2631ac3 | 2009-01-05 20:38:27 +0000 | [diff] [blame] | 205 | |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 206 | // Mark all aliases that are not in the api as internal as well. |
| 207 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 208 | I != E; ++I) { |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 209 | if (!shouldInternalize(*I, ExternalNames)) |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 212 | I->setVisibility(GlobalValue::DefaultVisibility); |
Rafael Espindola | 0fb7716 | 2013-09-04 18:37:36 +0000 | [diff] [blame] | 213 | I->setLinkage(GlobalValue::InternalLinkage); |
| 214 | Changed = true; |
| 215 | ++NumAliases; |
| 216 | DEBUG(dbgs() << "Internalized alias " << I->getName() << "\n"); |
| 217 | } |
Duncan Sands | 27a5300 | 2009-01-05 21:24:45 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 4eb40df | 2006-01-03 19:13:17 +0000 | [diff] [blame] | 219 | return Changed; |
| 220 | } |
| 221 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 222 | ModulePass *llvm::createInternalizePass() { return new InternalizePass(); } |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 223 | |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 224 | ModulePass *llvm::createInternalizePass(ArrayRef<const char *> ExportList) { |
| 225 | return new InternalizePass(ExportList); |
Devang Patel | 753d94a | 2006-07-20 17:48:05 +0000 | [diff] [blame] | 226 | } |