Allow indirect branch via GOT slot for x32
[official-gcc.git] / gcc / doc / match-and-simplify.texi
blob876483f681f6a00543bc71a663baaa45a2b063e2
1 @c Copyright (C) 2014-2015 Free Software Foundation, Inc.
2 @c Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @node Match and Simplify
7 @chapter Match and Simplify
8 @cindex Match and Simplify
10 The GIMPLE and GENERIC pattern matching project match-and-simplify
11 tries to address several issues.
13 @enumerate
14 @item unify expression simplifications currently spread and duplicated
15     over separate files like fold-const.c, gimple-fold.c and builtins.c
16 @item allow for a cheap way to implement building and simplifying
17     non-trivial GIMPLE expressions, avoiding the need to go through
18     building and simplifying GENERIC via fold_buildN and then
19     gimplifying via force_gimple_operand
20 @end enumerate
22 To address these the project introduces a simple domain specific language
23 to write expression simplifications from which code targeting GIMPLE
24 and GENERIC is auto-generated.  The GENERIC variant follows the
25 fold_buildN API while for the GIMPLE variant and to address 2) new
26 APIs are introduced.
28 @menu
29 * GIMPLE API::
30 * The Language::
31 @end menu
33 @node GIMPLE API
34 @section GIMPLE API
35 @cindex GIMPLE API
37 @deftypefn {GIMPLE function} tree gimple_simplify (enum tree_code, tree, tree, gimple_seq *, tree (*)(tree))
38 @deftypefnx {GIMPLE function} tree gimple_simplify (enum tree_code, tree, tree, tree, gimple_seq *, tree (*)(tree))
39 @deftypefnx {GIMPLE function} tree gimple_simplify (enum tree_code, tree, tree, tree, tree, gimple_seq *, tree (*)(tree))
40 @deftypefnx {GIMPLE function} tree gimple_simplify (enum built_in_function, tree, tree, gimple_seq *, tree (*)(tree))
41 @deftypefnx {GIMPLE function} tree gimple_simplify (enum built_in_function, tree, tree, tree, gimple_seq *, tree (*)(tree))
42 @deftypefnx {GIMPLE function} tree gimple_simplify (enum built_in_function, tree, tree, tree, tree, gimple_seq *, tree (*)(tree))
43 The main GIMPLE API entry to the expression simplifications mimicing
44 that of the GENERIC fold_@{unary,binary,ternary@} functions.
45 @end deftypefn
47 thus providing n-ary overloads for operation or function.  The
48 additional arguments are a gimple_seq where built statements are
49 inserted on (if @code{NULL} then simplifications requiring new statements
50 are not performed) and a valueization hook that can be used to
51 tie simplifications to a SSA lattice.
53 In addition to those APIs @code{fold_stmt} is overloaded with
54 a valueization hook:
56 @deftypefn bool fold_stmt (gimple_stmt_iterator *, tree (*)(tree));
57 @end deftypefn
60 Ontop of these a @code{fold_buildN}-like API for GIMPLE is introduced:
62 @deftypefn {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum tree_code, tree, tree, tree (*valueize) (tree) = NULL);
63 @deftypefnx {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum tree_code, tree, tree, tree, tree (*valueize) (tree) = NULL);
64 @deftypefnx {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum tree_code, tree, tree, tree, tree, tree (*valueize) (tree) = NULL);
65 @deftypefnx {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum built_in_function, tree, tree, tree (*valueize) (tree) = NULL);
66 @deftypefnx {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum built_in_function, tree, tree, tree, tree (*valueize) (tree) = NULL);
67 @deftypefnx {GIMPLE function} tree gimple_build (gimple_seq *, location_t, enum built_in_function, tree, tree, tree, tree, tree (*valueize) (tree) = NULL);
68 @deftypefnx {GIMPLE function} tree gimple_convert (gimple_seq *, location_t, tree, tree);
69 @end deftypefn
71 which is supposed to replace @code{force_gimple_operand (fold_buildN (...), ...)}
72 and calls to @code{fold_convert}.  Overloads without the @code{location_t}
73 argument exist.  Built statements are inserted on the provided sequence
74 and simplification is performed using the optional valueization hook.
77 @node The Language
78 @section The Language
79 @cindex The Language
81 The language to write expression simplifications in resembles other
82 domain-specific languages GCC uses.  Thus it is lispy.  Lets start
83 with an example from the match.pd file:
85 @smallexample
86 (simplify
87   (bit_and @@0 integer_all_onesp)
88   @@0)
89 @end smallexample
91 This example contains all required parts of an expression simplification.
92 A simplification is wrapped inside a @code{(simplify ...)} expression.
93 That contains at least two operands - an expression that is matched
94 with the GIMPLE or GENERIC IL and a replacement expression that is
95 returned if the match was successful.
97 Expressions have an operator ID, @code{bit_and} in this case.  Expressions can
98 be lower-case tree codes with @code{_expr} stripped off or builtin
99 function code names in all-caps, like @code{BUILT_IN_SQRT}.
101 @code{@@n} denotes a so-called capture.  It captures the operand and lets
102 you refer to it in other places of the match-and-simplify.  In the
103 above example it is refered to in the replacement expression.  Captures
104 are @code{@@} followed by a number or an identifier.
106 @smallexample
107 (simplify
108   (bit_xor @@0 @@0)
109   @{ build_zero_cst (type); @})
110 @end smallexample
112 In this example @code{@@0} is mentioned twice which constrains the matched
113 expression to have two equal operands.  This example also introduces
114 operands written in C code.  These can be used in the expression
115 replacements and are supposed to evaluate to a tree node which has to
116 be a valid GIMPLE operand (so you cannot generate expressions in C code).
118 @smallexample
119 (simplify
120   (trunc_mod integer_zerop@@0 @@1)
121   (if (!integer_zerop (@@1)))
122   @@0)
123 @end smallexample
125 Here @code{@@0} captures the first operand of the trunc_mod expression
126 which is also predicated with @code{integer_zerop}.  Expression operands
127 may be either expressions, predicates or captures.  Captures
128 can be unconstrained or capture expresions or predicates.
130 This example introduces an optional operand of simplify,
131 the if-expression.  This condition is evaluated after the
132 expression matched in the IL and is required to evaluate to true
133 to enable the replacement expression.  The expression operand
134 of the @code{if} is a standard C expression which may contain references
135 to captures.
137 A @code{if} expression can be used to specify a common condition
138 for multiple simplify patterns, avoiding the need
139 to repeat that multiple times:
141 @smallexample
142 (if (!TYPE_SATURATING (type)
143      && !FLOAT_TYPE_P (type) && !FIXED_POINT_TYPE_P (type))
144   (simplify
145     (minus (plus @@0 @@1) @@0)
146     @@1)
147   (simplify
148     (minus (minus @@0 @@1) @@0)
149     (negate @@1)))
150 @end smallexample
152 Ifs can be nested.
154 Captures can also be used for capturing results of sub-expressions.
156 @smallexample
157 #if GIMPLE
158 (simplify
159   (pointer_plus (addr@@2 @@0) INTEGER_CST_P@@1)
160   (if (is_gimple_min_invariant (@@2)))
161   @{
162     HOST_WIDE_INT off;
163     tree base = get_addr_base_and_unit_offset (@@0, &off);
164     off += tree_to_uhwi (@@1);
165     /* Now with that we should be able to simply write
166        (addr (mem_ref (addr @@base) (plus @@off @@1)))  */
167     build1 (ADDR_EXPR, type,
168             build2 (MEM_REF, TREE_TYPE (TREE_TYPE (@@2)),
169                     build_fold_addr_expr (base),
170                     build_int_cst (ptr_type_node, off)));
171   @})
172 #endif
173 @end smallexample
175 In the above example, @code{@@2} captures the result of the expression
176 @code{(addr @@0)}.  For outermost expression only its type can be captured,
177 and the keyword @code{type} is reserved for this purpose.  The above
178 example also gives a way to conditionalize patterns to only apply
179 to @code{GIMPLE} or @code{GENERIC} by means of using the pre-defined
180 preprocessor macros @code{GIMPLE} and @code{GENERIC} and using
181 preprocessor directives.
183 @smallexample
184 (simplify
185   (bit_and:c integral_op_p@@0 (bit_ior:c (bit_not @@0) @@1))
186   (bit_and @@1 @@0))
187 @end smallexample
189 Here we introduce flags on match expressions.  There is currently
190 a single flag, @code{c}, which denotes that the expression should
191 be also matched commutated.  Thus the above match expression
192 is really the following four match expressions:
194   (bit_and integral_op_p@@0 (bit_ior (bit_not @@0) @@1))
195   (bit_and (bit_ior (bit_not @@0) @@1) integral_op_p@@0)
196   (bit_and integral_op_p@@0 (bit_ior @@1 (bit_not @@0)))
197   (bit_and (bit_ior @@1 (bit_not @@0)) integral_op_p@@0)
199 Usual canonicalizations you know from GENERIC expressions are
200 applied before matching, so for example constant operands always
201 come second in commutative expressions.
203 More features exist to avoid too much repetition.
205 @smallexample
206 (for op (plus pointer_plus minus bit_ior bit_xor)
207   (simplify
208     (op @@0 integer_zerop)
209     @@0))
210 @end smallexample
212 A @code{for} expression can be used to repeat a pattern for each
213 operator specified, substituting @code{op}.  @code{for} can be
214 nested and a @code{for} can have multiple operators to iterate.
216 @smallexample
217 (for opa (plus minus)
218      opb (minus plus)
219   (for opc (plus minus)
220     (simplify...
221 @end smallexample
223 In this example the pattern will be repeated four times with
224 @code{opa, opb, opc} being @code{plus, minus, plus},
225 @code{plus, minus, minus}, @code{minus, plus, plus},
226 @code{minus, plus, minus}.
228 To avoid repeating operator lists in @code{for} you can name
229 them via
231 @smallexample
232 (define_operator_list pmm plus minus mult)
233 @end smallexample
235 and use them in @code{for} operator lists where they get expanded.
237 @smallexample
238 (for opa (pmm trunc_div)
239  (simplify...
240 @end smallexample
242 So this example iterates over @code{plus}, @code{minus}, @code{mult}
243 and @code{trunc_div}.
245 Using operator lists can also remove the need to explicitely write
246 a @code{for}.  All operator list uses that appear in a @code{simplify}
247 or @code{match} pattern in operator positions will implicitely
248 be added to a new @code{for}.  For example
250 @smallexample
251 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
252 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
253 (simplify
254  (SQRT (POW @@0 @@1))
255  (POW (abs @@0) (mult @@1 @{ built_real (TREE_TYPE (@@1), dconsthalf); @})))
256 @end smallexample
258 is the same as
260 @smallexample
261 (for SQRT (BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
262      POW (BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
263  (simplify
264   (SQRT (POW @@0 @@1))
265   (POW (abs @@0) (mult @@1 @{ built_real (TREE_TYPE (@@1), dconsthalf); @}))))
266 @end smallexample
268 Another building block are @code{with} expressions in the
269 result expression which nest the generated code in a new C block
270 followed by its argument:
272 @smallexample
273 (simplify
274  (convert (mult @@0 @@1))
275  (with @{ tree utype = unsigned_type_for (type); @}
276   (convert (mult (convert:utype @@0) (convert:utype @@1)))))
277 @end smallexample
279 This allows code nested in the @code{with} to refer to the declared
280 variables.  In the above case we use the feature to specify the
281 type of a generated expression with the @code{:type} syntax where
282 @code{type} needs to be an identifier that refers to the desired type.
283 Usually the types of the generated result expressions are
284 determined from the context, but sometimes like in the above case
285 it is required that you specify them explicitely.
287 As intermediate conversions are often optional there is a way to
288 avoid the need to repeat patterns both with and without such
289 conversions.  Namely you can mark a conversion as being optional
290 with a @code{?}:
292 @smallexample
293 (simplify
294  (eq (convert@@0 @@1) (convert? @@2))
295  (eq @@1 (convert @@2)))
296 @end smallexample
298 which will match both @code{(eq (convert @@1) (convert @@2))} and
299 @code{(eq (convert @@1) @@2)}.  The optional converts are supposed
300 to be all either present or not, thus
301 @code{(eq (convert? @@1) (convert? @@2))} will result in two
302 patterns only.  If you want to match all four combinations you
303 have access to two additional conditional converts as in
304 @code{(eq (convert1? @@1) (convert2? @@2))}.
306 Predicates available from the GCC middle-end need to be made
307 available explicitely via @code{define_predicates}:
309 @smallexample
310 (define_predicates
311  integer_onep integer_zerop integer_all_onesp)
312 @end smallexample
314 You can also define predicates using the pattern matching language
315 and the @code{match} form:
317 @smallexample
318 (match negate_expr_p
319  INTEGER_CST
320  (if (TYPE_OVERFLOW_WRAPS (type)
321       || may_negate_without_overflow_p (t))))
322 (match negate_expr_p
323  (negate @@0))
324 @end smallexample
326 This shows that for @code{match} expressions there is @code{t}
327 available which captures the outermost expression (something
328 not possible in the @code{simplify} context).  As you can see
329 @code{match} has an identifier as first operand which is how
330 you refer to the predicate in patterns.  Multiple @code{match}
331 for the same identifier add additional cases where the predicate
332 matches.
334 Predicates can also match an expression in which case you need
335 to provide a template specifying the identifier and where to
336 get its operands from:
338 @smallexample
339 (match (logical_inverted_value @@0)
340  (eq @@0 integer_zerop))
341 (match (logical_inverted_value @@0)
342  (bit_not truth_valued_p@@0))
343 @end smallexample
345 You can use the above predicate like
347 @smallexample
348 (simplify
349  (bit_and @@0 (logical_inverted_value @@0))
350  @{ build_zero_cst (type); @})
351 @end smallexample
353 Which will match a bitwise and of an operand with its logical
354 inverted value.