gcc/
[official-gcc.git] / gcc / rtl-chkp.c
blob0ad832386c0f76153c83c0ad016e0f7037c4c672
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)
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "symtab.h"
25 #include "tm.h"
26 #include "hard-reg-set.h"
27 #include "function.h"
28 #include "rtl.h"
29 #include "flags.h"
30 #include "alias.h"
31 #include "tree.h"
32 #include "insn-config.h"
33 #include "expmed.h"
34 #include "dojump.h"
35 #include "explow.h"
36 #include "calls.h"
37 #include "emit-rtl.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "target.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "predict.h"
45 #include "basic-block.h"
46 #include "fold-const.h"
47 #include "gimple-expr.h"
48 #include "gimple.h"
49 #include "bitmap.h"
50 #include "rtl-chkp.h"
51 #include "tree-chkp.h"
53 static hash_map<tree, rtx> *chkp_rtx_bounds_map;
55 /* Get bounds rtx associated with NODE via
56 chkp_set_rtl_bounds call. */
57 rtx
58 chkp_get_rtl_bounds (tree node)
60 rtx *slot;
62 if (!chkp_rtx_bounds_map)
63 return NULL_RTX;
65 slot = chkp_rtx_bounds_map->get (node);
66 return slot ? *slot : NULL_RTX;
69 /* Associate bounds rtx VAL with NODE. */
70 void
71 chkp_set_rtl_bounds (tree node, rtx val)
73 if (!chkp_rtx_bounds_map)
74 chkp_rtx_bounds_map = new hash_map<tree, rtx>;
76 chkp_rtx_bounds_map->put (node, val);
79 /* Reset all bounds stored via chkp_set_rtl_bounds. */
80 void
81 chkp_reset_rtl_bounds ()
83 if (!chkp_rtx_bounds_map)
84 return;
86 delete chkp_rtx_bounds_map;
87 chkp_rtx_bounds_map = NULL;
90 /* Split SLOT identifying slot for function value or
91 argument into two parts SLOT_VAL and SLOT_BND.
92 First is the slot for regular value and the other one is
93 for bounds. */
94 void
95 chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
97 int i;
98 int val_num = 0;
99 int bnd_num = 0;
100 rtx *val_tmps;
101 rtx *bnd_tmps;
103 *slot_bnd = 0;
105 if (!slot
106 || GET_CODE (slot) != PARALLEL)
108 *slot_val = slot;
109 return;
112 val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
113 bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
115 for (i = 0; i < XVECLEN (slot, 0); i++)
117 rtx elem = XVECEXP (slot, 0, i);
118 rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;
120 if (!reg)
121 continue;
123 if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
124 bnd_tmps[bnd_num++] = elem;
125 else
126 val_tmps[val_num++] = elem;
129 gcc_assert (val_num);
131 if (!bnd_num)
133 *slot_val = slot;
134 return;
137 if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
138 *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
139 gen_rtvec_v (val_num, val_tmps));
140 else
141 *slot_val = val_tmps[0];
143 if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
144 *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
145 gen_rtvec_v (bnd_num, bnd_tmps));
146 else
147 *slot_bnd = bnd_tmps[0];
150 /* Join previously splitted to VAL and BND rtx for function
151 value or argument and return it. */
153 chkp_join_splitted_slot (rtx val, rtx bnd)
155 rtx res;
156 int i, n = 0;
158 if (!bnd)
159 return val;
161 if (GET_CODE (val) == PARALLEL)
162 n += XVECLEN (val, 0);
163 else
164 n++;
166 if (GET_CODE (bnd) == PARALLEL)
167 n += XVECLEN (bnd, 0);
168 else
169 n++;
171 res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));
173 n = 0;
175 if (GET_CODE (val) == PARALLEL)
176 for (i = 0; i < XVECLEN (val, 0); i++)
177 XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
178 else
179 XVECEXP (res, 0, n++) = val;
181 if (GET_CODE (bnd) == PARALLEL)
182 for (i = 0; i < XVECLEN (bnd, 0); i++)
183 XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
184 else
185 XVECEXP (res, 0, n++) = bnd;
187 return res;
190 /* If PAR is PARALLEL holding registers then transform
191 it into PARALLEL holding EXPR_LISTs of those regs
192 and zero constant (similar to how function value
193 on multiple registers looks like). */
194 void
195 chkp_put_regs_to_expr_list (rtx par)
197 int n;
199 if (GET_CODE (par) != PARALLEL
200 || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
201 return;
203 for (n = 0; n < XVECLEN (par, 0); n++)
204 XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
205 XVECEXP (par, 0, n),
206 const0_rtx);
209 /* Search rtx PAR describing function return value for an
210 item related to value at offset OFFS and return it.
211 Return NULL if item was not found. */
213 chkp_get_value_with_offs (rtx par, rtx offs)
215 int n;
217 gcc_assert (GET_CODE (par) == PARALLEL);
219 for (n = 0; n < XVECLEN (par, 0); n++)
221 rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
222 if (INTVAL (offs) == INTVAL (par_offs))
223 return XEXP (XVECEXP (par, 0, n), 0);
226 return NULL;
229 /* Emit instructions to store BOUNDS for pointer VALUE
230 stored in MEM.
231 Function is used by expand to pass bounds for args
232 passed on stack. */
233 void
234 chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
236 gcc_assert (MEM_P (mem));
238 if (REG_P (bounds) || CONST_INT_P (bounds))
240 rtx ptr;
242 if (REG_P (value))
243 ptr = value;
244 else
246 rtx slot = adjust_address (value, Pmode, 0);
247 ptr = gen_reg_rtx (Pmode);
248 emit_move_insn (ptr, slot);
251 if (CONST_INT_P (bounds))
252 bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);
254 targetm.calls.store_bounds_for_arg (ptr, mem,
255 bounds, NULL);
257 else
259 int i;
261 gcc_assert (GET_CODE (bounds) == PARALLEL);
262 gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));
264 for (i = 0; i < XVECLEN (bounds, 0); i++)
266 rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
267 rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
268 rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
269 rtx ptr;
271 if (GET_CODE (value) == PARALLEL)
272 ptr = chkp_get_value_with_offs (value, offs);
273 else if (MEM_P (value))
275 rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
276 ptr = gen_reg_rtx (Pmode);
277 emit_move_insn (ptr, tmp);
279 else
280 ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));
282 targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
287 /* Emit code to copy bounds for structure VALUE of type TYPE
288 copied to SLOT. */
289 void
290 chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
292 bitmap have_bound;
293 bitmap_iterator bi;
294 unsigned i;
295 rtx tmp = NULL, bnd;
297 gcc_assert (TYPE_SIZE (type));
298 gcc_assert (MEM_P (value));
299 gcc_assert (MEM_P (slot));
300 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
302 bitmap_obstack_initialize (NULL);
303 have_bound = BITMAP_ALLOC (NULL);
304 chkp_find_bound_slots (type, have_bound);
306 EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
308 rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
309 rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);
311 if (!tmp)
312 tmp = gen_reg_rtx (Pmode);
314 emit_move_insn (tmp, ptr);
315 bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
316 targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
319 BITMAP_FREE (have_bound);
320 bitmap_obstack_release (NULL);