blob: 75fb4185f7f0749d784be910749f97135102a717 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner083c99e2004-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 Lattner2f7c9632001-06-06 20:29:01 +000012//
Chris Lattner083c99e2004-01-20 00:57:32 +000013// This is used when writing a file to disk, either in bytecode or assembly.
Chris Lattner2f7c9632001-06-06 20:29:01 +000014//
15//===----------------------------------------------------------------------===//
16
Reid Spencer88f3e072004-07-04 11:42:49 +000017#include "SlotCalculator.h"
Chris Lattnerde363042004-01-14 23:34:39 +000018#include "llvm/Constants.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000019#include "llvm/DerivedTypes.h"
Reid Spencer88f3e072004-07-04 11:42:49 +000020#include "llvm/Function.h"
Alkis Evlogimenosfd7a2d42004-07-29 12:17:34 +000021#include "llvm/Instructions.h"
Chris Lattnerde363042004-01-14 23:34:39 +000022#include "llvm/Module.h"
Chris Lattner1e6912a2001-09-07 16:31:52 +000023#include "llvm/SymbolTable.h"
Reid Spencer88f3e072004-07-04 11:42:49 +000024#include "llvm/Type.h"
Chris Lattnerc70b3f62004-01-20 19:50:34 +000025#include "llvm/Analysis/ConstantsScanner.h"
Alkis Evlogimenos6029d402003-10-31 03:02:34 +000026#include "Support/PostOrderIterator.h"
Chris Lattner5de22042001-11-27 00:03:19 +000027#include "Support/STLExtras.h"
Chris Lattner1e6912a2001-09-07 16:31:52 +000028#include <algorithm>
Reid Spencer88f3e072004-07-04 11:42:49 +000029#include <functional>
30
Chris Lattner189d19f2003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner1e6912a2001-09-07 16:31:52 +000033#if 0
Reid Spencer88f3e072004-07-04 11:42:49 +000034#include <iostream>
Chris Lattnerfaca62c2003-03-19 20:57:22 +000035#define SC_DEBUG(X) std::cerr << X
Chris Lattner1e6912a2001-09-07 16:31:52 +000036#else
37#define SC_DEBUG(X)
38#endif
Chris Lattner2f7c9632001-06-06 20:29:01 +000039
Reid Spencer0aff01a2004-05-26 07:37:11 +000040SlotCalculator::SlotCalculator(const Module *M ) {
Chris Lattnerca1f8af2004-01-18 21:07:07 +000041 ModuleContainsAllFunctionConstants = false;
Reid Spencer88f3e072004-07-04 11:42:49 +000042 ModuleTypeLevel = 0;
Chris Lattner2f7c9632001-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 Evlogimenosd478d3d2003-10-29 03:12:12 +000048 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner2f7c9632001-06-06 20:29:01 +000049 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattner6b727592004-06-17 18:19:28 +000050 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer88f3e072004-07-04 11:42:49 +000051 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +000052 }
53
54 if (M == 0) return; // Empty table...
Chris Lattner1e6912a2001-09-07 16:31:52 +000055 processModule();
Chris Lattner2f7c9632001-06-06 20:29:01 +000056}
57
Reid Spencer0aff01a2004-05-26 07:37:11 +000058SlotCalculator::SlotCalculator(const Function *M ) {
Chris Lattnerca1f8af2004-01-18 21:07:07 +000059 ModuleContainsAllFunctionConstants = false;
Chris Lattner2f7c9632001-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 Evlogimenosd478d3d2003-10-29 03:12:12 +000065 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner2f7c9632001-06-06 20:29:01 +000066 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
Chris Lattner6b727592004-06-17 18:19:28 +000067 assert(Type::getPrimitiveType((Type::TypeID)i));
Reid Spencer88f3e072004-07-04 11:42:49 +000068 insertType(Type::getPrimitiveType((Type::TypeID)i), true);
Chris Lattner2f7c9632001-06-06 20:29:01 +000069 }
70
71 if (TheModule == 0) return; // Empty table...
72
Chris Lattner1e6912a2001-09-07 16:31:52 +000073 processModule(); // Process module level stuff
Chris Lattner083c99e2004-01-20 00:57:32 +000074 incorporateFunction(M); // Start out in incorporated state
Chris Lattner2f7c9632001-06-06 20:29:01 +000075}
76
Chris Lattnerca1f8af2004-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 Lattnerca1f8af2004-01-18 21:07:07 +000080 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner083c99e2004-01-20 00:57:32 +000081 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattnerca1f8af2004-01-18 21:07:07 +000082 return I->second;
83}
84
Reid Spencer88f3e072004-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 Lattner083c99e2004-01-20 00:57:32 +000091SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner083c99e2004-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 Spencer88f3e072004-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 Lattner083c99e2004-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 Spencer88f3e072004-07-04 11:42:49 +0000111 if (Plane >= Table.size())
112 Table.resize(Plane+1);
113 return Table[Plane];
Chris Lattner083c99e2004-01-20 00:57:32 +0000114}
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000115
Chris Lattner62b7fd12002-04-07 20:49:59 +0000116// processModule - Process all of the module level function declarations and
Chris Lattner1e6912a2001-09-07 16:31:52 +0000117// types that are available.
118//
119void SlotCalculator::processModule() {
120 SC_DEBUG("begin processModule!\n");
Chris Lattnerda975502001-09-10 07:58:01 +0000121
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000122 // Add all of the global variables to the value table...
Chris Lattner1907f4e2001-10-13 06:35:09 +0000123 //
124 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
Chris Lattner251a7e62002-10-14 00:48:57 +0000125 I != E; ++I)
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000126 getOrCreateSlot(I);
Chris Lattnerda975502001-09-10 07:58:01 +0000127
Chris Lattner62b7fd12002-04-07 20:49:59 +0000128 // Scavenge the types out of the functions, then add the functions themselves
129 // to the value table...
Chris Lattner1e6912a2001-09-07 16:31:52 +0000130 //
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000131 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
132 I != E; ++I)
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000133 getOrCreateSlot(I);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000134
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000135 // Add all of the module level constants used as initializers
136 //
137 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
138 I != E; ++I)
139 if (I->hasInitializer())
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000140 getOrCreateSlot(I->getInitializer());
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000141
Chris Lattnerde363042004-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 Spencer0aff01a2004-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]))
148 if (AT->getElementType() == Type::SByteTy ||
Reid Spencer88f3e072004-07-04 11:42:49 +0000149 AT->getElementType() == Type::UByteTy) {
150 TypePlane &Plane = Table[plane];
151 unsigned FirstNonStringID = 0;
152 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
153 if (isa<ConstantAggregateZero>(Plane[i]) ||
154 cast<ConstantArray>(Plane[i])->isString()) {
155 // 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]);
160
161 // Keep the NodeMap up to date.
162 NodeMap[Plane[i]] = i;
163 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
164 }
165 ++FirstNonStringID;
166 }
Reid Spencer0aff01a2004-05-26 07:37:11 +0000167 }
Chris Lattnerde363042004-01-14 23:34:39 +0000168 }
169
Reid Spencer30d69a52004-07-18 00:18:30 +0000170 // 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
172 // the constants used by different functions together.
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000173 //
Reid Spencer30d69a52004-07-18 00:18:30 +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
177 // enabled.
Reid Spencer0aff01a2004-05-26 07:37:11 +0000178 ModuleContainsAllFunctionConstants = true;
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000179
Reid Spencer0aff01a2004-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) {
183 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
184 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencer30d69a52004-07-18 00:18:30 +0000185 if (isa<Constant>(I->getOperand(op)) &&
186 !isa<GlobalValue>(I->getOperand(op)))
Reid Spencer88f3e072004-07-04 11:42:49 +0000187 getOrCreateSlot(I->getOperand(op));
Reid Spencer0aff01a2004-05-26 07:37:11 +0000188 getOrCreateSlot(I->getType());
189 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Reid Spencer88f3e072004-07-04 11:42:49 +0000190 getOrCreateSlot(VAN->getArgType());
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000191 }
Reid Spencer0aff01a2004-05-26 07:37:11 +0000192 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner65f901e2004-01-10 23:46:13 +0000193 }
Chris Lattner65f901e2004-01-10 23:46:13 +0000194
Chris Lattnerda975502001-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 Spencer0aff01a2004-05-26 07:37:11 +0000197 SC_DEBUG("Inserting SymbolTable values:\n");
198 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner1e6912a2001-09-07 16:31:52 +0000199
Chris Lattner65f901e2004-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 Spencer88f3e072004-07-04 11:42:49 +0000209 if (Types.size() >= 64) {
Chris Lattner65f901e2004-01-10 23:46:13 +0000210 unsigned FirstNonValueTypeID = 0;
Reid Spencer88f3e072004-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 Lattner65f901e2004-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 Spencer88f3e072004-07-04 11:42:49 +0000217 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattner65f901e2004-01-10 23:46:13 +0000218
Reid Spencer88f3e072004-07-04 11:42:49 +0000219 // Keep the TypeMap up to date.
220 TypeMap[Types[i]] = i;
221 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattner65f901e2004-01-10 23:46:13 +0000222
223 // When we move a type, make sure to move its value plane as needed.
Chris Lattner01805902004-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 Lattner01805902004-01-11 23:29:26 +0000227 }
Chris Lattner65f901e2004-01-10 23:46:13 +0000228 }
229 ++FirstNonValueTypeID;
230 }
231 }
232
Chris Lattner1e6912a2001-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 Spencere7e96712004-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.
246 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
247 PE = ST->plane_end(); PI != PE; ++PI)
248 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer88f3e072004-07-04 11:42:49 +0000249 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere7e96712004-05-25 08:53:40 +0000250 getOrCreateSlot(VI->second);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000251}
252
253void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencere7e96712004-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
260 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
261 PE = ST->plane_end(); PI != PE; ++PI)
262 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer88f3e072004-07-04 11:42:49 +0000263 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer30d69a52004-07-18 00:18:30 +0000264 if (isa<Constant>(VI->second) &&
265 !isa<GlobalValue>(VI->second))
Reid Spencer88f3e072004-07-04 11:42:49 +0000266 getOrCreateSlot(VI->second);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000267}
268
269
Chris Lattnerd77dd782003-10-20 19:10:06 +0000270void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer88f3e072004-07-04 11:42:49 +0000271 assert((ModuleLevel.size() == 0 ||
272 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000273
Chris Lattner62b7fd12002-04-07 20:49:59 +0000274 SC_DEBUG("begin processFunction!\n");
Chris Lattner1e6912a2001-09-07 16:31:52 +0000275
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000276 // If we emitted all of the function constants, build a compaction table.
Reid Spencer0aff01a2004-05-26 07:37:11 +0000277 if ( ModuleContainsAllFunctionConstants)
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000278 buildCompactionTable(F);
Chris Lattner083c99e2004-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 Spencer88f3e072004-07-04 11:42:49 +0000284 ModuleTypeLevel = Types.size();
Chris Lattner1e6912a2001-09-07 16:31:52 +0000285
Chris Lattner62b7fd12002-04-07 20:49:59 +0000286 // Iterate over function arguments, adding them to the value table...
Chris Lattnerd77dd782003-10-20 19:10:06 +0000287 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000288 getOrCreateSlot(I);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000289
Reid Spencer0aff01a2004-05-26 07:37:11 +0000290 if ( !ModuleContainsAllFunctionConstants ) {
Chris Lattnerca1f8af2004-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 //
296
297 // Emit all of the constants that are being used by the instructions in
298 // the function...
Reid Spencer88f3e072004-07-04 11:42:49 +0000299 constant_iterator CI = constant_begin(F);
300 constant_iterator CE = constant_end(F);
301 while ( CI != CE ) {
302 this->getOrCreateSlot(*CI);
303 ++CI;
304 }
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000305
Chris Lattner1e6912a2001-09-07 16:31:52 +0000306 // If there is a symbol table, it is possible that the user has names for
307 // constants that are not being used. In this case, we will have problems
308 // if we don't emit the constants now, because otherwise we will get
Chris Lattner65f901e2004-01-10 23:46:13 +0000309 // symbol table references to constants not in the output. Scan for these
Chris Lattner1e6912a2001-09-07 16:31:52 +0000310 // constants now.
311 //
Chris Lattnerd77dd782003-10-20 19:10:06 +0000312 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner2f7c9632001-06-06 20:29:01 +0000313 }
314
Chris Lattner1e6912a2001-09-07 16:31:52 +0000315 SC_DEBUG("Inserting Instructions:\n");
316
317 // Add all of the instructions to the type planes...
Chris Lattner2d0e6f62004-01-15 20:24:09 +0000318 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
319 getOrCreateSlot(BB);
Chris Lattner7267b352003-10-21 17:39:59 +0000320 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
321 getOrCreateSlot(I);
Chris Lattner2c35b1c2003-10-21 17:40:54 +0000322 if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
323 getOrCreateSlot(VAN->getArgType());
Chris Lattner7267b352003-10-21 17:39:59 +0000324 }
Chris Lattner1e6912a2001-09-07 16:31:52 +0000325 }
326
Chris Lattner083c99e2004-01-20 00:57:32 +0000327 // If we are building a compaction table, prune out planes that do not benefit
328 // from being compactified.
329 if (!CompactionTable.empty())
330 pruneCompactionTable();
331
Chris Lattner62b7fd12002-04-07 20:49:59 +0000332 SC_DEBUG("end processFunction!\n");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000333}
334
Chris Lattner6915f8f2002-04-07 22:49:37 +0000335void SlotCalculator::purgeFunction() {
Reid Spencer88f3e072004-07-04 11:42:49 +0000336 assert((ModuleLevel.size() != 0 ||
337 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000338 unsigned NumModuleTypes = ModuleLevel.size();
339
Chris Lattner62b7fd12002-04-07 20:49:59 +0000340 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner1e6912a2001-09-07 16:31:52 +0000341
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000342 // First, free the compaction map if used.
343 CompactionNodeMap.clear();
Reid Spencer88f3e072004-07-04 11:42:49 +0000344 CompactionTypeMap.clear();
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000345
346 // Next, remove values from existing type planes
Chris Lattner083c99e2004-01-20 00:57:32 +0000347 for (unsigned i = 0; i != NumModuleTypes; ++i) {
348 // Size of plane before function came
349 unsigned ModuleLev = getModuleLevel(i);
350 assert(int(ModuleLev) >= 0 && "BAD!");
351
352 TypePlane &Plane = getPlane(i);
353
354 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
355 while (Plane.size() != ModuleLev) {
356 assert(!isa<GlobalValue>(Plane.back()) &&
357 "Functions cannot define globals!");
358 NodeMap.erase(Plane.back()); // Erase from nodemap
359 Plane.pop_back(); // Shrink plane
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360 }
Chris Lattner083c99e2004-01-20 00:57:32 +0000361 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000362
363 // We don't need this state anymore, free it up.
364 ModuleLevel.clear();
Reid Spencer88f3e072004-07-04 11:42:49 +0000365 ModuleTypeLevel = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000366
Chris Lattner083c99e2004-01-20 00:57:32 +0000367 // Finally, remove any type planes defined by the function...
Reid Spencer88f3e072004-07-04 11:42:49 +0000368 CompactionTypes.clear();
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000369 if (!CompactionTable.empty()) {
370 CompactionTable.clear();
371 } else {
Chris Lattner083c99e2004-01-20 00:57:32 +0000372 while (Table.size() > NumModuleTypes) {
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000373 TypePlane &Plane = Table.back();
374 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
375 << Plane.size() << "\n");
376 while (Plane.size()) {
Chris Lattner083c99e2004-01-20 00:57:32 +0000377 assert(!isa<GlobalValue>(Plane.back()) &&
378 "Functions cannot define globals!");
379 NodeMap.erase(Plane.back()); // Erase from nodemap
380 Plane.pop_back(); // Shrink plane
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000381 }
382
Chris Lattner083c99e2004-01-20 00:57:32 +0000383 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000384 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000385 }
Chris Lattner083c99e2004-01-20 00:57:32 +0000386
Chris Lattner62b7fd12002-04-07 20:49:59 +0000387 SC_DEBUG("end purgeFunction!\n");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000388}
389
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000390static inline bool hasNullValue(unsigned TyID) {
Reid Spencer88f3e072004-07-04 11:42:49 +0000391 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000392}
393
394/// getOrCreateCompactionTableSlot - This method is used to build up the initial
395/// approximation of the compaction table.
396unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
397 std::map<const Value*, unsigned>::iterator I =
398 CompactionNodeMap.lower_bound(V);
399 if (I != CompactionNodeMap.end() && I->first == V)
400 return I->second; // Already exists?
401
402 // Make sure the type is in the table.
Chris Lattner083c99e2004-01-20 00:57:32 +0000403 unsigned Ty;
Reid Spencer88f3e072004-07-04 11:42:49 +0000404 if (!CompactionTypes.empty())
Chris Lattner083c99e2004-01-20 00:57:32 +0000405 Ty = getOrCreateCompactionTableSlot(V->getType());
406 else // If the type plane was decompactified, use the global plane ID
407 Ty = getSlot(V->getType());
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000408 if (CompactionTable.size() <= Ty)
409 CompactionTable.resize(Ty+1);
410
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000411 TypePlane &TyPlane = CompactionTable[Ty];
412
413 // Make sure to insert the null entry if the thing we are inserting is not a
414 // null constant.
Chris Lattner6b727592004-06-17 18:19:28 +0000415 if (TyPlane.empty() && hasNullValue(V->getType()->getTypeID())) {
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000416 Value *ZeroInitializer = Constant::getNullValue(V->getType());
417 if (V != ZeroInitializer) {
418 TyPlane.push_back(ZeroInitializer);
419 CompactionNodeMap[ZeroInitializer] = 0;
420 }
421 }
422
423 unsigned SlotNo = TyPlane.size();
424 TyPlane.push_back(V);
425 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
426 return SlotNo;
427}
428
Reid Spencer88f3e072004-07-04 11:42:49 +0000429/// getOrCreateCompactionTableSlot - This method is used to build up the initial
430/// approximation of the compaction table.
431unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
432 std::map<const Type*, unsigned>::iterator I =
433 CompactionTypeMap.lower_bound(T);
434 if (I != CompactionTypeMap.end() && I->first == T)
435 return I->second; // Already exists?
436
437 unsigned SlotNo = CompactionTypes.size();
438 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
439 CompactionTypes.push_back(T);
440 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
441 return SlotNo;
442}
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000443
444/// buildCompactionTable - Since all of the function constants and types are
445/// stored in the module-level constant table, we don't need to emit a function
446/// constant table. Also due to this, the indices for various constants and
447/// types might be very large in large programs. In order to avoid blowing up
448/// the size of instructions in the bytecode encoding, we build a compaction
449/// table, which defines a mapping from function-local identifiers to global
450/// identifiers.
451void SlotCalculator::buildCompactionTable(const Function *F) {
452 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer88f3e072004-07-04 11:42:49 +0000453 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000454 // First step, insert the primitive types.
Reid Spencer88f3e072004-07-04 11:42:49 +0000455 CompactionTable.resize(Type::LastPrimitiveTyID+1);
456 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattner6b727592004-06-17 18:19:28 +0000457 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer88f3e072004-07-04 11:42:49 +0000458 CompactionTypes.push_back(PrimTy);
459 CompactionTypeMap[PrimTy] = i;
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000460 }
461
462 // Next, include any types used by function arguments.
463 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
464 getOrCreateCompactionTableSlot(I->getType());
465
466 // Next, find all of the types and values that are referred to by the
Reid Spencer88f3e072004-07-04 11:42:49 +0000467 // instructions in the function.
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000468 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
469 getOrCreateCompactionTableSlot(I->getType());
470 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencer30d69a52004-07-18 00:18:30 +0000471 if (isa<Constant>(I->getOperand(op)))
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000472 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner2d3a7a62004-04-27 15:13:33 +0000473 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000474 getOrCreateCompactionTableSlot(VAN->getArgType());
475 }
476
Reid Spencere7e96712004-05-25 08:53:40 +0000477 // Do the types in the symbol table
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000478 const SymbolTable &ST = F->getSymbolTable();
Reid Spencere7e96712004-05-25 08:53:40 +0000479 for (SymbolTable::type_const_iterator TI = ST.type_begin(),
480 TE = ST.type_end(); TI != TE; ++TI)
481 getOrCreateCompactionTableSlot(TI->second);
482
483 // Now do the constants and global values
484 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
485 PE = ST.plane_end(); PI != PE; ++PI)
486 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer88f3e072004-07-04 11:42:49 +0000487 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer30d69a52004-07-18 00:18:30 +0000488 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer88f3e072004-07-04 11:42:49 +0000489 getOrCreateCompactionTableSlot(VI->second);
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000490
491 // Now that we have all of the values in the table, and know what types are
492 // referenced, make sure that there is at least the zero initializer in any
493 // used type plane. Since the type was used, we will be emitting instructions
494 // to the plane even if there are no constants in it.
Reid Spencer88f3e072004-07-04 11:42:49 +0000495 CompactionTable.resize(CompactionTypes.size());
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000496 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer88f3e072004-07-04 11:42:49 +0000497 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000498 i != Type::LabelTyID) {
Reid Spencer88f3e072004-07-04 11:42:49 +0000499 const Type *Ty = CompactionTypes[i];
500 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
501 assert(Ty->getTypeID() != Type::VoidTyID);
502 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000503 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
504 }
505
506 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner083c99e2004-01-20 00:57:32 +0000507 // to emit the smallest possible binaries, do not compactify the type plane if
508 // it will not save us anything. Because we have not yet incorporated the
509 // function body itself yet, we don't know whether or not it's a good idea to
510 // compactify other planes. We will defer this decision until later.
Reid Spencer88f3e072004-07-04 11:42:49 +0000511 TypeList &GlobalTypes = Types;
Chris Lattner083c99e2004-01-20 00:57:32 +0000512
513 // All of the values types will be scrunched to the start of the types plane
514 // of the global table. Figure out just how many there are.
515 assert(!GlobalTypes.empty() && "No global types???");
516 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer88f3e072004-07-04 11:42:49 +0000517 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner083c99e2004-01-20 00:57:32 +0000518 --NumFCTypes;
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000519
Chris Lattner083c99e2004-01-20 00:57:32 +0000520 // If there are fewer that 64 types, no instructions will be exploded due to
521 // the size of the type operands. Thus there is no need to compactify types.
522 // Also, if the compaction table contains most of the entries in the global
523 // table, there really is no reason to compactify either.
524 if (NumFCTypes < 64) {
525 // Decompactifying types is tricky, because we have to move type planes all
526 // over the place. At least we don't need to worry about updating the
527 // CompactionNodeMap for non-types though.
528 std::vector<TypePlane> TmpCompactionTable;
529 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer88f3e072004-07-04 11:42:49 +0000530 TypeList TmpTypes;
531 std::swap(TmpTypes, CompactionTypes);
Chris Lattner083c99e2004-01-20 00:57:32 +0000532
533 // Move each plane back over to the uncompactified plane
Reid Spencer88f3e072004-07-04 11:42:49 +0000534 while (!TmpTypes.empty()) {
535 const Type *Ty = TmpTypes.back();
536 TmpTypes.pop_back();
537 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000538
Reid Spencer88f3e072004-07-04 11:42:49 +0000539 // Find the global slot number for this type.
540 int TySlot = getSlot(Ty);
541 assert(TySlot != -1 && "Type doesn't exist in global table?");
542
543 // Now we know where to put the compaction table plane.
544 if (CompactionTable.size() <= unsigned(TySlot))
545 CompactionTable.resize(TySlot+1);
546 // Move the plane back into the compaction table.
547 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000548
Reid Spencer88f3e072004-07-04 11:42:49 +0000549 // And remove the empty plane we just moved in.
550 TmpCompactionTable.pop_back();
Chris Lattner083c99e2004-01-20 00:57:32 +0000551 }
552 }
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000553}
554
Chris Lattner083c99e2004-01-20 00:57:32 +0000555
556/// pruneCompactionTable - Once the entire function being processed has been
557/// incorporated into the current compaction table, look over the compaction
558/// table and check to see if there are any values whose compaction will not
559/// save us any space in the bytecode file. If compactifying these values
560/// serves no purpose, then we might as well not even emit the compactification
561/// information to the bytecode file, saving a bit more space.
562///
563/// Note that the type plane has already been compactified if possible.
564///
565void SlotCalculator::pruneCompactionTable() {
Reid Spencer88f3e072004-07-04 11:42:49 +0000566 TypeList &TyPlane = CompactionTypes;
Chris Lattner083c99e2004-01-20 00:57:32 +0000567 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer88f3e072004-07-04 11:42:49 +0000568 if (!CompactionTable[ctp].empty()) {
Chris Lattner083c99e2004-01-20 00:57:32 +0000569 TypePlane &CPlane = CompactionTable[ctp];
570 unsigned GlobalSlot = ctp;
571 if (!TyPlane.empty())
572 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
573
574 if (GlobalSlot >= Table.size())
575 Table.resize(GlobalSlot+1);
576 TypePlane &GPlane = Table[GlobalSlot];
577
578 unsigned ModLevel = getModuleLevel(ctp);
579 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
580
581 // If the maximum index required if all entries in this plane were merged
582 // into the global plane is less than 64, go ahead and eliminate the
583 // plane.
584 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
585
586 // If there are no function-local values defined, and the maximum
587 // referenced global entry is less than 64, we don't need to compactify.
588 if (!PrunePlane && NumFunctionObjs == 0) {
589 unsigned MaxIdx = 0;
590 for (unsigned i = 0; i != ModLevel; ++i) {
591 unsigned Idx = NodeMap[CPlane[i]];
592 if (Idx > MaxIdx) MaxIdx = Idx;
593 }
594 PrunePlane = MaxIdx < 64;
595 }
596
597 // Ok, finally, if we decided to prune this plane out of the compaction
598 // table, do so now.
599 if (PrunePlane) {
600 TypePlane OldPlane;
601 std::swap(OldPlane, CPlane);
602
603 // Loop over the function local objects, relocating them to the global
604 // table plane.
605 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
606 const Value *V = OldPlane[i];
607 CompactionNodeMap.erase(V);
608 assert(NodeMap.count(V) == 0 && "Value already in table??");
609 getOrCreateSlot(V);
610 }
611
612 // For compactified global values, just remove them from the compaction
613 // node map.
614 for (unsigned i = 0; i != ModLevel; ++i)
615 CompactionNodeMap.erase(OldPlane[i]);
616
617 // Update the new modulelevel for this plane.
618 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
619 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
620 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
621 }
622 }
623}
624
Chris Lattner21699e82004-01-15 18:47:15 +0000625int SlotCalculator::getSlot(const Value *V) const {
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000626 // If there is a CompactionTable active...
627 if (!CompactionNodeMap.empty()) {
628 std::map<const Value*, unsigned>::const_iterator I =
629 CompactionNodeMap.find(V);
630 if (I != CompactionNodeMap.end())
631 return (int)I->second;
Chris Lattner083c99e2004-01-20 00:57:32 +0000632 // Otherwise, if it's not in the compaction table, it must be in a
633 // non-compactified plane.
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000634 }
635
Chris Lattner21699e82004-01-15 18:47:15 +0000636 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
637 if (I != NodeMap.end())
638 return (int)I->second;
639
Chris Lattner21699e82004-01-15 18:47:15 +0000640 return -1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641}
642
Reid Spencer88f3e072004-07-04 11:42:49 +0000643int SlotCalculator::getSlot(const Type*T) const {
644 // If there is a CompactionTable active...
645 if (!CompactionTypeMap.empty()) {
646 std::map<const Type*, unsigned>::const_iterator I =
647 CompactionTypeMap.find(T);
648 if (I != CompactionTypeMap.end())
649 return (int)I->second;
650 // Otherwise, if it's not in the compaction table, it must be in a
651 // non-compactified plane.
652 }
653
654 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
655 if (I != TypeMap.end())
656 return (int)I->second;
657
658 return -1;
659}
Chris Lattner1e6912a2001-09-07 16:31:52 +0000660
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000661int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner083c99e2004-01-20 00:57:32 +0000662 if (V->getType() == Type::VoidTy) return -1;
663
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000664 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner1e6912a2001-09-07 16:31:52 +0000665 if (SlotNo != -1) return SlotNo;
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000666
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000667 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000668 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000669 assert(CompactionNodeMap.empty() &&
670 "All needed constants should be in the compaction map already!");
671
Reid Spencer0aff01a2004-05-26 07:37:11 +0000672 // Do not index the characters that make up constant strings. We emit
673 // constant strings as special entities that don't require their
674 // individual characters to be emitted.
675 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerde363042004-01-14 23:34:39 +0000676 // This makes sure that if a constant has uses (for example an array of
677 // const ints), that they are inserted also.
678 //
679 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
680 I != E; ++I)
681 getOrCreateSlot(*I);
682 } else {
683 assert(ModuleLevel.empty() &&
684 "How can a constant string be directly accessed in a function?");
685 // Otherwise, if we are emitting a bytecode file and this IS a string,
686 // remember it.
687 if (!C->isNullValue())
688 ConstantStrings.push_back(cast<ConstantArray>(C));
689 }
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000690 }
691
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000692 return insertValue(V);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000693}
694
Reid Spencer88f3e072004-07-04 11:42:49 +0000695int SlotCalculator::getOrCreateSlot(const Type* T) {
696 int SlotNo = getSlot(T); // Check to see if it's already in!
697 if (SlotNo != -1) return SlotNo;
698 return insertType(T);
699}
Chris Lattner1e6912a2001-09-07 16:31:52 +0000700
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000701int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner1e6912a2001-09-07 16:31:52 +0000702 assert(D && "Can't insert a null value!");
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000703 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000704
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000705 // If we are building a compaction map, and if this plane is being compacted,
706 // insert the value into the compaction map, not into the global map.
707 if (!CompactionNodeMap.empty()) {
708 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencer30d69a52004-07-18 00:18:30 +0000709 assert(!isa<Constant>(D) &&
710 "Types, constants, and globals should be in global table!");
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000711
Chris Lattner083c99e2004-01-20 00:57:32 +0000712 int Plane = getSlot(D->getType());
713 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
714 "Didn't find value type!");
715 if (!CompactionTable[Plane].empty())
716 return getOrCreateCompactionTableSlot(D);
Chris Lattnerca1f8af2004-01-18 21:07:07 +0000717 }
718
Chris Lattner2f7c9632001-06-06 20:29:01 +0000719 // If this node does not contribute to a plane, or if the node has a
Chris Lattner1e6912a2001-09-07 16:31:52 +0000720 // name and we don't want names, then ignore the silly node... Note that types
721 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000722 //
Chris Lattner89d4dfb2001-07-26 16:28:37 +0000723 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer0aff01a2004-05-26 07:37:11 +0000724 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000725 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner1e6912a2001-09-07 16:31:52 +0000726 return -1; // We do need types unconditionally though
727 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000728
Chris Lattner1e6912a2001-09-07 16:31:52 +0000729 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000730 return doInsertValue(D);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000731}
732
Reid Spencer88f3e072004-07-04 11:42:49 +0000733int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
734 assert(Ty && "Can't insert a null type!");
735 assert(getSlot(Ty) == -1 && "Type is already in the table!");
736
737 // If we are building a compaction map, and if this plane is being compacted,
738 // insert the value into the compaction map, not into the global map.
739 if (!CompactionTypeMap.empty()) {
740 getOrCreateCompactionTableSlot(Ty);
741 }
742
743 // Insert the current type before any subtypes. This is important because
744 // recursive types elements are inserted in a bottom up order. Changing
745 // this here can break things. For example:
746 //
747 // global { \2 * } { { \2 }* null }
748 //
749 int ResultSlot = doInsertType(Ty);
750 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
751 ResultSlot << "\n");
752
753 // Loop over any contained types in the definition... in post
754 // order.
755 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
756 I != E; ++I) {
757 if (*I != Ty) {
758 const Type *SubTy = *I;
759 // If we haven't seen this sub type before, add it to our type table!
760 if (getSlot(SubTy) == -1) {
761 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattner420fd142004-07-12 20:29:52 +0000762 doInsertType(SubTy);
Reid Spencer30d69a52004-07-18 00:18:30 +0000763 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer88f3e072004-07-04 11:42:49 +0000764 }
765 }
766 }
767 return ResultSlot;
768}
769
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000770// doInsertValue - This is a small helper function to be called only
771// be insertValue.
Chris Lattner1e6912a2001-09-07 16:31:52 +0000772//
Alkis Evlogimenos8faf8d92003-10-17 02:02:40 +0000773int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000774 const Type *Typ = D->getType();
Chris Lattner89d4dfb2001-07-26 16:28:37 +0000775 unsigned Ty;
776
777 // Used for debugging DefSlot=-1 assertion...
778 //if (Typ == Type::TypeTy)
Chris Lattner8f191122001-10-01 18:26:53 +0000779 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner89d4dfb2001-07-26 16:28:37 +0000780
Chris Lattner2f7c9632001-06-06 20:29:01 +0000781 if (Typ->isDerivedType()) {
Chris Lattner083c99e2004-01-20 00:57:32 +0000782 int ValSlot;
783 if (CompactionTable.empty())
784 ValSlot = getSlot(Typ);
785 else
786 ValSlot = getGlobalSlot(Typ);
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000787 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner1e6912a2001-09-07 16:31:52 +0000788 // Nope, this is the first we have seen the type, process it.
Reid Spencer88f3e072004-07-04 11:42:49 +0000789 ValSlot = insertType(Typ, true);
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000790 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000791 }
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000792 Ty = (unsigned)ValSlot;
Chris Lattner89d4dfb2001-07-26 16:28:37 +0000793 } else {
Chris Lattner6b727592004-06-17 18:19:28 +0000794 Ty = Typ->getTypeID();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000795 }
796
797 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
798 Table.resize(Ty+1, TypePlane());
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000799
800 // If this is the first value to get inserted into the type plane, make sure
801 // to insert the implicit null value...
Reid Spencer0aff01a2004-05-26 07:37:11 +0000802 if (Table[Ty].empty() && hasNullValue(Ty)) {
Chris Lattnerfaca62c2003-03-19 20:57:22 +0000803 Value *ZeroInitializer = Constant::getNullValue(Typ);
804
805 // If we are pushing zeroinit, it will be handled below.
806 if (D != ZeroInitializer) {
807 Table[Ty].push_back(ZeroInitializer);
808 NodeMap[ZeroInitializer] = 0;
809 }
810 }
811
Chris Lattner1e6912a2001-09-07 16:31:52 +0000812 // Insert node into table and NodeMap...
813 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000814 Table[Ty].push_back(D);
Chris Lattner1e6912a2001-09-07 16:31:52 +0000815
Chris Lattner1907f4e2001-10-13 06:35:09 +0000816 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer88f3e072004-07-04 11:42:49 +0000817 DestSlot << " [");
Chris Lattner62b7fd12002-04-07 20:49:59 +0000818 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattner3462ae32001-12-03 22:26:30 +0000819 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer88f3e072004-07-04 11:42:49 +0000820 (isa<Function>(D) ? "F" : "o"))));
Chris Lattner1907f4e2001-10-13 06:35:09 +0000821 SC_DEBUG("]\n");
Chris Lattner1e6912a2001-09-07 16:31:52 +0000822 return (int)DestSlot;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000823}
Reid Spencer88f3e072004-07-04 11:42:49 +0000824
825// doInsertType - This is a small helper function to be called only
826// be insertType.
827//
828int SlotCalculator::doInsertType(const Type *Ty) {
829
830 // Insert node into table and NodeMap...
831 unsigned DestSlot = TypeMap[Ty] = Types.size();
832 Types.push_back(Ty);
833
834 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
835 return (int)DestSlot;
836}
837
838// vim: sw=2 ai