llvm.org GIT mirror llvm / 1bdf5dc
Optimize exact sdiv by a constant power of 2 to ashr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78714 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 10 years ago
2 changed file(s) with 24 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
30633063 // sdiv X, -1 == -X
30643064 if (RHS->isAllOnesValue())
30653065 return BinaryOperator::CreateNeg(*Context, Op0);
3066
3067 // sdiv X, C --> ashr X, log2(C)
3068 if (cast(&I)->isExact() &&
3069 RHS->getValue().isNonNegative() &&
3070 RHS->getValue().isPowerOf2()) {
3071 Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
3072 RHS->getValue().exactLogBase2());
3073 return BinaryOperator::CreateAShr(Op0, ShAmt, I.getName());
3074 }
30663075 }
30673076
30683077 // If the sign bits of both operands are zero (i.e. we can prove they are
0 ; RUN: llvm-as < %s | opt -instcombine | llvm-dis | FileCheck %s
1
2 ; CHECK: define i32 @foo
3 ; CHECK: sdiv i32 %x, 8
4 define i32 @foo(i32 %x) {
5 %y = sdiv i32 %x, 8
6 ret i32 %y
7 }
8
9 ; CHECK: define i32 @bar
10 ; CHECK: ashr i32 %x, 3
11 define i32 @bar(i32 %x) {
12 %y = sdiv exact i32 %x, 8
13 ret i32 %y
14 }