Mark function parallelized_function before add_new_function
[official-gcc.git] / gcc / rtl-chkp.c
blob60713c97467a9b62952039373b3442ab4f37f14a
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 "input.h"
28 #include "function.h"
29 #include "rtl.h"
30 #include "flags.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "varasm.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "target.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "is-a.h"
46 #include "predict.h"
47 #include "basic-block.h"
48 #include "fold-const.h"
49 #include "gimple-expr.h"
50 #include "gimple.h"
51 #include "bitmap.h"
52 #include "rtl-chkp.h"
53 #include "tree-chkp.h"
55 static hash_map<tree, rtx> *chkp_rtx_bounds_map;
57 /* Get bounds rtx associated with NODE via
58 chkp_set_rtl_bounds call. */
59 rtx
60 chkp_get_rtl_bounds (tree node)
62 rtx *slot;
64 if (!chkp_rtx_bounds_map)
65 return NULL_RTX;
67 slot = chkp_rtx_bounds_map->get (node);
68 return slot ? *slot : NULL_RTX;
71 /* Associate bounds rtx VAL with NODE. */
72 void
73 chkp_set_rtl_bounds (tree node, rtx val)
75 if (!chkp_rtx_bounds_map)
76 chkp_rtx_bounds_map = new hash_map<tree, rtx>;
78 chkp_rtx_bounds_map->put (node, val);
81 /* Reset all bounds stored via chkp_set_rtl_bounds. */
82 void
83 chkp_reset_rtl_bounds ()
85 if (!chkp_rtx_bounds_map)
86 return;
88 delete chkp_rtx_bounds_map;
89 chkp_rtx_bounds_map = NULL;
92 /* Split SLOT identifying slot for function value or
93 argument into two parts SLOT_VAL and SLOT_BND.
94 First is the slot for regular value and the other one is
95 for bounds. */
96 void
97 chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
99 int i;
100 int val_num = 0;
101 int bnd_num = 0;
102 rtx *val_tmps;
103 rtx *bnd_tmps;
105 *slot_bnd = 0;
107 if (!slot
108 || GET_CODE (slot) != PARALLEL)
110 *slot_val = slot;
111 return;
114 val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
115 bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
117 for (i = 0; i < XVECLEN (slot, 0); i++)
119 rtx elem = XVECEXP (slot, 0, i);
120 rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;
122 if (!reg)
123 continue;
125 if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
126 bnd_tmps[bnd_num++] = elem;
127 else
128 val_tmps[val_num++] = elem;
131 gcc_assert (val_num);
133 if (!bnd_num)
135 *slot_val = slot;
136 return;
139 if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
140 *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
141 gen_rtvec_v (val_num, val_tmps));
142 else
143 *slot_val = val_tmps[0];
145 if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
146 *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
147 gen_rtvec_v (bnd_num, bnd_tmps));
148 else
149 *slot_bnd = bnd_tmps[0];
152 /* Join previously splitted to VAL and BND rtx for function
153 value or argument and return it. */
155 chkp_join_splitted_slot (rtx val, rtx bnd)
157 rtx res;
158 int i, n = 0;
160 if (!bnd)
161 return val;
163 if (GET_CODE (val) == PARALLEL)
164 n += XVECLEN (val, 0);
165 else
166 n++;
168 if (GET_CODE (bnd) == PARALLEL)
169 n += XVECLEN (bnd, 0);
170 else
171 n++;
173 res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));
175 n = 0;
177 if (GET_CODE (val) == PARALLEL)
178 for (i = 0; i < XVECLEN (val, 0); i++)
179 XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
180 else
181 XVECEXP (res, 0, n++) = val;
183 if (GET_CODE (bnd) == PARALLEL)
184 for (i = 0; i < XVECLEN (bnd, 0); i++)
185 XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
186 else
187 XVECEXP (res, 0, n++) = bnd;
189 return res;
192 /* If PAR is PARALLEL holding registers then transform
193 it into PARALLEL holding EXPR_LISTs of those regs
194 and zero constant (similar to how function value
195 on multiple registers looks like). */
196 void
197 chkp_put_regs_to_expr_list (rtx par)
199 int n;
201 if (GET_CODE (par) != PARALLEL
202 || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
203 return;
205 for (n = 0; n < XVECLEN (par, 0); n++)
206 XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
207 XVECEXP (par, 0, n),
208 const0_rtx);
211 /* Search rtx PAR describing function return value for an
212 item related to value at offset OFFS and return it.
213 Return NULL if item was not found. */
215 chkp_get_value_with_offs (rtx par, rtx offs)
217 int n;
219 gcc_assert (GET_CODE (par) == PARALLEL);
221 for (n = 0; n < XVECLEN (par, 0); n++)
223 rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
224 if (INTVAL (offs) == INTVAL (par_offs))
225 return XEXP (XVECEXP (par, 0, n), 0);
228 return NULL;
231 /* Emit instructions to store BOUNDS for pointer VALUE
232 stored in MEM.
233 Function is used by expand to pass bounds for args
234 passed on stack. */
235 void
236 chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
238 gcc_assert (MEM_P (mem));
240 if (REG_P (bounds) || CONST_INT_P (bounds))
242 rtx ptr;
244 if (REG_P (value))
245 ptr = value;
246 else
248 rtx slot = adjust_address (value, Pmode, 0);
249 ptr = gen_reg_rtx (Pmode);
250 emit_move_insn (ptr, slot);
253 if (CONST_INT_P (bounds))
254 bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);
256 targetm.calls.store_bounds_for_arg (ptr, mem,
257 bounds, NULL);
259 else
261 int i;
263 gcc_assert (GET_CODE (bounds) == PARALLEL);
264 gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));
266 for (i = 0; i < XVECLEN (bounds, 0); i++)
268 rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
269 rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
270 rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
271 rtx ptr;
273 if (GET_CODE (value) == PARALLEL)
274 ptr = chkp_get_value_with_offs (value, offs);
275 else if (MEM_P (value))
277 rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
278 ptr = gen_reg_rtx (Pmode);
279 emit_move_insn (ptr, tmp);
281 else
282 ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));
284 targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
289 /* Emit code to copy bounds for structure VALUE of type TYPE
290 copied to SLOT. */
291 void
292 chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
294 bitmap have_bound;
295 bitmap_iterator bi;
296 unsigned i;
297 rtx tmp = NULL, bnd;
299 gcc_assert (TYPE_SIZE (type));
300 gcc_assert (MEM_P (value));
301 gcc_assert (MEM_P (slot));
302 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
304 bitmap_obstack_initialize (NULL);
305 have_bound = BITMAP_ALLOC (NULL);
306 chkp_find_bound_slots (type, have_bound);
308 EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
310 rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
311 rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);
313 if (!tmp)
314 tmp = gen_reg_rtx (Pmode);
316 emit_move_insn (tmp, ptr);
317 bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
318 targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
321 BITMAP_FREE (have_bound);
322 bitmap_obstack_release (NULL);