Toplevel symbols are externally addressable only if they
[smatch.git] / memops.c
blobb7f94094fb777781badd1776d14b33b6182811e1
1 /*
2 * memops - try to combine memory ops.
4 * Copyright (C) 2004 Linus Torvalds
5 */
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <stddef.h>
12 #include <assert.h>
14 #include "parse.h"
15 #include "expression.h"
16 #include "linearize.h"
17 #include "flow.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,
21 int local, int loads)
23 struct basic_block *parent;
25 if (bb_list_size(bb->parents) > 1)
26 loads = 0;
27 FOR_EACH_PTR(bb->parents, parent) {
28 struct instruction *one;
29 struct instruction *br;
30 pseudo_t phi;
32 FOR_EACH_PTR_REVERSE(parent->insns, one) {
33 int dominance;
34 if (one == insn)
35 goto no_dominance;
36 dominance = dominates(pseudo, insn, one, local);
37 if (dominance < 0) {
38 if (one->opcode == OP_LOAD)
39 continue;
40 return 0;
42 if (!dominance)
43 continue;
44 if (one->opcode == OP_LOAD && !loads)
45 continue;
46 goto found_dominator;
47 } END_FOR_EACH_PTR_REVERSE(one);
48 no_dominance:
49 if (parent->generation == generation)
50 continue;
51 parent->generation = generation;
53 if (!find_dominating_parents(pseudo, insn, parent, generation, dominators, local, loads))
54 return 0;
55 continue;
57 found_dominator:
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);
64 return 1;
68 * FIXME! This is wrong. Since we now distribute out the OP_SYMADDR,
69 * we can no longer really use "container()" to get from a user to
70 * the instruction that uses it.
72 * This happens to work, simply because the likelyhood of the
73 * (possibly non-instruction) containing the right bitpattern
74 * in the right place is pretty low. But this is still wrong.
76 * We should make symbol-pseudos count non-load/store usage,
77 * or something.
79 static int address_taken(pseudo_t pseudo)
81 pseudo_t *usep;
82 FOR_EACH_PTR(pseudo->users, usep) {
83 struct instruction *insn = container(usep, struct instruction, src);
84 if (insn->bb && (insn->opcode != OP_LOAD || insn->opcode != OP_STORE))
85 return 1;
86 } END_FOR_EACH_PTR(usep);
87 return 0;
90 static int local_pseudo(pseudo_t pseudo)
92 return pseudo->type == PSEUDO_SYM
93 && !(pseudo->sym->ctype.modifiers & (MOD_STATIC | MOD_NONLOCAL))
94 && !address_taken(pseudo);
97 static void simplify_loads(struct basic_block *bb)
99 struct instruction *insn;
101 FOR_EACH_PTR_REVERSE(bb->insns, insn) {
102 if (!insn->bb)
103 continue;
104 if (insn->opcode == OP_LOAD) {
105 struct instruction *dom;
106 pseudo_t pseudo = insn->src;
107 int local = local_pseudo(pseudo);
108 struct pseudo_list *dominators;
109 unsigned long generation;
111 RECURSE_PTR_REVERSE(insn, dom) {
112 int dominance;
113 if (!dom->bb)
114 continue;
115 dominance = dominates(pseudo, insn, dom, local);
116 if (dominance) {
117 /* possible partial dominance? */
118 if (dominance < 0) {
119 if (dom->opcode == OP_LOAD)
120 continue;
121 goto next_load;
123 /* Yeehaa! Found one! */
124 convert_load_instruction(insn, dom->target);
125 goto next_load;
127 } END_FOR_EACH_PTR_REVERSE(dom);
129 /* Ok, go find the parents */
130 generation = ++bb_generation;
131 bb->generation = generation;
132 dominators = NULL;
133 if (find_dominating_parents(pseudo, insn, bb, generation, &dominators, local, 1)) {
134 /* This happens with initial assignments to structures etc.. */
135 if (!dominators) {
136 if (local) {
137 assert(pseudo->type != PSEUDO_ARG);
138 convert_load_instruction(insn, value_pseudo(0));
140 goto next_load;
142 rewrite_load_instruction(insn, dominators);
145 next_load:
146 /* Do the next one */;
147 } END_FOR_EACH_PTR_REVERSE(insn);
150 static void kill_store(struct instruction *insn)
152 if (insn) {
153 insn->bb = NULL;
154 insn->opcode = OP_SNOP;
155 kill_use(&insn->target);
159 static void kill_dominated_stores(struct basic_block *bb)
161 struct instruction *insn;
163 FOR_EACH_PTR_REVERSE(bb->insns, insn) {
164 if (!insn->bb)
165 continue;
166 if (insn->opcode == OP_STORE) {
167 struct instruction *dom;
168 pseudo_t pseudo = insn->src;
169 int local = local_pseudo(pseudo);
171 RECURSE_PTR_REVERSE(insn, dom) {
172 int dominance;
173 if (!dom->bb)
174 continue;
175 dominance = dominates(pseudo, insn, dom, local);
176 if (dominance) {
177 /* possible partial dominance? */
178 if (dominance < 0)
179 goto next_store;
180 if (dom->opcode == OP_LOAD)
181 goto next_store;
182 /* Yeehaa! Found one! */
183 kill_store(dom);
185 } END_FOR_EACH_PTR_REVERSE(dom);
187 /* Ok, we should check the parents now */
189 next_store:
190 /* Do the next one */;
191 } END_FOR_EACH_PTR_REVERSE(insn);
194 void simplify_memops(struct entrypoint *ep)
196 struct basic_block *bb;
198 FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
199 simplify_loads(bb);
200 } END_FOR_EACH_PTR_REVERSE(bb);
202 FOR_EACH_PTR_REVERSE(ep->bbs, bb) {
203 kill_dominated_stores(bb);
204 } END_FOR_EACH_PTR_REVERSE(bb);