diff --git a/infer/tests/codetoanalyze/c/performance/compound_loop_guard.c b/infer/tests/codetoanalyze/c/performance/compound_loop_guard.c index fecf4e2e8..9bc8b8180 100644 --- a/infer/tests/codetoanalyze/c/performance/compound_loop_guard.c +++ b/infer/tests/codetoanalyze/c/performance/compound_loop_guard.c @@ -6,7 +6,7 @@ */ /* while loop that contains && in the guard. It gives the correct bound. - * Expected: Theta(m) */ + * Expected: O(m) */ int compound_while(int m) { int i = 0; int j = 3 * i; @@ -16,8 +16,7 @@ int compound_while(int m) { return j; } -/* this should give Theta(100) once we have extract_post in the range */ -int simplified_simulated_while_with_and(int p) { +int simplified_simulated_while_with_and_constant(int p) { int k = 0; int j = 0; B: @@ -29,7 +28,7 @@ B: } /* simulated goto that contains && */ -int simulated_while_with_and(int p) { +int simulated_while_with_and_linear(int p) { int i = 0; int k = 0; LOOP_COND: @@ -46,7 +45,7 @@ RETURN: } /* shortcut in the conditional, hence we won't loop, and get constant cost */ -int simulated_while_shortcut(int p) { +int simulated_while_shortcut_constant(int p) { int k = 0; int j = 0; B: @@ -57,7 +56,7 @@ B: return k; } -/* p should be in control vars */ +/* p should be in control vars. If p is 1, can run forever */ void while_and_or(int p) { int i = 0; while (p == 1 || (i < 30 && i >= 0)) { @@ -66,7 +65,7 @@ void while_and_or(int p) { } // should be constant cost -int nested_while_and_or(int p) { +int nested_while_and_or_constant(int p) { int i = 0; int j = 3 * i; while (p == 1 || (i < 30 && i >= 0)) { @@ -80,8 +79,7 @@ int nested_while_and_or(int p) { } /* j and i will be control variables for B */ -/* Expected: 5 + 100 */ -int simulated_nested_loop_with_and(int p) { +int simulated_nested_loop_with_and_constant(int p) { int k = 0; int t = 5; int j = 0; diff --git a/infer/tests/codetoanalyze/c/performance/cost-issues.exp b/infer/tests/codetoanalyze/c/performance/cost-issues.exp index 3407257e1..987ecb33c 100644 --- a/infer/tests/codetoanalyze/c/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/c/performance/cost-issues.exp @@ -2,67 +2,60 @@ codetoanalyze/c/performance/break.c, break_constant_FP, 8 + 5 ⋅ p + 2 ⋅ (1+m codetoanalyze/c/performance/break.c, break_loop, 5 + 5 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 10, column 3,{p},Loop at line 10, column 3] codetoanalyze/c/performance/break.c, break_loop_with_t, 7 + 5 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 22, column 3,{p},Loop at line 22, column 3] codetoanalyze/c/performance/compound_loop_guard.c, compound_while, 7 + 3 ⋅ m + 4 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 13, column 3,{m},Loop at line 13, column 3] -codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or, 20, OnUIThread:false, [] -codetoanalyze/c/performance/compound_loop_guard.c, simplified_simulated_while_with_and, 605, OnUIThread:false, [] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and, 3529, OnUIThread:false, [] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_shortcut, 9, OnUIThread:false, [] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_with_and, 6 + 3 ⋅ p + 4 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 43, column 3,{p},Loop at line 43, column 3] -codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 63, column 3] -codetoanalyze/c/performance/cost_test.c, alias2_OK, 7, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, alias_OK, 6, OnUIThread:false, [] +codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or_constant, 20, OnUIThread:false, [] +codetoanalyze/c/performance/compound_loop_guard.c, simplified_simulated_while_with_and_constant, 605, OnUIThread:false, [] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and_constant, 3529, OnUIThread:false, [] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_shortcut_constant, 9, OnUIThread:false, [] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_with_and_linear, 6 + 3 ⋅ p + 4 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 42, column 3,{p},Loop at line 42, column 3] +codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 62, column 3] codetoanalyze/c/performance/cost_test.c, always, 9, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, bar_OK, 19, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, call_infinite, ⊤, OnUIThread:false, [Call to infinite,Unbounded loop,Loop at line 146, column 3] -codetoanalyze/c/performance/cost_test.c, call_while_upto20_10_good, 56, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, call_while_upto20_minus100_bad, 606, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, call_while_upto20_unsigned_good, 6 + 4 ⋅ (-x + 20) + (21-min(20, x)), OnUIThread:false, [{21-min(20, x)},call to while_upto20_bad,Loop at line 119, column 3,{-x + 20},call to while_upto20_bad,Loop at line 119, column 3] -codetoanalyze/c/performance/cost_test.c, cond_OK, 27, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, call_infinite, ⊤, OnUIThread:false, [Call to infinite,Unbounded loop,Loop at line 90, column 3] +codetoanalyze/c/performance/cost_test.c, call_while_upto20_10_constant, 56, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, call_while_upto20_minus100_constant, 606, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, call_while_upto20_unsigned, 6 + 4 ⋅ (-x + 20) + (21-min(20, x)), OnUIThread:false, [{21-min(20, x)},call to while_upto20,Loop at line 63, column 3,{-x + 20},call to while_upto20,Loop at line 63, column 3] +codetoanalyze/c/performance/cost_test.c, cond_constant, 14, OnUIThread:false, [] codetoanalyze/c/performance/cost_test.c, div_const, 3, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, foo_OK, 6, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, ignore_character_symbols_constant_FP, 5 + 4 ⋅ (122-min(97, c)), OnUIThread:false, [{122-min(97, c)},Loop at line 155, column 3] -codetoanalyze/c/performance/cost_test.c, infinite, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 146, column 3] +codetoanalyze/c/performance/cost_test.c, foo_constant, 6, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, infinite, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 90, column 3] codetoanalyze/c/performance/cost_test.c, infinite_FN, 19, OnUIThread:false, [] codetoanalyze/c/performance/cost_test.c, iter_div_const_constant, 109, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, loop0_bad, 1105, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, loop1_bad, 1207, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, loop2_bad, 4 + 10 ⋅ k + 2 ⋅ (1+max(0, k)), OnUIThread:false, [{1+max(0, k)},Loop at line 87, column 3,{k},Loop at line 87, column 3] -codetoanalyze/c/performance/cost_test.c, loop3_bad, 187, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, main_bad, 241, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, loop0_constant, 1005, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, loop1_constant, 1107, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, loop2_linear, 4 + 4 ⋅ k + 2 ⋅ (1+max(0, k)), OnUIThread:false, [{1+max(0, k)},Loop at line 47, column 3,{k},Loop at line 47, column 3] +codetoanalyze/c/performance/cost_test.c, loop3_constant, 97, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test.c, loop_character_symbols_linear, 5 + 4 ⋅ (122-min(97, c)), OnUIThread:false, [{122-min(97, c)},Loop at line 98, column 3] codetoanalyze/c/performance/cost_test.c, unit_cost_function, 1, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test.c, while_upto20_bad, 4 + 4 ⋅ (-m + 20) + (21-min(20, m)), OnUIThread:false, [{21-min(20, m)},Loop at line 119, column 3,{-m + 20},Loop at line 119, column 3] +codetoanalyze/c/performance/cost_test.c, while_upto20, 4 + 4 ⋅ (-m + 20) + (21-min(20, m)), OnUIThread:false, [{21-min(20, m)},Loop at line 63, column 3,{-m + 20},Loop at line 63, column 3] codetoanalyze/c/performance/cost_test_deps.c, foo, 4, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, if_bad, 75, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, if_bad_loop, 203, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, loop_despite_inferbo, 1208, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, if_bad_constant, 75, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, if_bad_loop_constant, 203, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, loop_despite_inferbo_constant, 1208, OnUIThread:false, [] codetoanalyze/c/performance/cost_test_deps.c, loop_no_dep1, 609, OnUIThread:false, [] codetoanalyze/c/performance/cost_test_deps.c, loop_no_dep2, 614, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, nested_loop, 2547, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, real_while, 218, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop, 2529, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_cond_in_goto, 3534, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_more_expensive, 2534, OnUIThread:false, [] -codetoanalyze/c/performance/cost_test_deps.c, simulated_while, 218, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, nested_loop_constant, 2547, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, real_while_constant, 218, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_cond_in_goto_constant, 3534, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_constant, 2529, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_more_expensive_constant, 2534, OnUIThread:false, [] +codetoanalyze/c/performance/cost_test_deps.c, simulated_while_constant, 218, OnUIThread:false, [] codetoanalyze/c/performance/cost_test_deps.c, two_loops, 549, OnUIThread:false, [] -codetoanalyze/c/performance/exit.c, call_exit_unreachable_unreachable, ⊥, OnUIThread:false, [Unreachable node] -codetoanalyze/c/performance/exit.c, call_unreachable_constant, 1, OnUIThread:false, [] -codetoanalyze/c/performance/exit.c, compute_exit_unreachable_unreachable, ⊥, OnUIThread:false, [Unreachable node] -codetoanalyze/c/performance/exit.c, exit_unreachable_unreachable, ⊥, OnUIThread:false, [Unreachable node] +codetoanalyze/c/performance/exit.c, call_exit_unreachable, ⊥, OnUIThread:false, [Unreachable node] +codetoanalyze/c/performance/exit.c, call_unreachable_constant, 2, OnUIThread:false, [] +codetoanalyze/c/performance/exit.c, compute_exit_unreachable, ⊥, OnUIThread:false, [Unreachable node] +codetoanalyze/c/performance/exit.c, exit_unreachable, ⊥, OnUIThread:false, [Unreachable node] codetoanalyze/c/performance/exit.c, inline_exit_unreachable_FP, 3 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 30, column 3,{p},Loop at line 30, column 3] codetoanalyze/c/performance/exit.c, linear, 3 + 3 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 18, column 3,{p},Loop at line 18, column 3] -codetoanalyze/c/performance/instantiate.c, do_2K_times_Bad, 14006, OnUIThread:false, [] -codetoanalyze/c/performance/instantiate.c, do_2_times_Good, 20, OnUIThread:false, [] -codetoanalyze/c/performance/instantiate.c, do_half_m2_times, 3 + 5 ⋅ (m - 1) × m + 7 ⋅ m + 2 ⋅ m × (max(1, m)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 31, column 3,{max(1, m)},call to do_n_times,Loop at line 12, column 3,{m},Loop at line 31, column 3,{m},Loop at line 31, column 3,{m - 1},call to do_n_times,Loop at line 12, column 3] -codetoanalyze/c/performance/instantiate.c, do_m2_times, 3 + 7 ⋅ m + 5 ⋅ m × m + 2 ⋅ m × (1+max(0, m)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 24, column 3,{1+max(0, m)},call to do_n_times,Loop at line 12, column 3,{m},call to do_n_times,Loop at line 12, column 3,{m},Loop at line 24, column 3] +codetoanalyze/c/performance/instantiate.c, do_2_times_constant, 20, OnUIThread:false, [] +codetoanalyze/c/performance/instantiate.c, do_half_m2_times_quadratic, 3 + 5 ⋅ (m - 1) × m + 7 ⋅ m + 2 ⋅ m × (max(1, m)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 28, column 3,{max(1, m)},call to do_n_times,Loop at line 12, column 3,{m},Loop at line 28, column 3,{m},Loop at line 28, column 3,{m - 1},call to do_n_times,Loop at line 12, column 3] +codetoanalyze/c/performance/instantiate.c, do_m2_times_quadratic, 3 + 7 ⋅ m + 5 ⋅ m × m + 2 ⋅ m × (1+max(0, m)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 21, column 3,{1+max(0, m)},call to do_n_times,Loop at line 12, column 3,{m},call to do_n_times,Loop at line 12, column 3,{m},Loop at line 21, column 3] codetoanalyze/c/performance/instantiate.c, do_n_times, 3 + 5 ⋅ n + 2 ⋅ (1+max(0, n)), OnUIThread:false, [{1+max(0, n)},Loop at line 12, column 3,{n},Loop at line 12, column 3] codetoanalyze/c/performance/instantiate.c, no_op, 2, OnUIThread:false, [] -codetoanalyze/c/performance/invariant.c, do_k_times, 5 + 3 ⋅ n + 2 ⋅ (1+max(0, n)), OnUIThread:false, [{1+max(0, n)},Loop at line 23, column 3,{n},Loop at line 23, column 3] -codetoanalyze/c/performance/invariant.c, do_k_times_array, 5 + 6 ⋅ n + 2 ⋅ (1+max(0, n)), OnUIThread:false, [{1+max(0, n)},Loop at line 31, column 3,{n},Loop at line 31, column 3] -codetoanalyze/c/performance/invariant.c, do_n_m_times_nested, 7 + 5 ⋅ n + 3 ⋅ n × m + 2 ⋅ n × (1+max(0, m)) + 2 ⋅ (1+max(0, n)), OnUIThread:false, [{1+max(0, n)},Loop at line 40, column 3,{1+max(0, m)},Loop at line 41, column 5,{m},Loop at line 41, column 5,{n},Loop at line 40, column 3] -codetoanalyze/c/performance/invariant.c, two_loops_nested_invariant, 6 + 23 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 50, column 3,{p},Loop at line 50, column 3] +codetoanalyze/c/performance/invariant.c, do_n_m_times_nested, 7 + 5 ⋅ n + 3 ⋅ n × m + 2 ⋅ n × (1+max(0, m)) + 2 ⋅ (1+max(0, n)), OnUIThread:false, [{1+max(0, n)},Loop at line 24, column 3,{1+max(0, m)},Loop at line 25, column 5,{m},Loop at line 25, column 5,{n},Loop at line 24, column 3] +codetoanalyze/c/performance/invariant.c, two_loops_nested_invariant, 6 + 23 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},Loop at line 34, column 3,{p},Loop at line 34, column 3] codetoanalyze/c/performance/invariant.c, while_infinite_FN, 2, OnUIThread:false, [] codetoanalyze/c/performance/invariant.c, while_unique_def_FN, 15, OnUIThread:false, [] -codetoanalyze/c/performance/jump_inside_loop.c, jump_inside_loop, 2008, OnUIThread:false, [] -codetoanalyze/c/performance/jump_inside_loop.c, loop_always, 28, OnUIThread:false, [] +codetoanalyze/c/performance/jump_inside_loop.c, jump_inside_loop_constant_linear, 9 + (k - 1) + 4 ⋅ (max(1, k)), OnUIThread:false, [{max(1, k)},Loop at line 36, column 3,{k - 1},Loop at line 36, column 3] +codetoanalyze/c/performance/jump_inside_loop.c, loop_always_linear, 7 + k + 2 ⋅ (max(1, k)) + 2 ⋅ (1+max(1, k)), OnUIThread:false, [{1+max(1, k)},Loop at line 21, column 3,{max(1, k)},Loop at line 21, column 3,{k},Loop at line 21, column 3] codetoanalyze/c/performance/loops.c, __infer_globals_initializer_array1, 4, OnUIThread:false, [] codetoanalyze/c/performance/loops.c, __infer_globals_initializer_array2, 2, OnUIThread:false, [] codetoanalyze/c/performance/loops.c, do_while_independent_of_p, 228, OnUIThread:false, [] @@ -72,7 +65,7 @@ codetoanalyze/c/performance/loops.c, larger_state_FN, 1005, OnUIThread:false, [ codetoanalyze/c/performance/loops.c, loop_use_global_vars, 4 + 4 ⋅ x + 2 ⋅ (1+max(0, x)), OnUIThread:false, [{1+max(0, x)},Loop at line 69, column 3,{x},Loop at line 69, column 3] codetoanalyze/c/performance/loops.c, ptr_cmp, 5 + 5 ⋅ size + 2 ⋅ (2+max(-1, size)), OnUIThread:false, [{2+max(-1, size)},Loop at line 76, column 3,{size},Loop at line 76, column 3] codetoanalyze/c/performance/purity.c, loop, 7007, OnUIThread:false, [] -codetoanalyze/c/performance/switch_continue.c, test_switch, 601, OnUIThread:false, [] +codetoanalyze/c/performance/switch_continue.c, test_switch_FN, 601, OnUIThread:false, [] codetoanalyze/c/performance/switch_continue.c, unroll_loop, 16 + (n - 1) + 11 ⋅ (max(1, n)), OnUIThread:false, [{max(1, n)},Loop at line 43, column 11,{n - 1},Loop at line 43, column 11] codetoanalyze/c/performance/two_loops_symbolic.c, nop, 2, OnUIThread:false, [] codetoanalyze/c/performance/two_loops_symbolic.c, two_loops_symb, 8 + 5 ⋅ m + 5 ⋅ m + 2 ⋅ (1+max(0, m)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 16, column 3,{1+max(0, m)},Loop at line 13, column 3,{m},Loop at line 16, column 3,{m},Loop at line 13, column 3] diff --git a/infer/tests/codetoanalyze/c/performance/cost_test.c b/infer/tests/codetoanalyze/c/performance/cost_test.c index c8c16ca97..07a03f6c4 100644 --- a/infer/tests/codetoanalyze/c/performance/cost_test.c +++ b/infer/tests/codetoanalyze/c/performance/cost_test.c @@ -6,116 +6,60 @@ */ #include -// Cost: 5 -int foo_OK() { +int foo_constant() { int i, j; i = 17; j = 31; - return i + j + 3 + 7; } -// Cost: 17 -int bar_OK() { - - int j = 0; - - j++; - j++; - j++; - j = foo_OK(); - j++; - - return j; -} - -// Cost: 25 -int cond_OK(int i) { +int cond_constant(int i) { int x; if (i < 0) { - x = bar_OK(); + x = foo_constant(); } else { x = 1; } return x; } -// Cost: 5 -void alias_OK() { - - int i, j; - - j = i; - i = ++i; -} - -// Cost: 6 -void alias2_OK() { - - int i, j, z; - - j = 1; - z = 2; - - j = i; - i = z; -} - -// Cost: 1004 -int loop0_bad() { +int loop0_constant() { for (int i = 0; i < 100; i++) { - alias2_OK(); + foo_constant(); } return 0; } -// Cost: 1006 -int loop1_bad() { +int loop1_constant() { int k = 100; for (int i = 0; i < k; i++) { - alias2_OK(); + foo_constant(); } return 0; } -// Expected: Theta(k) -int loop2_bad(int k) { +// Expected: O(k) +int loop2_linear(int k) { for (int i = 0; i < k; i++) { - alias2_OK(); + alias2(); } return 0; } -// Expected: ~15 -int loop3_bad(int k) { +int loop3_constant(int k) { for (int i = k; i < k + 15; i++) { - alias2_OK(); + alias2(); } return 0; } -// Cost: 218 -// Shows that calling many times non expensive function can -// result in an expensive computation -int main_bad() { - - int k1, k2, k3, k4; - - cond_OK(2); - k1 = bar_OK() + foo_OK() + cond_OK(15) * 2; - k2 = bar_OK() + foo_OK() + cond_OK(17) * 3; - k3 = bar_OK() + foo_OK() + cond_OK(11) * 3; - k4 = bar_OK() + foo_OK() + cond_OK(19) * 3; - return 0; -} - -// Expected: Theta(20-m) -int while_upto20_bad(int m) { +// Expected: O(20-m) +int while_upto20(int m) { while (m < 20) { int l = 0; m++; @@ -123,11 +67,11 @@ int while_upto20_bad(int m) { return m; } -void call_while_upto20_minus100_bad() { while_upto20_bad(-100); } +void call_while_upto20_minus100_constant() { while_upto20(-100); } -void call_while_upto20_10_good() { while_upto20_bad(10); } +void call_while_upto20_10_constant() { while_upto20(10); } -void call_while_upto20_unsigned_good(unsigned x) { while_upto20_bad(x); } +void call_while_upto20_unsigned(unsigned x) { while_upto20(x); } // Cost: 1 void unit_cost_function() {} @@ -150,8 +94,7 @@ void infinite() { void call_infinite() { infinite(); } -// Cost should not include the symbol of c. -void ignore_character_symbols_constant_FP(char c) { +void loop_character_symbols_linear(char c) { for (; c < 'z';) { if (rand()) { c = 'a'; diff --git a/infer/tests/codetoanalyze/c/performance/cost_test_deps.c b/infer/tests/codetoanalyze/c/performance/cost_test_deps.c index adca991f7..22d666bf7 100644 --- a/infer/tests/codetoanalyze/c/performance/cost_test_deps.c +++ b/infer/tests/codetoanalyze/c/performance/cost_test_deps.c @@ -37,7 +37,7 @@ int loop_no_dep2(int k) { // This example works now because even though j in [-oo.+oo], // since control vars={k} (notice that we will remove {p,j} in the else branch), // we ignore j and find the right bound for the inner loop -int if_bad(int j) { +int if_bad_constant(int j) { int p = 10; if (p < 10 + j) { p++; @@ -54,7 +54,7 @@ int if_bad(int j) { // loop that depends on them. E.g.: below we still depend on {j} but in the // conditional prune statement, we will remove the temp. var that map to inner // {j}, not the outer {j} -int if_bad_loop() { +int if_bad_loop_constant() { int p = 10; for (int j = 0; j < 5; j++) { if (j < 2) { @@ -87,7 +87,7 @@ int two_loops() { // We don't get a false dependency to m (hence p) since // for if statements, we don't add prune variables as dependency -int loop_despite_inferbo(int p) { +int loop_despite_inferbo_constant(int p) { int k = 100; for (int i = 0; i < k; i++) { @@ -99,8 +99,7 @@ int loop_despite_inferbo(int p) { return p; } -/* Expected: 5 * 100 */ -int nested_loop() { +int nested_loop_constant() { int k = 0; for (int i = 0; i < 5; i++) { A: @@ -114,7 +113,7 @@ int nested_loop() { // Unlike the above program, B will be inside the inner loop, hence executed // around 105 times -int simulated_nested_loop(int p) { +int simulated_nested_loop_constant(int p) { int k = 0; int t = 5; int j = 0; @@ -129,7 +128,7 @@ int simulated_nested_loop(int p) { } // B will be inside the inner loop and executed ~500 times -int simulated_nested_loop_more_expensive(int p) { +int simulated_nested_loop_more_expensive_constant(int p) { int k = 0; int t = 5; int j = 0; @@ -146,7 +145,7 @@ int simulated_nested_loop_more_expensive(int p) { return k; } -int real_while() { +int real_while_constant() { int i = 0; int j = 3 * i; while (i < 30) { @@ -160,7 +159,7 @@ int real_while() { /* The following program is the version of real_while() with gotos */ -int simulated_while() { +int simulated_while_constant() { int i = 0; int j = 3 * i; LOOP_COND: @@ -179,7 +178,7 @@ RETURN: /* Conditional inside goto loop */ /* Expected: 5 * 100 */ -int simulated_nested_loop_cond_in_goto(int p) { +int simulated_nested_loop_cond_in_goto_constant(int p) { int k = 0; int t = 5; int j = 0; diff --git a/infer/tests/codetoanalyze/c/performance/exit.c b/infer/tests/codetoanalyze/c/performance/exit.c index dbe309ff2..a7df75c44 100644 --- a/infer/tests/codetoanalyze/c/performance/exit.c +++ b/infer/tests/codetoanalyze/c/performance/exit.c @@ -4,12 +4,12 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -void exit_unreachable_unreachable() { +void exit_unreachable() { exit(0); // modeled as unreachable } // constraint solver resolves all nodes to unreachable cost -void compute_exit_unreachable_unreachable() { +void compute_exit_unreachable() { int k = 0; exit(0); } @@ -19,7 +19,7 @@ void linear(int p) { } } -void call_exit_unreachable_unreachable(int p) { +void call_exit_unreachable(int p) { linear(p); exit(0); } diff --git a/infer/tests/codetoanalyze/c/performance/instantiate.c b/infer/tests/codetoanalyze/c/performance/instantiate.c index 3ad7e1b80..a847037cc 100644 --- a/infer/tests/codetoanalyze/c/performance/instantiate.c +++ b/infer/tests/codetoanalyze/c/performance/instantiate.c @@ -7,27 +7,24 @@ void no_op() { int x = 0; } -// Expected: Theta(n) +// Expected: O(n) void do_n_times(int n) { for (int i = 0; i < n; i++) { no_op(); } } -void do_2_times_Good() { do_n_times(2); } +void do_2_times_constant() { do_n_times(2); } -// Expected: ~2000 -void do_2K_times_Bad() { do_n_times(2000); } - -// Expected: Theta(m^2) -void do_m2_times(int m) { +// Expected: O(m^2) +void do_m2_times_quadratic(int m) { for (int i = 0; i < m; i++) { do_n_times(m); } } -// Expected: Theta(m^2) -void do_half_m2_times(int m) { +// Expected: O(m^2) +void do_half_m2_times_quadratic(int m) { for (int i = 0; i < m; i++) { do_n_times(i); } diff --git a/infer/tests/codetoanalyze/c/performance/invariant.c b/infer/tests/codetoanalyze/c/performance/invariant.c index 93765ab4a..7e520df2c 100644 --- a/infer/tests/codetoanalyze/c/performance/invariant.c +++ b/infer/tests/codetoanalyze/c/performance/invariant.c @@ -17,23 +17,7 @@ int while_unique_def_FN(int p) { return 0; } -/* Theta(n) */ -void do_k_times(int n) { - int k = n; - for (int i = 0; i < k; i++) { - } -} - -/* Theta(n) */ -void do_k_times_array(int n) { - int k = n; - int a[10]; - for (int i = 0; i < k; i++) { - a[k] = 4 + k; - } -} - -/* Expected Theta(n * m) */ +/* Expected O(n * m) */ void do_n_m_times_nested(int n, int m) { int k = n; int p = m; @@ -43,7 +27,7 @@ void do_n_m_times_nested(int n, int m) { } } -/* Expected ~ 3 * p. Also inner loop will have t as invariant */ +/* Expected:O(p). Also inner loop will have t as invariant */ void two_loops_nested_invariant(int p) { int t = 0; int m = p; diff --git a/infer/tests/codetoanalyze/c/performance/issues.exp b/infer/tests/codetoanalyze/c/performance/issues.exp index 26b1fa8c0..4ad078ad9 100644 --- a/infer/tests/codetoanalyze/c/performance/issues.exp +++ b/infer/tests/codetoanalyze/c/performance/issues.exp @@ -1,33 +1,32 @@ codetoanalyze/c/performance/compound_loop_guard.c, compound_while, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, simplified_simulated_while_with_and, 5, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and, 7, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and, 8, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_shortcut, 5, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_with_and, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] -codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 63, column 3] +codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or_constant, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or_constant, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, nested_while_and_or_constant, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, simplified_simulated_while_with_and_constant, 5, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and_constant, 7, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_nested_loop_with_and_constant, 8, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_shortcut_constant, 5, CONDITION_ALWAYS_FALSE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, simulated_while_with_and_linear, 4, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 62, column 3] codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, 2, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/c/performance/compound_loop_guard.c, while_and_or, 3, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/c/performance/cost_test.c, alias_OK, 5, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [Binary operation: ([-oo, +oo] + 1):signed32] -codetoanalyze/c/performance/cost_test.c, call_infinite, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to infinite,Unbounded loop,Loop at line 146, column 3] -codetoanalyze/c/performance/cost_test.c, infinite, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 146, column 3] +codetoanalyze/c/performance/cost_test.c, call_infinite, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Call to infinite,Unbounded loop,Loop at line 90, column 3] +codetoanalyze/c/performance/cost_test.c, infinite, 0, INFINITE_EXECUTION_TIME, no_bucket, ERROR, [Unbounded loop,Loop at line 90, column 3] codetoanalyze/c/performance/cost_test.c, infinite, 3, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,,Assignment,Binary operation: ([-oo, +oo] + [0, +oo]):signed32] codetoanalyze/c/performance/cost_test.c, infinite_FN, 3, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,,Assignment,Binary operation: ([-oo, +oo] + [0, +oo]):signed32] -codetoanalyze/c/performance/cost_test_deps.c, if_bad_loop, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/c/performance/cost_test_deps.c, if_bad_loop_constant, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/c/performance/cost_test_deps.c, loop_no_dep1, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/c/performance/cost_test_deps.c, loop_no_dep2, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/c/performance/cost_test_deps.c, real_while, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Assignment,,Assignment,Binary operation: ([0, +oo] + [0, 29]):signed32] -codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop, 7, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/c/performance/cost_test_deps.c, simulated_while, 10, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Assignment,,Assignment,Binary operation: ([0, +oo] + [0, 29]):signed32] +codetoanalyze/c/performance/cost_test_deps.c, real_while_constant, 4, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Assignment,,Assignment,Binary operation: ([0, +oo] + [0, 29]):signed32] +codetoanalyze/c/performance/cost_test_deps.c, simulated_nested_loop_constant, 7, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] +codetoanalyze/c/performance/cost_test_deps.c, simulated_while_constant, 10, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Assignment,,Assignment,Binary operation: ([0, +oo] + [0, 29]):signed32] codetoanalyze/c/performance/cost_test_deps.c, two_loops, 5, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([3, +oo] + 1):signed32] -codetoanalyze/c/performance/exit.c, call_exit_unreachable_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] -codetoanalyze/c/performance/exit.c, compute_exit_unreachable_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] -codetoanalyze/c/performance/exit.c, exit_unreachable_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] +codetoanalyze/c/performance/exit.c, call_exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] +codetoanalyze/c/performance/exit.c, compute_exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] +codetoanalyze/c/performance/exit.c, exit_unreachable, 0, EXECUTION_TIME_UNREACHABLE_AT_EXIT, no_bucket, ERROR, [Unreachable node] codetoanalyze/c/performance/invariant.c, while_infinite_FN, 2, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/c/performance/loops.c, if_in_loop, 5, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] -codetoanalyze/c/performance/switch_continue.c, test_switch, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] +codetoanalyze/c/performance/switch_continue.c, test_switch_FN, 3, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] codetoanalyze/c/performance/switch_continue.c, unroll_loop, 6, INTEGER_OVERFLOW_L5, no_bucket, ERROR, [,Assignment,Binary operation: ([0, +oo] + 1):signed32] codetoanalyze/c/performance/switch_continue.c, unroll_loop, 9, CONDITION_ALWAYS_TRUE, no_bucket, WARNING, [Here] diff --git a/infer/tests/codetoanalyze/c/performance/jump_inside_loop.c b/infer/tests/codetoanalyze/c/performance/jump_inside_loop.c index fad9e4cce..8004e9602 100644 --- a/infer/tests/codetoanalyze/c/performance/jump_inside_loop.c +++ b/infer/tests/codetoanalyze/c/performance/jump_inside_loop.c @@ -12,27 +12,27 @@ back-edge to the Loop label, and we are able to detect two exit-edges correctly. */ -int loop_always(int p) { +int loop_always_linear(int p, int k) { int i = 0; if (p > 0) { goto Loop; } - while (i < 5) { + while (i < k) { Loop: i++; } return 1; } -int jump_inside_loop(int p) { +int jump_inside_loop_constant_linear(int p, int k) { int i = 0; if (p > 0) { goto Loop; } else { return p; } - while (i < 500) { + while (i < k) { Loop: i++; } diff --git a/infer/tests/codetoanalyze/c/performance/switch_continue.c b/infer/tests/codetoanalyze/c/performance/switch_continue.c index 002dc71d9..5a3f832cd 100644 --- a/infer/tests/codetoanalyze/c/performance/switch_continue.c +++ b/infer/tests/codetoanalyze/c/performance/switch_continue.c @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -int test_switch() { +int test_switch_FN() { int value = 0; // infinite loop while (value < 100) { diff --git a/infer/tests/codetoanalyze/c/performance/two_loops_symbolic.c b/infer/tests/codetoanalyze/c/performance/two_loops_symbolic.c index 81f42f5bb..77bda2383 100644 --- a/infer/tests/codetoanalyze/c/performance/two_loops_symbolic.c +++ b/infer/tests/codetoanalyze/c/performance/two_loops_symbolic.c @@ -6,7 +6,7 @@ */ void nop() { int k = 0; } -// Expected: Theta(m) +// Expected: O(m) int two_loops_symb(int m) { int p = 10; @@ -19,7 +19,7 @@ int two_loops_symb(int m) { return p; } -// Expected: Theta(m + k) +// Expected: O(m + k) int two_loops_symb_diff(int m, int k) { int p = 10; for (int i = 0; i < m; i++) {