OLD | NEW |
(Empty) | |
| 1 //===- AbbrevTrieNode.h - Abbreviation lookup tries ----*- C++ -*-===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This header file defines class AbbrevTrieNode that implement abbreviation |
| 11 // lookup tries. These tries reduce the set of abbreviations that need to |
| 12 // be tested for best fit to a pnacl bitcode record, by sorting abbreviations |
| 13 // on literal constants that may appear in the abbreviations. By doing this, |
| 14 // we can reduce hundreds of possible abbreviations down to a small number |
| 15 // of possibly applicable abbreviations. |
| 16 // |
| 17 // The tries separate abbreviations based on constant size, and constants |
| 18 // that appear in the abbreviations. The trie is used to capture constants |
| 19 // that appear at any index, and use these constants to decide if a trie |
| 20 // node applies to the record. |
| 21 // |
| 22 //===----------------------------------------------------------------------===// |
| 23 |
| 24 #ifndef LLVM_BITCODE_NACL_ABBREV_TRIE_NODE_H |
| 25 #define LLVM_BITCODE_NACL_ABBREV_TRIE_NODE_H |
| 26 |
| 27 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 28 #include <map> |
| 29 #include <set> |
| 30 |
| 31 namespace llvm { |
| 32 |
| 33 // Models the association of an abbreviation index with the |
| 34 // corresponding abbreviation associated with that index. |
| 35 typedef std::pair<size_t, NaClBitCodeAbbrev*> AbbrevIndexPair; |
| 36 |
| 37 // Models a trie of abbreviation matches that can be used to reduce |
| 38 // the number of applicable abbreviations. This is done by moving |
| 39 // abbreviations that require literals to appear in them, to be in |
| 40 // successor nodes. Abbreviations without literals are stored |
| 41 // in this node. |
| 42 class AbbrevTrieNode { |
| 43 // For faster lookup, we could model the successor map as |
| 44 // std::map<std::pair<size_t, uint64_t>, AbbrevTrieNode*>. However, |
| 45 // we split up the pair into a nested map. This allows us to reduce |
| 46 // the size of the domain of the map considerably, as well as |
| 47 // avoiding much value (i.e. std::pair<size_t, uint64_t>) |
| 48 // copying. These modifications result in considerable better time |
| 49 // performance. |
| 50 // |
| 51 // We also do this representation because the trie is sparse, |
| 52 // with respect to where constants can appear. Hence, we don't |
| 53 // built a possible successor for all possible indicies, but only |
| 54 // for those that can contain constants in some abbreviation. |
| 55 typedef std::map<uint64_t, AbbrevTrieNode*> SuccessorValueMap; |
| 56 typedef std::map<size_t, SuccessorValueMap*> SuccessorMap; |
| 57 |
| 58 public: |
| 59 // Models the list of successor labels defined for a node. |
| 60 typedef std::vector< std::pair<size_t, uint64_t> > SuccessorLabels; |
| 61 |
| 62 // Creates an entry node into the trie. |
| 63 AbbrevTrieNode() {} |
| 64 |
| 65 ~AbbrevTrieNode(); |
| 66 |
| 67 // Print out the trie to the given stream, indenting the given |
| 68 // amount. If LocalOnly is true, no successor information is |
| 69 // printed. |
| 70 void Print(raw_ostream &Stream, |
| 71 const std::string &Indent, |
| 72 bool LocalOnly=false) const; |
| 73 |
| 74 // Adds matching constants, defined in Abbreviation, to the trie. |
| 75 // Returns true if any nodes were added to the trie to add the given |
| 76 // abbreviation. Note: This method only creates nodes, Abbreviations |
| 77 // must be aded in a separate pass using method Insert. Note: If |
| 78 // you call NaClBuildAbbrevLookupMap, it will construct the |
| 79 // (complete) abbrevation trie, calling Add and Insert in the |
| 80 // appropriate order. |
| 81 bool Add(NaClBitCodeAbbrev *Abbrev) { |
| 82 return Add(Abbrev, 0, 0); |
| 83 } |
| 84 |
| 85 // Inserts the given abbreviation (pair) in all trie nodes that |
| 86 // might match the given abbreviation. Should not be called until |
| 87 // all trie nodes are built using Add. |
| 88 void Insert(AbbrevIndexPair &AbbrevPair); |
| 89 |
| 90 // Returns the successor trie node matching the given Index/Value pair. |
| 91 AbbrevTrieNode* GetSuccessor(size_t Index, uint64_t Value) const; |
| 92 |
| 93 // Collects the set of successor (edge) labels defined for the node. |
| 94 void GetSuccessorLabels(SuccessorLabels &labels) const; |
| 95 |
| 96 // Returns a trie node (in the trie) that defines all possible |
| 97 // abbreviations that may apply to the given record. |
| 98 const AbbrevTrieNode *MatchRecord(const NaClBitcodeRecordData &Record) const; |
| 99 |
| 100 // Returns the abbreviations associated with the node. |
| 101 const std::set<AbbrevIndexPair> &GetAbbreviations() const { |
| 102 return Abbreviations; |
| 103 } |
| 104 |
| 105 private: |
| 106 // Adds matching constants, defined in Abbreviation, to the trie. |
| 107 // Returns true if any nodes were added to the trie to add the given |
| 108 // abbreviation. Index is the positition (within the abbreviation) |
| 109 // where search should begin for the next available |
| 110 // literal. SkipIndex is the smallest skipped index used to find the |
| 111 // next available literal. |
| 112 bool Add(NaClBitCodeAbbrev *Abbrev, |
| 113 size_t Index, |
| 114 size_t SkipIndex); |
| 115 |
| 116 // The set of possible successor trie nodes defined for this node. |
| 117 SuccessorMap Successors; |
| 118 |
| 119 // The set of abbreviations that apply if one can't match a pnacl |
| 120 // bitcode record against any of the successors. |
| 121 std::set<AbbrevIndexPair> Abbreviations; |
| 122 }; |
| 123 |
| 124 // A map from record sizes, to the corresponding trie one should use |
| 125 // to find abbreviations for records of that size. |
| 126 typedef std::map<size_t, AbbrevTrieNode*> AbbrevLookupSizeMap; |
| 127 |
| 128 // Builds abbreviation lookup trie for abbreviations |
| 129 void NaClBuildAbbrevLookupMap(AbbrevLookupSizeMap &LookupMap, |
| 130 const SmallVectorImpl<NaClBitCodeAbbrev*> &Abbrevs
, |
| 131 size_t InitialIndex = 0); |
| 132 |
| 133 } |
| 134 |
| 135 #endif |
OLD | NEW |