Skip to content

Commit b5b5efd

Browse files
committedFeb 25, 2015
[opaque pointer type] Bitcode support for explicit type parameter on GEP.
Like r230414, add bitcode support including backwards compatibility, for an explicit type parameter to GEP. At the suggestion of Duncan I tried coalescing the two older bitcodes into a single new bitcode, though I did hit a wrinkle: I couldn't figure out how to create an explicit abbreviation for a record with a variable number of arguments (the indicies to the gep). This means the discriminator between inbounds and non-inbounds gep is a full variable-length field I believe? Is my understanding correct? Is there a way to create such an abbreviation? Should I just use two bitcodes as before? Reviewers: dexonsmith Differential Revision: http://reviews.llvm.org/D7736 llvm-svn: 230415
1 parent 8503565 commit b5b5efd

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed
 

‎llvm/include/llvm/Bitcode/LLVMBitCodes.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ namespace bitc {
293293

294294
FUNC_CODE_INST_BINOP = 2, // BINOP: [opcode, ty, opval, opval]
295295
FUNC_CODE_INST_CAST = 3, // CAST: [opcode, ty, opty, opval]
296-
FUNC_CODE_INST_GEP = 4, // GEP: [n x operands]
296+
FUNC_CODE_INST_GEP_OLD = 4, // GEP: [n x operands]
297297
FUNC_CODE_INST_SELECT = 5, // SELECT: [ty, opval, opval, opval]
298298
FUNC_CODE_INST_EXTRACTELT = 6, // EXTRACTELT: [opty, opval, opval]
299299
FUNC_CODE_INST_INSERTELT = 7, // INSERTELT: [ty, opval, opval, opval]
@@ -327,7 +327,7 @@ namespace bitc {
327327
FUNC_CODE_INST_CMP2 = 28, // CMP2: [opty, opval, opval, pred]
328328
// new select on i1 or [N x i1]
329329
FUNC_CODE_INST_VSELECT = 29, // VSELECT: [ty,opval,opval,predty,pred]
330-
FUNC_CODE_INST_INBOUNDS_GEP= 30, // INBOUNDS_GEP: [n x operands]
330+
FUNC_CODE_INST_INBOUNDS_GEP_OLD = 30, // INBOUNDS_GEP: [n x operands]
331331
FUNC_CODE_INST_INDIRECTBR = 31, // INDIRECTBR: [opty, op0, op1, ...]
332332
// 32 is unused.
333333
FUNC_CODE_DEBUG_LOC_AGAIN = 33, // DEBUG_LOC_AGAIN
@@ -345,8 +345,9 @@ namespace bitc {
345345
FUNC_CODE_INST_LANDINGPAD = 40, // LANDINGPAD: [ty,val,val,num,id0,val0...]
346346
FUNC_CODE_INST_LOADATOMIC = 41, // LOAD: [opty, op, align, vol,
347347
// ordering, synchscope]
348-
FUNC_CODE_INST_STOREATOMIC = 42 // STORE: [ptrty,ptr,val, align, vol
348+
FUNC_CODE_INST_STOREATOMIC = 42, // STORE: [ptrty,ptr,val, align, vol
349349
// ordering, synchscope]
350+
FUNC_CODE_INST_GEP = 43, // GEP: [inbounds, n x operands]
350351
};
351352

352353
enum UseListCodes {

‎llvm/include/llvm/IR/Instructions.h

+7
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,13 @@ class GetElementPtrInst : public Instruction {
845845
return cast<SequentialType>(Instruction::getType());
846846
}
847847

848+
Type *getSourceElementType() const {
849+
SequentialType *Ty = cast<SequentialType>(getPointerOperandType());
850+
if (VectorType *VTy = dyn_cast<VectorType>(Ty))
851+
Ty = cast<SequentialType>(VTy->getElementType());
852+
return Ty->getElementType();
853+
}
854+
848855
/// \brief Returns the address space of this instruction's pointer type.
849856
unsigned getAddressSpace() const {
850857
// Note that this is always the same as the pointer operand's address space

‎llvm/lib/Bitcode/Reader/BitcodeReader.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -3062,9 +3062,22 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
30623062
InstructionList.push_back(I);
30633063
break;
30643064
}
3065-
case bitc::FUNC_CODE_INST_INBOUNDS_GEP:
3066-
case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
3065+
case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
3066+
case bitc::FUNC_CODE_INST_GEP_OLD:
3067+
case bitc::FUNC_CODE_INST_GEP: { // GEP: type, [n x operands]
30673068
unsigned OpNum = 0;
3069+
3070+
Type *Ty;
3071+
bool InBounds;
3072+
3073+
if (BitCode == bitc::FUNC_CODE_INST_GEP) {
3074+
InBounds = Record[OpNum++];
3075+
Ty = getTypeByID(Record[OpNum++]);
3076+
} else {
3077+
InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
3078+
Ty = nullptr;
3079+
}
3080+
30683081
Value *BasePtr;
30693082
if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
30703083
return Error("Invalid record");
@@ -3078,8 +3091,9 @@ std::error_code BitcodeReader::ParseFunctionBody(Function *F) {
30783091
}
30793092

30803093
I = GetElementPtrInst::Create(BasePtr, GEPIdx);
3094+
assert(!Ty || Ty == cast<GetElementPtrInst>(I)->getSourceElementType());
30813095
InstructionList.push_back(I);
3082-
if (BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP)
3096+
if (InBounds)
30833097
cast<GetElementPtrInst>(I)->setIsInBounds(true);
30843098
break;
30853099
}

‎llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ enum {
5656
FUNCTION_INST_CAST_ABBREV,
5757
FUNCTION_INST_RET_VOID_ABBREV,
5858
FUNCTION_INST_RET_VAL_ABBREV,
59-
FUNCTION_INST_UNREACHABLE_ABBREV
59+
FUNCTION_INST_UNREACHABLE_ABBREV,
60+
FUNCTION_INST_GEP_ABBREV,
6061
};
6162

6263
static unsigned GetEncodedCastOpcode(unsigned Opcode) {
@@ -1675,13 +1676,16 @@ static void WriteInstruction(const Instruction &I, unsigned InstID,
16751676
}
16761677
break;
16771678

1678-
case Instruction::GetElementPtr:
1679+
case Instruction::GetElementPtr: {
16791680
Code = bitc::FUNC_CODE_INST_GEP;
1680-
if (cast<GEPOperator>(&I)->isInBounds())
1681-
Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
1681+
AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
1682+
auto &GEPInst = cast<GetElementPtrInst>(I);
1683+
Vals.push_back(GEPInst.isInBounds());
1684+
Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
16821685
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
16831686
PushValueAndType(I.getOperand(i), InstID, Vals, VE);
16841687
break;
1688+
}
16851689
case Instruction::ExtractValue: {
16861690
Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
16871691
PushValueAndType(I.getOperand(0), InstID, Vals, VE);
@@ -2287,6 +2291,18 @@ static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
22872291
Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
22882292
llvm_unreachable("Unexpected abbrev ordering!");
22892293
}
2294+
{
2295+
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2296+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2297+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2298+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2299+
Log2_32_Ceil(VE.getTypes().size() + 1)));
2300+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2301+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2302+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
2303+
FUNCTION_INST_GEP_ABBREV)
2304+
llvm_unreachable("Unexpected abbrev ordering!");
2305+
}
22902306

22912307
Stream.ExitBlock();
22922308
}

