hppa: Export main in pr104869.C on hpux
[official-gcc.git] / gcc / recog.h
blob5c801e7bb813fda6c5b22a54076f046a1b73b0f1
1 /* Declarations for interface to insn recognizer and insn-output.cc.
2 Copyright (C) 1987-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_RECOG_H
21 #define GCC_RECOG_H
23 /* For enum tree_code ERROR_MARK. */
24 #include "tree.h"
26 /* Random number that should be large enough for all purposes. Also define
27 a type that has at least MAX_RECOG_ALTERNATIVES + 1 bits, with the extra
28 bit giving an invalid value that can be used to mean "uninitialized". */
29 #define MAX_RECOG_ALTERNATIVES 35
30 typedef uint64_t alternative_mask; /* Keep in sync with genattrtab.cc. */
32 /* A mask of all alternatives. */
33 #define ALL_ALTERNATIVES ((alternative_mask) -1)
35 /* A mask containing just alternative X. */
36 #define ALTERNATIVE_BIT(X) ((alternative_mask) 1 << (X))
38 /* Types of operands. */
39 enum op_type {
40 OP_IN,
41 OP_OUT,
42 OP_INOUT
45 #ifndef GENERATOR_FILE
46 struct operand_alternative
48 /* Pointer to the beginning of the constraint string for this alternative,
49 for easier access by alternative number. */
50 const char *constraint;
52 /* The register class valid for this alternative (possibly NO_REGS). */
53 ENUM_BITFIELD (reg_class) cl : 16;
55 /* "Badness" of this alternative, computed from number of '?' and '!'
56 characters in the constraint string. */
57 unsigned int reject : 16;
59 /* -1 if no matching constraint was found, or an operand number. */
60 int matches : 8;
61 /* The same information, but reversed: -1 if this operand is not
62 matched by any other, or the operand number of the operand that
63 matches this one. */
64 int matched : 8;
66 /* Bit ID is set if the constraint string includes a register constraint with
67 register filter ID. Use test_register_filters (REGISTER_FILTERS, REGNO)
68 to test whether REGNO is a valid start register for the operand. */
69 unsigned int register_filters : MAX (NUM_REGISTER_FILTERS, 1);
71 /* Nonzero if '&' was found in the constraint string. */
72 unsigned int earlyclobber : 1;
73 /* Nonzero if TARGET_MEM_CONSTRAINT was found in the constraint
74 string. */
75 unsigned int memory_ok : 1;
76 /* Nonzero if 'p' was found in the constraint string. */
77 unsigned int is_address : 1;
78 /* Nonzero if 'X' was found in the constraint string, or if the constraint
79 string for this alternative was empty. */
80 unsigned int anything_ok : 1;
83 /* Return the class for operand I of alternative ALT, taking matching
84 constraints into account. */
86 inline enum reg_class
87 alternative_class (const operand_alternative *alt, int i)
89 return alt[i].matches >= 0 ? alt[alt[i].matches].cl : alt[i].cl;
92 /* Return the mask of register filters that should be applied to operand I
93 of alternative ALT, taking matching constraints into account. */
95 inline unsigned int
96 alternative_register_filters (const operand_alternative *alt, int i)
98 return (alt[i].matches >= 0
99 ? alt[alt[i].matches].register_filters
100 : alt[i].register_filters);
102 #endif
104 /* A class for substituting one rtx for another within an instruction,
105 or for recursively simplifying the instruction as-is. Derived classes
106 can record or filter certain decisions. */
108 class insn_propagation : public simplify_context
110 public:
111 /* Assignments for RESULT_FLAGS.
113 UNSIMPLIFIED is true if a substitution has been made inside an rtx
114 X and if neither X nor its parent expressions could be simplified.
116 FIRST_SPARE_RESULT is the first flag available for derived classes. */
117 static const uint16_t UNSIMPLIFIED = 1U << 0;
118 static const uint16_t FIRST_SPARE_RESULT = 1U << 1;
120 insn_propagation (rtx_insn *);
121 insn_propagation (rtx_insn *, rtx, rtx, bool = true);
122 bool apply_to_pattern (rtx *);
123 bool apply_to_rvalue (rtx *);
125 /* Return true if we should accept a substitution into the address of
126 memory expression MEM. Undoing changes OLD_NUM_CHANGES and up restores
127 MEM's original address. */
128 virtual bool check_mem (int /*old_num_changes*/,
129 rtx /*mem*/) { return true; }
131 /* Note that we've simplified OLD_RTX into NEW_RTX. When substituting,
132 this only happens if a substitution occured within OLD_RTX.
133 Undoing OLD_NUM_CHANGES and up will restore the old form of OLD_RTX.
134 OLD_RESULT_FLAGS is the value that RESULT_FLAGS had before processing
135 OLD_RTX. */
136 virtual void note_simplification (int /*old_num_changes*/,
137 uint16_t /*old_result_flags*/,
138 rtx /*old_rtx*/, rtx /*new_rtx*/) {}
140 private:
141 bool apply_to_mem_1 (rtx);
142 bool apply_to_lvalue_1 (rtx);
143 bool apply_to_rvalue_1 (rtx *);
144 bool apply_to_pattern_1 (rtx *);
146 public:
147 /* The instruction that we are simplifying or propagating into. */
148 rtx_insn *insn;
150 /* If FROM is nonnull, we're replacing FROM with TO, otherwise we're
151 just doing a recursive simplification. */
152 rtx from;
153 rtx to;
155 /* The number of times that we have replaced FROM with TO. */
156 unsigned int num_replacements;
158 /* A bitmask of flags that describe the result of the simplificiation;
159 see above for details. */
160 uint16_t result_flags : 16;
162 /* True if we should unshare TO when making the next substitution,
163 false if we can use TO itself. */
164 uint16_t should_unshare : 1;
166 /* True if we should call check_mem after substituting into a memory. */
167 uint16_t should_check_mems : 1;
169 /* True if we should call note_simplification after each simplification. */
170 uint16_t should_note_simplifications : 1;
172 /* For future expansion. */
173 uint16_t spare : 13;
175 /* Gives the reason that a substitution failed, for debug purposes. */
176 const char *failure_reason;
179 /* Try to replace FROM with TO in INSN. SHARED_P is true if TO is shared
180 with other instructions, false if INSN can use TO directly. */
182 inline insn_propagation::insn_propagation (rtx_insn *insn, rtx from, rtx to,
183 bool shared_p)
184 : insn (insn),
185 from (from),
186 to (to),
187 num_replacements (0),
188 result_flags (0),
189 should_unshare (shared_p),
190 should_check_mems (false),
191 should_note_simplifications (false),
192 spare (0),
193 failure_reason (nullptr)
197 /* Try to simplify INSN without performing a substitution. */
199 inline insn_propagation::insn_propagation (rtx_insn *insn)
200 : insn_propagation (insn, NULL_RTX, NULL_RTX)
204 extern void init_recog (void);
205 extern void init_recog_no_volatile (void);
206 extern bool check_asm_operands (rtx);
207 extern int asm_operand_ok (rtx, const char *, const char **);
208 extern bool validate_change (rtx, rtx *, rtx, bool);
209 extern bool validate_unshare_change (rtx, rtx *, rtx, bool);
210 extern bool validate_change_xveclen (rtx, rtx *, int, bool);
211 extern bool canonicalize_change_group (rtx_insn *insn, rtx x);
212 extern bool insn_invalid_p (rtx_insn *, bool);
213 extern bool verify_changes (int);
214 extern void confirm_change_group (void);
215 extern bool apply_change_group (void);
216 extern int num_validated_changes (void);
217 extern void cancel_changes (int);
218 extern void temporarily_undo_changes (int);
219 extern void redo_changes (int);
220 extern bool constrain_operands (int, alternative_mask);
221 extern bool constrain_operands_cached (rtx_insn *, int);
222 extern bool memory_address_addr_space_p (machine_mode, rtx, addr_space_t,
223 code_helper = ERROR_MARK);
224 #define memory_address_p(mode,addr) \
225 memory_address_addr_space_p ((mode), (addr), ADDR_SPACE_GENERIC)
226 extern bool strict_memory_address_addr_space_p (machine_mode, rtx, addr_space_t,
227 code_helper = ERROR_MARK);
228 #define strict_memory_address_p(mode,addr) \
229 strict_memory_address_addr_space_p ((mode), (addr), ADDR_SPACE_GENERIC)
230 extern bool validate_replace_rtx_subexp (rtx, rtx, rtx_insn *, rtx *);
231 extern bool validate_replace_rtx (rtx, rtx, rtx_insn *);
232 extern bool validate_replace_rtx_part (rtx, rtx, rtx *, rtx_insn *);
233 extern bool validate_replace_rtx_part_nosimplify (rtx, rtx, rtx *, rtx_insn *);
234 extern void validate_replace_rtx_group (rtx, rtx, rtx_insn *);
235 extern void validate_replace_src_group (rtx, rtx, rtx_insn *);
236 extern bool validate_simplify_insn (rtx_insn *insn);
237 extern int num_changes_pending (void);
238 extern bool reg_fits_class_p (const_rtx, reg_class_t, int, machine_mode);
239 extern bool valid_insn_p (rtx_insn *);
241 extern bool offsettable_memref_p (rtx);
242 extern bool offsettable_nonstrict_memref_p (rtx);
243 extern bool offsettable_address_addr_space_p (int, machine_mode, rtx,
244 addr_space_t);
245 #define offsettable_address_p(strict,mode,addr) \
246 offsettable_address_addr_space_p ((strict), (mode), (addr), \
247 ADDR_SPACE_GENERIC)
248 extern bool mode_dependent_address_p (rtx, addr_space_t);
250 extern int recog (rtx, rtx_insn *, int *);
251 #ifndef GENERATOR_FILE
252 inline int recog_memoized (rtx_insn *insn);
253 #endif
254 extern void add_clobbers (rtx, int);
255 extern bool added_clobbers_hard_reg_p (int);
256 extern void insn_extract (rtx_insn *);
257 extern void extract_insn (rtx_insn *);
258 extern void extract_constrain_insn (rtx_insn *insn);
259 extern void extract_constrain_insn_cached (rtx_insn *);
260 extern void extract_insn_cached (rtx_insn *);
261 #ifndef GENERATOR_FILE
262 extern void preprocess_constraints (int, int, const char **,
263 operand_alternative *, rtx **);
264 extern const operand_alternative *preprocess_insn_constraints (unsigned int);
265 #endif
266 extern void preprocess_constraints (rtx_insn *);
267 extern rtx_insn *peep2_next_insn (int);
268 extern bool peep2_regno_dead_p (int, int);
269 extern bool peep2_reg_dead_p (int, rtx);
270 #ifdef HARD_CONST
271 extern rtx peep2_find_free_register (int, int, const char *,
272 machine_mode, HARD_REG_SET *);
273 #endif
274 extern rtx_insn *peephole2_insns (rtx, rtx_insn *, int *);
276 extern bool store_data_bypass_p (rtx_insn *, rtx_insn *);
277 extern bool if_test_bypass_p (rtx_insn *, rtx_insn *);
279 extern void copy_frame_info_to_split_insn (rtx_insn *, rtx_insn *);
281 #ifndef GENERATOR_FILE
282 /* Try recognizing the instruction INSN,
283 and return the code number that results.
284 Remember the code so that repeated calls do not
285 need to spend the time for actual rerecognition.
287 This function is the normal interface to instruction recognition.
288 The automatically-generated function `recog' is normally called
289 through this one. */
291 inline int
292 recog_memoized (rtx_insn *insn)
294 if (INSN_CODE (insn) < 0)
295 INSN_CODE (insn) = recog (PATTERN (insn), insn, 0);
296 return INSN_CODE (insn);
298 #endif
300 /* Skip chars until the next ',' or the end of the string. This is
301 useful to skip alternatives in a constraint string. */
302 inline const char *
303 skip_alternative (const char *p)
305 const char *r = p;
306 while (*r != '\0' && *r != ',')
307 r++;
308 if (*r == ',')
309 r++;
310 return r;
313 /* Nonzero means volatile operands are recognized. */
314 extern int volatile_ok;
316 /* RAII class for temporarily setting volatile_ok. */
318 class temporary_volatile_ok
320 public:
321 temporary_volatile_ok (int value) : save_volatile_ok (volatile_ok)
323 volatile_ok = value;
326 ~temporary_volatile_ok () { volatile_ok = save_volatile_ok; }
328 private:
329 temporary_volatile_ok (const temporary_volatile_ok &);
330 int save_volatile_ok;
333 /* Set by constrain_operands to the number of the alternative that
334 matched. */
335 extern int which_alternative;
337 /* The following vectors hold the results from insn_extract. */
339 struct recog_data_d
341 /* It is very tempting to make the 5 operand related arrays into a
342 structure and index on that. However, to be source compatible
343 with all of the existing md file insn constraints and output
344 templates, we need `operand' as a flat array. Without that
345 member, making an array for the rest seems pointless. */
347 /* Gives value of operand N. */
348 rtx operand[MAX_RECOG_OPERANDS];
350 /* Gives location where operand N was found. */
351 rtx *operand_loc[MAX_RECOG_OPERANDS];
353 /* Gives the constraint string for operand N. */
354 const char *constraints[MAX_RECOG_OPERANDS];
356 /* Nonzero if operand N is a match_operator or a match_parallel. */
357 char is_operator[MAX_RECOG_OPERANDS];
359 /* Gives the mode of operand N. */
360 machine_mode operand_mode[MAX_RECOG_OPERANDS];
362 /* Gives the type (in, out, inout) for operand N. */
363 enum op_type operand_type[MAX_RECOG_OPERANDS];
365 /* Gives location where the Nth duplicate-appearance of an operand
366 was found. This is something that matched MATCH_DUP. */
367 rtx *dup_loc[MAX_DUP_OPERANDS];
369 /* Gives the operand number that was duplicated in the Nth
370 duplicate-appearance of an operand. */
371 char dup_num[MAX_DUP_OPERANDS];
373 /* ??? Note that these are `char' instead of `unsigned char' to (try to)
374 avoid certain lossage from K&R C, wherein `unsigned char' default
375 promotes to `unsigned int' instead of `int' as in ISO C. As of 1999,
376 the most common places to bootstrap from K&R C are SunOS and HPUX,
377 both of which have signed characters by default. The only other
378 supported natives that have both K&R C and unsigned characters are
379 ROMP and Irix 3, and neither have been seen for a while, but do
380 continue to consider unsignedness when performing arithmetic inside
381 a comparison. */
383 /* The number of operands of the insn. */
384 char n_operands;
386 /* The number of MATCH_DUPs in the insn. */
387 char n_dups;
389 /* The number of alternatives in the constraints for the insn. */
390 char n_alternatives;
392 /* True if insn is ASM_OPERANDS. */
393 bool is_asm;
395 /* In case we are caching, hold insn data was generated for. */
396 rtx_insn *insn;
399 extern struct recog_data_d recog_data;
401 #ifndef GENERATOR_FILE
402 extern const operand_alternative *recog_op_alt;
404 /* Return a pointer to an array in which index OP describes the constraints
405 on operand OP of the current instruction alternative (which_alternative).
406 Only valid after calling preprocess_constraints and constrain_operands. */
408 inline const operand_alternative *
409 which_op_alt ()
411 gcc_checking_assert (IN_RANGE (which_alternative, 0,
412 recog_data.n_alternatives - 1));
413 return &recog_op_alt[which_alternative * recog_data.n_operands];
415 #endif
417 /* A table defined in insn-output.cc that give information about
418 each insn-code value. */
420 typedef bool (*insn_operand_predicate_fn) (rtx, machine_mode);
421 typedef const char * (*insn_output_fn) (rtx *, rtx_insn *);
423 struct insn_gen_fn
425 typedef void (*stored_funcptr) (void);
427 template<typename ...Ts>
428 rtx_insn *operator() (Ts... args) const
430 typedef rtx_insn *(*funcptr) (decltype ((void) args, NULL_RTX)...);
431 return ((funcptr) func) (args...);
434 // This is for compatibility of code that invokes functions like
435 // (*funcptr) (arg)
436 insn_gen_fn operator * (void) const { return *this; }
438 // The wrapped function pointer must be public and there must not be any
439 // constructors. Otherwise the insn_data_d struct initializers generated
440 // by genoutput.cc will result in static initializer functions, which defeats
441 // the purpose of the generated insn_data_d array.
442 stored_funcptr func;
445 struct insn_operand_data
447 const insn_operand_predicate_fn predicate;
449 const char *const constraint;
451 ENUM_BITFIELD(machine_mode) const mode : 16;
453 const char strict_low;
455 const char is_operator;
457 const char eliminable;
459 const char allows_mem;
462 /* Legal values for insn_data.output_format. Indicate what type of data
463 is stored in insn_data.output. */
464 #define INSN_OUTPUT_FORMAT_NONE 0 /* abort */
465 #define INSN_OUTPUT_FORMAT_SINGLE 1 /* const char * */
466 #define INSN_OUTPUT_FORMAT_MULTI 2 /* const char * const * */
467 #define INSN_OUTPUT_FORMAT_FUNCTION 3 /* const char * (*)(...) */
469 struct insn_data_d
471 const char *const name;
472 #if HAVE_DESIGNATED_UNION_INITIALIZERS
473 union {
474 const char *single;
475 const char *const *multi;
476 insn_output_fn function;
477 } output;
478 #else
479 struct {
480 const char *single;
481 const char *const *multi;
482 insn_output_fn function;
483 } output;
484 #endif
485 const insn_gen_fn genfun;
486 const struct insn_operand_data *const operand;
488 const char n_generator_args;
489 const char n_operands;
490 const char n_dups;
491 const char n_alternatives;
492 const char output_format;
495 extern const struct insn_data_d insn_data[];
496 extern int peep2_current_count;
498 #ifndef GENERATOR_FILE
499 #include "insn-codes.h"
501 /* An enum of boolean attributes that may only depend on the current
502 subtarget, not on things like operands or compiler phase. */
503 enum bool_attr {
504 BA_ENABLED,
505 BA_PREFERRED_FOR_SPEED,
506 BA_PREFERRED_FOR_SIZE,
507 BA_LAST = BA_PREFERRED_FOR_SIZE
510 /* Target-dependent globals. */
511 struct target_recog {
512 bool x_initialized;
513 alternative_mask x_bool_attr_masks[NUM_INSN_CODES][BA_LAST + 1];
514 operand_alternative *x_op_alt[NUM_INSN_CODES];
517 extern struct target_recog default_target_recog;
518 #if SWITCHABLE_TARGET
519 extern struct target_recog *this_target_recog;
520 #else
521 #define this_target_recog (&default_target_recog)
522 #endif
524 alternative_mask get_enabled_alternatives (rtx_insn *);
525 alternative_mask get_preferred_alternatives (rtx_insn *);
526 alternative_mask get_preferred_alternatives (rtx_insn *, basic_block);
527 bool check_bool_attrs (rtx_insn *);
529 void recog_init ();
531 /* This RAII class can help to undo tentative insn changes on failure.
532 When an object of the class goes out of scope, it undoes all group
533 changes that have been made via the validate_change machinery and
534 not yet confirmed via confirm_change_group.
536 For example:
538 insn_change_watermark watermark;
539 validate_change (..., true); // A
541 if (test)
542 // Undoes change A.
543 return false;
545 validate_change (..., true); // B
547 if (test)
548 // Undoes changes A and B.
549 return false;
551 confirm_change_group ();
553 Code that wants to avoid this behavior can use keep ():
555 insn_change_watermark watermark;
556 validate_change (..., true); // A
558 if (test)
559 // Undoes change A.
560 return false;
562 watermark.keep ();
563 validate_change (..., true); // B
565 if (test)
566 // Undoes change B, but not A.
567 return false;
569 confirm_change_group (); */
570 class insn_change_watermark
572 public:
573 insn_change_watermark () : m_old_num_changes (num_validated_changes ()) {}
574 ~insn_change_watermark ();
575 void keep () { m_old_num_changes = num_validated_changes (); }
577 private:
578 int m_old_num_changes;
581 inline insn_change_watermark::~insn_change_watermark ()
583 if (m_old_num_changes < num_validated_changes ())
584 cancel_changes (m_old_num_changes);
587 #endif
589 #endif /* GCC_RECOG_H */