forked from boegel/MICA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmica_reg.cpp
368 lines (297 loc) · 9.65 KB
/
mica_reg.cpp
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
/*
* This file is part of MICA, a Pin tool to collect
* microarchitecture-independent program characteristics using the Pin
* instrumentation framework.
*
* Please see the README.txt file distributed with the MICA release for more
* information.
*/
#include "pin.H"
/* MICA includes */
#include "mica_reg.h"
/* Global variables */
extern INT64 interval_size;
extern INT64 interval_ins_count;
extern INT64 interval_ins_count_for_hpc_alignment;
extern INT64 total_ins_count;
extern INT64 total_ins_count_for_hpc_alignment;
ofstream output_file_reg;
UINT64* opCounts; // array which keeps track of number-of-operands-per-instruction stats
BOOL* regRef; // register references
INT64* PCTable; // production addresses of registers
INT64* regUseCnt; // usage counters for each register
INT64* regUseDistr; // distribution of register usage
INT64* regAgeDistr; // distribution of register ages
/* initializing */
void init_reg(){
int i;
/* initializing total instruction counts is done in mica.cpp */
/* allocate memory */
opCounts = (UINT64*) checked_malloc(MAX_NUM_OPER * sizeof(UINT64));
regRef = (BOOL*) checked_malloc(MAX_NUM_REGS * sizeof(BOOL));
PCTable = (INT64*) checked_malloc(MAX_NUM_REGS * sizeof(INT64));
regUseCnt = (INT64*) checked_malloc(MAX_NUM_REGS * sizeof(INT64));
regUseDistr = (INT64*) checked_malloc(MAX_REG_USE * sizeof(INT64));
regAgeDistr = (INT64*) checked_malloc(MAX_COMM_DIST * sizeof(INT64));
/* initialize */
for(i = 0; i < MAX_NUM_OPER; i++){
opCounts[i] = 0;
}
for(i = 0; i < MAX_NUM_REGS; i++){
regRef[i] = false;
PCTable[i] = 0;
regUseCnt[i] = 0;
}
for(i = 0; i < MAX_REG_USE; i++){
regUseDistr[i] = 0;
}
for(i = 0; i < MAX_COMM_DIST; i++){
regAgeDistr[i] = 0;
}
if(interval_size != -1){
output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::trunc);
output_file_reg.close();
}
}
/* read register operand */
VOID readRegOp_reg(UINT32 regId){
/* *** REG *** */
/* register age */
INT64 age = total_ins_count - PCTable[regId]; // dependency distance
if(age >= MAX_COMM_DIST){
age = MAX_COMM_DIST - 1; // trim if needed
}
//assert(age >= 0);
regAgeDistr[age]++;
/* register usage */
regUseCnt[regId]++;
regRef[regId] = 1; // (operand) register was referenced
}
VOID writeRegOp_reg(UINT32 regId){
/* *** REG *** */
UINT32 num;
/* if register was referenced before, adjust use distribution */
if(regRef[regId]){
num = regUseCnt[regId];
if(num >= MAX_REG_USE) // trim if needed
num = MAX_REG_USE - 1;
//assert(num >= 0);
regUseDistr[num]++;
}
/* reset register stuff because of new value produced */
PCTable[regId] = total_ins_count; // last production = now
regUseCnt[regId] = 0; // new value is never used (yet)
regRef[regId] = true; // (destination) register was referenced (for tracking use distribution)
}
VOID reg_instr_full(VOID* _e){
/* counting instructions is done in all_instr_full() */
ins_buffer_entry* e = (ins_buffer_entry*)_e;
INT32 i;
for(i=0; i < e->regReadCnt; i++){
readRegOp_reg((UINT32)e->regsRead[i]);
}
for(i=0; i < e->regWriteCnt; i++){
writeRegOp_reg((UINT32)e->regsWritten[i]);
}
opCounts[e->regOpCnt]++;
}
ADDRINT reg_instr_intervals(VOID* _e) {
/* counting instructions is done in all_instr_intervals() */
ins_buffer_entry* e = (ins_buffer_entry*)_e;
INT32 i;
for(i=0; i < e->regReadCnt; i++){
readRegOp_reg((UINT32)e->regsRead[i]);
}
for(i=0; i < e->regWriteCnt; i++){
writeRegOp_reg((UINT32)e->regsWritten[i]);
}
opCounts[e->regOpCnt]++;
return (ADDRINT) (interval_ins_count_for_hpc_alignment == interval_size);
}
VOID reg_instr_interval_output(){
int i;
output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::app);
UINT64 totNumOps = 0;
UINT64 num;
/* total number of operands */
for(i = 1; i < MAX_NUM_OPER; i++){
totNumOps += opCounts[i]*i;
}
output_file_reg << interval_size << " " << totNumOps;
/* average degree of use */
num = 0;
for(i = 0; i < MAX_REG_USE; i++){
num += regUseDistr[i];
}
output_file_reg << " " << num;
num = 0;
for(i = 0; i < MAX_REG_USE; i++){
num += i * regUseDistr[i];
}
output_file_reg << " " << num;
/* register dependency distributions */
num = 0;
for(i = 0; i < MAX_COMM_DIST; i++){
num += regAgeDistr[i];
}
output_file_reg << " " << num;
num = 0;
for(i = 0; i < MAX_COMM_DIST; i++){
num += regAgeDistr[i];
if( (i == 1) || (i == 2) || (i == 4) || (i == 8) || (i == 16) || (i == 32) || (i == 64)){
output_file_reg << " " << num;
}
}
output_file_reg << endl;
output_file_reg.close();
}
VOID reg_instr_interval_reset(){
int i;
for(i = 0; i < MAX_NUM_OPER; i++){
opCounts[i] = 0;
}
/* do NOT reset register use counts or register definition addresses
* that should only be done when the register is written to */
/* for(i = 0; i < MAX_NUM_REGS; i++){
regRef[i] = false;
PCTable[i] = 0;
regUseCnt[i] = 0;
} */
for(i = 0; i < MAX_REG_USE; i++){
regUseDistr[i] = 0;
}
for(i = 0; i < MAX_COMM_DIST; i++){
regAgeDistr[i] = 0;
}
}
VOID reg_instr_interval() {
reg_instr_interval_output();
reg_instr_interval_reset();
interval_ins_count = 0;
interval_ins_count_for_hpc_alignment = 0;
}
VOID instrument_reg(INS ins, ins_buffer_entry* e){
UINT32 i, maxNumRegsProd, maxNumRegsCons, regReadCnt, regWriteCnt, opCnt, regOpCnt;
REG reg;
if(!e->setRead){
maxNumRegsCons = INS_MaxNumRRegs(ins); // maximum number of register consumations (reads)
regReadCnt = 0;
for(i = 0; i < maxNumRegsCons; i++){ // finding all register operands which are read
reg = INS_RegR(ins,i);
//assert(((UINT32)reg) < MAX_NUM_REGS);
/* only consider valid general-purpose registers (any bit-width) and floating-point registers,
* i.e. exlude branch, segment and pin registers, among others */
if(REG_valid(reg) && (REG_is_fr(reg) || REG_is_mm(reg) || REG_is_xmm(reg) || REG_is_gr(reg) || REG_is_gr8(reg) || REG_is_gr16(reg) || REG_is_gr32(reg) || REG_is_gr64(reg))){
regReadCnt++;
}
}
e->regReadCnt = regReadCnt;
e->regsRead = (REG*) checked_malloc(regReadCnt*sizeof(REG));
regReadCnt = 0;
for(i = 0; i < maxNumRegsCons; i++){ // finding all register operands which are read
reg = INS_RegR(ins,i);
//assert(((UINT32)reg) < MAX_NUM_REGS);
/* only consider valid general-purpose registers (any bit-width) and floating-point registers,
* i.e. exlude branch, segment and pin registers, among others */
if(REG_valid(reg) && (REG_is_fr(reg) || REG_is_mm(reg) || REG_is_xmm(reg) || REG_is_gr(reg) || REG_is_gr8(reg) || REG_is_gr16(reg) || REG_is_gr32(reg) || REG_is_gr64(reg))){
e->regsRead[regReadCnt++] = reg;
}
}
e->setRead = true;
}
if(!e->setWritten){
maxNumRegsProd = INS_MaxNumWRegs(ins);
regWriteCnt = 0;
for(i=0; i < maxNumRegsProd; i++){
reg = INS_RegW(ins, i);
//assert(((UINT32)reg) < MAX_NUM_REGS);
/* only consider valid general-purpose registers (any bit-width) and floating-point registers,
* i.e. exlude branch, segment and pin registers, among others */
if(REG_valid(reg) && (REG_is_fr(reg) || REG_is_mm(reg) || REG_is_xmm(reg) || REG_is_gr(reg) || REG_is_gr8(reg) || REG_is_gr16(reg) || REG_is_gr32(reg) || REG_is_gr64(reg))){
regWriteCnt++;
}
}
e->regWriteCnt = regWriteCnt;
e->regsWritten = (REG*)checked_malloc(regWriteCnt*sizeof(REG));
regWriteCnt = 0;
for(i=0; i < maxNumRegsProd; i++){
reg = INS_RegW(ins, i);
//assert(((UINT32)reg) < MAX_NUM_REGS);
/* only consider valid general-purpose registers (any bit-width) and floating-point registers,
* i.e. exlude branch, segment and pin registers, among others */
if(REG_valid(reg) && (REG_is_fr(reg) || REG_is_mm(reg) || REG_is_xmm(reg) || REG_is_gr(reg) || REG_is_gr8(reg) || REG_is_gr16(reg) || REG_is_gr32(reg) || REG_is_gr64(reg))){
e->regsWritten[regWriteCnt++] = reg;
}
}
e->setWritten = true;
}
if(!e->setRegOpCnt){
regOpCnt = 0;
opCnt = INS_OperandCount(ins);
for(i = 0; i < opCnt; i++){
if(INS_OperandIsReg(ins,i))
regOpCnt++;
}
/*if(regOpCnt >= MAX_NUM_OPER){
cerr << "BOOM! -> MAX_NUM_OPER is exceeded! (" << regOpCnt << ")" << endl;
exit(1);
}*/
e->regOpCnt = regOpCnt;
e->setRegOpCnt = true;
}
if(interval_size == -1){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)reg_instr_full, IARG_PTR, (void*)e, IARG_END);
}
else{
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)reg_instr_intervals, IARG_PTR, (void*)e, IARG_END);
/* only called if interval is full */
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR)reg_instr_interval, IARG_END);
}
}
/* finishing... */
VOID fini_reg(INT32 code, VOID* v){
if(interval_size == -1){
output_file_reg.open(mkfilename("reg_full_int"), ios::out|ios::trunc);
//output_file_reg << total_ins_count;
}
else{
output_file_reg.open(mkfilename("reg_phases_int"), ios::out|ios::app);
//output_file_reg << interval_ins_count;
}
int i;
UINT64 totNumOps = 0;
UINT64 num;
/* total number of operands */
for(i = 1; i < MAX_NUM_OPER; i++){
totNumOps += opCounts[i]*i;
}
output_file_reg << totNumOps;
// ** average degree of use **
num = 0;
for(i = 0; i < MAX_REG_USE; i++){
num += regUseDistr[i];
}
output_file_reg << " " << num;
num = 0;
for(i = 0; i < MAX_REG_USE; i++){
num += i * regUseDistr[i];
}
output_file_reg << " " << num;
// ** register dependency distributions **
num = 0;
for(i = 0; i < MAX_COMM_DIST; i++){
num += regAgeDistr[i];
}
output_file_reg << " " << num;
num = 0;
for(i = 0; i < MAX_COMM_DIST; i++){
num += regAgeDistr[i];
if( (i == 1) || (i == 2) || (i == 4) || (i == 8) || (i == 16) || (i == 32) || (i == 64)){
output_file_reg << " " << num;
}
}
output_file_reg << endl;
//output_file_reg << "number of instructions: " << total_ins_count_for_hpc_alignment << endl;
output_file_reg << " ";
output_file_reg.close();
}