‎llvm/test/Bitcode/function-encoding-rel-operands.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ define double @test_float_binops(i32 %a) nounwind {
3535

3636

3737
; CHECK: FUNCTION_BLOCK
38-
; skip checking operands of INST_INBOUNDS_GEP since that depends on ordering
38+
; skip checking operands of INST_GEP since that depends on ordering
3939
; between literals and the formal parameters.
40-
; CHECK: INST_INBOUNDS_GEP {{.*}}
40+
; CHECK: INST_GEP {{.*}}
4141
; CHECK: INST_LOAD {{.*}}op0=1 {{.*}}
4242
; CHECK: INST_CMP2 op0=1 {{.*}}
4343
; CHECK: INST_RET {{.*}}op0=1

‎llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,10 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
222222

223223
case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
224224
case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
225-
case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
226-
case bitc::FUNC_CODE_INST_INBOUNDS_GEP: return "INST_INBOUNDS_GEP";
225+
case bitc::FUNC_CODE_INST_GEP_OLD:
226+
return "INST_GEP_OLD";
227+
case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
228+
return "INST_INBOUNDS_GEP_OLD";
227229
case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
228230
case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
229231
case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
@@ -248,6 +250,8 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
248250
case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: return "DEBUG_LOC_AGAIN";
249251
case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
250252
case bitc::FUNC_CODE_DEBUG_LOC: return "DEBUG_LOC";
253+
case bitc::FUNC_CODE_INST_GEP:
254+
return "INST_GEP";
251255
}
252256
case bitc::VALUE_SYMTAB_BLOCK_ID:
253257
switch (CodeID) {

0 commit comments

Comments
 (0)