blob: 2d4cd0c4cb4c15f9cecb6212085cefd60c91b502 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman23c6d2c2005-04-21 21:48:46 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000010// This file implements a useful analysis step to figure out what numbered slots
11// values in a program will land in (keeping track of per plane information).
Chris Lattner00950542001-06-06 20:29:01 +000012//
Chris Lattner68e3dbc2004-01-20 00:57:32 +000013// This is used when writing a file to disk, either in bytecode or assembly.
Chris Lattner00950542001-06-06 20:29:01 +000014//
15//===----------------------------------------------------------------------===//
16
Reid Spencer24ab28f2004-07-04 11:42:49 +000017#include "SlotCalculator.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000018#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/DerivedTypes.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000020#include "llvm/Function.h"
Chris Lattner3bc5a602006-01-25 23:08:15 +000021#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000022#include "llvm/Instructions.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000023#include "llvm/Module.h"
Chris Lattner9a297902001-09-07 16:31:52 +000024#include "llvm/SymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000025#include "llvm/TypeSymbolTable.h"
Reid Spencer24ab28f2004-07-04 11:42:49 +000026#include "llvm/Type.h"
Chris Lattnerf2d577b2004-01-20 19:50:34 +000027#include "llvm/Analysis/ConstantsScanner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/PostOrderIterator.h"
29#include "llvm/ADT/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000030#include <algorithm>
Reid Spencer24ab28f2004-07-04 11:42:49 +000031#include <functional>
Chris Lattner31f84992003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Reid Spencera54b7cb2007-01-12 07:05:14 +000034#ifndef NDEBUG
Bill Wendling68fe61d2006-11-29 00:19:40 +000035#include "llvm/Support/Streams.h"
Reid Spencera54b7cb2007-01-12 07:05:14 +000036#include "llvm/Support/CommandLine.h"
37static cl::opt<bool> SlotCalculatorDebugOption("scdebug",cl::init(false),
38 cl::desc("Enable SlotCalculator debug output"), cl::Hidden);
39#define SC_DEBUG(X) if (SlotCalculatorDebugOption) cerr << X
Chris Lattner9a297902001-09-07 16:31:52 +000040#else
41#define SC_DEBUG(X)
42#endif
Chris Lattner00950542001-06-06 20:29:01 +000043
Reid Spencera54b7cb2007-01-12 07:05:14 +000044void SlotCalculator::insertPrimitives() {
45 // Preload the table with the built-in types. These built-in types are
46 // inserted first to ensure that they have low integer indices which helps to
47 // keep bytecode sizes small. Note that the first group of indices must match
48 // the Type::TypeIDs for the primitive types. After that the integer types are
49 // added, but the order and value is not critical. What is critical is that
50 // the indices of these "well known" slot numbers be properly maintained in
51 // Reader.h which uses them directly to extract values of these types.
52 SC_DEBUG("Inserting primitive types:\n");
53 // See WellKnownTypeSlots in Reader.h
54 insertType(Type::VoidTy, true); // 0: VoidTySlot
55 insertType(Type::FloatTy, true); // 1: FloatTySlot
56 insertType(Type::DoubleTy, true); // 2: DoubleTySlot
57 insertType(Type::LabelTy, true); // 3: LabelTySlot
58 assert(TypeMap.size() == Type::FirstDerivedTyID && "Invalid primitive insert");
59 // Above here *must* correspond 1:1 with the primitive types.
60 insertType(Type::Int1Ty, true); // 4: BoolTySlot
61 insertType(Type::Int8Ty, true); // 5: Int8TySlot
62 insertType(Type::Int16Ty, true); // 6: Int16TySlot
63 insertType(Type::Int32Ty, true); // 7: Int32TySlot
64 insertType(Type::Int64Ty, true); // 8: Int64TySlot
65}
66
Reid Spencer798ff642004-05-26 07:37:11 +000067SlotCalculator::SlotCalculator(const Module *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000068 ModuleContainsAllFunctionConstants = false;
Reid Spencer24ab28f2004-07-04 11:42:49 +000069 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +000070 TheModule = M;
71
Reid Spencera54b7cb2007-01-12 07:05:14 +000072 insertPrimitives();
Chris Lattner00950542001-06-06 20:29:01 +000073
74 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000075 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000076}
77
Reid Spencer798ff642004-05-26 07:37:11 +000078SlotCalculator::SlotCalculator(const Function *M ) {
Chris Lattner614cdcd2004-01-18 21:07:07 +000079 ModuleContainsAllFunctionConstants = false;
Chris Lattner00950542001-06-06 20:29:01 +000080 TheModule = M ? M->getParent() : 0;
81
Reid Spencera54b7cb2007-01-12 07:05:14 +000082 insertPrimitives();
Chris Lattner00950542001-06-06 20:29:01 +000083
84 if (TheModule == 0) return; // Empty table...
85
Chris Lattner9a297902001-09-07 16:31:52 +000086 processModule(); // Process module level stuff
Chris Lattner68e3dbc2004-01-20 00:57:32 +000087 incorporateFunction(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000088}
89
Chris Lattner614cdcd2004-01-18 21:07:07 +000090unsigned SlotCalculator::getGlobalSlot(const Value *V) const {
91 assert(!CompactionTable.empty() &&
92 "This method can only be used when compaction is enabled!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000093 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
Chris Lattner68e3dbc2004-01-20 00:57:32 +000094 assert(I != NodeMap.end() && "Didn't find global slot entry!");
Chris Lattner614cdcd2004-01-18 21:07:07 +000095 return I->second;
96}
97
Reid Spencer24ab28f2004-07-04 11:42:49 +000098unsigned SlotCalculator::getGlobalSlot(const Type* T) const {
99 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
100 assert(I != TypeMap.end() && "Didn't find global slot entry!");
101 return I->second;
102}
103
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000104SlotCalculator::TypePlane &SlotCalculator::getPlane(unsigned Plane) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000105 if (CompactionTable.empty()) { // No compaction table active?
106 // fall out
107 } else if (!CompactionTable[Plane].empty()) { // Compaction table active.
108 assert(Plane < CompactionTable.size());
109 return CompactionTable[Plane];
110 } else {
111 // Final case: compaction table active, but this plane is not
112 // compactified. If the type plane is compactified, unmap back to the
113 // global type plane corresponding to "Plane".
Reid Spencer24ab28f2004-07-04 11:42:49 +0000114 if (!CompactionTypes.empty()) {
115 const Type *Ty = CompactionTypes[Plane];
116 TypeMapType::iterator It = TypeMap.find(Ty);
117 assert(It != TypeMap.end() && "Type not in global constant map?");
118 Plane = It->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000119 }
120 }
121
122 // Okay we are just returning an entry out of the main Table. Make sure the
123 // plane exists and return it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000124 if (Plane >= Table.size())
125 Table.resize(Plane+1);
126 return Table[Plane];
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000127}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000128
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000129// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +0000130// types that are available.
131//
132void SlotCalculator::processModule() {
133 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +0000134
Chris Lattner3413d152003-03-19 20:57:22 +0000135 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +0000136 //
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)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000139 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +0000140
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 // Scavenge the types out of the functions, then add the functions themselves
142 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000143 //
Chris Lattner3413d152003-03-19 20:57:22 +0000144 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
145 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000146 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000147
Chris Lattner3413d152003-03-19 20:57:22 +0000148 // Add all of the module level constants used as initializers
149 //
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000150 for (Module::const_global_iterator I = TheModule->global_begin(),
151 E = TheModule->global_end(); I != E; ++I)
Chris Lattner3413d152003-03-19 20:57:22 +0000152 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000153 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000154
Chris Lattnerdcea6302004-01-14 23:34:39 +0000155 // Now that all global constants have been added, rearrange constant planes
156 // that contain constant strings so that the strings occur at the start of the
157 // plane, not somewhere in the middle.
158 //
Reid Spencer798ff642004-05-26 07:37:11 +0000159 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
160 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
Reid Spencer88cfda22006-12-31 05:44:24 +0000161 if (AT->getElementType() == Type::Int8Ty) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000162 TypePlane &Plane = Table[plane];
163 unsigned FirstNonStringID = 0;
164 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000165 if (isa<ConstantAggregateZero>(Plane[i]) ||
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000166 (isa<ConstantArray>(Plane[i]) &&
Chris Lattner236ca442004-10-24 04:27:59 +0000167 cast<ConstantArray>(Plane[i])->isString())) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000168 // Check to see if we have to shuffle this string around. If not,
169 // don't do anything.
170 if (i != FirstNonStringID) {
171 // Swap the plane entries....
172 std::swap(Plane[i], Plane[FirstNonStringID]);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000173
Reid Spencer24ab28f2004-07-04 11:42:49 +0000174 // Keep the NodeMap up to date.
175 NodeMap[Plane[i]] = i;
176 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
177 }
178 ++FirstNonStringID;
179 }
Reid Spencer798ff642004-05-26 07:37:11 +0000180 }
Chris Lattnerdcea6302004-01-14 23:34:39 +0000181 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000182
183 // Scan all of the functions for their constants, which allows us to emit
184 // more compact modules. This is optional, and is just used to compactify
Reid Spencere8404342004-07-18 00:18:30 +0000185 // the constants used by different functions together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000186 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000187 // This functionality tends to produce smaller bytecode files. This should
188 // not be used in the future by clients that want to, for example, build and
189 // emit functions on the fly. For now, however, it is unconditionally
Reid Spencere8404342004-07-18 00:18:30 +0000190 // enabled.
Reid Spencer798ff642004-05-26 07:37:11 +0000191 ModuleContainsAllFunctionConstants = true;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000192
Reid Spencer798ff642004-05-26 07:37:11 +0000193 SC_DEBUG("Inserting function constants:\n");
194 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
195 F != E; ++F) {
Chris Lattner3bc5a602006-01-25 23:08:15 +0000196 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
197 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
198 OI != E; ++OI) {
199 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
200 isa<InlineAsm>(*OI))
201 getOrCreateSlot(*OI);
202 }
Reid Spencer798ff642004-05-26 07:37:11 +0000203 getOrCreateSlot(I->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000204 }
Reid Spencer78d033e2007-01-06 07:24:44 +0000205 processSymbolTableConstants(&F->getValueSymbolTable());
Chris Lattnera14b0d42004-01-10 23:46:13 +0000206 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000207
Chris Lattner70cc3392001-09-10 07:58:01 +0000208 // Insert constants that are named at module level into the slot pool so that
209 // the module symbol table can refer to them...
Reid Spencer798ff642004-05-26 07:37:11 +0000210 SC_DEBUG("Inserting SymbolTable values:\n");
Reid Spencer78d033e2007-01-06 07:24:44 +0000211 processTypeSymbolTable(&TheModule->getTypeSymbolTable());
212 processValueSymbolTable(&TheModule->getValueSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000213
Chris Lattnera14b0d42004-01-10 23:46:13 +0000214 // Now that we have collected together all of the information relevant to the
215 // module, compactify the type table if it is particularly big and outputting
216 // a bytecode file. The basic problem we run into is that some programs have
217 // a large number of types, which causes the type field to overflow its size,
218 // which causes instructions to explode in size (particularly call
219 // instructions). To avoid this behavior, we "sort" the type table so that
220 // all non-value types are pushed to the end of the type table, giving nice
221 // low numbers to the types that can be used by instructions, thus reducing
222 // the amount of explodage we suffer.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000223 if (Types.size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000224 unsigned FirstNonValueTypeID = 0;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000225 for (unsigned i = 0, e = Types.size(); i != e; ++i)
226 if (Types[i]->isFirstClassType() || Types[i]->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000227 // Check to see if we have to shuffle this type around. If not, don't
228 // do anything.
229 if (i != FirstNonValueTypeID) {
230 // Swap the type ID's.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000231 std::swap(Types[i], Types[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000232
Reid Spencer24ab28f2004-07-04 11:42:49 +0000233 // Keep the TypeMap up to date.
234 TypeMap[Types[i]] = i;
235 TypeMap[Types[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000236
237 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000238 if (Table.size() > FirstNonValueTypeID) {
239 if (Table.size() <= i) Table.resize(i+1);
240 std::swap(Table[i], Table[FirstNonValueTypeID]);
Chris Lattner93802972004-01-11 23:29:26 +0000241 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000242 }
243 ++FirstNonValueTypeID;
244 }
245 }
246
Chris Lattner9a297902001-09-07 16:31:52 +0000247 SC_DEBUG("end processModule!\n");
248}
249
Reid Spencer78d033e2007-01-06 07:24:44 +0000250// processTypeSymbolTable - Insert all of the type sin the specified symbol
251// table.
252void SlotCalculator::processTypeSymbolTable(const TypeSymbolTable *ST) {
253 for (TypeSymbolTable::const_iterator TI = ST->begin(), TE = ST->end();
254 TI != TE; ++TI )
255 getOrCreateSlot(TI->second);
256}
257
Chris Lattner9a297902001-09-07 16:31:52 +0000258// processSymbolTable - Insert all of the values in the specified symbol table
259// into the values table...
260//
Reid Spencer78d033e2007-01-06 07:24:44 +0000261void SlotCalculator::processValueSymbolTable(const SymbolTable *ST) {
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000262 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000263 PE = ST->plane_end(); PI != PE; ++PI)
264 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000265 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencer9231ac82004-05-25 08:53:40 +0000266 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000267}
268
269void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
Reid Spencer9231ac82004-05-25 08:53:40 +0000270 // Now do the constant values in all planes
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000271 for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000272 PE = ST->plane_end(); PI != PE; ++PI)
273 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000274 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000275 if (isa<Constant>(VI->second) &&
276 !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000277 getOrCreateSlot(VI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000278}
279
280
Chris Lattnerce439b52003-10-20 19:10:06 +0000281void SlotCalculator::incorporateFunction(const Function *F) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000282 assert((ModuleLevel.size() == 0 ||
283 ModuleTypeLevel == 0) && "Module already incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000284
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000286
Chris Lattner614cdcd2004-01-18 21:07:07 +0000287 // If we emitted all of the function constants, build a compaction table.
Chris Lattner101cefa2006-06-07 22:20:03 +0000288 if (ModuleContainsAllFunctionConstants)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000289 buildCompactionTable(F);
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000290
291 // Update the ModuleLevel entries to be accurate.
292 ModuleLevel.resize(getNumPlanes());
293 for (unsigned i = 0, e = getNumPlanes(); i != e; ++i)
294 ModuleLevel[i] = getPlane(i).size();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000295 ModuleTypeLevel = Types.size();
Chris Lattner9a297902001-09-07 16:31:52 +0000296
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000297 // Iterate over function arguments, adding them to the value table...
Chris Lattnere4d5c442005-03-15 04:54:21 +0000298 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000299 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000300
Chris Lattner3bc5a602006-01-25 23:08:15 +0000301 if (!ModuleContainsAllFunctionConstants) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000302 // Iterate over all of the instructions in the function, looking for
303 // constant values that are referenced. Add these to the value pools
304 // before any nonconstant values. This will be turned into the constant
305 // pool for the bytecode writer.
306 //
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000307
Chris Lattner614cdcd2004-01-18 21:07:07 +0000308 // Emit all of the constants that are being used by the instructions in
309 // the function...
Chris Lattner3bc5a602006-01-25 23:08:15 +0000310 for (constant_iterator CI = constant_begin(F), CE = constant_end(F);
311 CI != CE; ++CI)
312 getOrCreateSlot(*CI);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000313
Chris Lattner9a297902001-09-07 16:31:52 +0000314 // If there is a symbol table, it is possible that the user has names for
315 // constants that are not being used. In this case, we will have problems
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000316 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000317 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000318 // constants now.
319 //
Reid Spencer78d033e2007-01-06 07:24:44 +0000320 processSymbolTableConstants(&F->getValueSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000321 }
322
Chris Lattner9a297902001-09-07 16:31:52 +0000323 SC_DEBUG("Inserting Instructions:\n");
324
325 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000326 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
327 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000328 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
329 getOrCreateSlot(I);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000330 }
Chris Lattner9a297902001-09-07 16:31:52 +0000331 }
332
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000333 // If we are building a compaction table, prune out planes that do not benefit
334 // from being compactified.
335 if (!CompactionTable.empty())
336 pruneCompactionTable();
337
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000338 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000339}
340
Chris Lattnerb5794002002-04-07 22:49:37 +0000341void SlotCalculator::purgeFunction() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000342 assert((ModuleLevel.size() != 0 ||
343 ModuleTypeLevel != 0) && "Module not incorporated!");
Chris Lattner00950542001-06-06 20:29:01 +0000344 unsigned NumModuleTypes = ModuleLevel.size();
345
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000346 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000347
Chris Lattner614cdcd2004-01-18 21:07:07 +0000348 // First, free the compaction map if used.
349 CompactionNodeMap.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000350 CompactionTypeMap.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000351
352 // Next, remove values from existing type planes
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000353 for (unsigned i = 0; i != NumModuleTypes; ++i) {
354 // Size of plane before function came
355 unsigned ModuleLev = getModuleLevel(i);
356 assert(int(ModuleLev) >= 0 && "BAD!");
357
358 TypePlane &Plane = getPlane(i);
359
360 assert(ModuleLev <= Plane.size() && "module levels higher than elements?");
361 while (Plane.size() != ModuleLev) {
362 assert(!isa<GlobalValue>(Plane.back()) &&
363 "Functions cannot define globals!");
364 NodeMap.erase(Plane.back()); // Erase from nodemap
365 Plane.pop_back(); // Shrink plane
Chris Lattner00950542001-06-06 20:29:01 +0000366 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000367 }
Chris Lattner00950542001-06-06 20:29:01 +0000368
369 // We don't need this state anymore, free it up.
370 ModuleLevel.clear();
Reid Spencer24ab28f2004-07-04 11:42:49 +0000371 ModuleTypeLevel = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000372
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000373 // Finally, remove any type planes defined by the function...
Reid Spencer24ab28f2004-07-04 11:42:49 +0000374 CompactionTypes.clear();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000375 if (!CompactionTable.empty()) {
376 CompactionTable.clear();
377 } else {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000378 while (Table.size() > NumModuleTypes) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000379 TypePlane &Plane = Table.back();
380 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
381 << Plane.size() << "\n");
382 while (Plane.size()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000383 assert(!isa<GlobalValue>(Plane.back()) &&
384 "Functions cannot define globals!");
385 NodeMap.erase(Plane.back()); // Erase from nodemap
386 Plane.pop_back(); // Shrink plane
Chris Lattner614cdcd2004-01-18 21:07:07 +0000387 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000388
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000389 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000390 }
Chris Lattner00950542001-06-06 20:29:01 +0000391 }
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000392
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000393 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000394}
395
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000396static inline bool hasNullValue(const Type *Ty) {
397 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000398}
399
400/// getOrCreateCompactionTableSlot - This method is used to build up the initial
401/// approximation of the compaction table.
402unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
403 std::map<const Value*, unsigned>::iterator I =
404 CompactionNodeMap.lower_bound(V);
405 if (I != CompactionNodeMap.end() && I->first == V)
406 return I->second; // Already exists?
407
408 // Make sure the type is in the table.
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000409 unsigned Ty;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000410 if (!CompactionTypes.empty())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000411 Ty = getOrCreateCompactionTableSlot(V->getType());
412 else // If the type plane was decompactified, use the global plane ID
413 Ty = getSlot(V->getType());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000414 if (CompactionTable.size() <= Ty)
415 CompactionTable.resize(Ty+1);
416
Chris Lattner614cdcd2004-01-18 21:07:07 +0000417 TypePlane &TyPlane = CompactionTable[Ty];
418
419 // Make sure to insert the null entry if the thing we are inserting is not a
420 // null constant.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000421 if (TyPlane.empty() && hasNullValue(V->getType())) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000422 Value *ZeroInitializer = Constant::getNullValue(V->getType());
423 if (V != ZeroInitializer) {
424 TyPlane.push_back(ZeroInitializer);
425 CompactionNodeMap[ZeroInitializer] = 0;
426 }
427 }
428
429 unsigned SlotNo = TyPlane.size();
430 TyPlane.push_back(V);
431 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
432 return SlotNo;
433}
434
Reid Spencer24ab28f2004-07-04 11:42:49 +0000435/// getOrCreateCompactionTableSlot - This method is used to build up the initial
436/// approximation of the compaction table.
437unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000438 CompactionTypeMapType::iterator I = CompactionTypeMap.lower_bound(T);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000439 if (I != CompactionTypeMap.end() && I->first == T)
440 return I->second; // Already exists?
441
442 unsigned SlotNo = CompactionTypes.size();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000443 SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << *T << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000444 CompactionTypes.push_back(T);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000445 CompactionTypeMap[T] = SlotNo;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000446 return SlotNo;
447}
Chris Lattner614cdcd2004-01-18 21:07:07 +0000448
449/// buildCompactionTable - Since all of the function constants and types are
450/// stored in the module-level constant table, we don't need to emit a function
451/// constant table. Also due to this, the indices for various constants and
452/// types might be very large in large programs. In order to avoid blowing up
453/// the size of instructions in the bytecode encoding, we build a compaction
454/// table, which defines a mapping from function-local identifiers to global
455/// identifiers.
456void SlotCalculator::buildCompactionTable(const Function *F) {
457 assert(CompactionNodeMap.empty() && "Compaction table already built!");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000458 assert(CompactionTypeMap.empty() && "Compaction types already built!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000459 // First step, insert the primitive types.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000460 CompactionTable.resize(Type::LastPrimitiveTyID+1);
461 for (unsigned i = 0; i <= Type::LastPrimitiveTyID; ++i) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000462 const Type *PrimTy = Type::getPrimitiveType((Type::TypeID)i);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000463 CompactionTypes.push_back(PrimTy);
464 CompactionTypeMap[PrimTy] = i;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000465 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000466 CompactionTypeMap[Type::Int1Ty] = CompactionTypes.size();
467 CompactionTypes.push_back(Type::Int1Ty);
468 CompactionTypeMap[Type::Int8Ty] = CompactionTypes.size();
469 CompactionTypes.push_back(Type::Int8Ty);
470 CompactionTypeMap[Type::Int16Ty] = CompactionTypes.size();
471 CompactionTypes.push_back(Type::Int16Ty);
472 CompactionTypeMap[Type::Int32Ty] = CompactionTypes.size();
473 CompactionTypes.push_back(Type::Int32Ty);
474 CompactionTypeMap[Type::Int64Ty] = CompactionTypes.size();
475 CompactionTypes.push_back(Type::Int64Ty);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000476
477 // Next, include any types used by function arguments.
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000478 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
479 I != E; ++I)
Chris Lattner614cdcd2004-01-18 21:07:07 +0000480 getOrCreateCompactionTableSlot(I->getType());
481
482 // Next, find all of the types and values that are referred to by the
Reid Spencer24ab28f2004-07-04 11:42:49 +0000483 // instructions in the function.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000484 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
485 getOrCreateCompactionTableSlot(I->getType());
486 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Chris Lattner101cefa2006-06-07 22:20:03 +0000487 if (isa<Constant>(I->getOperand(op)) || isa<InlineAsm>(I->getOperand(op)))
Chris Lattner614cdcd2004-01-18 21:07:07 +0000488 getOrCreateCompactionTableSlot(I->getOperand(op));
Chris Lattner614cdcd2004-01-18 21:07:07 +0000489 }
490
Reid Spencer9231ac82004-05-25 08:53:40 +0000491 // Now do the constants and global values
Reid Spencer78d033e2007-01-06 07:24:44 +0000492 const SymbolTable &ST = F->getValueSymbolTable();
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000493 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer9231ac82004-05-25 08:53:40 +0000494 PE = ST.plane_end(); PI != PE; ++PI)
495 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
Reid Spencer24ab28f2004-07-04 11:42:49 +0000496 VE = PI->second.end(); VI != VE; ++VI)
Reid Spencere8404342004-07-18 00:18:30 +0000497 if (isa<Constant>(VI->second) && !isa<GlobalValue>(VI->second))
Reid Spencer24ab28f2004-07-04 11:42:49 +0000498 getOrCreateCompactionTableSlot(VI->second);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000499
500 // Now that we have all of the values in the table, and know what types are
501 // referenced, make sure that there is at least the zero initializer in any
502 // used type plane. Since the type was used, we will be emitting instructions
503 // to the plane even if there are no constants in it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000504 CompactionTable.resize(CompactionTypes.size());
Chris Lattner614cdcd2004-01-18 21:07:07 +0000505 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000506 if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
Chris Lattner614cdcd2004-01-18 21:07:07 +0000507 i != Type::LabelTyID) {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000508 const Type *Ty = CompactionTypes[i];
Reid Spencera54b7cb2007-01-12 07:05:14 +0000509 SC_DEBUG("Getting Null Value #" << i << " for Type " << *Ty << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000510 assert(Ty->getTypeID() != Type::VoidTyID);
511 assert(Ty->getTypeID() != Type::LabelTyID);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000512 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
513 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000514
Chris Lattner614cdcd2004-01-18 21:07:07 +0000515 // Okay, now at this point, we have a legal compaction table. Since we want
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000516 // to emit the smallest possible binaries, do not compactify the type plane if
517 // it will not save us anything. Because we have not yet incorporated the
518 // function body itself yet, we don't know whether or not it's a good idea to
519 // compactify other planes. We will defer this decision until later.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000520 TypeList &GlobalTypes = Types;
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000521
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000522 // All of the values types will be scrunched to the start of the types plane
523 // of the global table. Figure out just how many there are.
524 assert(!GlobalTypes.empty() && "No global types???");
525 unsigned NumFCTypes = GlobalTypes.size()-1;
Reid Spencer24ab28f2004-07-04 11:42:49 +0000526 while (!GlobalTypes[NumFCTypes]->isFirstClassType())
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000527 --NumFCTypes;
Chris Lattner614cdcd2004-01-18 21:07:07 +0000528
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000529 // If there are fewer that 64 types, no instructions will be exploded due to
530 // the size of the type operands. Thus there is no need to compactify types.
531 // Also, if the compaction table contains most of the entries in the global
532 // table, there really is no reason to compactify either.
533 if (NumFCTypes < 64) {
534 // Decompactifying types is tricky, because we have to move type planes all
535 // over the place. At least we don't need to worry about updating the
536 // CompactionNodeMap for non-types though.
537 std::vector<TypePlane> TmpCompactionTable;
538 std::swap(CompactionTable, TmpCompactionTable);
Reid Spencer24ab28f2004-07-04 11:42:49 +0000539 TypeList TmpTypes;
540 std::swap(TmpTypes, CompactionTypes);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000541
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000542 // Move each plane back over to the uncompactified plane
Reid Spencer24ab28f2004-07-04 11:42:49 +0000543 while (!TmpTypes.empty()) {
544 const Type *Ty = TmpTypes.back();
545 TmpTypes.pop_back();
546 CompactionTypeMap.erase(Ty); // Decompactify type!
Chris Lattner614cdcd2004-01-18 21:07:07 +0000547
Reid Spencer24ab28f2004-07-04 11:42:49 +0000548 // Find the global slot number for this type.
549 int TySlot = getSlot(Ty);
550 assert(TySlot != -1 && "Type doesn't exist in global table?");
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000551
Reid Spencer24ab28f2004-07-04 11:42:49 +0000552 // Now we know where to put the compaction table plane.
553 if (CompactionTable.size() <= unsigned(TySlot))
554 CompactionTable.resize(TySlot+1);
555 // Move the plane back into the compaction table.
556 std::swap(CompactionTable[TySlot], TmpCompactionTable[TmpTypes.size()]);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000557
Reid Spencer24ab28f2004-07-04 11:42:49 +0000558 // And remove the empty plane we just moved in.
559 TmpCompactionTable.pop_back();
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000560 }
561 }
Chris Lattner614cdcd2004-01-18 21:07:07 +0000562}
563
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000564
565/// pruneCompactionTable - Once the entire function being processed has been
566/// incorporated into the current compaction table, look over the compaction
567/// table and check to see if there are any values whose compaction will not
568/// save us any space in the bytecode file. If compactifying these values
569/// serves no purpose, then we might as well not even emit the compactification
570/// information to the bytecode file, saving a bit more space.
571///
572/// Note that the type plane has already been compactified if possible.
573///
574void SlotCalculator::pruneCompactionTable() {
Reid Spencer24ab28f2004-07-04 11:42:49 +0000575 TypeList &TyPlane = CompactionTypes;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000576 for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
Reid Spencer24ab28f2004-07-04 11:42:49 +0000577 if (!CompactionTable[ctp].empty()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000578 TypePlane &CPlane = CompactionTable[ctp];
579 unsigned GlobalSlot = ctp;
580 if (!TyPlane.empty())
581 GlobalSlot = getGlobalSlot(TyPlane[ctp]);
582
583 if (GlobalSlot >= Table.size())
584 Table.resize(GlobalSlot+1);
585 TypePlane &GPlane = Table[GlobalSlot];
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000586
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000587 unsigned ModLevel = getModuleLevel(ctp);
588 unsigned NumFunctionObjs = CPlane.size()-ModLevel;
589
590 // If the maximum index required if all entries in this plane were merged
591 // into the global plane is less than 64, go ahead and eliminate the
592 // plane.
593 bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
594
595 // If there are no function-local values defined, and the maximum
596 // referenced global entry is less than 64, we don't need to compactify.
597 if (!PrunePlane && NumFunctionObjs == 0) {
598 unsigned MaxIdx = 0;
599 for (unsigned i = 0; i != ModLevel; ++i) {
600 unsigned Idx = NodeMap[CPlane[i]];
601 if (Idx > MaxIdx) MaxIdx = Idx;
602 }
603 PrunePlane = MaxIdx < 64;
604 }
605
606 // Ok, finally, if we decided to prune this plane out of the compaction
607 // table, do so now.
608 if (PrunePlane) {
609 TypePlane OldPlane;
610 std::swap(OldPlane, CPlane);
611
612 // Loop over the function local objects, relocating them to the global
613 // table plane.
614 for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
615 const Value *V = OldPlane[i];
616 CompactionNodeMap.erase(V);
617 assert(NodeMap.count(V) == 0 && "Value already in table??");
618 getOrCreateSlot(V);
619 }
620
621 // For compactified global values, just remove them from the compaction
622 // node map.
623 for (unsigned i = 0; i != ModLevel; ++i)
624 CompactionNodeMap.erase(OldPlane[i]);
625
626 // Update the new modulelevel for this plane.
627 assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
628 ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
629 assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
630 }
631 }
632}
633
Reid Spencer07ea1912004-08-26 22:32:00 +0000634/// Determine if the compaction table is actually empty. Because the
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000635/// compaction table always includes the primitive type planes, we
Reid Spencer07ea1912004-08-26 22:32:00 +0000636/// can't just check getCompactionTable().size() because it will never
637/// be zero. Furthermore, the ModuleLevel factors into whether a given
638/// plane is empty or not. This function does the necessary computation
639/// to determine if its actually empty.
640bool SlotCalculator::CompactionTableIsEmpty() const {
641 // Check a degenerate case, just in case.
Reid Spencera54b7cb2007-01-12 07:05:14 +0000642 if (CompactionTable.size() == 0)
643 return true;
Reid Spencer07ea1912004-08-26 22:32:00 +0000644
645 // Check each plane
646 for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
647 // If the plane is not empty
648 if (!CompactionTable[i].empty()) {
649 // If the module level is non-zero then at least the
650 // first element of the plane is valid and therefore not empty.
651 unsigned End = getModuleLevel(i);
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000652 if (End != 0)
Reid Spencer07ea1912004-08-26 22:32:00 +0000653 return false;
654 }
655 }
656 // All the compaction table planes are empty so the table is
657 // considered empty too.
658 return true;
659}
660
Chris Lattner8c202cd2004-01-15 18:47:15 +0000661int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000662 // If there is a CompactionTable active...
663 if (!CompactionNodeMap.empty()) {
664 std::map<const Value*, unsigned>::const_iterator I =
665 CompactionNodeMap.find(V);
666 if (I != CompactionNodeMap.end())
667 return (int)I->second;
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000668 // Otherwise, if it's not in the compaction table, it must be in a
669 // non-compactified plane.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000670 }
671
Chris Lattner8c202cd2004-01-15 18:47:15 +0000672 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
673 if (I != NodeMap.end())
674 return (int)I->second;
675
Chris Lattner8c202cd2004-01-15 18:47:15 +0000676 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000677}
678
Reid Spencer24ab28f2004-07-04 11:42:49 +0000679int SlotCalculator::getSlot(const Type*T) const {
680 // If there is a CompactionTable active...
681 if (!CompactionTypeMap.empty()) {
682 std::map<const Type*, unsigned>::const_iterator I =
683 CompactionTypeMap.find(T);
684 if (I != CompactionTypeMap.end())
685 return (int)I->second;
686 // Otherwise, if it's not in the compaction table, it must be in a
687 // non-compactified plane.
688 }
689
690 std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
691 if (I != TypeMap.end())
692 return (int)I->second;
693
694 return -1;
695}
Chris Lattner9a297902001-09-07 16:31:52 +0000696
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000697int SlotCalculator::getOrCreateSlot(const Value *V) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000698 if (V->getType() == Type::VoidTy) return -1;
699
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000700 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000701 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000702
Chris Lattner129baf62004-12-04 21:28:47 +0000703 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
704 assert(GV->getParent() != 0 && "Global not embedded into a module!");
705
Chris Lattner614cdcd2004-01-18 21:07:07 +0000706 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000707 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000708 assert(CompactionNodeMap.empty() &&
709 "All needed constants should be in the compaction map already!");
710
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000711 // Do not index the characters that make up constant strings. We emit
712 // constant strings as special entities that don't require their
Reid Spencer798ff642004-05-26 07:37:11 +0000713 // individual characters to be emitted.
714 if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
Chris Lattnerdcea6302004-01-14 23:34:39 +0000715 // This makes sure that if a constant has uses (for example an array of
716 // const ints), that they are inserted also.
717 //
718 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
719 I != E; ++I)
720 getOrCreateSlot(*I);
721 } else {
722 assert(ModuleLevel.empty() &&
723 "How can a constant string be directly accessed in a function?");
724 // Otherwise, if we are emitting a bytecode file and this IS a string,
725 // remember it.
726 if (!C->isNullValue())
727 ConstantStrings.push_back(cast<ConstantArray>(C));
728 }
Chris Lattner3413d152003-03-19 20:57:22 +0000729 }
730
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000731 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000732}
733
Reid Spencer24ab28f2004-07-04 11:42:49 +0000734int SlotCalculator::getOrCreateSlot(const Type* T) {
735 int SlotNo = getSlot(T); // Check to see if it's already in!
736 if (SlotNo != -1) return SlotNo;
737 return insertType(T);
738}
Chris Lattner9a297902001-09-07 16:31:52 +0000739
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000740int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000741 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000742 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000743
Chris Lattner614cdcd2004-01-18 21:07:07 +0000744 // If we are building a compaction map, and if this plane is being compacted,
745 // insert the value into the compaction map, not into the global map.
746 if (!CompactionNodeMap.empty()) {
747 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
Reid Spencere8404342004-07-18 00:18:30 +0000748 assert(!isa<Constant>(D) &&
749 "Types, constants, and globals should be in global table!");
Chris Lattner614cdcd2004-01-18 21:07:07 +0000750
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000751 int Plane = getSlot(D->getType());
752 assert(Plane != -1 && CompactionTable.size() > (unsigned)Plane &&
753 "Didn't find value type!");
754 if (!CompactionTable[Plane].empty())
755 return getOrCreateCompactionTableSlot(D);
Chris Lattner614cdcd2004-01-18 21:07:07 +0000756 }
757
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000758 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000759 // name and we don't want names, then ignore the silly node... Note that types
760 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000761 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000762 if (!dontIgnore) // Don't ignore nonignorables!
Reid Spencer798ff642004-05-26 07:37:11 +0000763 if (D->getType() == Type::VoidTy ) { // Ignore void type nodes
Chris Lattner3413d152003-03-19 20:57:22 +0000764 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000765 return -1; // We do need types unconditionally though
766 }
Chris Lattner00950542001-06-06 20:29:01 +0000767
Chris Lattner9a297902001-09-07 16:31:52 +0000768 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000769 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000770}
771
Reid Spencer24ab28f2004-07-04 11:42:49 +0000772int SlotCalculator::insertType(const Type *Ty, bool dontIgnore) {
773 assert(Ty && "Can't insert a null type!");
774 assert(getSlot(Ty) == -1 && "Type is already in the table!");
775
776 // If we are building a compaction map, and if this plane is being compacted,
777 // insert the value into the compaction map, not into the global map.
778 if (!CompactionTypeMap.empty()) {
779 getOrCreateCompactionTableSlot(Ty);
780 }
781
782 // Insert the current type before any subtypes. This is important because
783 // recursive types elements are inserted in a bottom up order. Changing
784 // this here can break things. For example:
785 //
786 // global { \2 * } { { \2 }* null }
787 //
788 int ResultSlot = doInsertType(Ty);
789 SC_DEBUG(" Inserted type: " << Ty->getDescription() << " slot=" <<
790 ResultSlot << "\n");
791
792 // Loop over any contained types in the definition... in post
793 // order.
794 for (po_iterator<const Type*> I = po_begin(Ty), E = po_end(Ty);
795 I != E; ++I) {
796 if (*I != Ty) {
797 const Type *SubTy = *I;
798 // If we haven't seen this sub type before, add it to our type table!
799 if (getSlot(SubTy) == -1) {
800 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
Chris Lattneraec6dd52004-07-12 20:29:52 +0000801 doInsertType(SubTy);
Reid Spencere8404342004-07-18 00:18:30 +0000802 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() << "\n");
Reid Spencer24ab28f2004-07-04 11:42:49 +0000803 }
804 }
805 }
806 return ResultSlot;
807}
808
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000809// doInsertValue - This is a small helper function to be called only
810// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000811//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000812int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000813 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000814 unsigned Ty;
815
816 // Used for debugging DefSlot=-1 assertion...
817 //if (Typ == Type::TypeTy)
Bill Wendling68fe61d2006-11-29 00:19:40 +0000818 // llvm_cerr << "Inserting type '"<<cast<Type>(D)->getDescription() <<"'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000819
Chris Lattner00950542001-06-06 20:29:01 +0000820 if (Typ->isDerivedType()) {
Chris Lattner68e3dbc2004-01-20 00:57:32 +0000821 int ValSlot;
822 if (CompactionTable.empty())
823 ValSlot = getSlot(Typ);
824 else
825 ValSlot = getGlobalSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000826 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000827 // Nope, this is the first we have seen the type, process it.
Reid Spencer24ab28f2004-07-04 11:42:49 +0000828 ValSlot = insertType(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000829 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000830 }
Chris Lattner3413d152003-03-19 20:57:22 +0000831 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000832 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000833 Ty = Typ->getTypeID();
Chris Lattner00950542001-06-06 20:29:01 +0000834 }
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000835
Chris Lattner00950542001-06-06 20:29:01 +0000836 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
837 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000838
839 // If this is the first value to get inserted into the type plane, make sure
840 // to insert the implicit null value...
Chris Lattner9e60d8d2005-05-05 22:21:19 +0000841 if (Table[Ty].empty() && hasNullValue(Typ)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000842 Value *ZeroInitializer = Constant::getNullValue(Typ);
843
844 // If we are pushing zeroinit, it will be handled below.
845 if (D != ZeroInitializer) {
846 Table[Ty].push_back(ZeroInitializer);
847 NodeMap[ZeroInitializer] = 0;
848 }
849 }
850
Chris Lattner9a297902001-09-07 16:31:52 +0000851 // Insert node into table and NodeMap...
852 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000853 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000854
Reid Spencera54b7cb2007-01-12 07:05:14 +0000855 SC_DEBUG(" Inserting value [" << Ty << "] = " << *D << " slot=" <<
Reid Spencer24ab28f2004-07-04 11:42:49 +0000856 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000857 // G = Global, C = Constant, T = Type, F = Function, o = other
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000858 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Reid Spencer24ab28f2004-07-04 11:42:49 +0000859 (isa<Function>(D) ? "F" : "o"))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000860 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000861 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000862}
Reid Spencer24ab28f2004-07-04 11:42:49 +0000863
864// doInsertType - This is a small helper function to be called only
865// be insertType.
866//
867int SlotCalculator::doInsertType(const Type *Ty) {
868
869 // Insert node into table and NodeMap...
870 unsigned DestSlot = TypeMap[Ty] = Types.size();
871 Types.push_back(Ty);
872
Reid Spencera54b7cb2007-01-12 07:05:14 +0000873 SC_DEBUG(" Inserting type [" << DestSlot << "] = " << *Ty << "\n" );
Reid Spencer24ab28f2004-07-04 11:42:49 +0000874 return (int)DestSlot;
875}