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/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/InitLLVM.h"
29 #include "llvm/Support/Regex.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"
38 cl::OptionCategory
ExtractCat("llvm-extract Options");
40 // InputFilename - The filename to read from.
41 static cl::opt
<std::string
> InputFilename(cl::Positional
,
42 cl::desc("<input bitcode file>"),
44 cl::value_desc("filename"));
46 static cl::opt
<std::string
> OutputFilename("o",
47 cl::desc("Specify output filename"),
48 cl::value_desc("filename"),
49 cl::init("-"), cl::cat(ExtractCat
));
51 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"),
54 static cl::opt
<bool> DeleteFn("delete",
55 cl::desc("Delete specified Globals from Module"),
58 static cl::opt
<bool> KeepConstInit("keep-const-init",
59 cl::desc("Keep initializers of constants"),
63 Recursive("recursive", cl::desc("Recursively extract all called functions"),
66 // ExtractFuncs - The functions to extract from the module.
67 static cl::list
<std::string
>
68 ExtractFuncs("func", cl::desc("Specify function to extract"),
69 cl::value_desc("function"), cl::cat(ExtractCat
));
71 // ExtractRegExpFuncs - The functions, matched via regular expression, to
72 // extract from the module.
73 static cl::list
<std::string
>
74 ExtractRegExpFuncs("rfunc",
75 cl::desc("Specify function(s) to extract using a "
76 "regular expression"),
77 cl::value_desc("rfunction"), cl::cat(ExtractCat
));
79 // ExtractBlocks - The blocks to extract from the module.
80 static cl::list
<std::string
> ExtractBlocks(
83 "Specify <function, basic block1[;basic block2...]> pairs to extract.\n"
84 "Each pair will create a function.\n"
85 "If multiple basic blocks are specified in one pair,\n"
86 "the first block in the sequence should dominate the rest.\n"
88 " --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
89 " --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
91 cl::value_desc("function:bb1[;bb2...]"), cl::cat(ExtractCat
));
93 // ExtractAlias - The alias to extract from the module.
94 static cl::list
<std::string
>
95 ExtractAliases("alias", cl::desc("Specify alias to extract"),
96 cl::value_desc("alias"), cl::cat(ExtractCat
));
98 // ExtractRegExpAliases - The aliases, matched via regular expression, to
99 // extract from the module.
100 static cl::list
<std::string
>
101 ExtractRegExpAliases("ralias",
102 cl::desc("Specify alias(es) to extract using a "
103 "regular expression"),
104 cl::value_desc("ralias"), cl::cat(ExtractCat
));
106 // ExtractGlobals - The globals to extract from the module.
107 static cl::list
<std::string
>
108 ExtractGlobals("glob", cl::desc("Specify global to extract"),
109 cl::value_desc("global"), cl::cat(ExtractCat
));
111 // ExtractRegExpGlobals - The globals, matched via regular expression, to
112 // extract from the module...
113 static cl::list
<std::string
>
114 ExtractRegExpGlobals("rglob",
115 cl::desc("Specify global(s) to extract using a "
116 "regular expression"),
117 cl::value_desc("rglobal"), cl::cat(ExtractCat
));
119 static cl::opt
<bool> OutputAssembly("S",
120 cl::desc("Write output as LLVM assembly"),
121 cl::Hidden
, cl::cat(ExtractCat
));
123 static cl::opt
<bool> PreserveBitcodeUseListOrder(
124 "preserve-bc-uselistorder",
125 cl::desc("Preserve use-list order when writing LLVM bitcode."),
126 cl::init(true), cl::Hidden
, cl::cat(ExtractCat
));
128 static cl::opt
<bool> PreserveAssemblyUseListOrder(
129 "preserve-ll-uselistorder",
130 cl::desc("Preserve use-list order when writing LLVM assembly."),
131 cl::init(false), cl::Hidden
, cl::cat(ExtractCat
));
133 int main(int argc
, char **argv
) {
134 InitLLVM
X(argc
, argv
);
137 cl::HideUnrelatedOptions(ExtractCat
);
138 cl::ParseCommandLineOptions(argc
, argv
, "llvm extractor\n");
140 // Use lazy loading, since we only care about selected global values.
142 std::unique_ptr
<Module
> M
= getLazyIRFileModule(InputFilename
, Err
, Context
);
145 Err
.print(argv
[0], errs());
149 // Use SetVector to avoid duplicates.
150 SetVector
<GlobalValue
*> GVs
;
152 // Figure out which aliases we should extract.
153 for (size_t i
= 0, e
= ExtractAliases
.size(); i
!= e
; ++i
) {
154 GlobalAlias
*GA
= M
->getNamedAlias(ExtractAliases
[i
]);
156 errs() << argv
[0] << ": program doesn't contain alias named '"
157 << ExtractAliases
[i
] << "'!\n";
163 // Extract aliases via regular expression matching.
164 for (size_t i
= 0, e
= ExtractRegExpAliases
.size(); i
!= e
; ++i
) {
166 Regex
RegEx(ExtractRegExpAliases
[i
]);
167 if (!RegEx
.isValid(Error
)) {
168 errs() << argv
[0] << ": '" << ExtractRegExpAliases
[i
] << "' "
169 "invalid regex: " << Error
;
172 for (Module::alias_iterator GA
= M
->alias_begin(), E
= M
->alias_end();
174 if (RegEx
.match(GA
->getName())) {
180 errs() << argv
[0] << ": program doesn't contain global named '"
181 << ExtractRegExpAliases
[i
] << "'!\n";
186 // Figure out which globals we should extract.
187 for (size_t i
= 0, e
= ExtractGlobals
.size(); i
!= e
; ++i
) {
188 GlobalValue
*GV
= M
->getNamedGlobal(ExtractGlobals
[i
]);
190 errs() << argv
[0] << ": program doesn't contain global named '"
191 << ExtractGlobals
[i
] << "'!\n";
197 // Extract globals via regular expression matching.
198 for (size_t i
= 0, e
= ExtractRegExpGlobals
.size(); i
!= e
; ++i
) {
200 Regex
RegEx(ExtractRegExpGlobals
[i
]);
201 if (!RegEx
.isValid(Error
)) {
202 errs() << argv
[0] << ": '" << ExtractRegExpGlobals
[i
] << "' "
203 "invalid regex: " << Error
;
206 for (auto &GV
: M
->globals()) {
207 if (RegEx
.match(GV
.getName())) {
213 errs() << argv
[0] << ": program doesn't contain global named '"
214 << ExtractRegExpGlobals
[i
] << "'!\n";
219 // Figure out which functions we should extract.
220 for (size_t i
= 0, e
= ExtractFuncs
.size(); i
!= e
; ++i
) {
221 GlobalValue
*GV
= M
->getFunction(ExtractFuncs
[i
]);
223 errs() << argv
[0] << ": program doesn't contain function named '"
224 << ExtractFuncs
[i
] << "'!\n";
229 // Extract functions via regular expression matching.
230 for (size_t i
= 0, e
= ExtractRegExpFuncs
.size(); i
!= e
; ++i
) {
232 StringRef RegExStr
= ExtractRegExpFuncs
[i
];
233 Regex
RegEx(RegExStr
);
234 if (!RegEx
.isValid(Error
)) {
235 errs() << argv
[0] << ": '" << ExtractRegExpFuncs
[i
] << "' "
236 "invalid regex: " << Error
;
239 for (Module::iterator F
= M
->begin(), E
= M
->end(); F
!= E
;
241 if (RegEx
.match(F
->getName())) {
247 errs() << argv
[0] << ": program doesn't contain global named '"
248 << ExtractRegExpFuncs
[i
] << "'!\n";
253 // Figure out which BasicBlocks we should extract.
254 SmallVector
<std::pair
<Function
*, SmallVector
<StringRef
, 16>>, 2> BBMap
;
255 for (StringRef StrPair
: ExtractBlocks
) {
256 SmallVector
<StringRef
, 16> BBNames
;
257 auto BBInfo
= StrPair
.split(':');
259 Function
*F
= M
->getFunction(BBInfo
.first
);
261 errs() << argv
[0] << ": program doesn't contain a function named '"
262 << BBInfo
.first
<< "'!\n";
265 // Add the function to the materialize list, and store the basic block names
266 // to check after materialization.
268 BBInfo
.second
.split(BBNames
, ';', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
269 BBMap
.push_back({F
, std::move(BBNames
)});
272 // Use *argv instead of argv[0] to work around a wrong GCC warning.
273 ExitOnError
ExitOnErr(std::string(*argv
) + ": error reading input: ");
276 std::vector
<llvm::Function
*> Workqueue
;
277 for (GlobalValue
*GV
: GVs
) {
278 if (auto *F
= dyn_cast
<Function
>(GV
)) {
279 Workqueue
.push_back(F
);
282 while (!Workqueue
.empty()) {
283 Function
*F
= &*Workqueue
.back();
284 Workqueue
.pop_back();
285 ExitOnErr(F
->materialize());
286 for (auto &BB
: *F
) {
288 CallBase
*CB
= dyn_cast
<CallBase
>(&I
);
291 Function
*CF
= CB
->getCalledFunction();
294 if (CF
->isDeclaration() || GVs
.count(CF
))
297 Workqueue
.push_back(CF
);
303 auto Materialize
= [&](GlobalValue
&GV
) { ExitOnErr(GV
.materialize()); };
305 // Materialize requisite global values.
307 for (size_t i
= 0, e
= GVs
.size(); i
!= e
; ++i
)
308 Materialize(*GVs
[i
]);
310 // Deleting. Materialize every GV that's *not* in GVs.
311 SmallPtrSet
<GlobalValue
*, 8> GVSet(GVs
.begin(), GVs
.end());
313 if (!GVSet
.count(&F
))
319 std::vector
<GlobalValue
*> Gvs(GVs
.begin(), GVs
.end());
320 legacy::PassManager Extract
;
321 Extract
.add(createGVExtractionPass(Gvs
, DeleteFn
, KeepConstInit
));
324 // Now that we have all the GVs we want, mark the module as fully
326 // FIXME: should the GVExtractionPass handle this?
327 ExitOnErr(M
->materializeAll());
330 // Extract the specified basic blocks from the module and erase the existing
332 if (!ExtractBlocks
.empty()) {
333 // Figure out which BasicBlocks we should extract.
334 SmallVector
<SmallVector
<BasicBlock
*, 16>, 4> GroupOfBBs
;
335 for (auto &P
: BBMap
) {
336 SmallVector
<BasicBlock
*, 16> BBs
;
337 for (StringRef BBName
: P
.second
) {
338 // The function has been materialized, so add its matching basic blocks
339 // to the block extractor list, or fail if a name is not found.
340 auto Res
= llvm::find_if(*P
.first
, [&](const BasicBlock
&BB
) {
341 return BB
.getName().equals(BBName
);
343 if (Res
== P
.first
->end()) {
344 errs() << argv
[0] << ": function " << P
.first
->getName()
345 << " doesn't contain a basic block named '" << BBName
349 BBs
.push_back(&*Res
);
351 GroupOfBBs
.push_back(BBs
);
354 legacy::PassManager PM
;
355 PM
.add(createBlockExtractorPass(GroupOfBBs
, true));
359 // In addition to deleting all other functions, we also want to spiff it
360 // up a little bit. Do this now.
361 legacy::PassManager Passes
;
364 Passes
.add(createGlobalDCEPass()); // Delete unreachable globals
365 Passes
.add(createStripDeadDebugInfoPass()); // Remove dead debug info
366 Passes
.add(createStripDeadPrototypesPass()); // Remove dead func decls
369 ToolOutputFile
Out(OutputFilename
, EC
, sys::fs::OF_None
);
371 errs() << EC
.message() << '\n';
377 createPrintModulePass(Out
.os(), "", PreserveAssemblyUseListOrder
));
378 else if (Force
|| !CheckBitcodeOutputToConsole(Out
.os()))
379 Passes
.add(createBitcodeWriterPass(Out
.os(), PreserveBitcodeUseListOrder
));
381 Passes
.run(*M
.get());