/
ilp.cc
362 lines (303 loc) · 9.72 KB
/
ilp.cc
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
#include "dr_api.h"
#include <stdint.h>
#include <map>
#include <set>
#include <utility>
#include <vector>
#define USE_CLEAN_CALLS
//#define THREAD_SAFE_CLEAN_CALLS
#define FIND_DEAD_EFLAGS
using namespace std;
typedef struct {
uint64_t total_ni;
uint64_t sum_ilp;
} ilp_stats;
static ilp_stats stats;
static ilp_stats offline_stats;
#ifdef THREAD_SAFE_CLEAN_CALLS
static void* stats_mutex;
#endif
static void event_exit(void);
static dr_emit_flags_t event_basic_block(void *drcontext, void *tag,
instrlist_t *bb, bool for_trace, bool translating);
DR_EXPORT void
dr_init(client_id_t id)
{
stats.total_ni = 0;
stats.sum_ilp = 0;
offline_stats.total_ni = 0;
offline_stats.sum_ilp = 0;
#ifdef THREAD_SAFE_CLEAN_CALLS
stats_mutex = dr_mutex_create();
#endif
dr_register_bb_event(event_basic_block);
dr_register_exit_event(event_exit);
}
static void
event_exit(void)
{
fprintf(stderr, "ilp=%.4f\n",
(double) stats.sum_ilp / stats.total_ni / 1000);
fprintf(stderr, "ilp-offline=%.4f\n",
(double) offline_stats.sum_ilp / offline_stats.total_ni / 1000);
#ifdef THREAD_SAFE_CLEAN_CALLS
dr_mutex_destroy(stats_mutex);
#endif
}
inline reg_id_t
get_full_size_reg(reg_id_t reg)
{
switch (reg)
{
case DR_REG_EAX: case DR_REG_AX: case DR_REG_AH: case DR_REG_AL: return DR_REG_EAX;
case DR_REG_ECX: case DR_REG_CX: case DR_REG_CH: case DR_REG_CL: return DR_REG_ECX;
case DR_REG_EDX: case DR_REG_DX: case DR_REG_DH: case DR_REG_DL: return DR_REG_EDX;
case DR_REG_EBX: case DR_REG_BX: case DR_REG_BH: case DR_REG_BL: return DR_REG_EBX;
}
return reg;
}
inline void
insert_unique(vector<opnd_t>& list, const opnd_t& opnd)
{
for (vector<opnd_t>::const_iterator it = list.begin();
it != list.end(); ++it)
{
if (opnd_same_address(opnd, *it))
return;
}
list.push_back(opnd);
}
inline pair<opnd_t, int>*
find_same_mem(vector< pair<opnd_t, int> >& list, const opnd_t& opnd)
{
for (vector< pair<opnd_t, int> >::iterator it = list.begin();
it != list.end(); ++it)
{
if (opnd_same_address(opnd, it->first))
{
return &(*it);
}
}
return NULL;
}
inline void
get_read_eflags(uint eflags, set<int>& read_eflags)
{
if (eflags & EFLAGS_READ_AF) read_eflags.insert(EFLAGS_AF);
if (eflags & EFLAGS_READ_CF) read_eflags.insert(EFLAGS_CF);
if (eflags & EFLAGS_READ_DF) read_eflags.insert(EFLAGS_DF);
if (eflags & EFLAGS_READ_OF) read_eflags.insert(EFLAGS_OF);
if (eflags & EFLAGS_READ_PF) read_eflags.insert(EFLAGS_PF);
}
inline void
get_write_eflags(uint eflags, set<int>& write_eflags)
{
if (eflags & EFLAGS_WRITE_AF) write_eflags.insert(EFLAGS_AF);
if (eflags & EFLAGS_WRITE_CF) write_eflags.insert(EFLAGS_CF);
if (eflags & EFLAGS_WRITE_DF) write_eflags.insert(EFLAGS_DF);
if (eflags & EFLAGS_WRITE_OF) write_eflags.insert(EFLAGS_OF);
if (eflags & EFLAGS_WRITE_PF) write_eflags.insert(EFLAGS_PF);
}
#define _MAX(x, y) (((x) > (y)) ? (x) : (y))
static void
calculate_ilp(instrlist_t* bb, int32_t& ni, int32_t& ilp)
{
ni = 0;
int nc = 0;
map<reg_id_t, int> reg_nc;
//vector< pair<opnd_t, int> > mem_nc;
int mem_nc = 0;
map<int, int> eflags_nc;
/* Look for the following types of dependencies:
* reg -> reg
* reg -> base_reg in base+disp memory
* mem -> mem, in opnd_same_mem() we trust
* EFLAGS
*/
for (instr_t* instr = instrlist_first(bb);
instr != NULL; instr = instr_get_next(instr))
{
int ic = 0;
/* Process source operands */
set<reg_id_t> src_regs;
vector<opnd_t> src_mems;
set<int> read_eflags;
int src_cnt = instr_num_srcs(instr);
for (int i = 0; i < src_cnt; ++i)
{
opnd_t opnd = instr_get_src(instr, i);
if (opnd_is_reg(opnd))
src_regs.insert(opnd_get_reg(opnd));
else if (opnd_is_base_disp(opnd))
{
src_regs.insert(opnd_get_base(opnd));
insert_unique(src_mems, opnd);
}
else if (opnd_is_abs_addr(opnd) || opnd_is_pc(opnd))
{
insert_unique(src_mems, opnd);
}
}
int dst_cnt_1 = instr_num_dsts(instr);
for (int i = 0; i < dst_cnt_1; ++i)
{
opnd_t opnd = instr_get_dst(instr, i);
if (opnd_is_reg(opnd))
src_regs.insert(opnd_get_reg(opnd));
else if (opnd_is_base_disp(opnd))
{
src_regs.insert(opnd_get_base(opnd));
insert_unique(src_mems, opnd);
}
else if (opnd_is_abs_addr(opnd) || opnd_is_pc(opnd))
{
insert_unique(src_mems, opnd);
}
}
uint eflags = instr_get_eflags(instr);
get_read_eflags(eflags, read_eflags);
for (set<reg_id_t>::const_iterator it = src_regs.begin();
it != src_regs.end(); ++it)
{
ic = _MAX(ic, reg_nc[*it]);
}
for (vector<opnd_t>::const_iterator it = src_mems.begin();
it != src_mems.end(); ++it)
{
//pair<opnd_t, int>* pRecord = find_same_mem(mem_nc, *it);
//if (pRecord)
// ic = _MAX(ic, pRecord->second);
ic = _MAX(ic, mem_nc);
}
for (set<int>::const_iterator it = read_eflags.begin();
it != read_eflags.end(); ++it)
{
ic = _MAX(ic, eflags_nc[*it]);
}
nc = _MAX(ic, nc);
/* Process destination operands */
set<reg_id_t> dst_regs;
vector<opnd_t> dst_mems;
set<int> write_eflags;
int dst_cnt = instr_num_dsts(instr);
for (int i = 0; i < dst_cnt; ++i)
{
opnd_t opnd = instr_get_dst(instr, i);
if (opnd_is_reg(opnd))
dst_regs.insert(opnd_get_reg(opnd));
else if (opnd_is_base_disp(opnd) || opnd_is_abs_addr(opnd))
{
insert_unique(dst_mems, opnd);
}
}
get_write_eflags(eflags, write_eflags);
for (set<reg_id_t>::const_iterator it = dst_regs.begin();
it != dst_regs.end(); ++it)
{
reg_nc[*it] = ic + 1;
}
for (vector<opnd_t>::const_iterator it = dst_mems.begin();
it != dst_mems.end(); ++it)
{
//pair<opnd_t, int>* pRecord = find_same_mem(mem_nc, *it);
//if (pRecord)
// pRecord->second = ic + 1;
//else
// mem_nc.push_back(pair<opnd_t, int>(*it, 1));
mem_nc = ic + 1;
}
for (set<int>::const_iterator it = write_eflags.begin();
it != write_eflags.end(); ++it)
{
eflags_nc[*it] = ic + 1;
}
/* Increment the instruction count */
ni++;
}
if (nc > 0)
ilp = (ni * 1000) / nc;
else /* no dependencies, all can execute in parallel */
ilp = (ni * 1000);
if (ilp < 1000)
{
dr_fprintf(STDERR, "Assertion FAILED: ilp=%d\n", ilp);
throw -1;
}
//dr_fprintf(STDERR, "BB: size=%d, ILP=%.3f\n", ni, (double) ilp / 1000);
}
#define TESTALL(mask, var) (((mask) & (var)) == (mask))
#define TESTANY(mask, var) (((mask) & (var)) != 0)
#define EFLAGS_DEAD(x) (TESTALL(EFLAGS_WRITE_6, (x)) \
&& !TESTANY(EFLAGS_READ_6, (x)))
static bool
find_dead_eflags_instr(instrlist_t *bb, instr_t* pos)
{
for (instr_t* ins = instrlist_first(bb);
ins != NULL; ins = instr_get_next(ins))
{
unsigned int flags = instr_get_arith_flags(ins);
if (EFLAGS_DEAD(flags))
{
pos = ins;
return true;
}
}
pos = instrlist_first(bb);
return false;
}
inline static void
preinsert_add64(void* dc, instrlist_t* bb, instr_t* pos,
void* absmem, int32_t addend)
{
instrlist_meta_preinsert(bb, pos,
LOCK(INSTR_CREATE_add(dc,
OPND_CREATE_ABSMEM((byte *)absmem, OPSZ_4),
OPND_CREATE_INT32(addend))));
instrlist_meta_preinsert(bb, pos,
LOCK(INSTR_CREATE_adc(dc,
OPND_CREATE_ABSMEM((byte *)absmem + 4, OPSZ_4),
OPND_CREATE_INT8(0))));
}
static void
update_ilp(int32_t ni, int32_t sum_offset)
{
#ifdef THREAD_SAFE_CLEAN_CALLS
dr_mutex_lock(stats_mutex);
#endif
stats.total_ni += ni;
stats.sum_ilp += sum_offset;
#ifdef THREAD_SAFE_CLEAN_CALLS
dr_mutex_unlock(stats_mutex);
#endif
}
static dr_emit_flags_t
event_basic_block(void *dc, void *tag, instrlist_t *bb,
bool for_trace, bool translating)
{
int32_t num_instr = 0;
int32_t ilp = 0;
int32_t ilp_sum_offset = 0;
calculate_ilp(bb, num_instr, ilp);
ilp_sum_offset = ilp * num_instr;
offline_stats.total_ni += num_instr;
offline_stats.sum_ilp += ilp_sum_offset;
instr_t* pos = instrlist_first(bb);
#ifdef USE_CLEAN_CALLS
dr_insert_clean_call(dc, bb, pos, (void*) update_ilp, false, 2,
OPND_CREATE_INT32(num_instr),
OPND_CREATE_INT32(ilp_sum_offset));
#else
#ifdef FIND_DEAD_EFLAGS
bool dead_eflags_found = find_dead_eflags_instr(bb, pos);
if (!dead_eflags_found)
#endif
dr_save_arith_flags(dc, bb, pos, SPILL_SLOT_1);
preinsert_add64(dc, bb, pos, &stats.total_ni, num_instr);
preinsert_add64(dc, bb, pos, &stats.sum_ilp, ilp_sum_offset);
#ifdef FIND_DEAD_EFLAGS
if (!dead_eflags_found)
#endif
dr_restore_arith_flags(dc, bb, pos, SPILL_SLOT_1);
#endif
return DR_EMIT_DEFAULT;
}