llvm.org GIT mirror llvm / ac95cc7
Convert more tools code from cerr and cout to errs() and outs(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76070 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
21 changed file(s) with 280 addition(s) and 287 deletion(s). Raw diff Collapse all Expand all
2424 #include "llvm/Support/MemoryBuffer.h"
2525 #include "llvm/Support/SourceMgr.h"
2626 #include "llvm/Support/raw_ostream.h"
27 #include
2827 #include
2928 using namespace llvm;
3029
106105 if (Program == 0) return true;
107106
108107 if (!run_as_child)
109 std::cout << "Read input file : '" << Filenames[0] << "'\n";
108 outs() << "Read input file : '" << Filenames[0] << "'\n";
110109
111110 for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
112111 std::auto_ptr M(ParseInputFile(Filenames[i], Context));
113112 if (M.get() == 0) return true;
114113
115114 if (!run_as_child)
116 std::cout << "Linking in input file: '" << Filenames[i] << "'\n";
115 outs() << "Linking in input file: '" << Filenames[i] << "'\n";
117116 std::string ErrorMessage;
118117 if (Linker::LinkModules(Program, M.get(), &ErrorMessage)) {
119118 errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
127126 }
128127
129128 if (!run_as_child)
130 std::cout << "*** All input ok\n";
129 outs() << "*** All input ok\n";
131130
132131 // All input files read successfully!
133132 return false;
161160 // file, then we know the compiler didn't crash, so try to diagnose a
162161 // miscompilation.
163162 if (!PassesToRun.empty()) {
164 std::cout << "Running selected passes on program to test for crash: ";
163 outs() << "Running selected passes on program to test for crash: ";
165164 if (runPasses(PassesToRun))
166165 return debugOptimizerCrash();
167166 }
170169 if (initializeExecutionEnvironment()) return true;
171170
172171 // Test to see if we have a code generator crash.
173 std::cout << "Running the code generator to test for a crash: ";
172 outs() << "Running the code generator to test for a crash: ";
174173 try {
175174 compileProgram(Program);
176 std::cout << '\n';
175 outs() << '\n';
177176 } catch (ToolExecutionError &TEE) {
178 std::cout << TEE.what();
177 outs() << TEE.what();
179178 return debugCodeGeneratorCrash();
180179 }
181180
186185 //
187186 bool CreatedOutput = false;
188187 if (ReferenceOutputFile.empty()) {
189 std::cout << "Generating reference output from raw program: ";
188 outs() << "Generating reference output from raw program: ";
190189 if(!createReferenceFile(Program)){
191190 return debugCodeGeneratorCrash();
192191 }
201200 // Diff the output of the raw program against the reference output. If it
202201 // matches, then we assume there is a miscompilation bug and try to
203202 // diagnose it.
204 std::cout << "*** Checking the code generator...\n";
203 outs() << "*** Checking the code generator...\n";
205204 try {
206205 if (!diffProgram()) {
207 std::cout << "\n*** Output matches: Debugging miscompilation!\n";
206 outs() << "\n*** Output matches: Debugging miscompilation!\n";
208207 return debugMiscompilation();
209208 }
210209 } catch (ToolExecutionError &TEE) {
212211 return debugCodeGeneratorCrash();
213212 }
214213
215 std::cout << "\n*** Input program does not match reference diff!\n";
216 std::cout << "Debugging code generator problem!\n";
214 outs() << "\n*** Input program does not match reference diff!\n";
215 outs() << "Debugging code generator problem!\n";
217216 try {
218217 return debugCodeGenerator();
219218 } catch (ToolExecutionError &TEE) {
226225 unsigned NumPrint = Funcs.size();
227226 if (NumPrint > 10) NumPrint = 10;
228227 for (unsigned i = 0; i != NumPrint; ++i)
229 std::cout << " " << Funcs[i]->getName();
228 outs() << " " << Funcs[i]->getName();
230229 if (NumPrint < Funcs.size())
231 std::cout << "... <" << Funcs.size() << " total>";
232 std::cout << std::flush;
230 outs() << "... <" << Funcs.size() << " total>";
231 outs().flush();
233232 }
234233
235234 void llvm::PrintGlobalVariableList(const std::vector &GVs) {
236235 unsigned NumPrint = GVs.size();
237236 if (NumPrint > 10) NumPrint = 10;
238237 for (unsigned i = 0; i != NumPrint; ++i)
239 std::cout << " " << GVs[i]->getName();
238 outs() << " " << GVs[i]->getName();
240239 if (NumPrint < GVs.size())
241 std::cout << "... <" << GVs.size() << " total>";
242 std::cout << std::flush;
243 }
240 outs() << "... <" << GVs.size() << " total>";
241 outs().flush();
242 }
247247 /// optimizations fail for some reason (optimizer crashes), return true,
248248 /// otherwise return false. If DeleteOutput is set to true, the bitcode is
249249 /// deleted on success, and the filename string is undefined. This prints to
250 /// cout a single line message indicating whether compilation was successful
250 /// outs() a single line message indicating whether compilation was successful
251251 /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments
252252 /// to pass to the child bugpoint instance.
253253 ///
2727 #include "llvm/Transforms/Utils/Cloning.h"
2828 #include "llvm/Support/FileUtilities.h"
2929 #include "llvm/Support/CommandLine.h"
30 #include
31 #include
3230 #include
3331 using namespace llvm;
3432
6462 sys::Path PrefixOutput;
6563 Module *OrigProgram = 0;
6664 if (!Prefix.empty()) {
67 std::cout << "Checking to see if these passes crash: "
68 << getPassesString(Prefix) << ": ";
65 outs() << "Checking to see if these passes crash: "
66 << getPassesString(Prefix) << ": ";
6967 std::string PfxOutput;
7068 if (BD.runPasses(Prefix, PfxOutput))
7169 return KeepPrefix;
8280 PrefixOutput.eraseFromDisk();
8381 }
8482
85 std::cout << "Checking to see if these passes crash: "
86 << getPassesString(Suffix) << ": ";
83 outs() << "Checking to see if these passes crash: "
84 << getPassesString(Suffix) << ": ";
8785
8886 if (BD.runPasses(Suffix)) {
8987 delete OrigProgram; // The suffix crashes alone...
142140 GVSet.insert(CMGV);
143141 }
144142
145 std::cout << "Checking for crash with only these global variables: ";
143 outs() << "Checking for crash with only these global variables: ";
146144 PrintGlobalVariableList(GVs);
147 std::cout << ": ";
145 outs() << ": ";
148146
149147 // Loop over and delete any global variables which we aren't supposed to be
150148 // playing with...
216214 Functions.insert(CMF);
217215 }
218216
219 std::cout << "Checking for crash with only these functions: ";
217 outs() << "Checking for crash with only these functions: ";
220218 PrintFunctionList(Funcs);
221 std::cout << ": ";
219 outs() << ": ";
222220
223221 // Loop over and delete any functions which we aren't supposed to be playing
224222 // with...
276274 for (unsigned i = 0, e = BBs.size(); i != e; ++i)
277275 Blocks.insert(cast(ValueMap[BBs[i]]));
278276
279 std::cout << "Checking for crash with only these blocks:";
277 outs() << "Checking for crash with only these blocks:";
280278 unsigned NumPrint = Blocks.size();
281279 if (NumPrint > 10) NumPrint = 10;
282280 for (unsigned i = 0, e = NumPrint; i != e; ++i)
283 std::cout << " " << BBs[i]->getName();
281 outs() << " " << BBs[i]->getName();
284282 if (NumPrint < Blocks.size())
285 std::cout << "... <" << Blocks.size() << " total>";
286 std::cout << ": ";
283 outs() << "... <" << Blocks.size() << " total>";
284 outs() << ": ";
287285
288286 // Loop over and delete any hack up any blocks that are not listed...
289287 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
381379 Instructions.insert(cast(ValueMap[Insts[i]]));
382380 }
383381
384 std::cout << "Checking for crash with only " << Instructions.size();
382 outs() << "Checking for crash with only " << Instructions.size();
385383 if (Instructions.size() == 1)
386 std::cout << " instruction: ";
384 outs() << " instruction: ";
387385 else
388 std::cout << " instructions: ";
386 outs() << " instructions: ";
389387
390388 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
391389 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI)
444442 delete M; // No change made...
445443 } else {
446444 // See if the program still causes a crash...
447 std::cout << "\nChecking to see if we can delete global inits: ";
445 outs() << "\nChecking to see if we can delete global inits: ";
448446
449447 if (TestFn(BD, M)) { // Still crashes?
450448 BD.setNewProgram(M);
451 std::cout << "\n*** Able to remove all global initializers!\n";
449 outs() << "\n*** Able to remove all global initializers!\n";
452450 } else { // No longer crashes?
453 std::cout << " - Removing all global inits hides problem!\n";
451 outs() << " - Removing all global inits hides problem!\n";
454452 delete M;
455453
456454 std::vector GVs;
461459 GVs.push_back(I);
462460
463461 if (GVs.size() > 1 && !BugpointIsInterrupted) {
464 std::cout << "\n*** Attempting to reduce the number of global "
462 outs() << "\n*** Attempting to reduce the number of global "
465463 << "variables in the testcase\n";
466464
467465 unsigned OldSize = GVs.size();
482480 Functions.push_back(I);
483481
484482 if (Functions.size() > 1 && !BugpointIsInterrupted) {
485 std::cout << "\n*** Attempting to reduce the number of functions "
483 outs() << "\n*** Attempting to reduce the number of functions "
486484 "in the testcase\n";
487485
488486 unsigned OldSize = Functions.size();
531529 do {
532530 if (BugpointIsInterrupted) break;
533531 --Simplification;
534 std::cout << "\n*** Attempting to reduce testcase by deleting instruc"
535 << "tions: Simplification Level #" << Simplification << '\n';
532 outs() << "\n*** Attempting to reduce testcase by deleting instruc"
533 << "tions: Simplification Level #" << Simplification << '\n';
536534
537535 // Now that we have deleted the functions that are unnecessary for the
538536 // program, try to remove instructions that are not necessary to cause the
560558 } else {
561559 if (BugpointIsInterrupted) goto ExitLoops;
562560
563 std::cout << "Checking instruction: " << *I;
561 outs() << "Checking instruction: " << *I;
564562 Module *M = BD.deleteInstructionFromProgram(I, Simplification);
565563
566564 // Find out if the pass still crashes on this pass...
587585
588586 // Try to clean up the testcase by running funcresolve and globaldce...
589587 if (!BugpointIsInterrupted) {
590 std::cout << "\n*** Attempting to perform final cleanups: ";
588 outs() << "\n*** Attempting to perform final cleanups: ";
591589 Module *M = CloneModule(BD.getProgram());
592590 M = BD.performFinalCleanups(M, true);
593591
613611 /// out exactly which pass is crashing.
614612 ///
615613 bool BugDriver::debugOptimizerCrash(const std::string &ID) {
616 std::cout << "\n*** Debugging optimizer crash!\n";
614 outs() << "\n*** Debugging optimizer crash!\n";
617615
618616 // Reduce the list of passes which causes the optimizer to crash...
619617 if (!BugpointIsInterrupted)
620618 ReducePassList(*this).reduceList(PassesToRun);
621619
622 std::cout << "\n*** Found crashing pass"
623 << (PassesToRun.size() == 1 ? ": " : "es: ")
624 << getPassesString(PassesToRun) << '\n';
620 outs() << "\n*** Found crashing pass"
621 << (PassesToRun.size() == 1 ? ": " : "es: ")
622 << getPassesString(PassesToRun) << '\n';
625623
626624 EmitProgressBitcode(ID);
627625
1818 #include "llvm/Support/FileUtilities.h"
1919 #include "llvm/Support/SystemUtils.h"
2020 #include
21 #include
2221
2322 using namespace llvm;
2423
125124 /// environment for executing LLVM programs.
126125 ///
127126 bool BugDriver::initializeExecutionEnvironment() {
128 std::cout << "Initializing execution environment: ";
127 outs() << "Initializing execution environment: ";
129128
130129 // Create an instance of the AbstractInterpreter interface as specified on
131130 // the command line
187186 if (!Interpreter)
188187 errs() << Message;
189188 else // Display informational messages on stdout instead of stderr
190 std::cout << Message;
189 outs() << Message;
191190
192191 std::string Path = SafeInterpreterPath;
193192 if (Path.empty())
259258 "\"safe\" backend right now!\n";
260259 break;
261260 }
262 if (!SafeInterpreter) { std::cout << Message << "\nExiting.\n"; exit(1); }
261 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
263262
264263 gcc = GCC::create(getToolName(), Message, &GCCToolArgv);
265 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
264 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
266265
267266 // If there was an error creating the selected interpreter, quit with error.
268267 return Interpreter == 0;
354353 errs() << "";
355354 static bool FirstTimeout = true;
356355 if (FirstTimeout) {
357 std::cout << "\n"
356 outs() << "\n"
358357 "*** Program execution timed out! This mechanism is designed to handle\n"
359358 " programs stuck in infinite loops gracefully. The -timeout option\n"
360359 " can be used to change the timeout threshold or disable it completely\n"
417416 }
418417 try {
419418 ReferenceOutputFile = executeProgramSafely(Filename);
420 std::cout << "\nReference output is: " << ReferenceOutputFile << "\n\n";
419 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
421420 } catch (ToolExecutionError &TEE) {
422421 errs() << TEE.what();
423422 if (Interpreter != SafeInterpreter) {
3131 #include "llvm/System/Path.h"
3232 #include "llvm/System/Signals.h"
3333 #include
34 #include
35 #include
3634 using namespace llvm;
3735
3836 namespace llvm {
144142 Module *NewM = runPassesOn(M, LoopExtractPasses);
145143 if (NewM == 0) {
146144 Module *Old = swapProgramIn(M);
147 std::cout << "*** Loop extraction failed: ";
145 outs() << "*** Loop extraction failed: ";
148146 EmitProgressBitcode("loopextraction", true);
149 std::cout << "*** Sorry. :( Please report a bug!\n";
147 outs() << "*** Sorry. :( Please report a bug!\n";
150148 swapProgramIn(Old);
151149 return 0;
152150 }
326324 sys::Path uniqueFilename("bugpoint-extractblocks");
327325 std::string ErrMsg;
328326 if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) {
329 std::cout << "*** Basic Block extraction failed!\n";
327 outs() << "*** Basic Block extraction failed!\n";
330328 errs() << "Error creating temporary file: " << ErrMsg << "\n";
331329 M = swapProgramIn(M);
332330 EmitProgressBitcode("basicblockextractfail", true);
335333 }
336334 sys::RemoveFileOnSignal(uniqueFilename);
337335
338 std::ofstream BlocksToNotExtractFile(uniqueFilename.c_str());
339 if (!BlocksToNotExtractFile) {
340 std::cout << "*** Basic Block extraction failed!\n";
341 errs() << "Error writing list of blocks to not extract: " << ErrMsg
336 std::string ErrorInfo;
337 raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(),
338 /*Binary=*/false, /*Force=*/true,
339 ErrorInfo);
340 if (!ErrorInfo.empty()) {
341 outs() << "*** Basic Block extraction failed!\n";
342 errs() << "Error writing list of blocks to not extract: " << ErrorInfo
342343 << "\n";
343344 M = swapProgramIn(M);
344345 EmitProgressBitcode("basicblockextractfail", true);
370371 free(ExtraArg);
371372
372373 if (Ret == 0) {
373 std::cout << "*** Basic Block extraction failed, please report a bug!\n";
374 outs() << "*** Basic Block extraction failed, please report a bug!\n";
374375 M = swapProgramIn(M);
375376 EmitProgressBitcode("basicblockextractfail", true);
376377 swapProgramIn(M);
1818 #include "llvm/Pass.h"
1919 #include
2020 #include
21 #include
2221 using namespace llvm;
2322
2423 /// runManyPasses - Take the specified pass list and create different
3029 ///
3130 bool BugDriver::runManyPasses(const std::vector &AllPasses) {
3231 setPassesToRun(AllPasses);
33 std::cout << "Starting bug finding procedure...\n\n";
32 outs() << "Starting bug finding procedure...\n\n";
3433
3534 // Creating a reference output if necessary
3635 if (initializeExecutionEnvironment()) return false;
3736
38 std::cout << "\n";
37 outs() << "\n";
3938 if (ReferenceOutputFile.empty()) {
40 std::cout << "Generating reference output from raw program: \n";
39 outs() << "Generating reference output from raw program: \n";
4140 if (!createReferenceFile(Program))
4241 return false;
4342 }
5453 //
5554 // Step 2: Run optimizer passes on the program and check for success.
5655 //
57 std::cout << "Running selected passes on program to test for crash: ";
56 outs() << "Running selected passes on program to test for crash: ";
5857 for(int i = 0, e = PassesToRun.size(); i != e; i++) {
59 std::cout << "-" << PassesToRun[i]->getPassArgument( )<< " ";
58 outs() << "-" << PassesToRun[i]->getPassArgument( )<< " ";
6059 }
6160
6261 std::string Filename;
6362 if(runPasses(PassesToRun, Filename, false)) {
64 std::cout << "\n";
65 std::cout << "Optimizer passes caused failure!\n\n";
63 outs() << "\n";
64 outs() << "Optimizer passes caused failure!\n\n";
6665 debugOptimizerCrash();
6766 return true;
6867 } else {
69 std::cout << "Combination " << num << " optimized successfully!\n";
68 outs() << "Combination " << num << " optimized successfully!\n";
7069 }
7170
7271 //
7372 // Step 3: Compile the optimized code.
7473 //
75 std::cout << "Running the code generator to test for a crash: ";
74 outs() << "Running the code generator to test for a crash: ";
7675 try {
7776 compileProgram(Program);
78 std::cout << '\n';
77 outs() << '\n';
7978 } catch (ToolExecutionError &TEE) {
80 std::cout << "\n*** compileProgram threw an exception: ";
81 std::cout << TEE.what();
79 outs() << "\n*** compileProgram threw an exception: ";
80 outs() << TEE.what();
8281 return debugCodeGeneratorCrash();
8382 }
8483
8685 // Step 4: Run the program and compare its output to the reference
8786 // output (created above).
8887 //
89 std::cout << "*** Checking if passes caused miscompliation:\n";
88 outs() << "*** Checking if passes caused miscompliation:\n";
9089 try {
9190 if (diffProgram(Filename, "", false)) {
92 std::cout << "\n*** diffProgram returned true!\n";
91 outs() << "\n*** diffProgram returned true!\n";
9392 debugMiscompilation();
9493 return true;
9594 } else {
96 std::cout << "\n*** diff'd output matches!\n";
95 outs() << "\n*** diff'd output matches!\n";
9796 }
9897 } catch (ToolExecutionError &TEE) {
9998 errs() << TEE.what();
103102
104103 sys::Path(Filename).eraseFromDisk();
105104
106 std::cout << "\n\n";
105 outs() << "\n\n";
107106 num++;
108107 } //end while
109108
2525 #include "llvm/Support/CommandLine.h"
2626 #include "llvm/Support/FileUtilities.h"
2727 #include "llvm/Config/config.h" // for HAVE_LINK_R
28 #include
2928 using namespace llvm;
3029
3130 namespace llvm {
5655 std::vector &Suffix) {
5756 // First, run the program with just the Suffix passes. If it is still broken
5857 // with JUST the kept passes, discard the prefix passes.
59 std::cout << "Checking to see if '" << getPassesString(Suffix)
60 << "' compiles correctly: ";
58 outs() << "Checking to see if '" << getPassesString(Suffix)
59 << "' compiles correctly: ";
6160
6261 std::string BitcodeResult;
6362 if (BD.runPasses(Suffix, BitcodeResult, false/*delete*/, true/*quiet*/)) {
7069
7170 // Check to see if the finished program matches the reference output...
7271 if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) {
73 std::cout << " nope.\n";
72 outs() << " nope.\n";
7473 if (Suffix.empty()) {
7574 errs() << BD.getToolName() << ": I'm confused: the test fails when "
7675 << "no passes are run, nondeterministic program?\n";
7877 }
7978 return KeepSuffix; // Miscompilation detected!
8079 }
81 std::cout << " yup.\n"; // No miscompilation!
80 outs() << " yup.\n"; // No miscompilation!
8281
8382 if (Prefix.empty()) return NoFailure;
8483
8584 // Next, see if the program is broken if we run the "prefix" passes first,
8685 // then separately run the "kept" passes.
87 std::cout << "Checking to see if '" << getPassesString(Prefix)
88 << "' compiles correctly: ";
86 outs() << "Checking to see if '" << getPassesString(Prefix)
87 << "' compiles correctly: ";
8988
9089 // If it is not broken with the kept passes, it's possible that the prefix
9190 // passes must be run before the kept passes to break it. If the program
103102
104103 // If the prefix maintains the predicate by itself, only keep the prefix!
105104 if (BD.diffProgram(BitcodeResult)) {
106 std::cout << " nope.\n";
105 outs() << " nope.\n";
107106 sys::Path(BitcodeResult).eraseFromDisk();
108107 return KeepPrefix;
109108 }
110 std::cout << " yup.\n"; // No miscompilation!
109 outs() << " yup.\n"; // No miscompilation!
111110
112111 // Ok, so now we know that the prefix passes work, try running the suffix
113112 // passes on the result of the prefix passes.
124123 if (Suffix.empty())
125124 return NoFailure;
126125
127 std::cout << "Checking to see if '" << getPassesString(Suffix)
126 outs() << "Checking to see if '" << getPassesString(Suffix)
128127 << "' passes compile correctly after the '"
129128 << getPassesString(Prefix) << "' passes: ";
130129
139138
140139 // Run the result...
141140 if (BD.diffProgram(BitcodeResult, "", true/*delete bitcode*/)) {
142 std::cout << " nope.\n";
141 outs() << " nope.\n";
143142 delete OriginalInput; // We pruned down the original input...
144143 return KeepSuffix;
145144 }
146145
147146 // Otherwise, we must not be running the bad pass anymore.
148 std::cout << " yup.\n"; // No miscompilation!
147 outs() << " yup.\n"; // No miscompilation!
149148 delete BD.swapProgramIn(OriginalInput); // Restore orig program & free test
150149 return NoFailure;
151150 }
212211 bool ReduceMiscompilingFunctions::TestFuncs(const std::vector&Funcs){
213212 // Test to see if the function is misoptimized if we ONLY run it on the
214213 // functions listed in Funcs.
215 std::cout << "Checking to see if the program is misoptimized when "
216 << (Funcs.size()==1 ? "this function is" : "these functions are")
217 << " run through the pass"
218 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
214 outs() << "Checking to see if the program is misoptimized when "
215 << (Funcs.size()==1 ? "this function is" : "these functions are")
216 << " run through the pass"
217 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
219218 PrintFunctionList(Funcs);
220 std::cout << '\n';
219 outs() << '\n';
221220
222221 // Split the module into the two halves of the program we want.
223222 DenseMap ValueMap;
309308 delete ToOptimize;
310309 BD.switchToInterpreter(AI);
311310
312 std::cout << " Testing after loop extraction:\n";
311 outs() << " Testing after loop extraction:\n";
313312 // Clone modules, the tester function will free them.
314313 Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
315314 Module *TNOBackup = CloneModule(ToNotOptimize);
316315 if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) {
317 std::cout << "*** Loop extraction masked the problem. Undoing.\n";
316 outs() << "*** Loop extraction masked the problem. Undoing.\n";
318317 // If the program is not still broken, then loop extraction did something
319318 // that masked the error. Stop loop extraction now.
320319 delete TOLEBackup;
324323 ToOptimizeLoopExtracted = TOLEBackup;
325324 ToNotOptimize = TNOBackup;
326325
327 std::cout << "*** Loop extraction successful!\n";
326 outs() << "*** Loop extraction successful!\n";
328327
329328 std::vector > MisCompFunctions;
330329 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
393392 bool ReduceMiscompiledBlocks::TestFuncs(const std::vector &BBs) {
394393 // Test to see if the function is misoptimized if we ONLY run it on the
395394 // functions listed in Funcs.
396 std::cout << "Checking to see if the program is misoptimized when all ";
395 outs() << "Checking to see if the program is misoptimized when all ";
397396 if (!BBs.empty()) {
398 std::cout << "but these " << BBs.size() << " blocks are extracted: ";
397 outs() << "but these " << BBs.size() << " blocks are extracted: ";
399398 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
400 std::cout << BBs[i]->getName() << " ";
401 if (BBs.size() > 10) std::cout << "...";
399 outs() << BBs[i]->getName() << " ";
400 if (BBs.size() > 10) outs() << "...";
402401 } else {
403 std::cout << "blocks are extracted.";
404 }
405 std::cout << '\n';
402 outs() << "blocks are extracted.";
403 }
404 outs() << '\n';
406405
407406 // Split the module into the two halves of the program we want.
408407 DenseMap ValueMap;
525524 if (!BugpointIsInterrupted)
526525 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
527526
528 std::cout << "\n*** The following function"
529 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
530 << " being miscompiled: ";
527 outs() << "\n*** The following function"
528 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
529 << " being miscompiled: ";
531530 PrintFunctionList(MiscompiledFunctions);
532 std::cout << '\n';
531 outs() << '\n';
533532
534533 // See if we can rip any loops out of the miscompiled functions and still
535534 // trigger the problem.
548547 if (!BugpointIsInterrupted)
549548 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
550549
551 std::cout << "\n*** The following function"
552 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
553 << " being miscompiled: ";
550 outs() << "\n*** The following function"
551 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
552 << " being miscompiled: ";
554553 PrintFunctionList(MiscompiledFunctions);
555 std::cout << '\n';
554 outs() << '\n';
556555 }
557556
558557 if (!BugpointIsInterrupted &&
568567 // Do the reduction...
569568 ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions);
570569
571 std::cout << "\n*** The following function"
572 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
573 << " being miscompiled: ";
570 outs() << "\n*** The following function"
571 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
572 << " being miscompiled: ";
574573 PrintFunctionList(MiscompiledFunctions);
575 std::cout << '\n';
574 outs() << '\n';
576575 }
577576
578577 return MiscompiledFunctions;
585584 static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
586585 // Run the optimization passes on ToOptimize, producing a transformed version
587586 // of the functions being tested.
588 std::cout << " Optimizing functions being tested: ";
587 outs() << " Optimizing functions being tested: ";
589588 Module *Optimized = BD.runPassesOn(Test, BD.getPassesToRun(),
590589 /*AutoDebugCrashes*/true);
591 std::cout << "done.\n";
590 outs() << "done.\n";
592591 delete Test;
593592
594 std::cout << " Checking to see if the merged program executes correctly: ";
593 outs() << " Checking to see if the merged program executes correctly: ";
595594 bool Broken = TestMergedProgram(BD, Optimized, Safe, true);
596 std::cout << (Broken ? " nope.\n" : " yup.\n");
595 outs() << (Broken ? " nope.\n" : " yup.\n");
597596 return Broken;
598597 }
599598
611610 return false;
612611 }
613612
614 std::cout << "\n*** Found miscompiling pass"
615 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
616 << getPassesString(getPassesToRun()) << '\n';
613 outs() << "\n*** Found miscompiling pass"
614 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
615 << getPassesString(getPassesToRun()) << '\n';
617616 EmitProgressBitcode("passinput");
618617
619618 std::vector MiscompiledFunctions =
620619 DebugAMiscompilation(*this, TestOptimizer);
621620
622621 // Output a bunch of bitcode files for the user...
623 std::cout << "Outputting reduced bitcode files which expose the problem:\n";
622 outs() << "Outputting reduced bitcode files which expose the problem:\n";
624623 DenseMap ValueMap;
625624 Module *ToNotOptimize = CloneModule(getProgram(), ValueMap);
626625 Module *ToOptimize = SplitFunctionsOutOfModule(ToNotOptimize,
627626 MiscompiledFunctions,
628627 ValueMap);
629628
630 std::cout << " Non-optimized portion: ";
629 outs() << " Non-optimized portion: ";
631630 ToNotOptimize = swapProgramIn(ToNotOptimize);
632631 EmitProgressBitcode("tonotoptimize", true);
633632 setNewProgram(ToNotOptimize); // Delete hacked module.
634633
635 std::cout << " Portion that is input to optimizer: ";
634 outs() << " Portion that is input to optimizer: ";
636635 ToOptimize = swapProgramIn(ToOptimize);
637636 EmitProgressBitcode("tooptimize");
638637 setNewProgram(ToOptimize); // Delete hacked module.
859858 bool BugDriver::debugCodeGenerator() {
860859 if ((void*)SafeInterpreter == (void*)Interpreter) {
861860 std::string Result = executeProgramSafely("bugpoint.safe.out");
862 std::cout << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
863 << "the reference diff. This may be due to a\n front-end "
864 << "bug or a bug in the original program, but this can also "
865 << "happen if bugpoint isn't running the program with the "
866 << "right flags or input.\n I left the result of executing "
867 << "the program with the \"safe\" backend in this file for "
868 << "you: '"
869 << Result << "'.\n";
861 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
862 << "the reference diff. This may be due to a\n front-end "
863 << "bug or a bug in the original program, but this can also "
864 << "happen if bugpoint isn't running the program with the "
865 << "right flags or input.\n I left the result of executing "
866 << "the program with the \"safe\" backend in this file for "
867 << "you: '"
868 << Result << "'.\n";
870869 return true;
871870 }
872871
911910 std::string SharedObject = compileSharedObject(SafeModuleBC.toString());
912911 delete ToNotCodeGen;
913912
914 std::cout << "You can reproduce the problem with the command line: \n";
913 outs() << "You can reproduce the problem with the command line: \n";
915914 if (isExecutingJIT()) {
916 std::cout << " lli -load " << SharedObject << " " << TestModuleBC;
915 outs() << " lli -load " << SharedObject << " " << TestModuleBC;
917916 } else {
918 std::cout << " llc -f " << TestModuleBC << " -o " << TestModuleBC<< ".s\n";
919 std::cout << " gcc " << SharedObject << " " << TestModuleBC
917 outs() << " llc -f " << TestModuleBC << " -o " << TestModuleBC<< ".s\n";
918 outs() << " gcc " << SharedObject << " " << TestModuleBC
920919 << ".s -o " << TestModuleBC << ".exe";
921920 #if defined (HAVE_LINK_R)
922 std::cout << " -Wl,-R.";
921 outs() << " -Wl,-R.";
923922 #endif
924 std::cout << "\n";
925 std::cout << " " << TestModuleBC << ".exe";
923 outs() << "\n";
924 outs() << " " << TestModuleBC << ".exe";
926925 }
927926 for (unsigned i=0, e = InputArgv.size(); i != e; ++i)
928 std::cout << " " << InputArgv[i];
929 std::cout << '\n';
930 std::cout << "The shared object was created with:\n llc -march=c "
931 << SafeModuleBC << " -o temporary.c\n"
932 << " gcc -xc temporary.c -O2 -o " << SharedObject
927 outs() << " " << InputArgv[i];
928 outs() << '\n';
929 outs() << "The shared object was created with:\n llc -march=c "
930 << SafeModuleBC << " -o temporary.c\n"
931 << " gcc -xc temporary.c -O2 -o " << SharedObject
933932 #if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
934 << " -G" // Compile a shared library, `-G' for Sparc
933 << " -G" // Compile a shared library, `-G' for Sparc
935934 #else
936 << " -fPIC -shared" // `-shared' for Linux/X86, maybe others
935 << " -fPIC -shared" // `-shared' for Linux/X86, maybe others
937936 #endif
938 << " -fno-strict-aliasing\n";
937 << " -fno-strict-aliasing\n";
939938
940939 return false;
941940 }
2626 #include "llvm/Target/TargetData.h"
2727 #include "llvm/Support/FileUtilities.h"
2828 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Streams.h"
3029 #include "llvm/System/Path.h"
3130 #include "llvm/System/Program.h"
3231 #include "llvm/Config/alloca.h"
7069 //
7170 std::string Filename = "bugpoint-" + ID + ".bc";
7271 if (writeProgramToFile(Filename)) {
73 cerr << "Error opening file '" << Filename << "' for writing!\n";
72 errs() << "Error opening file '" << Filename << "' for writing!\n";
7473 return;
7574 }
7675
77 cout << "Emitted bitcode to '" << Filename << "'\n";
76 outs() << "Emitted bitcode to '" << Filename << "'\n";
7877 if (NoFlyer || PassesToRun.empty()) return;
79 cout << "\n*** You can reproduce the problem with: ";
80 cout << "opt " << Filename << " ";
81 cout << getPassesString(PassesToRun) << "\n";
78 outs() << "\n*** You can reproduce the problem with: ";
79 outs() << "opt " << Filename << " ";
80 outs() << getPassesString(PassesToRun) << "\n";
8281 }
8382
8483 int BugDriver::runPassesAsChild(const std::vector &Passes) {
8786 std::ios::binary;
8887 std::ofstream OutFile(ChildOutput.c_str(), io_mode);
8988 if (!OutFile.good()) {
90 cerr << "Error opening bitcode file: " << ChildOutput << "\n";
89 errs() << "Error opening bitcode file: " << ChildOutput << "\n";
9190 return 1;
9291 }
9392
9998 if (Passes[i]->getNormalCtor())
10099 PM.add(Passes[i]->getNormalCtor()());
101100 else
102 cerr << "Cannot create pass yet: " << Passes[i]->getPassName() << "\n";
101 errs() << "Cannot create pass yet: " << Passes[i]->getPassName() << "\n";
103102 }
104103 // Check that the module is well formed on completion of optimization
105104 PM.add(createVerifierPass());
120119 /// optimizations fail for some reason (optimizer crashes), return true,
121120 /// otherwise return false. If DeleteOutput is set to true, the bitcode is
122121 /// deleted on success, and the filename string is undefined. This prints to
123 /// cout a single line message indicating whether compilation was successful or
124 /// failed.
122 /// outs() a single line message indicating whether compilation was successful
123 /// or failed.
125124 ///
126125 bool BugDriver::runPasses(const std::vector &Passes,
127126 std::string &OutputFilename, bool DeleteOutput,
128127 bool Quiet, unsigned NumExtraArgs,
129128 const char * const *ExtraArgs) const {
130129 // setup the output file name
131 cout << std::flush;
130 outs().flush();
132131 sys::Path uniqueFilename("bugpoint-output.bc");
133132 std::string ErrMsg;
134133 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
135 cerr << getToolName() << ": Error making unique filename: "
136 << ErrMsg << "\n";
134 errs() << getToolName() << ": Error making unique filename: "
135 << ErrMsg << "\n";
137136 return(1);
138137 }
139138 OutputFilename = uniqueFilename.toString();
141140 // set up the input file name
142141 sys::Path inputFilename("bugpoint-input.bc");
143142 if (inputFilename.makeUnique(true, &ErrMsg)) {
144 cerr << getToolName() << ": Error making unique filename: "
145 << ErrMsg << "\n";
143 errs() << getToolName() << ": Error making unique filename: "
144 << ErrMsg << "\n";
146145 return(1);
147146 }
148147 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
149148 std::ios::binary;
150149 std::ofstream InFile(inputFilename.c_str(), io_mode);
151150 if (!InFile.good()) {
152 cerr << "Error opening bitcode file: " << inputFilename << "\n";
151 errs() << "Error opening bitcode file: " << inputFilename << "\n";
153152 return(1);
154153 }
155154 WriteBitcodeToFile(Program, InFile);
211210
212211 if (!Quiet) {
213212 if (result == 0)
214 cout << "Success!\n";
213 outs() << "Success!\n";
215214 else if (result > 0)
216 cout << "Exited with error code '" << result << "'\n";
215 outs() << "Exited with error code '" << result << "'\n";
217216 else if (result < 0) {
218217 if (result == -1)
219 cout << "Execute failed: " << ErrMsg << "\n";
218 outs() << "Execute failed: " << ErrMsg << "\n";
220219 else
221 cout << "Crashed with signal #" << abs(result) << "\n";
220 outs() << "Crashed with signal #" << abs(result) << "\n";
222221 }
223222 if (result & 0x01000000)
224 cout << "Dumped core\n";
223 outs() << "Dumped core\n";
225224 }
226225
227226 // Was the child successful?
241240 if (runPasses(Passes, BitcodeResult, false/*delete*/, true/*quiet*/,
242241 NumExtraArgs, ExtraArgs)) {
243242 if (AutoDebugCrashes) {
244 cerr << " Error running this sequence of passes"
245 << " on the input program!\n";
243 errs() << " Error running this sequence of passes"
244 << " on the input program!\n";
246245 delete OldProgram;
247246 EmitProgressBitcode("pass-error", false);
248247 exit(debugOptimizerCrash());
256255
257256 Module *Ret = ParseInputFile(BitcodeResult, Context);
258257 if (Ret == 0) {
259 cerr << getToolName() << ": Error reading bitcode file '"
260 << BitcodeResult << "'!\n";
258 errs() << getToolName() << ": Error reading bitcode file '"
259 << BitcodeResult << "'!\n";
261260 exit(1);
262261 }
263262 sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
2424 #include "llvm/System/Process.h"
2525 #include "llvm/System/Signals.h"
2626 #include "llvm/LinkAllVMCore.h"
27 #include
2827 using namespace llvm;
2928
3029 // AsChild - Specifies that this invocation of bugpoint is being generated
2323 #include "llvm/Support/ManagedStatic.h"
2424 #include "llvm/Support/PrettyStackTrace.h"
2525 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/Streams.h"
2726 #include "llvm/Support/SystemUtils.h"
2827 #include "llvm/Support/raw_ostream.h"
2928 #include "llvm/System/Signals.h"
7271 if (!DisableVerify) {
7372 std::string Err;
7473 if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
75 cerr << argv[0]
76 << ": assembly parsed, but does not verify as correct!\n";
77 cerr << Err;
74 errs() << argv[0]
75 << ": assembly parsed, but does not verify as correct!\n";
76 errs() << Err;
7877 return 1;
7978 }
8079 }
8180
82 if (DumpAsm) cerr << "Here's the assembly:\n" << *M.get();
81 if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
8382
8483 if (OutputFilename != "") { // Specified an output filename?
8584 if (OutputFilename != "-") { // Not stdout?
132131 if (Force || !CheckBitcodeOutputToConsole(Out,true))
133132 WriteBitcodeToFile(M.get(), *Out);
134133 } catch (const std::string& msg) {
135 cerr << argv[0] << ": " << msg << "\n";
134 errs() << argv[0] << ": " << msg << "\n";
136135 exitCode = 1;
137136 } catch (...) {
138 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
137 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
139138 exitCode = 1;
140139 }
141140
3636 #include "llvm/Support/PrettyStackTrace.h"
3737 #include "llvm/System/Signals.h"
3838 #include
39 #include
40 #include
4139 #include
4240 using namespace llvm;
4341
2424 #include "llvm/Support/ManagedStatic.h"
2525 #include "llvm/Support/MemoryBuffer.h"
2626 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Streams.h"
2827 #include "llvm/Support/raw_ostream.h"
2928 #include "llvm/System/Signals.h"
3029 #include
6564 }
6665
6766 if (M.get() == 0) {
68 cerr << argv[0] << ": ";
67 errs() << argv[0] << ": ";
6968 if (ErrorMessage.size())
70 cerr << ErrorMessage << "\n";
69 errs() << ErrorMessage << "\n";
7170 else
72 cerr << "bitcode didn't read correctly.\n";
71 errs() << "bitcode didn't read correctly.\n";
7372 return 1;
7473 }
7574
129128 delete Out;
130129 return 0;
131130 } catch (const std::string& msg) {
132 cerr << argv[0] << ": " << msg << "\n";
131 errs() << argv[0] << ": " << msg << "\n";
133132 } catch (...) {
134 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
133 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
135134 }
136135
137136 return 1;
6868
6969 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename);
7070 if (Buffer == 0) {
71 cerr << argv[0] << ": Error reading file '" + InputFilename + "'\n";
71 errs() << argv[0] << ": Error reading file '" + InputFilename + "'\n";
7272 return 1;
7373 } else {
7474 M.reset(ParseBitcodeFile(Buffer, Context));
7676 delete Buffer;
7777
7878 if (M.get() == 0) {
79 cerr << argv[0] << ": bitcode didn't read correctly.\n";
79 errs() << argv[0] << ": bitcode didn't read correctly.\n";
8080 return 1;
8181 }
8282
8989 Function *F = M.get()->getFunction(ExtractFunc);
9090
9191 if (F == 0 && G == 0) {
92 cerr << argv[0] << ": program doesn't contain function named '"
93 << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
92 errs() << argv[0] << ": program doesn't contain function named '"
93 << ExtractFunc << "' or a global named '" << ExtractGlobal << "'!\n";
9494 return 1;
9595 }
9696
2525 #include "llvm/Transforms/Scalar.h"
2626 #include "llvm/Support/PassNameParser.h"
2727 #include "llvm/Support/PluginLoader.h"
28 #include
2928 using namespace llvm;
3029
3130 // Pass Name Options as generated by the PassNameParser
3333 #include "llvm/Support/ManagedStatic.h"
3434 #include "llvm/Support/MemoryBuffer.h"
3535 #include "llvm/Support/PrettyStackTrace.h"
36 #include "llvm/Support/Streams.h"
3736 #include "llvm/Support/SystemUtils.h"
3837 #include "llvm/System/Signals.h"
3938 #include "llvm/Config/config.h"
40 #include
4139 #include
4240 #include
4341 using namespace llvm;
122120 /// Message - The message to print to standard error.
123121 ///
124122 static void PrintAndExit(const std::string &Message, int errcode = 1) {
125 cerr << progname << ": " << Message << "\n";
123 errs() << progname << ": " << Message << "\n";
126124 llvm_shutdown();
127125 exit(errcode);
128126 }
131129 std::vector::const_iterator I = args.begin(), E = args.end();
132130 for (; I != E; ++I)
133131 if (*I)
134 cout << "'" << *I << "'" << " ";
135 cout << "\n" << std::flush;
132 outs() << "'" << *I << "'" << " ";
133 outs() << "\n"; outs().flush();
136134 }
137135
138136 /// CopyEnv - This function takes an array of environment variables and makes a
217215 void GenerateBitcode(Module* M, const std::string& FileName) {
218216
219217 if (Verbose)
220 cout << "Generating Bitcode To " << FileName << '\n';
218 outs() << "Generating Bitcode To " << FileName << '\n';
221219
222220 // Create the output file.
223 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
224 std::ios::binary;
225 std::ofstream Out(FileName.c_str(), io_mode);
226 if (!Out.good())
227 PrintAndExit("error opening '" + FileName + "' for writing!");
221 std::string ErrorInfo;
222 raw_fd_ostream Out(FileName.c_str(), /*Binary=*/true, /*Force=*/true,
223 ErrorInfo);
224 if (!ErrorInfo.empty())
225 PrintAndExit(ErrorInfo);
228226
229227 // Ensure that the bitcode file gets removed from the disk if we get a
230228 // terminating signal.
265263 args.push_back(0);
266264
267265 if (Verbose) {
268 cout << "Generating Assembly With: \n";
266 outs() << "Generating Assembly With: \n";
269267 PrintCommand(args);
270268 }
271269
288286 args.push_back(0);
289287
290288 if (Verbose) {
291 cout << "Generating C Source With: \n";
289 outs() << "Generating C Source With: \n";
292290 PrintCommand(args);
293291 }
294292
385383 Args.push_back(0);
386384
387385 if (Verbose) {
388 cout << "Generating Native Executable With:\n";
386 outs() << "Generating Native Executable With:\n";
389387 PrintCommand(Args);
390388 }
391389
400398 /// bitcode file for the program.
401399 static void EmitShellScript(char **argv) {
402400 if (Verbose)
403 cout << "Emitting Shell Script\n";
401 outs() << "Emitting Shell Script\n";
404402 #if defined(_WIN32) || defined(__CYGWIN__)
405403 // Windows doesn't support #!/bin/sh style shell scripts in .exe files. To
406404 // support windows systems, we copy the llvm-stub.exe executable from the
417415 #endif
418416
419417 // Output the script to start the program...
420 std::ofstream Out2(OutputFilename.c_str());
421 if (!Out2.good())
422 PrintAndExit("error opening '" + OutputFilename + "' for writing!");
418 std::string ErrorInfo;
419 raw_fd_ostream Out2(OutputFilename.c_str(), /*Binary=*/false, /*Force=*/true,
420 ErrorInfo);
421 if (!ErrorInfo.empty())
422 PrintAndExit(ErrorInfo);
423423
424424 Out2 << "#!/bin/sh\n";
425425 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
2020 #include "llvm/Support/ManagedStatic.h"
2121 #include "llvm/Support/MemoryBuffer.h"
2222 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Streams.h"
2423 #include "llvm/System/Signals.h"
2524 #include "llvm/System/Path.h"
2625 #include
4948 LLVMContext& Context) {
5049 sys::Path Filename;
5150 if (!Filename.set(FN)) {
52 cerr << "Invalid file name: '" << FN << "'\n";
51 errs() << "Invalid file name: '" << FN << "'\n";
5352 return std::auto_ptr();
5453 }
5554
5655 std::string ErrorMessage;
5756 if (Filename.exists()) {
58 if (Verbose) cerr << "Loading '" << Filename.c_str() << "'\n";
57 if (Verbose) errs() << "Loading '" << Filename.c_str() << "'\n";
5958 Module* Result = 0;
6059
6160 const std::string &FNStr = Filename.toString();
6766 if (Result) return std::auto_ptr(Result); // Load successful!
6867
6968 if (Verbose) {
70 cerr << "Error opening bitcode file: '" << Filename.c_str() << "'";
71 if (ErrorMessage.size()) cerr << ": " << ErrorMessage;
72 cerr << "\n";
69 errs() << "Error opening bitcode file: '" << Filename.c_str() << "'";
70 if (ErrorMessage.size()) errs() << ": " << ErrorMessage;
71 errs() << "\n";
7372 }
7473 } else {
75 cerr << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
74 errs() << "Bitcode file: '" << Filename.c_str() << "' does not exist.\n";
7675 }
7776
7877 return std::auto_ptr();
9291
9392 std::auto_ptr Composite(LoadFile(InputFilenames[BaseArg], Context));
9493 if (Composite.get() == 0) {
95 cerr << argv[0] << ": error loading file '"
96 << InputFilenames[BaseArg] << "'\n";
94 errs() << argv[0] << ": error loading file '"
95 << InputFilenames[BaseArg] << "'\n";
9796 return 1;
9897 }
9998
10099 for (unsigned i = BaseArg+1; i < InputFilenames.size(); ++i) {
101100 std::auto_ptr M(LoadFile(InputFilenames[i], Context));
102101 if (M.get() == 0) {
103 cerr << argv[0] << ": error loading file '" <
102 errs() << argv[0] << ": error loading file '" <
104103 return 1;
105104 }
106105
107 if (Verbose) cerr << "Linking in '" << InputFilenames[i] << "'\n";
106 if (Verbose) errs() << "Linking in '" << InputFilenames[i] << "'\n";
108107
109108 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
110 cerr << argv[0] << ": link error in '" << InputFilenames[i]
111 << "': " << ErrorMessage << "\n";
109 errs() << argv[0] << ": link error in '" << InputFilenames[i]
110 << "': " << ErrorMessage << "\n";
112111 return 1;
113112 }
114113 }
116115 // TODO: Iterate over the -l list and link in any modules containing
117116 // global symbols that have not been resolved so far.
118117
119 if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
118 if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite.get();
120119
121120 // FIXME: outs() is not binary!
122121 raw_ostream *Out = &outs(); // Default to printing to stdout...
138137 }
139138
140139 if (verifyModule(*Composite.get())) {
141 cerr << argv[0] << ": linked module is broken!\n";
140 errs() << argv[0] << ": linked module is broken!\n";
142141 return 1;
143142 }
144143
145 if (Verbose) cerr << "Writing bitcode...\n";
144 if (Verbose) errs() << "Writing bitcode...\n";
146145 WriteBitcodeToFile(Composite.get(), *Out);
147146
148147 if (Out != &outs()) delete Out;
2929 #include
3030 #include
3131 #include
32 #include
3332 using namespace llvm;
3433
3534 namespace {
9998 if (GV.hasLocalLinkage () && ExternalOnly)
10099 return;
101100 if (OutputFormat == posix) {
102 std::cout << GV.getName () << " " << TypeCharForSymbol(GV) << " "
103 << SymbolAddrStr << "\n";
101 outs() << GV.getName () << " " << TypeCharForSymbol(GV) << " "
102 << SymbolAddrStr << "\n";
104103 } else if (OutputFormat == bsd) {
105 std::cout << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
106 << GV.getName () << "\n";
104 outs() << SymbolAddrStr << " " << TypeCharForSymbol(GV) << " "
105 << GV.getName () << "\n";
107106 } else if (OutputFormat == sysv) {
108107 std::string PaddedName (GV.getName ());
109108 while (PaddedName.length () < 20)
110109 PaddedName += " ";
111 std::cout << PaddedName << "|" << SymbolAddrStr << "| "
112 << TypeCharForSymbol(GV)
113 << " | | | |\n";
110 outs() << PaddedName << "|" << SymbolAddrStr << "| "
111 << TypeCharForSymbol(GV)
112 << " | | | |\n";
114113 }
115114 }
116115
117116 static void DumpSymbolNamesFromModule(Module *M) {
118117 const std::string &Filename = M->getModuleIdentifier ();
119118 if (OutputFormat == posix && MultipleFiles) {
120 std::cout << Filename << ":\n";
119 outs() << Filename << ":\n";
121120 } else if (OutputFormat == bsd && MultipleFiles) {
122 std::cout << "\n" << Filename << ":\n";
121 outs() << "\n" << Filename << ":\n";
123122 } else if (OutputFormat == sysv) {
124 std::cout << "\n\nSymbols from " << Filename << ":\n\n"
125 << "Name Value Class Type"
126 << " Size Line Section\n";
123 outs() << "\n\nSymbols from " << Filename << ":\n\n"
124 << "Name Value Class Type"
125 << " Size Line Section\n";
127126 }
128127 std::for_each (M->begin(), M->end(), DumpSymbolNameForGlobalValue);
129128 std::for_each (M->global_begin(), M->global_end(),
1212
1313 #include "llvm/CompilerDriver/CompilationGraph.h"
1414 #include "llvm/CompilerDriver/Plugin.h"
15
16 #include
15 #include "llvm/Support/raw_ostream.h"
1716
1817 namespace {
1918 struct MyPlugin : public llvmc::BasePlugin {
2019 void PopulateLanguageMap(llvmc::LanguageMap&) const
21 { std::cout << "Hello!\n"; }
20 { outs() << "Hello!\n"; }
2221
2322 void PopulateCompilationGraph(llvmc::CompilationGraph&) const
2423 {}
2929 #include "llvm/Target/TargetAsmInfo.h"
3030 #include "llvm/Target/TargetMachine.h"
3131 #include "llvm/Target/TargetRegistry.h"
32
33 #include
3432
3533 using namespace llvm;
3634
2828 #include "llvm/Module.h"
2929 #include "llvm/Analysis/CallGraph.h"
3030 #include "llvm/Support/CFG.h"
31 #include "llvm/Support/raw_ostream.h"
3132 #include "llvm/ADT/SCCIterator.h"
32 #include
3333 using namespace llvm;
3434
3535 namespace {
7272
7373 bool CFGSCC::runOnFunction(Function &F) {
7474 unsigned sccNum = 0;
75 std::cout << "SCCs for Function " << F.getName() << " in PostOrder:";
75 outs() << "SCCs for Function " << F.getName() << " in PostOrder:";
7676 for (scc_iterator SCCI = scc_begin(&F),
7777 E = scc_end(&F); SCCI != E; ++SCCI) {
7878 std::vector &nextSCC = *SCCI;
79 std::cout << "\nSCC #" << ++sccNum << " : ";
79 outs() << "\nSCC #" << ++sccNum << " : ";
8080 for (std::vector::const_iterator I = nextSCC.begin(),
8181 E = nextSCC.end(); I != E; ++I)
82 std::cout << (*I)->getName() << ", ";
82 outs() << (*I)->getName() << ", ";
8383 if (nextSCC.size() == 1 && SCCI.hasLoop())
84 std::cout << " (Has self-loop).";
84 outs() << " (Has self-loop).";
8585 }
86 std::cout << "\n";
86 outs() << "\n";
8787
8888 return true;
8989 }
9393 bool CallGraphSCC::runOnModule(Module &M) {
9494 CallGraphNode* rootNode = getAnalysis().getRoot();
9595 unsigned sccNum = 0;
96 std::cout << "SCCs for the program in PostOrder:";
96 outs() << "SCCs for the program in PostOrder:";
9797 for (scc_iterator SCCI = scc_begin(rootNode),
9898 E = scc_end(rootNode); SCCI != E; ++SCCI) {
9999 const std::vector &nextSCC = *SCCI;
100 std::cout << "\nSCC #" << ++sccNum << " : ";
100 outs() << "\nSCC #" << ++sccNum << " : ";
101101 for (std::vector::const_iterator I = nextSCC.begin(),
102102 E = nextSCC.end(); I != E; ++I)
103 std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
104 : std::string("Indirect CallGraph node")) << ", ";
103 outs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
104 : std::string("Indirect CallGraph node")) << ", ";
105105 if (nextSCC.size() == 1 && SCCI.hasLoop())
106 std::cout << " (Has self-loop).";
106 outs() << " (Has self-loop).";
107107 }
108 std::cout << "\n";
108 outs() << "\n";
109109
110110 return true;
111111 }
2929 #include "llvm/Support/MemoryBuffer.h"
3030 #include "llvm/Support/PluginLoader.h"
3131 #include "llvm/Support/StandardPasses.h"
32 #include "llvm/Support/Streams.h"
3332 #include "llvm/Support/SystemUtils.h"
3433 #include "llvm/Support/raw_ostream.h"
3534 #include "llvm/LinkAllPasses.h"
125124
126125 virtual bool runOnSCC(const std::vector&SCC) {
127126 if (!Quiet) {
128 cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
127 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
129128
130129 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
131130 Function *F = SCC[i]->getFunction();
132 if (F)
131 if (F) {
132 outs().flush();
133133 getAnalysisID(PassToPrint).print(cout, F->getParent());
134 cout << std::flush;
135 }
134136 }
135137 }
136138 // Get and print pass...
155157
156158 virtual bool runOnModule(Module &M) {
157159 if (!Quiet) {
158 cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
160 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
161 outs().flush();
159162 getAnalysisID(PassToPrint).print(cout, &M);
163 cout << std::flush;
160164 }
161165
162166 // Get and print pass...
180184
181185 virtual bool runOnFunction(Function &F) {
182186 if (!Quiet) {
183 cout << "Printing analysis '" << PassToPrint->getPassName()
184 << "' for function '" << F.getName() << "':\n";
187 outs() << "Printing analysis '" << PassToPrint->getPassName()
188 << "' for function '" << F.getName() << "':\n";
185189 }
186190 // Get and print pass...
191 outs().flush();
187192 getAnalysisID(PassToPrint).print(cout, F.getParent());
193 cout << std::flush;
188194 return false;
189195 }
190196
206212
207213 virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
208214 if (!Quiet) {
209 cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
210 getAnalysisID(PassToPrint).print(cout,
215 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
216 outs().flush();
217 getAnalysisID(PassToPrint).print(cout,
211218 L->getHeader()->getParent()->getParent());
219 cout << std::flush;
212220 }
213221 // Get and print pass...
214222 return false;
232240
233241 virtual bool runOnBasicBlock(BasicBlock &BB) {
234242 if (!Quiet) {
235 cout << "Printing Analysis info for BasicBlock '" << BB.getName()
236 << "': Pass " << PassToPrint->getPassName() << ":\n";
243 outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
244 << "': Pass " << PassToPrint->getPassName() << ":\n";
237245 }
238246
239247 // Get and print pass...
248 outs().flush();
240249 getAnalysisID(PassToPrint).print(cout, BB.getParent()->getParent());
250 cout << std::flush;
241251 return false;
242252 }
243253
329339 }
330340
331341 if (M.get() == 0) {
332 cerr << argv[0] << ": ";
342 errs() << argv[0] << ": ";
333343 if (ErrorMessage.size())
334 cerr << ErrorMessage << "\n";
344 errs() << ErrorMessage << "\n";
335345 else
336 cerr << "bitcode didn't read correctly.\n";
346 errs() << "bitcode didn't read correctly.\n";
337347 return 1;
338348 }
339349
340350 // Figure out what stream we are supposed to write to...
341 // FIXME: cout is not binary!
351 // FIXME: outs() is not binary!
342352 raw_ostream *Out = &outs(); // Default to printing to stdout...
343353 if (OutputFilename != "-") {
344354 std::string ErrorInfo;
413423 if (PassInf->getNormalCtor())
414424 P = PassInf->getNormalCtor()();
415425 else
416 cerr << argv[0] << ": cannot create pass: "
417 << PassInf->getPassName() << "\n";
426 errs() << argv[0] << ": cannot create pass: "
427 << PassInf->getPassName() << "\n";
418428 if (P) {
419429 bool isBBPass = dynamic_cast(P) != 0;
420430 bool isLPass = !isBBPass && dynamic_cast(P) != 0;
469479 if (!NoVerify && !VerifyEach)
470480 Passes.add(createVerifierPass());
471481
472 // Write bitcode out to disk or cout as the last step...
482 // Write bitcode out to disk or outs() as the last step...
473483 if (!NoOutput && !AnalyzeOnly)
474484 Passes.add(createBitcodeWriterPass(*Out));
475485
476486 // Now that we have all of the passes ready, run them.
477487 Passes.run(*M.get());
478488
479 // Delete the ofstream.
489 // Delete the raw_fd_ostream.
480490 if (Out != &outs())
481491 delete Out;
482492 return 0;
483493
484494 } catch (const std::string& msg) {
485 cerr << argv[0] << ": " << msg << "\n";
495 errs() << argv[0] << ": " << msg << "\n";
486496 } catch (...) {
487 cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
497 errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
488498 }
489499 llvm_shutdown();
490500 return 1;