summaryrefslogtreecommitdiff
path: root/lib/Bitcode
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2008-05-23 01:55:30 +0000
committerDan Gohman <gohman@apple.com>2008-05-23 01:55:30 +0000
commite4977cf750eaff28275429191821420c20b0c64f (patch)
treef1adda18e6df59cfb561a32a7d2034a053e06826 /lib/Bitcode
parent8f8e2692705f37d61e1840e799288f2ade1e410f (diff)
downloadllvm-e4977cf750eaff28275429191821420c20b0c64f.tar.gz
llvm-e4977cf750eaff28275429191821420c20b0c64f.tar.bz2
llvm-e4977cf750eaff28275429191821420c20b0c64f.tar.xz
Make structs and arrays first-class types, and add assembly
and bitcode support for the extractvalue and insertvalue instructions and constant expressions. Note that this does not yet include CodeGen support. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51468 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bitcode')
-rw-r--r--lib/Bitcode/Reader/BitcodeReader.cpp64
-rw-r--r--lib/Bitcode/Writer/BitcodeWriter.cpp24
-rw-r--r--lib/Bitcode/Writer/ValueEnumerator.cpp14
3 files changed, 95 insertions, 7 deletions
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index c4f2247edc..4fa08a0f3a 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -770,6 +770,29 @@ 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<Constant*, 16> 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));
+ }
+ 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<Constant*, 16> 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));
+ }
+ V = ConstantExpr::getInsertValue(Elts[0], Elts[1],
+ &Elts[2], Elts.size()-1);
+ break;
+ }
case bitc::CST_CODE_CE_SELECT: // CE_SELECT: [opval#, opval#, opval#]
if (Record.size() < 3) return Error("Invalid CE_SELECT record");
V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
@@ -1301,6 +1324,47 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
break;
}
+ case bitc::FUNC_CODE_INST_EXTRACTVAL: { // EXTRACTVAL: [n x operands]
+ unsigned OpNum = 0;
+ Value *Agg;
+ if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
+ return Error("Invalid EXTRACTVAL record");
+
+ SmallVector<Value*, 16> EXTRACTVALIdx;
+ while (OpNum != Record.size()) {
+ Value *Op;
+ if (getValueTypePair(Record, OpNum, NextValueNo, Op))
+ return Error("Invalid EXTRACTVAL record");
+ EXTRACTVALIdx.push_back(Op);
+ }
+
+ I = ExtractValueInst::Create(Agg,
+ EXTRACTVALIdx.begin(), EXTRACTVALIdx.end());
+ break;
+ }
+
+ case bitc::FUNC_CODE_INST_INSERTVAL: { // INSERTVAL: [n x operands]
+ unsigned OpNum = 0;
+ Value *Agg;
+ if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
+ return Error("Invalid INSERTVAL record");
+ Value *Val;
+ if (getValueTypePair(Record, OpNum, NextValueNo, Val))
+ return Error("Invalid INSERTVAL record");
+
+ SmallVector<Value*, 16> INSERTVALIdx;
+ while (OpNum != Record.size()) {
+ Value *Op;
+ if (getValueTypePair(Record, OpNum, NextValueNo, Op))
+ return Error("Invalid INSERTVAL record");
+ INSERTVALIdx.push_back(Op);
+ }
+
+ I = InsertValueInst::Create(Agg, Val,
+ INSERTVALIdx.begin(), INSERTVALIdx.end());
+ break;
+ }
+
case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
unsigned OpNum = 0;
Value *TrueVal, *FalseVal, *Cond;
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index ad651d36bf..cb5963cb8e 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -610,6 +610,20 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
Record.push_back(VE.getValueID(C->getOperand(i)));
}
break;
+ 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)));
+ }
+ break;
+ 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)));
+ }
+ break;
case Instruction::Select:
Code = bitc::CST_CODE_CE_SELECT;
Record.push_back(VE.getValueID(C->getOperand(0)));
@@ -718,6 +732,16 @@ static void WriteInstruction(const Instruction &I, unsigned InstID,
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
PushValueAndType(I.getOperand(i), InstID, Vals, VE);
break;
+ case Instruction::ExtractValue:
+ Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
+ for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
+ PushValueAndType(I.getOperand(i), InstID, Vals, VE);
+ break;
+ case Instruction::InsertValue:
+ Code = bitc::FUNC_CODE_INST_INSERTVAL;
+ for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
+ PushValueAndType(I.getOperand(i), InstID, Vals, VE);
+ break;
case Instruction::Select:
Code = bitc::FUNC_CODE_INST_SELECT;
PushValueAndType(I.getOperand(1), InstID, Vals, VE);
diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp
index 92271ce2b9..21d0dfe7eb 100644
--- a/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -21,9 +21,9 @@
#include <algorithm>
using namespace llvm;
-static bool isFirstClassType(const std::pair<const llvm::Type*,
- unsigned int> &P) {
- return P.first->isFirstClassType();
+static bool isSingleValueType(const std::pair<const llvm::Type*,
+ unsigned int> &P) {
+ return P.first->isSingleValueType();
}
static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
@@ -103,10 +103,10 @@ ValueEnumerator::ValueEnumerator(const Module *M) {
// in the table (have low bit-width).
std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
- // Partition the Type ID's so that the first-class types occur before the
+ // Partition the Type ID's so that the single-value types occur before the
// aggregate types. This allows the aggregate types to be dropped from the
// type table after parsing the global variable initializers.
- std::partition(Types.begin(), Types.end(), isFirstClassType);
+ std::partition(Types.begin(), Types.end(), isSingleValueType);
// Now that we rearranged the type table, rebuild TypeMap.
for (unsigned i = 0, e = Types.size(); i != e; ++i)
@@ -264,11 +264,11 @@ void ValueEnumerator::EnumerateParamAttrs(const PAListPtr &PAL) {
/// there are none, return -1.
int ValueEnumerator::PurgeAggregateValues() {
// If there are no aggregate values at the end of the list, return -1.
- if (Values.empty() || Values.back().first->getType()->isFirstClassType())
+ if (Values.empty() || Values.back().first->getType()->isSingleValueType())
return -1;
// Otherwise, remove aggregate values...
- while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
+ while (!Values.empty() && !Values.back().first->getType()->isSingleValueType())
Values.pop_back();
// ... and return the new size.