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