forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptcse.h
More file actions
492 lines (420 loc) · 12.1 KB
/
optcse.h
File metadata and controls
492 lines (420 loc) · 12.1 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#ifndef _OPTCSE_H
#define _OPTCSE_H
#include "compiler.h"
struct CSEdsc;
class CSE_Candidate;
// Base class for CSE Heuristics
//
// Also usable as a "do nothing" heuristic.
//
class CSE_HeuristicCommon
{
protected:
CSE_HeuristicCommon(Compiler*);
Compiler* m_pCompiler;
unsigned m_addCSEcount;
CSEdsc** sortTab;
size_t sortSiz;
bool madeChanges;
Compiler::codeOptimize codeOptKind;
bool enableConstCSE;
#ifdef DEBUG
jitstd::vector<unsigned>* m_sequence;
#endif
public:
virtual void Initialize()
{
}
virtual void SortCandidates()
{
}
virtual bool PromotionCheck(CSE_Candidate* candidate)
{
return false;
}
virtual void PerformCSE(CSE_Candidate* candidate);
virtual void Cleanup()
{
// Add termination marker to cse sequence
INDEBUG(m_sequence->push_back(0));
}
// This currently mixes legality and profitability,
// eventually it should just be pure legality and
// the derived classes handle the profitability.
//
bool CanConsiderTree(GenTree* tree, bool isReturn);
virtual bool ConsiderTree(GenTree* tree, bool isReturn)
{
return false;
}
virtual void AdjustHeuristic(CSE_Candidate* candidate)
{
}
virtual const char* Name() const
{
return "Common CSE Heuristic";
}
virtual void ConsiderCandidates();
bool MadeChanges() const
{
return madeChanges;
}
Compiler::codeOptimize CodeOptKind() const
{
return codeOptKind;
}
bool IsCompatibleType(var_types cseLclVarTyp, var_types expTyp);
#ifdef DEBUG
virtual void DumpMetrics();
virtual void Announce()
{
JITDUMP("%s\n", Name());
}
#endif
};
#ifdef DEBUG
// Randomized CSE heuristic
//
// Performs CSEs randomly, useful for stress
//
class CSE_HeuristicRandom : public CSE_HeuristicCommon
{
private:
CLRRandom m_cseRNG;
unsigned m_bias;
public:
CSE_HeuristicRandom(Compiler*);
void ConsiderCandidates();
bool ConsiderTree(GenTree* tree, bool isReturn);
const char* Name() const
{
return "Random CSE Heuristic";
}
#ifdef DEBUG
virtual void Announce();
#endif
};
// Replay CSE heuristic
//
// Performs CSE specified by JitReplayCSE
//
class CSE_HeuristicReplay : public CSE_HeuristicCommon
{
public:
CSE_HeuristicReplay(Compiler*);
void ConsiderCandidates();
bool ConsiderTree(GenTree* tree, bool isReturn);
const char* Name() const
{
return "Replay CSE Heuristic";
}
#ifdef DEBUG
virtual void Announce();
#endif
};
// Reinforcement Learning CSE heuristic
//
// Uses a "linear" feature model with
// softmax policy.
//
class CSE_HeuristicRL : public CSE_HeuristicCommon
{
private:
struct Choice
{
Choice(CSEdsc* dsc, double preference) : m_dsc(dsc), m_preference(preference), m_softmax(0)
{
}
CSEdsc* m_dsc;
double m_preference;
double m_softmax;
};
enum
{
numParameters = 14,
booleanScale = 5,
maxSteps = 65, // MAX_CSE_CNT + 1 (for stopping)
};
double m_parameters[numParameters];
double m_alpha;
double m_rewards[maxSteps];
CLRRandom m_cseRNG;
bool m_updateParameters;
bool m_greedy;
bool m_verbose;
void GetFeatures(CSEdsc* dsc, double* features);
void DumpFeatures(CSEdsc* dsc, double* features);
double Preference(CSEdsc* dsc);
Choice& ChooseSoftmax(ArrayStack<Choice>& choices);
Choice& ChooseGreedy(ArrayStack<Choice>& choices);
void BuildChoices(ArrayStack<Choice>& choices);
void Softmax(ArrayStack<Choice>& choices);
void DumpChoices(ArrayStack<Choice>& choices, int higlight = -1);
void DumpChoices(ArrayStack<Choice>& choices, CSEdsc* higlight);
void UpdateParameters();
void GreedyPolicy();
void SoftmaxPolicy();
void UpdateParametersStep(CSEdsc* dsc, ArrayStack<Choice>& choices, double reward, double* delta);
Choice* FindChoice(CSEdsc* dsc, ArrayStack<Choice>& choices);
public:
CSE_HeuristicRL(Compiler*);
void ConsiderCandidates();
bool ConsiderTree(GenTree* tree, bool isReturn);
const char* Name() const
{
return "Reinforcement Learning CSE Heuristic";
}
#ifdef DEBUG
virtual void DumpMetrics();
virtual void Announce();
// Likelihood of each choice made in the sequence
jitstd::vector<double>* m_likelihoods;
// Likelihood of each action from starting state
jitstd::vector<double>* m_baseLikelihoods;
jitstd::vector<char*>* m_features;
#endif
};
#endif
// Standard CSE heuristic
//
// The following class handles the CSE heuristics
// we use a complex set of heuristic rules
// to determine if it is likely to be profitable to perform this CSE
//
class CSE_Heuristic : public CSE_HeuristicCommon
{
private:
weight_t aggressiveRefCnt;
weight_t moderateRefCnt;
unsigned enregCount; // count of the number of predicted enregistered variables
bool largeFrame;
bool hugeFrame;
public:
CSE_Heuristic(Compiler*);
void Initialize();
void SortCandidates();
bool PromotionCheck(CSE_Candidate* candidate);
void AdjustHeuristic(CSE_Candidate* candidate);
bool ConsiderTree(GenTree* tree, bool isReturn);
const char* Name() const
{
return "Standard CSE Heuristic";
}
};
// Generic list of nodes - used by the CSE logic
struct treeStmtLst
{
treeStmtLst* tslNext;
GenTree* tslTree; // tree node
Statement* tslStmt; // statement containing the tree
BasicBlock* tslBlock; // block containing the statement
};
// The following logic keeps track of expressions via a simple hash table.
struct CSEdsc
{
CSEdsc* csdNextInBucket; // used by the hash table
size_t csdHashKey; // the original hashkey
ssize_t csdConstDefValue; // When we CSE similar constants, this is the value that we use as the def
ValueNum csdConstDefVN; // When we CSE similar constants, this is the ValueNumber that we use for the LclVar
// assignment
unsigned csdIndex; // 1..optCSECandidateCount
bool csdIsSharedConst; // true if this CSE is a shared const
bool csdLiveAcrossCall;
unsigned short csdDefCount; // definition count
unsigned short csdUseCount; // use count (excluding the implicit uses at defs)
weight_t csdDefWtCnt; // weighted def count
weight_t csdUseWtCnt; // weighted use count (excluding the implicit uses at defs)
GenTree* csdTree; // treenode containing the 1st occurrence
Statement* csdStmt; // stmt containing the 1st occurrence
BasicBlock* csdBlock; // block containing the 1st occurrence
treeStmtLst* csdTreeList; // list of matching tree nodes: head
treeStmtLst* csdTreeLast; // list of matching tree nodes: tail
// The exception set that is now required for all defs of this CSE.
// This will be set to NoVN if we decide to abandon this CSE
ValueNum defExcSetPromise;
// The set of exceptions we currently can use for CSE uses.
ValueNum defExcSetCurrent;
// if all def occurrences share the same conservative normal value
// number, this will reflect it; otherwise, NoVN.
// not used for shared const CSE's
ValueNum defConservNormVN;
// We may form candidates that we can't use.
// Is this a viable cse?
bool IsViable()
{
if (defExcSetPromise == ValueNumStore::NoVN)
{
// Multiple defs with incompatible def sets
//
return false;
}
if ((csdDefCount == 0) || (csdUseCount == 0))
{
// No uses, or perhaps unreachable uses.
//
return false;
}
if ((csdDefWtCnt <= 0) || (csdUseWtCnt <= 0))
{
// No hot uses, or messed up profile
//
return false;
}
return true;
}
};
// The following class nested within CSE_Heuristic encapsulates the information
// about the current CSE candidate that is under consideration
//
// TODO-Cleanup: This is still very much based upon the old Lexical CSE implementation
// and needs to be reworked for the Value Number based implementation
//
class CSE_Candidate
{
CSE_HeuristicCommon* m_context;
CSEdsc* m_CseDsc;
unsigned m_cseIndex;
weight_t m_defCount;
weight_t m_useCount;
unsigned m_Cost;
unsigned m_Size;
// When this Candidate is successfully promoted to a CSE we record
// the following information about what category was used when promoting it.
//
// We will set m_Aggressive:
// When we believe that the CSE very valuable in terms of weighted ref counts,
// such that it would always be enregistered by the register allocator.
//
// We will set m_Moderate:
// When we believe that the CSE is moderately valuable in terms of weighted ref counts,
// such that it is more likely than not to be enregistered by the register allocator
//
// We will set m_Conservative:
// When we didn't set m_Aggressive or m_Moderate.
// Such candidates typically are expensive to compute and thus are
// always profitable to promote even when they aren't enregistered.
//
// We will set m_StressCSE:
// When the candidate is only being promoted because of a Stress mode.
//
// We will set m_Random
// When the candidate is randomly promoted
//
bool m_Aggressive;
bool m_Moderate;
bool m_Conservative;
bool m_StressCSE;
bool m_Random;
public:
CSE_Candidate(CSE_HeuristicCommon* context, CSEdsc* cseDsc)
: m_context(context)
, m_CseDsc(cseDsc)
, m_cseIndex(m_CseDsc->csdIndex)
, m_defCount(0)
, m_useCount(0)
, m_Cost(0)
, m_Size(0)
, m_Aggressive(false)
, m_Moderate(false)
, m_Conservative(false)
, m_StressCSE(false)
, m_Random(false)
{
}
CSEdsc* CseDsc()
{
return m_CseDsc;
}
unsigned CseIndex()
{
return m_cseIndex;
}
weight_t DefCount()
{
return m_defCount;
}
weight_t UseCount()
{
return m_useCount;
}
// TODO-CQ: With ValNum CSE's the Expr and its cost can vary.
GenTree* Expr()
{
return m_CseDsc->csdTree;
}
unsigned Cost()
{
return m_Cost;
}
unsigned Size()
{
return m_Size;
}
bool IsSharedConst()
{
return m_CseDsc->csdIsSharedConst;
}
bool LiveAcrossCall()
{
return m_CseDsc->csdLiveAcrossCall;
}
void SetAggressive()
{
m_Aggressive = true;
}
bool IsAggressive()
{
return m_Aggressive;
}
void SetModerate()
{
m_Moderate = true;
}
bool IsModerate()
{
return m_Moderate;
}
void SetConservative()
{
m_Conservative = true;
}
bool IsConservative()
{
return m_Conservative;
}
void SetStressCSE()
{
m_StressCSE = true;
}
bool IsStressCSE()
{
return m_StressCSE;
}
void SetRandom()
{
m_Random = true;
}
bool IsRandom()
{
return m_Random;
}
void InitializeCounts()
{
m_Size = Expr()->GetCostSz(); // always the GetCostSz()
if (m_context->CodeOptKind() == Compiler::SMALL_CODE)
{
m_Cost = m_Size; // the estimated code size
m_defCount = m_CseDsc->csdDefCount; // def count
m_useCount = m_CseDsc->csdUseCount; // use count (excluding the implicit uses at defs)
}
else
{
m_Cost = Expr()->GetCostEx(); // the estimated execution cost
m_defCount = m_CseDsc->csdDefWtCnt; // weighted def count
m_useCount = m_CseDsc->csdUseWtCnt; // weighted use count (excluding the implicit uses at defs)
}
}
};
#endif // _OPTCSE_H