From 81a0c0b44e582baca8b68754a7fcabfc3aef2e7a Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 31 May 2008 00:58:22 +0000 Subject: IR, bitcode reader, bitcode writer, and asmparser changes to insertvalue and extractvalue to use constant indices instead of Value* indices. And begin updating LangRef.html. There's definately more to come here, but I'm checking this basic support in now to make it available to people who are interested. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51806 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bitcode/Reader/BitcodeReader.cpp | 88 ++++++++++++++++++++++-------------- lib/Bitcode/Writer/BitcodeWriter.cpp | 26 +++++++---- 2 files changed, 71 insertions(+), 43 deletions(-) (limited to 'lib/Bitcode') diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 818b47c..60767bd 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -770,27 +770,45 @@ bool BitcodeReader::ParseConstants() { V = ConstantExpr::getGetElementPtr(Elts[0], &Elts[1], Elts.size()-1); break; } - case bitc::CST_CODE_CE_EXTRACTVAL: { // CE_EXTRACTVAL: [n x operands] - if (Record.size() & 1) return Error("Invalid CE_EXTRACTVAL record"); - SmallVector Elts; - for (unsigned i = 0, e = Record.size(); i != e; i += 2) { - const Type *ElTy = getTypeByID(Record[i]); - if (!ElTy) return Error("Invalid CE_EXTRACTVAL record"); - Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); + case bitc::CST_CODE_CE_EXTRACTVAL: { + // CE_EXTRACTVAL: [opty, opval, n x indices] + const Type *AggTy = getTypeByID(Record[0]); + if (!AggTy || !AggTy->isAggregateType()) + return Error("Invalid CE_INSERTVAL record"); + Constant *Agg = ValueList.getConstantFwdRef(Record[1], AggTy); + SmallVector Indices; + for (unsigned i = 2, e = Record.size(); i != e; ++i) { + uint64_t Index = Record[i]; + if ((unsigned)Index != Index) + return Error("Invalid CE_EXTRACTVAL record"); + Indices.push_back((unsigned)Index); } - V = ConstantExpr::getExtractValue(Elts[0], &Elts[1], Elts.size()-1); - break; - } - case bitc::CST_CODE_CE_INSERTVAL: { // CE_INSERTVAL: [n x operands] - if (Record.size() & 1) return Error("Invalid CE_INSERTVAL record"); - SmallVector Elts; - for (unsigned i = 0, e = Record.size(); i != e; i += 2) { - const Type *ElTy = getTypeByID(Record[i]); - if (!ElTy) return Error("Invalid CE_INSERTVAL record"); - Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy)); + if (!ExtractValueInst::getIndexedType(AggTy, + Indices.begin(), Indices.end())) + return Error("Invalid CE_EXTRACTVAL record"); + V = ConstantExpr::getExtractValue(Agg, &Indices[0], Indices.size()); + break; + } + case bitc::CST_CODE_CE_INSERTVAL: { + // CE_INSERTVAL: [opty, opval, opty, opval, n x indices] + const Type *AggTy = getTypeByID(Record[0]); + if (!AggTy || !AggTy->isAggregateType()) + return Error("Invalid CE_INSERTVAL record"); + Constant *Agg = ValueList.getConstantFwdRef(Record[1], AggTy); + const Type *ValTy = getTypeByID(Record[2]); + Constant *Val = ValueList.getConstantFwdRef(Record[2], ValTy); + SmallVector Indices; + for (unsigned i = 4, e = Record.size(); i != e; ++i) { + uint64_t Index = Record[i]; + if ((unsigned)Index != Index) + return Error("Invalid CE_INSERTVAL record"); + Indices.push_back((unsigned)Index); } - V = ConstantExpr::getInsertValue(Elts[0], Elts[1], - &Elts[2], Elts.size()-1); + if (ExtractValueInst::getIndexedType(AggTy, + Indices.begin(), + Indices.end()) != ValTy) + return Error("Invalid CE_INSERTVAL record"); + V = ConstantExpr::getInsertValue(Agg, Val, &Indices[0], Indices.size()); break; } case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#] @@ -1324,18 +1342,20 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { break; } - case bitc::FUNC_CODE_INST_EXTRACTVAL: { // EXTRACTVAL: [n x operands] + case bitc::FUNC_CODE_INST_EXTRACTVAL: { + // EXTRACTVAL: [opty, opval, n x indices] unsigned OpNum = 0; Value *Agg; if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) return Error("Invalid EXTRACTVAL record"); - SmallVector EXTRACTVALIdx; - while (OpNum != Record.size()) { - Value *Op; - if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error("Invalid EXTRACTVAL record"); - EXTRACTVALIdx.push_back(Op); + SmallVector EXTRACTVALIdx; + for (unsigned RecSize = Record.size(); + OpNum != RecSize; ++OpNum) { + uint64_t Index = Record[OpNum]; + if ((unsigned)Index != Index) + return Error("Invalid EXTRACTVAL index"); + EXTRACTVALIdx.push_back((unsigned)Index); } I = ExtractValueInst::Create(Agg, @@ -1343,7 +1363,8 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { break; } - case bitc::FUNC_CODE_INST_INSERTVAL: { // INSERTVAL: [n x operands] + case bitc::FUNC_CODE_INST_INSERTVAL: { + // INSERTVAL: [opty, opval, opty, opval, n x indices] unsigned OpNum = 0; Value *Agg; if (getValueTypePair(Record, OpNum, NextValueNo, Agg)) @@ -1352,12 +1373,13 @@ bool BitcodeReader::ParseFunctionBody(Function *F) { if (getValueTypePair(Record, OpNum, NextValueNo, Val)) return Error("Invalid INSERTVAL record"); - SmallVector INSERTVALIdx; - while (OpNum != Record.size()) { - Value *Op; - if (getValueTypePair(Record, OpNum, NextValueNo, Op)) - return Error("Invalid INSERTVAL record"); - INSERTVALIdx.push_back(Op); + SmallVector INSERTVALIdx; + for (unsigned RecSize = Record.size(); + OpNum != RecSize; ++OpNum) { + uint64_t Index = Record[OpNum]; + if ((unsigned)Index != Index) + return Error("Invalid INSERTVAL index"); + INSERTVALIdx.push_back((unsigned)Index); } I = InsertValueInst::Create(Agg, Val, diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index cb5963c..376cc05 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -610,20 +610,26 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, Record.push_back(VE.getValueID(C->getOperand(i))); } break; - case Instruction::ExtractValue: + case Instruction::ExtractValue: { Code = bitc::CST_CODE_CE_EXTRACTVAL; - for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { - Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); - Record.push_back(VE.getValueID(C->getOperand(i))); - } + Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); + Record.push_back(VE.getValueID(C->getOperand(0))); + const SmallVector &Indices = CE->getIndices(); + for (unsigned i = 0, e = Indices.size(); i != e; ++i) + Record.push_back(Indices[i]); break; - case Instruction::InsertValue: + } + case Instruction::InsertValue: { Code = bitc::CST_CODE_CE_INSERTVAL; - for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { - Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); - Record.push_back(VE.getValueID(C->getOperand(i))); - } + Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); + Record.push_back(VE.getValueID(C->getOperand(0))); + Record.push_back(VE.getTypeID(C->getOperand(1)->getType())); + Record.push_back(VE.getValueID(C->getOperand(1))); + const SmallVector &Indices = CE->getIndices(); + for (unsigned i = 0, e = Indices.size(); i != e; ++i) + Record.push_back(Indices[i]); break; + } case Instruction::Select: Code = bitc::CST_CODE_CE_SELECT; Record.push_back(VE.getValueID(C->getOperand(0))); -- cgit v1.1