1 //===- llvm-extract.cpp - LLVM function extraction utility ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This utility changes the input module to only contain a single function,
10 // which is primarily used for debugging transformations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/Bitcode/BitcodeWriterPass.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/IRPrintingPasses.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/LegacyPassManager.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IRReader/IRReader.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/InitLLVM.h"
28 #include "llvm/Support/Regex.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/SystemUtils.h"
31 #include "llvm/Support/ToolOutputFile.h"
32 #include "llvm/Transforms/IPO.h"
36 // InputFilename - The filename to read from.
37 static cl::opt
<std::string
>
38 InputFilename(cl::Positional
, cl::desc("<input bitcode file>"),
39 cl::init("-"), cl::value_desc("filename"));
41 static cl::opt
<std::string
>
42 OutputFilename("o", cl::desc("Specify output filename"),
43 cl::value_desc("filename"), cl::init("-"));
46 Force("f", cl::desc("Enable binary output on terminals"));
49 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
52 Recursive("recursive",
53 cl::desc("Recursively extract all called functions"));
55 // ExtractFuncs - The functions to extract from the module.
56 static cl::list
<std::string
>
57 ExtractFuncs("func", cl::desc("Specify function to extract"),
58 cl::ZeroOrMore
, cl::value_desc("function"));
60 // ExtractRegExpFuncs - The functions, matched via regular expression, to
61 // extract from the module.
62 static cl::list
<std::string
>
63 ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
64 "regular expression"),
65 cl::ZeroOrMore
, cl::value_desc("rfunction"));
67 // ExtractBlocks - The blocks to extract from the module.
68 static cl::list
<std::string
>
70 cl::desc("Specify <function, basic block> pairs to extract"),
71 cl::ZeroOrMore
, cl::value_desc("function:bb"));
73 // ExtractAlias - The alias to extract from the module.
74 static cl::list
<std::string
>
75 ExtractAliases("alias", cl::desc("Specify alias to extract"),
76 cl::ZeroOrMore
, cl::value_desc("alias"));
79 // ExtractRegExpAliases - The aliases, matched via regular expression, to
80 // extract from the module.
81 static cl::list
<std::string
>
82 ExtractRegExpAliases("ralias", cl::desc("Specify alias(es) to extract using a "
83 "regular expression"),
84 cl::ZeroOrMore
, cl::value_desc("ralias"));
86 // ExtractGlobals - The globals to extract from the module.
87 static cl::list
<std::string
>
88 ExtractGlobals("glob", cl::desc("Specify global to extract"),
89 cl::ZeroOrMore
, cl::value_desc("global"));
91 // ExtractRegExpGlobals - The globals, matched via regular expression, to
92 // extract from the module...
93 static cl::list
<std::string
>
94 ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
95 "regular expression"),
96 cl::ZeroOrMore
, cl::value_desc("rglobal"));
100 cl::desc("Write output as LLVM assembly"), cl::Hidden
);
102 static cl::opt
<bool> PreserveBitcodeUseListOrder(
103 "preserve-bc-uselistorder",
104 cl::desc("Preserve use-list order when writing LLVM bitcode."),
105 cl::init(true), cl::Hidden
);
107 static cl::opt
<bool> PreserveAssemblyUseListOrder(
108 "preserve-ll-uselistorder",
109 cl::desc("Preserve use-list order when writing LLVM assembly."),
110 cl::init(false), cl::Hidden
);
112 int main(int argc
, char **argv
) {
113 InitLLVM
X(argc
, argv
);
116 cl::ParseCommandLineOptions(argc
, argv
, "llvm extractor\n");
118 // Use lazy loading, since we only care about selected global values.
120 std::unique_ptr
<Module
> M
= getLazyIRFileModule(InputFilename
, Err
, Context
);
123 Err
.print(argv
[0], errs());
127 // Use SetVector to avoid duplicates.
128 SetVector
<GlobalValue
*> GVs
;
130 // Figure out which aliases we should extract.
131 for (size_t i
= 0, e
= ExtractAliases
.size(); i
!= e
; ++i
) {
132 GlobalAlias
*GA
= M
->getNamedAlias(ExtractAliases
[i
]);
134 errs() << argv
[0] << ": program doesn't contain alias named '"
135 << ExtractAliases
[i
] << "'!\n";
141 // Extract aliases via regular expression matching.
142 for (size_t i
= 0, e
= ExtractRegExpAliases
.size(); i
!= e
; ++i
) {
144 Regex
RegEx(ExtractRegExpAliases
[i
]);
145 if (!RegEx
.isValid(Error
)) {
146 errs() << argv
[0] << ": '" << ExtractRegExpAliases
[i
] << "' "
147 "invalid regex: " << Error
;
150 for (Module::alias_iterator GA
= M
->alias_begin(), E
= M
->alias_end();
152 if (RegEx
.match(GA
->getName())) {
158 errs() << argv
[0] << ": program doesn't contain global named '"
159 << ExtractRegExpAliases
[i
] << "'!\n";
164 // Figure out which globals we should extract.
165 for (size_t i
= 0, e
= ExtractGlobals
.size(); i
!= e
; ++i
) {
166 GlobalValue
*GV
= M
->getNamedGlobal(ExtractGlobals
[i
]);
168 errs() << argv
[0] << ": program doesn't contain global named '"
169 << ExtractGlobals
[i
] << "'!\n";
175 // Extract globals via regular expression matching.
176 for (size_t i
= 0, e
= ExtractRegExpGlobals
.size(); i
!= e
; ++i
) {
178 Regex
RegEx(ExtractRegExpGlobals
[i
]);
179 if (!RegEx
.isValid(Error
)) {
180 errs() << argv
[0] << ": '" << ExtractRegExpGlobals
[i
] << "' "
181 "invalid regex: " << Error
;
184 for (auto &GV
: M
->globals()) {
185 if (RegEx
.match(GV
.getName())) {
191 errs() << argv
[0] << ": program doesn't contain global named '"
192 << ExtractRegExpGlobals
[i
] << "'!\n";
197 // Figure out which functions we should extract.
198 for (size_t i
= 0, e
= ExtractFuncs
.size(); i
!= e
; ++i
) {
199 GlobalValue
*GV
= M
->getFunction(ExtractFuncs
[i
]);
201 errs() << argv
[0] << ": program doesn't contain function named '"
202 << ExtractFuncs
[i
] << "'!\n";
207 // Extract functions via regular expression matching.
208 for (size_t i
= 0, e
= ExtractRegExpFuncs
.size(); i
!= e
; ++i
) {
210 StringRef RegExStr
= ExtractRegExpFuncs
[i
];
211 Regex
RegEx(RegExStr
);
212 if (!RegEx
.isValid(Error
)) {
213 errs() << argv
[0] << ": '" << ExtractRegExpFuncs
[i
] << "' "
214 "invalid regex: " << Error
;
217 for (Module::iterator F
= M
->begin(), E
= M
->end(); F
!= E
;
219 if (RegEx
.match(F
->getName())) {
225 errs() << argv
[0] << ": program doesn't contain global named '"
226 << ExtractRegExpFuncs
[i
] << "'!\n";
231 // Figure out which BasicBlocks we should extract.
232 SmallVector
<BasicBlock
*, 4> BBs
;
233 for (StringRef StrPair
: ExtractBlocks
) {
234 auto BBInfo
= StrPair
.split(':');
236 Function
*F
= M
->getFunction(BBInfo
.first
);
238 errs() << argv
[0] << ": program doesn't contain a function named '"
239 << BBInfo
.first
<< "'!\n";
242 // Do not materialize this function.
244 // Get the basic block.
245 auto Res
= llvm::find_if(*F
, [&](const BasicBlock
&BB
) {
246 return BB
.getName().equals(BBInfo
.second
);
248 if (Res
== F
->end()) {
249 errs() << argv
[0] << ": function " << F
->getName()
250 << " doesn't contain a basic block named '" << BBInfo
.second
254 BBs
.push_back(&*Res
);
257 // Use *argv instead of argv[0] to work around a wrong GCC warning.
258 ExitOnError
ExitOnErr(std::string(*argv
) + ": error reading input: ");
261 std::vector
<llvm::Function
*> Workqueue
;
262 for (GlobalValue
*GV
: GVs
) {
263 if (auto *F
= dyn_cast
<Function
>(GV
)) {
264 Workqueue
.push_back(F
);
267 while (!Workqueue
.empty()) {
268 Function
*F
= &*Workqueue
.back();
269 Workqueue
.pop_back();
270 ExitOnErr(F
->materialize());
271 for (auto &BB
: *F
) {
273 auto *CI
= dyn_cast
<CallInst
>(&I
);
276 Function
*CF
= CI
->getCalledFunction();
279 if (CF
->isDeclaration() || GVs
.count(CF
))
282 Workqueue
.push_back(CF
);
288 auto Materialize
= [&](GlobalValue
&GV
) { ExitOnErr(GV
.materialize()); };
290 // Materialize requisite global values.
292 for (size_t i
= 0, e
= GVs
.size(); i
!= e
; ++i
)
293 Materialize(*GVs
[i
]);
295 // Deleting. Materialize every GV that's *not* in GVs.
296 SmallPtrSet
<GlobalValue
*, 8> GVSet(GVs
.begin(), GVs
.end());
298 if (!GVSet
.count(&F
))
304 std::vector
<GlobalValue
*> Gvs(GVs
.begin(), GVs
.end());
305 legacy::PassManager Extract
;
306 Extract
.add(createGVExtractionPass(Gvs
, DeleteFn
));
309 // Now that we have all the GVs we want, mark the module as fully
311 // FIXME: should the GVExtractionPass handle this?
312 ExitOnErr(M
->materializeAll());
315 // Extract the specified basic blocks from the module and erase the existing
317 if (!ExtractBlocks
.empty()) {
318 legacy::PassManager PM
;
319 PM
.add(createBlockExtractorPass(BBs
, true));
323 // In addition to deleting all other functions, we also want to spiff it
324 // up a little bit. Do this now.
325 legacy::PassManager Passes
;
328 Passes
.add(createGlobalDCEPass()); // Delete unreachable globals
329 Passes
.add(createStripDeadDebugInfoPass()); // Remove dead debug info
330 Passes
.add(createStripDeadPrototypesPass()); // Remove dead func decls
333 ToolOutputFile
Out(OutputFilename
, EC
, sys::fs::F_None
);
335 errs() << EC
.message() << '\n';
341 createPrintModulePass(Out
.os(), "", PreserveAssemblyUseListOrder
));
342 else if (Force
|| !CheckBitcodeOutputToConsole(Out
.os(), true))
343 Passes
.add(createBitcodeWriterPass(Out
.os(), PreserveBitcodeUseListOrder
));
345 Passes
.run(*M
.get());