Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===- InstCombineAddSub.cpp ------------------------------------*- C++ -*-===// |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 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 | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the visit functions for add, fadd, sub, and fsub. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 13 | #include "InstCombineInternal.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
| 15 | #include "llvm/ADT/APInt.h" |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/InstructionSimplify.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ValueTracking.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" |
| 25 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 26 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Type.h" |
| 28 | #include "llvm/IR/Value.h" |
| 29 | #include "llvm/Support/AlignOf.h" |
| 30 | #include "llvm/Support/Casting.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 31 | #include "llvm/Support/KnownBits.h" |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 32 | #include <cassert> |
| 33 | #include <utility> |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | using namespace PatternMatch; |
| 37 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "instcombine" |
| 39 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | /// Class representing coefficient of floating-point addend. |
| 43 | /// This class needs to be highly efficient, which is especially true for |
| 44 | /// the constructor. As of I write this comment, the cost of the default |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 45 | /// constructor is merely 4-byte-store-zero (Assuming compiler is able to |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 46 | /// perform write-merging). |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 47 | /// |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 48 | class FAddendCoef { |
| 49 | public: |
Suyog Sarda | de409fd | 2014-07-17 06:09:34 +0000 | [diff] [blame] | 50 | // The constructor has to initialize a APFloat, which is unnecessary for |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 51 | // most addends which have coefficient either 1 or -1. So, the constructor |
| 52 | // is expensive. In order to avoid the cost of the constructor, we should |
| 53 | // reuse some instances whenever possible. The pre-created instances |
| 54 | // FAddCombine::Add[0-5] embodies this idea. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 55 | FAddendCoef() = default; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 56 | ~FAddendCoef(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 57 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 58 | // If possible, don't define operator+/operator- etc because these |
| 59 | // operators inevitably call FAddendCoef's constructor which is not cheap. |
| 60 | void operator=(const FAddendCoef &A); |
| 61 | void operator+=(const FAddendCoef &A); |
| 62 | void operator*=(const FAddendCoef &S); |
| 63 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 64 | void set(short C) { |
| 65 | assert(!insaneIntVal(C) && "Insane coefficient"); |
| 66 | IsFp = false; IntVal = C; |
| 67 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 68 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 69 | void set(const APFloat& C); |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 70 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 71 | void negate(); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 72 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 73 | bool isZero() const { return isInt() ? !IntVal : getFpVal().isZero(); } |
| 74 | Value *getValue(Type *) const; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 75 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 76 | bool isOne() const { return isInt() && IntVal == 1; } |
| 77 | bool isTwo() const { return isInt() && IntVal == 2; } |
| 78 | bool isMinusOne() const { return isInt() && IntVal == -1; } |
| 79 | bool isMinusTwo() const { return isInt() && IntVal == -2; } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 80 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 81 | private: |
| 82 | bool insaneIntVal(int V) { return V > 4 || V < -4; } |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 83 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 84 | APFloat *getFpValPtr() |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 85 | { return reinterpret_cast<APFloat *>(&FpValBuf.buffer[0]); } |
| 86 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 87 | const APFloat *getFpValPtr() const |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 88 | { return reinterpret_cast<const APFloat *>(&FpValBuf.buffer[0]); } |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 89 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 90 | const APFloat &getFpVal() const { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 91 | assert(IsFp && BufHasFpVal && "Incorret state"); |
David Greene | 530430b | 2013-01-14 21:04:40 +0000 | [diff] [blame] | 92 | return *getFpValPtr(); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 95 | APFloat &getFpVal() { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 96 | assert(IsFp && BufHasFpVal && "Incorret state"); |
| 97 | return *getFpValPtr(); |
| 98 | } |
| 99 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 100 | bool isInt() const { return !IsFp; } |
| 101 | |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 102 | // If the coefficient is represented by an integer, promote it to a |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 103 | // floating point. |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 104 | void convertToFpType(const fltSemantics &Sem); |
| 105 | |
| 106 | // Construct an APFloat from a signed integer. |
| 107 | // TODO: We should get rid of this function when APFloat can be constructed |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 108 | // from an *SIGNED* integer. |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 109 | APFloat createAPFloatFromInt(const fltSemantics &Sem, int Val); |
Shuxin Yang | 5b841c4 | 2012-12-19 01:10:17 +0000 | [diff] [blame] | 110 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 111 | bool IsFp = false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 112 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 113 | // True iff FpValBuf contains an instance of APFloat. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 114 | bool BufHasFpVal = false; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 115 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 116 | // The integer coefficient of an individual addend is either 1 or -1, |
| 117 | // and we try to simplify at most 4 addends from neighboring at most |
| 118 | // two instructions. So the range of <IntVal> falls in [-4, 4]. APInt |
| 119 | // is overkill of this end. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 120 | short IntVal = 0; |
Shuxin Yang | 5b841c4 | 2012-12-19 01:10:17 +0000 | [diff] [blame] | 121 | |
| 122 | AlignedCharArrayUnion<APFloat> FpValBuf; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 123 | }; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 124 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 125 | /// FAddend is used to represent floating-point addend. An addend is |
| 126 | /// represented as <C, V>, where the V is a symbolic value, and C is a |
| 127 | /// constant coefficient. A constant addend is represented as <C, 0>. |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 128 | class FAddend { |
| 129 | public: |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 130 | FAddend() = default; |
| 131 | |
| 132 | void operator+=(const FAddend &T) { |
| 133 | assert((Val == T.Val) && "Symbolic-values disagree"); |
| 134 | Coeff += T.Coeff; |
| 135 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 136 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 137 | Value *getSymVal() const { return Val; } |
| 138 | const FAddendCoef &getCoef() const { return Coeff; } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 139 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 140 | bool isConstant() const { return Val == nullptr; } |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 141 | bool isZero() const { return Coeff.isZero(); } |
| 142 | |
Richard Trieu | 7a08381 | 2016-02-18 22:09:30 +0000 | [diff] [blame] | 143 | void set(short Coefficient, Value *V) { |
| 144 | Coeff.set(Coefficient); |
| 145 | Val = V; |
| 146 | } |
| 147 | void set(const APFloat &Coefficient, Value *V) { |
| 148 | Coeff.set(Coefficient); |
| 149 | Val = V; |
| 150 | } |
| 151 | void set(const ConstantFP *Coefficient, Value *V) { |
| 152 | Coeff.set(Coefficient->getValueAPF()); |
| 153 | Val = V; |
| 154 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 155 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 156 | void negate() { Coeff.negate(); } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 157 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 158 | /// Drill down the U-D chain one step to find the definition of V, and |
| 159 | /// try to break the definition into one or two addends. |
| 160 | static unsigned drillValueDownOneStep(Value* V, FAddend &A0, FAddend &A1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 161 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 162 | /// Similar to FAddend::drillDownOneStep() except that the value being |
| 163 | /// splitted is the addend itself. |
| 164 | unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1) const; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 165 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 166 | private: |
| 167 | void Scale(const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 168 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 169 | // This addend has the value of "Coeff * Val". |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 170 | Value *Val = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 171 | FAddendCoef Coeff; |
| 172 | }; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 173 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 174 | /// FAddCombine is the class for optimizing an unsafe fadd/fsub along |
| 175 | /// with its neighboring at most two instructions. |
| 176 | /// |
| 177 | class FAddCombine { |
| 178 | public: |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 179 | FAddCombine(InstCombiner::BuilderTy &B) : Builder(B) {} |
| 180 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 181 | Value *simplify(Instruction *FAdd); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 182 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 183 | private: |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 184 | using AddendVect = SmallVector<const FAddend *, 4>; |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 185 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 186 | Value *simplifyFAdd(AddendVect& V, unsigned InstrQuota); |
Shuxin Yang | 2eca602 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 187 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 188 | /// Convert given addend to a Value |
| 189 | Value *createAddendVal(const FAddend &A, bool& NeedNeg); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 190 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 191 | /// Return the number of instructions needed to emit the N-ary addition. |
| 192 | unsigned calcInstrNumber(const AddendVect& Vect); |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 193 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 194 | Value *createFSub(Value *Opnd0, Value *Opnd1); |
| 195 | Value *createFAdd(Value *Opnd0, Value *Opnd1); |
| 196 | Value *createFMul(Value *Opnd0, Value *Opnd1); |
| 197 | Value *createFNeg(Value *V); |
| 198 | Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 199 | void createInstPostProc(Instruction *NewInst, bool NoNumber = false); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 200 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 201 | // Debugging stuff are clustered here. |
| 202 | #ifndef NDEBUG |
| 203 | unsigned CreateInstrNum; |
| 204 | void initCreateInstNum() { CreateInstrNum = 0; } |
| 205 | void incCreateInstNum() { CreateInstrNum++; } |
| 206 | #else |
| 207 | void initCreateInstNum() {} |
| 208 | void incCreateInstNum() {} |
| 209 | #endif |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 210 | |
| 211 | InstCombiner::BuilderTy &Builder; |
| 212 | Instruction *Instr = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 213 | }; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 214 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 215 | } // end anonymous namespace |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 216 | |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | // |
| 219 | // Implementation of |
| 220 | // {FAddendCoef, FAddend, FAddition, FAddCombine}. |
| 221 | // |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | FAddendCoef::~FAddendCoef() { |
| 224 | if (BufHasFpVal) |
| 225 | getFpValPtr()->~APFloat(); |
| 226 | } |
| 227 | |
| 228 | void FAddendCoef::set(const APFloat& C) { |
| 229 | APFloat *P = getFpValPtr(); |
| 230 | |
| 231 | if (isInt()) { |
| 232 | // As the buffer is meanless byte stream, we cannot call |
| 233 | // APFloat::operator=(). |
| 234 | new(P) APFloat(C); |
| 235 | } else |
| 236 | *P = C; |
| 237 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 238 | IsFp = BufHasFpVal = true; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 241 | void FAddendCoef::convertToFpType(const fltSemantics &Sem) { |
| 242 | if (!isInt()) |
| 243 | return; |
| 244 | |
| 245 | APFloat *P = getFpValPtr(); |
| 246 | if (IntVal > 0) |
| 247 | new(P) APFloat(Sem, IntVal); |
| 248 | else { |
| 249 | new(P) APFloat(Sem, 0 - IntVal); |
| 250 | P->changeSign(); |
| 251 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 252 | IsFp = BufHasFpVal = true; |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | APFloat FAddendCoef::createAPFloatFromInt(const fltSemantics &Sem, int Val) { |
| 256 | if (Val >= 0) |
| 257 | return APFloat(Sem, Val); |
| 258 | |
| 259 | APFloat T(Sem, 0 - Val); |
| 260 | T.changeSign(); |
| 261 | |
| 262 | return T; |
| 263 | } |
| 264 | |
| 265 | void FAddendCoef::operator=(const FAddendCoef &That) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 266 | if (That.isInt()) |
| 267 | set(That.IntVal); |
| 268 | else |
| 269 | set(That.getFpVal()); |
| 270 | } |
| 271 | |
| 272 | void FAddendCoef::operator+=(const FAddendCoef &That) { |
| 273 | enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven; |
| 274 | if (isInt() == That.isInt()) { |
| 275 | if (isInt()) |
| 276 | IntVal += That.IntVal; |
| 277 | else |
| 278 | getFpVal().add(That.getFpVal(), RndMode); |
| 279 | return; |
| 280 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 281 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 282 | if (isInt()) { |
| 283 | const APFloat &T = That.getFpVal(); |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 284 | convertToFpType(T.getSemantics()); |
| 285 | getFpVal().add(T, RndMode); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 288 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 289 | APFloat &T = getFpVal(); |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 290 | T.add(createAPFloatFromInt(T.getSemantics(), That.IntVal), RndMode); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 293 | void FAddendCoef::operator*=(const FAddendCoef &That) { |
| 294 | if (That.isOne()) |
| 295 | return; |
| 296 | |
| 297 | if (That.isMinusOne()) { |
| 298 | negate(); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (isInt() && That.isInt()) { |
| 303 | int Res = IntVal * (int)That.IntVal; |
| 304 | assert(!insaneIntVal(Res) && "Insane int value"); |
| 305 | IntVal = Res; |
| 306 | return; |
| 307 | } |
| 308 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 309 | const fltSemantics &Semantic = |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 310 | isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics(); |
| 311 | |
| 312 | if (isInt()) |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 313 | convertToFpType(Semantic); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 314 | APFloat &F0 = getFpVal(); |
| 315 | |
| 316 | if (That.isInt()) |
Shuxin Yang | 389ed4b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 317 | F0.multiply(createAPFloatFromInt(Semantic, That.IntVal), |
| 318 | APFloat::rmNearestTiesToEven); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 319 | else |
| 320 | F0.multiply(That.getFpVal(), APFloat::rmNearestTiesToEven); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void FAddendCoef::negate() { |
| 324 | if (isInt()) |
| 325 | IntVal = 0 - IntVal; |
| 326 | else |
| 327 | getFpVal().changeSign(); |
| 328 | } |
| 329 | |
| 330 | Value *FAddendCoef::getValue(Type *Ty) const { |
| 331 | return isInt() ? |
| 332 | ConstantFP::get(Ty, float(IntVal)) : |
| 333 | ConstantFP::get(Ty->getContext(), getFpVal()); |
| 334 | } |
| 335 | |
| 336 | // The definition of <Val> Addends |
| 337 | // ========================================= |
| 338 | // A + B <1, A>, <1,B> |
| 339 | // A - B <1, A>, <1,B> |
| 340 | // 0 - B <-1, B> |
| 341 | // C * A, <C, A> |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 342 | // A + C <1, A> <C, NULL> |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 343 | // 0 +/- 0 <0, NULL> (corner case) |
| 344 | // |
| 345 | // Legend: A and B are not constant, C is constant |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 346 | unsigned FAddend::drillValueDownOneStep |
| 347 | (Value *Val, FAddend &Addend0, FAddend &Addend1) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 348 | Instruction *I = nullptr; |
| 349 | if (!Val || !(I = dyn_cast<Instruction>(Val))) |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | |
| 352 | unsigned Opcode = I->getOpcode(); |
| 353 | |
| 354 | if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) { |
| 355 | ConstantFP *C0, *C1; |
| 356 | Value *Opnd0 = I->getOperand(0); |
| 357 | Value *Opnd1 = I->getOperand(1); |
| 358 | if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 359 | Opnd0 = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 360 | |
| 361 | if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 362 | Opnd1 = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 363 | |
| 364 | if (Opnd0) { |
| 365 | if (!C0) |
| 366 | Addend0.set(1, Opnd0); |
| 367 | else |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 368 | Addend0.set(C0, nullptr); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (Opnd1) { |
| 372 | FAddend &Addend = Opnd0 ? Addend1 : Addend0; |
| 373 | if (!C1) |
| 374 | Addend.set(1, Opnd1); |
| 375 | else |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 376 | Addend.set(C1, nullptr); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 377 | if (Opcode == Instruction::FSub) |
| 378 | Addend.negate(); |
| 379 | } |
| 380 | |
| 381 | if (Opnd0 || Opnd1) |
| 382 | return Opnd0 && Opnd1 ? 2 : 1; |
| 383 | |
| 384 | // Both operands are zero. Weird! |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 385 | Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | |
| 389 | if (I->getOpcode() == Instruction::FMul) { |
| 390 | Value *V0 = I->getOperand(0); |
| 391 | Value *V1 = I->getOperand(1); |
| 392 | if (ConstantFP *C = dyn_cast<ConstantFP>(V0)) { |
| 393 | Addend0.set(C, V1); |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | if (ConstantFP *C = dyn_cast<ConstantFP>(V1)) { |
| 398 | Addend0.set(C, V0); |
| 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | // Try to break *this* addend into two addends. e.g. Suppose this addend is |
| 407 | // <2.3, V>, and V = X + Y, by calling this function, we obtain two addends, |
| 408 | // i.e. <2.3, X> and <2.3, Y>. |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 409 | unsigned FAddend::drillAddendDownOneStep |
| 410 | (FAddend &Addend0, FAddend &Addend1) const { |
| 411 | if (isConstant()) |
| 412 | return 0; |
| 413 | |
| 414 | unsigned BreakNum = FAddend::drillValueDownOneStep(Val, Addend0, Addend1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 415 | if (!BreakNum || Coeff.isOne()) |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 416 | return BreakNum; |
| 417 | |
| 418 | Addend0.Scale(Coeff); |
| 419 | |
| 420 | if (BreakNum == 2) |
| 421 | Addend1.Scale(Coeff); |
| 422 | |
| 423 | return BreakNum; |
| 424 | } |
| 425 | |
| 426 | Value *FAddCombine::simplify(Instruction *I) { |
Warren Ristow | 8b2f27c | 2018-04-14 19:18:28 +0000 | [diff] [blame] | 427 | assert(I->hasAllowReassoc() && I->hasNoSignedZeros() && |
| 428 | "Expected 'reassoc'+'nsz' instruction"); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 429 | |
| 430 | // Currently we are not able to handle vector type. |
| 431 | if (I->getType()->isVectorTy()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 432 | return nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 433 | |
| 434 | assert((I->getOpcode() == Instruction::FAdd || |
| 435 | I->getOpcode() == Instruction::FSub) && "Expect add/sub"); |
| 436 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 437 | // Save the instruction before calling other member-functions. |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 438 | Instr = I; |
| 439 | |
| 440 | FAddend Opnd0, Opnd1, Opnd0_0, Opnd0_1, Opnd1_0, Opnd1_1; |
| 441 | |
| 442 | unsigned OpndNum = FAddend::drillValueDownOneStep(I, Opnd0, Opnd1); |
| 443 | |
| 444 | // Step 1: Expand the 1st addend into Opnd0_0 and Opnd0_1. |
| 445 | unsigned Opnd0_ExpNum = 0; |
| 446 | unsigned Opnd1_ExpNum = 0; |
| 447 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 448 | if (!Opnd0.isConstant()) |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 449 | Opnd0_ExpNum = Opnd0.drillAddendDownOneStep(Opnd0_0, Opnd0_1); |
| 450 | |
| 451 | // Step 2: Expand the 2nd addend into Opnd1_0 and Opnd1_1. |
| 452 | if (OpndNum == 2 && !Opnd1.isConstant()) |
| 453 | Opnd1_ExpNum = Opnd1.drillAddendDownOneStep(Opnd1_0, Opnd1_1); |
| 454 | |
| 455 | // Step 3: Try to optimize Opnd0_0 + Opnd0_1 + Opnd1_0 + Opnd1_1 |
| 456 | if (Opnd0_ExpNum && Opnd1_ExpNum) { |
| 457 | AddendVect AllOpnds; |
| 458 | AllOpnds.push_back(&Opnd0_0); |
| 459 | AllOpnds.push_back(&Opnd1_0); |
| 460 | if (Opnd0_ExpNum == 2) |
| 461 | AllOpnds.push_back(&Opnd0_1); |
| 462 | if (Opnd1_ExpNum == 2) |
| 463 | AllOpnds.push_back(&Opnd1_1); |
| 464 | |
| 465 | // Compute instruction quota. We should save at least one instruction. |
| 466 | unsigned InstQuota = 0; |
| 467 | |
| 468 | Value *V0 = I->getOperand(0); |
| 469 | Value *V1 = I->getOperand(1); |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 470 | InstQuota = ((!isa<Constant>(V0) && V0->hasOneUse()) && |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 471 | (!isa<Constant>(V1) && V1->hasOneUse())) ? 2 : 1; |
| 472 | |
| 473 | if (Value *R = simplifyFAdd(AllOpnds, InstQuota)) |
| 474 | return R; |
| 475 | } |
| 476 | |
| 477 | if (OpndNum != 2) { |
| 478 | // The input instruction is : "I=0.0 +/- V". If the "V" were able to be |
| 479 | // splitted into two addends, say "V = X - Y", the instruction would have |
| 480 | // been optimized into "I = Y - X" in the previous steps. |
| 481 | // |
| 482 | const FAddendCoef &CE = Opnd0.getCoef(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 483 | return CE.isOne() ? Opnd0.getSymVal() : nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1] |
| 487 | if (Opnd1_ExpNum) { |
| 488 | AddendVect AllOpnds; |
| 489 | AllOpnds.push_back(&Opnd0); |
| 490 | AllOpnds.push_back(&Opnd1_0); |
| 491 | if (Opnd1_ExpNum == 2) |
| 492 | AllOpnds.push_back(&Opnd1_1); |
| 493 | |
| 494 | if (Value *R = simplifyFAdd(AllOpnds, 1)) |
| 495 | return R; |
| 496 | } |
| 497 | |
| 498 | // step 5: Try to optimize Opnd1 + Opnd0_0 [+ Opnd0_1] |
| 499 | if (Opnd0_ExpNum) { |
| 500 | AddendVect AllOpnds; |
| 501 | AllOpnds.push_back(&Opnd1); |
| 502 | AllOpnds.push_back(&Opnd0_0); |
| 503 | if (Opnd0_ExpNum == 2) |
| 504 | AllOpnds.push_back(&Opnd0_1); |
| 505 | |
| 506 | if (Value *R = simplifyFAdd(AllOpnds, 1)) |
| 507 | return R; |
| 508 | } |
| 509 | |
Sanjay Patel | dc185ee | 2018-08-12 15:48:26 +0000 | [diff] [blame] | 510 | return nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 514 | unsigned AddendNum = Addends.size(); |
| 515 | assert(AddendNum <= 4 && "Too many addends"); |
| 516 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 517 | // For saving intermediate results; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 518 | unsigned NextTmpIdx = 0; |
| 519 | FAddend TmpResult[3]; |
| 520 | |
| 521 | // Points to the constant addend of the resulting simplified expression. |
| 522 | // If the resulting expr has constant-addend, this constant-addend is |
| 523 | // desirable to reside at the top of the resulting expression tree. Placing |
| 524 | // constant close to supper-expr(s) will potentially reveal some optimization |
| 525 | // opportunities in super-expr(s). |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 526 | const FAddend *ConstAdd = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 527 | |
| 528 | // Simplified addends are placed <SimpVect>. |
| 529 | AddendVect SimpVect; |
| 530 | |
| 531 | // The outer loop works on one symbolic-value at a time. Suppose the input |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 532 | // addends are : <a1, x>, <b1, y>, <a2, x>, <c1, z>, <b2, y>, ... |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 533 | // The symbolic-values will be processed in this order: x, y, z. |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 534 | for (unsigned SymIdx = 0; SymIdx < AddendNum; SymIdx++) { |
| 535 | |
| 536 | const FAddend *ThisAddend = Addends[SymIdx]; |
| 537 | if (!ThisAddend) { |
| 538 | // This addend was processed before. |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | Value *Val = ThisAddend->getSymVal(); |
| 543 | unsigned StartIdx = SimpVect.size(); |
| 544 | SimpVect.push_back(ThisAddend); |
| 545 | |
| 546 | // The inner loop collects addends sharing same symbolic-value, and these |
| 547 | // addends will be later on folded into a single addend. Following above |
| 548 | // example, if the symbolic value "y" is being processed, the inner loop |
| 549 | // will collect two addends "<b1,y>" and "<b2,Y>". These two addends will |
| 550 | // be later on folded into "<b1+b2, y>". |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 551 | for (unsigned SameSymIdx = SymIdx + 1; |
| 552 | SameSymIdx < AddendNum; SameSymIdx++) { |
| 553 | const FAddend *T = Addends[SameSymIdx]; |
| 554 | if (T && T->getSymVal() == Val) { |
| 555 | // Set null such that next iteration of the outer loop will not process |
| 556 | // this addend again. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 557 | Addends[SameSymIdx] = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 558 | SimpVect.push_back(T); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // If multiple addends share same symbolic value, fold them together. |
| 563 | if (StartIdx + 1 != SimpVect.size()) { |
| 564 | FAddend &R = TmpResult[NextTmpIdx ++]; |
| 565 | R = *SimpVect[StartIdx]; |
| 566 | for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++) |
| 567 | R += *SimpVect[Idx]; |
| 568 | |
| 569 | // Pop all addends being folded and push the resulting folded addend. |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 570 | SimpVect.resize(StartIdx); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 571 | if (Val) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 572 | if (!R.isZero()) { |
| 573 | SimpVect.push_back(&R); |
| 574 | } |
| 575 | } else { |
| 576 | // Don't push constant addend at this time. It will be the last element |
| 577 | // of <SimpVect>. |
| 578 | ConstAdd = &R; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
Craig Topper | 5871321 | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 583 | assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) && |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 584 | "out-of-bound access"); |
| 585 | |
| 586 | if (ConstAdd) |
| 587 | SimpVect.push_back(ConstAdd); |
| 588 | |
| 589 | Value *Result; |
| 590 | if (!SimpVect.empty()) |
| 591 | Result = createNaryFAdd(SimpVect, InstrQuota); |
| 592 | else { |
| 593 | // The addition is folded to 0.0. |
| 594 | Result = ConstantFP::get(Instr->getType(), 0.0); |
| 595 | } |
| 596 | |
| 597 | return Result; |
| 598 | } |
| 599 | |
| 600 | Value *FAddCombine::createNaryFAdd |
| 601 | (const AddendVect &Opnds, unsigned InstrQuota) { |
| 602 | assert(!Opnds.empty() && "Expect at least one addend"); |
| 603 | |
| 604 | // Step 1: Check if the # of instructions needed exceeds the quota. |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 605 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 606 | unsigned InstrNeeded = calcInstrNumber(Opnds); |
| 607 | if (InstrNeeded > InstrQuota) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 608 | return nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 609 | |
| 610 | initCreateInstNum(); |
| 611 | |
| 612 | // step 2: Emit the N-ary addition. |
| 613 | // Note that at most three instructions are involved in Fadd-InstCombine: the |
| 614 | // addition in question, and at most two neighboring instructions. |
| 615 | // The resulting optimized addition should have at least one less instruction |
| 616 | // than the original addition expression tree. This implies that the resulting |
| 617 | // N-ary addition has at most two instructions, and we don't need to worry |
| 618 | // about tree-height when constructing the N-ary addition. |
| 619 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 620 | Value *LastVal = nullptr; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 621 | bool LastValNeedNeg = false; |
| 622 | |
| 623 | // Iterate the addends, creating fadd/fsub using adjacent two addends. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 624 | for (const FAddend *Opnd : Opnds) { |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 625 | bool NeedNeg; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 626 | Value *V = createAddendVal(*Opnd, NeedNeg); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 627 | if (!LastVal) { |
| 628 | LastVal = V; |
| 629 | LastValNeedNeg = NeedNeg; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (LastValNeedNeg == NeedNeg) { |
| 634 | LastVal = createFAdd(LastVal, V); |
| 635 | continue; |
| 636 | } |
| 637 | |
| 638 | if (LastValNeedNeg) |
| 639 | LastVal = createFSub(V, LastVal); |
| 640 | else |
| 641 | LastVal = createFSub(LastVal, V); |
| 642 | |
| 643 | LastValNeedNeg = false; |
| 644 | } |
| 645 | |
| 646 | if (LastValNeedNeg) { |
| 647 | LastVal = createFNeg(LastVal); |
| 648 | } |
| 649 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 650 | #ifndef NDEBUG |
| 651 | assert(CreateInstrNum == InstrNeeded && |
| 652 | "Inconsistent in instruction numbers"); |
| 653 | #endif |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 654 | |
| 655 | return LastVal; |
| 656 | } |
| 657 | |
Sanjay Patel | c242dbb | 2014-12-18 21:11:09 +0000 | [diff] [blame] | 658 | Value *FAddCombine::createFSub(Value *Opnd0, Value *Opnd1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 659 | Value *V = Builder.CreateFSub(Opnd0, Opnd1); |
Shuxin Yang | 2eca602 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 660 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 661 | createInstPostProc(I); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 662 | return V; |
| 663 | } |
| 664 | |
| 665 | Value *FAddCombine::createFNeg(Value *V) { |
Sanjay Patel | ea3c802 | 2014-12-19 16:44:08 +0000 | [diff] [blame] | 666 | Value *Zero = cast<Value>(ConstantFP::getZeroValueForNegation(V->getType())); |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 667 | Value *NewV = createFSub(Zero, V); |
| 668 | if (Instruction *I = dyn_cast<Instruction>(NewV)) |
| 669 | createInstPostProc(I, true); // fneg's don't receive instruction numbers. |
| 670 | return NewV; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Sanjay Patel | c242dbb | 2014-12-18 21:11:09 +0000 | [diff] [blame] | 673 | Value *FAddCombine::createFAdd(Value *Opnd0, Value *Opnd1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 674 | Value *V = Builder.CreateFAdd(Opnd0, Opnd1); |
Shuxin Yang | 2eca602 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 675 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 676 | createInstPostProc(I); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 677 | return V; |
| 678 | } |
| 679 | |
| 680 | Value *FAddCombine::createFMul(Value *Opnd0, Value *Opnd1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 681 | Value *V = Builder.CreateFMul(Opnd0, Opnd1); |
Shuxin Yang | 2eca602 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 682 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 683 | createInstPostProc(I); |
| 684 | return V; |
| 685 | } |
| 686 | |
Sanjay Patel | c242dbb | 2014-12-18 21:11:09 +0000 | [diff] [blame] | 687 | void FAddCombine::createInstPostProc(Instruction *NewInstr, bool NoNumber) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 688 | NewInstr->setDebugLoc(Instr->getDebugLoc()); |
| 689 | |
| 690 | // Keep track of the number of instruction created. |
Owen Anderson | 1664dc8 | 2014-01-20 07:44:53 +0000 | [diff] [blame] | 691 | if (!NoNumber) |
| 692 | incCreateInstNum(); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 693 | |
| 694 | // Propagate fast-math flags |
| 695 | NewInstr->setFastMathFlags(Instr->getFastMathFlags()); |
| 696 | } |
| 697 | |
| 698 | // Return the number of instruction needed to emit the N-ary addition. |
| 699 | // NOTE: Keep this function in sync with createAddendVal(). |
| 700 | unsigned FAddCombine::calcInstrNumber(const AddendVect &Opnds) { |
| 701 | unsigned OpndNum = Opnds.size(); |
| 702 | unsigned InstrNeeded = OpndNum - 1; |
| 703 | |
Jim Grosbach | bdbd734 | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 704 | // The number of addends in the form of "(-1)*x". |
| 705 | unsigned NegOpndNum = 0; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 706 | |
| 707 | // Adjust the number of instructions needed to emit the N-ary add. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 708 | for (const FAddend *Opnd : Opnds) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 709 | if (Opnd->isConstant()) |
| 710 | continue; |
| 711 | |
Matt Arsenault | 02907f3 | 2017-04-24 17:24:37 +0000 | [diff] [blame] | 712 | // The constant check above is really for a few special constant |
| 713 | // coefficients. |
| 714 | if (isa<UndefValue>(Opnd->getSymVal())) |
| 715 | continue; |
| 716 | |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 717 | const FAddendCoef &CE = Opnd->getCoef(); |
| 718 | if (CE.isMinusOne() || CE.isMinusTwo()) |
| 719 | NegOpndNum++; |
| 720 | |
| 721 | // Let the addend be "c * x". If "c == +/-1", the value of the addend |
| 722 | // is immediately available; otherwise, it needs exactly one instruction |
| 723 | // to evaluate the value. |
| 724 | if (!CE.isMinusOne() && !CE.isOne()) |
| 725 | InstrNeeded++; |
| 726 | } |
| 727 | if (NegOpndNum == OpndNum) |
| 728 | InstrNeeded++; |
| 729 | return InstrNeeded; |
| 730 | } |
| 731 | |
| 732 | // Input Addend Value NeedNeg(output) |
| 733 | // ================================================================ |
| 734 | // Constant C C false |
| 735 | // <+/-1, V> V coefficient is -1 |
| 736 | // <2/-2, V> "fadd V, V" coefficient is -2 |
| 737 | // <C, V> "fmul V, C" false |
| 738 | // |
| 739 | // NOTE: Keep this function in sync with FAddCombine::calcInstrNumber. |
Sanjay Patel | c242dbb | 2014-12-18 21:11:09 +0000 | [diff] [blame] | 740 | Value *FAddCombine::createAddendVal(const FAddend &Opnd, bool &NeedNeg) { |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 741 | const FAddendCoef &Coeff = Opnd.getCoef(); |
| 742 | |
| 743 | if (Opnd.isConstant()) { |
| 744 | NeedNeg = false; |
| 745 | return Coeff.getValue(Instr->getType()); |
| 746 | } |
| 747 | |
| 748 | Value *OpndVal = Opnd.getSymVal(); |
| 749 | |
| 750 | if (Coeff.isMinusOne() || Coeff.isOne()) { |
| 751 | NeedNeg = Coeff.isMinusOne(); |
| 752 | return OpndVal; |
| 753 | } |
| 754 | |
| 755 | if (Coeff.isTwo() || Coeff.isMinusTwo()) { |
| 756 | NeedNeg = Coeff.isMinusTwo(); |
| 757 | return createFAdd(OpndVal, OpndVal); |
| 758 | } |
| 759 | |
| 760 | NeedNeg = false; |
| 761 | return createFMul(OpndVal, Coeff.getValue(Instr->getType())); |
| 762 | } |
| 763 | |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 764 | // Checks if any operand is negative and we can convert add to sub. |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 765 | // This function checks for following negative patterns |
| 766 | // ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C)) |
| 767 | // ADD(XOR(AND(Z, C), C), 1) == NEG(OR(Z, ~C)) |
| 768 | // XOR(AND(Z, C), (C + 1)) == NEG(OR(Z, ~C)) if C is even |
Benjamin Kramer | 6cbe670 | 2014-07-07 14:47:51 +0000 | [diff] [blame] | 769 | static Value *checkForNegativeOperand(BinaryOperator &I, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 770 | InstCombiner::BuilderTy &Builder) { |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 771 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 772 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 773 | // This function creates 2 instructions to replace ADD, we need at least one |
| 774 | // of LHS or RHS to have one use to ensure benefit in transform. |
| 775 | if (!LHS->hasOneUse() && !RHS->hasOneUse()) |
| 776 | return nullptr; |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 777 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 778 | Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
| 779 | const APInt *C1 = nullptr, *C2 = nullptr; |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 780 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 781 | // if ONE is on other side, swap |
| 782 | if (match(RHS, m_Add(m_Value(X), m_One()))) |
| 783 | std::swap(LHS, RHS); |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 784 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 785 | if (match(LHS, m_Add(m_Value(X), m_One()))) { |
| 786 | // if XOR on other side, swap |
| 787 | if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1)))) |
| 788 | std::swap(X, RHS); |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 789 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 790 | if (match(X, m_Xor(m_Value(Y), m_APInt(C1)))) { |
| 791 | // X = XOR(Y, C1), Y = OR(Z, C2), C2 = NOT(C1) ==> X == NOT(AND(Z, C1)) |
| 792 | // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, AND(Z, C1)) |
| 793 | if (match(Y, m_Or(m_Value(Z), m_APInt(C2))) && (*C2 == ~(*C1))) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 794 | Value *NewAnd = Builder.CreateAnd(Z, *C1); |
| 795 | return Builder.CreateSub(RHS, NewAnd, "sub"); |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 796 | } else if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && (*C1 == *C2)) { |
| 797 | // X = XOR(Y, C1), Y = AND(Z, C2), C2 == C1 ==> X == NOT(OR(Z, ~C1)) |
| 798 | // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, OR(Z, ~C1)) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 799 | Value *NewOr = Builder.CreateOr(Z, ~(*C1)); |
| 800 | return Builder.CreateSub(RHS, NewOr, "sub"); |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 804 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 805 | // Restore LHS and RHS |
| 806 | LHS = I.getOperand(0); |
| 807 | RHS = I.getOperand(1); |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 808 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 809 | // if XOR is on other side, swap |
| 810 | if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1)))) |
| 811 | std::swap(LHS, RHS); |
| 812 | |
| 813 | // C2 is ODD |
| 814 | // LHS = XOR(Y, C1), Y = AND(Z, C2), C1 == (C2 + 1) => LHS == NEG(OR(Z, ~C2)) |
| 815 | // ADD(LHS, RHS) == SUB(RHS, OR(Z, ~C2)) |
| 816 | if (match(LHS, m_Xor(m_Value(Y), m_APInt(C1)))) |
| 817 | if (C1->countTrailingZeros() == 0) |
| 818 | if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && *C1 == (*C2 + 1)) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 819 | Value *NewOr = Builder.CreateOr(Z, ~(*C2)); |
| 820 | return Builder.CreateSub(RHS, NewOr, "sub"); |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 821 | } |
| 822 | return nullptr; |
| 823 | } |
| 824 | |
Sanjay Patel | 4a47f5f | 2019-02-28 19:05:26 +0000 | [diff] [blame] | 825 | /// Wrapping flags may allow combining constants separated by an extend. |
| 826 | static Instruction *foldNoWrapAdd(BinaryOperator &Add, |
| 827 | InstCombiner::BuilderTy &Builder) { |
| 828 | Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1); |
| 829 | Type *Ty = Add.getType(); |
| 830 | Constant *Op1C; |
| 831 | if (!match(Op1, m_Constant(Op1C))) |
| 832 | return nullptr; |
| 833 | |
| 834 | // Try this match first because it results in an add in the narrow type. |
| 835 | // (zext (X +nuw C2)) + C1 --> zext (X + (C2 + trunc(C1))) |
| 836 | Value *X; |
| 837 | const APInt *C1, *C2; |
| 838 | if (match(Op1, m_APInt(C1)) && |
| 839 | match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) && |
| 840 | C1->isNegative() && C1->sge(-C2->sext(C1->getBitWidth()))) { |
| 841 | Constant *NewC = |
| 842 | ConstantInt::get(X->getType(), *C2 + C1->trunc(C2->getBitWidth())); |
| 843 | return new ZExtInst(Builder.CreateNUWAdd(X, NewC), Ty); |
| 844 | } |
| 845 | |
| 846 | // More general combining of constants in the wide type. |
| 847 | // (sext (X +nsw NarrowC)) + C --> (sext X) + (sext(NarrowC) + C) |
| 848 | Constant *NarrowC; |
| 849 | if (match(Op0, m_OneUse(m_SExt(m_NSWAdd(m_Value(X), m_Constant(NarrowC)))))) { |
| 850 | Constant *WideC = ConstantExpr::getSExt(NarrowC, Ty); |
| 851 | Constant *NewC = ConstantExpr::getAdd(WideC, Op1C); |
| 852 | Value *WideX = Builder.CreateSExt(X, Ty); |
| 853 | return BinaryOperator::CreateAdd(WideX, NewC); |
| 854 | } |
| 855 | // (zext (X +nuw NarrowC)) + C --> (zext X) + (zext(NarrowC) + C) |
| 856 | if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_Constant(NarrowC)))))) { |
| 857 | Constant *WideC = ConstantExpr::getZExt(NarrowC, Ty); |
| 858 | Constant *NewC = ConstantExpr::getAdd(WideC, Op1C); |
| 859 | Value *WideX = Builder.CreateZExt(X, Ty); |
| 860 | return BinaryOperator::CreateAdd(WideX, NewC); |
| 861 | } |
| 862 | |
| 863 | return nullptr; |
| 864 | } |
| 865 | |
Sanjay Patel | 8d810fe | 2017-10-13 16:43:58 +0000 | [diff] [blame] | 866 | Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) { |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 867 | Value *Op0 = Add.getOperand(0), *Op1 = Add.getOperand(1); |
Sanjay Patel | 2150651 | 2017-10-13 16:29:38 +0000 | [diff] [blame] | 868 | Constant *Op1C; |
| 869 | if (!match(Op1, m_Constant(Op1C))) |
| 870 | return nullptr; |
| 871 | |
Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 872 | if (Instruction *NV = foldBinOpIntoSelectOrPhi(Add)) |
Sanjay Patel | 8d810fe | 2017-10-13 16:43:58 +0000 | [diff] [blame] | 873 | return NV; |
| 874 | |
Roman Lebedev | 886c4ef | 2019-05-31 09:47:04 +0000 | [diff] [blame] | 875 | Value *X; |
| 876 | Constant *Op00C; |
| 877 | |
| 878 | // add (sub C1, X), C2 --> sub (add C1, C2), X |
| 879 | if (match(Op0, m_Sub(m_Constant(Op00C), m_Value(X)))) |
| 880 | return BinaryOperator::CreateSub(ConstantExpr::getAdd(Op00C, Op1C), X); |
| 881 | |
| 882 | Value *Y; |
Sanjay Patel | 818b253 | 2018-07-28 16:48:44 +0000 | [diff] [blame] | 883 | |
| 884 | // add (sub X, Y), -1 --> add (not Y), X |
| 885 | if (match(Op0, m_OneUse(m_Sub(m_Value(X), m_Value(Y)))) && |
| 886 | match(Op1, m_AllOnes())) |
| 887 | return BinaryOperator::CreateAdd(Builder.CreateNot(Y), X); |
| 888 | |
Sanjay Patel | f0242de | 2017-10-13 20:29:11 +0000 | [diff] [blame] | 889 | // zext(bool) + C -> bool ? C + 1 : C |
| 890 | if (match(Op0, m_ZExt(m_Value(X))) && |
| 891 | X->getType()->getScalarSizeInBits() == 1) |
Sanjay Patel | 76ed9ea | 2017-10-13 17:00:47 +0000 | [diff] [blame] | 892 | return SelectInst::Create(X, AddOne(Op1C), Op1); |
Roman Lebedev | 796fa66 | 2019-12-05 20:44:22 +0300 | [diff] [blame] | 893 | // sext(bool) + C -> bool ? C - 1 : C |
| 894 | if (match(Op0, m_SExt(m_Value(X))) && |
| 895 | X->getType()->getScalarSizeInBits() == 1) |
| 896 | return SelectInst::Create(X, SubOne(Op1C), Op1); |
Sanjay Patel | 2150651 | 2017-10-13 16:29:38 +0000 | [diff] [blame] | 897 | |
Sanjay Patel | f0242de | 2017-10-13 20:29:11 +0000 | [diff] [blame] | 898 | // ~X + C --> (C-1) - X |
| 899 | if (match(Op0, m_Not(m_Value(X)))) |
| 900 | return BinaryOperator::CreateSub(SubOne(Op1C), X); |
| 901 | |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 902 | const APInt *C; |
| 903 | if (!match(Op1, m_APInt(C))) |
| 904 | return nullptr; |
| 905 | |
Robert Lougher | 8681ef8 | 2019-05-07 19:36:41 +0000 | [diff] [blame] | 906 | // (X | C2) + C --> (X | C2) ^ C2 iff (C2 == -C) |
| 907 | const APInt *C2; |
| 908 | if (match(Op0, m_Or(m_Value(), m_APInt(C2))) && *C2 == -*C) |
| 909 | return BinaryOperator::CreateXor(Op0, ConstantInt::get(Add.getType(), *C2)); |
| 910 | |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 911 | if (C->isSignMask()) { |
| 912 | // If wrapping is not allowed, then the addition must set the sign bit: |
| 913 | // X + (signmask) --> X | signmask |
| 914 | if (Add.hasNoSignedWrap() || Add.hasNoUnsignedWrap()) |
| 915 | return BinaryOperator::CreateOr(Op0, Op1); |
| 916 | |
| 917 | // If wrapping is allowed, then the addition flips the sign bit of LHS: |
| 918 | // X + (signmask) --> X ^ signmask |
| 919 | return BinaryOperator::CreateXor(Op0, Op1); |
| 920 | } |
| 921 | |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 922 | // Is this add the last step in a convoluted sext? |
| 923 | // add(zext(xor i16 X, -32768), -32768) --> sext X |
Sanjay Patel | 76ed9ea | 2017-10-13 17:00:47 +0000 | [diff] [blame] | 924 | Type *Ty = Add.getType(); |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 925 | if (match(Op0, m_ZExt(m_Xor(m_Value(X), m_APInt(C2)))) && |
| 926 | C2->isMinSignedValue() && C2->sext(Ty->getScalarSizeInBits()) == *C) |
| 927 | return CastInst::Create(Instruction::SExt, X, Ty); |
| 928 | |
Sanjay Patel | 2f3ead7 | 2017-06-25 14:15:28 +0000 | [diff] [blame] | 929 | if (C->isOneValue() && Op0->hasOneUse()) { |
| 930 | // add (sext i1 X), 1 --> zext (not X) |
| 931 | // TODO: The smallest IR representation is (select X, 0, 1), and that would |
| 932 | // not require the one-use check. But we need to remove a transform in |
| 933 | // visitSelect and make sure that IR value tracking for select is equal or |
| 934 | // better than for these ops. |
| 935 | if (match(Op0, m_SExt(m_Value(X))) && |
| 936 | X->getType()->getScalarSizeInBits() == 1) |
| 937 | return new ZExtInst(Builder.CreateNot(X), Ty); |
| 938 | |
| 939 | // Shifts and add used to flip and mask off the low bit: |
| 940 | // add (ashr (shl i32 X, 31), 31), 1 --> and (not X), 1 |
| 941 | const APInt *C3; |
| 942 | if (match(Op0, m_AShr(m_Shl(m_Value(X), m_APInt(C2)), m_APInt(C3))) && |
| 943 | C2 == C3 && *C2 == Ty->getScalarSizeInBits() - 1) { |
| 944 | Value *NotX = Builder.CreateNot(X); |
| 945 | return BinaryOperator::CreateAnd(NotX, ConstantInt::get(Ty, 1)); |
| 946 | } |
Sanjay Patel | 2e069f2 | 2017-05-10 13:56:52 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 949 | return nullptr; |
| 950 | } |
| 951 | |
Sanjoy Das | 6f1937b | 2018-04-26 20:52:28 +0000 | [diff] [blame] | 952 | // Matches multiplication expression Op * C where C is a constant. Returns the |
| 953 | // constant value in C and the other operand in Op. Returns true if such a |
| 954 | // match is found. |
| 955 | static bool MatchMul(Value *E, Value *&Op, APInt &C) { |
| 956 | const APInt *AI; |
| 957 | if (match(E, m_Mul(m_Value(Op), m_APInt(AI)))) { |
| 958 | C = *AI; |
| 959 | return true; |
| 960 | } |
| 961 | if (match(E, m_Shl(m_Value(Op), m_APInt(AI)))) { |
| 962 | C = APInt(AI->getBitWidth(), 1); |
| 963 | C <<= *AI; |
| 964 | return true; |
| 965 | } |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | // Matches remainder expression Op % C where C is a constant. Returns the |
| 970 | // constant value in C and the other operand in Op. Returns the signedness of |
| 971 | // the remainder operation in IsSigned. Returns true if such a match is |
| 972 | // found. |
| 973 | static bool MatchRem(Value *E, Value *&Op, APInt &C, bool &IsSigned) { |
| 974 | const APInt *AI; |
| 975 | IsSigned = false; |
| 976 | if (match(E, m_SRem(m_Value(Op), m_APInt(AI)))) { |
| 977 | IsSigned = true; |
| 978 | C = *AI; |
| 979 | return true; |
| 980 | } |
| 981 | if (match(E, m_URem(m_Value(Op), m_APInt(AI)))) { |
| 982 | C = *AI; |
| 983 | return true; |
| 984 | } |
| 985 | if (match(E, m_And(m_Value(Op), m_APInt(AI))) && (*AI + 1).isPowerOf2()) { |
| 986 | C = *AI + 1; |
| 987 | return true; |
| 988 | } |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | // Matches division expression Op / C with the given signedness as indicated |
| 993 | // by IsSigned, where C is a constant. Returns the constant value in C and the |
| 994 | // other operand in Op. Returns true if such a match is found. |
| 995 | static bool MatchDiv(Value *E, Value *&Op, APInt &C, bool IsSigned) { |
| 996 | const APInt *AI; |
| 997 | if (IsSigned && match(E, m_SDiv(m_Value(Op), m_APInt(AI)))) { |
| 998 | C = *AI; |
| 999 | return true; |
| 1000 | } |
| 1001 | if (!IsSigned) { |
| 1002 | if (match(E, m_UDiv(m_Value(Op), m_APInt(AI)))) { |
| 1003 | C = *AI; |
| 1004 | return true; |
| 1005 | } |
| 1006 | if (match(E, m_LShr(m_Value(Op), m_APInt(AI)))) { |
| 1007 | C = APInt(AI->getBitWidth(), 1); |
| 1008 | C <<= *AI; |
| 1009 | return true; |
| 1010 | } |
| 1011 | } |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
| 1015 | // Returns whether C0 * C1 with the given signedness overflows. |
| 1016 | static bool MulWillOverflow(APInt &C0, APInt &C1, bool IsSigned) { |
| 1017 | bool overflow; |
| 1018 | if (IsSigned) |
| 1019 | (void)C0.smul_ov(C1, overflow); |
| 1020 | else |
| 1021 | (void)C0.umul_ov(C1, overflow); |
| 1022 | return overflow; |
| 1023 | } |
| 1024 | |
| 1025 | // Simplifies X % C0 + (( X / C0 ) % C1) * C0 to X % (C0 * C1), where (C0 * C1) |
| 1026 | // does not overflow. |
| 1027 | Value *InstCombiner::SimplifyAddWithRemainder(BinaryOperator &I) { |
| 1028 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 1029 | Value *X, *MulOpV; |
| 1030 | APInt C0, MulOpC; |
| 1031 | bool IsSigned; |
| 1032 | // Match I = X % C0 + MulOpV * C0 |
| 1033 | if (((MatchRem(LHS, X, C0, IsSigned) && MatchMul(RHS, MulOpV, MulOpC)) || |
| 1034 | (MatchRem(RHS, X, C0, IsSigned) && MatchMul(LHS, MulOpV, MulOpC))) && |
| 1035 | C0 == MulOpC) { |
| 1036 | Value *RemOpV; |
| 1037 | APInt C1; |
| 1038 | bool Rem2IsSigned; |
| 1039 | // Match MulOpC = RemOpV % C1 |
| 1040 | if (MatchRem(MulOpV, RemOpV, C1, Rem2IsSigned) && |
| 1041 | IsSigned == Rem2IsSigned) { |
| 1042 | Value *DivOpV; |
| 1043 | APInt DivOpC; |
| 1044 | // Match RemOpV = X / C0 |
| 1045 | if (MatchDiv(RemOpV, DivOpV, DivOpC, IsSigned) && X == DivOpV && |
| 1046 | C0 == DivOpC && !MulWillOverflow(C0, C1, IsSigned)) { |
| 1047 | Value *NewDivisor = |
| 1048 | ConstantInt::get(X->getType()->getContext(), C0 * C1); |
| 1049 | return IsSigned ? Builder.CreateSRem(X, NewDivisor, "srem") |
| 1050 | : Builder.CreateURem(X, NewDivisor, "urem"); |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | return nullptr; |
| 1056 | } |
| 1057 | |
Roman Lebedev | cbf8446 | 2018-06-06 19:38:27 +0000 | [diff] [blame] | 1058 | /// Fold |
| 1059 | /// (1 << NBits) - 1 |
| 1060 | /// Into: |
| 1061 | /// ~(-(1 << NBits)) |
| 1062 | /// Because a 'not' is better for bit-tracking analysis and other transforms |
| 1063 | /// than an 'add'. The new shl is always nsw, and is nuw if old `and` was. |
| 1064 | static Instruction *canonicalizeLowbitMask(BinaryOperator &I, |
| 1065 | InstCombiner::BuilderTy &Builder) { |
| 1066 | Value *NBits; |
| 1067 | if (!match(&I, m_Add(m_OneUse(m_Shl(m_One(), m_Value(NBits))), m_AllOnes()))) |
| 1068 | return nullptr; |
| 1069 | |
| 1070 | Constant *MinusOne = Constant::getAllOnesValue(NBits->getType()); |
| 1071 | Value *NotMask = Builder.CreateShl(MinusOne, NBits, "notmask"); |
| 1072 | // Be wary of constant folding. |
| 1073 | if (auto *BOp = dyn_cast<BinaryOperator>(NotMask)) { |
| 1074 | // Always NSW. But NUW propagates from `add`. |
| 1075 | BOp->setHasNoSignedWrap(); |
| 1076 | BOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 1077 | } |
| 1078 | |
| 1079 | return BinaryOperator::CreateNot(NotMask, I.getName()); |
| 1080 | } |
| 1081 | |
Sanjay Patel | 81e8d76 | 2019-03-26 17:50:08 +0000 | [diff] [blame] | 1082 | static Instruction *foldToUnsignedSaturatedAdd(BinaryOperator &I) { |
| 1083 | assert(I.getOpcode() == Instruction::Add && "Expecting add instruction"); |
| 1084 | Type *Ty = I.getType(); |
| 1085 | auto getUAddSat = [&]() { |
| 1086 | return Intrinsic::getDeclaration(I.getModule(), Intrinsic::uadd_sat, Ty); |
| 1087 | }; |
| 1088 | |
| 1089 | // add (umin X, ~Y), Y --> uaddsat X, Y |
| 1090 | Value *X, *Y; |
| 1091 | if (match(&I, m_c_Add(m_c_UMin(m_Value(X), m_Not(m_Value(Y))), |
| 1092 | m_Deferred(Y)))) |
| 1093 | return CallInst::Create(getUAddSat(), { X, Y }); |
| 1094 | |
| 1095 | // add (umin X, ~C), C --> uaddsat X, C |
| 1096 | const APInt *C, *NotC; |
| 1097 | if (match(&I, m_Add(m_UMin(m_Value(X), m_APInt(NotC)), m_APInt(C))) && |
| 1098 | *C == ~*NotC) |
| 1099 | return CallInst::Create(getUAddSat(), { X, ConstantInt::get(Ty, *C) }); |
| 1100 | |
| 1101 | return nullptr; |
| 1102 | } |
| 1103 | |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1104 | Instruction * |
| 1105 | InstCombiner::canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract( |
| 1106 | BinaryOperator &I) { |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1107 | assert((I.getOpcode() == Instruction::Add || |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1108 | I.getOpcode() == Instruction::Or || |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1109 | I.getOpcode() == Instruction::Sub) && |
Roman Lebedev | 9948fac | 2019-10-21 08:21:54 +0000 | [diff] [blame] | 1110 | "Expecting add/or/sub instruction"); |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1111 | |
| 1112 | // We have a subtraction/addition between a (potentially truncated) *logical* |
| 1113 | // right-shift of X and a "select". |
| 1114 | Value *X, *Select; |
| 1115 | Instruction *LowBitsToSkip, *Extract; |
| 1116 | if (!match(&I, m_c_BinOp(m_TruncOrSelf(m_CombineAnd( |
| 1117 | m_LShr(m_Value(X), m_Instruction(LowBitsToSkip)), |
| 1118 | m_Instruction(Extract))), |
| 1119 | m_Value(Select)))) |
| 1120 | return nullptr; |
| 1121 | |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1122 | // `add`/`or` is commutative; but for `sub`, "select" *must* be on RHS. |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1123 | if (I.getOpcode() == Instruction::Sub && I.getOperand(1) != Select) |
| 1124 | return nullptr; |
| 1125 | |
| 1126 | Type *XTy = X->getType(); |
| 1127 | bool HadTrunc = I.getType() != XTy; |
| 1128 | |
| 1129 | // If there was a truncation of extracted value, then we'll need to produce |
| 1130 | // one extra instruction, so we need to ensure one instruction will go away. |
| 1131 | if (HadTrunc && !match(&I, m_c_BinOp(m_OneUse(m_Value()), m_Value()))) |
| 1132 | return nullptr; |
| 1133 | |
| 1134 | // Extraction should extract high NBits bits, with shift amount calculated as: |
| 1135 | // low bits to skip = shift bitwidth - high bits to extract |
| 1136 | // The shift amount itself may be extended, and we need to look past zero-ext |
| 1137 | // when matching NBits, that will matter for matching later. |
| 1138 | Constant *C; |
| 1139 | Value *NBits; |
| 1140 | if (!match( |
| 1141 | LowBitsToSkip, |
| 1142 | m_ZExtOrSelf(m_Sub(m_Constant(C), m_ZExtOrSelf(m_Value(NBits))))) || |
| 1143 | !match(C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, |
| 1144 | APInt(C->getType()->getScalarSizeInBits(), |
| 1145 | X->getType()->getScalarSizeInBits())))) |
| 1146 | return nullptr; |
| 1147 | |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1148 | // Sign-extending value can be zero-extended if we `sub`tract it, |
| 1149 | // or sign-extended otherwise. |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1150 | auto SkipExtInMagic = [&I](Value *&V) { |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1151 | if (I.getOpcode() == Instruction::Sub) |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1152 | match(V, m_ZExtOrSelf(m_Value(V))); |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1153 | else |
| 1154 | match(V, m_SExtOrSelf(m_Value(V))); |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1155 | }; |
| 1156 | |
| 1157 | // Now, finally validate the sign-extending magic. |
| 1158 | // `select` itself may be appropriately extended, look past that. |
| 1159 | SkipExtInMagic(Select); |
| 1160 | |
| 1161 | ICmpInst::Predicate Pred; |
| 1162 | const APInt *Thr; |
| 1163 | Value *SignExtendingValue, *Zero; |
| 1164 | bool ShouldSignext; |
Roman Lebedev | 9948fac | 2019-10-21 08:21:54 +0000 | [diff] [blame] | 1165 | // It must be a select between two values we will later establish to be a |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1166 | // sign-extending value and a zero constant. The condition guarding the |
| 1167 | // sign-extension must be based on a sign bit of the same X we had in `lshr`. |
| 1168 | if (!match(Select, m_Select(m_ICmp(Pred, m_Specific(X), m_APInt(Thr)), |
| 1169 | m_Value(SignExtendingValue), m_Value(Zero))) || |
| 1170 | !isSignBitCheck(Pred, *Thr, ShouldSignext)) |
| 1171 | return nullptr; |
| 1172 | |
| 1173 | // icmp-select pair is commutative. |
| 1174 | if (!ShouldSignext) |
| 1175 | std::swap(SignExtendingValue, Zero); |
| 1176 | |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1177 | // If we should not perform sign-extension then we must add/or/subtract zero. |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1178 | if (!match(Zero, m_Zero())) |
| 1179 | return nullptr; |
| 1180 | // Otherwise, it should be some constant, left-shifted by the same NBits we |
| 1181 | // had in `lshr`. Said left-shift can also be appropriately extended. |
| 1182 | // Again, we must look past zero-ext when looking for NBits. |
| 1183 | SkipExtInMagic(SignExtendingValue); |
| 1184 | Constant *SignExtendingValueBaseConstant; |
| 1185 | if (!match(SignExtendingValue, |
| 1186 | m_Shl(m_Constant(SignExtendingValueBaseConstant), |
| 1187 | m_ZExtOrSelf(m_Specific(NBits))))) |
| 1188 | return nullptr; |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1189 | // If we `sub`, then the constant should be one, else it should be all-ones. |
| 1190 | if (I.getOpcode() == Instruction::Sub |
| 1191 | ? !match(SignExtendingValueBaseConstant, m_One()) |
| 1192 | : !match(SignExtendingValueBaseConstant, m_AllOnes())) |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1193 | return nullptr; |
| 1194 | |
| 1195 | auto *NewAShr = BinaryOperator::CreateAShr(X, LowBitsToSkip, |
| 1196 | Extract->getName() + ".sext"); |
| 1197 | NewAShr->copyIRFlags(Extract); // Preserve `exact`-ness. |
| 1198 | if (!HadTrunc) |
| 1199 | return NewAShr; |
| 1200 | |
| 1201 | Builder.Insert(NewAShr); |
| 1202 | return TruncInst::CreateTruncOrBitCast(NewAShr, I.getType()); |
| 1203 | } |
| 1204 | |
Dinesh Dwivedi | adc0773 | 2014-06-27 07:47:35 +0000 | [diff] [blame] | 1205 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1206 | if (Value *V = SimplifyAddInst(I.getOperand(0), I.getOperand(1), |
| 1207 | I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), |
| 1208 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1209 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 1210 | |
Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1211 | if (SimplifyAssociativeOrCommutative(I)) |
| 1212 | return &I; |
| 1213 | |
Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1214 | if (Instruction *X = foldVectorBinop(I)) |
Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1215 | return X; |
| 1216 | |
Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1217 | // (A*B)+(A*C) -> A*(B+C) etc |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 1218 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1219 | return replaceInstUsesWith(I, V); |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 1220 | |
Sanjay Patel | 8d810fe | 2017-10-13 16:43:58 +0000 | [diff] [blame] | 1221 | if (Instruction *X = foldAddWithConstant(I)) |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 1222 | return X; |
Sanjay Patel | 53c5c3d | 2017-02-18 22:20:09 +0000 | [diff] [blame] | 1223 | |
Sanjay Patel | 4a47f5f | 2019-02-28 19:05:26 +0000 | [diff] [blame] | 1224 | if (Instruction *X = foldNoWrapAdd(I, Builder)) |
| 1225 | return X; |
| 1226 | |
Sanjay Patel | 4133d4a | 2017-05-10 00:07:16 +0000 | [diff] [blame] | 1227 | // FIXME: This should be moved into the above helper function to allow these |
Sanjay Patel | 2150651 | 2017-10-13 16:29:38 +0000 | [diff] [blame] | 1228 | // transforms for general constant or constant splat vectors. |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1229 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Sanjay Patel | 2118952 | 2017-10-13 18:32:53 +0000 | [diff] [blame] | 1230 | Type *Ty = I.getType(); |
Sanjay Patel | 79acd2a | 2016-07-16 18:29:26 +0000 | [diff] [blame] | 1231 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1232 | Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr; |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1233 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Sanjay Patel | 2118952 | 2017-10-13 18:32:53 +0000 | [diff] [blame] | 1234 | unsigned TySizeBits = Ty->getScalarSizeInBits(); |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1235 | const APInt &RHSVal = CI->getValue(); |
Eli Friedman | a2cc287 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 1236 | unsigned ExtendAmt = 0; |
| 1237 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 1238 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 1239 | if (XorRHS->getValue() == -RHSVal) { |
| 1240 | if (RHSVal.isPowerOf2()) |
| 1241 | ExtendAmt = TySizeBits - RHSVal.logBase2() - 1; |
| 1242 | else if (XorRHS->getValue().isPowerOf2()) |
| 1243 | ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1244 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1245 | |
Eli Friedman | a2cc287 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 1246 | if (ExtendAmt) { |
| 1247 | APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1248 | if (!MaskedValueIsZero(XorLHS, Mask, 0, &I)) |
Eli Friedman | a2cc287 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 1249 | ExtendAmt = 0; |
| 1250 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1251 | |
Eli Friedman | a2cc287 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 1252 | if (ExtendAmt) { |
Sanjay Patel | 2118952 | 2017-10-13 18:32:53 +0000 | [diff] [blame] | 1253 | Constant *ShAmt = ConstantInt::get(Ty, ExtendAmt); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1254 | Value *NewShl = Builder.CreateShl(XorLHS, ShAmt, "sext"); |
Eli Friedman | a2cc287 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 1255 | return BinaryOperator::CreateAShr(NewShl, ShAmt); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1256 | } |
Benjamin Kramer | b16bd77 | 2011-12-24 17:31:53 +0000 | [diff] [blame] | 1257 | |
| 1258 | // If this is a xor that was canonicalized from a sub, turn it back into |
| 1259 | // a sub and fuse this add with it. |
| 1260 | if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) { |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1261 | KnownBits LHSKnown = computeKnownBits(XorLHS, 0, &I); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1262 | if ((XorRHS->getValue() | LHSKnown.Zero).isAllOnesValue()) |
Benjamin Kramer | b16bd77 | 2011-12-24 17:31:53 +0000 | [diff] [blame] | 1263 | return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI), |
| 1264 | XorLHS); |
| 1265 | } |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1266 | // (X + signmask) + C could have gotten canonicalized to (X^signmask) + C, |
| 1267 | // transform them into (X + (signmask ^ C)) |
| 1268 | if (XorRHS->getValue().isSignMask()) |
Craig Topper | eafbd57 | 2015-12-21 01:02:28 +0000 | [diff] [blame] | 1269 | return BinaryOperator::CreateAdd(XorLHS, |
| 1270 | ConstantExpr::getXor(XorRHS, CI)); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
Sanjay Patel | 2118952 | 2017-10-13 18:32:53 +0000 | [diff] [blame] | 1274 | if (Ty->isIntOrIntVectorTy(1)) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1275 | return BinaryOperator::CreateXor(LHS, RHS); |
| 1276 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1277 | // X + X --> X << 1 |
Chris Lattner | d406764 | 2011-02-17 20:55:29 +0000 | [diff] [blame] | 1278 | if (LHS == RHS) { |
Sanjay Patel | 2118952 | 2017-10-13 18:32:53 +0000 | [diff] [blame] | 1279 | auto *Shl = BinaryOperator::CreateShl(LHS, ConstantInt::get(Ty, 1)); |
| 1280 | Shl->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 1281 | Shl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 1282 | return Shl; |
Chris Lattner | 5592071 | 2011-02-17 02:23:02 +0000 | [diff] [blame] | 1283 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1284 | |
Sanjay Patel | b869f76 | 2017-10-13 21:28:50 +0000 | [diff] [blame] | 1285 | Value *A, *B; |
| 1286 | if (match(LHS, m_Neg(m_Value(A)))) { |
| 1287 | // -A + -B --> -(A + B) |
| 1288 | if (match(RHS, m_Neg(m_Value(B)))) |
| 1289 | return BinaryOperator::CreateNeg(Builder.CreateAdd(A, B)); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1290 | |
Sanjay Patel | b869f76 | 2017-10-13 21:28:50 +0000 | [diff] [blame] | 1291 | // -A + B --> B - A |
| 1292 | return BinaryOperator::CreateSub(RHS, A); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | // A + -B --> A - B |
Sanjay Patel | b869f76 | 2017-10-13 21:28:50 +0000 | [diff] [blame] | 1296 | if (match(RHS, m_Neg(m_Value(B)))) |
| 1297 | return BinaryOperator::CreateSub(LHS, B); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1298 | |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 1299 | if (Value *V = checkForNegativeOperand(I, Builder)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1300 | return replaceInstUsesWith(I, V); |
Dinesh Dwivedi | 562fd75 | 2014-06-19 10:36:52 +0000 | [diff] [blame] | 1301 | |
Gil Rapaport | da2e2ca | 2018-06-26 05:31:18 +0000 | [diff] [blame] | 1302 | // (A + 1) + ~B --> A - B |
| 1303 | // ~B + (A + 1) --> A - B |
Roman Lebedev | 04d3d3b | 2019-07-01 15:55:24 +0000 | [diff] [blame] | 1304 | // (~B + A) + 1 --> A - B |
| 1305 | // (A + ~B) + 1 --> A - B |
| 1306 | if (match(&I, m_c_BinOp(m_Add(m_Value(A), m_One()), m_Not(m_Value(B)))) || |
| 1307 | match(&I, m_BinOp(m_c_Add(m_Not(m_Value(B)), m_Value(A)), m_One()))) |
Gil Rapaport | da2e2ca | 2018-06-26 05:31:18 +0000 | [diff] [blame] | 1308 | return BinaryOperator::CreateSub(A, B); |
| 1309 | |
Sanjoy Das | 6f1937b | 2018-04-26 20:52:28 +0000 | [diff] [blame] | 1310 | // X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1) |
| 1311 | if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V); |
| 1312 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1313 | // A+B --> A|B iff A and B have no bits set in common. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1314 | if (haveNoCommonBitsSet(LHS, RHS, DL, &AC, &I, &DT)) |
Jingyue Wu | ca32190 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 1315 | return BinaryOperator::CreateOr(LHS, RHS); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1316 | |
Sanjay Patel | 79acd2a | 2016-07-16 18:29:26 +0000 | [diff] [blame] | 1317 | // FIXME: We already did a check for ConstantInt RHS above this. |
| 1318 | // FIXME: Is this pattern covered by another fold? No regression tests fail on |
| 1319 | // removal. |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1320 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1321 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1322 | Value *X; |
| 1323 | ConstantInt *C2; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1324 | if (LHS->hasOneUse() && |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1325 | match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) && |
| 1326 | CRHS->getValue() == (CRHS->getValue() & C2->getValue())) { |
| 1327 | // See if all bits from the first bit set in the Add RHS up are included |
| 1328 | // in the mask. First, get the rightmost bit. |
| 1329 | const APInt &AddRHSV = CRHS->getValue(); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1331 | // Form a mask of all bits from the lowest bit added through the top. |
| 1332 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1334 | // See if the and mask includes all of these bits. |
| 1335 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1337 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1338 | // Okay, the xform is safe. Insert the new add pronto. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1339 | Value *NewAdd = Builder.CreateAdd(X, CRHS, LHS->getName()); |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1340 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1341 | } |
| 1342 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | // add (select X 0 (sub n A)) A --> select X A n |
| 1346 | { |
| 1347 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 1348 | Value *A = RHS; |
| 1349 | if (!SI) { |
| 1350 | SI = dyn_cast<SelectInst>(RHS); |
| 1351 | A = LHS; |
| 1352 | } |
| 1353 | if (SI && SI->hasOneUse()) { |
| 1354 | Value *TV = SI->getTrueValue(); |
| 1355 | Value *FV = SI->getFalseValue(); |
| 1356 | Value *N; |
| 1357 | |
| 1358 | // Can we fold the add into the argument of the select? |
| 1359 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1360 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1361 | // Fold the add into the true select value. |
| 1362 | return SelectInst::Create(SI->getCondition(), N, A); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1364 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1365 | // Fold the add into the false select value. |
| 1366 | return SelectInst::Create(SI->getCondition(), A, N); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Sanjay Patel | 90a3634 | 2018-09-14 22:23:35 +0000 | [diff] [blame] | 1370 | if (Instruction *Ext = narrowMathIfNoOverflow(I)) |
Sanjay Patel | 2426eb4 | 2018-09-14 20:40:46 +0000 | [diff] [blame] | 1371 | return Ext; |
David Majnemer | a1cfd7c | 2016-12-30 00:28:58 +0000 | [diff] [blame] | 1372 | |
David Majnemer | ab07f00 | 2014-08-11 22:32:02 +0000 | [diff] [blame] | 1373 | // (add (xor A, B) (and A, B)) --> (or A, B) |
Sanjay Patel | 28b3aa3 | 2017-10-13 20:12:21 +0000 | [diff] [blame] | 1374 | // (add (and A, B) (xor A, B)) --> (or A, B) |
Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 1375 | if (match(&I, m_c_BinOp(m_Xor(m_Value(A), m_Value(B)), |
| 1376 | m_c_And(m_Deferred(A), m_Deferred(B))))) |
Sanjay Patel | 28b3aa3 | 2017-10-13 20:12:21 +0000 | [diff] [blame] | 1377 | return BinaryOperator::CreateOr(A, B); |
Chad Rosier | 7813dce | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 1378 | |
David Majnemer | ab07f00 | 2014-08-11 22:32:02 +0000 | [diff] [blame] | 1379 | // (add (or A, B) (and A, B)) --> (add A, B) |
Sanjay Patel | 28b3aa3 | 2017-10-13 20:12:21 +0000 | [diff] [blame] | 1380 | // (add (and A, B) (or A, B)) --> (add A, B) |
Roman Lebedev | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 1381 | if (match(&I, m_c_BinOp(m_Or(m_Value(A), m_Value(B)), |
| 1382 | m_c_And(m_Deferred(A), m_Deferred(B))))) { |
Sanjay Patel | 28b3aa3 | 2017-10-13 20:12:21 +0000 | [diff] [blame] | 1383 | I.setOperand(0, A); |
| 1384 | I.setOperand(1, B); |
| 1385 | return &I; |
David Majnemer | ab07f00 | 2014-08-11 22:32:02 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 1388 | // TODO(jingyue): Consider willNotOverflowSignedAdd and |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 1389 | // willNotOverflowUnsignedAdd to reduce the number of invocations of |
Jingyue Wu | 33bd53d | 2014-06-17 00:42:07 +0000 | [diff] [blame] | 1390 | // computeKnownBits. |
Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1391 | bool Changed = false; |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 1392 | if (!I.hasNoSignedWrap() && willNotOverflowSignedAdd(LHS, RHS, I)) { |
Rafael Espindola | d1a2c2d | 2014-06-02 22:01:04 +0000 | [diff] [blame] | 1393 | Changed = true; |
| 1394 | I.setHasNoSignedWrap(true); |
| 1395 | } |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 1396 | if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedAdd(LHS, RHS, I)) { |
Jingyue Wu | 33bd53d | 2014-06-17 00:42:07 +0000 | [diff] [blame] | 1397 | Changed = true; |
| 1398 | I.setHasNoUnsignedWrap(true); |
| 1399 | } |
Rafael Espindola | d1a2c2d | 2014-06-02 22:01:04 +0000 | [diff] [blame] | 1400 | |
Roman Lebedev | cbf8446 | 2018-06-06 19:38:27 +0000 | [diff] [blame] | 1401 | if (Instruction *V = canonicalizeLowbitMask(I, Builder)) |
| 1402 | return V; |
| 1403 | |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1404 | if (Instruction *V = |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 1405 | canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I)) |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 1406 | return V; |
| 1407 | |
Sanjay Patel | 81e8d76 | 2019-03-26 17:50:08 +0000 | [diff] [blame] | 1408 | if (Instruction *SatAdd = foldToUnsignedSaturatedAdd(I)) |
| 1409 | return SatAdd; |
| 1410 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1411 | return Changed ? &I : nullptr; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Sanjay Patel | c229cfe | 2019-07-26 11:19:18 +0000 | [diff] [blame] | 1414 | /// Eliminate an op from a linear interpolation (lerp) pattern. |
| 1415 | static Instruction *factorizeLerp(BinaryOperator &I, |
| 1416 | InstCombiner::BuilderTy &Builder) { |
| 1417 | Value *X, *Y, *Z; |
| 1418 | if (!match(&I, m_c_FAdd(m_OneUse(m_c_FMul(m_Value(Y), |
| 1419 | m_OneUse(m_FSub(m_FPOne(), |
| 1420 | m_Value(Z))))), |
| 1421 | m_OneUse(m_c_FMul(m_Value(X), m_Deferred(Z)))))) |
| 1422 | return nullptr; |
| 1423 | |
| 1424 | // (Y * (1.0 - Z)) + (X * Z) --> Y + Z * (X - Y) [8 commuted variants] |
| 1425 | Value *XY = Builder.CreateFSubFMF(X, Y, &I); |
| 1426 | Value *MulZ = Builder.CreateFMulFMF(Z, XY, &I); |
| 1427 | return BinaryOperator::CreateFAddFMF(Y, MulZ, &I); |
| 1428 | } |
| 1429 | |
Sanjay Patel | dc185ee | 2018-08-12 15:48:26 +0000 | [diff] [blame] | 1430 | /// Factor a common operand out of fadd/fsub of fmul/fdiv. |
| 1431 | static Instruction *factorizeFAddFSub(BinaryOperator &I, |
| 1432 | InstCombiner::BuilderTy &Builder) { |
| 1433 | assert((I.getOpcode() == Instruction::FAdd || |
| 1434 | I.getOpcode() == Instruction::FSub) && "Expecting fadd/fsub"); |
| 1435 | assert(I.hasAllowReassoc() && I.hasNoSignedZeros() && |
| 1436 | "FP factorization requires FMF"); |
Sanjay Patel | c229cfe | 2019-07-26 11:19:18 +0000 | [diff] [blame] | 1437 | |
| 1438 | if (Instruction *Lerp = factorizeLerp(I, Builder)) |
| 1439 | return Lerp; |
| 1440 | |
Sanjay Patel | dc185ee | 2018-08-12 15:48:26 +0000 | [diff] [blame] | 1441 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1442 | Value *X, *Y, *Z; |
| 1443 | bool IsFMul; |
| 1444 | if ((match(Op0, m_OneUse(m_FMul(m_Value(X), m_Value(Z)))) && |
| 1445 | match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z))))) || |
| 1446 | (match(Op0, m_OneUse(m_FMul(m_Value(Z), m_Value(X)))) && |
| 1447 | match(Op1, m_OneUse(m_c_FMul(m_Value(Y), m_Specific(Z)))))) |
| 1448 | IsFMul = true; |
| 1449 | else if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Z)))) && |
| 1450 | match(Op1, m_OneUse(m_FDiv(m_Value(Y), m_Specific(Z))))) |
| 1451 | IsFMul = false; |
| 1452 | else |
| 1453 | return nullptr; |
| 1454 | |
| 1455 | // (X * Z) + (Y * Z) --> (X + Y) * Z |
| 1456 | // (X * Z) - (Y * Z) --> (X - Y) * Z |
| 1457 | // (X / Z) + (Y / Z) --> (X + Y) / Z |
| 1458 | // (X / Z) - (Y / Z) --> (X - Y) / Z |
| 1459 | bool IsFAdd = I.getOpcode() == Instruction::FAdd; |
| 1460 | Value *XY = IsFAdd ? Builder.CreateFAddFMF(X, Y, &I) |
| 1461 | : Builder.CreateFSubFMF(X, Y, &I); |
| 1462 | |
| 1463 | // Bail out if we just created a denormal constant. |
| 1464 | // TODO: This is copied from a previous implementation. Is it necessary? |
| 1465 | const APFloat *C; |
| 1466 | if (match(XY, m_APFloat(C)) && !C->isNormal()) |
| 1467 | return nullptr; |
| 1468 | |
| 1469 | return IsFMul ? BinaryOperator::CreateFMulFMF(XY, Z, &I) |
| 1470 | : BinaryOperator::CreateFDivFMF(XY, Z, &I); |
| 1471 | } |
| 1472 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1473 | Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1474 | if (Value *V = SimplifyFAddInst(I.getOperand(0), I.getOperand(1), |
| 1475 | I.getFastMathFlags(), |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 1476 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1477 | return replaceInstUsesWith(I, V); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1478 | |
Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1479 | if (SimplifyAssociativeOrCommutative(I)) |
| 1480 | return &I; |
| 1481 | |
Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1482 | if (Instruction *X = foldVectorBinop(I)) |
Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1483 | return X; |
| 1484 | |
Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 1485 | if (Instruction *FoldedFAdd = foldBinOpIntoSelectOrPhi(I)) |
| 1486 | return FoldedFAdd; |
Michael Ilseman | e2754dc | 2012-12-14 22:08:26 +0000 | [diff] [blame] | 1487 | |
Sanjay Patel | 1170daa | 2018-04-16 14:13:57 +0000 | [diff] [blame] | 1488 | // (-X) + Y --> Y - X |
Sanjay Patel | 5483f42 | 2019-07-29 13:20:46 +0000 | [diff] [blame] | 1489 | Value *X, *Y; |
| 1490 | if (match(&I, m_c_FAdd(m_FNeg(m_Value(X)), m_Value(Y)))) |
| 1491 | return BinaryOperator::CreateFSubFMF(Y, X, &I); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1492 | |
Sanjay Patel | e9ee7b4 | 2019-07-29 13:50:25 +0000 | [diff] [blame] | 1493 | // Similar to above, but look through fmul/fdiv for the negated term. |
| 1494 | // (-X * Y) + Z --> Z - (X * Y) [4 commuted variants] |
| 1495 | Value *Z; |
| 1496 | if (match(&I, m_c_FAdd(m_OneUse(m_c_FMul(m_FNeg(m_Value(X)), m_Value(Y))), |
| 1497 | m_Value(Z)))) { |
| 1498 | Value *XY = Builder.CreateFMulFMF(X, Y, &I); |
| 1499 | return BinaryOperator::CreateFSubFMF(Z, XY, &I); |
| 1500 | } |
| 1501 | // (-X / Y) + Z --> Z - (X / Y) [2 commuted variants] |
| 1502 | // (X / -Y) + Z --> Z - (X / Y) [2 commuted variants] |
| 1503 | if (match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_FNeg(m_Value(X)), m_Value(Y))), |
| 1504 | m_Value(Z))) || |
| 1505 | match(&I, m_c_FAdd(m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))), |
| 1506 | m_Value(Z)))) { |
| 1507 | Value *XY = Builder.CreateFDivFMF(X, Y, &I); |
| 1508 | return BinaryOperator::CreateFSubFMF(Z, XY, &I); |
| 1509 | } |
| 1510 | |
Dan Gohman | 6f34abd | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1511 | // Check for (fadd double (sitofp x), y), see if we can merge this into an |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1512 | // integer add followed by a promotion. |
Sanjay Patel | 5483f42 | 2019-07-29 13:20:46 +0000 | [diff] [blame] | 1513 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1514 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
Artur Pilipenko | 4cc6130 | 2017-03-21 11:32:15 +0000 | [diff] [blame] | 1515 | Value *LHSIntVal = LHSConv->getOperand(0); |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1516 | Type *FPType = LHSConv->getType(); |
| 1517 | |
| 1518 | // TODO: This check is overly conservative. In many cases known bits |
| 1519 | // analysis can tell us that the result of the addition has less significant |
| 1520 | // bits than the integer type can hold. |
| 1521 | auto IsValidPromotion = [](Type *FTy, Type *ITy) { |
Artur Pilipenko | 0632bdc | 2017-04-22 07:24:52 +0000 | [diff] [blame] | 1522 | Type *FScalarTy = FTy->getScalarType(); |
| 1523 | Type *IScalarTy = ITy->getScalarType(); |
| 1524 | |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1525 | // Do we have enough bits in the significand to represent the result of |
| 1526 | // the integer addition? |
| 1527 | unsigned MaxRepresentableBits = |
Artur Pilipenko | 0632bdc | 2017-04-22 07:24:52 +0000 | [diff] [blame] | 1528 | APFloat::semanticsPrecision(FScalarTy->getFltSemantics()); |
| 1529 | return IScalarTy->getIntegerBitWidth() <= MaxRepresentableBits; |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1530 | }; |
Artur Pilipenko | 4cc6130 | 2017-03-21 11:32:15 +0000 | [diff] [blame] | 1531 | |
Dan Gohman | 6f34abd | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1532 | // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1533 | // ... if the constant fits in the integer value. This is useful for things |
| 1534 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 1535 | // requires a constant pool load, and generally allows the add to be better |
| 1536 | // instcombined. |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1537 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 1538 | if (IsValidPromotion(FPType, LHSIntVal->getType())) { |
| 1539 | Constant *CI = |
| 1540 | ConstantExpr::getFPToSI(CFP, LHSIntVal->getType()); |
| 1541 | if (LHSConv->hasOneUse() && |
| 1542 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 1543 | willNotOverflowSignedAdd(LHSIntVal, CI, I)) { |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1544 | // Insert the new integer add. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1545 | Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, CI, "addconv"); |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1546 | return new SIToFPInst(NewAdd, I.getType()); |
| 1547 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1548 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1549 | |
Dan Gohman | 6f34abd | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1550 | // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1551 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
Artur Pilipenko | 4cc6130 | 2017-03-21 11:32:15 +0000 | [diff] [blame] | 1552 | Value *RHSIntVal = RHSConv->getOperand(0); |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1553 | // It's enough to check LHS types only because we require int types to |
| 1554 | // be the same for this transform. |
| 1555 | if (IsValidPromotion(FPType, LHSIntVal->getType())) { |
| 1556 | // Only do this if x/y have the same type, if at least one of them has a |
| 1557 | // single use (so we don't increase the number of int->fp conversions), |
| 1558 | // and if the integer add will not overflow. |
| 1559 | if (LHSIntVal->getType() == RHSIntVal->getType() && |
| 1560 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 1561 | willNotOverflowSignedAdd(LHSIntVal, RHSIntVal, I)) { |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1562 | // Insert the new integer add. |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1563 | Value *NewAdd = Builder.CreateNSWAdd(LHSIntVal, RHSIntVal, "addconv"); |
Artur Pilipenko | 134d94f | 2017-04-21 18:45:25 +0000 | [diff] [blame] | 1564 | return new SIToFPInst(NewAdd, I.getType()); |
| 1565 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1569 | |
Quentin Colombet | aa103b3 | 2017-09-20 17:32:16 +0000 | [diff] [blame] | 1570 | // Handle specials cases for FAdd with selects feeding the operation |
| 1571 | if (Value *V = SimplifySelectsFeedingBinaryOp(I, LHS, RHS)) |
| 1572 | return replaceInstUsesWith(I, V); |
Jean-Luc Duprat | 3e4fc3e | 2013-05-06 16:55:50 +0000 | [diff] [blame] | 1573 | |
Warren Ristow | 8b2f27c | 2018-04-14 19:18:28 +0000 | [diff] [blame] | 1574 | if (I.hasAllowReassoc() && I.hasNoSignedZeros()) { |
Sanjay Patel | dc185ee | 2018-08-12 15:48:26 +0000 | [diff] [blame] | 1575 | if (Instruction *F = factorizeFAddFSub(I, Builder)) |
| 1576 | return F; |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 1577 | if (Value *V = FAddCombine(Builder).simplify(&I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1578 | return replaceInstUsesWith(I, V); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Sanjay Patel | 70043b7 | 2018-07-13 01:18:07 +0000 | [diff] [blame] | 1581 | return nullptr; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1584 | /// Optimize pointer differences into the same array into a size. Consider: |
| 1585 | /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer |
| 1586 | /// operands to the ptrtoint instructions for the LHS/RHS of the subtract. |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1587 | Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS, |
Nikita Popov | 0e322c8 | 2020-01-01 11:11:05 +0100 | [diff] [blame^] | 1588 | Type *Ty, bool IsNUW) { |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1589 | // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize |
| 1590 | // this. |
| 1591 | bool Swapped = false; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1592 | GEPOperator *GEP1 = nullptr, *GEP2 = nullptr; |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1594 | // For now we require one side to be the base pointer "A" or a constant |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1595 | // GEP derived from it. |
| 1596 | if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1597 | // (gep X, ...) - X |
| 1598 | if (LHSGEP->getOperand(0) == RHS) { |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1599 | GEP1 = LHSGEP; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1600 | Swapped = false; |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1601 | } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) { |
| 1602 | // (gep X, ...) - (gep X, ...) |
| 1603 | if (LHSGEP->getOperand(0)->stripPointerCasts() == |
| 1604 | RHSGEP->getOperand(0)->stripPointerCasts()) { |
| 1605 | GEP2 = RHSGEP; |
| 1606 | GEP1 = LHSGEP; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1607 | Swapped = false; |
| 1608 | } |
| 1609 | } |
| 1610 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1611 | |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1612 | if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) { |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1613 | // X - (gep X, ...) |
| 1614 | if (RHSGEP->getOperand(0) == LHS) { |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1615 | GEP1 = RHSGEP; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1616 | Swapped = true; |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1617 | } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { |
| 1618 | // (gep X, ...) - (gep X, ...) |
| 1619 | if (RHSGEP->getOperand(0)->stripPointerCasts() == |
| 1620 | LHSGEP->getOperand(0)->stripPointerCasts()) { |
| 1621 | GEP2 = LHSGEP; |
| 1622 | GEP1 = RHSGEP; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1623 | Swapped = true; |
| 1624 | } |
| 1625 | } |
| 1626 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1627 | |
Hiroshi Yamauchi | 6085521 | 2017-07-27 18:27:11 +0000 | [diff] [blame] | 1628 | if (!GEP1) |
| 1629 | // No GEP found. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1630 | return nullptr; |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1631 | |
Hiroshi Yamauchi | 6085521 | 2017-07-27 18:27:11 +0000 | [diff] [blame] | 1632 | if (GEP2) { |
| 1633 | // (gep X, ...) - (gep X, ...) |
| 1634 | // |
| 1635 | // Avoid duplicating the arithmetic if there are more than one non-constant |
| 1636 | // indices between the two GEPs and either GEP has a non-constant index and |
| 1637 | // multiple users. If zero non-constant index, the result is a constant and |
| 1638 | // there is no duplication. If one non-constant index, the result is an add |
| 1639 | // or sub with a constant, which is no larger than the original code, and |
| 1640 | // there's no duplicated arithmetic, even if either GEP has multiple |
| 1641 | // users. If more than one non-constant indices combined, as long as the GEP |
| 1642 | // with at least one non-constant index doesn't have multiple users, there |
| 1643 | // is no duplication. |
| 1644 | unsigned NumNonConstantIndices1 = GEP1->countNonConstantIndices(); |
| 1645 | unsigned NumNonConstantIndices2 = GEP2->countNonConstantIndices(); |
| 1646 | if (NumNonConstantIndices1 + NumNonConstantIndices2 > 1 && |
| 1647 | ((NumNonConstantIndices1 > 0 && !GEP1->hasOneUse()) || |
| 1648 | (NumNonConstantIndices2 > 0 && !GEP2->hasOneUse()))) { |
| 1649 | return nullptr; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1653 | // Emit the offset of the GEP and an intptr_t. |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1654 | Value *Result = EmitGEPOffset(GEP1); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1655 | |
Nikita Popov | 0e322c8 | 2020-01-01 11:11:05 +0100 | [diff] [blame^] | 1656 | // If this is a single inbounds GEP and the original sub was nuw, |
| 1657 | // then the final multiplication is also nuw. We match an extra add zero |
| 1658 | // here, because that's what EmitGEPOffset() generates. |
| 1659 | Instruction *I; |
| 1660 | if (IsNUW && !GEP2 && !Swapped && GEP1->isInBounds() && |
| 1661 | match(Result, m_Add(m_Instruction(I), m_Zero())) && |
| 1662 | I->getOpcode() == Instruction::Mul) |
| 1663 | I->setHasNoUnsignedWrap(); |
| 1664 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1665 | // If we had a constant expression GEP on the other side offsetting the |
| 1666 | // pointer, subtract it from the offset we have. |
Benjamin Kramer | 7746eb6 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1667 | if (GEP2) { |
| 1668 | Value *Offset = EmitGEPOffset(GEP2); |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1669 | Result = Builder.CreateSub(Result, Offset); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1670 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1671 | |
| 1672 | // If we have p - gep(p, ...) then we have to negate the result. |
| 1673 | if (Swapped) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1674 | Result = Builder.CreateNeg(Result, "diff.neg"); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1675 | |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1676 | return Builder.CreateIntCast(Result, Ty, true); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1679 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1680 | if (Value *V = SimplifySubInst(I.getOperand(0), I.getOperand(1), |
| 1681 | I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), |
| 1682 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1683 | return replaceInstUsesWith(I, V); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1684 | |
Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 1685 | if (Instruction *X = foldVectorBinop(I)) |
Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 1686 | return X; |
| 1687 | |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 1688 | // (A*B)-(A*C) -> A*(B-C) etc |
| 1689 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 1690 | return replaceInstUsesWith(I, V); |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 1691 | |
David Majnemer | a92687d | 2014-07-31 04:49:29 +0000 | [diff] [blame] | 1692 | // If this is a 'B = x-(-A)', change to B = x+A. |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 1693 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1694 | if (Value *V = dyn_castNegVal(Op1)) { |
| 1695 | BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); |
David Majnemer | a92687d | 2014-07-31 04:49:29 +0000 | [diff] [blame] | 1696 | |
| 1697 | if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) { |
| 1698 | assert(BO->getOpcode() == Instruction::Sub && |
| 1699 | "Expected a subtraction operator!"); |
| 1700 | if (BO->hasNoSignedWrap() && I.hasNoSignedWrap()) |
| 1701 | Res->setHasNoSignedWrap(true); |
David Majnemer | 0e6c986 | 2014-08-22 16:41:23 +0000 | [diff] [blame] | 1702 | } else { |
| 1703 | if (cast<Constant>(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap()) |
| 1704 | Res->setHasNoSignedWrap(true); |
David Majnemer | a92687d | 2014-07-31 04:49:29 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1707 | return Res; |
| 1708 | } |
| 1709 | |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 1710 | if (I.getType()->isIntOrIntVectorTy(1)) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1711 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Replace (-1 - A) with (~A). |
| 1714 | if (match(Op0, m_AllOnes())) |
| 1715 | return BinaryOperator::CreateNot(Op1); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1716 | |
Sanjay Patel | 1a8d5c3 | 2018-03-03 17:53:25 +0000 | [diff] [blame] | 1717 | // (~X) - (~Y) --> Y - X |
| 1718 | Value *X, *Y; |
| 1719 | if (match(Op0, m_Not(m_Value(X))) && match(Op1, m_Not(m_Value(Y)))) |
| 1720 | return BinaryOperator::CreateSub(Y, X); |
| 1721 | |
Sanjay Patel | 577c705 | 2018-07-29 18:13:16 +0000 | [diff] [blame] | 1722 | // (X + -1) - Y --> ~Y + X |
| 1723 | if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes())))) |
| 1724 | return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X); |
| 1725 | |
| 1726 | // Y - (X + 1) --> ~X + Y |
| 1727 | if (match(Op1, m_OneUse(m_Add(m_Value(X), m_One())))) |
| 1728 | return BinaryOperator::CreateAdd(Builder.CreateNot(X), Op0); |
| 1729 | |
Roman Lebedev | 9f0c839 | 2019-07-03 09:41:50 +0000 | [diff] [blame] | 1730 | // Y - ~X --> (X + 1) + Y |
| 1731 | if (match(Op1, m_OneUse(m_Not(m_Value(X))))) { |
| 1732 | return BinaryOperator::CreateAdd( |
| 1733 | Builder.CreateAdd(Op0, ConstantInt::get(I.getType(), 1)), X); |
| 1734 | } |
| 1735 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1736 | if (Constant *C = dyn_cast<Constant>(Op0)) { |
Sanjay Patel | 3bd957b | 2018-06-03 16:35:26 +0000 | [diff] [blame] | 1737 | bool IsNegate = match(C, m_ZeroInt()); |
Sanjay Patel | b6404a8 | 2017-12-06 21:22:57 +0000 | [diff] [blame] | 1738 | Value *X; |
Sanjay Patel | 3bd957b | 2018-06-03 16:35:26 +0000 | [diff] [blame] | 1739 | if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { |
| 1740 | // 0 - (zext bool) --> sext bool |
| 1741 | // C - (zext bool) --> bool ? C - 1 : C |
| 1742 | if (IsNegate) |
| 1743 | return CastInst::CreateSExtOrBitCast(X, I.getType()); |
Sanjay Patel | b6404a8 | 2017-12-06 21:22:57 +0000 | [diff] [blame] | 1744 | return SelectInst::Create(X, SubOne(C), C); |
Sanjay Patel | 3bd957b | 2018-06-03 16:35:26 +0000 | [diff] [blame] | 1745 | } |
| 1746 | if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { |
| 1747 | // 0 - (sext bool) --> zext bool |
| 1748 | // C - (sext bool) --> bool ? C + 1 : C |
| 1749 | if (IsNegate) |
| 1750 | return CastInst::CreateZExtOrBitCast(X, I.getType()); |
| 1751 | return SelectInst::Create(X, AddOne(C), C); |
| 1752 | } |
Sanjay Patel | b6404a8 | 2017-12-06 21:22:57 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1754 | // C - ~X == X + (1+C) |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1755 | if (match(Op1, m_Not(m_Value(X)))) |
| 1756 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
| 1757 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1758 | // Try to fold constant sub into select arguments. |
| 1759 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1760 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1761 | return R; |
| 1762 | |
Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 1763 | // Try to fold constant sub into PHI values. |
| 1764 | if (PHINode *PN = dyn_cast<PHINode>(Op1)) |
| 1765 | if (Instruction *R = foldOpIntoPhi(I, PN)) |
| 1766 | return R; |
| 1767 | |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1768 | Constant *C2; |
Roman Lebedev | 39390d8 | 2019-05-31 09:47:16 +0000 | [diff] [blame] | 1769 | |
| 1770 | // C-(C2-X) --> X+(C-C2) |
| 1771 | if (match(Op1, m_Sub(m_Constant(C2), m_Value(X)))) |
| 1772 | return BinaryOperator::CreateAdd(X, ConstantExpr::getSub(C, C2)); |
| 1773 | |
| 1774 | // C-(X+C2) --> (C-C2)-X |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1775 | if (match(Op1, m_Add(m_Value(X), m_Constant(C2)))) |
| 1776 | return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X); |
Benjamin Kramer | 72196f3 | 2014-01-19 15:24:22 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1779 | const APInt *Op0C; |
| 1780 | if (match(Op0, m_APInt(Op0C))) { |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1781 | |
Craig Topper | 73ba1c8 | 2017-06-07 07:40:37 +0000 | [diff] [blame] | 1782 | if (Op0C->isNullValue()) { |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1783 | Value *Op1Wide; |
| 1784 | match(Op1, m_TruncOrSelf(m_Value(Op1Wide))); |
| 1785 | bool HadTrunc = Op1Wide != Op1; |
| 1786 | bool NoTruncOrTruncIsOneUse = !HadTrunc || Op1->hasOneUse(); |
| 1787 | unsigned BitWidth = Op1Wide->getType()->getScalarSizeInBits(); |
| 1788 | |
David Majnemer | 72a643d | 2014-11-03 05:53:55 +0000 | [diff] [blame] | 1789 | Value *X; |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1790 | const APInt *ShAmt; |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1791 | // -(X >>u 31) -> (X >>s 31) |
| 1792 | if (NoTruncOrTruncIsOneUse && |
| 1793 | match(Op1Wide, m_LShr(m_Value(X), m_APInt(ShAmt))) && |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1794 | *ShAmt == BitWidth - 1) { |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1795 | Value *ShAmtOp = cast<Instruction>(Op1Wide)->getOperand(1); |
Roman Lebedev | 6514414 | 2019-10-01 17:50:09 +0000 | [diff] [blame] | 1796 | Instruction *NewShift = BinaryOperator::CreateAShr(X, ShAmtOp); |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1797 | NewShift->copyIRFlags(Op1Wide); |
| 1798 | if (!HadTrunc) |
| 1799 | return NewShift; |
| 1800 | Builder.Insert(NewShift); |
| 1801 | return TruncInst::CreateTruncOrBitCast(NewShift, Op1->getType()); |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1802 | } |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1803 | // -(X >>s 31) -> (X >>u 31) |
| 1804 | if (NoTruncOrTruncIsOneUse && |
| 1805 | match(Op1Wide, m_AShr(m_Value(X), m_APInt(ShAmt))) && |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1806 | *ShAmt == BitWidth - 1) { |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1807 | Value *ShAmtOp = cast<Instruction>(Op1Wide)->getOperand(1); |
Roman Lebedev | 6514414 | 2019-10-01 17:50:09 +0000 | [diff] [blame] | 1808 | Instruction *NewShift = BinaryOperator::CreateLShr(X, ShAmtOp); |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1809 | NewShift->copyIRFlags(Op1Wide); |
| 1810 | if (!HadTrunc) |
| 1811 | return NewShift; |
| 1812 | Builder.Insert(NewShift); |
| 1813 | return TruncInst::CreateTruncOrBitCast(NewShift, Op1->getType()); |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1814 | } |
Craig Topper | 3b768e86 | 2018-05-23 17:29:03 +0000 | [diff] [blame] | 1815 | |
Roman Lebedev | 053014f | 2019-10-01 17:50:20 +0000 | [diff] [blame] | 1816 | if (!HadTrunc && Op1->hasOneUse()) { |
Craig Topper | 3b768e86 | 2018-05-23 17:29:03 +0000 | [diff] [blame] | 1817 | Value *LHS, *RHS; |
| 1818 | SelectPatternFlavor SPF = matchSelectPattern(Op1, LHS, RHS).Flavor; |
| 1819 | if (SPF == SPF_ABS || SPF == SPF_NABS) { |
| 1820 | // This is a negate of an ABS/NABS pattern. Just swap the operands |
| 1821 | // of the select. |
Roman Lebedev | 0efeaa8 | 2019-08-01 12:31:35 +0000 | [diff] [blame] | 1822 | cast<SelectInst>(Op1)->swapValues(); |
Craig Topper | 3b768e86 | 2018-05-23 17:29:03 +0000 | [diff] [blame] | 1823 | // Don't swap prof metadata, we didn't change the branch behavior. |
Roman Lebedev | 0efeaa8 | 2019-08-01 12:31:35 +0000 | [diff] [blame] | 1824 | return replaceInstUsesWith(I, Op1); |
Craig Topper | 3b768e86 | 2018-05-23 17:29:03 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1827 | } |
Matthias Braun | ec68334 | 2015-04-30 22:04:26 +0000 | [diff] [blame] | 1828 | |
| 1829 | // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known |
| 1830 | // zero. |
Craig Topper | b4da684 | 2017-04-06 21:06:03 +0000 | [diff] [blame] | 1831 | if (Op0C->isMask()) { |
Craig Topper | 8205a1a | 2017-05-24 16:53:07 +0000 | [diff] [blame] | 1832 | KnownBits RHSKnown = computeKnownBits(Op1, 0, &I); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1833 | if ((*Op0C | RHSKnown.Zero).isAllOnesValue()) |
Sanjay Patel | 6d6eca5 | 2016-10-14 16:31:54 +0000 | [diff] [blame] | 1834 | return BinaryOperator::CreateXor(Op1, Op0); |
Matthias Braun | ec68334 | 2015-04-30 22:04:26 +0000 | [diff] [blame] | 1835 | } |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
David Majnemer | 72a643d | 2014-11-03 05:53:55 +0000 | [diff] [blame] | 1838 | { |
Suyog Sarda | cba4b1d | 2014-10-08 08:37:49 +0000 | [diff] [blame] | 1839 | Value *Y; |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1840 | // X-(X+Y) == -Y X-(Y+X) == -Y |
Craig Topper | 98851ad | 2017-04-10 16:59:40 +0000 | [diff] [blame] | 1841 | if (match(Op1, m_c_Add(m_Specific(Op0), m_Value(Y)))) |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1842 | return BinaryOperator::CreateNeg(Y); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1843 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1844 | // (X-Y)-X == -Y |
| 1845 | if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y)))) |
| 1846 | return BinaryOperator::CreateNeg(Y); |
| 1847 | } |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1848 | |
David Bolvansky | 358b80b | 2019-09-04 12:00:33 +0000 | [diff] [blame] | 1849 | // (sub (or A, B) (and A, B)) --> (xor A, B) |
| 1850 | { |
| 1851 | Value *A, *B; |
| 1852 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
| 1853 | match(Op0, m_c_Or(m_Specific(A), m_Specific(B)))) |
| 1854 | return BinaryOperator::CreateXor(A, B); |
| 1855 | } |
| 1856 | |
David Bolvansky | 0e07248 | 2019-09-04 17:30:53 +0000 | [diff] [blame] | 1857 | // (sub (and A, B) (or A, B)) --> neg (xor A, B) |
| 1858 | { |
| 1859 | Value *A, *B; |
| 1860 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
| 1861 | match(Op1, m_c_Or(m_Specific(A), m_Specific(B))) && |
| 1862 | (Op0->hasOneUse() || Op1->hasOneUse())) |
| 1863 | return BinaryOperator::CreateNeg(Builder.CreateXor(A, B)); |
| 1864 | } |
| 1865 | |
Hiroshi Yamauchi | 0445e31 | 2017-07-26 21:54:43 +0000 | [diff] [blame] | 1866 | // (sub (or A, B), (xor A, B)) --> (and A, B) |
David Majnemer | 312c3e5 | 2014-10-19 08:32:32 +0000 | [diff] [blame] | 1867 | { |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 1868 | Value *A, *B; |
David Majnemer | 312c3e5 | 2014-10-19 08:32:32 +0000 | [diff] [blame] | 1869 | if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 1870 | match(Op0, m_c_Or(m_Specific(A), m_Specific(B)))) |
David Majnemer | 312c3e5 | 2014-10-19 08:32:32 +0000 | [diff] [blame] | 1871 | return BinaryOperator::CreateAnd(A, B); |
| 1872 | } |
| 1873 | |
David Bolvansky | 420cbb6 | 2019-09-04 18:03:21 +0000 | [diff] [blame] | 1874 | // (sub (xor A, B) (or A, B)) --> neg (and A, B) |
| 1875 | { |
| 1876 | Value *A, *B; |
| 1877 | if (match(Op0, m_Xor(m_Value(A), m_Value(B))) && |
| 1878 | match(Op1, m_c_Or(m_Specific(A), m_Specific(B))) && |
| 1879 | (Op0->hasOneUse() || Op1->hasOneUse())) |
| 1880 | return BinaryOperator::CreateNeg(Builder.CreateAnd(A, B)); |
| 1881 | } |
| 1882 | |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 1883 | { |
| 1884 | Value *Y; |
David Majnemer | 72a643d | 2014-11-03 05:53:55 +0000 | [diff] [blame] | 1885 | // ((X | Y) - X) --> (~X & Y) |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 1886 | if (match(Op0, m_OneUse(m_c_Or(m_Value(Y), m_Specific(Op1))))) |
David Majnemer | 72a643d | 2014-11-03 05:53:55 +0000 | [diff] [blame] | 1887 | return BinaryOperator::CreateAnd( |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1888 | Y, Builder.CreateNot(Op1, Op1->getName() + ".not")); |
David Majnemer | 72a643d | 2014-11-03 05:53:55 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Roman Lebedev | cc0216b | 2020-01-03 19:53:29 +0300 | [diff] [blame] | 1891 | { |
| 1892 | // (sub (and Op1, (neg X)), Op1) --> neg (and Op1, (add X, -1)) |
| 1893 | Value *X; |
| 1894 | if (match(Op0, m_OneUse(m_c_And(m_Specific(Op1), |
| 1895 | m_OneUse(m_Neg(m_Value(X))))))) { |
| 1896 | return BinaryOperator::CreateNeg(Builder.CreateAnd( |
| 1897 | Op1, Builder.CreateAdd(X, Constant::getAllOnesValue(I.getType())))); |
| 1898 | } |
| 1899 | } |
| 1900 | |
Roman Lebedev | 7973aa0 | 2020-01-03 21:10:51 +0300 | [diff] [blame] | 1901 | { |
| 1902 | // (sub (and Op1, C), Op1) --> neg (and Op1, ~C) |
| 1903 | Constant *C; |
| 1904 | if (match(Op0, m_OneUse(m_And(m_Specific(Op1), m_Constant(C))))) { |
| 1905 | return BinaryOperator::CreateNeg( |
| 1906 | Builder.CreateAnd(Op1, Builder.CreateNot(C))); |
| 1907 | } |
| 1908 | } |
| 1909 | |
Roman Lebedev | 4d8e47c | 2020-01-04 16:31:18 +0300 | [diff] [blame] | 1910 | { |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1911 | // If we have a subtraction between some value and a select between |
| 1912 | // said value and something else, sink subtraction into select hands, i.e.: |
| 1913 | // sub (select %Cond, %TrueVal, %FalseVal), %Op1 |
| 1914 | // -> |
| 1915 | // select %Cond, (sub %TrueVal, %Op1), (sub %FalseVal, %Op1) |
| 1916 | // or |
Roman Lebedev | 772ede3 | 2020-01-04 16:50:53 +0300 | [diff] [blame] | 1917 | // sub %Op0, (select %Cond, %TrueVal, %FalseVal) |
| 1918 | // -> |
| 1919 | // select %Cond, (sub %Op0, %TrueVal), (sub %Op0, %FalseVal) |
| 1920 | // This will result in select between new subtraction and 0. |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1921 | auto SinkSubIntoSelect = |
| 1922 | [Ty = I.getType()](Value *Select, Value *OtherHandOfSub, |
| 1923 | auto SubBuilder) -> Instruction * { |
| 1924 | Value *Cond, *TrueVal, *FalseVal; |
| 1925 | if (!match(Select, m_OneUse(m_Select(m_Value(Cond), m_Value(TrueVal), |
| 1926 | m_Value(FalseVal))))) |
| 1927 | return nullptr; |
| 1928 | if (OtherHandOfSub != TrueVal && OtherHandOfSub != FalseVal) |
| 1929 | return nullptr; |
Roman Lebedev | 772ede3 | 2020-01-04 16:50:53 +0300 | [diff] [blame] | 1930 | // While it is really tempting to just create two subtractions and let |
| 1931 | // InstCombine fold one of those to 0, it isn't possible to do so |
| 1932 | // because of worklist visitation order. So ugly it is. |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1933 | bool OtherHandOfSubIsTrueVal = OtherHandOfSub == TrueVal; |
| 1934 | Value *NewSub = SubBuilder(OtherHandOfSubIsTrueVal ? FalseVal : TrueVal); |
| 1935 | Constant *Zero = Constant::getNullValue(Ty); |
Roman Lebedev | 772ede3 | 2020-01-04 16:50:53 +0300 | [diff] [blame] | 1936 | SelectInst *NewSel = |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1937 | SelectInst::Create(Cond, OtherHandOfSubIsTrueVal ? Zero : NewSub, |
| 1938 | OtherHandOfSubIsTrueVal ? NewSub : Zero); |
Roman Lebedev | 772ede3 | 2020-01-04 16:50:53 +0300 | [diff] [blame] | 1939 | // Preserve prof metadata if any. |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1940 | NewSel->copyMetadata(cast<Instruction>(*Select)); |
Roman Lebedev | 772ede3 | 2020-01-04 16:50:53 +0300 | [diff] [blame] | 1941 | return NewSel; |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1942 | }; |
| 1943 | if (Instruction *NewSel = SinkSubIntoSelect( |
| 1944 | /*Select=*/Op0, /*OtherHandOfSub=*/Op1, |
| 1945 | [Builder = &Builder, Op1](Value *OtherHandOfSelect) { |
| 1946 | return Builder->CreateSub(OtherHandOfSelect, |
| 1947 | /*OtherHandOfSub=*/Op1); |
| 1948 | })) |
Roman Lebedev | 4d8e47c | 2020-01-04 16:31:18 +0300 | [diff] [blame] | 1949 | return NewSel; |
Roman Lebedev | 6d05bc2 | 2020-01-04 17:24:20 +0300 | [diff] [blame] | 1950 | if (Instruction *NewSel = SinkSubIntoSelect( |
| 1951 | /*Select=*/Op1, /*OtherHandOfSub=*/Op0, |
| 1952 | [Builder = &Builder, Op0](Value *OtherHandOfSelect) { |
| 1953 | return Builder->CreateSub(/*OtherHandOfSub=*/Op0, |
| 1954 | OtherHandOfSelect); |
| 1955 | })) |
| 1956 | return NewSel; |
Roman Lebedev | 4d8e47c | 2020-01-04 16:31:18 +0300 | [diff] [blame] | 1957 | } |
| 1958 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1959 | if (Op1->hasOneUse()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1960 | Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
| 1961 | Constant *C = nullptr; |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1962 | |
| 1963 | // (X - (Y - Z)) --> (X + (Z - Y)). |
| 1964 | if (match(Op1, m_Sub(m_Value(Y), m_Value(Z)))) |
| 1965 | return BinaryOperator::CreateAdd(Op0, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1966 | Builder.CreateSub(Z, Y, Op1->getName())); |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1967 | |
| 1968 | // (X - (X & Y)) --> (X & ~Y) |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 1969 | if (match(Op1, m_c_And(m_Value(Y), m_Specific(Op0)))) |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1970 | return BinaryOperator::CreateAnd(Op0, |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1971 | Builder.CreateNot(Y, Y->getName() + ".not")); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1972 | |
David Majnemer | bdeef60 | 2014-07-02 06:07:09 +0000 | [diff] [blame] | 1973 | // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow. |
Roman Lebedev | 0931145 | 2019-12-05 15:10:25 +0300 | [diff] [blame] | 1974 | if (match(Op0, m_Zero())) { |
| 1975 | Constant *Op11C; |
| 1976 | if (match(Op1, m_SDiv(m_Value(X), m_Constant(Op11C))) && |
| 1977 | !Op11C->containsUndefElement() && Op11C->isNotMinSignedValue() && |
| 1978 | Op11C->isNotOneValue()) { |
| 1979 | Instruction *BO = |
| 1980 | BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(Op11C)); |
| 1981 | BO->setIsExact(cast<BinaryOperator>(Op1)->isExact()); |
| 1982 | return BO; |
| 1983 | } |
Chen Zheng | 923c7c9 | 2019-04-08 12:08:03 +0000 | [diff] [blame] | 1984 | } |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1985 | |
| 1986 | // 0 - (X << Y) -> (-X << Y) when X is freely negatable. |
| 1987 | if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero())) |
| 1988 | if (Value *XNeg = dyn_castNegVal(X)) |
| 1989 | return BinaryOperator::CreateShl(XNeg, Y); |
| 1990 | |
Sanjay Patel | c6c5965 | 2016-10-14 15:24:31 +0000 | [diff] [blame] | 1991 | // Subtracting -1/0 is the same as adding 1/0: |
| 1992 | // sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y) |
| 1993 | // 'nuw' is dropped in favor of the canonical form. |
| 1994 | if (match(Op1, m_SExt(m_Value(Y))) && |
| 1995 | Y->getType()->getScalarSizeInBits() == 1) { |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 1996 | Value *Zext = Builder.CreateZExt(Y, I.getType()); |
Sanjay Patel | c6c5965 | 2016-10-14 15:24:31 +0000 | [diff] [blame] | 1997 | BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Zext); |
| 1998 | Add->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 1999 | return Add; |
| 2000 | } |
Roman Lebedev | 796fa66 | 2019-12-05 20:44:22 +0300 | [diff] [blame] | 2001 | // sub [nsw] X, zext(bool Y) -> add [nsw] X, sext(bool Y) |
| 2002 | // 'nuw' is dropped in favor of the canonical form. |
| 2003 | if (match(Op1, m_ZExt(m_Value(Y))) && Y->getType()->isIntOrIntVectorTy(1)) { |
| 2004 | Value *Sext = Builder.CreateSExt(Y, I.getType()); |
| 2005 | BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Sext); |
| 2006 | Add->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 2007 | return Add; |
| 2008 | } |
Sanjay Patel | c6c5965 | 2016-10-14 15:24:31 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 2010 | // X - A*-B -> X + A*B |
| 2011 | // X - -A*B -> X + A*B |
| 2012 | Value *A, *B; |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 2013 | if (match(Op1, m_c_Mul(m_Value(A), m_Neg(m_Value(B))))) |
Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 2014 | return BinaryOperator::CreateAdd(Op0, Builder.CreateMul(A, B)); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 2015 | |
Florian Hahn | 4094f34 | 2019-01-15 11:18:21 +0000 | [diff] [blame] | 2016 | // X - A*C -> X + A*-C |
Craig Topper | 0d830ff | 2017-04-10 18:09:25 +0000 | [diff] [blame] | 2017 | // No need to handle commuted multiply because multiply handling will |
| 2018 | // ensure constant will be move to the right hand side. |
Florian Hahn | 4094f34 | 2019-01-15 11:18:21 +0000 | [diff] [blame] | 2019 | if (match(Op1, m_Mul(m_Value(A), m_Constant(C))) && !isa<ConstantExpr>(C)) { |
| 2020 | Value *NewMul = Builder.CreateMul(A, ConstantExpr::getNeg(C)); |
Chris Lattner | 7d0e43f | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 2021 | return BinaryOperator::CreateAdd(Op0, NewMul); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2022 | } |
| 2023 | } |
| 2024 | |
David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 2025 | { |
| 2026 | // ~A - Min/Max(~A, O) -> Max/Min(A, ~O) - A |
| 2027 | // ~A - Min/Max(O, ~A) -> Max/Min(A, ~O) - A |
| 2028 | // Min/Max(~A, O) - ~A -> A - Max/Min(A, ~O) |
| 2029 | // Min/Max(O, ~A) - ~A -> A - Max/Min(A, ~O) |
| 2030 | // So long as O here is freely invertible, this will be neutral or a win. |
| 2031 | Value *LHS, *RHS, *A; |
| 2032 | Value *NotA = Op0, *MinMax = Op1; |
| 2033 | SelectPatternFlavor SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor; |
| 2034 | if (!SelectPatternResult::isMinOrMax(SPF)) { |
| 2035 | NotA = Op1; |
| 2036 | MinMax = Op0; |
| 2037 | SPF = matchSelectPattern(MinMax, LHS, RHS).Flavor; |
| 2038 | } |
| 2039 | if (SelectPatternResult::isMinOrMax(SPF) && |
| 2040 | match(NotA, m_Not(m_Value(A))) && (NotA == LHS || NotA == RHS)) { |
| 2041 | if (NotA == LHS) |
| 2042 | std::swap(LHS, RHS); |
| 2043 | // LHS is now O above and expected to have at least 2 uses (the min/max) |
| 2044 | // NotA is epected to have 2 uses from the min/max and 1 from the sub. |
Roman Lebedev | 0410489 | 2019-08-13 12:49:16 +0000 | [diff] [blame] | 2045 | if (isFreeToInvert(LHS, !LHS->hasNUsesOrMore(3)) && |
David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 2046 | !NotA->hasNUsesOrMore(4)) { |
| 2047 | // Note: We don't generate the inverse max/min, just create the not of |
| 2048 | // it and let other folds do the rest. |
| 2049 | Value *Not = Builder.CreateNot(MinMax); |
| 2050 | if (NotA == Op0) |
| 2051 | return BinaryOperator::CreateSub(Not, A); |
| 2052 | else |
| 2053 | return BinaryOperator::CreateSub(A, Not); |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2058 | // Optimize pointer differences into the same array into a size. Consider: |
| 2059 | // &A[10] - &A[0]: we should compile this to "10". |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2060 | Value *LHSOp, *RHSOp; |
| 2061 | if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && |
| 2062 | match(Op1, m_PtrToInt(m_Value(RHSOp)))) |
Nikita Popov | 0e322c8 | 2020-01-01 11:11:05 +0100 | [diff] [blame^] | 2063 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType(), |
| 2064 | I.hasNoUnsignedWrap())) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2065 | return replaceInstUsesWith(I, Res); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 2066 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2067 | // trunc(p)-trunc(q) -> trunc(p-q) |
| 2068 | if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && |
| 2069 | match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) |
Nikita Popov | 0e322c8 | 2020-01-01 11:11:05 +0100 | [diff] [blame^] | 2070 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType(), |
| 2071 | /* IsNUW */ false)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2072 | return replaceInstUsesWith(I, Res); |
Michael Ilseman | 9fc0f25 | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 2073 | |
Sanjay Patel | 3cd1aa8 | 2018-06-06 21:58:12 +0000 | [diff] [blame] | 2074 | // Canonicalize a shifty way to code absolute value to the common pattern. |
| 2075 | // There are 2 potential commuted variants. |
| 2076 | // We're relying on the fact that we only do this transform when the shift has |
| 2077 | // exactly 2 uses and the xor has exactly 1 use (otherwise, we might increase |
| 2078 | // instructions). |
| 2079 | Value *A; |
| 2080 | const APInt *ShAmt; |
| 2081 | Type *Ty = I.getType(); |
| 2082 | if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) && |
| 2083 | Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 && |
| 2084 | match(Op0, m_OneUse(m_c_Xor(m_Specific(A), m_Specific(Op1))))) { |
| 2085 | // B = ashr i32 A, 31 ; smear the sign bit |
| 2086 | // sub (xor A, B), B ; flip bits if negative and subtract -1 (add 1) |
| 2087 | // --> (A < 0) ? -A : A |
| 2088 | Value *Cmp = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(Ty)); |
| 2089 | // Copy the nuw/nsw flags from the sub to the negate. |
| 2090 | Value *Neg = Builder.CreateNeg(A, "", I.hasNoUnsignedWrap(), |
| 2091 | I.hasNoSignedWrap()); |
| 2092 | return SelectInst::Create(Cmp, Neg, A); |
| 2093 | } |
| 2094 | |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 2095 | if (Instruction *V = |
Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 2096 | canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I)) |
Roman Lebedev | 7cdeac4 | 2019-10-07 20:53:27 +0000 | [diff] [blame] | 2097 | return V; |
| 2098 | |
Craig Topper | 2da7381 | 2018-09-15 18:54:10 +0000 | [diff] [blame] | 2099 | if (Instruction *Ext = narrowMathIfNoOverflow(I)) |
| 2100 | return Ext; |
| 2101 | |
David Majnemer | 57d5bc8 | 2014-08-19 23:36:30 +0000 | [diff] [blame] | 2102 | bool Changed = false; |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 2103 | if (!I.hasNoSignedWrap() && willNotOverflowSignedSub(Op0, Op1, I)) { |
David Majnemer | 57d5bc8 | 2014-08-19 23:36:30 +0000 | [diff] [blame] | 2104 | Changed = true; |
| 2105 | I.setHasNoSignedWrap(true); |
| 2106 | } |
Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 2107 | if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedSub(Op0, Op1, I)) { |
David Majnemer | 42158f3 | 2014-08-20 07:17:31 +0000 | [diff] [blame] | 2108 | Changed = true; |
| 2109 | I.setHasNoUnsignedWrap(true); |
| 2110 | } |
David Majnemer | 57d5bc8 | 2014-08-19 23:36:30 +0000 | [diff] [blame] | 2111 | |
| 2112 | return Changed ? &I : nullptr; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Cameron McInally | 2557ca2 | 2019-05-20 19:10:30 +0000 | [diff] [blame] | 2115 | /// This eliminates floating-point negation in either 'fneg(X)' or |
| 2116 | /// 'fsub(-0.0, X)' form by combining into a constant operand. |
| 2117 | static Instruction *foldFNegIntoConstant(Instruction &I) { |
| 2118 | Value *X; |
| 2119 | Constant *C; |
| 2120 | |
| 2121 | // Fold negation into constant operand. This is limited with one-use because |
| 2122 | // fneg is assumed better for analysis and cheaper in codegen than fmul/fdiv. |
| 2123 | // -(X * C) --> X * (-C) |
Cameron McInally | 8bec58d | 2019-05-20 21:00:42 +0000 | [diff] [blame] | 2124 | // FIXME: It's arguable whether these should be m_OneUse or not. The current |
| 2125 | // belief is that the FNeg allows for better reassociation opportunities. |
Cameron McInally | 2557ca2 | 2019-05-20 19:10:30 +0000 | [diff] [blame] | 2126 | if (match(&I, m_FNeg(m_OneUse(m_FMul(m_Value(X), m_Constant(C)))))) |
| 2127 | return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I); |
| 2128 | // -(X / C) --> X / (-C) |
| 2129 | if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Value(X), m_Constant(C)))))) |
| 2130 | return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I); |
| 2131 | // -(C / X) --> (-C) / X |
| 2132 | if (match(&I, m_FNeg(m_OneUse(m_FDiv(m_Constant(C), m_Value(X)))))) |
| 2133 | return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I); |
| 2134 | |
| 2135 | return nullptr; |
| 2136 | } |
| 2137 | |
Sanjay Patel | 435cdec | 2019-07-31 16:53:22 +0000 | [diff] [blame] | 2138 | static Instruction *hoistFNegAboveFMulFDiv(Instruction &I, |
| 2139 | InstCombiner::BuilderTy &Builder) { |
| 2140 | Value *FNeg; |
| 2141 | if (!match(&I, m_FNeg(m_Value(FNeg)))) |
| 2142 | return nullptr; |
| 2143 | |
| 2144 | Value *X, *Y; |
| 2145 | if (match(FNeg, m_OneUse(m_FMul(m_Value(X), m_Value(Y))))) |
| 2146 | return BinaryOperator::CreateFMulFMF(Builder.CreateFNegFMF(X, &I), Y, &I); |
| 2147 | |
| 2148 | if (match(FNeg, m_OneUse(m_FDiv(m_Value(X), m_Value(Y))))) |
| 2149 | return BinaryOperator::CreateFDivFMF(Builder.CreateFNegFMF(X, &I), Y, &I); |
| 2150 | |
| 2151 | return nullptr; |
| 2152 | } |
| 2153 | |
Cameron McInally | e75412a | 2019-05-10 20:01:04 +0000 | [diff] [blame] | 2154 | Instruction *InstCombiner::visitFNeg(UnaryOperator &I) { |
Cameron McInally | 08200d6 | 2019-06-11 16:21:21 +0000 | [diff] [blame] | 2155 | Value *Op = I.getOperand(0); |
| 2156 | |
| 2157 | if (Value *V = SimplifyFNegInst(Op, I.getFastMathFlags(), |
Cameron McInally | e75412a | 2019-05-10 20:01:04 +0000 | [diff] [blame] | 2158 | SQ.getWithInstruction(&I))) |
| 2159 | return replaceInstUsesWith(I, V); |
| 2160 | |
Cameron McInally | 2557ca2 | 2019-05-20 19:10:30 +0000 | [diff] [blame] | 2161 | if (Instruction *X = foldFNegIntoConstant(I)) |
| 2162 | return X; |
| 2163 | |
Cameron McInally | 08200d6 | 2019-06-11 16:21:21 +0000 | [diff] [blame] | 2164 | Value *X, *Y; |
| 2165 | |
| 2166 | // If we can ignore the sign of zeros: -(X - Y) --> (Y - X) |
| 2167 | if (I.hasNoSignedZeros() && |
| 2168 | match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) |
| 2169 | return BinaryOperator::CreateFSubFMF(Y, X, &I); |
| 2170 | |
Sanjay Patel | 435cdec | 2019-07-31 16:53:22 +0000 | [diff] [blame] | 2171 | if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) |
| 2172 | return R; |
| 2173 | |
Cameron McInally | e75412a | 2019-05-10 20:01:04 +0000 | [diff] [blame] | 2174 | return nullptr; |
| 2175 | } |
| 2176 | |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2177 | Instruction *InstCombiner::visitFSub(BinaryOperator &I) { |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 2178 | if (Value *V = SimplifyFSubInst(I.getOperand(0), I.getOperand(1), |
| 2179 | I.getFastMathFlags(), |
Craig Topper | a420562 | 2017-06-09 03:21:29 +0000 | [diff] [blame] | 2180 | SQ.getWithInstruction(&I))) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2181 | return replaceInstUsesWith(I, V); |
Michael Ilseman | d5787be | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 2182 | |
Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 2183 | if (Instruction *X = foldVectorBinop(I)) |
Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 2184 | return X; |
| 2185 | |
Sanjay Patel | a9ca709 | 2018-04-06 17:24:08 +0000 | [diff] [blame] | 2186 | // Subtraction from -0.0 is the canonical form of fneg. |
| 2187 | // fsub nsz 0, X ==> fsub nsz -0.0, X |
Sanjay Patel | 7b0fc75 | 2018-06-21 17:06:36 +0000 | [diff] [blame] | 2188 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | a9ca709 | 2018-04-06 17:24:08 +0000 | [diff] [blame] | 2189 | if (I.hasNoSignedZeros() && match(Op0, m_PosZeroFP())) |
| 2190 | return BinaryOperator::CreateFNegFMF(Op1, &I); |
Sanjay Patel | 03e2526 | 2018-04-05 21:37:17 +0000 | [diff] [blame] | 2191 | |
Cameron McInally | 2557ca2 | 2019-05-20 19:10:30 +0000 | [diff] [blame] | 2192 | if (Instruction *X = foldFNegIntoConstant(I)) |
| 2193 | return X; |
| 2194 | |
Sanjay Patel | 435cdec | 2019-07-31 16:53:22 +0000 | [diff] [blame] | 2195 | if (Instruction *R = hoistFNegAboveFMulFDiv(I, Builder)) |
| 2196 | return R; |
| 2197 | |
Sanjay Patel | a194b2d | 2018-08-08 14:29:08 +0000 | [diff] [blame] | 2198 | Value *X, *Y; |
| 2199 | Constant *C; |
| 2200 | |
Sanjay Patel | a9ca709 | 2018-04-06 17:24:08 +0000 | [diff] [blame] | 2201 | // If Op0 is not -0.0 or we can ignore -0.0: Z - (X - Y) --> Z + (Y - X) |
Sanjay Patel | 04683de | 2018-04-05 23:21:15 +0000 | [diff] [blame] | 2202 | // Canonicalize to fadd to make analysis easier. |
| 2203 | // This can also help codegen because fadd is commutative. |
Sanjay Patel | a9ca709 | 2018-04-06 17:24:08 +0000 | [diff] [blame] | 2204 | // Note that if this fsub was really an fneg, the fadd with -0.0 will get |
| 2205 | // killed later. We still limit that particular transform with 'hasOneUse' |
| 2206 | // because an fneg is assumed better/cheaper than a generic fsub. |
Sanjay Patel | 04683de | 2018-04-05 23:21:15 +0000 | [diff] [blame] | 2207 | if (I.hasNoSignedZeros() || CannotBeNegativeZero(Op0, SQ.TLI)) { |
| 2208 | if (match(Op1, m_OneUse(m_FSub(m_Value(X), m_Value(Y))))) { |
| 2209 | Value *NewSub = Builder.CreateFSubFMF(Y, X, &I); |
| 2210 | return BinaryOperator::CreateFAddFMF(Op0, NewSub, &I); |
| 2211 | } |
| 2212 | } |
| 2213 | |
Stephen Lin | a9b57f6 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 2214 | if (isa<Constant>(Op0)) |
| 2215 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 2216 | if (Instruction *NV = FoldOpIntoSelect(I, SI)) |
| 2217 | return NV; |
| 2218 | |
Sanjay Patel | deaf4f3 | 2018-04-05 17:06:45 +0000 | [diff] [blame] | 2219 | // X - C --> X + (-C) |
Sanjay Patel | ceb595b | 2018-05-30 23:55:12 +0000 | [diff] [blame] | 2220 | // But don't transform constant expressions because there's an inverse fold |
| 2221 | // for X + (-Y) --> X - Y. |
Sanjay Patel | ceb595b | 2018-05-30 23:55:12 +0000 | [diff] [blame] | 2222 | if (match(Op1, m_Constant(C)) && !isa<ConstantExpr>(Op1)) |
Sanjay Patel | deaf4f3 | 2018-04-05 17:06:45 +0000 | [diff] [blame] | 2223 | return BinaryOperator::CreateFAddFMF(Op0, ConstantExpr::getFNeg(C), &I); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2224 | |
Sanjay Patel | deaf4f3 | 2018-04-05 17:06:45 +0000 | [diff] [blame] | 2225 | // X - (-Y) --> X + Y |
Sanjay Patel | deaf4f3 | 2018-04-05 17:06:45 +0000 | [diff] [blame] | 2226 | if (match(Op1, m_FNeg(m_Value(Y)))) |
| 2227 | return BinaryOperator::CreateFAddFMF(Op0, Y, &I); |
Sanjay Patel | 4a9116e | 2018-02-23 17:07:29 +0000 | [diff] [blame] | 2228 | |
Sanjay Patel | ff98682 | 2018-04-11 15:57:18 +0000 | [diff] [blame] | 2229 | // Similar to above, but look through a cast of the negated value: |
| 2230 | // X - (fptrunc(-Y)) --> X + fptrunc(Y) |
Sanjay Patel | ebec420 | 2018-08-09 15:07:13 +0000 | [diff] [blame] | 2231 | Type *Ty = I.getType(); |
| 2232 | if (match(Op1, m_OneUse(m_FPTrunc(m_FNeg(m_Value(Y)))))) |
| 2233 | return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPTrunc(Y, Ty), &I); |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2234 | |
Sanjay Patel | ebec420 | 2018-08-09 15:07:13 +0000 | [diff] [blame] | 2235 | // X - (fpext(-Y)) --> X + fpext(Y) |
| 2236 | if (match(Op1, m_OneUse(m_FPExt(m_FNeg(m_Value(Y)))))) |
| 2237 | return BinaryOperator::CreateFAddFMF(Op0, Builder.CreateFPExt(Y, Ty), &I); |
| 2238 | |
Sanjay Patel | 99c57c6 | 2019-07-28 17:10:06 +0000 | [diff] [blame] | 2239 | // Similar to above, but look through fmul/fdiv of the negated value: |
| 2240 | // Op0 - (-X * Y) --> Op0 + (X * Y) |
| 2241 | // Op0 - (Y * -X) --> Op0 + (X * Y) |
| 2242 | if (match(Op1, m_OneUse(m_c_FMul(m_FNeg(m_Value(X)), m_Value(Y))))) { |
| 2243 | Value *FMul = Builder.CreateFMulFMF(X, Y, &I); |
| 2244 | return BinaryOperator::CreateFAddFMF(Op0, FMul, &I); |
| 2245 | } |
| 2246 | // Op0 - (-X / Y) --> Op0 + (X / Y) |
| 2247 | // Op0 - (X / -Y) --> Op0 + (X / Y) |
| 2248 | if (match(Op1, m_OneUse(m_FDiv(m_FNeg(m_Value(X)), m_Value(Y)))) || |
| 2249 | match(Op1, m_OneUse(m_FDiv(m_Value(X), m_FNeg(m_Value(Y)))))) { |
| 2250 | Value *FDiv = Builder.CreateFDivFMF(X, Y, &I); |
| 2251 | return BinaryOperator::CreateFAddFMF(Op0, FDiv, &I); |
| 2252 | } |
| 2253 | |
Sanjay Patel | ebec420 | 2018-08-09 15:07:13 +0000 | [diff] [blame] | 2254 | // Handle special cases for FSub with selects feeding the operation |
Quentin Colombet | aa103b3 | 2017-09-20 17:32:16 +0000 | [diff] [blame] | 2255 | if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1)) |
| 2256 | return replaceInstUsesWith(I, V); |
| 2257 | |
Warren Ristow | 8b2f27c | 2018-04-14 19:18:28 +0000 | [diff] [blame] | 2258 | if (I.hasAllowReassoc() && I.hasNoSignedZeros()) { |
Sanjay Patel | 2054dd7 | 2018-08-08 16:04:48 +0000 | [diff] [blame] | 2259 | // (Y - X) - Y --> -X |
| 2260 | if (match(Op0, m_FSub(m_Specific(Op1), m_Value(X)))) |
| 2261 | return BinaryOperator::CreateFNegFMF(X, &I); |
| 2262 | |
Sanjay Patel | fe83969 | 2018-08-08 16:19:22 +0000 | [diff] [blame] | 2263 | // Y - (X + Y) --> -X |
| 2264 | // Y - (Y + X) --> -X |
| 2265 | if (match(Op1, m_c_FAdd(m_Specific(Op0), m_Value(X)))) |
| 2266 | return BinaryOperator::CreateFNegFMF(X, &I); |
| 2267 | |
Sanjay Patel | 55accd7 | 2018-08-09 18:42:12 +0000 | [diff] [blame] | 2268 | // (X * C) - X --> X * (C - 1.0) |
| 2269 | if (match(Op0, m_FMul(m_Specific(Op1), m_Constant(C)))) { |
| 2270 | Constant *CSubOne = ConstantExpr::getFSub(C, ConstantFP::get(Ty, 1.0)); |
| 2271 | return BinaryOperator::CreateFMulFMF(Op1, CSubOne, &I); |
| 2272 | } |
| 2273 | // X - (X * C) --> X * (1.0 - C) |
| 2274 | if (match(Op1, m_FMul(m_Specific(Op0), m_Constant(C)))) { |
| 2275 | Constant *OneSubC = ConstantExpr::getFSub(ConstantFP::get(Ty, 1.0), C); |
| 2276 | return BinaryOperator::CreateFMulFMF(Op0, OneSubC, &I); |
| 2277 | } |
| 2278 | |
Sanjay Patel | dc185ee | 2018-08-12 15:48:26 +0000 | [diff] [blame] | 2279 | if (Instruction *F = factorizeFAddFSub(I, Builder)) |
| 2280 | return F; |
| 2281 | |
Sanjay Patel | 2054dd7 | 2018-08-08 16:04:48 +0000 | [diff] [blame] | 2282 | // TODO: This performs reassociative folds for FP ops. Some fraction of the |
| 2283 | // functionality has been subsumed by simple pattern matching here and in |
| 2284 | // InstSimplify. We should let a dedicated reassociation pass handle more |
| 2285 | // complex pattern matching and remove this from InstCombine. |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 2286 | if (Value *V = FAddCombine(Builder).simplify(&I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 2287 | return replaceInstUsesWith(I, V); |
Shuxin Yang | 37a1efe | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2290 | return nullptr; |
Chris Lattner | 82aa888 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 2291 | } |