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 cl::OptionCategory
ExtractCat("llvm-extract Options");
38 // InputFilename - The filename to read from.
39 static cl::opt
<std::string
> InputFilename(cl::Positional
,
40 cl::desc("<input bitcode file>"),
42 cl::value_desc("filename"));
44 static cl::opt
<std::string
> OutputFilename("o",
45 cl::desc("Specify output filename"),
46 cl::value_desc("filename"),
47 cl::init("-"), cl::cat(ExtractCat
));
49 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"),
52 static cl::opt
<bool> DeleteFn("delete",
53 cl::desc("Delete specified Globals from Module"),
57 Recursive("recursive", cl::desc("Recursively extract all called functions"),
60 // ExtractFuncs - The functions to extract from the module.
61 static cl::list
<std::string
>
62 ExtractFuncs("func", cl::desc("Specify function to extract"),
63 cl::ZeroOrMore
, cl::value_desc("function"),
66 // ExtractRegExpFuncs - The functions, matched via regular expression, to
67 // extract from the module.
68 static cl::list
<std::string
>
69 ExtractRegExpFuncs("rfunc",
70 cl::desc("Specify function(s) to extract using a "
71 "regular expression"),
72 cl::ZeroOrMore
, cl::value_desc("rfunction"),
75 // ExtractBlocks - The blocks to extract from the module.
76 static cl::list
<std::string
> ExtractBlocks(
79 "Specify <function, basic block1[;basic block2...]> pairs to extract.\n"
80 "Each pair will create a function.\n"
81 "If multiple basic blocks are specified in one pair,\n"
82 "the first block in the sequence should dominate the rest.\n"
84 " --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
85 " --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
87 cl::ZeroOrMore
, cl::value_desc("function:bb1[;bb2...]"),
90 // ExtractAlias - The alias to extract from the module.
91 static cl::list
<std::string
>
92 ExtractAliases("alias", cl::desc("Specify alias to extract"),
93 cl::ZeroOrMore
, cl::value_desc("alias"),
96 // ExtractRegExpAliases - The aliases, matched via regular expression, to
97 // extract from the module.
98 static cl::list
<std::string
>
99 ExtractRegExpAliases("ralias",
100 cl::desc("Specify alias(es) to extract using a "
101 "regular expression"),
102 cl::ZeroOrMore
, cl::value_desc("ralias"),
103 cl::cat(ExtractCat
));
105 // ExtractGlobals - The globals to extract from the module.
106 static cl::list
<std::string
>
107 ExtractGlobals("glob", cl::desc("Specify global to extract"),
108 cl::ZeroOrMore
, cl::value_desc("global"),
109 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::ZeroOrMore
, cl::value_desc("rglobal"),
118 cl::cat(ExtractCat
));
120 static cl::opt
<bool> OutputAssembly("S",
121 cl::desc("Write output as LLVM assembly"),
122 cl::Hidden
, cl::cat(ExtractCat
));
124 static cl::opt
<bool> PreserveBitcodeUseListOrder(
125 "preserve-bc-uselistorder",
126 cl::desc("Preserve use-list order when writing LLVM bitcode."),
127 cl::init(true), cl::Hidden
, cl::cat(ExtractCat
));
129 static cl::opt
<bool> PreserveAssemblyUseListOrder(
130 "preserve-ll-uselistorder",
131 cl::desc("Preserve use-list order when writing LLVM assembly."),
132 cl::init(false), cl::Hidden
, cl::cat(ExtractCat
));
134 int main(int argc
, char **argv
) {
135 InitLLVM
X(argc
, argv
);
138 cl::HideUnrelatedOptions(ExtractCat
);
139 cl::ParseCommandLineOptions(argc
, argv
, "llvm extractor\n");
141 // Use lazy loading, since we only care about selected global values.
143 std::unique_ptr
<Module
> M
= getLazyIRFileModule(InputFilename
, Err
, Context
);
146 Err
.print(argv
[0], errs());
150 // Use SetVector to avoid duplicates.
151 SetVector
<GlobalValue
*> GVs
;
153 // Figure out which aliases we should extract.
154 for (size_t i
= 0, e
= ExtractAliases
.size(); i
!= e
; ++i
) {
155 GlobalAlias
*GA
= M
->getNamedAlias(ExtractAliases
[i
]);
157 errs() << argv
[0] << ": program doesn't contain alias named '"
158 << ExtractAliases
[i
] << "'!\n";
164 // Extract aliases via regular expression matching.
165 for (size_t i
= 0, e
= ExtractRegExpAliases
.size(); i
!= e
; ++i
) {
167 Regex
RegEx(ExtractRegExpAliases
[i
]);
168 if (!RegEx
.isValid(Error
)) {
169 errs() << argv
[0] << ": '" << ExtractRegExpAliases
[i
] << "' "
170 "invalid regex: " << Error
;
173 for (Module::alias_iterator GA
= M
->alias_begin(), E
= M
->alias_end();
175 if (RegEx
.match(GA
->getName())) {
181 errs() << argv
[0] << ": program doesn't contain global named '"
182 << ExtractRegExpAliases
[i
] << "'!\n";
187 // Figure out which globals we should extract.
188 for (size_t i
= 0, e
= ExtractGlobals
.size(); i
!= e
; ++i
) {
189 GlobalValue
*GV
= M
->getNamedGlobal(ExtractGlobals
[i
]);
191 errs() << argv
[0] << ": program doesn't contain global named '"
192 << ExtractGlobals
[i
] << "'!\n";
198 // Extract globals via regular expression matching.
199 for (size_t i
= 0, e
= ExtractRegExpGlobals
.size(); i
!= e
; ++i
) {
201 Regex
RegEx(ExtractRegExpGlobals
[i
]);
202 if (!RegEx
.isValid(Error
)) {
203 errs() << argv
[0] << ": '" << ExtractRegExpGlobals
[i
] << "' "
204 "invalid regex: " << Error
;
207 for (auto &GV
: M
->globals()) {
208 if (RegEx
.match(GV
.getName())) {
214 errs() << argv
[0] << ": program doesn't contain global named '"
215 << ExtractRegExpGlobals
[i
] << "'!\n";
220 // Figure out which functions we should extract.
221 for (size_t i
= 0, e
= ExtractFuncs
.size(); i
!= e
; ++i
) {
222 GlobalValue
*GV
= M
->getFunction(ExtractFuncs
[i
]);
224 errs() << argv
[0] << ": program doesn't contain function named '"
225 << ExtractFuncs
[i
] << "'!\n";
230 // Extract functions via regular expression matching.
231 for (size_t i
= 0, e
= ExtractRegExpFuncs
.size(); i
!= e
; ++i
) {
233 StringRef RegExStr
= ExtractRegExpFuncs
[i
];
234 Regex
RegEx(RegExStr
);
235 if (!RegEx
.isValid(Error
)) {
236 errs() << argv
[0] << ": '" << ExtractRegExpFuncs
[i
] << "' "
237 "invalid regex: " << Error
;
240 for (Module::iterator F
= M
->begin(), E
= M
->end(); F
!= E
;
242 if (RegEx
.match(F
->getName())) {
248 errs() << argv
[0] << ": program doesn't contain global named '"
249 << ExtractRegExpFuncs
[i
] << "'!\n";
254 // Figure out which BasicBlocks we should extract.
255 SmallVector
<SmallVector
<BasicBlock
*, 16>, 4> GroupOfBBs
;
256 for (StringRef StrPair
: ExtractBlocks
) {
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 // Do not materialize this function.
267 // Get the basic blocks.
268 SmallVector
<BasicBlock
*, 16> BBs
;
269 SmallVector
<StringRef
, 16> BBNames
;
270 BBInfo
.second
.split(BBNames
, ';', /*MaxSplit=*/-1,
271 /*KeepEmpty=*/false);
272 for (StringRef BBName
: BBNames
) {
273 auto Res
= llvm::find_if(*F
, [&](const BasicBlock
&BB
) {
274 return BB
.getName().equals(BBName
);
276 if (Res
== F
->end()) {
277 errs() << argv
[0] << ": function " << F
->getName()
278 << " doesn't contain a basic block named '" << BBInfo
.second
282 BBs
.push_back(&*Res
);
284 GroupOfBBs
.push_back(BBs
);
287 // Use *argv instead of argv[0] to work around a wrong GCC warning.
288 ExitOnError
ExitOnErr(std::string(*argv
) + ": error reading input: ");
291 std::vector
<llvm::Function
*> Workqueue
;
292 for (GlobalValue
*GV
: GVs
) {
293 if (auto *F
= dyn_cast
<Function
>(GV
)) {
294 Workqueue
.push_back(F
);
297 while (!Workqueue
.empty()) {
298 Function
*F
= &*Workqueue
.back();
299 Workqueue
.pop_back();
300 ExitOnErr(F
->materialize());
301 for (auto &BB
: *F
) {
303 CallBase
*CB
= dyn_cast
<CallBase
>(&I
);
306 Function
*CF
= CB
->getCalledFunction();
309 if (CF
->isDeclaration() || GVs
.count(CF
))
312 Workqueue
.push_back(CF
);
318 auto Materialize
= [&](GlobalValue
&GV
) { ExitOnErr(GV
.materialize()); };
320 // Materialize requisite global values.
322 for (size_t i
= 0, e
= GVs
.size(); i
!= e
; ++i
)
323 Materialize(*GVs
[i
]);
325 // Deleting. Materialize every GV that's *not* in GVs.
326 SmallPtrSet
<GlobalValue
*, 8> GVSet(GVs
.begin(), GVs
.end());
328 if (!GVSet
.count(&F
))
334 std::vector
<GlobalValue
*> Gvs(GVs
.begin(), GVs
.end());
335 legacy::PassManager Extract
;
336 Extract
.add(createGVExtractionPass(Gvs
, DeleteFn
));
339 // Now that we have all the GVs we want, mark the module as fully
341 // FIXME: should the GVExtractionPass handle this?
342 ExitOnErr(M
->materializeAll());
345 // Extract the specified basic blocks from the module and erase the existing
347 if (!ExtractBlocks
.empty()) {
348 legacy::PassManager PM
;
349 PM
.add(createBlockExtractorPass(GroupOfBBs
, true));
353 // In addition to deleting all other functions, we also want to spiff it
354 // up a little bit. Do this now.
355 legacy::PassManager Passes
;
358 Passes
.add(createGlobalDCEPass()); // Delete unreachable globals
359 Passes
.add(createStripDeadDebugInfoPass()); // Remove dead debug info
360 Passes
.add(createStripDeadPrototypesPass()); // Remove dead func decls
363 ToolOutputFile
Out(OutputFilename
, EC
, sys::fs::OF_None
);
365 errs() << EC
.message() << '\n';
371 createPrintModulePass(Out
.os(), "", PreserveAssemblyUseListOrder
));
372 else if (Force
|| !CheckBitcodeOutputToConsole(Out
.os(), true))
373 Passes
.add(createBitcodeWriterPass(Out
.os(), PreserveBitcodeUseListOrder
));
375 Passes
.run(*M
.get());