Preserve SSA info for more propagated copy
[official-gcc.git] / gcc / testsuite / jit.dg / test-nonnull-attribute.c
blob42c38acb96234d13a985311fc1b5ad6ace1553f5
1 /* { dg-do compile { target x86_64-*-* } } */
3 #include <stdlib.h>
4 #include <stdio.h>
6 #include "libgccjit.h"
8 #define TEST_ESCHEWS_SET_OPTIONS
9 static void set_options (gcc_jit_context *ctxt, const char *argv0)
11 // Set "-O2".
12 gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 2);
15 #define TEST_COMPILING_TO_FILE
16 #define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
17 #define OUTPUT_FILENAME "output-of-test-nonnull.c.s"
18 #include "harness.h"
20 void
21 create_code (gcc_jit_context *ctxt, void *user_data)
23 /* Let's try to inject the equivalent of:
25 __attribute__((nonnull(1)))
26 int t(int *a) {
27 if (!a) {
28 return -1;
30 return *a;
33 gcc_jit_type *int_type =
34 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
35 gcc_jit_type *pint_type = gcc_jit_type_get_pointer(int_type);
37 gcc_jit_param *a =
38 gcc_jit_context_new_param (ctxt, NULL, pint_type, "a");
40 gcc_jit_function *func_t =
41 gcc_jit_context_new_function (ctxt, NULL,
42 GCC_JIT_FUNCTION_EXPORTED,
43 int_type,
44 "t",
45 1, &a,
46 0);
47 /* Adding `nonnull(1)` attribute. */
48 int indexes[1] = {1};
49 gcc_jit_function_add_integer_array_attribute (
50 func_t,
51 GCC_JIT_FN_ATTRIBUTE_NONNULL,
52 indexes,
56 /* if (!a) {
57 return -1;
58 } */
59 gcc_jit_block *if_cond =
60 gcc_jit_function_new_block (func_t, "if_cond");
61 gcc_jit_block *if_body =
62 gcc_jit_function_new_block (func_t, "if_body");
63 gcc_jit_block *after_if =
64 gcc_jit_function_new_block (func_t, "after_if");
66 /* if (!a) */
67 gcc_jit_block_end_with_conditional (
68 if_cond, NULL,
69 gcc_jit_context_new_comparison (
70 ctxt, NULL,
71 GCC_JIT_COMPARISON_EQ,
72 gcc_jit_param_as_rvalue (a),
73 gcc_jit_context_null (ctxt, pint_type)),
74 if_body,
75 after_if);
76 /* return -1; */
77 gcc_jit_block_end_with_return (
78 if_body, NULL,
79 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, -1));
81 /* return *a; */
82 gcc_jit_block_end_with_return (
83 after_if, NULL,
84 gcc_jit_lvalue_as_rvalue (
85 gcc_jit_rvalue_dereference (
86 gcc_jit_param_as_rvalue (a), NULL)));
89 /* { dg-final { jit-verify-output-file-was-created "" } } */
90 /* Check that the "if block" was optimized away */
91 /* { dg-final { jit-verify-assembler-output-not "testq" } } */
92 /* { dg-final { jit-verify-assembler-output-not "-1" } } */