1 //===- llvm-extract.cpp - LLVM function extraction utility ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This utility changes the input module to only contain a single function, 11 // which is primarily used for debugging transformations. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ADT/SetVector.h" 16 #include "llvm/ADT/SmallPtrSet.h" 17 #include "llvm/Bitcode/BitcodeWriterPass.h" 18 #include "llvm/IR/DataLayout.h" 19 #include "llvm/IR/IRPrintingPasses.h" 20 #include "llvm/IR/LLVMContext.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IRReader/IRReader.h" 23 #include "llvm/PassManager.h" 24 #include "llvm/Support/CommandLine.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/ManagedStatic.h" 27 #include "llvm/Support/PrettyStackTrace.h" 28 #include "llvm/Support/Regex.h" 29 #include "llvm/Support/Signals.h" 30 #include "llvm/Support/SourceMgr.h" 31 #include "llvm/Support/SystemUtils.h" 32 #include "llvm/Support/ToolOutputFile.h" 33 #include "llvm/Transforms/IPO.h" 34 #include <memory> 35 using namespace llvm; 36 37 // InputFilename - The filename to read from. 38 static cl::opt<std::string> 39 InputFilename(cl::Positional, cl::desc("<input bitcode file>"), 40 cl::init("-"), cl::value_desc("filename")); 41 42 static cl::opt<std::string> 43 OutputFilename("o", cl::desc("Specify output filename"), 44 cl::value_desc("filename"), cl::init("-")); 45 46 static cl::opt<bool> 47 Force("f", cl::desc("Enable binary output on terminals")); 48 49 static cl::opt<bool> 50 DeleteFn("delete", cl::desc("Delete specified Globals from Module")); 51 52 // ExtractFuncs - The functions to extract from the module. 53 static cl::list<std::string> 54 ExtractFuncs("func", cl::desc("Specify function to extract"), 55 cl::ZeroOrMore, cl::value_desc("function")); 56 57 // ExtractRegExpFuncs - The functions, matched via regular expression, to 58 // extract from the module. 59 static cl::list<std::string> 60 ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a " 61 "regular expression"), 62 cl::ZeroOrMore, cl::value_desc("rfunction")); 63 64 // ExtractAlias - The alias to extract from the module. 65 static cl::list<std::string> 66 ExtractAliases("alias", cl::desc("Specify alias to extract"), 67 cl::ZeroOrMore, cl::value_desc("alias")); 68 69 70 // ExtractRegExpAliases - The aliases, matched via regular expression, to 71 // extract from the module. 72 static cl::list<std::string> 73 ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a " 74 "regular expression"), 75 cl::ZeroOrMore, cl::value_desc("ralias")); 76 77 // ExtractGlobals - The globals to extract from the module. 78 static cl::list<std::string> 79 ExtractGlobals("glob", cl::desc("Specify global to extract"), 80 cl::ZeroOrMore, cl::value_desc("global")); 81 82 // ExtractRegExpGlobals - The globals, matched via regular expression, to 83 // extract from the module... 84 static cl::list<std::string> 85 ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a " 86 "regular expression"), 87 cl::ZeroOrMore, cl::value_desc("rglobal")); 88 89 static cl::opt<bool> 90 OutputAssembly("S", 91 cl::desc("Write output as LLVM assembly"), cl::Hidden); 92 93 int main(int argc, char **argv) { 94 // Print a stack trace if we signal out. 95 sys::PrintStackTraceOnErrorSignal(); 96 PrettyStackTraceProgram X(argc, argv); 97 98 LLVMContext &Context = getGlobalContext(); 99 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 100 cl::ParseCommandLineOptions(argc, argv, "llvm extractor\n"); 101 102 // Use lazy loading, since we only care about selected global values. 103 SMDiagnostic Err; 104 std::unique_ptr<Module> M = getLazyIRFileModule(InputFilename, Err, Context); 105 106 if (!M.get()) { 107 Err.print(argv[0], errs()); 108 return 1; 109 } 110 111 // Use SetVector to avoid duplicates. 112 SetVector<GlobalValue *> GVs; 113 114 // Figure out which aliases we should extract. 115 for (size_t i = 0, e = ExtractAliases.size(); i != e; ++i) { 116 GlobalAlias *GA = M->getNamedAlias(ExtractAliases[i]); 117 if (!GA) { 118 errs() << argv[0] << ": program doesn't contain alias named '" 119 << ExtractAliases[i] << "'!\n"; 120 return 1; 121 } 122 GVs.insert(GA); 123 } 124 125 // Extract aliases via regular expression matching. 126 for (size_t i = 0, e = ExtractRegExpAliases.size(); i != e; ++i) { 127 std::string Error; 128 Regex RegEx(ExtractRegExpAliases[i]); 129 if (!RegEx.isValid(Error)) { 130 errs() << argv[0] << ": '" << ExtractRegExpAliases[i] << "' " 131 "invalid regex: " << Error; 132 } 133 bool match = false; 134 for (Module::alias_iterator GA = M->alias_begin(), E = M->alias_end(); 135 GA != E; GA++) { 136 if (RegEx.match(GA->getName())) { 137 GVs.insert(&*GA); 138 match = true; 139 } 140 } 141 if (!match) { 142 errs() << argv[0] << ": program doesn't contain global named '" 143 << ExtractRegExpAliases[i] << "'!\n"; 144 return 1; 145 } 146 } 147 148 // Figure out which globals we should extract. 149 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) { 150 GlobalValue *GV = M->getNamedGlobal(ExtractGlobals[i]); 151 if (!GV) { 152 errs() << argv[0] << ": program doesn't contain global named '" 153 << ExtractGlobals[i] << "'!\n"; 154 return 1; 155 } 156 GVs.insert(GV); 157 } 158 159 // Extract globals via regular expression matching. 160 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) { 161 std::string Error; 162 Regex RegEx(ExtractRegExpGlobals[i]); 163 if (!RegEx.isValid(Error)) { 164 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' " 165 "invalid regex: " << Error; 166 } 167 bool match = false; 168 for (auto &GV : M->globals()) { 169 if (RegEx.match(GV.getName())) { 170 GVs.insert(&GV); 171 match = true; 172 } 173 } 174 if (!match) { 175 errs() << argv[0] << ": program doesn't contain global named '" 176 << ExtractRegExpGlobals[i] << "'!\n"; 177 return 1; 178 } 179 } 180 181 // Figure out which functions we should extract. 182 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) { 183 GlobalValue *GV = M->getFunction(ExtractFuncs[i]); 184 if (!GV) { 185 errs() << argv[0] << ": program doesn't contain function named '" 186 << ExtractFuncs[i] << "'!\n"; 187 return 1; 188 } 189 GVs.insert(GV); 190 } 191 // Extract functions via regular expression matching. 192 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) { 193 std::string Error; 194 StringRef RegExStr = ExtractRegExpFuncs[i]; 195 Regex RegEx(RegExStr); 196 if (!RegEx.isValid(Error)) { 197 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' " 198 "invalid regex: " << Error; 199 } 200 bool match = false; 201 for (Module::iterator F = M->begin(), E = M->end(); F != E; 202 F++) { 203 if (RegEx.match(F->getName())) { 204 GVs.insert(&*F); 205 match = true; 206 } 207 } 208 if (!match) { 209 errs() << argv[0] << ": program doesn't contain global named '" 210 << ExtractRegExpFuncs[i] << "'!\n"; 211 return 1; 212 } 213 } 214 215 // Materialize requisite global values. 216 if (!DeleteFn) 217 for (size_t i = 0, e = GVs.size(); i != e; ++i) { 218 GlobalValue *GV = GVs[i]; 219 if (GV->isMaterializable()) { 220 std::string ErrInfo; 221 if (GV->Materialize(&ErrInfo)) { 222 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; 223 return 1; 224 } 225 } 226 } 227 else { 228 // Deleting. Materialize every GV that's *not* in GVs. 229 SmallPtrSet<GlobalValue *, 8> GVSet(GVs.begin(), GVs.end()); 230 for (auto &G : M->globals()) { 231 if (!GVSet.count(&G) && G.isMaterializable()) { 232 std::string ErrInfo; 233 if (G.Materialize(&ErrInfo)) { 234 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; 235 return 1; 236 } 237 } 238 } 239 for (auto &F : *M) { 240 if (!GVSet.count(&F) && F.isMaterializable()) { 241 std::string ErrInfo; 242 if (F.Materialize(&ErrInfo)) { 243 errs() << argv[0] << ": error reading input: " << ErrInfo << "\n"; 244 return 1; 245 } 246 } 247 } 248 } 249 250 // In addition to deleting all other functions, we also want to spiff it 251 // up a little bit. Do this now. 252 PassManager Passes; 253 Passes.add(new DataLayoutPass()); // Use correct DataLayout 254 255 std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end()); 256 257 Passes.add(createGVExtractionPass(Gvs, DeleteFn)); 258 if (!DeleteFn) 259 Passes.add(createGlobalDCEPass()); // Delete unreachable globals 260 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info 261 Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls 262 263 std::error_code EC; 264 tool_output_file Out(OutputFilename, EC, sys::fs::F_None); 265 if (EC) { 266 errs() << EC.message() << '\n'; 267 return 1; 268 } 269 270 if (OutputAssembly) 271 Passes.add(createPrintModulePass(Out.os())); 272 else if (Force || !CheckBitcodeOutputToConsole(Out.os(), true)) 273 Passes.add(createBitcodeWriterPass(Out.os())); 274 275 Passes.run(*M.get()); 276 277 // Declare success. 278 Out.keep(); 279 280 return 0; 281 } 282