]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rtl-chkp.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / rtl-chkp.c
1 /* RTL manipulation functions exported by Pointer Bounds Checker.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Ilya Enkovich (ilya.enkovich@intel.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "symtab.h"
25 #include "hashtab.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "tm.h"
29 #include "hard-reg-set.h"
30 #include "input.h"
31 #include "function.h"
32 #include "rtl.h"
33 #include "flags.h"
34 #include "statistics.h"
35 #include "alias.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "insn-config.h"
39 #include "expmed.h"
40 #include "dojump.h"
41 #include "explow.h"
42 #include "calls.h"
43 #include "emit-rtl.h"
44 #include "varasm.h"
45 #include "stmt.h"
46 #include "expr.h"
47 #include "target.h"
48 #include "tree-ssa-alias.h"
49 #include "internal-fn.h"
50 #include "is-a.h"
51 #include "predict.h"
52 #include "basic-block.h"
53 #include "fold-const.h"
54 #include "gimple-expr.h"
55 #include "gimple.h"
56 #include "bitmap.h"
57 #include "rtl-chkp.h"
58 #include "tree-chkp.h"
59 #include "hash-map.h"
60
61 static hash_map<tree, rtx> *chkp_rtx_bounds_map;
62
63 /* Get bounds rtx associated with NODE via
64 chkp_set_rtl_bounds call. */
65 rtx
66 chkp_get_rtl_bounds (tree node)
67 {
68 rtx *slot;
69
70 if (!chkp_rtx_bounds_map)
71 return NULL_RTX;
72
73 slot = chkp_rtx_bounds_map->get (node);
74 return slot ? *slot : NULL_RTX;
75 }
76
77 /* Associate bounds rtx VAL with NODE. */
78 void
79 chkp_set_rtl_bounds (tree node, rtx val)
80 {
81 if (!chkp_rtx_bounds_map)
82 chkp_rtx_bounds_map = new hash_map<tree, rtx>;
83
84 chkp_rtx_bounds_map->put (node, val);
85 }
86
87 /* Reset all bounds stored via chkp_set_rtl_bounds. */
88 void
89 chkp_reset_rtl_bounds ()
90 {
91 if (!chkp_rtx_bounds_map)
92 return;
93
94 delete chkp_rtx_bounds_map;
95 chkp_rtx_bounds_map = NULL;
96 }
97
98 /* Split SLOT identifying slot for function value or
99 argument into two parts SLOT_VAL and SLOT_BND.
100 First is the slot for regular value and the other one is
101 for bounds. */
102 void
103 chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
104 {
105 int i;
106 int val_num = 0;
107 int bnd_num = 0;
108 rtx *val_tmps;
109 rtx *bnd_tmps;
110
111 *slot_bnd = 0;
112
113 if (!slot
114 || GET_CODE (slot) != PARALLEL)
115 {
116 *slot_val = slot;
117 return;
118 }
119
120 val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
121 bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
122
123 for (i = 0; i < XVECLEN (slot, 0); i++)
124 {
125 rtx elem = XVECEXP (slot, 0, i);
126 rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;
127
128 if (!reg)
129 continue;
130
131 if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
132 bnd_tmps[bnd_num++] = elem;
133 else
134 val_tmps[val_num++] = elem;
135 }
136
137 gcc_assert (val_num);
138
139 if (!bnd_num)
140 {
141 *slot_val = slot;
142 return;
143 }
144
145 if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
146 *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
147 gen_rtvec_v (val_num, val_tmps));
148 else
149 *slot_val = val_tmps[0];
150
151 if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
152 *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
153 gen_rtvec_v (bnd_num, bnd_tmps));
154 else
155 *slot_bnd = bnd_tmps[0];
156 }
157
158 /* Join previously splitted to VAL and BND rtx for function
159 value or argument and return it. */
160 rtx
161 chkp_join_splitted_slot (rtx val, rtx bnd)
162 {
163 rtx res;
164 int i, n = 0;
165
166 if (!bnd)
167 return val;
168
169 if (GET_CODE (val) == PARALLEL)
170 n += XVECLEN (val, 0);
171 else
172 n++;
173
174 if (GET_CODE (bnd) == PARALLEL)
175 n += XVECLEN (bnd, 0);
176 else
177 n++;
178
179 res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));
180
181 n = 0;
182
183 if (GET_CODE (val) == PARALLEL)
184 for (i = 0; i < XVECLEN (val, 0); i++)
185 XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
186 else
187 XVECEXP (res, 0, n++) = val;
188
189 if (GET_CODE (bnd) == PARALLEL)
190 for (i = 0; i < XVECLEN (bnd, 0); i++)
191 XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
192 else
193 XVECEXP (res, 0, n++) = bnd;
194
195 return res;
196 }
197
198 /* If PAR is PARALLEL holding registers then transform
199 it into PARALLEL holding EXPR_LISTs of those regs
200 and zero constant (similar to how function value
201 on multiple registers looks like). */
202 void
203 chkp_put_regs_to_expr_list (rtx par)
204 {
205 int n;
206
207 if (GET_CODE (par) != PARALLEL
208 || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
209 return;
210
211 for (n = 0; n < XVECLEN (par, 0); n++)
212 XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
213 XVECEXP (par, 0, n),
214 const0_rtx);
215 }
216
217 /* Search rtx PAR describing function return value for an
218 item related to value at offset OFFS and return it.
219 Return NULL if item was not found. */
220 rtx
221 chkp_get_value_with_offs (rtx par, rtx offs)
222 {
223 int n;
224
225 gcc_assert (GET_CODE (par) == PARALLEL);
226
227 for (n = 0; n < XVECLEN (par, 0); n++)
228 {
229 rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
230 if (INTVAL (offs) == INTVAL (par_offs))
231 return XEXP (XVECEXP (par, 0, n), 0);
232 }
233
234 return NULL;
235 }
236
237 /* Emit instructions to store BOUNDS for pointer VALUE
238 stored in MEM.
239 Function is used by expand to pass bounds for args
240 passed on stack. */
241 void
242 chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
243 {
244 gcc_assert (MEM_P (mem));
245
246 if (REG_P (bounds) || CONST_INT_P (bounds))
247 {
248 rtx ptr;
249
250 if (REG_P (value))
251 ptr = value;
252 else
253 {
254 rtx slot = adjust_address (value, Pmode, 0);
255 ptr = gen_reg_rtx (Pmode);
256 emit_move_insn (ptr, slot);
257 }
258
259 if (CONST_INT_P (bounds))
260 bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);
261
262 targetm.calls.store_bounds_for_arg (ptr, mem,
263 bounds, NULL);
264 }
265 else
266 {
267 int i;
268
269 gcc_assert (GET_CODE (bounds) == PARALLEL);
270 gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));
271
272 for (i = 0; i < XVECLEN (bounds, 0); i++)
273 {
274 rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
275 rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
276 rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
277 rtx ptr;
278
279 if (GET_CODE (value) == PARALLEL)
280 ptr = chkp_get_value_with_offs (value, offs);
281 else if (MEM_P (value))
282 {
283 rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
284 ptr = gen_reg_rtx (Pmode);
285 emit_move_insn (ptr, tmp);
286 }
287 else
288 ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));
289
290 targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
291 }
292 }
293 }
294
295 /* Emit code to copy bounds for structure VALUE of type TYPE
296 copied to SLOT. */
297 void
298 chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
299 {
300 bitmap have_bound;
301 bitmap_iterator bi;
302 unsigned i;
303 rtx tmp = NULL, bnd;
304
305 gcc_assert (TYPE_SIZE (type));
306 gcc_assert (MEM_P (value));
307 gcc_assert (MEM_P (slot));
308 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
309
310 bitmap_obstack_initialize (NULL);
311 have_bound = BITMAP_ALLOC (NULL);
312 chkp_find_bound_slots (type, have_bound);
313
314 EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
315 {
316 rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
317 rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);
318
319 if (!tmp)
320 tmp = gen_reg_rtx (Pmode);
321
322 emit_move_insn (tmp, ptr);
323 bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
324 targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
325 }
326
327 BITMAP_FREE (have_bound);
328 bitmap_obstack_release (NULL);
329 }