aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-fold-const.h
blob: 42353ea050c8c026d8a669452d5c1f99054383a8 (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
/* Fold GENERIC expressions.
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
   Contributed by Sebastian Pop <s.pop@laposte.net>
   
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 2, 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 COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */


/* This file defines an interface of the tree folder.  
   For the moment, the functions in this file are just wrappers around 
   the "big-ugly" fold function.  The final aim is to completely split
   up the fold function into small pieces in such a way that client 
   passes don't see the changes to the underlying implementation.  */

#ifndef GCC_TREE_FOLD_H
#define GCC_TREE_FOLD_H



/* Interface for integer operations folding.  */
extern tree tree_fold_lcm (tree, tree);
extern tree tree_fold_gcd (tree, tree);
extern tree tree_fold_bezout (tree, tree, tree *, tree *, tree *, tree *);
extern tree tree_fold_factorial (tree);



/* Fold the addition.  */

static inline tree 
tree_fold_plus (tree type, 
		tree a,
		tree b)
{
  tree res = fold (build (PLUS_EXPR, type, a, b));
  
  /* Strip away the NON_LVALUE_EXPR: fixes bootstrap on Darwin.  */
  if (TREE_CODE (res) == NON_LVALUE_EXPR)
    return TREE_OPERAND (res, 0);
  
  else
    return res;
}

/* Fold the substraction.  */

static inline tree 
tree_fold_minus (tree type, 
		 tree a,
		 tree b)
{
  tree res = fold (build (MINUS_EXPR, type, a, b));
  
  /* Strip away the NON_LVALUE_EXPR: fixes bootstrap on Darwin.  */
  if (TREE_CODE (res) == NON_LVALUE_EXPR)
    return TREE_OPERAND (res, 0);
  
  else 
    return res;
}

/* Fold the multiplication.  */

static inline tree 
tree_fold_multiply (tree type, 
		    tree a,
		    tree b)
{
  tree res = fold (build (MULT_EXPR, type, a, b));
  
  /* Strip away the NON_LVALUE_EXPR: fixes bootstrap on Darwin.  */
  if (TREE_CODE (res) == NON_LVALUE_EXPR)
    return TREE_OPERAND (res, 0);
  
  else
    return res;
}

/* Division for integer result that rounds the quotient toward zero.  */

static inline tree 
tree_fold_trunc_div (tree type, 
		     tree a,
		     tree b)
{
  return fold (build (TRUNC_DIV_EXPR, type, a, b));
}

/* Division for integer result that rounds the quotient toward infinity.  */

static inline tree 
tree_fold_ceil_div (tree type, 
		    tree a,
		    tree b)
{
  return fold (build (CEIL_DIV_EXPR, type, a, b));
}

/* Division for integer result that rounds toward minus infinity.  */

static inline tree 
tree_fold_floor_div (tree type, 
		     tree a,
		     tree b)
{
  return fold (build (FLOOR_DIV_EXPR, type, a, b));
}

/* Division which is not supposed to need rounding.  */

static inline tree 
tree_fold_exact_div (tree type, 
		     tree a,
		     tree b)
{
  return fold (build (EXACT_DIV_EXPR, type, a, b));
}

/* Computes the minimum.  */

static inline tree 
tree_fold_min (tree type, 
	       tree a,
	       tree b)
{
  return fold (build (MIN_EXPR, type, a, b));
}

/* Computes the maximum.  */

static inline tree 
tree_fold_max (tree type, 
	       tree a,
	       tree b)
{
  return fold (build (MAX_EXPR, type, a, b));
}

/* Computes the absolute value.  */

static inline tree 
tree_fold_abs (tree type, 
	       tree a)
{
  return fold (build1 (ABS_EXPR, type, a));
}

/* The binomial coefficient.  */

static inline tree 
tree_fold_binomial (tree n,
		    tree k)
{
  return tree_fold_exact_div 
    (integer_type_node, tree_fold_factorial (n), 
     tree_fold_multiply 
     (integer_type_node, tree_fold_factorial (k),
      tree_fold_factorial 
      (tree_fold_minus (integer_type_node, n, k))));
}

/* Determines whether (a divides b), or (a == gcd (a, b)).  */

static inline bool 
tree_fold_divides_p (tree type, 
		     tree a, 
		     tree b)
{
  if (integer_onep (a))
    return true;
  
  return integer_zerop 
    (tree_fold_minus 
     (type, a, tree_fold_gcd (a, b)));
}

/* Given two integer constants A and B, determine whether "A >= B".  */

static inline bool
tree_is_ge (tree a, tree b)
{
  tree cmp = fold (build (GE_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

/* Given two integer constants A and B, determine whether "A > B".  */

static inline bool
tree_is_gt (tree a, tree b)
{
  tree cmp = fold (build (GT_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

/* Given two integer constants A and B, determine whether "A <= B".  */

static inline bool
tree_is_le (tree a, tree b)
{
  tree cmp = fold (build (LE_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

/* Given two integer constants A and B, determine whether "A < B".  */

static inline bool
tree_is_lt (tree a, tree b)
{
  tree cmp = fold (build (LT_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

/* Given two integer constants A and B, determine whether "A == B".  */

static inline bool
tree_is_eq (tree a, tree b)
{
  tree cmp = fold (build (EQ_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

/* Given two integer constants A and B, determine whether "A != B".  */

static inline bool
tree_is_ne (tree a, tree b)
{
  tree cmp = fold (build (NE_EXPR, boolean_type_node, a, b));
  return (tree_int_cst_sgn (cmp) != 0);
}

#endif  /* GCC_TREE_FOLD_H  */