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 (phi_parent(bb2
, p2
) != source
)
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
= phi
->def
;
126 if (def
->src1
== VOID
|| !def
->bb
)
129 if (last
->src1
!= def
->src1
)
134 } END_FOR_EACH_PTR(phi
);
137 pseudo_t pseudo
= last
? last
->src1
: VOID
;
138 convert_instruction_target(insn
, pseudo
);
143 return if_convert_phi(insn
);
146 static void try_to_kill(struct instruction
*);
148 static void kill_use(pseudo_t pseudo
)
150 if (pseudo
->type
== PSEUDO_REG
) {
151 if (ptr_list_size((struct ptr_list
*)pseudo
->users
) == 1) {
152 try_to_kill(pseudo
->def
);
157 static void try_to_kill(struct instruction
*insn
)
159 int opcode
= insn
->opcode
;
161 case OP_BINARY
... OP_BINCMP_END
:
163 kill_use(insn
->src1
);
164 kill_use(insn
->src2
);
166 case OP_NOT
: case OP_NEG
:
168 kill_use(insn
->src1
);
171 case OP_PHI
: case OP_SETVAL
:
178 * Kill trivially dead instructions
180 static int dead_insn(struct instruction
*insn
, pseudo_t src1
, pseudo_t src2
)
183 FOR_EACH_PTR(insn
->target
->users
, usep
) {
186 } END_FOR_EACH_PTR(usep
);
194 static inline int constant(pseudo_t pseudo
)
196 return pseudo
->type
== PSEUDO_VAL
;
199 static int replace_with_pseudo(struct instruction
*insn
, pseudo_t pseudo
)
201 convert_instruction_target(insn
, pseudo
);
206 static int simplify_constant_rightside(struct instruction
*insn
)
208 long long value
= insn
->src2
->value
;
210 switch (insn
->opcode
) {
211 case OP_ADD
: case OP_SUB
:
212 case OP_OR
: case OP_XOR
:
213 case OP_SHL
: case OP_SHR
:
215 return replace_with_pseudo(insn
, insn
->src1
);
218 case OP_AND
: case OP_MUL
:
220 return replace_with_pseudo(insn
, insn
->src2
);
226 static int simplify_constant_leftside(struct instruction
*insn
)
231 static int simplify_constant_binop(struct instruction
*insn
)
233 /* FIXME! Verify signs and sizes!! */
234 long long left
= insn
->src1
->value
;
235 long long right
= insn
->src2
->value
;
238 switch (insn
->opcode
) {
246 /* FIXME! Check sign! */
252 /* FIXME! Check sign! */
258 /* FIXME! Check sign! */
265 /* FIXME! Check sign! */
285 /* Binary comparison */
293 /* FIXME! Check sign! */
297 /* FIXME! Check sign! */
301 /* FIXME! Check sign! */
305 /* FIXME! Check sign! */
309 /* FIXME! Check sign! */
310 res
= (unsigned long long) left
< (unsigned long long) right
;
313 /* FIXME! Check sign! */
314 res
= (unsigned long long) left
> (unsigned long long) right
;
317 /* FIXME! Check sign! */
318 res
= (unsigned long long) left
<= (unsigned long long) right
;
321 /* FIXME! Check sign! */
322 res
= (unsigned long long) left
>= (unsigned long long) right
;
327 mask
= 1ULL << (insn
->type
->bit_size
-1);
328 res
&= mask
| (mask
-1);
330 /* FIXME!! Sign??? */
331 replace_with_pseudo(insn
, value_pseudo(res
));
335 static int simplify_binop(struct instruction
*insn
)
337 if (dead_insn(insn
, insn
->src1
, insn
->src2
))
339 if (constant(insn
->src1
)) {
340 if (constant(insn
->src2
))
341 return simplify_constant_binop(insn
);
342 return simplify_constant_leftside(insn
);
344 if (constant(insn
->src2
))
345 return simplify_constant_rightside(insn
);
349 static int simplify_constant_unop(struct instruction
*insn
)
354 static int simplify_unop(struct instruction
*insn
)
356 if (dead_insn(insn
, insn
->src1
, VOID
))
358 if (constant(insn
->src1
))
359 return simplify_constant_unop(insn
);
363 int simplify_instruction(struct instruction
*insn
)
366 static struct instruction
*last_setcc
;
367 struct instruction
*setcc
= last_setcc
;
371 switch (insn
->opcode
) {
372 case OP_BINARY
... OP_BINCMP_END
:
373 return simplify_binop(insn
);
375 case OP_NOT
: case OP_NEG
:
376 return simplify_unop(insn
);
379 if (dead_insn(insn
, VOID
, VOID
))
383 if (dead_insn(insn
, VOID
, VOID
)) {
387 return clean_up_phi(insn
);
389 if (dead_insn(insn
, insn
->src1
, VOID
))
396 assert(setcc
&& setcc
->bb
);
397 if (dead_insn(insn
, insn
->src1
, insn
->src2
)) {
402 if (!constant(cond
) && insn
->src1
!= insn
->src2
)
406 replace_with_pseudo(insn
, cond
->value
? insn
->src1
: insn
->src2
);
410 if (!cond
|| !constant(cond
))
412 insert_branch(insn
->bb
, insn
, cond
->value
? insn
->bb_true
: insn
->bb_false
);