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 /// \file | 10 /// \file |
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 // | 652 // |
653 // Note: this currently assumes that the given IR was verified to be valid | 653 // Note: this currently assumes that the given IR was verified to be valid |
654 // PNaCl bitcode. Otherwise, the behavior is undefined. | 654 // PNaCl bitcode. Otherwise, the behavior is undefined. |
655 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { | 655 class LLVM2ICEGlobalsConverter : public LLVM2ICEConverter { |
656 LLVM2ICEGlobalsConverter() = delete; | 656 LLVM2ICEGlobalsConverter() = delete; |
657 LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; | 657 LLVM2ICEGlobalsConverter(const LLVM2ICEGlobalsConverter &) = delete; |
658 LLVM2ICEGlobalsConverter & | 658 LLVM2ICEGlobalsConverter & |
659 operator=(const LLVM2ICEGlobalsConverter &) = delete; | 659 operator=(const LLVM2ICEGlobalsConverter &) = delete; |
660 | 660 |
661 public: | 661 public: |
662 explicit LLVM2ICEGlobalsConverter(Ice::Converter &Converter) | 662 explicit LLVM2ICEGlobalsConverter(Ice::Converter &Converter, |
663 : LLVM2ICEConverter(Converter) {} | 663 Ice::VariableDeclarationList *G) |
| 664 : LLVM2ICEConverter(Converter), GlobalPool(G) {} |
664 | 665 |
665 /// Converts global variables, and their initializers into ICE global variable | 666 /// Converts global variables, and their initializers into ICE global variable |
666 /// declarations, for module Mod. Returns the set of converted declarations. | 667 /// declarations, for module Mod. Returns the set of converted declarations. |
667 std::unique_ptr<Ice::VariableDeclarationList> | 668 void convertGlobalsToIce(Module *Mod); |
668 convertGlobalsToIce(Module *Mod); | |
669 | 669 |
670 private: | 670 private: |
671 // Adds the Initializer to the list of initializers for the Global variable | 671 // Adds the Initializer to the list of initializers for the Global variable |
672 // declaration. | 672 // declaration. |
673 void addGlobalInitializer(Ice::VariableDeclaration &Global, | 673 void addGlobalInitializer(Ice::VariableDeclaration &Global, |
674 const Constant *Initializer) { | 674 const Constant *Initializer) { |
675 constexpr bool HasOffset = false; | 675 constexpr bool HasOffset = false; |
676 constexpr Ice::RelocOffsetT Offset = 0; | 676 constexpr Ice::RelocOffsetT Offset = 0; |
677 addGlobalInitializer(Global, Initializer, HasOffset, Offset); | 677 addGlobalInitializer(Global, Initializer, HasOffset, Offset); |
678 } | 678 } |
(...skipping 11 matching lines...) Expand all Loading... |
690 const auto CI = dyn_cast<ConstantInt>(C); | 690 const auto CI = dyn_cast<ConstantInt>(C); |
691 if (CI && CI->getType()->isIntegerTy(32)) | 691 if (CI && CI->getType()->isIntegerTy(32)) |
692 return CI->getSExtValue(); | 692 return CI->getSExtValue(); |
693 | 693 |
694 std::string Buffer; | 694 std::string Buffer; |
695 raw_string_ostream StrBuf(Buffer); | 695 raw_string_ostream StrBuf(Buffer); |
696 StrBuf << "Constant not i32 literal: " << *C; | 696 StrBuf << "Constant not i32 literal: " << *C; |
697 report_fatal_error(StrBuf.str()); | 697 report_fatal_error(StrBuf.str()); |
698 return 0; | 698 return 0; |
699 } | 699 } |
| 700 |
| 701 Ice::VariableDeclarationList *GlobalPool; |
700 }; | 702 }; |
701 | 703 |
702 std::unique_ptr<Ice::VariableDeclarationList> | 704 void LLVM2ICEGlobalsConverter::convertGlobalsToIce(Module *Mod) { |
703 LLVM2ICEGlobalsConverter::convertGlobalsToIce(Module *Mod) { | |
704 std::unique_ptr<Ice::VariableDeclarationList> VariableDeclarations( | |
705 new Ice::VariableDeclarationList); | |
706 for (Module::const_global_iterator I = Mod->global_begin(), | 705 for (Module::const_global_iterator I = Mod->global_begin(), |
707 E = Mod->global_end(); | 706 E = Mod->global_end(); |
708 I != E; ++I) { | 707 I != E; ++I) { |
709 | 708 |
710 const GlobalVariable *GV = I; | 709 const GlobalVariable *GV = I; |
711 | 710 |
712 Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV); | 711 Ice::GlobalDeclaration *Var = getConverter().getGlobalDeclaration(GV); |
713 auto *VarDecl = cast<Ice::VariableDeclaration>(Var); | 712 auto *VarDecl = cast<Ice::VariableDeclaration>(Var); |
714 VariableDeclarations->push_back(VarDecl); | 713 GlobalPool->push_back(VarDecl); |
715 | 714 |
716 if (!GV->hasInternalLinkage() && GV->hasInitializer()) { | 715 if (!GV->hasInternalLinkage() && GV->hasInitializer()) { |
717 std::string Buffer; | 716 std::string Buffer; |
718 raw_string_ostream StrBuf(Buffer); | 717 raw_string_ostream StrBuf(Buffer); |
719 StrBuf << "Can't define external global declaration: " << GV->getName(); | 718 StrBuf << "Can't define external global declaration: " << GV->getName(); |
720 report_fatal_error(StrBuf.str()); | 719 report_fatal_error(StrBuf.str()); |
721 } | 720 } |
722 | 721 |
723 if (!GV->hasInitializer()) { | 722 if (!GV->hasInitializer()) { |
724 if (Ctx->getFlags().getAllowUninitializedGlobals()) | 723 if (Ctx->getFlags().getAllowUninitializedGlobals()) |
(...skipping 12 matching lines...) Expand all Loading... |
737 E = CompoundInit->op_end(); | 736 E = CompoundInit->op_end(); |
738 I != E; ++I) { | 737 I != E; ++I) { |
739 if (const auto Init = dyn_cast<Constant>(I)) { | 738 if (const auto Init = dyn_cast<Constant>(I)) { |
740 addGlobalInitializer(*VarDecl, Init); | 739 addGlobalInitializer(*VarDecl, Init); |
741 } | 740 } |
742 } | 741 } |
743 } else { | 742 } else { |
744 addGlobalInitializer(*VarDecl, Initializer); | 743 addGlobalInitializer(*VarDecl, Initializer); |
745 } | 744 } |
746 } | 745 } |
747 return VariableDeclarations; | |
748 } | 746 } |
749 | 747 |
750 void LLVM2ICEGlobalsConverter::addGlobalInitializer( | 748 void LLVM2ICEGlobalsConverter::addGlobalInitializer( |
751 Ice::VariableDeclaration &Global, const Constant *Initializer, | 749 Ice::VariableDeclaration &Global, const Constant *Initializer, |
752 bool HasOffset, Ice::RelocOffsetT Offset) { | 750 bool HasOffset, Ice::RelocOffsetT Offset) { |
753 (void)HasOffset; | 751 (void)HasOffset; |
754 assert(HasOffset || Offset == 0); | 752 assert(HasOffset || Offset == 0); |
755 | 753 |
756 if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { | 754 if (const auto CDA = dyn_cast<ConstantDataArray>(Initializer)) { |
757 assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && | 755 assert(!HasOffset && isa<IntegerType>(CDA->getElementType()) && |
758 (cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8)); | 756 (cast<IntegerType>(CDA->getElementType())->getBitWidth() == 8)); |
759 Global.addInitializer(Ice::VariableDeclaration::DataInitializer::create( | 757 Global.addInitializer(Ice::VariableDeclaration::DataInitializer::create( |
760 CDA->getRawDataValues().data(), CDA->getNumElements())); | 758 GlobalPool, CDA->getRawDataValues().data(), CDA->getNumElements())); |
761 return; | 759 return; |
762 } | 760 } |
763 | 761 |
764 if (isa<ConstantAggregateZero>(Initializer)) { | 762 if (isa<ConstantAggregateZero>(Initializer)) { |
765 if (const auto AT = dyn_cast<ArrayType>(Initializer->getType())) { | 763 if (const auto AT = dyn_cast<ArrayType>(Initializer->getType())) { |
766 assert(!HasOffset && isa<IntegerType>(AT->getElementType()) && | 764 assert(!HasOffset && isa<IntegerType>(AT->getElementType()) && |
767 (cast<IntegerType>(AT->getElementType())->getBitWidth() == 8)); | 765 (cast<IntegerType>(AT->getElementType())->getBitWidth() == 8)); |
768 Global.addInitializer(Ice::VariableDeclaration::ZeroInitializer::create( | 766 Global.addInitializer(Ice::VariableDeclaration::ZeroInitializer::create( |
769 AT->getNumElements())); | 767 GlobalPool, AT->getNumElements())); |
770 } else { | 768 } else { |
771 llvm_unreachable("Unhandled constant aggregate zero type"); | 769 llvm_unreachable("Unhandled constant aggregate zero type"); |
772 } | 770 } |
773 return; | 771 return; |
774 } | 772 } |
775 | 773 |
776 if (const auto Exp = dyn_cast<ConstantExpr>(Initializer)) { | 774 if (const auto Exp = dyn_cast<ConstantExpr>(Initializer)) { |
777 switch (Exp->getOpcode()) { | 775 switch (Exp->getOpcode()) { |
778 case Instruction::Add: | 776 case Instruction::Add: |
779 assert(!HasOffset); | 777 assert(!HasOffset); |
780 addGlobalInitializer(Global, Exp->getOperand(0), true, | 778 addGlobalInitializer(Global, Exp->getOperand(0), true, |
781 getIntegerLiteralConstant(Exp->getOperand(1))); | 779 getIntegerLiteralConstant(Exp->getOperand(1))); |
782 return; | 780 return; |
783 case Instruction::PtrToInt: { | 781 case Instruction::PtrToInt: { |
784 assert(TypeConverter.convertToIceType(Exp->getType()) == | 782 assert(TypeConverter.convertToIceType(Exp->getType()) == |
785 Ice::getPointerType()); | 783 Ice::getPointerType()); |
786 const auto GV = dyn_cast<GlobalValue>(Exp->getOperand(0)); | 784 const auto GV = dyn_cast<GlobalValue>(Exp->getOperand(0)); |
787 assert(GV); | 785 assert(GV); |
788 const Ice::GlobalDeclaration *Addr = | 786 const Ice::GlobalDeclaration *Addr = |
789 getConverter().getGlobalDeclaration(GV); | 787 getConverter().getGlobalDeclaration(GV); |
790 Global.addInitializer(Ice::VariableDeclaration::RelocInitializer::create( | 788 Global.addInitializer(Ice::VariableDeclaration::RelocInitializer::create( |
791 Addr, {Ice::RelocOffset::create(Ctx, Offset)})); | 789 GlobalPool, Addr, {Ice::RelocOffset::create(Ctx, Offset)})); |
792 return; | 790 return; |
793 } | 791 } |
794 default: | 792 default: |
795 break; | 793 break; |
796 } | 794 } |
797 } | 795 } |
798 | 796 |
799 std::string Buffer; | 797 std::string Buffer; |
800 raw_string_ostream StrBuf(Buffer); | 798 raw_string_ostream StrBuf(Buffer); |
801 StrBuf << "Unhandled global initializer: " << Initializer; | 799 StrBuf << "Unhandled global initializer: " << Initializer; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
883 if (!IceFunc->validateTypeSignature(Ctx)) | 881 if (!IceFunc->validateTypeSignature(Ctx)) |
884 report_fatal_error(IceFunc->getTypeSignatureError(Ctx)); | 882 report_fatal_error(IceFunc->getTypeSignatureError(Ctx)); |
885 GlobalDeclarationMap[&Func] = IceFunc; | 883 GlobalDeclarationMap[&Func] = IceFunc; |
886 } | 884 } |
887 // Install global variable declarations. | 885 // Install global variable declarations. |
888 for (Module::const_global_iterator I = Mod->global_begin(), | 886 for (Module::const_global_iterator I = Mod->global_begin(), |
889 E = Mod->global_end(); | 887 E = Mod->global_end(); |
890 I != E; ++I) { | 888 I != E; ++I) { |
891 const GlobalVariable *GV = I; | 889 const GlobalVariable *GV = I; |
892 constexpr bool NoSuppressMangling = false; | 890 constexpr bool NoSuppressMangling = false; |
893 auto *Var = | 891 auto *Var = VariableDeclaration::create( |
894 VariableDeclaration::create(Ctx, NoSuppressMangling, GV->getLinkage()); | 892 GlobalDeclarationsPool.get(), NoSuppressMangling, GV->getLinkage()); |
895 Var->setAlignment(GV->getAlignment()); | 893 Var->setAlignment(GV->getAlignment()); |
896 Var->setIsConstant(GV->isConstant()); | 894 Var->setIsConstant(GV->isConstant()); |
897 Var->setName(GV->getName()); | 895 Var->setName(GV->getName()); |
898 if (!Var->verifyLinkageCorrect(Ctx)) { | 896 if (!Var->verifyLinkageCorrect(Ctx)) { |
899 std::string Buffer; | 897 std::string Buffer; |
900 raw_string_ostream StrBuf(Buffer); | 898 raw_string_ostream StrBuf(Buffer); |
901 StrBuf << "Global " << Var->getName() | 899 StrBuf << "Global " << Var->getName() |
902 << " has incorrect linkage: " << Var->getLinkageName(); | 900 << " has incorrect linkage: " << Var->getLinkageName(); |
903 if (Var->isExternal()) | 901 if (Var->isExternal()) |
904 StrBuf << "\n Use flag -allow-externally-defined-symbols to override"; | 902 StrBuf << "\n Use flag -allow-externally-defined-symbols to override"; |
905 report_fatal_error(StrBuf.str()); | 903 report_fatal_error(StrBuf.str()); |
906 } | 904 } |
907 GlobalDeclarationMap[GV] = Var; | 905 GlobalDeclarationMap[GV] = Var; |
908 } | 906 } |
909 } | 907 } |
910 | 908 |
911 void Converter::convertGlobals(Module *Mod) { | 909 void Converter::convertGlobals(Module *Mod) { |
912 lowerGlobals(LLVM2ICEGlobalsConverter(*this).convertGlobalsToIce(Mod)); | 910 LLVM2ICEGlobalsConverter(*this, GlobalDeclarationsPool.get()) |
| 911 .convertGlobalsToIce(Mod); |
| 912 lowerGlobals(std::move(GlobalDeclarationsPool)); |
913 } | 913 } |
914 | 914 |
915 void Converter::convertFunctions() { | 915 void Converter::convertFunctions() { |
916 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; | 916 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; |
917 for (const Function &I : *Mod) { | 917 for (const Function &I : *Mod) { |
918 if (I.empty()) | 918 if (I.empty()) |
919 continue; | 919 continue; |
920 | 920 |
921 TimerIdT TimerID = 0; | 921 TimerIdT TimerID = 0; |
922 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); | 922 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); |
923 if (TimeThisFunction) { | 923 if (TimeThisFunction) { |
924 TimerID = Ctx->getTimerID(StackID, I.getName()); | 924 TimerID = Ctx->getTimerID(StackID, I.getName()); |
925 Ctx->pushTimer(TimerID, StackID); | 925 Ctx->pushTimer(TimerID, StackID); |
926 } | 926 } |
927 LLVM2ICEFunctionConverter FunctionConverter(*this); | 927 LLVM2ICEFunctionConverter FunctionConverter(*this); |
928 FunctionConverter.convertFunction(&I); | 928 FunctionConverter.convertFunction(&I); |
929 if (TimeThisFunction) | 929 if (TimeThisFunction) |
930 Ctx->popTimer(TimerID, StackID); | 930 Ctx->popTimer(TimerID, StackID); |
931 } | 931 } |
932 } | 932 } |
933 | 933 |
934 } // end of namespace Ice | 934 } // end of namespace Ice |
OLD | NEW |