Skip to content

Commit

Permalink
[cost] import c performance test to objc
Browse files Browse the repository at this point in the history
Summary: Basically copying test file from c/performance to objc/performance.

Reviewed By: skcho

Differential Revision: D21972945

fbshipit-source-id: 6e4412598
  • Loading branch information
roro47 authored and facebook-github-bot committed Jun 12, 2020
1 parent 61d5fde commit 525bd35
Show file tree
Hide file tree
Showing 14 changed files with 814 additions and 0 deletions.
36 changes: 36 additions & 0 deletions infer/tests/codetoanalyze/objc/performance/break.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* t is also in control variables but once we have invariant analysis, it
* shouldn't be */
int break_loop(int p, int t) {
for (int i = 0; i < p; i++) {
// do something
if (t < 0)
break;
// do something
}
return 0;
}

/* t will be in control variables but once we have invariant analysis,
* it shouldn't be. */
int break_loop_with_t(int p, int t) {
for (int i = 0; i < p; i++) {
// do something
if (t < 0) {
t++;
break;
}
// do something
}
return 0;
}

/* calling break_loop with a negative t should give constant
cost. Currently, this doesn't work since we can't do case analysis
on the domain. */
int break_constant_FP(int p) { return break_loop(p, -1); }
95 changes: 95 additions & 0 deletions infer/tests/codetoanalyze/objc/performance/compound_loop_guard.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/* while loop that contains && in the guard. It gives the correct bound.
* Expected: O(m) */
int compound_while(int m) {
int i = 0;
int j = 3 * i;
while (j == 0 && i < m) {
i++;
}
return j;
}

int simplified_simulated_while_with_and_constant(int p) {
int k = 0;
int j = 0;
B:
j++;
if (k == 0 && j < 100) {
goto B; // continue;
}
return k;
}

/* simulated goto that contains && */
int simulated_while_with_and_linear(int p) {
int i = 0;
int k = 0;
LOOP_COND:
if (k == 0 && i < p) { // k == 0 always true
goto INCR;
} else {
goto RETURN;
}
INCR:
i++;
goto LOOP_COND;
RETURN:
return i;
}

/* shortcut in the conditional, hence we won't loop, and get constant cost */
int simulated_while_shortcut_constant(int p) {
int k = 0;
int j = 0;
B:
j++;
if (k == 1 && j < 100) {
goto B; // continue;
}
return k;
}

