Preserve SSA info for more propagated copy
[official-gcc.git] / gcc / testsuite / jit.dg / test-tls.c
blob3b20182ac1071142111919dca41d576a1e09602b
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
6 #include "libgccjit.h"
8 #include "harness.h"
10 void
11 create_code (gcc_jit_context *ctxt, void *user_data)
13 /* Let's try to inject the equivalent of:
15 _Thread_local int foo;
17 int test_using_tls()
19 foo = 42;
20 return foo;
23 gcc_jit_type *int_type =
24 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
26 gcc_jit_lvalue *foo =
27 gcc_jit_context_new_global (
28 ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, int_type, "foo");
29 gcc_jit_lvalue_set_tls_model (foo, GCC_JIT_TLS_MODEL_GLOBAL_DYNAMIC);
31 /* Build the test_fn. */
32 gcc_jit_function *test_fn =
33 gcc_jit_context_new_function (ctxt, NULL,
34 GCC_JIT_FUNCTION_EXPORTED,
35 int_type,
36 "test_using_tls",
37 0, NULL,
38 0);
39 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
40 gcc_jit_block_add_assignment (
41 block, NULL,
42 foo,
43 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
44 gcc_jit_block_end_with_return (block,
45 NULL,
46 gcc_jit_lvalue_as_rvalue (foo));
49 void
50 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
52 typedef int (*fn_type) (void);
53 CHECK_NON_NULL (result);
55 fn_type test_using_tls =
56 (fn_type)gcc_jit_result_get_code (result, "test_using_tls");
57 CHECK_NON_NULL (test_using_tls);
58 int return_value = test_using_tls();
59 CHECK_VALUE (return_value, 42);
61 int *foo = (int *)gcc_jit_result_get_global (result, "foo");
62 CHECK_NON_NULL (foo);
63 CHECK_VALUE (*foo, 42);