test-sum-of-squares.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "libgccjit.h"
  5. #include "harness.h"
  6. static char *dump_vrp1;
  7. void
  8. create_code (gcc_jit_context *ctxt, void *user_data)
  9. {
  10. /*
  11. Simple sum-of-squares, to test conditionals and looping
  12. int loop_test (int n)
  13. {
  14. int i;
  15. int sum = 0;
  16. for (i = 0; i < n ; i ++)
  17. {
  18. sum += i * i;
  19. }
  20. return sum;
  21. */
  22. gcc_jit_context_enable_dump (ctxt, "tree-vrp1", &dump_vrp1);
  23. gcc_jit_type *the_type =
  24. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  25. gcc_jit_type *return_type = the_type;
  26. gcc_jit_param *n =
  27. gcc_jit_context_new_param (ctxt, NULL, the_type, "n");
  28. gcc_jit_param *params[1] = {n};
  29. gcc_jit_function *func =
  30. gcc_jit_context_new_function (ctxt, NULL,
  31. GCC_JIT_FUNCTION_EXPORTED,
  32. return_type,
  33. "loop_test",
  34. 1, params, 0);
  35. /* Build locals: */
  36. gcc_jit_lvalue *i =
  37. gcc_jit_function_new_local (func, NULL, the_type, "i");
  38. gcc_jit_lvalue *sum =
  39. gcc_jit_function_new_local (func, NULL, the_type, "sum");
  40. gcc_jit_block *initial =
  41. gcc_jit_function_new_block (func, "initial");
  42. gcc_jit_block *loop_cond =
  43. gcc_jit_function_new_block (func, "loop_cond");
  44. gcc_jit_block *loop_body =
  45. gcc_jit_function_new_block (func, "loop_body");
  46. gcc_jit_block *after_loop =
  47. gcc_jit_function_new_block (func, "after_loop");
  48. /* sum = 0; */
  49. gcc_jit_block_add_assignment (
  50. initial, NULL,
  51. sum,
  52. gcc_jit_context_new_rvalue_from_int (ctxt, the_type, 0));
  53. /* i = 0; */
  54. gcc_jit_block_add_assignment (
  55. initial, NULL,
  56. i,
  57. gcc_jit_context_new_rvalue_from_int (ctxt, the_type, 0));
  58. gcc_jit_block_end_with_jump (initial, NULL, loop_cond);
  59. /* if (i >= n) */
  60. gcc_jit_block_end_with_conditional (
  61. loop_cond, NULL,
  62. gcc_jit_context_new_comparison (
  63. ctxt, NULL,
  64. GCC_JIT_COMPARISON_GE,
  65. gcc_jit_lvalue_as_rvalue (i),
  66. gcc_jit_param_as_rvalue (n)),
  67. after_loop,
  68. loop_body);
  69. /* sum += i * i */
  70. gcc_jit_block_add_assignment (
  71. loop_body, NULL,
  72. sum,
  73. gcc_jit_context_new_binary_op (
  74. ctxt, NULL,
  75. GCC_JIT_BINARY_OP_PLUS, the_type,
  76. gcc_jit_lvalue_as_rvalue (sum),
  77. gcc_jit_context_new_binary_op (
  78. ctxt, NULL,
  79. GCC_JIT_BINARY_OP_MULT, the_type,
  80. gcc_jit_lvalue_as_rvalue (i),
  81. gcc_jit_lvalue_as_rvalue (i))));
  82. /* i++ */
  83. gcc_jit_block_add_assignment (
  84. loop_body, NULL,
  85. i,
  86. gcc_jit_context_new_binary_op (
  87. ctxt, NULL,
  88. GCC_JIT_BINARY_OP_PLUS, the_type,
  89. gcc_jit_lvalue_as_rvalue (i),
  90. gcc_jit_context_new_rvalue_from_int (
  91. ctxt,
  92. the_type,
  93. 1)));
  94. gcc_jit_block_end_with_jump (loop_body, NULL, loop_cond);
  95. /* return sum */
  96. gcc_jit_block_end_with_return (
  97. after_loop,
  98. NULL,
  99. gcc_jit_lvalue_as_rvalue (sum));
  100. }
  101. void
  102. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  103. {
  104. typedef int (*loop_test_fn_type) (int);
  105. CHECK_NON_NULL (result);
  106. loop_test_fn_type loop_test =
  107. (loop_test_fn_type)gcc_jit_result_get_code (result, "loop_test");
  108. CHECK_NON_NULL (loop_test);
  109. int val = loop_test (10);
  110. note ("loop_test returned: %d", val);
  111. CHECK_VALUE (val, 285);
  112. CHECK_NON_NULL (dump_vrp1);
  113. /* PR jit/64166
  114. An example of using gcc_jit_context_enable_dump to verify a property
  115. of the compile.
  116. In this case, verify that vrp is able to deduce the
  117. bounds of the iteration variable. Specifically, verify that some
  118. variable is known to be in the range negative infinity to some
  119. expression based on param "n" (actually n-1). */
  120. CHECK_STRING_CONTAINS (dump_vrp1, ": [-INF, n_");
  121. free (dump_vrp1);
  122. }