Skip to content

Commit 0a656d8

Browse files
authored
[Bitcode] Add abbreviations for additional instructions (#146825)
Add abbreviations for icmp/fcmp, store and br, which are the most common instructions that don't have abbreviations yet. This requires increasing the abbreviation size to 5 bits. This gives about 3-5% bitcode size reductions for the clang build.
1 parent 791bb60 commit 0a656d8

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ enum {
141141

142142
// FUNCTION_BLOCK abbrev id's.
143143
FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
144+
FUNCTION_INST_STORE_ABBREV,
144145
FUNCTION_INST_UNOP_ABBREV,
145146
FUNCTION_INST_UNOP_FLAGS_ABBREV,
146147
FUNCTION_INST_BINOP_ABBREV,
@@ -149,8 +150,12 @@ enum {
149150
FUNCTION_INST_CAST_FLAGS_ABBREV,
150151
FUNCTION_INST_RET_VOID_ABBREV,
151152
FUNCTION_INST_RET_VAL_ABBREV,
153+
FUNCTION_INST_BR_UNCOND_ABBREV,
154+
FUNCTION_INST_BR_COND_ABBREV,
152155
FUNCTION_INST_UNREACHABLE_ABBREV,
153156
FUNCTION_INST_GEP_ABBREV,
157+
FUNCTION_INST_CMP_ABBREV,
158+
FUNCTION_INST_CMP_FLAGS_ABBREV,
154159
FUNCTION_DEBUG_RECORD_VALUE_ABBREV,
155160
};
156161

@@ -3197,12 +3202,17 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
31973202
case Instruction::FCmp: {
31983203
// compare returning Int1Ty or vector of Int1Ty
31993204
Code = bitc::FUNC_CODE_INST_CMP2;
3200-
pushValueAndType(I.getOperand(0), InstID, Vals);
3205+
AbbrevToUse = FUNCTION_INST_CMP_ABBREV;
3206+
if (pushValueAndType(I.getOperand(0), InstID, Vals))
3207+
AbbrevToUse = 0;
32013208
pushValue(I.getOperand(1), InstID, Vals);
32023209
Vals.push_back(cast<CmpInst>(I).getPredicate());
32033210
uint64_t Flags = getOptimizationFlags(&I);
3204-
if (Flags != 0)
3211+
if (Flags != 0) {
32053212
Vals.push_back(Flags);
3213+
if (AbbrevToUse)
3214+
AbbrevToUse = FUNCTION_INST_CMP_FLAGS_ABBREV;
3215+
}
32063216
break;
32073217
}
32083218

@@ -3224,11 +3234,13 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
32243234
case Instruction::Br:
32253235
{
32263236
Code = bitc::FUNC_CODE_INST_BR;
3237+
AbbrevToUse = FUNCTION_INST_BR_UNCOND_ABBREV;
32273238
const BranchInst &II = cast<BranchInst>(I);
32283239
Vals.push_back(VE.getValueID(II.getSuccessor(0)));
32293240
if (II.isConditional()) {
32303241
Vals.push_back(VE.getValueID(II.getSuccessor(1)));
32313242
pushValue(II.getCondition(), InstID, Vals);
3243+
AbbrevToUse = FUNCTION_INST_BR_COND_ABBREV;
32323244
}
32333245
}
32343246
break;
@@ -3449,12 +3461,16 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
34493461
}
34503462
break;
34513463
case Instruction::Store:
3452-
if (cast<StoreInst>(I).isAtomic())
3464+
if (cast<StoreInst>(I).isAtomic()) {
34533465
Code = bitc::FUNC_CODE_INST_STOREATOMIC;
3454-
else
3466+
} else {
34553467
Code = bitc::FUNC_CODE_INST_STORE;
3456-
pushValueAndType(I.getOperand(1), InstID, Vals); // ptrty + ptr
3457-
pushValueAndType(I.getOperand(0), InstID, Vals); // valty + val
3468+
AbbrevToUse = FUNCTION_INST_STORE_ABBREV;
3469+
}
3470+
if (pushValueAndType(I.getOperand(1), InstID, Vals)) // ptrty + ptr
3471+
AbbrevToUse = 0;
3472+
if (pushValueAndType(I.getOperand(0), InstID, Vals)) // valty + val
3473+
AbbrevToUse = 0;
34583474
Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign()));
34593475
Vals.push_back(cast<StoreInst>(I).isVolatile());
34603476
if (cast<StoreInst>(I).isAtomic()) {
@@ -3677,7 +3693,7 @@ void ModuleBitcodeWriter::writeFunction(
36773693
// in the VST.
36783694
FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo();
36793695

3680-
Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
3696+
Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 5);
36813697
VE.incorporateFunction(F);
36823698

36833699
SmallVector<unsigned, 64> Vals;
@@ -3952,6 +3968,17 @@ void ModuleBitcodeWriter::writeBlockInfo() {
39523968
FUNCTION_INST_LOAD_ABBREV)
39533969
llvm_unreachable("Unexpected abbrev ordering!");
39543970
}
3971+
{
3972+
auto Abbv = std::make_shared<BitCodeAbbrev>();
3973+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_STORE));
3974+
Abbv->Add(ValAbbrevOp); // op1
3975+
Abbv->Add(ValAbbrevOp); // op0
3976+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // align
3977+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
3978+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
3979+
FUNCTION_INST_STORE_ABBREV)
3980+
llvm_unreachable("Unexpected abbrev ordering!");
3981+
}
39553982
{ // INST_UNOP abbrev for FUNCTION_BLOCK.
39563983
auto Abbv = std::make_shared<BitCodeAbbrev>();
39573984
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP));
@@ -4029,6 +4056,26 @@ void ModuleBitcodeWriter::writeBlockInfo() {
40294056
FUNCTION_INST_RET_VAL_ABBREV)
40304057
llvm_unreachable("Unexpected abbrev ordering!");
40314058
}
4059+
{
4060+
auto Abbv = std::make_shared<BitCodeAbbrev>();
4061+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR));
4062+
// TODO: Use different abbrev for absolute value reference (succ0)?
4063+
Abbv->Add(ValAbbrevOp); // succ0
4064+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4065+
FUNCTION_INST_BR_UNCOND_ABBREV)
4066+
llvm_unreachable("Unexpected abbrev ordering!");
4067+
}
4068+
{
4069+
auto Abbv = std::make_shared<BitCodeAbbrev>();
4070+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR));
4071+
// TODO: Use different abbrev for absolute value references (succ0, succ1)?
4072+
Abbv->Add(ValAbbrevOp); // succ0
4073+
Abbv->Add(ValAbbrevOp); // succ1
4074+
Abbv->Add(ValAbbrevOp); // cond
4075+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4076+
FUNCTION_INST_BR_COND_ABBREV)
4077+
llvm_unreachable("Unexpected abbrev ordering!");
4078+
}
40324079
{ // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
40334080
auto Abbv = std::make_shared<BitCodeAbbrev>();
40344081
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
@@ -4047,6 +4094,27 @@ void ModuleBitcodeWriter::writeBlockInfo() {
40474094
FUNCTION_INST_GEP_ABBREV)
40484095
llvm_unreachable("Unexpected abbrev ordering!");
40494096
}
4097+
{
4098+
auto Abbv = std::make_shared<BitCodeAbbrev>();
4099+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2));
4100+
Abbv->Add(ValAbbrevOp); // op0
4101+
Abbv->Add(ValAbbrevOp); // op1
4102+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred
4103+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4104+
FUNCTION_INST_CMP_ABBREV)
4105+
llvm_unreachable("Unexpected abbrev ordering!");
4106+
}
4107+
{
4108+
auto Abbv = std::make_shared<BitCodeAbbrev>();
4109+
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2));
4110+
Abbv->Add(ValAbbrevOp); // op0
4111+
Abbv->Add(ValAbbrevOp); // op1
4112+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred
4113+
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags
4114+
if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
4115+
FUNCTION_INST_CMP_FLAGS_ABBREV)
4116+
llvm_unreachable("Unexpected abbrev ordering!");
4117+
}
40504118
{
40514119
auto Abbv = std::make_shared<BitCodeAbbrev>();
40524120
Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ define double @test_float_binops(i32 %a) nounwind {
4848
; between literals and the formal parameters.
4949
; CHECK: INST_GEP {{.*}}
5050
; CHECK: INST_LOAD {{.*}}op0=1 {{.*}}
51-
; CHECK: INST_CMP2 op0=1 {{.*}}
51+
; CHECK: INST_CMP2 {{.*}}op0=1 {{.*}}
5252
; CHECK: INST_RET {{.*}}op0=1
5353
define i1 @test_load(i32 %a, {i32, i32}* %ptr) nounwind {
5454
entry:

0 commit comments

Comments
 (0)