Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1 | //===- InstCombineAddSub.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visit functions for add, fadd, sub, and fsub. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "InstCombine.h" |
Craig Topper | b9df53a | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 18 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 19 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | using namespace PatternMatch; |
| 22 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 23 | #define DEBUG_TYPE "instcombine" |
| 24 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | /// Class representing coefficient of floating-point addend. |
| 28 | /// This class needs to be highly efficient, which is especially true for |
| 29 | /// the constructor. As of I write this comment, the cost of the default |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 30 | /// constructor is merely 4-byte-store-zero (Assuming compiler is able to |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 31 | /// perform write-merging). |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 32 | /// |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 33 | class FAddendCoef { |
| 34 | public: |
| 35 | // The constructor has to initialize a APFloat, which is uncessary for |
| 36 | // most addends which have coefficient either 1 or -1. So, the constructor |
| 37 | // is expensive. In order to avoid the cost of the constructor, we should |
| 38 | // reuse some instances whenever possible. The pre-created instances |
| 39 | // FAddCombine::Add[0-5] embodies this idea. |
| 40 | // |
| 41 | FAddendCoef() : IsFp(false), BufHasFpVal(false), IntVal(0) {} |
| 42 | ~FAddendCoef(); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 43 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 44 | void set(short C) { |
| 45 | assert(!insaneIntVal(C) && "Insane coefficient"); |
| 46 | IsFp = false; IntVal = C; |
| 47 | } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 48 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 49 | void set(const APFloat& C); |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 50 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 51 | void negate(); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 52 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 53 | bool isZero() const { return isInt() ? !IntVal : getFpVal().isZero(); } |
| 54 | Value *getValue(Type *) const; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 55 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 56 | // If possible, don't define operator+/operator- etc because these |
| 57 | // operators inevitably call FAddendCoef's constructor which is not cheap. |
| 58 | void operator=(const FAddendCoef &A); |
| 59 | void operator+=(const FAddendCoef &A); |
| 60 | void operator-=(const FAddendCoef &A); |
| 61 | void operator*=(const FAddendCoef &S); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 62 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 63 | bool isOne() const { return isInt() && IntVal == 1; } |
| 64 | bool isTwo() const { return isInt() && IntVal == 2; } |
| 65 | bool isMinusOne() const { return isInt() && IntVal == -1; } |
| 66 | bool isMinusTwo() const { return isInt() && IntVal == -2; } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 67 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 68 | private: |
| 69 | bool insaneIntVal(int V) { return V > 4 || V < -4; } |
| 70 | APFloat *getFpValPtr(void) |
Shuxin Yang | d6b51d1 | 2012-12-19 01:10:17 +0000 | [diff] [blame] | 71 | { return reinterpret_cast<APFloat*>(&FpValBuf.buffer[0]); } |
David Greene | 4ee576f | 2013-01-14 21:04:40 +0000 | [diff] [blame] | 72 | const APFloat *getFpValPtr(void) const |
| 73 | { return reinterpret_cast<const APFloat*>(&FpValBuf.buffer[0]); } |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 74 | |
| 75 | const APFloat &getFpVal(void) const { |
| 76 | assert(IsFp && BufHasFpVal && "Incorret state"); |
David Greene | 4ee576f | 2013-01-14 21:04:40 +0000 | [diff] [blame] | 77 | return *getFpValPtr(); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 80 | APFloat &getFpVal(void) { |
| 81 | assert(IsFp && BufHasFpVal && "Incorret state"); |
| 82 | return *getFpValPtr(); |
| 83 | } |
| 84 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 85 | bool isInt() const { return !IsFp; } |
| 86 | |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 87 | // If the coefficient is represented by an integer, promote it to a |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 88 | // floating point. |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 89 | void convertToFpType(const fltSemantics &Sem); |
| 90 | |
| 91 | // Construct an APFloat from a signed integer. |
| 92 | // TODO: We should get rid of this function when APFloat can be constructed |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 93 | // from an *SIGNED* integer. |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 94 | APFloat createAPFloatFromInt(const fltSemantics &Sem, int Val); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 95 | private: |
Shuxin Yang | d6b51d1 | 2012-12-19 01:10:17 +0000 | [diff] [blame] | 96 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 97 | bool IsFp; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 98 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 99 | // True iff FpValBuf contains an instance of APFloat. |
| 100 | bool BufHasFpVal; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 101 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 102 | // The integer coefficient of an individual addend is either 1 or -1, |
| 103 | // and we try to simplify at most 4 addends from neighboring at most |
| 104 | // two instructions. So the range of <IntVal> falls in [-4, 4]. APInt |
| 105 | // is overkill of this end. |
| 106 | short IntVal; |
Shuxin Yang | d6b51d1 | 2012-12-19 01:10:17 +0000 | [diff] [blame] | 107 | |
| 108 | AlignedCharArrayUnion<APFloat> FpValBuf; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 109 | }; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 110 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 111 | /// FAddend is used to represent floating-point addend. An addend is |
| 112 | /// represented as <C, V>, where the V is a symbolic value, and C is a |
| 113 | /// constant coefficient. A constant addend is represented as <C, 0>. |
| 114 | /// |
| 115 | class FAddend { |
| 116 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 117 | FAddend() { Val = nullptr; } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 118 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 119 | Value *getSymVal (void) const { return Val; } |
| 120 | const FAddendCoef &getCoef(void) const { return Coeff; } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 121 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 122 | bool isConstant() const { return Val == nullptr; } |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 123 | bool isZero() const { return Coeff.isZero(); } |
| 124 | |
| 125 | void set(short Coefficient, Value *V) { Coeff.set(Coefficient), Val = V; } |
| 126 | void set(const APFloat& Coefficient, Value *V) |
| 127 | { Coeff.set(Coefficient); Val = V; } |
| 128 | void set(const ConstantFP* Coefficient, Value *V) |
| 129 | { Coeff.set(Coefficient->getValueAPF()); Val = V; } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 130 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 131 | void negate() { Coeff.negate(); } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 132 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 133 | /// Drill down the U-D chain one step to find the definition of V, and |
| 134 | /// try to break the definition into one or two addends. |
| 135 | static unsigned drillValueDownOneStep(Value* V, FAddend &A0, FAddend &A1); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 136 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 137 | /// Similar to FAddend::drillDownOneStep() except that the value being |
| 138 | /// splitted is the addend itself. |
| 139 | unsigned drillAddendDownOneStep(FAddend &Addend0, FAddend &Addend1) const; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 140 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 141 | void operator+=(const FAddend &T) { |
| 142 | assert((Val == T.Val) && "Symbolic-values disagree"); |
| 143 | Coeff += T.Coeff; |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | void Scale(const FAddendCoef& ScaleAmt) { Coeff *= ScaleAmt; } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 148 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 149 | // This addend has the value of "Coeff * Val". |
| 150 | Value *Val; |
| 151 | FAddendCoef Coeff; |
| 152 | }; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 153 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 154 | /// FAddCombine is the class for optimizing an unsafe fadd/fsub along |
| 155 | /// with its neighboring at most two instructions. |
| 156 | /// |
| 157 | class FAddCombine { |
| 158 | public: |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 159 | FAddCombine(InstCombiner::BuilderTy *B) : Builder(B), Instr(nullptr) {} |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 160 | Value *simplify(Instruction *FAdd); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 161 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 162 | private: |
| 163 | typedef SmallVector<const FAddend*, 4> AddendVect; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 164 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 165 | Value *simplifyFAdd(AddendVect& V, unsigned InstrQuota); |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 166 | |
| 167 | Value *performFactorization(Instruction *I); |
| 168 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 169 | /// Convert given addend to a Value |
| 170 | Value *createAddendVal(const FAddend &A, bool& NeedNeg); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 171 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 172 | /// Return the number of instructions needed to emit the N-ary addition. |
| 173 | unsigned calcInstrNumber(const AddendVect& Vect); |
| 174 | Value *createFSub(Value *Opnd0, Value *Opnd1); |
| 175 | Value *createFAdd(Value *Opnd0, Value *Opnd1); |
| 176 | Value *createFMul(Value *Opnd0, Value *Opnd1); |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 177 | Value *createFDiv(Value *Opnd0, Value *Opnd1); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 178 | Value *createFNeg(Value *V); |
| 179 | Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 180 | void createInstPostProc(Instruction *NewInst, bool NoNumber = false); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 181 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 182 | InstCombiner::BuilderTy *Builder; |
| 183 | Instruction *Instr; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 184 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 185 | private: |
| 186 | // Debugging stuff are clustered here. |
| 187 | #ifndef NDEBUG |
| 188 | unsigned CreateInstrNum; |
| 189 | void initCreateInstNum() { CreateInstrNum = 0; } |
| 190 | void incCreateInstNum() { CreateInstrNum++; } |
| 191 | #else |
| 192 | void initCreateInstNum() {} |
| 193 | void incCreateInstNum() {} |
| 194 | #endif |
| 195 | }; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 196 | } |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 197 | |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | // |
| 200 | // Implementation of |
| 201 | // {FAddendCoef, FAddend, FAddition, FAddCombine}. |
| 202 | // |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | FAddendCoef::~FAddendCoef() { |
| 205 | if (BufHasFpVal) |
| 206 | getFpValPtr()->~APFloat(); |
| 207 | } |
| 208 | |
| 209 | void FAddendCoef::set(const APFloat& C) { |
| 210 | APFloat *P = getFpValPtr(); |
| 211 | |
| 212 | if (isInt()) { |
| 213 | // As the buffer is meanless byte stream, we cannot call |
| 214 | // APFloat::operator=(). |
| 215 | new(P) APFloat(C); |
| 216 | } else |
| 217 | *P = C; |
| 218 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 219 | IsFp = BufHasFpVal = true; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 222 | void FAddendCoef::convertToFpType(const fltSemantics &Sem) { |
| 223 | if (!isInt()) |
| 224 | return; |
| 225 | |
| 226 | APFloat *P = getFpValPtr(); |
| 227 | if (IntVal > 0) |
| 228 | new(P) APFloat(Sem, IntVal); |
| 229 | else { |
| 230 | new(P) APFloat(Sem, 0 - IntVal); |
| 231 | P->changeSign(); |
| 232 | } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 233 | IsFp = BufHasFpVal = true; |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | APFloat FAddendCoef::createAPFloatFromInt(const fltSemantics &Sem, int Val) { |
| 237 | if (Val >= 0) |
| 238 | return APFloat(Sem, Val); |
| 239 | |
| 240 | APFloat T(Sem, 0 - Val); |
| 241 | T.changeSign(); |
| 242 | |
| 243 | return T; |
| 244 | } |
| 245 | |
| 246 | void FAddendCoef::operator=(const FAddendCoef &That) { |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 247 | if (That.isInt()) |
| 248 | set(That.IntVal); |
| 249 | else |
| 250 | set(That.getFpVal()); |
| 251 | } |
| 252 | |
| 253 | void FAddendCoef::operator+=(const FAddendCoef &That) { |
| 254 | enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven; |
| 255 | if (isInt() == That.isInt()) { |
| 256 | if (isInt()) |
| 257 | IntVal += That.IntVal; |
| 258 | else |
| 259 | getFpVal().add(That.getFpVal(), RndMode); |
| 260 | return; |
| 261 | } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 262 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 263 | if (isInt()) { |
| 264 | const APFloat &T = That.getFpVal(); |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 265 | convertToFpType(T.getSemantics()); |
| 266 | getFpVal().add(T, RndMode); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 267 | return; |
| 268 | } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 269 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 270 | APFloat &T = getFpVal(); |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 271 | T.add(createAPFloatFromInt(T.getSemantics(), That.IntVal), RndMode); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void FAddendCoef::operator-=(const FAddendCoef &That) { |
| 275 | enum APFloat::roundingMode RndMode = APFloat::rmNearestTiesToEven; |
| 276 | if (isInt() == That.isInt()) { |
| 277 | if (isInt()) |
| 278 | IntVal -= That.IntVal; |
| 279 | else |
| 280 | getFpVal().subtract(That.getFpVal(), RndMode); |
| 281 | return; |
| 282 | } |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 283 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 284 | if (isInt()) { |
| 285 | const APFloat &T = That.getFpVal(); |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 286 | convertToFpType(T.getSemantics()); |
| 287 | getFpVal().subtract(T, RndMode); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | APFloat &T = getFpVal(); |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 292 | T.subtract(createAPFloatFromInt(T.getSemantics(), IntVal), RndMode); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void FAddendCoef::operator*=(const FAddendCoef &That) { |
| 296 | if (That.isOne()) |
| 297 | return; |
| 298 | |
| 299 | if (That.isMinusOne()) { |
| 300 | negate(); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | if (isInt() && That.isInt()) { |
| 305 | int Res = IntVal * (int)That.IntVal; |
| 306 | assert(!insaneIntVal(Res) && "Insane int value"); |
| 307 | IntVal = Res; |
| 308 | return; |
| 309 | } |
| 310 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 311 | const fltSemantics &Semantic = |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 312 | isInt() ? That.getFpVal().getSemantics() : getFpVal().getSemantics(); |
| 313 | |
| 314 | if (isInt()) |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 315 | convertToFpType(Semantic); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 316 | APFloat &F0 = getFpVal(); |
| 317 | |
| 318 | if (That.isInt()) |
Shuxin Yang | c76067b | 2013-03-25 20:43:41 +0000 | [diff] [blame] | 319 | F0.multiply(createAPFloatFromInt(Semantic, That.IntVal), |
| 320 | APFloat::rmNearestTiesToEven); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 321 | else |
| 322 | F0.multiply(That.getFpVal(), APFloat::rmNearestTiesToEven); |
| 323 | |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | void FAddendCoef::negate() { |
| 328 | if (isInt()) |
| 329 | IntVal = 0 - IntVal; |
| 330 | else |
| 331 | getFpVal().changeSign(); |
| 332 | } |
| 333 | |
| 334 | Value *FAddendCoef::getValue(Type *Ty) const { |
| 335 | return isInt() ? |
| 336 | ConstantFP::get(Ty, float(IntVal)) : |
| 337 | ConstantFP::get(Ty->getContext(), getFpVal()); |
| 338 | } |
| 339 | |
| 340 | // The definition of <Val> Addends |
| 341 | // ========================================= |
| 342 | // A + B <1, A>, <1,B> |
| 343 | // A - B <1, A>, <1,B> |
| 344 | // 0 - B <-1, B> |
| 345 | // C * A, <C, A> |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 346 | // A + C <1, A> <C, NULL> |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 347 | // 0 +/- 0 <0, NULL> (corner case) |
| 348 | // |
| 349 | // Legend: A and B are not constant, C is constant |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 350 | // |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 351 | unsigned FAddend::drillValueDownOneStep |
| 352 | (Value *Val, FAddend &Addend0, FAddend &Addend1) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 353 | Instruction *I = nullptr; |
| 354 | if (!Val || !(I = dyn_cast<Instruction>(Val))) |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | unsigned Opcode = I->getOpcode(); |
| 358 | |
| 359 | if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) { |
| 360 | ConstantFP *C0, *C1; |
| 361 | Value *Opnd0 = I->getOperand(0); |
| 362 | Value *Opnd1 = I->getOperand(1); |
| 363 | if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 364 | Opnd0 = nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 365 | |
| 366 | if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 367 | Opnd1 = nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 368 | |
| 369 | if (Opnd0) { |
| 370 | if (!C0) |
| 371 | Addend0.set(1, Opnd0); |
| 372 | else |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 373 | Addend0.set(C0, nullptr); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (Opnd1) { |
| 377 | FAddend &Addend = Opnd0 ? Addend1 : Addend0; |
| 378 | if (!C1) |
| 379 | Addend.set(1, Opnd1); |
| 380 | else |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 381 | Addend.set(C1, nullptr); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 382 | if (Opcode == Instruction::FSub) |
| 383 | Addend.negate(); |
| 384 | } |
| 385 | |
| 386 | if (Opnd0 || Opnd1) |
| 387 | return Opnd0 && Opnd1 ? 2 : 1; |
| 388 | |
| 389 | // Both operands are zero. Weird! |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 390 | Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 391 | return 1; |
| 392 | } |
| 393 | |
| 394 | if (I->getOpcode() == Instruction::FMul) { |
| 395 | Value *V0 = I->getOperand(0); |
| 396 | Value *V1 = I->getOperand(1); |
| 397 | if (ConstantFP *C = dyn_cast<ConstantFP>(V0)) { |
| 398 | Addend0.set(C, V1); |
| 399 | return 1; |
| 400 | } |
| 401 | |
| 402 | if (ConstantFP *C = dyn_cast<ConstantFP>(V1)) { |
| 403 | Addend0.set(C, V0); |
| 404 | return 1; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | // Try to break *this* addend into two addends. e.g. Suppose this addend is |
| 412 | // <2.3, V>, and V = X + Y, by calling this function, we obtain two addends, |
| 413 | // i.e. <2.3, X> and <2.3, Y>. |
| 414 | // |
| 415 | unsigned FAddend::drillAddendDownOneStep |
| 416 | (FAddend &Addend0, FAddend &Addend1) const { |
| 417 | if (isConstant()) |
| 418 | return 0; |
| 419 | |
| 420 | unsigned BreakNum = FAddend::drillValueDownOneStep(Val, Addend0, Addend1); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 421 | if (!BreakNum || Coeff.isOne()) |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 422 | return BreakNum; |
| 423 | |
| 424 | Addend0.Scale(Coeff); |
| 425 | |
| 426 | if (BreakNum == 2) |
| 427 | Addend1.Scale(Coeff); |
| 428 | |
| 429 | return BreakNum; |
| 430 | } |
| 431 | |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 432 | // Try to perform following optimization on the input instruction I. Return the |
| 433 | // simplified expression if was successful; otherwise, return 0. |
| 434 | // |
| 435 | // Instruction "I" is Simplified into |
| 436 | // ------------------------------------------------------- |
| 437 | // (x * y) +/- (x * z) x * (y +/- z) |
| 438 | // (y / x) +/- (z / x) (y +/- z) / x |
| 439 | // |
| 440 | Value *FAddCombine::performFactorization(Instruction *I) { |
| 441 | assert((I->getOpcode() == Instruction::FAdd || |
| 442 | I->getOpcode() == Instruction::FSub) && "Expect add/sub"); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 443 | |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 444 | Instruction *I0 = dyn_cast<Instruction>(I->getOperand(0)); |
| 445 | Instruction *I1 = dyn_cast<Instruction>(I->getOperand(1)); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 446 | |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 447 | if (!I0 || !I1 || I0->getOpcode() != I1->getOpcode()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 448 | return nullptr; |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 449 | |
| 450 | bool isMpy = false; |
| 451 | if (I0->getOpcode() == Instruction::FMul) |
| 452 | isMpy = true; |
| 453 | else if (I0->getOpcode() != Instruction::FDiv) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 454 | return nullptr; |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 455 | |
| 456 | Value *Opnd0_0 = I0->getOperand(0); |
| 457 | Value *Opnd0_1 = I0->getOperand(1); |
| 458 | Value *Opnd1_0 = I1->getOperand(0); |
| 459 | Value *Opnd1_1 = I1->getOperand(1); |
| 460 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 461 | // Input Instr I Factor AddSub0 AddSub1 |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 462 | // ---------------------------------------------- |
| 463 | // (x*y) +/- (x*z) x y z |
| 464 | // (y/x) +/- (z/x) x y z |
| 465 | // |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 466 | Value *Factor = nullptr; |
| 467 | Value *AddSub0 = nullptr, *AddSub1 = nullptr; |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 468 | |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 469 | if (isMpy) { |
| 470 | if (Opnd0_0 == Opnd1_0 || Opnd0_0 == Opnd1_1) |
| 471 | Factor = Opnd0_0; |
| 472 | else if (Opnd0_1 == Opnd1_0 || Opnd0_1 == Opnd1_1) |
| 473 | Factor = Opnd0_1; |
| 474 | |
| 475 | if (Factor) { |
| 476 | AddSub0 = (Factor == Opnd0_0) ? Opnd0_1 : Opnd0_0; |
| 477 | AddSub1 = (Factor == Opnd1_0) ? Opnd1_1 : Opnd1_0; |
| 478 | } |
| 479 | } else if (Opnd0_1 == Opnd1_1) { |
| 480 | Factor = Opnd0_1; |
| 481 | AddSub0 = Opnd0_0; |
| 482 | AddSub1 = Opnd1_0; |
| 483 | } |
| 484 | |
| 485 | if (!Factor) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 486 | return nullptr; |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 487 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 488 | FastMathFlags Flags; |
| 489 | Flags.setUnsafeAlgebra(); |
| 490 | if (I0) Flags &= I->getFastMathFlags(); |
| 491 | if (I1) Flags &= I->getFastMathFlags(); |
| 492 | |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 493 | // Create expression "NewAddSub = AddSub0 +/- AddsSub1" |
| 494 | Value *NewAddSub = (I->getOpcode() == Instruction::FAdd) ? |
| 495 | createFAdd(AddSub0, AddSub1) : |
| 496 | createFSub(AddSub0, AddSub1); |
| 497 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(NewAddSub)) { |
| 498 | const APFloat &F = CFP->getValueAPF(); |
Michael Gottesman | c3cfe53 | 2013-06-26 23:17:31 +0000 | [diff] [blame] | 499 | if (!F.isNormal()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 500 | return nullptr; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 501 | } else if (Instruction *II = dyn_cast<Instruction>(NewAddSub)) |
| 502 | II->setFastMathFlags(Flags); |
| 503 | |
| 504 | if (isMpy) { |
| 505 | Value *RI = createFMul(Factor, NewAddSub); |
| 506 | if (Instruction *II = dyn_cast<Instruction>(RI)) |
| 507 | II->setFastMathFlags(Flags); |
| 508 | return RI; |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 511 | Value *RI = createFDiv(NewAddSub, Factor); |
| 512 | if (Instruction *II = dyn_cast<Instruction>(RI)) |
| 513 | II->setFastMathFlags(Flags); |
| 514 | return RI; |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 517 | Value *FAddCombine::simplify(Instruction *I) { |
| 518 | assert(I->hasUnsafeAlgebra() && "Should be in unsafe mode"); |
| 519 | |
| 520 | // Currently we are not able to handle vector type. |
| 521 | if (I->getType()->isVectorTy()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 522 | return nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 523 | |
| 524 | assert((I->getOpcode() == Instruction::FAdd || |
| 525 | I->getOpcode() == Instruction::FSub) && "Expect add/sub"); |
| 526 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 527 | // Save the instruction before calling other member-functions. |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 528 | Instr = I; |
| 529 | |
| 530 | FAddend Opnd0, Opnd1, Opnd0_0, Opnd0_1, Opnd1_0, Opnd1_1; |
| 531 | |
| 532 | unsigned OpndNum = FAddend::drillValueDownOneStep(I, Opnd0, Opnd1); |
| 533 | |
| 534 | // Step 1: Expand the 1st addend into Opnd0_0 and Opnd0_1. |
| 535 | unsigned Opnd0_ExpNum = 0; |
| 536 | unsigned Opnd1_ExpNum = 0; |
| 537 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 538 | if (!Opnd0.isConstant()) |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 539 | Opnd0_ExpNum = Opnd0.drillAddendDownOneStep(Opnd0_0, Opnd0_1); |
| 540 | |
| 541 | // Step 2: Expand the 2nd addend into Opnd1_0 and Opnd1_1. |
| 542 | if (OpndNum == 2 && !Opnd1.isConstant()) |
| 543 | Opnd1_ExpNum = Opnd1.drillAddendDownOneStep(Opnd1_0, Opnd1_1); |
| 544 | |
| 545 | // Step 3: Try to optimize Opnd0_0 + Opnd0_1 + Opnd1_0 + Opnd1_1 |
| 546 | if (Opnd0_ExpNum && Opnd1_ExpNum) { |
| 547 | AddendVect AllOpnds; |
| 548 | AllOpnds.push_back(&Opnd0_0); |
| 549 | AllOpnds.push_back(&Opnd1_0); |
| 550 | if (Opnd0_ExpNum == 2) |
| 551 | AllOpnds.push_back(&Opnd0_1); |
| 552 | if (Opnd1_ExpNum == 2) |
| 553 | AllOpnds.push_back(&Opnd1_1); |
| 554 | |
| 555 | // Compute instruction quota. We should save at least one instruction. |
| 556 | unsigned InstQuota = 0; |
| 557 | |
| 558 | Value *V0 = I->getOperand(0); |
| 559 | Value *V1 = I->getOperand(1); |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 560 | InstQuota = ((!isa<Constant>(V0) && V0->hasOneUse()) && |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 561 | (!isa<Constant>(V1) && V1->hasOneUse())) ? 2 : 1; |
| 562 | |
| 563 | if (Value *R = simplifyFAdd(AllOpnds, InstQuota)) |
| 564 | return R; |
| 565 | } |
| 566 | |
| 567 | if (OpndNum != 2) { |
| 568 | // The input instruction is : "I=0.0 +/- V". If the "V" were able to be |
| 569 | // splitted into two addends, say "V = X - Y", the instruction would have |
| 570 | // been optimized into "I = Y - X" in the previous steps. |
| 571 | // |
| 572 | const FAddendCoef &CE = Opnd0.getCoef(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 573 | return CE.isOne() ? Opnd0.getSymVal() : nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1] |
| 577 | if (Opnd1_ExpNum) { |
| 578 | AddendVect AllOpnds; |
| 579 | AllOpnds.push_back(&Opnd0); |
| 580 | AllOpnds.push_back(&Opnd1_0); |
| 581 | if (Opnd1_ExpNum == 2) |
| 582 | AllOpnds.push_back(&Opnd1_1); |
| 583 | |
| 584 | if (Value *R = simplifyFAdd(AllOpnds, 1)) |
| 585 | return R; |
| 586 | } |
| 587 | |
| 588 | // step 5: Try to optimize Opnd1 + Opnd0_0 [+ Opnd0_1] |
| 589 | if (Opnd0_ExpNum) { |
| 590 | AddendVect AllOpnds; |
| 591 | AllOpnds.push_back(&Opnd1); |
| 592 | AllOpnds.push_back(&Opnd0_0); |
| 593 | if (Opnd0_ExpNum == 2) |
| 594 | AllOpnds.push_back(&Opnd0_1); |
| 595 | |
| 596 | if (Value *R = simplifyFAdd(AllOpnds, 1)) |
| 597 | return R; |
| 598 | } |
| 599 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 600 | // step 6: Try factorization as the last resort, |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 601 | return performFactorization(I); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | Value *FAddCombine::simplifyFAdd(AddendVect& Addends, unsigned InstrQuota) { |
| 605 | |
| 606 | unsigned AddendNum = Addends.size(); |
| 607 | assert(AddendNum <= 4 && "Too many addends"); |
| 608 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 609 | // For saving intermediate results; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 610 | unsigned NextTmpIdx = 0; |
| 611 | FAddend TmpResult[3]; |
| 612 | |
| 613 | // Points to the constant addend of the resulting simplified expression. |
| 614 | // If the resulting expr has constant-addend, this constant-addend is |
| 615 | // desirable to reside at the top of the resulting expression tree. Placing |
| 616 | // constant close to supper-expr(s) will potentially reveal some optimization |
| 617 | // opportunities in super-expr(s). |
| 618 | // |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 619 | const FAddend *ConstAdd = nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 620 | |
| 621 | // Simplified addends are placed <SimpVect>. |
| 622 | AddendVect SimpVect; |
| 623 | |
| 624 | // The outer loop works on one symbolic-value at a time. Suppose the input |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 625 | // addends are : <a1, x>, <b1, y>, <a2, x>, <c1, z>, <b2, y>, ... |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 626 | // The symbolic-values will be processed in this order: x, y, z. |
| 627 | // |
| 628 | for (unsigned SymIdx = 0; SymIdx < AddendNum; SymIdx++) { |
| 629 | |
| 630 | const FAddend *ThisAddend = Addends[SymIdx]; |
| 631 | if (!ThisAddend) { |
| 632 | // This addend was processed before. |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | Value *Val = ThisAddend->getSymVal(); |
| 637 | unsigned StartIdx = SimpVect.size(); |
| 638 | SimpVect.push_back(ThisAddend); |
| 639 | |
| 640 | // The inner loop collects addends sharing same symbolic-value, and these |
| 641 | // addends will be later on folded into a single addend. Following above |
| 642 | // example, if the symbolic value "y" is being processed, the inner loop |
| 643 | // will collect two addends "<b1,y>" and "<b2,Y>". These two addends will |
| 644 | // be later on folded into "<b1+b2, y>". |
| 645 | // |
| 646 | for (unsigned SameSymIdx = SymIdx + 1; |
| 647 | SameSymIdx < AddendNum; SameSymIdx++) { |
| 648 | const FAddend *T = Addends[SameSymIdx]; |
| 649 | if (T && T->getSymVal() == Val) { |
| 650 | // Set null such that next iteration of the outer loop will not process |
| 651 | // this addend again. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 652 | Addends[SameSymIdx] = nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 653 | SimpVect.push_back(T); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // If multiple addends share same symbolic value, fold them together. |
| 658 | if (StartIdx + 1 != SimpVect.size()) { |
| 659 | FAddend &R = TmpResult[NextTmpIdx ++]; |
| 660 | R = *SimpVect[StartIdx]; |
| 661 | for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++) |
| 662 | R += *SimpVect[Idx]; |
| 663 | |
| 664 | // Pop all addends being folded and push the resulting folded addend. |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 665 | SimpVect.resize(StartIdx); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 666 | if (Val) { |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 667 | if (!R.isZero()) { |
| 668 | SimpVect.push_back(&R); |
| 669 | } |
| 670 | } else { |
| 671 | // Don't push constant addend at this time. It will be the last element |
| 672 | // of <SimpVect>. |
| 673 | ConstAdd = &R; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
Craig Topper | b9df53a | 2013-07-15 04:27:47 +0000 | [diff] [blame] | 678 | assert((NextTmpIdx <= array_lengthof(TmpResult) + 1) && |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 679 | "out-of-bound access"); |
| 680 | |
| 681 | if (ConstAdd) |
| 682 | SimpVect.push_back(ConstAdd); |
| 683 | |
| 684 | Value *Result; |
| 685 | if (!SimpVect.empty()) |
| 686 | Result = createNaryFAdd(SimpVect, InstrQuota); |
| 687 | else { |
| 688 | // The addition is folded to 0.0. |
| 689 | Result = ConstantFP::get(Instr->getType(), 0.0); |
| 690 | } |
| 691 | |
| 692 | return Result; |
| 693 | } |
| 694 | |
| 695 | Value *FAddCombine::createNaryFAdd |
| 696 | (const AddendVect &Opnds, unsigned InstrQuota) { |
| 697 | assert(!Opnds.empty() && "Expect at least one addend"); |
| 698 | |
| 699 | // Step 1: Check if the # of instructions needed exceeds the quota. |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 700 | // |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 701 | unsigned InstrNeeded = calcInstrNumber(Opnds); |
| 702 | if (InstrNeeded > InstrQuota) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 703 | return nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 704 | |
| 705 | initCreateInstNum(); |
| 706 | |
| 707 | // step 2: Emit the N-ary addition. |
| 708 | // Note that at most three instructions are involved in Fadd-InstCombine: the |
| 709 | // addition in question, and at most two neighboring instructions. |
| 710 | // The resulting optimized addition should have at least one less instruction |
| 711 | // than the original addition expression tree. This implies that the resulting |
| 712 | // N-ary addition has at most two instructions, and we don't need to worry |
| 713 | // about tree-height when constructing the N-ary addition. |
| 714 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 715 | Value *LastVal = nullptr; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 716 | bool LastValNeedNeg = false; |
| 717 | |
| 718 | // Iterate the addends, creating fadd/fsub using adjacent two addends. |
| 719 | for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end(); |
| 720 | I != E; I++) { |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 721 | bool NeedNeg; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 722 | Value *V = createAddendVal(**I, NeedNeg); |
| 723 | if (!LastVal) { |
| 724 | LastVal = V; |
| 725 | LastValNeedNeg = NeedNeg; |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | if (LastValNeedNeg == NeedNeg) { |
| 730 | LastVal = createFAdd(LastVal, V); |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | if (LastValNeedNeg) |
| 735 | LastVal = createFSub(V, LastVal); |
| 736 | else |
| 737 | LastVal = createFSub(LastVal, V); |
| 738 | |
| 739 | LastValNeedNeg = false; |
| 740 | } |
| 741 | |
| 742 | if (LastValNeedNeg) { |
| 743 | LastVal = createFNeg(LastVal); |
| 744 | } |
| 745 | |
| 746 | #ifndef NDEBUG |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 747 | assert(CreateInstrNum == InstrNeeded && |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 748 | "Inconsistent in instruction numbers"); |
| 749 | #endif |
| 750 | |
| 751 | return LastVal; |
| 752 | } |
| 753 | |
| 754 | Value *FAddCombine::createFSub |
| 755 | (Value *Opnd0, Value *Opnd1) { |
| 756 | Value *V = Builder->CreateFSub(Opnd0, Opnd1); |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 757 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 758 | createInstPostProc(I); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 759 | return V; |
| 760 | } |
| 761 | |
| 762 | Value *FAddCombine::createFNeg(Value *V) { |
| 763 | Value *Zero = cast<Value>(ConstantFP::get(V->getType(), 0.0)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 764 | Value *NewV = createFSub(Zero, V); |
| 765 | if (Instruction *I = dyn_cast<Instruction>(NewV)) |
| 766 | createInstPostProc(I, true); // fneg's don't receive instruction numbers. |
| 767 | return NewV; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | Value *FAddCombine::createFAdd |
| 771 | (Value *Opnd0, Value *Opnd1) { |
| 772 | Value *V = Builder->CreateFAdd(Opnd0, Opnd1); |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 773 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 774 | createInstPostProc(I); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 775 | return V; |
| 776 | } |
| 777 | |
| 778 | Value *FAddCombine::createFMul(Value *Opnd0, Value *Opnd1) { |
| 779 | Value *V = Builder->CreateFMul(Opnd0, Opnd1); |
Shuxin Yang | a0c9939 | 2013-03-14 18:08:26 +0000 | [diff] [blame] | 780 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 781 | createInstPostProc(I); |
| 782 | return V; |
| 783 | } |
| 784 | |
| 785 | Value *FAddCombine::createFDiv(Value *Opnd0, Value *Opnd1) { |
| 786 | Value *V = Builder->CreateFDiv(Opnd0, Opnd1); |
| 787 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 788 | createInstPostProc(I); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 789 | return V; |
| 790 | } |
| 791 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 792 | void FAddCombine::createInstPostProc(Instruction *NewInstr, |
| 793 | bool NoNumber) { |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 794 | NewInstr->setDebugLoc(Instr->getDebugLoc()); |
| 795 | |
| 796 | // Keep track of the number of instruction created. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 797 | if (!NoNumber) |
| 798 | incCreateInstNum(); |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 799 | |
| 800 | // Propagate fast-math flags |
| 801 | NewInstr->setFastMathFlags(Instr->getFastMathFlags()); |
| 802 | } |
| 803 | |
| 804 | // Return the number of instruction needed to emit the N-ary addition. |
| 805 | // NOTE: Keep this function in sync with createAddendVal(). |
| 806 | unsigned FAddCombine::calcInstrNumber(const AddendVect &Opnds) { |
| 807 | unsigned OpndNum = Opnds.size(); |
| 808 | unsigned InstrNeeded = OpndNum - 1; |
| 809 | |
Jim Grosbach | 03fceff | 2013-04-05 21:20:12 +0000 | [diff] [blame] | 810 | // The number of addends in the form of "(-1)*x". |
| 811 | unsigned NegOpndNum = 0; |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 812 | |
| 813 | // Adjust the number of instructions needed to emit the N-ary add. |
| 814 | for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end(); |
| 815 | I != E; I++) { |
| 816 | const FAddend *Opnd = *I; |
| 817 | if (Opnd->isConstant()) |
| 818 | continue; |
| 819 | |
| 820 | const FAddendCoef &CE = Opnd->getCoef(); |
| 821 | if (CE.isMinusOne() || CE.isMinusTwo()) |
| 822 | NegOpndNum++; |
| 823 | |
| 824 | // Let the addend be "c * x". If "c == +/-1", the value of the addend |
| 825 | // is immediately available; otherwise, it needs exactly one instruction |
| 826 | // to evaluate the value. |
| 827 | if (!CE.isMinusOne() && !CE.isOne()) |
| 828 | InstrNeeded++; |
| 829 | } |
| 830 | if (NegOpndNum == OpndNum) |
| 831 | InstrNeeded++; |
| 832 | return InstrNeeded; |
| 833 | } |
| 834 | |
| 835 | // Input Addend Value NeedNeg(output) |
| 836 | // ================================================================ |
| 837 | // Constant C C false |
| 838 | // <+/-1, V> V coefficient is -1 |
| 839 | // <2/-2, V> "fadd V, V" coefficient is -2 |
| 840 | // <C, V> "fmul V, C" false |
| 841 | // |
| 842 | // NOTE: Keep this function in sync with FAddCombine::calcInstrNumber. |
| 843 | Value *FAddCombine::createAddendVal |
| 844 | (const FAddend &Opnd, bool &NeedNeg) { |
| 845 | const FAddendCoef &Coeff = Opnd.getCoef(); |
| 846 | |
| 847 | if (Opnd.isConstant()) { |
| 848 | NeedNeg = false; |
| 849 | return Coeff.getValue(Instr->getType()); |
| 850 | } |
| 851 | |
| 852 | Value *OpndVal = Opnd.getSymVal(); |
| 853 | |
| 854 | if (Coeff.isMinusOne() || Coeff.isOne()) { |
| 855 | NeedNeg = Coeff.isMinusOne(); |
| 856 | return OpndVal; |
| 857 | } |
| 858 | |
| 859 | if (Coeff.isTwo() || Coeff.isMinusTwo()) { |
| 860 | NeedNeg = Coeff.isMinusTwo(); |
| 861 | return createFAdd(OpndVal, OpndVal); |
| 862 | } |
| 863 | |
| 864 | NeedNeg = false; |
| 865 | return createFMul(OpndVal, Coeff.getValue(Instr->getType())); |
| 866 | } |
| 867 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 868 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 869 | // other computations (because it has a constant operand), return the |
| 870 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 871 | // Otherwise, return null. |
| 872 | // |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 873 | static inline Value *dyn_castFoldableMul(Value *V, Constant *&CST) { |
| 874 | if (!V->hasOneUse() || !V->getType()->isIntOrIntVectorTy()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 875 | return nullptr; |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 876 | |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 877 | Instruction *I = dyn_cast<Instruction>(V); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 878 | if (!I) return nullptr; |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 880 | if (I->getOpcode() == Instruction::Mul) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 881 | if ((CST = dyn_cast<Constant>(I->getOperand(1)))) |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 882 | return I->getOperand(0); |
| 883 | if (I->getOpcode() == Instruction::Shl) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 884 | if ((CST = dyn_cast<Constant>(I->getOperand(1)))) { |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 885 | // The multiplier is really 1 << CST. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 886 | CST = ConstantExpr::getShl(ConstantInt::get(V->getType(), 1), CST); |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 887 | return I->getOperand(0); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 888 | } |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 889 | return nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | |
| 893 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
| 894 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
| 895 | /// This basically requires proving that the add in the original type would not |
| 896 | /// overflow to change the sign bit or have a carry out. |
| 897 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { |
| 898 | // There are different heuristics we can use for this. Here are some simple |
| 899 | // ones. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 900 | |
| 901 | // Add has the property that adding any two 2's complement numbers can only |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 902 | // have one carry bit which can change a sign. As such, if LHS and RHS each |
| 903 | // have at least two sign bits, we know that the addition of the two values |
| 904 | // will sign extend fine. |
| 905 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) |
| 906 | return true; |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 907 | |
| 908 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 909 | // If one of the operands only has one non-zero bit, and if the other operand |
| 910 | // has a known-zero bit in a more significant place than it (not including the |
| 911 | // sign bit) the ripple may go up to and fill the zero, but won't change the |
| 912 | // sign. For example, (X & ~4) + 1. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 914 | // TODO: Implement. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 916 | return false; |
| 917 | } |
| 918 | |
| 919 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 920 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 921 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 922 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 923 | if (Value *V = SimplifyVectorOp(I)) |
| 924 | return ReplaceInstUsesWith(I, V); |
| 925 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 926 | if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 927 | I.hasNoUnsignedWrap(), DL)) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 928 | return ReplaceInstUsesWith(I, V); |
| 929 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 930 | // (A*B)+(A*C) -> A*(B+C) etc |
| 931 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 932 | return ReplaceInstUsesWith(I, V); |
| 933 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 934 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 935 | // X + (signbit) --> X ^ signbit |
| 936 | const APInt &Val = CI->getValue(); |
| 937 | if (Val.isSignBit()) |
| 938 | return BinaryOperator::CreateXor(LHS, RHS); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 939 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 940 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 941 | // (X & 254)+1 -> (X&254)|1 |
| 942 | if (SimplifyDemandedInstructionBits(I)) |
| 943 | return &I; |
| 944 | |
| 945 | // zext(bool) + C -> bool ? C + 1 : C |
| 946 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) |
| 947 | if (ZI->getSrcTy()->isIntegerTy(1)) |
| 948 | return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 949 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 950 | Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr; |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 951 | if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 952 | uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 953 | const APInt &RHSVal = CI->getValue(); |
Eli Friedman | be7cfa6 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 954 | unsigned ExtendAmt = 0; |
| 955 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 956 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 957 | if (XorRHS->getValue() == -RHSVal) { |
| 958 | if (RHSVal.isPowerOf2()) |
| 959 | ExtendAmt = TySizeBits - RHSVal.logBase2() - 1; |
| 960 | else if (XorRHS->getValue().isPowerOf2()) |
| 961 | ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 962 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 963 | |
Eli Friedman | be7cfa6 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 964 | if (ExtendAmt) { |
| 965 | APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt); |
| 966 | if (!MaskedValueIsZero(XorLHS, Mask)) |
| 967 | ExtendAmt = 0; |
| 968 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 969 | |
Eli Friedman | be7cfa6 | 2010-01-31 04:29:12 +0000 | [diff] [blame] | 970 | if (ExtendAmt) { |
| 971 | Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt); |
| 972 | Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext"); |
| 973 | return BinaryOperator::CreateAShr(NewShl, ShAmt); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 974 | } |
Benjamin Kramer | 49064ff | 2011-12-24 17:31:53 +0000 | [diff] [blame] | 975 | |
| 976 | // If this is a xor that was canonicalized from a sub, turn it back into |
| 977 | // a sub and fuse this add with it. |
| 978 | if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) { |
| 979 | IntegerType *IT = cast<IntegerType>(I.getType()); |
Benjamin Kramer | 49064ff | 2011-12-24 17:31:53 +0000 | [diff] [blame] | 980 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 981 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 982 | computeKnownBits(XorLHS, LHSKnownZero, LHSKnownOne); |
Benjamin Kramer | 49064ff | 2011-12-24 17:31:53 +0000 | [diff] [blame] | 983 | if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue()) |
| 984 | return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI), |
| 985 | XorLHS); |
| 986 | } |
David Majnemer | 8ec23cb | 2013-05-06 21:21:31 +0000 | [diff] [blame] | 987 | // (X + signbit) + C could have gotten canonicalized to (X ^ signbit) + C, |
| 988 | // transform them into (X + (signbit ^ C)) |
| 989 | if (XorRHS->getValue().isSignBit()) |
| 990 | return BinaryOperator::CreateAdd(XorLHS, |
| 991 | ConstantExpr::getXor(XorRHS, CI)); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 995 | if (isa<Constant>(RHS) && isa<PHINode>(LHS)) |
| 996 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 997 | return NV; |
| 998 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 999 | if (I.getType()->getScalarType()->isIntegerTy(1)) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1000 | return BinaryOperator::CreateXor(LHS, RHS); |
| 1001 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1002 | // X + X --> X << 1 |
Chris Lattner | bd9f6bf | 2011-02-17 20:55:29 +0000 | [diff] [blame] | 1003 | if (LHS == RHS) { |
Chris Lattner | 41429e3 | 2011-02-17 02:23:02 +0000 | [diff] [blame] | 1004 | BinaryOperator *New = |
| 1005 | BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1)); |
| 1006 | New->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 1007 | New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 1008 | return New; |
| 1009 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1010 | |
| 1011 | // -A + B --> B - A |
| 1012 | // -A + -B --> -(A + B) |
| 1013 | if (Value *LHSV = dyn_castNegVal(LHS)) { |
Nuno Lopes | 0f68fbb | 2012-06-08 22:30:05 +0000 | [diff] [blame] | 1014 | if (!isa<Constant>(RHS)) |
| 1015 | if (Value *RHSV = dyn_castNegVal(RHS)) { |
| 1016 | Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum"); |
| 1017 | return BinaryOperator::CreateNeg(NewAdd); |
| 1018 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1019 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1020 | return BinaryOperator::CreateSub(RHS, LHSV); |
| 1021 | } |
| 1022 | |
| 1023 | // A + -B --> A - B |
| 1024 | if (!isa<Constant>(RHS)) |
| 1025 | if (Value *V = dyn_castNegVal(RHS)) |
| 1026 | return BinaryOperator::CreateSub(LHS, V); |
| 1027 | |
| 1028 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1029 | { |
| 1030 | Constant *C2; |
| 1031 | if (Value *X = dyn_castFoldableMul(LHS, C2)) { |
| 1032 | if (X == RHS) // X*C + X --> X * (C+1) |
| 1033 | return BinaryOperator::CreateMul(RHS, AddOne(C2)); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1034 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1035 | // X*C1 + X*C2 --> X * (C1+C2) |
| 1036 | Constant *C1; |
| 1037 | if (X == dyn_castFoldableMul(RHS, C1)) |
| 1038 | return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2)); |
| 1039 | } |
| 1040 | |
| 1041 | // X + X*C --> X * (C+1) |
| 1042 | if (dyn_castFoldableMul(RHS, C2) == LHS) |
| 1043 | return BinaryOperator::CreateMul(LHS, AddOne(C2)); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1046 | // A+B --> A|B iff A and B have no bits set in common. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1047 | if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1048 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 1049 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1050 | computeKnownBits(LHS, LHSKnownZero, LHSKnownOne); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1051 | if (LHSKnownZero != 0) { |
| 1052 | APInt RHSKnownOne(IT->getBitWidth(), 0); |
| 1053 | APInt RHSKnownZero(IT->getBitWidth(), 0); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1054 | computeKnownBits(RHS, RHSKnownZero, RHSKnownOne); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1056 | // No bits in common -> bitwise or. |
| 1057 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
| 1058 | return BinaryOperator::CreateOr(LHS, RHS); |
| 1059 | } |
| 1060 | } |
| 1061 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1062 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1063 | { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1064 | Value *W, *X, *Y, *Z; |
| 1065 | if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && |
| 1066 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { |
| 1067 | if (W != Y) { |
| 1068 | if (W == Z) { |
| 1069 | std::swap(Y, Z); |
| 1070 | } else if (Y == X) { |
| 1071 | std::swap(W, X); |
| 1072 | } else if (X == Z) { |
| 1073 | std::swap(Y, Z); |
| 1074 | std::swap(W, X); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (W == Y) { |
| 1079 | Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName()); |
| 1080 | return BinaryOperator::CreateMul(W, NewAdd); |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1085 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 1086 | Value *X; |
| 1087 | if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1088 | return BinaryOperator::CreateSub(SubOne(CRHS), X); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1089 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1090 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1091 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1092 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1093 | Value *X; |
| 1094 | ConstantInt *C2; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1095 | if (LHS->hasOneUse() && |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1096 | match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) && |
| 1097 | CRHS->getValue() == (CRHS->getValue() & C2->getValue())) { |
| 1098 | // See if all bits from the first bit set in the Add RHS up are included |
| 1099 | // in the mask. First, get the rightmost bit. |
| 1100 | const APInt &AddRHSV = CRHS->getValue(); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1102 | // Form a mask of all bits from the lowest bit added through the top. |
| 1103 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1105 | // See if the and mask includes all of these bits. |
| 1106 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1108 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 1109 | // Okay, the xform is safe. Insert the new add pronto. |
| 1110 | Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName()); |
| 1111 | return BinaryOperator::CreateAnd(NewAdd, C2); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | // Try to fold constant add into select arguments. |
| 1116 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
| 1117 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1118 | return R; |
| 1119 | } |
| 1120 | |
| 1121 | // add (select X 0 (sub n A)) A --> select X A n |
| 1122 | { |
| 1123 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| 1124 | Value *A = RHS; |
| 1125 | if (!SI) { |
| 1126 | SI = dyn_cast<SelectInst>(RHS); |
| 1127 | A = LHS; |
| 1128 | } |
| 1129 | if (SI && SI->hasOneUse()) { |
| 1130 | Value *TV = SI->getTrueValue(); |
| 1131 | Value *FV = SI->getFalseValue(); |
| 1132 | Value *N; |
| 1133 | |
| 1134 | // Can we fold the add into the argument of the select? |
| 1135 | // We check both true and false select arguments for a matching subtract. |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1136 | if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1137 | // Fold the add into the true select value. |
| 1138 | return SelectInst::Create(SI->getCondition(), N, A); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1140 | if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A)))) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1141 | // Fold the add into the false select value. |
| 1142 | return SelectInst::Create(SI->getCondition(), A, N); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | // Check for (add (sext x), y), see if we can merge this into an |
| 1147 | // integer add followed by a sext. |
| 1148 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { |
| 1149 | // (add (sext x), cst) --> (sext (add x, cst')) |
| 1150 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1151 | Constant *CI = |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1152 | ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
| 1153 | if (LHSConv->hasOneUse() && |
| 1154 | ConstantExpr::getSExt(CI, I.getType()) == RHSC && |
| 1155 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 1156 | // Insert the new, smaller add. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1157 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1158 | CI, "addconv"); |
| 1159 | return new SExtInst(NewAdd, I.getType()); |
| 1160 | } |
| 1161 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1163 | // (add (sext x), (sext y)) --> (sext (add int x, y)) |
| 1164 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { |
| 1165 | // Only do this if x/y have the same type, if at last one of them has a |
| 1166 | // single use (so we don't increase the number of sexts), and if the |
| 1167 | // integer add will not overflow. |
| 1168 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 1169 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 1170 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 1171 | RHSConv->getOperand(0))) { |
| 1172 | // Insert the new integer add. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1173 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
Chris Lattner | 3168c7d | 2010-01-05 20:56:24 +0000 | [diff] [blame] | 1174 | RHSConv->getOperand(0), "addconv"); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1175 | return new SExtInst(NewAdd, I.getType()); |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
Chad Rosier | c1fc5e4 | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 1180 | // Check for (x & y) + (x ^ y) |
| 1181 | { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1182 | Value *A = nullptr, *B = nullptr; |
Chad Rosier | c1fc5e4 | 2012-04-26 23:29:14 +0000 | [diff] [blame] | 1183 | if (match(RHS, m_Xor(m_Value(A), m_Value(B))) && |
| 1184 | (match(LHS, m_And(m_Specific(A), m_Specific(B))) || |
| 1185 | match(LHS, m_And(m_Specific(B), m_Specific(A))))) |
| 1186 | return BinaryOperator::CreateOr(A, B); |
| 1187 | |
| 1188 | if (match(LHS, m_Xor(m_Value(A), m_Value(B))) && |
| 1189 | (match(RHS, m_And(m_Specific(A), m_Specific(B))) || |
| 1190 | match(RHS, m_And(m_Specific(B), m_Specific(A))))) |
| 1191 | return BinaryOperator::CreateOr(A, B); |
| 1192 | } |
| 1193 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1194 | return Changed ? &I : nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { |
Duncan Sands | 096aa79 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 1198 | bool Changed = SimplifyAssociativeOrCommutative(I); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1199 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 1200 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1201 | if (Value *V = SimplifyVectorOp(I)) |
| 1202 | return ReplaceInstUsesWith(I, V); |
| 1203 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1204 | if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), DL)) |
Michael Ilseman | c244f38 | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 1205 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1206 | |
Stephen Lin | a98ce50 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1207 | if (isa<Constant>(RHS)) { |
| 1208 | if (isa<PHINode>(LHS)) |
| 1209 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 1210 | return NV; |
| 1211 | |
| 1212 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
| 1213 | if (Instruction *NV = FoldOpIntoSelect(I, SI)) |
| 1214 | return NV; |
| 1215 | } |
Michael Ilseman | 07acee7 | 2012-12-14 22:08:26 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1217 | // -A + B --> B - A |
| 1218 | // -A + -B --> -(A + B) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1219 | if (Value *LHSV = dyn_castFNegVal(LHS)) { |
| 1220 | Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV); |
| 1221 | RI->copyFastMathFlags(&I); |
| 1222 | return RI; |
| 1223 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1224 | |
| 1225 | // A + -B --> A - B |
| 1226 | if (!isa<Constant>(RHS)) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1227 | if (Value *V = dyn_castFNegVal(RHS)) { |
| 1228 | Instruction *RI = BinaryOperator::CreateFSub(LHS, V); |
| 1229 | RI->copyFastMathFlags(&I); |
| 1230 | return RI; |
| 1231 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1232 | |
Dan Gohman | a9445e1 | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1233 | // Check for (fadd double (sitofp x), y), see if we can merge this into an |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1234 | // integer add followed by a promotion. |
| 1235 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
Dan Gohman | a9445e1 | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1236 | // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1237 | // ... if the constant fits in the integer value. This is useful for things |
| 1238 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 1239 | // requires a constant pool load, and generally allows the add to be better |
| 1240 | // instcombined. |
| 1241 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1242 | Constant *CI = |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1243 | ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
| 1244 | if (LHSConv->hasOneUse() && |
| 1245 | ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
| 1246 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 1247 | // Insert the new integer add. |
| 1248 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
| 1249 | CI, "addconv"); |
| 1250 | return new SIToFPInst(NewAdd, I.getType()); |
| 1251 | } |
| 1252 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1253 | |
Dan Gohman | a9445e1 | 2010-03-02 01:11:08 +0000 | [diff] [blame] | 1254 | // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1255 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
| 1256 | // Only do this if x/y have the same type, if at last one of them has a |
| 1257 | // single use (so we don't increase the number of int->fp conversions), |
| 1258 | // and if the integer add will not overflow. |
| 1259 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 1260 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 1261 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 1262 | RHSConv->getOperand(0))) { |
| 1263 | // Insert the new integer add. |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1264 | Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1265 | RHSConv->getOperand(0),"addconv"); |
| 1266 | return new SIToFPInst(NewAdd, I.getType()); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1270 | |
Jean-Luc Duprat | c5cf6e5 | 2013-05-06 16:55:50 +0000 | [diff] [blame] | 1271 | // select C, 0, B + select C, A, 0 -> select C, A, B |
| 1272 | { |
| 1273 | Value *A1, *B1, *C1, *A2, *B2, *C2; |
| 1274 | if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) && |
| 1275 | match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) { |
| 1276 | if (C1 == C2) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1277 | Constant *Z1=nullptr, *Z2=nullptr; |
Jean-Luc Duprat | c5cf6e5 | 2013-05-06 16:55:50 +0000 | [diff] [blame] | 1278 | Value *A, *B, *C=C1; |
| 1279 | if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) { |
| 1280 | Z1 = dyn_cast<Constant>(A1); A = A2; |
| 1281 | Z2 = dyn_cast<Constant>(B2); B = B1; |
| 1282 | } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) { |
| 1283 | Z1 = dyn_cast<Constant>(B1); B = B2; |
| 1284 | Z2 = dyn_cast<Constant>(A2); A = A1; |
| 1285 | } |
| 1286 | |
| 1287 | if (Z1 && Z2 && |
| 1288 | (I.hasNoSignedZeros() || |
| 1289 | (Z1->isNegativeZeroValue() && Z2->isNegativeZeroValue()))) { |
| 1290 | return SelectInst::Create(C, A, B); |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 1296 | if (I.hasUnsafeAlgebra()) { |
| 1297 | if (Value *V = FAddCombine(Builder).simplify(&I)) |
| 1298 | return ReplaceInstUsesWith(I, V); |
| 1299 | } |
| 1300 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1301 | return Changed ? &I : nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1305 | /// Optimize pointer differences into the same array into a size. Consider: |
| 1306 | /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer |
| 1307 | /// operands to the ptrtoint instructions for the LHS/RHS of the subtract. |
| 1308 | /// |
| 1309 | Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1310 | Type *Ty) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1311 | assert(DL && "Must have target data info for this"); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1313 | // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize |
| 1314 | // this. |
| 1315 | bool Swapped = false; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1316 | GEPOperator *GEP1 = nullptr, *GEP2 = nullptr; |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1317 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1318 | // For now we require one side to be the base pointer "A" or a constant |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1319 | // GEP derived from it. |
| 1320 | if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1321 | // (gep X, ...) - X |
| 1322 | if (LHSGEP->getOperand(0) == RHS) { |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1323 | GEP1 = LHSGEP; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1324 | Swapped = false; |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1325 | } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) { |
| 1326 | // (gep X, ...) - (gep X, ...) |
| 1327 | if (LHSGEP->getOperand(0)->stripPointerCasts() == |
| 1328 | RHSGEP->getOperand(0)->stripPointerCasts()) { |
| 1329 | GEP2 = RHSGEP; |
| 1330 | GEP1 = LHSGEP; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1331 | Swapped = false; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1335 | |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1336 | if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1337 | // X - (gep X, ...) |
| 1338 | if (RHSGEP->getOperand(0) == LHS) { |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1339 | GEP1 = RHSGEP; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1340 | Swapped = true; |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1341 | } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { |
| 1342 | // (gep X, ...) - (gep X, ...) |
| 1343 | if (RHSGEP->getOperand(0)->stripPointerCasts() == |
| 1344 | LHSGEP->getOperand(0)->stripPointerCasts()) { |
| 1345 | GEP2 = LHSGEP; |
| 1346 | GEP1 = RHSGEP; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1347 | Swapped = true; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1351 | |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1352 | // Avoid duplicating the arithmetic if GEP2 has non-constant indices and |
| 1353 | // multiple users. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1354 | if (!GEP1 || |
| 1355 | (GEP2 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse())) |
| 1356 | return nullptr; |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1358 | // Emit the offset of the GEP and an intptr_t. |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1359 | Value *Result = EmitGEPOffset(GEP1); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1361 | // If we had a constant expression GEP on the other side offsetting the |
| 1362 | // pointer, subtract it from the offset we have. |
Benjamin Kramer | d234863 | 2012-02-20 14:34:57 +0000 | [diff] [blame] | 1363 | if (GEP2) { |
| 1364 | Value *Offset = EmitGEPOffset(GEP2); |
| 1365 | Result = Builder->CreateSub(Result, Offset); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1366 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1367 | |
| 1368 | // If we have p - gep(p, ...) then we have to negate the result. |
| 1369 | if (Swapped) |
| 1370 | Result = Builder->CreateNeg(Result, "diff.neg"); |
| 1371 | |
| 1372 | return Builder->CreateIntCast(Result, Ty, true); |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
| 1377 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1378 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1379 | if (Value *V = SimplifyVectorOp(I)) |
| 1380 | return ReplaceInstUsesWith(I, V); |
| 1381 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 1382 | if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(), |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1383 | I.hasNoUnsignedWrap(), DL)) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 1384 | return ReplaceInstUsesWith(I, V); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1385 | |
Duncan Sands | 37bf92b | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 1386 | // (A*B)-(A*C) -> A*(B-C) etc |
| 1387 | if (Value *V = SimplifyUsingDistributiveLaws(I)) |
| 1388 | return ReplaceInstUsesWith(I, V); |
| 1389 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1390 | // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW. |
| 1391 | if (Value *V = dyn_castNegVal(Op1)) { |
| 1392 | BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); |
| 1393 | Res->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 1394 | Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 1395 | return Res; |
| 1396 | } |
| 1397 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1398 | if (I.getType()->isIntegerTy(1)) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1399 | return BinaryOperator::CreateXor(Op0, Op1); |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Replace (-1 - A) with (~A). |
| 1402 | if (match(Op0, m_AllOnes())) |
| 1403 | return BinaryOperator::CreateNot(Op1); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1404 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1405 | if (Constant *C = dyn_cast<Constant>(Op0)) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1406 | // C - ~X == X + (1+C) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1407 | Value *X = nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1408 | if (match(Op1, m_Not(m_Value(X)))) |
| 1409 | return BinaryOperator::CreateAdd(X, AddOne(C)); |
| 1410 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1411 | // Try to fold constant sub into select arguments. |
| 1412 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1413 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 1414 | return R; |
| 1415 | |
| 1416 | // C-(X+C2) --> (C-C2)-X |
| 1417 | Constant *C2; |
| 1418 | if (match(Op1, m_Add(m_Value(X), m_Constant(C2)))) |
| 1419 | return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X); |
| 1420 | |
| 1421 | if (SimplifyDemandedInstructionBits(I)) |
| 1422 | return &I; |
| 1423 | |
| 1424 | // Fold (sub 0, (zext bool to B)) --> (sext bool to B) |
| 1425 | if (C->isNullValue() && match(Op1, m_ZExt(m_Value(X)))) |
| 1426 | if (X->getType()->getScalarType()->isIntegerTy(1)) |
| 1427 | return CastInst::CreateSExtOrBitCast(X, Op1->getType()); |
| 1428 | |
| 1429 | // Fold (sub 0, (sext bool to B)) --> (zext bool to B) |
| 1430 | if (C->isNullValue() && match(Op1, m_SExt(m_Value(X)))) |
| 1431 | if (X->getType()->getScalarType()->isIntegerTy(1)) |
| 1432 | return CastInst::CreateZExtOrBitCast(X, Op1->getType()); |
| 1433 | } |
| 1434 | |
| 1435 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1436 | // -(X >>u 31) -> (X >>s 31) |
| 1437 | // -(X >>s 31) -> (X >>u 31) |
| 1438 | if (C->isZero()) { |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1439 | Value *X; ConstantInt *CI; |
| 1440 | if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) && |
| 1441 | // Verify we are shifting out everything but the sign bit. |
| 1442 | CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1) |
| 1443 | return BinaryOperator::CreateAShr(X, CI); |
| 1444 | |
| 1445 | if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) && |
| 1446 | // Verify we are shifting out everything but the sign bit. |
| 1447 | CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1) |
| 1448 | return BinaryOperator::CreateLShr(X, CI); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1449 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1452 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1453 | { Value *Y; |
| 1454 | // X-(X+Y) == -Y X-(Y+X) == -Y |
| 1455 | if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) || |
| 1456 | match(Op1, m_Add(m_Value(Y), m_Specific(Op0)))) |
| 1457 | return BinaryOperator::CreateNeg(Y); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1459 | // (X-Y)-X == -Y |
| 1460 | if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y)))) |
| 1461 | return BinaryOperator::CreateNeg(Y); |
| 1462 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1463 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1464 | if (Op1->hasOneUse()) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1465 | Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
| 1466 | Constant *C = nullptr; |
| 1467 | Constant *CI = nullptr; |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1468 | |
| 1469 | // (X - (Y - Z)) --> (X + (Z - Y)). |
| 1470 | if (match(Op1, m_Sub(m_Value(Y), m_Value(Z)))) |
| 1471 | return BinaryOperator::CreateAdd(Op0, |
| 1472 | Builder->CreateSub(Z, Y, Op1->getName())); |
| 1473 | |
| 1474 | // (X - (X & Y)) --> (X & ~Y) |
| 1475 | // |
| 1476 | if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) || |
| 1477 | match(Op1, m_And(m_Specific(Op0), m_Value(Y)))) |
| 1478 | return BinaryOperator::CreateAnd(Op0, |
| 1479 | Builder->CreateNot(Y, Y->getName() + ".not")); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1480 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1481 | // 0 - (X sdiv C) -> (X sdiv -C) |
| 1482 | if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && |
| 1483 | match(Op0, m_Zero())) |
| 1484 | return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C)); |
| 1485 | |
| 1486 | // 0 - (X << Y) -> (-X << Y) when X is freely negatable. |
| 1487 | if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero())) |
| 1488 | if (Value *XNeg = dyn_castNegVal(X)) |
| 1489 | return BinaryOperator::CreateShl(XNeg, Y); |
| 1490 | |
| 1491 | // X - X*C --> X * (1-C) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1492 | if (match(Op1, m_Mul(m_Specific(Op0), m_Constant(CI)))) { |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1493 | Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(),1), CI); |
| 1494 | return BinaryOperator::CreateMul(Op0, CP1); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1497 | // X - X<<C --> X * (1-(1<<C)) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1498 | if (match(Op1, m_Shl(m_Specific(Op0), m_Constant(CI)))) { |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1499 | Constant *One = ConstantInt::get(I.getType(), 1); |
| 1500 | C = ConstantExpr::getSub(One, ConstantExpr::getShl(One, CI)); |
| 1501 | return BinaryOperator::CreateMul(Op0, C); |
| 1502 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1504 | // X - A*-B -> X + A*B |
| 1505 | // X - -A*B -> X + A*B |
| 1506 | Value *A, *B; |
| 1507 | if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) || |
| 1508 | match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B)))) |
| 1509 | return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B)); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1511 | // X - A*CI -> X + A*-CI |
| 1512 | // X - CI*A -> X + A*-CI |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1513 | if (match(Op1, m_Mul(m_Value(A), m_Constant(CI))) || |
| 1514 | match(Op1, m_Mul(m_Constant(CI), m_Value(A)))) { |
Chris Lattner | b9b9044 | 2011-02-10 05:14:58 +0000 | [diff] [blame] | 1515 | Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI)); |
| 1516 | return BinaryOperator::CreateAdd(Op0, NewMul); |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1520 | Constant *C1; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1521 | if (Value *X = dyn_castFoldableMul(Op0, C1)) { |
| 1522 | if (X == Op1) // X*C - X --> X * (C-1) |
| 1523 | return BinaryOperator::CreateMul(Op1, SubOne(C1)); |
| 1524 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1525 | Constant *C2; // X*C1 - X*C2 -> X * (C1-C2) |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1526 | if (X == dyn_castFoldableMul(Op1, C2)) |
| 1527 | return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); |
| 1528 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1530 | // Optimize pointer differences into the same array into a size. Consider: |
| 1531 | // &A[10] - &A[0]: we should compile this to "10". |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1532 | if (DL) { |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1533 | Value *LHSOp, *RHSOp; |
| 1534 | if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && |
| 1535 | match(Op1, m_PtrToInt(m_Value(RHSOp)))) |
| 1536 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
| 1537 | return ReplaceInstUsesWith(I, Res); |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1538 | |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1539 | // trunc(p)-trunc(q) -> trunc(p-q) |
| 1540 | if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && |
| 1541 | match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) |
| 1542 | if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
| 1543 | return ReplaceInstUsesWith(I, Res); |
| 1544 | } |
Michael Ilseman | 4d96e6f | 2012-12-12 20:57:53 +0000 | [diff] [blame] | 1545 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1546 | return nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | Instruction *InstCombiner::visitFSub(BinaryOperator &I) { |
| 1550 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 1551 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1552 | if (Value *V = SimplifyVectorOp(I)) |
| 1553 | return ReplaceInstUsesWith(I, V); |
| 1554 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1555 | if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), DL)) |
Michael Ilseman | c244f38 | 2012-12-12 00:28:32 +0000 | [diff] [blame] | 1556 | return ReplaceInstUsesWith(I, V); |
| 1557 | |
Stephen Lin | a98ce50 | 2013-07-20 07:13:13 +0000 | [diff] [blame] | 1558 | if (isa<Constant>(Op0)) |
| 1559 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 1560 | if (Instruction *NV = FoldOpIntoSelect(I, SI)) |
| 1561 | return NV; |
| 1562 | |
Owen Anderson | 0c326f0 | 2013-07-26 21:40:29 +0000 | [diff] [blame] | 1563 | // If this is a 'B = x-(-A)', change to B = x+A, potentially looking |
| 1564 | // through FP extensions/truncations along the way. |
Owen Anderson | 605b342 | 2013-07-30 23:53:17 +0000 | [diff] [blame] | 1565 | if (Value *V = dyn_castFNegVal(Op1)) { |
| 1566 | Instruction *NewI = BinaryOperator::CreateFAdd(Op0, V); |
| 1567 | NewI->copyFastMathFlags(&I); |
| 1568 | return NewI; |
| 1569 | } |
Owen Anderson | 0c326f0 | 2013-07-26 21:40:29 +0000 | [diff] [blame] | 1570 | if (FPTruncInst *FPTI = dyn_cast<FPTruncInst>(Op1)) { |
| 1571 | if (Value *V = dyn_castFNegVal(FPTI->getOperand(0))) { |
| 1572 | Value *NewTrunc = Builder->CreateFPTrunc(V, I.getType()); |
Owen Anderson | 605b342 | 2013-07-30 23:53:17 +0000 | [diff] [blame] | 1573 | Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewTrunc); |
| 1574 | NewI->copyFastMathFlags(&I); |
| 1575 | return NewI; |
Owen Anderson | 0c326f0 | 2013-07-26 21:40:29 +0000 | [diff] [blame] | 1576 | } |
| 1577 | } else if (FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) { |
| 1578 | if (Value *V = dyn_castFNegVal(FPEI->getOperand(0))) { |
Owen Anderson | 107c578 | 2013-07-26 22:06:21 +0000 | [diff] [blame] | 1579 | Value *NewExt = Builder->CreateFPExt(V, I.getType()); |
Owen Anderson | 605b342 | 2013-07-30 23:53:17 +0000 | [diff] [blame] | 1580 | Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewExt); |
| 1581 | NewI->copyFastMathFlags(&I); |
| 1582 | return NewI; |
Owen Anderson | 0c326f0 | 2013-07-26 21:40:29 +0000 | [diff] [blame] | 1583 | } |
| 1584 | } |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1585 | |
Shuxin Yang | 1a31500 | 2012-12-18 23:10:12 +0000 | [diff] [blame] | 1586 | if (I.hasUnsafeAlgebra()) { |
| 1587 | if (Value *V = FAddCombine(Builder).simplify(&I)) |
| 1588 | return ReplaceInstUsesWith(I, V); |
| 1589 | } |
| 1590 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1591 | return nullptr; |
Chris Lattner | 53a19b7 | 2010-01-05 07:18:46 +0000 | [diff] [blame] | 1592 | } |