-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtage.cpp
More file actions
670 lines (591 loc) · 22.5 KB
/
vtage.cpp
File metadata and controls
670 lines (591 loc) · 22.5 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "pin.H"
#include <functional>
using std::string;
using std::endl;
using std::cerr;
using std::size_t;
std::ostream * out = &cerr;
// KNOBs
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"outfile", "tool.out", "Output file for the pintool");
KNOB<BOOL> KnobPid(KNOB_MODE_WRITEONCE, "pintool",
"pid", "0", "Append pid to output");
KNOB<UINT64> KnobInstLimit(KNOB_MODE_WRITEONCE, "pintool",
"inst_limit", "0", "Limit of instructions analyzed");
// GLOBALS
enum InsType {unsupported_ins, fp_add, fp_sub, fp_mul, fp_div, int_mul, int_div, load};
enum PredictionStatus {incorrect=0, correct=1};
UINT64 count_correct = 0;
UINT64 count_seen = 0;
UINT64 count_fp_add_corr = 0;
UINT64 count_fp_add_seen = 0;
UINT64 count_fp_sub_corr = 0;
UINT64 count_fp_sub_seen = 0;
UINT64 count_fp_mul_corr = 0;
UINT64 count_fp_mul_seen = 0;
UINT64 count_fp_div_corr = 0;
UINT64 count_fp_div_seen = 0;
UINT64 count_int_mul_corr = 0;
UINT64 count_int_mul_seen = 0;
UINT64 count_int_div_corr = 0;
UINT64 count_int_div_seen = 0;
UINT64 count_load_corr = 0;
UINT64 count_load_seen = 0;
// Bits tracking global history. Most recent history at at rightmost bit
UINT64 history = 0;
const int lvp_size = 8192;
const int counter_max = 7; // 3 bit saturating counter
const int forward_transition_probabilities[] = {1, 16, 16, 16, 16, 32, 32};
struct lvp_entry {
bool is_large_value;
union {
ADDRINT non_large_value;
PIN_REGISTER large_value;
} u;
UINT8 counter;
};
lvp_entry* lvp_table;
void lvp_table_init() {
lvp_table = new lvp_entry[lvp_size];
for (int table_entry = 0; table_entry < lvp_size; table_entry++) {
lvp_table[table_entry].is_large_value = false;
lvp_table[table_entry].u.non_large_value = 0;
lvp_table[table_entry].counter = 0;
}
}
const int vt_table_size = 1024;
const int num_vt_tables = 6;
struct vt_entry {
bool is_large_value;
union {
ADDRINT non_large_value;
PIN_REGISTER large_value;
} u;
UINT8 counter;
ADDRINT tag;
// Usefulness counter
bool useful;
};
// Array of pointers, each pointer is to a vt_table, ordered according to their
// rank (0th table is first rank, 1st table is second rank, etc.)
vt_entry* vt_tables[num_vt_tables];
void vt_tables_init() {
for (int i = 0; i < num_vt_tables; i++) {
vt_tables[i] = new vt_entry[vt_table_size];
}
for (int table_index = 0; table_index < num_vt_tables; table_index++) {
for (int table_entry = 0; table_entry < vt_table_size; table_entry++) {
vt_tables[table_index][table_entry].is_large_value = false;
vt_tables[table_index][table_entry].u.non_large_value = 0;
vt_tables[table_index][table_entry].counter = 0;
vt_tables[table_index][table_entry].tag = 0;
vt_tables[table_index][table_entry].useful = false;
}
}
}
// For non-Load instructions we need to use their Opcodes (no built-in functions)
// Found using https://www.felixcloutier.com/x86/ and https://intelxed.github.io/ref-manual/xed-iclass-enum_8h.html
bool is_fp_add(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_ADDPD // Add Packed Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_ADDPS // Add Packed Single-Precision Floating-Point Values
|| opcode == XED_ICLASS_ADDSD // Add Scalar Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_ADDSS // Add Scalar Single-Precision Floating-Point Values
) {
return true;
}
return false;
}
bool is_fp_sub(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_SUBPD // Subtract Packed Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_SUBPS // Subtract Packed Single-Precision Floating-Point Values
|| opcode == XED_ICLASS_SUBSD // Subtract Scalar Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_SUBSS // Subtract Scalar Single-Precision Floating-Point Values
) {
return true;
}
return false;
}
bool is_fp_mul(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_MULPD // Multiply Packed Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_MULPS // Multiply Packed Single-Precision Floating-Point Values
|| opcode == XED_ICLASS_MULSD // Multiply Scalar Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_MULSS // Multiply Scalar Single-Precision Floating-Point Values
) {
return true;
}
return false;
}
bool is_fp_div(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_DIVPD // Divide Packed Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_DIVPS // Divide Packed Single-Precision Floating-Point Values
|| opcode == XED_ICLASS_DIVSD // Divide Scalar Double-Precision Floating-Point Values
|| opcode == XED_ICLASS_DIVSS // Divide Scalar Single-Precision Floating-Point Values
) {
return true;
}
return false;
}
bool is_int_mul(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_MUL // Unsigned divide
|| opcode == XED_ICLASS_IMUL // Signed divide
) {
return true;
}
return false;
}
bool is_int_div(INS ins) {
OPCODE opcode = INS_Opcode(ins);
if (opcode == XED_ICLASS_DIV // Unsigned divide
|| opcode == XED_ICLASS_IDIV // Signed divide
) {
return true;
}
return false;
}
InsType get_ins_type(INS ins) {
if (is_fp_add(ins)) {
return fp_add;
}
if (is_fp_sub(ins)) {
return fp_sub;
}
if (is_fp_mul(ins)) {
return fp_mul;
}
if (is_fp_div(ins)) {
return fp_div;
}
if (is_int_mul(ins)) {
return int_mul;
}
if (is_int_div(ins)) {
return int_div;
}
if (INS_IsMemoryRead(ins)) {
return load;
}
return unsupported_ins;
}
void update_seen_count(UINT64 ins_type) {
switch(ins_type) {
case fp_add : count_fp_add_seen++; break;
case fp_sub : count_fp_sub_seen++; break;
case fp_mul : count_fp_mul_seen++; break;
case fp_div : count_fp_div_seen++; break;
case int_mul : count_int_mul_seen++; break;
case int_div : count_int_div_seen++; break;
case load : count_load_seen++; break;
}
}
void add_correct(UINT64 ins_type) {
count_correct++;
switch(ins_type) {
case fp_add : count_fp_add_corr++; break;
case fp_sub : count_fp_sub_corr++; break;
case fp_mul : count_fp_mul_corr++; break;
case fp_div : count_fp_div_corr++; break;
case int_mul : count_int_mul_corr++; break;
case int_div : count_int_div_corr++; break;
case load : count_load_corr++; break;
}
}
void PrintResults(bool limit_reached) {
string output_file = KnobOutputFile.Value();
if(KnobPid.Value()) output_file += "." + getpid();
if (!output_file.empty()) { out = new std::ofstream(output_file.c_str());}
if(limit_reached) {
*out << "Reason: limit reached\n";
}
else {
*out << "Reason: fini\n";
}
*out << count_seen << endl;
*out << count_correct << endl;
*out << count_fp_add_seen << endl;
*out << count_fp_add_corr << endl;
*out << count_fp_sub_seen << endl;
*out << count_fp_sub_corr << endl;
*out << count_fp_mul_seen << endl;
*out << count_fp_mul_corr << endl;
*out << count_fp_div_seen << endl;
*out << count_fp_div_corr << endl;
*out << count_int_mul_seen << endl;
*out << count_int_mul_corr << endl;
*out << count_int_div_seen << endl;
*out << count_int_div_corr << endl;
*out << count_load_seen << endl;
*out << count_load_corr << endl;
}
UINT64 getRelevantHistoryBits(UINT64 table_index) {
// Handle possible overflow
if (table_index == 5) {
return history & UINT64(-1);
}
UINT64 num_bits = 1UL << (table_index + 1UL);
return history & ((1UL << num_bits) - 1UL);
}
void update_vt_c_and_u(int table_index, size_t vt_index, PredictionStatus prediction_status) {
if (prediction_status == correct) {
UINT8 counter_val = vt_tables[table_index][vt_index].counter;
if (counter_val< counter_max) {
int rand_val = rand() % forward_transition_probabilities[counter_val];
if (rand_val == 0) {
vt_tables[table_index][vt_index].counter++;
}
}
vt_tables[table_index][vt_index].useful = true;
}
else {
if (vt_tables[table_index][vt_index].counter > 0) {
vt_tables[table_index][vt_index].counter--;
}
vt_tables[table_index][vt_index].useful = false;
}
}
void update_lvp_c(ADDRINT ins_ptr, PredictionStatus prediction_status) {
size_t lvp_index = (std::hash<unsigned long long>{}(ins_ptr) % lvp_size);
if (prediction_status == correct) {
UINT8 counter_val = lvp_table[lvp_index].counter;
if (counter_val < counter_max) {
int rand_val = rand() % forward_transition_probabilities[counter_val];
if (rand_val == 0) {
lvp_table[lvp_index].counter++;
}
}
}
else {
if (lvp_table[lvp_index].counter > 0) {
lvp_table[lvp_index].counter--;
}
}
}
size_t compute_vt_index (ADDRINT ins_ptr, int table_index) {
// Compute index into this vt_table
unsigned long long history_bits = getRelevantHistoryBits(table_index);
size_t history_hash = std::hash<unsigned long long>{}(history_bits);
size_t ins_ptr_hash = std::hash<unsigned long long>{}(ins_ptr);
// Szudzik's function
size_t combined = (history_hash >= ins_ptr_hash
? history_hash * history_hash + history_hash + ins_ptr_hash
: history_hash + ins_ptr_hash * ins_ptr_hash);
size_t vt_index = (std::hash<unsigned long long>{}(combined) % vt_table_size);
return vt_index;
}
// Finds a VT above the `provider_index` VT for which ins_ptr hashes to
// a non-useful (useful bit = 0) entry. Returns this VT index or -1 if none
// found
int find_upper_non_useful_vt(ADDRINT ins_ptr, int provider_index) {
std::vector<int> upper_tables;
for (int i = provider_index + 1; i < num_vt_tables; i++) {
upper_tables.push_back(i);
}
std::random_shuffle (upper_tables.begin(), upper_tables.end());
for (std::vector<int>::iterator it = upper_tables.begin() ; it != upper_tables.end(); ++it) {
size_t vt_index = compute_vt_index(ins_ptr, *it);
if (vt_tables[*it][vt_index].useful == false) {
return *it;
}
}
return -1;
}
void reset_upper_vt_usefulness(ADDRINT ins_ptr, int provider_index) {
std::vector<int> upper_tables;
for (int i = provider_index + 1; i < num_vt_tables; i++) {
upper_tables.push_back(i);
}
for (std::vector<int>::iterator it = upper_tables.begin() ; it != upper_tables.end(); ++it) {
size_t vt_index = compute_vt_index(ins_ptr, *it);
vt_tables[*it][vt_index].useful = false;
}
}
void handle_vt_misprediction(ADDRINT ins_ptr, int table_index, size_t vt_index, ADDRINT actual_value) {
if (vt_tables[table_index][vt_index].counter == 0) {
vt_tables[table_index][vt_index].is_large_value = false;
vt_tables[table_index][vt_index].u.non_large_value = actual_value;
}
int new_provider_index = find_upper_non_useful_vt(ins_ptr, table_index);
// Didn't find non-useful entry
if (new_provider_index == -1) {
// Reset the usefulness counter of all matching entries in the upper VT's
reset_upper_vt_usefulness(ins_ptr, table_index);
}
// Found an upper VT for which ins_ptr hashes to a non-useful entry
else {
size_t new_vt_index = compute_vt_index(ins_ptr, new_provider_index);
vt_tables[new_provider_index][new_vt_index].is_large_value = false;
vt_tables[new_provider_index][new_vt_index].u.non_large_value = actual_value;
vt_tables[new_provider_index][new_vt_index].counter = 0;
vt_tables[new_provider_index][new_vt_index].tag = ins_ptr;
vt_tables[new_provider_index][new_vt_index].useful = 0;
}
}
void handle_lvp_misprediction(ADDRINT ins_ptr, ADDRINT actual_value) {
size_t lvp_index = (std::hash<unsigned long long>{}(ins_ptr) % lvp_size);
if (lvp_table[lvp_index].counter == 0) {
lvp_table[lvp_index].is_large_value = false;
lvp_table[lvp_index].u.non_large_value = actual_value;
}
int new_provider_index = find_upper_non_useful_vt(ins_ptr, -1);
// Didn't find non-useful entry
if (new_provider_index == -1) {
// Reset the usefulness counter of all matching entries in the upper VT's
reset_upper_vt_usefulness(ins_ptr, -1);
}
// Found an upper VT for which ins_ptr hashes to a non-useful entry
else {
size_t new_vt_index = compute_vt_index(ins_ptr, new_provider_index);
vt_tables[new_provider_index][new_vt_index].is_large_value = false;
vt_tables[new_provider_index][new_vt_index].u.non_large_value = actual_value;
vt_tables[new_provider_index][new_vt_index].counter = 0;
vt_tables[new_provider_index][new_vt_index].tag = ins_ptr;
vt_tables[new_provider_index][new_vt_index].useful = 0;
}
}
ADDRINT non_large_vt_prediction(ADDRINT ins_ptr, int table_index, size_t vt_index) {
return vt_tables[table_index][vt_index].u.non_large_value;
}
ADDRINT non_large_lvp_prediction(ADDRINT ins_ptr) {
size_t lvp_index = (std::hash<unsigned long long>{}(ins_ptr) % lvp_size);
return lvp_table[lvp_index].u.non_large_value;
}
void predictValNormalReg(ADDRINT ins_ptr, ADDRINT actual_value, UINT64 ins_type) {
update_seen_count(ins_type);
bool found_entry = false;
int table_index;
size_t vt_index;
for (table_index = num_vt_tables - 1; table_index >= 0; table_index--) {
vt_index = compute_vt_index(ins_ptr, table_index);
// Check if this vt entry's tag matches ins_ptr
if (vt_tables[table_index][vt_index].tag == ins_ptr) {
// This vt_table has the longest history match
found_entry = true;
break;
}
}
if (found_entry) {
// Use the `table_index` vt_table to predict
if (non_large_vt_prediction(ins_ptr, table_index, vt_index) == actual_value) {
add_correct(ins_type);
update_vt_c_and_u(table_index, vt_index, correct);
}
else {
update_vt_c_and_u(table_index, vt_index, incorrect);
handle_vt_misprediction(ins_ptr, table_index, vt_index, actual_value);
}
}
else {
// Use the base predictor
if (non_large_lvp_prediction(ins_ptr) == actual_value) {
add_correct(ins_type);
update_lvp_c(ins_ptr, correct);
}
else {
update_lvp_c(ins_ptr, incorrect);
handle_lvp_misprediction(ins_ptr, actual_value);
}
}
if (count_seen == KnobInstLimit.Value()) {
PrintResults(true);
PIN_ExitProcess(EXIT_SUCCESS);
}
}
// For comparing PIN_REGISTERs to each other
bool operator == (const PIN_REGISTER &a, const PIN_REGISTER &b) {
for(unsigned int i=0; i < MAX_DWORDS_PER_PIN_REG; i++) {
if (a.dword[i] != b.dword[i]) {
return false;
}
}
return true;
}
bool operator != (const PIN_REGISTER &a, const PIN_REGISTER &b) {
return !(a==b);
}
PIN_REGISTER large_lvp_prediction(ADDRINT ins_ptr) {
size_t lvp_index = (std::hash<unsigned long long>{}(ins_ptr) % lvp_size);
return lvp_table[lvp_index].u.large_value;
}
PIN_REGISTER large_vt_prediction(ADDRINT ins_ptr, int table_index, size_t vt_index) {
return vt_tables[table_index][vt_index].u.large_value;
}
void handle_lvp_misprediction_large(ADDRINT ins_ptr, PIN_REGISTER actual_value) {
size_t lvp_index = (std::hash<unsigned long long>{}(ins_ptr) % lvp_size);
if (lvp_table[lvp_index].counter == 0) {
lvp_table[lvp_index].is_large_value = true;
lvp_table[lvp_index].u.large_value = actual_value;
}
int new_provider_index = find_upper_non_useful_vt(ins_ptr, -1);
// Didn't find non-useful entry
if (new_provider_index == -1) {
// Reset the usefulness counter of all matching entries in the upper VT's
reset_upper_vt_usefulness(ins_ptr, -1);
}
// Found an upper VT for which ins_ptr hashes to a non-useful entry
else {
size_t new_vt_index = compute_vt_index(ins_ptr, new_provider_index);
vt_tables[new_provider_index][new_vt_index].is_large_value = true;
vt_tables[new_provider_index][new_vt_index].u.large_value = actual_value;
vt_tables[new_provider_index][new_vt_index].counter = 0;
vt_tables[new_provider_index][new_vt_index].tag = ins_ptr;
vt_tables[new_provider_index][new_vt_index].useful = 0;
}
}
void handle_vt_misprediction_large(ADDRINT ins_ptr, int table_index, size_t vt_index, PIN_REGISTER actual_value) {
if (vt_tables[table_index][vt_index].counter == 0) {
vt_tables[table_index][vt_index].is_large_value = true;
vt_tables[table_index][vt_index].u.large_value = actual_value;
}
int new_provider_index = find_upper_non_useful_vt(ins_ptr, table_index);
// Didn't find non-useful entry
if (new_provider_index == -1) {
// Reset the usefulness counter of all matching entries in the upper VT's
reset_upper_vt_usefulness(ins_ptr, table_index);
}
// Found an upper VT for which ins_ptr hashes to a non-useful entry
else {
size_t new_vt_index = compute_vt_index(ins_ptr, new_provider_index);
vt_tables[new_provider_index][new_vt_index].is_large_value = true;
vt_tables[new_provider_index][new_vt_index].u.large_value = actual_value;
vt_tables[new_provider_index][new_vt_index].counter = 0;
vt_tables[new_provider_index][new_vt_index].tag = ins_ptr;
vt_tables[new_provider_index][new_vt_index].useful = 0;
}
}
void predictValLargeReg(ADDRINT ins_ptr, const CONTEXT* context, REG dest_reg, UINT64 ins_type) {
update_seen_count(ins_type);
PIN_REGISTER actual_value;
PIN_GetContextRegval(context, dest_reg, reinterpret_cast<UINT8*>(&actual_value));
bool found_entry = false;
int table_index;
size_t vt_index;
for (table_index = num_vt_tables - 1; table_index >= 0; table_index--) {
vt_index = compute_vt_index(ins_ptr, table_index);
// Check if this vt entry's tag matches ins_ptr
if (vt_tables[table_index][vt_index].tag == ins_ptr) {
// This vt_table has the longest history match
found_entry = true;
break;
}
}
if (found_entry) {
// Use the `table_index` vt_table to predict
if (large_vt_prediction(ins_ptr, table_index, vt_index) == actual_value) {
add_correct(ins_type);
update_vt_c_and_u(table_index, vt_index, correct);
}
else {
update_vt_c_and_u(table_index, vt_index, incorrect);
handle_vt_misprediction_large(ins_ptr, table_index, vt_index, actual_value);
}
}
else {
// Use the base predictor
if (large_lvp_prediction(ins_ptr) == actual_value) {
add_correct(ins_type);
update_lvp_c(ins_ptr, correct);
}
else {
update_lvp_c(ins_ptr, incorrect);
handle_lvp_misprediction_large(ins_ptr, actual_value);
}
}
if (count_seen == KnobInstLimit.Value()) {
PrintResults(true);
PIN_ExitProcess(EXIT_SUCCESS);
}
}
static INT32 Usage() {
cerr << "This pin tool collects a profile of the Lipasti value predictor\n";
cerr << KNOB_BASE::StringKnobSummary();
cerr << endl;
return -1;
}
void docount() {
count_seen++;
}
void updateBranchHistory(INT32 taken) {
history <<= 1;
history |= taken;
}
void Instruction(INS ins, void *v) {
// Increment count seen
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) docount, IARG_END);
InsType ins_type = get_ins_type(ins);
if (ins_type != unsupported_ins) {
// Second operand is our dest reg; if it isn't we don't yet support it
if (!INS_OperandIsReg(ins,0)) {
*out << INS_Disassemble(ins) << endl;
return;
}
// First register in a multiply instruction is dest reg
REG dest_reg = INS_OperandReg(ins,0);
assert(dest_reg);
// The intstruction uses larger registers
if (REG_is_xmm(dest_reg) || REG_is_st(dest_reg)) {
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR) predictValLargeReg,
IARG_INST_PTR, IARG_CONST_CONTEXT, IARG_UINT64, dest_reg, IARG_UINT64, ins_type, IARG_END);
}
else {
INS_InsertCall(ins, IPOINT_AFTER, (AFUNPTR) predictValNormalReg,
IARG_INST_PTR, IARG_REG_VALUE, dest_reg, IARG_UINT64, ins_type, IARG_END);
}
}
// Keep track of branch history
if( INS_IsRet(ins) )
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
else if( INS_IsSyscall(ins) )
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
else if (INS_IsDirectBranch(ins) or INS_IsDirectCall(ins))
{
if( INS_IsCall(ins) ) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
else {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
}
else if( INS_IsBranch(ins) or INS_IsCall(ins) ) //"and INS_IsDirectCall/Branch(ins)" would be redundant
{
if( INS_IsCall(ins) ) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
else {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR) updateBranchHistory,
IARG_BRANCH_TAKEN, IARG_END);
}
}
}
/* ===================================================================== */
void fini(int n, void *v) {
PrintResults(false);
}
/* ===================================================================== */
int main(int argc, char *argv[]) {
if( PIN_Init(argc,argv) ) {
return Usage();
}
vt_tables_init();
lvp_table_init();
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(fini, 0);
PIN_StartProgram();
return 0;
}