Initial support for writing bitcode files. This currently only writes types,
[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/Module.h"
16 #include "llvm/TypeSymbolTable.h"
17 #include "llvm/ValueSymbolTable.h"
18 using namespace llvm;
19
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)
25     EnumerateValue(I);
26
27   // Enumerate the functions.
28   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
29     EnumerateValue(I);
30
31   // Enumerate the global variable initializers.
32   for (Module::const_global_iterator I = M->global_begin(),
33          E = M->global_end(); I != E; ++I)
34     if (I->hasInitializer())
35       EnumerateValue(I->getInitializer());
36
37   // FIXME: Implement the 'string constant' optimization.
38
39   // Enumerate types used by the type symbol table.
40   EnumerateTypeSymbolTable(M->getTypeSymbolTable());
41
42   // Insert constants that are named at module level into the slot pool so that
43   // the module symbol table can refer to them...
44   EnumerateValueSymbolTable(M->getValueSymbolTable());
45   
46   // Enumerate types used by function bodies.
47   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
48     for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
49       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
50         for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
51              OI != E; ++OI)
52           EnumerateType((*OI)->getType());
53         EnumerateType(I->getType());
54       }
55   }
56     
57   
58   // FIXME: std::partition the type and value tables so that first-class types
59   // come earlier than aggregates.
60   
61   // FIXME: Sort type/value tables by frequency.
62 }
63
64 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
65 /// table.
66 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
67   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
68        TI != TE; ++TI)
69     EnumerateType(TI->second);
70 }
71
72 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
73 /// table into the values table.
74 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
75   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); 
76        VI != VE; ++VI)
77     EnumerateValue(VI->getValue());
78 }
79
80 void ValueEnumerator::EnumerateValue(const Value *V) {
81   assert(V->getType() != Type::VoidTy && "Can't insert void values!");
82   
83   // Check to see if it's already in!
84   unsigned &ValueID = ValueMap[V];
85   if (ValueID) {
86     // Increment use count.
87     Values[ValueID-1].second++;
88     return;
89   }
90   
91   // Add the value.
92   Values.push_back(std::make_pair(V, 1U));
93   ValueID = Values.size();
94
95   if (const Constant *C = dyn_cast<Constant>(V)) {
96     if (isa<GlobalValue>(C)) {
97       // Initializers for globals are handled explicitly elsewhere.
98     } else {
99       // This makes sure that if a constant has uses (for example an array of
100       // const ints), that they are inserted also.
101       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
102            I != E; ++I)
103         EnumerateValue(*I);
104     }
105   }
106
107   EnumerateType(V->getType());
108 }
109
110
111 void ValueEnumerator::EnumerateType(const Type *Ty) {
112   unsigned &TypeID = TypeMap[Ty];
113   
114   if (TypeID) {
115     // If we've already seen this type, just increase its occurrence count.
116     Types[TypeID-1].second++;
117     return;
118   }
119   
120   // First time we saw this type, add it.
121   Types.push_back(std::make_pair(Ty, 1U));
122   TypeID = Types.size();
123   
124   // Enumerate subtypes.
125   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
126        I != E; ++I)
127     EnumerateType(*I);
128 }
129
130
131
132 #if 0
133
134 void SlotCalculator::incorporateFunction(const Function *F) {
135   SC_DEBUG("begin processFunction!\n");
136   
137   // Iterate over function arguments, adding them to the value table...
138   for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
139       I != E; ++I)
140     CreateFunctionValueSlot(I);
141   
142   SC_DEBUG("Inserting Instructions:\n");
143   
144   // Add all of the instructions to the type planes...
145   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
146     CreateFunctionValueSlot(BB);
147     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
148       if (I->getType() != Type::VoidTy)
149         CreateFunctionValueSlot(I);
150     }
151   }
152   
153   SC_DEBUG("end processFunction!\n");
154 }
155
156 void SlotCalculator::purgeFunction() {
157   SC_DEBUG("begin purgeFunction!\n");
158   
159   // Next, remove values from existing type planes
160   for (DenseMap<unsigned,unsigned,
161           ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
162        E = ModuleLevel.end(); I != E; ++I) {
163     unsigned PlaneNo = I->first;
164     unsigned ModuleLev = I->second;
165     
166     // Pop all function-local values in this type-plane off of Table.
167     TypePlane &Plane = getPlane(PlaneNo);
168     assert(ModuleLev < Plane.size() && "module levels higher than elements?");
169     for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
170       NodeMap.erase(Plane.back());       // Erase from nodemap
171       Plane.pop_back();                  // Shrink plane
172     }
173   }
174
175   ModuleLevel.clear();
176
177   // Finally, remove any type planes defined by the function...
178   while (Table.size() > NumModuleTypes) {
179     TypePlane &Plane = Table.back();
180     SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
181              << Plane.size() << "\n");
182     for (unsigned i = 0, e = Plane.size(); i != e; ++i)
183       NodeMap.erase(Plane[i]);   // Erase from nodemap
184     
185     Table.pop_back();                // Nuke the plane, we don't like it.
186   }
187   
188   SC_DEBUG("end purgeFunction!\n");
189 }
190
191 inline static bool hasImplicitNull(const Type* Ty) {
192   return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
193 }
194
195 void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
196   assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
197   
198   const Type *Ty = V->getType();
199   assert(Ty != Type::VoidTy && "Can't insert void values!");
200   assert(!isa<Constant>(V) && "Not a function-local value!");
201   
202   unsigned TyPlane = getOrCreateTypeSlot(Ty);
203   if (Table.size() <= TyPlane)    // Make sure we have the type plane allocated.
204     Table.resize(TyPlane+1, TypePlane());
205   
206   // If this is the first value noticed of this type within this function,
207   // remember the module level for this type plane in ModuleLevel.  This reminds
208   // us to remove the values in purgeFunction and tells us how many to remove.
209   if (TyPlane < NumModuleTypes)
210     ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
211   
212   // If this is the first value to get inserted into the type plane, make sure
213   // to insert the implicit null value.
214   if (Table[TyPlane].empty()) {
215     // Label's and opaque types can't have a null value.
216     if (hasImplicitNull(Ty)) {
217       Value *ZeroInitializer = Constant::getNullValue(Ty);
218       
219       // If we are pushing zeroinit, it will be handled below.
220       if (V != ZeroInitializer) {
221         Table[TyPlane].push_back(ZeroInitializer);
222         NodeMap[ZeroInitializer] = 0;
223       }
224     }
225   }
226   
227   // Insert node into table and NodeMap...
228   NodeMap[V] = Table[TyPlane].size();
229   Table[TyPlane].push_back(V);
230   
231   SC_DEBUG("  Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
232            NodeMap[V] << "\n");
233 }
234
235 #endif