9 static char *dump_vrp1
;
12 create_code (gcc_jit_context
*ctxt
, void *user_data
)
15 Simple sum-of-squares, to test conditionals and looping
21 for (i = 0; i < n ; i ++)
27 gcc_jit_context_enable_dump (ctxt
, "tree-vrp1", &dump_vrp1
);
29 gcc_jit_type
*the_type
=
30 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
31 gcc_jit_type
*return_type
= the_type
;
34 gcc_jit_context_new_param (ctxt
, NULL
, the_type
, "n");
35 gcc_jit_param
*params
[1] = {n
};
36 gcc_jit_function
*func
=
37 gcc_jit_context_new_function (ctxt
, NULL
,
38 GCC_JIT_FUNCTION_EXPORTED
,
45 gcc_jit_function_new_local (func
, NULL
, the_type
, "i");
47 gcc_jit_function_new_local (func
, NULL
, the_type
, "sum");
49 gcc_jit_block
*initial
=
50 gcc_jit_function_new_block (func
, "initial");
51 gcc_jit_block
*loop_cond
=
52 gcc_jit_function_new_block (func
, "loop_cond");
53 gcc_jit_block
*loop_body
=
54 gcc_jit_function_new_block (func
, "loop_body");
55 gcc_jit_block
*after_loop
=
56 gcc_jit_function_new_block (func
, "after_loop");
59 gcc_jit_block_add_assignment (
62 gcc_jit_context_new_rvalue_from_int (ctxt
, the_type
, 0));
65 gcc_jit_block_add_assignment (
68 gcc_jit_context_new_rvalue_from_int (ctxt
, the_type
, 0));
70 gcc_jit_block_end_with_jump (initial
, NULL
, loop_cond
);
73 gcc_jit_block_end_with_conditional (
75 gcc_jit_context_new_comparison (
77 GCC_JIT_COMPARISON_GE
,
78 gcc_jit_lvalue_as_rvalue (i
),
79 gcc_jit_param_as_rvalue (n
)),
84 gcc_jit_block_add_assignment (
87 gcc_jit_context_new_binary_op (
89 GCC_JIT_BINARY_OP_PLUS
, the_type
,
90 gcc_jit_lvalue_as_rvalue (sum
),
91 gcc_jit_context_new_binary_op (
93 GCC_JIT_BINARY_OP_MULT
, the_type
,
94 gcc_jit_lvalue_as_rvalue (i
),
95 gcc_jit_lvalue_as_rvalue (i
))));
98 gcc_jit_block_add_assignment (
101 gcc_jit_context_new_binary_op (
103 GCC_JIT_BINARY_OP_PLUS
, the_type
,
104 gcc_jit_lvalue_as_rvalue (i
),
105 gcc_jit_context_new_rvalue_from_int (
110 gcc_jit_block_end_with_jump (loop_body
, NULL
, loop_cond
);
113 gcc_jit_block_end_with_return (
116 gcc_jit_lvalue_as_rvalue (sum
));
120 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
122 typedef int (*loop_test_fn_type
) (int);
123 CHECK_NON_NULL (result
);
124 loop_test_fn_type loop_test
=
125 (loop_test_fn_type
)gcc_jit_result_get_code (result
, "loop_test");
126 CHECK_NON_NULL (loop_test
);
127 int val
= loop_test (10);
128 note ("loop_test returned: %d", val
);
129 CHECK_VALUE (val
, 285);
131 CHECK_NON_NULL (dump_vrp1
);
133 An example of using gcc_jit_context_enable_dump to verify a property
136 In particular, this example verifies that we can read the dump from a
137 pass with multiple instances. Given that the format of the VRP dump
138 is subject to change, merely verify that it contains the function
140 CHECK_STRING_CONTAINS (dump_vrp1
, ";; Function loop_test");