blob: c6aba09fe51d7721dccc5d74f978f76ab73bcd4c [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman23c6d2c2005-04-21 21:48:46 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000010// This file implements a useful analysis step to figure out what numbered slots
11// values in a program will land in (keeping track of per plane information).
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000013// This is used when writing a file to disk, either in bytecode or assembly.
Chris Lattner00950542001-06-06 20:29:01 +000014//
15//===----------------------------------------------------------------------===//
16
Reid Spencer24ab28f2004-07-04 11:42:49 +000017#include "SlotCalculator.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000018#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/DerivedTypes.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000020#include "llvm/Function.h"
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 Lattner9e60d8d2005-05-05 22:21:19 +0000124 for (Module::const_global_iterator I = TheModule->global_begin(),
125 E = TheModule->global_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000126 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000127
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 // Scavenge the types out of the functions, then add the functions themselves
129 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000130 //
Chris Lattner3413d152003-03-19 20:57:22 +0000131 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
132 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000133 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000134
Chris Lattner3413d152003-03-19 20:57:22 +0000135 // Add all of the module level constants used as initializers
136 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000137 for (Module::const_global_iterator I = TheModule->global_begin(),
138 E = TheModule->global_end(); I != E; ++I)
Chris Lattner3413d152003-03-19 20:57:22 +0000139 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000140 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000141
Chris Lattnerdcea6302004-01-14 23:34:39 +0000142 // Now that all global constants have been added, rearrange constant planes
143 // that contain constant strings so that the strings occur at the start of the
144 // plane, not somewhere in the middle.
145 //
Reid Spencer798ff642004-05-26 07:37:11 +0000146 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
147 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
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)
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000153 if (isa<ConstantAggregateZero>(Plane[i]) ||
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000154 (isa<ConstantArray>(Plane[i]) &&
Chris Lattner236ca442004-10-24 04:27:59 +0000155 cast<ConstantArray>(Plane[i])->isString())) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000156 // Check to see if we have to shuffle this string around. If not,
157 // don't do anything.
158 if (i != FirstNonStringID) {
159 // Swap the plane entries....
160 std::swap(Plane[i], Plane[FirstNonStringID]);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000161
Reid Spencer24ab28f2004-07-04 11:42:49 +0000162 // Keep the NodeMap up to date.
163 NodeMap[Plane[i]] = i;
164 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
165 }
166 ++FirstNonStringID;
167 }
Reid Spencer798ff642004-05-26 07:37:11 +0000168 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000169 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000170
171 // Scan all of the functions for their constants, which allows us to emit
172 // more compact modules. This is optional, and is just used to compactify
Reid Spencere8404342004-07-18 00:18:30 +0000173 // the constants used by different functions together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000174 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000175 // This functionality tends to produce smaller bytecode files. This should
176 // not be used in the future by clients that want to, for example, build and
177 // emit functions on the fly. For now, however, it is unconditionally
Reid Spencere8404342004-07-18 00:18:30 +0000178 // enabled.
Reid Spencer798ff642004-05-26 07:37:11 +0000179 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000180
Reid Spencer798ff642004-05-26 07:37:11 +0000181 SC_DEBUG("Inserting function constants:\n");
182 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
183 F != E; ++F) {
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)
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000186 if (isa<Constant>(I->getOperand(op)) &&
Reid Spencere8404342004-07-18 00:18:30 +0000187 !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());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000190 }
Reid Spencer798ff642004-05-26 07:37:11 +0000191 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000192 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000193
Chris Lattner70cc3392001-09-10 07:58:01 +0000194 // Insert constants that are named at module level into the slot pool so that
195 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000196 SC_DEBUG("Inserting SymbolTable values:\n");
197 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000198
Chris Lattnera14b0d42004-01-10 23:46:13 +0000199 // Now that we have collected together all of the information relevant to the
200 // module, compactify the type table if it is particularly big and outputting
201 // a bytecode file. The basic problem we run into is that some programs have
202 // a large number of types, which causes the type field to overflow its size,
203 // which causes instructions to explode in size (particularly call
204 // instructions). To avoid this behavior, we "sort" the type table so that
205 // all non-value types are pushed to the end of the type table, giving nice
206 // low numbers to the types that can be used by instructions, thus reducing
207 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000208 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000209 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000210 for (unsigned i = 0, e = Types.size(); i != e; ++i)
211 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000212 // Check to see if we have to shuffle this type around. If not, don't
213 // do anything.
214 if (i != FirstNonValueTypeID) {
215 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000216 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000217
Reid Spencer24ab28f2004-07-04 11:42:49 +0000218 // Keep the TypeMap up to date.
219 TypeMap[Types[i]] = i;
220 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000221
222 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000223 if (Table.size() > FirstNonValueTypeID) {
224 if (Table.size() <= i) Table.resize(i+1);
225 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000226 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000227 }
228 ++FirstNonValueTypeID;
229 }
230 }
231
Chris Lattner9a297902001-09-07 16:31:52 +0000232 SC_DEBUG("end processModule!\n");
233}
234
235// processSymbolTable - Insert all of the values in the specified symbol table
236// into the values table...
237//
238void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000239 // Do the types first.
240 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
241 TE = ST->type_end(); TI != TE; ++TI )
242 getOrCreateSlot(TI->second);
243
244 // Now do the values.
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000245 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000246 PE = ST->plane_end(); PI != PE; ++PI)
247 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000248 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000249 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000250}
251
252void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000253 // Do the types first
254 for (SymbolTable::type_const_iterator TI = ST->type_begin(),
255 TE = ST->type_end(); TI != TE; ++TI )
256 getOrCreateSlot(TI->second);
257
258 // Now do the constant values in all planes
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000259 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000260 PE = ST->plane_end(); PI != PE; ++PI)
261 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000262 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000263 if (isa<Constant>(VI->second) &&
264 !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000265 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000266}
267
268
Chris Lattnerce439b52003-10-20 19:10:06 +0000269void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000270 assert((ModuleLevel.size() == 0 ||
271 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000272
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000273 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000274
Chris Lattner614cdcd2004-01-18 21:07:07 +0000275 // If we emitted all of the function constants, build a compaction table.
Reid Spencer798ff642004-05-26 07:37:11 +0000276 if ( ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000277 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000278
279 // Update the ModuleLevel entries to be accurate.
280 ModuleLevel.resize(getNumPlanes());
281 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
282 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000283 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000284
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 // Iterate over function arguments, adding them to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000286 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000287 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000288
Reid Spencer798ff642004-05-26 07:37:11 +0000289 if ( !ModuleContainsAllFunctionConstants ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000290 // Iterate over all of the instructions in the function, looking for
291 // constant values that are referenced. Add these to the value pools
292 // before any nonconstant values. This will be turned into the constant
293 // pool for the bytecode writer.
294 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000295
Chris Lattner614cdcd2004-01-18 21:07:07 +0000296 // Emit all of the constants that are being used by the instructions in
297 // the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000298 constant_iterator CI = constant_begin(F);
299 constant_iterator CE = constant_end(F);
300 while ( CI != CE ) {
301 this->getOrCreateSlot(*CI);
302 ++CI;
303 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000304
Chris Lattner9a297902001-09-07 16:31:52 +0000305 // If there is a symbol table, it is possible that the user has names for
306 // constants that are not being used. In this case, we will have problems
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000307 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000308 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000309 // constants now.
310 //
Chris Lattnerce439b52003-10-20 19:10:06 +0000311 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000312 }
313
Chris Lattner9a297902001-09-07 16:31:52 +0000314 SC_DEBUG("Inserting Instructions:\n");
315
316 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000317 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
318 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000319 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
320 getOrCreateSlot(I);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000321 }
Chris Lattner9a297902001-09-07 16:31:52 +0000322 }
323
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000324 // If we are building a compaction table, prune out planes that do not benefit
325 // from being compactified.
326 if (!CompactionTable.empty())
327 pruneCompactionTable();
328
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000329 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000330}
331
Chris Lattnerb5794002002-04-07 22:49:37 +0000332void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000333 assert((ModuleLevel.size() != 0 ||
334 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000335 unsigned NumModuleTypes = ModuleLevel.size();
336
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000337 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000338
Chris Lattner614cdcd2004-01-18 21:07:07 +0000339 // First, free the compaction map if used.
340 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000341 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000342
343 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000344 for (unsigned i = 0; i != NumModuleTypes; ++i) {
345 // Size of plane before function came
346 unsigned ModuleLev = getModuleLevel(i);
347 assert(int(ModuleLev) >= 0 && "BAD!");
348
349 TypePlane &Plane = getPlane(i);
350
351 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
352 while (Plane.size() != ModuleLev) {
353 assert(!isa<GlobalValue>(Plane.back()) &&
354 "Functions cannot define globals!");
355 NodeMap.erase(Plane.back()); // Erase from nodemap
356 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000357 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000358 }
Chris Lattner00950542001-06-06 20:29:01 +0000359
360 // We don't need this state anymore, free it up.
361 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000362 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000363
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000364 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000365 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000366 if (!CompactionTable.empty()) {
367 CompactionTable.clear();
368 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000369 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000370 TypePlane &Plane = Table.back();
371 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
372 << Plane.size() << "\n");
373 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000374 assert(!isa<GlobalValue>(Plane.back()) &&
375 "Functions cannot define globals!");
376 NodeMap.erase(Plane.back()); // Erase from nodemap
377 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000378 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000379
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000380 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000381 }
Chris Lattner00950542001-06-06 20:29:01 +0000382 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000383
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000384 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000385}
386
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000387static inline bool hasNullValue(const Type *Ty) {
388 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000389}
390
391/// getOrCreateCompactionTableSlot - This method is used to build up the initial
392/// approximation of the compaction table.
393unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
394 std::map<const Value*, unsigned>::iterator I =
395 CompactionNodeMap.lower_bound(V);
396 if (I != CompactionNodeMap.end() && I->first == V)
397 return I->second; // Already exists?
398
399 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000400 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000401 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000402 Ty = getOrCreateCompactionTableSlot(V->getType());
403 else // If the type plane was decompactified, use the global plane ID
404 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000405 if (CompactionTable.size() <= Ty)
406 CompactionTable.resize(Ty+1);
407
Chris Lattner614cdcd2004-01-18 21:07:07 +0000408 TypePlane &TyPlane = CompactionTable[Ty];
409
410 // Make sure to insert the null entry if the thing we are inserting is not a
411 // null constant.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000412 if (TyPlane.empty() && hasNullValue(V->getType())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000413 Value *ZeroInitializer = Constant::getNullValue(V->getType());
414 if (V != ZeroInitializer) {
415 TyPlane.push_back(ZeroInitializer);
416 CompactionNodeMap[ZeroInitializer] = 0;
417 }
418 }
419
420 unsigned SlotNo = TyPlane.size();
421 TyPlane.push_back(V);
422 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
423 return SlotNo;
424}
425
Reid Spencer24ab28f2004-07-04 11:42:49 +0000426/// getOrCreateCompactionTableSlot - This method is used to build up the initial
427/// approximation of the compaction table.
428unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
429 std::map<const Type*, unsigned>::iterator I =
430 CompactionTypeMap.lower_bound(T);
431 if (I != CompactionTypeMap.end() && I->first == T)
432 return I->second; // Already exists?
433
434 unsigned SlotNo = CompactionTypes.size();
435 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
436 CompactionTypes.push_back(T);
437 CompactionTypeMap.insert(std::make_pair(T, SlotNo));
438 return SlotNo;
439}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000440
441/// buildCompactionTable - Since all of the function constants and types are
442/// stored in the module-level constant table, we don't need to emit a function
443/// constant table. Also due to this, the indices for various constants and
444/// types might be very large in large programs. In order to avoid blowing up
445/// the size of instructions in the bytecode encoding, we build a compaction
446/// table, which defines a mapping from function-local identifiers to global
447/// identifiers.
448void SlotCalculator::buildCompactionTable(const Function *F) {
449 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000450 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000451 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000452 CompactionTable.resize(Type::LastPrimitiveTyID+1);
453 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000454 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000455 CompactionTypes.push_back(PrimTy);
456 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000457 }
458
459 // Next, include any types used by function arguments.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000460 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
461 I != E; ++I)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000462 getOrCreateCompactionTableSlot(I->getType());
463
464 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000465 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000466 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
467 getOrCreateCompactionTableSlot(I->getType());
468 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Reid Spencere8404342004-07-18 00:18:30 +0000469 if (isa<Constant>(I->getOperand(op)))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000470 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner614cdcd2004-01-18 21:07:07 +0000471 }
472
Reid Spencer9231ac82004-05-25 08:53:40 +0000473 // Do the types in the symbol table
Chris Lattner614cdcd2004-01-18 21:07:07 +0000474 const SymbolTable &ST = F->getSymbolTable();
Reid Spencer9231ac82004-05-25 08:53:40 +0000475 for (SymbolTable::type_const_iterator TI = ST.type_begin(),
476 TE = ST.type_end(); TI != TE; ++TI)
477 getOrCreateCompactionTableSlot(TI->second);
478
479 // Now do the constants and global values
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000480 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000481 PE = ST.plane_end(); PI != PE; ++PI)
482 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000483 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000484 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000485 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000486
487 // Now that we have all of the values in the table, and know what types are
488 // referenced, make sure that there is at least the zero initializer in any
489 // used type plane. Since the type was used, we will be emitting instructions
490 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000491 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000492 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000493 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000494 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000495 const Type *Ty = CompactionTypes[i];
496 SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
497 assert(Ty->getTypeID() != Type::VoidTyID);
498 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000499 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
500 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000501
Chris Lattner614cdcd2004-01-18 21:07:07 +0000502 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000503 // to emit the smallest possible binaries, do not compactify the type plane if
504 // it will not save us anything. Because we have not yet incorporated the
505 // function body itself yet, we don't know whether or not it's a good idea to
506 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000507 TypeList &GlobalTypes = Types;
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000508
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000509 // All of the values types will be scrunched to the start of the types plane
510 // of the global table. Figure out just how many there are.
511 assert(!GlobalTypes.empty() && "No global types???");
512 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000513 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000514 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000515
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000516 // If there are fewer that 64 types, no instructions will be exploded due to
517 // the size of the type operands. Thus there is no need to compactify types.
518 // Also, if the compaction table contains most of the entries in the global
519 // table, there really is no reason to compactify either.
520 if (NumFCTypes < 64) {
521 // Decompactifying types is tricky, because we have to move type planes all
522 // over the place. At least we don't need to worry about updating the
523 // CompactionNodeMap for non-types though.
524 std::vector<TypePlane> TmpCompactionTable;
525 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000526 TypeList TmpTypes;
527 std::swap(TmpTypes, CompactionTypes);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000528
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000529 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000530 while (!TmpTypes.empty()) {
531 const Type *Ty = TmpTypes.back();
532 TmpTypes.pop_back();
533 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000534
Reid Spencer24ab28f2004-07-04 11:42:49 +0000535 // Find the global slot number for this type.
536 int TySlot = getSlot(Ty);
537 assert(TySlot != -1 && "Type doesn't exist in global table?");
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000538
Reid Spencer24ab28f2004-07-04 11:42:49 +0000539 // Now we know where to put the compaction table plane.
540 if (CompactionTable.size() <= unsigned(TySlot))
541 CompactionTable.resize(TySlot+1);
542 // Move the plane back into the compaction table.
543 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000544
Reid Spencer24ab28f2004-07-04 11:42:49 +0000545 // And remove the empty plane we just moved in.
546 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000547 }
548 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000549}
550
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000551
552/// pruneCompactionTable - Once the entire function being processed has been
553/// incorporated into the current compaction table, look over the compaction
554/// table and check to see if there are any values whose compaction will not
555/// save us any space in the bytecode file. If compactifying these values
556/// serves no purpose, then we might as well not even emit the compactification
557/// information to the bytecode file, saving a bit more space.
558///
559/// Note that the type plane has already been compactified if possible.
560///
561void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000562 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000563 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000564 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000565 TypePlane &CPlane = CompactionTable[ctp];
566 unsigned GlobalSlot = ctp;
567 if (!TyPlane.empty())
568 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
569
570 if (GlobalSlot >= Table.size())
571 Table.resize(GlobalSlot+1);
572 TypePlane &GPlane = Table[GlobalSlot];
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000573
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000574 unsigned ModLevel = getModuleLevel(ctp);
575 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
576
577 // If the maximum index required if all entries in this plane were merged
578 // into the global plane is less than 64, go ahead and eliminate the
579 // plane.
580 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
581
582 // If there are no function-local values defined, and the maximum
583 // referenced global entry is less than 64, we don't need to compactify.
584 if (!PrunePlane && NumFunctionObjs == 0) {
585 unsigned MaxIdx = 0;
586 for (unsigned i = 0; i != ModLevel; ++i) {
587 unsigned Idx = NodeMap[CPlane[i]];
588 if (Idx > MaxIdx) MaxIdx = Idx;
589 }
590 PrunePlane = MaxIdx < 64;
591 }
592
593 // Ok, finally, if we decided to prune this plane out of the compaction
594 // table, do so now.
595 if (PrunePlane) {
596 TypePlane OldPlane;
597 std::swap(OldPlane, CPlane);
598
599 // Loop over the function local objects, relocating them to the global
600 // table plane.
601 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
602 const Value *V = OldPlane[i];
603 CompactionNodeMap.erase(V);
604 assert(NodeMap.count(V) == 0 && "Value already in table??");
605 getOrCreateSlot(V);
606 }
607
608 // For compactified global values, just remove them from the compaction
609 // node map.
610 for (unsigned i = 0; i != ModLevel; ++i)
611 CompactionNodeMap.erase(OldPlane[i]);
612
613 // Update the new modulelevel for this plane.
614 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
615 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
616 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
617 }
618 }
619}
620
Reid Spencer07ea1912004-08-26 22:32:00 +0000621/// Determine if the compaction table is actually empty. Because the
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000622/// compaction table always includes the primitive type planes, we
Reid Spencer07ea1912004-08-26 22:32:00 +0000623/// can't just check getCompactionTable().size() because it will never
624/// be zero. Furthermore, the ModuleLevel factors into whether a given
625/// plane is empty or not. This function does the necessary computation
626/// to determine if its actually empty.
627bool SlotCalculator::CompactionTableIsEmpty() const {
628 // Check a degenerate case, just in case.
629 if (CompactionTable.size() == 0) return true;
630
631 // Check each plane
632 for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
633 // If the plane is not empty
634 if (!CompactionTable[i].empty()) {
635 // If the module level is non-zero then at least the
636 // first element of the plane is valid and therefore not empty.
637 unsigned End = getModuleLevel(i);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000638 if (End != 0)
Reid Spencer07ea1912004-08-26 22:32:00 +0000639 return false;
640 }
641 }
642 // All the compaction table planes are empty so the table is
643 // considered empty too.
644 return true;
645}
646
Chris Lattner8c202cd2004-01-15 18:47:15 +0000647int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000648 // If there is a CompactionTable active...
649 if (!CompactionNodeMap.empty()) {
650 std::map<const Value*, unsigned>::const_iterator I =
651 CompactionNodeMap.find(V);
652 if (I != CompactionNodeMap.end())
653 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000654 // Otherwise, if it's not in the compaction table, it must be in a
655 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000656 }
657
Chris Lattner8c202cd2004-01-15 18:47:15 +0000658 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
659 if (I != NodeMap.end())
660 return (int)I->second;
661
Chris Lattner8c202cd2004-01-15 18:47:15 +0000662 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000663}
664
Reid Spencer24ab28f2004-07-04 11:42:49 +0000665int SlotCalculator::getSlot(const Type*T) const {
666 // If there is a CompactionTable active...
667 if (!CompactionTypeMap.empty()) {
668 std::map<const Type*, unsigned>::const_iterator I =
669 CompactionTypeMap.find(T);
670 if (I != CompactionTypeMap.end())
671 return (int)I->second;
672 // Otherwise, if it's not in the compaction table, it must be in a
673 // non-compactified plane.
674 }
675
676 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
677 if (I != TypeMap.end())
678 return (int)I->second;
679
680 return -1;
681}
Chris Lattner9a297902001-09-07 16:31:52 +0000682
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000683int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000684 if (V->getType() == Type::VoidTy) return -1;
685
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000686 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000687 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000688
Chris Lattner129baf62004-12-04 21:28:47 +0000689 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
690 assert(GV->getParent() != 0 && "Global not embedded into a module!");
691
Chris Lattner614cdcd2004-01-18 21:07:07 +0000692 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000693 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000694 assert(CompactionNodeMap.empty() &&
695 "All needed constants should be in the compaction map already!");
696
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000697 // Do not index the characters that make up constant strings. We emit
698 // constant strings as special entities that don't require their
Reid Spencer798ff642004-05-26 07:37:11 +0000699 // individual characters to be emitted.
700 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000701 // This makes sure that if a constant has uses (for example an array of
702 // const ints), that they are inserted also.
703 //
704 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
705 I != E; ++I)
706 getOrCreateSlot(*I);
707 } else {
708 assert(ModuleLevel.empty() &&
709 "How can a constant string be directly accessed in a function?");
710 // Otherwise, if we are emitting a bytecode file and this IS a string,
711 // remember it.
712 if (!C->isNullValue())
713 ConstantStrings.push_back(cast<ConstantArray>(C));
714 }
Chris Lattner3413d152003-03-19 20:57:22 +0000715 }
716
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000717 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000718}
719
Reid Spencer24ab28f2004-07-04 11:42:49 +0000720int SlotCalculator::getOrCreateSlot(const Type* T) {
721 int SlotNo = getSlot(T); // Check to see if it's already in!
722 if (SlotNo != -1) return SlotNo;
723 return insertType(T);
724}
Chris Lattner9a297902001-09-07 16:31:52 +0000725
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000726int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000727 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000728 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000729
Chris Lattner614cdcd2004-01-18 21:07:07 +0000730 // If we are building a compaction map, and if this plane is being compacted,
731 // insert the value into the compaction map, not into the global map.
732 if (!CompactionNodeMap.empty()) {
733 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencere8404342004-07-18 00:18:30 +0000734 assert(!isa<Constant>(D) &&
735 "Types, constants, and globals should be in global table!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000736
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000737 int Plane = getSlot(D->getType());
738 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
739 "Didn't find value type!");
740 if (!CompactionTable[Plane].empty())
741 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000742 }
743
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000744 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000745 // name and we don't want names, then ignore the silly node... Note that types
746 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000747 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000748 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000749 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000750 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000751 return -1; // We do need types unconditionally though
752 }
Chris Lattner00950542001-06-06 20:29:01 +0000753
Chris Lattner9a297902001-09-07 16:31:52 +0000754 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000755 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000756}
757
Reid Spencer24ab28f2004-07-04 11:42:49 +0000758int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
759 assert(Ty && "Can't insert a null type!");
760 assert(getSlot(Ty) == -1 && "Type is already in the table!");
761
762 // If we are building a compaction map, and if this plane is being compacted,
763 // insert the value into the compaction map, not into the global map.
764 if (!CompactionTypeMap.empty()) {
765 getOrCreateCompactionTableSlot(Ty);
766 }
767
768 // Insert the current type before any subtypes. This is important because
769 // recursive types elements are inserted in a bottom up order. Changing
770 // this here can break things. For example:
771 //
772 // global { \2 * } { { \2 }* null }
773 //
774 int ResultSlot = doInsertType(Ty);
775 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
776 ResultSlot << "\n");
777
778 // Loop over any contained types in the definition... in post
779 // order.
780 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
781 I != E; ++I) {
782 if (*I != Ty) {
783 const Type *SubTy = *I;
784 // If we haven't seen this sub type before, add it to our type table!
785 if (getSlot(SubTy) == -1) {
786 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattneraec6dd52004-07-12 20:29:52 +0000787 doInsertType(SubTy);
Reid Spencere8404342004-07-18 00:18:30 +0000788 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000789 }
790 }
791 }
792 return ResultSlot;
793}
794
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000795// doInsertValue - This is a small helper function to be called only
796// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000797//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000798int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000799 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000800 unsigned Ty;
801
802 // Used for debugging DefSlot=-1 assertion...
803 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000804 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000805
Chris Lattner00950542001-06-06 20:29:01 +0000806 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000807 int ValSlot;
808 if (CompactionTable.empty())
809 ValSlot = getSlot(Typ);
810 else
811 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000812 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000813 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000814 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000815 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000816 }
Chris Lattner3413d152003-03-19 20:57:22 +0000817 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000818 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000819 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000820 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000821
Chris Lattner00950542001-06-06 20:29:01 +0000822 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
823 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000824
825 // If this is the first value to get inserted into the type plane, make sure
826 // to insert the implicit null value...
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000827 if (Table[Ty].empty() && hasNullValue(Typ)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000828 Value *ZeroInitializer = Constant::getNullValue(Typ);
829
830 // If we are pushing zeroinit, it will be handled below.
831 if (D != ZeroInitializer) {
832 Table[Ty].push_back(ZeroInitializer);
833 NodeMap[ZeroInitializer] = 0;
834 }
835 }
836
Chris Lattner9a297902001-09-07 16:31:52 +0000837 // Insert node into table and NodeMap...
838 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000839 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000840
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000841 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000842 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000843 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000844 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000845 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000846 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000847 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000848}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000849
850// doInsertType - This is a small helper function to be called only
851// be insertType.
852//
853int SlotCalculator::doInsertType(const Type *Ty) {
854
855 // Insert node into table and NodeMap...
856 unsigned DestSlot = TypeMap[Ty] = Types.size();
857 Types.push_back(Ty);
858
859 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
860 return (int)DestSlot;
861}
862