blob: cf770c4bbc98b303dec3c8ed7100608671f0f91d [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 Spencer24ab28f2004-07-04 11:42:49 +000025#include "llvm/Type.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000026#include "llvm/Analysis/ConstantsScanner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/PostOrderIterator.h"
28#include "llvm/ADT/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000029#include <algorithm>
Reid Spencer24ab28f2004-07-04 11:42:49 +000030#include <functional>
Chris Lattner31f84992003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner9a297902001-09-07 16:31:52 +000033#if 0
Bill Wendling68fe61d2006-11-29 00:19:40 +000034#include "llvm/Support/Streams.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000035#define SC_DEBUG(X) cerr << X
Chris Lattner9a297902001-09-07 16:31:52 +000036#else
37#define SC_DEBUG(X)
38#endif
Chris Lattner00950542001-06-06 20:29:01 +000039
Reid Spencer798ff642004-05-26 07:37:11 +000040SlotCalculator::SlotCalculator(const Module *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000041 ModuleContainsAllFunctionConstants = false;
Reid Spencer24ab28f2004-07-04 11:42:49 +000042 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +000043 TheModule = M;
44
45 // Preload table... Make sure that all of the primitive types are in the table
46 // and that their Primitive ID is equal to their slot #
47 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000048 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000049 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000050 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer24ab28f2004-07-04 11:42:49 +000051 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000052 }
53
54 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000055 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000056}
57
Reid Spencer798ff642004-05-26 07:37:11 +000058SlotCalculator::SlotCalculator(const Function *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000059 ModuleContainsAllFunctionConstants = false;
Chris Lattner00950542001-06-06 20:29:01 +000060 TheModule = M ? M->getParent() : 0;
61
62 // Preload table... Make sure that all of the primitive types are in the table
63 // and that their Primitive ID is equal to their slot #
64 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000065 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000066 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +000067 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer24ab28f2004-07-04 11:42:49 +000068 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000069 }
70
71 if (TheModule == 0) return; // Empty table...
72
Chris Lattner9a297902001-09-07 16:31:52 +000073 processModule(); // Process module level stuff
Chris Lattner68e3dbc2004-01-20 00:57:32 +000074 incorporateFunction(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000075}
76
Chris Lattner614cdcd2004-01-18 21:07:07 +000077unsigned SlotCalculator::getGlobalSlot(const Value *V) const {
78 assert(!CompactionTable.empty() &&
79 "This method can only be used when compaction is enabled!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000080 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner68e3dbc2004-01-20 00:57:32 +000081 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000082 return I->second;
83}
84
Reid Spencer24ab28f2004-07-04 11:42:49 +000085unsigned SlotCalculator::getGlobalSlot(const Type* T) const {
86 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
87 assert(I != TypeMap.end() && "Didn't find global slot entry!");
88 return I->second;
89}
90
Chris Lattner68e3dbc2004-01-20 00:57:32 +000091SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +000092 if (CompactionTable.empty()) { // No compaction table active?
93 // fall out
94 } else if (!CompactionTable[Plane].empty()) { // Compaction table active.
95 assert(Plane < CompactionTable.size());
96 return CompactionTable[Plane];
97 } else {
98 // Final case: compaction table active, but this plane is not
99 // compactified. If the type plane is compactified, unmap back to the
100 // global type plane corresponding to "Plane".
Reid Spencer24ab28f2004-07-04 11:42:49 +0000101 if (!CompactionTypes.empty()) {
102 const Type *Ty = CompactionTypes[Plane];
103 TypeMapType::iterator It = TypeMap.find(Ty);
104 assert(It != TypeMap.end() && "Type not in global constant map?");
105 Plane = It->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000106 }
107 }
108
109 // Okay we are just returning an entry out of the main Table. Make sure the
110 // plane exists and return it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000111 if (Plane >= Table.size())
112 Table.resize(Plane+1);
113 return Table[Plane];
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000114}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000115
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000116// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +0000117// types that are available.
118//
119void SlotCalculator::processModule() {
120 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000121
Chris Lattner3413d152003-03-19 20:57:22 +0000122 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +0000123 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000124 for (Module::const_global_iterator I = TheModule->global_begin(),
125 E = TheModule->global_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000126 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000127
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 // Scavenge the types out of the functions, then add the functions themselves
129 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000130 //
Chris Lattner3413d152003-03-19 20:57:22 +0000131 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
132 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000133 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000134
Chris Lattner3413d152003-03-19 20:57:22 +0000135 // Add all of the module level constants used as initializers
136 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000137 for (Module::const_global_iterator I = TheModule->global_begin(),
138 E = TheModule->global_end(); I != E; ++I)
Chris Lattner3413d152003-03-19 20:57:22 +0000139 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000140 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000141
Chris Lattnerdcea6302004-01-14 23:34:39 +0000142 // Now that all global constants have been added, rearrange constant planes
143 // that contain constant strings so that the strings occur at the start of the
144 // plane, not somewhere in the middle.
145 //
Reid Spencer798ff642004-05-26 07:37:11 +0000146 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
147 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
Reid Spencer88cfda22006-12-31 05:44:24 +0000148 if (AT->getElementType() == Type::Int8Ty) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000149 TypePlane &Plane = Table[plane];
150 unsigned FirstNonStringID = 0;
151 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000152 if (isa<ConstantAggregateZero>(Plane[i]) ||
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000153 (isa<ConstantArray>(Plane[i]) &&
Chris Lattner236ca442004-10-24 04:27:59 +0000154 cast<ConstantArray>(Plane[i])->isString())) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000155 // Check to see if we have to shuffle this string around. If not,
156 // don't do anything.
157 if (i != FirstNonStringID) {
158 // Swap the plane entries....
159 std::swap(Plane[i], Plane[FirstNonStringID]);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000160
Reid Spencer24ab28f2004-07-04 11:42:49 +0000161 // Keep the NodeMap up to date.
162 NodeMap[Plane[i]] = i;
163 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
164 }
165 ++FirstNonStringID;
166 }
Reid Spencer798ff642004-05-26 07:37:11 +0000167 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000168 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000169
170 // Scan all of the functions for their constants, which allows us to emit
171 // more compact modules. This is optional, and is just used to compactify
Reid Spencere8404342004-07-18 00:18:30 +0000172 // the constants used by different functions together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000173 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000174 // This functionality tends to produce smaller bytecode files. This should
175 // not be used in the future by clients that want to, for example, build and
176 // emit functions on the fly. For now, however, it is unconditionally
Reid Spencere8404342004-07-18 00:18:30 +0000177 // enabled.
Reid Spencer798ff642004-05-26 07:37:11 +0000178 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000179
Reid Spencer798ff642004-05-26 07:37:11 +0000180 SC_DEBUG("Inserting function constants:\n");
181 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
182 F != E; ++F) {
Chris Lattner3bc5a602006-01-25 23:08:15 +0000183 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
184 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
185 OI != E; ++OI) {
186 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
187 isa<InlineAsm>(*OI))
188 getOrCreateSlot(*OI);
189 }
Reid Spencer798ff642004-05-26 07:37:11 +0000190 getOrCreateSlot(I->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000191 }
Reid Spencer798ff642004-05-26 07:37:11 +0000192 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000193 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000194
Chris Lattner70cc3392001-09-10 07:58:01 +0000195 // Insert constants that are named at module level into the slot pool so that
196 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000197 SC_DEBUG("Inserting SymbolTable values:\n");
198 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000199
Chris Lattnera14b0d42004-01-10 23:46:13 +0000200 // Now that we have collected together all of the information relevant to the
201 // module, compactify the type table if it is particularly big and outputting
202 // a bytecode file. The basic problem we run into is that some programs have
203 // a large number of types, which causes the type field to overflow its size,
204 // which causes instructions to explode in size (particularly call
205 // instructions). To avoid this behavior, we "sort" the type table so that
206 // all non-value types are pushed to the end of the type table, giving nice
207 // low numbers to the types that can be used by instructions, thus reducing
208 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000209 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000210 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000211 for (unsigned i = 0, e = Types.size(); i != e; ++i)
212 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000213 // Check to see if we have to shuffle this type around. If not, don't
214 // do anything.
215 if (i != FirstNonValueTypeID) {
216 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000217 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000218
Reid Spencer24ab28f2004-07-04 11:42:49 +0000219 // Keep the TypeMap up to date.
220 TypeMap[Types[i]] = i;
221 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000222
223 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000224 if (Table.size() > FirstNonValueTypeID) {
225 if (Table.size() <= i) Table.resize(i+1);
226 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000227 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000228 }
229 ++FirstNonValueTypeID;
230 }
231 }
232
Chris Lattner9a297902001-09-07 16:31:52 +0000233 SC_DEBUG("end processModule!\n");
234}
235
236// processSymbolTable - Insert all of the values in the specified symbol table
237// into the values table...
238//
239void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000240 // Do the types first.
241 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
242 TE = ST->type_end(); TI != TE; ++TI )
243 getOrCreateSlot(TI->second);
244
245 // Now do the values.
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000246 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000247 PE = ST->plane_end(); PI != PE; ++PI)
248 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000249 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000250 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000251}
252
253void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000254 // Do the types first
255 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
256 TE = ST->type_end(); TI != TE; ++TI )
257 getOrCreateSlot(TI->second);
258
259 // Now do the constant values in all planes
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000260 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000261 PE = ST->plane_end(); PI != PE; ++PI)
262 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000263 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000264 if (isa<Constant>(VI->second) &&
265 !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000266 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000267}
268
269
Chris Lattnerce439b52003-10-20 19:10:06 +0000270void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000271 assert((ModuleLevel.size() == 0 ||
272 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000273
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000274 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000275
Chris Lattner614cdcd2004-01-18 21:07:07 +0000276 // If we emitted all of the function constants, build a compaction table.
Chris Lattner101cefa2006-06-07 22:20:03 +0000277 if (ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000278 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000279
280 // Update the ModuleLevel entries to be accurate.
281 ModuleLevel.resize(getNumPlanes());
282 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
283 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000284 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000285
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000286 // Iterate over function arguments, adding them to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000287 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000288 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000289
Chris Lattner3bc5a602006-01-25 23:08:15 +0000290 if (!ModuleContainsAllFunctionConstants) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000291 // Iterate over all of the instructions in the function, looking for
292 // constant values that are referenced. Add these to the value pools
293 // before any nonconstant values. This will be turned into the constant
294 // pool for the bytecode writer.
295 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000296
Chris Lattner614cdcd2004-01-18 21:07:07 +0000297 // Emit all of the constants that are being used by the instructions in
298 // the function...
Chris Lattner3bc5a602006-01-25 23:08:15 +0000299 for (constant_iterator CI = constant_begin(F), CE = constant_end(F);
300 CI != CE; ++CI)
301 getOrCreateSlot(*CI);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000302
Chris Lattner9a297902001-09-07 16:31:52 +0000303 // If there is a symbol table, it is possible that the user has names for
304 // constants that are not being used. In this case, we will have problems
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000305 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000306 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000307 // constants now.
308 //
Chris Lattnerce439b52003-10-20 19:10:06 +0000309 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000310 }
311
Chris Lattner9a297902001-09-07 16:31:52 +0000312 SC_DEBUG("Inserting Instructions:\n");
313
314 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000315 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
316 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000317 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
318 getOrCreateSlot(I);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000319 }
Chris Lattner9a297902001-09-07 16:31:52 +0000320 }
321
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000322 // If we are building a compaction table, prune out planes that do not benefit
323 // from being compactified.
324 if (!CompactionTable.empty())
325 pruneCompactionTable();
326
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000327 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000328}
329
Chris Lattnerb5794002002-04-07 22:49:37 +0000330void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000331 assert((ModuleLevel.size() != 0 ||
332 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000333 unsigned NumModuleTypes = ModuleLevel.size();
334
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000335 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000336
Chris Lattner614cdcd2004-01-18 21:07:07 +0000337 // First, free the compaction map if used.
338 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000339 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000340
341 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000342 for (unsigned i = 0; i != NumModuleTypes; ++i) {
343 // Size of plane before function came
344 unsigned ModuleLev = getModuleLevel(i);
345 assert(int(ModuleLev) >= 0 && "BAD!");
346
347 TypePlane &Plane = getPlane(i);
348
349 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
350 while (Plane.size() != ModuleLev) {
351 assert(!isa<GlobalValue>(Plane.back()) &&
352 "Functions cannot define globals!");
353 NodeMap.erase(Plane.back()); // Erase from nodemap
354 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000355 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000356 }
Chris Lattner00950542001-06-06 20:29:01 +0000357
358 // We don't need this state anymore, free it up.
359 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000360 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000361
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000362 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000363 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000364 if (!CompactionTable.empty()) {
365 CompactionTable.clear();
366 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000367 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000368 TypePlane &Plane = Table.back();
369 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
370 << Plane.size() << "\n");
371 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000372 assert(!isa<GlobalValue>(Plane.back()) &&
373 "Functions cannot define globals!");
374 NodeMap.erase(Plane.back()); // Erase from nodemap
375 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000376 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000377
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000378 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000379 }
Chris Lattner00950542001-06-06 20:29:01 +0000380 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000381
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000382 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000383}
384
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000385static inline bool hasNullValue(const Type *Ty) {
386 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000387}
388
389/// getOrCreateCompactionTableSlot - This method is used to build up the initial
390/// approximation of the compaction table.
391unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
392 std::map<const Value*, unsigned>::iterator I =
393 CompactionNodeMap.lower_bound(V);
394 if (I != CompactionNodeMap.end() && I->first == V)
395 return I->second; // Already exists?
396
397 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000398 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000399 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000400 Ty = getOrCreateCompactionTableSlot(V->getType());
401 else // If the type plane was decompactified, use the global plane ID
402 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000403 if (CompactionTable.size() <= Ty)
404 CompactionTable.resize(Ty+1);
405
Chris Lattner614cdcd2004-01-18 21:07:07 +0000406 TypePlane &TyPlane = CompactionTable[Ty];
407
408 // Make sure to insert the null entry if the thing we are inserting is not a
409 // null constant.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000410 if (TyPlane.empty() && hasNullValue(V->getType())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000411 Value *ZeroInitializer = Constant::getNullValue(V->getType());
412 if (V != ZeroInitializer) {
413 TyPlane.push_back(ZeroInitializer);
414 CompactionNodeMap[ZeroInitializer] = 0;
415 }
416 }
417
418 unsigned SlotNo = TyPlane.size();
419 TyPlane.push_back(V);
420 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
421 return SlotNo;
422}
423
Reid Spencer24ab28f2004-07-04 11:42:49 +0000424/// getOrCreateCompactionTableSlot - This method is used to build up the initial
425/// approximation of the compaction table.
426unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
427 std::map<const Type*, unsigned>::iterator I =
428 CompactionTypeMap.lower_bound(T);
429 if (I != CompactionTypeMap.end() && I->first == T)
430 return I->second; // Already exists?
431
432 unsigned SlotNo = CompactionTypes.size();
433 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
434 CompactionTypes.push_back(T);
435 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
436 return SlotNo;
437}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000438
439/// buildCompactionTable - Since all of the function constants and types are
440/// stored in the module-level constant table, we don't need to emit a function
441/// constant table. Also due to this, the indices for various constants and
442/// types might be very large in large programs. In order to avoid blowing up
443/// the size of instructions in the bytecode encoding, we build a compaction
444/// table, which defines a mapping from function-local identifiers to global
445/// identifiers.
446void SlotCalculator::buildCompactionTable(const Function *F) {
447 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000448 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000449 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000450 CompactionTable.resize(Type::LastPrimitiveTyID+1);
451 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000452 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000453 CompactionTypes.push_back(PrimTy);
454 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000455 }
456
457 // Next, include any types used by function arguments.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000458 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
459 I != E; ++I)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000460 getOrCreateCompactionTableSlot(I->getType());
461
462 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000463 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000464 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
465 getOrCreateCompactionTableSlot(I->getType());
466 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Chris Lattner101cefa2006-06-07 22:20:03 +0000467 if (isa<Constant>(I->getOperand(op)) || isa<InlineAsm>(I->getOperand(op)))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000468 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner614cdcd2004-01-18 21:07:07 +0000469 }
470
Reid Spencer9231ac82004-05-25 08:53:40 +0000471 // Do the types in the symbol table
Chris Lattner614cdcd2004-01-18 21:07:07 +0000472 const SymbolTable &ST = F->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000473 for (SymbolTable::type_const_iterator TI = ST.type_begin(),
474 TE = ST.type_end(); TI != TE; ++TI)
475 getOrCreateCompactionTableSlot(TI->second);
476
477 // Now do the constants and global values
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000478 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000479 PE = ST.plane_end(); PI != PE; ++PI)
480 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000481 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000482 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000483 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000484
485 // Now that we have all of the values in the table, and know what types are
486 // referenced, make sure that there is at least the zero initializer in any
487 // used type plane. Since the type was used, we will be emitting instructions
488 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000489 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000490 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000491 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000492 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000493 const Type *Ty = CompactionTypes[i];
494 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
495 assert(Ty->getTypeID() != Type::VoidTyID);
496 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000497 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
498 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000499
Chris Lattner614cdcd2004-01-18 21:07:07 +0000500 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000501 // to emit the smallest possible binaries, do not compactify the type plane if
502 // it will not save us anything. Because we have not yet incorporated the
503 // function body itself yet, we don't know whether or not it's a good idea to
504 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000505 TypeList &GlobalTypes = Types;
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000506
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000507 // All of the values types will be scrunched to the start of the types plane
508 // of the global table. Figure out just how many there are.
509 assert(!GlobalTypes.empty() && "No global types???");
510 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000511 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000512 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000513
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000514 // If there are fewer that 64 types, no instructions will be exploded due to
515 // the size of the type operands. Thus there is no need to compactify types.
516 // Also, if the compaction table contains most of the entries in the global
517 // table, there really is no reason to compactify either.
518 if (NumFCTypes < 64) {
519 // Decompactifying types is tricky, because we have to move type planes all
520 // over the place. At least we don't need to worry about updating the
521 // CompactionNodeMap for non-types though.
522 std::vector<TypePlane> TmpCompactionTable;
523 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000524 TypeList TmpTypes;
525 std::swap(TmpTypes, CompactionTypes);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000526
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000527 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000528 while (!TmpTypes.empty()) {
529 const Type *Ty = TmpTypes.back();
530 TmpTypes.pop_back();
531 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000532
Reid Spencer24ab28f2004-07-04 11:42:49 +0000533 // Find the global slot number for this type.
534 int TySlot = getSlot(Ty);
535 assert(TySlot != -1 && "Type doesn't exist in global table?");
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000536
Reid Spencer24ab28f2004-07-04 11:42:49 +0000537 // Now we know where to put the compaction table plane.
538 if (CompactionTable.size() <= unsigned(TySlot))
539 CompactionTable.resize(TySlot+1);
540 // Move the plane back into the compaction table.
541 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000542
Reid Spencer24ab28f2004-07-04 11:42:49 +0000543 // And remove the empty plane we just moved in.
544 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000545 }
546 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000547}
548
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000549
550/// pruneCompactionTable - Once the entire function being processed has been
551/// incorporated into the current compaction table, look over the compaction
552/// table and check to see if there are any values whose compaction will not
553/// save us any space in the bytecode file. If compactifying these values
554/// serves no purpose, then we might as well not even emit the compactification
555/// information to the bytecode file, saving a bit more space.
556///
557/// Note that the type plane has already been compactified if possible.
558///
559void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000560 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000561 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000562 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000563 TypePlane &CPlane = CompactionTable[ctp];
564 unsigned GlobalSlot = ctp;
565 if (!TyPlane.empty())
566 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
567
568 if (GlobalSlot >= Table.size())
569 Table.resize(GlobalSlot+1);
570 TypePlane &GPlane = Table[GlobalSlot];
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000571
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000572 unsigned ModLevel = getModuleLevel(ctp);
573 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
574
575 // If the maximum index required if all entries in this plane were merged
576 // into the global plane is less than 64, go ahead and eliminate the
577 // plane.
578 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
579
580 // If there are no function-local values defined, and the maximum
581 // referenced global entry is less than 64, we don't need to compactify.
582 if (!PrunePlane && NumFunctionObjs == 0) {
583 unsigned MaxIdx = 0;
584 for (unsigned i = 0; i != ModLevel; ++i) {
585 unsigned Idx = NodeMap[CPlane[i]];
586 if (Idx > MaxIdx) MaxIdx = Idx;
587 }
588 PrunePlane = MaxIdx < 64;
589 }
590
591 // Ok, finally, if we decided to prune this plane out of the compaction
592 // table, do so now.
593 if (PrunePlane) {
594 TypePlane OldPlane;
595 std::swap(OldPlane, CPlane);
596
597 // Loop over the function local objects, relocating them to the global
598 // table plane.
599 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
600 const Value *V = OldPlane[i];
601 CompactionNodeMap.erase(V);
602 assert(NodeMap.count(V) == 0 && "Value already in table??");
603 getOrCreateSlot(V);
604 }
605
606 // For compactified global values, just remove them from the compaction
607 // node map.
608 for (unsigned i = 0; i != ModLevel; ++i)
609 CompactionNodeMap.erase(OldPlane[i]);
610
611 // Update the new modulelevel for this plane.
612 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
613 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
614 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
615 }
616 }
617}
618
Reid Spencer07ea1912004-08-26 22:32:00 +0000619/// Determine if the compaction table is actually empty. Because the
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000620/// compaction table always includes the primitive type planes, we
Reid Spencer07ea1912004-08-26 22:32:00 +0000621/// can't just check getCompactionTable().size() because it will never
622/// be zero. Furthermore, the ModuleLevel factors into whether a given
623/// plane is empty or not. This function does the necessary computation
624/// to determine if its actually empty.
625bool SlotCalculator::CompactionTableIsEmpty() const {
626 // Check a degenerate case, just in case.
627 if (CompactionTable.size() == 0) return true;
628
629 // Check each plane
630 for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
631 // If the plane is not empty
632 if (!CompactionTable[i].empty()) {
633 // If the module level is non-zero then at least the
634 // first element of the plane is valid and therefore not empty.
635 unsigned End = getModuleLevel(i);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000636 if (End != 0)
Reid Spencer07ea1912004-08-26 22:32:00 +0000637 return false;
638 }
639 }
640 // All the compaction table planes are empty so the table is
641 // considered empty too.
642 return true;
643}
644
Chris Lattner8c202cd2004-01-15 18:47:15 +0000645int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000646 // If there is a CompactionTable active...
647 if (!CompactionNodeMap.empty()) {
648 std::map<const Value*, unsigned>::const_iterator I =
649 CompactionNodeMap.find(V);
650 if (I != CompactionNodeMap.end())
651 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000652 // Otherwise, if it's not in the compaction table, it must be in a
653 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000654 }
655
Chris Lattner8c202cd2004-01-15 18:47:15 +0000656 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
657 if (I != NodeMap.end())
658 return (int)I->second;
659
Chris Lattner8c202cd2004-01-15 18:47:15 +0000660 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000661}
662
Reid Spencer24ab28f2004-07-04 11:42:49 +0000663int SlotCalculator::getSlot(const Type*T) const {
664 // If there is a CompactionTable active...
665 if (!CompactionTypeMap.empty()) {
666 std::map<const Type*, unsigned>::const_iterator I =
667 CompactionTypeMap.find(T);
668 if (I != CompactionTypeMap.end())
669 return (int)I->second;
670 // Otherwise, if it's not in the compaction table, it must be in a
671 // non-compactified plane.
672 }
673
674 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
675 if (I != TypeMap.end())
676 return (int)I->second;
677
678 return -1;
679}
Chris Lattner9a297902001-09-07 16:31:52 +0000680
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000681int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000682 if (V->getType() == Type::VoidTy) return -1;
683
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000684 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000685 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000686
Chris Lattner129baf62004-12-04 21:28:47 +0000687 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
688 assert(GV->getParent() != 0 && "Global not embedded into a module!");
689
Chris Lattner614cdcd2004-01-18 21:07:07 +0000690 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000691 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000692 assert(CompactionNodeMap.empty() &&
693 "All needed constants should be in the compaction map already!");
694
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000695 // Do not index the characters that make up constant strings. We emit
696 // constant strings as special entities that don't require their
Reid Spencer798ff642004-05-26 07:37:11 +0000697 // individual characters to be emitted.
698 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000699 // This makes sure that if a constant has uses (for example an array of
700 // const ints), that they are inserted also.
701 //
702 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
703 I != E; ++I)
704 getOrCreateSlot(*I);
705 } else {
706 assert(ModuleLevel.empty() &&
707 "How can a constant string be directly accessed in a function?");
708 // Otherwise, if we are emitting a bytecode file and this IS a string,
709 // remember it.
710 if (!C->isNullValue())
711 ConstantStrings.push_back(cast<ConstantArray>(C));
712 }
Chris Lattner3413d152003-03-19 20:57:22 +0000713 }
714
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000715 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000716}
717
Reid Spencer24ab28f2004-07-04 11:42:49 +0000718int SlotCalculator::getOrCreateSlot(const Type* T) {
719 int SlotNo = getSlot(T); // Check to see if it's already in!
720 if (SlotNo != -1) return SlotNo;
721 return insertType(T);
722}
Chris Lattner9a297902001-09-07 16:31:52 +0000723
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000724int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000725 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000726 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000727
Chris Lattner614cdcd2004-01-18 21:07:07 +0000728 // If we are building a compaction map, and if this plane is being compacted,
729 // insert the value into the compaction map, not into the global map.
730 if (!CompactionNodeMap.empty()) {
731 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencere8404342004-07-18 00:18:30 +0000732 assert(!isa<Constant>(D) &&
733 "Types, constants, and globals should be in global table!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000734
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000735 int Plane = getSlot(D->getType());
736 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
737 "Didn't find value type!");
738 if (!CompactionTable[Plane].empty())
739 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000740 }
741
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000742 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000743 // name and we don't want names, then ignore the silly node... Note that types
744 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000745 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000746 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000747 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000748 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000749 return -1; // We do need types unconditionally though
750 }
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattner9a297902001-09-07 16:31:52 +0000752 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000753 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000754}
755
Reid Spencer24ab28f2004-07-04 11:42:49 +0000756int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
757 assert(Ty && "Can't insert a null type!");
758 assert(getSlot(Ty) == -1 && "Type is already in the table!");
759
760 // If we are building a compaction map, and if this plane is being compacted,
761 // insert the value into the compaction map, not into the global map.
762 if (!CompactionTypeMap.empty()) {
763 getOrCreateCompactionTableSlot(Ty);
764 }
765
766 // Insert the current type before any subtypes. This is important because
767 // recursive types elements are inserted in a bottom up order. Changing
768 // this here can break things. For example:
769 //
770 // global { \2 * } { { \2 }* null }
771 //
772 int ResultSlot = doInsertType(Ty);
773 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
774 ResultSlot << "\n");
775
776 // Loop over any contained types in the definition... in post
777 // order.
778 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
779 I != E; ++I) {
780 if (*I != Ty) {
781 const Type *SubTy = *I;
782 // If we haven't seen this sub type before, add it to our type table!
783 if (getSlot(SubTy) == -1) {
784 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattneraec6dd52004-07-12 20:29:52 +0000785 doInsertType(SubTy);
Reid Spencere8404342004-07-18 00:18:30 +0000786 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000787 }
788 }
789 }
790 return ResultSlot;
791}
792
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000793// doInsertValue - This is a small helper function to be called only
794// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000795//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000796int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000797 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000798 unsigned Ty;
799
800 // Used for debugging DefSlot=-1 assertion...
801 //if (Typ == Type::TypeTy)
Bill Wendling68fe61d2006-11-29 00:19:40 +0000802 // llvm_cerr << "Inserting type '"<<cast<Type>(D)->getDescription() <<"'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000803
Chris Lattner00950542001-06-06 20:29:01 +0000804 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000805 int ValSlot;
806 if (CompactionTable.empty())
807 ValSlot = getSlot(Typ);
808 else
809 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000810 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000811 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000812 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000813 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000814 }
Chris Lattner3413d152003-03-19 20:57:22 +0000815 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000816 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000817 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000818 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000819
Chris Lattner00950542001-06-06 20:29:01 +0000820 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
821 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000822
823 // If this is the first value to get inserted into the type plane, make sure
824 // to insert the implicit null value...
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000825 if (Table[Ty].empty() && hasNullValue(Typ)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000826 Value *ZeroInitializer = Constant::getNullValue(Typ);
827
828 // If we are pushing zeroinit, it will be handled below.
829 if (D != ZeroInitializer) {
830 Table[Ty].push_back(ZeroInitializer);
831 NodeMap[ZeroInitializer] = 0;
832 }
833 }
834
Chris Lattner9a297902001-09-07 16:31:52 +0000835 // Insert node into table and NodeMap...
836 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000837 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000838
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000839 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000840 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000841 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000842 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000843 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000844 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000845 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000846}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000847
848// doInsertType - This is a small helper function to be called only
849// be insertType.
850//
851int SlotCalculator::doInsertType(const Type *Ty) {
852
853 // Insert node into table and NodeMap...
854 unsigned DestSlot = TypeMap[Ty] = Types.size();
855 Types.push_back(Ty);
856
857 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
858 return (int)DestSlot;
859}
860