Skip to content

Commit c3b9c36

Browse files
authored
[SCCP] Propagate exact flags (#72432)
This patch propagates exact flags for `ashr->lshr` and `sdiv->udiv` in SCCP. This missed optimization is discovered with the help of AliveToolkit/alive2#962.
1 parent 42a4d5e commit c3b9c36

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static bool replaceSignedInst(SCCPSolver &Solver,
187187
if (InsertedValues.count(Op0) || !isNonNegative(Op0))
188188
return false;
189189
NewInst = BinaryOperator::CreateLShr(Op0, Inst.getOperand(1), "", &Inst);
190+
NewInst->setIsExact(Inst.isExact());
190191
break;
191192
}
192193
case Instruction::SDiv:
@@ -199,6 +200,8 @@ static bool replaceSignedInst(SCCPSolver &Solver,
199200
auto NewOpcode = Inst.getOpcode() == Instruction::SDiv ? Instruction::UDiv
200201
: Instruction::URem;
201202
NewInst = BinaryOperator::Create(NewOpcode, Op0, Op1, "", &Inst);
203+
if (Inst.getOpcode() == Instruction::SDiv)
204+
NewInst->setIsExact(Inst.isExact());
202205
break;
203206
}
204207
default:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt -passes=sccp < %s -S | FileCheck %s
3+
4+
define i8 @ashr_to_lshr(i8 %x, i8 %y) {
5+
; CHECK-LABEL: define i8 @ashr_to_lshr(
6+
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
7+
; CHECK-NEXT: [[P:%.*]] = and i8 [[X]], 127
8+
; CHECK-NEXT: [[R:%.*]] = lshr exact i8 [[P]], [[Y]]
9+
; CHECK-NEXT: ret i8 [[R]]
10+
;
11+
%p = and i8 %x, 127
12+
%r = ashr exact i8 %p, %y
13+
ret i8 %r
14+
}
15+
16+
define i8 @sdiv_to_udiv(i8 %x, i8 %y) {
17+
; CHECK-LABEL: define i8 @sdiv_to_udiv(
18+
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
19+
; CHECK-NEXT: [[X1:%.*]] = and i8 [[X]], 127
20+
; CHECK-NEXT: [[Y1:%.*]] = and i8 [[Y]], 127
21+
; CHECK-NEXT: [[R:%.*]] = udiv exact i8 [[X1]], [[Y1]]
22+
; CHECK-NEXT: ret i8 [[R]]
23+
;
24+
%x1 = and i8 %x, 127
25+
%y1 = and i8 %y, 127
26+
%r = sdiv exact i8 %x1, %y1
27+
ret i8 %r
28+
}

0 commit comments

Comments
 (0)