trivial scaffolding for param attrs
[oota-llvm.git] / lib / Bitcode / Writer / ValueEnumerator.cpp
1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueEnumerator class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ValueEnumerator.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/TypeSymbolTable.h"
18 #include "llvm/ValueSymbolTable.h"
19 using namespace llvm;
20
21 /// ValueEnumerator - Enumerate module-level information.
22 ValueEnumerator::ValueEnumerator(const Module *M) {
23   // Enumerate the global variables.
24   for (Module::const_global_iterator I = M->global_begin(),
25          E = M->global_end(); I != E; ++I)
26     EnumerateValue(I);
27
28   // Enumerate the functions.
29   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
30     EnumerateValue(I);
31
32   // Enumerate the aliases.
33   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
34        I != E; ++I)
35     EnumerateValue(I);
36   
37   // Enumerate the global variable initializers.
38   for (Module::const_global_iterator I = M->global_begin(),
39          E = M->global_end(); I != E; ++I)
40     if (I->hasInitializer())
41       EnumerateValue(I->getInitializer());
42
43   // Enumerate the aliasees.
44   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
45        I != E; ++I)
46     EnumerateValue(I->getAliasee());
47   
48   // FIXME: Implement the 'string constant' optimization.
49
50   // Enumerate types used by the type symbol table.
51   EnumerateTypeSymbolTable(M->getTypeSymbolTable());
52
53   // Insert constants that are named at module level into the slot pool so that
54   // the module symbol table can refer to them...
55   EnumerateValueSymbolTable(M->getValueSymbolTable());
56   
57   // Enumerate types used by function bodies and argument lists.
58   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
59     
60     for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
61          I != E; ++I)
62       EnumerateType(I->getType());
63     
64     for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
65       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
66         for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
67              OI != E; ++OI)
68           EnumerateType((*OI)->getType());
69         EnumerateType(I->getType());
70       }
71   }
72     
73   
74   // FIXME: std::partition the type and value tables so that first-class types
75   // come earlier than aggregates.  FIXME: Emit a marker into the module
76   // indicating which aggregates types AND values can be dropped form the table.
77   
78   // FIXME: Sort type/value tables by frequency.
79     
80   // FIXME: Sort constants by type to reduce size.
81 }
82
83 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
84 /// table.
85 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
86   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
87        TI != TE; ++TI)
88     EnumerateType(TI->second);
89 }
90
91 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
92 /// table into the values table.
93 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
94   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); 
95        VI != VE; ++VI)
96     EnumerateValue(VI->getValue());
97 }
98
99 void ValueEnumerator::EnumerateValue(const Value *V) {
100   assert(V->getType() != Type::VoidTy && "Can't insert void values!");
101   
102   // Check to see if it's already in!
103   unsigned &ValueID = ValueMap[V];
104   if (ValueID) {
105     // Increment use count.
106     Values[ValueID-1].second++;
107     return;
108   }
109   
110   // Add the value.
111   Values.push_back(std::make_pair(V, 1U));
112   ValueID = Values.size();
113
114   if (const Constant *C = dyn_cast<Constant>(V)) {
115     if (isa<GlobalValue>(C)) {
116       // Initializers for globals are handled explicitly elsewhere.
117     } else {
118       // This makes sure that if a constant has uses (for example an array of
119       // const ints), that they are inserted also.
120       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
121            I != E; ++I)
122         EnumerateValue(*I);
123     }
124   }
125
126   EnumerateType(V->getType());
127 }
128
129
130 void ValueEnumerator::EnumerateType(const Type *Ty) {
131   unsigned &TypeID = TypeMap[Ty];
132   
133   if (TypeID) {
134     // If we've already seen this type, just increase its occurrence count.
135     Types[TypeID-1].second++;
136     return;
137   }
138   
139   // First time we saw this type, add it.
140   Types.push_back(std::make_pair(Ty, 1U));
141   TypeID = Types.size();
142   
143   // Enumerate subtypes.
144   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
145        I != E; ++I)
146     EnumerateType(*I);
147   
148   // If this is a function type, enumerate the param attrs.
149   if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty))
150     EnumerateParamAttrs(FTy->getParamAttrs());
151 }
152
153 void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) {
154   if (PAL == 0) return;  // null is always 0.
155   // Do a lookup.
156   unsigned &Entry = ParamAttrMap[PAL];
157   if (Entry == 0) {
158     // Never saw this before, add it.
159     ParamAttrs.push_back(PAL);
160     Entry = ParamAttrs.size();
161   }
162 }
163
164
165 /// PurgeAggregateValues - If there are any aggregate values at the end of the
166 /// value list, remove them and return the count of the remaining values.  If
167 /// there are none, return -1.
168 int ValueEnumerator::PurgeAggregateValues() {
169   // If there are no aggregate values at the end of the list, return -1.
170   if (Values.empty() || Values.back().first->getType()->isFirstClassType())
171     return -1;
172   
173   // Otherwise, remove aggregate values...
174   while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
175     Values.pop_back();
176   
177   // ... and return the new size.
178   return Values.size();
179 }
180
181 void ValueEnumerator::incorporateFunction(const Function &F) {
182   NumModuleValues = Values.size();
183   
184   // Adding function arguments to the value table.
185   for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
186       I != E; ++I)
187     EnumerateValue(I);
188
189   FirstFuncConstantID = Values.size();
190   
191   // Add all function-level constants to the value table.
192   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
193     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
194       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
195            OI != E; ++OI) {
196         if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
197             isa<InlineAsm>(*OI))
198           EnumerateValue(*OI);
199       }
200     BasicBlocks.push_back(BB);
201     ValueMap[BB] = BasicBlocks.size();
202   }
203   
204   FirstInstID = Values.size();
205   
206   // Add all of the instructions.
207   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
208     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
209       if (I->getType() != Type::VoidTy)
210         EnumerateValue(I);
211     }
212   }
213 }
214
215 void ValueEnumerator::purgeFunction() {
216   /// Remove purged values from the ValueMap.
217   for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
218     ValueMap.erase(Values[i].first);
219   for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
220     ValueMap.erase(BasicBlocks[i]);
221     
222   Values.resize(NumModuleValues);
223   BasicBlocks.clear();
224 }
225