1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// $Id$ -*-c++-*-
//***************************************************************************
// File:
// InstrForest.h
//
// Purpose:
// Convert SSA graph to instruction trees for instruction selection.
//
// Strategy:
// The basic idea is that we would like to group instructions into a single
// tree if one or more of them might be potentially combined into a single
// complex instruction in the target machine.
// Since this grouping is completely machine-independent, it is as
// aggressive as possible. In particular, we group two instructions
// O and I if:
// (1) Instruction O computes an operand of instruction I, and
// (2) O and I are part of the same basic block, and
// (3) O has only a single use, viz., I.
//
// History:
// 6/28/01 - Vikram Adve - Created
//**************************************************************************/
#ifndef LLVM_CODEGEN_INSTRFOREST_H
#define LLVM_CODEGEN_INSTRFOREST_H
#include "llvm/Instruction.h"
#include "Support/NonCopyable.h"
#include "Support/HashExtras.h"
#include <ext/hash_set>
class Constant;
class BasicBlock;
class Function;
class InstrTreeNode;
class InstrForest;
//--------------------------------------------------------------------------
// OpLabel values for special-case nodes created for instruction selection.
// All op-labels not defined here are identical to the instruction
// opcode returned by Instruction::getInstType()
//--------------------------------------------------------------------------
const int InvalidOp = -1;
const int VRegListOp = 97;
const int VRegNodeOp = 98;
const int ConstantNodeOp= 99;
const int LabelNodeOp = 100;
const int RetValueOp = 100 + Instruction::Ret;
const int BrCondOp = 100 + Instruction::Br;
const int BAndOp = 100 + Instruction::And;
const int BOrOp = 100 + Instruction::Or;
const int BXorOp = 100 + Instruction::Xor;
const int BNotOp = 100 + Instruction::Not;
const int SetCCOp = 100 + Instruction::SetEQ;
const int AllocaN = 100 + Instruction::Alloca; // 121
const int LoadIdx = 100 + Instruction::Load; // 122
const int GetElemPtrIdx= 100 + Instruction::GetElementPtr; // 124
const int ToBoolTy = 100 + Instruction::Cast; // 126
const int ToUByteTy = ToBoolTy + 1;
const int ToSByteTy = ToBoolTy + 2;
const int ToUShortTy = ToBoolTy + 3;
const int ToShortTy = ToBoolTy + 4;
const int ToUIntTy = ToBoolTy + 5;
const int ToIntTy = ToBoolTy + 6;
const int ToULongTy = ToBoolTy + 7;
const int ToLongTy = ToBoolTy + 8;
const int ToFloatTy = ToBoolTy + 9;
const int ToDoubleTy = ToBoolTy + 10;
const int ToArrayTy = ToBoolTy + 11;
const int ToPointerTy = ToBoolTy + 12;
//-------------------------------------------------------------------------
// Data types needed by BURG and implemented by us
//-------------------------------------------------------------------------
typedef int OpLabel;
typedef int StateLabel;
//-------------------------------------------------------------------------
// Declarations of data and functions created by BURG
//-------------------------------------------------------------------------
extern short* burm_nts[];
extern StateLabel burm_label (InstrTreeNode* p);
extern StateLabel burm_state (OpLabel op, StateLabel leftState,
StateLabel rightState);
extern StateLabel burm_rule (StateLabel state, int goalNT);
extern InstrTreeNode** burm_kids (InstrTreeNode* p, int eruleno,
InstrTreeNode* kids[]);
extern void printcover (InstrTreeNode*, int, int);
extern void printtree (InstrTreeNode*);
extern int treecost (InstrTreeNode*, int, int);
extern void printMatches (InstrTreeNode*);
//------------------------------------------------------------------------
// class InstrTreeNode
//
// A single tree node in the instruction tree used for
// instruction selection via BURG.
//------------------------------------------------------------------------
class InstrTreeNode : public NonCopyableV {
public:
enum InstrTreeNodeType { NTInstructionNode,
NTVRegListNode,
NTVRegNode,
NTConstNode,
NTLabelNode };
// BASIC TREE NODE START
InstrTreeNode* LeftChild;
InstrTreeNode* RightChild;
InstrTreeNode* Parent;
OpLabel opLabel;
StateLabel state;
// BASIC TREE NODE END
protected:
InstrTreeNodeType treeNodeType;
Value* val;
public:
InstrTreeNode(InstrTreeNodeType nodeType, Value* _val)
: treeNodeType(nodeType), val(_val) {
LeftChild = RightChild = Parent = 0;
opLabel = InvalidOp;
}
virtual ~InstrTreeNode() {}
InstrTreeNodeType getNodeType () const { return treeNodeType; }
Value* getValue () const { return val; }
inline OpLabel getOpLabel () const { return opLabel; }
inline InstrTreeNode* leftChild() const {
return LeftChild;
}
// If right child is a list node, recursively get its *left* child
inline InstrTreeNode* rightChild() const {
return (!RightChild ? 0 :
(RightChild->getOpLabel() == VRegListOp
? RightChild->LeftChild : RightChild));
}
inline InstrTreeNode *parent() const {
return Parent;
}
void dump(int dumpChildren, int indent) const;
protected:
virtual void dumpNode(int indent) const = 0;
friend class InstrForest;
};
class InstructionNode : public InstrTreeNode {
public:
InstructionNode(Instruction *_instr);
Instruction *getInstruction() const {
assert(treeNodeType == NTInstructionNode);
return cast<Instruction>(val);
}
protected:
virtual void dumpNode(int indent) const;
};
class VRegListNode : public InstrTreeNode {
public:
VRegListNode() : InstrTreeNode(NTVRegListNode, 0) {
opLabel = VRegListOp;
}
protected:
virtual void dumpNode(int indent) const;
};
class VRegNode : public InstrTreeNode {
public:
VRegNode(Value* _val) : InstrTreeNode(NTVRegNode, _val) {
opLabel = VRegNodeOp;
}
protected:
virtual void dumpNode(int indent) const;
};
class ConstantNode : public InstrTreeNode {
public:
ConstantNode(Constant *constVal)
: InstrTreeNode(NTConstNode, (Value*)constVal) {
opLabel = ConstantNodeOp;
}
Constant *getConstVal() const { return (Constant*) val;}
protected:
virtual void dumpNode(int indent) const;
};
class LabelNode : public InstrTreeNode {
public:
LabelNode(BasicBlock* BB) : InstrTreeNode(NTLabelNode, (Value*)BB) {
opLabel = LabelNodeOp;
}
BasicBlock *getBasicBlock() const { return (BasicBlock*)val;}
protected:
virtual void dumpNode(int indent) const;
};
//------------------------------------------------------------------------
// class InstrForest
//
// A forest of instruction trees, usually for a single method.
//
// Methods:
// buildTreesForMethod() Builds the forest of trees for a method
// getTreeNodeForInstr() Returns the tree node for an Instruction
// getRootSet() Returns a set of root nodes for all the trees
//
//------------------------------------------------------------------------
class InstrForest : private std::hash_map<const Instruction *, InstructionNode*> {
private:
std::hash_set<InstructionNode*> treeRoots;
public:
/*ctor*/ InstrForest (Function *F);
/*dtor*/ ~InstrForest ();
inline InstructionNode *getTreeNodeForInstr(Instruction* instr) {
return (*this)[instr];
}
inline const std::hash_set<InstructionNode*> &getRootSet() const {
return treeRoots;
}
void dump() const;
private:
//
// Private methods for buidling the instruction forest
//
void setLeftChild (InstrTreeNode* parent, InstrTreeNode* child);
void setRightChild(InstrTreeNode* parent, InstrTreeNode* child);
void setParent (InstrTreeNode* child, InstrTreeNode* parent);
void noteTreeNodeForInstr(Instruction* instr, InstructionNode* treeNode);
InstructionNode* buildTreeForInstruction(Instruction* instr);
};
#endif
|