blob: 7583bb96c9ec8c995fdc98e29c83cb763deec2e0 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
Devang Patel7c368852009-07-28 21:49:47 +000017#include "llvm/Metadata.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Module.h"
19#include "llvm/TypeSymbolTable.h"
20#include "llvm/ValueSymbolTable.h"
Duncan Sandsf5588dc2007-11-27 13:23:08 +000021#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <algorithm>
23using namespace llvm;
24
Dan Gohmane6b1ee62008-05-23 01:55:30 +000025static bool isSingleValueType(const std::pair<const llvm::Type*,
26 unsigned int> &P) {
27 return P.first->isSingleValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028}
29
30static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
31 return isa<IntegerType>(V.first->getType());
32}
33
34static 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
41/// 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 Sandsf5588dc2007-11-27 13:23:08 +000049 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 EnumerateValue(I);
Devang Pateld222f862008-09-25 21:00:45 +000051 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000052 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 // 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
59 // Remember what is the cutoff between globalvalue's and other constants.
60 unsigned FirstConstant = Values.size();
61
62 // 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
68 // 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
73 // 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
80 // Enumerate types used by function bodies and argument lists.
81 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
82
83 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
84 I != E; ++I)
85 EnumerateType(I->getType());
86
87 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)
91 EnumerateOperandType(*OI);
92 EnumerateType(I->getType());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000093 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +000094 EnumerateAttributes(CI->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000095 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +000096 EnumerateAttributes(II->getAttributes());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 }
98 }
99
100 // Optimize constant ordering.
101 OptimizeConstants(FirstConstant, Values.size());
102
103 // 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);
106
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000107 // Partition the Type ID's so that the single-value types occur before the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // aggregate types. This allows the aggregate types to be dropped from the
109 // type table after parsing the global variable initializers.
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000110 std::partition(Types.begin(), Types.end(), isSingleValueType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
115}
116
117// Optimize constant ordering.
Dan Gohman089efff2008-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}
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
152/// 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 }
178
179 // Enumerate the type of this value.
180 EnumerateType(V->getType());
181
182 if (const Constant *C = dyn_cast<Constant>(V)) {
183 if (isa<GlobalValue>(C)) {
184 // Initializers for globals are handled explicitly elsewhere.
185 } 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.
189 } 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.
198 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
199 I != E; ++I)
200 EnumerateValue(*I);
201
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;
207 }
208 }
Devang Patel54199ef2009-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 Patel3e1ef932009-07-29 22:34:41 +0000224 if (const NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
Devang Patel3e1ef932009-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 }
Benjamin Kramerd77b9832009-07-31 14:22:13 +0000230 Values.push_back(std::make_pair(V, 1U));
231 ValueMap[V] = Values.size();
232 return;
Devang Patel3e1ef932009-07-29 22:34:41 +0000233 }
234
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 // Add the value.
236 Values.push_back(std::make_pair(V, 1U));
237 ValueID = Values.size();
238}
239
240
241void ValueEnumerator::EnumerateType(const Type *Ty) {
242 unsigned &TypeID = TypeMap[Ty];
243
244 if (TypeID) {
245 // If we've already seen this type, just increase its occurrence count.
246 Types[TypeID-1].second++;
247 return;
248 }
249
250 // First time we saw this type, add it.
251 Types.push_back(std::make_pair(Ty, 1U));
252 TypeID = Types.size();
253
254 // Enumerate subtypes.
255 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
256 I != E; ++I)
257 EnumerateType(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258}
259
260// Enumerate the types for the specified value. If the value is a constant,
261// walk through it, enumerating the types of the constant.
262void ValueEnumerator::EnumerateOperandType(const Value *V) {
263 EnumerateType(V->getType());
264 if (const Constant *C = dyn_cast<Constant>(V)) {
265 // If this constant is already enumerated, ignore it, we know its type must
266 // be enumerated.
267 if (ValueMap.count(V)) return;
268
269 // This constant may have operands, make sure to enumerate the types in
270 // them.
271 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
272 EnumerateOperandType(C->getOperand(i));
Nick Lewycky117f4382009-05-10 20:57:05 +0000273
274 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000275 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
276 Value *Elem = N->getElement(i);
277 if (Elem)
278 EnumerateOperandType(Elem);
279 }
Nick Lewycky117f4382009-05-10 20:57:05 +0000280 }
Devang Patel54199ef2009-07-23 01:07:34 +0000281 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000282 EnumerateValue(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283}
284
Devang Pateld222f862008-09-25 21:00:45 +0000285void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000286 if (PAL.isEmpty()) return; // null is always 0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // Do a lookup.
Devang Pateld222f862008-09-25 21:00:45 +0000288 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 if (Entry == 0) {
290 // Never saw this before, add it.
Devang Pateld222f862008-09-25 21:00:45 +0000291 Attributes.push_back(PAL);
292 Entry = Attributes.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 }
294}
295
296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297void ValueEnumerator::incorporateFunction(const Function &F) {
298 NumModuleValues = Values.size();
299
300 // Adding function arguments to the value table.
301 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
302 I != E; ++I)
303 EnumerateValue(I);
304
305 FirstFuncConstantID = Values.size();
306
307 // Add all function-level constants to the value table.
308 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
309 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
310 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
311 OI != E; ++OI) {
312 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
313 isa<InlineAsm>(*OI))
314 EnumerateValue(*OI);
315 }
316 BasicBlocks.push_back(BB);
317 ValueMap[BB] = BasicBlocks.size();
318 }
319
320 // Optimize the constant layout.
321 OptimizeConstants(FirstFuncConstantID, Values.size());
322
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000323 // Add the function's parameter attributes so they are available for use in
324 // the function's instruction.
Devang Pateld222f862008-09-25 21:00:45 +0000325 EnumerateAttributes(F.getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000326
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 FirstInstID = Values.size();
328
329 // Add all of the instructions.
330 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
331 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
332 if (I->getType() != Type::VoidTy)
333 EnumerateValue(I);
334 }
335 }
336}
337
338void ValueEnumerator::purgeFunction() {
339 /// Remove purged values from the ValueMap.
340 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
341 ValueMap.erase(Values[i].first);
342 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
343 ValueMap.erase(BasicBlocks[i]);
344
345 Values.resize(NumModuleValues);
346 BasicBlocks.clear();
347}
348