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