1*38fd1498Szrj /* Operations with affine combinations of trees.
2*38fd1498Szrj Copyright (C) 2005-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it
7*38fd1498Szrj under the terms of the GNU General Public License as published by the
8*38fd1498Szrj Free Software Foundation; either version 3, or (at your option) any
9*38fd1498Szrj later version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT
12*38fd1498Szrj ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "backend.h"
24*38fd1498Szrj #include "rtl.h"
25*38fd1498Szrj #include "tree.h"
26*38fd1498Szrj #include "gimple.h"
27*38fd1498Szrj #include "ssa.h"
28*38fd1498Szrj #include "tree-pretty-print.h"
29*38fd1498Szrj #include "fold-const.h"
30*38fd1498Szrj #include "tree-affine.h"
31*38fd1498Szrj #include "gimplify.h"
32*38fd1498Szrj #include "dumpfile.h"
33*38fd1498Szrj #include "cfgexpand.h"
34*38fd1498Szrj
35*38fd1498Szrj /* Extends CST as appropriate for the affine combinations COMB. */
36*38fd1498Szrj
37*38fd1498Szrj static widest_int
wide_int_ext_for_comb(const widest_int & cst,tree type)38*38fd1498Szrj wide_int_ext_for_comb (const widest_int &cst, tree type)
39*38fd1498Szrj {
40*38fd1498Szrj return wi::sext (cst, TYPE_PRECISION (type));
41*38fd1498Szrj }
42*38fd1498Szrj
43*38fd1498Szrj /* Likewise for polynomial offsets. */
44*38fd1498Szrj
45*38fd1498Szrj static poly_widest_int
wide_int_ext_for_comb(const poly_widest_int & cst,tree type)46*38fd1498Szrj wide_int_ext_for_comb (const poly_widest_int &cst, tree type)
47*38fd1498Szrj {
48*38fd1498Szrj return wi::sext (cst, TYPE_PRECISION (type));
49*38fd1498Szrj }
50*38fd1498Szrj
51*38fd1498Szrj /* Initializes affine combination COMB so that its value is zero in TYPE. */
52*38fd1498Szrj
53*38fd1498Szrj static void
aff_combination_zero(aff_tree * comb,tree type)54*38fd1498Szrj aff_combination_zero (aff_tree *comb, tree type)
55*38fd1498Szrj {
56*38fd1498Szrj int i;
57*38fd1498Szrj comb->type = type;
58*38fd1498Szrj comb->offset = 0;
59*38fd1498Szrj comb->n = 0;
60*38fd1498Szrj for (i = 0; i < MAX_AFF_ELTS; i++)
61*38fd1498Szrj comb->elts[i].coef = 0;
62*38fd1498Szrj comb->rest = NULL_TREE;
63*38fd1498Szrj }
64*38fd1498Szrj
65*38fd1498Szrj /* Sets COMB to CST. */
66*38fd1498Szrj
67*38fd1498Szrj void
aff_combination_const(aff_tree * comb,tree type,const poly_widest_int & cst)68*38fd1498Szrj aff_combination_const (aff_tree *comb, tree type, const poly_widest_int &cst)
69*38fd1498Szrj {
70*38fd1498Szrj aff_combination_zero (comb, type);
71*38fd1498Szrj comb->offset = wide_int_ext_for_comb (cst, comb->type);;
72*38fd1498Szrj }
73*38fd1498Szrj
74*38fd1498Szrj /* Sets COMB to single element ELT. */
75*38fd1498Szrj
76*38fd1498Szrj void
aff_combination_elt(aff_tree * comb,tree type,tree elt)77*38fd1498Szrj aff_combination_elt (aff_tree *comb, tree type, tree elt)
78*38fd1498Szrj {
79*38fd1498Szrj aff_combination_zero (comb, type);
80*38fd1498Szrj
81*38fd1498Szrj comb->n = 1;
82*38fd1498Szrj comb->elts[0].val = elt;
83*38fd1498Szrj comb->elts[0].coef = 1;
84*38fd1498Szrj }
85*38fd1498Szrj
86*38fd1498Szrj /* Scales COMB by SCALE. */
87*38fd1498Szrj
88*38fd1498Szrj void
aff_combination_scale(aff_tree * comb,const widest_int & scale_in)89*38fd1498Szrj aff_combination_scale (aff_tree *comb, const widest_int &scale_in)
90*38fd1498Szrj {
91*38fd1498Szrj unsigned i, j;
92*38fd1498Szrj
93*38fd1498Szrj widest_int scale = wide_int_ext_for_comb (scale_in, comb->type);
94*38fd1498Szrj if (scale == 1)
95*38fd1498Szrj return;
96*38fd1498Szrj
97*38fd1498Szrj if (scale == 0)
98*38fd1498Szrj {
99*38fd1498Szrj aff_combination_zero (comb, comb->type);
100*38fd1498Szrj return;
101*38fd1498Szrj }
102*38fd1498Szrj
103*38fd1498Szrj comb->offset = wide_int_ext_for_comb (scale * comb->offset, comb->type);
104*38fd1498Szrj for (i = 0, j = 0; i < comb->n; i++)
105*38fd1498Szrj {
106*38fd1498Szrj widest_int new_coef
107*38fd1498Szrj = wide_int_ext_for_comb (scale * comb->elts[i].coef, comb->type);
108*38fd1498Szrj /* A coefficient may become zero due to overflow. Remove the zero
109*38fd1498Szrj elements. */
110*38fd1498Szrj if (new_coef == 0)
111*38fd1498Szrj continue;
112*38fd1498Szrj comb->elts[j].coef = new_coef;
113*38fd1498Szrj comb->elts[j].val = comb->elts[i].val;
114*38fd1498Szrj j++;
115*38fd1498Szrj }
116*38fd1498Szrj comb->n = j;
117*38fd1498Szrj
118*38fd1498Szrj if (comb->rest)
119*38fd1498Szrj {
120*38fd1498Szrj tree type = comb->type;
121*38fd1498Szrj if (POINTER_TYPE_P (type))
122*38fd1498Szrj type = sizetype;
123*38fd1498Szrj if (comb->n < MAX_AFF_ELTS)
124*38fd1498Szrj {
125*38fd1498Szrj comb->elts[comb->n].coef = scale;
126*38fd1498Szrj comb->elts[comb->n].val = comb->rest;
127*38fd1498Szrj comb->rest = NULL_TREE;
128*38fd1498Szrj comb->n++;
129*38fd1498Szrj }
130*38fd1498Szrj else
131*38fd1498Szrj comb->rest = fold_build2 (MULT_EXPR, type, comb->rest,
132*38fd1498Szrj wide_int_to_tree (type, scale));
133*38fd1498Szrj }
134*38fd1498Szrj }
135*38fd1498Szrj
136*38fd1498Szrj /* Adds ELT * SCALE to COMB. */
137*38fd1498Szrj
138*38fd1498Szrj void
aff_combination_add_elt(aff_tree * comb,tree elt,const widest_int & scale_in)139*38fd1498Szrj aff_combination_add_elt (aff_tree *comb, tree elt, const widest_int &scale_in)
140*38fd1498Szrj {
141*38fd1498Szrj unsigned i;
142*38fd1498Szrj tree type;
143*38fd1498Szrj
144*38fd1498Szrj widest_int scale = wide_int_ext_for_comb (scale_in, comb->type);
145*38fd1498Szrj if (scale == 0)
146*38fd1498Szrj return;
147*38fd1498Szrj
148*38fd1498Szrj for (i = 0; i < comb->n; i++)
149*38fd1498Szrj if (operand_equal_p (comb->elts[i].val, elt, 0))
150*38fd1498Szrj {
151*38fd1498Szrj widest_int new_coef
152*38fd1498Szrj = wide_int_ext_for_comb (comb->elts[i].coef + scale, comb->type);
153*38fd1498Szrj if (new_coef != 0)
154*38fd1498Szrj {
155*38fd1498Szrj comb->elts[i].coef = new_coef;
156*38fd1498Szrj return;
157*38fd1498Szrj }
158*38fd1498Szrj
159*38fd1498Szrj comb->n--;
160*38fd1498Szrj comb->elts[i] = comb->elts[comb->n];
161*38fd1498Szrj
162*38fd1498Szrj if (comb->rest)
163*38fd1498Szrj {
164*38fd1498Szrj gcc_assert (comb->n == MAX_AFF_ELTS - 1);
165*38fd1498Szrj comb->elts[comb->n].coef = 1;
166*38fd1498Szrj comb->elts[comb->n].val = comb->rest;
167*38fd1498Szrj comb->rest = NULL_TREE;
168*38fd1498Szrj comb->n++;
169*38fd1498Szrj }
170*38fd1498Szrj return;
171*38fd1498Szrj }
172*38fd1498Szrj if (comb->n < MAX_AFF_ELTS)
173*38fd1498Szrj {
174*38fd1498Szrj comb->elts[comb->n].coef = scale;
175*38fd1498Szrj comb->elts[comb->n].val = elt;
176*38fd1498Szrj comb->n++;
177*38fd1498Szrj return;
178*38fd1498Szrj }
179*38fd1498Szrj
180*38fd1498Szrj type = comb->type;
181*38fd1498Szrj if (POINTER_TYPE_P (type))
182*38fd1498Szrj type = sizetype;
183*38fd1498Szrj
184*38fd1498Szrj if (scale == 1)
185*38fd1498Szrj elt = fold_convert (type, elt);
186*38fd1498Szrj else
187*38fd1498Szrj elt = fold_build2 (MULT_EXPR, type,
188*38fd1498Szrj fold_convert (type, elt),
189*38fd1498Szrj wide_int_to_tree (type, scale));
190*38fd1498Szrj
191*38fd1498Szrj if (comb->rest)
192*38fd1498Szrj comb->rest = fold_build2 (PLUS_EXPR, type, comb->rest,
193*38fd1498Szrj elt);
194*38fd1498Szrj else
195*38fd1498Szrj comb->rest = elt;
196*38fd1498Szrj }
197*38fd1498Szrj
198*38fd1498Szrj /* Adds CST to C. */
199*38fd1498Szrj
200*38fd1498Szrj static void
aff_combination_add_cst(aff_tree * c,const poly_widest_int & cst)201*38fd1498Szrj aff_combination_add_cst (aff_tree *c, const poly_widest_int &cst)
202*38fd1498Szrj {
203*38fd1498Szrj c->offset = wide_int_ext_for_comb (c->offset + cst, c->type);
204*38fd1498Szrj }
205*38fd1498Szrj
206*38fd1498Szrj /* Adds COMB2 to COMB1. */
207*38fd1498Szrj
208*38fd1498Szrj void
aff_combination_add(aff_tree * comb1,aff_tree * comb2)209*38fd1498Szrj aff_combination_add (aff_tree *comb1, aff_tree *comb2)
210*38fd1498Szrj {
211*38fd1498Szrj unsigned i;
212*38fd1498Szrj
213*38fd1498Szrj aff_combination_add_cst (comb1, comb2->offset);
214*38fd1498Szrj for (i = 0; i < comb2->n; i++)
215*38fd1498Szrj aff_combination_add_elt (comb1, comb2->elts[i].val, comb2->elts[i].coef);
216*38fd1498Szrj if (comb2->rest)
217*38fd1498Szrj aff_combination_add_elt (comb1, comb2->rest, 1);
218*38fd1498Szrj }
219*38fd1498Szrj
220*38fd1498Szrj /* Converts affine combination COMB to TYPE. */
221*38fd1498Szrj
222*38fd1498Szrj void
aff_combination_convert(aff_tree * comb,tree type)223*38fd1498Szrj aff_combination_convert (aff_tree *comb, tree type)
224*38fd1498Szrj {
225*38fd1498Szrj unsigned i, j;
226*38fd1498Szrj tree comb_type = comb->type;
227*38fd1498Szrj
228*38fd1498Szrj if (TYPE_PRECISION (type) > TYPE_PRECISION (comb_type))
229*38fd1498Szrj {
230*38fd1498Szrj tree val = fold_convert (type, aff_combination_to_tree (comb));
231*38fd1498Szrj tree_to_aff_combination (val, type, comb);
232*38fd1498Szrj return;
233*38fd1498Szrj }
234*38fd1498Szrj
235*38fd1498Szrj comb->type = type;
236*38fd1498Szrj if (comb->rest && !POINTER_TYPE_P (type))
237*38fd1498Szrj comb->rest = fold_convert (type, comb->rest);
238*38fd1498Szrj
239*38fd1498Szrj if (TYPE_PRECISION (type) == TYPE_PRECISION (comb_type))
240*38fd1498Szrj return;
241*38fd1498Szrj
242*38fd1498Szrj comb->offset = wide_int_ext_for_comb (comb->offset, comb->type);
243*38fd1498Szrj for (i = j = 0; i < comb->n; i++)
244*38fd1498Szrj {
245*38fd1498Szrj if (comb->elts[i].coef == 0)
246*38fd1498Szrj continue;
247*38fd1498Szrj comb->elts[j].coef = comb->elts[i].coef;
248*38fd1498Szrj comb->elts[j].val = fold_convert (type, comb->elts[i].val);
249*38fd1498Szrj j++;
250*38fd1498Szrj }
251*38fd1498Szrj
252*38fd1498Szrj comb->n = j;
253*38fd1498Szrj if (comb->n < MAX_AFF_ELTS && comb->rest)
254*38fd1498Szrj {
255*38fd1498Szrj comb->elts[comb->n].coef = 1;
256*38fd1498Szrj comb->elts[comb->n].val = comb->rest;
257*38fd1498Szrj comb->rest = NULL_TREE;
258*38fd1498Szrj comb->n++;
259*38fd1498Szrj }
260*38fd1498Szrj }
261*38fd1498Szrj
262*38fd1498Szrj /* Splits EXPR into an affine combination of parts. */
263*38fd1498Szrj
264*38fd1498Szrj void
tree_to_aff_combination(tree expr,tree type,aff_tree * comb)265*38fd1498Szrj tree_to_aff_combination (tree expr, tree type, aff_tree *comb)
266*38fd1498Szrj {
267*38fd1498Szrj aff_tree tmp;
268*38fd1498Szrj enum tree_code code;
269*38fd1498Szrj tree cst, core, toffset;
270*38fd1498Szrj poly_int64 bitpos, bitsize, bytepos;
271*38fd1498Szrj machine_mode mode;
272*38fd1498Szrj int unsignedp, reversep, volatilep;
273*38fd1498Szrj
274*38fd1498Szrj STRIP_NOPS (expr);
275*38fd1498Szrj
276*38fd1498Szrj code = TREE_CODE (expr);
277*38fd1498Szrj switch (code)
278*38fd1498Szrj {
279*38fd1498Szrj case POINTER_PLUS_EXPR:
280*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
281*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 1), sizetype, &tmp);
282*38fd1498Szrj aff_combination_add (comb, &tmp);
283*38fd1498Szrj return;
284*38fd1498Szrj
285*38fd1498Szrj case PLUS_EXPR:
286*38fd1498Szrj case MINUS_EXPR:
287*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
288*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 1), type, &tmp);
289*38fd1498Szrj if (code == MINUS_EXPR)
290*38fd1498Szrj aff_combination_scale (&tmp, -1);
291*38fd1498Szrj aff_combination_add (comb, &tmp);
292*38fd1498Szrj return;
293*38fd1498Szrj
294*38fd1498Szrj case MULT_EXPR:
295*38fd1498Szrj cst = TREE_OPERAND (expr, 1);
296*38fd1498Szrj if (TREE_CODE (cst) != INTEGER_CST)
297*38fd1498Szrj break;
298*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
299*38fd1498Szrj aff_combination_scale (comb, wi::to_widest (cst));
300*38fd1498Szrj return;
301*38fd1498Szrj
302*38fd1498Szrj case NEGATE_EXPR:
303*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
304*38fd1498Szrj aff_combination_scale (comb, -1);
305*38fd1498Szrj return;
306*38fd1498Szrj
307*38fd1498Szrj case BIT_NOT_EXPR:
308*38fd1498Szrj /* ~x = -x - 1 */
309*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
310*38fd1498Szrj aff_combination_scale (comb, -1);
311*38fd1498Szrj aff_combination_add_cst (comb, -1);
312*38fd1498Szrj return;
313*38fd1498Szrj
314*38fd1498Szrj case ADDR_EXPR:
315*38fd1498Szrj /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
316*38fd1498Szrj if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF)
317*38fd1498Szrj {
318*38fd1498Szrj expr = TREE_OPERAND (expr, 0);
319*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 0), type, comb);
320*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 1), sizetype, &tmp);
321*38fd1498Szrj aff_combination_add (comb, &tmp);
322*38fd1498Szrj return;
323*38fd1498Szrj }
324*38fd1498Szrj core = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize, &bitpos,
325*38fd1498Szrj &toffset, &mode, &unsignedp, &reversep,
326*38fd1498Szrj &volatilep);
327*38fd1498Szrj if (!multiple_p (bitpos, BITS_PER_UNIT, &bytepos))
328*38fd1498Szrj break;
329*38fd1498Szrj aff_combination_const (comb, type, bytepos);
330*38fd1498Szrj if (TREE_CODE (core) == MEM_REF)
331*38fd1498Szrj {
332*38fd1498Szrj tree mem_offset = TREE_OPERAND (core, 1);
333*38fd1498Szrj aff_combination_add_cst (comb, wi::to_poly_widest (mem_offset));
334*38fd1498Szrj core = TREE_OPERAND (core, 0);
335*38fd1498Szrj }
336*38fd1498Szrj else
337*38fd1498Szrj core = build_fold_addr_expr (core);
338*38fd1498Szrj
339*38fd1498Szrj if (TREE_CODE (core) == ADDR_EXPR)
340*38fd1498Szrj aff_combination_add_elt (comb, core, 1);
341*38fd1498Szrj else
342*38fd1498Szrj {
343*38fd1498Szrj tree_to_aff_combination (core, type, &tmp);
344*38fd1498Szrj aff_combination_add (comb, &tmp);
345*38fd1498Szrj }
346*38fd1498Szrj if (toffset)
347*38fd1498Szrj {
348*38fd1498Szrj tree_to_aff_combination (toffset, type, &tmp);
349*38fd1498Szrj aff_combination_add (comb, &tmp);
350*38fd1498Szrj }
351*38fd1498Szrj return;
352*38fd1498Szrj
353*38fd1498Szrj case MEM_REF:
354*38fd1498Szrj if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
355*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (TREE_OPERAND (expr, 0), 0),
356*38fd1498Szrj type, comb);
357*38fd1498Szrj else if (integer_zerop (TREE_OPERAND (expr, 1)))
358*38fd1498Szrj {
359*38fd1498Szrj aff_combination_elt (comb, type, expr);
360*38fd1498Szrj return;
361*38fd1498Szrj }
362*38fd1498Szrj else
363*38fd1498Szrj aff_combination_elt (comb, type,
364*38fd1498Szrj build2 (MEM_REF, TREE_TYPE (expr),
365*38fd1498Szrj TREE_OPERAND (expr, 0),
366*38fd1498Szrj build_int_cst
367*38fd1498Szrj (TREE_TYPE (TREE_OPERAND (expr, 1)), 0)));
368*38fd1498Szrj tree_to_aff_combination (TREE_OPERAND (expr, 1), sizetype, &tmp);
369*38fd1498Szrj aff_combination_add (comb, &tmp);
370*38fd1498Szrj return;
371*38fd1498Szrj
372*38fd1498Szrj CASE_CONVERT:
373*38fd1498Szrj {
374*38fd1498Szrj tree otype = TREE_TYPE (expr);
375*38fd1498Szrj tree inner = TREE_OPERAND (expr, 0);
376*38fd1498Szrj tree itype = TREE_TYPE (inner);
377*38fd1498Szrj enum tree_code icode = TREE_CODE (inner);
378*38fd1498Szrj
379*38fd1498Szrj /* In principle this is a valid folding, but it isn't necessarily
380*38fd1498Szrj an optimization, so do it here and not in fold_unary. */
381*38fd1498Szrj if ((icode == PLUS_EXPR || icode == MINUS_EXPR || icode == MULT_EXPR)
382*38fd1498Szrj && TREE_CODE (itype) == INTEGER_TYPE
383*38fd1498Szrj && TREE_CODE (otype) == INTEGER_TYPE
384*38fd1498Szrj && TYPE_PRECISION (otype) > TYPE_PRECISION (itype))
385*38fd1498Szrj {
386*38fd1498Szrj tree op0 = TREE_OPERAND (inner, 0), op1 = TREE_OPERAND (inner, 1);
387*38fd1498Szrj
388*38fd1498Szrj /* If inner type has undefined overflow behavior, fold conversion
389*38fd1498Szrj for below two cases:
390*38fd1498Szrj (T1)(X *+- CST) -> (T1)X *+- (T1)CST
391*38fd1498Szrj (T1)(X + X) -> (T1)X + (T1)X. */
392*38fd1498Szrj if (TYPE_OVERFLOW_UNDEFINED (itype)
393*38fd1498Szrj && (TREE_CODE (op1) == INTEGER_CST
394*38fd1498Szrj || (icode == PLUS_EXPR && operand_equal_p (op0, op1, 0))))
395*38fd1498Szrj {
396*38fd1498Szrj op0 = fold_convert (otype, op0);
397*38fd1498Szrj op1 = fold_convert (otype, op1);
398*38fd1498Szrj expr = fold_build2 (icode, otype, op0, op1);
399*38fd1498Szrj tree_to_aff_combination (expr, type, comb);
400*38fd1498Szrj return;
401*38fd1498Szrj }
402*38fd1498Szrj wide_int minv, maxv;
403*38fd1498Szrj /* If inner type has wrapping overflow behavior, fold conversion
404*38fd1498Szrj for below case:
405*38fd1498Szrj (T1)(X - CST) -> (T1)X - (T1)CST
406*38fd1498Szrj if X - CST doesn't overflow by range information. Also handle
407*38fd1498Szrj (T1)(X + CST) as (T1)(X - (-CST)). */
408*38fd1498Szrj if (TYPE_UNSIGNED (itype)
409*38fd1498Szrj && TYPE_OVERFLOW_WRAPS (itype)
410*38fd1498Szrj && TREE_CODE (op0) == SSA_NAME
411*38fd1498Szrj && TREE_CODE (op1) == INTEGER_CST
412*38fd1498Szrj && icode != MULT_EXPR
413*38fd1498Szrj && get_range_info (op0, &minv, &maxv) == VR_RANGE)
414*38fd1498Szrj {
415*38fd1498Szrj if (icode == PLUS_EXPR)
416*38fd1498Szrj op1 = wide_int_to_tree (itype, -wi::to_wide (op1));
417*38fd1498Szrj if (wi::geu_p (minv, wi::to_wide (op1)))
418*38fd1498Szrj {
419*38fd1498Szrj op0 = fold_convert (otype, op0);
420*38fd1498Szrj op1 = fold_convert (otype, op1);
421*38fd1498Szrj expr = fold_build2 (MINUS_EXPR, otype, op0, op1);
422*38fd1498Szrj tree_to_aff_combination (expr, type, comb);
423*38fd1498Szrj return;
424*38fd1498Szrj }
425*38fd1498Szrj }
426*38fd1498Szrj }
427*38fd1498Szrj }
428*38fd1498Szrj break;
429*38fd1498Szrj
430*38fd1498Szrj default:
431*38fd1498Szrj {
432*38fd1498Szrj if (poly_int_tree_p (expr))
433*38fd1498Szrj {
434*38fd1498Szrj aff_combination_const (comb, type, wi::to_poly_widest (expr));
435*38fd1498Szrj return;
436*38fd1498Szrj }
437*38fd1498Szrj break;
438*38fd1498Szrj }
439*38fd1498Szrj }
440*38fd1498Szrj
441*38fd1498Szrj aff_combination_elt (comb, type, expr);
442*38fd1498Szrj }
443*38fd1498Szrj
444*38fd1498Szrj /* Creates EXPR + ELT * SCALE in TYPE. EXPR is taken from affine
445*38fd1498Szrj combination COMB. */
446*38fd1498Szrj
447*38fd1498Szrj static tree
add_elt_to_tree(tree expr,tree type,tree elt,const widest_int & scale_in)448*38fd1498Szrj add_elt_to_tree (tree expr, tree type, tree elt, const widest_int &scale_in)
449*38fd1498Szrj {
450*38fd1498Szrj enum tree_code code;
451*38fd1498Szrj
452*38fd1498Szrj widest_int scale = wide_int_ext_for_comb (scale_in, type);
453*38fd1498Szrj
454*38fd1498Szrj elt = fold_convert (type, elt);
455*38fd1498Szrj if (scale == 1)
456*38fd1498Szrj {
457*38fd1498Szrj if (!expr)
458*38fd1498Szrj return elt;
459*38fd1498Szrj
460*38fd1498Szrj return fold_build2 (PLUS_EXPR, type, expr, elt);
461*38fd1498Szrj }
462*38fd1498Szrj
463*38fd1498Szrj if (scale == -1)
464*38fd1498Szrj {
465*38fd1498Szrj if (!expr)
466*38fd1498Szrj return fold_build1 (NEGATE_EXPR, type, elt);
467*38fd1498Szrj
468*38fd1498Szrj return fold_build2 (MINUS_EXPR, type, expr, elt);
469*38fd1498Szrj }
470*38fd1498Szrj
471*38fd1498Szrj if (!expr)
472*38fd1498Szrj return fold_build2 (MULT_EXPR, type, elt, wide_int_to_tree (type, scale));
473*38fd1498Szrj
474*38fd1498Szrj if (wi::neg_p (scale))
475*38fd1498Szrj {
476*38fd1498Szrj code = MINUS_EXPR;
477*38fd1498Szrj scale = -scale;
478*38fd1498Szrj }
479*38fd1498Szrj else
480*38fd1498Szrj code = PLUS_EXPR;
481*38fd1498Szrj
482*38fd1498Szrj elt = fold_build2 (MULT_EXPR, type, elt, wide_int_to_tree (type, scale));
483*38fd1498Szrj return fold_build2 (code, type, expr, elt);
484*38fd1498Szrj }
485*38fd1498Szrj
486*38fd1498Szrj /* Makes tree from the affine combination COMB. */
487*38fd1498Szrj
488*38fd1498Szrj tree
aff_combination_to_tree(aff_tree * comb)489*38fd1498Szrj aff_combination_to_tree (aff_tree *comb)
490*38fd1498Szrj {
491*38fd1498Szrj tree type = comb->type, base = NULL_TREE, expr = NULL_TREE;
492*38fd1498Szrj unsigned i;
493*38fd1498Szrj poly_widest_int off;
494*38fd1498Szrj int sgn;
495*38fd1498Szrj
496*38fd1498Szrj gcc_assert (comb->n == MAX_AFF_ELTS || comb->rest == NULL_TREE);
497*38fd1498Szrj
498*38fd1498Szrj i = 0;
499*38fd1498Szrj if (POINTER_TYPE_P (type))
500*38fd1498Szrj {
501*38fd1498Szrj type = sizetype;
502*38fd1498Szrj if (comb->n > 0 && comb->elts[0].coef == 1
503*38fd1498Szrj && POINTER_TYPE_P (TREE_TYPE (comb->elts[0].val)))
504*38fd1498Szrj {
505*38fd1498Szrj base = comb->elts[0].val;
506*38fd1498Szrj ++i;
507*38fd1498Szrj }
508*38fd1498Szrj }
509*38fd1498Szrj
510*38fd1498Szrj for (; i < comb->n; i++)
511*38fd1498Szrj expr = add_elt_to_tree (expr, type, comb->elts[i].val, comb->elts[i].coef);
512*38fd1498Szrj
513*38fd1498Szrj if (comb->rest)
514*38fd1498Szrj expr = add_elt_to_tree (expr, type, comb->rest, 1);
515*38fd1498Szrj
516*38fd1498Szrj /* Ensure that we get x - 1, not x + (-1) or x + 0xff..f if x is
517*38fd1498Szrj unsigned. */
518*38fd1498Szrj if (known_lt (comb->offset, 0))
519*38fd1498Szrj {
520*38fd1498Szrj off = -comb->offset;
521*38fd1498Szrj sgn = -1;
522*38fd1498Szrj }
523*38fd1498Szrj else
524*38fd1498Szrj {
525*38fd1498Szrj off = comb->offset;
526*38fd1498Szrj sgn = 1;
527*38fd1498Szrj }
528*38fd1498Szrj expr = add_elt_to_tree (expr, type, wide_int_to_tree (type, off), sgn);
529*38fd1498Szrj
530*38fd1498Szrj if (base)
531*38fd1498Szrj return fold_build_pointer_plus (base, expr);
532*38fd1498Szrj else
533*38fd1498Szrj return fold_convert (comb->type, expr);
534*38fd1498Szrj }
535*38fd1498Szrj
536*38fd1498Szrj /* Copies the tree elements of COMB to ensure that they are not shared. */
537*38fd1498Szrj
538*38fd1498Szrj void
unshare_aff_combination(aff_tree * comb)539*38fd1498Szrj unshare_aff_combination (aff_tree *comb)
540*38fd1498Szrj {
541*38fd1498Szrj unsigned i;
542*38fd1498Szrj
543*38fd1498Szrj for (i = 0; i < comb->n; i++)
544*38fd1498Szrj comb->elts[i].val = unshare_expr (comb->elts[i].val);
545*38fd1498Szrj if (comb->rest)
546*38fd1498Szrj comb->rest = unshare_expr (comb->rest);
547*38fd1498Szrj }
548*38fd1498Szrj
549*38fd1498Szrj /* Remove M-th element from COMB. */
550*38fd1498Szrj
551*38fd1498Szrj void
aff_combination_remove_elt(aff_tree * comb,unsigned m)552*38fd1498Szrj aff_combination_remove_elt (aff_tree *comb, unsigned m)
553*38fd1498Szrj {
554*38fd1498Szrj comb->n--;
555*38fd1498Szrj if (m <= comb->n)
556*38fd1498Szrj comb->elts[m] = comb->elts[comb->n];
557*38fd1498Szrj if (comb->rest)
558*38fd1498Szrj {
559*38fd1498Szrj comb->elts[comb->n].coef = 1;
560*38fd1498Szrj comb->elts[comb->n].val = comb->rest;
561*38fd1498Szrj comb->rest = NULL_TREE;
562*38fd1498Szrj comb->n++;
563*38fd1498Szrj }
564*38fd1498Szrj }
565*38fd1498Szrj
566*38fd1498Szrj /* Adds C * COEF * VAL to R. VAL may be NULL, in that case only
567*38fd1498Szrj C * COEF is added to R. */
568*38fd1498Szrj
569*38fd1498Szrj
570*38fd1498Szrj static void
aff_combination_add_product(aff_tree * c,const widest_int & coef,tree val,aff_tree * r)571*38fd1498Szrj aff_combination_add_product (aff_tree *c, const widest_int &coef, tree val,
572*38fd1498Szrj aff_tree *r)
573*38fd1498Szrj {
574*38fd1498Szrj unsigned i;
575*38fd1498Szrj tree aval, type;
576*38fd1498Szrj
577*38fd1498Szrj for (i = 0; i < c->n; i++)
578*38fd1498Szrj {
579*38fd1498Szrj aval = c->elts[i].val;
580*38fd1498Szrj if (val)
581*38fd1498Szrj {
582*38fd1498Szrj type = TREE_TYPE (aval);
583*38fd1498Szrj aval = fold_build2 (MULT_EXPR, type, aval,
584*38fd1498Szrj fold_convert (type, val));
585*38fd1498Szrj }
586*38fd1498Szrj
587*38fd1498Szrj aff_combination_add_elt (r, aval, coef * c->elts[i].coef);
588*38fd1498Szrj }
589*38fd1498Szrj
590*38fd1498Szrj if (c->rest)
591*38fd1498Szrj {
592*38fd1498Szrj aval = c->rest;
593*38fd1498Szrj if (val)
594*38fd1498Szrj {
595*38fd1498Szrj type = TREE_TYPE (aval);
596*38fd1498Szrj aval = fold_build2 (MULT_EXPR, type, aval,
597*38fd1498Szrj fold_convert (type, val));
598*38fd1498Szrj }
599*38fd1498Szrj
600*38fd1498Szrj aff_combination_add_elt (r, aval, coef);
601*38fd1498Szrj }
602*38fd1498Szrj
603*38fd1498Szrj if (val)
604*38fd1498Szrj {
605*38fd1498Szrj if (c->offset.is_constant ())
606*38fd1498Szrj /* Access coeffs[0] directly, for efficiency. */
607*38fd1498Szrj aff_combination_add_elt (r, val, coef * c->offset.coeffs[0]);
608*38fd1498Szrj else
609*38fd1498Szrj {
610*38fd1498Szrj /* c->offset is polynomial, so multiply VAL rather than COEF
611*38fd1498Szrj by it. */
612*38fd1498Szrj tree offset = wide_int_to_tree (TREE_TYPE (val), c->offset);
613*38fd1498Szrj val = fold_build2 (MULT_EXPR, TREE_TYPE (val), val, offset);
614*38fd1498Szrj aff_combination_add_elt (r, val, coef);
615*38fd1498Szrj }
616*38fd1498Szrj }
617*38fd1498Szrj else
618*38fd1498Szrj aff_combination_add_cst (r, coef * c->offset);
619*38fd1498Szrj }
620*38fd1498Szrj
621*38fd1498Szrj /* Multiplies C1 by C2, storing the result to R */
622*38fd1498Szrj
623*38fd1498Szrj void
aff_combination_mult(aff_tree * c1,aff_tree * c2,aff_tree * r)624*38fd1498Szrj aff_combination_mult (aff_tree *c1, aff_tree *c2, aff_tree *r)
625*38fd1498Szrj {
626*38fd1498Szrj unsigned i;
627*38fd1498Szrj gcc_assert (TYPE_PRECISION (c1->type) == TYPE_PRECISION (c2->type));
628*38fd1498Szrj
629*38fd1498Szrj aff_combination_zero (r, c1->type);
630*38fd1498Szrj
631*38fd1498Szrj for (i = 0; i < c2->n; i++)
632*38fd1498Szrj aff_combination_add_product (c1, c2->elts[i].coef, c2->elts[i].val, r);
633*38fd1498Szrj if (c2->rest)
634*38fd1498Szrj aff_combination_add_product (c1, 1, c2->rest, r);
635*38fd1498Szrj if (c2->offset.is_constant ())
636*38fd1498Szrj /* Access coeffs[0] directly, for efficiency. */
637*38fd1498Szrj aff_combination_add_product (c1, c2->offset.coeffs[0], NULL, r);
638*38fd1498Szrj else
639*38fd1498Szrj {
640*38fd1498Szrj /* c2->offset is polynomial, so do the multiplication in tree form. */
641*38fd1498Szrj tree offset = wide_int_to_tree (c2->type, c2->offset);
642*38fd1498Szrj aff_combination_add_product (c1, 1, offset, r);
643*38fd1498Szrj }
644*38fd1498Szrj }
645*38fd1498Szrj
646*38fd1498Szrj /* Returns the element of COMB whose value is VAL, or NULL if no such
647*38fd1498Szrj element exists. If IDX is not NULL, it is set to the index of VAL in
648*38fd1498Szrj COMB. */
649*38fd1498Szrj
650*38fd1498Szrj static struct aff_comb_elt *
aff_combination_find_elt(aff_tree * comb,tree val,unsigned * idx)651*38fd1498Szrj aff_combination_find_elt (aff_tree *comb, tree val, unsigned *idx)
652*38fd1498Szrj {
653*38fd1498Szrj unsigned i;
654*38fd1498Szrj
655*38fd1498Szrj for (i = 0; i < comb->n; i++)
656*38fd1498Szrj if (operand_equal_p (comb->elts[i].val, val, 0))
657*38fd1498Szrj {
658*38fd1498Szrj if (idx)
659*38fd1498Szrj *idx = i;
660*38fd1498Szrj
661*38fd1498Szrj return &comb->elts[i];
662*38fd1498Szrj }
663*38fd1498Szrj
664*38fd1498Szrj return NULL;
665*38fd1498Szrj }
666*38fd1498Szrj
667*38fd1498Szrj /* Element of the cache that maps ssa name NAME to its expanded form
668*38fd1498Szrj as an affine expression EXPANSION. */
669*38fd1498Szrj
670*38fd1498Szrj struct name_expansion
671*38fd1498Szrj {
672*38fd1498Szrj aff_tree expansion;
673*38fd1498Szrj
674*38fd1498Szrj /* True if the expansion for the name is just being generated. */
675*38fd1498Szrj unsigned in_progress : 1;
676*38fd1498Szrj };
677*38fd1498Szrj
678*38fd1498Szrj /* Expands SSA names in COMB recursively. CACHE is used to cache the
679*38fd1498Szrj results. */
680*38fd1498Szrj
681*38fd1498Szrj void
aff_combination_expand(aff_tree * comb ATTRIBUTE_UNUSED,hash_map<tree,name_expansion * > ** cache)682*38fd1498Szrj aff_combination_expand (aff_tree *comb ATTRIBUTE_UNUSED,
683*38fd1498Szrj hash_map<tree, name_expansion *> **cache)
684*38fd1498Szrj {
685*38fd1498Szrj unsigned i;
686*38fd1498Szrj aff_tree to_add, current, curre;
687*38fd1498Szrj tree e, rhs;
688*38fd1498Szrj gimple *def;
689*38fd1498Szrj widest_int scale;
690*38fd1498Szrj struct name_expansion *exp;
691*38fd1498Szrj
692*38fd1498Szrj aff_combination_zero (&to_add, comb->type);
693*38fd1498Szrj for (i = 0; i < comb->n; i++)
694*38fd1498Szrj {
695*38fd1498Szrj tree type, name;
696*38fd1498Szrj enum tree_code code;
697*38fd1498Szrj
698*38fd1498Szrj e = comb->elts[i].val;
699*38fd1498Szrj type = TREE_TYPE (e);
700*38fd1498Szrj name = e;
701*38fd1498Szrj /* Look through some conversions. */
702*38fd1498Szrj if (CONVERT_EXPR_P (e)
703*38fd1498Szrj && (TYPE_PRECISION (type)
704*38fd1498Szrj >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (e, 0)))))
705*38fd1498Szrj name = TREE_OPERAND (e, 0);
706*38fd1498Szrj if (TREE_CODE (name) != SSA_NAME)
707*38fd1498Szrj continue;
708*38fd1498Szrj def = SSA_NAME_DEF_STMT (name);
709*38fd1498Szrj if (!is_gimple_assign (def) || gimple_assign_lhs (def) != name)
710*38fd1498Szrj continue;
711*38fd1498Szrj
712*38fd1498Szrj code = gimple_assign_rhs_code (def);
713*38fd1498Szrj if (code != SSA_NAME
714*38fd1498Szrj && !IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
715*38fd1498Szrj && (get_gimple_rhs_class (code) != GIMPLE_SINGLE_RHS
716*38fd1498Szrj || !is_gimple_min_invariant (gimple_assign_rhs1 (def))))
717*38fd1498Szrj continue;
718*38fd1498Szrj
719*38fd1498Szrj /* We do not know whether the reference retains its value at the
720*38fd1498Szrj place where the expansion is used. */
721*38fd1498Szrj if (TREE_CODE_CLASS (code) == tcc_reference)
722*38fd1498Szrj continue;
723*38fd1498Szrj
724*38fd1498Szrj if (!*cache)
725*38fd1498Szrj *cache = new hash_map<tree, name_expansion *>;
726*38fd1498Szrj name_expansion **slot = &(*cache)->get_or_insert (e);
727*38fd1498Szrj exp = *slot;
728*38fd1498Szrj
729*38fd1498Szrj if (!exp)
730*38fd1498Szrj {
731*38fd1498Szrj exp = XNEW (struct name_expansion);
732*38fd1498Szrj exp->in_progress = 1;
733*38fd1498Szrj *slot = exp;
734*38fd1498Szrj rhs = gimple_assign_rhs_to_tree (def);
735*38fd1498Szrj if (e != name)
736*38fd1498Szrj rhs = fold_convert (type, rhs);
737*38fd1498Szrj
738*38fd1498Szrj tree_to_aff_combination_expand (rhs, comb->type, ¤t, cache);
739*38fd1498Szrj exp->expansion = current;
740*38fd1498Szrj exp->in_progress = 0;
741*38fd1498Szrj }
742*38fd1498Szrj else
743*38fd1498Szrj {
744*38fd1498Szrj /* Since we follow the definitions in the SSA form, we should not
745*38fd1498Szrj enter a cycle unless we pass through a phi node. */
746*38fd1498Szrj gcc_assert (!exp->in_progress);
747*38fd1498Szrj current = exp->expansion;
748*38fd1498Szrj }
749*38fd1498Szrj
750*38fd1498Szrj /* Accumulate the new terms to TO_ADD, so that we do not modify
751*38fd1498Szrj COMB while traversing it; include the term -coef * E, to remove
752*38fd1498Szrj it from COMB. */
753*38fd1498Szrj scale = comb->elts[i].coef;
754*38fd1498Szrj aff_combination_zero (&curre, comb->type);
755*38fd1498Szrj aff_combination_add_elt (&curre, e, -scale);
756*38fd1498Szrj aff_combination_scale (¤t, scale);
757*38fd1498Szrj aff_combination_add (&to_add, ¤t);
758*38fd1498Szrj aff_combination_add (&to_add, &curre);
759*38fd1498Szrj }
760*38fd1498Szrj aff_combination_add (comb, &to_add);
761*38fd1498Szrj }
762*38fd1498Szrj
763*38fd1498Szrj /* Similar to tree_to_aff_combination, but follows SSA name definitions
764*38fd1498Szrj and expands them recursively. CACHE is used to cache the expansions
765*38fd1498Szrj of the ssa names, to avoid exponential time complexity for cases
766*38fd1498Szrj like
767*38fd1498Szrj
768*38fd1498Szrj a1 = a0 + a0;
769*38fd1498Szrj a2 = a1 + a1;
770*38fd1498Szrj a3 = a2 + a2;
771*38fd1498Szrj ... */
772*38fd1498Szrj
773*38fd1498Szrj void
tree_to_aff_combination_expand(tree expr,tree type,aff_tree * comb,hash_map<tree,name_expansion * > ** cache)774*38fd1498Szrj tree_to_aff_combination_expand (tree expr, tree type, aff_tree *comb,
775*38fd1498Szrj hash_map<tree, name_expansion *> **cache)
776*38fd1498Szrj {
777*38fd1498Szrj tree_to_aff_combination (expr, type, comb);
778*38fd1498Szrj aff_combination_expand (comb, cache);
779*38fd1498Szrj }
780*38fd1498Szrj
781*38fd1498Szrj /* Frees memory occupied by struct name_expansion in *VALUE. Callback for
782*38fd1498Szrj hash_map::traverse. */
783*38fd1498Szrj
784*38fd1498Szrj bool
free_name_expansion(tree const &,name_expansion ** value,void *)785*38fd1498Szrj free_name_expansion (tree const &, name_expansion **value, void *)
786*38fd1498Szrj {
787*38fd1498Szrj free (*value);
788*38fd1498Szrj return true;
789*38fd1498Szrj }
790*38fd1498Szrj
791*38fd1498Szrj /* Frees memory allocated for the CACHE used by
792*38fd1498Szrj tree_to_aff_combination_expand. */
793*38fd1498Szrj
794*38fd1498Szrj void
free_affine_expand_cache(hash_map<tree,name_expansion * > ** cache)795*38fd1498Szrj free_affine_expand_cache (hash_map<tree, name_expansion *> **cache)
796*38fd1498Szrj {
797*38fd1498Szrj if (!*cache)
798*38fd1498Szrj return;
799*38fd1498Szrj
800*38fd1498Szrj (*cache)->traverse<void *, free_name_expansion> (NULL);
801*38fd1498Szrj delete (*cache);
802*38fd1498Szrj *cache = NULL;
803*38fd1498Szrj }
804*38fd1498Szrj
805*38fd1498Szrj /* If VAL != CST * DIV for any constant CST, returns false.
806*38fd1498Szrj Otherwise, if *MULT_SET is true, additionally compares CST and MULT,
807*38fd1498Szrj and if they are different, returns false. Finally, if neither of these
808*38fd1498Szrj two cases occur, true is returned, and CST is stored to MULT and MULT_SET
809*38fd1498Szrj is set to true. */
810*38fd1498Szrj
811*38fd1498Szrj static bool
wide_int_constant_multiple_p(const poly_widest_int & val,const poly_widest_int & div,bool * mult_set,poly_widest_int * mult)812*38fd1498Szrj wide_int_constant_multiple_p (const poly_widest_int &val,
813*38fd1498Szrj const poly_widest_int &div,
814*38fd1498Szrj bool *mult_set, poly_widest_int *mult)
815*38fd1498Szrj {
816*38fd1498Szrj poly_widest_int rem, cst;
817*38fd1498Szrj
818*38fd1498Szrj if (known_eq (val, 0))
819*38fd1498Szrj {
820*38fd1498Szrj if (*mult_set && maybe_ne (*mult, 0))
821*38fd1498Szrj return false;
822*38fd1498Szrj *mult_set = true;
823*38fd1498Szrj *mult = 0;
824*38fd1498Szrj return true;
825*38fd1498Szrj }
826*38fd1498Szrj
827*38fd1498Szrj if (maybe_eq (div, 0))
828*38fd1498Szrj return false;
829*38fd1498Szrj
830*38fd1498Szrj if (!multiple_p (val, div, &cst))
831*38fd1498Szrj return false;
832*38fd1498Szrj
833*38fd1498Szrj if (*mult_set && maybe_ne (*mult, cst))
834*38fd1498Szrj return false;
835*38fd1498Szrj
836*38fd1498Szrj *mult_set = true;
837*38fd1498Szrj *mult = cst;
838*38fd1498Szrj return true;
839*38fd1498Szrj }
840*38fd1498Szrj
841*38fd1498Szrj /* Returns true if VAL = X * DIV for some constant X. If this is the case,
842*38fd1498Szrj X is stored to MULT. */
843*38fd1498Szrj
844*38fd1498Szrj bool
aff_combination_constant_multiple_p(aff_tree * val,aff_tree * div,poly_widest_int * mult)845*38fd1498Szrj aff_combination_constant_multiple_p (aff_tree *val, aff_tree *div,
846*38fd1498Szrj poly_widest_int *mult)
847*38fd1498Szrj {
848*38fd1498Szrj bool mult_set = false;
849*38fd1498Szrj unsigned i;
850*38fd1498Szrj
851*38fd1498Szrj if (val->n == 0 && known_eq (val->offset, 0))
852*38fd1498Szrj {
853*38fd1498Szrj *mult = 0;
854*38fd1498Szrj return true;
855*38fd1498Szrj }
856*38fd1498Szrj if (val->n != div->n)
857*38fd1498Szrj return false;
858*38fd1498Szrj
859*38fd1498Szrj if (val->rest || div->rest)
860*38fd1498Szrj return false;
861*38fd1498Szrj
862*38fd1498Szrj if (!wide_int_constant_multiple_p (val->offset, div->offset,
863*38fd1498Szrj &mult_set, mult))
864*38fd1498Szrj return false;
865*38fd1498Szrj
866*38fd1498Szrj for (i = 0; i < div->n; i++)
867*38fd1498Szrj {
868*38fd1498Szrj struct aff_comb_elt *elt
869*38fd1498Szrj = aff_combination_find_elt (val, div->elts[i].val, NULL);
870*38fd1498Szrj if (!elt)
871*38fd1498Szrj return false;
872*38fd1498Szrj if (!wide_int_constant_multiple_p (elt->coef, div->elts[i].coef,
873*38fd1498Szrj &mult_set, mult))
874*38fd1498Szrj return false;
875*38fd1498Szrj }
876*38fd1498Szrj
877*38fd1498Szrj gcc_assert (mult_set);
878*38fd1498Szrj return true;
879*38fd1498Szrj }
880*38fd1498Szrj
881*38fd1498Szrj /* Prints the affine VAL to the FILE. */
882*38fd1498Szrj
883*38fd1498Szrj static void
print_aff(FILE * file,aff_tree * val)884*38fd1498Szrj print_aff (FILE *file, aff_tree *val)
885*38fd1498Szrj {
886*38fd1498Szrj unsigned i;
887*38fd1498Szrj signop sgn = TYPE_SIGN (val->type);
888*38fd1498Szrj if (POINTER_TYPE_P (val->type))
889*38fd1498Szrj sgn = SIGNED;
890*38fd1498Szrj fprintf (file, "{\n type = ");
891*38fd1498Szrj print_generic_expr (file, val->type, TDF_VOPS|TDF_MEMSYMS);
892*38fd1498Szrj fprintf (file, "\n offset = ");
893*38fd1498Szrj print_dec (val->offset, file, sgn);
894*38fd1498Szrj if (val->n > 0)
895*38fd1498Szrj {
896*38fd1498Szrj fprintf (file, "\n elements = {\n");
897*38fd1498Szrj for (i = 0; i < val->n; i++)
898*38fd1498Szrj {
899*38fd1498Szrj fprintf (file, " [%d] = ", i);
900*38fd1498Szrj print_generic_expr (file, val->elts[i].val, TDF_VOPS|TDF_MEMSYMS);
901*38fd1498Szrj
902*38fd1498Szrj fprintf (file, " * ");
903*38fd1498Szrj print_dec (val->elts[i].coef, file, sgn);
904*38fd1498Szrj if (i != val->n - 1)
905*38fd1498Szrj fprintf (file, ", \n");
906*38fd1498Szrj }
907*38fd1498Szrj fprintf (file, "\n }");
908*38fd1498Szrj }
909*38fd1498Szrj if (val->rest)
910*38fd1498Szrj {
911*38fd1498Szrj fprintf (file, "\n rest = ");
912*38fd1498Szrj print_generic_expr (file, val->rest, TDF_VOPS|TDF_MEMSYMS);
913*38fd1498Szrj }
914*38fd1498Szrj fprintf (file, "\n}");
915*38fd1498Szrj }
916*38fd1498Szrj
917*38fd1498Szrj /* Prints the affine VAL to the standard error, used for debugging. */
918*38fd1498Szrj
919*38fd1498Szrj DEBUG_FUNCTION void
debug_aff(aff_tree * val)920*38fd1498Szrj debug_aff (aff_tree *val)
921*38fd1498Szrj {
922*38fd1498Szrj print_aff (stderr, val);
923*38fd1498Szrj fprintf (stderr, "\n");
924*38fd1498Szrj }
925*38fd1498Szrj
926*38fd1498Szrj /* Computes address of the reference REF in ADDR. The size of the accessed
927*38fd1498Szrj location is stored to SIZE. Returns the ultimate containing object to
928*38fd1498Szrj which REF refers. */
929*38fd1498Szrj
930*38fd1498Szrj tree
get_inner_reference_aff(tree ref,aff_tree * addr,poly_widest_int * size)931*38fd1498Szrj get_inner_reference_aff (tree ref, aff_tree *addr, poly_widest_int *size)
932*38fd1498Szrj {
933*38fd1498Szrj poly_int64 bitsize, bitpos;
934*38fd1498Szrj tree toff;
935*38fd1498Szrj machine_mode mode;
936*38fd1498Szrj int uns, rev, vol;
937*38fd1498Szrj aff_tree tmp;
938*38fd1498Szrj tree base = get_inner_reference (ref, &bitsize, &bitpos, &toff, &mode,
939*38fd1498Szrj &uns, &rev, &vol);
940*38fd1498Szrj tree base_addr = build_fold_addr_expr (base);
941*38fd1498Szrj
942*38fd1498Szrj /* ADDR = &BASE + TOFF + BITPOS / BITS_PER_UNIT. */
943*38fd1498Szrj
944*38fd1498Szrj tree_to_aff_combination (base_addr, sizetype, addr);
945*38fd1498Szrj
946*38fd1498Szrj if (toff)
947*38fd1498Szrj {
948*38fd1498Szrj tree_to_aff_combination (toff, sizetype, &tmp);
949*38fd1498Szrj aff_combination_add (addr, &tmp);
950*38fd1498Szrj }
951*38fd1498Szrj
952*38fd1498Szrj aff_combination_const (&tmp, sizetype, bits_to_bytes_round_down (bitpos));
953*38fd1498Szrj aff_combination_add (addr, &tmp);
954*38fd1498Szrj
955*38fd1498Szrj *size = bits_to_bytes_round_up (bitsize);
956*38fd1498Szrj
957*38fd1498Szrj return base;
958*38fd1498Szrj }
959*38fd1498Szrj
960*38fd1498Szrj /* Returns true if a region of size SIZE1 at position 0 and a region of
961*38fd1498Szrj size SIZE2 at position DIFF cannot overlap. */
962*38fd1498Szrj
963*38fd1498Szrj bool
aff_comb_cannot_overlap_p(aff_tree * diff,const poly_widest_int & size1,const poly_widest_int & size2)964*38fd1498Szrj aff_comb_cannot_overlap_p (aff_tree *diff, const poly_widest_int &size1,
965*38fd1498Szrj const poly_widest_int &size2)
966*38fd1498Szrj {
967*38fd1498Szrj /* Unless the difference is a constant, we fail. */
968*38fd1498Szrj if (diff->n != 0)
969*38fd1498Szrj return false;
970*38fd1498Szrj
971*38fd1498Szrj if (!ordered_p (diff->offset, 0))
972*38fd1498Szrj return false;
973*38fd1498Szrj
974*38fd1498Szrj if (maybe_lt (diff->offset, 0))
975*38fd1498Szrj {
976*38fd1498Szrj /* The second object is before the first one, we succeed if the last
977*38fd1498Szrj element of the second object is before the start of the first one. */
978*38fd1498Szrj return known_le (diff->offset + size2, 0);
979*38fd1498Szrj }
980*38fd1498Szrj else
981*38fd1498Szrj {
982*38fd1498Szrj /* We succeed if the second object starts after the first one ends. */
983*38fd1498Szrj return known_le (size1, diff->offset);
984*38fd1498Szrj }
985*38fd1498Szrj }
986*38fd1498Szrj
987