You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
2.7 KiB

/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
class Hoist {
int foo(int x, int y) {
return x + y;
}
int bar(int x) {
return 3 * x;
}
// will only report one even though both calls are invariant, due to
// location clashing (no column information)
void clash_function_calls_hoist(int size) {
int x = 10;
int y = 5;
for (int i = 0; i < size; i++) {
foo(x, foo(y, x + y));
}
}
// will report both
void two_function_call_hoist(int size) {
int x = 10;
int y = 5;
for (int i = 0; i < size; i++) {
foo(x, bar(y));
}
}
// it is ok to move fun call to a temp. var
void reassigned_temp_hoist(int size) {
int x = 10;
int y = 5;
int d = 0;
for (int i = 0; i < size; i++) {
d = foo(x, y);
d = 30;
}
}
// it is ok to just hoist function call into a temp var.
void used_in_loop_body_before_def_temp_hoist(int size, int[] M) {
int x = 10;
int y = 5;
int d = 20;
for (int i = 0; i < size; i++) {
M[i] = d;
d = foo(x, y);
}
}
void loop_guard_hoist(int size, int[] M) {
int x = 10;
int d = 0;
int y = 5;
for (int i = 0; i < foo(d, size); i++) {}
}
void legit_hoist(int size, int[] M) {
int x = 10;
int y = 5;
int d = 0;
for (int i = 0; i < size; i++) {
d = foo(x, x + y);
M[i] = d;
}
}
// x is not invariant, hence d won't be invariant as well
void dep_not_invariant_dont_hoist(int size, int[] M) {
int x = 10;
int y = 5;
int d = 20;
for (int i = 0; i < size; i++) {
if (i > 3) {
x = 3;
}
d = foo(x, y); // shouldn't be hoisted
}
}
// foo(x) is not guaranteed to execute
void not_guaranteed_to_execute_dont_hoist(int size, int x, int y) {
int i = 0;
while (i < size) {
if (i > 3) {
foo(x, y);
}
i++;
}
}
void x_not_invariant_dont_hoist(int size, int x, int y) {
int i = 0;
while (i < size) {
x = foo(x, y) + y;
i++;
}
}
void array_store_hoist(int size, int[] M) {
int x = 10;
int y = 5;
int d = 0;
for (int i = 0; i < size; i++) {
M[i] = foo(x, x + y);
}
}
// y = ... can be taken out of the inner loop
void nested_loop_hoist(int size, int x, int y) {
int i = 0;
while (i < size) {
for (int j = 0; i < j; j++) {
y = foo(i, x);
}
i++;
}
}
// j is not invariant, y can't be taken out
void nested_loop_dont_hoist(int size, int x, int y) {
int i = 0;
while (i < size) {
for (int j = 0; j < i; j++) {
y = foo(j, x);
}
i++;
}
}
}