llvm.org GIT mirror llvm / e17b658
Implement support for globally associating abbrevs with block IDs, which relieves us from having to emit the abbrevs into each instance of the block. This shrinks kc.bit from 3368K to 3333K, but will be a more significant win once instructions are abbreviated. The VST went from: Block ID #14 (VALUE_SYMTAB): Num Instances: 2345 Total Size: 1.29508e+07b/1.61885e+06B/404713W Average Size: 5522.73b/690.342B/172.585W % of file: 48.0645 Tot/Avg SubBlocks: 0/0 Tot/Avg Abbrevs: 7035/3 Tot/Avg Records: 120924/51.5667 % Abbrev Recs: 100 to: Block ID #14 (VALUE_SYMTAB): Num Instances: 2345 Total Size: 1.26713e+07b/1.58391e+06B/395978W Average Size: 5403.53b/675.442B/168.86W % of file: 47.5198 Tot/Avg SubBlocks: 0/0 Tot/Avg Abbrevs: 0/0 Tot/Avg Records: 120924/51.5667 % Abbrev Recs: 100 because we didn't emit the same 3 abbrevs 2345 times :) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36767 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
5 changed file(s) with 266 addition(s) and 66 deletion(s). Raw diff Collapse all Expand all
6565 /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
6666 /// blocks.
6767 enum BlockInfoCodes {
68 BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#]
69 BLOCKINFO_CODE_ABBREV = 2 // ABBREV: [standard abbrev encoding]
68 BLOCKINFO_CODE_SETBID = 1 // SETBID: [blockid#]
69 // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
70 // block, instead of the BlockInfo block.
7071 // BLOCKNAME: give string name to block, if desired.
7172 };
7273
4747 /// BlockScope - This tracks the codesize of parent blocks.
4848 SmallVector BlockScope;
4949
50 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
51 /// These describe abbreviations that all blocks of the specified ID inherit.
52 struct BlockInfo {
53 unsigned BlockID;
54 std::vector Abbrevs;
55 };
56 std::vector BlockInfoRecords;
57
5058 /// FirstChar - This remembers the first byte of the stream.
5159 const unsigned char *FirstChar;
5260 public:
8088 std::vector &Abbrevs = BlockScope[S].PrevAbbrevs;
8189 for (unsigned i = 0, e = Abbrevs.size(); i != e; ++i)
8290 Abbrevs[i]->dropRef();
91 }
92
93 // Free the BlockInfoRecords.
94 while (!BlockInfoRecords.empty()) {
95 BlockInfo &Info = BlockInfoRecords.back();
96 // Free blockinfo abbrev info.
97 for (unsigned i = 0, e = Info.Abbrevs.size(); i != e; ++i)
98 Info.Abbrevs[i]->dropRef();
99 BlockInfoRecords.pop_back();
83100 }
84101 }
85102
205222 // Block Manipulation
206223 //===--------------------------------------------------------------------===//
207224
225 private:
226 /// getBlockInfo - If there is block info for the specified ID, return it,
227 /// otherwise return null.
228 BlockInfo *getBlockInfo(unsigned BlockID) {
229 // Common case, the most recent entry matches BlockID.
230 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
231 return &BlockInfoRecords.back();
232
233 for (unsigned i = 0, e = BlockInfoRecords.size(); i != e; ++i)
234 if (BlockInfoRecords[i].BlockID == BlockID)
235 return &BlockInfoRecords[i];
236 return 0;
237 }
238 public:
239
240
208241 // Block header:
209242 // [ENTER_SUBBLOCK, blockid, newcodelen, , blocklen]
210243
235268
236269 /// EnterSubBlock - Having read the ENTER_SUBBLOCK abbrevid, read and enter
237270 /// the block, returning the BlockID of the block we just entered.
238 bool EnterSubBlock(unsigned *NumWordsP = 0) {
271 bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = 0) {
272 // Save the current block's state on BlockScope.
239273 BlockScope.push_back(Block(CurCodeSize));
240274 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
275
276 // Add the abbrevs specific to this block to the CurAbbrevs list.
277 if (BlockInfo *Info = getBlockInfo(BlockID)) {
278 for (unsigned i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
279 CurAbbrevs.push_back(Info->Abbrevs[i]);
280 CurAbbrevs.back()->addRef();
281 }
282 }
241283
242284 // Get the codesize of this block.
243285 CurCodeSize = ReadVBR(bitc::CodeLenWidth);
351393 }
352394 CurAbbrevs.push_back(Abbv);
353395 }
396
397 //===--------------------------------------------------------------------===//
398 // BlockInfo Block Reading
399 //===--------------------------------------------------------------------===//
400
401 private:
402 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
403 if (BlockInfo *BI = getBlockInfo(BlockID))
404 return *BI;
405
406 // Otherwise, add a new record.
407 BlockInfoRecords.push_back(BlockInfo());
408 BlockInfoRecords.back().BlockID = BlockID;
409 return BlockInfoRecords.back();
410 }
411
412 public:
413
414 bool ReadBlockInfoBlock() {
415 if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
416
417 SmallVector Record;
418 BlockInfo *CurBlockInfo = 0;
419
420 // Read all the records for this module.
421 while (1) {
422 unsigned Code = ReadCode();
423 if (Code == bitc::END_BLOCK)
424 return ReadBlockEnd();
425 if (Code == bitc::ENTER_SUBBLOCK) {
426 ReadSubBlockID();
427 if (SkipBlock()) return true;
428 continue;
429 }
430
431 // Read abbrev records, associate them with CurBID.
432 if (Code == bitc::DEFINE_ABBREV) {
433 if (!CurBlockInfo) return true;
434 ReadAbbrevRecord();
435
436 // ReadAbbrevRecord installs the abbrev in CurAbbrevs. Move it to the
437 // appropriate BlockInfo.
438 BitCodeAbbrev *Abbv = CurAbbrevs.back();
439 CurAbbrevs.pop_back();
440 CurBlockInfo->Abbrevs.push_back(Abbv);
441 continue;
442 }
443
444 // Read a record.
445 switch (ReadRecord(Code, Record)) {
446 default: break; // Default behavior, ignore unknown content.
447 case bitc::BLOCKINFO_CODE_SETBID:
448 if (Record.size() < 1) return true;
449 CurBlockInfo = &getOrCreateBlockInfo(Record[0]);
450 break;
451 }
452 }
453 }
354454 };
355455
356456 } // End llvm namespace
2828 /// CurValue - The current value. Only bits < CurBit are valid.
2929 uint32_t CurValue;
3030
31 // CurCodeSize - This is the declared size of code values used for the current
32 // block, in bits.
31 /// CurCodeSize - This is the declared size of code values used for the
32 /// current block, in bits.
3333 unsigned CurCodeSize;
3434
35 /// BlockInfoCurBID - When emitting a BLOCKINFO_BLOCK, this is the currently
36 /// selected BLOCK ID.
37 unsigned BlockInfoCurBID;
38
3539 /// CurAbbrevs - Abbrevs installed at in this block.
3640 std::vector CurAbbrevs;
3741
4549 /// BlockScope - This tracks the current blocks that we have entered.
4650 std::vector BlockScope;
4751
52 /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
53 /// These describe abbreviations that all blocks of the specified ID inherit.
54 struct BlockInfo {
55 unsigned BlockID;
56 std::vector Abbrevs;
57 };
58 std::vector BlockInfoRecords;
59
4860 public:
4961 BitstreamWriter(std::vector &O)
5062 : Out(O), CurBit(0), CurValue(0), CurCodeSize(2) {}
5264 ~BitstreamWriter() {
5365 assert(CurBit == 0 && "Unflused data remaining");
5466 assert(BlockScope.empty() && CurAbbrevs.empty() && "Block imbalance");
67
68 // Free the BlockInfoRecords.
69 while (!BlockInfoRecords.empty()) {
70 BlockInfo &Info = BlockInfoRecords.back();
71 // Free blockinfo abbrev info.
72 for (unsigned i = 0, e = Info.Abbrevs.size(); i != e; ++i)
73 Info.Abbrevs[i]->dropRef();
74 BlockInfoRecords.pop_back();
75 }
5576 }
5677 //===--------------------------------------------------------------------===//
5778 // Basic Primitives for emitting bits to the stream.
138159 // Block Manipulation
139160 //===--------------------------------------------------------------------===//
140161
162 /// getBlockInfo - If there is block info for the specified ID, return it,
163 /// otherwise return null.
164 BlockInfo *getBlockInfo(unsigned BlockID) {
165 // Common case, the most recent entry matches BlockID.
166 if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
167 return &BlockInfoRecords.back();
168
169 for (unsigned i = 0, e = BlockInfoRecords.size(); i != e; ++i)
170 if (BlockInfoRecords[i].BlockID == BlockID)
171 return &BlockInfoRecords[i];
172 return 0;
173 }
174
141175 void EnterSubblock(unsigned BlockID, unsigned CodeLen) {
142176 // Block header:
143177 // [ENTER_SUBBLOCK, blockid, newcodelen, , blocklen]
145179 EmitVBR(BlockID, bitc::BlockIDWidth);
146180 EmitVBR(CodeLen, bitc::CodeLenWidth);
147181 FlushToWord();
148 BlockScope.push_back(Block(CurCodeSize, Out.size()/4));
149 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
182
183 unsigned BlockSizeWordLoc = Out.size();
184 unsigned OldCodeSize = CurCodeSize;
150185
151186 // Emit a placeholder, which will be replaced when the block is popped.
152187 Emit(0, bitc::BlockSizeWidth);
153188
154189 CurCodeSize = CodeLen;
190
191 // Push the outer block's abbrev set onto the stack, start out with an
192 // empty abbrev set.
193 BlockScope.push_back(Block(OldCodeSize, BlockSizeWordLoc/4));
194 BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
195
196 // If there is a blockinfo for this BlockID, add all the predefined abbrevs
197 // to the abbrev list.
198 if (BlockInfo *Info = getBlockInfo(BlockID)) {
199 for (unsigned i = 0, e = Info->Abbrevs.size(); i != e; ++i) {
200 CurAbbrevs.push_back(Info->Abbrevs[i]);
201 Info->Abbrevs[i]->addRef();
202 }
203 }
155204 }
156205
157206 void ExitBlock() {
260309 EmitVBR64(Vals[i], 6);
261310 }
262311 }
263
312
264313 //===--------------------------------------------------------------------===//
265314 // Abbrev Emission
266315 //===--------------------------------------------------------------------===//
267316
268 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
269 /// method takes ownership of the specified abbrev.
270 unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
271 // Emit the abbreviation as a record.
317 private:
318 // Emit the abbreviation as a DEFINE_ABBREV record.
319 void EncodeAbbrev(BitCodeAbbrev *Abbv) {
272320 EmitCode(bitc::DEFINE_ABBREV);
273321 EmitVBR(Abbv->getNumOperandInfos(), 5);
274322 for (unsigned i = 0, e = Abbv->getNumOperandInfos(); i != e; ++i) {
282330 EmitVBR64(Op.getEncodingData(), 5);
283331 }
284332 }
285
333 }
334 public:
335
336 /// EmitAbbrev - This emits an abbreviation to the stream. Note that this
337 /// method takes ownership of the specified abbrev.
338 unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
339 // Emit the abbreviation as a record.
340 EncodeAbbrev(Abbv);
286341 CurAbbrevs.push_back(Abbv);
287342 return CurAbbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
288343 }
344
345 //===--------------------------------------------------------------------===//
346 // BlockInfo Block Emission
347 //===--------------------------------------------------------------------===//
348
349 /// EnterBlockInfoBlock - Start emitting the BLOCKINFO_BLOCK.
350 void EnterBlockInfoBlock(unsigned CodeWidth) {
351 EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, CodeWidth);
352 BlockInfoCurBID = -1U;
353 }
354 private:
355 /// SwitchToBlockID - If we aren't already talking about the specified block
356 /// ID, emit a BLOCKINFO_CODE_SETBID record.
357 void SwitchToBlockID(unsigned BlockID) {
358 if (BlockInfoCurBID == BlockID) return;
359 SmallVector V;
360 V.push_back(BlockID);
361 EmitRecord(bitc::BLOCKINFO_CODE_SETBID, V);
362 BlockInfoCurBID = BlockID;
363 }
364
365 BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
366 if (BlockInfo *BI = getBlockInfo(BlockID))
367 return *BI;
368
369 // Otherwise, add a new record.
370 BlockInfoRecords.push_back(BlockInfo());
371 BlockInfoRecords.back().BlockID = BlockID;
372 return BlockInfoRecords.back();
373 }
374
375 public:
376
377 /// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
378 /// BlockID.
379 unsigned EmitBlockInfoAbbrev(unsigned BlockID, BitCodeAbbrev *Abbv) {
380 SwitchToBlockID(BlockID);
381 EncodeAbbrev(Abbv);
382
383 // Add the abbrev to the specified block record.
384 BlockInfo &Info = getOrCreateBlockInfo(BlockID);
385 Info.Abbrevs.push_back(Abbv);
386
387 return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
388 }
289389 };
290390
291391
181181 //===----------------------------------------------------------------------===//
182182
183183 bool BitcodeReader::ParseParamAttrBlock() {
184 if (Stream.EnterSubBlock())
184 if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
185185 return Error("Malformed block record");
186186
187187 if (!ParamAttrs.empty())
238238
239239
240240 bool BitcodeReader::ParseTypeTable() {
241 if (Stream.EnterSubBlock())
241 if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID))
242242 return Error("Malformed block record");
243243
244244 if (!TypeList.empty())
377377
378378
379379 bool BitcodeReader::ParseTypeSymbolTable() {
380 if (Stream.EnterSubBlock())
380 if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID))
381381 return Error("Malformed block record");
382382
383383 SmallVector Record;
425425 }
426426
427427 bool BitcodeReader::ParseValueSymbolTable() {
428 if (Stream.EnterSubBlock())
428 if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
429429 return Error("Malformed block record");
430430
431431 SmallVector Record;
535535
536536
537537 bool BitcodeReader::ParseConstants() {
538 if (Stream.EnterSubBlock())
538 if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
539539 return Error("Malformed block record");
540540
541541 SmallVector Record;
767767 if (TheModule)
768768 return Error("Multiple MODULE_BLOCKs in same stream");
769769
770 if (Stream.EnterSubBlock())
770 if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
771771 return Error("Malformed block record");
772772
773773 // Otherwise, create the module.
10211021 unsigned BlockID = Stream.ReadSubBlockID();
10221022
10231023 // We only know the MODULE subblock ID.
1024 if (BlockID == bitc::MODULE_BLOCK_ID) {
1024 switch (BlockID) {
1025 case bitc::BLOCKINFO_BLOCK_ID:
1026 if (Stream.ReadBlockInfoBlock())
1027 return Error("Malformed BlockInfoBlock");
1028 break;
1029 case bitc::MODULE_BLOCK_ID:
10251030 if (ParseModule(Buffer->getBufferIdentifier()))
10261031 return true;
1027 } else if (Stream.SkipBlock()) {
1028 return Error("Malformed block record");
1032 break;
1033 default:
1034 if (Stream.SkipBlock())
1035 return Error("Malformed block record");
1036 break;
10291037 }
10301038 }
10311039
10711079
10721080 /// ParseFunctionBody - Lazily parse the specified function body block.
10731081 bool BitcodeReader::ParseFunctionBody(Function *F) {
1074 if (Stream.EnterSubBlock())
1082 if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
10751083 return Error("Malformed block record");
10761084
10771085 unsigned ModuleValueListSize = ValueList.size();
713713 if (VST.empty()) return;
714714 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 3);
715715
716 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
717 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
718 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
722 if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_8_ABBREV)
723 assert(0 && "Unexpected abbrev ordering!");
724 }
725
726 { // 7-bit fixed width VST_ENTRY strings.
727 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
728 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
729 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
730 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
732 if (Stream.EmitAbbrev(Abbv) != VST_ENTRY_7_ABBREV)
733 assert(0 && "Unexpected abbrev ordering!");
734 }
735 { // 7-bit fixed width VST_BBENTRY strings.
736 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
737 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
738 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
739 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
740 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
741 if (Stream.EmitAbbrev(Abbv) != VST_BBENTRY_7_ABBREV)
742 assert(0 && "Unexpected abbrev ordering!");
743 }
744
745
746716 // FIXME: Set up the abbrev, we know how many values there are!
747717 // FIXME: We know if the type names can use 7-bit ascii.
748718 SmallVector NameVals;
908878 // We only want to emit block info records for blocks that have multiple
909879 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
910880 // blocks can defined their abbrevs inline.
911 Stream.EnterSubblock(bitc::BLOCKINFO_BLOCK_ID, 2);
912
913 #if 0
914 // Configure TYPE_SYMTAB_BLOCK's.
915
916 // Add an abbrev for VST_ENTRY where the characters each fit in 7 bits.
917 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
918 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
919 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8); // Value ID
920 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
921
922 xxx = Stream.EmitAbbrev(Abbv);
923 #endif
881 Stream.EnterBlockInfoBlock(2);
882
883 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
884 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
885 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
886 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
887 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
888 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
889 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
890 Abbv) != VST_ENTRY_8_ABBREV)
891 assert(0 && "Unexpected abbrev ordering!");
892 }
893
894 { // 7-bit fixed width VST_ENTRY strings.
895 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
896 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
897 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
898 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
899 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
900 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
901 Abbv) != VST_ENTRY_7_ABBREV)
902 assert(0 && "Unexpected abbrev ordering!");
903 }
904 { // 7-bit fixed width VST_BBENTRY strings.
905 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
906 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
907 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
908 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
909 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
910 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
911 Abbv) != VST_BBENTRY_7_ABBREV)
912 assert(0 && "Unexpected abbrev ordering!");
913 }
914
924915 Stream.ExitBlock();
925916 }
926917