forked from OSchip/llvm-project
teach instsimplify to transform (X / Y) * Y to X
when the div is an exact udiv. llvm-svn: 124994
This commit is contained in:
parent
9c70414551
commit
6e57b15228
|
@ -735,9 +735,11 @@ static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
|
||||||
// (X / Y) * Y -> X if the division is exact.
|
// (X / Y) * Y -> X if the division is exact.
|
||||||
Value *X = 0, *Y = 0;
|
Value *X = 0, *Y = 0;
|
||||||
if ((match(Op0, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
|
if ((match(Op0, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
|
||||||
(match(Op1, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y)
|
(match(Op0, m_UDiv(m_Value(X), m_Value(Y))) && Y == Op1) ||
|
||||||
BinaryOperator *SDiv = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1);
|
(match(Op1, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op0) || // Y * (X / Y)
|
||||||
if (SDiv->isExact())
|
(match(Op1, m_UDiv(m_Value(X), m_Value(Y))) && Y == Op0)) {
|
||||||
|
BinaryOperator *Div = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1);
|
||||||
|
if (Div->isExact())
|
||||||
return X;
|
return X;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -137,6 +137,7 @@ define i32 @sdiv5(i32 %x, i32 %y) {
|
||||||
; CHECK: ret i32 %x
|
; CHECK: ret i32 %x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
define i32 @udiv1(i32 %x, i32 %y) {
|
define i32 @udiv1(i32 %x, i32 %y) {
|
||||||
; CHECK: @udiv1
|
; CHECK: @udiv1
|
||||||
; (no overflow X * Y) / Y -> X
|
; (no overflow X * Y) / Y -> X
|
||||||
|
@ -164,3 +165,22 @@ define i32 @udiv3(i32 %x, i32 %y) {
|
||||||
ret i32 %div
|
ret i32 %div
|
||||||
; CHECK: ret i32 0
|
; CHECK: ret i32 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define i32 @udiv4(i32 %x, i32 %y) {
|
||||||
|
; CHECK: @udiv4
|
||||||
|
; (X / Y) * Y -> X if the division is exact
|
||||||
|
%div = udiv exact i32 %x, %y
|
||||||
|
%mul = mul i32 %div, %y
|
||||||
|
ret i32 %mul
|
||||||
|
; CHECK: ret i32 %x
|
||||||
|
}
|
||||||
|
|
||||||
|
define i32 @udiv5(i32 %x, i32 %y) {
|
||||||
|
; CHECK: @udiv5
|
||||||
|
; Y * (X / Y) -> X if the division is exact
|
||||||
|
%div = udiv exact i32 %x, %y
|
||||||
|
%mul = mul i32 %y, %div
|
||||||
|
ret i32 %mul
|
||||||
|
; CHECK: ret i32 %x
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue