llvm.org GIT mirror llvm / 7867927
Fold "icmp pred (srem X, Y), Y" like we do for urem. Handle signed comparisons in the urem case, though not the other way around. This is enough to get #3 from PR9343! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126991 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
3 changed file(s) with 63 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
16701670 }
16711671 }
16721672
1673 if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
1673 Value *V;
1674 if (LBO && match(LBO, m_URem(m_Value(V), m_Specific(RHS)))) {
1675 bool KnownNonNegative, KnownNegative;
1676 switch (Pred) {
1677 default:
1678 break;
1679 case ICmpInst::ICMP_SGT:
1680 case ICmpInst::ICMP_SGE:
1681 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD);
1682 if (!KnownNonNegative)
1683 break;
1684 // fall-through
1685 case ICmpInst::ICMP_EQ:
1686 case ICmpInst::ICMP_UGT:
1687 case ICmpInst::ICMP_UGE:
1688 return ConstantInt::getFalse(RHS->getContext());
1689 case ICmpInst::ICMP_SLT:
1690 case ICmpInst::ICMP_SLE:
1691 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD);
1692 if (!KnownNonNegative)
1693 break;
1694 // fall-through
1695 case ICmpInst::ICMP_NE:
1696 case ICmpInst::ICMP_ULT:
1697 case ICmpInst::ICMP_ULE:
1698 return ConstantInt::getTrue(RHS->getContext());
1699 }
1700 }
1701
1702 if (LBO && match(LBO, m_SRem(m_Value(), m_Specific(RHS)))) {
16741703 switch (Pred) {
16751704 default:
16761705 break;
16771706 case ICmpInst::ICMP_EQ:
1678 case ICmpInst::ICMP_UGT:
1679 case ICmpInst::ICMP_UGE:
1707 case ICmpInst::ICMP_SGT:
1708 case ICmpInst::ICMP_SGE:
16801709 return ConstantInt::getFalse(RHS->getContext());
16811710 case ICmpInst::ICMP_NE:
1682 case ICmpInst::ICMP_ULT:
1683 case ICmpInst::ICMP_ULE:
1711 case ICmpInst::ICMP_SLT:
1712 case ICmpInst::ICMP_SLE:
16841713 return ConstantInt::getTrue(RHS->getContext());
16851714 }
16861715 }
378378 }
379379
380380 ; PR9343 #1
381 ; CHECK: test39
381 ; CHECK: @test39
382382 ; CHECK %B = icmp eq i32 %X, 0
383383 define i1 @test39(i32 %X, i32 %Y) {
384384 %A = ashr exact i32 %X, %Y
386386 ret i1 %B
387387 }
388388
389 ; CHECK: test40
389 ; CHECK: @test40
390390 ; CHECK: %B = icmp ne i32 %X, 0
391391 define i1 @test40(i32 %X, i32 %Y) {
392392 %A = lshr exact i32 %X, %Y
393393 %B = icmp ne i32 %A, 0
394394 ret i1 %B
395395 }
396
397 ; PR9343 #3
398 ; CHECK: @test41
399 ; CHECK: ret i1 true
400 define i1 @test41(i32 %X, i32 %Y) {
401 %A = urem i32 %X, %Y
402 %B = icmp ugt i32 %Y, %A
403 ret i1 %B
404 }
235235 ; CHECK: ret i1 %B
236236 }
237237
238 define i1 @urem5(i16 %X, i32 %Y) {
239 ; CHECK: @urem5
240 %A = zext i16 %X to i32
241 %B = urem i32 %A, %Y
242 %C = icmp slt i32 %B, %Y
243 ret i1 %C
244 ; CHECK: ret i1 true
245 }
246
238247 define i1 @srem1(i32 %X) {
239248 ; CHECK: @srem1
240249 %A = srem i32 %X, -5
243252 ; CHECK: ret i1 false
244253 }
245254
255 define i1 @srem2(i32 %X, i32 %Y) {
256 ; CHECK: @srem2
257 %neg = sub i32 %Y, 0
258 %A = srem i32 %X, %Y
259 %B = icmp slt i32 %A, %neg
260 ret i1 %B
261 ; CHECK: ret i1 true
262 }
263
246264 define i1 @udiv1(i32 %X) {
247265 ; CHECK: @udiv1
248266 %A = udiv i32 %X, 1000000