/* 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)) {
i++;
}
}

// should be constant cost
int nested_while_and_or_constant(int p) {
int i = 0;
int j = 3 * i;
while (p == 1 || (i < 30 && i >= 0)) {
while (p == 1 || (j < 5 && j >= 0)) {

return j;
}
i++;
}
return j;
}

/* j and i will be control variables for B */
int simulated_nested_loop_with_and_constant(int p) {
int k = 0;
int t = 5;
int j = 0;
for (int i = 0; i < 5; i++) {
B:
t = 3;
j++;
if (k == 0 && j < 100) { // k == 0 always true
goto B; // continue;
}
}
return k;
}
66 changes: 66 additions & 0 deletions infer/tests/codetoanalyze/objc/performance/cost-issues.exp
Original file line number Diff line number Diff line change
@@ -1,9 +1,75 @@
codetoanalyze/objc/performance/araii.m, Araii.dealloc, 4, OnUIThread:false, []
codetoanalyze/objc/performance/araii.m, Araii.initWithBuffer, 15, OnUIThread:false, []
codetoanalyze/objc/performance/araii.m, memory_leak_raii_main, 18, OnUIThread:false, []
codetoanalyze/objc/performance/break.m, break_constant_FP, 8 + 5 ⋅ p + 2 ⋅ (1+max(0, p)), OnUIThread:false, [{1+max(0, p)},call to break_loop,Loop at line 10, column 3,{p},call to break_loop,Loop at line 10, column 3]
codetoanalyze/objc/performance/break.m, 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/objc/performance/break.m, 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/objc/performance/cf.m, array_count_linear, 6 + 3 ⋅ arr.length + 2 ⋅ (arr.length + 1), OnUIThread:false, [{arr.length + 1},Loop at line 18, column 3,{arr.length},Loop at line 18, column 3]
codetoanalyze/objc/performance/cf.m, cf_array_create_copy_linear, 1010, OnUIThread:false, []
codetoanalyze/objc/performance/cf.m, cf_array_create_linear, 11 + 3 ⋅ x + 2 ⋅ (1+max(0, x)), OnUIThread:false, [{1+max(0, x)},Loop at line 41, column 3,{x},Loop at line 41, column 3]
codetoanalyze/objc/performance/cf.m, dict_count_linear, 6 + 3 ⋅ dict.length + 2 ⋅ (dict.length + 1), OnUIThread:false, [{dict.length + 1},Loop at line 24, column 3,{dict.length},Loop at line 24, column 3]
codetoanalyze/objc/performance/compound_loop_guard.m, 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/objc/performance/compound_loop_guard.m, nested_while_and_or_constant, 20, OnUIThread:false, []
codetoanalyze/objc/performance/compound_loop_guard.m, simplified_simulated_while_with_and_constant, 605, OnUIThread:false, []
codetoanalyze/objc/performance/compound_loop_guard.m, simulated_nested_loop_with_and_constant, 3529, OnUIThread:false, []
codetoanalyze/objc/performance/compound_loop_guard.m, simulated_while_shortcut_constant, 9, OnUIThread:false, []
codetoanalyze/objc/performance/compound_loop_guard.m, 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/objc/performance/compound_loop_guard.m, while_and_or, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 62, column 3]
codetoanalyze/objc/performance/control.m, __infer_globals_initializer_gvar, 2, OnUIThread:false, []
codetoanalyze/objc/performance/control.m, wrong_cvar_FP, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 20, column 3]
codetoanalyze/objc/performance/cost_test.m, always, 9, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, call_infinite, ⊤, OnUIThread:false, [Call to infinite,Unbounded loop,Loop at line 72, column 3]
codetoanalyze/objc/performance/cost_test.m, call_while_upto20_10_constant, 56, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, call_while_upto20_minus100_constant, 606, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, 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 45, column 3,{-x + 20},call to while_upto20,Loop at line 45, column 3]
codetoanalyze/objc/performance/cost_test.m, cond_constant, 14, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, div_const, 3, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, foo_constant, 6, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, infinite, ⊤, OnUIThread:false, [Unbounded loop,Loop at line 72, column 3]
codetoanalyze/objc/performance/cost_test.m, infinite_FN, 19, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, iter_div_const_constant, 109, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, loop0_constant, 1005, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, loop3_constant, 97, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, loop_character_symbols_linear, 5 + 4 ⋅ (122-min(97, c)), OnUIThread:false, [{122-min(97, c)},Loop at line 80, column 3]
codetoanalyze/objc/performance/cost_test.m, unit_cost_function, 1, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test.m, while_upto20, 4 + 4 ⋅ (-m + 20) + (21-min(20, m)), OnUIThread:false, [{21-min(20, m)},Loop at line 45, column 3,{-m + 20},Loop at line 45, column 3]
codetoanalyze/objc/performance/cost_test_deps.m, if_bad_constant, 75, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, if_bad_loop_constant, 203, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, loop_despite_inferbo_constant, 1208, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, nested_loop_constant, 2547, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, real_while_constant, 218, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, simulated_nested_loop_cond_in_goto_constant, 3534, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, simulated_nested_loop_constant, 2529, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, simulated_nested_loop_more_expensive_constant, 2534, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, simulated_while_constant, 218, OnUIThread:false, []
codetoanalyze/objc/performance/cost_test_deps.m, two_loops, 549, OnUIThread:false, []
codetoanalyze/objc/performance/exit.m, call_exit_unreachable, ⊥, OnUIThread:false, [Unreachable node]
codetoanalyze/objc/performance/exit.m, call_unreachable_constant, 2, OnUIThread:false, []
codetoanalyze/objc/performance/exit.m, compute_exit_unreachable, ⊥, OnUIThread:false, [Unreachable node]
codetoanalyze/objc/performance/exit.m, exit_unreachable, ⊥, OnUIThread:false, [Unreachable node]
codetoanalyze/objc/performance/exit.m, 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/objc/performance/exit.m, 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/objc/performance/instantiate.m, do_2_times_constant, 20, OnUIThread:false, []
codetoanalyze/objc/performance/instantiate.m, 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/objc/performance/instantiate.m, 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/objc/performance/instantiate.m, 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/objc/performance/instantiate.m, no_op, 2, OnUIThread:false, []
codetoanalyze/objc/performance/invariant.m, 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/objc/performance/invariant.m, 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/objc/performance/invariant.m, while_infinite_FN, 2, OnUIThread:false, []
codetoanalyze/objc/performance/invariant.m, while_unique_def_FN, 15, OnUIThread:false, []
codetoanalyze/objc/performance/jump_inside_loop.m, 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/objc/performance/jump_inside_loop.m, 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/objc/performance/loops.m, __infer_globals_initializer_array1, 4, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, __infer_globals_initializer_array2, 2, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, do_while_independent_of_p, 228, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, if_in_loop, 324, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, if_out_loop, 515, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, larger_state_FN, 1005, OnUIThread:false, []
codetoanalyze/objc/performance/loops.m, 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/objc/performance/loops.m, 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/objc/performance/purity.m, loop, 7007, OnUIThread:false, []
codetoanalyze/objc/performance/switch_continue.m, test_switch_FN, 601, OnUIThread:false, []
codetoanalyze/objc/performance/switch_continue.m, 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/objc/performance/two_loops_symbolic.m, nop, 2, OnUIThread:false, []
codetoanalyze/objc/performance/two_loops_symbolic.m, two_loops_symb_diff, 8 + 5 ⋅ k + 5 ⋅ m + 2 ⋅ (1+max(0, k)) + 2 ⋅ (1+max(0, m)), OnUIThread:false, [{1+max(0, m)},Loop at line 12, column 3,{1+max(0, k)},Loop at line 15, column 3,{m},Loop at line 12, column 3,{k},Loop at line 15, column 3]
93 changes: 93 additions & 0 deletions infer/tests/codetoanalyze/objc/performance/cost_test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdint.h>

