OLD | NEW |
---|---|
1 //===- subzero/src/llvm2ice.cpp - Driver for testing ----------------------===// | 1 //===- subzero/src/IceConverter.h - Converts LLVM to Ice ------*- C++ -*-===// |
Jim Stichnoth
2014/06/24 23:06:37
Fill to 80 chars :)
And the filename is wrong.
(no
Karl
2014/06/25 16:02:05
Done.
| |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
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 file defines a driver that uses LLVM capabilities to parse a | 10 // This file implements the LLVM to ICE converter. |
11 // bitcode file and build the LLVM IR, and then convert the LLVM basic | |
12 // blocks, instructions, and operands into their Subzero equivalents. | |
13 // | 11 // |
14 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
15 | 13 |
14 #include "IceConverter.h" | |
15 | |
16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
17 #include "IceCfgNode.h" | 17 #include "IceCfgNode.h" |
18 #include "IceDefs.h" | 18 #include "IceDefs.h" |
19 #include "IceGlobalContext.h" | 19 #include "IceGlobalContext.h" |
20 #include "IceInst.h" | 20 #include "IceInst.h" |
21 #include "IceOperand.h" | 21 #include "IceOperand.h" |
22 #include "IceTargetLowering.h" | 22 #include "IceTargetLowering.h" |
23 #include "IceTypes.h" | 23 #include "IceTypes.h" |
24 | 24 |
25 #include "llvm/IR/Constant.h" | 25 #include "llvm/IR/Constant.h" |
26 #include "llvm/IR/Constants.h" | 26 #include "llvm/IR/Constants.h" |
27 #include "llvm/IR/DataLayout.h" | 27 #include "llvm/IR/DataLayout.h" |
28 #include "llvm/IR/Instruction.h" | 28 #include "llvm/IR/Instruction.h" |
29 #include "llvm/IR/Instructions.h" | 29 #include "llvm/IR/Instructions.h" |
30 #include "llvm/IR/LLVMContext.h" | 30 #include "llvm/IR/LLVMContext.h" |
31 #include "llvm/IR/Module.h" | 31 #include "llvm/IR/Module.h" |
32 #include "llvm/IRReader/IRReader.h" | |
33 #include "llvm/Support/CommandLine.h" | |
34 #include "llvm/Support/ErrorHandling.h" | |
35 #include "llvm/Support/raw_os_ostream.h" | |
36 #include "llvm/Support/SourceMgr.h" | |
37 | 32 |
38 #include <fstream> | |
39 #include <iostream> | 33 #include <iostream> |
40 | 34 |
41 using namespace llvm; | 35 using namespace llvm; |
42 | 36 |
Karl
2014/06/25 16:02:05
Enclosed following code in an unnamed namespace.
| |
43 // Debugging helper | 37 // Debugging helper |
44 template <typename T> static std::string LLVMObjectAsString(const T *O) { | 38 template <typename T> static std::string LLVMObjectAsString(const T *O) { |
45 std::string Dump; | 39 std::string Dump; |
46 raw_string_ostream Stream(Dump); | 40 raw_string_ostream Stream(Dump); |
47 O->print(Stream); | 41 O->print(Stream); |
48 return Stream.str(); | 42 return Stream.str(); |
49 } | 43 } |
50 | 44 |
51 // Converter from LLVM to ICE. The entry point is the convertFunction method. | 45 // Converter from LLVM to ICE. The entry point is the convertFunction method. |
52 // | 46 // |
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 private: | 607 private: |
614 // Data | 608 // Data |
615 Ice::GlobalContext *Ctx; | 609 Ice::GlobalContext *Ctx; |
616 Ice::Cfg *Func; | 610 Ice::Cfg *Func; |
617 Ice::CfgNode *CurrentNode; | 611 Ice::CfgNode *CurrentNode; |
618 Ice::Type SubzeroPointerType; | 612 Ice::Type SubzeroPointerType; |
619 std::map<const Value *, Ice::Variable *> VarMap; | 613 std::map<const Value *, Ice::Variable *> VarMap; |
620 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; | 614 std::map<const BasicBlock *, Ice::CfgNode *> NodeMap; |
621 }; | 615 }; |
622 | 616 |
623 static cl::list<Ice::VerboseItem> VerboseList( | 617 int LLVM2ICEConverterDriver::convertToIce(Module *Mod) { |
624 "verbose", cl::CommaSeparated, | |
625 cl::desc("Verbose options (can be comma-separated):"), | |
626 cl::values( | |
627 clEnumValN(Ice::IceV_Instructions, "inst", "Print basic instructions"), | |
628 clEnumValN(Ice::IceV_Deleted, "del", "Include deleted instructions"), | |
629 clEnumValN(Ice::IceV_InstNumbers, "instnum", | |
630 "Print instruction numbers"), | |
631 clEnumValN(Ice::IceV_Preds, "pred", "Show predecessors"), | |
632 clEnumValN(Ice::IceV_Succs, "succ", "Show successors"), | |
633 clEnumValN(Ice::IceV_Liveness, "live", "Liveness information"), | |
634 clEnumValN(Ice::IceV_RegManager, "rmgr", "Register manager status"), | |
635 clEnumValN(Ice::IceV_RegOrigins, "orig", "Physical register origins"), | |
636 clEnumValN(Ice::IceV_LinearScan, "regalloc", "Linear scan details"), | |
637 clEnumValN(Ice::IceV_Frame, "frame", "Stack frame layout details"), | |
638 clEnumValN(Ice::IceV_Timing, "time", "Pass timing details"), | |
639 clEnumValN(Ice::IceV_All, "all", "Use all verbose options"), | |
640 clEnumValN(Ice::IceV_None, "none", "No verbosity"), clEnumValEnd)); | |
641 static cl::opt<Ice::TargetArch> TargetArch( | |
642 "target", cl::desc("Target architecture:"), cl::init(Ice::Target_X8632), | |
643 cl::values( | |
644 clEnumValN(Ice::Target_X8632, "x8632", "x86-32"), | |
645 clEnumValN(Ice::Target_X8632, "x86-32", "x86-32 (same as x8632)"), | |
646 clEnumValN(Ice::Target_X8632, "x86_32", "x86-32 (same as x8632)"), | |
647 clEnumValN(Ice::Target_X8664, "x8664", "x86-64"), | |
648 clEnumValN(Ice::Target_X8664, "x86-64", "x86-64 (same as x8664)"), | |
649 clEnumValN(Ice::Target_X8664, "x86_64", "x86-64 (same as x8664)"), | |
650 clEnumValN(Ice::Target_ARM32, "arm", "arm32"), | |
651 clEnumValN(Ice::Target_ARM32, "arm32", "arm32 (same as arm)"), | |
652 clEnumValN(Ice::Target_ARM64, "arm64", "arm64"), clEnumValEnd)); | |
653 static cl::opt<Ice::OptLevel> | |
654 OptLevel(cl::desc("Optimization level"), cl::init(Ice::Opt_m1), | |
655 cl::value_desc("level"), | |
656 cl::values(clEnumValN(Ice::Opt_m1, "Om1", "-1"), | |
657 clEnumValN(Ice::Opt_m1, "O-1", "-1"), | |
658 clEnumValN(Ice::Opt_0, "O0", "0"), | |
659 clEnumValN(Ice::Opt_1, "O1", "1"), | |
660 clEnumValN(Ice::Opt_2, "O2", "2"), clEnumValEnd)); | |
661 static cl::opt<std::string> IRFilename(cl::Positional, cl::desc("<IR file>"), | |
662 cl::init("-")); | |
663 static cl::opt<std::string> OutputFilename("o", | |
664 cl::desc("Override output filename"), | |
665 cl::init("-"), | |
666 cl::value_desc("filename")); | |
667 static cl::opt<std::string> LogFilename("log", cl::desc("Set log filename"), | |
668 cl::init("-"), | |
669 cl::value_desc("filename")); | |
670 static cl::opt<std::string> | |
671 TestPrefix("prefix", cl::desc("Prepend a prefix to symbol names for testing"), | |
672 cl::init(""), cl::value_desc("prefix")); | |
673 static cl::opt<bool> | |
674 DisableInternal("external", | |
675 cl::desc("Disable 'internal' linkage type for testing")); | |
676 static cl::opt<bool> | |
677 DisableTranslation("notranslate", cl::desc("Disable Subzero translation")); | |
678 | |
679 static cl::opt<bool> SubzeroTimingEnabled( | |
680 "timing", cl::desc("Enable breakdown timing of Subzero translation")); | |
681 | |
682 static cl::opt<NaClFileFormat> InputFileFormat( | |
683 "bitcode-format", cl::desc("Define format of input file:"), | |
684 cl::values(clEnumValN(LLVMFormat, "llvm", "LLVM file (default)"), | |
685 clEnumValN(PNaClFormat, "pnacl", "PNaCl bitcode file"), | |
686 clEnumValEnd), | |
687 cl::init(LLVMFormat)); | |
688 | |
689 int main(int argc, char **argv) { | |
690 int ExitStatus = 0; | 618 int ExitStatus = 0; |
691 | 619 |
692 cl::ParseCommandLineOptions(argc, argv); | |
693 | |
694 // Parse the input LLVM IR file into a module. | |
695 SMDiagnostic Err; | |
696 Module *Mod; | |
697 | |
698 { | |
699 Ice::Timer T; | |
700 Mod = NaClParseIRFile(IRFilename, InputFileFormat, Err, getGlobalContext()); | |
701 | |
702 if (SubzeroTimingEnabled) { | |
703 std::cerr << "[Subzero timing] IR Parsing: " << T.getElapsedSec() | |
704 << " sec\n"; | |
705 } | |
706 } | |
707 | |
708 if (!Mod) { | |
709 Err.print(argv[0], errs()); | |
710 return 1; | |
711 } | |
712 | |
713 Ice::VerboseMask VMask = Ice::IceV_None; | |
714 for (unsigned i = 0; i != VerboseList.size(); ++i) | |
715 VMask |= VerboseList[i]; | |
716 | |
717 std::ofstream Ofs; | |
718 if (OutputFilename != "-") { | |
719 Ofs.open(OutputFilename.c_str(), std::ofstream::out); | |
720 } | |
721 raw_os_ostream *Os = | |
722 new raw_os_ostream(OutputFilename == "-" ? std::cout : Ofs); | |
723 Os->SetUnbuffered(); | |
724 std::ofstream Lfs; | |
725 if (LogFilename != "-") { | |
726 Lfs.open(LogFilename.c_str(), std::ofstream::out); | |
727 } | |
728 raw_os_ostream *Ls = new raw_os_ostream(LogFilename == "-" ? std::cout : Lfs); | |
729 Ls->SetUnbuffered(); | |
730 | |
731 // Ideally, Func would be declared inside the loop and its object | 620 // Ideally, Func would be declared inside the loop and its object |
732 // would be automatically deleted at the end of the loop iteration. | 621 // would be automatically deleted at the end of the loop iteration. |
733 // However, emitting the constant pool requires a valid Cfg object, | 622 // However, emitting the constant pool requires a valid Cfg object, |
734 // so we need to defer deleting the last non-empty Cfg object until | 623 // so we need to defer deleting the last non-empty Cfg object until |
735 // outside the loop and after emitting the constant pool. TODO: | 624 // outside the loop and after emitting the constant pool. TODO: |
736 // Since all constants are globally pooled in the Ice::GlobalContext | 625 // Since all constants are globally pooled in the Ice::GlobalContext |
737 // object, change all Ice::Constant related functions to use | 626 // object, change all Ice::Constant related functions to use |
738 // GlobalContext instead of Cfg, and then clean up this loop. | 627 // GlobalContext instead of Cfg, and then clean up this loop. |
739 OwningPtr<Ice::Cfg> Func; | 628 OwningPtr<Ice::Cfg> Func; |
740 Ice::GlobalContext Ctx(Ls, Os, VMask, TargetArch, OptLevel, TestPrefix); | |
741 | 629 |
742 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { | 630 for (Module::const_iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { |
743 if (I->empty()) | 631 if (I->empty()) |
744 continue; | 632 continue; |
745 LLVM2ICEConverter FunctionConverter(&Ctx); | 633 LLVM2ICEConverter FunctionConverter(Ctx); |
746 | 634 |
747 Ice::Timer TConvert; | 635 Ice::Timer TConvert; |
748 Func.reset(FunctionConverter.convertFunction(I)); | 636 Func.reset(FunctionConverter.convertFunction(I)); |
749 if (DisableInternal) | 637 if (DisableInternal) |
750 Func->setInternal(false); | 638 Func->setInternal(false); |
751 | 639 |
752 if (SubzeroTimingEnabled) { | 640 if (SubzeroTimingEnabled) { |
753 std::cerr << "[Subzero timing] Convert function " | 641 std::cerr << "[Subzero timing] Convert function " |
754 << Func->getFunctionName() << ": " << TConvert.getElapsedSec() | 642 << Func->getFunctionName() << ": " << TConvert.getElapsedSec() |
755 << " sec\n"; | 643 << " sec\n"; |
(...skipping 22 matching lines...) Expand all Loading... | |
778 << " sec\n"; | 666 << " sec\n"; |
779 } | 667 } |
780 } | 668 } |
781 } | 669 } |
782 | 670 |
783 if (!DisableTranslation && Func) | 671 if (!DisableTranslation && Func) |
784 Func->getTarget()->emitConstants(); | 672 Func->getTarget()->emitConstants(); |
785 | 673 |
786 return ExitStatus; | 674 return ExitStatus; |
787 } | 675 } |
OLD | NEW |