llvm.org GIT mirror llvm / 1bfd87a
Return a std::unique_ptr from parseInputFile and propagate. NFC. The memory management in BugPoint is fairly convoluted, so this just unwraps one layer by changing the return type of functions that always return owned Modules. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216464 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
6 changed file(s) with 92 addition(s) and 113 deletion(s). Raw diff Collapse all Expand all
8181 delete gcc;
8282 }
8383
84
85 /// ParseInputFile - Given a bitcode or assembly input filename, parse and
86 /// return it, or return null if not possible.
87 ///
88 Module *llvm::ParseInputFile(const std::string &Filename,
89 LLVMContext& Ctxt) {
84 std::unique_ptr llvm::parseInputFile(StringRef Filename,
85 LLVMContext &Ctxt) {
9086 SMDiagnostic Err;
91 Module *Result = ParseIRFile(Filename, Err, Ctxt);
87 std::unique_ptr Result (ParseIRFile(Filename, Err, Ctxt));
9288 if (!Result)
9389 Err.print("bugpoint", errs());
9490
119115 assert(!Filenames.empty() && "Must specify at least on input filename!");
120116
121117 // Load the first input file.
122 Program = ParseInputFile(Filenames[0], Context);
118 Program = parseInputFile(Filenames[0], Context).release();
123119 if (!Program) return true;
124120
125121 outs() << "Read input file : '" << Filenames[0] << "'\n";
126122
127123 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
128 std::unique_ptr M(ParseInputFile(Filenames[i], Context));
124 std::unique_ptr M = parseInputFile(Filenames[i], Context);
129125 if (!M.get()) return true;
130126
131127 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
1717
1818 #include "llvm/IR/ValueMap.h"
1919 #include "llvm/Transforms/Utils/ValueMapper.h"
20 #include
2021 #include
2122 #include
2223
209210 void EmitProgressBitcode(const Module *M, const std::string &ID,
210211 bool NoFlyer = false) const;
211212
212 /// deleteInstructionFromProgram - This method clones the current Program and
213 /// deletes the specified instruction from the cloned module. It then runs a
214 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code
215 /// which depends on the value. The modified module is then returned.
216 ///
217 Module *deleteInstructionFromProgram(const Instruction *I, unsigned Simp);
218
219 /// performFinalCleanups - This method clones the current Program and performs
220 /// a series of cleanups intended to get rid of extra cruft on the module. If
221 /// the MayModifySemantics argument is true, then the cleanups is allowed to
213 /// This method clones the current Program and deletes the specified
214 /// instruction from the cloned module. It then runs a series of cleanup
215 /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
216 /// value. The modified module is then returned.
217 ///
218 std::unique_ptr deleteInstructionFromProgram(const Instruction *I,
219 unsigned Simp);
220
221 /// This method clones the current Program and performs a series of cleanups
222 /// intended to get rid of extra cruft on the module. If the
223 /// MayModifySemantics argument is true, then the cleanups is allowed to
222224 /// modify how the code behaves.
223225 ///
224 Module *performFinalCleanups(Module *M, bool MayModifySemantics = false);
225
226 /// ExtractLoop - Given a module, extract up to one loop from it into a new
227 /// function. This returns null if there are no extractable loops in the
228 /// program or if the loop extractor crashes.
229 Module *ExtractLoop(Module *M);
230
231 /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
232 /// into their own functions. The only detail is that M is actually a module
233 /// cloned from the one the BBs are in, so some mapping needs to be performed.
234 /// If this operation fails for some reason (ie the implementation is buggy),
235 /// this function should return null, otherwise it returns a new Module.
236 Module *ExtractMappedBlocksFromModule(const std::vector &BBs,
237 Module *M);
238
239 /// runPassesOn - Carefully run the specified set of pass on the specified
240 /// module, returning the transformed module on success, or a null pointer on
241 /// failure. If AutoDebugCrashes is set to true, then bugpoint will
242 /// automatically attempt to track down a crashing pass if one exists, and
243 /// this method will never return null.
244 Module *runPassesOn(Module *M, const std::vector &Passes,
245 bool AutoDebugCrashes = false, unsigned NumExtraArgs = 0,
246 const char * const *ExtraArgs = nullptr);
226 std::unique_ptr performFinalCleanups(Module *M,
227 bool MayModifySemantics = false);
228
229 /// Given a module, extract up to one loop from it into a new function. This
230 /// returns null if there are no extractable loops in the program or if the
231 /// loop extractor crashes.
232 std::unique_ptr extractLoop(Module *M);
233
234 /// Extract all but the specified basic blocks into their own functions. The
235 /// only detail is that M is actually a module cloned from the one the BBs are
236 /// in, so some mapping needs to be performed. If this operation fails for
237 /// some reason (ie the implementation is buggy), this function should return
238 /// null, otherwise it returns a new Module.
239 std::unique_ptr
240 extractMappedBlocksFromModule(const std::vector &BBs,
241 Module *M);
242
243 /// Carefully run the specified set of pass on the specified/ module,
244 /// returning the transformed module on success, or a null pointer on failure.
245 /// If AutoDebugCrashes is set to true, then bugpoint will automatically
246 /// attempt to track down a crashing pass if one exists, and this method will
247 /// never return null.
248 std::unique_ptr runPassesOn(Module *M,
249 const std::vector &Passes,
250 bool AutoDebugCrashes = false,
251 unsigned NumExtraArgs = 0,
252 const char *const *ExtraArgs = nullptr);
247253
248254 /// runPasses - Run the specified passes on Program, outputting a bitcode
249255 /// file and writting the filename into OutputFile if successful. If the
295301 bool initializeExecutionEnvironment();
296302 };
297303
298 /// ParseInputFile - Given a bitcode or assembly input filename, parse and
299 /// return it, or return null if not possible.
300 ///
301 Module *ParseInputFile(const std::string &InputFilename,
302 LLVMContext& ctxt);
303
304 /// Given a bitcode or assembly input filename, parse and return it, or return
305 /// null if not possible.
306 ///
307 std::unique_ptr parseInputFile(StringRef InputFilename,
308 LLVMContext &ctxt);
304309
305310 /// getPassesString - Turn a list of passes into a string which indicates the
306311 /// command line options that must be passed to add the passes.
7171
7272 OrigProgram = BD.Program;
7373
74 BD.Program = ParseInputFile(PrefixOutput, BD.getContext());
74 BD.Program = parseInputFile(PrefixOutput, BD.getContext()).release();
7575 if (BD.Program == nullptr) {
7676 errs() << BD.getToolName() << ": Error reading bitcode file '"
7777 << PrefixOutput << "'!\n";
319319 std::vector Passes;
320320 Passes.push_back("simplifycfg");
321321 Passes.push_back("verify");
322 Module *New = BD.runPassesOn(M, Passes);
322 std::unique_ptr New = BD.runPassesOn(M, Passes);
323323 delete M;
324324 if (!New) {
325325 errs() << "simplifycfg failed!\n";
326326 exit(1);
327327 }
328 M = New;
328 M = New.release();
329329
330330 // Try running on the hacked up program...
331331 if (TestFn(BD, M)) {
575575 continue;
576576
577577 outs() << "Checking instruction: " << *I;
578 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
578 std::unique_ptr M =
579 BD.deleteInstructionFromProgram(I, Simplification);
579580
580581 // Find out if the pass still crashes on this pass...
581 if (TestFn(BD, M)) {
582 if (TestFn(BD, M.get())) {
582583 // Yup, it does, we delete the old module, and continue trying
583584 // to reduce the testcase...
584 BD.setNewProgram(M);
585 BD.setNewProgram(M.release());
585586 InstructionsToSkipBeforeDeleting = CurInstructionNum;
586587 goto TryAgain; // I wish I had a multi-level break here!
587588 }
588
589 // This pass didn't crash without this instruction, try the next
590 // one.
591 delete M;
592589 }
593590 }
594591
604601 if (!BugpointIsInterrupted) {
605602 outs() << "\n*** Attempting to perform final cleanups: ";
606603 Module *M = CloneModule(BD.getProgram());
607 M = BD.performFinalCleanups(M, true);
604 M = BD.performFinalCleanups(M, true).release();
608605
609606 // Find out if the pass still crashes on the cleaned up program...
610607 if (TestFn(BD, M)) {
8181 }
8282 } // end anonymous namespace
8383
84 /// deleteInstructionFromProgram - This method clones the current Program and
85 /// deletes the specified instruction from the cloned module. It then runs a
86 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
87 /// depends on the value. The modified module is then returned.
88 ///
89 Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
90 unsigned Simplification) {
84 std::unique_ptr
85 BugDriver::deleteInstructionFromProgram(const Instruction *I,
86 unsigned Simplification) {
9187 // FIXME, use vmap?
9288 Module *Clone = CloneModule(Program);
9389
122118 Passes.push_back("simplifycfg"); // Delete dead control flow
123119
124120 Passes.push_back("verify");
125 Module *New = runPassesOn(Clone, Passes);
121 std::unique_ptr New = runPassesOn(Clone, Passes);
126122 delete Clone;
127123 if (!New) {
128124 errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n";
131127 return New;
132128 }
133129
134 /// performFinalCleanups - This method clones the current Program and performs
135 /// a series of cleanups intended to get rid of extra cruft on the module
136 /// before handing it to the user.
137 ///
138 Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
130 std::unique_ptr
131 BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
139132 // Make all functions external, so GlobalDCE doesn't delete them...
140133 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
141134 I->setLinkage(GlobalValue::ExternalLinkage);
148141 else
149142 CleanupPasses.push_back("deadargelim");
150143
151 Module *New = runPassesOn(M, CleanupPasses);
144 std::unique_ptr New = runPassesOn(M, CleanupPasses);
152145 if (!New) {
153146 errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n";
154 return M;
147 return nullptr;
155148 }
156149 delete M;
157150 return New;
158151 }
159152
160
161 /// ExtractLoop - Given a module, extract up to one loop from it into a new
162 /// function. This returns null if there are no extractable loops in the
163 /// program or if the loop extractor crashes.
164 Module *BugDriver::ExtractLoop(Module *M) {
153 std::unique_ptr BugDriver::extractLoop(Module *M) {
165154 std::vector LoopExtractPasses;
166155 LoopExtractPasses.push_back("loop-extract-single");
167156
168 Module *NewM = runPassesOn(M, LoopExtractPasses);
157 std::unique_ptr NewM = runPassesOn(M, LoopExtractPasses);
169158 if (!NewM) {
170159 outs() << "*** Loop extraction failed: ";
171160 EmitProgressBitcode(M, "loopextraction", true);
178167 // to avoid taking forever.
179168 static unsigned NumExtracted = 32;
180169 if (M->size() == NewM->size() || --NumExtracted == 0) {
181 delete NewM;
182170 return nullptr;
183171 } else {
184172 assert(M->size() < NewM->size() && "Loop extract removed functions?");
355343 // Basic Block Extraction Code
356344 //===----------------------------------------------------------------------===//
357345
358 /// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
359 /// into their own functions. The only detail is that M is actually a module
360 /// cloned from the one the BBs are in, so some mapping needs to be performed.
361 /// If this operation fails for some reason (ie the implementation is buggy),
362 /// this function should return null, otherwise it returns a new Module.
363 Module *BugDriver::ExtractMappedBlocksFromModule(const
364 std::vector &BBs,
365 Module *M) {
346 std::unique_ptr
347 BugDriver::extractMappedBlocksFromModule(const std::vector &BBs,
348 Module *M) {
366349 SmallString<128> Filename;
367350 int FD;
368351 std::error_code EC = sys::fs::createUniqueFile(
400383
401384 std::vector PI;
402385 PI.push_back("extract-blocks");
403 Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
386 std::unique_ptr Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
404387
405388 sys::fs::remove(Filename.c_str());
406389
127127 // Ok, so now we know that the prefix passes work, try running the suffix
128128 // passes on the result of the prefix passes.
129129 //
130 std::unique_ptr PrefixOutput(
131 ParseInputFile(BitcodeResult, BD.getContext()));
130 std::unique_ptr PrefixOutput =
131 parseInputFile(BitcodeResult, BD.getContext());
132132 if (!PrefixOutput) {
133133 errs() << BD.getToolName() << ": Error reading bitcode file '"
134134 << BitcodeResult << "'!\n";
315315 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
316316 MiscompiledFunctions,
317317 VMap);
318 Module *ToOptimizeLoopExtracted = BD.ExtractLoop(ToOptimize);
318 Module *ToOptimizeLoopExtracted = BD.extractLoop(ToOptimize).release();
319319 if (!ToOptimizeLoopExtracted) {
320320 // If the loop extractor crashed or if there were no extractible loops,
321321 // then this chapter of our odyssey is over with.
333333 // extraction.
334334 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
335335 bool Failure;
336 Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
337 false, Error, Failure);
336 Module *New = TestMergedProgram(BD, ToOptimizeLoopExtracted,
337 ToNotOptimize, false, Error, Failure);
338338 if (!New)
339339 return false;
340340
363363 << OutputPrefix << "-loop-extract-fail-*.bc files.\n";
364364 delete ToOptimize;
365365 delete ToNotOptimize;
366 delete ToOptimizeLoopExtracted;
367366 return MadeChange;
368367 }
369368 delete ToOptimize;
532531
533532 // Try the extraction. If it doesn't work, then the block extractor crashed
534533 // or something, in which case bugpoint can't chase down this possibility.
535 if (Module *New = BD.ExtractMappedBlocksFromModule(BBsOnClone, ToOptimize)) {
534 if (std::unique_ptr New =
535 BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize)) {
536536 delete ToOptimize;
537537 // Run the predicate,
538538 // note that the predicate will delete both input modules.
539 bool Ret = TestFn(BD, New, ToNotOptimize, Error);
539 bool Ret = TestFn(BD, New.get(), ToNotOptimize, Error);
540540 delete BD.swapProgramIn(Orig);
541541 return Ret;
542542 }
590590 Module *ToExtract = SplitFunctionsOutOfModule(ProgClone,
591591 MiscompiledFunctions,
592592 VMap);
593 Module *Extracted = BD.ExtractMappedBlocksFromModule(Blocks, ToExtract);
593 std::unique_ptr Extracted =
594 BD.extractMappedBlocksFromModule(Blocks, ToExtract);
594595 if (!Extracted) {
595596 // Weird, extraction should have worked.
596597 errs() << "Nondeterministic problem extracting blocks??\n";
611612 I->getFunctionType()));
612613
613614 std::string ErrorMsg;
614 if (Linker::LinkModules(ProgClone, Extracted, Linker::DestroySource,
615 if (Linker::LinkModules(ProgClone, Extracted.get(), Linker::DestroySource,
615616 &ErrorMsg)) {
616617 errs() << BD.getToolName() << ": Error linking modules together:"
617618 << ErrorMsg << '\n';
618619 exit(1);
619620 }
620 delete Extracted;
621621
622622 // Set the new program and delete the old one.
623623 BD.setNewProgram(ProgClone);
729729 // Run the optimization passes on ToOptimize, producing a transformed version
730730 // of the functions being tested.
731731 outs() << " Optimizing functions being tested: ";
732 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
733 /*AutoDebugCrashes*/true);
732 std::unique_ptr Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
733 /*AutoDebugCrashes*/ true);
734734 outs() << "done.\n";
735735 delete Test;
736736
737737 outs() << " Checking to see if the merged program executes correctly: ";
738738 bool Broken;
739 Module *New = TestMergedProgram(BD, Optimized, Safe, true, Error, Broken);
739 Module *New =
740 TestMergedProgram(BD, Optimized.get(), Safe, true, Error, Broken);
740741 if (New) {
741742 outs() << (Broken ? " nope.\n" : " yup.\n");
742743 // Delete the original and set the new program.
795796 static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
796797 Module *Safe) {
797798 // Clean up the modules, removing extra cruft that we don't need anymore...
798 Test = BD.performFinalCleanups(Test);
799 Test = BD.performFinalCleanups(Test).release();
799800
800801 // If we are executing the JIT, we have several nasty issues to take care of.
801802 if (!BD.isExecutingJIT()) return;
246246 }
247247
248248
249 /// runPassesOn - Carefully run the specified set of pass on the specified
250 /// module, returning the transformed module on success, or a null pointer on
251 /// failure.
252 Module *BugDriver::runPassesOn(Module *M,
253 const std::vector &Passes,
254 bool AutoDebugCrashes, unsigned NumExtraArgs,
255 const char * const *ExtraArgs) {
249 std::unique_ptr
250 BugDriver::runPassesOn(Module *M, const std::vector &Passes,
251 bool AutoDebugCrashes, unsigned NumExtraArgs,
252 const char *const *ExtraArgs) {
256253 std::string BitcodeResult;
257254 if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/,
258255 NumExtraArgs, ExtraArgs)) {
266263 return nullptr;
267264 }
268265
269 Module *Ret = ParseInputFile(BitcodeResult, Context);
266 std::unique_ptr Ret = parseInputFile(BitcodeResult, Context);
270267 if (!Ret) {
271268 errs() << getToolName() << ": Error reading bitcode file '"
272269 << BitcodeResult << "'!\n";