llvm.org GIT mirror llvm / a499d20
Add support to llvm-extract for extracting multiple functions and/or multiple global variables at a time. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@95825 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 35 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
3333
3434 =item B<--func> I
3535
36 Extract the function named I from the LLVM bitcode.
36 Extract the function named I from the LLVM bitcode. May be
37 specified multiple times to extract multiple functions at once.
38
39 =item B<--glob> I
40
41 Extract the global variable named I from the LLVM bitcode. May be
42 specified multiple times to extract multiple global variables at once.
3743
3844 =item B<--help>
3945
4848 Relink("relink",
4949 cl::desc("Turn external linkage for callees of function to delete"));
5050
51 // ExtractFunc - The function to extract from the module...
52 static cl::opt
53 ExtractFunc("func", cl::desc("Specify function to extract"), cl::init(""),
54 cl::value_desc("function"));
51 // ExtractFuncs - The functions to extract from the module...
52 static cl::list
53 ExtractFuncs("func", cl::desc("Specify function to extract"),
54 cl::ZeroOrMore, cl::value_desc("function"));
5555
56 // ExtractGlobal - The global to extract from the module...
57 static cl::opt
58 ExtractGlobal("glob", cl::desc("Specify global to extract"), cl::init(""),
59 cl::value_desc("global"));
56 // ExtractGlobals - The globals to extract from the module...
57 static cl::list
58 ExtractGlobals("glob", cl::desc("Specify global to extract"),
59 cl::ZeroOrMore, cl::value_desc("global"));
6060
6161 static cl::opt
6262 OutputAssembly("S",
8080 return 1;
8181 }
8282
83 // Figure out which function we should extract
84 GlobalVariable *G = !ExtractGlobal.empty() ?
85 M.get()->getNamedGlobal(ExtractGlobal) : 0;
83 std::vector GVs;
8684
87 // Figure out which function we should extract
88 if (ExtractFunc.empty() && ExtractGlobal.empty()) ExtractFunc = "main";
89 Function *F = M.get()->getFunction(ExtractFunc);
85 // Figure out which globals we should extract.
86 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
87 GlobalValue *GV = M.get()->getNamedGlobal(ExtractGlobals[i]);
88 if (!GV) {
89 errs() << argv[0] << ": program doesn't contain global named '"
90 << ExtractGlobals[i] << "'!\n";
91 return 1;
92 }
93 GVs.push_back(GV);
94 }
9095
91 if (F == 0 && G == 0) {
92 errs() << argv[0] << ": program doesn't contain function named '"
93 << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
94 return 1;
96 // Figure out which functions we should extract.
97 for (size_t i = 0, e = ExtractFuncs.size(); i != e; ++i) {
98 GlobalValue *GV = M.get()->getFunction(ExtractFuncs[i]);
99 if (!GV) {
100 errs() << argv[0] << ": program doesn't contain function named '"
101 << ExtractFuncs[i] << "'!\n";
102 return 1;
103 }
104 GVs.push_back(GV);
95105 }
96106
97107 // In addition to deleting all other functions, we also want to spiff it
98108 // up a little bit. Do this now.
99109 PassManager Passes;
100110 Passes.add(new TargetData(M.get())); // Use correct TargetData
101 // Either isolate the function or delete it from the Module
102 std::vector GVs;
103 if (F) GVs.push_back(F);
104 if (G) GVs.push_back(G);
105111
106112 Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
107113 if (!DeleteFn)