1 /* { dg-do compile { target x86_64-*-* } } */
8 #define TEST_ESCHEWS_SET_OPTIONS
9 static void set_options (gcc_jit_context
*ctxt
, const char *argv0
)
12 gcc_jit_context_set_int_option(ctxt
, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL
, 3);
15 #define TEST_COMPILING_TO_FILE
16 #define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
17 #define OUTPUT_FILENAME "output-of-test-const-attribute.c.s"
21 create_code (gcc_jit_context
*ctxt
, void *user_data
)
23 /* Let's try to inject the equivalent of:
24 __attribute__ ((const))
36 gcc_jit_type
*int_type
=
37 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
39 /* Creating the `foo` function. */
41 gcc_jit_context_new_param (ctxt
, NULL
, int_type
, "x");
42 gcc_jit_param
*params
[1] = {n
};
43 gcc_jit_function
*foo_func
=
44 gcc_jit_context_new_function (ctxt
, NULL
,
45 GCC_JIT_FUNCTION_IMPORTED
,
50 gcc_jit_function_add_attribute(foo_func
, GCC_JIT_FN_ATTRIBUTE_CONST
);
52 /* Creating the `xxx` function. */
53 gcc_jit_function
*xxx_func
=
54 gcc_jit_context_new_function (ctxt
, NULL
,
55 GCC_JIT_FUNCTION_EXPORTED
,
61 gcc_jit_block
*block
= gcc_jit_function_new_block (xxx_func
, NULL
);
65 gcc_jit_function_new_local (xxx_func
, NULL
, int_type
, "x");
67 gcc_jit_function_new_local (xxx_func
, NULL
, int_type
, "sum");
70 gcc_jit_block_add_assignment (
73 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 45));
75 gcc_jit_block_add_assignment (
78 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 0));
80 /* while (x >>= 1) { sum += foo (x) * 2; } */
81 gcc_jit_block
*loop_cond
=
82 gcc_jit_function_new_block (xxx_func
, "loop_cond");
83 gcc_jit_block
*loop_body
=
84 gcc_jit_function_new_block (xxx_func
, "loop_body");
85 gcc_jit_block
*after_loop
=
86 gcc_jit_function_new_block (xxx_func
, "after_loop");
88 gcc_jit_block_end_with_jump (block
, NULL
, loop_cond
);
92 /* Since gccjit doesn't (yet?) have support for `>>=` operator, we will decompose it into:
94 gcc_jit_block_add_assignment_op (
97 GCC_JIT_BINARY_OP_RSHIFT
,
98 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 1));
99 /* The condition itself */
100 gcc_jit_block_end_with_conditional (
102 gcc_jit_context_new_comparison (
104 GCC_JIT_COMPARISON_NE
,
105 gcc_jit_lvalue_as_rvalue (x
),
106 gcc_jit_context_new_rvalue_from_int (ctxt
, int_type
, 0)),
110 /* sum += foo (x) * 2; */
111 gcc_jit_rvalue
*arg
= gcc_jit_lvalue_as_rvalue(x
);
112 gcc_jit_block_add_assignment_op (
115 GCC_JIT_BINARY_OP_PLUS
,
116 gcc_jit_context_new_binary_op (
118 GCC_JIT_BINARY_OP_MULT
, int_type
,
119 gcc_jit_context_new_call (ctxt
, NULL
, foo_func
, 1, &arg
),
120 gcc_jit_context_new_rvalue_from_int (
124 gcc_jit_block_end_with_jump (loop_body
, NULL
, loop_cond
);
127 gcc_jit_block_end_with_return (after_loop
, NULL
, gcc_jit_lvalue_as_rvalue(sum
));
130 /* { dg-final { jit-verify-output-file-was-created "" } } */
131 /* Check that the loop was optimized away */
132 /* { dg-final { jit-verify-assembler-output-not "jne" } } */