blob: f8d4b417f1d3854a37bb5b7d0a271f5164c1a2b9 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
John Criswellb576c942003-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 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"
Chris Lattnerdcea6302004-01-14 23:34:39 +000020#include "llvm/iOther.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000021#include "llvm/Function.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000022#include "llvm/Module.h"
Chris Lattner9a297902001-09-07 16:31:52 +000023#include "llvm/SymbolTable.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000024#include "llvm/Type.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000025#include "llvm/Analysis/ConstantsScanner.h"
Alkis Evlogimenos088eb452003-10-31 03:02:34 +000026#include "Support/PostOrderIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000028#include <algorithm>
Reid Spencer24ab28f2004-07-04 11:42:49 +000029#include <functional>
30
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
Reid Spencer24ab28f2004-07-04 11:42:49 +000034#include <iostream>
Chris Lattner3413d152003-03-19 20:57:22 +000035#define SC_DEBUG(X) std::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!");
80 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
81 V = CPR->getValue();
82 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner68e3dbc2004-01-20 00:57:32 +000083 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000084 return I->second;
85}
86
Reid Spencer24ab28f2004-07-04 11:42:49 +000087unsigned SlotCalculator::getGlobalSlot(const Type* T) const {
88 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
89 assert(I != TypeMap.end() && "Didn't find global slot entry!");
90 return I->second;
91}
92
Chris Lattner68e3dbc2004-01-20 00:57:32 +000093SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +000094 if (CompactionTable.empty()) { // No compaction table active?
95 // fall out
96 } else if (!CompactionTable[Plane].empty()) { // Compaction table active.
97 assert(Plane < CompactionTable.size());
98 return CompactionTable[Plane];
99 } else {
100 // Final case: compaction table active, but this plane is not
101 // compactified. If the type plane is compactified, unmap back to the
102 // global type plane corresponding to "Plane".
Reid Spencer24ab28f2004-07-04 11:42:49 +0000103 if (!CompactionTypes.empty()) {
104 const Type *Ty = CompactionTypes[Plane];
105 TypeMapType::iterator It = TypeMap.find(Ty);
106 assert(It != TypeMap.end() && "Type not in global constant map?");
107 Plane = It->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000108 }
109 }
110
111 // Okay we are just returning an entry out of the main Table. Make sure the
112 // plane exists and return it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000113 if (Plane >= Table.size())
114 Table.resize(Plane+1);
115 return Table[Plane];
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000116}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000117
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000118// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +0000119// types that are available.
120//
121void SlotCalculator::processModule() {
122 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000123
Chris Lattner3413d152003-03-19 20:57:22 +0000124 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +0000125 //
126 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
Chris Lattner479b54b2002-10-14 00:48:57 +0000127 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000128 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000129
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000130 // Scavenge the types out of the functions, then add the functions themselves
131 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000132 //
Chris Lattner3413d152003-03-19 20:57:22 +0000133 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
134 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000135 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000136
Chris Lattner3413d152003-03-19 20:57:22 +0000137 // Add all of the module level constants used as initializers
138 //
139 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
140 I != E; ++I)
141 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000142 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000143
Chris Lattnerdcea6302004-01-14 23:34:39 +0000144 // Now that all global constants have been added, rearrange constant planes
145 // that contain constant strings so that the strings occur at the start of the
146 // plane, not somewhere in the middle.
147 //
Reid Spencer798ff642004-05-26 07:37:11 +0000148 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
149 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
150 if (AT->getElementType() == Type::SByteTy ||
Reid Spencer24ab28f2004-07-04 11:42:49 +0000151 AT->getElementType() == Type::UByteTy) {
152 TypePlane &Plane = Table[plane];
153 unsigned FirstNonStringID = 0;
154 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
155 if (isa<ConstantAggregateZero>(Plane[i]) ||
156 cast<ConstantArray>(Plane[i])->isString()) {
157 // Check to see if we have to shuffle this string around. If not,
158 // don't do anything.
159 if (i != FirstNonStringID) {
160 // Swap the plane entries....
161 std::swap(Plane[i], Plane[FirstNonStringID]);
162
163 // Keep the NodeMap up to date.
164 NodeMap[Plane[i]] = i;
165 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
166 }
167 ++FirstNonStringID;
168 }
Reid Spencer798ff642004-05-26 07:37:11 +0000169 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000170 }
171
Chris Lattnera14b0d42004-01-10 23:46:13 +0000172 // If we are emitting a bytecode file, scan all of the functions for their
173 // constants, which allows us to emit more compact modules. This is optional,
174 // and is just used to compactify the constants used by different functions
175 // together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000176 //
177 // This functionality is completely optional for the bytecode writer, but
178 // tends to produce smaller bytecode files. This should not be used in the
179 // future by clients that want to, for example, build and emit functions on
180 // the fly. For now, however, it is unconditionally enabled when building
181 // bytecode information.
182 //
Reid Spencer798ff642004-05-26 07:37:11 +0000183 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000184
Reid Spencer798ff642004-05-26 07:37:11 +0000185 SC_DEBUG("Inserting function constants:\n");
186 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
187 F != E; ++F) {
188 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
189 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000190 if (isa<Constant>(I->getOperand(op)))
191 getOrCreateSlot(I->getOperand(op));
Reid Spencer798ff642004-05-26 07:37:11 +0000192 getOrCreateSlot(I->getType());
193 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000194 getOrCreateSlot(VAN->getArgType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000195 }
Reid Spencer798ff642004-05-26 07:37:11 +0000196 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000197 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000198
Chris Lattner70cc3392001-09-10 07:58:01 +0000199 // Insert constants that are named at module level into the slot pool so that
200 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000201 SC_DEBUG("Inserting SymbolTable values:\n");
202 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000203
Chris Lattnera14b0d42004-01-10 23:46:13 +0000204 // Now that we have collected together all of the information relevant to the
205 // module, compactify the type table if it is particularly big and outputting
206 // a bytecode file. The basic problem we run into is that some programs have
207 // a large number of types, which causes the type field to overflow its size,
208 // which causes instructions to explode in size (particularly call
209 // instructions). To avoid this behavior, we "sort" the type table so that
210 // all non-value types are pushed to the end of the type table, giving nice
211 // low numbers to the types that can be used by instructions, thus reducing
212 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000213 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000214 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000215 for (unsigned i = 0, e = Types.size(); i != e; ++i)
216 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000217 // Check to see if we have to shuffle this type around. If not, don't
218 // do anything.
219 if (i != FirstNonValueTypeID) {
220 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000221 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000222
Reid Spencer24ab28f2004-07-04 11:42:49 +0000223 // Keep the TypeMap up to date.
224 TypeMap[Types[i]] = i;
225 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000226
227 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000228 if (Table.size() > FirstNonValueTypeID) {
229 if (Table.size() <= i) Table.resize(i+1);
230 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000231 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000232 }
233 ++FirstNonValueTypeID;
234 }
235 }
236
Chris Lattner9a297902001-09-07 16:31:52 +0000237 SC_DEBUG("end processModule!\n");
238}
239
240// processSymbolTable - Insert all of the values in the specified symbol table
241// into the values table...
242//
243void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000244 // Do the types first.
245 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
246 TE = ST->type_end(); TI != TE; ++TI )
247 getOrCreateSlot(TI->second);
248
249 // Now do the values.
250 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
251 PE = ST->plane_end(); PI != PE; ++PI)
252 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000253 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000254 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000255}
256
257void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000258 // Do the types first
259 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
260 TE = ST->type_end(); TI != TE; ++TI )
261 getOrCreateSlot(TI->second);
262
263 // Now do the constant values in all planes
264 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
265 PE = ST->plane_end(); PI != PE; ++PI)
266 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000267 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000268 if (isa<Constant>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000269 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000270}
271
272
Chris Lattnerce439b52003-10-20 19:10:06 +0000273void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000274 assert((ModuleLevel.size() == 0 ||
275 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000276
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000277 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000278
Chris Lattner614cdcd2004-01-18 21:07:07 +0000279 // If we emitted all of the function constants, build a compaction table.
Reid Spencer798ff642004-05-26 07:37:11 +0000280 if ( ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000281 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000282
283 // Update the ModuleLevel entries to be accurate.
284 ModuleLevel.resize(getNumPlanes());
285 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
286 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000287 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000288
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000289 // Iterate over function arguments, adding them to the value table...
Chris Lattnerce439b52003-10-20 19:10:06 +0000290 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000291 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000292
Reid Spencer798ff642004-05-26 07:37:11 +0000293 if ( !ModuleContainsAllFunctionConstants ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000294 // Iterate over all of the instructions in the function, looking for
295 // constant values that are referenced. Add these to the value pools
296 // before any nonconstant values. This will be turned into the constant
297 // pool for the bytecode writer.
298 //
299
300 // Emit all of the constants that are being used by the instructions in
301 // the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000302 constant_iterator CI = constant_begin(F);
303 constant_iterator CE = constant_end(F);
304 while ( CI != CE ) {
305 this->getOrCreateSlot(*CI);
306 ++CI;
307 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000308
Chris Lattner9a297902001-09-07 16:31:52 +0000309 // If there is a symbol table, it is possible that the user has names for
310 // constants that are not being used. In this case, we will have problems
311 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000312 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000313 // constants now.
314 //
Chris Lattnerce439b52003-10-20 19:10:06 +0000315 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000316 }
317
Chris Lattner9a297902001-09-07 16:31:52 +0000318 SC_DEBUG("Inserting Instructions:\n");
319
320 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000321 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
322 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000323 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
324 getOrCreateSlot(I);
Chris Lattner2765c412003-10-21 17:40:54 +0000325 if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
326 getOrCreateSlot(VAN->getArgType());
Chris Lattner389dbfb2003-10-21 17:39:59 +0000327 }
Chris Lattner9a297902001-09-07 16:31:52 +0000328 }
329
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000330 // If we are building a compaction table, prune out planes that do not benefit
331 // from being compactified.
332 if (!CompactionTable.empty())
333 pruneCompactionTable();
334
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000335 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000336}
337
Chris Lattnerb5794002002-04-07 22:49:37 +0000338void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000339 assert((ModuleLevel.size() != 0 ||
340 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000341 unsigned NumModuleTypes = ModuleLevel.size();
342
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000343 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000344
Chris Lattner614cdcd2004-01-18 21:07:07 +0000345 // First, free the compaction map if used.
346 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000347 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000348
349 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000350 for (unsigned i = 0; i != NumModuleTypes; ++i) {
351 // Size of plane before function came
352 unsigned ModuleLev = getModuleLevel(i);
353 assert(int(ModuleLev) >= 0 && "BAD!");
354
355 TypePlane &Plane = getPlane(i);
356
357 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
358 while (Plane.size() != ModuleLev) {
359 assert(!isa<GlobalValue>(Plane.back()) &&
360 "Functions cannot define globals!");
361 NodeMap.erase(Plane.back()); // Erase from nodemap
362 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000363 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000364 }
Chris Lattner00950542001-06-06 20:29:01 +0000365
366 // We don't need this state anymore, free it up.
367 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000368 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000369
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000370 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000371 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000372 if (!CompactionTable.empty()) {
373 CompactionTable.clear();
374 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000375 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000376 TypePlane &Plane = Table.back();
377 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
378 << Plane.size() << "\n");
379 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000380 assert(!isa<GlobalValue>(Plane.back()) &&
381 "Functions cannot define globals!");
382 NodeMap.erase(Plane.back()); // Erase from nodemap
383 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000384 }
385
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000386 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000387 }
Chris Lattner00950542001-06-06 20:29:01 +0000388 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000389
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000390 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000391}
392
Chris Lattner614cdcd2004-01-18 21:07:07 +0000393static inline bool hasNullValue(unsigned TyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000394 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000395}
396
397/// getOrCreateCompactionTableSlot - This method is used to build up the initial
398/// approximation of the compaction table.
399unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
Chris Lattner71151ae2004-02-09 00:15:41 +0000400 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
401 V = CPR->getValue();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000402 std::map<const Value*, unsigned>::iterator I =
403 CompactionNodeMap.lower_bound(V);
404 if (I != CompactionNodeMap.end() && I->first == V)
405 return I->second; // Already exists?
406
407 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000408 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000409 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000410 Ty = getOrCreateCompactionTableSlot(V->getType());
411 else // If the type plane was decompactified, use the global plane ID
412 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000413 if (CompactionTable.size() <= Ty)
414 CompactionTable.resize(Ty+1);
415
Chris Lattner614cdcd2004-01-18 21:07:07 +0000416 TypePlane &TyPlane = CompactionTable[Ty];
417
418 // Make sure to insert the null entry if the thing we are inserting is not a
419 // null constant.
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000420 if (TyPlane.empty() && hasNullValue(V->getType()->getTypeID())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000421 Value *ZeroInitializer = Constant::getNullValue(V->getType());
422 if (V != ZeroInitializer) {
423 TyPlane.push_back(ZeroInitializer);
424 CompactionNodeMap[ZeroInitializer] = 0;
425 }
426 }
427
428 unsigned SlotNo = TyPlane.size();
429 TyPlane.push_back(V);
430 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
431 return SlotNo;
432}
433
Reid Spencer24ab28f2004-07-04 11:42:49 +0000434/// getOrCreateCompactionTableSlot - This method is used to build up the initial
435/// approximation of the compaction table.
436unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
437 std::map<const Type*, unsigned>::iterator I =
438 CompactionTypeMap.lower_bound(T);
439 if (I != CompactionTypeMap.end() && I->first == T)
440 return I->second; // Already exists?
441
442 unsigned SlotNo = CompactionTypes.size();
443 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
444 CompactionTypes.push_back(T);
445 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
446 return SlotNo;
447}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000448
449/// buildCompactionTable - Since all of the function constants and types are
450/// stored in the module-level constant table, we don't need to emit a function
451/// constant table. Also due to this, the indices for various constants and
452/// types might be very large in large programs. In order to avoid blowing up
453/// the size of instructions in the bytecode encoding, we build a compaction
454/// table, which defines a mapping from function-local identifiers to global
455/// identifiers.
456void SlotCalculator::buildCompactionTable(const Function *F) {
457 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000458 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000459 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000460 CompactionTable.resize(Type::LastPrimitiveTyID+1);
461 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000462 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000463 CompactionTypes.push_back(PrimTy);
464 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000465 }
466
467 // Next, include any types used by function arguments.
468 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
469 getOrCreateCompactionTableSlot(I->getType());
470
471 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000472 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000473 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
474 getOrCreateCompactionTableSlot(I->getType());
475 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
476 if (isa<Constant>(I->getOperand(op)) ||
477 isa<GlobalValue>(I->getOperand(op)))
478 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner6ffe5512004-04-27 15:13:33 +0000479 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000480 getOrCreateCompactionTableSlot(VAN->getArgType());
481 }
482
Reid Spencer9231ac82004-05-25 08:53:40 +0000483 // Do the types in the symbol table
Chris Lattner614cdcd2004-01-18 21:07:07 +0000484 const SymbolTable &ST = F->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000485 for (SymbolTable::type_const_iterator TI = ST.type_begin(),
486 TE = ST.type_end(); TI != TE; ++TI)
487 getOrCreateCompactionTableSlot(TI->second);
488
489 // Now do the constants and global values
490 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
491 PE = ST.plane_end(); PI != PE; ++PI)
492 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000493 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000494 if (isa<Constant>(VI->second) || isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000495 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000496
497 // Now that we have all of the values in the table, and know what types are
498 // referenced, make sure that there is at least the zero initializer in any
499 // used type plane. Since the type was used, we will be emitting instructions
500 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000501 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000502 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000503 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000504 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000505 const Type *Ty = CompactionTypes[i];
506 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
507 assert(Ty->getTypeID() != Type::VoidTyID);
508 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000509 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
510 }
511
512 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000513 // to emit the smallest possible binaries, do not compactify the type plane if
514 // it will not save us anything. Because we have not yet incorporated the
515 // function body itself yet, we don't know whether or not it's a good idea to
516 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000517 TypeList &GlobalTypes = Types;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000518
519 // All of the values types will be scrunched to the start of the types plane
520 // of the global table. Figure out just how many there are.
521 assert(!GlobalTypes.empty() && "No global types???");
522 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000523 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000524 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000525
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000526 // If there are fewer that 64 types, no instructions will be exploded due to
527 // the size of the type operands. Thus there is no need to compactify types.
528 // Also, if the compaction table contains most of the entries in the global
529 // table, there really is no reason to compactify either.
530 if (NumFCTypes < 64) {
531 // Decompactifying types is tricky, because we have to move type planes all
532 // over the place. At least we don't need to worry about updating the
533 // CompactionNodeMap for non-types though.
534 std::vector<TypePlane> TmpCompactionTable;
535 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000536 TypeList TmpTypes;
537 std::swap(TmpTypes, CompactionTypes);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000538
539 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000540 while (!TmpTypes.empty()) {
541 const Type *Ty = TmpTypes.back();
542 TmpTypes.pop_back();
543 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000544
Reid Spencer24ab28f2004-07-04 11:42:49 +0000545 // Find the global slot number for this type.
546 int TySlot = getSlot(Ty);
547 assert(TySlot != -1 && "Type doesn't exist in global table?");
548
549 // Now we know where to put the compaction table plane.
550 if (CompactionTable.size() <= unsigned(TySlot))
551 CompactionTable.resize(TySlot+1);
552 // Move the plane back into the compaction table.
553 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000554
Reid Spencer24ab28f2004-07-04 11:42:49 +0000555 // And remove the empty plane we just moved in.
556 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000557 }
558 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000559}
560
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000561
562/// pruneCompactionTable - Once the entire function being processed has been
563/// incorporated into the current compaction table, look over the compaction
564/// table and check to see if there are any values whose compaction will not
565/// save us any space in the bytecode file. If compactifying these values
566/// serves no purpose, then we might as well not even emit the compactification
567/// information to the bytecode file, saving a bit more space.
568///
569/// Note that the type plane has already been compactified if possible.
570///
571void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000572 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000573 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000574 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000575 TypePlane &CPlane = CompactionTable[ctp];
576 unsigned GlobalSlot = ctp;
577 if (!TyPlane.empty())
578 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
579
580 if (GlobalSlot >= Table.size())
581 Table.resize(GlobalSlot+1);
582 TypePlane &GPlane = Table[GlobalSlot];
583
584 unsigned ModLevel = getModuleLevel(ctp);
585 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
586
587 // If the maximum index required if all entries in this plane were merged
588 // into the global plane is less than 64, go ahead and eliminate the
589 // plane.
590 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
591
592 // If there are no function-local values defined, and the maximum
593 // referenced global entry is less than 64, we don't need to compactify.
594 if (!PrunePlane && NumFunctionObjs == 0) {
595 unsigned MaxIdx = 0;
596 for (unsigned i = 0; i != ModLevel; ++i) {
597 unsigned Idx = NodeMap[CPlane[i]];
598 if (Idx > MaxIdx) MaxIdx = Idx;
599 }
600 PrunePlane = MaxIdx < 64;
601 }
602
603 // Ok, finally, if we decided to prune this plane out of the compaction
604 // table, do so now.
605 if (PrunePlane) {
606 TypePlane OldPlane;
607 std::swap(OldPlane, CPlane);
608
609 // Loop over the function local objects, relocating them to the global
610 // table plane.
611 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
612 const Value *V = OldPlane[i];
613 CompactionNodeMap.erase(V);
614 assert(NodeMap.count(V) == 0 && "Value already in table??");
615 getOrCreateSlot(V);
616 }
617
618 // For compactified global values, just remove them from the compaction
619 // node map.
620 for (unsigned i = 0; i != ModLevel; ++i)
621 CompactionNodeMap.erase(OldPlane[i]);
622
623 // Update the new modulelevel for this plane.
624 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
625 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
626 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
627 }
628 }
629}
630
Chris Lattner8c202cd2004-01-15 18:47:15 +0000631int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000632 // If there is a CompactionTable active...
633 if (!CompactionNodeMap.empty()) {
634 std::map<const Value*, unsigned>::const_iterator I =
635 CompactionNodeMap.find(V);
636 if (I != CompactionNodeMap.end())
637 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000638 // Otherwise, if it's not in the compaction table, it must be in a
639 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000640 }
641
Chris Lattner8c202cd2004-01-15 18:47:15 +0000642 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
643 if (I != NodeMap.end())
644 return (int)I->second;
645
646 // Do not number ConstantPointerRef's at all. They are an abomination.
647 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
648 return getSlot(CPR->getValue());
649
650 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000651}
652
Reid Spencer24ab28f2004-07-04 11:42:49 +0000653int SlotCalculator::getSlot(const Type*T) const {
654 // If there is a CompactionTable active...
655 if (!CompactionTypeMap.empty()) {
656 std::map<const Type*, unsigned>::const_iterator I =
657 CompactionTypeMap.find(T);
658 if (I != CompactionTypeMap.end())
659 return (int)I->second;
660 // Otherwise, if it's not in the compaction table, it must be in a
661 // non-compactified plane.
662 }
663
664 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
665 if (I != TypeMap.end())
666 return (int)I->second;
667
668 return -1;
669}
Chris Lattner9a297902001-09-07 16:31:52 +0000670
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000671int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000672 if (V->getType() == Type::VoidTy) return -1;
673
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000674 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000675 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000676
Chris Lattner8c202cd2004-01-15 18:47:15 +0000677 // Do not number ConstantPointerRef's at all. They are an abomination.
678 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
679 return getOrCreateSlot(CPR->getValue());
680
Chris Lattner614cdcd2004-01-18 21:07:07 +0000681 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000682 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000683 assert(CompactionNodeMap.empty() &&
684 "All needed constants should be in the compaction map already!");
685
Reid Spencer798ff642004-05-26 07:37:11 +0000686 // Do not index the characters that make up constant strings. We emit
687 // constant strings as special entities that don't require their
688 // individual characters to be emitted.
689 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000690 // This makes sure that if a constant has uses (for example an array of
691 // const ints), that they are inserted also.
692 //
693 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
694 I != E; ++I)
695 getOrCreateSlot(*I);
696 } else {
697 assert(ModuleLevel.empty() &&
698 "How can a constant string be directly accessed in a function?");
699 // Otherwise, if we are emitting a bytecode file and this IS a string,
700 // remember it.
701 if (!C->isNullValue())
702 ConstantStrings.push_back(cast<ConstantArray>(C));
703 }
Chris Lattner3413d152003-03-19 20:57:22 +0000704 }
705
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000706 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000707}
708
Reid Spencer24ab28f2004-07-04 11:42:49 +0000709int SlotCalculator::getOrCreateSlot(const Type* T) {
710 int SlotNo = getSlot(T); // Check to see if it's already in!
711 if (SlotNo != -1) return SlotNo;
712 return insertType(T);
713}
Chris Lattner9a297902001-09-07 16:31:52 +0000714
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000715int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000716 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000717 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000718
Chris Lattner614cdcd2004-01-18 21:07:07 +0000719 // If we are building a compaction map, and if this plane is being compacted,
720 // insert the value into the compaction map, not into the global map.
721 if (!CompactionNodeMap.empty()) {
722 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencer24ab28f2004-07-04 11:42:49 +0000723 assert(!isa<Constant>(D) && !isa<GlobalValue>(D) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000724 "Types, constants, and globals should be in global SymTab!");
725
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000726 int Plane = getSlot(D->getType());
727 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
728 "Didn't find value type!");
729 if (!CompactionTable[Plane].empty())
730 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000731 }
732
Chris Lattner00950542001-06-06 20:29:01 +0000733 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000734 // name and we don't want names, then ignore the silly node... Note that types
735 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000736 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000737 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000738 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000739 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000740 return -1; // We do need types unconditionally though
741 }
Chris Lattner00950542001-06-06 20:29:01 +0000742
Chris Lattner9a297902001-09-07 16:31:52 +0000743 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000744 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000745}
746
Reid Spencer24ab28f2004-07-04 11:42:49 +0000747int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
748 assert(Ty && "Can't insert a null type!");
749 assert(getSlot(Ty) == -1 && "Type is already in the table!");
750
751 // If we are building a compaction map, and if this plane is being compacted,
752 // insert the value into the compaction map, not into the global map.
753 if (!CompactionTypeMap.empty()) {
754 getOrCreateCompactionTableSlot(Ty);
755 }
756
757 // Insert the current type before any subtypes. This is important because
758 // recursive types elements are inserted in a bottom up order. Changing
759 // this here can break things. For example:
760 //
761 // global { \2 * } { { \2 }* null }
762 //
763 int ResultSlot = doInsertType(Ty);
764 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
765 ResultSlot << "\n");
766
767 // Loop over any contained types in the definition... in post
768 // order.
769 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
770 I != E; ++I) {
771 if (*I != Ty) {
772 const Type *SubTy = *I;
773 // If we haven't seen this sub type before, add it to our type table!
774 if (getSlot(SubTy) == -1) {
775 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
776 int Slot = doInsertType(SubTy);
777 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
778 " slot=" << Slot << "\n");
779 }
780 }
781 }
782 return ResultSlot;
783}
784
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000785// doInsertValue - This is a small helper function to be called only
786// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000787//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000788int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000789 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000790 unsigned Ty;
791
792 // Used for debugging DefSlot=-1 assertion...
793 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000794 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000795
Chris Lattner00950542001-06-06 20:29:01 +0000796 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000797 int ValSlot;
798 if (CompactionTable.empty())
799 ValSlot = getSlot(Typ);
800 else
801 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000802 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000803 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000804 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000805 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000806 }
Chris Lattner3413d152003-03-19 20:57:22 +0000807 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000808 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000809 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000810 }
811
812 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
813 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000814
815 // If this is the first value to get inserted into the type plane, make sure
816 // to insert the implicit null value...
Reid Spencer798ff642004-05-26 07:37:11 +0000817 if (Table[Ty].empty() && hasNullValue(Ty)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000818 Value *ZeroInitializer = Constant::getNullValue(Typ);
819
820 // If we are pushing zeroinit, it will be handled below.
821 if (D != ZeroInitializer) {
822 Table[Ty].push_back(ZeroInitializer);
823 NodeMap[ZeroInitializer] = 0;
824 }
825 }
826
Chris Lattner9a297902001-09-07 16:31:52 +0000827 // Insert node into table and NodeMap...
828 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000829 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000830
Chris Lattnerf1fef652001-10-13 06:35:09 +0000831 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000832 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000833 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000834 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000835 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000836 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000837 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000838}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000839
840// doInsertType - This is a small helper function to be called only
841// be insertType.
842//
843int SlotCalculator::doInsertType(const Type *Ty) {
844
845 // Insert node into table and NodeMap...
846 unsigned DestSlot = TypeMap[Ty] = Types.size();
847 Types.push_back(Ty);
848
849 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
850 return (int)DestSlot;
851}
852
853// vim: sw=2 ai