blob: c22249527b9135abb9e95c72098f43c5591ce259 [file] [log] [blame]
Chris Lattner53a19b72010-01-05 07:18:46 +00001//===- 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"
15#include "llvm/Analysis/InstructionSimplify.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000016#include "llvm/DataLayout.h"
Chris Lattner53a19b72010-01-05 07:18:46 +000017#include "llvm/Support/GetElementPtrTypeIterator.h"
18#include "llvm/Support/PatternMatch.h"
19using namespace llvm;
20using namespace PatternMatch;
21
22/// AddOne - Add one to a ConstantInt.
23static Constant *AddOne(Constant *C) {
24 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
25}
26/// SubOne - Subtract one from a ConstantInt.
27static Constant *SubOne(ConstantInt *C) {
28 return ConstantInt::get(C->getContext(), C->getValue()-1);
29}
30
31
32// dyn_castFoldableMul - If this value is a multiply that can be folded into
33// other computations (because it has a constant operand), return the
34// non-constant operand of the multiply, and set CST to point to the multiplier.
35// Otherwise, return null.
36//
37static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000038 if (!V->hasOneUse() || !V->getType()->isIntegerTy())
Chris Lattner3168c7d2010-01-05 20:56:24 +000039 return 0;
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000040
Chris Lattner3168c7d2010-01-05 20:56:24 +000041 Instruction *I = dyn_cast<Instruction>(V);
42 if (I == 0) return 0;
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000043
Chris Lattner3168c7d2010-01-05 20:56:24 +000044 if (I->getOpcode() == Instruction::Mul)
45 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1))))
46 return I->getOperand(0);
47 if (I->getOpcode() == Instruction::Shl)
48 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) {
49 // The multiplier is really 1 << CST.
50 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
51 uint32_t CSTVal = CST->getLimitedValue(BitWidth);
52 CST = ConstantInt::get(V->getType()->getContext(),
53 APInt(BitWidth, 1).shl(CSTVal));
54 return I->getOperand(0);
Chris Lattner53a19b72010-01-05 07:18:46 +000055 }
56 return 0;
57}
58
59
60/// WillNotOverflowSignedAdd - Return true if we can prove that:
61/// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS))
62/// This basically requires proving that the add in the original type would not
63/// overflow to change the sign bit or have a carry out.
64bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) {
65 // There are different heuristics we can use for this. Here are some simple
66 // ones.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000067
68 // Add has the property that adding any two 2's complement numbers can only
Chris Lattner53a19b72010-01-05 07:18:46 +000069 // have one carry bit which can change a sign. As such, if LHS and RHS each
70 // have at least two sign bits, we know that the addition of the two values
71 // will sign extend fine.
72 if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1)
73 return true;
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000074
75
Chris Lattner53a19b72010-01-05 07:18:46 +000076 // If one of the operands only has one non-zero bit, and if the other operand
77 // has a known-zero bit in a more significant place than it (not including the
78 // sign bit) the ripple may go up to and fill the zero, but won't change the
79 // sign. For example, (X & ~4) + 1.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000080
Chris Lattner53a19b72010-01-05 07:18:46 +000081 // TODO: Implement.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +000082
Chris Lattner53a19b72010-01-05 07:18:46 +000083 return false;
84}
85
86Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Duncan Sands096aa792010-11-13 15:10:37 +000087 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner53a19b72010-01-05 07:18:46 +000088 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
89
90 if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(),
91 I.hasNoUnsignedWrap(), TD))
92 return ReplaceInstUsesWith(I, V);
93
Duncan Sands37bf92b2010-12-22 13:36:08 +000094 // (A*B)+(A*C) -> A*(B+C) etc
95 if (Value *V = SimplifyUsingDistributiveLaws(I))
96 return ReplaceInstUsesWith(I, V);
97
Chris Lattnerb9b90442011-02-10 05:14:58 +000098 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
99 // X + (signbit) --> X ^ signbit
100 const APInt &Val = CI->getValue();
101 if (Val.isSignBit())
102 return BinaryOperator::CreateXor(LHS, RHS);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000103
Chris Lattnerb9b90442011-02-10 05:14:58 +0000104 // See if SimplifyDemandedBits can simplify this. This handles stuff like
105 // (X & 254)+1 -> (X&254)|1
106 if (SimplifyDemandedInstructionBits(I))
107 return &I;
108
109 // zext(bool) + C -> bool ? C + 1 : C
110 if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS))
111 if (ZI->getSrcTy()->isIntegerTy(1))
112 return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000113
Chris Lattnerb9b90442011-02-10 05:14:58 +0000114 Value *XorLHS = 0; ConstantInt *XorRHS = 0;
115 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000116 uint32_t TySizeBits = I.getType()->getScalarSizeInBits();
Chris Lattnerb9b90442011-02-10 05:14:58 +0000117 const APInt &RHSVal = CI->getValue();
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000118 unsigned ExtendAmt = 0;
119 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext.
120 // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext.
121 if (XorRHS->getValue() == -RHSVal) {
122 if (RHSVal.isPowerOf2())
123 ExtendAmt = TySizeBits - RHSVal.logBase2() - 1;
124 else if (XorRHS->getValue().isPowerOf2())
125 ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1;
Chris Lattner53a19b72010-01-05 07:18:46 +0000126 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000127
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000128 if (ExtendAmt) {
129 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt);
130 if (!MaskedValueIsZero(XorLHS, Mask))
131 ExtendAmt = 0;
132 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000133
Eli Friedmanbe7cfa62010-01-31 04:29:12 +0000134 if (ExtendAmt) {
135 Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt);
136 Value *NewShl = Builder->CreateShl(XorLHS, ShAmt, "sext");
137 return BinaryOperator::CreateAShr(NewShl, ShAmt);
Chris Lattner53a19b72010-01-05 07:18:46 +0000138 }
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000139
140 // If this is a xor that was canonicalized from a sub, turn it back into
141 // a sub and fuse this add with it.
142 if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) {
143 IntegerType *IT = cast<IntegerType>(I.getType());
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000144 APInt LHSKnownOne(IT->getBitWidth(), 0);
145 APInt LHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000146 ComputeMaskedBits(XorLHS, LHSKnownZero, LHSKnownOne);
Benjamin Kramer49064ff2011-12-24 17:31:53 +0000147 if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue())
148 return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI),
149 XorLHS);
150 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000151 }
152 }
153
Chris Lattnerb9b90442011-02-10 05:14:58 +0000154 if (isa<Constant>(RHS) && isa<PHINode>(LHS))
155 if (Instruction *NV = FoldOpIntoPhi(I))
156 return NV;
157
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000158 if (I.getType()->isIntegerTy(1))
Chris Lattner53a19b72010-01-05 07:18:46 +0000159 return BinaryOperator::CreateXor(LHS, RHS);
160
Chris Lattnerb9b90442011-02-10 05:14:58 +0000161 // X + X --> X << 1
Chris Lattnerbd9f6bf2011-02-17 20:55:29 +0000162 if (LHS == RHS) {
Chris Lattner41429e32011-02-17 02:23:02 +0000163 BinaryOperator *New =
164 BinaryOperator::CreateShl(LHS, ConstantInt::get(I.getType(), 1));
165 New->setHasNoSignedWrap(I.hasNoSignedWrap());
166 New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
167 return New;
168 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000169
170 // -A + B --> B - A
171 // -A + -B --> -(A + B)
172 if (Value *LHSV = dyn_castNegVal(LHS)) {
Nuno Lopes0f68fbb2012-06-08 22:30:05 +0000173 if (!isa<Constant>(RHS))
174 if (Value *RHSV = dyn_castNegVal(RHS)) {
175 Value *NewAdd = Builder->CreateAdd(LHSV, RHSV, "sum");
176 return BinaryOperator::CreateNeg(NewAdd);
177 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000178
Chris Lattner53a19b72010-01-05 07:18:46 +0000179 return BinaryOperator::CreateSub(RHS, LHSV);
180 }
181
182 // A + -B --> A - B
183 if (!isa<Constant>(RHS))
184 if (Value *V = dyn_castNegVal(RHS))
185 return BinaryOperator::CreateSub(LHS, V);
186
187
188 ConstantInt *C2;
189 if (Value *X = dyn_castFoldableMul(LHS, C2)) {
190 if (X == RHS) // X*C + X --> X * (C+1)
191 return BinaryOperator::CreateMul(RHS, AddOne(C2));
192
193 // X*C1 + X*C2 --> X * (C1+C2)
194 ConstantInt *C1;
195 if (X == dyn_castFoldableMul(RHS, C1))
196 return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2));
197 }
198
199 // X + X*C --> X * (C+1)
200 if (dyn_castFoldableMul(RHS, C2) == LHS)
201 return BinaryOperator::CreateMul(LHS, AddOne(C2));
202
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000203 // A+B --> A|B iff A and B have no bits set in common.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000204 if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000205 APInt LHSKnownOne(IT->getBitWidth(), 0);
206 APInt LHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000207 ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
Chris Lattner53a19b72010-01-05 07:18:46 +0000208 if (LHSKnownZero != 0) {
209 APInt RHSKnownOne(IT->getBitWidth(), 0);
210 APInt RHSKnownZero(IT->getBitWidth(), 0);
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000211 ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000212
Chris Lattner53a19b72010-01-05 07:18:46 +0000213 // No bits in common -> bitwise or.
214 if ((LHSKnownZero|RHSKnownZero).isAllOnesValue())
215 return BinaryOperator::CreateOr(LHS, RHS);
216 }
217 }
218
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000219 // W*X + Y*Z --> W * (X+Z) iff W == Y
Chris Lattnerb9b90442011-02-10 05:14:58 +0000220 {
Chris Lattner53a19b72010-01-05 07:18:46 +0000221 Value *W, *X, *Y, *Z;
222 if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
223 match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
224 if (W != Y) {
225 if (W == Z) {
226 std::swap(Y, Z);
227 } else if (Y == X) {
228 std::swap(W, X);
229 } else if (X == Z) {
230 std::swap(Y, Z);
231 std::swap(W, X);
232 }
233 }
234
235 if (W == Y) {
236 Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName());
237 return BinaryOperator::CreateMul(W, NewAdd);
238 }
239 }
240 }
241
242 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
243 Value *X = 0;
244 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
245 return BinaryOperator::CreateSub(SubOne(CRHS), X);
246
247 // (X & FF00) + xx00 -> (X+xx00) & FF00
248 if (LHS->hasOneUse() &&
Chris Lattnerb9b90442011-02-10 05:14:58 +0000249 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) &&
250 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) {
251 // See if all bits from the first bit set in the Add RHS up are included
252 // in the mask. First, get the rightmost bit.
253 const APInt &AddRHSV = CRHS->getValue();
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000254
Chris Lattnerb9b90442011-02-10 05:14:58 +0000255 // Form a mask of all bits from the lowest bit added through the top.
256 APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1));
Chris Lattner53a19b72010-01-05 07:18:46 +0000257
Chris Lattnerb9b90442011-02-10 05:14:58 +0000258 // See if the and mask includes all of these bits.
259 APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue());
Chris Lattner53a19b72010-01-05 07:18:46 +0000260
Chris Lattnerb9b90442011-02-10 05:14:58 +0000261 if (AddRHSHighBits == AddRHSHighBitsAnd) {
262 // Okay, the xform is safe. Insert the new add pronto.
263 Value *NewAdd = Builder->CreateAdd(X, CRHS, LHS->getName());
264 return BinaryOperator::CreateAnd(NewAdd, C2);
Chris Lattner53a19b72010-01-05 07:18:46 +0000265 }
266 }
267
268 // Try to fold constant add into select arguments.
269 if (SelectInst *SI = dyn_cast<SelectInst>(LHS))
270 if (Instruction *R = FoldOpIntoSelect(I, SI))
271 return R;
272 }
273
274 // add (select X 0 (sub n A)) A --> select X A n
275 {
276 SelectInst *SI = dyn_cast<SelectInst>(LHS);
277 Value *A = RHS;
278 if (!SI) {
279 SI = dyn_cast<SelectInst>(RHS);
280 A = LHS;
281 }
282 if (SI && SI->hasOneUse()) {
283 Value *TV = SI->getTrueValue();
284 Value *FV = SI->getFalseValue();
285 Value *N;
286
287 // Can we fold the add into the argument of the select?
288 // We check both true and false select arguments for a matching subtract.
Chris Lattnerb9b90442011-02-10 05:14:58 +0000289 if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A))))
Chris Lattner53a19b72010-01-05 07:18:46 +0000290 // Fold the add into the true select value.
291 return SelectInst::Create(SI->getCondition(), N, A);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000292
Chris Lattnerb9b90442011-02-10 05:14:58 +0000293 if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A))))
Chris Lattner53a19b72010-01-05 07:18:46 +0000294 // Fold the add into the false select value.
295 return SelectInst::Create(SI->getCondition(), A, N);
296 }
297 }
298
299 // Check for (add (sext x), y), see if we can merge this into an
300 // integer add followed by a sext.
301 if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) {
302 // (add (sext x), cst) --> (sext (add x, cst'))
303 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) {
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000304 Constant *CI =
Chris Lattner53a19b72010-01-05 07:18:46 +0000305 ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType());
306 if (LHSConv->hasOneUse() &&
307 ConstantExpr::getSExt(CI, I.getType()) == RHSC &&
308 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
309 // Insert the new, smaller add.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000310 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
Chris Lattner53a19b72010-01-05 07:18:46 +0000311 CI, "addconv");
312 return new SExtInst(NewAdd, I.getType());
313 }
314 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000315
Chris Lattner53a19b72010-01-05 07:18:46 +0000316 // (add (sext x), (sext y)) --> (sext (add int x, y))
317 if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) {
318 // Only do this if x/y have the same type, if at last one of them has a
319 // single use (so we don't increase the number of sexts), and if the
320 // integer add will not overflow.
321 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
322 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
323 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
324 RHSConv->getOperand(0))) {
325 // Insert the new integer add.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000326 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
Chris Lattner3168c7d2010-01-05 20:56:24 +0000327 RHSConv->getOperand(0), "addconv");
Chris Lattner53a19b72010-01-05 07:18:46 +0000328 return new SExtInst(NewAdd, I.getType());
329 }
330 }
331 }
332
Chad Rosierc1fc5e42012-04-26 23:29:14 +0000333 // Check for (x & y) + (x ^ y)
334 {
335 Value *A = 0, *B = 0;
336 if (match(RHS, m_Xor(m_Value(A), m_Value(B))) &&
337 (match(LHS, m_And(m_Specific(A), m_Specific(B))) ||
338 match(LHS, m_And(m_Specific(B), m_Specific(A)))))
339 return BinaryOperator::CreateOr(A, B);
340
341 if (match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
342 (match(RHS, m_And(m_Specific(A), m_Specific(B))) ||
343 match(RHS, m_And(m_Specific(B), m_Specific(A)))))
344 return BinaryOperator::CreateOr(A, B);
345 }
346
Chris Lattner53a19b72010-01-05 07:18:46 +0000347 return Changed ? &I : 0;
348}
349
350Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
Duncan Sands096aa792010-11-13 15:10:37 +0000351 bool Changed = SimplifyAssociativeOrCommutative(I);
Chris Lattner53a19b72010-01-05 07:18:46 +0000352 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
353
Michael Ilsemanc244f382012-12-12 00:28:32 +0000354 if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), TD))
355 return ReplaceInstUsesWith(I, V);
Chris Lattner53a19b72010-01-05 07:18:46 +0000356
357 // -A + B --> B - A
358 // -A + -B --> -(A + B)
359 if (Value *LHSV = dyn_castFNegVal(LHS))
360 return BinaryOperator::CreateFSub(RHS, LHSV);
361
362 // A + -B --> A - B
363 if (!isa<Constant>(RHS))
364 if (Value *V = dyn_castFNegVal(RHS))
365 return BinaryOperator::CreateFSub(LHS, V);
366
Dan Gohmana9445e12010-03-02 01:11:08 +0000367 // Check for (fadd double (sitofp x), y), see if we can merge this into an
Chris Lattner53a19b72010-01-05 07:18:46 +0000368 // integer add followed by a promotion.
369 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) {
Dan Gohmana9445e12010-03-02 01:11:08 +0000370 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst))
Chris Lattner53a19b72010-01-05 07:18:46 +0000371 // ... if the constant fits in the integer value. This is useful for things
372 // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer
373 // requires a constant pool load, and generally allows the add to be better
374 // instcombined.
375 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) {
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000376 Constant *CI =
Chris Lattner53a19b72010-01-05 07:18:46 +0000377 ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType());
378 if (LHSConv->hasOneUse() &&
379 ConstantExpr::getSIToFP(CI, I.getType()) == CFP &&
380 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) {
381 // Insert the new integer add.
382 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
383 CI, "addconv");
384 return new SIToFPInst(NewAdd, I.getType());
385 }
386 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000387
Dan Gohmana9445e12010-03-02 01:11:08 +0000388 // (fadd double (sitofp x), (sitofp y)) --> (sitofp (add int x, y))
Chris Lattner53a19b72010-01-05 07:18:46 +0000389 if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) {
390 // Only do this if x/y have the same type, if at last one of them has a
391 // single use (so we don't increase the number of int->fp conversions),
392 // and if the integer add will not overflow.
393 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&&
394 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) &&
395 WillNotOverflowSignedAdd(LHSConv->getOperand(0),
396 RHSConv->getOperand(0))) {
397 // Insert the new integer add.
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000398 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0),
Chris Lattner53a19b72010-01-05 07:18:46 +0000399 RHSConv->getOperand(0),"addconv");
400 return new SIToFPInst(NewAdd, I.getType());
401 }
402 }
403 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000404
Chris Lattner53a19b72010-01-05 07:18:46 +0000405 return Changed ? &I : 0;
406}
407
408
Chris Lattner53a19b72010-01-05 07:18:46 +0000409/// Optimize pointer differences into the same array into a size. Consider:
410/// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer
411/// operands to the ptrtoint instructions for the LHS/RHS of the subtract.
412///
413Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000414 Type *Ty) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000415 assert(TD && "Must have target data info for this");
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000416
Chris Lattner53a19b72010-01-05 07:18:46 +0000417 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
418 // this.
419 bool Swapped = false;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000420 GEPOperator *GEP1 = 0, *GEP2 = 0;
421
Chris Lattner53a19b72010-01-05 07:18:46 +0000422 // For now we require one side to be the base pointer "A" or a constant
Benjamin Kramerd2348632012-02-20 14:34:57 +0000423 // GEP derived from it.
424 if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000425 // (gep X, ...) - X
426 if (LHSGEP->getOperand(0) == RHS) {
Benjamin Kramerd2348632012-02-20 14:34:57 +0000427 GEP1 = LHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000428 Swapped = false;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000429 } else if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
430 // (gep X, ...) - (gep X, ...)
431 if (LHSGEP->getOperand(0)->stripPointerCasts() ==
432 RHSGEP->getOperand(0)->stripPointerCasts()) {
433 GEP2 = RHSGEP;
434 GEP1 = LHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000435 Swapped = false;
436 }
437 }
438 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000439
Benjamin Kramerd2348632012-02-20 14:34:57 +0000440 if (GEPOperator *RHSGEP = dyn_cast<GEPOperator>(RHS)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000441 // X - (gep X, ...)
442 if (RHSGEP->getOperand(0) == LHS) {
Benjamin Kramerd2348632012-02-20 14:34:57 +0000443 GEP1 = RHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000444 Swapped = true;
Benjamin Kramerd2348632012-02-20 14:34:57 +0000445 } else if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) {
446 // (gep X, ...) - (gep X, ...)
447 if (RHSGEP->getOperand(0)->stripPointerCasts() ==
448 LHSGEP->getOperand(0)->stripPointerCasts()) {
449 GEP2 = LHSGEP;
450 GEP1 = RHSGEP;
Chris Lattner53a19b72010-01-05 07:18:46 +0000451 Swapped = true;
452 }
453 }
454 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000455
Benjamin Kramerd2348632012-02-20 14:34:57 +0000456 // Avoid duplicating the arithmetic if GEP2 has non-constant indices and
457 // multiple users.
458 if (GEP1 == 0 ||
459 (GEP2 != 0 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse()))
Chris Lattner53a19b72010-01-05 07:18:46 +0000460 return 0;
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000461
Chris Lattner53a19b72010-01-05 07:18:46 +0000462 // Emit the offset of the GEP and an intptr_t.
Benjamin Kramerd2348632012-02-20 14:34:57 +0000463 Value *Result = EmitGEPOffset(GEP1);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000464
Chris Lattner53a19b72010-01-05 07:18:46 +0000465 // If we had a constant expression GEP on the other side offsetting the
466 // pointer, subtract it from the offset we have.
Benjamin Kramerd2348632012-02-20 14:34:57 +0000467 if (GEP2) {
468 Value *Offset = EmitGEPOffset(GEP2);
469 Result = Builder->CreateSub(Result, Offset);
Chris Lattner53a19b72010-01-05 07:18:46 +0000470 }
Chris Lattner53a19b72010-01-05 07:18:46 +0000471
472 // If we have p - gep(p, ...) then we have to negate the result.
473 if (Swapped)
474 Result = Builder->CreateNeg(Result, "diff.neg");
475
476 return Builder->CreateIntCast(Result, Ty, true);
477}
478
479
480Instruction *InstCombiner::visitSub(BinaryOperator &I) {
481 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
482
Duncan Sandsfea3b212010-12-15 14:07:39 +0000483 if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(),
484 I.hasNoUnsignedWrap(), TD))
485 return ReplaceInstUsesWith(I, V);
Chris Lattner53a19b72010-01-05 07:18:46 +0000486
Duncan Sands37bf92b2010-12-22 13:36:08 +0000487 // (A*B)-(A*C) -> A*(B-C) etc
488 if (Value *V = SimplifyUsingDistributiveLaws(I))
489 return ReplaceInstUsesWith(I, V);
490
Chris Lattner53a19b72010-01-05 07:18:46 +0000491 // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW.
492 if (Value *V = dyn_castNegVal(Op1)) {
493 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V);
494 Res->setHasNoSignedWrap(I.hasNoSignedWrap());
495 Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
496 return Res;
497 }
498
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000499 if (I.getType()->isIntegerTy(1))
Chris Lattner53a19b72010-01-05 07:18:46 +0000500 return BinaryOperator::CreateXor(Op0, Op1);
Chris Lattnerb9b90442011-02-10 05:14:58 +0000501
502 // Replace (-1 - A) with (~A).
503 if (match(Op0, m_AllOnes()))
504 return BinaryOperator::CreateNot(Op1);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000505
Chris Lattner53a19b72010-01-05 07:18:46 +0000506 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) {
Chris Lattner53a19b72010-01-05 07:18:46 +0000507 // C - ~X == X + (1+C)
508 Value *X = 0;
509 if (match(Op1, m_Not(m_Value(X))))
510 return BinaryOperator::CreateAdd(X, AddOne(C));
511
512 // -(X >>u 31) -> (X >>s 31)
513 // -(X >>s 31) -> (X >>u 31)
514 if (C->isZero()) {
Chris Lattnerb9b90442011-02-10 05:14:58 +0000515 Value *X; ConstantInt *CI;
516 if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) &&
517 // Verify we are shifting out everything but the sign bit.
518 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
519 return BinaryOperator::CreateAShr(X, CI);
520
521 if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) &&
522 // Verify we are shifting out everything but the sign bit.
523 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1)
524 return BinaryOperator::CreateLShr(X, CI);
Chris Lattner53a19b72010-01-05 07:18:46 +0000525 }
526
527 // Try to fold constant sub into select arguments.
528 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
529 if (Instruction *R = FoldOpIntoSelect(I, SI))
530 return R;
531
Chris Lattnerb9b90442011-02-10 05:14:58 +0000532 // C-(X+C2) --> (C-C2)-X
533 ConstantInt *C2;
534 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(C2))))
535 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X);
Benjamin Kramer1fdfae02011-12-24 17:31:38 +0000536
537 if (SimplifyDemandedInstructionBits(I))
538 return &I;
Chris Lattner53a19b72010-01-05 07:18:46 +0000539 }
540
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000541
Chris Lattnerb9b90442011-02-10 05:14:58 +0000542 { Value *Y;
543 // X-(X+Y) == -Y X-(Y+X) == -Y
544 if (match(Op1, m_Add(m_Specific(Op0), m_Value(Y))) ||
545 match(Op1, m_Add(m_Value(Y), m_Specific(Op0))))
546 return BinaryOperator::CreateNeg(Y);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000547
Chris Lattnerb9b90442011-02-10 05:14:58 +0000548 // (X-Y)-X == -Y
549 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y))))
550 return BinaryOperator::CreateNeg(Y);
551 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000552
Chris Lattnerb9b90442011-02-10 05:14:58 +0000553 if (Op1->hasOneUse()) {
554 Value *X = 0, *Y = 0, *Z = 0;
555 Constant *C = 0;
556 ConstantInt *CI = 0;
557
558 // (X - (Y - Z)) --> (X + (Z - Y)).
559 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z))))
560 return BinaryOperator::CreateAdd(Op0,
561 Builder->CreateSub(Z, Y, Op1->getName()));
562
563 // (X - (X & Y)) --> (X & ~Y)
564 //
565 if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) ||
566 match(Op1, m_And(m_Specific(Op0), m_Value(Y))))
567 return BinaryOperator::CreateAnd(Op0,
568 Builder->CreateNot(Y, Y->getName() + ".not"));
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000569
Chris Lattnerb9b90442011-02-10 05:14:58 +0000570 // 0 - (X sdiv C) -> (X sdiv -C)
571 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) &&
572 match(Op0, m_Zero()))
573 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C));
574
575 // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
576 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero()))
577 if (Value *XNeg = dyn_castNegVal(X))
578 return BinaryOperator::CreateShl(XNeg, Y);
579
580 // X - X*C --> X * (1-C)
581 if (match(Op1, m_Mul(m_Specific(Op0), m_ConstantInt(CI)))) {
582 Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(),1), CI);
583 return BinaryOperator::CreateMul(Op0, CP1);
Chris Lattner53a19b72010-01-05 07:18:46 +0000584 }
585
Chris Lattnerb9b90442011-02-10 05:14:58 +0000586 // X - X<<C --> X * (1-(1<<C))
587 if (match(Op1, m_Shl(m_Specific(Op0), m_ConstantInt(CI)))) {
588 Constant *One = ConstantInt::get(I.getType(), 1);
589 C = ConstantExpr::getSub(One, ConstantExpr::getShl(One, CI));
590 return BinaryOperator::CreateMul(Op0, C);
591 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000592
Chris Lattnerb9b90442011-02-10 05:14:58 +0000593 // X - A*-B -> X + A*B
594 // X - -A*B -> X + A*B
595 Value *A, *B;
596 if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) ||
597 match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B))))
598 return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B));
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000599
Chris Lattnerb9b90442011-02-10 05:14:58 +0000600 // X - A*CI -> X + A*-CI
601 // X - CI*A -> X + A*-CI
602 if (match(Op1, m_Mul(m_Value(A), m_ConstantInt(CI))) ||
603 match(Op1, m_Mul(m_ConstantInt(CI), m_Value(A)))) {
604 Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI));
605 return BinaryOperator::CreateAdd(Op0, NewMul);
Chris Lattner53a19b72010-01-05 07:18:46 +0000606 }
607 }
608
609 ConstantInt *C1;
610 if (Value *X = dyn_castFoldableMul(Op0, C1)) {
611 if (X == Op1) // X*C - X --> X * (C-1)
612 return BinaryOperator::CreateMul(Op1, SubOne(C1));
613
614 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
615 if (X == dyn_castFoldableMul(Op1, C2))
616 return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2));
617 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000618
Chris Lattner53a19b72010-01-05 07:18:46 +0000619 // Optimize pointer differences into the same array into a size. Consider:
620 // &A[10] - &A[0]: we should compile this to "10".
621 if (TD) {
622 Value *LHSOp, *RHSOp;
623 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) &&
624 match(Op1, m_PtrToInt(m_Value(RHSOp))))
625 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
626 return ReplaceInstUsesWith(I, Res);
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000627
Chris Lattner53a19b72010-01-05 07:18:46 +0000628 // trunc(p)-trunc(q) -> trunc(p-q)
629 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) &&
630 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp)))))
631 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType()))
632 return ReplaceInstUsesWith(I, Res);
633 }
Michael Ilseman4d96e6f2012-12-12 20:57:53 +0000634
Chris Lattner53a19b72010-01-05 07:18:46 +0000635 return 0;
636}
637
638Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
639 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
640
Michael Ilsemanc244f382012-12-12 00:28:32 +0000641 if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), TD))
642 return ReplaceInstUsesWith(I, V);
643
Chris Lattner53a19b72010-01-05 07:18:46 +0000644 // If this is a 'B = x-(-A)', change to B = x+A...
645 if (Value *V = dyn_castFNegVal(Op1))
646 return BinaryOperator::CreateFAdd(Op0, V);
647
Chris Lattner53a19b72010-01-05 07:18:46 +0000648 return 0;
649}