Fixed some bugs.
[llvm/zpu.git] / lib / Bitcode / Writer / ValueEnumerator.cpp
blob2f02262c36aff12518340a7898d0662b470e521e
1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueEnumerator class.
12 //===----------------------------------------------------------------------===//
14 #include "ValueEnumerator.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/TypeSymbolTable.h"
19 #include "llvm/ValueSymbolTable.h"
20 #include "llvm/Instructions.h"
21 #include <algorithm>
22 using namespace llvm;
24 static bool isSingleValueType(const std::pair<const llvm::Type*,
25 unsigned int> &P) {
26 return P.first->isSingleValueType();
29 static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
30 return V.first->getType()->isIntegerTy();
33 static 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;
40 /// ValueEnumerator - Enumerate module-level information.
41 ValueEnumerator::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);
47 // Enumerate the functions.
48 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
49 EnumerateValue(I);
50 EnumerateAttributes(cast<Function>(I)->getAttributes());
53 // Enumerate the aliases.
54 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
55 I != E; ++I)
56 EnumerateValue(I);
58 // Remember what is the cutoff between globalvalue's and other constants.
59 unsigned FirstConstant = Values.size();
61 // 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());
67 // 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());
72 // Enumerate types used by the type symbol table.
73 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
75 // Insert constants and metadata that are named at module level into the slot
76 // pool so that the module symbol table can refer to them...
77 EnumerateValueSymbolTable(M->getValueSymbolTable());
78 EnumerateNamedMetadata(M);
80 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
82 // Enumerate types used by function bodies and argument lists.
83 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
85 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
86 I != E; ++I)
87 EnumerateType(I->getType());
89 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
90 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
91 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
92 OI != E; ++OI) {
93 if (MDNode *MD = dyn_cast<MDNode>(*OI))
94 if (MD->isFunctionLocal() && MD->getFunction())
95 // These will get enumerated during function-incorporation.
96 continue;
97 EnumerateOperandType(*OI);
99 EnumerateType(I->getType());
100 if (const CallInst *CI = dyn_cast<CallInst>(I))
101 EnumerateAttributes(CI->getAttributes());
102 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
103 EnumerateAttributes(II->getAttributes());
105 // Enumerate metadata attached with this instruction.
106 MDs.clear();
107 I->getAllMetadataOtherThanDebugLoc(MDs);
108 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
109 EnumerateMetadata(MDs[i].second);
111 if (!I->getDebugLoc().isUnknown()) {
112 MDNode *Scope, *IA;
113 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
114 if (Scope) EnumerateMetadata(Scope);
115 if (IA) EnumerateMetadata(IA);
120 // Optimize constant ordering.
121 OptimizeConstants(FirstConstant, Values.size());
123 // Sort the type table by frequency so that most commonly used types are early
124 // in the table (have low bit-width).
125 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
127 // Partition the Type ID's so that the single-value types occur before the
128 // aggregate types. This allows the aggregate types to be dropped from the
129 // type table after parsing the global variable initializers.
130 std::partition(Types.begin(), Types.end(), isSingleValueType);
132 // Now that we rearranged the type table, rebuild TypeMap.
133 for (unsigned i = 0, e = Types.size(); i != e; ++i)
134 TypeMap[Types[i].first] = i+1;
137 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
138 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
139 assert (I != InstructionMap.end() && "Instruction is not mapped!");
140 return I->second;
143 void ValueEnumerator::setInstructionID(const Instruction *I) {
144 InstructionMap[I] = InstructionCount++;
147 unsigned ValueEnumerator::getValueID(const Value *V) const {
148 if (isa<MDNode>(V) || isa<MDString>(V)) {
149 ValueMapType::const_iterator I = MDValueMap.find(V);
150 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
151 return I->second-1;
154 ValueMapType::const_iterator I = ValueMap.find(V);
155 assert(I != ValueMap.end() && "Value not in slotcalculator!");
156 return I->second-1;
159 // Optimize constant ordering.
160 namespace {
161 struct CstSortPredicate {
162 ValueEnumerator &VE;
163 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
164 bool operator()(const std::pair<const Value*, unsigned> &LHS,
165 const std::pair<const Value*, unsigned> &RHS) {
166 // Sort by plane.
167 if (LHS.first->getType() != RHS.first->getType())
168 return VE.getTypeID(LHS.first->getType()) <
169 VE.getTypeID(RHS.first->getType());
170 // Then by frequency.
171 return LHS.second > RHS.second;
176 /// OptimizeConstants - Reorder constant pool for denser encoding.
177 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
178 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
180 CstSortPredicate P(*this);
181 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
183 // Ensure that integer constants are at the start of the constant pool. This
184 // is important so that GEP structure indices come before gep constant exprs.
185 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
186 isIntegerValue);
188 // Rebuild the modified portion of ValueMap.
189 for (; CstStart != CstEnd; ++CstStart)
190 ValueMap[Values[CstStart].first] = CstStart+1;
194 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
195 /// table.
196 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
197 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
198 TI != TE; ++TI)
199 EnumerateType(TI->second);
202 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
203 /// table into the values table.
204 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
205 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
206 VI != VE; ++VI)
207 EnumerateValue(VI->getValue());
210 /// EnumerateNamedMetadata - Insert all of the values referenced by
211 /// named metadata in the specified module.
212 void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
213 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
214 E = M->named_metadata_end(); I != E; ++I)
215 EnumerateNamedMDNode(I);
218 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
219 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
220 EnumerateMetadata(MD->getOperand(i));
223 /// EnumerateMDNodeOperands - Enumerate all non-function-local values
224 /// and types referenced by the given MDNode.
225 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
226 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
227 if (Value *V = N->getOperand(i)) {
228 if (isa<MDNode>(V) || isa<MDString>(V))
229 EnumerateMetadata(V);
230 else if (!isa<Instruction>(V) && !isa<Argument>(V))
231 EnumerateValue(V);
232 } else
233 EnumerateType(Type::getVoidTy(N->getContext()));
237 void ValueEnumerator::EnumerateMetadata(const Value *MD) {
238 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
240 // Enumerate the type of this value.
241 EnumerateType(MD->getType());
243 const MDNode *N = dyn_cast<MDNode>(MD);
245 // In the module-level pass, skip function-local nodes themselves, but
246 // do walk their operands.
247 if (N && N->isFunctionLocal() && N->getFunction()) {
248 EnumerateMDNodeOperands(N);
249 return;
252 // Check to see if it's already in!
253 unsigned &MDValueID = MDValueMap[MD];
254 if (MDValueID) {
255 // Increment use count.
256 MDValues[MDValueID-1].second++;
257 return;
259 MDValues.push_back(std::make_pair(MD, 1U));
260 MDValueID = MDValues.size();
262 // Enumerate all non-function-local operands.
263 if (N)
264 EnumerateMDNodeOperands(N);
267 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
268 /// information reachable from the given MDNode.
269 void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
270 assert(N->isFunctionLocal() && N->getFunction() &&
271 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
273 // Enumerate the type of this value.
274 EnumerateType(N->getType());
276 // Check to see if it's already in!
277 unsigned &MDValueID = MDValueMap[N];
278 if (MDValueID) {
279 // Increment use count.
280 MDValues[MDValueID-1].second++;
281 return;
283 MDValues.push_back(std::make_pair(N, 1U));
284 MDValueID = MDValues.size();
286 // To incoroporate function-local information visit all function-local
287 // MDNodes and all function-local values they reference.
288 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
289 if (Value *V = N->getOperand(i)) {
290 if (MDNode *O = dyn_cast<MDNode>(V)) {
291 if (O->isFunctionLocal() && O->getFunction())
292 EnumerateFunctionLocalMetadata(O);
293 } else if (isa<Instruction>(V) || isa<Argument>(V))
294 EnumerateValue(V);
297 // Also, collect all function-local MDNodes for easy access.
298 FunctionLocalMDs.push_back(N);
301 void ValueEnumerator::EnumerateValue(const Value *V) {
302 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
303 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
304 "EnumerateValue doesn't handle Metadata!");
306 // Check to see if it's already in!
307 unsigned &ValueID = ValueMap[V];
308 if (ValueID) {
309 // Increment use count.
310 Values[ValueID-1].second++;
311 return;
314 // Enumerate the type of this value.
315 EnumerateType(V->getType());
317 if (const Constant *C = dyn_cast<Constant>(V)) {
318 if (isa<GlobalValue>(C)) {
319 // Initializers for globals are handled explicitly elsewhere.
320 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
321 // Do not enumerate the initializers for an array of simple characters.
322 // The initializers just polute the value table, and we emit the strings
323 // specially.
324 } else if (C->getNumOperands()) {
325 // If a constant has operands, enumerate them. This makes sure that if a
326 // constant has uses (for example an array of const ints), that they are
327 // inserted also.
329 // We prefer to enumerate them with values before we enumerate the user
330 // itself. This makes it more likely that we can avoid forward references
331 // in the reader. We know that there can be no cycles in the constants
332 // graph that don't go through a global variable.
333 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
334 I != E; ++I)
335 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
336 EnumerateValue(*I);
338 // Finally, add the value. Doing this could make the ValueID reference be
339 // dangling, don't reuse it.
340 Values.push_back(std::make_pair(V, 1U));
341 ValueMap[V] = Values.size();
342 return;
346 // Add the value.
347 Values.push_back(std::make_pair(V, 1U));
348 ValueID = Values.size();
352 void ValueEnumerator::EnumerateType(const Type *Ty) {
353 unsigned &TypeID = TypeMap[Ty];
355 if (TypeID) {
356 // If we've already seen this type, just increase its occurrence count.
357 Types[TypeID-1].second++;
358 return;
361 // First time we saw this type, add it.
362 Types.push_back(std::make_pair(Ty, 1U));
363 TypeID = Types.size();
365 // Enumerate subtypes.
366 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
367 I != E; ++I)
368 EnumerateType(*I);
371 // Enumerate the types for the specified value. If the value is a constant,
372 // walk through it, enumerating the types of the constant.
373 void ValueEnumerator::EnumerateOperandType(const Value *V) {
374 EnumerateType(V->getType());
376 if (const Constant *C = dyn_cast<Constant>(V)) {
377 // If this constant is already enumerated, ignore it, we know its type must
378 // be enumerated.
379 if (ValueMap.count(V)) return;
381 // This constant may have operands, make sure to enumerate the types in
382 // them.
383 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
384 const User *Op = C->getOperand(i);
386 // Don't enumerate basic blocks here, this happens as operands to
387 // blockaddress.
388 if (isa<BasicBlock>(Op)) continue;
390 EnumerateOperandType(Op);
393 if (const MDNode *N = dyn_cast<MDNode>(V)) {
394 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
395 if (Value *Elem = N->getOperand(i))
396 EnumerateOperandType(Elem);
398 } else if (isa<MDString>(V) || isa<MDNode>(V))
399 EnumerateMetadata(V);
402 void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
403 if (PAL.isEmpty()) return; // null is always 0.
404 // Do a lookup.
405 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
406 if (Entry == 0) {
407 // Never saw this before, add it.
408 Attributes.push_back(PAL);
409 Entry = Attributes.size();
414 void ValueEnumerator::incorporateFunction(const Function &F) {
415 InstructionCount = 0;
416 NumModuleValues = Values.size();
417 NumModuleMDValues = MDValues.size();
419 // Adding function arguments to the value table.
420 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
421 I != E; ++I)
422 EnumerateValue(I);
424 FirstFuncConstantID = Values.size();
426 // Add all function-level constants to the value table.
427 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
428 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
429 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
430 OI != E; ++OI) {
431 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
432 isa<InlineAsm>(*OI))
433 EnumerateValue(*OI);
435 BasicBlocks.push_back(BB);
436 ValueMap[BB] = BasicBlocks.size();
439 // Optimize the constant layout.
440 OptimizeConstants(FirstFuncConstantID, Values.size());
442 // Add the function's parameter attributes so they are available for use in
443 // the function's instruction.
444 EnumerateAttributes(F.getAttributes());
446 FirstInstID = Values.size();
448 SmallVector<MDNode *, 8> FnLocalMDVector;
449 // Add all of the instructions.
450 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
451 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
452 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
453 OI != E; ++OI) {
454 if (MDNode *MD = dyn_cast<MDNode>(*OI))
455 if (MD->isFunctionLocal() && MD->getFunction())
456 // Enumerate metadata after the instructions they might refer to.
457 FnLocalMDVector.push_back(MD);
460 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
461 I->getAllMetadataOtherThanDebugLoc(MDs);
462 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
463 MDNode *N = MDs[i].second;
464 if (N->isFunctionLocal() && N->getFunction())
465 FnLocalMDVector.push_back(N);
468 if (!I->getType()->isVoidTy())
469 EnumerateValue(I);
473 // Add all of the function-local metadata.
474 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
475 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
478 void ValueEnumerator::purgeFunction() {
479 /// Remove purged values from the ValueMap.
480 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
481 ValueMap.erase(Values[i].first);
482 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
483 MDValueMap.erase(MDValues[i].first);
484 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
485 ValueMap.erase(BasicBlocks[i]);
487 Values.resize(NumModuleValues);
488 MDValues.resize(NumModuleMDValues);
489 BasicBlocks.clear();
490 FunctionLocalMDs.clear();
493 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
494 DenseMap<const BasicBlock*, unsigned> &IDMap) {
495 unsigned Counter = 0;
496 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
497 IDMap[BB] = ++Counter;
500 /// getGlobalBasicBlockID - This returns the function-specific ID for the
501 /// specified basic block. This is relatively expensive information, so it
502 /// should only be used by rare constructs such as address-of-label.
503 unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
504 unsigned &Idx = GlobalBasicBlockIDs[BB];
505 if (Idx != 0)
506 return Idx-1;
508 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
509 return getGlobalBasicBlockID(BB);