diff options
author | Richard Sandiford <richard.sandiford@arm.com> | 2020-12-17 00:15:08 +0000 |
---|---|---|
committer | Richard Sandiford <richard.sandiford@arm.com> | 2020-12-17 00:15:08 +0000 |
commit | a240ea024dd8533d898c8cd779dedd0747bbbf2e (patch) | |
tree | 629eeeeafd6d1c4ca7fbefe69769fe6e29b5d5b2 /gcc/recog.h | |
parent | 0d74260a1f6704da869b87d163f4be31fd0f2b41 (diff) | |
download | gcc-a240ea024dd8533d898c8cd779dedd0747bbbf2e.zip gcc-a240ea024dd8533d898c8cd779dedd0747bbbf2e.tar.gz gcc-a240ea024dd8533d898c8cd779dedd0747bbbf2e.tar.bz2 |
recog: Add an RAII class for undoing insn changes
When using validate_change to make a group of changes, you have
to remember to cancel them if something goes wrong. This patch
adds an RAII class to make that easier. See the comments in the
patch for details and examples.
gcc/
* recog.h (insn_change_watermark): New class.
Diffstat (limited to 'gcc/recog.h')
-rw-r--r-- | gcc/recog.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/gcc/recog.h b/gcc/recog.h index d6af2aa..b8de43b 100644 --- a/gcc/recog.h +++ b/gcc/recog.h @@ -503,6 +503,57 @@ alternative_mask get_preferred_alternatives (rtx_insn *, basic_block); bool check_bool_attrs (rtx_insn *); void recog_init (); + +/* This RAII class can help to undo tentative insn changes on failure. + When an object of the class goes out of scope, it undoes all group + changes that have been made via the validate_change machinery and + not yet confirmed via confirm_change_group. + + For example: + + insn_change_watermark watermark; + validate_change (..., true); // A + ... + if (test) + // Undoes change A. + return false; + ... + validate_change (..., true); // B + ... + if (test) + // Undoes changes A and B. + return false; + ... + confirm_change_group (); + + Code that wants to avoid this behavior can use keep (): + + insn_change_watermark watermark; + validate_change (..., true); // A + ... + if (test) + // Undoes change A. + return false; + ... + watermark.keep (); + validate_change (..., true); // B + ... + if (test) + // Undoes change B, but not A. + return false; + ... + confirm_change_group (); */ +class insn_change_watermark +{ +public: + insn_change_watermark () : m_old_num_changes (num_validated_changes ()) {} + ~insn_change_watermark () { cancel_changes (m_old_num_changes); } + void keep () { m_old_num_changes = num_validated_changes (); } + +private: + int m_old_num_changes; +}; + #endif #endif /* GCC_RECOG_H */ |