| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1 | //===- InstCombineMulDivRem.cpp -------------------------------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv, |
| 10 | // srem, urem, frem. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/ADT/APInt.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| Duncan Sands | d0eb6d3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 19 | #include "llvm/IR/BasicBlock.h" |
| 20 | #include "llvm/IR/Constant.h" |
| 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/InstrTypes.h" |
| 23 | #include "llvm/IR/Instruction.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IntrinsicInst.h" |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Intrinsics.h" |
| 27 | #include "llvm/IR/Operator.h" |
| Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 28 | #include "llvm/IR/PatternMatch.h" |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Type.h" |
| 30 | #include "llvm/IR/Value.h" |
| 31 | #include "llvm/Support/Casting.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/KnownBits.h" |
| 34 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" |
| Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 36 | #include <cassert> |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | #include <utility> |
| 40 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | using namespace PatternMatch; |
| 43 | |
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 44 | #define DEBUG_TYPE "instcombine" |
| 45 | |
| Sanjay Patel | 6eccf48 | 2015-09-09 15:24:36 +0000 | [diff] [blame] | 46 | /// The specific integer value is used in a context where it is known to be |
| 47 | /// non-zero. If this allows us to simplify the computation, do so and return |
| 48 | /// the new operand, otherwise return null. |
| Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 49 | static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC, |
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 50 | Instruction &CxtI) { |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 51 | // If V has multiple uses, then we would have to do more analysis to determine |
| 52 | // if this is safe. For example, the use could be in dynamically unreached |
| 53 | // code. |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 54 | if (!V->hasOneUse()) return nullptr; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 55 | |
| Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 56 | bool MadeChange = false; |
| 57 | |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 58 | // ((1 << A) >>u B) --> (1 << (A-B)) |
| 59 | // Because V cannot be zero, we know that B is less than A. |
| David Majnemer | dad2103 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 60 | Value *A = nullptr, *B = nullptr, *One = nullptr; |
| 61 | if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) && |
| 62 | match(One, m_One())) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 63 | A = IC.Builder.CreateSub(A, B); |
| 64 | return IC.Builder.CreateShl(One, A); |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 65 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 66 | |
| Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 67 | // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it |
| 68 | // inexact. Similarly for <<. |
| Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 69 | BinaryOperator *I = dyn_cast<BinaryOperator>(V); |
| 70 | if (I && I->isLogicalShift() && |
| Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 71 | IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) { |
| Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 72 | // We know that this is an exact/nuw shift and that the input is a |
| 73 | // non-zero context as well. |
| 74 | if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) { |
| 75 | I->setOperand(0, V2); |
| 76 | MadeChange = true; |
| Chris Lattner | 388cb8a | 2011-05-23 00:32:19 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| Sanjay Patel | a8ef4a5 | 2016-05-22 17:08:52 +0000 | [diff] [blame] | 79 | if (I->getOpcode() == Instruction::LShr && !I->isExact()) { |
| 80 | I->setIsExact(); |
| 81 | MadeChange = true; |
| 82 | } |
| 83 | |
| 84 | if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) { |
| 85 | I->setHasNoUnsignedWrap(); |
| 86 | MadeChange = true; |
| 87 | } |
| 88 | } |
| 89 | |
| Chris Lattner | 162dfc3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 90 | // TODO: Lots more we could do here: |
| Chris Lattner | 162dfc3 | 2011-05-22 18:26:48 +0000 | [diff] [blame] | 91 | // If V is a phi node, we can call this on each of its operands. |
| 92 | // "select cond, X, 0" can simplify to "X". |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 93 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 94 | return MadeChange ? V : nullptr; |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 97 | /// A helper routine of InstCombiner::visitMul(). |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 98 | /// |
| Simon Pilgrim | 0b9f391 | 2018-02-08 14:10:01 +0000 | [diff] [blame] | 99 | /// If C is a scalar/vector of known powers of 2, then this function returns |
| 100 | /// a new scalar/vector obtained from logBase2 of C. |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 101 | /// Return a null pointer otherwise. |
| Simon Pilgrim | 0b9f391 | 2018-02-08 14:10:01 +0000 | [diff] [blame] | 102 | static Constant *getLogBase2(Type *Ty, Constant *C) { |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 103 | const APInt *IVal; |
| Simon Pilgrim | be0dd72 | 2018-02-13 13:16:26 +0000 | [diff] [blame] | 104 | if (match(C, m_APInt(IVal)) && IVal->isPowerOf2()) |
| 105 | return ConstantInt::get(Ty, IVal->logBase2()); |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 106 | |
| Simon Pilgrim | 0b9f391 | 2018-02-08 14:10:01 +0000 | [diff] [blame] | 107 | if (!Ty->isVectorTy()) |
| 108 | return nullptr; |
| 109 | |
| 110 | SmallVector<Constant *, 4> Elts; |
| 111 | for (unsigned I = 0, E = Ty->getVectorNumElements(); I != E; ++I) { |
| 112 | Constant *Elt = C->getAggregateElement(I); |
| 113 | if (!Elt) |
| 114 | return nullptr; |
| 115 | if (isa<UndefValue>(Elt)) { |
| 116 | Elts.push_back(UndefValue::get(Ty->getScalarType())); |
| 117 | continue; |
| 118 | } |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 119 | if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2()) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 120 | return nullptr; |
| Simon Pilgrim | 0b9f391 | 2018-02-08 14:10:01 +0000 | [diff] [blame] | 121 | Elts.push_back(ConstantInt::get(Ty->getScalarType(), IVal->logBase2())); |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return ConstantVector::get(Elts); |
| 125 | } |
| 126 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 127 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 128 | if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1), |
| 129 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 130 | return replaceInstUsesWith(I, V); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 131 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 132 | if (SimplifyAssociativeOrCommutative(I)) |
| 133 | return &I; |
| 134 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 135 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 136 | return X; |
| 137 | |
| Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 138 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 139 | return replaceInstUsesWith(I, V); |
| Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 140 | |
| David Majnemer | 027bc80 | 2014-11-22 04:52:38 +0000 | [diff] [blame] | 141 | // X * -1 == 0 - X |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 142 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| David Majnemer | 027bc80 | 2014-11-22 04:52:38 +0000 | [diff] [blame] | 143 | if (match(Op1, m_AllOnes())) { |
| 144 | BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName()); |
| 145 | if (I.hasNoSignedWrap()) |
| 146 | BO->setHasNoSignedWrap(); |
| 147 | return BO; |
| 148 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 149 | |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 150 | // Also allow combining multiply instructions on vectors. |
| 151 | { |
| 152 | Value *NewOp; |
| 153 | Constant *C1, *C2; |
| 154 | const APInt *IVal; |
| 155 | if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)), |
| 156 | m_Constant(C1))) && |
| David Majnemer | fd4a6d2 | 2014-11-22 04:52:52 +0000 | [diff] [blame] | 157 | match(C1, m_APInt(IVal))) { |
| 158 | // ((X << C2)*C1) == (X * (C1 << C2)) |
| 159 | Constant *Shl = ConstantExpr::getShl(C1, C2); |
| 160 | BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0)); |
| 161 | BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl); |
| 162 | if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap()) |
| 163 | BO->setHasNoUnsignedWrap(); |
| 164 | if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() && |
| 165 | Shl->isNotMinSignedValue()) |
| 166 | BO->setHasNoSignedWrap(); |
| 167 | return BO; |
| 168 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 169 | |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 170 | if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) { |
| Simon Pilgrim | 0b9f391 | 2018-02-08 14:10:01 +0000 | [diff] [blame] | 171 | // Replace X*(2^C) with X << C, where C is either a scalar or a vector. |
| 172 | if (Constant *NewCst = getLogBase2(NewOp->getType(), C1)) { |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 173 | BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst); |
| Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 174 | |
| Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 175 | if (I.hasNoUnsignedWrap()) |
| 176 | Shl->setHasNoUnsignedWrap(); |
| David Majnemer | 45951a6 | 2015-04-18 04:41:30 +0000 | [diff] [blame] | 177 | if (I.hasNoSignedWrap()) { |
| Craig Topper | 5fe0197 | 2017-06-27 19:57:53 +0000 | [diff] [blame] | 178 | const APInt *V; |
| Craig Topper | 4e63db8 | 2018-09-11 17:57:20 +0000 | [diff] [blame] | 179 | if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1) |
| David Majnemer | 45951a6 | 2015-04-18 04:41:30 +0000 | [diff] [blame] | 180 | Shl->setHasNoSignedWrap(); |
| 181 | } |
| Tilmann Scheller | 2bc5cb6 | 2014-10-07 10:19:34 +0000 | [diff] [blame] | 182 | |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 183 | return Shl; |
| 184 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 185 | } |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 186 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 187 | |
| Rafael Espindola | 65281bf | 2013-05-31 14:27:15 +0000 | [diff] [blame] | 188 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 189 | // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n |
| 190 | // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n |
| 191 | // The "* (2**n)" thus becomes a potential shifting opportunity. |
| Stuart Hastings | 8284374 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 192 | { |
| 193 | const APInt & Val = CI->getValue(); |
| 194 | const APInt &PosVal = Val.abs(); |
| 195 | if (Val.isNegative() && PosVal.isPowerOf2()) { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 196 | Value *X = nullptr, *Y = nullptr; |
| Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 197 | if (Op0->hasOneUse()) { |
| 198 | ConstantInt *C1; |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 199 | Value *Sub = nullptr; |
| Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 200 | if (match(Op0, m_Sub(m_Value(Y), m_Value(X)))) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 201 | Sub = Builder.CreateSub(X, Y, "suba"); |
| Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 202 | else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1)))) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 203 | Sub = Builder.CreateSub(Builder.CreateNeg(C1), Y, "subc"); |
| Stuart Hastings | 2380483 | 2011-06-01 16:42:47 +0000 | [diff] [blame] | 204 | if (Sub) |
| 205 | return |
| 206 | BinaryOperator::CreateMul(Sub, |
| 207 | ConstantInt::get(Y->getType(), PosVal)); |
| Stuart Hastings | 8284374 | 2011-05-30 20:00:33 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 211 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 212 | |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 213 | if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) |
| 214 | return FoldedMul; |
| 215 | |
| Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 216 | // Simplify mul instructions with a constant RHS. |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 217 | if (isa<Constant>(Op1)) { |
| Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 218 | // Canonicalize (X+C1)*CI -> X*CI+C1*CI. |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 219 | Value *X; |
| 220 | Constant *C1; |
| 221 | if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) { |
| 222 | Value *Mul = Builder.CreateMul(C1, Op1); |
| 223 | // Only go forward with the transform if C1*CI simplifies to a tidier |
| 224 | // constant. |
| 225 | if (!match(Mul, m_Mul(m_Value(), m_Value()))) |
| 226 | return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul); |
| Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 227 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| Sanjay Patel | 604cb9e | 2018-02-14 16:50:55 +0000 | [diff] [blame] | 230 | // -X * C --> X * -C |
| 231 | Value *X, *Y; |
| 232 | Constant *Op1C; |
| 233 | if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C))) |
| 234 | return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C)); |
| 235 | |
| 236 | // -X * -Y --> X * Y |
| 237 | if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) { |
| 238 | auto *NewMul = BinaryOperator::CreateMul(X, Y); |
| 239 | if (I.hasNoSignedWrap() && |
| 240 | cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() && |
| 241 | cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap()) |
| 242 | NewMul->setHasNoSignedWrap(); |
| 243 | return NewMul; |
| David Majnemer | 8279a750 | 2014-11-22 07:25:19 +0000 | [diff] [blame] | 244 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 245 | |
| Chen Zheng | 4952e66 | 2019-01-01 01:09:20 +0000 | [diff] [blame] | 246 | // -X * Y --> -(X * Y) |
| 247 | // X * -Y --> -(X * Y) |
| 248 | if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y)))) |
| 249 | return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y)); |
| 250 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 251 | // (X / Y) * Y = X - (X % Y) |
| 252 | // (X / Y) * -Y = (X % Y) - X |
| 253 | { |
| Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 254 | Value *Y = Op1; |
| 255 | BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0); |
| 256 | if (!Div || (Div->getOpcode() != Instruction::UDiv && |
| 257 | Div->getOpcode() != Instruction::SDiv)) { |
| 258 | Y = Op0; |
| 259 | Div = dyn_cast<BinaryOperator>(Op1); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 260 | } |
| Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 261 | Value *Neg = dyn_castNegVal(Y); |
| 262 | if (Div && Div->hasOneUse() && |
| 263 | (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) && |
| 264 | (Div->getOpcode() == Instruction::UDiv || |
| 265 | Div->getOpcode() == Instruction::SDiv)) { |
| 266 | Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 267 | |
| Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 268 | // If the division is exact, X % Y is zero, so we end up with X or -X. |
| Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 269 | if (Div->isExact()) { |
| 270 | if (DivOp1 == Y) |
| 271 | return replaceInstUsesWith(I, X); |
| 272 | return BinaryOperator::CreateNeg(X); |
| 273 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 274 | |
| Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 275 | auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem |
| 276 | : Instruction::SRem; |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 277 | Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1); |
| Sanjay Patel | a0a5682 | 2017-03-14 17:27:27 +0000 | [diff] [blame] | 278 | if (DivOp1 == Y) |
| 279 | return BinaryOperator::CreateSub(X, Rem); |
| 280 | return BinaryOperator::CreateSub(Rem, X); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | /// i1 mul -> i1 and. |
| Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 285 | if (I.getType()->isIntOrIntVectorTy(1)) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 286 | return BinaryOperator::CreateAnd(Op0, Op1); |
| 287 | |
| 288 | // X*(1 << Y) --> X << Y |
| 289 | // (1 << Y)*X --> X << Y |
| 290 | { |
| 291 | Value *Y; |
| David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 292 | BinaryOperator *BO = nullptr; |
| 293 | bool ShlNSW = false; |
| 294 | if (match(Op0, m_Shl(m_One(), m_Value(Y)))) { |
| 295 | BO = BinaryOperator::CreateShl(Op1, Y); |
| David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 296 | ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap(); |
| David Majnemer | 8e6f6a9 | 2014-11-24 16:41:13 +0000 | [diff] [blame] | 297 | } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) { |
| David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 298 | BO = BinaryOperator::CreateShl(Op0, Y); |
| David Majnemer | 087dc8b | 2015-01-04 07:36:02 +0000 | [diff] [blame] | 299 | ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap(); |
| David Majnemer | 546f810 | 2014-11-22 08:57:02 +0000 | [diff] [blame] | 300 | } |
| 301 | if (BO) { |
| 302 | if (I.hasNoUnsignedWrap()) |
| 303 | BO->setHasNoUnsignedWrap(); |
| 304 | if (I.hasNoSignedWrap() && ShlNSW) |
| 305 | BO->setHasNoSignedWrap(); |
| 306 | return BO; |
| 307 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 308 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 309 | |
| Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 310 | // (bool X) * Y --> X ? Y : 0 |
| Sanjay Patel | 7558d86 | 2018-02-13 22:24:37 +0000 | [diff] [blame] | 311 | // Y * (bool X) --> X ? Y : 0 |
| Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 312 | if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) |
| 313 | return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0)); |
| Sanjay Patel | cb8ac00 | 2018-02-13 20:41:22 +0000 | [diff] [blame] | 314 | if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) |
| 315 | return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0)); |
| 316 | |
| Sanjay Patel | 7558d86 | 2018-02-13 22:24:37 +0000 | [diff] [blame] | 317 | // (lshr X, 31) * Y --> (ashr X, 31) & Y |
| 318 | // Y * (lshr X, 31) --> (ashr X, 31) & Y |
| 319 | // TODO: We are not checking one-use because the elimination of the multiply |
| 320 | // is better for analysis? |
| 321 | // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be |
| 322 | // more similar to what we're doing above. |
| 323 | const APInt *C; |
| 324 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) |
| 325 | return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1); |
| 326 | if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1) |
| 327 | return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 328 | |
| Sanjay Patel | 90a3634 | 2018-09-14 22:23:35 +0000 | [diff] [blame] | 329 | if (Instruction *Ext = narrowMathIfNoOverflow(I)) |
| 330 | return Ext; |
| David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 331 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 332 | bool Changed = false; |
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 333 | if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) { |
| David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 334 | Changed = true; |
| 335 | I.setHasNoSignedWrap(true); |
| 336 | } |
| 337 | |
| Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 338 | if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) { |
| David Majnemer | b1296ec | 2014-12-26 09:50:35 +0000 | [diff] [blame] | 339 | Changed = true; |
| 340 | I.setHasNoUnsignedWrap(true); |
| 341 | } |
| 342 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 343 | return Changed ? &I : nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 347 | if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1), |
| 348 | I.getFastMathFlags(), |
| Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 349 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 350 | return replaceInstUsesWith(I, V); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 351 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 352 | if (SimplifyAssociativeOrCommutative(I)) |
| 353 | return &I; |
| 354 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 355 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 356 | return X; |
| 357 | |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 358 | if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I)) |
| 359 | return FoldedMul; |
| 360 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 361 | // X * -1.0 --> -X |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 362 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 363 | if (match(Op1, m_SpecificFP(-1.0))) |
| 364 | return BinaryOperator::CreateFNegFMF(Op0, &I); |
| Sanjay Patel | 6b9c7a9 | 2018-02-23 17:14:28 +0000 | [diff] [blame] | 365 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 366 | // -X * -Y --> X * Y |
| 367 | Value *X, *Y; |
| 368 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) |
| 369 | return BinaryOperator::CreateFMulFMF(X, Y, &I); |
| Owen Anderson | f74cfe0 | 2014-01-16 20:36:42 +0000 | [diff] [blame] | 370 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 371 | // -X * C --> X * -C |
| 372 | Constant *C; |
| 373 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C))) |
| 374 | return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I); |
| Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 375 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 376 | // Sink negation: -X * Y --> -(X * Y) |
| 377 | if (match(Op0, m_OneUse(m_FNeg(m_Value(X))))) |
| 378 | return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op1, &I), &I); |
| Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 379 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 380 | // Sink negation: Y * -X --> -(X * Y) |
| 381 | if (match(Op1, m_OneUse(m_FNeg(m_Value(X))))) |
| 382 | return BinaryOperator::CreateFNegFMF(Builder.CreateFMulFMF(X, Op0, &I), &I); |
| Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 383 | |
| Sanjay Patel | e29375d | 2018-03-02 23:06:45 +0000 | [diff] [blame] | 384 | // fabs(X) * fabs(X) -> X * X |
| 385 | if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::fabs>(m_Value(X)))) |
| 386 | return BinaryOperator::CreateFMulFMF(X, X, &I); |
| 387 | |
| 388 | // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E) |
| 389 | if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1)) |
| 390 | return replaceInstUsesWith(I, V); |
| 391 | |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 392 | if (I.hasAllowReassoc()) { |
| 393 | // Reassociate constant RHS with another constant to form constant |
| 394 | // expression. |
| 395 | if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) { |
| 396 | Constant *C1; |
| 397 | if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) { |
| 398 | // (C1 / X) * C --> (C * C1) / X |
| 399 | Constant *CC1 = ConstantExpr::getFMul(C, C1); |
| 400 | if (CC1->isNormalFP()) |
| 401 | return BinaryOperator::CreateFDivFMF(CC1, X, &I); |
| 402 | } |
| 403 | if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) { |
| 404 | // (X / C1) * C --> X * (C / C1) |
| 405 | Constant *CDivC1 = ConstantExpr::getFDiv(C, C1); |
| 406 | if (CDivC1->isNormalFP()) |
| 407 | return BinaryOperator::CreateFMulFMF(X, CDivC1, &I); |
| Sanjay Patel | 204edec | 2018-03-13 14:46:32 +0000 | [diff] [blame] | 408 | |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 409 | // If the constant was a denormal, try reassociating differently. |
| 410 | // (X / C1) * C --> X / (C1 / C) |
| 411 | Constant *C1DivC = ConstantExpr::getFDiv(C1, C); |
| 412 | if (Op0->hasOneUse() && C1DivC->isNormalFP()) |
| 413 | return BinaryOperator::CreateFDivFMF(X, C1DivC, &I); |
| 414 | } |
| 415 | |
| 416 | // We do not need to match 'fadd C, X' and 'fsub X, C' because they are |
| 417 | // canonicalized to 'fadd X, C'. Distributing the multiply may allow |
| 418 | // further folds and (X * C) + C2 is 'fma'. |
| 419 | if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) { |
| 420 | // (X + C1) * C --> (X * C) + (C * C1) |
| 421 | Constant *CC1 = ConstantExpr::getFMul(C, C1); |
| 422 | Value *XC = Builder.CreateFMulFMF(X, C, &I); |
| 423 | return BinaryOperator::CreateFAddFMF(XC, CC1, &I); |
| 424 | } |
| 425 | if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) { |
| 426 | // (C1 - X) * C --> (C * C1) - (X * C) |
| 427 | Constant *CC1 = ConstantExpr::getFMul(C, C1); |
| 428 | Value *XC = Builder.CreateFMulFMF(X, C, &I); |
| 429 | return BinaryOperator::CreateFSubFMF(CC1, XC, &I); |
| 430 | } |
| Sanjay Patel | 204edec | 2018-03-13 14:46:32 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 433 | // sqrt(X) * sqrt(Y) -> sqrt(X * Y) |
| 434 | // nnan disallows the possibility of returning a number if both operands are |
| 435 | // negative (in that case, we should return NaN). |
| 436 | if (I.hasNoNaNs() && |
| 437 | match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) && |
| 438 | match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) { |
| 439 | Value *XY = Builder.CreateFMulFMF(X, Y, &I); |
| Neil Henning | 57f5d0a | 2018-10-08 10:32:33 +0000 | [diff] [blame] | 440 | Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I); |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 441 | return replaceInstUsesWith(I, Sqrt); |
| Sanjay Patel | 4fd4fd6 | 2018-03-26 15:03:57 +0000 | [diff] [blame] | 442 | } |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 443 | |
| Sanjay Patel | 773e04c | 2019-04-08 21:23:50 +0000 | [diff] [blame] | 444 | // Like the similar transform in instsimplify, this requires 'nsz' because |
| 445 | // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0. |
| 446 | if (I.hasNoNaNs() && I.hasNoSignedZeros() && Op0 == Op1 && |
| 447 | Op0->hasNUses(2)) { |
| 448 | // Peek through fdiv to find squaring of square root: |
| 449 | // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y |
| 450 | if (match(Op0, m_FDiv(m_Value(X), |
| 451 | m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) { |
| 452 | Value *XX = Builder.CreateFMulFMF(X, X, &I); |
| 453 | return BinaryOperator::CreateFDivFMF(XX, Y, &I); |
| 454 | } |
| 455 | // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X) |
| 456 | if (match(Op0, m_FDiv(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)), |
| 457 | m_Value(X)))) { |
| 458 | Value *XX = Builder.CreateFMulFMF(X, X, &I); |
| 459 | return BinaryOperator::CreateFDivFMF(Y, XX, &I); |
| 460 | } |
| 461 | } |
| 462 | |
| Dmitry Venikov | 8817658 | 2019-01-31 06:28:10 +0000 | [diff] [blame] | 463 | // exp(X) * exp(Y) -> exp(X + Y) |
| 464 | // Match as long as at least one of exp has only one use. |
| 465 | if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) && |
| 466 | match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y))) && |
| 467 | (Op0->hasOneUse() || Op1->hasOneUse())) { |
| 468 | Value *XY = Builder.CreateFAddFMF(X, Y, &I); |
| 469 | Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I); |
| 470 | return replaceInstUsesWith(I, Exp); |
| 471 | } |
| 472 | |
| 473 | // exp2(X) * exp2(Y) -> exp2(X + Y) |
| 474 | // Match as long as at least one of exp2 has only one use. |
| 475 | if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) && |
| 476 | match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y))) && |
| 477 | (Op0->hasOneUse() || Op1->hasOneUse())) { |
| 478 | Value *XY = Builder.CreateFAddFMF(X, Y, &I); |
| 479 | Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I); |
| 480 | return replaceInstUsesWith(I, Exp2); |
| 481 | } |
| 482 | |
| Sanjay Patel | 81b3b10 | 2018-04-03 22:19:19 +0000 | [diff] [blame] | 483 | // (X*Y) * X => (X*X) * Y where Y != X |
| 484 | // The purpose is two-fold: |
| 485 | // 1) to form a power expression (of X). |
| 486 | // 2) potentially shorten the critical path: After transformation, the |
| 487 | // latency of the instruction Y is amortized by the expression of X*X, |
| 488 | // and therefore Y is in a "less critical" position compared to what it |
| 489 | // was before the transformation. |
| 490 | if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) && |
| 491 | Op1 != Y) { |
| 492 | Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I); |
| 493 | return BinaryOperator::CreateFMulFMF(XX, Y, &I); |
| 494 | } |
| 495 | if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) && |
| 496 | Op0 != Y) { |
| 497 | Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I); |
| 498 | return BinaryOperator::CreateFMulFMF(XX, Y, &I); |
| Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 499 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| Sanjay Patel | 2fd0acf | 2018-03-02 20:32:46 +0000 | [diff] [blame] | 502 | // log2(X * 0.5) * Y = log2(X) * Y - Y |
| 503 | if (I.isFast()) { |
| 504 | IntrinsicInst *Log2 = nullptr; |
| 505 | if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>( |
| 506 | m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { |
| 507 | Log2 = cast<IntrinsicInst>(Op0); |
| 508 | Y = Op1; |
| Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 509 | } |
| Sanjay Patel | 2fd0acf | 2018-03-02 20:32:46 +0000 | [diff] [blame] | 510 | if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>( |
| 511 | m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) { |
| 512 | Log2 = cast<IntrinsicInst>(Op1); |
| 513 | Y = Op0; |
| 514 | } |
| 515 | if (Log2) { |
| 516 | Log2->setArgOperand(0, X); |
| 517 | Log2->copyFastMathFlags(&I); |
| 518 | Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I); |
| 519 | return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I); |
| Pedro Artigas | d879504 | 2012-11-30 19:09:41 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 523 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 526 | /// Fold a divide or remainder with a select instruction divisor when one of the |
| 527 | /// select operands is zero. In that case, we can use the other select operand |
| 528 | /// because div/rem by zero is undefined. |
| 529 | bool InstCombiner::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) { |
| 530 | SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1)); |
| 531 | if (!SI) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 532 | return false; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 533 | |
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 534 | int NonNullOperand; |
| 535 | if (match(SI->getTrueValue(), m_Zero())) |
| 536 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 537 | NonNullOperand = 2; |
| 538 | else if (match(SI->getFalseValue(), m_Zero())) |
| 539 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 540 | NonNullOperand = 1; |
| 541 | else |
| 542 | return false; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 543 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 544 | // Change the div/rem to use 'Y' instead of the select. |
| 545 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 546 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 547 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 548 | // problem. However, the select, or the condition of the select may have |
| 549 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 550 | // propagate the known value for the select into other uses of it, and |
| 551 | // propagate a known value of the condition into its other users. |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 552 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 553 | // If the select and condition only have a single use, don't bother with this, |
| 554 | // early exit. |
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 555 | Value *SelectCond = SI->getCondition(); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 556 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 557 | return true; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 558 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 559 | // Scan the current block backward, looking for other uses of SI. |
| Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 560 | BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin(); |
| Sanjay Patel | 72d339a | 2017-10-06 23:43:06 +0000 | [diff] [blame] | 561 | Type *CondTy = SelectCond->getType(); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 562 | while (BBI != BBFront) { |
| 563 | --BBI; |
| Serguei Katkov | d894fb4 | 2018-06-04 02:52:36 +0000 | [diff] [blame] | 564 | // If we found an instruction that we can't assume will return, so |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 565 | // information from below it cannot be propagated above it. |
| Serguei Katkov | d894fb4 | 2018-06-04 02:52:36 +0000 | [diff] [blame] | 566 | if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI)) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 567 | break; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 568 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 569 | // Replace uses of the select or its condition with the known values. |
| 570 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 571 | I != E; ++I) { |
| 572 | if (*I == SI) { |
| 573 | *I = SI->getOperand(NonNullOperand); |
| Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 574 | Worklist.Add(&*BBI); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 575 | } else if (*I == SelectCond) { |
| Sanjay Patel | 72d339a | 2017-10-06 23:43:06 +0000 | [diff] [blame] | 576 | *I = NonNullOperand == 1 ? ConstantInt::getTrue(CondTy) |
| 577 | : ConstantInt::getFalse(CondTy); |
| Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 578 | Worklist.Add(&*BBI); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 581 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 582 | // If we past the instruction, quit looking for it. |
| 583 | if (&*BBI == SI) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 584 | SI = nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 585 | if (&*BBI == SelectCond) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 586 | SelectCond = nullptr; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 587 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 588 | // If we ran out of things to eliminate, break out of the loop. |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 589 | if (!SelectCond && !SI) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 590 | break; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 591 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 596 | /// True if the multiply can not be expressed in an int this size. |
| 597 | static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product, |
| 598 | bool IsSigned) { |
| 599 | bool Overflow; |
| 600 | Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow); |
| 601 | return Overflow; |
| 602 | } |
| 603 | |
| Sanjay Patel | 9d2099c | 2018-07-15 17:06:59 +0000 | [diff] [blame] | 604 | /// True if C1 is a multiple of C2. Quotient contains C1/C2. |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 605 | static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient, |
| 606 | bool IsSigned) { |
| 607 | assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal"); |
| 608 | |
| 609 | // Bail if we will divide by zero. |
| 610 | if (C2.isNullValue()) |
| 611 | return false; |
| 612 | |
| 613 | // Bail if we would divide INT_MIN by -1. |
| 614 | if (IsSigned && C1.isMinSignedValue() && C2.isAllOnesValue()) |
| 615 | return false; |
| 616 | |
| 617 | APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 618 | if (IsSigned) |
| 619 | APInt::sdivrem(C1, C2, Quotient, Remainder); |
| 620 | else |
| 621 | APInt::udivrem(C1, C2, Quotient, Remainder); |
| 622 | |
| 623 | return Remainder.isMinValue(); |
| 624 | } |
| 625 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 626 | /// This function implements the transforms common to both integer division |
| 627 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 628 | /// division instructions. |
| Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 629 | /// Common integer divide transforms |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 630 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 631 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 632 | bool IsSigned = I.getOpcode() == Instruction::SDiv; |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 633 | Type *Ty = I.getType(); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 634 | |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 635 | // The RHS is known non-zero. |
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 636 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 637 | I.setOperand(1, V); |
| 638 | return &I; |
| 639 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 640 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 641 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 642 | // This does not apply for fdiv. |
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 643 | if (simplifyDivRemOfSelectWithZeroOp(I)) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 644 | return &I; |
| 645 | |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 646 | const APInt *C2; |
| 647 | if (match(Op1, m_APInt(C2))) { |
| 648 | Value *X; |
| 649 | const APInt *C1; |
| David Majnemer | f9a095d | 2014-08-16 08:55:06 +0000 | [diff] [blame] | 650 | |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 651 | // (X / C1) / C2 -> X / (C1*C2) |
| 652 | if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) || |
| 653 | (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) { |
| 654 | APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 655 | if (!multiplyOverflows(*C1, *C2, Product, IsSigned)) |
| 656 | return BinaryOperator::Create(I.getOpcode(), X, |
| 657 | ConstantInt::get(Ty, Product)); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 658 | } |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 659 | |
| 660 | if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) || |
| 661 | (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) { |
| 662 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 663 | |
| 664 | // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1. |
| 665 | if (isMultiple(*C2, *C1, Quotient, IsSigned)) { |
| 666 | auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X, |
| 667 | ConstantInt::get(Ty, Quotient)); |
| 668 | NewDiv->setIsExact(I.isExact()); |
| 669 | return NewDiv; |
| 670 | } |
| 671 | |
| 672 | // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2. |
| 673 | if (isMultiple(*C1, *C2, Quotient, IsSigned)) { |
| 674 | auto *Mul = BinaryOperator::Create(Instruction::Mul, X, |
| 675 | ConstantInt::get(Ty, Quotient)); |
| 676 | auto *OBO = cast<OverflowingBinaryOperator>(Op0); |
| 677 | Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); |
| 678 | Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); |
| 679 | return Mul; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) && |
| 684 | *C1 != C1->getBitWidth() - 1) || |
| 685 | (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))))) { |
| 686 | APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned); |
| 687 | APInt C1Shifted = APInt::getOneBitSet( |
| 688 | C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue())); |
| 689 | |
| Sanjay Patel | 9d2099c | 2018-07-15 17:06:59 +0000 | [diff] [blame] | 690 | // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1. |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 691 | if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) { |
| 692 | auto *BO = BinaryOperator::Create(I.getOpcode(), X, |
| 693 | ConstantInt::get(Ty, Quotient)); |
| 694 | BO->setIsExact(I.isExact()); |
| 695 | return BO; |
| 696 | } |
| 697 | |
| Sanjay Patel | 9d2099c | 2018-07-15 17:06:59 +0000 | [diff] [blame] | 698 | // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2. |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 699 | if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) { |
| 700 | auto *Mul = BinaryOperator::Create(Instruction::Mul, X, |
| 701 | ConstantInt::get(Ty, Quotient)); |
| 702 | auto *OBO = cast<OverflowingBinaryOperator>(Op0); |
| 703 | Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap()); |
| 704 | Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap()); |
| 705 | return Mul; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (!C2->isNullValue()) // avoid X udiv 0 |
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 710 | if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I)) |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 711 | return FoldedDiv; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 714 | if (match(Op0, m_One())) { |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 715 | assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?"); |
| 716 | if (IsSigned) { |
| Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 717 | // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the |
| 718 | // result is one, if Op1 is -1 then the result is minus one, otherwise |
| 719 | // it's zero. |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 720 | Value *Inc = Builder.CreateAdd(Op1, Op0); |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 721 | Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3)); |
| 722 | return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0)); |
| Craig Topper | 218a359 | 2017-04-17 03:41:47 +0000 | [diff] [blame] | 723 | } else { |
| 724 | // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the |
| 725 | // result is one, otherwise it's zero. |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 726 | return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty); |
| Nick Lewycky | f0cf8fa | 2014-05-14 03:03:05 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
| Benjamin Kramer | 57b3df5 | 2011-04-30 18:16:00 +0000 | [diff] [blame] | 730 | // See if we can fold away this div instruction. |
| 731 | if (SimplifyDemandedInstructionBits(I)) |
| 732 | return &I; |
| 733 | |
| Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 734 | // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y |
| Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 735 | Value *X, *Z; |
| 736 | if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1 |
| 737 | if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || |
| 738 | (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) |
| Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 739 | return BinaryOperator::Create(I.getOpcode(), X, Op1); |
| Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 740 | |
| 741 | // (X << Y) / X -> 1 << Y |
| 742 | Value *Y; |
| 743 | if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y)))) |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 744 | return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y); |
| Sanjay Patel | 9530f18 | 2018-01-21 16:14:51 +0000 | [diff] [blame] | 745 | if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y)))) |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 746 | return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 747 | |
| Sanjay Patel | 510d647 | 2018-02-11 17:20:32 +0000 | [diff] [blame] | 748 | // X / (X * Y) -> 1 / Y if the multiplication does not overflow. |
| 749 | if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) { |
| 750 | bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap(); |
| 751 | bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap(); |
| 752 | if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) { |
| Sanjay Patel | 39059d2 | 2018-02-12 14:14:56 +0000 | [diff] [blame] | 753 | I.setOperand(0, ConstantInt::get(Ty, 1)); |
| Sanjay Patel | 510d647 | 2018-02-11 17:20:32 +0000 | [diff] [blame] | 754 | I.setOperand(1, Y); |
| 755 | return &I; |
| 756 | } |
| 757 | } |
| 758 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 759 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 762 | static const unsigned MaxDepth = 6; |
| 763 | |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 764 | namespace { |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 765 | |
| 766 | using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1, |
| 767 | const BinaryOperator &I, |
| 768 | InstCombiner &IC); |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 769 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 770 | /// Used to maintain state for visitUDivOperand(). |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 771 | struct UDivFoldAction { |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 772 | /// Informs visitUDiv() how to fold this operand. This can be zero if this |
| 773 | /// action joins two actions together. |
| 774 | FoldUDivOperandCb FoldAction; |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 775 | |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 776 | /// Which operand to fold. |
| 777 | Value *OperandToFold; |
| 778 | |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 779 | union { |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 780 | /// The instruction returned when FoldAction is invoked. |
| 781 | Instruction *FoldResult; |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 782 | |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 783 | /// Stores the LHS action index if this action joins two actions together. |
| 784 | size_t SelectLHSIdx; |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
| 787 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 788 | : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {} |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 789 | UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS) |
| 790 | : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {} |
| 791 | }; |
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 792 | |
| 793 | } // end anonymous namespace |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 794 | |
| 795 | // X udiv 2^C -> X >> C |
| 796 | static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1, |
| 797 | const BinaryOperator &I, InstCombiner &IC) { |
| Simon Pilgrim | 94cc89d | 2018-02-08 14:46:10 +0000 | [diff] [blame] | 798 | Constant *C1 = getLogBase2(Op0->getType(), cast<Constant>(Op1)); |
| 799 | if (!C1) |
| 800 | llvm_unreachable("Failed to constant fold udiv -> logbase2"); |
| 801 | BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1); |
| Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 802 | if (I.isExact()) |
| 803 | LShr->setIsExact(); |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 804 | return LShr; |
| 805 | } |
| 806 | |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 807 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 808 | // X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2) |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 809 | static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 810 | InstCombiner &IC) { |
| Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 811 | Value *ShiftLeft; |
| 812 | if (!match(Op1, m_ZExt(m_Value(ShiftLeft)))) |
| 813 | ShiftLeft = Op1; |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 814 | |
| Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 815 | Constant *CI; |
| Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 816 | Value *N; |
| Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 817 | if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N)))) |
| Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 818 | llvm_unreachable("match should never fail here!"); |
| Simon Pilgrim | 2a90acd | 2018-02-08 15:19:38 +0000 | [diff] [blame] | 819 | Constant *Log2Base = getLogBase2(N->getType(), CI); |
| 820 | if (!Log2Base) |
| 821 | llvm_unreachable("getLogBase2 should never fail here!"); |
| 822 | N = IC.Builder.CreateAdd(N, Log2Base); |
| Andrea Di Biagio | a82d52d | 2016-09-26 12:07:23 +0000 | [diff] [blame] | 823 | if (Op1 != ShiftLeft) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 824 | N = IC.Builder.CreateZExt(N, Op1->getType()); |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 825 | BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N); |
| Suyog Sarda | 65f5ae9 | 2014-10-07 12:04:07 +0000 | [diff] [blame] | 826 | if (I.isExact()) |
| 827 | LShr->setIsExact(); |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 828 | return LShr; |
| 829 | } |
| 830 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 831 | // Recursively visits the possible right hand operands of a udiv |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 832 | // instruction, seeing through select instructions, to determine if we can |
| 833 | // replace the udiv with something simpler. If we find that an operand is not |
| 834 | // able to simplify the udiv, we abort the entire transformation. |
| 835 | static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I, |
| 836 | SmallVectorImpl<UDivFoldAction> &Actions, |
| 837 | unsigned Depth = 0) { |
| 838 | // Check to see if this is an unsigned division with an exact power of 2, |
| 839 | // if so, convert to a right shift. |
| 840 | if (match(Op1, m_Power2())) { |
| 841 | Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1)); |
| 842 | return Actions.size(); |
| 843 | } |
| 844 | |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 845 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| 846 | if (match(Op1, m_Shl(m_Power2(), m_Value())) || |
| 847 | match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) { |
| 848 | Actions.push_back(UDivFoldAction(foldUDivShl, Op1)); |
| 849 | return Actions.size(); |
| 850 | } |
| 851 | |
| 852 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 853 | if (Depth++ == MaxDepth) |
| 854 | return 0; |
| 855 | |
| 856 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| David Majnemer | 492e612 | 2014-08-30 09:19:05 +0000 | [diff] [blame] | 857 | if (size_t LHSIdx = |
| 858 | visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth)) |
| 859 | if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) { |
| 860 | Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1)); |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 861 | return Actions.size(); |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 867 | /// If we have zero-extended operands of an unsigned div or rem, we may be able |
| 868 | /// to narrow the operation (sink the zext below the math). |
| 869 | static Instruction *narrowUDivURem(BinaryOperator &I, |
| 870 | InstCombiner::BuilderTy &Builder) { |
| 871 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 872 | Value *N = I.getOperand(0); |
| 873 | Value *D = I.getOperand(1); |
| 874 | Type *Ty = I.getType(); |
| 875 | Value *X, *Y; |
| 876 | if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) && |
| 877 | X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) { |
| 878 | // udiv (zext X), (zext Y) --> zext (udiv X, Y) |
| 879 | // urem (zext X), (zext Y) --> zext (urem X, Y) |
| 880 | Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y); |
| 881 | return new ZExtInst(NarrowOp, Ty); |
| 882 | } |
| 883 | |
| 884 | Constant *C; |
| 885 | if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) || |
| 886 | (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) { |
| 887 | // If the constant is the same in the smaller type, use the narrow version. |
| 888 | Constant *TruncC = ConstantExpr::getTrunc(C, X->getType()); |
| 889 | if (ConstantExpr::getZExt(TruncC, Ty) != C) |
| 890 | return nullptr; |
| 891 | |
| 892 | // udiv (zext X), C --> zext (udiv X, C') |
| 893 | // urem (zext X), C --> zext (urem X, C') |
| 894 | // udiv C, (zext X) --> zext (udiv C', X) |
| 895 | // urem C, (zext X) --> zext (urem C', X) |
| 896 | Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC) |
| 897 | : Builder.CreateBinOp(Opcode, TruncC, X); |
| 898 | return new ZExtInst(NarrowOp, Ty); |
| 899 | } |
| 900 | |
| 901 | return nullptr; |
| 902 | } |
| 903 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 904 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 905 | if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1), |
| 906 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 907 | return replaceInstUsesWith(I, V); |
| Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 908 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 909 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 910 | return X; |
| 911 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 912 | // Handle the integer div common cases |
| 913 | if (Instruction *Common = commonIDivTransforms(I)) |
| 914 | return Common; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 915 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 916 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | 38a86d3 | 2018-06-25 22:50:26 +0000 | [diff] [blame] | 917 | Value *X; |
| 918 | const APInt *C1, *C2; |
| 919 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) { |
| 920 | // (X lshr C1) udiv C2 --> X udiv (C2 << C1) |
| 921 | bool Overflow; |
| 922 | APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow); |
| 923 | if (!Overflow) { |
| 924 | bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value())); |
| 925 | BinaryOperator *BO = BinaryOperator::CreateUDiv( |
| 926 | X, ConstantInt::get(X->getType(), C2ShlC1)); |
| 927 | if (IsExact) |
| 928 | BO->setIsExact(); |
| 929 | return BO; |
| David Majnemer | a252138 | 2014-10-13 21:48:30 +0000 | [diff] [blame] | 930 | } |
| Nadav Rotem | 11935b2 | 2012-08-28 10:01:43 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| Sanjay Patel | 38a86d3 | 2018-06-25 22:50:26 +0000 | [diff] [blame] | 933 | // Op0 / C where C is large (negative) --> zext (Op0 >= C) |
| 934 | // TODO: Could use isKnownNegative() to handle non-constant values. |
| Sanjay Patel | 7c45deb | 2018-06-26 12:41:15 +0000 | [diff] [blame] | 935 | Type *Ty = I.getType(); |
| Sanjay Patel | 38a86d3 | 2018-06-25 22:50:26 +0000 | [diff] [blame] | 936 | if (match(Op1, m_Negative())) { |
| 937 | Value *Cmp = Builder.CreateICmpUGE(Op0, Op1); |
| Sanjay Patel | 7c45deb | 2018-06-26 12:41:15 +0000 | [diff] [blame] | 938 | return CastInst::CreateZExtOrBitCast(Cmp, Ty); |
| 939 | } |
| 940 | // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined) |
| 941 | if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { |
| 942 | Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); |
| 943 | return CastInst::CreateZExtOrBitCast(Cmp, Ty); |
| Sanjay Patel | 38a86d3 | 2018-06-25 22:50:26 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 946 | if (Instruction *NarrowDiv = narrowUDivURem(I, Builder)) |
| 947 | return NarrowDiv; |
| Benjamin Kramer | 9aa91b1 | 2011-04-30 18:16:07 +0000 | [diff] [blame] | 948 | |
| Sanjay Patel | 6d6eab6 | 2018-07-26 19:22:41 +0000 | [diff] [blame] | 949 | // If the udiv operands are non-overflowing multiplies with a common operand, |
| 950 | // then eliminate the common factor: |
| 951 | // (A * B) / (A * X) --> B / X (and commuted variants) |
| 952 | // TODO: The code would be reduced if we had m_c_NUWMul pattern matching. |
| 953 | // TODO: If -reassociation handled this generally, we could remove this. |
| 954 | Value *A, *B; |
| 955 | if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) { |
| 956 | if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) || |
| 957 | match(Op1, m_NUWMul(m_Value(X), m_Specific(A)))) |
| 958 | return BinaryOperator::CreateUDiv(B, X); |
| 959 | if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) || |
| 960 | match(Op1, m_NUWMul(m_Value(X), m_Specific(B)))) |
| 961 | return BinaryOperator::CreateUDiv(A, X); |
| 962 | } |
| 963 | |
| David Majnemer | 37f8f44 | 2013-07-04 21:17:49 +0000 | [diff] [blame] | 964 | // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...)))) |
| 965 | SmallVector<UDivFoldAction, 6> UDivActions; |
| 966 | if (visitUDivOperand(Op0, Op1, I, UDivActions)) |
| 967 | for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) { |
| 968 | FoldUDivOperandCb Action = UDivActions[i].FoldAction; |
| 969 | Value *ActionOp1 = UDivActions[i].OperandToFold; |
| 970 | Instruction *Inst; |
| 971 | if (Action) |
| 972 | Inst = Action(Op0, ActionOp1, I, *this); |
| 973 | else { |
| 974 | // This action joins two actions together. The RHS of this action is |
| 975 | // simply the last action we processed, we saved the LHS action index in |
| 976 | // the joining action. |
| 977 | size_t SelectRHSIdx = i - 1; |
| 978 | Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult; |
| 979 | size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx; |
| 980 | Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult; |
| 981 | Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(), |
| 982 | SelectLHS, SelectRHS); |
| 983 | } |
| 984 | |
| 985 | // If this is the last action to process, return it to the InstCombiner. |
| 986 | // Otherwise, we insert it before the UDiv and record it so that we may |
| 987 | // use it as part of a joining action (i.e., a SelectInst). |
| 988 | if (e - i != 1) { |
| 989 | Inst->insertBefore(&I); |
| 990 | UDivActions[i].FoldResult = Inst; |
| 991 | } else |
| 992 | return Inst; |
| 993 | } |
| 994 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 995 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 999 | if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1), |
| 1000 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1001 | return replaceInstUsesWith(I, V); |
| Duncan Sands | 771e82a | 2011-01-28 16:51:11 +0000 | [diff] [blame] | 1002 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1003 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1004 | return X; |
| 1005 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1006 | // Handle the integer div common cases |
| 1007 | if (Instruction *Common = commonIDivTransforms(I)) |
| 1008 | return Common; |
| 1009 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1010 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | 6a96d90 | 2018-06-25 21:39:41 +0000 | [diff] [blame] | 1011 | Value *X; |
| 1012 | // sdiv Op0, -1 --> -Op0 |
| 1013 | // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined) |
| 1014 | if (match(Op1, m_AllOnes()) || |
| 1015 | (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))) |
| 1016 | return BinaryOperator::CreateNeg(Op0); |
| 1017 | |
| Sanjay Patel | c6ada53 | 2016-06-27 17:25:57 +0000 | [diff] [blame] | 1018 | const APInt *Op1C; |
| Sanjay Patel | bedd1f9 | 2016-06-27 18:38:40 +0000 | [diff] [blame] | 1019 | if (match(Op1, m_APInt(Op1C))) { |
| Sanjay Patel | bedd1f9 | 2016-06-27 18:38:40 +0000 | [diff] [blame] | 1020 | // sdiv exact X, C --> ashr exact X, log2(C) |
| 1021 | if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) { |
| 1022 | Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2()); |
| 1023 | return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName()); |
| 1024 | } |
| Sanjay Patel | 59ed2ff | 2016-06-27 22:27:11 +0000 | [diff] [blame] | 1025 | |
| 1026 | // If the dividend is sign-extended and the constant divisor is small enough |
| 1027 | // to fit in the source type, shrink the division to the narrower type: |
| 1028 | // (sext X) sdiv C --> sext (X sdiv C) |
| 1029 | Value *Op0Src; |
| 1030 | if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) && |
| 1031 | Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) { |
| 1032 | |
| 1033 | // In the general case, we need to make sure that the dividend is not the |
| 1034 | // minimum signed value because dividing that by -1 is UB. But here, we |
| 1035 | // know that the -1 divisor case is already handled above. |
| 1036 | |
| 1037 | Constant *NarrowDivisor = |
| 1038 | ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType()); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1039 | Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor); |
| Sanjay Patel | 59ed2ff | 2016-06-27 22:27:11 +0000 | [diff] [blame] | 1040 | return new SExtInst(NarrowOp, Op0->getType()); |
| 1041 | } |
| Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1042 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1043 | |
| Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1044 | if (Constant *RHS = dyn_cast<Constant>(Op1)) { |
| David Majnemer | f28e2a4 | 2014-07-02 06:42:13 +0000 | [diff] [blame] | 1045 | // X/INT_MIN -> X == INT_MIN |
| 1046 | if (RHS->isMinSignedValue()) |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1047 | return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), I.getType()); |
| David Majnemer | f28e2a4 | 2014-07-02 06:42:13 +0000 | [diff] [blame] | 1048 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1049 | // -X/C --> X/-C provided the negation doesn't overflow. |
| David Majnemer | fa4699e | 2014-11-22 20:00:34 +0000 | [diff] [blame] | 1050 | Value *X; |
| 1051 | if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) { |
| 1052 | auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS)); |
| 1053 | BO->setIsExact(I.isExact()); |
| 1054 | return BO; |
| 1055 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1059 | // unsigned inputs), turn this into a udiv. |
| Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1060 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
| Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1061 | if (MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1062 | if (MaskedValueIsZero(Op1, Mask, 0, &I)) { |
| 1063 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 1064 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1065 | BO->setIsExact(I.isExact()); |
| 1066 | return BO; |
| 1067 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1068 | |
| Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 1069 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
| Craig Topper | f248468 | 2017-04-17 01:51:19 +0000 | [diff] [blame] | 1070 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 1071 | // Safe because the only negative value (1 << Y) can take on is |
| 1072 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 1073 | // the sign bit set. |
| 1074 | auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 1075 | BO->setIsExact(I.isExact()); |
| 1076 | return BO; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1079 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1080 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1083 | /// Remove negation and try to convert division into multiplication. |
| Sanjay Patel | 90f4c8e | 2018-02-20 16:08:15 +0000 | [diff] [blame] | 1084 | static Instruction *foldFDivConstantDivisor(BinaryOperator &I) { |
| 1085 | Constant *C; |
| 1086 | if (!match(I.getOperand(1), m_Constant(C))) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1087 | return nullptr; |
| Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 1088 | |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1089 | // -X / C --> X / -C |
| 1090 | Value *X; |
| 1091 | if (match(I.getOperand(0), m_FNeg(m_Value(X)))) |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1092 | return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I); |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1093 | |
| Sanjay Patel | 90f4c8e | 2018-02-20 16:08:15 +0000 | [diff] [blame] | 1094 | // If the constant divisor has an exact inverse, this is always safe. If not, |
| 1095 | // then we can still create a reciprocal if fast-math-flags allow it and the |
| 1096 | // constant is a regular number (not zero, infinite, or denormal). |
| 1097 | if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP()))) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1098 | return nullptr; |
| Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1099 | |
| Sanjay Patel | 90f4c8e | 2018-02-20 16:08:15 +0000 | [diff] [blame] | 1100 | // Disallow denormal constants because we don't know what would happen |
| 1101 | // on all targets. |
| 1102 | // TODO: Use Intrinsic::canonicalize or let function attributes tell us that |
| 1103 | // denorms are flushed? |
| 1104 | auto *RecipC = ConstantExpr::getFDiv(ConstantFP::get(I.getType(), 1.0), C); |
| 1105 | if (!RecipC->isNormalFP()) |
| 1106 | return nullptr; |
| 1107 | |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1108 | // X / C --> X * (1 / C) |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1109 | return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I); |
| Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1112 | /// Remove negation and try to reassociate constant math. |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1113 | static Instruction *foldFDivConstantDividend(BinaryOperator &I) { |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1114 | Constant *C; |
| 1115 | if (!match(I.getOperand(0), m_Constant(C))) |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1116 | return nullptr; |
| 1117 | |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1118 | // C / -X --> -C / X |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1119 | Value *X; |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1120 | if (match(I.getOperand(1), m_FNeg(m_Value(X)))) |
| 1121 | return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I); |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (!I.hasAllowReassoc() || !I.hasAllowReciprocal()) |
| 1124 | return nullptr; |
| 1125 | |
| 1126 | // Try to reassociate C / X expressions where X includes another constant. |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1127 | Constant *C2, *NewC = nullptr; |
| 1128 | if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) { |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1129 | // C / (X * C2) --> (C / C2) / X |
| 1130 | NewC = ConstantExpr::getFDiv(C, C2); |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1131 | } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) { |
| Sanjay Patel | 6f716a7 | 2018-02-21 00:01:45 +0000 | [diff] [blame] | 1132 | // C / (X / C2) --> (C * C2) / X |
| 1133 | NewC = ConstantExpr::getFMul(C, C2); |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1134 | } |
| 1135 | // Disallow denormal constants because we don't know what would happen |
| 1136 | // on all targets. |
| 1137 | // TODO: Use Intrinsic::canonicalize or let function attributes tell us that |
| 1138 | // denorms are flushed? |
| 1139 | if (!NewC || !NewC->isNormalFP()) |
| 1140 | return nullptr; |
| 1141 | |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1142 | return BinaryOperator::CreateFDivFMF(NewC, X, &I); |
| Sanjay Patel | e412954 | 2018-02-19 21:17:58 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1145 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1146 | if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1), |
| 1147 | I.getFastMathFlags(), |
| Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1148 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1149 | return replaceInstUsesWith(I, V); |
| Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1150 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1151 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1152 | return X; |
| 1153 | |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1154 | if (Instruction *R = foldFDivConstantDivisor(I)) |
| 1155 | return R; |
| Sanjay Patel | 2816560 | 2018-02-19 23:09:03 +0000 | [diff] [blame] | 1156 | |
| Sanjay Patel | d8dd015 | 2018-02-20 23:51:16 +0000 | [diff] [blame] | 1157 | if (Instruction *R = foldFDivConstantDividend(I)) |
| 1158 | return R; |
| Sanjay Patel | b39bcc0 | 2018-02-14 23:04:17 +0000 | [diff] [blame] | 1159 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1160 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1161 | if (isa<Constant>(Op0)) |
| 1162 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1163 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1164 | return R; |
| 1165 | |
| Sanjay Patel | 29b98ae | 2018-02-20 17:14:53 +0000 | [diff] [blame] | 1166 | if (isa<Constant>(Op1)) |
| Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1167 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 1168 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1169 | return R; |
| Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1170 | |
| Sanjay Patel | 31a9046 | 2018-02-26 16:02:45 +0000 | [diff] [blame] | 1171 | if (I.hasAllowReassoc() && I.hasAllowReciprocal()) { |
| Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1172 | Value *X, *Y; |
| Sanjay Patel | 91bb775 | 2018-02-16 17:52:32 +0000 | [diff] [blame] | 1173 | if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && |
| 1174 | (!isa<Constant>(Y) || !isa<Constant>(Op1))) { |
| 1175 | // (X / Y) / Z => X / (Y * Z) |
| Sanjay Patel | 31a9046 | 2018-02-26 16:02:45 +0000 | [diff] [blame] | 1176 | Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I); |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1177 | return BinaryOperator::CreateFDivFMF(X, YZ, &I); |
| Shuxin Yang | 320f52a | 2013-01-14 22:48:41 +0000 | [diff] [blame] | 1178 | } |
| Sanjay Patel | 91bb775 | 2018-02-16 17:52:32 +0000 | [diff] [blame] | 1179 | if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) && |
| 1180 | (!isa<Constant>(Y) || !isa<Constant>(Op0))) { |
| 1181 | // Z / (X / Y) => (Y * Z) / X |
| Sanjay Patel | 31a9046 | 2018-02-26 16:02:45 +0000 | [diff] [blame] | 1182 | Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I); |
| Sanjay Patel | 5a6f904 | 2018-02-21 22:18:55 +0000 | [diff] [blame] | 1183 | return BinaryOperator::CreateFDivFMF(YZ, X, &I); |
| Benjamin Kramer | 8564e0d | 2011-03-30 15:42:35 +0000 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
| Sanjay Patel | 339b4d3 | 2018-02-15 15:07:12 +0000 | [diff] [blame] | 1187 | if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) { |
| Sanjay Patel | 65da14d | 2018-02-16 16:13:20 +0000 | [diff] [blame] | 1188 | // sin(X) / cos(X) -> tan(X) |
| 1189 | // cos(X) / sin(X) -> 1/tan(X) (cotangent) |
| 1190 | Value *X; |
| 1191 | bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) && |
| 1192 | match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X))); |
| 1193 | bool IsCot = |
| 1194 | !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) && |
| 1195 | match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X))); |
| Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 1196 | |
| Sanjay Patel | 65da14d | 2018-02-16 16:13:20 +0000 | [diff] [blame] | 1197 | if ((IsTan || IsCot) && hasUnaryFloatFn(&TLI, I.getType(), LibFunc_tan, |
| 1198 | LibFunc_tanf, LibFunc_tanl)) { |
| 1199 | IRBuilder<> B(&I); |
| 1200 | IRBuilder<>::FastMathFlagGuard FMFGuard(B); |
| 1201 | B.setFastMathFlags(I.getFastMathFlags()); |
| Craig Topper | c1892ec | 2019-01-31 17:23:29 +0000 | [diff] [blame] | 1202 | AttributeList Attrs = |
| 1203 | cast<CallBase>(Op0)->getCalledFunction()->getAttributes(); |
| Mikael Holmen | e3605d0 | 2018-10-18 06:27:53 +0000 | [diff] [blame] | 1204 | Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf, |
| 1205 | LibFunc_tanl, B, Attrs); |
| Sanjay Patel | 65da14d | 2018-02-16 16:13:20 +0000 | [diff] [blame] | 1206 | if (IsCot) |
| 1207 | Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res); |
| 1208 | return replaceInstUsesWith(I, Res); |
| Dmitry Venikov | e5fbf59 | 2018-01-11 06:33:00 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| Sanjay Patel | 1998cc6 | 2018-02-12 18:38:35 +0000 | [diff] [blame] | 1212 | // -X / -Y -> X / Y |
| 1213 | Value *X, *Y; |
| 1214 | if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y)))) { |
| 1215 | I.setOperand(0, X); |
| 1216 | I.setOperand(1, Y); |
| Matt Arsenault | fdb78f8 | 2017-01-10 23:08:54 +0000 | [diff] [blame] | 1217 | return &I; |
| 1218 | } |
| 1219 | |
| Sanjay Patel | 4a4f35f | 2018-02-12 19:39:21 +0000 | [diff] [blame] | 1220 | // X / (X * Y) --> 1.0 / Y |
| 1221 | // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed. |
| 1222 | // We can ignore the possibility that X is infinity because INF/INF is NaN. |
| 1223 | if (I.hasNoNaNs() && I.hasAllowReassoc() && |
| 1224 | match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) { |
| 1225 | I.setOperand(0, ConstantFP::get(I.getType(), 1.0)); |
| 1226 | I.setOperand(1, Y); |
| 1227 | return &I; |
| 1228 | } |
| 1229 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1230 | return nullptr; |
| Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1233 | /// This function implements the transforms common to both integer remainder |
| 1234 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 1235 | /// remainder instructions. |
| Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 1236 | /// Common integer remainder transforms |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1237 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 1238 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1239 | |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1240 | // The RHS is known non-zero. |
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1241 | if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { |
| Chris Lattner | 7c99f19 | 2011-05-22 18:18:41 +0000 | [diff] [blame] | 1242 | I.setOperand(1, V); |
| 1243 | return &I; |
| 1244 | } |
| 1245 | |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1246 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 1247 | if (simplifyDivRemOfSelectWithZeroOp(I)) |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1248 | return &I; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1249 | |
| Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1250 | if (isa<Constant>(Op1)) { |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1251 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 1252 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 1253 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1254 | return R; |
| Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1255 | } else if (auto *PN = dyn_cast<PHINode>(Op0I)) { |
| Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1256 | const APInt *Op1Int; |
| 1257 | if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() && |
| 1258 | (I.getOpcode() == Instruction::URem || |
| 1259 | !Op1Int->isMinSignedValue())) { |
| Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1260 | // foldOpIntoPhi will speculate instructions to the end of the PHI's |
| Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1261 | // predecessor blocks, so do this only if we know the srem or urem |
| 1262 | // will not fault. |
| Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1263 | if (Instruction *NV = foldOpIntoPhi(I, PN)) |
| Sanjoy Das | b7e861a | 2016-06-05 21:17:04 +0000 | [diff] [blame] | 1264 | return NV; |
| 1265 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | // See if we can fold away this rem instruction. |
| 1269 | if (SimplifyDemandedInstructionBits(I)) |
| 1270 | return &I; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1274 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1278 | if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1), |
| 1279 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1280 | return replaceInstUsesWith(I, V); |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1281 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1282 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1283 | return X; |
| 1284 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1285 | if (Instruction *common = commonIRemTransforms(I)) |
| 1286 | return common; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1287 | |
| Sanjay Patel | bb78938 | 2017-08-24 22:54:01 +0000 | [diff] [blame] | 1288 | if (Instruction *NarrowRem = narrowUDivURem(I, Builder)) |
| 1289 | return NarrowRem; |
| David Majnemer | 6c30f49 | 2013-05-12 00:07:05 +0000 | [diff] [blame] | 1290 | |
| David Majnemer | 470b077 | 2013-05-11 09:01:28 +0000 | [diff] [blame] | 1291 | // X urem Y -> X and Y-1, where Y is a power of 2, |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1292 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| Sanjay Patel | 3575f0c | 2018-06-26 16:30:00 +0000 | [diff] [blame] | 1293 | Type *Ty = I.getType(); |
| Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 1294 | if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) { |
| Sanjay Patel | 3575f0c | 2018-06-26 16:30:00 +0000 | [diff] [blame] | 1295 | Constant *N1 = Constant::getAllOnesValue(Ty); |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1296 | Value *Add = Builder.CreateAdd(Op1, N1); |
| Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 1297 | return BinaryOperator::CreateAnd(Op0, Add); |
| 1298 | } |
| 1299 | |
| Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1300 | // 1 urem X -> zext(X != 1) |
| Sanjay Patel | 9adea01 | 2018-06-26 16:39:29 +0000 | [diff] [blame] | 1301 | if (match(Op0, m_One())) |
| 1302 | return CastInst::CreateZExtOrBitCast(Builder.CreateICmpNE(Op1, Op0), Ty); |
| Nick Lewycky | 7459be6 | 2013-07-13 01:16:47 +0000 | [diff] [blame] | 1303 | |
| Sanjay Patel | 30ef70b | 2016-09-22 22:36:26 +0000 | [diff] [blame] | 1304 | // X urem C -> X < C ? X : X - C, where C >= signbit. |
| Simon Pilgrim | 1889f26 | 2018-02-08 18:36:01 +0000 | [diff] [blame] | 1305 | if (match(Op1, m_Negative())) { |
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1306 | Value *Cmp = Builder.CreateICmpULT(Op0, Op1); |
| 1307 | Value *Sub = Builder.CreateSub(Op0, Op1); |
| Sanjay Patel | 30ef70b | 2016-09-22 22:36:26 +0000 | [diff] [blame] | 1308 | return SelectInst::Create(Cmp, Op0, Sub); |
| 1309 | } |
| 1310 | |
| Sanjay Patel | 3575f0c | 2018-06-26 16:30:00 +0000 | [diff] [blame] | 1311 | // If the divisor is a sext of a boolean, then the divisor must be max |
| 1312 | // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also |
| 1313 | // max unsigned value. In that case, the remainder is 0: |
| 1314 | // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0 |
| 1315 | Value *X; |
| 1316 | if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { |
| 1317 | Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); |
| 1318 | return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0); |
| 1319 | } |
| 1320 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1321 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1325 | if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1), |
| 1326 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1327 | return replaceInstUsesWith(I, V); |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1328 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1329 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1330 | return X; |
| 1331 | |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1332 | // Handle the integer rem common cases |
| 1333 | if (Instruction *Common = commonIRemTransforms(I)) |
| 1334 | return Common; |
| Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 1335 | |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1336 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1337 | { |
| 1338 | const APInt *Y; |
| 1339 | // X % -Y -> X % Y |
| Simon Pilgrim | a54e8e4 | 2018-02-08 19:00:45 +0000 | [diff] [blame] | 1340 | if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) { |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1341 | Worklist.AddValue(I.getOperand(1)); |
| David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1342 | I.setOperand(1, ConstantInt::get(I.getType(), -*Y)); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1343 | return &I; |
| 1344 | } |
| David Majnemer | db07730 | 2014-10-13 22:37:51 +0000 | [diff] [blame] | 1345 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1346 | |
| 1347 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 1348 | // unsigned inputs), turn this into a urem. |
| Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1349 | APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits())); |
| Craig Topper | 1a18a7c | 2017-04-17 01:51:24 +0000 | [diff] [blame] | 1350 | if (MaskedValueIsZero(Op1, Mask, 0, &I) && |
| 1351 | MaskedValueIsZero(Op0, Mask, 0, &I)) { |
| 1352 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| 1353 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // If it's a constant vector, flip any negative values positive. |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1357 | if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) { |
| 1358 | Constant *C = cast<Constant>(Op1); |
| 1359 | unsigned VWidth = C->getType()->getVectorNumElements(); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1360 | |
| 1361 | bool hasNegative = false; |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1362 | bool hasMissing = false; |
| 1363 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1364 | Constant *Elt = C->getAggregateElement(i); |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1365 | if (!Elt) { |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1366 | hasMissing = true; |
| 1367 | break; |
| 1368 | } |
| 1369 | |
| 1370 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt)) |
| Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1371 | if (RHS->isNegative()) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1372 | hasNegative = true; |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1373 | } |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1374 | |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1375 | if (hasNegative && !hasMissing) { |
| Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1376 | SmallVector<Constant *, 16> Elts(VWidth); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1377 | for (unsigned i = 0; i != VWidth; ++i) { |
| Chris Lattner | 8213c8a | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 1378 | Elts[i] = C->getAggregateElement(i); // Handle undef, etc. |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1379 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) { |
| Chris Lattner | b1a1512 | 2011-07-15 06:08:15 +0000 | [diff] [blame] | 1380 | if (RHS->isNegative()) |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1381 | Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS)); |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | Constant *NewRHSV = ConstantVector::get(Elts); |
| Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1386 | if (NewRHSV != C) { // Don't loop on -MININT |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1387 | Worklist.AddValue(I.getOperand(1)); |
| 1388 | I.setOperand(1, NewRHSV); |
| 1389 | return &I; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1394 | return nullptr; |
| Chris Lattner | dc054bf | 2010-01-05 06:09:35 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
| Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1398 | if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1), |
| 1399 | I.getFastMathFlags(), |
| Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1400 | SQ.getWithInstruction(&I))) |
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1401 | return replaceInstUsesWith(I, V); |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1402 | |
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1403 | if (Instruction *X = foldVectorBinop(I)) |
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1404 | return X; |
| 1405 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1406 | return nullptr; |
| Duncan Sands | a3e3699 | 2011-05-02 16:27:02 +0000 | [diff] [blame] | 1407 | } |