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 FOR_EACH_PTR(bb
->parents
, parent
) {
26 struct instruction
*one
;
27 struct instruction
*br
;
30 FOR_EACH_PTR_REVERSE(parent
->insns
, one
) {
34 dominance
= dominates(pseudo
, insn
, one
, local
);
36 if (one
->opcode
== OP_LOAD
)
43 } END_FOR_EACH_PTR_REVERSE(one
);
45 if (parent
->generation
== generation
)
47 parent
->generation
= generation
;
49 if (!find_dominating_parents(pseudo
, insn
, parent
, generation
, dominators
, local
))
54 br
= delete_last_instruction(&parent
->insns
);
55 phi
= alloc_phi(parent
, one
->target
, one
->size
);
56 phi
->ident
= phi
->ident
? : one
->target
->ident
;
57 add_instruction(&parent
->insns
, br
);
58 use_pseudo(insn
, phi
, add_pseudo(dominators
, phi
));
59 } END_FOR_EACH_PTR(parent
);
63 static int address_taken(pseudo_t pseudo
)
65 struct pseudo_user
*pu
;
66 FOR_EACH_PTR(pseudo
->users
, pu
) {
67 struct instruction
*insn
= pu
->insn
;
68 if (insn
->bb
&& (insn
->opcode
!= OP_LOAD
&& insn
->opcode
!= OP_STORE
))
70 } END_FOR_EACH_PTR(pu
);
74 static int local_pseudo(pseudo_t pseudo
)
76 return pseudo
->type
== PSEUDO_SYM
77 && !(pseudo
->sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_NONLOCAL
))
78 && !address_taken(pseudo
);
81 static void simplify_loads(struct basic_block
*bb
)
83 struct instruction
*insn
;
85 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
88 if (insn
->opcode
== OP_LOAD
) {
89 struct instruction
*dom
;
90 pseudo_t pseudo
= insn
->src
;
91 int local
= local_pseudo(pseudo
);
92 struct pseudo_list
*dominators
;
93 unsigned long generation
;
95 /* Check for illegal offsets.. */
98 if (insn
->type
->ctype
.modifiers
& MOD_VOLATILE
)
101 RECURSE_PTR_REVERSE(insn
, dom
) {
105 dominance
= dominates(pseudo
, insn
, dom
, local
);
107 /* possible partial dominance? */
109 if (dom
->opcode
== OP_LOAD
)
113 /* Yeehaa! Found one! */
114 convert_load_instruction(insn
, dom
->target
);
117 } END_FOR_EACH_PTR_REVERSE(dom
);
119 /* OK, go find the parents */
120 generation
= ++bb_generation
;
121 bb
->generation
= generation
;
123 if (find_dominating_parents(pseudo
, insn
, bb
, generation
, &dominators
, local
)) {
124 /* This happens with initial assignments to structures etc.. */
127 assert(pseudo
->type
!= PSEUDO_ARG
);
128 convert_load_instruction(insn
, value_pseudo(0));
132 rewrite_load_instruction(insn
, dominators
);
136 /* Do the next one */;
137 } END_FOR_EACH_PTR_REVERSE(insn
);
140 static void kill_store(struct instruction
*insn
)
144 insn
->opcode
= OP_SNOP
;
145 kill_use(&insn
->target
);
149 static void kill_dominated_stores(struct basic_block
*bb
)
151 struct instruction
*insn
;
153 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
156 if (insn
->opcode
== OP_STORE
) {
157 struct instruction
*dom
;
158 pseudo_t pseudo
= insn
->src
;
159 int local
= local_pseudo(pseudo
);
161 RECURSE_PTR_REVERSE(insn
, dom
) {
165 dominance
= dominates(pseudo
, insn
, dom
, local
);
167 /* possible partial dominance? */
170 if (dom
->opcode
== OP_LOAD
)
172 /* Yeehaa! Found one! */
175 } END_FOR_EACH_PTR_REVERSE(dom
);
177 /* OK, we should check the parents now */
180 /* Do the next one */;
181 } END_FOR_EACH_PTR_REVERSE(insn
);
184 void simplify_memops(struct entrypoint
*ep
)
186 struct basic_block
*bb
;
188 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
190 } END_FOR_EACH_PTR_REVERSE(bb
);
192 FOR_EACH_PTR_REVERSE(ep
->bbs
, bb
) {
193 kill_dominated_stores(bb
);
194 } END_FOR_EACH_PTR_REVERSE(bb
);