-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimplify.c
2823 lines (2594 loc) · 69.9 KB
/
simplify.c
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Simplify - do instruction simplification before CSE
*
* Copyright (C) 2004 Linus Torvalds
*/
///
// Instruction simplification
// --------------------------
//
// Notation
// ^^^^^^^^
// The following conventions are used to describe the simplications:
// * Uppercase letters are reserved for constants:
// * `M` for a constant mask,
// * `S` for a constant shift,
// * `N` for a constant number of bits (usually other than a shift),
// * `C` or 'K' for others constants.
// * Lowercase letters `a`, `b`, `x`, `y`, ... are used for non-constants
// or when it doesn't matter if the pseudo is a constant or not.
// * Primes are used if needed to distinguish symbols (`M`, `M'`, ...).
// * Expressions or sub-expressions involving only constants are
// understood to be evaluated.
// * `$mask(N)` is used for `((1 << N) -1)`
// * `$trunc(x, N)` is used for `(x & $mask(N))`
// * Expressions like `(-1 << S)`, `(-1 >> S)` and others formulae are
// understood to be truncated to the size of the current instruction
// (needed, since in general this size is not the same as the one used
// by sparse for the evaluation of arithmetic operations).
// * `TRUNC(x, N)` is used for a truncation *to* a size of `N` bits
// * `ZEXT(x, N)` is used for a zero-extension *from* a size of `N` bits
// * `OP(x, C)` is used to represent some generic operation using a constant,
// including when the constant is implicit (e.g. `TRUNC(x, N)`).
// * `MASK(x, M)` is used to respresent a 'masking' instruction:
// - `AND(x, M)`
// - `LSR(x, S)`, with `M` = (-1 << S)
// - `SHL(x, S)`, with `M` = (-1 >> S)
// - `TRUNC(x, N)`, with `M` = $mask(N)
// - `ZEXT(x, N)`, with `M` = $mask(N)
// * `SHIFT(x, S)` is used for `LSR(x, S)` or `SHL(x, S)`.
#include <assert.h>
#include "parse.h"
#include "expression.h"
#include "linearize.h"
#include "simplify.h"
#include "flow.h"
#include "symbol.h"
#include "flowgraph.h"
///
// Utilities
// ^^^^^^^^^
///
// check if a pseudo is a power of 2
static inline bool is_pow2(pseudo_t src)
{
if (src->type != PSEUDO_VAL)
return false;
return is_power_of_2(src->value);
}
///
// find the trivial parent for a phi-source
static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseudo)
{
/* Can't go upwards if the pseudo is defined in the bb it came from.. */
if (pseudo->type == PSEUDO_REG) {
struct instruction *def = pseudo->def;
if (def->bb == source)
return source;
}
if (bb_list_size(source->children) != 1 || bb_list_size(source->parents) != 1)
return source;
return first_basic_block(source->parents);
}
///
// copy the phi-node's phisrcs into to given array
// @return: 0 if the the list contained the expected
// number of element, a positive number if there was
// more than expected and a negative one if less.
//
// :note: we can't reuse ptr_list_to_array() for the phi-sources
// because any VOIDs in the phi-list must be ignored here
// as in this context they mean 'entry has been removed'.
static int get_phisources(struct instruction *sources[], int nbr, struct instruction *insn)
{
pseudo_t phi;
int i = 0;
assert(insn->opcode == OP_PHI);
FOR_EACH_PTR(insn->phi_list, phi) {
struct instruction *def;
if (phi == VOID)
continue;
if (i >= nbr)
return 1;
def = phi->def;
assert(def->opcode == OP_PHISOURCE);
sources[i++] = def;
} END_FOR_EACH_PTR(phi);
return i - nbr;
}
static int if_convert_phi(struct instruction *insn)
{
struct instruction *array[2];
struct basic_block *parents[2];
struct basic_block *bb, *bb1, *bb2, *source;
struct instruction *br;
pseudo_t p1, p2;
bb = insn->bb;
if (get_phisources(array, 2, insn))
return 0;
if (ptr_list_to_array(bb->parents, parents, 2) != 2)
return 0;
p1 = array[0]->phi_src;
bb1 = array[0]->bb;
p2 = array[1]->phi_src;
bb2 = array[1]->bb;
/* Only try the simple "direct parents" case */
if ((bb1 != parents[0] || bb2 != parents[1]) &&
(bb1 != parents[1] || bb2 != parents[0]))
return 0;
/*
* See if we can find a common source for this..
*/
source = phi_parent(bb1, p1);
if (source != phi_parent(bb2, p2))
return 0;
/*
* Cool. We now know that 'source' is the exclusive
* parent of both phi-nodes, so the exit at the
* end of it fully determines which one it is, and
* we can turn it into a select.
*
* HOWEVER, right now we only handle regular
* conditional branches. No multijumps or computed
* stuff. Verify that here.
*/
br = last_instruction(source->insns);
if (!br || br->opcode != OP_CBR)
return 0;
assert(br->cond);
assert(br->bb_false);
/*
* We're in business. Match up true/false with p1/p2.
*/
if (br->bb_true == bb2 || br->bb_false == bb1) {
pseudo_t p = p1;
p1 = p2;
p2 = p;
}
/*
* OK, we can now replace that last
*
* br cond, a, b
*
* with the sequence
*
* setcc cond
* select pseudo, p1, p2
* br cond, a, b
*
* and remove the phi-node. If it then
* turns out that 'a' or 'b' is entirely
* empty (common case), and now no longer
* a phi-source, we'll be able to simplify
* the conditional branch too.
*/
insert_select(source, br, insn, p1, p2);
kill_instruction(insn);
return REPEAT_CSE;
}
///
// detect trivial phi-nodes
// @insn: the phi-node
// @pseudo: the candidate resulting pseudo (NULL when starting)
// @return: the unique result if the phi-node is trivial, NULL otherwise
//
// A phi-node is trivial if it has a single possible result:
// * all operands are the same
// * the operands are themselves defined by a chain or cycle of phi-nodes
// and the set of all operands involved contains a single value
// not defined by these phi-nodes
//
// Since the result is unique, these phi-nodes can be removed.
static pseudo_t trivial_phi(pseudo_t pseudo, struct instruction *insn, struct pseudo_list **list)
{
pseudo_t target = insn->target;
pseudo_t phi;
add_pseudo(list, target);
FOR_EACH_PTR(insn->phi_list, phi) {
struct instruction *def;
pseudo_t src;
if (phi == VOID)
continue;
def = phi->def;
if (!def->bb)
continue;
src = def->phi_src; // bypass OP_PHISRC & get the real source
if (src == VOID)
continue;
if (src == target)
continue;
if (!pseudo) {
pseudo = src;
continue;
}
if (src == pseudo)
continue;
if (DEF_OPCODE(def, src) == OP_PHI) {
if (pseudo_in_list(*list, src))
continue;
if ((pseudo = trivial_phi(pseudo, def, list)))
continue;
}
return NULL;
} END_FOR_EACH_PTR(phi);
return pseudo ? pseudo : VOID;
}
static int clean_up_phi(struct instruction *insn)
{
struct pseudo_list *list = NULL;
pseudo_t pseudo;
if ((pseudo = trivial_phi(NULL, insn, &list))) {
convert_instruction_target(insn, pseudo);
kill_instruction(insn);
return REPEAT_CSE;
}
return if_convert_phi(insn);
}
static int delete_pseudo_user_list_entry(struct pseudo_user_list **list, pseudo_t *entry, int count)
{
struct pseudo_user *pu;
FOR_EACH_PTR(*list, pu) {
if (pu->userp == entry) {
MARK_CURRENT_DELETED(pu);
if (!--count)
goto out;
}
} END_FOR_EACH_PTR(pu);
assert(count <= 0);
out:
if (pseudo_user_list_empty(*list))
*list = NULL;
return count;
}
static inline void rem_usage(pseudo_t p, pseudo_t *usep, int kill)
{
if (has_use_list(p)) {
delete_pseudo_user_list_entry(&p->users, usep, 1);
if (kill && !p->users && has_definition(p))
kill_instruction(p->def);
}
}
static inline void remove_usage(pseudo_t p, pseudo_t *usep)
{
rem_usage(p, usep, 1);
}
void kill_use(pseudo_t *usep)
{
if (usep) {
pseudo_t p = *usep;
*usep = VOID;
rem_usage(p, usep, 1);
}
}
// Like kill_use() but do not (recursively) kill dead instructions
void remove_use(pseudo_t *usep)
{
pseudo_t p = *usep;
*usep = VOID;
rem_usage(p, usep, 0);
}
static void kill_use_list(struct pseudo_list *list)
{
pseudo_t p;
FOR_EACH_PTR(list, p) {
if (p == VOID)
continue;
kill_use(THIS_ADDRESS(p));
} END_FOR_EACH_PTR(p);
}
static void kill_asm(struct instruction *insn)
{
struct asm_constraint *con;
FOR_EACH_PTR(insn->asm_rules->inputs, con) {
kill_use(&con->pseudo);
} END_FOR_EACH_PTR(con);
}
///
// kill an instruction
// @insn: the instruction to be killed
// @force: if unset, the normal case, the instruction is not killed
// if not free of possible side-effect; if set the instruction
// is unconditionally killed.
//
// The killed instruction is removed from its BB and the usage
// of all its operands are removed. The instruction is also
// marked as killed by setting its ->bb to NULL.
int kill_insn(struct instruction *insn, int force)
{
if (!insn || !insn->bb)
return 0;
switch (insn->opcode) {
case OP_SEL:
case OP_RANGE:
kill_use(&insn->src3);
/* fall through */
case OP_BINARY ... OP_BINCMP_END:
kill_use(&insn->src2);
/* fall through */
case OP_UNOP ... OP_UNOP_END:
case OP_SLICE:
case OP_PHISOURCE:
case OP_SYMADDR:
case OP_CBR:
case OP_SWITCH:
case OP_COMPUTEDGOTO:
kill_use(&insn->src1);
break;
case OP_PHI:
kill_use_list(insn->phi_list);
break;
case OP_CALL:
if (!force) {
/* a "pure" function can be killed too */
struct symbol *fntype = first_symbol(insn->fntypes);
if (!(fntype->ctype.modifiers & MOD_PURE))
return 0;
}
kill_use_list(insn->arguments);
if (insn->func->type == PSEUDO_REG)
kill_use(&insn->func);
break;
case OP_LOAD:
if (!force && insn->is_volatile)
return 0;
kill_use(&insn->src);
break;
case OP_STORE:
if (!force)
return 0;
kill_use(&insn->src);
kill_use(&insn->target);
break;
case OP_ASM:
if (!force)
return 0;
kill_asm(insn);
break;
case OP_ENTRY:
/* ignore */
return 0;
case OP_BR:
case OP_LABEL:
case OP_SETVAL:
case OP_SETFVAL:
default:
break;
}
insn->bb = NULL;
return repeat_phase |= REPEAT_CSE;
}
static inline bool has_target(struct instruction *insn)
{
return opcode_table[insn->opcode].flags & OPF_TARGET;
}
void remove_dead_insns(struct entrypoint *ep)
{
struct basic_block *bb;
FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
struct instruction *insn;
FOR_EACH_PTR_REVERSE(bb->insns, insn) {
if (!insn->bb)
continue;
if (!has_target(insn))
continue;
if (!has_users(insn->target))
kill_instruction(insn);
} END_FOR_EACH_PTR_REVERSE(insn);
} END_FOR_EACH_PTR_REVERSE(bb);
}
static inline int constant(pseudo_t pseudo)
{
return pseudo->type == PSEUDO_VAL;
}
///
// is this same signed value when interpreted with both size?
static inline bool is_signed_constant(long long val, unsigned osize, unsigned nsize)
{
return bits_extend(val, osize, 1) == bits_extend(val, nsize, 1);
}
///
// is @src generated by an instruction with the given opcode and size?
static inline pseudo_t is_same_op(pseudo_t src, int op, unsigned osize)
{
struct instruction *def;
if (src->type != PSEUDO_REG)
return NULL;
def = src->def;
if (def->opcode != op)
return NULL;
if (def->orig_type->bit_size != osize)
return NULL;
return def->src;
}
static bool is_negate_of(pseudo_t p, pseudo_t ref)
{
struct instruction *def;
return (DEF_OPCODE(def, p) == OP_NEG) && (def->src == ref);
}
///
// replace the operand of an instruction
// @insn: the instruction
// @pp: the address of the instruction's operand
// @new: the new value for the operand
// @return: REPEAT_CSE.
static inline int replace_pseudo(struct instruction *insn, pseudo_t *pp, pseudo_t new)
{
pseudo_t old = *pp;
use_pseudo(insn, new, pp);
remove_usage(old, pp);
return REPEAT_CSE;
}
int replace_with_pseudo(struct instruction *insn, pseudo_t pseudo)
{
convert_instruction_target(insn, pseudo);
return kill_instruction(insn);
}
static inline int replace_with_value(struct instruction *insn, long long val)
{
return replace_with_pseudo(insn, value_pseudo(val));
}
///
// replace a binop with an unop
// @insn: the instruction to be replaced
// @op: the instruction's new opcode
// @src: the instruction's new operand
// @return: REPEAT_CSE
static inline int replace_with_unop(struct instruction *insn, int op, pseudo_t src)
{
insn->opcode = op;
replace_pseudo(insn, &insn->src1, src);
remove_usage(insn->src2, &insn->src2);
return REPEAT_CSE;
}
///
// replace rightside's value
// @insn: the instruction to be replaced
// @op: the instruction's new opcode
// @src: the instruction's new operand
// @return: REPEAT_CSE
static inline int replace_binop_value(struct instruction *insn, int op, long long val)
{
insn->opcode = op;
insn->src2 = value_pseudo(val);
return REPEAT_CSE;
}
///
// replace binop's opcode and values
// @insn: the instruction to be replaced
// @op: the instruction's new opcode
// @return: REPEAT_CSE
static inline int replace_binop(struct instruction *insn, int op, pseudo_t *pa, pseudo_t a, pseudo_t *pb, pseudo_t b)
{
pseudo_t olda = *pa;
pseudo_t oldb = *pb;
insn->opcode = op;
use_pseudo(insn, a, pa);
use_pseudo(insn, b, pb);
remove_usage(olda, pa);
remove_usage(oldb, pb);
return REPEAT_CSE;
}
///
// replace the opcode of an instruction
// @return: REPEAT_CSE
static inline int replace_opcode(struct instruction *insn, int op)
{
insn->opcode = op;
return REPEAT_CSE;
}
///
// create an instruction pair OUT(IN(a, b), c)
static int replace_insn_pair(struct instruction *out, int op_out, struct instruction *in, int op_in, pseudo_t a, pseudo_t b, pseudo_t c)
{
pseudo_t old_a = in->src1;
pseudo_t old_b = in->src2;
pseudo_t old_1 = out->src1;
pseudo_t old_2 = out->src2;
use_pseudo(in, a, &in->src1);
use_pseudo(in, b, &in->src2);
use_pseudo(out, in->target, &out->src1);
use_pseudo(out, c, &out->src2);
remove_usage(old_a, &in->src1);
remove_usage(old_b, &in->src2);
remove_usage(old_1, &out->src1);
remove_usage(old_2, &out->src2);
out->opcode = op_out;
in->opcode = op_in;
return REPEAT_CSE;
}
///
// create an instruction pair OUT(IN(a, b), c) with swapped opcodes
static inline int swap_insn(struct instruction *out, struct instruction *in, pseudo_t a, pseudo_t b, pseudo_t c)
{
return replace_insn_pair(out, in->opcode, in, out->opcode, a, b, c);
}
///
// create an instruction pair OUT(SELECT(a, b, c), d)
static int swap_select(struct instruction *out, struct instruction *in, pseudo_t a, pseudo_t b, pseudo_t c, pseudo_t d)
{
use_pseudo(in, c, &in->src3);
swap_insn(out, in, a, b, d);
kill_use(&out->src3);
return REPEAT_CSE;
}
static inline int def_opcode(pseudo_t p)
{
if (p->type != PSEUDO_REG)
return OP_BADOP;
return p->def->opcode;
}
static unsigned int value_size(long long value)
{
value >>= 8;
if (!value)
return 8;
value >>= 8;
if (!value)
return 16;
value >>= 16;
if (!value)
return 32;
return 64;
}
///
// try to determine the maximum size of bits in a pseudo
//
// Right now this only follow casts and constant values, but we
// could look at things like AND instructions, etc.
static unsigned int operand_size(struct instruction *insn, pseudo_t pseudo)
{
unsigned int size = insn->size;
if (pseudo->type == PSEUDO_REG) {
struct instruction *src = pseudo->def;
if (src && src->opcode == OP_ZEXT && src->orig_type) {
unsigned int orig_size = src->orig_type->bit_size;
if (orig_size < size)
size = orig_size;
}
}
if (pseudo->type == PSEUDO_VAL) {
unsigned int orig_size = value_size(pseudo->value);
if (orig_size < size)
size = orig_size;
}
return size;
}
static pseudo_t eval_op(int op, unsigned size, pseudo_t src1, pseudo_t src2)
{
/* FIXME! Verify signs and sizes!! */
long long left = src1->value;
long long right = src2->value;
unsigned long long ul, ur;
long long res, mask, bits;
mask = 1ULL << (size-1);
bits = mask | (mask-1);
if (left & mask)
left |= ~bits;
if (right & mask)
right |= ~bits;
ul = left & bits;
ur = right & bits;
switch (op) {
case OP_NEG:
res = -left;
break;
case OP_NOT:
res = ~ul;
break;
case OP_ADD:
res = left + right;
break;
case OP_SUB:
res = left - right;
break;
case OP_MUL:
res = ul * ur;
break;
case OP_DIVU:
if (!ur)
goto undef;
res = ul / ur;
break;
case OP_DIVS:
if (!right)
goto undef;
if (left == mask && right == -1)
goto undef;
res = left / right;
break;
case OP_MODU:
if (!ur)
goto undef;
res = ul % ur;
break;
case OP_MODS:
if (!right)
goto undef;
if (left == mask && right == -1)
goto undef;
res = left % right;
break;
case OP_SHL:
if (ur >= size)
goto undef;
res = left << right;
break;
case OP_LSR:
if (ur >= size)
goto undef;
res = ul >> ur;
break;
case OP_ASR:
if (ur >= size)
goto undef;
res = left >> right;
break;
/* Logical */
case OP_AND:
res = left & right;
break;
case OP_OR:
res = left | right;
break;
case OP_XOR:
res = left ^ right;
break;
/* Binary comparison */
case OP_SET_EQ:
res = left == right;
break;
case OP_SET_NE:
res = left != right;
break;
case OP_SET_LE:
res = left <= right;
break;
case OP_SET_GE:
res = left >= right;
break;
case OP_SET_LT:
res = left < right;
break;
case OP_SET_GT:
res = left > right;
break;
case OP_SET_B:
res = ul < ur;
break;
case OP_SET_A:
res = ul > ur;
break;
case OP_SET_BE:
res = ul <= ur;
break;
case OP_SET_AE:
res = ul >= ur;
break;
default:
return NULL;
}
// Warning: this should be done with the output size which may
// be different than the input size used here. But it differs
// only for compares which are not concerned since only returning
// 0 or 1 and for casts which are not handled here.
res &= bits;
return value_pseudo(res);
undef:
return NULL;
}
static inline pseudo_t eval_unop(int op, unsigned size, pseudo_t src)
{
return eval_op(op, size, src, VOID);
}
///
// Simplifications
// ^^^^^^^^^^^^^^^
///
// try to simplify MASK(OR(AND(x, M'), b), M)
// @insn: the masking instruction
// @mask: the associated mask (M)
// @ora: one of the OR's operands, guaranteed to be PSEUDO_REG
// @orb: the other OR's operand
// @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
static int simplify_mask_or_and(struct instruction *insn, unsigned long long mask,
pseudo_t ora, pseudo_t orb)
{
unsigned long long omask, nmask;
struct instruction *and = ora->def;
pseudo_t src2 = and->src2;
if (and->opcode != OP_AND)
return 0;
if (!constant(src2))
return 0;
omask = src2->value;
nmask = omask & mask;
if (nmask == 0) {
// if (M' & M) == 0: ((a & M') | b) -> b
return replace_pseudo(insn, &insn->src1, orb);
}
if (!one_use(insn->src1))
return 0; // can't modify anything inside the OR
if (nmask == mask) {
struct instruction *or = insn->src1->def;
pseudo_t *arg = (ora == or->src1) ? &or->src1 : &or->src2;
// if (M' & M) == M: ((a & M') | b) -> (a | b)
return replace_pseudo(or, arg, and->src1);
}
if (nmask != omask && one_use(ora)) {
// if (M' & M) != M': AND(a, M') -> AND(a, (M' & M))
and->src2 = value_pseudo(nmask);
return REPEAT_CSE;
}
return 0;
}
///
// try to simplify MASK(OR(a, b), M)
// @insn: the masking instruction
// @mask: the associated mask (M)
// @or: the OR instruction
// @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
static int simplify_mask_or(struct instruction *insn, unsigned long long mask, struct instruction *or)
{
pseudo_t src1 = or->src1;
pseudo_t src2 = or->src2;
int rc;
if (src1->type == PSEUDO_REG) {
if ((rc = simplify_mask_or_and(insn, mask, src1, src2)))
return rc;
}
if (src2->type == PSEUDO_REG) {
if ((rc = simplify_mask_or_and(insn, mask, src2, src1)))
return rc;
} else if (src2->type == PSEUDO_VAL) {
unsigned long long oval = src2->value;
unsigned long long nval = oval & mask;
// Try to simplify:
// MASK(OR(x, C), M)
if (nval == 0) {
// if (C & M) == 0: OR(x, C) -> x
return replace_pseudo(insn, &insn->src1, src1);
}
if (nval == mask) {
// if (C & M) == M: OR(x, C) -> M
return replace_pseudo(insn, &insn->src1, value_pseudo(mask));
}
if (nval != oval && one_use(or->target)) {
// if (C & M) != C: OR(x, C) -> OR(x, (C & M))
return replace_pseudo(or, &or->src2, value_pseudo(nval));
}
}
return 0;
}
///
// try to simplify MASK(SHIFT(OR(a, b), S), M)
// @sh: the shift instruction
// @or: the OR instruction
// @mask: the mask associated to MASK (M):
// @return: 0 if no changes have been made, one or more REPEAT_* flags otherwise.
static int simplify_mask_shift_or(struct instruction *sh, struct instruction *or, unsigned long long mask)
{
unsigned long long smask = bits_mask(sh->size);
int shift = sh->src2->value;
if (sh->opcode == OP_LSR)
mask <<= shift;
else
mask >>= shift;
return simplify_mask_or(sh, smask & mask, or);
}
static int simplify_mask_shift(struct instruction *sh, unsigned long long mask)
{
struct instruction *inner;
if (!constant(sh->src2) || sh->tainted)
return 0;
switch (DEF_OPCODE(inner, sh->src1)) {
case OP_OR:
if (one_use(sh->target))
return simplify_mask_shift_or(sh, inner, mask);
break;
}
return 0;
}
static pseudo_t eval_insn(struct instruction *insn)
{
unsigned size = insn->size;
if (opcode_table[insn->opcode].flags & OPF_COMPARE)
size = insn->itype->bit_size;
return eval_op(insn->opcode, size, insn->src1, insn->src2);
}
static long long check_shift_count(struct instruction *insn, unsigned long long uval)
{
unsigned int size = insn->size;
long long sval = uval;
if (insn->tainted)
return -1;
if (uval < size)
return uval;
insn->tainted = 1;
sval = sign_extend_safe(sval, size);
sval = sign_extend_safe(sval, bits_in_int);
if (sval < 0)
insn->src2 = value_pseudo(sval);
return -1;
}
static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long value)
{
struct instruction *def;
unsigned long long mask, omask, nmask;
unsigned long long nval;
unsigned int size;
pseudo_t src2;
if (!value)
return replace_with_pseudo(insn, pseudo);
value = check_shift_count(insn, value);
if (value < 0)
return 0;
size = insn->size;
switch (insn->opcode) {
case OP_ASR:
if (value >= size)
return 0;
if (pseudo->type != PSEUDO_REG)
break;
def = pseudo->def;
switch (def->opcode) {
case OP_LSR:
case OP_ASR:
if (def == insn) // cyclic DAG!
break;
src2 = def->src2;
if (src2->type != PSEUDO_VAL)
break;
nval = src2->value;
if (nval > insn->size || nval == 0)
break;
value += nval;
if (def->opcode == OP_LSR)
insn->opcode = OP_LSR;
else if (value >= size)
value = size - 1;
goto new_value;
case OP_ZEXT:
// transform:
// zext.N %t <- (O) %a
// asr.N %r <- %t, C
// into
// zext.N %t <- (O) %a
// lsr.N %r <- %t, C
insn->opcode = OP_LSR;
return REPEAT_CSE;
}
break;
case OP_LSR:
size = operand_size(insn, pseudo);
if (value >= size)
goto zero;
switch(DEF_OPCODE(def, pseudo)) {
case OP_AND:
// replace (A & M) >> S
// by (A >> S) & (M >> S)
if (!constant(def->src2))
break;
mask = bits_mask(insn->size - value) << value;
omask = def->src2->value;
nmask = omask & mask;
if (nmask == 0)
return replace_with_value(insn, 0);
if (nmask == mask)
return replace_pseudo(insn, &insn->src1, def->src1);
if (!one_use(pseudo))
break;
def->opcode = OP_LSR;
def->src2 = insn->src2;
insn->opcode = OP_AND;
insn->src2 = value_pseudo(omask >> value);
return REPEAT_CSE;
case OP_LSR:
goto case_shift_shift;
case OP_OR:
mask = bits_mask(size);
return simplify_mask_shift_or(insn, def, mask);
case OP_SHL:
// replace ((x << S) >> S)
// by (x & (-1 >> S))
if (def->src2 != insn->src2)
break;
mask = bits_mask(insn->size - value);
goto replace_mask;
}
break;
case OP_SHL: