llvm.org GIT mirror llvm / 4e0a55d
Add -rfunc and -rglob options to llvm-extract to support regular expression matching. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139945 91177308-0d34-0410-b5e6-96231b3b80d8 Chad Rosier 8 years ago
2 changed file(s) with 83 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
3636 Extract the function named I from the LLVM bitcode. May be
3737 specified multiple times to extract multiple functions at once.
3838
39 =item B<--rfunc> I
40
41 Extract the function(s) matching I from the LLVM bitcode.
42 All functions matching the regular expression will be extracted. May be
43 specified multiple times.
44
3945 =item B<--glob> I
4046
4147 Extract the global variable named I from the LLVM bitcode. May be
4248 specified multiple times to extract multiple global variables at once.
49
50 =item B<--rglob> I
51
52 Extract the global variable(s) matching I from the LLVM
53 bitcode. All global variables matching the regular expression will be extracted.
54 May be specified multiple times.
4355
4456 =item B<-help>
4557
2525 #include "llvm/Support/ToolOutputFile.h"
2626 #include "llvm/Support/SystemUtils.h"
2727 #include "llvm/Support/Signals.h"
28 #include "llvm/Support/Regex.h"
2829 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SetVector.h"
2931 #include
3032 using namespace llvm;
3133
4446 static cl::opt
4547 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
4648
47 // ExtractFuncs - The functions to extract from the module...
49 // ExtractFuncs - The functions to extract from the module.
4850 static cl::list
4951 ExtractFuncs("func", cl::desc("Specify function to extract"),
5052 cl::ZeroOrMore, cl::value_desc("function"));
5153
52 // ExtractGlobals - The globals to extract from the module...
54 // ExtractRegExpFuncs - The functions, matched via regular expression, to
55 // extract from the module.
56 static cl::list
57 ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
58 "regular expression"),
59 cl::ZeroOrMore, cl::value_desc("rfunction"));
60
61 // ExtractGlobals - The globals to extract from the module.
5362 static cl::list
5463 ExtractGlobals("glob", cl::desc("Specify global to extract"),
5564 cl::ZeroOrMore, cl::value_desc("global"));
65
66 // ExtractRegExpGlobals - The globals, matched via regular expression, to
67 // extract from the module...
68 static cl::list
69 ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
70 "regular expression"),
71 cl::ZeroOrMore, cl::value_desc("rglobal"));
5672
5773 static cl::opt
5874 OutputAssembly("S",
7793 return 1;
7894 }
7995
80 std::vector GVs;
96 // Use SetVector to avoid duplicates.
97 SetVector GVs;
8198
8299 // Figure out which globals we should extract.
83100 for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
87104 << ExtractGlobals[i] << "'!\n";
88105 return 1;
89106 }
90 GVs.push_back(GV);
107 GVs.insert(GV);
108 }
109
110 // Extract globals via regular expression matching.
111 for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
112 std::string Error;
113 Regex RegEx(ExtractRegExpGlobals[i]);
114 if (!RegEx.isValid(Error)) {
115 errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
116 "invalid regex: " << Error;
117 }
118 bool match = false;
119 for (Module::global_iterator GV = M.get()->global_begin(),
120 E = M.get()->global_end(); GV != E; GV++) {
121 if (RegEx.match(GV->getName())) {
122 GVs.insert(&*GV);
123 match = true;
124 }
125 }
126 if (!match) {
127 errs() << argv[0] << ": program doesn't contain global named '"
128 << ExtractRegExpGlobals[i] << "'!\n";
129 return 1;
130 }
91131 }
92132
93133 // Figure out which functions we should extract.
98138 << ExtractFuncs[i] << "'!\n";
99139 return 1;
100140 }
101 GVs.push_back(GV);
141 GVs.insert(GV);
142 }
143 // Extract functions via regular expression matching.
144 for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
145 std::string Error;
146 StringRef RegExStr = ExtractRegExpFuncs[i];
147 Regex RegEx(RegExStr);
148 if (!RegEx.isValid(Error)) {
149 errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
150 "invalid regex: " << Error;
151 }
152 bool match = false;
153 for (Module::iterator F = M.get()->begin(), E = M.get()->end(); F != E;
154 F++) {
155 if (RegEx.match(F->getName())) {
156 GVs.insert(&*F);
157 match = true;
158 }
159 }
160 if (!match) {
161 errs() << argv[0] << ": program doesn't contain global named '"
162 << ExtractRegExpFuncs[i] << "'!\n";
163 return 1;
164 }
102165 }
103166
104167 // Materialize requisite global values.
144207 PassManager Passes;
145208 Passes.add(new TargetData(M.get())); // Use correct TargetData
146209
147 Passes.add(createGVExtractionPass(GVs, DeleteFn));
210 std::vector Gvs(GVs.begin(), GVs.end());
211
212 Passes.add(createGVExtractionPass(Gvs, DeleteFn));
148213 if (!DeleteFn)
149214 Passes.add(createGlobalDCEPass()); // Delete unreachable globals
150215 Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info