OLD | NEW |
1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// | 1 //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
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 1197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1208 if (isIRGenerationDisabled()) { | 1208 if (isIRGenerationDisabled()) { |
1209 CurrentNode = nullptr; | 1209 CurrentNode = nullptr; |
1210 for (Ice::Type ArgType : Signature.getArgList()) { | 1210 for (Ice::Type ArgType : Signature.getArgList()) { |
1211 (void)ArgType; | 1211 (void)ArgType; |
1212 setNextLocalInstIndex(nullptr); | 1212 setNextLocalInstIndex(nullptr); |
1213 } | 1213 } |
1214 } else { | 1214 } else { |
1215 Func->setFunctionName(FuncDecl->getName()); | 1215 Func->setFunctionName(FuncDecl->getName()); |
1216 Func->setReturnType(Signature.getReturnType()); | 1216 Func->setReturnType(Signature.getReturnType()); |
1217 Func->setInternal(FuncDecl->getLinkage() == GlobalValue::InternalLinkage); | 1217 Func->setInternal(FuncDecl->getLinkage() == GlobalValue::InternalLinkage); |
1218 CurrentNode = installNextBasicBlock(); | 1218 constexpr NaClBcIndexSize_t EntryBlock = 0; |
| 1219 CurrentNode = getBasicBlock(EntryBlock); |
1219 Func->setEntryNode(CurrentNode); | 1220 Func->setEntryNode(CurrentNode); |
1220 for (Ice::Type ArgType : Signature.getArgList()) { | 1221 for (Ice::Type ArgType : Signature.getArgList()) { |
1221 Func->addArg(getNextInstVar(ArgType)); | 1222 Func->addArg(getNextInstVar(ArgType)); |
1222 } | 1223 } |
1223 } | 1224 } |
1224 bool ParserResult = ParseThisBlock(); | 1225 bool ParserResult = ParseThisBlock(); |
1225 | 1226 |
1226 // Temporarily end per-function timing, which will be resumed by | 1227 // Temporarily end per-function timing, which will be resumed by |
1227 // the translator function. This is because translation may be | 1228 // the translator function. This is because translation may be |
1228 // done asynchronously in a separate thread. | 1229 // done asynchronously in a separate thread. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1276 if (isIRGenerationDisabled()) | 1277 if (isIRGenerationDisabled()) |
1277 return nullptr; | 1278 return nullptr; |
1278 std::string Buffer; | 1279 std::string Buffer; |
1279 raw_string_ostream StrBuf(Buffer); | 1280 raw_string_ostream StrBuf(Buffer); |
1280 StrBuf << "Value index " << Index << " not defined!"; | 1281 StrBuf << "Value index " << Index << " not defined!"; |
1281 Fatal(StrBuf.str()); | 1282 Fatal(StrBuf.str()); |
1282 } | 1283 } |
1283 return Op; | 1284 return Op; |
1284 } | 1285 } |
1285 | 1286 |
| 1287 // Returns the Index-th basic block in the list of basic blocks. |
| 1288 Ice::CfgNode *getBasicBlock(NaClBcIndexSize_t Index) { |
| 1289 assert(!isIRGenerationDisabled()); |
| 1290 Ice::CfgNode *&Node = BbMap[Index]; |
| 1291 if (Node == nullptr) |
| 1292 Node = Func->makeNode(); |
| 1293 return Node; |
| 1294 } |
| 1295 |
1286 private: | 1296 private: |
| 1297 typedef std::unordered_map<NaClBcIndexSize_t, Ice::CfgNode *> CfgNodeMap; |
| 1298 |
1287 Ice::TimerMarker Timer; | 1299 Ice::TimerMarker Timer; |
1288 // The corresponding ICE function defined by the function block. | 1300 // The corresponding ICE function defined by the function block. |
1289 std::unique_ptr<Ice::Cfg> Func; | 1301 std::unique_ptr<Ice::Cfg> Func; |
| 1302 // The specified number of basic blocks in the bitcode file. |
| 1303 NaClBcIndexSize_t SpecifiedNumBbs = 0; |
1290 // The index to the current basic block being built. | 1304 // The index to the current basic block being built. |
1291 NaClBcIndexSize_t CurrentBbIndex = 0; | 1305 NaClBcIndexSize_t CurrentBbIndex = 0; |
1292 // The basic block being built. | 1306 // The basic block being built. |
1293 Ice::CfgNode *CurrentNode = nullptr; | 1307 Ice::CfgNode *CurrentNode = nullptr; |
| 1308 // Map from basic block id (as defined in the bitcode file) to |
| 1309 // the corresponding basic block that implements it. |
| 1310 CfgNodeMap BbMap; |
1294 // The ID for the function. | 1311 // The ID for the function. |
1295 NaClBcIndexSize_t FcnId; | 1312 NaClBcIndexSize_t FcnId; |
1296 // The corresponding function declaration. | 1313 // The corresponding function declaration. |
1297 Ice::FunctionDeclaration *FuncDecl; | 1314 Ice::FunctionDeclaration *FuncDecl; |
1298 // Holds the dividing point between local and global absolute value indices. | 1315 // Holds the dividing point between local and global absolute value indices. |
1299 size_t CachedNumGlobalValueIDs; | 1316 size_t CachedNumGlobalValueIDs; |
1300 // Holds operands local to the function block, based on indices | 1317 // Holds operands local to the function block, based on indices |
1301 // defined in the bitcode file. | 1318 // defined in the bitcode file. |
1302 std::vector<Ice::Operand *> LocalOperands; | 1319 std::vector<Ice::Operand *> LocalOperands; |
1303 // Holds the index within LocalOperands corresponding to the next | 1320 // Holds the index within LocalOperands corresponding to the next |
(...skipping 22 matching lines...) Expand all Loading... |
1326 // Error recover with value that is always acceptable. | 1343 // Error recover with value that is always acceptable. |
1327 Alignment = 1; | 1344 Alignment = 1; |
1328 } | 1345 } |
1329 | 1346 |
1330 bool ParseBlock(unsigned BlockID) override; | 1347 bool ParseBlock(unsigned BlockID) override; |
1331 | 1348 |
1332 void ProcessRecord() override; | 1349 void ProcessRecord() override; |
1333 | 1350 |
1334 void ExitBlock() override; | 1351 void ExitBlock() override; |
1335 | 1352 |
1336 // Creates and appends a new basic block to the list of basic blocks. | 1353 bool verifyAndRenameBasicBlocks(); |
1337 Ice::CfgNode *installNextBasicBlock() { | |
1338 assert(!isIRGenerationDisabled()); | |
1339 return Func->makeNode(); | |
1340 } | |
1341 | |
1342 // Returns the Index-th basic block in the list of basic blocks. | |
1343 Ice::CfgNode *getBasicBlock(NaClBcIndexSize_t Index) { | |
1344 assert(!isIRGenerationDisabled()); | |
1345 const Ice::NodeList &Nodes = Func->getNodes(); | |
1346 if (Index >= Nodes.size()) { | |
1347 std::string Buffer; | |
1348 raw_string_ostream StrBuf(Buffer); | |
1349 StrBuf << "Reference to basic block " << Index | |
1350 << " not found. Must be less than " << Nodes.size(); | |
1351 Error(StrBuf.str()); | |
1352 // TODO(kschimpf) Remove error recovery once implementation complete. | |
1353 Index = 0; | |
1354 } | |
1355 return Nodes[Index]; | |
1356 } | |
1357 | 1354 |
1358 // Returns the Index-th basic block in the list of basic blocks. | 1355 // Returns the Index-th basic block in the list of basic blocks. |
1359 // Assumes Index corresponds to a branch instruction. Hence, if | 1356 // Assumes Index corresponds to a branch instruction. Hence, if |
1360 // the branch references the entry block, it also generates a | 1357 // the branch references the entry block, it also generates a |
1361 // corresponding error. | 1358 // corresponding error. |
1362 Ice::CfgNode *getBranchBasicBlock(NaClBcIndexSize_t Index) { | 1359 Ice::CfgNode *getBranchBasicBlock(NaClBcIndexSize_t Index) { |
1363 assert(!isIRGenerationDisabled()); | 1360 assert(!isIRGenerationDisabled()); |
1364 if (Index == 0) { | 1361 if (Index == 0) { |
1365 Error("Branch to entry block not allowed"); | 1362 Error("Branch to entry block not allowed"); |
1366 // TODO(kschimpf) Remove error recovery once implementation complete. | 1363 // TODO(kschimpf) Remove error recovery once implementation complete. |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1954 void appendErrorInstruction(Ice::Type Ty) { | 1951 void appendErrorInstruction(Ice::Type Ty) { |
1955 // Note: we don't worry about downstream translation errors because | 1952 // Note: we don't worry about downstream translation errors because |
1956 // the function will not be translated if any errors occur. | 1953 // the function will not be translated if any errors occur. |
1957 if (Ty == Ice::IceType_void) | 1954 if (Ty == Ice::IceType_void) |
1958 return; | 1955 return; |
1959 Ice::Variable *Var = getNextInstVar(Ty); | 1956 Ice::Variable *Var = getNextInstVar(Ty); |
1960 CurrentNode->appendInst(Ice::InstAssign::create(Func.get(), Var, Var)); | 1957 CurrentNode->appendInst(Ice::InstAssign::create(Func.get(), Var, Var)); |
1961 } | 1958 } |
1962 }; | 1959 }; |
1963 | 1960 |
| 1961 bool FunctionParser::verifyAndRenameBasicBlocks() { |
| 1962 const size_t NumFoundBbs = BbMap.size(); |
| 1963 // Verify number of basic blocks found match amount specified in function. |
| 1964 if (NumFoundBbs != SpecifiedNumBbs) { |
| 1965 std::string Buffer; |
| 1966 raw_string_ostream StrBuf(Buffer); |
| 1967 StrBuf << "Function specified " << SpecifiedNumBbs |
| 1968 << "basic blocks. Found: " << NumFoundBbs; |
| 1969 Error(StrBuf.str()); |
| 1970 return false; |
| 1971 } |
| 1972 // Verify size limit allowed for basic blocks. |
| 1973 if (NumFoundBbs > NaClBcIndexSize_t_Max) { |
| 1974 std::string Buffer; |
| 1975 raw_string_ostream StrBuf(Buffer); |
| 1976 StrBuf << "Functions can't define more than " << NaClBcIndexSize_t_Max |
| 1977 << "basic blocks. Found: " << NumFoundBbs; |
| 1978 Error(StrBuf.str()); |
| 1979 return false; |
| 1980 } |
| 1981 // Sort list of Bbs, verifying that no basic blocks are missing. |
| 1982 Ice::NodeList SortedBbs; |
| 1983 for (size_t i = 0; i < NumFoundBbs; ++i) { |
| 1984 CfgNodeMap::iterator pos = BbMap.find(i); |
| 1985 if (pos == BbMap.end()) { |
| 1986 std::string Buffer; |
| 1987 raw_string_ostream StrBuf(Buffer); |
| 1988 StrBuf << "Can't find definition for basic block " << i << "."; |
| 1989 Error(StrBuf.str()); |
| 1990 return false; |
| 1991 } |
| 1992 SortedBbs.push_back(pos->second); |
| 1993 } |
| 1994 // Install sorted basic blocks. |
| 1995 Func->swapNodes(SortedBbs); |
| 1996 return true; |
| 1997 } |
| 1998 |
1964 void FunctionParser::ExitBlock() { | 1999 void FunctionParser::ExitBlock() { |
1965 // Check if the last instruction in the function was terminating. | 2000 // Check if the last instruction in the function was terminating. |
1966 if (!InstIsTerminating) { | 2001 if (!InstIsTerminating) { |
1967 Error("Last instruction in function not terminator"); | 2002 Error("Last instruction in function not terminator"); |
1968 if (isIRGenerationDisabled()) | 2003 if (isIRGenerationDisabled()) |
1969 return; | 2004 return; |
1970 // Recover by inserting an unreachable instruction. | 2005 // Recover by inserting an unreachable instruction. |
1971 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); | 2006 CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); |
1972 } | 2007 } |
1973 if (isIRGenerationDisabled()) | 2008 if (isIRGenerationDisabled()) |
1974 return; | 2009 return; |
| 2010 if (!verifyAndRenameBasicBlocks()) |
| 2011 return; |
1975 // Before translating, check for blocks without instructions, and | 2012 // Before translating, check for blocks without instructions, and |
1976 // insert unreachable. This shouldn't happen, but be safe. | 2013 // insert unreachable. This shouldn't happen, but be safe. |
1977 size_t Index = 0; | 2014 size_t Index = 0; |
1978 for (Ice::CfgNode *Node : Func->getNodes()) { | 2015 for (Ice::CfgNode *Node : Func->getNodes()) { |
1979 if (Node->getInsts().empty()) { | 2016 if (Node->getInsts().empty()) { |
1980 std::string Buffer; | 2017 std::string Buffer; |
1981 raw_string_ostream StrBuf(Buffer); | 2018 raw_string_ostream StrBuf(Buffer); |
1982 StrBuf << "Basic block " << Index << " contains no instructions"; | 2019 StrBuf << "Basic block " << Index << " contains no instructions"; |
1983 Error(StrBuf.str()); | 2020 Error(StrBuf.str()); |
1984 // TODO(kschimpf) Remove error recovery once implementation complete. | 2021 // TODO(kschimpf) Remove error recovery once implementation complete. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2023 NumBbsRaw = 1; | 2060 NumBbsRaw = 1; |
2024 } else if (NumBbsRaw > NaClBcIndexSize_t_Max) { | 2061 } else if (NumBbsRaw > NaClBcIndexSize_t_Max) { |
2025 std::string Buffer; | 2062 std::string Buffer; |
2026 raw_string_ostream StrBuf(Buffer); | 2063 raw_string_ostream StrBuf(Buffer); |
2027 StrBuf << "To many basic blocks specified: " << NumBbsRaw; | 2064 StrBuf << "To many basic blocks specified: " << NumBbsRaw; |
2028 Error(StrBuf.str()); | 2065 Error(StrBuf.str()); |
2029 NumBbsRaw = NaClBcIndexSize_t_Max; | 2066 NumBbsRaw = NaClBcIndexSize_t_Max; |
2030 } | 2067 } |
2031 if (isIRGenerationDisabled()) | 2068 if (isIRGenerationDisabled()) |
2032 return; | 2069 return; |
2033 if (Func->getNodes().size() != 1) { | 2070 if (SpecifiedNumBbs != 0) { |
2034 Error("Duplicate function block count record"); | 2071 Error("Duplicate function block count record"); |
2035 return; | 2072 return; |
2036 } | 2073 } |
2037 // Install the basic blocks, skipping bb0 which was created in the | 2074 SpecifiedNumBbs = NumBbsRaw; |
2038 // constructor. | |
2039 for (size_t i = 1, NumBbs = NumBbsRaw; i < NumBbs; ++i) | |
2040 installNextBasicBlock(); | |
2041 return; | 2075 return; |
2042 } | 2076 } |
2043 case naclbitc::FUNC_CODE_INST_BINOP: { | 2077 case naclbitc::FUNC_CODE_INST_BINOP: { |
2044 // BINOP: [opval, opval, opcode] | 2078 // BINOP: [opval, opval, opcode] |
2045 if (!isValidRecordSize(3, "binop")) | 2079 if (!isValidRecordSize(3, "binop")) |
2046 return; | 2080 return; |
2047 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); | 2081 Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); |
2048 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); | 2082 Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); |
2049 if (isIRGenerationDisabled()) { | 2083 if (isIRGenerationDisabled()) { |
2050 assert(Op1 == nullptr && Op2 == nullptr); | 2084 assert(Op1 == nullptr && Op2 == nullptr); |
(...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2854 std::string Nm(Name.data(), Name.size()); | 2888 std::string Nm(Name.data(), Name.size()); |
2855 V->setName(getFunctionParser()->getFunc(), Nm); | 2889 V->setName(getFunctionParser()->getFunc(), Nm); |
2856 } | 2890 } |
2857 } else { | 2891 } else { |
2858 reportUnableToAssign("variable", Index, Name); | 2892 reportUnableToAssign("variable", Index, Name); |
2859 } | 2893 } |
2860 } | 2894 } |
2861 | 2895 |
2862 void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index, | 2896 void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index, |
2863 StringType &Name) { | 2897 StringType &Name) { |
| 2898 if (!Ice::BuildDefs::dump()) |
| 2899 return; |
2864 if (isIRGenerationDisabled()) | 2900 if (isIRGenerationDisabled()) |
2865 return; | 2901 return; |
2866 if (Index >= getFunctionParser()->getFunc()->getNumNodes()) { | 2902 Ice::CfgNode *Bb = getFunctionParser()->getBasicBlock(Index); |
2867 reportUnableToAssign("block", Index, Name); | |
2868 return; | |
2869 } | |
2870 std::string Nm(Name.data(), Name.size()); | 2903 std::string Nm(Name.data(), Name.size()); |
2871 if (Ice::BuildDefs::dump()) | 2904 Bb->setName(Nm); |
2872 getFunctionParser()->getFunc()->getNodes()[Index]->setName(Nm); | |
2873 } | 2905 } |
2874 | 2906 |
2875 bool FunctionParser::ParseBlock(unsigned BlockID) { | 2907 bool FunctionParser::ParseBlock(unsigned BlockID) { |
2876 switch (BlockID) { | 2908 switch (BlockID) { |
2877 case naclbitc::CONSTANTS_BLOCK_ID: { | 2909 case naclbitc::CONSTANTS_BLOCK_ID: { |
2878 ConstantsParser Parser(BlockID, this); | 2910 ConstantsParser Parser(BlockID, this); |
2879 return Parser.ParseThisBlock(); | 2911 return Parser.ParseThisBlock(); |
2880 } | 2912 } |
2881 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { | 2913 case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
2882 if (PNaClAllowLocalSymbolTables) { | 2914 if (PNaClAllowLocalSymbolTables) { |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3108 } | 3140 } |
3109 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { | 3141 if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { |
3110 ErrStream | 3142 ErrStream |
3111 << IRFilename | 3143 << IRFilename |
3112 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; | 3144 << ": Bitcode stream should be a multiple of 4 bytes in length.\n"; |
3113 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); | 3145 llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); |
3114 } | 3146 } |
3115 } | 3147 } |
3116 | 3148 |
3117 } // end of namespace Ice | 3149 } // end of namespace Ice |
OLD | NEW |