Skip to content

Commit

Permalink
Allow non-zero lhist min value (#617)
Browse files Browse the repository at this point in the history
Signed division isn't allowed when using bpf as a target[1]. When `min = 0`
the llvm can apparently figure out that signed division isn't required
and it rewrites it to unsigned. However when `min > 0` the  llvm seems
to account for `value - min < 0` and uses signed division.
As `value < min` has already been checked earlier we can be sure that
`value - min >= 0` and always use unsigned division.

[1]: llvm-mirror/llvm@2462cfb
  • Loading branch information
fbs authored and danobi committed May 11, 2019
1 parent 0e2ce5f commit 51fdb6a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ast/codegen_llvm.cpp
Expand Up @@ -1679,12 +1679,12 @@ void CodegenLLVM::createLinearFunction()
b_.CreateCondBr(cmp1, gt_max, le_max);

b_.SetInsertPoint(gt_max);
Value *div = b_.CreateSDiv(b_.CreateSub(b_.CreateLoad(max_alloc), b_.CreateLoad(min_alloc)), b_.CreateLoad(step_alloc));
Value *div = b_.CreateUDiv(b_.CreateSub(b_.CreateLoad(max_alloc), b_.CreateLoad(min_alloc)), b_.CreateLoad(step_alloc));
b_.CreateStore(b_.CreateAdd(div, b_.getInt64(1)), result_alloc);
b_.CreateRet(b_.CreateLoad(result_alloc));

b_.SetInsertPoint(le_max);
Value *div3 = b_.CreateSDiv(b_.CreateSub(b_.CreateLoad(value_alloc), b_.CreateLoad(min_alloc)), b_.CreateLoad(step_alloc));
Value *div3 = b_.CreateUDiv(b_.CreateSub(b_.CreateLoad(value_alloc), b_.CreateLoad(min_alloc)), b_.CreateLoad(step_alloc));
b_.CreateStore(b_.CreateAdd(div3, b_.getInt64(1)), result_alloc);
b_.CreateRet(b_.CreateLoad(result_alloc));
}
Expand Down

0 comments on commit 51fdb6a

Please sign in to comment.