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 LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
00015 #define LLVM_LIB_BITCODE_WRITER_VALUEENUMERATOR_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallVector.h"
00019 #include "llvm/ADT/UniqueVector.h"
00020 #include "llvm/IR/Attributes.h"
00021 #include "llvm/IR/UseListOrder.h"
00022 #include <vector>
00023 
00024 namespace llvm {
00025 
00026 class Type;
00027 class Value;
00028 class Instruction;
00029 class BasicBlock;
00030 class Comdat;
00031 class Function;
00032 class Module;
00033 class MDNode;
00034 class NamedMDNode;
00035 class AttributeSet;
00036 class ValueSymbolTable;
00037 class MDSymbolTable;
00038 class raw_ostream;
00039 
00040 class ValueEnumerator {
00041 public:
00042   typedef std::vector<Type*> TypeList;
00043 
00044   // For each value, we remember its Value* and occurrence frequency.
00045   typedef std::vector<std::pair<const Value*, unsigned> > ValueList;
00046 
00047   UseListOrderStack UseListOrders;
00048 
00049 private:
00050   typedef DenseMap<Type*, unsigned> TypeMapType;
00051   TypeMapType TypeMap;
00052   TypeList Types;
00053 
00054   typedef DenseMap<const Value*, unsigned> ValueMapType;
00055   ValueMapType ValueMap;
00056   ValueList Values;
00057 
00058   typedef UniqueVector<const Comdat *> ComdatSetType;
00059   ComdatSetType Comdats;
00060 
00061   ValueList MDValues;
00062   SmallVector<const MDNode *, 8> FunctionLocalMDs;
00063   ValueMapType MDValueMap;
00064 
00065   typedef DenseMap<AttributeSet, unsigned> AttributeGroupMapType;
00066   AttributeGroupMapType AttributeGroupMap;
00067   std::vector<AttributeSet> AttributeGroups;
00068 
00069   typedef DenseMap<AttributeSet, unsigned> AttributeMapType;
00070   AttributeMapType AttributeMap;
00071   std::vector<AttributeSet> Attribute;
00072 
00073   /// GlobalBasicBlockIDs - This map memoizes the basic block ID's referenced by
00074   /// the "getGlobalBasicBlockID" method.
00075   mutable DenseMap<const BasicBlock*, unsigned> GlobalBasicBlockIDs;
00076 
00077   typedef DenseMap<const Instruction*, unsigned> InstructionMapType;
00078   InstructionMapType InstructionMap;
00079   unsigned InstructionCount;
00080 
00081   /// BasicBlocks - This contains all the basic blocks for the currently
00082   /// incorporated function.  Their reverse mapping is stored in ValueMap.
00083   std::vector<const BasicBlock*> BasicBlocks;
00084 
00085   /// When a function is incorporated, this is the size of the Values list
00086   /// before incorporation.
00087   unsigned NumModuleValues;
00088 
00089   /// When a function is incorporated, this is the size of the MDValues list
00090   /// before incorporation.
00091   unsigned NumModuleMDValues;
00092 
00093   unsigned FirstFuncConstantID;
00094   unsigned FirstInstID;
00095 
00096   ValueEnumerator(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
00097   void operator=(const ValueEnumerator &) LLVM_DELETED_FUNCTION;
00098 public:
00099   ValueEnumerator(const Module *M);
00100 
00101   void dump() const;
00102   void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const;
00103 
00104   unsigned getValueID(const Value *V) const;
00105 
00106   unsigned getTypeID(Type *T) const {
00107     TypeMapType::const_iterator I = TypeMap.find(T);
00108     assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
00109     return I->second-1;
00110   }
00111 
00112   unsigned getInstructionID(const Instruction *I) const;
00113   void setInstructionID(const Instruction *I);
00114 
00115   unsigned getAttributeID(AttributeSet PAL) const {
00116     if (PAL.isEmpty()) return 0;  // Null maps to zero.
00117     AttributeMapType::const_iterator I = AttributeMap.find(PAL);
00118     assert(I != AttributeMap.end() && "Attribute not in ValueEnumerator!");
00119     return I->second;
00120   }
00121 
00122   unsigned getAttributeGroupID(AttributeSet PAL) const {
00123     if (PAL.isEmpty()) return 0;  // Null maps to zero.
00124     AttributeGroupMapType::const_iterator I = AttributeGroupMap.find(PAL);
00125     assert(I != AttributeGroupMap.end() && "Attribute not in ValueEnumerator!");
00126     return I->second;
00127   }
00128 
00129   /// getFunctionConstantRange - Return the range of values that corresponds to
00130   /// function-local constants.
00131   void getFunctionConstantRange(unsigned &Start, unsigned &End) const {
00132     Start = FirstFuncConstantID;
00133     End = FirstInstID;
00134   }
00135 
00136   const ValueList &getValues() const { return Values; }
00137   const ValueList &getMDValues() const { return MDValues; }
00138   const SmallVectorImpl<const MDNode *> &getFunctionLocalMDValues() const {
00139     return FunctionLocalMDs;
00140   }
00141   const TypeList &getTypes() const { return Types; }
00142   const std::vector<const BasicBlock*> &getBasicBlocks() const {
00143     return BasicBlocks;
00144   }
00145   const std::vector<AttributeSet> &getAttributes() const {
00146     return Attribute;
00147   }
00148   const std::vector<AttributeSet> &getAttributeGroups() const {
00149     return AttributeGroups;
00150   }
00151 
00152   const ComdatSetType &getComdats() const { return Comdats; }
00153   unsigned getComdatID(const Comdat *C) const;
00154 
00155   /// getGlobalBasicBlockID - This returns the function-specific ID for the
00156   /// specified basic block.  This is relatively expensive information, so it
00157   /// should only be used by rare constructs such as address-of-label.
00158   unsigned getGlobalBasicBlockID(const BasicBlock *BB) const;
00159 
00160   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
00161   /// use these two methods to get its data into the ValueEnumerator!
00162   ///
00163   void incorporateFunction(const Function &F);
00164   void purgeFunction();
00165 
00166 private:
00167   void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
00168 
00169   void EnumerateMDNodeOperands(const MDNode *N);
00170   void EnumerateMetadata(const Value *MD);
00171   void EnumerateFunctionLocalMetadata(const MDNode *N);
00172   void EnumerateNamedMDNode(const NamedMDNode *NMD);
00173   void EnumerateValue(const Value *V);
00174   void EnumerateType(Type *T);
00175   void EnumerateOperandType(const Value *V);
00176   void EnumerateAttributes(AttributeSet PAL);
00177 
00178   void EnumerateValueSymbolTable(const ValueSymbolTable &ST);
00179   void EnumerateNamedMetadata(const Module *M);
00180 };
00181 
00182 } // End llvm namespace
00183 
00184 #endif