llvm.org GIT mirror llvm / 359e6a4
Merging r325148: ------------------------------------------------------------------------ r325148 | ctopper | 2018-02-14 19:08:33 +0100 (Wed, 14 Feb 2018) | 7 lines [InstCombine] Don't fold select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W)) if the binop is rem or div. The select may have been preventing a division by zero or INT_MIN/-1 so removing it might not be safe. Fixes PR36362. Differential Revision: https://reviews.llvm.org/D43276 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@325501 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 1 year, 6 months ago
2 changed file(s) with 34 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
16421642 }
16431643 }
16441644
1645 auto canMergeSelectThroughBinop = [](BinaryOperator *BO) {
1646 // The select might be preventing a division by 0.
1647 switch (BO->getOpcode()) {
1648 default:
1649 return true;
1650 case Instruction::SRem:
1651 case Instruction::URem:
1652 case Instruction::SDiv:
1653 case Instruction::UDiv:
1654 return false;
1655 }
1656 };
1657
16451658 // Try to simplify a binop sandwiched between 2 selects with the same
16461659 // condition.
16471660 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
16481661 BinaryOperator *TrueBO;
1649 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO)))) {
1662 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) &&
1663 canMergeSelectThroughBinop(TrueBO)) {
16501664 if (auto *TrueBOSI = dyn_cast(TrueBO->getOperand(0))) {
16511665 if (TrueBOSI->getCondition() == CondVal) {
16521666 TrueBO->setOperand(0, TrueBOSI->getTrueValue());
16651679
16661680 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
16671681 BinaryOperator *FalseBO;
1668 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO)))) {
1682 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) &&
1683 canMergeSelectThroughBinop(FalseBO)) {
16691684 if (auto *FalseBOSI = dyn_cast(FalseBO->getOperand(0))) {
16701685 if (FalseBOSI->getCondition() == CondVal) {
16711686 FalseBO->setOperand(0, FalseBOSI->getFalseValue());
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ;RUN: opt -instcombine -S %s | FileCheck %s
2
3 ; We shouldn't remove the select before the srem
4 define i32 @foo(i1 %a, i32 %b, i32 %c) {
5 ; CHECK-LABEL: @foo(
6 ; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[A:%.*]], i32 [[B:%.*]], i32 -1
7 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[C:%.*]], [[SEL1]]
8 ; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[A]], i32 [[REM]], i32 0
9 ; CHECK-NEXT: ret i32 [[SEL2]]
10 ;
11 %sel1 = select i1 %a, i32 %b, i32 -1
12 %rem = srem i32 %c, %sel1
13 %sel2 = select i1 %a, i32 %rem, i32 0
14 ret i32 %sel2
15 }
16