OLD | NEW |
1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// | 1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This is the llc code generator driver. It provides a convenient | 10 // This is the llc code generator driver. It provides a convenient |
11 // command-line interface for generating native assembly-language code | 11 // command-line interface for generating native assembly-language code |
12 // or C code, given LLVM bitcode. | 12 // or C code, given LLVM bitcode. |
13 // | 13 // |
14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
15 | 15 |
16 #include "llvm/IR/LLVMContext.h" | 16 #include "llvm/IR/LLVMContext.h" |
17 #include "llvm/ADT/Triple.h" | 17 #include "llvm/ADT/Triple.h" |
| 18 #include "llvm/Analysis/NaCl.h" |
18 #include "llvm/Assembly/PrintModulePass.h" | 19 #include "llvm/Assembly/PrintModulePass.h" |
19 #include "llvm/Support/DataStream.h" // @LOCALMOD | 20 #include "llvm/Support/DataStream.h" // @LOCALMOD |
20 #include "llvm/CodeGen/CommandFlags.h" | 21 #include "llvm/CodeGen/CommandFlags.h" |
21 #include "llvm/CodeGen/IntrinsicLowering.h" // @LOCALMOD | 22 #include "llvm/CodeGen/IntrinsicLowering.h" // @LOCALMOD |
22 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" | 23 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" |
23 #include "llvm/CodeGen/LinkAllCodegenComponents.h" | 24 #include "llvm/CodeGen/LinkAllCodegenComponents.h" |
24 #include "llvm/IR/DataLayout.h" | 25 #include "llvm/IR/DataLayout.h" |
25 #include "llvm/IR/Module.h" | 26 #include "llvm/IR/Module.h" |
26 #include "llvm/MC/SubtargetFeature.h" | 27 #include "llvm/MC/SubtargetFeature.h" |
27 #include "llvm/Pass.h" | 28 #include "llvm/Pass.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 101 |
101 // The option below overlaps very much with bitcode streaming. | 102 // The option below overlaps very much with bitcode streaming. |
102 // We keep it separate because it is still experimental and we want | 103 // We keep it separate because it is still experimental and we want |
103 // to use it without changing the outside behavior which is especially | 104 // to use it without changing the outside behavior which is especially |
104 // relevant for the sandboxed case. | 105 // relevant for the sandboxed case. |
105 static cl::opt<bool> | 106 static cl::opt<bool> |
106 ReduceMemoryFootprint("reduce-memory-footprint", | 107 ReduceMemoryFootprint("reduce-memory-footprint", |
107 cl::desc("Aggressively reduce memory used by llc"), | 108 cl::desc("Aggressively reduce memory used by llc"), |
108 cl::init(false)); | 109 cl::init(false)); |
109 | 110 |
| 111 static cl::opt<bool> |
| 112 PNaClABIVerify("pnaclabi-verify", |
| 113 cl::desc("Verify PNaCl bitcode ABI before translating"), |
| 114 cl::init(false)); |
| 115 static cl::opt<bool> |
| 116 PNaClABIVerifyFatalErrors("pnaclabi-verify-fatal-errors", |
| 117 cl::desc("PNaCl ABI verification errors are fatal"), |
| 118 cl::init(false)); |
110 // @LOCALMOD-END | 119 // @LOCALMOD-END |
111 | 120 |
112 // Determine optimization level. | 121 // Determine optimization level. |
113 static cl::opt<char> | 122 static cl::opt<char> |
114 OptLevel("O", | 123 OptLevel("O", |
115 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " | 124 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
116 "(default = '-O2')"), | 125 "(default = '-O2')"), |
117 cl::Prefix, | 126 cl::Prefix, |
118 cl::ZeroOrMore, | 127 cl::ZeroOrMore, |
119 cl::init(' ')); | 128 cl::init(' ')); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); | 309 cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); |
301 | 310 |
302 // Compile the module TimeCompilations times to give better compile time | 311 // Compile the module TimeCompilations times to give better compile time |
303 // metrics. | 312 // metrics. |
304 for (unsigned I = TimeCompilations; I; --I) | 313 for (unsigned I = TimeCompilations; I; --I) |
305 if (int RetVal = compileModule(argv, Context)) | 314 if (int RetVal = compileModule(argv, Context)) |
306 return RetVal; | 315 return RetVal; |
307 return 0; | 316 return 0; |
308 } | 317 } |
309 | 318 |
| 319 // @LOCALMOD-BEGIN |
| 320 static void CheckABIVerifyErrors(PNaClABIErrorReporter &Reporter, |
| 321 const Twine &Name) { |
| 322 if (PNaClABIVerify && Reporter.getErrorCount() > 0) { |
| 323 errs() << (PNaClABIVerifyFatalErrors ? "ERROR:" : "WARNING:"); |
| 324 errs() << Name << " is not valid PNaCl bitcode:\n"; |
| 325 Reporter.printErrors(errs()); |
| 326 if (PNaClABIVerifyFatalErrors) |
| 327 exit(1); |
| 328 } |
| 329 Reporter.reset(); |
| 330 } |
| 331 // @LOCALMOD-END |
| 332 |
310 static int compileModule(char **argv, LLVMContext &Context) { | 333 static int compileModule(char **argv, LLVMContext &Context) { |
311 // Load the module to be compiled... | 334 // Load the module to be compiled... |
312 SMDiagnostic Err; | 335 SMDiagnostic Err; |
313 std::auto_ptr<Module> M; | 336 std::auto_ptr<Module> M; |
314 Module *mod = 0; | 337 Module *mod = 0; |
315 Triple TheTriple; | 338 Triple TheTriple; |
316 | 339 |
317 bool SkipModule = MCPU == "help" || | 340 bool SkipModule = MCPU == "help" || |
318 (!MAttrs.empty() && MAttrs.front() == "help"); | 341 (!MAttrs.empty() && MAttrs.front() == "help"); |
319 | 342 |
| 343 PNaClABIErrorReporter ABIErrorReporter; // @LOCALMOD |
| 344 |
320 // If user just wants to list available options, skip module loading | 345 // If user just wants to list available options, skip module loading |
321 if (!SkipModule) { | 346 if (!SkipModule) { |
322 // @LOCALMOD-BEGIN | 347 // @LOCALMOD-BEGIN |
323 #if defined(__native_client__) && defined(NACL_SRPC) | 348 #if defined(__native_client__) && defined(NACL_SRPC) |
324 if (LazyBitcode) { | 349 if (LazyBitcode) { |
325 std::string StrError; | 350 std::string StrError; |
326 M.reset(getStreamedBitcodeModule( | 351 M.reset(getStreamedBitcodeModule( |
327 std::string("<SRPC stream>"), | 352 std::string("<SRPC stream>"), |
328 NaClBitcodeStreamer, Context, &StrError)); | 353 NaClBitcodeStreamer, Context, &StrError)); |
329 if (!StrError.empty()) { | 354 if (!StrError.empty()) { |
330 Err = SMDiagnostic(InputFilename, SourceMgr::DK_Error, StrError); | 355 Err = SMDiagnostic(InputFilename, SourceMgr::DK_Error, StrError); |
331 } | 356 } |
332 } else { | 357 } else { |
333 // Avoid using ParseIRFile to avoid pulling in the LLParser. | 358 // Avoid using ParseIRFile to avoid pulling in the LLParser. |
334 // Only handle binary bitcode. | 359 // Only handle binary bitcode. |
335 llvm_unreachable("native client SRPC only supports streaming"); | 360 llvm_unreachable("native client SRPC only supports streaming"); |
336 } | 361 } |
337 #else | 362 #else |
338 M.reset(ParseIRFile(InputFilename, Err, Context)); | 363 M.reset(ParseIRFile(InputFilename, Err, Context)); |
339 #endif | 364 #endif |
340 // @LOCALMOD-END | 365 // @LOCALMOD-END |
341 | 366 |
342 mod = M.get(); | 367 mod = M.get(); |
343 if (mod == 0) { | 368 if (mod == 0) { |
344 Err.print(argv[0], errs()); | 369 Err.print(argv[0], errs()); |
345 return 1; | 370 return 1; |
346 } | 371 } |
347 | 372 |
348 // @LOCALMOD-BEGIN | 373 // @LOCALMOD-BEGIN |
| 374 if (PNaClABIVerify) { |
| 375 // Verify the module (but not the functions yet) |
| 376 ModulePass *VerifyPass = createPNaClABIVerifyModulePass(&ABIErrorReporter)
; |
| 377 VerifyPass->runOnModule(*mod); |
| 378 CheckABIVerifyErrors(ABIErrorReporter, "Module"); |
| 379 } |
349 #if defined(__native_client__) && defined(NACL_SRPC) | 380 #if defined(__native_client__) && defined(NACL_SRPC) |
350 RecordMetadataForSrpc(*mod); | 381 RecordMetadataForSrpc(*mod); |
351 | 382 |
352 // To determine if we should compile PIC or not, we needed to load at | 383 // To determine if we should compile PIC or not, we needed to load at |
353 // least the metadata. Since we've already constructed the commandline, | 384 // least the metadata. Since we've already constructed the commandline, |
354 // we have to hack this in after commandline processing. | 385 // we have to hack this in after commandline processing. |
355 if (mod->getOutputFormat() == Module::SharedOutputFormat) { | 386 if (mod->getOutputFormat() == Module::SharedOutputFormat) { |
356 RelocModel = Reloc::PIC_; | 387 RelocModel = Reloc::PIC_; |
357 } | 388 } |
358 // Also set PIC_ for dynamic executables: | 389 // Also set PIC_ for dynamic executables: |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 if (!Out) return 1; | 496 if (!Out) return 1; |
466 #endif | 497 #endif |
467 | 498 |
468 // Build up all of the passes that we want to do to the module. | 499 // Build up all of the passes that we want to do to the module. |
469 // @LOCALMOD-BEGIN | 500 // @LOCALMOD-BEGIN |
470 OwningPtr<PassManagerBase> PM; | 501 OwningPtr<PassManagerBase> PM; |
471 if (LazyBitcode || ReduceMemoryFootprint) | 502 if (LazyBitcode || ReduceMemoryFootprint) |
472 PM.reset(new FunctionPassManager(mod)); | 503 PM.reset(new FunctionPassManager(mod)); |
473 else | 504 else |
474 PM.reset(new PassManager()); | 505 PM.reset(new PassManager()); |
| 506 |
| 507 // Add the ABI verifier pass before the analysis and code emission passes. |
| 508 FunctionPass *FunctionVerifyPass = NULL; |
| 509 if (PNaClABIVerify) { |
| 510 FunctionVerifyPass = createPNaClABIVerifyFunctionsPass(&ABIErrorReporter); |
| 511 PM->add(FunctionVerifyPass); |
| 512 } |
475 // @LOCALMOD-END | 513 // @LOCALMOD-END |
476 | 514 |
477 // Add an appropriate TargetLibraryInfo pass for the module's triple. | 515 // Add an appropriate TargetLibraryInfo pass for the module's triple. |
478 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple); | 516 TargetLibraryInfo *TLI = new TargetLibraryInfo(TheTriple); |
479 if (DisableSimplifyLibCalls) | 517 if (DisableSimplifyLibCalls) |
480 TLI->disableAllFunctions(); | 518 TLI->disableAllFunctions(); |
481 PM->add(TLI); | 519 PM->add(TLI); |
482 | 520 |
483 // Add intenal analysis passes from the target machine. | 521 // Add intenal analysis passes from the target machine. |
484 Target.addAnalysisPasses(*PM.get()); | 522 Target.addAnalysisPasses(*PM.get()); |
(...skipping 14 matching lines...) Expand all Loading... |
499 else | 537 else |
500 Target.setMCRelaxAll(true); | 538 Target.setMCRelaxAll(true); |
501 } | 539 } |
502 | 540 |
503 | 541 |
504 #if defined __native_client__ && defined(NACL_SRPC) | 542 #if defined __native_client__ && defined(NACL_SRPC) |
505 { | 543 { |
506 raw_fd_ostream ROS(GetObjectFileFD(), true); | 544 raw_fd_ostream ROS(GetObjectFileFD(), true); |
507 ROS.SetBufferSize(1 << 20); | 545 ROS.SetBufferSize(1 << 20); |
508 formatted_raw_ostream FOS(ROS); | 546 formatted_raw_ostream FOS(ROS); |
| 547 |
509 // Ask the target to add backend passes as necessary. | 548 // Ask the target to add backend passes as necessary. |
510 if (Target.addPassesToEmitFile(*PM, FOS, FileType, NoVerify)) { | 549 if (Target.addPassesToEmitFile(*PM, FOS, FileType, NoVerify)) { |
511 errs() << argv[0] << ": target does not support generation of this" | 550 errs() << argv[0] << ": target does not support generation of this" |
512 << " file type!\n"; | 551 << " file type!\n"; |
513 return 1; | 552 return 1; |
514 } | 553 } |
515 | 554 |
516 if (LazyBitcode || ReduceMemoryFootprint) { | 555 if (LazyBitcode || ReduceMemoryFootprint) { |
517 FunctionPassManager* P = static_cast<FunctionPassManager*>(PM.get()); | 556 FunctionPassManager* P = static_cast<FunctionPassManager*>(PM.get()); |
518 P->doInitialization(); | 557 P->doInitialization(); |
519 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) { | 558 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) { |
520 P->run(*I); | 559 P->run(*I); |
| 560 CheckABIVerifyErrors(ABIErrorReporter, "Function " + I->getName()); |
521 if (ReduceMemoryFootprint) { | 561 if (ReduceMemoryFootprint) { |
522 I->Dematerialize(); | 562 I->Dematerialize(); |
523 } | 563 } |
524 } | 564 } |
525 P->doFinalization(); | 565 P->doFinalization(); |
526 } else { | 566 } else { |
527 static_cast<PassManager*>(PM.get())->run(*mod); | 567 static_cast<PassManager*>(PM.get())->run(*mod); |
528 } | 568 } |
529 FOS.flush(); | 569 FOS.flush(); |
530 ROS.flush(); | 570 ROS.flush(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 } | 603 } |
564 | 604 |
565 // Before executing passes, print the final values of the LLVM options. | 605 // Before executing passes, print the final values of the LLVM options. |
566 cl::PrintOptionValues(); | 606 cl::PrintOptionValues(); |
567 | 607 |
568 if (LazyBitcode || ReduceMemoryFootprint) { | 608 if (LazyBitcode || ReduceMemoryFootprint) { |
569 FunctionPassManager *P = static_cast<FunctionPassManager*>(PM.get()); | 609 FunctionPassManager *P = static_cast<FunctionPassManager*>(PM.get()); |
570 P->doInitialization(); | 610 P->doInitialization(); |
571 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) { | 611 for (Module::iterator I = mod->begin(), E = mod->end(); I != E; ++I) { |
572 P->run(*I); | 612 P->run(*I); |
| 613 CheckABIVerifyErrors(ABIErrorReporter, "Function " + I->getName()); |
573 if (ReduceMemoryFootprint) { | 614 if (ReduceMemoryFootprint) { |
574 I->Dematerialize(); | 615 I->Dematerialize(); |
575 } | 616 } |
576 } | 617 } |
577 P->doFinalization(); | 618 P->doFinalization(); |
578 } else { | 619 } else { |
579 static_cast<PassManager*>(PM.get())->run(*mod); | 620 static_cast<PassManager*>(PM.get())->run(*mod); |
580 } | 621 } |
581 } | 622 } |
582 | 623 |
(...skipping 22 matching lines...) Expand all Loading... |
605 } | 646 } |
606 | 647 |
607 #if !defined(NACL_SRPC) | 648 #if !defined(NACL_SRPC) |
608 int | 649 int |
609 main (int argc, char **argv) { | 650 main (int argc, char **argv) { |
610 return llc_main(argc, argv); | 651 return llc_main(argc, argv); |
611 } | 652 } |
612 #else | 653 #else |
613 // main() is in nacl_file.cpp. | 654 // main() is in nacl_file.cpp. |
614 #endif | 655 #endif |
OLD | NEW |