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