LLVM API Documentation

ValueEnumerator.h

Go to the documentation of this file.
00001 //===-- Bitcode/Writer/ValueEnumerator.h - Number values --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class gives values and types Unique ID's.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef VALUE_ENUMERATOR_H
00015 #define VALUE_ENUMERATOR_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/Attributes.h"
00019 #include <vector>
00020 
00021 namespace llvm {
00022 
00023 class Type;
00024 class Value;
00025 class BasicBlock;
00026 class Function;
00027 class Module;
00028 class AttrListPtr;
00029 class TypeSymbolTable;
00030 class ValueSymbolTable;
00031 
00032 class ValueEnumerator {
00033 public:
00034   // For each type, we remember its Type* and occurrence frequency.
00035   typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
00036 
00037   // For each value, we remember its Value* and occurrence frequency.
00038   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
00039 private:
00040   typedef DenseMap<const Type*, unsigned> TypeMapType;
00041   TypeMapType TypeMap;
00042   TypeList Types;
00043 
00044   typedef DenseMap<const Value*, unsigned> ValueMapType;
00045   ValueMapType ValueMap;
00046   ValueList Values;
00047   
00048   typedef DenseMap<void*, unsigned> AttributeMapType;
00049   AttributeMapType AttributeMap;
00050   std::vector<AttrListPtr> Attributes;
00051   
00052   /// BasicBlocks - This contains all the basic blocks for the currently
00053   /// incorporated function.  Their reverse mapping is stored in ValueMap.
00054   std::vector<const BasicBlock*> BasicBlocks;
00055   
00056   /// When a function is incorporated, this is the size of the Values list
00057   /// before incorporation.
00058   unsigned NumModuleValues;
00059   unsigned FirstFuncConstantID;
00060   unsigned FirstInstID;
00061   
00062   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
00063   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
00064 public:
00065   ValueEnumerator(const Module *M);
00066 
00067   unsigned getValueID(const Value *V) const {
00068     ValueMapType::const_iterator I = ValueMap.find(V);
00069     assert(I != ValueMap.end() && "Value not in slotcalculator!");
00070     return I->second-1;
00071   }
00072   
00073   unsigned getTypeID(const Type *T) const {
00074     TypeMapType::const_iterator I = TypeMap.find(T);
00075     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
00076     return I->second-1;
00077   }
00078   
00079   unsigned getAttributeID(const AttrListPtr &PAL) const {
00080     if (PAL.isEmpty()) return 0;  // Null maps to zero.
00081     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
00082     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
00083     return I->second;
00084   }
00085 
00086   /// getFunctionConstantRange - Return the range of values that corresponds to
00087   /// function-local constants.
00088   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
00089     Start = FirstFuncConstantID;
00090     End = FirstInstID;
00091   }
00092   
00093   const ValueList &getValues() const { return Values; }
00094   const TypeList &getTypes() const { return Types; }
00095   const std::vector<const BasicBlock*> &getBasicBlocks() const {
00096     return BasicBlocks; 
00097   }
00098   const std::vector<AttrListPtr> &getAttributes() const {
00099     return Attributes;
00100   }
00101 
00102   /// PurgeAggregateValues - If there are any aggregate values at the end of the
00103   /// value list, remove them and return the count of the remaining values.  If
00104   /// there are none, return -1.
00105   int PurgeAggregateValues();
00106   
00107   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
00108   /// use these two methods to get its data into the ValueEnumerator!
00109   ///
00110   void incorporateFunction(const Function &F);
00111   void purgeFunction();
00112 
00113 private:
00114   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
00115     
00116   void EnumerateValue(const Value *V);
00117   void EnumerateType(const Type *T);
00118   void EnumerateOperandType(const Value *V);
00119   void EnumerateAttributes(const AttrListPtr &PAL);
00120   
00121   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
00122   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
00123 };
00124 
00125 } // End llvm namespace
00126 
00127 #endif