LLVM API Documentation

SlotCalculator.h

Go to the documentation of this file.
00001 //===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class calculates the slots that values will land in.  This is useful for
00011 // when writing bytecode or assembly out, because you have to know these things.
00012 //
00013 // Specifically, this class calculates the "type plane numbering" that you see
00014 // for a function if you strip out all of the symbols in it.  For assembly
00015 // writing, this is used when a symbol does not have a name.  For bytecode
00016 // writing, this is always used, and the symbol table is added on later.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
00021 #define LLVM_ANALYSIS_SLOTCALCULATOR_H
00022 
00023 #include <vector>
00024 #include <map>
00025 
00026 namespace llvm {
00027 
00028 class Value;
00029 class Type;
00030 class Module;
00031 class Function;
00032 class SymbolTable;
00033 class ConstantArray;
00034 
00035 class SlotCalculator {
00036   const Module *TheModule;
00037 
00038   typedef std::vector<const Type*> TypeList;
00039   typedef std::vector<const Value*> TypePlane;
00040   std::vector<TypePlane> Table;
00041   TypeList Types;
00042   typedef std::map<const Value*, unsigned> NodeMapType;
00043   NodeMapType NodeMap;
00044 
00045   typedef std::map<const Type*, unsigned> TypeMapType;
00046   TypeMapType TypeMap;
00047 
00048   /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
00049   /// of all of the constants strings that need to be emitted.
00050   std::vector<const ConstantArray*> ConstantStrings;
00051 
00052   /// ModuleLevel - Used to keep track of which values belong to the module,
00053   /// and which values belong to the currently incorporated function.
00054   ///
00055   std::vector<unsigned> ModuleLevel;
00056   unsigned ModuleTypeLevel;
00057 
00058   /// ModuleContainsAllFunctionConstants - This flag is set to true if all
00059   /// function constants are incorporated into the module constant table.  This
00060   /// is only possible if building information for a bytecode file.
00061   bool ModuleContainsAllFunctionConstants;
00062 
00063   /// CompactionTable/NodeMap - When function compaction has been performed,
00064   /// these entries provide a compacted view of the namespace needed to emit
00065   /// instructions in a function body.  The 'getSlot()' method automatically
00066   /// returns these entries if applicable, or the global entries if not.
00067   std::vector<TypePlane> CompactionTable;
00068   TypeList CompactionTypes;
00069   typedef std::map<const Value*, unsigned> CompactionNodeMapType;
00070   CompactionNodeMapType CompactionNodeMap;
00071   typedef std::map<const Type*, unsigned> CompactionTypeMapType;
00072   CompactionTypeMapType CompactionTypeMap;
00073 
00074   SlotCalculator(const SlotCalculator &);  // DO NOT IMPLEMENT
00075   void operator=(const SlotCalculator &);  // DO NOT IMPLEMENT
00076 public:
00077   SlotCalculator(const Module *M);
00078   // Start out in incorp state
00079   SlotCalculator(const Function *F);
00080 
00081   /// getSlot - Return the slot number of the specified value in it's type
00082   /// plane.  This returns < 0 on error!
00083   ///
00084   int getSlot(const Value *V) const;
00085   int getSlot(const Type* T) const;
00086 
00087   /// getGlobalSlot - Return a slot number from the global table.  This can only
00088   /// be used when a compaction table is active.
00089   unsigned getGlobalSlot(const Value *V) const;
00090   unsigned getGlobalSlot(const Type *V) const;
00091 
00092   inline unsigned getNumPlanes() const {
00093     if (CompactionTable.empty())
00094       return Table.size();
00095     else
00096       return CompactionTable.size();
00097   }
00098 
00099   inline unsigned getNumTypes() const {
00100     if (CompactionTypes.empty())
00101       return Types.size();
00102     else
00103       return CompactionTypes.size();
00104   }
00105 
00106   inline unsigned getModuleLevel(unsigned Plane) const {
00107     return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
00108   }
00109 
00110   /// Returns the number of types in the type list that are at module level
00111   inline unsigned getModuleTypeLevel() const {
00112     return ModuleTypeLevel;
00113   }
00114 
00115   TypePlane &getPlane(unsigned Plane);
00116   TypeList& getTypes() {
00117     if (!CompactionTypes.empty())
00118       return CompactionTypes;
00119     return Types;
00120   }
00121 
00122   /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
00123   /// use these two methods to get its data into the SlotCalculator!
00124   ///
00125   void incorporateFunction(const Function *F);
00126   void purgeFunction();
00127 
00128   /// string_iterator/string_begin/end - Access the list of module-level
00129   /// constant strings that have been incorporated.  This is only applicable to
00130   /// bytecode files.
00131   typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
00132   string_iterator string_begin() const { return ConstantStrings.begin(); }
00133   string_iterator string_end() const   { return ConstantStrings.end(); }
00134 
00135   const std::vector<TypePlane> &getCompactionTable() const {
00136     return CompactionTable;
00137   }
00138 
00139   const TypeList& getCompactionTypes() const { return CompactionTypes; }
00140 
00141   /// @brief Determine if the compaction table (not types) is empty
00142   bool CompactionTableIsEmpty() const;
00143 
00144 private:
00145   // getOrCreateSlot - Values can be crammed into here at will... if
00146   // they haven't been inserted already, they get inserted, otherwise
00147   // they are ignored.
00148   //
00149   int getOrCreateSlot(const Value *V);
00150   int getOrCreateSlot(const Type *T);
00151 
00152   // insertValue - Insert a value into the value table... Return the
00153   // slot that it occupies, or -1 if the declaration is to be ignored
00154   // because of the IgnoreNamedNodes flag.
00155   //
00156   int insertValue(const Value *D, bool dontIgnore = false);
00157   int insertType(const Type *T, bool dontIgnore = false);
00158 
00159   // doInsertValue - Small helper function to be called only be insertVal.
00160   int doInsertValue(const Value *V);
00161   int doInsertType(const Type *T);
00162 
00163   // processModule - Process all of the module level function declarations and
00164   // types that are available.
00165   //
00166   void processModule();
00167 
00168   // processSymbolTable - Insert all of the values in the specified symbol table
00169   // into the values table...
00170   //
00171   void processSymbolTable(const SymbolTable *ST);
00172   void processSymbolTableConstants(const SymbolTable *ST);
00173 
00174   void buildCompactionTable(const Function *F);
00175   unsigned getOrCreateCompactionTableSlot(const Value *V);
00176   unsigned getOrCreateCompactionTableSlot(const Type *V);
00177   void pruneCompactionTable();
00178 };
00179 
00180 } // End llvm namespace
00181 
00182 #endif