summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
diff options
context:
space:
mode:
authorStepan Dyatkovskiy <stpworld@narod.ru>2012-05-12 10:48:17 +0000
committerStepan Dyatkovskiy <stpworld@narod.ru>2012-05-12 10:48:17 +0000
commit0beab5e1cde833214271d533afea5405aae7e0ce (patch)
treedab10bdcbf0fa2c6fa0d069354a47feca9f94fbf /llvm/lib/Bitcode/Reader/BitcodeReader.cpp
parentca0c4996098068828c7688103cd0a80edf8cba09 (diff)
downloadbcm5719-llvm-0beab5e1cde833214271d533afea5405aae7e0ce.tar.gz
bcm5719-llvm-0beab5e1cde833214271d533afea5405aae7e0ce.zip
Recommited r156374 with critical fixes in BitcodeReader/Writer:
Ordinary patch for PR1255. Added new case-ranges orientated methods for adding/removing cases in SwitchInst. After this patch cases will internally representated as ConstantArray-s instead of ConstantInt, externally cases wrapped within the ConstantRangesSet object. Old methods of SwitchInst are also works well, but marked as deprecated. So on this stage we have no side effects except that I added support for case ranges in BitcodeReader/Writer, of course test for Bitcode is also added. Old "switch" format is also supported. llvm-svn: 156704
Diffstat (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp')
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.cpp82
1 files changed, 75 insertions, 7 deletions
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 040f3438fe9..49b6e4f42ae 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -28,6 +28,10 @@
#include "llvm/OperandTraits.h"
using namespace llvm;
+enum {
+ SWITCH_INST_MAGIC = 0x4B5 // May 2012 => 1205 => Hex
+};
+
void BitcodeReader::materializeForwardReferencedFunctions() {
while (!BlockAddrFwdRefs.empty()) {
Function *F = BlockAddrFwdRefs.begin()->first;
@@ -927,6 +931,16 @@ bool BitcodeReader::ResolveGlobalAndAliasInits() {
return false;
}
+APInt ReadWideAPInt(const uint64_t *Vals, unsigned ActiveWords,
+ unsigned TypeBits) {
+ SmallVector<uint64_t, 8> Words;
+ Words.resize(ActiveWords);
+ for (unsigned i = 0; i != ActiveWords; ++i)
+ Words[i] = DecodeSignRotatedValue(Vals[i]);
+
+ return APInt(TypeBits, Words);
+}
+
bool BitcodeReader::ParseConstants() {
if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
return Error("Malformed block record");
@@ -983,13 +997,11 @@ bool BitcodeReader::ParseConstants() {
return Error("Invalid WIDE_INTEGER record");
unsigned NumWords = Record.size();
- SmallVector<uint64_t, 8> Words;
- Words.resize(NumWords);
- for (unsigned i = 0; i != NumWords; ++i)
- Words[i] = DecodeSignRotatedValue(Record[i]);
- V = ConstantInt::get(Context,
- APInt(cast<IntegerType>(CurTy)->getBitWidth(),
- Words));
+
+ APInt VInt = ReadWideAPInt(&Record[0], NumWords,
+ cast<IntegerType>(CurTy)->getBitWidth());
+ V = ConstantInt::get(Context, VInt);
+
break;
}
case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
@@ -2221,6 +2233,62 @@ bool BitcodeReader::ParseFunctionBody(Function *F) {
break;
}
case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
+ // Check magic
+ if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
+ // New SwitchInst format with case ranges.
+
+ Type *OpTy = getTypeByID(Record[1]);
+ unsigned ValueBitWidth = cast<IntegerType>(OpTy)->getBitWidth();
+
+ Value *Cond = getFnValueByID(Record[2], OpTy);
+ BasicBlock *Default = getBasicBlock(Record[3]);
+ if (OpTy == 0 || Cond == 0 || Default == 0)
+ return Error("Invalid SWITCH record");
+
+ unsigned NumCases = Record[4];
+
+ SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
+ InstructionList.push_back(SI);
+
+ unsigned CurIdx = 5;
+ for (unsigned i = 0; i != NumCases; ++i) {
+ CRSBuilder CaseBuilder;
+ unsigned NumItems = Record[CurIdx++];
+ for (unsigned ci = 0; ci != NumItems; ++ci) {
+ bool isSingleNumber = Record[CurIdx++];
+
+ APInt Low;
+ unsigned ActiveWords = 1;
+ if (ValueBitWidth > 64)
+ ActiveWords = Record[CurIdx++];
+ Low = ReadWideAPInt(&Record[CurIdx], ActiveWords, ValueBitWidth);
+ CurIdx += ActiveWords;
+
+ if (!isSingleNumber) {
+ ActiveWords = 1;
+ if (ValueBitWidth > 64)
+ ActiveWords = Record[CurIdx++];
+ APInt High =
+ ReadWideAPInt(&Record[CurIdx], ActiveWords, ValueBitWidth);
+ CaseBuilder.add(cast<ConstantInt>(ConstantInt::get(OpTy, Low)),
+ cast<ConstantInt>(ConstantInt::get(OpTy, High)));
+ CurIdx += ActiveWords;
+ } else
+ CaseBuilder.add(cast<ConstantInt>(ConstantInt::get(OpTy, Low)));
+ }
+ BasicBlock *DestBB = getBasicBlock(Record[CurIdx++]);
+ ConstantRangesSet Case = CaseBuilder.getCase();
+ SI->addCase(Case, DestBB);
+ }
+ uint16_t Hash = SI->Hash();
+ if (Hash != (Record[0] & 0xFFFF))
+ return Error("Invalid SWITCH record");
+ I = SI;
+ break;
+ }
+
+ // Old SwitchInst format without case ranges.
+
if (Record.size() < 3 || (Record.size() & 1) == 0)
return Error("Invalid SWITCH record");
Type *OpTy = getTypeByID(Record[0]);
OpenPOWER on IntegriCloud