| 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. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include "IceConverter.h" | 14 #include "IceConverter.h" |
| 15 | 15 |
| 16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
| 17 #include "IceCfgNode.h" | 17 #include "IceCfgNode.h" |
| 18 #include "IceClFlags.h" | 18 #include "IceClFlags.h" |
| 19 #include "IceDefs.h" | 19 #include "IceDefs.h" |
| 20 #include "IceGlobalContext.h" | 20 #include "IceGlobalContext.h" |
| 21 #include "IceGlobalInits.h" | 21 #include "IceGlobalInits.h" |
| 22 #include "IceInst.h" | 22 #include "IceInst.h" |
| 23 #include "IceOperand.h" | 23 #include "IceOperand.h" |
| 24 #include "IceTargetLowering.h" | 24 #include "IceTargetLowering.h" |
| 25 #include "IceTypes.h" | 25 #include "IceTypes.h" |
| 26 #include "IceTypeConverter.h" | 26 #include "IceTypeConverter.h" |
| 27 | 27 |
| 28 #pragma clang diagnostic push | 28 #pragma clang diagnostic push |
| 29 #pragma clang diagnostic ignored "-Wunused-parameter" | 29 #pragma clang diagnostic ignored "-Wunused-parameter" |
| 30 #pragma clang diagnostic ignored "-Wshadow" |
| 30 #include "llvm/IR/Constant.h" | 31 #include "llvm/IR/Constant.h" |
| 31 #include "llvm/IR/Constants.h" | 32 #include "llvm/IR/Constants.h" |
| 32 #include "llvm/IR/DataLayout.h" | 33 #include "llvm/IR/DataLayout.h" |
| 33 #include "llvm/IR/Instruction.h" | 34 #include "llvm/IR/Instruction.h" |
| 34 #include "llvm/IR/Instructions.h" | 35 #include "llvm/IR/Instructions.h" |
| 35 #include "llvm/IR/LLVMContext.h" | 36 #include "llvm/IR/LLVMContext.h" |
| 36 #include "llvm/IR/Module.h" | 37 #include "llvm/IR/Module.h" |
| 37 #pragma clang diagnostic pop | 38 #pragma clang diagnostic pop |
| 38 | 39 |
| 39 // TODO(kschimpf): Remove two namespaces being visible at once. | 40 // TODO(kschimpf): Remove two namespaces being visible at once. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 53 // TODO(stichnot): Redesign Converter, LLVM2ICEConverter, | 54 // TODO(stichnot): Redesign Converter, LLVM2ICEConverter, |
| 54 // LLVM2ICEFunctionConverter, and LLVM2ICEGlobalsConverter with | 55 // LLVM2ICEFunctionConverter, and LLVM2ICEGlobalsConverter with |
| 55 // respect to Translator. In particular, the unique_ptr ownership | 56 // respect to Translator. In particular, the unique_ptr ownership |
| 56 // rules in LLVM2ICEFunctionConverter. | 57 // rules in LLVM2ICEFunctionConverter. |
| 57 class LLVM2ICEConverter { | 58 class LLVM2ICEConverter { |
| 58 LLVM2ICEConverter() = delete; | 59 LLVM2ICEConverter() = delete; |
| 59 LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; | 60 LLVM2ICEConverter(const LLVM2ICEConverter &) = delete; |
| 60 LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; | 61 LLVM2ICEConverter &operator=(const LLVM2ICEConverter &) = delete; |
| 61 | 62 |
| 62 public: | 63 public: |
| 63 explicit LLVM2ICEConverter(Ice::Converter &Converter) | 64 explicit LLVM2ICEConverter(Ice::Converter &MyConverter) |
| 64 : Converter(Converter), Ctx(Converter.getContext()), | 65 : Converter(MyConverter), Ctx(MyConverter.getContext()), |
| 65 TypeConverter(Converter.getModule()->getContext()) {} | 66 TypeConverter(MyConverter.getModule()->getContext()) {} |
| 66 | 67 |
| 67 Ice::Converter &getConverter() const { return Converter; } | 68 Ice::Converter &getConverter() const { return Converter; } |
| 68 | 69 |
| 69 protected: | 70 protected: |
| 70 Ice::Converter &Converter; | 71 Ice::Converter &Converter; |
| 71 Ice::GlobalContext *Ctx; | 72 Ice::GlobalContext *Ctx; |
| 72 const Ice::TypeConverter TypeConverter; | 73 const Ice::TypeConverter TypeConverter; |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 // Converter from LLVM functions to ICE. The entry point is the | 76 // Converter from LLVM functions to ICE. The entry point is the |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 Converter.translateFcn(std::move(Func)); | 122 Converter.translateFcn(std::move(Func)); |
| 122 } | 123 } |
| 123 | 124 |
| 124 // convertConstant() does not use Func or require it to be a valid | 125 // convertConstant() does not use Func or require it to be a valid |
| 125 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing | 126 // Ice::Cfg pointer. As such, it's suitable for e.g. constructing |
| 126 // global initializers. | 127 // global initializers. |
| 127 Ice::Constant *convertConstant(const Constant *Const) { | 128 Ice::Constant *convertConstant(const Constant *Const) { |
| 128 if (const auto GV = dyn_cast<GlobalValue>(Const)) { | 129 if (const auto GV = dyn_cast<GlobalValue>(Const)) { |
| 129 Ice::GlobalDeclaration *Decl = getConverter().getGlobalDeclaration(GV); | 130 Ice::GlobalDeclaration *Decl = getConverter().getGlobalDeclaration(GV); |
| 130 bool IsUndefined = false; | 131 bool IsUndefined = false; |
| 131 if (const auto *Func = llvm::dyn_cast<Ice::FunctionDeclaration>(Decl)) | 132 if (const auto *MyFunc = llvm::dyn_cast<Ice::FunctionDeclaration>(Decl)) |
| 132 IsUndefined = Func->isProto(); | 133 IsUndefined = MyFunc->isProto(); |
| 133 else if (const auto *Var = llvm::dyn_cast<Ice::VariableDeclaration>(Decl)) | 134 else if (const auto *Var = llvm::dyn_cast<Ice::VariableDeclaration>(Decl)) |
| 134 IsUndefined = !Var->hasInitializer(); | 135 IsUndefined = !Var->hasInitializer(); |
| 135 else | 136 else |
| 136 report_fatal_error("Unhandled GlobalDeclaration type"); | 137 report_fatal_error("Unhandled GlobalDeclaration type"); |
| 137 if (IsUndefined) | 138 if (IsUndefined) |
| 138 return Ctx->getConstantExternSym(Decl->getName()); | 139 return Ctx->getConstantExternSym(Decl->getName()); |
| 139 else { | 140 else { |
| 140 const Ice::RelocOffsetT Offset = 0; | 141 const Ice::RelocOffsetT Offset = 0; |
| 141 return Ctx->getConstantSym(Offset, Decl->getName(), | 142 return Ctx->getConstantSym(Offset, Decl->getName(), |
| 142 Decl->getSuppressMangling()); | 143 Decl->getSuppressMangling()); |
| (...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 else { | 727 else { |
| 727 std::string Buffer; | 728 std::string Buffer; |
| 728 raw_string_ostream StrBuf(Buffer); | 729 raw_string_ostream StrBuf(Buffer); |
| 729 StrBuf << "Global declaration missing initializer: " << GV->getName(); | 730 StrBuf << "Global declaration missing initializer: " << GV->getName(); |
| 730 report_fatal_error(StrBuf.str()); | 731 report_fatal_error(StrBuf.str()); |
| 731 } | 732 } |
| 732 } | 733 } |
| 733 | 734 |
| 734 const Constant *Initializer = GV->getInitializer(); | 735 const Constant *Initializer = GV->getInitializer(); |
| 735 if (const auto CompoundInit = dyn_cast<ConstantStruct>(Initializer)) { | 736 if (const auto CompoundInit = dyn_cast<ConstantStruct>(Initializer)) { |
| 736 for (ConstantStruct::const_op_iterator I = CompoundInit->op_begin(), | 737 for (ConstantStruct::const_op_iterator CII = CompoundInit->op_begin(), |
| 737 E = CompoundInit->op_end(); | 738 CIE = CompoundInit->op_end(); |
| 738 I != E; ++I) { | 739 CII != CIE; ++CII) { |
| 739 if (const auto Init = dyn_cast<Constant>(I)) { | 740 if (const auto Init = dyn_cast<Constant>(CII)) { |
| 740 addGlobalInitializer(*VarDecl, Init); | 741 addGlobalInitializer(*VarDecl, Init); |
| 741 } | 742 } |
| 742 } | 743 } |
| 743 } else { | 744 } else { |
| 744 addGlobalInitializer(*VarDecl, Initializer); | 745 addGlobalInitializer(*VarDecl, Initializer); |
| 745 } | 746 } |
| 746 } | 747 } |
| 747 return std::move(VariableDeclarations); | 748 return std::move(VariableDeclarations); |
| 748 } | 749 } |
| 749 | 750 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 std::string Buffer; | 800 std::string Buffer; |
| 800 raw_string_ostream StrBuf(Buffer); | 801 raw_string_ostream StrBuf(Buffer); |
| 801 StrBuf << "Unhandled global initializer: " << Initializer; | 802 StrBuf << "Unhandled global initializer: " << Initializer; |
| 802 report_fatal_error(StrBuf.str()); | 803 report_fatal_error(StrBuf.str()); |
| 803 } | 804 } |
| 804 | 805 |
| 805 } // end of anonymous namespace | 806 } // end of anonymous namespace |
| 806 | 807 |
| 807 namespace Ice { | 808 namespace Ice { |
| 808 | 809 |
| 809 void Converter::nameUnnamedGlobalVariables(Module *Mod) { | 810 void Converter::nameUnnamedGlobalVariables(Module *MyMod) { |
| 810 const IceString &GlobalPrefix = Ctx->getFlags().getDefaultGlobalPrefix(); | 811 const IceString &GlobalPrefix = Ctx->getFlags().getDefaultGlobalPrefix(); |
| 811 if (GlobalPrefix.empty()) | 812 if (GlobalPrefix.empty()) |
| 812 return; | 813 return; |
| 813 uint32_t NameIndex = 0; | 814 uint32_t NameIndex = 0; |
| 814 for (auto V = Mod->global_begin(), E = Mod->global_end(); V != E; ++V) { | 815 for (auto V = MyMod->global_begin(), E = MyMod->global_end(); V != E; ++V) { |
| 815 if (!V->hasName()) { | 816 if (!V->hasName()) { |
| 816 V->setName(createUnnamedName(GlobalPrefix, NameIndex)); | 817 V->setName(createUnnamedName(GlobalPrefix, NameIndex)); |
| 817 ++NameIndex; | 818 ++NameIndex; |
| 818 } else { | 819 } else { |
| 819 checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix); | 820 checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix); |
| 820 } | 821 } |
| 821 } | 822 } |
| 822 } | 823 } |
| 823 | 824 |
| 824 void Converter::nameUnnamedFunctions(Module *Mod) { | 825 void Converter::nameUnnamedFunctions(Module *MyMod) { |
| 825 const IceString &FunctionPrefix = Ctx->getFlags().getDefaultFunctionPrefix(); | 826 const IceString &FunctionPrefix = Ctx->getFlags().getDefaultFunctionPrefix(); |
| 826 if (FunctionPrefix.empty()) | 827 if (FunctionPrefix.empty()) |
| 827 return; | 828 return; |
| 828 uint32_t NameIndex = 0; | 829 uint32_t NameIndex = 0; |
| 829 for (Function &F : *Mod) { | 830 for (Function &F : *MyMod) { |
| 830 if (!F.hasName()) { | 831 if (!F.hasName()) { |
| 831 F.setName(createUnnamedName(FunctionPrefix, NameIndex)); | 832 F.setName(createUnnamedName(FunctionPrefix, NameIndex)); |
| 832 ++NameIndex; | 833 ++NameIndex; |
| 833 } else { | 834 } else { |
| 834 checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix); | 835 checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix); |
| 835 } | 836 } |
| 836 } | 837 } |
| 837 } | 838 } |
| 838 | 839 |
| 839 void Converter::convertToIce() { | 840 void Converter::convertToIce() { |
| 840 TimerMarker T(TimerStack::TT_convertToIce, Ctx); | 841 TimerMarker T(TimerStack::TT_convertToIce, Ctx); |
| 841 nameUnnamedGlobalVariables(Mod); | 842 nameUnnamedGlobalVariables(Mod); |
| 842 nameUnnamedFunctions(Mod); | 843 nameUnnamedFunctions(Mod); |
| 843 installGlobalDeclarations(Mod); | 844 installGlobalDeclarations(Mod); |
| 844 convertGlobals(Mod); | 845 convertGlobals(Mod); |
| 845 convertFunctions(); | 846 convertFunctions(); |
| 846 } | 847 } |
| 847 | 848 |
| 848 GlobalDeclaration *Converter::getGlobalDeclaration(const GlobalValue *V) { | 849 GlobalDeclaration *Converter::getGlobalDeclaration(const GlobalValue *V) { |
| 849 GlobalDeclarationMapType::const_iterator Pos = GlobalDeclarationMap.find(V); | 850 GlobalDeclarationMapType::const_iterator Pos = GlobalDeclarationMap.find(V); |
| 850 if (Pos == GlobalDeclarationMap.end()) { | 851 if (Pos == GlobalDeclarationMap.end()) { |
| 851 std::string Buffer; | 852 std::string Buffer; |
| 852 raw_string_ostream StrBuf(Buffer); | 853 raw_string_ostream StrBuf(Buffer); |
| 853 StrBuf << "Can't find global declaration for: " << V->getName(); | 854 StrBuf << "Can't find global declaration for: " << V->getName(); |
| 854 report_fatal_error(StrBuf.str()); | 855 report_fatal_error(StrBuf.str()); |
| 855 } | 856 } |
| 856 return Pos->second; | 857 return Pos->second; |
| 857 } | 858 } |
| 858 | 859 |
| 859 void Converter::installGlobalDeclarations(Module *Mod) { | 860 void Converter::installGlobalDeclarations(Module *MyMod) { |
| 860 const TypeConverter Converter(Mod->getContext()); | 861 const TypeConverter Converter(MyMod->getContext()); |
| 861 // Install function declarations. | 862 // Install function declarations. |
| 862 for (const Function &Func : *Mod) { | 863 for (const Function &Func : *MyMod) { |
| 863 FuncSigType Signature; | 864 FuncSigType Signature; |
| 864 FunctionType *FuncType = Func.getFunctionType(); | 865 FunctionType *FuncType = Func.getFunctionType(); |
| 865 Signature.setReturnType( | 866 Signature.setReturnType( |
| 866 Converter.convertToIceType(FuncType->getReturnType())); | 867 Converter.convertToIceType(FuncType->getReturnType())); |
| 867 for (size_t I = 0; I < FuncType->getNumParams(); ++I) { | 868 for (size_t I = 0; I < FuncType->getNumParams(); ++I) { |
| 868 Signature.appendArgType( | 869 Signature.appendArgType( |
| 869 Converter.convertToIceType(FuncType->getParamType(I))); | 870 Converter.convertToIceType(FuncType->getParamType(I))); |
| 870 } | 871 } |
| 871 FunctionDeclaration *IceFunc = FunctionDeclaration::create( | 872 FunctionDeclaration *IceFunc = FunctionDeclaration::create( |
| 872 Ctx, Signature, Func.getCallingConv(), Func.getLinkage(), Func.empty()); | 873 Ctx, Signature, Func.getCallingConv(), Func.getLinkage(), Func.empty()); |
| 873 IceFunc->setName(Func.getName()); | 874 IceFunc->setName(Func.getName()); |
| 874 GlobalDeclarationMap[&Func] = IceFunc; | 875 GlobalDeclarationMap[&Func] = IceFunc; |
| 875 } | 876 } |
| 876 // Install global variable declarations. | 877 // Install global variable declarations. |
| 877 for (Module::const_global_iterator I = Mod->global_begin(), | 878 for (Module::const_global_iterator I = MyMod->global_begin(), |
| 878 E = Mod->global_end(); | 879 E = MyMod->global_end(); |
| 879 I != E; ++I) { | 880 I != E; ++I) { |
| 880 const GlobalVariable *GV = I; | 881 const GlobalVariable *GV = I; |
| 881 VariableDeclaration *Var = VariableDeclaration::create(Ctx); | 882 VariableDeclaration *Var = VariableDeclaration::create(Ctx); |
| 882 Var->setName(GV->getName()); | 883 Var->setName(GV->getName()); |
| 883 Var->setAlignment(GV->getAlignment()); | 884 Var->setAlignment(GV->getAlignment()); |
| 884 Var->setIsConstant(GV->isConstant()); | 885 Var->setIsConstant(GV->isConstant()); |
| 885 Var->setLinkage(GV->getLinkage()); | 886 Var->setLinkage(GV->getLinkage()); |
| 886 GlobalDeclarationMap[GV] = Var; | 887 GlobalDeclarationMap[GV] = Var; |
| 887 } | 888 } |
| 888 } | 889 } |
| 889 | 890 |
| 890 void Converter::convertGlobals(Module *Mod) { | 891 void Converter::convertGlobals(Module *MyMod) { |
| 891 lowerGlobals(LLVM2ICEGlobalsConverter(*this).convertGlobalsToIce(Mod)); | 892 lowerGlobals(LLVM2ICEGlobalsConverter(*this).convertGlobalsToIce(MyMod)); |
| 892 } | 893 } |
| 893 | 894 |
| 894 void Converter::convertFunctions() { | 895 void Converter::convertFunctions() { |
| 895 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; | 896 const TimerStackIdT StackID = GlobalContext::TSK_Funcs; |
| 896 for (const Function &I : *Mod) { | 897 for (const Function &I : *Mod) { |
| 897 if (I.empty()) | 898 if (I.empty()) |
| 898 continue; | 899 continue; |
| 899 | 900 |
| 900 TimerIdT TimerID = 0; | 901 TimerIdT TimerID = 0; |
| 901 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); | 902 const bool TimeThisFunction = Ctx->getFlags().getTimeEachFunction(); |
| 902 if (TimeThisFunction) { | 903 if (TimeThisFunction) { |
| 903 TimerID = Ctx->getTimerID(StackID, I.getName()); | 904 TimerID = Ctx->getTimerID(StackID, I.getName()); |
| 904 Ctx->pushTimer(TimerID, StackID); | 905 Ctx->pushTimer(TimerID, StackID); |
| 905 } | 906 } |
| 906 LLVM2ICEFunctionConverter FunctionConverter(*this); | 907 LLVM2ICEFunctionConverter FunctionConverter(*this); |
| 907 FunctionConverter.convertFunction(&I); | 908 FunctionConverter.convertFunction(&I); |
| 908 if (TimeThisFunction) | 909 if (TimeThisFunction) |
| 909 Ctx->popTimer(TimerID, StackID); | 910 Ctx->popTimer(TimerID, StackID); |
| 910 } | 911 } |
| 911 } | 912 } |
| 912 | 913 |
| 913 } // end of namespace Ice | 914 } // end of namespace Ice |
| OLD | NEW |