2 * memops - try to combine memory ops.
4 * Copyright (C) 2004 Linus Torvalds
15 #include "expression.h"
16 #include "linearize.h"
19 static int find_dominating_parents(pseudo_t pseudo
, struct instruction
*insn
,
20 struct basic_block
*bb
, unsigned long generation
, struct pseudo_list
**dominators
,
23 struct basic_block
*parent
;
25 if (bb_list_size(bb
->parents
) > 1)
27 FOR_EACH_PTR(bb
->parents
, parent
) {
28 struct instruction
*one
;
29 struct instruction
*br
;
32 FOR_EACH_PTR_REVERSE(parent
->insns
, one
) {
36 dominance
= dominates(pseudo
, insn
, one
, local
);
38 if (one
->opcode
== OP_LOAD
)
44 if (one
->opcode
== OP_LOAD
&& !loads
)
47 } END_FOR_EACH_PTR_REVERSE(one
);
49 if (parent
->generation
== generation
)
51 parent
->generation
= generation
;
53 if (!find_dominating_parents(pseudo
, insn
, parent
, generation
, dominators
, local
, loads
))
58 br
= delete_last_instruction(&parent
->insns
);
59 phi
= alloc_phi(parent
, one
->target
, one
->size
);
60 phi
->ident
= phi
->ident
? : one
->target
->ident
;
61 add_instruction(&parent
->insns
, br
);
62 use_pseudo(phi
, add_pseudo(dominators
, phi
));
63 } END_FOR_EACH_PTR(parent
);
67 static inline int address_taken(pseudo_t pseudo
)
70 FOR_EACH_PTR(pseudo
->users
, usep
) {
71 struct instruction
*insn
= container(usep
, struct instruction
, src
);
72 if (insn
->bb
&& insn
->opcode
== OP_SETVAL
)
74 } END_FOR_EACH_PTR(usep
);
78 static int local_pseudo(pseudo_t pseudo
)
80 return pseudo
->type
== PSEUDO_SYM
81 && !(pseudo
->sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_NONLOCAL
))
82 && !address_taken(pseudo
);
85 static void simplify_loads(struct basic_block
*bb
)
87 struct instruction
*insn
;
89 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
92 if (insn
->opcode
== OP_LOAD
) {
93 struct instruction
*dom
;
94 pseudo_t pseudo
= insn
->src
;
95 int local
= local_pseudo(pseudo
);
96 struct pseudo_list
*dominators
;
97 unsigned long generation
;
99 RECURSE_PTR_REVERSE(insn
, dom
) {
103 dominance
= dominates(pseudo
, insn
, dom
, local
);
105 /* possible partial dominance? */
107 if (dom
->opcode
== OP_LOAD
)
111 /* Yeehaa! Found one! */
112 convert_load_instruction(insn
, dom
->target
);
115 } END_FOR_EACH_PTR_REVERSE(dom
);
117 /* Ok, go find the parents */
118 generation
= ++bb_generation
;
119 bb
->generation
= generation
;
121 if (find_dominating_parents(pseudo
, insn
, bb
, generation
, &dominators
, local
, 1)) {
122 /* This happens with initial assignments to structures etc.. */
125 assert(pseudo
->type
!= PSEUDO_ARG
);
126 convert_load_instruction(insn
, value_pseudo(0));
130 rewrite_load_instruction(insn
, dominators
);
134 /* Do the next one */;
135 } END_FOR_EACH_PTR_REVERSE(insn
);
138 static void kill_store(struct instruction
*insn
)
142 insn
->opcode
= OP_SNOP
;
143 kill_use(&insn
->target
);
147 static void kill_dominated_stores(struct basic_block
*bb
)
149 struct instruction
*insn
;
151 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
154 if (insn
->opcode
== OP_STORE
) {
155 struct instruction
*dom
;
156 pseudo_t pseudo
= insn
->src
;
157 int local
= local_pseudo(pseudo
);
159 RECURSE_PTR_REVERSE(insn
, dom
) {
163 dominance
= dominates(pseudo
, insn
, dom
, local
);
165 /* possible partial dominance? */
168 if (dom
->opcode
== OP_LOAD
)
170 /* Yeehaa! Found one! */
173 } END_FOR_EACH_PTR_REVERSE(dom
);
175 /* Ok, we should check the parents now */
178 /* Do the next one */;
179 } END_FOR_EACH_PTR_REVERSE(insn
);
182 void simplify_memops(struct entrypoint
*ep
)
184 struct basic_block
*bb
;
186 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
188 } END_FOR_EACH_PTR_REVERSE(bb
);
190 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
191 kill_dominated_stores(bb
);
192 } END_FOR_EACH_PTR_REVERSE(bb
);