-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority.cc
More file actions
201 lines (174 loc) · 4.94 KB
/
Copy pathpriority.cc
File metadata and controls
201 lines (174 loc) · 4.94 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
/* definitions of various priority functions used for deciding the
* order live ranges should be assigned to a register.
*/
/*--------------------------INCLUDES---------------------------*/
#include <cmath>
#include "priority.h"
#include "live_range.h"
#include "live_unit.h"
#include "cfg_tools.h"
#include "params.h"
#include "shared_globals.h"
/*------------------MODULE LOCAL DECLARATIONS------------------*/
namespace {
Priority BasePriority(
LiveRange* lr,
bool use_log,
bool square_len,
bool normalize
);
Priority LiveUnit_ComputePriority(LiveRange* lr, LiveUnit* lu);
bool LiveUnit_CanMoveLoad(LiveRange* lr, LiveUnit* lu);
int LiveUnit_LoadLoopDepth(LiveRange* lr, LiveUnit* lu);
inline double log2(double v){
return log(v)/log(2);
}
}
/*--------------------BEGIN IMPLEMENTATION---------------------*/
namespace Chow{
namespace PriorityFuns {
Priority Classic(LiveRange* lr)
{
return BasePriority(lr, false, false, true);
}
Priority NoNormal(LiveRange* lr)
{
return BasePriority(lr, false, false, false);
}
Priority Gnu(LiveRange* lr)
{
return BasePriority(lr, true, false, true);
}
Priority SquareNormal(LiveRange* lr)
{
return BasePriority(lr, false, true, true);
}
Priority GnuSquareNormal(LiveRange* lr)
{
return BasePriority(lr, true, true, true);
}
}}//end Chow::PriorityFuns namespace
/*-------------------BEGIN LOCAL DEFINITIONS-------------------*/
namespace {
/*
*=======================================
* BasePriority()
*=======================================
*
***/
Priority BasePriority(
LiveRange* lr,
bool use_log,
bool square_len,
bool normalize
)
{
Priority pr = 0.0;
Unsigned_Int clu = 0; //count of live units
for(LiveRange::iterator luIT = lr->begin(), luE = lr->end();
luIT != luE; ++luIT)
{
pr += LiveUnit_ComputePriority(lr, *luIT);
clu++;
}
if(use_log) pr = log2(pr);
if(square_len) clu = clu*clu;
if(normalize) pr = pr/clu;
return pr;
}
/*
*=======================================
* LiveUnit_ComputePriority()
*=======================================
*
***/
inline bool need_store(LiveUnit* lu){
if(Params::Algorithm::move_loads_and_stores){
return lu->need_store && !lu->internal_store;
}
return lu->need_store;
}
Priority LiveUnit_ComputePriority(LiveRange* lr, LiveUnit* lu)
{
using Params::Machine::load_save_weight;
using Params::Machine::store_save_weight;
using Params::Machine::move_cost_weight;
using Params::Algorithm::loop_depth_weight;
Priority unitPrio =
load_save_weight * lu->uses
+ store_save_weight * lu->defs
- move_cost_weight * need_store(lu);
unitPrio *= pow(loop_depth_weight, Globals::depths[bid(lu->block)]);
//treat load loop cost separte in case we can move it up from a loop
int loadLoopDepth = LiveUnit_LoadLoopDepth(lr, lu);
unitPrio -= (move_cost_weight * lu->need_load)
* pow(loop_depth_weight, loadLoopDepth);
return unitPrio;
}
/*
*=======================================
* LiveUnit_CanMoveLoad()
*=======================================
* used by LiveUnit to compute priority taking into account the final
* resting place of the load
*
***/
bool LiveUnit_CanMoveLoad(LiveRange* lr, LiveUnit* lu)
{
if(lu->need_load) return false;
//the load can be moved if there is at least on predecessor *in* the
//live range. we know there must be at least one *NOT* in the live
//range because the unit needs a load and this only happens when it
//is an entry point
int lrPreds = 0;
Edge* e;
Block_ForAllPreds(e, lu->block)
{
if(lr->ContainsBlock(e->pred)) lrPreds++;
}
return (lrPreds > 0 && Params::Algorithm::move_loads_and_stores);
}
/*
*=======================================
* LiveUnit_LoadLoopDepth()
* computes loop depth level for inserting loads. the level could be
* decreased by one to account for moving a load up from a loop header
*=======================================
*
***/
int LiveUnit_LoadLoopDepth(LiveRange* lr, LiveUnit* lu)
{
int depth = depths[bid(lu->block)];
if(Block_IsLoopHeader(lu->block) && LiveUnit_CanMoveLoad(lr, lu))
{
depth -= 1;
}
return depth;
}
#if 0
//kept for prosperity in case we want to compare orig priority to the
//priority used when moving loads and stores
Priority LiveRange_OrigComputePriority(LiveRange* lr)
{
using Params::Machine::load_save_weight;
using Params::Machine::store_save_weight;
using Params::Machine::move_cost_weight;
using Params::Algorithm::loop_depth_weight;
int clu = 0; //count of live units
Priority pr = 0.0;
for(LiveRange::iterator it = lr->begin(); it != lr->end(); it++)
{
LiveUnit* lu = *it;
Priority unitPrio =
load_save_weight * lu->uses
+ store_save_weight * lu->defs
- move_cost_weight * lu->need_store
- move_cost_weight * lu->need_load;
pr += unitPrio
* pow(loop_depth_weight, Globals::depths[bid(lu->block)]);
clu++;
}
return pr/clu;
}
#endif
}//end anonymous namespace