- // Figure out which function we should extract
- Function *F = M.get()->getNamedFunction(ExtractFunc);
- if (F == 0) {
- llvm_cerr << argv[0] << ": program doesn't contain function named '"
- << ExtractFunc << "'!\n";
+ // Figure out which function we should extract
+ if (!ExtractFunc.size() && !ExtractGlobal.size()) ExtractFunc = "main";
+ Function *F = M.get()->getFunction(ExtractFunc);
+
+ if (F == 0 && G == 0) {
+ cerr << argv[0] << ": program doesn't contain function named '"
+ << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
+ return 1;
+ }
+
+ // In addition to deleting all other functions, we also want to spiff it
+ // up a little bit. Do this now.
+ PassManager Passes;
+ Passes.add(new TargetData(M.get())); // Use correct TargetData
+ // Either isolate the function or delete it from the Module
+ std::vector<GlobalValue*> GVs;
+ if (F) GVs.push_back(F);
+ if (G) GVs.push_back(G);
+
+ Passes.add(createGVExtractionPass(GVs, DeleteFn, Relink));
+ if (!DeleteFn)
+ Passes.add(createGlobalDCEPass()); // Delete unreachable globals
+ Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
+ Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
+
+ std::ostream *Out = 0;
+
+ if (OutputFilename != "-") { // Not stdout?
+ if (!Force && std::ifstream(OutputFilename.c_str())) {
+ // If force is not specified, make sure not to overwrite a file!
+ cerr << argv[0] << ": error opening '" << OutputFilename
+ << "': file exists!\n"
+ << "Use -f command line argument to force output\n";