blob: 30e0bb21b6bf0cf90d1486fed157c663532c8ca5 [file] [log] [blame]
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00001//===- FunctionRepBuilder.h - Structures for graph building ------*- C++ -*--=//
2//
3// This file defines the FunctionRepBuilder and InitVisitor classes that are
4// used to build the local data structure graph for a method.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef DATA_STRUCTURE_METHOD_REP_BUILDER_H
9#define DATA_STRUCTURE_METHOD_REP_BUILDER_H
10
11#include "llvm/Analysis/DataStructure.h"
12#include "llvm/Support/InstVisitor.h"
Anand Shuklaa9284032002-06-25 20:35:19 +000013#include <iostream>
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000014
15// DEBUG_DATA_STRUCTURE_CONSTRUCTION - Define this to 1 if you want debug output
Chris Lattnerfe145682002-04-17 03:24:59 +000016//#define DEBUG_DATA_STRUCTURE_CONSTRUCTION 1
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000017
18class FunctionRepBuilder;
19
20// InitVisitor - Used to initialize the worklists for data structure analysis.
21// Iterate over the instructions in the method, creating nodes for malloc and
22// call instructions. Add all uses of these to the worklist of instructions
23// to process.
24//
25class InitVisitor : public InstVisitor<InitVisitor> {
26 FunctionRepBuilder *Rep;
27 Function *Func;
28public:
29 InitVisitor(FunctionRepBuilder *R, Function *F) : Rep(R), Func(F) {}
30
Chris Lattner18961502002-06-25 16:12:52 +000031 void visitCallInst(CallInst &CI);
32 void visitAllocationInst(AllocationInst &AI);
33 void visitInstruction(Instruction &I);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000034
35 // visitOperand - If the specified instruction operand is a global value, add
36 // a node for it...
37 //
38 void visitOperand(Value *V);
39};
40
41
42// FunctionRepBuilder - This builder object creates the datastructure graph for
43// a method.
44//
45class FunctionRepBuilder : InstVisitor<FunctionRepBuilder> {
46 friend class InitVisitor;
47 FunctionDSGraph *F;
48 PointerValSet RetNode;
49
50 // ValueMap - Mapping between values we are processing and the possible
51 // datastructures that they may point to...
Anand Shuklaa9284032002-06-25 20:35:19 +000052 std::map<Value*, PointerValSet> ValueMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000053
54 // CallMap - Keep track of which call nodes correspond to which call insns.
55 // The reverse mapping is stored in the CallDSNodes themselves.
56 //
Anand Shuklaa9284032002-06-25 20:35:19 +000057 std::map<CallInst*, CallDSNode*> CallMap;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000058
59 // Worklist - Vector of (pointer typed) instructions to process still...
60 std::vector<Instruction *> WorkList;
61
62 // Nodes - Keep track of all of the resultant nodes, because there may not
63 // be edges connecting these to anything.
64 //
Chris Lattner1120c8b2002-03-28 17:56:03 +000065 std::vector<AllocDSNode*> AllocNodes;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000066 std::vector<ShadowDSNode*> ShadowNodes;
Chris Lattner1120c8b2002-03-28 17:56:03 +000067 std::vector<GlobalDSNode*> GlobalNodes;
68 std::vector<CallDSNode*> CallNodes;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000069
70 // addAllUsesToWorkList - Add all of the instructions users of the specified
71 // value to the work list for further processing...
72 //
73 void addAllUsesToWorkList(Value *V);
74
75public:
76 FunctionRepBuilder(FunctionDSGraph *f) : F(f) {
77 initializeWorkList(F->getFunction());
78 processWorkList();
79 }
80
Chris Lattner1120c8b2002-03-28 17:56:03 +000081 const std::vector<AllocDSNode*> &getAllocNodes() const { return AllocNodes; }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000082 const std::vector<ShadowDSNode*> &getShadowNodes() const {return ShadowNodes;}
Chris Lattner1120c8b2002-03-28 17:56:03 +000083 const std::vector<GlobalDSNode*> &getGlobalNodes() const {return GlobalNodes;}
84 const std::vector<CallDSNode*> &getCallNodes() const { return CallNodes; }
85
Chris Lattnerfe145682002-04-17 03:24:59 +000086
87 ShadowDSNode *makeSynthesizedShadow(const Type *Ty, DSNode *Parent);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000088
89 const PointerValSet &getRetNode() const { return RetNode; }
90
Anand Shuklaa9284032002-06-25 20:35:19 +000091 const std::map<Value*, PointerValSet> &getValueMap() const { return ValueMap; }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000092private:
93 static PointerVal getIndexedPointerDest(const PointerVal &InP,
Chris Lattner18961502002-06-25 16:12:52 +000094 const MemAccessInst &MAI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000095
96 void initializeWorkList(Function *Func);
97 void processWorkList() {
98 // While the worklist still has instructions to process, process them!
99 while (!WorkList.empty()) {
100 Instruction *I = WorkList.back(); WorkList.pop_back();
Anand Shuklaa9284032002-06-25 20:35:19 +0000101
Chris Lattnerfe145682002-04-17 03:24:59 +0000102#ifdef DEBUG_DATA_STRUCTURE_CONSTRUCTION
Anand Shuklaa9284032002-06-25 20:35:19 +0000103 std::cerr << "Processing worklist inst: " << I;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000104#endif
105
Chris Lattner18961502002-06-25 16:12:52 +0000106 visit(*I); // Dispatch to a visitXXX function based on instruction type...
Chris Lattnerfe145682002-04-17 03:24:59 +0000107#ifdef DEBUG_DATA_STRUCTURE_CONSTRUCTION
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000108 if (I->hasName() && ValueMap.count(I)) {
Anand Shuklaa9284032002-06-25 20:35:19 +0000109 std::cerr << "Inst %" << I->getName() << " value is:\n";
110 ValueMap[I].print(std::cerr);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000111 }
112#endif
113 }
114 }
115
116 //===--------------------------------------------------------------------===//
117 // Functions used to process the worklist of instructions...
118 //
119 // Allow the visitor base class to invoke these methods...
120 friend class InstVisitor<FunctionRepBuilder>;
121
Chris Lattner18961502002-06-25 16:12:52 +0000122 void visitGetElementPtrInst(GetElementPtrInst &GEP);
123 void visitReturnInst(ReturnInst &RI);
124 void visitLoadInst(LoadInst &LI);
125 void visitStoreInst(StoreInst &SI);
126 void visitCallInst(CallInst &CI);
127 void visitPHINode(PHINode &PN);
128 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
129 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
130 void visitInstruction(Instruction &I) {
131 std::cerr << "\n\n\nUNKNOWN INSTRUCTION type: " << I << "\n\n\n";
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000132 assert(0 && "Cannot proceed");
133 }
134};
135
136#endif