1 //===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class calculates the slots that values will land in. This is useful for
11 // when writing bytecode or assembly out, because you have to know these things.
13 // Specifically, this class calculates the "type plane numbering" that you see
14 // for a function if you strip out all of the symbols in it. For assembly
15 // writing, this is used when a symbol does not have a name. For bytecode
16 // writing, this is always used, and the symbol table is added on later.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
21 #define LLVM_ANALYSIS_SLOTCALCULATOR_H
35 class SlotCalculator {
36 const Module *TheModule;
38 typedef std::vector<const Type*> TypeList;
39 typedef std::vector<const Value*> TypePlane;
40 std::vector<TypePlane> Table;
42 typedef std::map<const Value*, unsigned> NodeMapType;
45 typedef std::map<const Type*, unsigned> TypeMapType;
48 /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
49 /// of all of the constants strings that need to be emitted.
50 std::vector<const ConstantArray*> ConstantStrings;
52 /// ModuleLevel - Used to keep track of which values belong to the module,
53 /// and which values belong to the currently incorporated function.
55 std::vector<unsigned> ModuleLevel;
56 unsigned ModuleTypeLevel;
58 /// ModuleContainsAllFunctionConstants - This flag is set to true if all
59 /// function constants are incorporated into the module constant table. This
60 /// is only possible if building information for a bytecode file.
61 bool ModuleContainsAllFunctionConstants;
63 /// CompactionTable/NodeMap - When function compaction has been performed,
64 /// these entries provide a compacted view of the namespace needed to emit
65 /// instructions in a function body. The 'getSlot()' method automatically
66 /// returns these entries if applicable, or the global entries if not.
67 std::vector<TypePlane> CompactionTable;
68 TypeList CompactionTypes;
69 typedef std::map<const Value*, unsigned> CompactionNodeMapType;
70 CompactionNodeMapType CompactionNodeMap;
71 typedef std::map<const Type*, unsigned> CompactionTypeMapType;
72 CompactionTypeMapType CompactionTypeMap;
74 SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT
75 void operator=(const SlotCalculator &); // DO NOT IMPLEMENT
77 SlotCalculator(const Module *M);
78 // Start out in incorp state
79 SlotCalculator(const Function *F);
81 /// getSlot - Return the slot number of the specified value in it's type
82 /// plane. This returns < 0 on error!
84 int getSlot(const Value *V) const;
85 int getSlot(const Type* T) const;
87 /// getGlobalSlot - Return a slot number from the global table. This can only
88 /// be used when a compaction table is active.
89 unsigned getGlobalSlot(const Value *V) const;
90 unsigned getGlobalSlot(const Type *V) const;
92 inline unsigned getNumPlanes() const {
93 if (CompactionTable.empty())
96 return CompactionTable.size();
99 inline unsigned getNumTypes() const {
100 if (CompactionTypes.empty())
103 return CompactionTypes.size();
106 inline unsigned getModuleLevel(unsigned Plane) const {
107 return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
110 /// Returns the number of types in the type list that are at module level
111 inline unsigned getModuleTypeLevel() const {
112 return ModuleTypeLevel;
115 TypePlane &getPlane(unsigned Plane);
116 TypeList& getTypes() {
117 if (!CompactionTypes.empty())
118 return CompactionTypes;
122 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
123 /// use these two methods to get its data into the SlotCalculator!
125 void incorporateFunction(const Function *F);
126 void purgeFunction();
128 /// string_iterator/string_begin/end - Access the list of module-level
129 /// constant strings that have been incorporated. This is only applicable to
131 typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
132 string_iterator string_begin() const { return ConstantStrings.begin(); }
133 string_iterator string_end() const { return ConstantStrings.end(); }
135 const std::vector<TypePlane> &getCompactionTable() const {
136 return CompactionTable;
139 const TypeList& getCompactionTypes() const { return CompactionTypes; }
141 /// @brief Determine if the compaction table (not types) is empty
142 bool CompactionTableIsEmpty() const;
145 // getOrCreateSlot - Values can be crammed into here at will... if
146 // they haven't been inserted already, they get inserted, otherwise
149 int getOrCreateSlot(const Value *V);
150 int getOrCreateSlot(const Type *T);
152 // insertValue - Insert a value into the value table... Return the
153 // slot that it occupies, or -1 if the declaration is to be ignored
154 // because of the IgnoreNamedNodes flag.
156 int insertValue(const Value *D, bool dontIgnore = false);
157 int insertType(const Type *T, bool dontIgnore = false);
159 // doInsertValue - Small helper function to be called only be insertVal.
160 int doInsertValue(const Value *V);
161 int doInsertType(const Type *T);
163 // processModule - Process all of the module level function declarations and
164 // types that are available.
166 void processModule();
168 // processSymbolTable - Insert all of the values in the specified symbol table
169 // into the values table...
171 void processSymbolTable(const SymbolTable *ST);
172 void processSymbolTableConstants(const SymbolTable *ST);
174 void buildCompactionTable(const Function *F);
175 unsigned getOrCreateCompactionTableSlot(const Value *V);
176 unsigned getOrCreateCompactionTableSlot(const Type *V);
177 void pruneCompactionTable();
180 } // End llvm namespace