2 * Simplify - do instruction simplification before CSE
4 * Copyright (C) 2004 Linus Torvalds
10 #include "expression.h"
11 #include "linearize.h"
14 /* Find the trivial parent for a phi-source */
15 static struct basic_block
*phi_parent(struct basic_block
*source
, pseudo_t pseudo
)
17 /* Can't go upwards if the pseudo is defined in the bb it came from.. */
18 if (pseudo
->type
== PSEUDO_REG
) {
19 struct instruction
*def
= pseudo
->def
;
20 if (def
->bb
== source
)
23 if (bb_list_size(source
->children
) != 1 || bb_list_size(source
->parents
) != 1)
25 return first_basic_block(source
->parents
);
28 static void clear_phi(struct instruction
*insn
)
33 FOR_EACH_PTR(insn
->phi_list
, phi
) {
34 *THIS_ADDRESS(phi
) = VOID
;
35 } END_FOR_EACH_PTR(phi
);
38 static int if_convert_phi(struct instruction
*insn
)
41 struct basic_block
*parents
[3];
42 struct basic_block
*bb
, *bb1
, *bb2
, *source
;
43 struct instruction
*br
;
47 if (linearize_ptr_list((struct ptr_list
*)insn
->phi_list
, (void **)array
, 3) != 2)
49 if (linearize_ptr_list((struct ptr_list
*)bb
->parents
, (void **)parents
, 3) != 2)
51 p1
= array
[0]->def
->src1
;
52 bb1
= array
[0]->def
->bb
;
53 p2
= array
[1]->def
->src1
;
54 bb2
= array
[1]->def
->bb
;
56 /* Only try the simple "direct parents" case */
57 if ((bb1
!= parents
[0] || bb2
!= parents
[1]) &&
58 (bb1
!= parents
[1] || bb2
!= parents
[0]))
62 * See if we can find a common source for this..
64 source
= phi_parent(bb1
, p1
);
65 if (source
!= phi_parent(bb2
, p2
))
69 * Cool. We now know that 'source' is the exclusive
70 * parent of both phi-nodes, so the exit at the
71 * end of it fully determines which one it is, and
72 * we can turn it into a select.
74 * HOWEVER, right now we only handle regular
75 * conditional branches. No multijumps or computed
76 * stuff. Verify that here.
78 br
= last_instruction(source
->insns
);
79 if (!br
|| br
->opcode
!= OP_BR
)
86 * We're in business. Match up true/false with p1/p2.
88 if (br
->bb_true
== bb2
|| br
->bb_false
== bb1
) {
95 * Ok, we can now replace that last
102 * select pseudo, p1, p2
105 * and remove the phi-node. If it then
106 * turns out that 'a' or 'b' is entirely
107 * empty (common case), and now no longer
108 * a phi-source, we'll be able to simplify
109 * the conditional branch too.
111 insert_select(source
, br
, insn
, p1
, p2
);
116 static int clean_up_phi(struct instruction
*insn
)
119 struct instruction
*last
;
124 FOR_EACH_PTR(insn
->phi_list
, phi
) {
125 struct instruction
*def
;
129 if (def
->src1
== VOID
|| !def
->bb
)
132 if (last
->src1
!= def
->src1
)
137 } END_FOR_EACH_PTR(phi
);
140 pseudo_t pseudo
= last
? last
->src1
: VOID
;
141 convert_instruction_target(insn
, pseudo
);
146 return if_convert_phi(insn
);
149 static inline void remove_usage(pseudo_t p
, pseudo_t
*usep
)
151 if (has_use_list(p
)) {
152 delete_ptr_list_entry((struct ptr_list
**)&p
->users
, usep
, 1);
154 kill_instruction(p
->def
);
158 void kill_use(pseudo_t
*usep
)
163 remove_usage(p
, usep
);
167 void kill_instruction(struct instruction
*insn
)
169 if (!insn
|| !insn
->bb
)
172 switch (insn
->opcode
) {
173 case OP_BINARY
... OP_BINCMP_END
:
175 kill_use(&insn
->src1
);
176 kill_use(&insn
->src2
);
177 repeat_phase
|= REPEAT_CSE
;
180 case OP_NOT
: case OP_NEG
:
182 kill_use(&insn
->src1
);
183 repeat_phase
|= REPEAT_CSE
;
188 repeat_phase
|= REPEAT_CSE
;
193 repeat_phase
|= REPEAT_CSE
;
195 repeat_phase
|= REPEAT_SYMBOL_CLEANUP
;
201 * Kill trivially dead instructions
203 static int dead_insn(struct instruction
*insn
, pseudo_t
*src1
, pseudo_t
*src2
)
206 FOR_EACH_PTR(insn
->target
->users
, usep
) {
209 } END_FOR_EACH_PTR(usep
);
217 static inline int constant(pseudo_t pseudo
)
219 return pseudo
->type
== PSEUDO_VAL
;
222 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
224 convert_instruction_target(insn
, pseudo
);
229 static int simplify_constant_rightside(struct instruction
*insn
)
231 long long value
= insn
->src2
->value
;
233 switch (insn
->opcode
) {
236 insn
->opcode
= OP_ADD
;
237 insn
->src2
= value_pseudo(-value
);
242 case OP_OR
: case OP_XOR
:
243 case OP_SHL
: case OP_SHR
:
245 return replace_with_pseudo(insn
, insn
->src1
);
248 case OP_AND
: case OP_MUL
:
250 return replace_with_pseudo(insn
, insn
->src2
);
256 static int simplify_constant_leftside(struct instruction
*insn
)
258 long long value
= insn
->src1
->value
;
260 switch (insn
->opcode
) {
261 case OP_ADD
: case OP_OR
: case OP_XOR
:
263 return replace_with_pseudo(insn
, insn
->src2
);
266 case OP_SHL
: case OP_SHR
:
267 case OP_AND
: case OP_MUL
:
269 return replace_with_pseudo(insn
, insn
->src1
);
275 static int simplify_constant_binop(struct instruction
*insn
)
277 /* FIXME! Verify signs and sizes!! */
278 long long left
= insn
->src1
->value
;
279 long long right
= insn
->src2
->value
;
282 switch (insn
->opcode
) {
290 /* FIXME! Check sign! */
296 /* FIXME! Check sign! */
302 /* FIXME! Check sign! */
309 /* FIXME! Check sign! */
329 /* Binary comparison */
337 /* FIXME! Check sign! */
341 /* FIXME! Check sign! */
345 /* FIXME! Check sign! */
349 /* FIXME! Check sign! */
353 /* FIXME! Check sign! */
354 res
= (unsigned long long) left
< (unsigned long long) right
;
357 /* FIXME! Check sign! */
358 res
= (unsigned long long) left
> (unsigned long long) right
;
361 /* FIXME! Check sign! */
362 res
= (unsigned long long) left
<= (unsigned long long) right
;
365 /* FIXME! Check sign! */
366 res
= (unsigned long long) left
>= (unsigned long long) right
;
371 mask
= 1ULL << (insn
->size
-1);
372 res
&= mask
| (mask
-1);
374 /* FIXME!! Sign??? */
375 replace_with_pseudo(insn
, value_pseudo(res
));
379 static int simplify_binop(struct instruction
*insn
)
381 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
))
383 if (constant(insn
->src1
)) {
384 if (constant(insn
->src2
))
385 return simplify_constant_binop(insn
);
386 return simplify_constant_leftside(insn
);
388 if (constant(insn
->src2
))
389 return simplify_constant_rightside(insn
);
393 static int simplify_constant_unop(struct instruction
*insn
)
398 static int simplify_unop(struct instruction
*insn
)
400 if (dead_insn(insn
, &insn
->src1
, NULL
))
402 if (constant(insn
->src1
))
403 return simplify_constant_unop(insn
);
407 static int simplify_memop(struct instruction
*insn
)
409 pseudo_t addr
= insn
->src
;
412 if (addr
->type
== PSEUDO_REG
) {
413 struct instruction
*def
= addr
->def
;
414 if (def
->opcode
== OP_SETVAL
&& def
->src
) {
415 kill_use(&insn
->src
);
416 use_pseudo(def
->src
, &insn
->src
);
417 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
419 if (def
->opcode
== OP_ADD
) {
439 warning(insn
->bb
->pos
, "crazy programmer");
441 insn
->offset
+= off
->value
;
442 use_pseudo(new, &insn
->src
);
443 remove_usage(addr
, &insn
->src
);
444 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
447 static int simplify_cast(struct instruction
*insn
)
451 if (dead_insn(insn
, &insn
->src
, NULL
))
453 if (insn
->opcode
== OP_PTRCAST
)
455 orig_size
= insn
->orig_type
? insn
->orig_type
->bit_size
: 0;
458 if (insn
->size
!= orig_size
)
460 return replace_with_pseudo(insn
, insn
->src
);
463 static int simplify_select(struct instruction
*insn
, struct instruction
*setcc
)
465 pseudo_t cond
, src1
, src2
;
467 assert(setcc
&& setcc
->bb
);
468 if (dead_insn(insn
, &insn
->src1
, &insn
->src2
)) {
475 if (constant(cond
) || src1
== src2
) {
477 kill_use(&setcc
->cond
);
478 replace_with_pseudo(insn
, cond
->value
? src1
: src2
);
481 if (constant(src1
) && constant(src2
)) {
482 long long val1
= src1
->value
;
483 long long val2
= src2
->value
;
485 /* The pair 0/1 is special - replace with SETNE/SETEQ */
486 if ((val1
| val2
) == 1) {
487 int opcode
= OP_SET_EQ
;
492 insn
->opcode
= opcode
;
493 insn
->src2
= src1
; /* Zero */
494 use_pseudo(cond
, &insn
->src1
);
496 kill_use(&setcc
->cond
);
503 static int simplify_branch(struct instruction
*insn
)
505 pseudo_t cond
= insn
->cond
;
506 if (!cond
|| !constant(cond
))
508 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);
512 static int simplify_switch(struct instruction
*insn
)
514 pseudo_t cond
= insn
->cond
;
516 struct multijmp
*jmp
;
520 val
= insn
->cond
->value
;
522 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
524 if (jmp
->begin
> jmp
->end
)
526 if (val
>= jmp
->begin
&& val
<= jmp
->end
)
528 } END_FOR_EACH_PTR(jmp
);
529 warning(insn
->bb
->pos
, "Impossible case statement");
533 insert_branch(insn
->bb
, insn
, jmp
->target
);
537 int simplify_instruction(struct instruction
*insn
)
539 static struct instruction
*last_setcc
;
540 struct instruction
*setcc
= last_setcc
;
546 switch (insn
->opcode
) {
547 case OP_BINARY
... OP_BINCMP_END
:
548 return simplify_binop(insn
);
550 case OP_NOT
: case OP_NEG
:
551 return simplify_unop(insn
);
552 case OP_LOAD
: case OP_STORE
:
553 return simplify_memop(insn
);
555 if (dead_insn(insn
, NULL
, NULL
))
556 return REPEAT_CSE
| REPEAT_SYMBOL_CLEANUP
;
560 return simplify_cast(insn
);
562 if (dead_insn(insn
, NULL
, NULL
)) {
566 return clean_up_phi(insn
);
568 if (dead_insn(insn
, &insn
->src1
, NULL
))
575 return simplify_select(insn
, setcc
);
577 return simplify_branch(insn
);
579 return simplify_switch(insn
);