libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / rtl-chkp.c
blob524b3ba1af625c0da7fd24a18bad5e1866532ac4
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 "expr.h"
26 #include "target.h"
27 #include "tree-ssa-alias.h"
28 #include "internal-fn.h"
29 #include "is-a.h"
30 #include "predict.h"
31 #include "basic-block.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "fold-const.h"
43 #include "gimple-expr.h"
44 #include "gimple.h"
45 #include "bitmap.h"
46 #include "rtl-chkp.h"
47 #include "tree-chkp.h"
48 #include "hash-map.h"
50 static hash_map<tree, rtx> *chkp_rtx_bounds_map;
52 /* Get bounds rtx associated with NODE via
53 chkp_set_rtl_bounds call. */
54 rtx
55 chkp_get_rtl_bounds (tree node)
57 rtx *slot;
59 if (!chkp_rtx_bounds_map)
60 return NULL_RTX;
62 slot = chkp_rtx_bounds_map->get (node);
63 return slot ? *slot : NULL_RTX;
66 /* Associate bounds rtx VAL with NODE. */
67 void
68 chkp_set_rtl_bounds (tree node, rtx val)
70 if (!chkp_rtx_bounds_map)
71 chkp_rtx_bounds_map = new hash_map<tree, rtx>;
73 chkp_rtx_bounds_map->put (node, val);
76 /* Reset all bounds stored via chkp_set_rtl_bounds. */
77 void
78 chkp_reset_rtl_bounds ()
80 if (!chkp_rtx_bounds_map)
81 return;
83 delete chkp_rtx_bounds_map;
84 chkp_rtx_bounds_map = NULL;
87 /* Split SLOT identifying slot for function value or
88 argument into two parts SLOT_VAL and SLOT_BND.
89 First is the slot for regular value and the other one is
90 for bounds. */
91 void
92 chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
94 int i;
95 int val_num = 0;
96 int bnd_num = 0;
97 rtx *val_tmps;
98 rtx *bnd_tmps;
100 *slot_bnd = 0;
102 if (!slot
103 || GET_CODE (slot) != PARALLEL)
105 *slot_val = slot;
106 return;
109 val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
110 bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
112 for (i = 0; i < XVECLEN (slot, 0); i++)
114 rtx elem = XVECEXP (slot, 0, i);
115 rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;
117 if (!reg)
118 continue;
120 if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
121 bnd_tmps[bnd_num++] = elem;
122 else
123 val_tmps[val_num++] = elem;
126 gcc_assert (val_num);
128 if (!bnd_num)
130 *slot_val = slot;
131 return;
134 if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
135 *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
136 gen_rtvec_v (val_num, val_tmps));
137 else
138 *slot_val = val_tmps[0];
140 if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
141 *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
142 gen_rtvec_v (bnd_num, bnd_tmps));
143 else
144 *slot_bnd = bnd_tmps[0];
147 /* Join previously splitted to VAL and BND rtx for function
148 value or argument and return it. */
150 chkp_join_splitted_slot (rtx val, rtx bnd)
152 rtx res;
153 int i, n = 0;
155 if (!bnd)
156 return val;
158 if (GET_CODE (val) == PARALLEL)
159 n += XVECLEN (val, 0);
160 else
161 n++;
163 if (GET_CODE (bnd) == PARALLEL)
164 n += XVECLEN (bnd, 0);
165 else
166 n++;
168 res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));
170 n = 0;
172 if (GET_CODE (val) == PARALLEL)
173 for (i = 0; i < XVECLEN (val, 0); i++)
174 XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
175 else
176 XVECEXP (res, 0, n++) = val;
178 if (GET_CODE (bnd) == PARALLEL)
179 for (i = 0; i < XVECLEN (bnd, 0); i++)
180 XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
181 else
182 XVECEXP (res, 0, n++) = bnd;
184 return res;
187 /* If PAR is PARALLEL holding registers then transform
188 it into PARALLEL holding EXPR_LISTs of those regs
189 and zero constant (similar to how function value
190 on multiple registers looks like). */
191 void
192 chkp_put_regs_to_expr_list (rtx par)
194 int n;
196 if (GET_CODE (par) != PARALLEL
197 || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
198 return;
200 for (n = 0; n < XVECLEN (par, 0); n++)
201 XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
202 XVECEXP (par, 0, n),
203 const0_rtx);
206 /* Search rtx PAR describing function return value for an
207 item related to value at offset OFFS and return it.
208 Return NULL if item was not found. */
210 chkp_get_value_with_offs (rtx par, rtx offs)
212 int n;
214 gcc_assert (GET_CODE (par) == PARALLEL);
216 for (n = 0; n < XVECLEN (par, 0); n++)
218 rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
219 if (INTVAL (offs) == INTVAL (par_offs))
220 return XEXP (XVECEXP (par, 0, n), 0);
223 return NULL;
226 /* Emit instructions to store BOUNDS for pointer VALUE
227 stored in MEM.
228 Function is used by expand to pass bounds for args
229 passed on stack. */
230 void
231 chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
233 gcc_assert (MEM_P (mem));
235 if (REG_P (bounds) || CONST_INT_P (bounds))
237 rtx ptr;
239 if (REG_P (value))
240 ptr = value;
241 else
243 rtx slot = adjust_address (value, Pmode, 0);
244 ptr = gen_reg_rtx (Pmode);
245 emit_move_insn (ptr, slot);
248 if (CONST_INT_P (bounds))
249 bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);
251 targetm.calls.store_bounds_for_arg (ptr, mem,
252 bounds, NULL);
254 else
256 int i;
258 gcc_assert (GET_CODE (bounds) == PARALLEL);
259 gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));
261 for (i = 0; i < XVECLEN (bounds, 0); i++)
263 rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
264 rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
265 rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
266 rtx ptr;
268 if (GET_CODE (value) == PARALLEL)
269 ptr = chkp_get_value_with_offs (value, offs);
270 else if (MEM_P (value))
272 rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
273 ptr = gen_reg_rtx (Pmode);
274 emit_move_insn (ptr, tmp);
276 else
277 ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));
279 targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
284 /* Emit code to copy bounds for structure VALUE of type TYPE
285 copied to SLOT. */
286 void
287 chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
289 bitmap have_bound;
290 bitmap_iterator bi;
291 unsigned i;
292 rtx tmp = NULL, bnd;
294 gcc_assert (TYPE_SIZE (type));
295 gcc_assert (MEM_P (value));
296 gcc_assert (MEM_P (slot));
297 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
299 bitmap_obstack_initialize (NULL);
300 have_bound = BITMAP_ALLOC (NULL);
301 chkp_find_bound_slots (type, have_bound);
303 EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
305 rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
306 rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);
308 if (!tmp)
309 tmp = gen_reg_rtx (Pmode);
311 emit_move_insn (tmp, ptr);
312 bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
313 targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
316 BITMAP_FREE (have_bound);
317 bitmap_obstack_release (NULL);