blob: 405c0edbd3a9678901cce91c23abb2e63435a3a9 [file] [log] [blame]
Chris Lattnerdb9b9982004-01-20 19:50:12 +00001//===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
John Criswell6fbcc262003-10-20 20:19:47 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman23c6d2c2005-04-21 21:48:46 +00007//
John Criswell6fbcc262003-10-20 20:19:47 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnerb5794002002-04-07 22:49:37 +000010// 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.
12//
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.
Chris Lattner00950542001-06-06 20:29:01 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattnerdb9b9982004-01-20 19:50:12 +000020#ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
21#define LLVM_ANALYSIS_SLOTCALCULATOR_H
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner00950542001-06-06 20:29:01 +000023#include <vector>
24#include <map>
Brian Gaeked0fde302003-11-11 22:41:34 +000025
26namespace llvm {
27
Chris Lattner644dc172001-07-14 06:08:51 +000028class Value;
Reid Spencer24ab28f2004-07-04 11:42:49 +000029class Type;
Chris Lattner4c4007b2001-09-07 16:27:05 +000030class Module;
Chris Lattnere7506a32002-03-23 22:51:58 +000031class Function;
Chris Lattner184b2fa2002-04-09 18:35:38 +000032class SymbolTable;
Reid Spencer78d033e2007-01-06 07:24:44 +000033class TypeSymbolTable;
Chris Lattner7851e1b2004-01-14 23:37:43 +000034class ConstantArray;
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner4c4007b2001-09-07 16:27:05 +000036class SlotCalculator {
Chris Lattner00950542001-06-06 20:29:01 +000037 const Module *TheModule;
Chris Lattner8ce75012004-01-14 02:49:34 +000038
Reid Spencer24ab28f2004-07-04 11:42:49 +000039 typedef std::vector<const Type*> TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +000040 typedef std::vector<const Value*> TypePlane;
41 std::vector<TypePlane> Table;
Reid Spencer24ab28f2004-07-04 11:42:49 +000042 TypeList Types;
43 typedef std::map<const Value*, unsigned> NodeMapType;
44 NodeMapType NodeMap;
45
46 typedef std::map<const Type*, unsigned> TypeMapType;
47 TypeMapType TypeMap;
Chris Lattner00950542001-06-06 20:29:01 +000048
Chris Lattner7851e1b2004-01-14 23:37:43 +000049 /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
50 /// of all of the constants strings that need to be emitted.
51 std::vector<const ConstantArray*> ConstantStrings;
52
53 /// ModuleLevel - Used to keep track of which values belong to the module,
54 /// and which values belong to the currently incorporated function.
55 ///
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::vector<unsigned> ModuleLevel;
Reid Spencer24ab28f2004-07-04 11:42:49 +000057 unsigned ModuleTypeLevel;
Chris Lattner00950542001-06-06 20:29:01 +000058
Chris Lattneraf894e92004-01-18 21:03:49 +000059 /// ModuleContainsAllFunctionConstants - This flag is set to true if all
60 /// function constants are incorporated into the module constant table. This
61 /// is only possible if building information for a bytecode file.
62 bool ModuleContainsAllFunctionConstants;
63
64 /// CompactionTable/NodeMap - When function compaction has been performed,
65 /// these entries provide a compacted view of the namespace needed to emit
66 /// instructions in a function body. The 'getSlot()' method automatically
67 /// returns these entries if applicable, or the global entries if not.
68 std::vector<TypePlane> CompactionTable;
Reid Spencer24ab28f2004-07-04 11:42:49 +000069 TypeList CompactionTypes;
70 typedef std::map<const Value*, unsigned> CompactionNodeMapType;
71 CompactionNodeMapType CompactionNodeMap;
72 typedef std::map<const Type*, unsigned> CompactionTypeMapType;
73 CompactionTypeMapType CompactionTypeMap;
Chris Lattneraf894e92004-01-18 21:03:49 +000074
75 SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT
76 void operator=(const SlotCalculator &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000077public:
Chris Lattner3bc5a602006-01-25 23:08:15 +000078 SlotCalculator(const Module *M);
Chris Lattnere7506a32002-03-23 22:51:58 +000079 // Start out in incorp state
Chris Lattner3bc5a602006-01-25 23:08:15 +000080 SlotCalculator(const Function *F);
Misha Brukman23c6d2c2005-04-21 21:48:46 +000081
Chris Lattneraf894e92004-01-18 21:03:49 +000082 /// getSlot - Return the slot number of the specified value in it's type
83 /// plane. This returns < 0 on error!
Chris Lattner7851e1b2004-01-14 23:37:43 +000084 ///
Chris Lattneraf894e92004-01-18 21:03:49 +000085 int getSlot(const Value *V) const;
Reid Spencer24ab28f2004-07-04 11:42:49 +000086 int getSlot(const Type* T) const;
Chris Lattner00950542001-06-06 20:29:01 +000087
Chris Lattneraf894e92004-01-18 21:03:49 +000088 /// getGlobalSlot - Return a slot number from the global table. This can only
89 /// be used when a compaction table is active.
90 unsigned getGlobalSlot(const Value *V) const;
Reid Spencer24ab28f2004-07-04 11:42:49 +000091 unsigned getGlobalSlot(const Type *V) const;
Chris Lattneraf894e92004-01-18 21:03:49 +000092
93 inline unsigned getNumPlanes() const {
94 if (CompactionTable.empty())
95 return Table.size();
96 else
97 return CompactionTable.size();
98 }
Reid Spencer24ab28f2004-07-04 11:42:49 +000099
100 inline unsigned getNumTypes() const {
101 if (CompactionTypes.empty())
102 return Types.size();
103 else
104 return CompactionTypes.size();
105 }
106
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000107 inline unsigned getModuleLevel(unsigned Plane) const {
108 return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000109 }
110
Reid Spencer24ab28f2004-07-04 11:42:49 +0000111 /// Returns the number of types in the type list that are at module level
112 inline unsigned getModuleTypeLevel() const {
113 return ModuleTypeLevel;
114 }
115
Chris Lattnera2b4f932004-01-20 00:54:47 +0000116 TypePlane &getPlane(unsigned Plane);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000117 TypeList& getTypes() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000118 if (!CompactionTypes.empty())
119 return CompactionTypes;
120 return Types;
121 }
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner7851e1b2004-01-14 23:37:43 +0000123 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
124 /// use these two methods to get its data into the SlotCalculator!
125 ///
Chris Lattnerb5794002002-04-07 22:49:37 +0000126 void incorporateFunction(const Function *F);
127 void purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner7851e1b2004-01-14 23:37:43 +0000129 /// string_iterator/string_begin/end - Access the list of module-level
130 /// constant strings that have been incorporated. This is only applicable to
131 /// bytecode files.
132 typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
133 string_iterator string_begin() const { return ConstantStrings.begin(); }
134 string_iterator string_end() const { return ConstantStrings.end(); }
135
Chris Lattneraf894e92004-01-18 21:03:49 +0000136 const std::vector<TypePlane> &getCompactionTable() const {
137 return CompactionTable;
138 }
Chris Lattner7851e1b2004-01-14 23:37:43 +0000139
Reid Spencer24ab28f2004-07-04 11:42:49 +0000140 const TypeList& getCompactionTypes() const { return CompactionTypes; }
141
Reid Spencer07ea1912004-08-26 22:32:00 +0000142 /// @brief Determine if the compaction table (not types) is empty
143 bool CompactionTableIsEmpty() const;
144
Chris Lattneraf894e92004-01-18 21:03:49 +0000145private:
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000146 // getOrCreateSlot - Values can be crammed into here at will... if
147 // they haven't been inserted already, they get inserted, otherwise
148 // they are ignored.
Chris Lattner00950542001-06-06 20:29:01 +0000149 //
Chris Lattner3bc5a602006-01-25 23:08:15 +0000150 int getOrCreateSlot(const Value *V);
151 int getOrCreateSlot(const Type *T);
Chris Lattner00950542001-06-06 20:29:01 +0000152
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000153 // insertValue - Insert a value into the value table... Return the
154 // slot that it occupies, or -1 if the declaration is to be ignored
155 // because of the IgnoreNamedNodes flag.
Chris Lattner00950542001-06-06 20:29:01 +0000156 //
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000157 int insertValue(const Value *D, bool dontIgnore = false);
Chris Lattner3bc5a602006-01-25 23:08:15 +0000158 int insertType(const Type *T, bool dontIgnore = false);
Chris Lattner00950542001-06-06 20:29:01 +0000159
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000160 // doInsertValue - Small helper function to be called only be insertVal.
Chris Lattner3bc5a602006-01-25 23:08:15 +0000161 int doInsertValue(const Value *V);
162 int doInsertType(const Type *T);
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattnerb5794002002-04-07 22:49:37 +0000164 // processModule - Process all of the module level function declarations and
Chris Lattner4c4007b2001-09-07 16:27:05 +0000165 // types that are available.
Chris Lattner00950542001-06-06 20:29:01 +0000166 //
Chris Lattner4c4007b2001-09-07 16:27:05 +0000167 void processModule();
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner4c4007b2001-09-07 16:27:05 +0000169 // processSymbolTable - Insert all of the values in the specified symbol table
170 // into the values table...
Chris Lattner00950542001-06-06 20:29:01 +0000171 //
Reid Spencer78d033e2007-01-06 07:24:44 +0000172 void processTypeSymbolTable(const TypeSymbolTable *ST);
173 void processValueSymbolTable(const SymbolTable *ST);
Chris Lattner4c4007b2001-09-07 16:27:05 +0000174 void processSymbolTableConstants(const SymbolTable *ST);
Chris Lattneraf894e92004-01-18 21:03:49 +0000175
176 void buildCompactionTable(const Function *F);
177 unsigned getOrCreateCompactionTableSlot(const Value *V);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000178 unsigned getOrCreateCompactionTableSlot(const Type *V);
Chris Lattnera2b4f932004-01-20 00:54:47 +0000179 void pruneCompactionTable();
Chris Lattner00950542001-06-06 20:29:01 +0000180};
181
Brian Gaeked0fde302003-11-11 22:41:34 +0000182} // End llvm namespace
183
Chris Lattner00950542001-06-06 20:29:01 +0000184#endif