| 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 "-O3". |
| 12 |
gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3); |
| 13 |
} |
| 14 |
|
| 15 |
#define TEST_COMPILING_TO_FILE |
| 16 |
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER |
| 17 |
#define OUTPUT_FILENAME "output-of-test-pure-attribute.c.s" |
| 18 |
#include "harness.h" |
| 19 |
|
| 20 |
void |
| 21 |
create_code (gcc_jit_context *ctxt, void *user_data) |
| 22 |
{ |
| 23 |
/* Let's try to inject the equivalent of: |
| 24 |
__attribute__ ((pure)) |
| 25 |
int foo (int x); |
| 26 |
int xxx(void) |
| 27 |
{ |
| 28 |
int x = 45; |
| 29 |
int sum = 0; |
| 30 |
|
| 31 |
while (x >>= 1) |
| 32 |
sum += foo (x) * 2; |
| 33 |
return sum; |
| 34 |
} |
| 35 |
*/ |
| 36 |
gcc_jit_type *int_type = |
| 37 |
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); |
| 38 |
|
| 39 |
/* Creating the `foo` function. */ |
| 40 |
gcc_jit_param *n = |
| 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, |
| 46 |
int_type, |
| 47 |
"foo", |
| 48 |
1, params, |
| 49 |
0); |
| 50 |
gcc_jit_function_add_attribute(foo_func, GCC_JIT_FN_ATTRIBUTE_PURE); |
| 51 |
|
| 52 |
/* Creating the `xxx` function. */ |
| 53 |
gcc_jit_function *xxx_func = |
| 54 |
gcc_jit_context_new_function (ctxt, NULL, |
| 55 |
GCC_JIT_FUNCTION_EXPORTED, |
| 56 |
int_type, |
| 57 |
"xxx", |
| 58 |
0, NULL, |
| 59 |
0); |
| 60 |
|
| 61 |
gcc_jit_block *block = gcc_jit_function_new_block (xxx_func, NULL); |
| 62 |
|
| 63 |
/* Build locals: */ |
| 64 |
gcc_jit_lvalue *x = |
| 65 |
gcc_jit_function_new_local (xxx_func, NULL, int_type, "x"); |
| 66 |
gcc_jit_lvalue *sum = |
| 67 |
gcc_jit_function_new_local (xxx_func, NULL, int_type, "sum"); |
| 68 |
|
| 69 |
/* int x = 45 */ |
| 70 |
gcc_jit_block_add_assignment ( |
| 71 |
block, NULL, |
| 72 |
x, |
| 73 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 45)); |
| 74 |
/* int sum = 0 */ |
| 75 |
gcc_jit_block_add_assignment ( |
| 76 |
block, NULL, |
| 77 |
sum, |
| 78 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)); |
| 79 |
|
| 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"); |
| 87 |
|
| 88 |
gcc_jit_block_end_with_jump (block, NULL, loop_cond); |
| 89 |
|
| 90 |
|
| 91 |
/* if (x >>= 1) */ |
| 92 |
/* Since gccjit doesn't (yet?) have support for `>>=` operator, we will decompose it into: |
| 93 |
`if (x = x >> 1)` */ |
| 94 |
gcc_jit_block_add_assignment_op ( |
| 95 |
loop_cond, NULL, |
| 96 |
x, |
| 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 ( |
| 101 |
loop_cond, NULL, |
| 102 |
gcc_jit_context_new_comparison ( |
| 103 |
ctxt, NULL, |
| 104 |
GCC_JIT_COMPARISON_NE, |
| 105 |
gcc_jit_lvalue_as_rvalue (x), |
| 106 |
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)), |
| 107 |
after_loop, |
| 108 |
loop_body); |
| 109 |
|
| 110 |
/* sum += foo (x) * 2; */ |
| 111 |
gcc_jit_rvalue *arg = gcc_jit_lvalue_as_rvalue(x); |
| 112 |
gcc_jit_block_add_assignment_op ( |
| 113 |
loop_body, NULL, |
| 114 |
x, |
| 115 |
GCC_JIT_BINARY_OP_PLUS, |
| 116 |
gcc_jit_context_new_binary_op ( |
| 117 |
ctxt, NULL, |
| 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 ( |
| 121 |
ctxt, |
| 122 |
int_type, |
| 123 |
2))); |
| 124 |
gcc_jit_block_end_with_jump (loop_body, NULL, loop_cond); |
| 125 |
|
| 126 |
/* return sum; */ |
| 127 |
gcc_jit_block_end_with_return (after_loop, NULL, gcc_jit_lvalue_as_rvalue(sum)); |
| 128 |
} |
| 129 |
|
| 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" } } */ |