blob: 92271ce2b910301ccb33483a2a5913e2754f73bb [file] [log] [blame]
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
Chris Lattnerff7fc5d2007-05-06 00:35:24 +000015#include "llvm/Constants.h"
Chris Lattner50954f52007-05-03 22:46:43 +000016#include "llvm/DerivedTypes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "llvm/Module.h"
18#include "llvm/TypeSymbolTable.h"
19#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000020#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000021#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000022using namespace llvm;
23
Chris Lattner12f535b2007-05-04 05:05:48 +000024static bool isFirstClassType(const std::pair<const llvm::Type*,
25 unsigned int> &P) {
26 return P.first->isFirstClassType();
27}
28
Chris Lattner6da91d32007-05-04 05:21:47 +000029static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
30 return isa<IntegerType>(V.first->getType());
31}
32
Chris Lattner12f535b2007-05-04 05:05:48 +000033static bool CompareByFrequency(const std::pair<const llvm::Type*,
34 unsigned int> &P1,
35 const std::pair<const llvm::Type*,
36 unsigned int> &P2) {
37 return P1.second > P2.second;
38}
39
Chris Lattnerfd57cec2007-04-22 06:24:45 +000040/// ValueEnumerator - Enumerate module-level information.
41ValueEnumerator::ValueEnumerator(const Module *M) {
42 // Enumerate the global variables.
43 for (Module::const_global_iterator I = M->global_begin(),
44 E = M->global_end(); I != E; ++I)
45 EnumerateValue(I);
46
47 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000048 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000049 EnumerateValue(I);
Duncan Sandsdc024672007-11-27 13:23:08 +000050 EnumerateParamAttrs(cast<Function>(I)->getParamAttrs());
51 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000052
Chris Lattner07d98b42007-04-26 02:46:40 +000053 // Enumerate the aliases.
54 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
55 I != E; ++I)
56 EnumerateValue(I);
57
Chris Lattner6da91d32007-05-04 05:21:47 +000058 // Remember what is the cutoff between globalvalue's and other constants.
59 unsigned FirstConstant = Values.size();
60
Chris Lattnerfd57cec2007-04-22 06:24:45 +000061 // Enumerate the global variable initializers.
62 for (Module::const_global_iterator I = M->global_begin(),
63 E = M->global_end(); I != E; ++I)
64 if (I->hasInitializer())
65 EnumerateValue(I->getInitializer());
66
Chris Lattner07d98b42007-04-26 02:46:40 +000067 // Enumerate the aliasees.
68 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
69 I != E; ++I)
70 EnumerateValue(I->getAliasee());
71
Chris Lattnerfd57cec2007-04-22 06:24:45 +000072 // Enumerate types used by the type symbol table.
73 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
74
75 // Insert constants that are named at module level into the slot pool so that
76 // the module symbol table can refer to them...
77 EnumerateValueSymbolTable(M->getValueSymbolTable());
78
Chris Lattner8d35c792007-04-26 03:50:57 +000079 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000080 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner8d35c792007-04-26 03:50:57 +000081
82 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
83 I != E; ++I)
84 EnumerateType(I->getType());
85
Chris Lattnerfd57cec2007-04-22 06:24:45 +000086 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
87 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
88 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
89 OI != E; ++OI)
Chris Lattner33f1d5b2007-05-06 08:35:19 +000090 EnumerateOperandType(*OI);
Chris Lattnerfd57cec2007-04-22 06:24:45 +000091 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000092 if (const CallInst *CI = dyn_cast<CallInst>(I))
93 EnumerateParamAttrs(CI->getParamAttrs());
94 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
95 EnumerateParamAttrs(II->getParamAttrs());
Chris Lattnerfd57cec2007-04-22 06:24:45 +000096 }
97 }
Chris Lattner12f535b2007-05-04 05:05:48 +000098
Chris Lattner6da91d32007-05-04 05:21:47 +000099 // Optimize constant ordering.
100 OptimizeConstants(FirstConstant, Values.size());
101
Chris Lattner12f535b2007-05-04 05:05:48 +0000102 // Sort the type table by frequency so that most commonly used types are early
103 // in the table (have low bit-width).
104 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000105
Chris Lattner12f535b2007-05-04 05:05:48 +0000106 // Partition the Type ID's so that the first-class types occur before the
107 // aggregate types. This allows the aggregate types to be dropped from the
108 // type table after parsing the global variable initializers.
109 std::partition(Types.begin(), Types.end(), isFirstClassType);
110
111 // Now that we rearranged the type table, rebuild TypeMap.
112 for (unsigned i = 0, e = Types.size(); i != e; ++i)
113 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000114}
115
Chris Lattner6da91d32007-05-04 05:21:47 +0000116// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000117namespace {
118 struct CstSortPredicate {
119 ValueEnumerator &VE;
120 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
121 bool operator()(const std::pair<const Value*, unsigned> &LHS,
122 const std::pair<const Value*, unsigned> &RHS) {
123 // Sort by plane.
124 if (LHS.first->getType() != RHS.first->getType())
125 return VE.getTypeID(LHS.first->getType()) <
126 VE.getTypeID(RHS.first->getType());
127 // Then by frequency.
128 return LHS.second > RHS.second;
129 }
130 };
131}
Chris Lattner6da91d32007-05-04 05:21:47 +0000132
133/// OptimizeConstants - Reorder constant pool for denser encoding.
134void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
135 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
136
137 CstSortPredicate P(*this);
138 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
139
140 // Ensure that integer constants are at the start of the constant pool. This
141 // is important so that GEP structure indices come before gep constant exprs.
142 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
143 isIntegerValue);
144
145 // Rebuild the modified portion of ValueMap.
146 for (; CstStart != CstEnd; ++CstStart)
147 ValueMap[Values[CstStart].first] = CstStart+1;
148}
149
150
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000151/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
152/// table.
153void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
154 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
155 TI != TE; ++TI)
156 EnumerateType(TI->second);
157}
158
159/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
160/// table into the values table.
161void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
162 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
163 VI != VE; ++VI)
164 EnumerateValue(VI->getValue());
165}
166
167void ValueEnumerator::EnumerateValue(const Value *V) {
168 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
169
170 // Check to see if it's already in!
171 unsigned &ValueID = ValueMap[V];
172 if (ValueID) {
173 // Increment use count.
174 Values[ValueID-1].second++;
175 return;
176 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000177
Chris Lattner7a303d12007-05-06 01:00:28 +0000178 // Enumerate the type of this value.
179 EnumerateType(V->getType());
180
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000181 if (const Constant *C = dyn_cast<Constant>(V)) {
182 if (isa<GlobalValue>(C)) {
183 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000184 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
185 // Do not enumerate the initializers for an array of simple characters.
186 // The initializers just polute the value table, and we emit the strings
187 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000188 } else if (C->getNumOperands()) {
189 // If a constant has operands, enumerate them. This makes sure that if a
190 // constant has uses (for example an array of const ints), that they are
191 // inserted also.
192
193 // We prefer to enumerate them with values before we enumerate the user
194 // itself. This makes it more likely that we can avoid forward references
195 // in the reader. We know that there can be no cycles in the constants
196 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000197 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
198 I != E; ++I)
199 EnumerateValue(*I);
Chris Lattner7a303d12007-05-06 01:00:28 +0000200
201 // Finally, add the value. Doing this could make the ValueID reference be
202 // dangling, don't reuse it.
203 Values.push_back(std::make_pair(V, 1U));
204 ValueMap[V] = Values.size();
205 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000206 }
207 }
Chris Lattner7a303d12007-05-06 01:00:28 +0000208
209 // Add the value.
210 Values.push_back(std::make_pair(V, 1U));
211 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000212}
213
214
215void ValueEnumerator::EnumerateType(const Type *Ty) {
216 unsigned &TypeID = TypeMap[Ty];
217
218 if (TypeID) {
219 // If we've already seen this type, just increase its occurrence count.
220 Types[TypeID-1].second++;
221 return;
222 }
223
224 // First time we saw this type, add it.
225 Types.push_back(std::make_pair(Ty, 1U));
226 TypeID = Types.size();
227
228 // Enumerate subtypes.
229 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
230 I != E; ++I)
231 EnumerateType(*I);
232}
233
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000234// Enumerate the types for the specified value. If the value is a constant,
235// walk through it, enumerating the types of the constant.
236void ValueEnumerator::EnumerateOperandType(const Value *V) {
237 EnumerateType(V->getType());
238 if (const Constant *C = dyn_cast<Constant>(V)) {
239 // If this constant is already enumerated, ignore it, we know its type must
240 // be enumerated.
241 if (ValueMap.count(V)) return;
242
243 // This constant may have operands, make sure to enumerate the types in
244 // them.
245 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
246 EnumerateOperandType(C->getOperand(i));
247 }
248}
249
Chris Lattner58d74912008-03-12 17:45:29 +0000250void ValueEnumerator::EnumerateParamAttrs(const PAListPtr &PAL) {
251 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000252 // Do a lookup.
Chris Lattner58d74912008-03-12 17:45:29 +0000253 unsigned &Entry = ParamAttrMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000254 if (Entry == 0) {
255 // Never saw this before, add it.
256 ParamAttrs.push_back(PAL);
257 Entry = ParamAttrs.size();
258 }
259}
260
261
Chris Lattner198f34a2007-04-26 03:27:58 +0000262/// PurgeAggregateValues - If there are any aggregate values at the end of the
263/// value list, remove them and return the count of the remaining values. If
264/// there are none, return -1.
265int ValueEnumerator::PurgeAggregateValues() {
266 // If there are no aggregate values at the end of the list, return -1.
267 if (Values.empty() || Values.back().first->getType()->isFirstClassType())
268 return -1;
269
270 // Otherwise, remove aggregate values...
271 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
272 Values.pop_back();
273
274 // ... and return the new size.
275 return Values.size();
276}
277
Chris Lattner8d35c792007-04-26 03:50:57 +0000278void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000279 NumModuleValues = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000280
Chris Lattner8d35c792007-04-26 03:50:57 +0000281 // Adding function arguments to the value table.
282 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000283 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000284 EnumerateValue(I);
285
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000286 FirstFuncConstantID = Values.size();
287
Chris Lattner8d35c792007-04-26 03:50:57 +0000288 // Add all function-level constants to the value table.
289 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
290 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
291 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
292 OI != E; ++OI) {
293 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
294 isa<InlineAsm>(*OI))
295 EnumerateValue(*OI);
296 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000297 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000298 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000299 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300
Chris Lattner6da91d32007-05-04 05:21:47 +0000301 // Optimize the constant layout.
302 OptimizeConstants(FirstFuncConstantID, Values.size());
303
Duncan Sandsdc024672007-11-27 13:23:08 +0000304 // Add the function's parameter attributes so they are available for use in
305 // the function's instruction.
306 EnumerateParamAttrs(F.getParamAttrs());
307
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000308 FirstInstID = Values.size();
309
Chris Lattner8d35c792007-04-26 03:50:57 +0000310 // Add all of the instructions.
311 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000312 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
313 if (I->getType() != Type::VoidTy)
Chris Lattner8d35c792007-04-26 03:50:57 +0000314 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000315 }
316 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000317}
318
Chris Lattner8d35c792007-04-26 03:50:57 +0000319void ValueEnumerator::purgeFunction() {
320 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000321 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000322 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000323 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
324 ValueMap.erase(BasicBlocks[i]);
325
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000326 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000327 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000328}
329