Index: clang/test/CodeGen/pr30188.c =================================================================== --- /dev/null +++ clang/test/CodeGen/pr30188.c @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 -O2 -emit-llvm -o - %s | FileCheck %s + +// Check that optimizers are able to optimize the number of loads in the loop. +// CHECK: load +// CHECK: load +// CHECK: load +// CHECK-NOT: load + +struct b { + int x_; +}; + +struct a { + int l_; + struct b *data_; +}; + +int BinarySearch(struct a *input, struct b t) { + if (input->l_ > 0) { + int low = 0; + int high = input->l_; + while (high != low + 1) { + int mid = (high + low) / 2; + if (input->data_[mid].x_ > t.x_) + high = mid; + else + low = mid; + } + return low; + } + return -1; +} Index: clang/test/CodeGenCXX/pr30188.cpp =================================================================== --- /dev/null +++ clang/test/CodeGenCXX/pr30188.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -O2 -emit-llvm -o - %s | FileCheck %s + +// Check that optimizers are able to optimize the number of loads in the loop. +// CHECK: load +// CHECK-NOT: load + +int test(float exp, float *array) { + int hi = 255; + int lo = 0; + int offset = 0; + + while(hi > lo) { + unsigned delta = (hi - lo) / 2u; + delta = 1u > delta ? 1u : delta; + offset = lo + delta; + + if (array[offset] > exp) + hi = hi - delta; + else + lo = lo + delta; + } + + return offset; +}