OLD | NEW |
1 //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===// | 1 //===- subzero/src/IceConverter.cpp - Converts LLVM to Ice ---------------===// |
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 implements the LLVM to ICE converter. | 10 // This file implements the LLVM to ICE converter. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 class LLVM2ICEFunctionConverter : LLVM2ICEConverter { | 77 class LLVM2ICEFunctionConverter : LLVM2ICEConverter { |
78 LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; | 78 LLVM2ICEFunctionConverter(const LLVM2ICEFunctionConverter &) = delete; |
79 LLVM2ICEFunctionConverter & | 79 LLVM2ICEFunctionConverter & |
80 operator=(const LLVM2ICEFunctionConverter &) = delete; | 80 operator=(const LLVM2ICEFunctionConverter &) = delete; |
81 | 81 |
82 public: | 82 public: |
83 LLVM2ICEFunctionConverter(Ice::Converter &Converter) | 83 LLVM2ICEFunctionConverter(Ice::Converter &Converter) |
84 : LLVM2ICEConverter(Converter), Func(nullptr) {} | 84 : LLVM2ICEConverter(Converter), Func(nullptr) {} |
85 | 85 |
86 void convertFunction(const Function *F) { | 86 void convertFunction(const Function *F) { |
87 Func = Ice::Cfg::create(Ctx); | 87 if (Ctx->isIRGenerationDisabled()) |
| 88 return; |
| 89 Func = Ice::Cfg::create(Ctx, Converter.getNextSequenceNumber()); |
88 Ice::Cfg::setCurrentCfg(Func.get()); | 90 Ice::Cfg::setCurrentCfg(Func.get()); |
89 | 91 |
90 VarMap.clear(); | 92 VarMap.clear(); |
91 NodeMap.clear(); | 93 NodeMap.clear(); |
92 Func->setFunctionName(F->getName()); | 94 Func->setFunctionName(F->getName()); |
93 Func->setReturnType(convertToIceType(F->getReturnType())); | 95 Func->setReturnType(convertToIceType(F->getReturnType())); |
94 Func->setInternal(F->hasInternalLinkage()); | 96 Func->setInternal(F->hasInternalLinkage()); |
95 Ice::TimerMarker T(Ice::TimerStack::TT_llvmConvert, Func.get()); | 97 Ice::TimerMarker T(Ice::TimerStack::TT_llvmConvert, Func.get()); |
96 | 98 |
97 // The initial definition/use of each arg is the entry node. | 99 // The initial definition/use of each arg is the entry node. |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { | 653 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { |
652 LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; | 654 LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; |
653 LLVM2ICEGlobalsConverter & | 655 LLVM2ICEGlobalsConverter & |
654 operator-(const LLVM2ICEGlobalsConverter &) = delete; | 656 operator-(const LLVM2ICEGlobalsConverter &) = delete; |
655 | 657 |
656 public: | 658 public: |
657 LLVM2ICEGlobalsConverter(Ice::Converter &Converter) | 659 LLVM2ICEGlobalsConverter(Ice::Converter &Converter) |
658 : LLVM2ICEConverter(Converter) {} | 660 : LLVM2ICEConverter(Converter) {} |
659 | 661 |
660 /// Converts global variables, and their initializers into ICE | 662 /// Converts global variables, and their initializers into ICE |
661 /// global variable declarations, for module Mod. Puts corresponding | 663 /// global variable declarations, for module Mod. Returns the set of |
662 /// converted declarations into VariableDeclarations. | 664 /// converted declarations. |
663 void convertGlobalsToIce(Module *Mod, | 665 std::unique_ptr<Ice::VariableDeclarationList> |
664 Ice::VariableDeclarationList &VariableDeclarations); | 666 convertGlobalsToIce(Module *Mod); |
665 | 667 |
666 private: | 668 private: |
667 // Adds the Initializer to the list of initializers for the Global | 669 // Adds the Initializer to the list of initializers for the Global |
668 // variable declaraation. | 670 // variable declaraation. |
669 void addGlobalInitializer(Ice::VariableDeclaration &Global, | 671 void addGlobalInitializer(Ice::VariableDeclaration &Global, |
670 const Constant *Initializer) { | 672 const Constant *Initializer) { |
671 const bool HasOffset = false; | 673 const bool HasOffset = false; |
672 const Ice::RelocOffsetT Offset = 0; | 674 const Ice::RelocOffsetT Offset = 0; |
673 addGlobalInitializer(Global, Initializer, HasOffset, Offset); | 675 addGlobalInitializer(Global, Initializer, HasOffset, Offset); |
674 } | 676 } |
(...skipping 14 matching lines...) Expand all Loading... |
689 return CI->getSExtValue(); | 691 return CI->getSExtValue(); |
690 | 692 |
691 std::string Buffer; | 693 std::string Buffer; |
692 raw_string_ostream StrBuf(Buffer); | 694 raw_string_ostream StrBuf(Buffer); |
693 StrBuf << "Constant not i32 literal: " << *C; | 695 StrBuf << "Constant not i32 literal: " << *C; |
694 report_fatal_error(StrBuf.str()); | 696 report_fatal_error(StrBuf.str()); |
695 return 0; | 697 return 0; |
696 } | 698 } |
697 }; | 699 }; |
698 | 700 |
699 void LLVM2ICEGlobalsConverter::convertGlobalsToIce( | 701 std::unique_ptr<Ice::VariableDeclarationList> |
700 Module *Mod, Ice::VariableDeclarationList &VariableDeclarations) { | 702 LLVM2ICEGlobalsConverter::convertGlobalsToIce(Module *Mod) { |
| 703 std::unique_ptr<Ice::VariableDeclarationList> VariableDeclarations( |
| 704 new Ice::VariableDeclarationList); |
701 for (Module::const_global_iterator I = Mod->global_begin(), | 705 for (Module::const_global_iterator I = Mod->global_begin(), |
702 E = Mod->global_end(); | 706 E = Mod->global_end(); |
703 I != E; ++I) { | 707 I != E; ++I) { |
704 | 708 |
705 const GlobalVariable *GV = I; | 709 const GlobalVariable *GV = I; |
706 | 710 |
707 Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV); | 711 Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV); |
708 Ice::VariableDeclaration *VarDecl = cast<Ice::VariableDeclaration>(Var); | 712 Ice::VariableDeclaration *VarDecl = cast<Ice::VariableDeclaration>(Var); |
709 VariableDeclarations.push_back(VarDecl); | 713 VariableDeclarations->push_back(VarDecl); |
710 | 714 |
711 if (!GV->hasInternalLinkage() && GV->hasInitializer()) { | 715 if (!GV->hasInternalLinkage() && GV->hasInitializer()) { |
712 std::string Buffer; | 716 std::string Buffer; |
713 raw_string_ostream StrBuf(Buffer); | 717 raw_string_ostream StrBuf(Buffer); |
714 StrBuf << "Can't define external global declaration: " << GV->getName(); | 718 StrBuf << "Can't define external global declaration: " << GV->getName(); |
715 report_fatal_error(StrBuf.str()); | 719 report_fatal_error(StrBuf.str()); |
716 } | 720 } |
717 | 721 |
718 if (!GV->hasInitializer()) { | 722 if (!GV->hasInitializer()) { |
719 if (Ctx->getFlags().getAllowUninitializedGlobals()) | 723 if (Ctx->getFlags().getAllowUninitializedGlobals()) |
(...skipping 12 matching lines...) Expand all Loading... |
732 E = CompoundInit->op_end(); | 736 E = CompoundInit->op_end(); |
733 I != E; ++I) { | 737 I != E; ++I) { |
734 if (const auto Init = dyn_cast<Constant>(I)) { | 738 if (const auto Init = dyn_cast<Constant>(I)) { |
735 addGlobalInitializer(*VarDecl, Init); | 739 addGlobalInitializer(*VarDecl, Init); |
736 } | 740 } |
737 } | 741 } |
738 } else { | 742 } else { |
739 addGlobalInitializer(*VarDecl, Initializer); | 743 addGlobalInitializer(*VarDecl, Initializer); |
740 } | 744 } |
741 } | 745 } |
| 746 return std::move(VariableDeclarations); |
742 } | 747 } |
743 | 748 |
744 void LLVM2ICEGlobalsConverter::addGlobalInitializer( | 749 void LLVM2ICEGlobalsConverter::addGlobalInitializer( |
745 Ice::VariableDeclaration &Global, const Constant *Initializer, | 750 Ice::VariableDeclaration &Global, const Constant *Initializer, |
746 bool HasOffset, Ice::RelocOffsetT Offset) { | 751 bool HasOffset, Ice::RelocOffsetT Offset) { |
747 (void)HasOffset; | 752 (void)HasOffset; |
748 assert(HasOffset || Offset == 0); | 753 assert(HasOffset || Offset == 0); |
749 | 754 |
750 if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { | 755 if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { |
751 assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && | 756 assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
794 raw_string_ostream StrBuf(Buffer); | 799 raw_string_ostream StrBuf(Buffer); |
795 StrBuf << "Unhandled global initializer: " << Initializer; | 800 StrBuf << "Unhandled global initializer: " << Initializer; |
796 report_fatal_error(StrBuf.str()); | 801 report_fatal_error(StrBuf.str()); |
797 } | 802 } |
798 | 803 |
799 } // end of anonymous namespace | 804 } // end of anonymous namespace |
800 | 805 |
801 namespace Ice { | 806 namespace Ice { |
802 | 807 |
803 void Converter::nameUnnamedGlobalVariables(Module *Mod) { | 808 void Converter::nameUnnamedGlobalVariables(Module *Mod) { |
804 const IceString &GlobalPrefix = Flags.getDefaultGlobalPrefix(); | 809 const IceString &GlobalPrefix = Ctx->getFlags().getDefaultGlobalPrefix(); |
805 if (GlobalPrefix.empty()) | 810 if (GlobalPrefix.empty()) |
806 return; | 811 return; |
807 uint32_t NameIndex = 0; | 812 uint32_t NameIndex = 0; |
808 for (auto V = Mod->global_begin(), E = Mod->global_end(); V != E; ++V) { | 813 for (auto V = Mod->global_begin(), E = Mod->global_end(); V != E; ++V) { |
809 if (!V->hasName()) { | 814 if (!V->hasName()) { |
810 V->setName(createUnnamedName(GlobalPrefix, NameIndex)); | 815 V->setName(createUnnamedName(GlobalPrefix, NameIndex)); |
811 ++NameIndex; | 816 ++NameIndex; |
812 } else { | 817 } else { |
813 checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix); | 818 checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix); |
814 } | 819 } |
815 } | 820 } |
816 } | 821 } |
817 | 822 |
818 void Converter::nameUnnamedFunctions(Module *Mod) { | 823 void Converter::nameUnnamedFunctions(Module *Mod) { |
819 const IceString &FunctionPrefix = Flags.getDefaultFunctionPrefix(); | 824 const IceString &FunctionPrefix = Ctx->getFlags().getDefaultFunctionPrefix(); |
820 if (FunctionPrefix.empty()) | 825 if (FunctionPrefix.empty()) |
821 return; | 826 return; |
822 uint32_t NameIndex = 0; | 827 uint32_t NameIndex = 0; |
823 for (Function &F : *Mod) { | 828 for (Function &F : *Mod) { |
824 if (!F.hasName()) { | 829 if (!F.hasName()) { |
825 F.setName(createUnnamedName(FunctionPrefix, NameIndex)); | 830 F.setName(createUnnamedName(FunctionPrefix, NameIndex)); |
826 ++NameIndex; | 831 ++NameIndex; |
827 } else { | 832 } else { |
828 checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix); | 833 checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix); |
829 } | 834 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 VariableDeclaration *Var = VariableDeclaration::create(); | 880 VariableDeclaration *Var = VariableDeclaration::create(); |
876 Var->setName(GV->getName()); | 881 Var->setName(GV->getName()); |
877 Var->setAlignment(GV->getAlignment()); | 882 Var->setAlignment(GV->getAlignment()); |
878 Var->setIsConstant(GV->isConstant()); | 883 Var->setIsConstant(GV->isConstant()); |
879 Var->setLinkage(GV->getLinkage()); | 884 Var->setLinkage(GV->getLinkage()); |
880 GlobalDeclarationMap[GV] = Var; | 885 GlobalDeclarationMap[GV] = Var; |
881 } | 886 } |
882 } | 887 } |
883 | 888 |
884 void Converter::convertGlobals(Module *Mod) { | 889 void Converter::convertGlobals(Module *Mod) { |
885 LLVM2ICEGlobalsConverter GlobalsConverter(*this); | 890 lowerGlobals(LLVM2ICEGlobalsConverter(*this).convertGlobalsToIce(Mod)); |
886 VariableDeclarationList VariableDeclarations; | |
887 GlobalsConverter.convertGlobalsToIce(Mod, VariableDeclarations); | |
888 lowerGlobals(VariableDeclarations); | |
889 } | 891 } |
890 | 892 |
891 void Converter::convertFunctions() { | 893 void Converter::convertFunctions() { |
892 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; | 894 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; |
893 for (const Function &I : *Mod) { | 895 for (const Function &I : *Mod) { |
894 if (I.empty()) | 896 if (I.empty()) |
895 continue; | 897 continue; |
896 | 898 |
897 TimerIdT TimerID = 0; | 899 TimerIdT TimerID = 0; |
898 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); | 900 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); |
899 if (TimeThisFunction) { | 901 if (TimeThisFunction) { |
900 TimerID = Ctx->getTimerID(StackID, I.getName()); | 902 TimerID = Ctx->getTimerID(StackID, I.getName()); |
901 Ctx->pushTimer(TimerID, StackID); | 903 Ctx->pushTimer(TimerID, StackID); |
902 } | 904 } |
903 LLVM2ICEFunctionConverter FunctionConverter(*this); | 905 LLVM2ICEFunctionConverter FunctionConverter(*this); |
904 FunctionConverter.convertFunction(&I); | 906 FunctionConverter.convertFunction(&I); |
905 if (TimeThisFunction) | 907 if (TimeThisFunction) |
906 Ctx->popTimer(TimerID, StackID); | 908 Ctx->popTimer(TimerID, StackID); |
907 } | 909 } |
908 } | 910 } |
909 | 911 |
910 } // end of namespace Ice | 912 } // end of namespace Ice |
OLD | NEW |