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/Module.h"
22 #include "llvm/IRPrinter/IRPrintingPasses.h"
23 #include "llvm/IRReader/IRReader.h"
24 #include "llvm/Passes/PassBuilder.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"
34 #include "llvm/Transforms/IPO/BlockExtractor.h"
35 #include "llvm/Transforms/IPO/ExtractGV.h"
36 #include "llvm/Transforms/IPO/GlobalDCE.h"
37 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
38 #include "llvm/Transforms/IPO/StripSymbols.h"
44 cl::OptionCategory
ExtractCat("llvm-extract Options");
46 // InputFilename - The filename to read from.
47 static cl::opt
<std::string
> InputFilename(cl::Positional
,
48 cl::desc("<input bitcode file>"),
50 cl::value_desc("filename"));
52 static cl::opt
<std::string
> OutputFilename("o",
53 cl::desc("Specify output filename"),
54 cl::value_desc("filename"),
55 cl::init("-"), cl::cat(ExtractCat
));
57 static cl::opt
<bool> Force("f", cl::desc("Enable binary output on terminals"),
60 static cl::opt
<bool> DeleteFn("delete",
61 cl::desc("Delete specified Globals from Module"),
64 static cl::opt
<bool> KeepConstInit("keep-const-init",
65 cl::desc("Keep initializers of constants"),
69 Recursive("recursive", cl::desc("Recursively extract all called functions"),
72 // ExtractFuncs - The functions to extract from the module.
73 static cl::list
<std::string
>
74 ExtractFuncs("func", cl::desc("Specify function to extract"),
75 cl::value_desc("function"), cl::cat(ExtractCat
));
77 // ExtractRegExpFuncs - The functions, matched via regular expression, to
78 // extract from the module.
79 static cl::list
<std::string
>
80 ExtractRegExpFuncs("rfunc",
81 cl::desc("Specify function(s) to extract using a "
82 "regular expression"),
83 cl::value_desc("rfunction"), cl::cat(ExtractCat
));
85 // ExtractBlocks - The blocks to extract from the module.
86 static cl::list
<std::string
> ExtractBlocks(
89 "Specify <function, basic block1[;basic block2...]> pairs to extract.\n"
90 "Each pair will create a function.\n"
91 "If multiple basic blocks are specified in one pair,\n"
92 "the first block in the sequence should dominate the rest.\n"
94 " --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
95 " --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
97 cl::value_desc("function:bb1[;bb2...]"), cl::cat(ExtractCat
));
99 // ExtractAlias - The alias to extract from the module.
100 static cl::list
<std::string
>
101 ExtractAliases("alias", cl::desc("Specify alias to extract"),
102 cl::value_desc("alias"), cl::cat(ExtractCat
));
104 // ExtractRegExpAliases - The aliases, matched via regular expression, to
105 // extract from the module.
106 static cl::list
<std::string
>
107 ExtractRegExpAliases("ralias",
108 cl::desc("Specify alias(es) to extract using a "
109 "regular expression"),
110 cl::value_desc("ralias"), cl::cat(ExtractCat
));
112 // ExtractGlobals - The globals to extract from the module.
113 static cl::list
<std::string
>
114 ExtractGlobals("glob", cl::desc("Specify global to extract"),
115 cl::value_desc("global"), cl::cat(ExtractCat
));
117 // ExtractRegExpGlobals - The globals, matched via regular expression, to
118 // extract from the module...
119 static cl::list
<std::string
>
120 ExtractRegExpGlobals("rglob",
121 cl::desc("Specify global(s) to extract using a "
122 "regular expression"),
123 cl::value_desc("rglobal"), cl::cat(ExtractCat
));
125 static cl::opt
<bool> OutputAssembly("S",
126 cl::desc("Write output as LLVM assembly"),
127 cl::Hidden
, cl::cat(ExtractCat
));
129 static cl::opt
<bool> PreserveBitcodeUseListOrder(
130 "preserve-bc-uselistorder",
131 cl::desc("Preserve use-list order when writing LLVM bitcode."),
132 cl::init(true), cl::Hidden
, cl::cat(ExtractCat
));
134 static cl::opt
<bool> PreserveAssemblyUseListOrder(
135 "preserve-ll-uselistorder",
136 cl::desc("Preserve use-list order when writing LLVM assembly."),
137 cl::init(false), cl::Hidden
, cl::cat(ExtractCat
));
139 int main(int argc
, char **argv
) {
140 InitLLVM
X(argc
, argv
);
143 cl::HideUnrelatedOptions(ExtractCat
);
144 cl::ParseCommandLineOptions(argc
, argv
, "llvm extractor\n");
146 // Use lazy loading, since we only care about selected global values.
148 std::unique_ptr
<Module
> M
= getLazyIRFileModule(InputFilename
, Err
, Context
);
151 Err
.print(argv
[0], errs());
155 // Use SetVector to avoid duplicates.
156 SetVector
<GlobalValue
*> GVs
;
158 // Figure out which aliases we should extract.
159 for (size_t i
= 0, e
= ExtractAliases
.size(); i
!= e
; ++i
) {
160 GlobalAlias
*GA
= M
->getNamedAlias(ExtractAliases
[i
]);
162 errs() << argv
[0] << ": program doesn't contain alias named '"
163 << ExtractAliases
[i
] << "'!\n";
169 // Extract aliases via regular expression matching.
170 for (size_t i
= 0, e
= ExtractRegExpAliases
.size(); i
!= e
; ++i
) {
172 Regex
RegEx(ExtractRegExpAliases
[i
]);
173 if (!RegEx
.isValid(Error
)) {
174 errs() << argv
[0] << ": '" << ExtractRegExpAliases
[i
] << "' "
175 "invalid regex: " << Error
;
178 for (Module::alias_iterator GA
= M
->alias_begin(), E
= M
->alias_end();
180 if (RegEx
.match(GA
->getName())) {
186 errs() << argv
[0] << ": program doesn't contain global named '"
187 << ExtractRegExpAliases
[i
] << "'!\n";
192 // Figure out which globals we should extract.
193 for (size_t i
= 0, e
= ExtractGlobals
.size(); i
!= e
; ++i
) {
194 GlobalValue
*GV
= M
->getNamedGlobal(ExtractGlobals
[i
]);
196 errs() << argv
[0] << ": program doesn't contain global named '"
197 << ExtractGlobals
[i
] << "'!\n";
203 // Extract globals via regular expression matching.
204 for (size_t i
= 0, e
= ExtractRegExpGlobals
.size(); i
!= e
; ++i
) {
206 Regex
RegEx(ExtractRegExpGlobals
[i
]);
207 if (!RegEx
.isValid(Error
)) {
208 errs() << argv
[0] << ": '" << ExtractRegExpGlobals
[i
] << "' "
209 "invalid regex: " << Error
;
212 for (auto &GV
: M
->globals()) {
213 if (RegEx
.match(GV
.getName())) {
219 errs() << argv
[0] << ": program doesn't contain global named '"
220 << ExtractRegExpGlobals
[i
] << "'!\n";
225 // Figure out which functions we should extract.
226 for (size_t i
= 0, e
= ExtractFuncs
.size(); i
!= e
; ++i
) {
227 GlobalValue
*GV
= M
->getFunction(ExtractFuncs
[i
]);
229 errs() << argv
[0] << ": program doesn't contain function named '"
230 << ExtractFuncs
[i
] << "'!\n";
235 // Extract functions via regular expression matching.
236 for (size_t i
= 0, e
= ExtractRegExpFuncs
.size(); i
!= e
; ++i
) {
238 StringRef RegExStr
= ExtractRegExpFuncs
[i
];
239 Regex
RegEx(RegExStr
);
240 if (!RegEx
.isValid(Error
)) {
241 errs() << argv
[0] << ": '" << ExtractRegExpFuncs
[i
] << "' "
242 "invalid regex: " << Error
;
245 for (Module::iterator F
= M
->begin(), E
= M
->end(); F
!= E
;
247 if (RegEx
.match(F
->getName())) {
253 errs() << argv
[0] << ": program doesn't contain global named '"
254 << ExtractRegExpFuncs
[i
] << "'!\n";
259 // Figure out which BasicBlocks we should extract.
260 SmallVector
<std::pair
<Function
*, SmallVector
<StringRef
, 16>>, 2> BBMap
;
261 for (StringRef StrPair
: ExtractBlocks
) {
262 SmallVector
<StringRef
, 16> BBNames
;
263 auto BBInfo
= StrPair
.split(':');
265 Function
*F
= M
->getFunction(BBInfo
.first
);
267 errs() << argv
[0] << ": program doesn't contain a function named '"
268 << BBInfo
.first
<< "'!\n";
271 // Add the function to the materialize list, and store the basic block names
272 // to check after materialization.
274 BBInfo
.second
.split(BBNames
, ';', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
275 BBMap
.push_back({F
, std::move(BBNames
)});
278 // Use *argv instead of argv[0] to work around a wrong GCC warning.
279 ExitOnError
ExitOnErr(std::string(*argv
) + ": error reading input: ");
282 std::vector
<llvm::Function
*> Workqueue
;
283 for (GlobalValue
*GV
: GVs
) {
284 if (auto *F
= dyn_cast
<Function
>(GV
)) {
285 Workqueue
.push_back(F
);
288 while (!Workqueue
.empty()) {
289 Function
*F
= &*Workqueue
.back();
290 Workqueue
.pop_back();
291 ExitOnErr(F
->materialize());
292 for (auto &BB
: *F
) {
294 CallBase
*CB
= dyn_cast
<CallBase
>(&I
);
297 Function
*CF
= CB
->getCalledFunction();
300 if (CF
->isDeclaration() || !GVs
.insert(CF
))
302 Workqueue
.push_back(CF
);
308 auto Materialize
= [&](GlobalValue
&GV
) { ExitOnErr(GV
.materialize()); };
310 // Materialize requisite global values.
312 for (size_t i
= 0, e
= GVs
.size(); i
!= e
; ++i
)
313 Materialize(*GVs
[i
]);
315 // Deleting. Materialize every GV that's *not* in GVs.
316 SmallPtrSet
<GlobalValue
*, 8> GVSet(GVs
.begin(), GVs
.end());
318 if (!GVSet
.count(&F
))
324 std::vector
<GlobalValue
*> Gvs(GVs
.begin(), GVs
.end());
325 LoopAnalysisManager LAM
;
326 FunctionAnalysisManager FAM
;
327 CGSCCAnalysisManager CGAM
;
328 ModuleAnalysisManager MAM
;
332 PB
.registerModuleAnalyses(MAM
);
333 PB
.registerCGSCCAnalyses(CGAM
);
334 PB
.registerFunctionAnalyses(FAM
);
335 PB
.registerLoopAnalyses(LAM
);
336 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
338 ModulePassManager PM
;
339 PM
.addPass(ExtractGVPass(Gvs
, DeleteFn
, KeepConstInit
));
342 // Now that we have all the GVs we want, mark the module as fully
344 // FIXME: should the GVExtractionPass handle this?
345 ExitOnErr(M
->materializeAll());
348 // Extract the specified basic blocks from the module and erase the existing
350 if (!ExtractBlocks
.empty()) {
351 // Figure out which BasicBlocks we should extract.
352 std::vector
<std::vector
<BasicBlock
*>> GroupOfBBs
;
353 for (auto &P
: BBMap
) {
354 std::vector
<BasicBlock
*> BBs
;
355 for (StringRef BBName
: P
.second
) {
356 // The function has been materialized, so add its matching basic blocks
357 // to the block extractor list, or fail if a name is not found.
358 auto Res
= llvm::find_if(*P
.first
, [&](const BasicBlock
&BB
) {
359 return BB
.getName() == BBName
;
361 if (Res
== P
.first
->end()) {
362 errs() << argv
[0] << ": function " << P
.first
->getName()
363 << " doesn't contain a basic block named '" << BBName
367 BBs
.push_back(&*Res
);
369 GroupOfBBs
.push_back(BBs
);
372 LoopAnalysisManager LAM
;
373 FunctionAnalysisManager FAM
;
374 CGSCCAnalysisManager CGAM
;
375 ModuleAnalysisManager MAM
;
379 PB
.registerModuleAnalyses(MAM
);
380 PB
.registerCGSCCAnalyses(CGAM
);
381 PB
.registerFunctionAnalyses(FAM
);
382 PB
.registerLoopAnalyses(LAM
);
383 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
385 ModulePassManager PM
;
386 PM
.addPass(BlockExtractorPass(std::move(GroupOfBBs
), true));
390 // In addition to deleting all other functions, we also want to spiff it
391 // up a little bit. Do this now.
393 LoopAnalysisManager LAM
;
394 FunctionAnalysisManager FAM
;
395 CGSCCAnalysisManager CGAM
;
396 ModuleAnalysisManager MAM
;
400 PB
.registerModuleAnalyses(MAM
);
401 PB
.registerCGSCCAnalyses(CGAM
);
402 PB
.registerFunctionAnalyses(FAM
);
403 PB
.registerLoopAnalyses(LAM
);
404 PB
.crossRegisterProxies(LAM
, FAM
, CGAM
, MAM
);
406 ModulePassManager PM
;
408 PM
.addPass(GlobalDCEPass());
409 PM
.addPass(StripDeadDebugInfoPass());
410 PM
.addPass(StripDeadPrototypesPass());
413 ToolOutputFile
Out(OutputFilename
, EC
, sys::fs::OF_None
);
415 errs() << EC
.message() << '\n';
420 PM
.addPass(PrintModulePass(Out
.os(), "", PreserveAssemblyUseListOrder
));
421 else if (Force
|| !CheckBitcodeOutputToConsole(Out
.os()))
422 PM
.addPass(BitcodeWriterPass(Out
.os(), PreserveBitcodeUseListOrder
));