| 1 |
/* { dg-do compile { target x86_64-*-* } } */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <stdio.h> |
| 5 |
|
| 6 |
#include "libgccjit.h" |
| 7 |
|
| 8 |
#define TEST_ESCHEWS_SET_OPTIONS |
| 9 |
static void set_options (gcc_jit_context *ctxt, const char *argv0) |
| 10 |
{ |
| 11 |
// Set "-O0". |
| 12 |
gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 0); |
| 13 |
} |
| 14 |
|
| 15 |
#define TEST_COMPILING_TO_FILE |
| 16 |
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER |
| 17 |
#define OUTPUT_FILENAME "output-of-test-always_inline-attribute.c.s" |
| 18 |
#include "harness.h" |
| 19 |
|
| 20 |
gcc_jit_function* |
| 21 |
create_function (gcc_jit_context *ctxt, |
| 22 |
const char *func_name, |
| 23 |
gcc_jit_type *int_type, |
| 24 |
gcc_jit_type *pint_type) |
| 25 |
{ |
| 26 |
/* The `a` function argument */ |
| 27 |
gcc_jit_param *a = gcc_jit_context_new_param (ctxt, NULL, pint_type, "a"); |
| 28 |
gcc_jit_function *func = |
| 29 |
gcc_jit_context_new_function (ctxt, NULL, |
| 30 |
GCC_JIT_FUNCTION_INTERNAL, |
| 31 |
int_type, |
| 32 |
func_name, |
| 33 |
1, &a, |
| 34 |
0); |
| 35 |
|
| 36 |
gcc_jit_block *if_cond = |
| 37 |
gcc_jit_function_new_block (func, "if_cond"); |
| 38 |
gcc_jit_block *if_body = |
| 39 |
gcc_jit_function_new_block (func, "if_body"); |
| 40 |
gcc_jit_block *after_if = |
| 41 |
gcc_jit_function_new_block (func, "after_if"); |
| 42 |
|
| 43 |
/* if (!a) */ |
| 44 |
gcc_jit_block_end_with_conditional ( |
| 45 |
if_cond, NULL, |
| 46 |
gcc_jit_context_new_comparison ( |
| 47 |
ctxt, NULL, |
| 48 |
GCC_JIT_COMPARISON_EQ, |
| 49 |
gcc_jit_param_as_rvalue (a), |
| 50 |
gcc_jit_context_null (ctxt, pint_type)), |
| 51 |
if_body, |
| 52 |
after_if); |
| 53 |
/* return -1; */ |
| 54 |
gcc_jit_block_end_with_return ( |
| 55 |
if_body, NULL, |
| 56 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, -1)); |
| 57 |
|
| 58 |
/* return *a; */ |
| 59 |
gcc_jit_block_end_with_return ( |
| 60 |
after_if, NULL, |
| 61 |
gcc_jit_lvalue_as_rvalue ( |
| 62 |
gcc_jit_rvalue_dereference ( |
| 63 |
gcc_jit_param_as_rvalue (a), NULL))); |
| 64 |
|
| 65 |
return func; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
void |
| 70 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 71 |
{ |
| 72 |
/* Let's try to inject the equivalent of: |
| 73 |
__attribute__ ((always_inline)) |
| 74 |
static inline int removed (int *a) { |
| 75 |
if (!a) { |
| 76 |
return -1; |
| 77 |
} |
| 78 |
return *a; |
| 79 |
} |
| 80 |
static int not_removed (int *a) { |
| 81 |
if (!a) { |
| 82 |
return -1; |
| 83 |
} |
| 84 |
return *a; |
| 85 |
} |
| 86 |
int foo () { |
| 87 |
int x = 0; |
| 88 |
x += removed(NULL); |
| 89 |
x += not_removed(NULL); |
| 90 |
return x; |
| 91 |
} |
| 92 |
*/ |
| 93 |
gcc_jit_type *int_type = |
| 94 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 95 |
gcc_jit_type *pint_type = gcc_jit_type_get_pointer (int_type); |
| 96 |
|
| 97 |
/* Creating the `removed` function. */ |
| 98 |
gcc_jit_function *removed_func = |
| 99 |
create_function (ctxt, "removed", int_type, pint_type); |
| 100 |
/* This one is to declare the function as "inline" */ |
| 101 |
gcc_jit_function_add_attribute(removed_func, GCC_JIT_FN_ATTRIBUTE_INLINE); |
| 102 |
/* __attribute__ ((always_inline)) */ |
| 103 |
gcc_jit_function_add_attribute(removed_func, GCC_JIT_FN_ATTRIBUTE_ALWAYS_INLINE); |
| 104 |
|
| 105 |
/* Creating the `not_removed` function. */ |
| 106 |
gcc_jit_function *not_removed_func = |
| 107 |
create_function (ctxt, "not_removed", int_type, pint_type); |
| 108 |
|
| 109 |
/* Creating the `foo` function. */ |
| 110 |
gcc_jit_function *foo_func = |
| 111 |
gcc_jit_context_new_function (ctxt, NULL, |
| 112 |
GCC_JIT_FUNCTION_EXPORTED, |
| 113 |
int_type, |
| 114 |
"foo", |
| 115 |
0, NULL, |
| 116 |
0); |
| 117 |
|
| 118 |
gcc_jit_block *foo_block = gcc_jit_function_new_block (foo_func, NULL); |
| 119 |
|
| 120 |
/* Build locals: */ |
| 121 |
gcc_jit_lvalue *x = |
| 122 |
gcc_jit_function_new_local (foo_func, NULL, int_type, "x"); |
| 123 |
|
| 124 |
/* int x = 0; */ |
| 125 |
gcc_jit_block_add_assignment ( |
| 126 |
foo_block, NULL, |
| 127 |
x, |
| 128 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)); |
| 129 |
|
| 130 |
/* x += removed(NULL); */ |
| 131 |
gcc_jit_rvalue *null = gcc_jit_context_null (ctxt, pint_type); |
| 132 |
gcc_jit_block_add_assignment_op ( |
| 133 |
foo_block, NULL, |
| 134 |
x, |
| 135 |
GCC_JIT_BINARY_OP_PLUS, |
| 136 |
gcc_jit_context_new_call (ctxt, NULL, removed_func, 1, &null)); |
| 137 |
|
| 138 |
/* x += not_removed(NULL); */ |
| 139 |
gcc_jit_block_add_assignment_op ( |
| 140 |
foo_block, NULL, |
| 141 |
x, |
| 142 |
GCC_JIT_BINARY_OP_PLUS, |
| 143 |
gcc_jit_context_new_call (ctxt, NULL, not_removed_func, 1, &null)); |
| 144 |
|
| 145 |
/* return x; */ |
| 146 |
gcc_jit_block_end_with_return (foo_block, NULL, gcc_jit_lvalue_as_rvalue(x)); |
| 147 |
} |
| 148 |
|
| 149 |
/* { dg-final { jit-verify-output-file-was-created "" } } */ |
| 150 |
/* Check that the "removed" function was inlined, but not the others */ |
| 151 |
/* { dg-final { jit-verify-assembler-output-not ".type\\s+removed,\\s+@function" { target { ! *-*-darwin* } } } } */ |
| 152 |
/* { dg-final { jit-verify-assembler-output ".type\\s+not_removed,\\s+@function" { target { ! *-*-darwin* } } } } */ |
| 153 |
/* { dg-final { jit-verify-assembler-output ".type\\s+foo,\\s+@function" { target { ! *-*-darwin* } } } } */ |
| 154 |
|
| 155 |
/* { dg-final { jit-verify-assembler-output-not "\\n_removed:" { target *-*-darwin* } } } */ |
| 156 |
/* { dg-final { jit-verify-assembler-output "\\n_not_removed:" { target *-*-darwin* } } } */ |
| 157 |
/* { dg-final { jit-verify-assembler-output "\\n_foo:" { target *-*-darwin* } } } */ |