blob: d1af03eae47184633ba644c167943c9f524c8cde [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"
Reid Spencer24ab28f2004-07-04 11:42:49 +000020#include "llvm/Function.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000021#include "llvm/Instructions.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"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/PostOrderIterator.h"
27#include "llvm/ADT/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!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000080 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner68e3dbc2004-01-20 00:57:32 +000081 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000082 return I->second;
83}
84
Reid Spencer24ab28f2004-07-04 11:42:49 +000085unsigned SlotCalculator::getGlobalSlot(const Type* T) const {
86 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
87 assert(I != TypeMap.end() && "Didn't find global slot entry!");
88 return I->second;
89}
90
Chris Lattner68e3dbc2004-01-20 00:57:32 +000091SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +000092 if (CompactionTable.empty()) { // No compaction table active?
93 // fall out
94 } else if (!CompactionTable[Plane].empty()) { // Compaction table active.
95 assert(Plane < CompactionTable.size());
96 return CompactionTable[Plane];
97 } else {
98 // Final case: compaction table active, but this plane is not
99 // compactified. If the type plane is compactified, unmap back to the
100 // global type plane corresponding to "Plane".
Reid Spencer24ab28f2004-07-04 11:42:49 +0000101 if (!CompactionTypes.empty()) {
102 const Type *Ty = CompactionTypes[Plane];
103 TypeMapType::iterator It = TypeMap.find(Ty);
104 assert(It != TypeMap.end() && "Type not in global constant map?");
105 Plane = It->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000106 }
107 }
108
109 // Okay we are just returning an entry out of the main Table. Make sure the
110 // plane exists and return it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000111 if (Plane >= Table.size())
112 Table.resize(Plane+1);
113 return Table[Plane];
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000114}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000115
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000116// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +0000117// types that are available.
118//
119void SlotCalculator::processModule() {
120 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000121
Chris Lattner3413d152003-03-19 20:57:22 +0000122 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +0000123 //
Chris Lattnere4d5c442005-03-15 04:54:21 +0000124 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Chris Lattner479b54b2002-10-14 00:48:57 +0000125 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000126 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000127
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 // Scavenge the types out of the functions, then add the functions themselves
129 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000130 //
Chris Lattner3413d152003-03-19 20:57:22 +0000131 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
132 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000133 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000134
Chris Lattner3413d152003-03-19 20:57:22 +0000135 // Add all of the module level constants used as initializers
136 //
Chris Lattnere4d5c442005-03-15 04:54:21 +0000137 for (Module::const_global_iterator I = TheModule->global_begin(), E = TheModule->global_end();
Chris Lattner3413d152003-03-19 20:57:22 +0000138 I != E; ++I)
139 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000140 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000141
Chris Lattnerdcea6302004-01-14 23:34:39 +0000142 // Now that all global constants have been added, rearrange constant planes
143 // that contain constant strings so that the strings occur at the start of the
144 // plane, not somewhere in the middle.
145 //
Reid Spencer798ff642004-05-26 07:37:11 +0000146 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
147 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
148 if (AT->getElementType() == Type::SByteTy ||
Reid Spencer24ab28f2004-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)
Chris Lattner5fd2ab32004-10-23 03:10:23 +0000153 if (isa<ConstantAggregateZero>(Plane[i]) ||
Chris Lattner236ca442004-10-24 04:27:59 +0000154 (isa<ConstantArray>(Plane[i]) &&
155 cast<ConstantArray>(Plane[i])->isString())) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000156 // Check to see if we have to shuffle this string around. If not,
157 // don't do anything.
158 if (i != FirstNonStringID) {
159 // Swap the plane entries....
160 std::swap(Plane[i], Plane[FirstNonStringID]);
161
162 // Keep the NodeMap up to date.
163 NodeMap[Plane[i]] = i;
164 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
165 }
166 ++FirstNonStringID;
167 }
Reid Spencer798ff642004-05-26 07:37:11 +0000168 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000169 }
170
Reid Spencere8404342004-07-18 00:18:30 +0000171 // Scan all of the functions for their constants, which allows us to emit
172 // more compact modules. This is optional, and is just used to compactify
173 // the constants used by different functions together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000174 //
Reid Spencere8404342004-07-18 00:18:30 +0000175 // This functionality tends to produce smaller bytecode files. This should
176 // not be used in the future by clients that want to, for example, build and
177 // emit functions on the fly. For now, however, it is unconditionally
178 // enabled.
Reid Spencer798ff642004-05-26 07:37:11 +0000179 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000180
Reid Spencer798ff642004-05-26 07:37:11 +0000181 SC_DEBUG("Inserting function constants:\n");
182 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
183 F != E; ++F) {
184 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
185 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencere8404342004-07-18 00:18:30 +0000186 if (isa<Constant>(I->getOperand(op)) &&
187 !isa<GlobalValue>(I->getOperand(op)))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000188 getOrCreateSlot(I->getOperand(op));
Reid Spencer798ff642004-05-26 07:37:11 +0000189 getOrCreateSlot(I->getType());
190 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000191 getOrCreateSlot(VAN->getArgType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000192 }
Reid Spencer798ff642004-05-26 07:37:11 +0000193 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000194 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000195
Chris Lattner70cc3392001-09-10 07:58:01 +0000196 // Insert constants that are named at module level into the slot pool so that
197 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000198 SC_DEBUG("Inserting SymbolTable values:\n");
199 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000200
Chris Lattnera14b0d42004-01-10 23:46:13 +0000201 // Now that we have collected together all of the information relevant to the
202 // module, compactify the type table if it is particularly big and outputting
203 // a bytecode file. The basic problem we run into is that some programs have
204 // a large number of types, which causes the type field to overflow its size,
205 // which causes instructions to explode in size (particularly call
206 // instructions). To avoid this behavior, we "sort" the type table so that
207 // all non-value types are pushed to the end of the type table, giving nice
208 // low numbers to the types that can be used by instructions, thus reducing
209 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000210 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000211 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000212 for (unsigned i = 0, e = Types.size(); i != e; ++i)
213 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000214 // Check to see if we have to shuffle this type around. If not, don't
215 // do anything.
216 if (i != FirstNonValueTypeID) {
217 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000218 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000219
Reid Spencer24ab28f2004-07-04 11:42:49 +0000220 // Keep the TypeMap up to date.
221 TypeMap[Types[i]] = i;
222 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000223
224 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000225 if (Table.size() > FirstNonValueTypeID) {
226 if (Table.size() <= i) Table.resize(i+1);
227 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000228 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000229 }
230 ++FirstNonValueTypeID;
231 }
232 }
233
Chris Lattner9a297902001-09-07 16:31:52 +0000234 SC_DEBUG("end processModule!\n");
235}
236
237// processSymbolTable - Insert all of the values in the specified symbol table
238// into the values table...
239//
240void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000241 // Do the types first.
242 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
243 TE = ST->type_end(); TI != TE; ++TI )
244 getOrCreateSlot(TI->second);
245
246 // Now do the values.
247 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
248 PE = ST->plane_end(); PI != PE; ++PI)
249 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000250 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000251 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000252}
253
254void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000255 // Do the types first
256 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
257 TE = ST->type_end(); TI != TE; ++TI )
258 getOrCreateSlot(TI->second);
259
260 // Now do the constant values in all planes
261 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
262 PE = ST->plane_end(); PI != PE; ++PI)
263 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000264 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000265 if (isa<Constant>(VI->second) &&
266 !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000267 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000268}
269
270
Chris Lattnerce439b52003-10-20 19:10:06 +0000271void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000272 assert((ModuleLevel.size() == 0 ||
273 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000274
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000275 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000276
Chris Lattner614cdcd2004-01-18 21:07:07 +0000277 // If we emitted all of the function constants, build a compaction table.
Reid Spencer798ff642004-05-26 07:37:11 +0000278 if ( ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000279 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000280
281 // Update the ModuleLevel entries to be accurate.
282 ModuleLevel.resize(getNumPlanes());
283 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
284 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000285 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000286
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000287 // Iterate over function arguments, adding them to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000288 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000289 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000290
Reid Spencer798ff642004-05-26 07:37:11 +0000291 if ( !ModuleContainsAllFunctionConstants ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000292 // Iterate over all of the instructions in the function, looking for
293 // constant values that are referenced. Add these to the value pools
294 // before any nonconstant values. This will be turned into the constant
295 // pool for the bytecode writer.
296 //
297
298 // Emit all of the constants that are being used by the instructions in
299 // the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000300 constant_iterator CI = constant_begin(F);
301 constant_iterator CE = constant_end(F);
302 while ( CI != CE ) {
303 this->getOrCreateSlot(*CI);
304 ++CI;
305 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000306
Chris Lattner9a297902001-09-07 16:31:52 +0000307 // If there is a symbol table, it is possible that the user has names for
308 // constants that are not being used. In this case, we will have problems
309 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000310 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000311 // constants now.
312 //
Chris Lattnerce439b52003-10-20 19:10:06 +0000313 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
315
Chris Lattner9a297902001-09-07 16:31:52 +0000316 SC_DEBUG("Inserting Instructions:\n");
317
318 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000319 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
320 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000321 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
322 getOrCreateSlot(I);
Chris Lattner2765c412003-10-21 17:40:54 +0000323 if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
324 getOrCreateSlot(VAN->getArgType());
Chris Lattner389dbfb2003-10-21 17:39:59 +0000325 }
Chris Lattner9a297902001-09-07 16:31:52 +0000326 }
327
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000328 // If we are building a compaction table, prune out planes that do not benefit
329 // from being compactified.
330 if (!CompactionTable.empty())
331 pruneCompactionTable();
332
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000333 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000334}
335
Chris Lattnerb5794002002-04-07 22:49:37 +0000336void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000337 assert((ModuleLevel.size() != 0 ||
338 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000339 unsigned NumModuleTypes = ModuleLevel.size();
340
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000341 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000342
Chris Lattner614cdcd2004-01-18 21:07:07 +0000343 // First, free the compaction map if used.
344 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000345 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000346
347 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000348 for (unsigned i = 0; i != NumModuleTypes; ++i) {
349 // Size of plane before function came
350 unsigned ModuleLev = getModuleLevel(i);
351 assert(int(ModuleLev) >= 0 && "BAD!");
352
353 TypePlane &Plane = getPlane(i);
354
355 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
356 while (Plane.size() != ModuleLev) {
357 assert(!isa<GlobalValue>(Plane.back()) &&
358 "Functions cannot define globals!");
359 NodeMap.erase(Plane.back()); // Erase from nodemap
360 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000361 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000362 }
Chris Lattner00950542001-06-06 20:29:01 +0000363
364 // We don't need this state anymore, free it up.
365 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000366 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000367
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000368 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000369 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000370 if (!CompactionTable.empty()) {
371 CompactionTable.clear();
372 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000373 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000374 TypePlane &Plane = Table.back();
375 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
376 << Plane.size() << "\n");
377 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000378 assert(!isa<GlobalValue>(Plane.back()) &&
379 "Functions cannot define globals!");
380 NodeMap.erase(Plane.back()); // Erase from nodemap
381 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000382 }
383
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000384 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000385 }
Chris Lattner00950542001-06-06 20:29:01 +0000386 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000387
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000388 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000389}
390
Chris Lattner614cdcd2004-01-18 21:07:07 +0000391static inline bool hasNullValue(unsigned TyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000392 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000393}
394
395/// getOrCreateCompactionTableSlot - This method is used to build up the initial
396/// approximation of the compaction table.
397unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
398 std::map<const Value*, unsigned>::iterator I =
399 CompactionNodeMap.lower_bound(V);
400 if (I != CompactionNodeMap.end() && I->first == V)
401 return I->second; // Already exists?
402
403 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000404 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000405 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000406 Ty = getOrCreateCompactionTableSlot(V->getType());
407 else // If the type plane was decompactified, use the global plane ID
408 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000409 if (CompactionTable.size() <= Ty)
410 CompactionTable.resize(Ty+1);
411
Chris Lattner614cdcd2004-01-18 21:07:07 +0000412 TypePlane &TyPlane = CompactionTable[Ty];
413
414 // Make sure to insert the null entry if the thing we are inserting is not a
415 // null constant.
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000416 if (TyPlane.empty() && hasNullValue(V->getType()->getTypeID())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000417 Value *ZeroInitializer = Constant::getNullValue(V->getType());
418 if (V != ZeroInitializer) {
419 TyPlane.push_back(ZeroInitializer);
420 CompactionNodeMap[ZeroInitializer] = 0;
421 }
422 }
423
424 unsigned SlotNo = TyPlane.size();
425 TyPlane.push_back(V);
426 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
427 return SlotNo;
428}
429
Reid Spencer24ab28f2004-07-04 11:42:49 +0000430/// getOrCreateCompactionTableSlot - This method is used to build up the initial
431/// approximation of the compaction table.
432unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
433 std::map<const Type*, unsigned>::iterator I =
434 CompactionTypeMap.lower_bound(T);
435 if (I != CompactionTypeMap.end() && I->first == T)
436 return I->second; // Already exists?
437
438 unsigned SlotNo = CompactionTypes.size();
439 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
440 CompactionTypes.push_back(T);
441 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
442 return SlotNo;
443}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000444
445/// buildCompactionTable - Since all of the function constants and types are
446/// stored in the module-level constant table, we don't need to emit a function
447/// constant table. Also due to this, the indices for various constants and
448/// types might be very large in large programs. In order to avoid blowing up
449/// the size of instructions in the bytecode encoding, we build a compaction
450/// table, which defines a mapping from function-local identifiers to global
451/// identifiers.
452void SlotCalculator::buildCompactionTable(const Function *F) {
453 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000454 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000455 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000456 CompactionTable.resize(Type::LastPrimitiveTyID+1);
457 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000458 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000459 CompactionTypes.push_back(PrimTy);
460 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000461 }
462
463 // Next, include any types used by function arguments.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000464 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000465 getOrCreateCompactionTableSlot(I->getType());
466
467 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000468 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000469 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
470 getOrCreateCompactionTableSlot(I->getType());
471 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencere8404342004-07-18 00:18:30 +0000472 if (isa<Constant>(I->getOperand(op)))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000473 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner6ffe5512004-04-27 15:13:33 +0000474 if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000475 getOrCreateCompactionTableSlot(VAN->getArgType());
476 }
477
Reid Spencer9231ac82004-05-25 08:53:40 +0000478 // Do the types in the symbol table
Chris Lattner614cdcd2004-01-18 21:07:07 +0000479 const SymbolTable &ST = F->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000480 for (SymbolTable::type_const_iterator TI = ST.type_begin(),
481 TE = ST.type_end(); TI != TE; ++TI)
482 getOrCreateCompactionTableSlot(TI->second);
483
484 // Now do the constants and global values
485 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
486 PE = ST.plane_end(); PI != PE; ++PI)
487 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000488 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000489 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000490 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000491
492 // Now that we have all of the values in the table, and know what types are
493 // referenced, make sure that there is at least the zero initializer in any
494 // used type plane. Since the type was used, we will be emitting instructions
495 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000496 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000497 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000498 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000499 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000500 const Type *Ty = CompactionTypes[i];
501 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
502 assert(Ty->getTypeID() != Type::VoidTyID);
503 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000504 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
505 }
506
507 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000508 // to emit the smallest possible binaries, do not compactify the type plane if
509 // it will not save us anything. Because we have not yet incorporated the
510 // function body itself yet, we don't know whether or not it's a good idea to
511 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000512 TypeList &GlobalTypes = Types;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000513
514 // All of the values types will be scrunched to the start of the types plane
515 // of the global table. Figure out just how many there are.
516 assert(!GlobalTypes.empty() && "No global types???");
517 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000518 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000519 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000520
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000521 // If there are fewer that 64 types, no instructions will be exploded due to
522 // the size of the type operands. Thus there is no need to compactify types.
523 // Also, if the compaction table contains most of the entries in the global
524 // table, there really is no reason to compactify either.
525 if (NumFCTypes < 64) {
526 // Decompactifying types is tricky, because we have to move type planes all
527 // over the place. At least we don't need to worry about updating the
528 // CompactionNodeMap for non-types though.
529 std::vector<TypePlane> TmpCompactionTable;
530 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000531 TypeList TmpTypes;
532 std::swap(TmpTypes, CompactionTypes);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000533
534 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000535 while (!TmpTypes.empty()) {
536 const Type *Ty = TmpTypes.back();
537 TmpTypes.pop_back();
538 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000539
Reid Spencer24ab28f2004-07-04 11:42:49 +0000540 // Find the global slot number for this type.
541 int TySlot = getSlot(Ty);
542 assert(TySlot != -1 && "Type doesn't exist in global table?");
543
544 // Now we know where to put the compaction table plane.
545 if (CompactionTable.size() <= unsigned(TySlot))
546 CompactionTable.resize(TySlot+1);
547 // Move the plane back into the compaction table.
548 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000549
Reid Spencer24ab28f2004-07-04 11:42:49 +0000550 // And remove the empty plane we just moved in.
551 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000552 }
553 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000554}
555
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000556
557/// pruneCompactionTable - Once the entire function being processed has been
558/// incorporated into the current compaction table, look over the compaction
559/// table and check to see if there are any values whose compaction will not
560/// save us any space in the bytecode file. If compactifying these values
561/// serves no purpose, then we might as well not even emit the compactification
562/// information to the bytecode file, saving a bit more space.
563///
564/// Note that the type plane has already been compactified if possible.
565///
566void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000567 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000568 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000569 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000570 TypePlane &CPlane = CompactionTable[ctp];
571 unsigned GlobalSlot = ctp;
572 if (!TyPlane.empty())
573 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
574
575 if (GlobalSlot >= Table.size())
576 Table.resize(GlobalSlot+1);
577 TypePlane &GPlane = Table[GlobalSlot];
578
579 unsigned ModLevel = getModuleLevel(ctp);
580 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
581
582 // If the maximum index required if all entries in this plane were merged
583 // into the global plane is less than 64, go ahead and eliminate the
584 // plane.
585 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
586
587 // If there are no function-local values defined, and the maximum
588 // referenced global entry is less than 64, we don't need to compactify.
589 if (!PrunePlane && NumFunctionObjs == 0) {
590 unsigned MaxIdx = 0;
591 for (unsigned i = 0; i != ModLevel; ++i) {
592 unsigned Idx = NodeMap[CPlane[i]];
593 if (Idx > MaxIdx) MaxIdx = Idx;
594 }
595 PrunePlane = MaxIdx < 64;
596 }
597
598 // Ok, finally, if we decided to prune this plane out of the compaction
599 // table, do so now.
600 if (PrunePlane) {
601 TypePlane OldPlane;
602 std::swap(OldPlane, CPlane);
603
604 // Loop over the function local objects, relocating them to the global
605 // table plane.
606 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
607 const Value *V = OldPlane[i];
608 CompactionNodeMap.erase(V);
609 assert(NodeMap.count(V) == 0 && "Value already in table??");
610 getOrCreateSlot(V);
611 }
612
613 // For compactified global values, just remove them from the compaction
614 // node map.
615 for (unsigned i = 0; i != ModLevel; ++i)
616 CompactionNodeMap.erase(OldPlane[i]);
617
618 // Update the new modulelevel for this plane.
619 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
620 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
621 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
622 }
623 }
624}
625
Reid Spencer07ea1912004-08-26 22:32:00 +0000626/// Determine if the compaction table is actually empty. Because the
627/// compaction table always includes the primitive type planes, we
628/// can't just check getCompactionTable().size() because it will never
629/// be zero. Furthermore, the ModuleLevel factors into whether a given
630/// plane is empty or not. This function does the necessary computation
631/// to determine if its actually empty.
632bool SlotCalculator::CompactionTableIsEmpty() const {
633 // Check a degenerate case, just in case.
634 if (CompactionTable.size() == 0) return true;
635
636 // Check each plane
637 for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
638 // If the plane is not empty
639 if (!CompactionTable[i].empty()) {
640 // If the module level is non-zero then at least the
641 // first element of the plane is valid and therefore not empty.
642 unsigned End = getModuleLevel(i);
643 if (End != 0)
644 return false;
645 }
646 }
647 // All the compaction table planes are empty so the table is
648 // considered empty too.
649 return true;
650}
651
Chris Lattner8c202cd2004-01-15 18:47:15 +0000652int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000653 // If there is a CompactionTable active...
654 if (!CompactionNodeMap.empty()) {
655 std::map<const Value*, unsigned>::const_iterator I =
656 CompactionNodeMap.find(V);
657 if (I != CompactionNodeMap.end())
658 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000659 // Otherwise, if it's not in the compaction table, it must be in a
660 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000661 }
662
Chris Lattner8c202cd2004-01-15 18:47:15 +0000663 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
664 if (I != NodeMap.end())
665 return (int)I->second;
666
Chris Lattner8c202cd2004-01-15 18:47:15 +0000667 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000668}
669
Reid Spencer24ab28f2004-07-04 11:42:49 +0000670int SlotCalculator::getSlot(const Type*T) const {
671 // If there is a CompactionTable active...
672 if (!CompactionTypeMap.empty()) {
673 std::map<const Type*, unsigned>::const_iterator I =
674 CompactionTypeMap.find(T);
675 if (I != CompactionTypeMap.end())
676 return (int)I->second;
677 // Otherwise, if it's not in the compaction table, it must be in a
678 // non-compactified plane.
679 }
680
681 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
682 if (I != TypeMap.end())
683 return (int)I->second;
684
685 return -1;
686}
Chris Lattner9a297902001-09-07 16:31:52 +0000687
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000688int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000689 if (V->getType() == Type::VoidTy) return -1;
690
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000691 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000692 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000693
Chris Lattner129baf62004-12-04 21:28:47 +0000694 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
695 assert(GV->getParent() != 0 && "Global not embedded into a module!");
696
Chris Lattner614cdcd2004-01-18 21:07:07 +0000697 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000698 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000699 assert(CompactionNodeMap.empty() &&
700 "All needed constants should be in the compaction map already!");
701
Reid Spencer798ff642004-05-26 07:37:11 +0000702 // Do not index the characters that make up constant strings. We emit
703 // constant strings as special entities that don't require their
704 // individual characters to be emitted.
705 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000706 // This makes sure that if a constant has uses (for example an array of
707 // const ints), that they are inserted also.
708 //
709 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
710 I != E; ++I)
711 getOrCreateSlot(*I);
712 } else {
713 assert(ModuleLevel.empty() &&
714 "How can a constant string be directly accessed in a function?");
715 // Otherwise, if we are emitting a bytecode file and this IS a string,
716 // remember it.
717 if (!C->isNullValue())
718 ConstantStrings.push_back(cast<ConstantArray>(C));
719 }
Chris Lattner3413d152003-03-19 20:57:22 +0000720 }
721
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000722 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000723}
724
Reid Spencer24ab28f2004-07-04 11:42:49 +0000725int SlotCalculator::getOrCreateSlot(const Type* T) {
726 int SlotNo = getSlot(T); // Check to see if it's already in!
727 if (SlotNo != -1) return SlotNo;
728 return insertType(T);
729}
Chris Lattner9a297902001-09-07 16:31:52 +0000730
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000731int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000732 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000733 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000734
Chris Lattner614cdcd2004-01-18 21:07:07 +0000735 // If we are building a compaction map, and if this plane is being compacted,
736 // insert the value into the compaction map, not into the global map.
737 if (!CompactionNodeMap.empty()) {
738 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencere8404342004-07-18 00:18:30 +0000739 assert(!isa<Constant>(D) &&
740 "Types, constants, and globals should be in global table!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000741
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000742 int Plane = getSlot(D->getType());
743 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
744 "Didn't find value type!");
745 if (!CompactionTable[Plane].empty())
746 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000747 }
748
Chris Lattner00950542001-06-06 20:29:01 +0000749 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000750 // name and we don't want names, then ignore the silly node... Note that types
751 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000752 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000753 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000754 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000755 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000756 return -1; // We do need types unconditionally though
757 }
Chris Lattner00950542001-06-06 20:29:01 +0000758
Chris Lattner9a297902001-09-07 16:31:52 +0000759 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000760 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000761}
762
Reid Spencer24ab28f2004-07-04 11:42:49 +0000763int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
764 assert(Ty && "Can't insert a null type!");
765 assert(getSlot(Ty) == -1 && "Type is already in the table!");
766
767 // If we are building a compaction map, and if this plane is being compacted,
768 // insert the value into the compaction map, not into the global map.
769 if (!CompactionTypeMap.empty()) {
770 getOrCreateCompactionTableSlot(Ty);
771 }
772
773 // Insert the current type before any subtypes. This is important because
774 // recursive types elements are inserted in a bottom up order. Changing
775 // this here can break things. For example:
776 //
777 // global { \2 * } { { \2 }* null }
778 //
779 int ResultSlot = doInsertType(Ty);
780 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
781 ResultSlot << "\n");
782
783 // Loop over any contained types in the definition... in post
784 // order.
785 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
786 I != E; ++I) {
787 if (*I != Ty) {
788 const Type *SubTy = *I;
789 // If we haven't seen this sub type before, add it to our type table!
790 if (getSlot(SubTy) == -1) {
791 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattneraec6dd52004-07-12 20:29:52 +0000792 doInsertType(SubTy);
Reid Spencere8404342004-07-18 00:18:30 +0000793 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000794 }
795 }
796 }
797 return ResultSlot;
798}
799
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000800// doInsertValue - This is a small helper function to be called only
801// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000802//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000803int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000804 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000805 unsigned Ty;
806
807 // Used for debugging DefSlot=-1 assertion...
808 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000809 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000810
Chris Lattner00950542001-06-06 20:29:01 +0000811 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000812 int ValSlot;
813 if (CompactionTable.empty())
814 ValSlot = getSlot(Typ);
815 else
816 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000817 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000818 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000819 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000820 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000821 }
Chris Lattner3413d152003-03-19 20:57:22 +0000822 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000823 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000824 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000825 }
826
827 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
828 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000829
830 // If this is the first value to get inserted into the type plane, make sure
831 // to insert the implicit null value...
Reid Spencer798ff642004-05-26 07:37:11 +0000832 if (Table[Ty].empty() && hasNullValue(Ty)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000833 Value *ZeroInitializer = Constant::getNullValue(Typ);
834
835 // If we are pushing zeroinit, it will be handled below.
836 if (D != ZeroInitializer) {
837 Table[Ty].push_back(ZeroInitializer);
838 NodeMap[ZeroInitializer] = 0;
839 }
840 }
841
Chris Lattner9a297902001-09-07 16:31:52 +0000842 // Insert node into table and NodeMap...
843 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000844 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000845
Chris Lattnerf1fef652001-10-13 06:35:09 +0000846 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000847 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000848 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000849 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000850 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000851 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000852 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000853}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000854
855// doInsertType - This is a small helper function to be called only
856// be insertType.
857//
858int SlotCalculator::doInsertType(const Type *Ty) {
859
860 // Insert node into table and NodeMap...
861 unsigned DestSlot = TypeMap[Ty] = Types.size();
862 Types.push_back(Ty);
863
864 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
865 return (int)DestSlot;
866}
867
868// vim: sw=2 ai