int foo_constant() {
int i, j;
i = 17;
j = 31;
return i + j + 3 + 7;
}

int cond_constant(int i) {
int x;

if (i < 0) {
x = foo_constant();
} else {
x = 1;
}
return x;
}

int loop0_constant() {

for (int i = 0; i < 100; i++) {
foo_constant();
}
return 0;
}

int loop3_constant(int k) {

for (int i = k; i < k + 15; i++) {
alias2();
}
return 0;
}

// Expected: O(20-m)
int while_upto20(int m) {
while (m < 20) {
int l = 0;
m++;
}
return m;
}

void call_while_upto20_minus100_constant() { while_upto20(-100); }

void call_while_upto20_10_constant() { while_upto20(10); }

void call_while_upto20_unsigned(unsigned x) { while_upto20(x); }

// Cost: 1
void unit_cost_function() {}

int always(int i) { return i % 2 == (i + 2) % 2; }

void infinite_FN() {
int z;
for (int i = 0; always(i); i++) {
z += i;
}
}

void infinite() {
int z;
for (int i = 0; i % 2 == (i + 2) % 2; i++) {
z += i;
}
}

void call_infinite() { infinite(); }

void loop_character_symbols_linear(char c) {
for (; c < 'z';) {
if (rand()) {
c = 'a';
}
}
}

unsigned int div_const(unsigned int n) { return n / 2; }

void iter_div_const_constant() {
unsigned int n = div_const(20);
for (int i = 0; i < n; i++) {
}
}
Loading

0 comments on commit 525bd35

Please sign in to comment.