1 //===-- SlotCalculator.cpp - Calculate what slots values land in ------------=//
3 // This file implements a useful analysis step to figure out what numbered
4 // slots values in a program will land in (keeping track of per plane
5 // information as required.
7 // This is used primarily for when writing a file to disk, either in bytecode
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Analysis/SlotCalculator.h"
13 #include "llvm/ConstantPool.h"
14 #include "llvm/Method.h"
15 #include "llvm/Module.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/ConstPoolVals.h"
18 #include "llvm/iOther.h"
19 #include "llvm/DerivedTypes.h"
21 SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
22 IgnoreNamedNodes = IgnoreNamed;
25 // Preload table... Make sure that all of the primitive types are in the table
26 // and that their Primitive ID is equal to their slot #
28 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
29 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
30 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i));
33 if (M == 0) return; // Empty table...
35 bool Result = processModule(M);
36 assert(Result == false && "Error in processModule!");
39 SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) {
40 IgnoreNamedNodes = IgnoreNamed;
41 TheModule = M ? M->getParent() : 0;
43 // Preload table... Make sure that all of the primitive types are in the table
44 // and that their Primitive ID is equal to their slot #
46 for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
47 assert(Type::getPrimitiveType((Type::PrimitiveID)i));
48 insertVal(Type::getPrimitiveType((Type::PrimitiveID)i));
51 if (TheModule == 0) return; // Empty table...
53 bool Result = processModule(TheModule);
54 assert(Result == false && "Error in processModule!");
59 void SlotCalculator::incorporateMethod(const Method *M) {
60 assert(ModuleLevel.size() == 0 && "Module already incorporated!");
62 // Save the Table state before we process the method...
63 for (unsigned i = 0; i < Table.size(); ++i) {
64 ModuleLevel.push_back(Table[i].size());
67 // Process the method to incorporate its values into our table
71 void SlotCalculator::purgeMethod() {
72 assert(ModuleLevel.size() != 0 && "Module not incorporated!");
73 unsigned NumModuleTypes = ModuleLevel.size();
75 // First, remove values from existing type planes
76 for (unsigned i = 0; i < NumModuleTypes; ++i) {
77 unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came
78 while (Table[i].size() != ModuleSize) {
79 NodeMap.erase(NodeMap.find(Table[i].back())); // Erase from nodemap
80 Table[i].pop_back(); // Shrink plane
84 // We don't need this state anymore, free it up.
87 // Next, remove any type planes defined by the method...
88 while (NumModuleTypes != Table.size()) {
89 TypePlane &Plane = Table.back();
90 while (Plane.size()) {
91 NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
92 Plane.pop_back(); // Shrink plane
95 Table.pop_back(); // Nuke the plane, we don't like it.
99 bool SlotCalculator::processConstant(const ConstPoolVal *CPV) {
100 //cerr << "Inserting constant: '" << CPV->getStrValue() << endl;
105 // processType - This callback occurs when an derived type is discovered
106 // at the class level. This activity occurs when processing a constant pool.
108 bool SlotCalculator::processType(const Type *Ty) {
109 //cerr << "processType: " << Ty->getName() << endl;
110 // TODO: Don't leak memory!!! Free this in the dtor!
111 insertVal(new ConstPoolType(Ty));
115 bool SlotCalculator::visitMethod(const Method *M) {
116 //cerr << "visitMethod: '" << M->getType()->getName() << "'\n";
121 bool SlotCalculator::processMethodArgument(const MethodArgument *MA) {
126 bool SlotCalculator::processBasicBlock(const BasicBlock *BB) {
128 ModuleAnalyzer::processBasicBlock(BB); // Lets visit the instructions too!
132 bool SlotCalculator::processInstruction(const Instruction *I) {
137 int SlotCalculator::getValSlot(const Value *D) const {
138 map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
139 if (I == NodeMap.end()) return -1;
141 return (int)I->second;
144 void SlotCalculator::insertVal(const Value *D) {
147 // If this node does not contribute to a plane, or if the node has a
148 // name and we don't want names, then ignore the silly node...
150 if (D->getType() == Type::VoidTy || (IgnoreNamedNodes && D->hasName()))
153 const Type *Typ = D->getType();
154 unsigned Ty = Typ->getPrimitiveID();
155 if (Typ->isDerivedType()) {
156 int DefSlot = getValSlot(Typ);
157 if (DefSlot == -1) { // Have we already entered this type?
158 // This can happen if a type is first seen in an instruction. For
159 // example, if you say 'malloc uint', this defines a type 'uint*' that
160 // may be undefined at this point.
162 cerr << "SHOULDN'T HAPPEN Adding Type ba: " << Typ->getName() << endl;
163 assert(0 && "Shouldn't this be taken care of by processType!?!?!");
164 // Nope... add this to the Type plane now!
167 DefSlot = getValSlot(Typ);
168 assert(DefSlot >= 0 && "Type didn't get inserted correctly!");
170 Ty = (unsigned)DefSlot;
173 if (Table.size() <= Ty) // Make sure we have the type plane allocated...
174 Table.resize(Ty+1, TypePlane());
176 // Insert node into table and NodeMap...
177 NodeMap[D] = Table[Ty].size();
179 if (Typ == Type::TypeTy && !D->isType()) {
180 // If it's a type constant, add the Type also
182 // All Type instances should be constant types!
183 const ConstPoolType *CPT = (const ConstPoolType*)D->castConstantAsserting();
184 int Slot = getValSlot(CPT->getValue());
186 // Only add if it's not already here!
187 NodeMap[CPT->getValue()] = Table[Ty].size();
188 } else if (!CPT->hasName()) { // If the type has no name...
189 NodeMap[D] = (unsigned)Slot; // Don't readd type, merge.
193 Table[Ty].push_back(D);