blob: fdf7174b8551becb917eba948de3a3dadde6c6ac [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
John Criswellb576c942003-10-20 19:43:21 +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 Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000010// This file implements a useful analysis step to figure out what numbered slots
11// values in a program will land in (keeping track of per plane information).
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000013// This is used when writing a file to disk, either in bytecode or assembly.
Chris Lattner00950542001-06-06 20:29:01 +000014//
15//===----------------------------------------------------------------------===//
16
Reid Spencer24ab28f2004-07-04 11:42:49 +000017#include "SlotCalculator.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000018#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/DerivedTypes.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000020#include "llvm/Function.h"
Chris Lattner3bc5a602006-01-25 23:08:15 +000021#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000022#include "llvm/Instructions.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000023#include "llvm/Module.h"
Chris Lattner9a297902001-09-07 16:31:52 +000024#include "llvm/SymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000025#include "llvm/TypeSymbolTable.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000026#include "llvm/Type.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Analysis/ConstantsScanner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/PostOrderIterator.h"
29#include "llvm/ADT/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000030#include <algorithm>
Reid Spencer24ab28f2004-07-04 11:42:49 +000031#include <functional>
Chris Lattner31f84992003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner9a297902001-09-07 16:31:52 +000034#if 0
Bill Wendling68fe61d2006-11-29 00:19:40 +000035#include "llvm/Support/Streams.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000036#define SC_DEBUG(X) cerr << X
Chris Lattner9a297902001-09-07 16:31:52 +000037#else
38#define SC_DEBUG(X)
39#endif
Chris Lattner00950542001-06-06 20:29:01 +000040
Reid Spencer798ff642004-05-26 07:37:11 +000041SlotCalculator::SlotCalculator(const Module *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000042 ModuleContainsAllFunctionConstants = false;
Reid Spencer24ab28f2004-07-04 11:42:49 +000043 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +000044 TheModule = M;
45
46 // Preload table... Make sure that all of the primitive types are in the table
47 // and that their Primitive ID is equal to their slot #
48 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000049 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000050 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000051 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer24ab28f2004-07-04 11:42:49 +000052 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000053 }
54
55 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000056 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000057}
58
Reid Spencer798ff642004-05-26 07:37:11 +000059SlotCalculator::SlotCalculator(const Function *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000060 ModuleContainsAllFunctionConstants = false;
Chris Lattner00950542001-06-06 20:29:01 +000061 TheModule = M ? M->getParent() : 0;
62
63 // Preload table... Make sure that all of the primitive types are in the table
64 // and that their Primitive ID is equal to their slot #
65 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000066 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000067 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000068 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer24ab28f2004-07-04 11:42:49 +000069 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000070 }
71
72 if (TheModule == 0) return; // Empty table...
73
Chris Lattner9a297902001-09-07 16:31:52 +000074 processModule(); // Process module level stuff
Chris Lattner68e3dbc2004-01-20 00:57:32 +000075 incorporateFunction(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000076}
77
Chris Lattner614cdcd2004-01-18 21:07:07 +000078unsigned SlotCalculator::getGlobalSlot(const Value *V) const {
79 assert(!CompactionTable.empty() &&
80 "This method can only be used when compaction is enabled!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000081 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner68e3dbc2004-01-20 00:57:32 +000082 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000083 return I->second;
84}
85
Reid Spencer24ab28f2004-07-04 11:42:49 +000086unsigned SlotCalculator::getGlobalSlot(const Type* T) const {
87 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
88 assert(I != TypeMap.end() && "Didn't find global slot entry!");
89 return I->second;
90}
91
Chris Lattner68e3dbc2004-01-20 00:57:32 +000092SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +000093 if (CompactionTable.empty()) { // No compaction table active?
94 // fall out
95 } else if (!CompactionTable[Plane].empty()) { // Compaction table active.
96 assert(Plane < CompactionTable.size());
97 return CompactionTable[Plane];
98 } else {
99 // Final case: compaction table active, but this plane is not
100 // compactified. If the type plane is compactified, unmap back to the
101 // global type plane corresponding to "Plane".
Reid Spencer24ab28f2004-07-04 11:42:49 +0000102 if (!CompactionTypes.empty()) {
103 const Type *Ty = CompactionTypes[Plane];
104 TypeMapType::iterator It = TypeMap.find(Ty);
105 assert(It != TypeMap.end() && "Type not in global constant map?");
106 Plane = It->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000107 }
108 }
109
110 // Okay we are just returning an entry out of the main Table. Make sure the
111 // plane exists and return it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000112 if (Plane >= Table.size())
113 Table.resize(Plane+1);
114 return Table[Plane];
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000115}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000116
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000117// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +0000118// types that are available.
119//
120void SlotCalculator::processModule() {
121 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000122
Chris Lattner3413d152003-03-19 20:57:22 +0000123 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +0000124 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000125 for (Module::const_global_iterator I = TheModule->global_begin(),
126 E = TheModule->global_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000127 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000128
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129 // Scavenge the types out of the functions, then add the functions themselves
130 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000131 //
Chris Lattner3413d152003-03-19 20:57:22 +0000132 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
133 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000134 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000135
Chris Lattner3413d152003-03-19 20:57:22 +0000136 // Add all of the module level constants used as initializers
137 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000138 for (Module::const_global_iterator I = TheModule->global_begin(),
139 E = TheModule->global_end(); I != E; ++I)
Chris Lattner3413d152003-03-19 20:57:22 +0000140 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000141 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000142
Chris Lattnerdcea6302004-01-14 23:34:39 +0000143 // Now that all global constants have been added, rearrange constant planes
144 // that contain constant strings so that the strings occur at the start of the
145 // plane, not somewhere in the middle.
146 //
Reid Spencer798ff642004-05-26 07:37:11 +0000147 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
148 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
Reid Spencer88cfda22006-12-31 05:44:24 +0000149 if (AT->getElementType() == Type::Int8Ty) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000150 TypePlane &Plane = Table[plane];
151 unsigned FirstNonStringID = 0;
152 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000153 if (isa<ConstantAggregateZero>(Plane[i]) ||
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000154 (isa<ConstantArray>(Plane[i]) &&
Chris Lattner236ca442004-10-24 04:27:59 +0000155 cast<ConstantArray>(Plane[i])->isString())) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000156 // Check to see if we have to shuffle this string around. If not,
157 // don't do anything.
158 if (i != FirstNonStringID) {
159 // Swap the plane entries....
160 std::swap(Plane[i], Plane[FirstNonStringID]);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000161
Reid Spencer24ab28f2004-07-04 11:42:49 +0000162 // Keep the NodeMap up to date.
163 NodeMap[Plane[i]] = i;
164 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
165 }
166 ++FirstNonStringID;
167 }
Reid Spencer798ff642004-05-26 07:37:11 +0000168 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000169 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000170
171 // Scan all of the functions for their constants, which allows us to emit
172 // more compact modules. This is optional, and is just used to compactify
Reid Spencere8404342004-07-18 00:18:30 +0000173 // the constants used by different functions together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000174 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000175 // This functionality tends to produce smaller bytecode files. This should
176 // not be used in the future by clients that want to, for example, build and
177 // emit functions on the fly. For now, however, it is unconditionally
Reid Spencere8404342004-07-18 00:18:30 +0000178 // enabled.
Reid Spencer798ff642004-05-26 07:37:11 +0000179 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000180
Reid Spencer798ff642004-05-26 07:37:11 +0000181 SC_DEBUG("Inserting function constants:\n");
182 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
183 F != E; ++F) {
Chris Lattner3bc5a602006-01-25 23:08:15 +0000184 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
185 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
186 OI != E; ++OI) {
187 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
188 isa<InlineAsm>(*OI))
189 getOrCreateSlot(*OI);
190 }
Reid Spencer798ff642004-05-26 07:37:11 +0000191 getOrCreateSlot(I->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000192 }
Reid Spencer78d033e2007-01-06 07:24:44 +0000193 processSymbolTableConstants(&F->getValueSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000194 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000195
Chris Lattner70cc3392001-09-10 07:58:01 +0000196 // Insert constants that are named at module level into the slot pool so that
197 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000198 SC_DEBUG("Inserting SymbolTable values:\n");
Reid Spencer78d033e2007-01-06 07:24:44 +0000199 processTypeSymbolTable(&TheModule->getTypeSymbolTable());
200 processValueSymbolTable(&TheModule->getValueSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000201
Chris Lattnera14b0d42004-01-10 23:46:13 +0000202 // Now that we have collected together all of the information relevant to the
203 // module, compactify the type table if it is particularly big and outputting
204 // a bytecode file. The basic problem we run into is that some programs have
205 // a large number of types, which causes the type field to overflow its size,
206 // which causes instructions to explode in size (particularly call
207 // instructions). To avoid this behavior, we "sort" the type table so that
208 // all non-value types are pushed to the end of the type table, giving nice
209 // low numbers to the types that can be used by instructions, thus reducing
210 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000211 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000212 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000213 for (unsigned i = 0, e = Types.size(); i != e; ++i)
214 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000215 // Check to see if we have to shuffle this type around. If not, don't
216 // do anything.
217 if (i != FirstNonValueTypeID) {
218 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000219 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000220
Reid Spencer24ab28f2004-07-04 11:42:49 +0000221 // Keep the TypeMap up to date.
222 TypeMap[Types[i]] = i;
223 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000224
225 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000226 if (Table.size() > FirstNonValueTypeID) {
227 if (Table.size() <= i) Table.resize(i+1);
228 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000229 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000230 }
231 ++FirstNonValueTypeID;
232 }
233 }
234
Chris Lattner9a297902001-09-07 16:31:52 +0000235 SC_DEBUG("end processModule!\n");
236}
237
Reid Spencer78d033e2007-01-06 07:24:44 +0000238// processTypeSymbolTable - Insert all of the type sin the specified symbol
239// table.
240void SlotCalculator::processTypeSymbolTable(const TypeSymbolTable *ST) {
241 for (TypeSymbolTable::const_iterator TI = ST->begin(), TE = ST->end();
242 TI != TE; ++TI )
243 getOrCreateSlot(TI->second);
244}
245
Chris Lattner9a297902001-09-07 16:31:52 +0000246// processSymbolTable - Insert all of the values in the specified symbol table
247// into the values table...
248//
Reid Spencer78d033e2007-01-06 07:24:44 +0000249void SlotCalculator::processValueSymbolTable(const SymbolTable *ST) {
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000250 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000251 PE = ST->plane_end(); PI != PE; ++PI)
252 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000253 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000254 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000255}
256
257void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000258 // Now do the constant values in all planes
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000259 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000260 PE = ST->plane_end(); PI != PE; ++PI)
261 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000262 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000263 if (isa<Constant>(VI->second) &&
264 !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000265 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000266}
267
268
Chris Lattnerce439b52003-10-20 19:10:06 +0000269void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000270 assert((ModuleLevel.size() == 0 ||
271 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000272
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000273 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000274
Chris Lattner614cdcd2004-01-18 21:07:07 +0000275 // If we emitted all of the function constants, build a compaction table.
Chris Lattner101cefa2006-06-07 22:20:03 +0000276 if (ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000277 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000278
279 // Update the ModuleLevel entries to be accurate.
280 ModuleLevel.resize(getNumPlanes());
281 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
282 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000283 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000284
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 // Iterate over function arguments, adding them to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000286 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000287 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000288
Chris Lattner3bc5a602006-01-25 23:08:15 +0000289 if (!ModuleContainsAllFunctionConstants) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000290 // Iterate over all of the instructions in the function, looking for
291 // constant values that are referenced. Add these to the value pools
292 // before any nonconstant values. This will be turned into the constant
293 // pool for the bytecode writer.
294 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000295
Chris Lattner614cdcd2004-01-18 21:07:07 +0000296 // Emit all of the constants that are being used by the instructions in
297 // the function...
Chris Lattner3bc5a602006-01-25 23:08:15 +0000298 for (constant_iterator CI = constant_begin(F), CE = constant_end(F);
299 CI != CE; ++CI)
300 getOrCreateSlot(*CI);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000301
Chris Lattner9a297902001-09-07 16:31:52 +0000302 // If there is a symbol table, it is possible that the user has names for
303 // constants that are not being used. In this case, we will have problems
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000304 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000305 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000306 // constants now.
307 //
Reid Spencer78d033e2007-01-06 07:24:44 +0000308 processSymbolTableConstants(&F->getValueSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
310
Chris Lattner9a297902001-09-07 16:31:52 +0000311 SC_DEBUG("Inserting Instructions:\n");
312
313 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000314 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
315 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000316 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
317 getOrCreateSlot(I);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000318 }
Chris Lattner9a297902001-09-07 16:31:52 +0000319 }
320
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000321 // If we are building a compaction table, prune out planes that do not benefit
322 // from being compactified.
323 if (!CompactionTable.empty())
324 pruneCompactionTable();
325
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000326 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000327}
328
Chris Lattnerb5794002002-04-07 22:49:37 +0000329void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000330 assert((ModuleLevel.size() != 0 ||
331 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000332 unsigned NumModuleTypes = ModuleLevel.size();
333
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000335
Chris Lattner614cdcd2004-01-18 21:07:07 +0000336 // First, free the compaction map if used.
337 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000338 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000339
340 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000341 for (unsigned i = 0; i != NumModuleTypes; ++i) {
342 // Size of plane before function came
343 unsigned ModuleLev = getModuleLevel(i);
344 assert(int(ModuleLev) >= 0 && "BAD!");
345
346 TypePlane &Plane = getPlane(i);
347
348 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
349 while (Plane.size() != ModuleLev) {
350 assert(!isa<GlobalValue>(Plane.back()) &&
351 "Functions cannot define globals!");
352 NodeMap.erase(Plane.back()); // Erase from nodemap
353 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000354 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000355 }
Chris Lattner00950542001-06-06 20:29:01 +0000356
357 // We don't need this state anymore, free it up.
358 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000359 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000360
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000361 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000362 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000363 if (!CompactionTable.empty()) {
364 CompactionTable.clear();
365 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000366 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000367 TypePlane &Plane = Table.back();
368 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
369 << Plane.size() << "\n");
370 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000371 assert(!isa<GlobalValue>(Plane.back()) &&
372 "Functions cannot define globals!");
373 NodeMap.erase(Plane.back()); // Erase from nodemap
374 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000375 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000376
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000377 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000378 }
Chris Lattner00950542001-06-06 20:29:01 +0000379 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000380
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000381 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000382}
383
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000384static inline bool hasNullValue(const Type *Ty) {
385 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000386}
387
388/// getOrCreateCompactionTableSlot - This method is used to build up the initial
389/// approximation of the compaction table.
390unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
391 std::map<const Value*, unsigned>::iterator I =
392 CompactionNodeMap.lower_bound(V);
393 if (I != CompactionNodeMap.end() && I->first == V)
394 return I->second; // Already exists?
395
396 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000397 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000398 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000399 Ty = getOrCreateCompactionTableSlot(V->getType());
400 else // If the type plane was decompactified, use the global plane ID
401 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000402 if (CompactionTable.size() <= Ty)
403 CompactionTable.resize(Ty+1);
404
Chris Lattner614cdcd2004-01-18 21:07:07 +0000405 TypePlane &TyPlane = CompactionTable[Ty];
406
407 // Make sure to insert the null entry if the thing we are inserting is not a
408 // null constant.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000409 if (TyPlane.empty() && hasNullValue(V->getType())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000410 Value *ZeroInitializer = Constant::getNullValue(V->getType());
411 if (V != ZeroInitializer) {
412 TyPlane.push_back(ZeroInitializer);
413 CompactionNodeMap[ZeroInitializer] = 0;
414 }
415 }
416
417 unsigned SlotNo = TyPlane.size();
418 TyPlane.push_back(V);
419 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
420 return SlotNo;
421}
422
Reid Spencer24ab28f2004-07-04 11:42:49 +0000423/// getOrCreateCompactionTableSlot - This method is used to build up the initial
424/// approximation of the compaction table.
425unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
426 std::map<const Type*, unsigned>::iterator I =
427 CompactionTypeMap.lower_bound(T);
428 if (I != CompactionTypeMap.end() && I->first == T)
429 return I->second; // Already exists?
430
431 unsigned SlotNo = CompactionTypes.size();
432 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
433 CompactionTypes.push_back(T);
434 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
435 return SlotNo;
436}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000437
438/// buildCompactionTable - Since all of the function constants and types are
439/// stored in the module-level constant table, we don't need to emit a function
440/// constant table. Also due to this, the indices for various constants and
441/// types might be very large in large programs. In order to avoid blowing up
442/// the size of instructions in the bytecode encoding, we build a compaction
443/// table, which defines a mapping from function-local identifiers to global
444/// identifiers.
445void SlotCalculator::buildCompactionTable(const Function *F) {
446 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000447 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000448 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000449 CompactionTable.resize(Type::LastPrimitiveTyID+1);
450 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000451 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000452 CompactionTypes.push_back(PrimTy);
453 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000454 }
455
456 // Next, include any types used by function arguments.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000457 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
458 I != E; ++I)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000459 getOrCreateCompactionTableSlot(I->getType());
460
461 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000462 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000463 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
464 getOrCreateCompactionTableSlot(I->getType());
465 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Chris Lattner101cefa2006-06-07 22:20:03 +0000466 if (isa<Constant>(I->getOperand(op)) || isa<InlineAsm>(I->getOperand(op)))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000467 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner614cdcd2004-01-18 21:07:07 +0000468 }
469
Reid Spencer9231ac82004-05-25 08:53:40 +0000470 // Now do the constants and global values
Reid Spencer78d033e2007-01-06 07:24:44 +0000471 const SymbolTable &ST = F->getValueSymbolTable();
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000472 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000473 PE = ST.plane_end(); PI != PE; ++PI)
474 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000475 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000476 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000477 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000478
479 // Now that we have all of the values in the table, and know what types are
480 // referenced, make sure that there is at least the zero initializer in any
481 // used type plane. Since the type was used, we will be emitting instructions
482 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000483 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000484 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000485 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000486 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000487 const Type *Ty = CompactionTypes[i];
488 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
489 assert(Ty->getTypeID() != Type::VoidTyID);
490 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000491 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
492 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000493
Chris Lattner614cdcd2004-01-18 21:07:07 +0000494 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000495 // to emit the smallest possible binaries, do not compactify the type plane if
496 // it will not save us anything. Because we have not yet incorporated the
497 // function body itself yet, we don't know whether or not it's a good idea to
498 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000499 TypeList &GlobalTypes = Types;
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000500
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000501 // All of the values types will be scrunched to the start of the types plane
502 // of the global table. Figure out just how many there are.
503 assert(!GlobalTypes.empty() && "No global types???");
504 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000505 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000506 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000507
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000508 // If there are fewer that 64 types, no instructions will be exploded due to
509 // the size of the type operands. Thus there is no need to compactify types.
510 // Also, if the compaction table contains most of the entries in the global
511 // table, there really is no reason to compactify either.
512 if (NumFCTypes < 64) {
513 // Decompactifying types is tricky, because we have to move type planes all
514 // over the place. At least we don't need to worry about updating the
515 // CompactionNodeMap for non-types though.
516 std::vector<TypePlane> TmpCompactionTable;
517 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000518 TypeList TmpTypes;
519 std::swap(TmpTypes, CompactionTypes);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000520
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000521 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000522 while (!TmpTypes.empty()) {
523 const Type *Ty = TmpTypes.back();
524 TmpTypes.pop_back();
525 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000526
Reid Spencer24ab28f2004-07-04 11:42:49 +0000527 // Find the global slot number for this type.
528 int TySlot = getSlot(Ty);
529 assert(TySlot != -1 && "Type doesn't exist in global table?");
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000530
Reid Spencer24ab28f2004-07-04 11:42:49 +0000531 // Now we know where to put the compaction table plane.
532 if (CompactionTable.size() <= unsigned(TySlot))
533 CompactionTable.resize(TySlot+1);
534 // Move the plane back into the compaction table.
535 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000536
Reid Spencer24ab28f2004-07-04 11:42:49 +0000537 // And remove the empty plane we just moved in.
538 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000539 }
540 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000541}
542
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000543
544/// pruneCompactionTable - Once the entire function being processed has been
545/// incorporated into the current compaction table, look over the compaction
546/// table and check to see if there are any values whose compaction will not
547/// save us any space in the bytecode file. If compactifying these values
548/// serves no purpose, then we might as well not even emit the compactification
549/// information to the bytecode file, saving a bit more space.
550///
551/// Note that the type plane has already been compactified if possible.
552///
553void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000554 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000555 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000556 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000557 TypePlane &CPlane = CompactionTable[ctp];
558 unsigned GlobalSlot = ctp;
559 if (!TyPlane.empty())
560 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
561
562 if (GlobalSlot >= Table.size())
563 Table.resize(GlobalSlot+1);
564 TypePlane &GPlane = Table[GlobalSlot];
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000565
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000566 unsigned ModLevel = getModuleLevel(ctp);
567 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
568
569 // If the maximum index required if all entries in this plane were merged
570 // into the global plane is less than 64, go ahead and eliminate the
571 // plane.
572 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
573
574 // If there are no function-local values defined, and the maximum
575 // referenced global entry is less than 64, we don't need to compactify.
576 if (!PrunePlane && NumFunctionObjs == 0) {
577 unsigned MaxIdx = 0;
578 for (unsigned i = 0; i != ModLevel; ++i) {
579 unsigned Idx = NodeMap[CPlane[i]];
580 if (Idx > MaxIdx) MaxIdx = Idx;
581 }
582 PrunePlane = MaxIdx < 64;
583 }
584
585 // Ok, finally, if we decided to prune this plane out of the compaction
586 // table, do so now.
587 if (PrunePlane) {
588 TypePlane OldPlane;
589 std::swap(OldPlane, CPlane);
590
591 // Loop over the function local objects, relocating them to the global
592 // table plane.
593 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
594 const Value *V = OldPlane[i];
595 CompactionNodeMap.erase(V);
596 assert(NodeMap.count(V) == 0 && "Value already in table??");
597 getOrCreateSlot(V);
598 }
599
600 // For compactified global values, just remove them from the compaction
601 // node map.
602 for (unsigned i = 0; i != ModLevel; ++i)
603 CompactionNodeMap.erase(OldPlane[i]);
604
605 // Update the new modulelevel for this plane.
606 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
607 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
608 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
609 }
610 }
611}
612
Reid Spencer07ea1912004-08-26 22:32:00 +0000613/// Determine if the compaction table is actually empty. Because the
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000614/// compaction table always includes the primitive type planes, we
Reid Spencer07ea1912004-08-26 22:32:00 +0000615/// can't just check getCompactionTable().size() because it will never
616/// be zero. Furthermore, the ModuleLevel factors into whether a given
617/// plane is empty or not. This function does the necessary computation
618/// to determine if its actually empty.
619bool SlotCalculator::CompactionTableIsEmpty() const {
620 // Check a degenerate case, just in case.
621 if (CompactionTable.size() == 0) return true;
622
623 // Check each plane
624 for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
625 // If the plane is not empty
626 if (!CompactionTable[i].empty()) {
627 // If the module level is non-zero then at least the
628 // first element of the plane is valid and therefore not empty.
629 unsigned End = getModuleLevel(i);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000630 if (End != 0)
Reid Spencer07ea1912004-08-26 22:32:00 +0000631 return false;
632 }
633 }
634 // All the compaction table planes are empty so the table is
635 // considered empty too.
636 return true;
637}
638
Chris Lattner8c202cd2004-01-15 18:47:15 +0000639int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000640 // If there is a CompactionTable active...
641 if (!CompactionNodeMap.empty()) {
642 std::map<const Value*, unsigned>::const_iterator I =
643 CompactionNodeMap.find(V);
644 if (I != CompactionNodeMap.end())
645 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000646 // Otherwise, if it's not in the compaction table, it must be in a
647 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000648 }
649
Chris Lattner8c202cd2004-01-15 18:47:15 +0000650 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
651 if (I != NodeMap.end())
652 return (int)I->second;
653
Chris Lattner8c202cd2004-01-15 18:47:15 +0000654 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000655}
656
Reid Spencer24ab28f2004-07-04 11:42:49 +0000657int SlotCalculator::getSlot(const Type*T) const {
658 // If there is a CompactionTable active...
659 if (!CompactionTypeMap.empty()) {
660 std::map<const Type*, unsigned>::const_iterator I =
661 CompactionTypeMap.find(T);
662 if (I != CompactionTypeMap.end())
663 return (int)I->second;
664 // Otherwise, if it's not in the compaction table, it must be in a
665 // non-compactified plane.
666 }
667
668 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
669 if (I != TypeMap.end())
670 return (int)I->second;
671
672 return -1;
673}
Chris Lattner9a297902001-09-07 16:31:52 +0000674
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000675int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000676 if (V->getType() == Type::VoidTy) return -1;
677
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000678 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000679 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000680
Chris Lattner129baf62004-12-04 21:28:47 +0000681 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
682 assert(GV->getParent() != 0 && "Global not embedded into a module!");
683
Chris Lattner614cdcd2004-01-18 21:07:07 +0000684 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000685 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000686 assert(CompactionNodeMap.empty() &&
687 "All needed constants should be in the compaction map already!");
688
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000689 // Do not index the characters that make up constant strings. We emit
690 // constant strings as special entities that don't require their
Reid Spencer798ff642004-05-26 07:37:11 +0000691 // individual characters to be emitted.
692 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000693 // This makes sure that if a constant has uses (for example an array of
694 // const ints), that they are inserted also.
695 //
696 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
697 I != E; ++I)
698 getOrCreateSlot(*I);
699 } else {
700 assert(ModuleLevel.empty() &&
701 "How can a constant string be directly accessed in a function?");
702 // Otherwise, if we are emitting a bytecode file and this IS a string,
703 // remember it.
704 if (!C->isNullValue())
705 ConstantStrings.push_back(cast<ConstantArray>(C));
706 }
Chris Lattner3413d152003-03-19 20:57:22 +0000707 }
708
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000709 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000710}
711
Reid Spencer24ab28f2004-07-04 11:42:49 +0000712int SlotCalculator::getOrCreateSlot(const Type* T) {
713 int SlotNo = getSlot(T); // Check to see if it's already in!
714 if (SlotNo != -1) return SlotNo;
715 return insertType(T);
716}
Chris Lattner9a297902001-09-07 16:31:52 +0000717
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000718int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000719 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000720 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000721
Chris Lattner614cdcd2004-01-18 21:07:07 +0000722 // If we are building a compaction map, and if this plane is being compacted,
723 // insert the value into the compaction map, not into the global map.
724 if (!CompactionNodeMap.empty()) {
725 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencere8404342004-07-18 00:18:30 +0000726 assert(!isa<Constant>(D) &&
727 "Types, constants, and globals should be in global table!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000728
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000729 int Plane = getSlot(D->getType());
730 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
731 "Didn't find value type!");
732 if (!CompactionTable[Plane].empty())
733 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000734 }
735
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000736 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000737 // name and we don't want names, then ignore the silly node... Note that types
738 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000739 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000740 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000741 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000742 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000743 return -1; // We do need types unconditionally though
744 }
Chris Lattner00950542001-06-06 20:29:01 +0000745
Chris Lattner9a297902001-09-07 16:31:52 +0000746 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000747 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000748}
749
Reid Spencer24ab28f2004-07-04 11:42:49 +0000750int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
751 assert(Ty && "Can't insert a null type!");
752 assert(getSlot(Ty) == -1 && "Type is already in the table!");
753
754 // If we are building a compaction map, and if this plane is being compacted,
755 // insert the value into the compaction map, not into the global map.
756 if (!CompactionTypeMap.empty()) {
757 getOrCreateCompactionTableSlot(Ty);
758 }
759
760 // Insert the current type before any subtypes. This is important because
761 // recursive types elements are inserted in a bottom up order. Changing
762 // this here can break things. For example:
763 //
764 // global { \2 * } { { \2 }* null }
765 //
766 int ResultSlot = doInsertType(Ty);
767 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
768 ResultSlot << "\n");
769
770 // Loop over any contained types in the definition... in post
771 // order.
772 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
773 I != E; ++I) {
774 if (*I != Ty) {
775 const Type *SubTy = *I;
776 // If we haven't seen this sub type before, add it to our type table!
777 if (getSlot(SubTy) == -1) {
778 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattneraec6dd52004-07-12 20:29:52 +0000779 doInsertType(SubTy);
Reid Spencere8404342004-07-18 00:18:30 +0000780 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000781 }
782 }
783 }
784 return ResultSlot;
785}
786
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000787// doInsertValue - This is a small helper function to be called only
788// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000789//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000790int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000791 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000792 unsigned Ty;
793
794 // Used for debugging DefSlot=-1 assertion...
795 //if (Typ == Type::TypeTy)
Bill Wendling68fe61d2006-11-29 00:19:40 +0000796 // llvm_cerr << "Inserting type '"<<cast<Type>(D)->getDescription() <<"'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000797
Chris Lattner00950542001-06-06 20:29:01 +0000798 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000799 int ValSlot;
800 if (CompactionTable.empty())
801 ValSlot = getSlot(Typ);
802 else
803 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000804 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000805 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000806 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000807 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000808 }
Chris Lattner3413d152003-03-19 20:57:22 +0000809 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000810 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000811 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000812 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000813
Chris Lattner00950542001-06-06 20:29:01 +0000814 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
815 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000816
817 // If this is the first value to get inserted into the type plane, make sure
818 // to insert the implicit null value...
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000819 if (Table[Ty].empty() && hasNullValue(Typ)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000820 Value *ZeroInitializer = Constant::getNullValue(Typ);
821
822 // If we are pushing zeroinit, it will be handled below.
823 if (D != ZeroInitializer) {
824 Table[Ty].push_back(ZeroInitializer);
825 NodeMap[ZeroInitializer] = 0;
826 }
827 }
828
Chris Lattner9a297902001-09-07 16:31:52 +0000829 // Insert node into table and NodeMap...
830 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000831 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000832
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000833 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000834 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000835 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000836 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000837 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000838 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000839 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000840}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000841
842// doInsertType - This is a small helper function to be called only
843// be insertType.
844//
845int SlotCalculator::doInsertType(const Type *Ty) {
846
847 // Insert node into table and NodeMap...
848 unsigned DestSlot = TypeMap[Ty] = Types.size();
849 Types.push_back(Ty);
850
851 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
852 return (int)DestSlot;
853}
854