blob: e88a88f726b3b33435f520702efdf776ac51a916 [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;
Chris Lattner7851e1b2004-01-14 23:37:43 +000033class ConstantArray;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner4c4007b2001-09-07 16:27:05 +000035class SlotCalculator {
Chris Lattner00950542001-06-06 20:29:01 +000036 const Module *TheModule;
Chris Lattner8ce75012004-01-14 02:49:34 +000037
Reid Spencer24ab28f2004-07-04 11:42:49 +000038 typedef std::vector<const Type*> TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +000039 typedef std::vector<const Value*> TypePlane;
40 std::vector<TypePlane> Table;
Reid Spencer24ab28f2004-07-04 11:42:49 +000041 TypeList Types;
42 typedef std::map<const Value*, unsigned> NodeMapType;
43 NodeMapType NodeMap;
44
45 typedef std::map<const Type*, unsigned> TypeMapType;
46 TypeMapType TypeMap;
Chris Lattner00950542001-06-06 20:29:01 +000047
Chris Lattner7851e1b2004-01-14 23:37:43 +000048 /// 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;
51
52 /// ModuleLevel - Used to keep track of which values belong to the module,
53 /// and which values belong to the currently incorporated function.
54 ///
Chris Lattner697954c2002-01-20 22:54:45 +000055 std::vector<unsigned> ModuleLevel;
Reid Spencer24ab28f2004-07-04 11:42:49 +000056 unsigned ModuleTypeLevel;
Chris Lattner00950542001-06-06 20:29:01 +000057
Chris Lattneraf894e92004-01-18 21:03:49 +000058 /// 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;
62
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;
Reid Spencer24ab28f2004-07-04 11:42:49 +000068 TypeList CompactionTypes;
69 typedef std::map<const Value*, unsigned> CompactionNodeMapType;
70 CompactionNodeMapType CompactionNodeMap;
71 typedef std::map<const Type*, unsigned> CompactionTypeMapType;
72 CompactionTypeMapType CompactionTypeMap;
Chris Lattneraf894e92004-01-18 21:03:49 +000073
74 SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT
75 void operator=(const SlotCalculator &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000076public:
Chris Lattner3bc5a602006-01-25 23:08:15 +000077 SlotCalculator(const Module *M);
Chris Lattnere7506a32002-03-23 22:51:58 +000078 // Start out in incorp state
Chris Lattner3bc5a602006-01-25 23:08:15 +000079 SlotCalculator(const Function *F);
Misha Brukman23c6d2c2005-04-21 21:48:46 +000080
Chris Lattneraf894e92004-01-18 21:03:49 +000081 /// getSlot - Return the slot number of the specified value in it's type
82 /// plane. This returns < 0 on error!
Chris Lattner7851e1b2004-01-14 23:37:43 +000083 ///
Chris Lattneraf894e92004-01-18 21:03:49 +000084 int getSlot(const Value *V) const;
Reid Spencer24ab28f2004-07-04 11:42:49 +000085 int getSlot(const Type* T) const;
Chris Lattner00950542001-06-06 20:29:01 +000086
Chris Lattneraf894e92004-01-18 21:03:49 +000087 /// 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;
Reid Spencer24ab28f2004-07-04 11:42:49 +000090 unsigned getGlobalSlot(const Type *V) const;
Chris Lattneraf894e92004-01-18 21:03:49 +000091
92 inline unsigned getNumPlanes() const {
93 if (CompactionTable.empty())
94 return Table.size();
95 else
96 return CompactionTable.size();
97 }
Reid Spencer24ab28f2004-07-04 11:42:49 +000098
99 inline unsigned getNumTypes() const {
100 if (CompactionTypes.empty())
101 return Types.size();
102 else
103 return CompactionTypes.size();
104 }
105
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000106 inline unsigned getModuleLevel(unsigned Plane) const {
107 return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000108 }
109
Reid Spencer24ab28f2004-07-04 11:42:49 +0000110 /// Returns the number of types in the type list that are at module level
111 inline unsigned getModuleTypeLevel() const {
112 return ModuleTypeLevel;
113 }
114
Chris Lattnera2b4f932004-01-20 00:54:47 +0000115 TypePlane &getPlane(unsigned Plane);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000116 TypeList& getTypes() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000117 if (!CompactionTypes.empty())
118 return CompactionTypes;
119 return Types;
120 }
Chris Lattner00950542001-06-06 20:29:01 +0000121
Chris Lattner7851e1b2004-01-14 23:37:43 +0000122 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
123 /// use these two methods to get its data into the SlotCalculator!
124 ///
Chris Lattnerb5794002002-04-07 22:49:37 +0000125 void incorporateFunction(const Function *F);
126 void purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000127
Chris Lattner7851e1b2004-01-14 23:37:43 +0000128 /// string_iterator/string_begin/end - Access the list of module-level
129 /// constant strings that have been incorporated. This is only applicable to
130 /// bytecode files.
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(); }
134
Chris Lattneraf894e92004-01-18 21:03:49 +0000135 const std::vector<TypePlane> &getCompactionTable() const {
136 return CompactionTable;
137 }
Chris Lattner7851e1b2004-01-14 23:37:43 +0000138
Reid Spencer24ab28f2004-07-04 11:42:49 +0000139 const TypeList& getCompactionTypes() const { return CompactionTypes; }
140
Reid Spencer07ea1912004-08-26 22:32:00 +0000141 /// @brief Determine if the compaction table (not types) is empty
142 bool CompactionTableIsEmpty() const;
143
Chris Lattneraf894e92004-01-18 21:03:49 +0000144private:
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000145 // getOrCreateSlot - Values can be crammed into here at will... if
146 // they haven't been inserted already, they get inserted, otherwise
147 // they are ignored.
Chris Lattner00950542001-06-06 20:29:01 +0000148 //
Chris Lattner3bc5a602006-01-25 23:08:15 +0000149 int getOrCreateSlot(const Value *V);
150 int getOrCreateSlot(const Type *T);
Chris Lattner00950542001-06-06 20:29:01 +0000151
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000152 // 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.
Chris Lattner00950542001-06-06 20:29:01 +0000155 //
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000156 int insertValue(const Value *D, bool dontIgnore = false);
Chris Lattner3bc5a602006-01-25 23:08:15 +0000157 int insertType(const Type *T, bool dontIgnore = false);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000159 // doInsertValue - Small helper function to be called only be insertVal.
Chris Lattner3bc5a602006-01-25 23:08:15 +0000160 int doInsertValue(const Value *V);
161 int doInsertType(const Type *T);
Chris Lattner00950542001-06-06 20:29:01 +0000162
Chris Lattnerb5794002002-04-07 22:49:37 +0000163 // processModule - Process all of the module level function declarations and
Chris Lattner4c4007b2001-09-07 16:27:05 +0000164 // types that are available.
Chris Lattner00950542001-06-06 20:29:01 +0000165 //
Chris Lattner4c4007b2001-09-07 16:27:05 +0000166 void processModule();
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner4c4007b2001-09-07 16:27:05 +0000168 // processSymbolTable - Insert all of the values in the specified symbol table
169 // into the values table...
Chris Lattner00950542001-06-06 20:29:01 +0000170 //
Chris Lattner4c4007b2001-09-07 16:27:05 +0000171 void processSymbolTable(const SymbolTable *ST);
172 void processSymbolTableConstants(const SymbolTable *ST);
Chris Lattneraf894e92004-01-18 21:03:49 +0000173
174 void buildCompactionTable(const Function *F);
175 unsigned getOrCreateCompactionTableSlot(const Value *V);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000176 unsigned getOrCreateCompactionTableSlot(const Type *V);
Chris Lattnera2b4f932004-01-20 00:54:47 +0000177 void pruneCompactionTable();
Chris Lattner00950542001-06-06 20:29:01 +0000178};
179
Brian Gaeked0fde302003-11-11 22:41:34 +0000180} // End llvm namespace
181
Chris Lattner00950542001-06-06 20:29:01 +0000182#endif