aboutsummaryrefslogtreecommitdiff
path: root/gcc/ssa-range-stmt.c
blob: 181a4b560a181e23218e029b3aedcbb52a7ffd14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* SSA range statement summary.
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
   Contributed by Andrew MacLeod <amacleod@redhat.com>.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "insn-codes.h"
#include "rtl.h"
#include "tree.h"
#include "gimple.h"
#include "cfghooks.h"
#include "tree-pass.h"
#include "ssa.h"
#include "optabs-tree.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "flags.h"
#include "fold-const.h"
#include "stor-layout.h"
#include "calls.h"
#include "cfganal.h"
#include "gimple-fold.h"
#include "tree-eh.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "tree-cfg.h"
#include "wide-int.h"
#include "ssa-range-stmt.h"


/* This routine will return what  is globally known about the range for an
   operand of any kind.  */
bool
get_operand_range (irange& r, tree op)
{
  /* This check allows unary operations to be handled without having to 
     make an explicit check for the existence of a second operand.  */
  if (!op)
    return false;

  if (TREE_CODE (op) == INTEGER_CST)
    r.set_range (TREE_TYPE (op), op, op);
  else
    if (TREE_CODE (op) == SSA_NAME)
      r = op;
    else
      if (TYPE_P (op))
	r.set_range_for_type (op);
      else
        /* Default to range for the type of the expression.   */
	r.set_range_for_type (TREE_TYPE (op));

  return true;
}


/* Initialize the SSA operands and validate that all operands of this
   expression are operable on iranges. 
   Return ERROR_MARK if they are not, otherwise the current tree code.  */
tree_code
range_stmt::validate_operands ()
{
  ssa1 = valid_irange_ssa (op1);
  ssa2 = valid_irange_ssa (op2);

  if (ssa1 || (TREE_CODE (op1) == INTEGER_CST && !TREE_OVERFLOW (op1))) 
   {
     if (!op2)
       return code;
     if (ssa2 || (TREE_CODE (op2) == INTEGER_CST && !TREE_OVERFLOW (op2)))
       return code;
   }
  return ERROR_MARK;
}

/* Build a range node from a stmt, if it possible.  */
void
range_stmt::from_stmt (gimple *s)
{
  switch (gimple_code (s))
    {
      case GIMPLE_COND:
        {
	  gcond *gc = as_a <gcond *> (s);
	  code  = gimple_cond_code (gc);
	  if (irange_op_handler (code))
	    {
	      lhs = gimple_get_lhs (s);
	      op1 = gimple_cond_lhs (gc);
	      op2 = gimple_cond_rhs (gc);
	      code = validate_operands ();
	      return;
	    }
	  break;
	}
      case GIMPLE_ASSIGN:
	{
	  gassign *ga = as_a <gassign *> (s);
	  code = gimple_assign_rhs_code (ga);
	  if (irange_op_handler (code))
	    {
	      lhs = gimple_get_lhs (s);
	      op1 = gimple_assign_rhs1 (ga);
	      if (get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS)
		op2 = gimple_assign_rhs2 (ga);
	      else
		op2 = NULL;
	      code = validate_operands ();
	      return;
	    }
	  break;
	}

      default:
        break;
    }
  code = ERROR_MARK;
  return;
}

/* This method will attempt to resolve a unary expression with value R1 to
   a range.  If the expression can be resolved, true is returned, and the
   range is returned in RES.  */

bool
range_stmt::fold (irange &res, const irange& r1) const
{
  irange r2;
  irange_operator *handler = irange_op_handler (code);
  gcc_assert (handler != NULL);

  // Empty ranges are viral.  
  if (r1.empty_p ())
    {
      if (lhs)
        res.clear (TREE_TYPE (lhs));
      else
        res.clear (r1.get_type ());
      return true;
    }

  /* Single ssa operations require the LHS type as the second range.  */
  if (lhs)
    r2.set_range_for_type (TREE_TYPE (lhs));
  else
    r2.clear ();

  return handler->fold_range (res, r1, r2);
}

/* This method will attempt to resolve a binary expression with operand
   values R1 tnd R2 to a range.  If the expression can be resolved, true is
   returned, and the range is returned in RES.  */

bool
range_stmt::fold (irange &res, const irange& r1, const irange& r2) const
{
  irange_operator *handler = irange_op_handler (code);
  gcc_assert (handler != NULL);

  // Empty ranges are viral.  
  if (r1.empty_p () || r2.empty_p ())
    {
      res.clear (r1.get_type ());
      return true;
    }

  // Make sure this isnt a unary operation being passed a second range.
  gcc_assert (op2);
  return handler->fold_range (res, r1, r2);
}

/* This method will attempt to evaluate the epression using whatever is
   globally known about the operands.  If it can be evaluated, TRUE is returned
   and the range is returned in RES.  */

bool
range_stmt::fold (irange &res) const
{
  irange r1, r2;
  
  if (!op2)
    {
      get_operand_range (r1, op1);
      return fold (res, r1);
    }

  get_operand_range (r1, op1);
  get_operand_range (r2, op2);
  return fold (res, r1, r2);
}

/* This method will attempt to evaluate the expression by replacing any
   occurrence of ssa_name NAME with the range NAME_RANGE. If it can be
   evaluated, TRUE is returned and the resulting range returned in RES.  */
bool
range_stmt::fold (irange& res, tree name, const irange& name_range) const
{
  irange r1, r2;

  if (!op2)
    {
      if (ssa1 == name)
	r1 = name_range;
      else
	get_operand_range (r1, op1);
      return fold (res, r1);
    }

  if (ssa1 == name)
    r1 = name_range;
  else
    get_operand_range (r1, op1);

  if (ssa2 == name)
    r2 = name_range;
  else
    get_operand_range (r2, op2);

  return fold (res, r1, r2);
}

/* This method will evaluate a range for the operand of a unary expression
   given a range for the LHS of the expression in LHS_RANGE. If it can be
   evaluated, TRUE is returned and the resulting range returned in RES.  */
bool
range_stmt::op1_irange (irange& r, const irange& lhs_range) const
{  
  irange type_range;
  type_range.set_range_for_type (TREE_TYPE (op1));
  return handler ()->op1_irange (r, lhs_range, type_range);
}

/* This method will evaluate a range for operand 1 of a binary expression
   given a range for the LHS in LHS_RANGE and a range for operand 2 in
   OP2_RANGE. If it can be evaluated, TRUE is returned and the resulting
   range returned in RES.  */
bool
range_stmt::op1_irange (irange& r, const irange& lhs_range,
			const irange& op2_range) const
{  
  gcc_assert (op2 != NULL);
  return handler ()->op1_irange (r, lhs_range, op2_range);
}

/* This method will evaluate a range for operand 2 of a binary expression
   given a range for the LHS in LHS_RANGE and a range for operand 1 in
   OP1_RANGE. If it can be evaluated, TRUE is returned and the resulting
   range returned in RES.  */
bool
range_stmt::op2_irange (irange& r, const irange& lhs_range,
			const irange& op1_range) const
{  
  return handler ()->op2_irange (r, lhs_range, op1_range);
}

/* This method will dump the internal state of the statement summary.  */
void
range_stmt::dump (FILE *f) const
{
  if (lhs)
    {
      print_generic_expr (f, lhs, TDF_SLIM);
      fprintf (f, " = ");
    }

  if (!op2)
    irange_op_handler (code)->dump (f);

  print_generic_expr (f, op1, TDF_SLIM);

  if (op2)
    {
      irange_op_handler (code)->dump (f);
      print_generic_expr (f, op2, TDF_SLIM);
    }

}