1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ValueEnumerator class.
12 //===----------------------------------------------------------------------===//
14 #include "ValueEnumerator.h"
15 #include "llvm/Module.h"
16 #include "llvm/TypeSymbolTable.h"
17 #include "llvm/ValueSymbolTable.h"
20 /// ValueEnumerator - Enumerate module-level information.
21 ValueEnumerator::ValueEnumerator(const Module *M) {
22 // Enumerate the global variables.
23 for (Module::const_global_iterator I = M->global_begin(),
24 E = M->global_end(); I != E; ++I)
27 // Enumerate the functions.
28 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
31 // Enumerate the aliases.
32 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
36 // Enumerate the global variable initializers.
37 for (Module::const_global_iterator I = M->global_begin(),
38 E = M->global_end(); I != E; ++I)
39 if (I->hasInitializer())
40 EnumerateValue(I->getInitializer());
42 // Enumerate the aliasees.
43 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
45 EnumerateValue(I->getAliasee());
47 // FIXME: Implement the 'string constant' optimization.
49 // Enumerate types used by the type symbol table.
50 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
52 // Insert constants that are named at module level into the slot pool so that
53 // the module symbol table can refer to them...
54 EnumerateValueSymbolTable(M->getValueSymbolTable());
56 // Enumerate types used by function bodies and argument lists.
57 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
59 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
61 EnumerateType(I->getType());
63 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
64 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
65 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
67 EnumerateType((*OI)->getType());
68 EnumerateType(I->getType());
73 // FIXME: std::partition the type and value tables so that first-class types
74 // come earlier than aggregates. FIXME: Emit a marker into the module
75 // indicating which aggregates types AND values can be dropped form the table.
77 // FIXME: Sort type/value tables by frequency.
79 // FIXME: Sort constants by type to reduce size.
82 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
84 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
85 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
87 EnumerateType(TI->second);
90 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
91 /// table into the values table.
92 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
93 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
95 EnumerateValue(VI->getValue());
98 void ValueEnumerator::EnumerateValue(const Value *V) {
99 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
101 // Check to see if it's already in!
102 unsigned &ValueID = ValueMap[V];
104 // Increment use count.
105 Values[ValueID-1].second++;
110 Values.push_back(std::make_pair(V, 1U));
111 ValueID = Values.size();
113 if (const Constant *C = dyn_cast<Constant>(V)) {
114 if (isa<GlobalValue>(C)) {
115 // Initializers for globals are handled explicitly elsewhere.
117 // This makes sure that if a constant has uses (for example an array of
118 // const ints), that they are inserted also.
119 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
125 EnumerateType(V->getType());
129 void ValueEnumerator::EnumerateType(const Type *Ty) {
130 unsigned &TypeID = TypeMap[Ty];
133 // If we've already seen this type, just increase its occurrence count.
134 Types[TypeID-1].second++;
138 // First time we saw this type, add it.
139 Types.push_back(std::make_pair(Ty, 1U));
140 TypeID = Types.size();
142 // Enumerate subtypes.
143 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
148 /// PurgeAggregateValues - If there are any aggregate values at the end of the
149 /// value list, remove them and return the count of the remaining values. If
150 /// there are none, return -1.
151 int ValueEnumerator::PurgeAggregateValues() {
152 // If there are no aggregate values at the end of the list, return -1.
153 if (Values.empty() || Values.back().first->getType()->isFirstClassType())
156 // Otherwise, remove aggregate values...
157 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
160 // ... and return the new size.
161 return Values.size();
164 void ValueEnumerator::incorporateFunction(const Function &F) {
165 ModuleLevel = Values.size();
167 // Adding function arguments to the value table.
168 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
172 // Add all function-level constants to the value table.
173 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
174 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
175 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
177 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
181 ValueMap[BB] = BasicBlocks.size();
182 BasicBlocks.push_back(BB);
185 // Add all of the instructions.
186 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
187 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
188 if (I->getType() != Type::VoidTy)
194 void ValueEnumerator::purgeFunction() {
195 /// Remove purged values from the ValueMap.
196 for (unsigned i = ModuleLevel, e = Values.size(); i != e; ++i)
197 ValueMap.erase(Values[i].first);
198 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
199 ValueMap.erase(BasicBlocks[i]);
201 Values.resize(ModuleLevel);