blob: c371a0e5ac26363a419290dc0ba9aca3ab24cd26 [file] [log] [blame]
Chris Lattnerdb9b9982004-01-20 19:50:12 +00001//===-- Analysis/SlotCalculator.h - Calculate value slots -------*- C++ -*-===//
John Criswell6fbcc262003-10-20 20:19:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnerb5794002002-04-07 22:49:37 +000010// This class calculates the slots that values will land in. This is useful for
11// when writing bytecode or assembly out, because you have to know these things.
12//
13// Specifically, this class calculates the "type plane numbering" that you see
14// for a function if you strip out all of the symbols in it. For assembly
15// writing, this is used when a symbol does not have a name. For bytecode
16// writing, this is always used, and the symbol table is added on later.
Chris Lattner00950542001-06-06 20:29:01 +000017//
18//===----------------------------------------------------------------------===//
19
Chris Lattnerdb9b9982004-01-20 19:50:12 +000020#ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
21#define LLVM_ANALYSIS_SLOTCALCULATOR_H
Chris Lattner00950542001-06-06 20:29:01 +000022
Chris Lattner00950542001-06-06 20:29:01 +000023#include <vector>
24#include <map>
Brian Gaeked0fde302003-11-11 22:41:34 +000025
26namespace llvm {
27
Chris Lattner644dc172001-07-14 06:08:51 +000028class Value;
Chris Lattner4c4007b2001-09-07 16:27:05 +000029class Module;
Chris Lattnere7506a32002-03-23 22:51:58 +000030class Function;
Chris Lattner184b2fa2002-04-09 18:35:38 +000031class SymbolTable;
Chris Lattner7851e1b2004-01-14 23:37:43 +000032class ConstantArray;
Chris Lattner00950542001-06-06 20:29:01 +000033
Chris Lattner4c4007b2001-09-07 16:27:05 +000034class SlotCalculator {
Chris Lattner00950542001-06-06 20:29:01 +000035 const Module *TheModule;
Chris Lattner8ce75012004-01-14 02:49:34 +000036
Chris Lattner7851e1b2004-01-14 23:37:43 +000037 /// BuildBytecodeInfo - If true, this is the creating information for the
38 /// bytecode writer, if false, we are building information for the assembly
39 /// emitter. The assembly emitter doesn't need named objects numbered, among
40 /// other differences.
Chris Lattner8ce75012004-01-14 02:49:34 +000041 bool BuildBytecodeInfo;
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattner697954c2002-01-20 22:54:45 +000043 typedef std::vector<const Value*> TypePlane;
44 std::vector<TypePlane> Table;
Chris Lattner7851e1b2004-01-14 23:37:43 +000045 std::map<const Value*, unsigned> NodeMap;
Chris Lattner00950542001-06-06 20:29:01 +000046
Chris Lattner7851e1b2004-01-14 23:37:43 +000047 /// ConstantStrings - If we are indexing for a bytecode file, this keeps track
48 /// of all of the constants strings that need to be emitted.
49 std::vector<const ConstantArray*> ConstantStrings;
50
51 /// ModuleLevel - Used to keep track of which values belong to the module,
52 /// and which values belong to the currently incorporated function.
53 ///
Chris Lattner697954c2002-01-20 22:54:45 +000054 std::vector<unsigned> ModuleLevel;
Chris Lattner00950542001-06-06 20:29:01 +000055
Chris Lattneraf894e92004-01-18 21:03:49 +000056 /// ModuleContainsAllFunctionConstants - This flag is set to true if all
57 /// function constants are incorporated into the module constant table. This
58 /// is only possible if building information for a bytecode file.
59 bool ModuleContainsAllFunctionConstants;
60
61 /// CompactionTable/NodeMap - When function compaction has been performed,
62 /// these entries provide a compacted view of the namespace needed to emit
63 /// instructions in a function body. The 'getSlot()' method automatically
64 /// returns these entries if applicable, or the global entries if not.
65 std::vector<TypePlane> CompactionTable;
66 std::map<const Value*, unsigned> CompactionNodeMap;
67
68 SlotCalculator(const SlotCalculator &); // DO NOT IMPLEMENT
69 void operator=(const SlotCalculator &); // DO NOT IMPLEMENT
Chris Lattner00950542001-06-06 20:29:01 +000070public:
Chris Lattner8ce75012004-01-14 02:49:34 +000071 SlotCalculator(const Module *M, bool BuildBytecodeInfo);
Chris Lattnere7506a32002-03-23 22:51:58 +000072 // Start out in incorp state
Chris Lattner7851e1b2004-01-14 23:37:43 +000073 SlotCalculator(const Function *F, bool BuildBytecodeInfo);
Chris Lattner00950542001-06-06 20:29:01 +000074
Chris Lattneraf894e92004-01-18 21:03:49 +000075 /// getSlot - Return the slot number of the specified value in it's type
76 /// plane. This returns < 0 on error!
Chris Lattner7851e1b2004-01-14 23:37:43 +000077 ///
Chris Lattneraf894e92004-01-18 21:03:49 +000078 int getSlot(const Value *V) const;
Chris Lattner00950542001-06-06 20:29:01 +000079
Chris Lattneraf894e92004-01-18 21:03:49 +000080 /// getGlobalSlot - Return a slot number from the global table. This can only
81 /// be used when a compaction table is active.
82 unsigned getGlobalSlot(const Value *V) const;
83
84 inline unsigned getNumPlanes() const {
85 if (CompactionTable.empty())
86 return Table.size();
87 else
88 return CompactionTable.size();
89 }
Chris Lattner00950542001-06-06 20:29:01 +000090 inline unsigned getModuleLevel(unsigned Plane) const {
91 return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
92 }
93
Chris Lattnera2b4f932004-01-20 00:54:47 +000094 TypePlane &getPlane(unsigned Plane);
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattner7851e1b2004-01-14 23:37:43 +000096 /// incorporateFunction/purgeFunction - If you'd like to deal with a function,
97 /// use these two methods to get its data into the SlotCalculator!
98 ///
Chris Lattnerb5794002002-04-07 22:49:37 +000099 void incorporateFunction(const Function *F);
100 void purgeFunction();
Chris Lattner00950542001-06-06 20:29:01 +0000101
Chris Lattner7851e1b2004-01-14 23:37:43 +0000102 /// string_iterator/string_begin/end - Access the list of module-level
103 /// constant strings that have been incorporated. This is only applicable to
104 /// bytecode files.
105 typedef std::vector<const ConstantArray*>::const_iterator string_iterator;
106 string_iterator string_begin() const { return ConstantStrings.begin(); }
107 string_iterator string_end() const { return ConstantStrings.end(); }
108
Chris Lattneraf894e92004-01-18 21:03:49 +0000109 const std::vector<TypePlane> &getCompactionTable() const {
110 return CompactionTable;
111 }
Chris Lattner7851e1b2004-01-14 23:37:43 +0000112
Chris Lattneraf894e92004-01-18 21:03:49 +0000113private:
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000114 // getOrCreateSlot - Values can be crammed into here at will... if
115 // they haven't been inserted already, they get inserted, otherwise
116 // they are ignored.
Chris Lattner00950542001-06-06 20:29:01 +0000117 //
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000118 int getOrCreateSlot(const Value *D);
Chris Lattner00950542001-06-06 20:29:01 +0000119
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000120 // insertValue - Insert a value into the value table... Return the
121 // slot that it occupies, or -1 if the declaration is to be ignored
122 // because of the IgnoreNamedNodes flag.
Chris Lattner00950542001-06-06 20:29:01 +0000123 //
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000124 int insertValue(const Value *D, bool dontIgnore = false);
Chris Lattner00950542001-06-06 20:29:01 +0000125
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000126 // doInsertValue - Small helper function to be called only be insertVal.
127 int doInsertValue(const Value *D);
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattnerb5794002002-04-07 22:49:37 +0000129 // processModule - Process all of the module level function declarations and
Chris Lattner4c4007b2001-09-07 16:27:05 +0000130 // types that are available.
Chris Lattner00950542001-06-06 20:29:01 +0000131 //
Chris Lattner4c4007b2001-09-07 16:27:05 +0000132 void processModule();
Chris Lattner00950542001-06-06 20:29:01 +0000133
Chris Lattner4c4007b2001-09-07 16:27:05 +0000134 // processSymbolTable - Insert all of the values in the specified symbol table
135 // into the values table...
Chris Lattner00950542001-06-06 20:29:01 +0000136 //
Chris Lattner4c4007b2001-09-07 16:27:05 +0000137 void processSymbolTable(const SymbolTable *ST);
138 void processSymbolTableConstants(const SymbolTable *ST);
Chris Lattneraf894e92004-01-18 21:03:49 +0000139
140 void buildCompactionTable(const Function *F);
141 unsigned getOrCreateCompactionTableSlot(const Value *V);
Chris Lattnera2b4f932004-01-20 00:54:47 +0000142 void pruneCompactionTable();
Chris Lattner00950542001-06-06 20:29:01 +0000143};
144
Brian Gaeked0fde302003-11-11 22:41:34 +0000145} // End llvm namespace
146
Chris Lattner00950542001-06-06 20:29:01 +0000147#endif