blob: 1ba895ebdaec5fc99dbecae722a126db20cb2399 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements a useful analysis step to figure out what numbered
11// slots values in a program will land in (keeping track of per plane
12// information as required.
13//
14// This is used primarily for when writing a file to disk, either in bytecode
15// or source format.
16//
17//===----------------------------------------------------------------------===//
18
Chris Lattnerb5794002002-04-07 22:49:37 +000019#include "llvm/SlotCalculator.h"
Chris Lattner9a297902001-09-07 16:31:52 +000020#include "llvm/Analysis/ConstantsScanner.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000021#include "llvm/Constants.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerdcea6302004-01-14 23:34:39 +000023#include "llvm/iOther.h"
24#include "llvm/Module.h"
Chris Lattner9a297902001-09-07 16:31:52 +000025#include "llvm/SymbolTable.h"
Alkis Evlogimenos088eb452003-10-31 03:02:34 +000026#include "Support/PostOrderIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/STLExtras.h"
Chris Lattner9a297902001-09-07 16:31:52 +000028#include <algorithm>
Chris Lattner31f84992003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner9a297902001-09-07 16:31:52 +000031#if 0
Chris Lattner3413d152003-03-19 20:57:22 +000032#define SC_DEBUG(X) std::cerr << X
Chris Lattner9a297902001-09-07 16:31:52 +000033#else
34#define SC_DEBUG(X)
35#endif
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner8ce75012004-01-14 02:49:34 +000037SlotCalculator::SlotCalculator(const Module *M, bool buildBytecodeInfo) {
38 BuildBytecodeInfo = buildBytecodeInfo;
Chris Lattner614cdcd2004-01-18 21:07:07 +000039 ModuleContainsAllFunctionConstants = false;
Chris Lattner00950542001-06-06 20:29:01 +000040 TheModule = M;
41
42 // Preload table... Make sure that all of the primitive types are in the table
43 // and that their Primitive ID is equal to their slot #
44 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000045 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000046 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
47 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Alkis Evlogimenos60596382003-10-17 02:02:40 +000048 insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000049 }
50
51 if (M == 0) return; // Empty table...
Chris Lattner9a297902001-09-07 16:31:52 +000052 processModule();
Chris Lattner00950542001-06-06 20:29:01 +000053}
54
Chris Lattner8ce75012004-01-14 02:49:34 +000055SlotCalculator::SlotCalculator(const Function *M, bool buildBytecodeInfo) {
56 BuildBytecodeInfo = buildBytecodeInfo;
Chris Lattner614cdcd2004-01-18 21:07:07 +000057 ModuleContainsAllFunctionConstants = false;
Chris Lattner00950542001-06-06 20:29:01 +000058 TheModule = M ? M->getParent() : 0;
59
60 // Preload table... Make sure that all of the primitive types are in the table
61 // and that their Primitive ID is equal to their slot #
62 //
Alkis Evlogimenos5d1bdcd2003-10-29 03:12:12 +000063 SC_DEBUG("Inserting primitive types:\n");
Chris Lattner00950542001-06-06 20:29:01 +000064 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
65 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
Alkis Evlogimenos60596382003-10-17 02:02:40 +000066 insertValue(Type::getPrimitiveType((Type::PrimitiveID)i), true);
Chris Lattner00950542001-06-06 20:29:01 +000067 }
68
69 if (TheModule == 0) return; // Empty table...
70
Chris Lattner9a297902001-09-07 16:31:52 +000071 processModule(); // Process module level stuff
Chris Lattnerb5794002002-04-07 22:49:37 +000072 incorporateFunction(M); // Start out in incorporated state
Chris Lattner00950542001-06-06 20:29:01 +000073}
74
Chris Lattner614cdcd2004-01-18 21:07:07 +000075unsigned SlotCalculator::getGlobalSlot(const Value *V) const {
76 assert(!CompactionTable.empty() &&
77 "This method can only be used when compaction is enabled!");
78 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
79 V = CPR->getValue();
80 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
81 assert(I != NodeMap.end() && "Didn't find entry!");
82 return I->second;
83}
84
85
Chris Lattner9a297902001-09-07 16:31:52 +000086
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000087// processModule - Process all of the module level function declarations and
Chris Lattner9a297902001-09-07 16:31:52 +000088// types that are available.
89//
90void SlotCalculator::processModule() {
91 SC_DEBUG("begin processModule!\n");
Chris Lattner70cc3392001-09-10 07:58:01 +000092
Chris Lattner3413d152003-03-19 20:57:22 +000093 // Add all of the global variables to the value table...
Chris Lattnerf1fef652001-10-13 06:35:09 +000094 //
95 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
Chris Lattner479b54b2002-10-14 00:48:57 +000096 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +000097 getOrCreateSlot(I);
Chris Lattner70cc3392001-09-10 07:58:01 +000098
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000099 // Scavenge the types out of the functions, then add the functions themselves
100 // to the value table...
Chris Lattner9a297902001-09-07 16:31:52 +0000101 //
Chris Lattner3413d152003-03-19 20:57:22 +0000102 for (Module::const_iterator I = TheModule->begin(), E = TheModule->end();
103 I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000104 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000105
Chris Lattner3413d152003-03-19 20:57:22 +0000106 // Add all of the module level constants used as initializers
107 //
108 for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
109 I != E; ++I)
110 if (I->hasInitializer())
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000111 getOrCreateSlot(I->getInitializer());
Chris Lattner3413d152003-03-19 20:57:22 +0000112
Chris Lattnerdcea6302004-01-14 23:34:39 +0000113 // Now that all global constants have been added, rearrange constant planes
114 // that contain constant strings so that the strings occur at the start of the
115 // plane, not somewhere in the middle.
116 //
117 if (BuildBytecodeInfo) {
118 TypePlane &Types = Table[Type::TypeTyID];
119 for (unsigned plane = 0, e = Table.size(); plane != e; ++plane) {
120 if (const ArrayType *AT = dyn_cast<ArrayType>(Types[plane]))
121 if (AT->getElementType() == Type::SByteTy ||
122 AT->getElementType() == Type::UByteTy) {
123 TypePlane &Plane = Table[plane];
124 unsigned FirstNonStringID = 0;
125 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
126 if (cast<ConstantArray>(Plane[i])->isString()) {
127 // Check to see if we have to shuffle this string around. If not,
128 // don't do anything.
129 if (i != FirstNonStringID) {
130 // Swap the plane entries....
131 std::swap(Plane[i], Plane[FirstNonStringID]);
132
133 // Keep the NodeMap up to date.
134 NodeMap[Plane[i]] = i;
135 NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
136 }
137 ++FirstNonStringID;
138 }
139 }
140 }
141 }
142
Chris Lattnera14b0d42004-01-10 23:46:13 +0000143 // If we are emitting a bytecode file, scan all of the functions for their
144 // constants, which allows us to emit more compact modules. This is optional,
145 // and is just used to compactify the constants used by different functions
146 // together.
Chris Lattner614cdcd2004-01-18 21:07:07 +0000147 //
148 // This functionality is completely optional for the bytecode writer, but
149 // tends to produce smaller bytecode files. This should not be used in the
150 // future by clients that want to, for example, build and emit functions on
151 // the fly. For now, however, it is unconditionally enabled when building
152 // bytecode information.
153 //
Chris Lattner8ce75012004-01-14 02:49:34 +0000154 if (BuildBytecodeInfo) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000155 ModuleContainsAllFunctionConstants = true;
156
Chris Lattnera14b0d42004-01-10 23:46:13 +0000157 SC_DEBUG("Inserting function constants:\n");
158 for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
Chris Lattner614cdcd2004-01-18 21:07:07 +0000159 F != E; ++F) {
160 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
161 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
162 if (isa<Constant>(I->getOperand(op)))
163 getOrCreateSlot(I->getOperand(op));
164 getOrCreateSlot(I->getType());
165 if (const VANextInst *VAN = dyn_cast<VANextInst>(*I))
166 getOrCreateSlot(VAN->getArgType());
167 }
168 processSymbolTableConstants(&F->getSymbolTable());
169 }
170
171
Chris Lattnera14b0d42004-01-10 23:46:13 +0000172 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000173
Chris Lattner70cc3392001-09-10 07:58:01 +0000174 // Insert constants that are named at module level into the slot pool so that
175 // the module symbol table can refer to them...
176 //
Chris Lattner8ce75012004-01-14 02:49:34 +0000177 if (BuildBytecodeInfo) {
Chris Lattner9a297902001-09-07 16:31:52 +0000178 SC_DEBUG("Inserting SymbolTable values:\n");
Chris Lattner6e6026b2002-11-20 18:36:02 +0000179 processSymbolTable(&TheModule->getSymbolTable());
Chris Lattner9a297902001-09-07 16:31:52 +0000180 }
181
Chris Lattnera14b0d42004-01-10 23:46:13 +0000182 // Now that we have collected together all of the information relevant to the
183 // module, compactify the type table if it is particularly big and outputting
184 // a bytecode file. The basic problem we run into is that some programs have
185 // a large number of types, which causes the type field to overflow its size,
186 // which causes instructions to explode in size (particularly call
187 // instructions). To avoid this behavior, we "sort" the type table so that
188 // all non-value types are pushed to the end of the type table, giving nice
189 // low numbers to the types that can be used by instructions, thus reducing
190 // the amount of explodage we suffer.
Chris Lattner8ce75012004-01-14 02:49:34 +0000191 if (BuildBytecodeInfo && Table[Type::TypeTyID].size() >= 64) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000192 // Scan through the type table moving value types to the start of the table.
Chris Lattner93802972004-01-11 23:29:26 +0000193 TypePlane *Types = &Table[Type::TypeTyID];
Chris Lattnera14b0d42004-01-10 23:46:13 +0000194 unsigned FirstNonValueTypeID = 0;
Chris Lattner93802972004-01-11 23:29:26 +0000195 for (unsigned i = 0, e = Types->size(); i != e; ++i)
196 if (cast<Type>((*Types)[i])->isFirstClassType() ||
197 cast<Type>((*Types)[i])->isPrimitiveType()) {
Chris Lattnera14b0d42004-01-10 23:46:13 +0000198 // Check to see if we have to shuffle this type around. If not, don't
199 // do anything.
200 if (i != FirstNonValueTypeID) {
Chris Lattner93802972004-01-11 23:29:26 +0000201 assert(i != Type::TypeTyID && FirstNonValueTypeID != Type::TypeTyID &&
202 "Cannot move around the type plane!");
203
Chris Lattnera14b0d42004-01-10 23:46:13 +0000204 // Swap the type ID's.
Chris Lattner93802972004-01-11 23:29:26 +0000205 std::swap((*Types)[i], (*Types)[FirstNonValueTypeID]);
Chris Lattnera14b0d42004-01-10 23:46:13 +0000206
207 // Keep the NodeMap up to date.
Chris Lattner93802972004-01-11 23:29:26 +0000208 NodeMap[(*Types)[i]] = i;
209 NodeMap[(*Types)[FirstNonValueTypeID]] = FirstNonValueTypeID;
Chris Lattnera14b0d42004-01-10 23:46:13 +0000210
211 // When we move a type, make sure to move its value plane as needed.
Chris Lattner93802972004-01-11 23:29:26 +0000212 if (Table.size() > FirstNonValueTypeID) {
213 if (Table.size() <= i) Table.resize(i+1);
214 std::swap(Table[i], Table[FirstNonValueTypeID]);
215 Types = &Table[Type::TypeTyID];
216 }
Chris Lattnera14b0d42004-01-10 23:46:13 +0000217 }
218 ++FirstNonValueTypeID;
219 }
220 }
221
Chris Lattner9a297902001-09-07 16:31:52 +0000222 SC_DEBUG("end processModule!\n");
223}
224
225// processSymbolTable - Insert all of the values in the specified symbol table
226// into the values table...
227//
228void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
229 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
230 for (SymbolTable::type_const_iterator TI = I->second.begin(),
231 TE = I->second.end(); TI != TE; ++TI)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000232 getOrCreateSlot(TI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000233}
234
235void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
236 for (SymbolTable::const_iterator I = ST->begin(), E = ST->end(); I != E; ++I)
237 for (SymbolTable::type_const_iterator TI = I->second.begin(),
238 TE = I->second.end(); TI != TE; ++TI)
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000239 if (isa<Constant>(TI->second) || isa<Type>(TI->second))
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000240 getOrCreateSlot(TI->second);
Chris Lattner9a297902001-09-07 16:31:52 +0000241}
242
243
Chris Lattnerce439b52003-10-20 19:10:06 +0000244void SlotCalculator::incorporateFunction(const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000245 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
246
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000247 SC_DEBUG("begin processFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000248
Chris Lattner614cdcd2004-01-18 21:07:07 +0000249 // If we emitted all of the function constants, build a compaction table.
250 if (BuildBytecodeInfo && ModuleContainsAllFunctionConstants)
251 buildCompactionTable(F);
252 else {
253 // Save the Table state before we process the function...
254 for (unsigned i = 0, e = Table.size(); i != e; ++i)
255 ModuleLevel.push_back(Table[i].size());
256 }
Chris Lattner9a297902001-09-07 16:31:52 +0000257
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000258 // Iterate over function arguments, adding them to the value table...
Chris Lattnerce439b52003-10-20 19:10:06 +0000259 for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000260 getOrCreateSlot(I);
Chris Lattner9a297902001-09-07 16:31:52 +0000261
Chris Lattner614cdcd2004-01-18 21:07:07 +0000262 if (BuildBytecodeInfo && // Assembly writer does not need this!
263 !ModuleContainsAllFunctionConstants) {
264 // Iterate over all of the instructions in the function, looking for
265 // constant values that are referenced. Add these to the value pools
266 // before any nonconstant values. This will be turned into the constant
267 // pool for the bytecode writer.
268 //
269
270 // Emit all of the constants that are being used by the instructions in
271 // the function...
Chris Lattnerce439b52003-10-20 19:10:06 +0000272 for_each(constant_begin(F), constant_end(F),
Chris Lattner614cdcd2004-01-18 21:07:07 +0000273 bind_obj(this, &SlotCalculator::getOrCreateSlot));
274
Chris Lattner9a297902001-09-07 16:31:52 +0000275 // If there is a symbol table, it is possible that the user has names for
276 // constants that are not being used. In this case, we will have problems
277 // if we don't emit the constants now, because otherwise we will get
Chris Lattnera14b0d42004-01-10 23:46:13 +0000278 // symbol table references to constants not in the output. Scan for these
Chris Lattner9a297902001-09-07 16:31:52 +0000279 // constants now.
280 //
Chris Lattnerce439b52003-10-20 19:10:06 +0000281 processSymbolTableConstants(&F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +0000282 }
283
Chris Lattner9a297902001-09-07 16:31:52 +0000284 SC_DEBUG("Inserting Instructions:\n");
285
286 // Add all of the instructions to the type planes...
Chris Lattnerd5c59d52004-01-15 20:24:09 +0000287 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
288 getOrCreateSlot(BB);
Chris Lattner389dbfb2003-10-21 17:39:59 +0000289 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
290 getOrCreateSlot(I);
Chris Lattner2765c412003-10-21 17:40:54 +0000291 if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
292 getOrCreateSlot(VAN->getArgType());
Chris Lattner389dbfb2003-10-21 17:39:59 +0000293 }
Chris Lattner9a297902001-09-07 16:31:52 +0000294 }
295
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000296 SC_DEBUG("end processFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000297}
298
Chris Lattnerb5794002002-04-07 22:49:37 +0000299void SlotCalculator::purgeFunction() {
Chris Lattner00950542001-06-06 20:29:01 +0000300 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
301 unsigned NumModuleTypes = ModuleLevel.size();
302
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000303 SC_DEBUG("begin purgeFunction!\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000304
Chris Lattner614cdcd2004-01-18 21:07:07 +0000305 // First, free the compaction map if used.
306 CompactionNodeMap.clear();
307
308 // Next, remove values from existing type planes
309 for (unsigned i = 0; i != NumModuleTypes; ++i)
310 if (i >= CompactionTable.size() || CompactionTable[i].empty()) {
311 unsigned ModuleSize = ModuleLevel[i];// Size of plane before function came
312 TypePlane &CurPlane = Table[i];
313
314 while (CurPlane.size() != ModuleSize) {
315 std::map<const Value *, unsigned>::iterator NI =
316 NodeMap.find(CurPlane.back());
317 assert(NI != NodeMap.end() && "Node not in nodemap?");
318 NodeMap.erase(NI); // Erase from nodemap
319 CurPlane.pop_back(); // Shrink plane
320 }
Chris Lattner00950542001-06-06 20:29:01 +0000321 }
Chris Lattner00950542001-06-06 20:29:01 +0000322
323 // We don't need this state anymore, free it up.
324 ModuleLevel.clear();
325
Chris Lattner614cdcd2004-01-18 21:07:07 +0000326 if (!CompactionTable.empty()) {
327 CompactionTable.clear();
328 } else {
329 // FIXME: this will require adjustment when we don't compact everything.
330
331 // Finally, remove any type planes defined by the function...
332 while (NumModuleTypes != Table.size()) {
333 TypePlane &Plane = Table.back();
334 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
335 << Plane.size() << "\n");
336 while (Plane.size()) {
337 NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
338 Plane.pop_back(); // Shrink plane
339 }
340
341 Table.pop_back(); // Nuke the plane, we don't like it.
Chris Lattner00950542001-06-06 20:29:01 +0000342 }
Chris Lattner00950542001-06-06 20:29:01 +0000343 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000344 SC_DEBUG("end purgeFunction!\n");
Chris Lattner00950542001-06-06 20:29:01 +0000345}
346
Chris Lattner614cdcd2004-01-18 21:07:07 +0000347static inline bool hasNullValue(unsigned TyID) {
348 return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
349 TyID != Type::VoidTyID;
350}
351
352/// getOrCreateCompactionTableSlot - This method is used to build up the initial
353/// approximation of the compaction table.
354unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
355 std::map<const Value*, unsigned>::iterator I =
356 CompactionNodeMap.lower_bound(V);
357 if (I != CompactionNodeMap.end() && I->first == V)
358 return I->second; // Already exists?
359
360 // Make sure the type is in the table.
361 unsigned Ty = getOrCreateCompactionTableSlot(V->getType());
362 if (CompactionTable.size() <= Ty)
363 CompactionTable.resize(Ty+1);
364
365 assert(!isa<Type>(V) || ModuleLevel.empty());
366
367 TypePlane &TyPlane = CompactionTable[Ty];
368
369 // Make sure to insert the null entry if the thing we are inserting is not a
370 // null constant.
371 if (TyPlane.empty() && hasNullValue(V->getType()->getPrimitiveID())) {
372 Value *ZeroInitializer = Constant::getNullValue(V->getType());
373 if (V != ZeroInitializer) {
374 TyPlane.push_back(ZeroInitializer);
375 CompactionNodeMap[ZeroInitializer] = 0;
376 }
377 }
378
379 unsigned SlotNo = TyPlane.size();
380 TyPlane.push_back(V);
381 CompactionNodeMap.insert(std::make_pair(V, SlotNo));
382 return SlotNo;
383}
384
385
386/// buildCompactionTable - Since all of the function constants and types are
387/// stored in the module-level constant table, we don't need to emit a function
388/// constant table. Also due to this, the indices for various constants and
389/// types might be very large in large programs. In order to avoid blowing up
390/// the size of instructions in the bytecode encoding, we build a compaction
391/// table, which defines a mapping from function-local identifiers to global
392/// identifiers.
393void SlotCalculator::buildCompactionTable(const Function *F) {
394 assert(CompactionNodeMap.empty() && "Compaction table already built!");
395 // First step, insert the primitive types.
396 CompactionTable.resize(Type::TypeTyID+1);
397 for (unsigned i = 0; i != Type::FirstDerivedTyID; ++i) {
398 const Type *PrimTy = Type::getPrimitiveType((Type::PrimitiveID)i);
399 CompactionTable[Type::TypeTyID].push_back(PrimTy);
400 CompactionNodeMap[PrimTy] = i;
401 }
402
403 // Next, include any types used by function arguments.
404 for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
405 getOrCreateCompactionTableSlot(I->getType());
406
407 // Next, find all of the types and values that are referred to by the
408 // instructions in the program.
409 for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
410 getOrCreateCompactionTableSlot(I->getType());
411 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
412 if (isa<Constant>(I->getOperand(op)) ||
413 isa<GlobalValue>(I->getOperand(op)))
414 getOrCreateCompactionTableSlot(I->getOperand(op));
415 if (const VANextInst *VAN = dyn_cast<VANextInst>(*I))
416 getOrCreateCompactionTableSlot(VAN->getArgType());
417 }
418
419 const SymbolTable &ST = F->getSymbolTable();
420 for (SymbolTable::const_iterator I = ST.begin(), E = ST.end(); I != E; ++I)
421 for (SymbolTable::type_const_iterator TI = I->second.begin(),
422 TE = I->second.end(); TI != TE; ++TI)
423 if (isa<Constant>(TI->second) || isa<Type>(TI->second) ||
424 isa<GlobalValue>(TI->second))
425 getOrCreateCompactionTableSlot(TI->second);
426
427 // Now that we have all of the values in the table, and know what types are
428 // referenced, make sure that there is at least the zero initializer in any
429 // used type plane. Since the type was used, we will be emitting instructions
430 // to the plane even if there are no constants in it.
431 CompactionTable.resize(CompactionTable[Type::TypeTyID].size());
432 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
433 if (CompactionTable[i].empty() && i != Type::VoidTyID &&
434 i != Type::LabelTyID) {
435 const Type *Ty = cast<Type>(CompactionTable[Type::TypeTyID][i]);
436 getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
437 }
438
439 // Okay, now at this point, we have a legal compaction table. Since we want
440 // to emit the smallest possible binaries, we delete planes that do not NEED
441 // to be compacted, starting with the type plane.
442
443
444 // If decided not to compact anything, do not modify ModuleLevels.
445 if (CompactionTable.empty())
446 // FIXME: must update ModuleLevel.
447 return;
448
449 // Finally, for any planes that we have decided to compact, update the
450 // ModuleLevel entries to be accurate.
451
452 // FIXME: This does not yet work for partially compacted tables.
453 ModuleLevel.resize(CompactionTable.size());
454 for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
455 ModuleLevel[i] = CompactionTable[i].size();
456}
457
Chris Lattner8c202cd2004-01-15 18:47:15 +0000458int SlotCalculator::getSlot(const Value *V) const {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000459 // If there is a CompactionTable active...
460 if (!CompactionNodeMap.empty()) {
461 std::map<const Value*, unsigned>::const_iterator I =
462 CompactionNodeMap.find(V);
463 if (I != CompactionNodeMap.end())
464 return (int)I->second;
465 return -1;
466 }
467
Chris Lattner8c202cd2004-01-15 18:47:15 +0000468 std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
469 if (I != NodeMap.end())
470 return (int)I->second;
471
472 // Do not number ConstantPointerRef's at all. They are an abomination.
473 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
474 return getSlot(CPR->getValue());
475
476 return -1;
Chris Lattner00950542001-06-06 20:29:01 +0000477}
478
Chris Lattner9a297902001-09-07 16:31:52 +0000479
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000480int SlotCalculator::getOrCreateSlot(const Value *V) {
481 int SlotNo = getSlot(V); // Check to see if it's already in!
Chris Lattner9a297902001-09-07 16:31:52 +0000482 if (SlotNo != -1) return SlotNo;
Chris Lattner3413d152003-03-19 20:57:22 +0000483
Chris Lattner8c202cd2004-01-15 18:47:15 +0000484 // Do not number ConstantPointerRef's at all. They are an abomination.
485 if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
486 return getOrCreateSlot(CPR->getValue());
487
Chris Lattner614cdcd2004-01-18 21:07:07 +0000488 if (!isa<GlobalValue>(V)) // Initializers for globals are handled explicitly
Chris Lattner3413d152003-03-19 20:57:22 +0000489 if (const Constant *C = dyn_cast<Constant>(V)) {
Chris Lattner614cdcd2004-01-18 21:07:07 +0000490 assert(CompactionNodeMap.empty() &&
491 "All needed constants should be in the compaction map already!");
492
Chris Lattnerdcea6302004-01-14 23:34:39 +0000493 // If we are emitting a bytecode file, do not index the characters that
494 // make up constant strings. We emit constant strings as special
495 // entities that don't require their individual characters to be emitted.
496 if (!BuildBytecodeInfo || !isa<ConstantArray>(C) ||
497 !cast<ConstantArray>(C)->isString()) {
498 // This makes sure that if a constant has uses (for example an array of
499 // const ints), that they are inserted also.
500 //
501 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
502 I != E; ++I)
503 getOrCreateSlot(*I);
504 } else {
505 assert(ModuleLevel.empty() &&
506 "How can a constant string be directly accessed in a function?");
507 // Otherwise, if we are emitting a bytecode file and this IS a string,
508 // remember it.
509 if (!C->isNullValue())
510 ConstantStrings.push_back(cast<ConstantArray>(C));
511 }
Chris Lattner3413d152003-03-19 20:57:22 +0000512 }
513
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000514 return insertValue(V);
Chris Lattner9a297902001-09-07 16:31:52 +0000515}
516
517
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000518int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
Chris Lattner9a297902001-09-07 16:31:52 +0000519 assert(D && "Can't insert a null value!");
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000520 assert(getSlot(D) == -1 && "Value is already in the table!");
Chris Lattner00950542001-06-06 20:29:01 +0000521
Chris Lattner614cdcd2004-01-18 21:07:07 +0000522 // If we are building a compaction map, and if this plane is being compacted,
523 // insert the value into the compaction map, not into the global map.
524 if (!CompactionNodeMap.empty()) {
525 if (D->getType() == Type::VoidTy) return -1; // Do not insert void values
526 assert(!isa<Type>(D) && !isa<Constant>(D) && !isa<GlobalValue>(D) &&
527 "Types, constants, and globals should be in global SymTab!");
528
529 // FIXME: this does not yet handle partially compacted tables yet!
530 return getOrCreateCompactionTableSlot(D);
531 }
532
Chris Lattner00950542001-06-06 20:29:01 +0000533 // If this node does not contribute to a plane, or if the node has a
Chris Lattner9a297902001-09-07 16:31:52 +0000534 // name and we don't want names, then ignore the silly node... Note that types
535 // do need slot numbers so that we can keep track of where other values land.
Chris Lattner00950542001-06-06 20:29:01 +0000536 //
Chris Lattner9b50c152001-07-26 16:28:37 +0000537 if (!dontIgnore) // Don't ignore nonignorables!
538 if (D->getType() == Type::VoidTy || // Ignore void type nodes
Chris Lattner8ce75012004-01-14 02:49:34 +0000539 (!BuildBytecodeInfo && // Ignore named and constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000540 (D->hasName() || isa<Constant>(D)) && !isa<Type>(D))) {
Chris Lattner3413d152003-03-19 20:57:22 +0000541 SC_DEBUG("ignored value " << *D << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000542 return -1; // We do need types unconditionally though
543 }
Chris Lattner00950542001-06-06 20:29:01 +0000544
Chris Lattner9a297902001-09-07 16:31:52 +0000545 // If it's a type, make sure that all subtypes of the type are included...
Chris Lattner949a3622003-07-23 15:30:06 +0000546 if (const Type *TheTy = dyn_cast<Type>(D)) {
Chris Lattnerf1fef652001-10-13 06:35:09 +0000547
548 // Insert the current type before any subtypes. This is important because
549 // recursive types elements are inserted in a bottom up order. Changing
550 // this here can break things. For example:
551 //
552 // global { \2 * } { { \2 }* null }
553 //
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000554 int ResultSlot = doInsertValue(TheTy);
555 SC_DEBUG(" Inserted type: " << TheTy->getDescription() << " slot=" <<
556 ResultSlot << "\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000557
Alkis Evlogimenos088eb452003-10-31 03:02:34 +0000558 // Loop over any contained types in the definition... in post
559 // order.
Chris Lattner9a297902001-09-07 16:31:52 +0000560 //
Alkis Evlogimenos088eb452003-10-31 03:02:34 +0000561 for (po_iterator<const Type*> I = po_begin(TheTy), E = po_end(TheTy);
Alkis Evlogimenos74fa84f2003-10-30 21:04:44 +0000562 I != E; ++I) {
Chris Lattner9a297902001-09-07 16:31:52 +0000563 if (*I != TheTy) {
Alkis Evlogimenos74fa84f2003-10-30 21:04:44 +0000564 const Type *SubTy = *I;
Alkis Evlogimenos088eb452003-10-31 03:02:34 +0000565 // If we haven't seen this sub type before, add it to our type table!
566 if (getSlot(SubTy) == -1) {
567 SC_DEBUG(" Inserting subtype: " << SubTy->getDescription() << "\n");
568 int Slot = doInsertValue(SubTy);
569 SC_DEBUG(" Inserted subtype: " << SubTy->getDescription() <<
570 " slot=" << Slot << "\n");
571 }
Alkis Evlogimenos74fa84f2003-10-30 21:04:44 +0000572 }
573 }
Chris Lattnerf1fef652001-10-13 06:35:09 +0000574 return ResultSlot;
Chris Lattner9a297902001-09-07 16:31:52 +0000575 }
576
577 // Okay, everything is happy, actually insert the silly value now...
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000578 return doInsertValue(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000579}
580
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000581// doInsertValue - This is a small helper function to be called only
582// be insertValue.
Chris Lattner9a297902001-09-07 16:31:52 +0000583//
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000584int SlotCalculator::doInsertValue(const Value *D) {
Chris Lattner00950542001-06-06 20:29:01 +0000585 const Type *Typ = D->getType();
Chris Lattner9b50c152001-07-26 16:28:37 +0000586 unsigned Ty;
587
588 // Used for debugging DefSlot=-1 assertion...
589 //if (Typ == Type::TypeTy)
Chris Lattnercfe26c92001-10-01 18:26:53 +0000590 // cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
Chris Lattner9b50c152001-07-26 16:28:37 +0000591
Chris Lattner00950542001-06-06 20:29:01 +0000592 if (Typ->isDerivedType()) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000593 int ValSlot = getSlot(Typ);
Chris Lattner3413d152003-03-19 20:57:22 +0000594 if (ValSlot == -1) { // Have we already entered this type?
Chris Lattner9a297902001-09-07 16:31:52 +0000595 // Nope, this is the first we have seen the type, process it.
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000596 ValSlot = insertValue(Typ, true);
Chris Lattner3413d152003-03-19 20:57:22 +0000597 assert(ValSlot != -1 && "ProcessType returned -1 for a type?");
Chris Lattner00950542001-06-06 20:29:01 +0000598 }
Chris Lattner3413d152003-03-19 20:57:22 +0000599 Ty = (unsigned)ValSlot;
Chris Lattner9b50c152001-07-26 16:28:37 +0000600 } else {
601 Ty = Typ->getPrimitiveID();
Chris Lattner00950542001-06-06 20:29:01 +0000602 }
603
604 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
605 Table.resize(Ty+1, TypePlane());
Chris Lattner3413d152003-03-19 20:57:22 +0000606
607 // If this is the first value to get inserted into the type plane, make sure
608 // to insert the implicit null value...
Chris Lattner80b97342004-01-17 23:25:43 +0000609 if (Table[Ty].empty() && BuildBytecodeInfo && hasNullValue(Ty)) {
Chris Lattner3413d152003-03-19 20:57:22 +0000610 Value *ZeroInitializer = Constant::getNullValue(Typ);
611
612 // If we are pushing zeroinit, it will be handled below.
613 if (D != ZeroInitializer) {
614 Table[Ty].push_back(ZeroInitializer);
615 NodeMap[ZeroInitializer] = 0;
616 }
617 }
618
Chris Lattner9a297902001-09-07 16:31:52 +0000619 // Insert node into table and NodeMap...
620 unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Chris Lattner00950542001-06-06 20:29:01 +0000621 Table[Ty].push_back(D);
Chris Lattner9a297902001-09-07 16:31:52 +0000622
Chris Lattnerf1fef652001-10-13 06:35:09 +0000623 SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
624 DestSlot << " [");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000625 // G = Global, C = Constant, T = Type, F = Function, o = other
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000626 SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000627 (isa<Type>(D) ? "T" : (isa<Function>(D) ? "F" : "o")))));
Chris Lattnerf1fef652001-10-13 06:35:09 +0000628 SC_DEBUG("]\n");
Chris Lattner9a297902001-09-07 16:31:52 +0000629 return (int)DestSlot;
Chris Lattner00950542001-06-06 20:29:01 +0000630}