blob: cf4a8761cbca3f1392c1a620d2bdc04ea11982d6 [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"
Devang Patel0a9f7b92009-07-28 21:49:47 +000017#include "llvm/Metadata.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000018#include "llvm/Module.h"
19#include "llvm/TypeSymbolTable.h"
20#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000021#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000022#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023using namespace llvm;
24
Dan Gohmane4977cf2008-05-23 01:55:30 +000025static bool isSingleValueType(const std::pair<const llvm::Type*,
26 unsigned int> &P) {
27 return P.first->isSingleValueType();
Chris Lattner12f535b2007-05-04 05:05:48 +000028}
29
Chris Lattner6da91d32007-05-04 05:21:47 +000030static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
31 return isa<IntegerType>(V.first->getType());
32}
33
Chris Lattner12f535b2007-05-04 05:05:48 +000034static bool CompareByFrequency(const std::pair<const llvm::Type*,
35 unsigned int> &P1,
36 const std::pair<const llvm::Type*,
37 unsigned int> &P2) {
38 return P1.second > P2.second;
39}
40
Chris Lattnerfd57cec2007-04-22 06:24:45 +000041/// ValueEnumerator - Enumerate module-level information.
42ValueEnumerator::ValueEnumerator(const Module *M) {
43 // Enumerate the global variables.
44 for (Module::const_global_iterator I = M->global_begin(),
45 E = M->global_end(); I != E; ++I)
46 EnumerateValue(I);
47
48 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000049 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000050 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000051 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000052 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000053
Chris Lattner07d98b42007-04-26 02:46:40 +000054 // Enumerate the aliases.
55 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
56 I != E; ++I)
57 EnumerateValue(I);
58
Chris Lattner6da91d32007-05-04 05:21:47 +000059 // Remember what is the cutoff between globalvalue's and other constants.
60 unsigned FirstConstant = Values.size();
61
Chris Lattnerfd57cec2007-04-22 06:24:45 +000062 // Enumerate the global variable initializers.
63 for (Module::const_global_iterator I = M->global_begin(),
64 E = M->global_end(); I != E; ++I)
65 if (I->hasInitializer())
66 EnumerateValue(I->getInitializer());
67
Chris Lattner07d98b42007-04-26 02:46:40 +000068 // Enumerate the aliasees.
69 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
70 I != E; ++I)
71 EnumerateValue(I->getAliasee());
72
Chris Lattnerfd57cec2007-04-22 06:24:45 +000073 // Enumerate types used by the type symbol table.
74 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
75
76 // Insert constants that are named at module level into the slot pool so that
77 // the module symbol table can refer to them...
78 EnumerateValueSymbolTable(M->getValueSymbolTable());
79
Chris Lattner8d35c792007-04-26 03:50:57 +000080 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000081 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner8d35c792007-04-26 03:50:57 +000082
83 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
84 I != E; ++I)
85 EnumerateType(I->getType());
86
Chris Lattnerfd57cec2007-04-22 06:24:45 +000087 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
88 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
89 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
90 OI != E; ++OI)
Chris Lattner33f1d5b2007-05-06 08:35:19 +000091 EnumerateOperandType(*OI);
Chris Lattnerfd57cec2007-04-22 06:24:45 +000092 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000093 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000094 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000095 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000096 EnumerateAttributes(II->getAttributes());
Chris Lattnerfd57cec2007-04-22 06:24:45 +000097 }
98 }
Chris Lattner12f535b2007-05-04 05:05:48 +000099
Chris Lattner6da91d32007-05-04 05:21:47 +0000100 // Optimize constant ordering.
101 OptimizeConstants(FirstConstant, Values.size());
102
Chris Lattner12f535b2007-05-04 05:05:48 +0000103 // Sort the type table by frequency so that most commonly used types are early
104 // in the table (have low bit-width).
105 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000106
Dan Gohmane4977cf2008-05-23 01:55:30 +0000107 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000108 // aggregate types. This allows the aggregate types to be dropped from the
109 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000110 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000111
112 // Now that we rearranged the type table, rebuild TypeMap.
113 for (unsigned i = 0, e = Types.size(); i != e; ++i)
114 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000115}
116
Chris Lattner6da91d32007-05-04 05:21:47 +0000117// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000118namespace {
119 struct CstSortPredicate {
120 ValueEnumerator &VE;
121 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
122 bool operator()(const std::pair<const Value*, unsigned> &LHS,
123 const std::pair<const Value*, unsigned> &RHS) {
124 // Sort by plane.
125 if (LHS.first->getType() != RHS.first->getType())
126 return VE.getTypeID(LHS.first->getType()) <
127 VE.getTypeID(RHS.first->getType());
128 // Then by frequency.
129 return LHS.second > RHS.second;
130 }
131 };
132}
Chris Lattner6da91d32007-05-04 05:21:47 +0000133
134/// OptimizeConstants - Reorder constant pool for denser encoding.
135void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
136 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
137
138 CstSortPredicate P(*this);
139 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
140
141 // Ensure that integer constants are at the start of the constant pool. This
142 // is important so that GEP structure indices come before gep constant exprs.
143 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
144 isIntegerValue);
145
146 // Rebuild the modified portion of ValueMap.
147 for (; CstStart != CstEnd; ++CstStart)
148 ValueMap[Values[CstStart].first] = CstStart+1;
149}
150
151
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000152/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
153/// table.
154void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
155 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
156 TI != TE; ++TI)
157 EnumerateType(TI->second);
158}
159
160/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
161/// table into the values table.
162void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
163 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
164 VI != VE; ++VI)
165 EnumerateValue(VI->getValue());
166}
167
168void ValueEnumerator::EnumerateValue(const Value *V) {
169 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
170
171 // Check to see if it's already in!
172 unsigned &ValueID = ValueMap[V];
173 if (ValueID) {
174 // Increment use count.
175 Values[ValueID-1].second++;
176 return;
177 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000178
Chris Lattner7a303d12007-05-06 01:00:28 +0000179 // Enumerate the type of this value.
180 EnumerateType(V->getType());
181
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000182 if (const Constant *C = dyn_cast<Constant>(V)) {
183 if (isa<GlobalValue>(C)) {
184 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000185 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
186 // Do not enumerate the initializers for an array of simple characters.
187 // The initializers just polute the value table, and we emit the strings
188 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000189 } else if (C->getNumOperands()) {
190 // If a constant has operands, enumerate them. This makes sure that if a
191 // constant has uses (for example an array of const ints), that they are
192 // inserted also.
193
194 // We prefer to enumerate them with values before we enumerate the user
195 // itself. This makes it more likely that we can avoid forward references
196 // in the reader. We know that there can be no cycles in the constants
197 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000198 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
199 I != E; ++I)
200 EnumerateValue(*I);
Chris Lattner7a303d12007-05-06 01:00:28 +0000201
202 // Finally, add the value. Doing this could make the ValueID reference be
203 // dangling, don't reuse it.
204 Values.push_back(std::make_pair(V, 1U));
205 ValueMap[V] = Values.size();
206 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000207 }
208 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000209
210 if (const MDNode *N = dyn_cast<MDNode>(V)) {
211 Values.push_back(std::make_pair(V, 1U));
212 ValueMap[V] = Values.size();
213 ValueID = Values.size();
214 for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
215 I != E; ++I) {
216 if (*I)
217 EnumerateValue(*I);
218 else
219 EnumerateType(Type::VoidTy);
220 }
221 return;
222 }
223
Devang Patelaa993142009-07-29 22:34:41 +0000224 if (const NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
Devang Patelaa993142009-07-29 22:34:41 +0000225 for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
226 E = N->elem_end(); I != E; ++I) {
227 MetadataBase *M = *I;
228 EnumerateValue(M);
229 }
Devang Patelaa993142009-07-29 22:34:41 +0000230 }
231
Chris Lattner7a303d12007-05-06 01:00:28 +0000232 // Add the value.
233 Values.push_back(std::make_pair(V, 1U));
234 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000235}
236
237
238void ValueEnumerator::EnumerateType(const Type *Ty) {
239 unsigned &TypeID = TypeMap[Ty];
240
241 if (TypeID) {
242 // If we've already seen this type, just increase its occurrence count.
243 Types[TypeID-1].second++;
244 return;
245 }
246
247 // First time we saw this type, add it.
248 Types.push_back(std::make_pair(Ty, 1U));
249 TypeID = Types.size();
250
251 // Enumerate subtypes.
252 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
253 I != E; ++I)
254 EnumerateType(*I);
255}
256
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000257// Enumerate the types for the specified value. If the value is a constant,
258// walk through it, enumerating the types of the constant.
259void ValueEnumerator::EnumerateOperandType(const Value *V) {
260 EnumerateType(V->getType());
261 if (const Constant *C = dyn_cast<Constant>(V)) {
262 // If this constant is already enumerated, ignore it, we know its type must
263 // be enumerated.
264 if (ValueMap.count(V)) return;
265
266 // This constant may have operands, make sure to enumerate the types in
267 // them.
268 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
269 EnumerateOperandType(C->getOperand(i));
Nick Lewyckycb337992009-05-10 20:57:05 +0000270
271 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +0000272 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
273 Value *Elem = N->getElement(i);
274 if (Elem)
275 EnumerateOperandType(Elem);
276 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000277 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000278 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000279 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000280}
281
Devang Patel05988662008-09-25 21:00:45 +0000282void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000283 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000284 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000285 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000286 if (Entry == 0) {
287 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000288 Attributes.push_back(PAL);
289 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000290 }
291}
292
293
Chris Lattner8d35c792007-04-26 03:50:57 +0000294void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000295 NumModuleValues = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000296
Chris Lattner8d35c792007-04-26 03:50:57 +0000297 // Adding function arguments to the value table.
298 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000299 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000300 EnumerateValue(I);
301
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000302 FirstFuncConstantID = Values.size();
303
Chris Lattner8d35c792007-04-26 03:50:57 +0000304 // Add all function-level constants to the value table.
305 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
306 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
307 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
308 OI != E; ++OI) {
309 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
310 isa<InlineAsm>(*OI))
311 EnumerateValue(*OI);
312 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000313 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000314 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000315 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000316
Chris Lattner6da91d32007-05-04 05:21:47 +0000317 // Optimize the constant layout.
318 OptimizeConstants(FirstFuncConstantID, Values.size());
319
Duncan Sandsdc024672007-11-27 13:23:08 +0000320 // Add the function's parameter attributes so they are available for use in
321 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000322 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000323
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000324 FirstInstID = Values.size();
325
Chris Lattner8d35c792007-04-26 03:50:57 +0000326 // Add all of the instructions.
327 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000328 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
329 if (I->getType() != Type::VoidTy)
Chris Lattner8d35c792007-04-26 03:50:57 +0000330 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000331 }
332 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000333}
334
Chris Lattner8d35c792007-04-26 03:50:57 +0000335void ValueEnumerator::purgeFunction() {
336 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000337 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000338 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000339 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
340 ValueMap.erase(BasicBlocks[i]);
341
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000342 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000343 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000344}
345