OLD | NEW |
(Empty) | |
| 1 //===-- NaClBitcodeBlockDist.cpp ---------------------------------------===// |
| 2 // implements distribution maps for blocks within PNaCl bitcode. |
| 3 // |
| 4 // The LLVM Compiler Infrastructure |
| 5 // |
| 6 // This file is distributed under the University of Illinois Open Source |
| 7 // License. See LICENSE.TXT for details. |
| 8 // |
| 9 //===----------------------------------------------------------------------===// |
| 10 |
| 11 #include "llvm/Bitcode/NaCl/NaClBitcodeBlockDist.h" |
| 12 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" |
| 13 |
| 14 using namespace llvm; |
| 15 |
| 16 /// GetBlockName - Return a symbolic block name if known, otherwise return |
| 17 /// null. |
| 18 static const char *GetBlockName(unsigned BlockID) { |
| 19 // Standard blocks for all bitcode files. |
| 20 if (BlockID < naclbitc::FIRST_APPLICATION_BLOCKID) { |
| 21 if (BlockID == naclbitc::BLOCKINFO_BLOCK_ID) |
| 22 return "BLOCKINFO_BLOCK"; |
| 23 return 0; |
| 24 } |
| 25 |
| 26 switch (BlockID) { |
| 27 default: return 0; |
| 28 case naclbitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; |
| 29 case naclbitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; |
| 30 case naclbitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID"; |
| 31 case naclbitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID"; |
| 32 case naclbitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; |
| 33 case naclbitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; |
| 34 case naclbitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; |
| 35 case naclbitc::METADATA_BLOCK_ID: return "METADATA_BLOCK"; |
| 36 case naclbitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK"; |
| 37 case naclbitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID"; |
| 38 case naclbitc::GLOBALVAR_BLOCK_ID: return "GLOBALVAR_BLOCK"; |
| 39 } |
| 40 } |
| 41 |
| 42 NaClBitcodeBlockDistElement NaClBitcodeBlockDist::DefaultSentinal; |
| 43 |
| 44 NaClBitcodeBlockDistElement::~NaClBitcodeBlockDistElement() {} |
| 45 |
| 46 NaClBitcodeDistElement *NaClBitcodeBlockDistElement:: |
| 47 CreateElement(NaClBitcodeDistValue Value) const { |
| 48 return new NaClBitcodeBlockDistElement(); |
| 49 } |
| 50 |
| 51 double NaClBitcodeBlockDistElement:: |
| 52 GetImportance(NaClBitcodeDistValue Value) const { |
| 53 return static_cast<double>(GetTotalBits()); |
| 54 } |
| 55 |
| 56 const char *NaClBitcodeBlockDistElement::GetTitle() const { |
| 57 return "Block Histogram"; |
| 58 } |
| 59 |
| 60 const char *NaClBitcodeBlockDistElement::GetValueHeader() const { |
| 61 return "Block"; |
| 62 } |
| 63 |
| 64 void NaClBitcodeBlockDistElement::PrintStatsHeader(raw_ostream &Stream) const { |
| 65 Stream << " %File"; |
| 66 NaClBitcodeBitsDistElement::PrintStatsHeader(Stream); |
| 67 } |
| 68 |
| 69 void NaClBitcodeBlockDistElement:: |
| 70 PrintRowStats(raw_ostream &Stream, const NaClBitcodeDist *Distribution) const { |
| 71 const NaClBitcodeBlockDist *BlockDist = |
| 72 cast<NaClBitcodeBlockDist>(Distribution); |
| 73 Stream << format(" %6.2f", |
| 74 (double) GetTotalBits()/BlockDist->GetTotalBits()*100.00); |
| 75 NaClBitcodeBitsDistElement::PrintRowStats(Stream, Distribution); |
| 76 } |
| 77 |
| 78 void NaClBitcodeBlockDistElement:: |
| 79 PrintRowValue(raw_ostream &Stream, NaClBitcodeDistValue Value, |
| 80 const NaClBitcodeDist *Distribution) const { |
| 81 Stream << NaClBitcodeBlockDist::GetName(Value); |
| 82 } |
| 83 |
| 84 NaClBitcodeBlockDist::~NaClBitcodeBlockDist() {} |
| 85 |
| 86 uint64_t NaClBitcodeBlockDist::GetTotalBits() const { |
| 87 uint64_t Total = 0; |
| 88 for (NaClBitcodeDist::const_iterator Iter = begin(), IterEnd = end(); |
| 89 Iter != IterEnd; ++Iter) { |
| 90 const NaClBitcodeBlockDistElement *Element = |
| 91 cast<NaClBitcodeBlockDistElement>(Iter->second); |
| 92 Total += Element->GetTotalBits(); |
| 93 } |
| 94 return Total; |
| 95 } |
| 96 |
| 97 std::string NaClBitcodeBlockDist::GetName(unsigned BlockID) { |
| 98 if (const char *BlockName = GetBlockName(BlockID)) { |
| 99 return BlockName; |
| 100 } |
| 101 |
| 102 std::string Str; |
| 103 raw_string_ostream StrStrm(Str); |
| 104 StrStrm << "UnknownBlock" << BlockID; |
| 105 return StrStrm.str(); |
| 106 } |
OLD | NEW |