From 1d90e530442477de247dcb613f5176fe7e9beded Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Mon, 19 Oct 2020 14:05:29 +0100 Subject: [PATCH] [InstCombine] foldOrOfICmps - pull out repeated getOperand() calls. NFCI. --- .../Transforms/InstCombine/InstCombineAndOrXor.cpp | 33 +++++++++------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 11cfa4a..8fa2111 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2281,9 +2281,10 @@ Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, return V; ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate(); - - ConstantInt *LHSC = dyn_cast(LHS->getOperand(1)); - ConstantInt *RHSC = dyn_cast(RHS->getOperand(1)); + Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); + Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1); + auto *LHSC = dyn_cast(LHS1); + auto *RHSC = dyn_cast(RHS1); // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3) // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3) @@ -2295,19 +2296,15 @@ Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, // 3) C1 ^ C2 is one-bit mask. // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask. // This implies all values in the two ranges differ by exactly one bit. - if ((PredL == ICmpInst::ICMP_ULT || PredL == ICmpInst::ICMP_ULE) && PredL == PredR && LHSC && RHSC && LHS->hasOneUse() && RHS->hasOneUse() && LHSC->getType() == RHSC->getType() && LHSC->getValue() == (RHSC->getValue())) { - Value *LAdd = LHS->getOperand(0); - Value *RAdd = RHS->getOperand(0); - Value *LAddOpnd, *RAddOpnd; ConstantInt *LAddC, *RAddC; - if (match(LAdd, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddC))) && - match(RAdd, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddC))) && + if (match(LHS0, m_Add(m_Value(LAddOpnd), m_ConstantInt(LAddC))) && + match(RHS0, m_Add(m_Value(RAddOpnd), m_ConstantInt(RAddC))) && LAddC->getValue().ugt(LHSC->getValue()) && RAddC->getValue().ugt(LHSC->getValue())) { @@ -2342,15 +2339,12 @@ Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) if (predicatesFoldable(PredL, PredR)) { - if (LHS->getOperand(0) == RHS->getOperand(1) && - LHS->getOperand(1) == RHS->getOperand(0)) + if (LHS0 == RHS1 && LHS1 == RHS0) LHS->swapOperands(); - if (LHS->getOperand(0) == RHS->getOperand(0) && - LHS->getOperand(1) == RHS->getOperand(1)) { - Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1); + if (LHS0 == RHS0 && LHS1 == RHS1) { unsigned Code = getICmpCode(LHS) | getICmpCode(RHS); bool IsSigned = LHS->isSigned() || RHS->isSigned(); - return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder); + return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder); } } @@ -2359,26 +2353,25 @@ Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder)) return V; - Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0); if (LHS->hasOneUse() || RHS->hasOneUse()) { // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1) // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1) Value *A = nullptr, *B = nullptr; if (PredL == ICmpInst::ICMP_EQ && LHSC && LHSC->isZero()) { B = LHS0; - if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS->getOperand(1)) + if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS1) A = RHS0; else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0) - A = RHS->getOperand(1); + A = RHS1; } // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1) // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1) else if (PredR == ICmpInst::ICMP_EQ && RHSC && RHSC->isZero()) { B = RHS0; - if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS->getOperand(1)) + if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS1) A = LHS0; else if (PredL == ICmpInst::ICMP_UGT && LHS0 == RHS0) - A = LHS->getOperand(1); + A = LHS1; } if (A && B) return Builder.CreateICmp( -- 2.7.4