IR: Add COMDATs to the IR
[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 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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ValueEnumerator.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ValueSymbolTable.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 using namespace llvm;
26
27 static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
28   return V.first->getType()->isIntOrIntVectorTy();
29 }
30
31 /// ValueEnumerator - Enumerate module-level information.
32 ValueEnumerator::ValueEnumerator(const Module *M) {
33   // Enumerate the global variables.
34   for (Module::const_global_iterator I = M->global_begin(),
35          E = M->global_end(); I != E; ++I)
36     EnumerateValue(I);
37
38   // Enumerate the functions.
39   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
40     EnumerateValue(I);
41     EnumerateAttributes(cast<Function>(I)->getAttributes());
42   }
43
44   // Enumerate the aliases.
45   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
46        I != E; ++I)
47     EnumerateValue(I);
48
49   // Remember what is the cutoff between globalvalue's and other constants.
50   unsigned FirstConstant = Values.size();
51
52   // Enumerate the global variable initializers.
53   for (Module::const_global_iterator I = M->global_begin(),
54          E = M->global_end(); I != E; ++I)
55     if (I->hasInitializer())
56       EnumerateValue(I->getInitializer());
57
58   // Enumerate the aliasees.
59   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
60        I != E; ++I)
61     EnumerateValue(I->getAliasee());
62
63   // Enumerate the prefix data constants.
64   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
65     if (I->hasPrefixData())
66       EnumerateValue(I->getPrefixData());
67
68   // Insert constants and metadata that are named at module level into the slot
69   // pool so that the module symbol table can refer to them...
70   EnumerateValueSymbolTable(M->getValueSymbolTable());
71   EnumerateNamedMetadata(M);
72
73   SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
74
75   // Enumerate types used by function bodies and argument lists.
76   for (const Function &F : *M) {
77     for (const Argument &A : F.args())
78       EnumerateType(A.getType());
79
80     for (const BasicBlock &BB : F)
81       for (const Instruction &I : BB) {
82         for (const Use &Op : I.operands()) {
83           if (MDNode *MD = dyn_cast<MDNode>(&Op))
84             if (MD->isFunctionLocal() && MD->getFunction())
85               // These will get enumerated during function-incorporation.
86               continue;
87           EnumerateOperandType(Op);
88         }
89         EnumerateType(I.getType());
90         if (const CallInst *CI = dyn_cast<CallInst>(&I))
91           EnumerateAttributes(CI->getAttributes());
92         else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
93           EnumerateAttributes(II->getAttributes());
94
95         // Enumerate metadata attached with this instruction.
96         MDs.clear();
97         I.getAllMetadataOtherThanDebugLoc(MDs);
98         for (unsigned i = 0, e = MDs.size(); i != e; ++i)
99           EnumerateMetadata(MDs[i].second);
100
101         if (!I.getDebugLoc().isUnknown()) {
102           MDNode *Scope, *IA;
103           I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
104           if (Scope) EnumerateMetadata(Scope);
105           if (IA) EnumerateMetadata(IA);
106         }
107       }
108   }
109
110   // Optimize constant ordering.
111   OptimizeConstants(FirstConstant, Values.size());
112 }
113
114 unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
115   InstructionMapType::const_iterator I = InstructionMap.find(Inst);
116   assert(I != InstructionMap.end() && "Instruction is not mapped!");
117   return I->second;
118 }
119
120 unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
121   unsigned ComdatID = Comdats.idFor(C);
122   assert(ComdatID && "Comdat not found!");
123   return ComdatID;
124 }
125
126 void ValueEnumerator::setInstructionID(const Instruction *I) {
127   InstructionMap[I] = InstructionCount++;
128 }
129
130 unsigned ValueEnumerator::getValueID(const Value *V) const {
131   if (isa<MDNode>(V) || isa<MDString>(V)) {
132     ValueMapType::const_iterator I = MDValueMap.find(V);
133     assert(I != MDValueMap.end() && "Value not in slotcalculator!");
134     return I->second-1;
135   }
136
137   ValueMapType::const_iterator I = ValueMap.find(V);
138   assert(I != ValueMap.end() && "Value not in slotcalculator!");
139   return I->second-1;
140 }
141
142 void ValueEnumerator::dump() const {
143   print(dbgs(), ValueMap, "Default");
144   dbgs() << '\n';
145   print(dbgs(), MDValueMap, "MetaData");
146   dbgs() << '\n';
147 }
148
149 void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
150                             const char *Name) const {
151
152   OS << "Map Name: " << Name << "\n";
153   OS << "Size: " << Map.size() << "\n";
154   for (ValueMapType::const_iterator I = Map.begin(),
155          E = Map.end(); I != E; ++I) {
156
157     const Value *V = I->first;
158     if (V->hasName())
159       OS << "Value: " << V->getName();
160     else
161       OS << "Value: [null]\n";
162     V->dump();
163
164     OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
165     for (const Use &U : V->uses()) {
166       if (&U != &*V->use_begin())
167         OS << ",";
168       if(U->hasName())
169         OS << " " << U->getName();
170       else
171         OS << " [null]";
172
173     }
174     OS <<  "\n\n";
175   }
176 }
177
178 /// OptimizeConstants - Reorder constant pool for denser encoding.
179 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
180   if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
181
182   std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
183                    [this](const std::pair<const Value *, unsigned> &LHS,
184                           const std::pair<const Value *, unsigned> &RHS) {
185     // Sort by plane.
186     if (LHS.first->getType() != RHS.first->getType())
187       return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
188     // Then by frequency.
189     return LHS.second > RHS.second;
190   });
191
192   // Ensure that integer and vector of integer constants are at the start of the
193   // constant pool.  This is important so that GEP structure indices come before
194   // gep constant exprs.
195   std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
196                  isIntOrIntVectorValue);
197
198   // Rebuild the modified portion of ValueMap.
199   for (; CstStart != CstEnd; ++CstStart)
200     ValueMap[Values[CstStart].first] = CstStart+1;
201 }
202
203
204 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
205 /// table into the values table.
206 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
207   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
208        VI != VE; ++VI)
209     EnumerateValue(VI->getValue());
210 }
211
212 /// EnumerateNamedMetadata - Insert all of the values referenced by
213 /// named metadata in the specified module.
214 void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
215   for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
216        E = M->named_metadata_end(); I != E; ++I)
217     EnumerateNamedMDNode(I);
218 }
219
220 void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
221   for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
222     EnumerateMetadata(MD->getOperand(i));
223 }
224
225 /// EnumerateMDNodeOperands - Enumerate all non-function-local values
226 /// and types referenced by the given MDNode.
227 void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
228   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
229     if (Value *V = N->getOperand(i)) {
230       if (isa<MDNode>(V) || isa<MDString>(V))
231         EnumerateMetadata(V);
232       else if (!isa<Instruction>(V) && !isa<Argument>(V))
233         EnumerateValue(V);
234     } else
235       EnumerateType(Type::getVoidTy(N->getContext()));
236   }
237 }
238
239 void ValueEnumerator::EnumerateMetadata(const Value *MD) {
240   assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
241
242   // Enumerate the type of this value.
243   EnumerateType(MD->getType());
244
245   const MDNode *N = dyn_cast<MDNode>(MD);
246
247   // In the module-level pass, skip function-local nodes themselves, but
248   // do walk their operands.
249   if (N && N->isFunctionLocal() && N->getFunction()) {
250     EnumerateMDNodeOperands(N);
251     return;
252   }
253
254   // Check to see if it's already in!
255   unsigned &MDValueID = MDValueMap[MD];
256   if (MDValueID) {
257     // Increment use count.
258     MDValues[MDValueID-1].second++;
259     return;
260   }
261   MDValues.push_back(std::make_pair(MD, 1U));
262   MDValueID = MDValues.size();
263
264   // Enumerate all non-function-local operands.
265   if (N)
266     EnumerateMDNodeOperands(N);
267 }
268
269 /// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
270 /// information reachable from the given MDNode.
271 void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
272   assert(N->isFunctionLocal() && N->getFunction() &&
273          "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
274
275   // Enumerate the type of this value.
276   EnumerateType(N->getType());
277
278   // Check to see if it's already in!
279   unsigned &MDValueID = MDValueMap[N];
280   if (MDValueID) {
281     // Increment use count.
282     MDValues[MDValueID-1].second++;
283     return;
284   }
285   MDValues.push_back(std::make_pair(N, 1U));
286   MDValueID = MDValues.size();
287
288   // To incoroporate function-local information visit all function-local
289   // MDNodes and all function-local values they reference.
290   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
291     if (Value *V = N->getOperand(i)) {
292       if (MDNode *O = dyn_cast<MDNode>(V)) {
293         if (O->isFunctionLocal() && O->getFunction())
294           EnumerateFunctionLocalMetadata(O);
295       } else if (isa<Instruction>(V) || isa<Argument>(V))
296         EnumerateValue(V);
297     }
298
299   // Also, collect all function-local MDNodes for easy access.
300   FunctionLocalMDs.push_back(N);
301 }
302
303 void ValueEnumerator::EnumerateValue(const Value *V) {
304   assert(!V->getType()->isVoidTy() && "Can't insert void values!");
305   assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
306          "EnumerateValue doesn't handle Metadata!");
307
308   // Check to see if it's already in!
309   unsigned &ValueID = ValueMap[V];
310   if (ValueID) {
311     // Increment use count.
312     Values[ValueID-1].second++;
313     return;
314   }
315
316   if (auto *GO = dyn_cast<GlobalObject>(V))
317     if (const Comdat *C = GO->getComdat())
318       Comdats.insert(C);
319
320   // Enumerate the type of this value.
321   EnumerateType(V->getType());
322
323   if (const Constant *C = dyn_cast<Constant>(V)) {
324     if (isa<GlobalValue>(C)) {
325       // Initializers for globals are handled explicitly elsewhere.
326     } else if (C->getNumOperands()) {
327       // If a constant has operands, enumerate them.  This makes sure that if a
328       // constant has uses (for example an array of const ints), that they are
329       // inserted also.
330
331       // We prefer to enumerate them with values before we enumerate the user
332       // itself.  This makes it more likely that we can avoid forward references
333       // in the reader.  We know that there can be no cycles in the constants
334       // graph that don't go through a global variable.
335       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
336            I != E; ++I)
337         if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
338           EnumerateValue(*I);
339
340       // Finally, add the value.  Doing this could make the ValueID reference be
341       // dangling, don't reuse it.
342       Values.push_back(std::make_pair(V, 1U));
343       ValueMap[V] = Values.size();
344       return;
345     }
346   }
347
348   // Add the value.
349   Values.push_back(std::make_pair(V, 1U));
350   ValueID = Values.size();
351 }
352
353
354 void ValueEnumerator::EnumerateType(Type *Ty) {
355   unsigned *TypeID = &TypeMap[Ty];
356
357   // We've already seen this type.
358   if (*TypeID)
359     return;
360
361   // If it is a non-anonymous struct, mark the type as being visited so that we
362   // don't recursively visit it.  This is safe because we allow forward
363   // references of these in the bitcode reader.
364   if (StructType *STy = dyn_cast<StructType>(Ty))
365     if (!STy->isLiteral())
366       *TypeID = ~0U;
367
368   // Enumerate all of the subtypes before we enumerate this type.  This ensures
369   // that the type will be enumerated in an order that can be directly built.
370   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
371        I != E; ++I)
372     EnumerateType(*I);
373
374   // Refresh the TypeID pointer in case the table rehashed.
375   TypeID = &TypeMap[Ty];
376
377   // Check to see if we got the pointer another way.  This can happen when
378   // enumerating recursive types that hit the base case deeper than they start.
379   //
380   // If this is actually a struct that we are treating as forward ref'able,
381   // then emit the definition now that all of its contents are available.
382   if (*TypeID && *TypeID != ~0U)
383     return;
384
385   // Add this type now that its contents are all happily enumerated.
386   Types.push_back(Ty);
387
388   *TypeID = Types.size();
389 }
390
391 // Enumerate the types for the specified value.  If the value is a constant,
392 // walk through it, enumerating the types of the constant.
393 void ValueEnumerator::EnumerateOperandType(const Value *V) {
394   EnumerateType(V->getType());
395
396   if (const Constant *C = dyn_cast<Constant>(V)) {
397     // If this constant is already enumerated, ignore it, we know its type must
398     // be enumerated.
399     if (ValueMap.count(V)) return;
400
401     // This constant may have operands, make sure to enumerate the types in
402     // them.
403     for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
404       const Value *Op = C->getOperand(i);
405
406       // Don't enumerate basic blocks here, this happens as operands to
407       // blockaddress.
408       if (isa<BasicBlock>(Op)) continue;
409
410       EnumerateOperandType(Op);
411     }
412
413     if (const MDNode *N = dyn_cast<MDNode>(V)) {
414       for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
415         if (Value *Elem = N->getOperand(i))
416           EnumerateOperandType(Elem);
417     }
418   } else if (isa<MDString>(V) || isa<MDNode>(V))
419     EnumerateMetadata(V);
420 }
421
422 void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
423   if (PAL.isEmpty()) return;  // null is always 0.
424
425   // Do a lookup.
426   unsigned &Entry = AttributeMap[PAL];
427   if (Entry == 0) {
428     // Never saw this before, add it.
429     Attribute.push_back(PAL);
430     Entry = Attribute.size();
431   }
432
433   // Do lookups for all attribute groups.
434   for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
435     AttributeSet AS = PAL.getSlotAttributes(i);
436     unsigned &Entry = AttributeGroupMap[AS];
437     if (Entry == 0) {
438       AttributeGroups.push_back(AS);
439       Entry = AttributeGroups.size();
440     }
441   }
442 }
443
444 void ValueEnumerator::incorporateFunction(const Function &F) {
445   InstructionCount = 0;
446   NumModuleValues = Values.size();
447   NumModuleMDValues = MDValues.size();
448
449   // Adding function arguments to the value table.
450   for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
451        I != E; ++I)
452     EnumerateValue(I);
453
454   FirstFuncConstantID = Values.size();
455
456   // Add all function-level constants to the value table.
457   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
458     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
459       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
460            OI != E; ++OI) {
461         if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
462             isa<InlineAsm>(*OI))
463           EnumerateValue(*OI);
464       }
465     BasicBlocks.push_back(BB);
466     ValueMap[BB] = BasicBlocks.size();
467   }
468
469   // Optimize the constant layout.
470   OptimizeConstants(FirstFuncConstantID, Values.size());
471
472   // Add the function's parameter attributes so they are available for use in
473   // the function's instruction.
474   EnumerateAttributes(F.getAttributes());
475
476   FirstInstID = Values.size();
477
478   SmallVector<MDNode *, 8> FnLocalMDVector;
479   // Add all of the instructions.
480   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
481     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
482       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
483            OI != E; ++OI) {
484         if (MDNode *MD = dyn_cast<MDNode>(*OI))
485           if (MD->isFunctionLocal() && MD->getFunction())
486             // Enumerate metadata after the instructions they might refer to.
487             FnLocalMDVector.push_back(MD);
488       }
489
490       SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
491       I->getAllMetadataOtherThanDebugLoc(MDs);
492       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
493         MDNode *N = MDs[i].second;
494         if (N->isFunctionLocal() && N->getFunction())
495           FnLocalMDVector.push_back(N);
496       }
497
498       if (!I->getType()->isVoidTy())
499         EnumerateValue(I);
500     }
501   }
502
503   // Add all of the function-local metadata.
504   for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
505     EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
506 }
507
508 void ValueEnumerator::purgeFunction() {
509   /// Remove purged values from the ValueMap.
510   for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
511     ValueMap.erase(Values[i].first);
512   for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
513     MDValueMap.erase(MDValues[i].first);
514   for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
515     ValueMap.erase(BasicBlocks[i]);
516
517   Values.resize(NumModuleValues);
518   MDValues.resize(NumModuleMDValues);
519   BasicBlocks.clear();
520   FunctionLocalMDs.clear();
521 }
522
523 static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
524                                  DenseMap<const BasicBlock*, unsigned> &IDMap) {
525   unsigned Counter = 0;
526   for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
527     IDMap[BB] = ++Counter;
528 }
529
530 /// getGlobalBasicBlockID - This returns the function-specific ID for the
531 /// specified basic block.  This is relatively expensive information, so it
532 /// should only be used by rare constructs such as address-of-label.
533 unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
534   unsigned &Idx = GlobalBasicBlockIDs[BB];
535   if (Idx != 0)
536     return Idx-1;
537
538   IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
539   return getGlobalBasicBlockID(BB);
540 }
541