@@ -1481,6 +1481,7 @@ OBJS = \
dce.o \
ddg.o \
debug.o \
+ deferred-cst.o \
hierarchical_discriminator.o \
dep-fusion.o \
df-core.o \
@@ -3191,6 +3192,7 @@ GTFILES = $(CPPLIB_H) $(srcdir)/input.h $(srcdir)/coretypes.h \
$(srcdir)/internal-fn.h \
$(srcdir)/calls.cc \
$(srcdir)/analyzer/analyzer-language.cc \
+ $(srcdir)/deferred-cst.cc \
@all_gtfiles@
# Compute the list of GT header files from the corresponding C sources,
@@ -4207,7 +4209,7 @@ PLUGIN_HEADERS = $(TREE_H) $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) \
lcm.h cfgloopmanip.h file-prefix-map.h builtins.def $(INSN_ATTR_H) \
pass-instances.def params.list $(srcdir)/../include/gomp-constants.h \
$(EXPR_H) $(srcdir)/analyzer/*.h $(srcdir)/diagnostics/*.h \
- $(srcdir)/text-art/*.h $(srcdir)/topics/*.h
+ $(srcdir)/text-art/*.h $(srcdir)/topics/*.h deferred-cst.h
# generate the 'build fragment' b-header-vars
s-header-vars: Makefile
@@ -57,6 +57,7 @@ along with GCC; see the file COPYING3. If not see
#include "gcc-rich-location.h"
#include "gcc-urlifier.h"
#include "diagnostics/diagnostics-selftests.h"
+#include "deferred-cst.h"
cpp_reader *parse_in; /* Declared in c-pragma.h. */
@@ -7215,13 +7216,33 @@ c_common_to_target_charset (HOST_WIDE_INT c)
return uc;
}
+/* Helper for fold_offsetof_internal. Add OFFSET to the ACCUMULATOR
+ tree. */
+static void
+accumulate_offset (tree *accumulator, tree offset)
+{
+ gcc_assert (accumulator);
+ gcc_assert (offset);
+
+ offset = convert (sizetype, offset);
+
+ if (*accumulator == NULL_TREE)
+ *accumulator = offset;
+ else
+ *accumulator = size_binop_loc (
+ input_location, PLUS_EXPR, *accumulator, offset);
+}
+
/* Fold an offsetof-like expression. EXPR is a nested sequence of component
references with an INDIRECT_REF of a constant at the bottom; much like the
traditional rendering of offsetof as a macro. TYPE is the desired type of
- the whole expression. Return the folded result. */
+ the whole expression. Return the folded result. NON_CST is set to the
+ sum of all non-constant portions of the offset. DEFERRED is set to the
+ deferred constant portion of the offset. */
-tree
-fold_offsetof (tree expr, tree type, enum tree_code ctx)
+static tree
+fold_offsetof_internal (tree expr, tree type, enum tree_code ctx, tree *non_cst,
+ tree *deferred)
{
tree base, off, t;
tree_code code = TREE_CODE (expr);
@@ -7231,10 +7252,12 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
return expr;
case REALPART_EXPR:
- return fold_offsetof (TREE_OPERAND (expr, 0), type, code);
+ return fold_offsetof_internal (TREE_OPERAND (expr, 0), type, code, non_cst,
+ deferred);
case IMAGPART_EXPR:
- base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
+ base = fold_offsetof_internal (TREE_OPERAND (expr, 0), type, code, non_cst,
+ deferred);
if (base == error_mark_node)
return base;
off = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (expr, 0))));
@@ -7259,7 +7282,8 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
return convert (type, TREE_OPERAND (expr, 0));
case COMPONENT_REF:
- base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
+ base = fold_offsetof_internal (TREE_OPERAND (expr, 0), type, code,
+ non_cst, deferred);
if (base == error_mark_node)
return base;
@@ -7270,13 +7294,21 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
"member %qD", t);
return error_mark_node;
}
- off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
- size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
- / BITS_PER_UNIT));
+ if (deferred_cst_type_p (DECL_CONTEXT (t)))
+ {
+ off = deferred_cst_call_ifn_field_offset (EXPR_LOCATION (expr), t);
+ accumulate_offset (deferred, off);
+ return base;
+ }
+ else
+ off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
+ size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
+ / BITS_PER_UNIT));
break;
case ARRAY_REF:
- base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
+ base = fold_offsetof_internal (TREE_OPERAND (expr, 0), type, code,
+ non_cst, deferred);
if (base == error_mark_node)
return base;
@@ -7328,13 +7360,26 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
t = convert (sizetype, t);
off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
+
+ /* For classification as cst/non-cst, the array index has to be
+ fully folded. */
+ off = c_fully_fold (off, false, NULL);
+
+ if (off == error_mark_node)
+ return off;
+
+ if (!TREE_CONSTANT (off))
+ {
+ accumulate_offset (non_cst, off);
+ return base;
+ }
break;
case COMPOUND_EXPR:
/* Handle static members of volatile structs. */
t = TREE_OPERAND (expr, 1);
gcc_checking_assert (VAR_P (get_base_address (t)));
- return fold_offsetof (t, type);
+ return fold_offsetof_internal (t, type, ERROR_MARK, non_cst, deferred);
default:
gcc_unreachable ();
@@ -7344,6 +7389,48 @@ fold_offsetof (tree expr, tree type, enum tree_code ctx)
return size_binop (PLUS_EXPR, base, convert (type, off));
return fold_build_pointer_plus (base, off);
}
+
+/* Like fold_offsetof_internal, but integrates deferred constant components
+ into the result. If such components exist, the result is wrapped in a
+ DEFERRED_CST_EXPR node. */
+
+tree
+fold_offsetof (tree expr, tree type, enum tree_code ctx)
+{
+ tree res = NULL_TREE;
+ tree non_cst = NULL_TREE;
+ tree deferred = NULL_TREE;
+ tree cst = fold_offsetof_internal (expr, type, ctx, &non_cst, &deferred);
+
+ res = cst;
+
+ if (res == error_mark_node)
+ return res;
+
+ /* DEFERRED_CST_EXPRs contain only ordinary constants and deferred
+ constant components. */
+ if (deferred != NULL_TREE)
+ {
+ if (POINTER_TYPE_P (type))
+ res = fold_build_pointer_plus (res, convert (sizetype, deferred));
+ else
+ res = size_binop (PLUS_EXPR, res, convert (type, deferred));
+
+ res = deferred_cst_build_expr (res);
+ }
+
+ /* Runtime-dependent terms are added only after the deferred
+ boundary has been constructed. */
+ if (non_cst != NULL_TREE)
+ {
+ if (POINTER_TYPE_P (type))
+ res = fold_build_pointer_plus (res, convert (type, non_cst));
+ else
+ res = size_binop (PLUS_EXPR, res, convert (type, non_cst));
+ }
+
+ return res;
+}
/* *PTYPE is an incomplete array. Complete it with a domain based on
INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
@@ -62,6 +62,7 @@ along with GCC; see the file COPYING3. If not see
#include "omp-offload.h" /* For offload_vars. */
#include "c-parser.h"
#include "gcc-urlifier.h"
+#include "deferred-cst.h"
#include "tree-pretty-print.h"
@@ -6628,6 +6629,10 @@ check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
}
else
{
+ *width = deferred_cst_resolve (*width,
+ DEFERRED_CST_CTX_TYPE_FORMATION,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
if (TREE_CODE (*width) != INTEGER_CST)
{
*width = c_fully_fold (*width, false, NULL);
@@ -7399,6 +7404,17 @@ grokdeclarator (const struct c_declarator *declarator,
size = c_fully_fold (size, false, &size_maybe_const);
+ tree deferred_size = size;
+ size = deferred_cst_resolve (deferred_size,
+ DEFERRED_CST_CTX_TYPE_FORMATION,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
+ if (deferred_size != size && TREE_CODE (size) == INTEGER_CST
+ && !TREE_OVERFLOW (size))
+ {
+ size_int_const = true;
+ }
+
if (pedantic && size_maybe_const && integer_zerop (size))
{
if (name)
@@ -10509,6 +10525,10 @@ build_enumerator (location_t decl_loc, location_t loc,
}
else
{
+ value = deferred_cst_resolve (value,
+ DEFERRED_CST_CTX_ENUMERATOR_DECL,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
if (TREE_CODE (value) != INTEGER_CST)
{
value = c_fully_fold (value, false, NULL);
@@ -13352,6 +13372,10 @@ declspecs_add_alignas (location_t loc,
if (align == error_mark_node)
return specs;
+ align = deferred_cst_resolve (align,
+ DEFERRED_CST_CTX_ALIGNMENT,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
/* Only accept the alignment if it's valid and greater than
the current one. Zero is invalid but by C11 required to
be silently ignored. */
@@ -26,9 +26,11 @@ along with GCC; see the file COPYING3. If not see
#include "c-tree.h"
#include "intl.h"
#include "gimplify.h"
+#include "deferred-cst.h"
static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool,
bool);
+static tree c_fully_fold_deferred_cst_internal (tree expr);
/* If DISABLE is true, stop issuing warnings. This is used when
parsing code that we know will not be executed. This function may
@@ -123,6 +125,7 @@ c_fully_fold (tree expr, bool in_init, bool *maybe_const, bool lval)
}
ret = c_fully_fold_internal (expr, in_init, maybe_const,
&maybe_const_itself, false, lval);
+ ret = c_fully_fold_deferred_cst_internal (ret);
if (eptype)
ret = fold_convert_loc (loc, eptype, ret);
*maybe_const &= maybe_const_itself;
@@ -718,6 +721,186 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
return ret;
}
+/* Return true if CODE is an operation which may be absorbed into a
+ DEFERRED_CST_EXPR. */
+
+static bool
+deferred_cst_operator_p (enum tree_code code)
+{
+ switch (TREE_CODE_CLASS (code))
+ {
+ case tcc_unary:
+ case tcc_binary:
+ case tcc_comparison:
+ return true;
+
+ default:
+ break;
+ }
+
+ switch (code)
+ {
+ case COND_EXPR:
+ case VEC_COND_EXPR:
+ return true;
+
+ default:
+ return false;
+ }
+}
+
+/* Internal helper for c_fully_fold. Fold surrounding constant
+ operations into DEFERRED_CST_EXPR nodes.
+
+ An operation is absorbed when:
+
+ - its tree code is explicitly supported by
+ deferred_cst_operator_p;
+ - every operand is constant-like; and
+ - at least one operand is a DEFERRED_CST_EXPR.
+
+ Existing deferred wrappers are removed when an operation is absorbed,
+ giving a single outer DEFERRED_CST_EXPR. For example:
+
+ DEFERRED_CST_EXPR (A) + 1
+ -> DEFERRED_CST_EXPR (A + 1)
+
+ (DEFERRED_CST_EXPR (A) + 1) * 2
+ -> DEFERRED_CST_EXPR ((A + 1) * 2)
+
+ DEFERRED_CST_EXPR (A) + DEFERRED_CST_EXPR (B)
+ -> DEFERRED_CST_EXPR (A + B)
+
+ DEFERRED_CST_EXPR (C) ? 1 : 2
+ -> DEFERRED_CST_EXPR (C ? 1 : 2)
+
+ An operation involving a nonconstant operand is not absorbed:
+
+ X + DEFERRED_CST_EXPR (A)
+ -> X + DEFERRED_CST_EXPR (A) */
+
+static tree
+c_fully_fold_deferred_cst_internal (tree expr)
+{
+ if (expr == NULL_TREE)
+ return NULL_TREE;
+
+ enum tree_code code = TREE_CODE (expr);
+
+ /* The true constants inside deferred constants can be folded here,
+ while the deferred components are represented as calls to
+ internal functions and thus can not and should not be folded
+ here. */
+ if (code == DEFERRED_CST_EXPR)
+ {
+ tree old_inner = TREE_OPERAND (expr, 0);
+
+ bool maybe_const_operands = true;
+ bool maybe_const_itself = true;
+
+ tree inner
+ = c_fully_fold_internal (old_inner, false,
+ &maybe_const_operands,
+ &maybe_const_itself,
+ false, false);
+
+ if (inner == old_inner)
+ return expr;
+
+ /* Some arithmetic operations can eliminate all deferred leaves,
+ for example: C*0 or C-C. */
+ if (CONSTANT_CLASS_P (inner))
+ return inner;
+
+ return deferred_cst_build_expr (inner);
+ }
+
+ /* Unsupported operations form a boundary for deferred folding. */
+ if (!deferred_cst_operator_p (code))
+ return expr;
+
+ int nops = TREE_OPERAND_LENGTH (expr);
+
+ /* Every currently-supported operator has at most three operands. */
+ gcc_assert (nops >= 1 && nops <= 3);
+
+ tree ops[3];
+ bool changed = false;
+ bool has_deferred = false;
+ bool all_constant = true;
+ bool has_side_effects = false;
+
+ for (int i = 0; i < nops; ++i)
+ {
+ tree old_op = TREE_OPERAND (expr, i);
+ tree op = c_fully_fold_deferred_cst_internal (old_op);
+
+ ops[i] = op;
+
+ if (op != old_op)
+ changed = true;
+
+ if (TREE_CODE (op) == DEFERRED_CST_EXPR)
+ has_deferred = true;
+ else if (!TREE_CONSTANT (op))
+ all_constant = false;
+
+ if (TREE_SIDE_EFFECTS (op))
+ has_side_effects = true;
+ }
+
+ /* There is no deferred expression directly participating in this
+ operation, or there are non-constant operands or operands with
+ side effects involved, so the operation itself must not become
+ deferred.
+
+ We may nevertheless need to propagate a transformation made in one
+ of the operands. */
+ if (!has_deferred || !all_constant || has_side_effects)
+ {
+ if (!changed)
+ return expr;
+
+ tree copy = copy_node (expr);
+ for (int i = 0; i < nops; i++)
+ TREE_OPERAND (copy, i) = ops[i];
+
+ return fold (copy);
+ }
+
+ /* Every operand is constant-like and at least one operand is
+ deferred. Move this complete operation inside one deferred
+ wrapper. */
+
+ tree inner = copy_node (expr);
+ for (int i = 0; i < nops; i++)
+ {
+ bool op_deferred_cst = ops[i] != NULL_TREE
+ && TREE_CODE (ops[i]) == DEFERRED_CST_EXPR;
+ TREE_OPERAND (inner, i) = op_deferred_cst
+ ? TREE_OPERAND (ops[i], 0) : ops[i];
+ }
+
+ bool maybe_const_operands = true;
+ bool maybe_const_itself = true;
+
+ /* The true constants inside deferred constants can be folded here,
+ while the deferred components are represented as calls to
+ internal functions and thus can not and should not be folded
+ here. */
+ inner = c_fully_fold_internal (inner, false,
+ &maybe_const_operands,
+ &maybe_const_itself,
+ false, false);
+
+ /* Some arithmetic operations can eliminate all deferred leaves,
+ for example: C*0 or C-C. */
+ if (CONSTANT_CLASS_P (inner))
+ return inner;
+
+ return deferred_cst_build_expr (inner);
+}
+
/* Fold X for consideration by one of the warning functions when checking
whether an expression has a constant value. */
@@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see
#include "c-objc-common.h"
#include "c-family/c-pragma.h"
#include "c-parser.h"
+#include "deferred-cst.h"
enum c_language_kind c_language = clk_c;
@@ -53,6 +54,10 @@ enum c_language_kind c_language = clk_c;
#undef LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE
#define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE c_get_sarif_source_language
+#undef LANG_HOOKS_DEFERRED_CST_CAPABILITIES
+#define LANG_HOOKS_DEFERRED_CST_CAPABILITIES \
+ (DEFERRED_CST_CAP_ENUM | DEFERRED_CST_CAP_OFFSETOF)
+
/* Each front end provides its own lang hook initializer. */
struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
@@ -77,6 +77,7 @@ along with GCC; see the file COPYING3. If not see
#include "asan.h"
#include "c-family/c-ubsan.h"
#include "gcc-urlifier.h"
+#include "deferred-cst.h"
/* We need to walk over decls with incomplete struct/union/enum types
after parsing the whole translation unit.
@@ -3571,6 +3572,11 @@ c_parser_static_assert_declaration_no_semi (c_parser *parser)
error_at (value_loc, "expression in static assertion is not an integer");
return;
}
+
+ value = deferred_cst_resolve (value,
+ DEFERRED_CST_CTX_STATIC_ASSERT,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
if (TREE_CODE (value) != INTEGER_CST)
{
value = c_fully_fold (value, false, NULL);
@@ -55,6 +55,7 @@ along with GCC; see the file COPYING3. If not see
#include "realmpfr.h"
#include "tree-pretty-print-markup.h"
#include "gcc-urlifier.h"
+#include "deferred-cst.h"
/* Possible cases of implicit conversions. Used to select diagnostic messages
and control folding initializers in convert_for_assignment. */
@@ -2293,6 +2294,17 @@ c_size_in_bytes (const_tree type)
/ BITS_PER_UNIT));
}
+/* Return true if INIT is a valid deferred constant initializer. */
+static bool
+deferred_constant_initializer_p (tree init)
+{
+ if (!init || !deferred_cst_present_p (init))
+ return false;
+
+ return initializer_constant_valid_p (init, TREE_TYPE (init))
+ != NULL_TREE;
+}
+
/* Return either DECL or its known constant value (if it has one). */
tree
@@ -2307,7 +2319,8 @@ decl_constant_value_1 (tree decl, bool in_init)
/* This is invalid if initial value is not constant.
If it has either a function call, a memory reference,
or a variable, then re-evaluating it could give different results. */
- && TREE_CONSTANT (DECL_INITIAL (decl))
+ && (TREE_CONSTANT (DECL_INITIAL (decl))
+ || deferred_constant_initializer_p (DECL_INITIAL (decl)))
/* Check for cases where this is sub-optimal, even though valid. */
&& (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
return DECL_INITIAL (decl);
@@ -3865,6 +3878,33 @@ mark_decl_used (tree ref, bool address)
}
}
+/* Return true if DECL is a literal of an enum type that is marked for
+ constant deferral. */
+static bool
+deferred_cst_enum_literal_p (tree decl)
+{
+ if (TREE_CODE (decl) != CONST_DECL)
+ return false;
+
+ tree enum_type = TREE_TYPE (decl);
+
+ return (enum_type
+ && TREE_CODE (enum_type) == ENUMERAL_TYPE
+ && deferred_cst_type_p (enum_type));
+}
+
+/* Build a deferred constant for the enumeration constant DECL. */
+static tree
+deferred_cst_build_enum_literal (tree decl)
+{
+ tree call
+ = deferred_cst_call_ifn_enum_value (DECL_SOURCE_LOCATION (decl), decl);
+
+ gcc_assert (call != error_mark_node);
+
+ return deferred_cst_build_expr (call);
+}
+
/* Build an external reference to identifier ID. FUN indicates
whether this will be used for a function call. LOC is the source
@@ -3938,8 +3978,13 @@ build_external_ref (location_t loc, tree id, bool fun, tree *type)
inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
}
- ref = DECL_INITIAL (ref);
- TREE_CONSTANT (ref) = 1;
+ if (deferred_cst_enum_literal_p (ref))
+ ref = deferred_cst_build_enum_literal (ref);
+ else
+ {
+ ref = DECL_INITIAL (ref);
+ TREE_CONSTANT (ref) = 1;
+ }
}
/* C99 6.7.4p3: An inline definition of a function with external
linkage ... shall not contain a reference to an identifier with
@@ -10269,7 +10314,9 @@ digest_init (location_t init_loc, tree decl, tree type, tree init,
/* Check to see if we have already given an error message. */
if (inside_init == error_mark_node)
;
- else if (require_constant && !TREE_CONSTANT (inside_init))
+ else if (require_constant
+ && !TREE_CONSTANT (inside_init)
+ && !deferred_constant_initializer_p (inside_init))
{
error_init (init_loc, "initializer element is not constant");
inside_init = error_mark_node;
@@ -11265,6 +11312,14 @@ set_init_index (location_t loc, tree first, tree last,
return;
}
+ first = deferred_cst_resolve (first,
+ DEFERRED_CST_CTX_ARRAY_DESIGNATOR,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
+ last = deferred_cst_resolve (last,
+ DEFERRED_CST_CTX_ARRAY_DESIGNATOR,
+ DEFERRED_CST_CONSEQUENCE_ERROR);
+
if (TREE_CODE (first) != INTEGER_CST)
{
first = c_fully_fold (first, false, NULL);
@@ -13590,6 +13645,16 @@ c_finish_return (location_t loc, tree retval, tree origtype, bool musttail_p)
return add_stmt (ret_stmt);
}
+/* A case label whose value cannot be represented by CASE_LABEL_EXPR
+ because at least one bound is a deferred constant. */
+struct c_deferred_case
+{
+ location_t loc;
+ tree low_value;
+ tree high_value;
+ tree label;
+};
+
struct c_switch {
/* The SWITCH_STMT being built. */
tree switch_stmt;
@@ -13618,6 +13683,11 @@ struct c_switch {
/* Remember whether the controlling expression had boolean type
before integer promotions for the sake of -Wswitch-bool. */
bool bool_cond_p;
+
+ /* Deferred constant cases cannot be represented by CASE_LABEL_EXPR,
+ so turn them into conditional edges that jump into the switch
+ body. */
+ vec<c_deferred_case> deferred_cases;
};
/* A stack of the currently active switch statements. The innermost
@@ -13696,11 +13766,76 @@ c_start_switch (location_t switch_loc,
cs->break_stmt_seen_p = false;
cs->bool_cond_p = bool_cond_p;
cs->next = c_switch_stack;
+ cs->deferred_cases.create (0);
c_switch_stack = cs;
return add_stmt (cs->switch_stmt);
}
+/* Process a case label, located at LOC, for the range LOW_VALUE
+ ... HIGH_VALUE with at least one boundary of range being a deferred
+ constant. If HIGH_VALUE is NULL_TREE, then the case label was
+ declared using the usual C/C++ syntax, rather than the GNU case
+ range extension. Returns the LABEL_EXPR created, or ERROR_MARK_NODE
+ if no LABEL_EXPR is created. ATTRS are the attributes to be applied
+ to the label. */
+static tree
+c_add_deferred_case_label (location_t loc, tree low_value,
+ tree high_value, tree attrs)
+{
+ struct c_switch *cs = c_switch_stack;
+ tree cond = SWITCH_STMT_COND (cs->switch_stmt);
+ tree type = TREE_TYPE (cond);
+
+ gcc_assert (low_value);
+ gcc_assert (deferred_cst_present_p (low_value)
+ || (high_value && deferred_cst_present_p (high_value)));
+
+ /* A deferred case is still required to have integral type. */
+ if (!INTEGRAL_TYPE_P (TREE_TYPE (low_value))
+ || (high_value && !INTEGRAL_TYPE_P (TREE_TYPE (high_value))))
+ {
+ error_at (loc, "case label does not have integer type");
+ return error_mark_node;
+ }
+
+ /* Apply the same conversion to the promoted type of the switch
+ expression that an ordinary case value receives. */
+ low_value = convert_and_check (loc, type, low_value);
+ if (low_value == error_mark_node)
+ return error_mark_node;
+ low_value = c_fully_fold (low_value, false, NULL);
+
+ if (high_value)
+ {
+ high_value = convert_and_check (loc, type, high_value);
+ if (high_value == error_mark_node)
+ return error_mark_node;
+ high_value = c_fully_fold (high_value, false, NULL);
+ }
+
+ if (low_value == error_mark_node
+ || (high_value && high_value == error_mark_node))
+ return error_mark_node;
+
+ tree label = create_artificial_label (loc);
+ decl_attributes (&label, attrs, 0);
+
+ c_deferred_case dc;
+ dc.loc = loc;
+ dc.low_value = low_value;
+ dc.high_value = high_value;
+ dc.label = label;
+
+ /* Do not add deferred cases to CS->cases. Their final values are not
+ known here, so they cannot participate in the compile-time overlap
+ checks performed for ordinary case labels. Deferred cases are
+ tested in source order before the remaining switch dispatch. */
+ cs->deferred_cases.safe_push (dc);
+
+ return add_stmt (build_stmt (loc, LABEL_EXPR, label));
+}
+
/* Process a case label at location LOC, with attributes ATTRS. */
tree
@@ -13708,6 +13843,14 @@ do_case (location_t loc, tree low_value, tree high_value, tree attrs)
{
tree label = NULL_TREE;
+ low_value = deferred_cst_resolve (low_value,
+ DEFERRED_CST_CTX_SWITCH_CASE,
+ DEFERRED_CST_CONSEQUENCE_CODECHANGE);
+
+ high_value = deferred_cst_resolve (high_value,
+ DEFERRED_CST_CTX_SWITCH_CASE,
+ DEFERRED_CST_CONSEQUENCE_CODECHANGE);
+
if (low_value && TREE_CODE (low_value) != INTEGER_CST)
{
low_value = c_fully_fold (low_value, false, NULL);
@@ -13738,14 +13881,82 @@ do_case (location_t loc, tree low_value, tree high_value, tree attrs)
loc))
return NULL_TREE;
- label = c_add_case_label (loc, c_switch_stack->cases,
- SWITCH_STMT_COND (c_switch_stack->switch_stmt),
- low_value, high_value, attrs);
+ if ((low_value && deferred_cst_present_p (low_value))
+ || (high_value && deferred_cst_present_p (high_value)))
+ label = c_add_deferred_case_label (loc, low_value, high_value, attrs);
+ else
+ label = c_add_case_label (loc, c_switch_stack->cases,
+ SWITCH_STMT_COND (c_switch_stack->switch_stmt),
+ low_value, high_value, attrs);
+
if (label == error_mark_node)
label = NULL_TREE;
return label;
}
+/* Build the dispatch for deferred case labels in CS.
+
+ CASE_LABEL_EXPR requires its bounds to be INTEGER_CSTs, so a case
+ whose value is deferred cannot be represented as an ordinary case
+ label. Instead, test deferred cases in source order before the
+ remaining switch dispatch and jump directly to artificial labels in
+ the switch body.
+
+ The resulting COMPOUND_EXPR is used as the switch condition; its
+ final operand is the saved controlling expression used by the
+ ordinary, non-deferred cases. */
+static tree
+c_build_deferred_case_dispatch (struct c_switch *cs)
+{
+ tree cond = SWITCH_STMT_COND (cs->switch_stmt);
+ tree saved_cond = save_expr (cond);
+ tree dispatch = NULL_TREE;
+
+ for (unsigned int i = 0; i < cs->deferred_cases.length (); ++i)
+ {
+ c_deferred_case &dc = cs->deferred_cases[i];
+
+ tree test;
+
+ if (dc.high_value)
+ {
+ tree ge = build2_loc (dc.loc, GE_EXPR,
+ boolean_type_node,
+ saved_cond, dc.low_value);
+ tree le = build2_loc (dc.loc, LE_EXPR,
+ boolean_type_node,
+ saved_cond, dc.high_value);
+
+ test = build2_loc (dc.loc, TRUTH_ANDIF_EXPR,
+ boolean_type_node, ge, le);
+ }
+ else
+ test = build2_loc (dc.loc, EQ_EXPR,
+ boolean_type_node,
+ saved_cond, dc.low_value);
+
+ tree jump = build1_loc (dc.loc, GOTO_EXPR,
+ void_type_node, dc.label);
+
+ tree branch = build3_loc (dc.loc, COND_EXPR,
+ void_type_node,
+ test, jump, void_node);
+
+ if (dispatch)
+ dispatch = build2_loc (dc.loc, COMPOUND_EXPR,
+ void_type_node, dispatch, branch);
+ else
+ dispatch = branch;
+ }
+
+ gcc_assert (dispatch);
+
+ return build2_loc (EXPR_LOCATION (cs->switch_stmt),
+ COMPOUND_EXPR,
+ TREE_TYPE (saved_cond),
+ dispatch, saved_cond);
+}
+
/* Finish the switch statement. TYPE is the original type of the
controlling expression of the switch, or NULL_TREE. */
@@ -13757,6 +13968,10 @@ c_finish_switch (tree body, tree type)
SWITCH_STMT_BODY (cs->switch_stmt) = body;
+ if (!cs->deferred_cases.is_empty ())
+ SWITCH_STMT_COND (cs->switch_stmt)
+ = c_build_deferred_case_dispatch (cs);
+
/* Emit warnings as needed. */
switch_location = EXPR_LOCATION (cs->switch_stmt);
c_do_switch_warnings (cs->cases, switch_location,
@@ -13768,6 +13983,7 @@ c_finish_switch (tree body, tree type)
SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = !cs->break_stmt_seen_p;
/* Pop the stack. */
+ cs->deferred_cases.release ();
c_switch_stack = cs->next;
splay_tree_delete (cs->cases);
c_release_switch_bindings (cs->bindings);
new file mode 100644
@@ -0,0 +1,451 @@
+/* Deferred constant expression support for GCC.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+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 3, 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 COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "stringpool.h"
+#include "fold-const.h"
+#include "internal-fn.h"
+#include "attribs.h"
+#include "tree-pass.h"
+#include "context.h"
+#include "cgraph.h"
+#include "function.h"
+#include "cfg.h"
+#include "basic-block.h"
+#include "gimple.h"
+#include "gimple-iterator.h"
+#include "gimple-walk.h"
+#include "diagnostic.h"
+
+#include "deferred-cst.h"
+
+struct deferred_cst_type_hasher : ggc_cache_ptr_hash<tree_node>
+{
+ static hashval_t
+ hash (tree t)
+ {
+ return htab_hash_pointer (t);
+ }
+};
+
+/* Set of types marked for constant deferral. */
+static GTY((cache)) hash_table<deferred_cst_type_hasher> *deferred_cst_types;
+
+/* Weak mappings between canonical STRING_CST tokens used as arguments to
+ IFN_DEFERRED_CST_* calls to the trees associated with those calls.
+
+ Each FIELD_DECL or enum CONST_DECL has at most one token. Thus repeated
+ IFN calls referring to the same declaration have equal arguments and,
+ since the IFNs are ECF_CONST, can compare equal during folding.
+
+ field_offset_decls: token -> FIELD_DECL
+ enum_value_decls: token -> CONST_DECL
+
+ field_offset_tokens: FIELD_DECL -> token
+ enum_value_tokens: CONST_DECL -> token. */
+
+static GTY((cache)) tree_cache_map *field_offset_decls;
+static GTY((cache)) tree_cache_map *enum_value_decls;
+
+static GTY((cache)) tree_cache_map *field_offset_tokens;
+static GTY((cache)) tree_cache_map *enum_value_tokens;
+
+/* Used only to make STRING_CST contents unique. The actual string
+ values have no semantic meaning; the STRING_CST nodes themselves
+ are the metadata tokens. */
+static unsigned HOST_WIDE_INT next_token = 0;
+
+static deferred_cst_resolver_fn deferred_cst_resolver;
+
+void
+deferred_cst_register_resolver (deferred_cst_resolver_fn resolver)
+{
+ gcc_assert (resolver);
+ deferred_cst_resolver = resolver;
+}
+
+tree
+deferred_cst_resolve (tree expr, deferred_cst_ctx ctx,
+ deferred_cst_consequence consequence)
+{
+ if (!deferred_cst_resolver || !deferred_cst_present_p (expr))
+ return expr;
+
+ struct deferred_cst_resolve_info info;
+ info.ctx = ctx;
+ info.consequence = consequence;
+
+ tree resolved = deferred_cst_resolver (expr, &info);
+ if (resolved == NULL_TREE)
+ return expr;
+
+ return resolved;
+}
+
+static bool
+markable_type_p (tree type)
+{
+ if (!type)
+ return false;
+
+ switch (TREE_CODE (type))
+ {
+ case RECORD_TYPE:
+ case ENUMERAL_TYPE:
+ return true;
+ default:
+ return false;
+ }
+}
+
+void
+deferred_cst_mark_type (tree type)
+{
+ type = TYPE_MAIN_VARIANT (type);
+
+ if (!markable_type_p (type))
+ internal_error (
+ "attempted to mark unsupported type for constant deferral");
+
+ if (!deferred_cst_types)
+ deferred_cst_types
+ = hash_table<deferred_cst_type_hasher>::create_ggc (13);
+
+ tree *slot = deferred_cst_types->find_slot (type, INSERT);
+ if (*slot == NULL_TREE)
+ *slot = type;
+}
+
+bool
+deferred_cst_type_p (tree type)
+{
+ type = TYPE_MAIN_VARIANT (type);
+ return (deferred_cst_types
+ && deferred_cst_types->find (type) != NULL);
+}
+
+tree
+deferred_cst_build_expr (tree expr)
+{
+ return build1_loc (EXPR_LOCATION (expr), DEFERRED_CST_EXPR,
+ TREE_TYPE (expr), expr);
+}
+
+/* Create a unique STRING_CST to use as a metadata token. */
+static tree
+make_token ()
+{
+ char buf[64];
+
+ int len = snprintf (buf, sizeof (buf),
+ "__deferred_cst_token_" HOST_WIDE_INT_PRINT_UNSIGNED,
+ next_token++);
+
+ gcc_assert (len > 0);
+ gcc_assert ((unsigned int) len < sizeof (buf));
+
+ /* Include the trailing NUL. */
+ unsigned int string_len = len + 1;
+
+ tree token = build_string (string_len, buf);
+
+ TREE_TYPE (token)
+ = build_array_type_nelts (char_type_node, string_len);
+
+ return token;
+}
+
+/* Return the canonical token for VALUE. If VALUE has not been seen
+ before, create a token and record both directions of the mapping. */
+static tree
+register_ifn_entry (tree_cache_map **values,
+ tree_cache_map **tokens,
+ tree value)
+{
+ gcc_assert (values);
+ gcc_assert (tokens);
+
+ if (*tokens)
+ {
+ tree *slot = (*tokens)->get (value);
+ if (slot)
+ return *slot;
+ }
+
+ tree token = make_token ();
+
+ if (!*values)
+ *values = tree_cache_map::create_ggc ();
+
+ if (!*tokens)
+ *tokens = tree_cache_map::create_ggc ();
+
+ bool existed = (*values)->put (token, value);
+ gcc_assert (!existed);
+
+ existed = (*tokens)->put (value, token);
+ gcc_assert (!existed);
+
+ return token;
+}
+
+/* Look up the value associated with TOKEN in MAP. */
+static tree
+lookup_ifn_entry (tree_cache_map **map, tree token)
+{
+ if (!token || !map || !*map)
+ return NULL_TREE;
+
+ tree *slot = (*map)->get (token);
+ return slot ? *slot : NULL_TREE;
+}
+
+/* Create a call to IFN_DEFERRED_CST_FIELD_OFFSET for the physical,
+ addressable FIELD whose byte offset is deferred.
+
+ FIELD must not be a bit-field. Callers handling bit-fields must pass
+ DECL_BIT_FIELD_REPRESENTATIVE (field).
+
+ Internally, these call hold the field declarations in a side-map
+ keyed by tokens used as call arguments. */
+tree
+deferred_cst_call_ifn_field_offset (location_t loc, tree field)
+{
+ if (!field || TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD (field))
+ return error_mark_node;
+
+ tree token = register_ifn_entry (&field_offset_decls,
+ &field_offset_tokens, field);
+ return build_call_expr_internal_loc (loc,
+ IFN_DEFERRED_CST_FIELD_OFFSET,
+ sizetype, 1, token);
+}
+
+tree
+deferred_cst_ifn_field_offset_field (tree token)
+{
+ return lookup_ifn_entry (&field_offset_decls, token);
+}
+
+/* Create a call to IFN_DEFERRED_CST_ENUM_VALUE for an enum value
+ declaration DECL. */
+tree
+deferred_cst_call_ifn_enum_value (location_t loc, tree decl)
+{
+ if (!decl || TREE_CODE (decl) != CONST_DECL)
+ return error_mark_node;
+
+ tree enum_type = TREE_TYPE (decl);
+
+ if (!enum_type || TREE_CODE (enum_type) != ENUMERAL_TYPE)
+ return error_mark_node;
+
+ tree init_type = TREE_TYPE (DECL_INITIAL (decl));
+
+ tree token = register_ifn_entry (&enum_value_decls,
+ &enum_value_tokens, decl);
+ return build_call_expr_internal_loc (loc,
+ IFN_DEFERRED_CST_ENUM_VALUE,
+ init_type, 1, token);
+}
+
+tree
+deferred_cst_ifn_enum_value_decl (tree token)
+{
+ return lookup_ifn_entry (&enum_value_decls, token);
+}
+
+static tree
+find_deferred_cst_node (tree *tp, int *walk_subtrees, void *)
+{
+ tree node = *tp;
+
+ if (!node)
+ return NULL_TREE;
+
+ switch (TREE_CODE (node))
+ {
+ case DEFERRED_CST_EXPR:
+ *walk_subtrees = 0;
+ return node;
+
+ default:
+ break;
+ }
+
+ /* Declarations and types are metadata here. Do not walk into their
+ contexts, types, or initializers. */
+ if (DECL_P (node) || TYPE_P (node))
+ *walk_subtrees = 0;
+
+ return NULL_TREE;
+}
+
+bool
+deferred_cst_present_p (tree expr)
+{
+ if (!expr)
+ return false;
+
+ tree root = expr;
+
+ return walk_tree_without_duplicates (
+ &root,
+ find_deferred_cst_node,
+ NULL)
+ != NULL_TREE;
+}
+
+/* Walker to go through functions and initializers. ICE if
+ DEFERRED_CST_EXPR is encountered. */
+static tree
+deferred_cst_barrier_walk (tree *tp, int *walk_subtrees, void *)
+{
+ tree node = *tp;
+
+ if (!node)
+ return NULL_TREE;
+
+ if (TYPE_P (node) || DECL_P (node))
+ {
+ *walk_subtrees = 0;
+ return NULL_TREE;
+ }
+
+ if (TREE_CODE (node) == DEFERRED_CST_EXPR)
+ internal_error ("unresolved deferred constant reached barrier");
+
+ return NULL_TREE;
+}
+
+static void
+deferred_cst_barrier_function (function *fn)
+{
+ basic_block bb;
+
+ FOR_EACH_BB_FN (bb, fn)
+ {
+ for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
+ !gsi_end_p (gsi);
+ gsi_next (&gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+
+ for (unsigned int i = 0; i < gimple_num_ops (stmt); ++i)
+ {
+ tree *operand = gimple_op_ptr (stmt, i);
+
+ if (!operand || !*operand)
+ continue;
+
+ walk_tree (operand,
+ deferred_cst_barrier_walk,
+ NULL,
+ NULL);
+ }
+ }
+ }
+}
+
+static tree
+deferred_cst_barrier_init (tree init)
+{
+ if (!init)
+ return init;
+
+ walk_tree (&init, deferred_cst_barrier_walk, NULL, NULL);
+ return init;
+}
+
+static void
+deferred_cst_barrier_functions ()
+{
+ cgraph_node *node;
+
+ FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+ {
+ function *fn = DECL_STRUCT_FUNCTION (node->decl);
+ gcc_assert (fn);
+
+ push_cfun (fn);
+ deferred_cst_barrier_function (fn);
+ pop_cfun ();
+ }
+}
+
+static void
+deferred_cst_barrier_inits ()
+{
+ varpool_node *vnode;
+
+ FOR_EACH_VARIABLE (vnode)
+ {
+ tree decl = vnode->decl;
+ tree init = DECL_INITIAL (decl);
+
+ if (!init || init == error_mark_node)
+ continue;
+
+ tree clean_init = deferred_cst_barrier_init (init);
+ if (clean_init != init)
+ DECL_INITIAL (decl) = clean_init;
+ }
+}
+
+namespace {
+
+const pass_data pass_data_deferred_cst_barrier =
+{
+ SIMPLE_IPA_PASS,
+ "deferred-cst-barrier",
+ OPTGROUP_NONE,
+ TV_NONE,
+ 0, 0, 0, 0, 0
+};
+
+class pass_deferred_cst_barrier : public simple_ipa_opt_pass
+{
+public:
+ pass_deferred_cst_barrier (gcc::context *ctxt)
+ : simple_ipa_opt_pass (pass_data_deferred_cst_barrier, ctxt)
+ {}
+
+ unsigned int execute (function *) final override;
+};
+
+unsigned int
+pass_deferred_cst_barrier::execute (function *)
+{
+ deferred_cst_barrier_functions ();
+ deferred_cst_barrier_inits ();
+ return 0;
+}
+
+} // anonymous namespace
+
+simple_ipa_opt_pass *
+make_pass_deferred_cst_barrier (gcc::context *ctxt)
+{
+ return new pass_deferred_cst_barrier (ctxt);
+}
+
+#include "gt-deferred-cst.h"
new file mode 100644
@@ -0,0 +1,98 @@
+/* Deferred constant expression support for GCC.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+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 3, 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 COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#ifndef GCC_DEFERRED_CST_H
+#define GCC_DEFERRED_CST_H
+
+/* Deferred-constant origins supported by a language frontend. */
+enum deferred_cst_capability
+{
+ DEFERRED_CST_CAP_ENUM = 1 << 0,
+ DEFERRED_CST_CAP_OFFSETOF = 1 << 1
+};
+
+/* Some contexts do not permit constant deferral. When encountering
+ deferred constants, they call the registered deferred-cst-resolver.
+
+ Consumers can use deferred_cst_register_resolver to set the current
+ RESOLVER callback, which should return either a resolved expression,
+ or NULL_TREE if no changes where made.
+
+ Frontends call deferred_cst_resolve where deferred constants are not
+ permitted or would have meaningful effects on code generation. It
+ returns the replacement expression, which may be the same as the input
+ if no changes were made. */
+
+enum deferred_cst_consequence {
+ DEFERRED_CST_CONSEQUENCE_ERROR,
+ DEFERRED_CST_CONSEQUENCE_CODECHANGE
+};
+
+enum deferred_cst_ctx {
+ DEFERRED_CST_CTX_TYPE_FORMATION,
+ DEFERRED_CST_CTX_STATIC_ASSERT,
+ DEFERRED_CST_CTX_ARRAY_DESIGNATOR,
+ DEFERRED_CST_CTX_ENUMERATOR_DECL,
+ DEFERRED_CST_CTX_ALIGNMENT,
+ DEFERRED_CST_CTX_SWITCH_CASE,
+};
+
+struct deferred_cst_resolve_info {
+ enum deferred_cst_ctx ctx;
+ enum deferred_cst_consequence consequence;
+};
+
+typedef tree (*deferred_cst_resolver_fn)
+ (tree, const deferred_cst_resolve_info *);
+extern void deferred_cst_register_resolver (deferred_cst_resolver_fn resolver);
+extern tree deferred_cst_resolve (tree expr, deferred_cst_ctx,
+ deferred_cst_consequence);
+
+/* Mark a record or enum TYPE for constant deferral. */
+extern void deferred_cst_mark_type (tree type);
+
+/* Return true if TYPE is marked for constant deferral. */
+extern bool deferred_cst_type_p (tree type);
+
+/* Build a DEFERRED_CST_EXPR around EXPR. */
+extern tree deferred_cst_build_expr (tree expr);
+
+/* Return true if EXPR contains a DEFERRED_CST_EXPR. */
+extern bool deferred_cst_present_p (tree expr);
+
+/* Create a call to IFN_DEFERRED_CST_FIELD_OFFSET for the physical
+ FIELD whose byte offset is deferred.
+
+ FIELD must not be a bit-field. Callers handling bit-fields must pass
+ DECL_BIT_FIELD_REPRESENTATIVE (field). */
+extern tree deferred_cst_call_ifn_field_offset (location_t loc, tree field);
+
+/* Get the FIELD_DECL associated with the argument TOKEN to a call of
+ IFN_DEFERRED_CST_FIELD_OFFSET. */
+extern tree deferred_cst_ifn_field_offset_field (tree token);
+
+/* Create a call to IFN_DEFERRED_CST_ENUM_VALUE for an enum value
+ declaration DECL. */
+extern tree deferred_cst_call_ifn_enum_value (location_t loc, tree decl);
+
+/* Get the CONST_DECL associated with the argument TOKEN to a call of
+ IFN_DEFERRED_CST_ENUM_VALUE. */
+extern tree deferred_cst_ifn_enum_value_decl (tree token);
+
+#endif
@@ -844,6 +844,11 @@ is_gimple_val (tree t)
|| TREE_CODE (t) == OMP_TARGET_DEVICE_MATCHES)
return true;
+ /* Technically, deferred constant expressions act as min invariants,
+ but they do not survice to where that would be relevant. */
+ if (TREE_CODE (t) == DEFERRED_CST_EXPR)
+ return true;
+
return (is_gimple_variable (t) || is_gimple_min_invariant (t));
}
@@ -2575,7 +2575,8 @@ get_gimple_rhs_num_ops (enum tree_code code)
|| (SYM) == WITH_SIZE_EXPR \
|| (SYM) == SSA_NAME \
|| (SYM) == OMP_NEXT_VARIANT \
- || (SYM) == OMP_TARGET_DEVICE_MATCHES) ? GIMPLE_SINGLE_RHS \
+ || (SYM) == OMP_TARGET_DEVICE_MATCHES \
+ || (SYM) == DEFERRED_CST_EXPR) ? GIMPLE_SINGLE_RHS \
: GIMPLE_INVALID_RHS),
#define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
@@ -20705,6 +20705,11 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
ret = GS_ALL_DONE;
break;
+ case DEFERRED_CST_EXPR:
+ /* DEFERRED_CST_EXPRs act as min invariants. */
+ ret = GS_ALL_DONE;
+ break;
+
case CONST_DECL:
/* If we require an lvalue, such as for ADDR_EXPR, retain the
CONST_DECL node. Otherwise the decl is replaceable by its
@@ -4322,6 +4322,24 @@ expand_reduc_sbool_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab)
assign_call_lhs (lhs, lhs_rtx, &ops[0]);
}
+/* IFN_DEFERRED_CST_FIELD_OFFSET should not survice GIMPLE. */
+
+void
+expand_DEFERRED_CST_FIELD_OFFSET (internal_fn, gcall *)
+{
+ internal_error ("%qs reached RTL expansion",
+ "IFN_DEFERRED_CST_FIELD_OFFSET");
+}
+
+/* IFN_DEFERRED_CST_ENUM_VALUE should not survive GIMPLE. */
+
+void
+expand_DEFERRED_CST_ENUM_VALUE (internal_fn, gcall *)
+{
+ internal_error ("%qs reached RTL expansion",
+ "IFN_DEFERRED_CST_ENUM_VALUE");
+}
+
/* Expanders for optabs that can use expand_direct_optab_fn. */
#define expand_unary_optab_fn(FN, STMT, OPTAB) \
@@ -649,6 +649,12 @@ DEF_INTERNAL_FN (BITINTTOFLOAT, ECF_PURE | ECF_LEAF, ". R . ")
DEF_INTERNAL_OPTAB_FN (BIT_ANDN, ECF_CONST, andn, binary)
DEF_INTERNAL_OPTAB_FN (BIT_IORN, ECF_CONST, iorn, binary)
+/* Deferred constant functions. */
+DEF_INTERNAL_FN (DEFERRED_CST_FIELD_OFFSET, ECF_CONST | ECF_LEAF | ECF_NOTHROW,
+ NULL)
+DEF_INTERNAL_FN (DEFERRED_CST_ENUM_VALUE, ECF_CONST | ECF_LEAF | ECF_NOTHROW,
+ NULL)
+
#undef DEF_INTERNAL_WIDENING_OPTAB_FN
#undef DEF_INTERNAL_SIGNED_COND_FN
#undef DEF_INTERNAL_COND_FN
@@ -160,6 +160,7 @@ extern const char *lhd_get_sarif_source_language (const char *);
#define LANG_HOOKS_GET_SUBSTRING_LOCATION lhd_get_substring_location
#define LANG_HOOKS_FINALIZE_EARLY_DEBUG lhd_finalize_early_debug
#define LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE lhd_get_sarif_source_language
+#define LANG_HOOKS_DEFERRED_CST_CAPABILITIES 0
/* Attribute hooks. */
#define LANG_HOOKS_ATTRIBUTE_TABLE
@@ -417,7 +418,8 @@ extern void lhd_end_section (void);
LANG_HOOKS_RUN_LANG_SELFTESTS, \
LANG_HOOKS_GET_SUBSTRING_LOCATION, \
LANG_HOOKS_FINALIZE_EARLY_DEBUG, \
- LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE \
+ LANG_HOOKS_GET_SARIF_SOURCE_LANGUAGE, \
+ LANG_HOOKS_DEFERRED_CST_CAPABILITIES \
}
#endif /* GCC_LANG_HOOKS_DEF_H */
@@ -694,6 +694,9 @@ struct lang_hooks
languages. */
const char *(*get_sarif_source_language) (const char *filename);
+ /* Deferred constant capabilities. */
+ unsigned int deferred_cst_capabilities;
+
/* Whenever you add entries here, make sure you adjust langhooks-def.h
and langhooks.cc accordingly. */
};
@@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see
/* Interprocedural optimization passes. */
INSERT_PASSES_AFTER (all_small_ipa_passes)
+ NEXT_PASS (pass_deferred_cst_barrier);
/* auto_profile_offline uses dwarf_name hook that only
works before free_lang_data. */
NEXT_PASS (pass_ipa_auto_profile_offline);
new file mode 100644
@@ -0,0 +1,59 @@
+/* This test performs very basic sanity checks and expects enum values
+ and offsetof expressions to be preserved through the frontend. The
+ deferred constant test plugin is used to get a stable list of deferred
+ constants. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11 -fplugin-arg-deferred_cst_plugin-output=deferred-cst-basic.out -fplugin-arg-deferred_cst_plugin-exit" } */
+
+/* { dg-final { scan-file deferred-cst-basic.out "\\\[fun\\\] test_offset -- offsetof\\(struct S, b\\)" } } */
+/* { dg-final { scan-file deferred-cst-basic.out "\\\[fun\\\] test_offset_old -- offsetof\\(struct S, d\\)" } } */
+/* { dg-final { scan-file deferred-cst-basic.out "\\\[fun\\\] test_enum -- E::E_B" } } */
+/* { dg-final { scan-file deferred-cst-basic.out "\\\[fun\\\] test_expression -- \\(offsetof\\(struct S, c\\) \\+ \\(E::E_C \\* 4\\)\\)" } } */
+
+#define __dc __attribute__((defer_constants))
+#define old_offsetof(type, member) ((long long int) &((type *)0)->member)
+
+struct S
+{
+ int a;
+ int b;
+ int c;
+ int d;
+} __dc;
+
+enum E
+{
+ E_A = 1,
+ E_B = 2,
+ E_C = 3
+} __dc;
+
+
+/* Deferred constants are preserved in ordinary runtime expressions. */
+
+extern void use_value (int);
+
+void
+test_offset (void)
+{
+ use_value (__builtin_offsetof (struct S, b));
+}
+
+void
+test_offset_old (void)
+{
+ use_value (old_offsetof(struct S, d));
+}
+
+void
+test_enum (void)
+{
+ use_value (E_B);
+}
+
+void
+test_expression (void)
+{
+ use_value (__builtin_offsetof (struct S, c) + E_C * 4);
+}
new file mode 100644
@@ -0,0 +1,37 @@
+/* This tests whether frontend folding properly eliminates deferred
+ constants from expressions that do not actually depend on any
+ deferred components. That this test produces no frontend errors
+ is evidence enough for that, as deferred constants are not allowed
+ in static assertions. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11" } */
+
+#define __dc __attribute__((defer_constants))
+
+enum E
+{
+ C = 4
+} __dc;
+
+_Static_assert (C * 0 == 0, "deferred constant");
+_Static_assert ((C * 4) - (C * 4) == 0, "deferred constant");
+_Static_assert ((C * 4 + 7) - (C * 4 + 7) == 0, "deferred constant");
+
+_Static_assert (1 + ((C + 7) - (C + 7)), "deferred constant");
+_Static_assert (3 + (C * 4) - (C * 4) == 3, "deferred constant");
+_Static_assert (((C - C) * 100) + 1, "deferred constant");
+
+_Static_assert ((C & 0) == 0, "deferred constant");
+_Static_assert ((C ^ C) == 0, "deferred constant");
+
+_Static_assert (C == C, "deferred constant");
+_Static_assert (!(C != C), "deferred constant");
+_Static_assert (C <= C, "deferred constant");
+_Static_assert (C >= C, "deferred constant");
+
+_Static_assert ((0 ? C : 1), "deferred constant");
+_Static_assert ((1 ? 1 : C), "deferred constant");
+
+_Static_assert (!(0 && C), "deferred constant");
+_Static_assert (1 || C, "deferred constant");
new file mode 100644
@@ -0,0 +1,77 @@
+/* This test exercises contexts in which deferred constants are not
+ permitted. Each is expected to produce a frontend error, indicating
+ that the corresponding expression is not a compile-time constant. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11" } */
+
+#define __dc __attribute__((defer_constants))
+
+enum E1
+{
+ C = 4
+} __dc;
+
+
+/* Deferred constants used as array bounds during structure type
+ formation. */
+
+struct S1
+{
+ int a[C]; /* { dg-error "variably modified 'a' at file scope" } */
+};
+
+
+/* Deferred constants used as array bounds at file scope. */
+
+int global_array[C]; /* { dg-error "variably modified 'global_array' at file scope" } */
+
+
+/* Deferred constants used as array bounds for block-scope objects which
+ require a constant size. */
+
+void
+test_local_array (void)
+{
+ static int local_array[C]; /* { dg-error "storage size of 'local_array' isn't constant" } */
+}
+
+
+/* Array designators require integer constant expressions. */
+
+int a[5] =
+{
+ [C] = 1 /* { dg-error "nonconstant array index in initializer" } */
+};
+
+
+/* Bit-field widths require integer constant expressions. */
+
+struct S2
+{
+ unsigned int x : C; /* { dg-error "bit-field 'x' width not an integer constant" } */
+};
+
+
+/* Enumerator values require integer constant expressions. */
+
+enum F
+{
+ F_A = C
+}; /* { dg-error "enumerator value for 'F_A' is not an integer constant" } */
+
+
+/* Static assertions require integer constant expressions. */
+
+enum E2
+{
+ C2 = 1 /* { dg-line deferred_static_assert } */
+} __dc;
+
+_Static_assert (C2, "deferred constant");
+/* { dg-error "expression in static assertion is not constant" "" { target *-*-* } deferred_static_assert } */
+
+
+/* Alignment specifications require integer constant expressions. */
+
+_Alignas (C) int aligned; /* { dg-error "requested alignment is not an integer constant" } */
new file mode 100644
@@ -0,0 +1,77 @@
+/* This test makes use of the test plugin's deferred constant resolver
+ to essential un-defer constants in contexts that do not permit them.
+ That this test does not produce frontend errors itself is evidence
+ that no deferred constants remain in illegal contexts. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11 -fplugin-arg-deferred_cst_plugin-resolve-early" } */
+
+#define __dc __attribute__((defer_constants))
+
+enum E1
+{
+ C = 4
+} __dc;
+
+
+/* Deferred constants used as array bounds during structure type
+ formation. */
+
+struct S1
+{
+ int a[C];
+};
+
+
+/* Deferred constants used as array bounds at file scope. */
+
+int global_array[C];
+
+
+/* Deferred constants used as array bounds for block-scope objects which
+ require a constant size. */
+
+void
+test_local_array (void)
+{
+ static int local_array[C];
+}
+
+
+/* Array designators require integer constant expressions. */
+
+int a[5] =
+{
+ [C] = 1
+};
+
+
+/* Bit-field widths require integer constant expressions. */
+
+struct S2
+{
+ unsigned int x : C;
+};
+
+
+/* Enumerator values require integer constant expressions. */
+
+enum F
+{
+ F_A = C
+};
+
+
+/* Static assertions require integer constant expressions. */
+
+enum E2
+{
+ C2 = 1
+} __dc;
+
+_Static_assert (C2, "deferred constant");
+
+
+/* Alignment specifications require integer constant expressions. */
+
+_Alignas (C) int aligned;
new file mode 100644
@@ -0,0 +1,235 @@
+/* This test uses the test plugins "resolve-late" option which maps
+ deferred constants to compile-time constants in an IPA pass, so AFTER
+ the frontend. It exercises contexts in which deferred constants are
+ permitted, such as runtime expressions, static initializers or case
+ labels. At runtime, it validates that the generated code still
+ behaves as expected. */
+
+/* { dg-do run } */
+/* { dg-options "-std=gnu11 -fplugin-arg-deferred_cst_plugin-resolve-late" } */
+
+#define __dc __attribute__((defer_constants))
+
+struct S
+{
+ char a;
+ int b;
+ long c;
+} __dc;
+
+enum E
+{
+ E_A = 3,
+ E_B = 7,
+ E_C = 11
+} __dc;
+
+/* Static scalar initializers. */
+
+static int var_offset = __builtin_offsetof (struct S, b);
+static int var_enum = E_B;
+static int var_expression
+ = __builtin_offsetof (struct S, c) + E_C * 4;
+
+/* Static aggregate initializers. */
+
+static struct
+{
+ int x;
+ struct
+ {
+ int y;
+ } nested;
+} var_aggregate =
+{
+ .x = E_B,
+ .nested = {
+ .y = __builtin_offsetof (struct S, c) + E_C * 4
+ }
+};
+
+/* Prevent the switch controlling expression from being folded away. */
+
+static int evaluations;
+
+__attribute__((noinline, noclone))
+static int
+get_value (int value)
+{
+ ++evaluations;
+ return value;
+}
+
+/* Mix ordinary and deferred cases, including fallthrough in both
+ directions. */
+
+__attribute__((noinline, noclone))
+static int
+test_switch (int value)
+{
+ switch (get_value (value))
+ {
+ case 1:
+ return 10;
+
+ case E_A:
+ return 30;
+
+ case 4:
+ goto ordinary_to_deferred;
+
+ case E_B:
+ ordinary_to_deferred:
+ return 70;
+
+ case E_C:
+ goto deferred_to_ordinary;
+
+ case 12:
+ deferred_to_ordinary:
+ return 110;
+
+ case E_A + E_C:
+ return 140;
+
+ default:
+ return -1;
+ }
+}
+
+/* Test actual fallthrough between ordinary and deferred labels. */
+
+__attribute__((noinline, noclone))
+static int
+test_fallthrough (int value)
+{
+ int result = 0;
+
+ switch (get_value (value))
+ {
+ case 4:
+ result += 1;
+ /* Fall through. */
+
+ case E_B:
+ result += 2;
+ break;
+
+ case E_C:
+ result += 4;
+ /* Fall through. */
+
+ case 12:
+ result += 8;
+ break;
+
+ default:
+ result = -1;
+ break;
+ }
+
+ return result;
+}
+
+/* Ordinary runtime expressions. */
+
+__attribute__((noinline, noclone))
+static int
+test_runtime (void)
+{
+ int offset_b = __builtin_offsetof (struct S, b);
+ int offset_c = __builtin_offsetof (struct S, c);
+
+ if (offset_b != __builtin_offsetof (struct S, b))
+ return 1;
+
+ if (offset_c != __builtin_offsetof (struct S, c))
+ return 2;
+
+ if (E_B != 7)
+ return 3;
+
+ if (E_C * 4 + offset_c != 44 + offset_c)
+ return 4;
+
+ if (E_A + E_B + E_C != 21)
+ return 5;
+
+ return 0;
+}
+
+int
+main (void)
+{
+ int offset_b = __builtin_offsetof (struct S, b);
+ int offset_c = __builtin_offsetof (struct S, c);
+
+ /* Static initializers. */
+
+ if (var_offset != offset_b)
+ __builtin_abort ();
+
+ if (var_enum != 7)
+ __builtin_abort ();
+
+ if (var_expression != offset_c + 44)
+ __builtin_abort ();
+
+ if (var_aggregate.x != 7)
+ __builtin_abort ();
+
+ if (var_aggregate.nested.y != offset_c + 44)
+ __builtin_abort ();
+
+ /* Runtime expressions. */
+
+ if (test_runtime () != 0)
+ __builtin_abort ();
+
+ /* Switch dispatch and single evaluation. */
+
+#define CHECK_SWITCH(VALUE, EXPECTED) \
+ do \
+ { \
+ int before = evaluations; \
+ if (test_switch (VALUE) != (EXPECTED)) \
+ __builtin_abort (); \
+ if (evaluations != before + 1) \
+ __builtin_abort (); \
+ } \
+ while (0)
+
+ CHECK_SWITCH (1, 10);
+ CHECK_SWITCH (3, 30);
+ CHECK_SWITCH (4, 70);
+ CHECK_SWITCH (7, 70);
+ CHECK_SWITCH (11, 110);
+ CHECK_SWITCH (12, 110);
+ CHECK_SWITCH (14, 140);
+ CHECK_SWITCH (99, -1);
+
+#undef CHECK_SWITCH
+
+ /* Fallthrough between ordinary and deferred labels. */
+
+#define CHECK_FALLTHROUGH(VALUE, EXPECTED) \
+ do \
+ { \
+ int before = evaluations; \
+ if (test_fallthrough (VALUE) != (EXPECTED)) \
+ __builtin_abort (); \
+ if (evaluations != before + 1) \
+ __builtin_abort (); \
+ } \
+ while (0)
+
+ CHECK_FALLTHROUGH (4, 3);
+ CHECK_FALLTHROUGH (7, 2);
+ CHECK_FALLTHROUGH (11, 12);
+ CHECK_FALLTHROUGH (12, 8);
+ CHECK_FALLTHROUGH (99, -1);
+
+#undef CHECK_FALLTHROUGH
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,51 @@
+/* This test validates that deferred constants are properly preserved
+ in static initializers. It is not fully built, but uses the test
+ plugin's deferred constant logs to validate the post-frontend state. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11 -fplugin-arg-deferred_cst_plugin-output=deferred-cst-static-init.out -fplugin-arg-deferred_cst_plugin-exit" } */
+
+/* { dg-final { scan-file deferred-cst-static-init.out "\\\[var\\\] var_offset -- offsetof\\(struct S, b\\)" } } */
+/* { dg-final { scan-file deferred-cst-static-init.out "\\\[var\\\] var_enum -- E::E_B" } } */
+/* { dg-final { scan-file deferred-cst-static-init.out "\\\[var\\\] var_aggregate.x -- E::E_B" } } */
+/* { dg-final { scan-file deferred-cst-static-init.out "\\\[var\\\] var_aggregate.nested.y -- \\(offsetof\\(struct S, c\\) \\+ \\(E::E_C \\* 4\\)\\)" } } */
+
+#define __dc __attribute__((defer_constants))
+
+struct S
+{
+ int a;
+ int b;
+ int c;
+} __dc;
+
+enum E
+{
+ E_A = 1,
+ E_B = 2,
+ E_C = 3
+} __dc;
+
+
+/* Deferred constants are permitted in static initializers. */
+
+static int var_offset = __builtin_offsetof (struct S, b);
+
+static int var_enum = E_B;
+
+static struct
+{
+ int x;
+
+ struct
+ {
+ int y;
+ } nested;
+} var_aggregate =
+{
+ .x = E_B,
+ .nested =
+ {
+ .y = (__builtin_offsetof (struct S, c) + E_C * 4)
+ }
+};
new file mode 100644
@@ -0,0 +1,67 @@
+/* This test validates that deferred constants are properly preserved
+ in case labels. It is not fully built, but uses the test plugin's
+ deferred constant logs to validate the post-frontend state. */
+
+/* { dg-do compile } */
+/* { dg-options "-std=gnu11 -fplugin-arg-deferred_cst_plugin-output=deferred-cst-switch-case.out -fplugin-arg-deferred_cst_plugin-exit" } */
+
+/* { dg-final { scan-file deferred-cst-switch-case.out "\\\[fun\\\] test_switch -- deferred_enum::DEFERRED_A" } } */
+/* { dg-final { scan-file deferred-cst-switch-case.out "\\\[fun\\\] test_switch -- deferred_enum::DEFERRED_B" } } */
+/* { dg-final { scan-file deferred-cst-switch-case.out "\\\[fun\\\] test_switch -- deferred_enum::DEFERRED_C" } } */
+/* { dg-final { scan-file deferred-cst-switch-case.out "\\\[fun\\\] test_switch -- \\(deferred_enum::DEFERRED_C \\+ 10\\)" } } */
+
+#define __dc __attribute__((defer_constants))
+
+enum deferred_enum
+{
+ DEFERRED_A = 10,
+ DEFERRED_B = 20,
+ DEFERRED_C = 30
+} __dc;
+
+enum ordinary_enum
+{
+ ORDINARY_A = 100,
+ ORDINARY_B = 200,
+ ORDINARY_C = 300
+};
+
+extern int get_value (void);
+extern void use_value (int);
+
+
+/* Deferred case labels can be mixed with ordinary case labels. Exercise
+ fallthrough in both directions and a deferred case range. */
+
+void
+test_switch (void)
+{
+ switch (get_value ())
+ {
+ case DEFERRED_A:
+ use_value (1);
+ /* FALLTHRU */
+ case ORDINARY_A:
+ use_value (2);
+ break;
+
+ case ORDINARY_B:
+ use_value (3);
+ /* FALLTHRU */
+ case DEFERRED_B:
+ use_value (4);
+ break;
+
+ case DEFERRED_C ... DEFERRED_C + 10:
+ use_value (5);
+ break;
+
+ case ORDINARY_C:
+ use_value (6);
+ break;
+
+ default:
+ use_value (7);
+ break;
+ }
+}
new file mode 100644
@@ -0,0 +1,836 @@
+/* Test plugin for deferred constant expressions.
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+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 3, 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 COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#include <gcc-plugin.h>
+#include <plugin-version.h>
+#include <tree.h>
+#include <tree-pass.h>
+#include <context.h>
+#include <cgraph.h>
+#include <gimple.h>
+#include <gimple-iterator.h>
+#include <function.h>
+#include <internal-fn.h>
+#include <print-tree.h>
+#include <tree-pretty-print.h>
+#include <diagnostic-core.h>
+#include <deferred-cst.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <string>
+#include <vector>
+
+int plugin_is_GPL_compatible;
+
+static const char *output_file = NULL;
+static bool resolve_deferred_constants_early = false;
+static bool resolve_deferred_constants_late = false;
+static bool ipa_exit = false;
+
+enum class context_kind
+{
+ variable,
+ function
+};
+
+struct deferred_report
+{
+ context_kind kind;
+ std::string context;
+ std::string expression;
+};
+
+static std::vector<deferred_report> reports;
+
+static tree
+check_defer_constants_attribute (tree *node, tree name, tree args, int flags, bool *no_add_attrs)
+{
+ if (!node || !*node)
+ return NULL_TREE;
+
+ tree type = *node;
+
+ switch (TREE_CODE (type))
+ {
+ case RECORD_TYPE:
+ case ENUMERAL_TYPE:
+ deferred_cst_mark_type (type);
+ break;
+ default:
+ error ("%'defer_constants%' attribute on invalid type");
+ *no_add_attrs = true;
+ }
+
+ return NULL_TREE;
+}
+
+static struct attribute_spec defer_constants_attribute = {
+ "defer_constants", 0, 0, false, false, false, false,
+ check_defer_constants_attribute, NULL
+};
+
+void
+on_register_attributes (void *plugin_data, void *user_data)
+{
+ register_attribute (&defer_constants_attribute);
+}
+
+/* Small stable printer for deferred constant expressions.
+
+ The intentionally supported grammar is:
+
+ EXPR :=
+ LEAF
+ | (EXPR + EXPR)
+ | (EXPR - EXPR)
+ | (EXPR * EXPR)
+
+ LEAF :=
+ INTEGER
+ | offsetof(TYPE, FIELD)
+ | ENUM::VALUE
+
+ Every non-leaf expression is parenthesized. */
+
+/* Return a stable printable name for DECL.
+
+ For tests we require declarations involved in deferred constants to have
+ names. */
+static const char *
+decl_name (tree decl)
+{
+ gcc_assert (decl);
+ gcc_assert (DECL_P (decl));
+ gcc_assert (DECL_NAME (decl));
+
+ return IDENTIFIER_POINTER (DECL_NAME (decl));
+}
+
+/* Append the name of a record/enum type.
+
+ Prefer TYPE_NAME's declaration/name. Tests using this plugin should use
+ named types rather than anonymous types, since the textual format is
+ deliberately intended to be stable. */
+static void
+append_type_name (std::string &out, tree type)
+{
+ gcc_assert (type);
+
+ tree name = TYPE_NAME (type);
+
+ if (name && TREE_CODE (name) == TYPE_DECL)
+ name = DECL_NAME (name);
+
+ gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
+
+ out += IDENTIFIER_POINTER (name);
+}
+
+/* Print an INTEGER_CST without relying on host integer width. */
+static void
+append_integer_cst (std::string &out, tree t)
+{
+ gcc_assert (TREE_CODE (t) == INTEGER_CST);
+
+ char *p = print_generic_expr_to_str (t);
+
+ /* INTEGER_CSTs appearing in the test grammar should print as a simple
+ integer. */
+ gcc_assert (p);
+ out += p;
+ free (p);
+}
+
+/* Print a deferred field-offset IFN. */
+static void
+append_field_offset (std::string &out, tree call)
+{
+ gcc_assert (TREE_CODE (call) == CALL_EXPR);
+ gcc_assert (CALL_EXPR_IFN (call) == IFN_DEFERRED_CST_FIELD_OFFSET);
+ gcc_assert (call_expr_nargs (call) == 1);
+
+ tree token = CALL_EXPR_ARG (call, 0);
+ tree field = deferred_cst_ifn_field_offset_field (token);
+
+ gcc_assert (field);
+ gcc_assert (TREE_CODE (field) == FIELD_DECL);
+
+ tree record = DECL_CONTEXT (field);
+
+ gcc_assert (record);
+ gcc_assert (TREE_CODE (record) == RECORD_TYPE);
+
+ out += "offsetof(struct ";
+
+ append_type_name (out, record);
+ out += ", ";
+ out += decl_name (field);
+ out += ")";
+}
+
+/* Print a deferred enum-value IFN. */
+static void
+append_enum_value (std::string &out, tree call)
+{
+ gcc_assert (TREE_CODE (call) == CALL_EXPR);
+ gcc_assert (CALL_EXPR_IFN (call) == IFN_DEFERRED_CST_ENUM_VALUE);
+ gcc_assert (call_expr_nargs (call) == 1);
+
+ tree token = CALL_EXPR_ARG (call, 0);
+ tree decl = deferred_cst_ifn_enum_value_decl (token);
+
+ gcc_assert (decl);
+ gcc_assert (TREE_CODE (decl) == CONST_DECL);
+
+ tree enum_type = TREE_TYPE (decl);
+
+ gcc_assert (enum_type);
+ gcc_assert (TREE_CODE (enum_type) == ENUMERAL_TYPE);
+
+ append_type_name (out, enum_type);
+ out += "::";
+ out += decl_name (decl);
+}
+
+/* Print one internal-function leaf. */
+static void
+append_deferred_ifn (std::string &out, tree call)
+{
+ gcc_assert (TREE_CODE (call) == CALL_EXPR);
+ gcc_assert (CALL_EXPR_FN (call) == NULL_TREE);
+
+ switch (CALL_EXPR_IFN (call))
+ {
+ case IFN_DEFERRED_CST_FIELD_OFFSET:
+ append_field_offset (out, call);
+ return;
+
+ case IFN_DEFERRED_CST_ENUM_VALUE:
+ append_enum_value (out, call);
+ return;
+
+ default:
+ /* The test consumer intentionally understands only the deferred
+ constant IFNs. */
+ gcc_unreachable ();
+ }
+}
+
+/* Append EXPR in the stable test grammar.
+
+ Nested DEFERRED_CST_EXPR wrappers are transparent. */
+static void
+append_deferred_expr (std::string &out, tree expr)
+{
+ gcc_assert (expr);
+
+ while (CONVERT_EXPR_P (expr))
+ {
+ gcc_assert (TREE_OPERAND (expr, 0));
+ expr = TREE_OPERAND (expr, 0);
+ }
+
+ switch (TREE_CODE (expr))
+ {
+ case DEFERRED_CST_EXPR:
+ append_deferred_expr (out, TREE_OPERAND (expr, 0));
+ return;
+
+ case INTEGER_CST:
+ append_integer_cst (out, expr);
+ return;
+
+ case CALL_EXPR:
+ append_deferred_ifn (out, expr);
+ return;
+
+ case PLUS_EXPR:
+ case MINUS_EXPR:
+ case MULT_EXPR:
+ {
+ const char *op;
+
+ switch (TREE_CODE (expr))
+ {
+ case PLUS_EXPR:
+ op = " + ";
+ break;
+
+ case MINUS_EXPR:
+ op = " - ";
+ break;
+
+ case MULT_EXPR:
+ op = " * ";
+ break;
+
+ default:
+ gcc_unreachable ();
+ }
+
+ out += "(";
+ append_deferred_expr (out, TREE_OPERAND (expr, 0));
+ out += op;
+ append_deferred_expr (out, TREE_OPERAND (expr, 1));
+ out += ")";
+ return;
+ }
+
+ default:
+ /* If the core starts propagating a new operation into deferred
+ constants, force the test consumer to be updated explicitly. */
+ debug_tree (expr);
+ internal_error ("unsupported expression in deferred constant test "
+ "consumer");
+ }
+}
+
+/* Record one outermost DEFERRED_CST_EXPR. */
+static void
+record_deferred_expr (context_kind kind, const std::string &context, tree expr)
+{
+ gcc_assert (TREE_CODE (expr) == DEFERRED_CST_EXPR);
+
+ std::string text;
+ append_deferred_expr (text, expr);
+
+ reports.push_back ({ kind, context, text });
+}
+
+static tree
+function_tree_walker (tree *tp, int *walk_subtrees, void *data)
+{
+ tree t = *tp;
+
+ if (!t)
+ return NULL_TREE;
+
+ if (TREE_CODE (t) != DEFERRED_CST_EXPR)
+ return NULL_TREE;
+
+ const char *fn_name = static_cast<const char *> (data);
+
+ record_deferred_expr (context_kind::function, fn_name, t);
+
+ *walk_subtrees = 0;
+ return NULL_TREE;
+}
+
+static void
+scan_function (cgraph_node *node)
+{
+ gcc_assert (node);
+ gcc_assert (node->decl);
+
+ if (!node->has_gimple_body_p ())
+ return;
+
+ const char *name = decl_name (node->decl);
+
+ push_cfun (DECL_STRUCT_FUNCTION (node->decl));
+
+ basic_block bb;
+ FOR_EACH_BB_FN (bb, cfun)
+ {
+ for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
+ !gsi_end_p (gsi);
+ gsi_next (&gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+
+ for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
+ {
+ tree op = gimple_op (stmt, i);
+
+ if (!op || !deferred_cst_present_p (op))
+ continue;
+
+ walk_tree (&op, function_tree_walker,
+ const_cast<char *> (name), NULL);
+ }
+ }
+ }
+
+ pop_cfun ();
+}
+
+/* Append ".field" or "[index]" for one CONSTRUCTOR element.
+
+ FIELD_DECL indices give us struct/union paths. INTEGER_CST indices give
+ us array paths. */
+static std::string
+constructor_element_context (const std::string &parent, tree index)
+{
+ std::string result = parent;
+
+ if (!index)
+ return result;
+
+ if (TREE_CODE (index) == FIELD_DECL)
+ {
+ result += ".";
+ result += decl_name (index);
+ return result;
+ }
+
+ if (TREE_CODE (index) == INTEGER_CST)
+ {
+ result += "[";
+ append_integer_cst (result, index);
+ result += "]";
+ return result;
+ }
+
+ debug_tree (index);
+ internal_error ("unsupported constructor index in deferred constant "
+ "test consumer");
+}
+
+static void
+scan_initializer (tree init, const std::string &context)
+{
+ if (!init)
+ return;
+
+ if (TREE_CODE (init) == DEFERRED_CST_EXPR)
+ {
+ record_deferred_expr (context_kind::variable, context, init);
+ return;
+ }
+
+ if (TREE_CODE (init) == CONSTRUCTOR)
+ {
+ unsigned HOST_WIDE_INT ix;
+ tree index;
+ tree value;
+
+ FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init),
+ ix, index, value)
+ {
+ std::string child
+ = constructor_element_context (context, index);
+ scan_initializer (value, child);
+ }
+
+ return;
+ }
+
+ /* A deferred expression can also be nested below some tree which is not
+ itself a CONSTRUCTOR. Walk that tree, retaining the same initializer
+ context. */
+ if (deferred_cst_present_p (init))
+ {
+ struct initializer_walk_data
+ {
+ const std::string *context;
+ } data = { &context };
+
+ auto walker = [] (tree *tp, int *walk_subtrees, void *opaque) -> tree
+ {
+ tree t = *tp;
+
+ if (!t)
+ return NULL_TREE;
+
+ if (TREE_CODE (t) != DEFERRED_CST_EXPR)
+ return NULL_TREE;
+
+ initializer_walk_data *d
+ = static_cast<initializer_walk_data *> (opaque);
+
+ record_deferred_expr (context_kind::variable, *d->context, t);
+ *walk_subtrees = 0;
+ return NULL_TREE;
+ };
+
+ walk_tree (&init, walker, &data, NULL);
+ }
+}
+
+static void
+scan_static_initializers ()
+{
+ varpool_node *node;
+
+ FOR_EACH_VARIABLE (node)
+ {
+ tree decl = node->decl;
+
+ if (!decl)
+ continue;
+
+ tree init = DECL_INITIAL (decl);
+
+ if (!init || init == error_mark_node)
+ continue;
+
+ /* Externals don't have a definition whose initializer belongs to this
+ compilation unit. */
+ if (DECL_EXTERNAL (decl))
+ continue;
+
+ if (!deferred_cst_present_p (init))
+ continue;
+
+ scan_initializer (init, decl_name (decl));
+ }
+}
+
+static const char *
+context_kind_name (context_kind kind)
+{
+ switch (kind)
+ {
+ case context_kind::function:
+ return "fun";
+
+ case context_kind::variable:
+ return "var";
+ }
+
+ gcc_unreachable ();
+}
+
+static bool
+report_less (const deferred_report &a, const deferred_report &b)
+{
+ if (a.kind != b.kind)
+ return a.kind < b.kind;
+
+ if (a.context != b.context)
+ return a.context < b.context;
+
+ return a.expression < b.expression;
+}
+
+static void
+emit_reports ()
+{
+ if (!output_file)
+ return;
+
+ /* Don't make tests depend on cgraph/varpool traversal order. */
+ std::sort (reports.begin (), reports.end (), report_less);
+
+ FILE *out = fopen (output_file, "w");
+ if (!out)
+ fatal_error (UNKNOWN_LOCATION,
+ "cannot open %s",
+ output_file);
+
+ for (const deferred_report &report : reports)
+ fprintf (out, "[%s] %s -- %s\n",
+ context_kind_name (report.kind),
+ report.context.c_str (),
+ report.expression.c_str ());
+
+ fclose (out);
+}
+
+/* Resolve deferred constant expression EXPR to its ordinary compile-time
+ value. */
+
+static tree
+resolve_deferred_expr (tree expr)
+{
+ gcc_assert (expr);
+
+ if (CONSTANT_CLASS_P (expr))
+ return expr;
+
+ if (TREE_CODE (expr) == DEFERRED_CST_EXPR)
+ return resolve_deferred_expr (TREE_OPERAND (expr, 0));
+
+ if (TREE_CODE (expr) == CALL_EXPR)
+ {
+ switch (CALL_EXPR_IFN (expr))
+ {
+ case IFN_DEFERRED_CST_FIELD_OFFSET:
+ {
+ gcc_assert (call_expr_nargs (expr) == 1);
+
+ tree token = CALL_EXPR_ARG (expr, 0);
+ tree field = deferred_cst_ifn_field_offset_field (token);
+
+ gcc_assert (field);
+ gcc_assert (TREE_CODE (field) == FIELD_DECL);
+
+ return fold_convert_loc (EXPR_LOCATION (expr),
+ TREE_TYPE (expr),
+ byte_position (field));
+ }
+
+ case IFN_DEFERRED_CST_ENUM_VALUE:
+ {
+ gcc_assert (call_expr_nargs (expr) == 1);
+
+ tree token = CALL_EXPR_ARG (expr, 0);
+ tree decl = deferred_cst_ifn_enum_value_decl (token);
+
+ gcc_assert (decl);
+ gcc_assert (TREE_CODE (decl) == CONST_DECL);
+
+ return fold_convert_loc (EXPR_LOCATION (expr),
+ TREE_TYPE (expr),
+ DECL_INITIAL (decl));
+ }
+
+ default:
+ break;
+ }
+ }
+
+ tree copy = copy_node (expr);
+
+ for (int i = 0; i < TREE_OPERAND_LENGTH (expr); ++i)
+ {
+ tree op = TREE_OPERAND (expr, i);
+
+ if (op)
+ TREE_OPERAND (copy, i) = resolve_deferred_expr (op);
+ }
+
+ return fold (copy);
+}
+
+static tree
+resolve_deferred_constants_early_cb (tree expr,
+ const deferred_cst_resolve_info *info)
+{
+ /* This test consumer always resolves deferred constants, independently
+ of why resolution was requested or what happens if deferral remains. */
+ (void) info;
+
+ if (!deferred_cst_present_p (expr))
+ return NULL_TREE;
+
+ return resolve_deferred_expr (expr);
+}
+
+static tree
+resolve_deferred_tree (tree *tp, int *walk_subtrees, void *)
+{
+ tree expr = *tp;
+
+ if (!expr || TREE_CODE (expr) != DEFERRED_CST_EXPR)
+ return NULL_TREE;
+
+ tree replacement = resolve_deferred_expr (expr);
+
+ gcc_assert (CONSTANT_CLASS_P (replacement));
+
+ *tp = replacement;
+ *walk_subtrees = 0;
+
+ return NULL_TREE;
+}
+
+static void
+resolve_function_late (cgraph_node *node)
+{
+ gcc_assert (node);
+ gcc_assert (node->decl);
+
+ if (!node->has_gimple_body_p ())
+ return;
+
+ push_cfun (DECL_STRUCT_FUNCTION (node->decl));
+
+ basic_block bb;
+ FOR_EACH_BB_FN (bb, cfun)
+ {
+ for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
+ !gsi_end_p (gsi);
+ gsi_next (&gsi))
+ {
+ gimple *stmt = gsi_stmt (gsi);
+
+ for (unsigned i = 0; i < gimple_num_ops (stmt); ++i)
+ {
+ tree *op = gimple_op_ptr (stmt, i);
+ if (!*op || !deferred_cst_present_p (*op))
+ continue;
+
+ walk_tree (op, resolve_deferred_tree, NULL, NULL);
+ gcc_assert (!deferred_cst_present_p (*op));
+ }
+ }
+ }
+
+ pop_cfun ();
+}
+
+static tree
+resolve_initializer_late (tree init)
+{
+ if (!init || !deferred_cst_present_p (init))
+ return init;
+
+ if (TREE_CODE (init) == CONSTRUCTOR)
+ {
+ unsigned HOST_WIDE_INT ix;
+ tree index;
+ tree value;
+
+ FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init),
+ ix, index, value)
+ {
+ tree replacement = resolve_initializer_late (value);
+ CONSTRUCTOR_ELT (init, ix)->value = replacement;
+ }
+
+ return init;
+ }
+
+ walk_tree (&init, resolve_deferred_tree, NULL, NULL);
+ gcc_assert (!deferred_cst_present_p (init));
+
+ return init;
+}
+
+static void
+resolve_static_initializers_late ()
+{
+ varpool_node *node;
+
+ FOR_EACH_VARIABLE (node)
+ {
+ tree decl = node->decl;
+
+ if (!decl || DECL_EXTERNAL (decl))
+ continue;
+
+ tree init = DECL_INITIAL (decl);
+
+ if (!init || init == error_mark_node
+ || !deferred_cst_present_p (init))
+ continue;
+
+ DECL_INITIAL (decl) = resolve_initializer_late (init);
+ }
+}
+
+namespace {
+
+const pass_data pass_data_deferred_cst_test =
+{
+ SIMPLE_IPA_PASS,
+ "deferred-cst-test",
+ OPTGROUP_NONE,
+ TV_NONE,
+ 0, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ 0 /* todo_flags_finish */
+};
+
+class pass_deferred_cst_test : public simple_ipa_opt_pass
+{
+public:
+ pass_deferred_cst_test (gcc::context *ctxt)
+ : simple_ipa_opt_pass (pass_data_deferred_cst_test, ctxt)
+ {}
+
+ unsigned int execute (function *) final override
+ {
+ reports.clear ();
+
+ if (output_file)
+ {
+ scan_static_initializers ();
+
+ cgraph_node *node;
+ FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+ scan_function (node);
+
+ emit_reports ();
+ }
+
+ if (resolve_deferred_constants_late)
+ {
+ resolve_static_initializers_late ();
+
+ cgraph_node *node;
+ FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
+ resolve_function_late (node);
+ }
+
+ fflush (stdout);
+ fflush (stderr);
+
+ /* Some tests may leave deferred constants unresolved, so allow
+ successful termination before barrier errors occur. */
+ if (ipa_exit)
+ exit (EXIT_SUCCESS);
+
+ return 0;
+ }
+};
+
+} /* anonymous namespace */
+
+int
+plugin_init (struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
+{
+ if (!plugin_default_version_check (version, &gcc_version))
+ {
+ error ("incompatible GCC/plugin versions: plugin built for GCC %s, "
+ "loaded by GCC %s",
+ gcc_version.basever, version->basever);
+ return 1;
+ }
+
+ for (int i = 0; i < plugin_info->argc; ++i)
+ {
+ if (!strcmp (plugin_info->argv[i].key, "output"))
+ output_file = plugin_info->argv[i].value;
+ else if (!strcmp (plugin_info->argv[i].key, "resolve-early"))
+ resolve_deferred_constants_early = true;
+ else if (!strcmp (plugin_info->argv[i].key, "resolve-late"))
+ resolve_deferred_constants_late = true;
+ else if (!strcmp (plugin_info->argv[i].key, "exit"))
+ ipa_exit = true;
+ }
+
+ /* Don't allow output from earlier invocation to satisfy tests if
+ compilation fails before the file is overwritten. */
+ if (output_file)
+ unlink (output_file);
+
+ if (resolve_deferred_constants_early)
+ deferred_cst_register_resolver (resolve_deferred_constants_early_cb);
+
+ register_callback (plugin_info->base_name, PLUGIN_ATTRIBUTES,
+ on_register_attributes, NULL);
+
+ static struct register_pass_info pass_info;
+ pass_info.pass = new pass_deferred_cst_test (g);
+ pass_info.reference_pass_name = "deferred-cst-barrier";
+ pass_info.ref_pass_instance_number = 1;
+ pass_info.pos_op = PASS_POS_INSERT_BEFORE;
+
+ register_callback (plugin_info->base_name,
+ PLUGIN_PASS_MANAGER_SETUP,
+ NULL, &pass_info);
+ return 0;
+}
@@ -212,6 +212,14 @@ set plugin_test_list [list \
taint-pr112975.c \
taint-pr112977.c } \
{ progress_notifications_plugin.cc } \
+ { deferred_cst_plugin.cc \
+ deferred_cst-basic.c \
+ deferred_cst-switch-case.c \
+ deferred_cst-static-init.c \
+ deferred_cst-invalid.c \
+ deferred_cst-resolved.c \
+ deferred_cst-canceled.c \
+ deferred_cst-run.c } \
]
foreach plugin_test $plugin_test_list {
@@ -514,6 +514,7 @@ extern gimple_opt_pass *make_pass_coroutine_early_expand_ifns (gcc::context *ctx
extern gimple_opt_pass *make_pass_adjust_alignment (gcc::context *ctxt);
/* IPA Passes */
+extern simple_ipa_opt_pass *make_pass_deferred_cst_barrier (gcc::context *ctxt);
extern simple_ipa_opt_pass *make_pass_ipa_lower_emutls (gcc::context *ctxt);
extern simple_ipa_opt_pass *make_pass_ipa_function_and_variable_visibility (gcc::context *ctxt);
extern simple_ipa_opt_pass *make_pass_ipa_strub_mode (gcc::context *ctxt);
@@ -4452,6 +4452,13 @@ dump_generic_node (pretty_printer *pp, tree node, int spc, dump_flags_t flags,
}
break;
+ case DEFERRED_CST_EXPR:
+ pp_string (pp, "DEFERRED_CST_EXPR <");
+ dump_generic_node (pp, TREE_OPERAND (node, 0),
+ spc, flags, false);
+ pp_character (pp, '>');
+ break;
+
case OMP_TARGET_DEVICE_MATCHES:
pp_string (pp, "OMP_TARGET_DEVICE_MATCHES <");
dump_generic_node (pp, OMP_TARGET_DEVICE_MATCHES_SELECTOR (node), spc,
@@ -1626,6 +1626,16 @@ DEFTREECODE (TARGET_OPTION_NODE, "target_option_node", tcc_exceptional, 0)
Operand 2 is additional data. */
DEFTREECODE (ANNOTATE_EXPR, "annotate_expr", tcc_expression, 3)
+/* DEFERRED_CST_EXPR.
+ Operand 0 is an expression with deferred const-ness.
+ Such expressions are not known constants during frontend execution
+ but may act like compile-time constants to some parts of code
+ generation.
+ For example, DEFERRED_CST_EXPRs may wrap offsetof-like expressions
+ or enum literals to prevent semantic meaning from being lost during
+ constant folding in language frontends. */
+DEFTREECODE (DEFERRED_CST_EXPR, "deferred_cst_expr", tcc_expression, 1)
+
/*
Local variables:
mode:c
@@ -62,6 +62,7 @@ along with GCC; see the file COPYING3. If not see
#include "opts.h"
#include "recog.h"
#include "gimple-expr.h"
+#include "deferred-cst.h"
/* The (assembler) name of the first globally-visible object output. */
extern GTY(()) const char *first_global_object_name;
@@ -5058,7 +5059,8 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
switch (TREE_CODE (value))
{
case CONSTRUCTOR:
- if (constructor_static_from_elts_p (value))
+ if (constructor_static_from_elts_p (value)
+ || deferred_cst_present_p (value))
{
unsigned HOST_WIDE_INT idx;
tree elt;
@@ -5107,6 +5109,7 @@ initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
case STRING_CST:
case COMPLEX_CST:
case RAW_DATA_CST:
+ case DEFERRED_CST_EXPR:
return null_pointer_node;
case ADDR_EXPR:
Add support for deferred constant expressions to the C frontend. Introduce the new DEFERRED_CST_EXPR tree node to represent constant expressions whose evaluation is deferred beyond the frontend. The underlying expression retains the identity of referenced declarations through calls to two new internal functions: * IFN_DEFERRED_CST_ENUM_VALUE * IFN_DEFERRED_CST_FIELD_OFFSET The deferred-cst interface provides facilities to mark record or enumeral types for constant deferral, which prohibits semantic loss of select value classes during frontend folding. Any enumerator of a marked enum type, or offsetof-like expression referring to a marked record type is turned into a representative IFN call and guarded behind a DEFERRED_CST_EXPR before it can be folded into an INTEGER_CST. Deferred constant expressions are allowed to occur in some contexts that usually require compile-time constants, specifically static initializers and switch cases. Additionally, they are permitted in contexts that do not generally require compile-time constants. They are not allowed in contexts that contribute to type formation, fundamentally influence the shape of the generated program, or absolutely require full evaluation during the frontend. This regards: array bounds, array designators, static assertions, bit-field widths, alignment specifiers. Deferred constants are lowered into GIMPLE as minimal invariants, but may only exist up to an IPA barrier pass at the very beginning of all_small_ipa_passes. It is the responsibility of a deferred constant consumer (which marked types for constant deferral) to lower all deferred constants before this barrier. Consumers may additionally register a resolver callback that the C frontend invokes whenever deferred constants are encountered in a context that does not permit them or would experience significant code changes because of them. That callback can return compile-time constants to avoid consequences of constant deferral. Additionally, this patch adds a test plugin and tests covering expression construction, folding, cancellation, early and late resolution, and different contexts that do or don't support deferred constants. gcc/ChangeLog: * Makefile.in (OBJS): Add deferred-cst.o. (GTFILES): Add deferred-cst.cc. (PLUGIN_HEADERS): Add deferred-cst.h. * deferred-cst.cc: New file. * deferred-cst.h: New file. * tree.def (DEFERRED_CST_EXPR): New tree code. * gimple-expr.cc (is_gimple_val): Accept deferred constants. * gimple.cc (get_gimple_rhs_num_ops): Handle DEFERRED_CST_EXPR. * gimplify.cc (gimplify_expr): Likewise. * internal-fn.def (DEFERRED_CST_FIELD_OFFSET): New internal function. (DEFERRED_CST_ENUM_VALUE): New internal function. * internal-fn.cc (expand_DEFERRED_CST_FIELD_OFFSET): Add ICE expansion handler. (expand_DEFERRED_CST_ENUM_VALUE): Likewise. * langhooks.h (struct lang_hooks): Add deferred_cst_capabilities member. * langhooks-def.h (LANG_HOOKS_DEFERRED_CST_CAPABILITIES): Define default capabilities and update LANG_HOOKS_INITIALIZER. * passes.def: Register the deferred constant barrier pass. * tree-pass.h (make_pass_deferred_cst_barrier): Declare. * tree-pretty-print.cc (dump_generic_node): Handle DEFERRED_CST_EXPR. * varasm.cc (initializer_constant_valid_p_1): Accept deferred constant expressions in static initializers. gcc/c-family/ChangeLog: * c-common.cc (accumulate_offset): New function. (fold_offsetof_internal): Likewise. (fold_offsetof): Preserve field identity when computing offsets for types marked for constant deferral. gcc/c/ChangeLog: * c-decl.cc (check_bitfield_type_and_width): Resolve deferred constant widths. (grokdeclarator): Resolve deferred array bounds. (build_enumerator): Resolve deferred enumerator values. (declspecs_add_alignas): Resolve deferred alignment expressions. * c-fold.cc (c_fully_fold): Handle deferred constant expressions. (deferred_cst_operator_p): New function. (c_fully_fold_deferred_cst_internal): Likewise. * c-lang.cc (LANG_HOOKS_DEFERRED_CST_CAPABILITIES): Advertise supported deferred constant capabilities. * c-parser.cc (c_parser_static_assert_declaration_no_semi): Resolve deferred constants in static assertions. * c-typeck.cc (decl_constant_value_1): Handle deferred constants. (build_external_ref): Preserve enumerator identities of marked enum types. (digest_init): Accept deferred constant initializers. (set_init_index): Resolve deferred array designators. (do_case): Handle deferred case values. (c_start_switch): Initialize deferred case handling. (c_finish_switch): Finish deferred case handling. (deferred_constant_initializer_p): New function. (deferred_cst_enum_literal_p): Likewise. (deferred_cst_build_enum_literal): Likewise. (c_add_deferred_case_label): Likewise. (c_build_deferred_case_dispatch): Likewise. (c_deferred_case): New structure. (c_switch): Add deferred_cases member. gcc/testsuite/ChangeLog: * gcc.dg/plugin/deferred_cst_plugin.cc: New test plugin. * gcc.dg/plugin/deferred_cst-basic.c: New test. * gcc.dg/plugin/deferred_cst-canceled.c: New test. * gcc.dg/plugin/deferred_cst-invalid.c: New test. * gcc.dg/plugin/deferred_cst-resolved.c: New test. * gcc.dg/plugin/deferred_cst-run.c: New test. * gcc.dg/plugin/deferred_cst-static-init.c: New test. * gcc.dg/plugin/deferred_cst-switch-case.c: New test. * gcc.dg/plugin/plugin.exp: Register deferred constant tests. Signed-off-by: York Jasper Niebuhr <yjn@yjn-systems.com> --- gcc/Makefile.in | 4 +- gcc/c-family/c-common.cc | 109 ++- gcc/c/c-decl.cc | 24 + gcc/c/c-fold.cc | 183 ++++ gcc/c/c-lang.cc | 5 + gcc/c/c-parser.cc | 6 + gcc/c/c-typeck.cc | 230 ++++- gcc/deferred-cst.cc | 451 ++++++++++ gcc/deferred-cst.h | 98 ++ gcc/gimple-expr.cc | 5 + gcc/gimple.cc | 3 +- gcc/gimplify.cc | 5 + gcc/internal-fn.cc | 18 + gcc/internal-fn.def | 6 + gcc/langhooks-def.h | 4 +- gcc/langhooks.h | 3 + gcc/passes.def | 1 + .../gcc.dg/plugin/deferred_cst-basic.c | 59 ++ .../gcc.dg/plugin/deferred_cst-canceled.c | 37 + .../gcc.dg/plugin/deferred_cst-invalid.c | 77 ++ .../gcc.dg/plugin/deferred_cst-resolved.c | 77 ++ .../gcc.dg/plugin/deferred_cst-run.c | 235 +++++ .../gcc.dg/plugin/deferred_cst-static-init.c | 51 ++ .../gcc.dg/plugin/deferred_cst-switch-case.c | 67 ++ .../gcc.dg/plugin/deferred_cst_plugin.cc | 836 ++++++++++++++++++ gcc/testsuite/gcc.dg/plugin/plugin.exp | 8 + gcc/tree-pass.h | 1 + gcc/tree-pretty-print.cc | 7 + gcc/tree.def | 10 + gcc/varasm.cc | 5 +- 30 files changed, 2603 insertions(+), 22 deletions(-) create mode 100644 gcc/deferred-cst.cc create mode 100644 gcc/deferred-cst.h create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-basic.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-canceled.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-invalid.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-resolved.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-run.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-static-init.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst-switch-case.c create mode 100644 gcc/testsuite/gcc.dg/plugin/deferred_cst_plugin.cc