* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / rtl-chkp.c
blob2d56ba29f4efd3362da7cac6d5367234a4aed485
1 /* RTL manipulation functions exported by Pointer Bounds Checker.
2 Copyright (C) 2014 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 "expr.h"
25 #include "target.h"
26 #include "tree-ssa-alias.h"
27 #include "internal-fn.h"
28 #include "is-a.h"
29 #include "predict.h"
30 #include "basic-block.h"
31 #include "tree.h"
32 #include "gimple-expr.h"
33 #include "gimple.h"
34 #include "bitmap.h"
35 #include "rtl-chkp.h"
36 #include "tree-chkp.h"
37 #include "hash-map.h"
39 static hash_map<tree, rtx> *chkp_rtx_bounds_map;
41 /* Get bounds rtx associated with NODE via
42 chkp_set_rtl_bounds call. */
43 rtx
44 chkp_get_rtl_bounds (tree node)
46 rtx *slot;
48 if (!chkp_rtx_bounds_map)
49 return NULL_RTX;
51 slot = chkp_rtx_bounds_map->get (node);
52 return slot ? *slot : NULL_RTX;
55 /* Associate bounds rtx VAL with NODE. */
56 void
57 chkp_set_rtl_bounds (tree node, rtx val)
59 if (!chkp_rtx_bounds_map)
60 chkp_rtx_bounds_map = new hash_map<tree, rtx>;
62 chkp_rtx_bounds_map->put (node, val);
65 /* Reset all bounds stored via chkp_set_rtl_bounds. */
66 void
67 chkp_reset_rtl_bounds ()
69 if (!chkp_rtx_bounds_map)
70 return;
72 delete chkp_rtx_bounds_map;
73 chkp_rtx_bounds_map = NULL;
76 /* Split SLOT identifying slot for function value or
77 argument into two parts SLOT_VAL and SLOT_BND.
78 First is the slot for regular value and the other one is
79 for bounds. */
80 void
81 chkp_split_slot (rtx slot, rtx *slot_val, rtx *slot_bnd)
83 int i;
84 int val_num = 0;
85 int bnd_num = 0;
86 rtx *val_tmps;
87 rtx *bnd_tmps;
89 *slot_bnd = 0;
91 if (!slot
92 || GET_CODE (slot) != PARALLEL)
94 *slot_val = slot;
95 return;
98 val_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
99 bnd_tmps = XALLOCAVEC (rtx, XVECLEN (slot, 0));
101 for (i = 0; i < XVECLEN (slot, 0); i++)
103 rtx elem = XVECEXP (slot, 0, i);
104 rtx reg = GET_CODE (elem) == EXPR_LIST ? XEXP (elem, 0) : elem;
106 if (!reg)
107 continue;
109 if (POINTER_BOUNDS_MODE_P (GET_MODE (reg)) || CONST_INT_P (reg))
110 bnd_tmps[bnd_num++] = elem;
111 else
112 val_tmps[val_num++] = elem;
115 gcc_assert (val_num);
117 if (!bnd_num)
119 *slot_val = slot;
120 return;
123 if ((GET_CODE (val_tmps[0]) == EXPR_LIST) || (val_num > 1))
124 *slot_val = gen_rtx_PARALLEL (GET_MODE (slot),
125 gen_rtvec_v (val_num, val_tmps));
126 else
127 *slot_val = val_tmps[0];
129 if ((GET_CODE (bnd_tmps[0]) == EXPR_LIST) || (bnd_num > 1))
130 *slot_bnd = gen_rtx_PARALLEL (VOIDmode,
131 gen_rtvec_v (bnd_num, bnd_tmps));
132 else
133 *slot_bnd = bnd_tmps[0];
136 /* Join previously splitted to VAL and BND rtx for function
137 value or argument and return it. */
139 chkp_join_splitted_slot (rtx val, rtx bnd)
141 rtx res;
142 int i, n = 0;
144 if (!bnd)
145 return val;
147 if (GET_CODE (val) == PARALLEL)
148 n += XVECLEN (val, 0);
149 else
150 n++;
152 if (GET_CODE (bnd) == PARALLEL)
153 n += XVECLEN (bnd, 0);
154 else
155 n++;
157 res = gen_rtx_PARALLEL (GET_MODE (val), rtvec_alloc (n));
159 n = 0;
161 if (GET_CODE (val) == PARALLEL)
162 for (i = 0; i < XVECLEN (val, 0); i++)
163 XVECEXP (res, 0, n++) = XVECEXP (val, 0, i);
164 else
165 XVECEXP (res, 0, n++) = val;
167 if (GET_CODE (bnd) == PARALLEL)
168 for (i = 0; i < XVECLEN (bnd, 0); i++)
169 XVECEXP (res, 0, n++) = XVECEXP (bnd, 0, i);
170 else
171 XVECEXP (res, 0, n++) = bnd;
173 return res;
176 /* If PAR is PARALLEL holding registers then transform
177 it into PARALLEL holding EXPR_LISTs of those regs
178 and zero constant (similar to how function value
179 on multiple registers looks like). */
180 void
181 chkp_put_regs_to_expr_list (rtx par)
183 int n;
185 if (GET_CODE (par) != PARALLEL
186 || GET_CODE (XVECEXP (par, 0, 0)) == EXPR_LIST)
187 return;
189 for (n = 0; n < XVECLEN (par, 0); n++)
190 XVECEXP (par, 0, n) = gen_rtx_EXPR_LIST (VOIDmode,
191 XVECEXP (par, 0, n),
192 const0_rtx);
195 /* Search rtx PAR describing function return value for an
196 item related to value at offset OFFS and return it.
197 Return NULL if item was not found. */
199 chkp_get_value_with_offs (rtx par, rtx offs)
201 int n;
203 gcc_assert (GET_CODE (par) == PARALLEL);
205 for (n = 0; n < XVECLEN (par, 0); n++)
207 rtx par_offs = XEXP (XVECEXP (par, 0, n), 1);
208 if (INTVAL (offs) == INTVAL (par_offs))
209 return XEXP (XVECEXP (par, 0, n), 0);
212 return NULL;
215 /* Emit instructions to store BOUNDS for pointer VALUE
216 stored in MEM.
217 Function is used by expand to pass bounds for args
218 passed on stack. */
219 void
220 chkp_emit_bounds_store (rtx bounds, rtx value, rtx mem)
222 gcc_assert (MEM_P (mem));
224 if (REG_P (bounds) || CONST_INT_P (bounds))
226 rtx ptr;
228 if (REG_P (value))
229 ptr = value;
230 else
232 rtx slot = adjust_address (value, Pmode, 0);
233 ptr = gen_reg_rtx (Pmode);
234 emit_move_insn (ptr, slot);
237 if (CONST_INT_P (bounds))
238 bounds = targetm.calls.load_bounds_for_arg (value, ptr, bounds);
240 targetm.calls.store_bounds_for_arg (ptr, mem,
241 bounds, NULL);
243 else
245 int i;
247 gcc_assert (GET_CODE (bounds) == PARALLEL);
248 gcc_assert (GET_CODE (value) == PARALLEL || MEM_P (value) || REG_P (value));
250 for (i = 0; i < XVECLEN (bounds, 0); i++)
252 rtx reg = XEXP (XVECEXP (bounds, 0, i), 0);
253 rtx offs = XEXP (XVECEXP (bounds, 0, i), 1);
254 rtx slot = adjust_address (mem, Pmode, INTVAL (offs));
255 rtx ptr;
257 if (GET_CODE (value) == PARALLEL)
258 ptr = chkp_get_value_with_offs (value, offs);
259 else if (MEM_P (value))
261 rtx tmp = adjust_address (value, Pmode, INTVAL (offs));
262 ptr = gen_reg_rtx (Pmode);
263 emit_move_insn (ptr, tmp);
265 else
266 ptr = gen_rtx_SUBREG (Pmode, value, INTVAL (offs));
268 targetm.calls.store_bounds_for_arg (ptr, slot, reg, NULL);
273 /* Emit code to copy bounds for structure VALUE of type TYPE
274 copied to SLOT. */
275 void
276 chkp_copy_bounds_for_stack_parm (rtx slot, rtx value, tree type)
278 bitmap have_bound;
279 bitmap_iterator bi;
280 unsigned i;
281 rtx tmp = NULL, bnd;
283 gcc_assert (TYPE_SIZE (type));
284 gcc_assert (MEM_P (value));
285 gcc_assert (MEM_P (slot));
286 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
288 bitmap_obstack_initialize (NULL);
289 have_bound = BITMAP_ALLOC (NULL);
290 chkp_find_bound_slots (type, have_bound);
292 EXECUTE_IF_SET_IN_BITMAP (have_bound, 0, i, bi)
294 rtx ptr = adjust_address (value, Pmode, i * POINTER_SIZE / 8);
295 rtx to = adjust_address (slot, Pmode, i * POINTER_SIZE / 8);
297 if (!tmp)
298 tmp = gen_reg_rtx (Pmode);
300 emit_move_insn (tmp, ptr);
301 bnd = targetm.calls.load_bounds_for_arg (ptr, tmp, NULL);
302 targetm.calls.store_bounds_for_arg (tmp, to, bnd, NULL);
305 BITMAP_FREE (have_bound);
306 bitmap_obstack_release (NULL);