aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/SelectionDAG/SelectionDAGCSEMap.cpp
blob: 7cec545f5d365cdf4e617a10fdfcd39317b9389f (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//===-- SelectionDAGCSEMap.cpp - Implement the SelectionDAG CSE Map -------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This implements the SelectionDAGCSEMap class.
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;

//===----------------------------------------------------------------------===//
// SelectionDAGCSEMap::NodeID Implementation

/// SetValueTypes - Value type lists are intern'd so we can represent them
/// solely with their pointer.
void SelectionDAGCSEMap::NodeID::SetValueTypes(SDVTList VTList) {
  AddPointer(VTList.VTs);  
}

SelectionDAGCSEMap::NodeID::NodeID(SDNode *N) {
  SetOpcode(N->getOpcode());
  // Add the return value info.
  SetValueTypes(N->getVTList());
  // Add the operand info.
  SetOperands(N->op_begin(), N->getNumOperands());

  // Handle SDNode leafs with special info.
  if (N->getNumOperands() == 0) {
    switch (N->getOpcode()) {
    default: break;  // Normal nodes don't need extra info.
    case ISD::TargetConstant:
    case ISD::Constant:
      AddInteger(cast<ConstantSDNode>(N)->getValue());
      break;
    case ISD::TargetConstantFP:
    case ISD::ConstantFP:
      AddInteger(DoubleToBits(cast<ConstantFPSDNode>(N)->getValue()));
      break;
    case ISD::TargetGlobalAddress:
    case ISD::GlobalAddress:
      AddPointer(cast<GlobalAddressSDNode>(N)->getGlobal());
      AddInteger(cast<GlobalAddressSDNode>(N)->getOffset());
      break;
    case ISD::BasicBlock:
      AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
      break;
    case ISD::Register:
      AddInteger(cast<RegisterSDNode>(N)->getReg());
      break;
    case ISD::SRCVALUE:
      AddPointer(cast<SrcValueSDNode>(N)->getValue());
      AddInteger(cast<SrcValueSDNode>(N)->getOffset());
      break;
    case ISD::FrameIndex:
    case ISD::TargetFrameIndex:
      AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
      break;
    case ISD::JumpTable:
    case ISD::TargetJumpTable:
      AddInteger(cast<JumpTableSDNode>(N)->getIndex());
      break;
    case ISD::ConstantPool:
    case ISD::TargetConstantPool:
      AddInteger(cast<ConstantPoolSDNode>(N)->getAlignment());
      AddInteger(cast<ConstantPoolSDNode>(N)->getOffset());
      AddPointer(cast<ConstantPoolSDNode>(N)->get());
      break;
    }
  }
}

SelectionDAGCSEMap::NodeID::NodeID(unsigned short ID, SDVTList VTList) {
  SetOpcode(ID);
  SetValueTypes(VTList);
  SetOperands();
}
SelectionDAGCSEMap::NodeID::NodeID(unsigned short ID, SDVTList VTList,
                                   SDOperand Op) {
  SetOpcode(ID);
  SetValueTypes(VTList);
  SetOperands(Op);
}
SelectionDAGCSEMap::NodeID::NodeID(unsigned short ID, SDVTList VTList, 
                                   SDOperand Op1, SDOperand Op2) {
  SetOpcode(ID);
  SetValueTypes(VTList);
  SetOperands(Op1, Op2);
}
SelectionDAGCSEMap::NodeID::NodeID(unsigned short ID, SDVTList VTList, 
                                   SDOperand Op1, SDOperand Op2,
                                   SDOperand Op3) {
  SetOpcode(ID);
  SetValueTypes(VTList);
  SetOperands(Op1, Op2, Op3);
}
SelectionDAGCSEMap::NodeID::NodeID(unsigned short ID, SDVTList VTList, 
                                   const SDOperand *OpList, unsigned N) {
  SetOpcode(ID);
  SetValueTypes(VTList);
  SetOperands(OpList, N);
}

void SelectionDAGCSEMap::NodeID::AddPointer(const void *Ptr) {
  // Note: this adds pointers to the hash using sizes and endianness that depend
  // on the host.  It doesn't matter however, because hashing on pointer values
  // in inherently unstable.  Nothing in the SelectionDAG should depend on the
  // ordering of nodes in the CSEMap.
  intptr_t PtrI = (intptr_t)Ptr;
  Bits.push_back(unsigned(PtrI));
  if (sizeof(intptr_t) > sizeof(unsigned))
    Bits.push_back(unsigned(uint64_t(PtrI) >> 32));
}

void SelectionDAGCSEMap::NodeID::AddOperand(SDOperand Op) {
  AddPointer(Op.Val);
  Bits.push_back(Op.ResNo);
}

void SelectionDAGCSEMap::NodeID::SetOperands(const SDOperand *Ops, 
                                             unsigned NumOps) {
  for (; NumOps; --NumOps, ++Ops)
    AddOperand(*Ops);
}


/// ComputeHash - Compute a strong hash value for this NodeID, for lookup in
/// the SelectionDAGCSEMap.
unsigned SelectionDAGCSEMap::NodeID::ComputeHash() const {
  // This is adapted from SuperFastHash by Paul Hsieh.
  unsigned Hash = Bits.size();
  for (const unsigned *BP = &Bits[0], *E = BP+Bits.size(); BP != E; ++BP) {
    unsigned Data = *BP;
    Hash         += Data & 0xFFFF;
    unsigned Tmp  = ((Data >> 16) << 11) ^ Hash;
    Hash          = (Hash << 16) ^ Tmp;
    Hash         += Hash >> 11;
  }
  
  // Force "avalanching" of final 127 bits.
  Hash ^= Hash << 3;
  Hash += Hash >> 5;
  Hash ^= Hash << 4;
  Hash += Hash >> 17;
  Hash ^= Hash << 25;
  Hash += Hash >> 6;
  return Hash;
}

bool SelectionDAGCSEMap::NodeID::operator==(const NodeID &RHS) const {
  if (Bits.size() != RHS.Bits.size()) return false;
  return memcmp(&Bits[0], &RHS.Bits[0], Bits.size()*sizeof(Bits[0])) == 0;
}


//===----------------------------------------------------------------------===//
// SelectionDAGCSEMap Implementation

SelectionDAGCSEMap::SelectionDAGCSEMap() : NumNodes(0) {
  NumBuckets = 64;
  Buckets = new void*[NumBuckets];
  memset(Buckets, 0, NumBuckets*sizeof(void*));
}
SelectionDAGCSEMap::~SelectionDAGCSEMap() {
  delete [] Buckets;
}

/// GetNextPtr - In order to save space, each bucket is a singly-linked-list. In
/// order to make deletion more efficient, we make the list circular, so we can
/// delete a node without computing its hash.  The problem with this is that the
/// start of the hash buckets are not SDNodes.  If NextInBucketPtr is a bucket
/// pointer, this method returns null: use GetBucketPtr when this happens.
SDNode *SelectionDAGCSEMap::GetNextPtr(void *NextInBucketPtr) {
  if (NextInBucketPtr >= Buckets && NextInBucketPtr < Buckets+NumBuckets)
    return 0;
  return static_cast<SDNode*>(NextInBucketPtr);
}

/// GetNextPtr - This is just like the previous GetNextPtr implementation, but
/// allows a bucket array to be specified.
SDNode *SelectionDAGCSEMap::GetNextPtr(void *NextInBucketPtr, void **Bucks, 
                                       unsigned NumBuck) {
  if (NextInBucketPtr >= Bucks && NextInBucketPtr < Bucks+NumBuck)
    return 0;
  return static_cast<SDNode*>(NextInBucketPtr);
}

void **SelectionDAGCSEMap::GetBucketPtr(void *NextInBucketPtr) {
  //assert(NextInBucketPtr >= Buckets && NextInBucketPtr < Buckets+NumBuckets &&
  //       "NextInBucketPtr is not a bucket ptr");
  return static_cast<void**>(NextInBucketPtr);
}

/// GetBucketFor - Hash the specified node ID and return the hash bucket for the
/// specified ID.
void **SelectionDAGCSEMap::GetBucketFor(const NodeID &ID) const {
  // NumBuckets is always a power of 2.
  unsigned BucketNum = ID.ComputeHash() & (NumBuckets-1);
  return Buckets+BucketNum;
}

/// GrowHashTable - Double the size of the hash table and rehash everything.
///
void SelectionDAGCSEMap::GrowHashTable() {
  void **OldBuckets = Buckets;
  unsigned OldNumBuckets = NumBuckets;
  NumBuckets <<= 1;
  
  // Reset the node count to zero: we're going to reinsert everything.
  NumNodes = 0;
  
  // Clear out new buckets.
  Buckets = new void*[NumBuckets];
  memset(Buckets, 0, NumBuckets*sizeof(void*));

  // Walk the old buckets, rehashing nodes into their new place.
  for (unsigned i = 0; i != OldNumBuckets; ++i) {
    void *Probe = OldBuckets[i];
    if (!Probe) continue;
    while (SDNode *NodeInBucket = GetNextPtr(Probe, OldBuckets, OldNumBuckets)){
      // Figure out the next link, remove NodeInBucket from the old link.
      Probe = NodeInBuck