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 MetadataBase;
00029 class AttrListPtr;
00030 class TypeSymbolTable;
00031 class ValueSymbolTable;
00032 
00033 class ValueEnumerator {
00034 public:
00035   // For each type, we remember its Type* and occurrence frequency.
00036   typedef std::vector<std::pair<const Type*, unsigned> > TypeList;
00037 
00038   // For each value, we remember its Value* and occurrence frequency.
00039   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
00040 private:
00041   typedef DenseMap<const Type*, unsigned> TypeMapType;
00042   TypeMapType TypeMap;
00043   TypeList Types;
00044 
00045   typedef DenseMap<const Value*, unsigned> ValueMapType;
00046   ValueMapType ValueMap;
00047   ValueList Values;
00048   ValueList MDValues;
00049   ValueMapType MDValueMap;
00050 
00051   typedef DenseMap<void*, unsigned> AttributeMapType;
00052   AttributeMapType AttributeMap;
00053   std::vector<AttrListPtr> Attributes;
00054   
00055   /// BasicBlocks - This contains all the basic blocks for the currently
00056   /// incorporated function.  Their reverse mapping is stored in ValueMap.
00057   std::vector<const BasicBlock*> BasicBlocks;
00058   
00059   /// When a function is incorporated, this is the size of the Values list
00060   /// before incorporation.
00061   unsigned NumModuleValues;
00062   unsigned FirstFuncConstantID;
00063   unsigned FirstInstID;
00064   
00065   ValueEnumerator(const ValueEnumerator &);  // DO NOT IMPLEMENT
00066   void operator=(const ValueEnumerator &);   // DO NOT IMPLEMENT
00067 public:
00068   ValueEnumerator(const Module *M);
00069 
00070   unsigned getValueID(const Value *V) const;
00071 
00072   unsigned getTypeID(const Type *T) const {
00073     TypeMapType::const_iterator I = TypeMap.find(T);
00074     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
00075     return I->second-1;
00076   }
00077   
00078   unsigned getAttributeID(const AttrListPtr &PAL) const {
00079     if (PAL.isEmpty()) return 0;  // Null maps to zero.
00080     AttributeMapType::const_iterator I = AttributeMap.find(PAL.getRawPointer());
00081     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
00082     return I->second;
00083   }
00084 
00085   /// getFunctionConstantRange - Return the range of values that corresponds to
00086   /// function-local constants.
00087   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
00088     Start = FirstFuncConstantID;
00089     End = FirstInstID;
00090   }
00091   
00092   const ValueList &getValues() const { return Values; }
00093   const ValueList &getMDValues() const { return MDValues; }
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   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
00103   /// use these two methods to get its data into the ValueEnumerator!
00104   ///
00105   void incorporateFunction(const Function &F);
00106   void purgeFunction();
00107 
00108 private:
00109   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
00110     
00111   void EnumerateMetadata(const MetadataBase *MD);
00112   void EnumerateValue(const Value *V);
00113   void EnumerateType(const Type *T);
00114   void EnumerateOperandType(const Value *V);
00115   void EnumerateAttributes(const AttrListPtr &PAL);
00116   
00117   void EnumerateTypeSymbolTable(const TypeSymbolTable &ST);
00118   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
00119 };
00120 
00121 } // End llvm namespace
00122 
00123 #endif