Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/Transforms/InstCombine/InstCombineAddSub.cpp @ 80:67baa08a3894
update to LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 25 Sep 2014 16:56:18 +0900 |
parents | 54457678186b |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
76:9e74acfe8c42 | 80:67baa08a3894 |
---|---|
13 | 13 |
14 #include "InstCombine.h" | 14 #include "InstCombine.h" |
15 #include "llvm/ADT/STLExtras.h" | 15 #include "llvm/ADT/STLExtras.h" |
16 #include "llvm/Analysis/InstructionSimplify.h" | 16 #include "llvm/Analysis/InstructionSimplify.h" |
17 #include "llvm/IR/DataLayout.h" | 17 #include "llvm/IR/DataLayout.h" |
18 #include "llvm/Support/GetElementPtrTypeIterator.h" | 18 #include "llvm/IR/GetElementPtrTypeIterator.h" |
19 #include "llvm/Support/PatternMatch.h" | 19 #include "llvm/IR/PatternMatch.h" |
20 using namespace llvm; | 20 using namespace llvm; |
21 using namespace PatternMatch; | 21 using namespace PatternMatch; |
22 | |
23 #define DEBUG_TYPE "instcombine" | |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 /// Class representing coefficient of floating-point addend. | 27 /// Class representing coefficient of floating-point addend. |
26 /// This class needs to be highly efficient, which is especially true for | 28 /// This class needs to be highly efficient, which is especially true for |
28 /// constructor is merely 4-byte-store-zero (Assuming compiler is able to | 30 /// constructor is merely 4-byte-store-zero (Assuming compiler is able to |
29 /// perform write-merging). | 31 /// perform write-merging). |
30 /// | 32 /// |
31 class FAddendCoef { | 33 class FAddendCoef { |
32 public: | 34 public: |
33 // The constructor has to initialize a APFloat, which is uncessary for | 35 // The constructor has to initialize a APFloat, which is unnecessary for |
34 // most addends which have coefficient either 1 or -1. So, the constructor | 36 // most addends which have coefficient either 1 or -1. So, the constructor |
35 // is expensive. In order to avoid the cost of the constructor, we should | 37 // is expensive. In order to avoid the cost of the constructor, we should |
36 // reuse some instances whenever possible. The pre-created instances | 38 // reuse some instances whenever possible. The pre-created instances |
37 // FAddCombine::Add[0-5] embodies this idea. | 39 // FAddCombine::Add[0-5] embodies this idea. |
38 // | 40 // |
110 /// represented as <C, V>, where the V is a symbolic value, and C is a | 112 /// represented as <C, V>, where the V is a symbolic value, and C is a |
111 /// constant coefficient. A constant addend is represented as <C, 0>. | 113 /// constant coefficient. A constant addend is represented as <C, 0>. |
112 /// | 114 /// |
113 class FAddend { | 115 class FAddend { |
114 public: | 116 public: |
115 FAddend() { Val = 0; } | 117 FAddend() { Val = nullptr; } |
116 | 118 |
117 Value *getSymVal (void) const { return Val; } | 119 Value *getSymVal (void) const { return Val; } |
118 const FAddendCoef &getCoef(void) const { return Coeff; } | 120 const FAddendCoef &getCoef(void) const { return Coeff; } |
119 | 121 |
120 bool isConstant() const { return Val == 0; } | 122 bool isConstant() const { return Val == nullptr; } |
121 bool isZero() const { return Coeff.isZero(); } | 123 bool isZero() const { return Coeff.isZero(); } |
122 | 124 |
123 void set(short Coefficient, Value *V) { Coeff.set(Coefficient), Val = V; } | 125 void set(short Coefficient, Value *V) { Coeff.set(Coefficient), Val = V; } |
124 void set(const APFloat& Coefficient, Value *V) | 126 void set(const APFloat& Coefficient, Value *V) |
125 { Coeff.set(Coefficient); Val = V; } | 127 { Coeff.set(Coefficient); Val = V; } |
152 /// FAddCombine is the class for optimizing an unsafe fadd/fsub along | 154 /// FAddCombine is the class for optimizing an unsafe fadd/fsub along |
153 /// with its neighboring at most two instructions. | 155 /// with its neighboring at most two instructions. |
154 /// | 156 /// |
155 class FAddCombine { | 157 class FAddCombine { |
156 public: | 158 public: |
157 FAddCombine(InstCombiner::BuilderTy *B) : Builder(B), Instr(0) {} | 159 FAddCombine(InstCombiner::BuilderTy *B) : Builder(B), Instr(nullptr) {} |
158 Value *simplify(Instruction *FAdd); | 160 Value *simplify(Instruction *FAdd); |
159 | 161 |
160 private: | 162 private: |
161 typedef SmallVector<const FAddend*, 4> AddendVect; | 163 typedef SmallVector<const FAddend*, 4> AddendVect; |
162 | 164 |
173 Value *createFAdd(Value *Opnd0, Value *Opnd1); | 175 Value *createFAdd(Value *Opnd0, Value *Opnd1); |
174 Value *createFMul(Value *Opnd0, Value *Opnd1); | 176 Value *createFMul(Value *Opnd0, Value *Opnd1); |
175 Value *createFDiv(Value *Opnd0, Value *Opnd1); | 177 Value *createFDiv(Value *Opnd0, Value *Opnd1); |
176 Value *createFNeg(Value *V); | 178 Value *createFNeg(Value *V); |
177 Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota); | 179 Value *createNaryFAdd(const AddendVect& Opnds, unsigned InstrQuota); |
178 void createInstPostProc(Instruction *NewInst); | 180 void createInstPostProc(Instruction *NewInst, bool NoNumber = false); |
179 | 181 |
180 InstCombiner::BuilderTy *Builder; | 182 InstCombiner::BuilderTy *Builder; |
181 Instruction *Instr; | 183 Instruction *Instr; |
182 | 184 |
183 private: | 185 private: |
346 // | 348 // |
347 // Legend: A and B are not constant, C is constant | 349 // Legend: A and B are not constant, C is constant |
348 // | 350 // |
349 unsigned FAddend::drillValueDownOneStep | 351 unsigned FAddend::drillValueDownOneStep |
350 (Value *Val, FAddend &Addend0, FAddend &Addend1) { | 352 (Value *Val, FAddend &Addend0, FAddend &Addend1) { |
351 Instruction *I = 0; | 353 Instruction *I = nullptr; |
352 if (Val == 0 || !(I = dyn_cast<Instruction>(Val))) | 354 if (!Val || !(I = dyn_cast<Instruction>(Val))) |
353 return 0; | 355 return 0; |
354 | 356 |
355 unsigned Opcode = I->getOpcode(); | 357 unsigned Opcode = I->getOpcode(); |
356 | 358 |
357 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) { | 359 if (Opcode == Instruction::FAdd || Opcode == Instruction::FSub) { |
358 ConstantFP *C0, *C1; | 360 ConstantFP *C0, *C1; |
359 Value *Opnd0 = I->getOperand(0); | 361 Value *Opnd0 = I->getOperand(0); |
360 Value *Opnd1 = I->getOperand(1); | 362 Value *Opnd1 = I->getOperand(1); |
361 if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero()) | 363 if ((C0 = dyn_cast<ConstantFP>(Opnd0)) && C0->isZero()) |
362 Opnd0 = 0; | 364 Opnd0 = nullptr; |
363 | 365 |
364 if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero()) | 366 if ((C1 = dyn_cast<ConstantFP>(Opnd1)) && C1->isZero()) |
365 Opnd1 = 0; | 367 Opnd1 = nullptr; |
366 | 368 |
367 if (Opnd0) { | 369 if (Opnd0) { |
368 if (!C0) | 370 if (!C0) |
369 Addend0.set(1, Opnd0); | 371 Addend0.set(1, Opnd0); |
370 else | 372 else |
371 Addend0.set(C0, 0); | 373 Addend0.set(C0, nullptr); |
372 } | 374 } |
373 | 375 |
374 if (Opnd1) { | 376 if (Opnd1) { |
375 FAddend &Addend = Opnd0 ? Addend1 : Addend0; | 377 FAddend &Addend = Opnd0 ? Addend1 : Addend0; |
376 if (!C1) | 378 if (!C1) |
377 Addend.set(1, Opnd1); | 379 Addend.set(1, Opnd1); |
378 else | 380 else |
379 Addend.set(C1, 0); | 381 Addend.set(C1, nullptr); |
380 if (Opcode == Instruction::FSub) | 382 if (Opcode == Instruction::FSub) |
381 Addend.negate(); | 383 Addend.negate(); |
382 } | 384 } |
383 | 385 |
384 if (Opnd0 || Opnd1) | 386 if (Opnd0 || Opnd1) |
385 return Opnd0 && Opnd1 ? 2 : 1; | 387 return Opnd0 && Opnd1 ? 2 : 1; |
386 | 388 |
387 // Both operands are zero. Weird! | 389 // Both operands are zero. Weird! |
388 Addend0.set(APFloat(C0->getValueAPF().getSemantics()), 0); | 390 Addend0.set(APFloat(C0->getValueAPF().getSemantics()), nullptr); |
389 return 1; | 391 return 1; |
390 } | 392 } |
391 | 393 |
392 if (I->getOpcode() == Instruction::FMul) { | 394 if (I->getOpcode() == Instruction::FMul) { |
393 Value *V0 = I->getOperand(0); | 395 Value *V0 = I->getOperand(0); |
441 | 443 |
442 Instruction *I0 = dyn_cast<Instruction>(I->getOperand(0)); | 444 Instruction *I0 = dyn_cast<Instruction>(I->getOperand(0)); |
443 Instruction *I1 = dyn_cast<Instruction>(I->getOperand(1)); | 445 Instruction *I1 = dyn_cast<Instruction>(I->getOperand(1)); |
444 | 446 |
445 if (!I0 || !I1 || I0->getOpcode() != I1->getOpcode()) | 447 if (!I0 || !I1 || I0->getOpcode() != I1->getOpcode()) |
446 return 0; | 448 return nullptr; |
447 | 449 |
448 bool isMpy = false; | 450 bool isMpy = false; |
449 if (I0->getOpcode() == Instruction::FMul) | 451 if (I0->getOpcode() == Instruction::FMul) |
450 isMpy = true; | 452 isMpy = true; |
451 else if (I0->getOpcode() != Instruction::FDiv) | 453 else if (I0->getOpcode() != Instruction::FDiv) |
452 return 0; | 454 return nullptr; |
453 | 455 |
454 Value *Opnd0_0 = I0->getOperand(0); | 456 Value *Opnd0_0 = I0->getOperand(0); |
455 Value *Opnd0_1 = I0->getOperand(1); | 457 Value *Opnd0_1 = I0->getOperand(1); |
456 Value *Opnd1_0 = I1->getOperand(0); | 458 Value *Opnd1_0 = I1->getOperand(0); |
457 Value *Opnd1_1 = I1->getOperand(1); | 459 Value *Opnd1_1 = I1->getOperand(1); |
459 // Input Instr I Factor AddSub0 AddSub1 | 461 // Input Instr I Factor AddSub0 AddSub1 |
460 // ---------------------------------------------- | 462 // ---------------------------------------------- |
461 // (x*y) +/- (x*z) x y z | 463 // (x*y) +/- (x*z) x y z |
462 // (y/x) +/- (z/x) x y z | 464 // (y/x) +/- (z/x) x y z |
463 // | 465 // |
464 Value *Factor = 0; | 466 Value *Factor = nullptr; |
465 Value *AddSub0 = 0, *AddSub1 = 0; | 467 Value *AddSub0 = nullptr, *AddSub1 = nullptr; |
466 | 468 |
467 if (isMpy) { | 469 if (isMpy) { |
468 if (Opnd0_0 == Opnd1_0 || Opnd0_0 == Opnd1_1) | 470 if (Opnd0_0 == Opnd1_0 || Opnd0_0 == Opnd1_1) |
469 Factor = Opnd0_0; | 471 Factor = Opnd0_0; |
470 else if (Opnd0_1 == Opnd1_0 || Opnd0_1 == Opnd1_1) | 472 else if (Opnd0_1 == Opnd1_0 || Opnd0_1 == Opnd1_1) |
479 AddSub0 = Opnd0_0; | 481 AddSub0 = Opnd0_0; |
480 AddSub1 = Opnd1_0; | 482 AddSub1 = Opnd1_0; |
481 } | 483 } |
482 | 484 |
483 if (!Factor) | 485 if (!Factor) |
484 return 0; | 486 return nullptr; |
487 | |
488 FastMathFlags Flags; | |
489 Flags.setUnsafeAlgebra(); | |
490 if (I0) Flags &= I->getFastMathFlags(); | |
491 if (I1) Flags &= I->getFastMathFlags(); | |
485 | 492 |
486 // Create expression "NewAddSub = AddSub0 +/- AddsSub1" | 493 // Create expression "NewAddSub = AddSub0 +/- AddsSub1" |
487 Value *NewAddSub = (I->getOpcode() == Instruction::FAdd) ? | 494 Value *NewAddSub = (I->getOpcode() == Instruction::FAdd) ? |
488 createFAdd(AddSub0, AddSub1) : | 495 createFAdd(AddSub0, AddSub1) : |
489 createFSub(AddSub0, AddSub1); | 496 createFSub(AddSub0, AddSub1); |
490 if (ConstantFP *CFP = dyn_cast<ConstantFP>(NewAddSub)) { | 497 if (ConstantFP *CFP = dyn_cast<ConstantFP>(NewAddSub)) { |
491 const APFloat &F = CFP->getValueAPF(); | 498 const APFloat &F = CFP->getValueAPF(); |
492 if (!F.isNormal()) | 499 if (!F.isNormal()) |
493 return 0; | 500 return nullptr; |
494 } | 501 } else if (Instruction *II = dyn_cast<Instruction>(NewAddSub)) |
495 | 502 II->setFastMathFlags(Flags); |
496 if (isMpy) | 503 |
497 return createFMul(Factor, NewAddSub); | 504 if (isMpy) { |
498 | 505 Value *RI = createFMul(Factor, NewAddSub); |
499 return createFDiv(NewAddSub, Factor); | 506 if (Instruction *II = dyn_cast<Instruction>(RI)) |
507 II->setFastMathFlags(Flags); | |
508 return RI; | |
509 } | |
510 | |
511 Value *RI = createFDiv(NewAddSub, Factor); | |
512 if (Instruction *II = dyn_cast<Instruction>(RI)) | |
513 II->setFastMathFlags(Flags); | |
514 return RI; | |
500 } | 515 } |
501 | 516 |
502 Value *FAddCombine::simplify(Instruction *I) { | 517 Value *FAddCombine::simplify(Instruction *I) { |
503 assert(I->hasUnsafeAlgebra() && "Should be in unsafe mode"); | 518 assert(I->hasUnsafeAlgebra() && "Should be in unsafe mode"); |
504 | 519 |
505 // Currently we are not able to handle vector type. | 520 // Currently we are not able to handle vector type. |
506 if (I->getType()->isVectorTy()) | 521 if (I->getType()->isVectorTy()) |
507 return 0; | 522 return nullptr; |
508 | 523 |
509 assert((I->getOpcode() == Instruction::FAdd || | 524 assert((I->getOpcode() == Instruction::FAdd || |
510 I->getOpcode() == Instruction::FSub) && "Expect add/sub"); | 525 I->getOpcode() == Instruction::FSub) && "Expect add/sub"); |
511 | 526 |
512 // Save the instruction before calling other member-functions. | 527 // Save the instruction before calling other member-functions. |
553 // The input instruction is : "I=0.0 +/- V". If the "V" were able to be | 568 // The input instruction is : "I=0.0 +/- V". If the "V" were able to be |
554 // splitted into two addends, say "V = X - Y", the instruction would have | 569 // splitted into two addends, say "V = X - Y", the instruction would have |
555 // been optimized into "I = Y - X" in the previous steps. | 570 // been optimized into "I = Y - X" in the previous steps. |
556 // | 571 // |
557 const FAddendCoef &CE = Opnd0.getCoef(); | 572 const FAddendCoef &CE = Opnd0.getCoef(); |
558 return CE.isOne() ? Opnd0.getSymVal() : 0; | 573 return CE.isOne() ? Opnd0.getSymVal() : nullptr; |
559 } | 574 } |
560 | 575 |
561 // step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1] | 576 // step 4: Try to optimize Opnd0 + Opnd1_0 [+ Opnd1_1] |
562 if (Opnd1_ExpNum) { | 577 if (Opnd1_ExpNum) { |
563 AddendVect AllOpnds; | 578 AddendVect AllOpnds; |
599 // If the resulting expr has constant-addend, this constant-addend is | 614 // If the resulting expr has constant-addend, this constant-addend is |
600 // desirable to reside at the top of the resulting expression tree. Placing | 615 // desirable to reside at the top of the resulting expression tree. Placing |
601 // constant close to supper-expr(s) will potentially reveal some optimization | 616 // constant close to supper-expr(s) will potentially reveal some optimization |
602 // opportunities in super-expr(s). | 617 // opportunities in super-expr(s). |
603 // | 618 // |
604 const FAddend *ConstAdd = 0; | 619 const FAddend *ConstAdd = nullptr; |
605 | 620 |
606 // Simplified addends are placed <SimpVect>. | 621 // Simplified addends are placed <SimpVect>. |
607 AddendVect SimpVect; | 622 AddendVect SimpVect; |
608 | 623 |
609 // The outer loop works on one symbolic-value at a time. Suppose the input | 624 // The outer loop works on one symbolic-value at a time. Suppose the input |
632 SameSymIdx < AddendNum; SameSymIdx++) { | 647 SameSymIdx < AddendNum; SameSymIdx++) { |
633 const FAddend *T = Addends[SameSymIdx]; | 648 const FAddend *T = Addends[SameSymIdx]; |
634 if (T && T->getSymVal() == Val) { | 649 if (T && T->getSymVal() == Val) { |
635 // Set null such that next iteration of the outer loop will not process | 650 // Set null such that next iteration of the outer loop will not process |
636 // this addend again. | 651 // this addend again. |
637 Addends[SameSymIdx] = 0; | 652 Addends[SameSymIdx] = nullptr; |
638 SimpVect.push_back(T); | 653 SimpVect.push_back(T); |
639 } | 654 } |
640 } | 655 } |
641 | 656 |
642 // If multiple addends share same symbolic value, fold them together. | 657 // If multiple addends share same symbolic value, fold them together. |
646 for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++) | 661 for (unsigned Idx = StartIdx + 1; Idx < SimpVect.size(); Idx++) |
647 R += *SimpVect[Idx]; | 662 R += *SimpVect[Idx]; |
648 | 663 |
649 // Pop all addends being folded and push the resulting folded addend. | 664 // Pop all addends being folded and push the resulting folded addend. |
650 SimpVect.resize(StartIdx); | 665 SimpVect.resize(StartIdx); |
651 if (Val != 0) { | 666 if (Val) { |
652 if (!R.isZero()) { | 667 if (!R.isZero()) { |
653 SimpVect.push_back(&R); | 668 SimpVect.push_back(&R); |
654 } | 669 } |
655 } else { | 670 } else { |
656 // Don't push constant addend at this time. It will be the last element | 671 // Don't push constant addend at this time. It will be the last element |
683 | 698 |
684 // Step 1: Check if the # of instructions needed exceeds the quota. | 699 // Step 1: Check if the # of instructions needed exceeds the quota. |
685 // | 700 // |
686 unsigned InstrNeeded = calcInstrNumber(Opnds); | 701 unsigned InstrNeeded = calcInstrNumber(Opnds); |
687 if (InstrNeeded > InstrQuota) | 702 if (InstrNeeded > InstrQuota) |
688 return 0; | 703 return nullptr; |
689 | 704 |
690 initCreateInstNum(); | 705 initCreateInstNum(); |
691 | 706 |
692 // step 2: Emit the N-ary addition. | 707 // step 2: Emit the N-ary addition. |
693 // Note that at most three instructions are involved in Fadd-InstCombine: the | 708 // Note that at most three instructions are involved in Fadd-InstCombine: the |
695 // The resulting optimized addition should have at least one less instruction | 710 // The resulting optimized addition should have at least one less instruction |
696 // than the original addition expression tree. This implies that the resulting | 711 // than the original addition expression tree. This implies that the resulting |
697 // N-ary addition has at most two instructions, and we don't need to worry | 712 // N-ary addition has at most two instructions, and we don't need to worry |
698 // about tree-height when constructing the N-ary addition. | 713 // about tree-height when constructing the N-ary addition. |
699 | 714 |
700 Value *LastVal = 0; | 715 Value *LastVal = nullptr; |
701 bool LastValNeedNeg = false; | 716 bool LastValNeedNeg = false; |
702 | 717 |
703 // Iterate the addends, creating fadd/fsub using adjacent two addends. | 718 // Iterate the addends, creating fadd/fsub using adjacent two addends. |
704 for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end(); | 719 for (AddendVect::const_iterator I = Opnds.begin(), E = Opnds.end(); |
705 I != E; I++) { | 720 I != E; I++) { |
744 return V; | 759 return V; |
745 } | 760 } |
746 | 761 |
747 Value *FAddCombine::createFNeg(Value *V) { | 762 Value *FAddCombine::createFNeg(Value *V) { |
748 Value *Zero = cast<Value>(ConstantFP::get(V->getType(), 0.0)); | 763 Value *Zero = cast<Value>(ConstantFP::get(V->getType(), 0.0)); |
749 return createFSub(Zero, V); | 764 Value *NewV = createFSub(Zero, V); |
765 if (Instruction *I = dyn_cast<Instruction>(NewV)) | |
766 createInstPostProc(I, true); // fneg's don't receive instruction numbers. | |
767 return NewV; | |
750 } | 768 } |
751 | 769 |
752 Value *FAddCombine::createFAdd | 770 Value *FAddCombine::createFAdd |
753 (Value *Opnd0, Value *Opnd1) { | 771 (Value *Opnd0, Value *Opnd1) { |
754 Value *V = Builder->CreateFAdd(Opnd0, Opnd1); | 772 Value *V = Builder->CreateFAdd(Opnd0, Opnd1); |
769 if (Instruction *I = dyn_cast<Instruction>(V)) | 787 if (Instruction *I = dyn_cast<Instruction>(V)) |
770 createInstPostProc(I); | 788 createInstPostProc(I); |
771 return V; | 789 return V; |
772 } | 790 } |
773 | 791 |
774 void FAddCombine::createInstPostProc(Instruction *NewInstr) { | 792 void FAddCombine::createInstPostProc(Instruction *NewInstr, |
793 bool NoNumber) { | |
775 NewInstr->setDebugLoc(Instr->getDebugLoc()); | 794 NewInstr->setDebugLoc(Instr->getDebugLoc()); |
776 | 795 |
777 // Keep track of the number of instruction created. | 796 // Keep track of the number of instruction created. |
778 incCreateInstNum(); | 797 if (!NoNumber) |
798 incCreateInstNum(); | |
779 | 799 |
780 // Propagate fast-math flags | 800 // Propagate fast-math flags |
781 NewInstr->setFastMathFlags(Instr->getFastMathFlags()); | 801 NewInstr->setFastMathFlags(Instr->getFastMathFlags()); |
782 } | 802 } |
783 | 803 |
843 | 863 |
844 NeedNeg = false; | 864 NeedNeg = false; |
845 return createFMul(OpndVal, Coeff.getValue(Instr->getType())); | 865 return createFMul(OpndVal, Coeff.getValue(Instr->getType())); |
846 } | 866 } |
847 | 867 |
848 /// AddOne - Add one to a ConstantInt. | 868 // If one of the operands only has one non-zero bit, and if the other |
849 static Constant *AddOne(Constant *C) { | 869 // operand has a known-zero bit in a more significant place than it (not |
850 return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); | 870 // including the sign bit) the ripple may go up to and fill the zero, but |
851 } | 871 // won't change the sign. For example, (X & ~4) + 1. |
852 | 872 static bool checkRippleForAdd(const APInt &Op0KnownZero, |
853 /// SubOne - Subtract one from a ConstantInt. | 873 const APInt &Op1KnownZero) { |
854 static Constant *SubOne(ConstantInt *C) { | 874 APInt Op1MaybeOne = ~Op1KnownZero; |
855 return ConstantInt::get(C->getContext(), C->getValue()-1); | 875 // Make sure that one of the operand has at most one bit set to 1. |
856 } | 876 if (Op1MaybeOne.countPopulation() != 1) |
857 | 877 return false; |
858 | 878 |
859 // dyn_castFoldableMul - If this value is a multiply that can be folded into | 879 // Find the most significant known 0 other than the sign bit. |
860 // other computations (because it has a constant operand), return the | 880 int BitWidth = Op0KnownZero.getBitWidth(); |
861 // non-constant operand of the multiply, and set CST to point to the multiplier. | 881 APInt Op0KnownZeroTemp(Op0KnownZero); |
862 // Otherwise, return null. | 882 Op0KnownZeroTemp.clearBit(BitWidth - 1); |
863 // | 883 int Op0ZeroPosition = BitWidth - Op0KnownZeroTemp.countLeadingZeros() - 1; |
864 static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST) { | 884 |
865 if (!V->hasOneUse() || !V->getType()->isIntegerTy()) | 885 int Op1OnePosition = BitWidth - Op1MaybeOne.countLeadingZeros() - 1; |
866 return 0; | 886 assert(Op1OnePosition >= 0); |
867 | 887 |
868 Instruction *I = dyn_cast<Instruction>(V); | 888 // This also covers the case of no known zero, since in that case |
869 if (I == 0) return 0; | 889 // Op0ZeroPosition is -1. |
870 | 890 return Op0ZeroPosition >= Op1OnePosition; |
871 if (I->getOpcode() == Instruction::Mul) | 891 } |
872 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) | |
873 return I->getOperand(0); | |
874 if (I->getOpcode() == Instruction::Shl) | |
875 if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { | |
876 // The multiplier is really 1 << CST. | |
877 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); | |
878 uint32_t CSTVal = CST->getLimitedValue(BitWidth); | |
879 CST = ConstantInt::get(V->getType()->getContext(), | |
880 APInt::getOneBitSet(BitWidth, CSTVal)); | |
881 return I->getOperand(0); | |
882 } | |
883 return 0; | |
884 } | |
885 | |
886 | 892 |
887 /// WillNotOverflowSignedAdd - Return true if we can prove that: | 893 /// WillNotOverflowSignedAdd - Return true if we can prove that: |
888 /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) | 894 /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
889 /// This basically requires proving that the add in the original type would not | 895 /// This basically requires proving that the add in the original type would not |
890 /// overflow to change the sign bit or have a carry out. | 896 /// overflow to change the sign bit or have a carry out. |
891 bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { | 897 /// TODO: Handle this for Vectors. |
898 bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS, | |
899 Instruction *CxtI) { | |
892 // There are different heuristics we can use for this. Here are some simple | 900 // There are different heuristics we can use for this. Here are some simple |
893 // ones. | 901 // ones. |
894 | 902 |
895 // Add has the property that adding any two 2's complement numbers can only | 903 // If LHS and RHS each have at least two sign bits, the addition will look |
896 // have one carry bit which can change a sign. As such, if LHS and RHS each | 904 // like |
897 // have at least two sign bits, we know that the addition of the two values | 905 // |
898 // will sign extend fine. | 906 // XX..... + |
899 if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) | 907 // YY..... |
908 // | |
909 // If the carry into the most significant position is 0, X and Y can't both | |
910 // be 1 and therefore the carry out of the addition is also 0. | |
911 // | |
912 // If the carry into the most significant position is 1, X and Y can't both | |
913 // be 0 and therefore the carry out of the addition is also 1. | |
914 // | |
915 // Since the carry into the most significant position is always equal to | |
916 // the carry out of the addition, there is no signed overflow. | |
917 if (ComputeNumSignBits(LHS, 0, CxtI) > 1 && | |
918 ComputeNumSignBits(RHS, 0, CxtI) > 1) | |
900 return true; | 919 return true; |
901 | 920 |
902 | 921 if (IntegerType *IT = dyn_cast<IntegerType>(LHS->getType())) { |
903 // If one of the operands only has one non-zero bit, and if the other operand | 922 int BitWidth = IT->getBitWidth(); |
904 // has a known-zero bit in a more significant place than it (not including the | 923 APInt LHSKnownZero(BitWidth, 0); |
905 // sign bit) the ripple may go up to and fill the zero, but won't change the | 924 APInt LHSKnownOne(BitWidth, 0); |
906 // sign. For example, (X & ~4) + 1. | 925 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, CxtI); |
907 | 926 |
908 // TODO: Implement. | 927 APInt RHSKnownZero(BitWidth, 0); |
909 | 928 APInt RHSKnownOne(BitWidth, 0); |
929 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, CxtI); | |
930 | |
931 // Addition of two 2's compliment numbers having opposite signs will never | |
932 // overflow. | |
933 if ((LHSKnownOne[BitWidth - 1] && RHSKnownZero[BitWidth - 1]) || | |
934 (LHSKnownZero[BitWidth - 1] && RHSKnownOne[BitWidth - 1])) | |
935 return true; | |
936 | |
937 // Check if carry bit of addition will not cause overflow. | |
938 if (checkRippleForAdd(LHSKnownZero, RHSKnownZero)) | |
939 return true; | |
940 if (checkRippleForAdd(RHSKnownZero, LHSKnownZero)) | |
941 return true; | |
942 } | |
910 return false; | 943 return false; |
911 } | 944 } |
912 | 945 |
946 /// WillNotOverflowUnsignedAdd - Return true if we can prove that: | |
947 /// (zext (add LHS, RHS)) === (add (zext LHS), (zext RHS)) | |
948 bool InstCombiner::WillNotOverflowUnsignedAdd(Value *LHS, Value *RHS, | |
949 Instruction *CxtI) { | |
950 // There are different heuristics we can use for this. Here is a simple one. | |
951 // If the sign bit of LHS and that of RHS are both zero, no unsigned wrap. | |
952 bool LHSKnownNonNegative, LHSKnownNegative; | |
953 bool RHSKnownNonNegative, RHSKnownNegative; | |
954 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, 0, AT, CxtI, DT); | |
955 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, 0, AT, CxtI, DT); | |
956 if (LHSKnownNonNegative && RHSKnownNonNegative) | |
957 return true; | |
958 | |
959 return false; | |
960 } | |
961 | |
962 /// \brief Return true if we can prove that: | |
963 /// (sub LHS, RHS) === (sub nsw LHS, RHS) | |
964 /// This basically requires proving that the add in the original type would not | |
965 /// overflow to change the sign bit or have a carry out. | |
966 /// TODO: Handle this for Vectors. | |
967 bool InstCombiner::WillNotOverflowSignedSub(Value *LHS, Value *RHS, | |
968 Instruction *CxtI) { | |
969 // If LHS and RHS each have at least two sign bits, the subtraction | |
970 // cannot overflow. | |
971 if (ComputeNumSignBits(LHS, 0, CxtI) > 1 && | |
972 ComputeNumSignBits(RHS, 0, CxtI) > 1) | |
973 return true; | |
974 | |
975 if (IntegerType *IT = dyn_cast<IntegerType>(LHS->getType())) { | |
976 unsigned BitWidth = IT->getBitWidth(); | |
977 APInt LHSKnownZero(BitWidth, 0); | |
978 APInt LHSKnownOne(BitWidth, 0); | |
979 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, CxtI); | |
980 | |
981 APInt RHSKnownZero(BitWidth, 0); | |
982 APInt RHSKnownOne(BitWidth, 0); | |
983 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, CxtI); | |
984 | |
985 // Subtraction of two 2's compliment numbers having identical signs will | |
986 // never overflow. | |
987 if ((LHSKnownOne[BitWidth - 1] && RHSKnownOne[BitWidth - 1]) || | |
988 (LHSKnownZero[BitWidth - 1] && RHSKnownZero[BitWidth - 1])) | |
989 return true; | |
990 | |
991 // TODO: implement logic similar to checkRippleForAdd | |
992 } | |
993 return false; | |
994 } | |
995 | |
996 /// \brief Return true if we can prove that: | |
997 /// (sub LHS, RHS) === (sub nuw LHS, RHS) | |
998 bool InstCombiner::WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, | |
999 Instruction *CxtI) { | |
1000 // If the LHS is negative and the RHS is non-negative, no unsigned wrap. | |
1001 bool LHSKnownNonNegative, LHSKnownNegative; | |
1002 bool RHSKnownNonNegative, RHSKnownNegative; | |
1003 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, DL, 0, AT, CxtI, DT); | |
1004 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, DL, 0, AT, CxtI, DT); | |
1005 if (LHSKnownNegative && RHSKnownNonNegative) | |
1006 return true; | |
1007 | |
1008 return false; | |
1009 } | |
1010 | |
1011 // Checks if any operand is negative and we can convert add to sub. | |
1012 // This function checks for following negative patterns | |
1013 // ADD(XOR(OR(Z, NOT(C)), C)), 1) == NEG(AND(Z, C)) | |
1014 // ADD(XOR(AND(Z, C), C), 1) == NEG(OR(Z, ~C)) | |
1015 // XOR(AND(Z, C), (C + 1)) == NEG(OR(Z, ~C)) if C is even | |
1016 static Value *checkForNegativeOperand(BinaryOperator &I, | |
1017 InstCombiner::BuilderTy *Builder) { | |
1018 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); | |
1019 | |
1020 // This function creates 2 instructions to replace ADD, we need at least one | |
1021 // of LHS or RHS to have one use to ensure benefit in transform. | |
1022 if (!LHS->hasOneUse() && !RHS->hasOneUse()) | |
1023 return nullptr; | |
1024 | |
1025 Value *X = nullptr, *Y = nullptr, *Z = nullptr; | |
1026 const APInt *C1 = nullptr, *C2 = nullptr; | |
1027 | |
1028 // if ONE is on other side, swap | |
1029 if (match(RHS, m_Add(m_Value(X), m_One()))) | |
1030 std::swap(LHS, RHS); | |
1031 | |
1032 if (match(LHS, m_Add(m_Value(X), m_One()))) { | |
1033 // if XOR on other side, swap | |
1034 if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1)))) | |
1035 std::swap(X, RHS); | |
1036 | |
1037 if (match(X, m_Xor(m_Value(Y), m_APInt(C1)))) { | |
1038 // X = XOR(Y, C1), Y = OR(Z, C2), C2 = NOT(C1) ==> X == NOT(AND(Z, C1)) | |
1039 // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, AND(Z, C1)) | |
1040 if (match(Y, m_Or(m_Value(Z), m_APInt(C2))) && (*C2 == ~(*C1))) { | |
1041 Value *NewAnd = Builder->CreateAnd(Z, *C1); | |
1042 return Builder->CreateSub(RHS, NewAnd, "sub"); | |
1043 } else if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && (*C1 == *C2)) { | |
1044 // X = XOR(Y, C1), Y = AND(Z, C2), C2 == C1 ==> X == NOT(OR(Z, ~C1)) | |
1045 // ADD(ADD(X, 1), RHS) == ADD(X, ADD(RHS, 1)) == SUB(RHS, OR(Z, ~C1)) | |
1046 Value *NewOr = Builder->CreateOr(Z, ~(*C1)); | |
1047 return Builder->CreateSub(RHS, NewOr, "sub"); | |
1048 } | |
1049 } | |
1050 } | |
1051 | |
1052 // Restore LHS and RHS | |
1053 LHS = I.getOperand(0); | |
1054 RHS = I.getOperand(1); | |
1055 | |
1056 // if XOR is on other side, swap | |
1057 if (match(RHS, m_Xor(m_Value(Y), m_APInt(C1)))) | |
1058 std::swap(LHS, RHS); | |
1059 | |
1060 // C2 is ODD | |
1061 // LHS = XOR(Y, C1), Y = AND(Z, C2), C1 == (C2 + 1) => LHS == NEG(OR(Z, ~C2)) | |
1062 // ADD(LHS, RHS) == SUB(RHS, OR(Z, ~C2)) | |
1063 if (match(LHS, m_Xor(m_Value(Y), m_APInt(C1)))) | |
1064 if (C1->countTrailingZeros() == 0) | |
1065 if (match(Y, m_And(m_Value(Z), m_APInt(C2))) && *C1 == (*C2 + 1)) { | |
1066 Value *NewOr = Builder->CreateOr(Z, ~(*C2)); | |
1067 return Builder->CreateSub(RHS, NewOr, "sub"); | |
1068 } | |
1069 return nullptr; | |
1070 } | |
1071 | |
913 Instruction *InstCombiner::visitAdd(BinaryOperator &I) { | 1072 Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
914 bool Changed = SimplifyAssociativeOrCommutative(I); | 1073 bool Changed = SimplifyAssociativeOrCommutative(I); |
915 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); | 1074 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
916 | 1075 |
917 if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(), | 1076 if (Value *V = SimplifyVectorOp(I)) |
918 I.hasNoUnsignedWrap(), TD)) | 1077 return ReplaceInstUsesWith(I, V); |
919 return ReplaceInstUsesWith(I, V); | 1078 |
920 | 1079 if (Value *V = SimplifyAddInst(LHS, RHS, I.hasNoSignedWrap(), |
921 // (A*B)+(A*C) -> A*(B+C) etc | 1080 I.hasNoUnsignedWrap(), DL, TLI, DT, AT)) |
1081 return ReplaceInstUsesWith(I, V); | |
1082 | |
1083 // (A*B)+(A*C) -> A*(B+C) etc | |
922 if (Value *V = SimplifyUsingDistributiveLaws(I)) | 1084 if (Value *V = SimplifyUsingDistributiveLaws(I)) |
923 return ReplaceInstUsesWith(I, V); | 1085 return ReplaceInstUsesWith(I, V); |
924 | 1086 |
925 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { | 1087 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
926 // X + (signbit) --> X ^ signbit | 1088 // X + (signbit) --> X ^ signbit |
936 // zext(bool) + C -> bool ? C + 1 : C | 1098 // zext(bool) + C -> bool ? C + 1 : C |
937 if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) | 1099 if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) |
938 if (ZI->getSrcTy()->isIntegerTy(1)) | 1100 if (ZI->getSrcTy()->isIntegerTy(1)) |
939 return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI); | 1101 return SelectInst::Create(ZI->getOperand(0), AddOne(CI), CI); |
940 | 1102 |
941 Value *XorLHS = 0; ConstantInt *XorRHS = 0; | 1103 Value *XorLHS = nullptr; ConstantInt *XorRHS = nullptr; |
942 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { | 1104 if (match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { |
943 uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); | 1105 uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); |
944 const APInt &RHSVal = CI->getValue(); | 1106 const APInt &RHSVal = CI->getValue(); |
945 unsigned ExtendAmt = 0; | 1107 unsigned ExtendAmt = 0; |
946 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. | 1108 // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
952 ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1; | 1114 ExtendAmt = TySizeBits - XorRHS->getValue().logBase2() - 1; |
953 } | 1115 } |
954 | 1116 |
955 if (ExtendAmt) { | 1117 if (ExtendAmt) { |
956 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt); | 1118 APInt Mask = APInt::getHighBitsSet(TySizeBits, ExtendAmt); |
957 if (!MaskedValueIsZero(XorLHS, Mask)) | 1119 if (!MaskedValueIsZero(XorLHS, Mask, 0, &I)) |
958 ExtendAmt = 0; | 1120 ExtendAmt = 0; |
959 } | 1121 } |
960 | 1122 |
961 if (ExtendAmt) { | 1123 if (ExtendAmt) { |
962 Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt); | 1124 Constant *ShAmt = ConstantInt::get(I.getType(), ExtendAmt); |
968 // a sub and fuse this add with it. | 1130 // a sub and fuse this add with it. |
969 if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) { | 1131 if (LHS->hasOneUse() && (XorRHS->getValue()+1).isPowerOf2()) { |
970 IntegerType *IT = cast<IntegerType>(I.getType()); | 1132 IntegerType *IT = cast<IntegerType>(I.getType()); |
971 APInt LHSKnownOne(IT->getBitWidth(), 0); | 1133 APInt LHSKnownOne(IT->getBitWidth(), 0); |
972 APInt LHSKnownZero(IT->getBitWidth(), 0); | 1134 APInt LHSKnownZero(IT->getBitWidth(), 0); |
973 ComputeMaskedBits(XorLHS, LHSKnownZero, LHSKnownOne); | 1135 computeKnownBits(XorLHS, LHSKnownZero, LHSKnownOne, 0, &I); |
974 if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue()) | 1136 if ((XorRHS->getValue() | LHSKnownZero).isAllOnesValue()) |
975 return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI), | 1137 return BinaryOperator::CreateSub(ConstantExpr::getAdd(XorRHS, CI), |
976 XorLHS); | 1138 XorLHS); |
977 } | 1139 } |
978 // (X + signbit) + C could have gotten canonicalized to (X ^ signbit) + C, | 1140 // (X + signbit) + C could have gotten canonicalized to (X ^ signbit) + C, |
985 | 1147 |
986 if (isa<Constant>(RHS) && isa<PHINode>(LHS)) | 1148 if (isa<Constant>(RHS) && isa<PHINode>(LHS)) |
987 if (Instruction *NV = FoldOpIntoPhi(I)) | 1149 if (Instruction *NV = FoldOpIntoPhi(I)) |
988 return NV; | 1150 return NV; |
989 | 1151 |
990 if (I.getType()->isIntegerTy(1)) | 1152 if (I.getType()->getScalarType()->isIntegerTy(1)) |
991 return BinaryOperator::CreateXor(LHS, RHS); | 1153 return BinaryOperator::CreateXor(LHS, RHS); |
992 | 1154 |
993 // X + X --> X << 1 | 1155 // X + X --> X << 1 |
994 if (LHS == RHS) { | 1156 if (LHS == RHS) { |
995 BinaryOperator *New = | 1157 BinaryOperator *New = |
1014 // A + -B --> A - B | 1176 // A + -B --> A - B |
1015 if (!isa<Constant>(RHS)) | 1177 if (!isa<Constant>(RHS)) |
1016 if (Value *V = dyn_castNegVal(RHS)) | 1178 if (Value *V = dyn_castNegVal(RHS)) |
1017 return BinaryOperator::CreateSub(LHS, V); | 1179 return BinaryOperator::CreateSub(LHS, V); |
1018 | 1180 |
1019 | 1181 if (Value *V = checkForNegativeOperand(I, Builder)) |
1020 ConstantInt *C2; | 1182 return ReplaceInstUsesWith(I, V); |
1021 if (Value *X = dyn_castFoldableMul(LHS, C2)) { | |
1022 if (X == RHS) // X*C + X --> X * (C+1) | |
1023 return BinaryOperator::CreateMul(RHS, AddOne(C2)); | |
1024 | |
1025 // X*C1 + X*C2 --> X * (C1+C2) | |
1026 ConstantInt *C1; | |
1027 if (X == dyn_castFoldableMul(RHS, C1)) | |
1028 return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2)); | |
1029 } | |
1030 | |
1031 // X + X*C --> X * (C+1) | |
1032 if (dyn_castFoldableMul(RHS, C2) == LHS) | |
1033 return BinaryOperator::CreateMul(LHS, AddOne(C2)); | |
1034 | 1183 |
1035 // A+B --> A|B iff A and B have no bits set in common. | 1184 // A+B --> A|B iff A and B have no bits set in common. |
1036 if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { | 1185 if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
1037 APInt LHSKnownOne(IT->getBitWidth(), 0); | 1186 APInt LHSKnownOne(IT->getBitWidth(), 0); |
1038 APInt LHSKnownZero(IT->getBitWidth(), 0); | 1187 APInt LHSKnownZero(IT->getBitWidth(), 0); |
1039 ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne); | 1188 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, 0, &I); |
1040 if (LHSKnownZero != 0) { | 1189 if (LHSKnownZero != 0) { |
1041 APInt RHSKnownOne(IT->getBitWidth(), 0); | 1190 APInt RHSKnownOne(IT->getBitWidth(), 0); |
1042 APInt RHSKnownZero(IT->getBitWidth(), 0); | 1191 APInt RHSKnownZero(IT->getBitWidth(), 0); |
1043 ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne); | 1192 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, 0, &I); |
1044 | 1193 |
1045 // No bits in common -> bitwise or. | 1194 // No bits in common -> bitwise or. |
1046 if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) | 1195 if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
1047 return BinaryOperator::CreateOr(LHS, RHS); | 1196 return BinaryOperator::CreateOr(LHS, RHS); |
1048 } | 1197 } |
1049 } | 1198 } |
1050 | 1199 |
1051 // W*X + Y*Z --> W * (X+Z) iff W == Y | 1200 if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
1052 { | 1201 Value *X; |
1053 Value *W, *X, *Y, *Z; | 1202 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X |
1054 if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && | 1203 return BinaryOperator::CreateSub(SubOne(CRHS), X); |
1055 match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { | |
1056 if (W != Y) { | |
1057 if (W == Z) { | |
1058 std::swap(Y, Z); | |
1059 } else if (Y == X) { | |
1060 std::swap(W, X); | |
1061 } else if (X == Z) { | |
1062 std::swap(Y, Z); | |
1063 std::swap(W, X); | |
1064 } | |
1065 } | |
1066 | |
1067 if (W == Y) { | |
1068 Value *NewAdd = Builder->CreateAdd(X, Z, LHS->getName()); | |
1069 return BinaryOperator::CreateMul(W, NewAdd); | |
1070 } | |
1071 } | |
1072 } | 1204 } |
1073 | 1205 |
1074 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { | 1206 if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
1075 Value *X = 0; | |
1076 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X | |
1077 return BinaryOperator::CreateSub(SubOne(CRHS), X); | |
1078 | |
1079 // (X & FF00) + xx00 -> (X+xx00) & FF00 | 1207 // (X & FF00) + xx00 -> (X+xx00) & FF00 |
1208 Value *X; | |
1209 ConstantInt *C2; | |
1080 if (LHS->hasOneUse() && | 1210 if (LHS->hasOneUse() && |
1081 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) && | 1211 match(LHS, m_And(m_Value(X), m_ConstantInt(C2))) && |
1082 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) { | 1212 CRHS->getValue() == (CRHS->getValue() & C2->getValue())) { |
1083 // See if all bits from the first bit set in the Add RHS up are included | 1213 // See if all bits from the first bit set in the Add RHS up are included |
1084 // in the mask. First, get the rightmost bit. | 1214 // in the mask. First, get the rightmost bit. |
1135 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { | 1265 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
1136 Constant *CI = | 1266 Constant *CI = |
1137 ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); | 1267 ConstantExpr::getTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
1138 if (LHSConv->hasOneUse() && | 1268 if (LHSConv->hasOneUse() && |
1139 ConstantExpr::getSExt(CI, I.getType()) == RHSC && | 1269 ConstantExpr::getSExt(CI, I.getType()) == RHSC && |
1140 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { | 1270 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI, &I)) { |
1141 // Insert the new, smaller add. | 1271 // Insert the new, smaller add. |
1142 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), | 1272 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
1143 CI, "addconv"); | 1273 CI, "addconv"); |
1144 return new SExtInst(NewAdd, I.getType()); | 1274 return new SExtInst(NewAdd, I.getType()); |
1145 } | 1275 } |
1151 // single use (so we don't increase the number of sexts), and if the | 1281 // single use (so we don't increase the number of sexts), and if the |
1152 // integer add will not overflow. | 1282 // integer add will not overflow. |
1153 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& | 1283 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
1154 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && | 1284 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
1155 WillNotOverflowSignedAdd(LHSConv->getOperand(0), | 1285 WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
1156 RHSConv->getOperand(0))) { | 1286 RHSConv->getOperand(0), &I)) { |
1157 // Insert the new integer add. | 1287 // Insert the new integer add. |
1158 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), | 1288 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
1159 RHSConv->getOperand(0), "addconv"); | 1289 RHSConv->getOperand(0), "addconv"); |
1160 return new SExtInst(NewAdd, I.getType()); | 1290 return new SExtInst(NewAdd, I.getType()); |
1161 } | 1291 } |
1162 } | 1292 } |
1163 } | 1293 } |
1164 | 1294 |
1165 // Check for (x & y) + (x ^ y) | 1295 // (add (xor A, B) (and A, B)) --> (or A, B) |
1166 { | 1296 { |
1167 Value *A = 0, *B = 0; | 1297 Value *A = nullptr, *B = nullptr; |
1168 if (match(RHS, m_Xor(m_Value(A), m_Value(B))) && | 1298 if (match(RHS, m_Xor(m_Value(A), m_Value(B))) && |
1169 (match(LHS, m_And(m_Specific(A), m_Specific(B))) || | 1299 (match(LHS, m_And(m_Specific(A), m_Specific(B))) || |
1170 match(LHS, m_And(m_Specific(B), m_Specific(A))))) | 1300 match(LHS, m_And(m_Specific(B), m_Specific(A))))) |
1171 return BinaryOperator::CreateOr(A, B); | 1301 return BinaryOperator::CreateOr(A, B); |
1172 | 1302 |
1174 (match(RHS, m_And(m_Specific(A), m_Specific(B))) || | 1304 (match(RHS, m_And(m_Specific(A), m_Specific(B))) || |
1175 match(RHS, m_And(m_Specific(B), m_Specific(A))))) | 1305 match(RHS, m_And(m_Specific(B), m_Specific(A))))) |
1176 return BinaryOperator::CreateOr(A, B); | 1306 return BinaryOperator::CreateOr(A, B); |
1177 } | 1307 } |
1178 | 1308 |
1179 return Changed ? &I : 0; | 1309 // (add (or A, B) (and A, B)) --> (add A, B) |
1310 { | |
1311 Value *A = nullptr, *B = nullptr; | |
1312 if (match(RHS, m_Or(m_Value(A), m_Value(B))) && | |
1313 (match(LHS, m_And(m_Specific(A), m_Specific(B))) || | |
1314 match(LHS, m_And(m_Specific(B), m_Specific(A))))) { | |
1315 auto *New = BinaryOperator::CreateAdd(A, B); | |
1316 New->setHasNoSignedWrap(I.hasNoSignedWrap()); | |
1317 New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | |
1318 return New; | |
1319 } | |
1320 | |
1321 if (match(LHS, m_Or(m_Value(A), m_Value(B))) && | |
1322 (match(RHS, m_And(m_Specific(A), m_Specific(B))) || | |
1323 match(RHS, m_And(m_Specific(B), m_Specific(A))))) { | |
1324 auto *New = BinaryOperator::CreateAdd(A, B); | |
1325 New->setHasNoSignedWrap(I.hasNoSignedWrap()); | |
1326 New->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | |
1327 return New; | |
1328 } | |
1329 } | |
1330 | |
1331 // TODO(jingyue): Consider WillNotOverflowSignedAdd and | |
1332 // WillNotOverflowUnsignedAdd to reduce the number of invocations of | |
1333 // computeKnownBits. | |
1334 if (!I.hasNoSignedWrap() && WillNotOverflowSignedAdd(LHS, RHS, &I)) { | |
1335 Changed = true; | |
1336 I.setHasNoSignedWrap(true); | |
1337 } | |
1338 if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedAdd(LHS, RHS, &I)) { | |
1339 Changed = true; | |
1340 I.setHasNoUnsignedWrap(true); | |
1341 } | |
1342 | |
1343 return Changed ? &I : nullptr; | |
1180 } | 1344 } |
1181 | 1345 |
1182 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { | 1346 Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { |
1183 bool Changed = SimplifyAssociativeOrCommutative(I); | 1347 bool Changed = SimplifyAssociativeOrCommutative(I); |
1184 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); | 1348 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
1185 | 1349 |
1186 if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), TD)) | 1350 if (Value *V = SimplifyVectorOp(I)) |
1351 return ReplaceInstUsesWith(I, V); | |
1352 | |
1353 if (Value *V = SimplifyFAddInst(LHS, RHS, I.getFastMathFlags(), DL, | |
1354 TLI, DT, AT)) | |
1187 return ReplaceInstUsesWith(I, V); | 1355 return ReplaceInstUsesWith(I, V); |
1188 | 1356 |
1189 if (isa<Constant>(RHS)) { | 1357 if (isa<Constant>(RHS)) { |
1190 if (isa<PHINode>(LHS)) | 1358 if (isa<PHINode>(LHS)) |
1191 if (Instruction *NV = FoldOpIntoPhi(I)) | 1359 if (Instruction *NV = FoldOpIntoPhi(I)) |
1196 return NV; | 1364 return NV; |
1197 } | 1365 } |
1198 | 1366 |
1199 // -A + B --> B - A | 1367 // -A + B --> B - A |
1200 // -A + -B --> -(A + B) | 1368 // -A + -B --> -(A + B) |
1201 if (Value *LHSV = dyn_castFNegVal(LHS)) | 1369 if (Value *LHSV = dyn_castFNegVal(LHS)) { |
1202 return BinaryOperator::CreateFSub(RHS, LHSV); | 1370 Instruction *RI = BinaryOperator::CreateFSub(RHS, LHSV); |
1371 RI->copyFastMathFlags(&I); | |
1372 return RI; | |
1373 } | |
1203 | 1374 |
1204 // A + -B --> A - B | 1375 // A + -B --> A - B |
1205 if (!isa<Constant>(RHS)) | 1376 if (!isa<Constant>(RHS)) |
1206 if (Value *V = dyn_castFNegVal(RHS)) | 1377 if (Value *V = dyn_castFNegVal(RHS)) { |
1207 return BinaryOperator::CreateFSub(LHS, V); | 1378 Instruction *RI = BinaryOperator::CreateFSub(LHS, V); |
1379 RI->copyFastMathFlags(&I); | |
1380 return RI; | |
1381 } | |
1208 | 1382 |
1209 // Check for (fadd double (sitofp x), y), see if we can merge this into an | 1383 // Check for (fadd double (sitofp x), y), see if we can merge this into an |
1210 // integer add followed by a promotion. | 1384 // integer add followed by a promotion. |
1211 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { | 1385 if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
1212 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) | 1386 // (fadd double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
1217 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { | 1391 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
1218 Constant *CI = | 1392 Constant *CI = |
1219 ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); | 1393 ConstantExpr::getFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
1220 if (LHSConv->hasOneUse() && | 1394 if (LHSConv->hasOneUse() && |
1221 ConstantExpr::getSIToFP(CI, I.getType()) == CFP && | 1395 ConstantExpr::getSIToFP(CI, I.getType()) == CFP && |
1222 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { | 1396 WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI, &I)) { |
1223 // Insert the new integer add. | 1397 // Insert the new integer add. |
1224 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), | 1398 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
1225 CI, "addconv"); | 1399 CI, "addconv"); |
1226 return new SIToFPInst(NewAdd, I.getType()); | 1400 return new SIToFPInst(NewAdd, I.getType()); |
1227 } | 1401 } |
1233 // single use (so we don't increase the number of int->fp conversions), | 1407 // single use (so we don't increase the number of int->fp conversions), |
1234 // and if the integer add will not overflow. | 1408 // and if the integer add will not overflow. |
1235 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& | 1409 if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
1236 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && | 1410 (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
1237 WillNotOverflowSignedAdd(LHSConv->getOperand(0), | 1411 WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
1238 RHSConv->getOperand(0))) { | 1412 RHSConv->getOperand(0), &I)) { |
1239 // Insert the new integer add. | 1413 // Insert the new integer add. |
1240 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), | 1414 Value *NewAdd = Builder->CreateNSWAdd(LHSConv->getOperand(0), |
1241 RHSConv->getOperand(0),"addconv"); | 1415 RHSConv->getOperand(0),"addconv"); |
1242 return new SIToFPInst(NewAdd, I.getType()); | 1416 return new SIToFPInst(NewAdd, I.getType()); |
1243 } | 1417 } |
1248 { | 1422 { |
1249 Value *A1, *B1, *C1, *A2, *B2, *C2; | 1423 Value *A1, *B1, *C1, *A2, *B2, *C2; |
1250 if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) && | 1424 if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) && |
1251 match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) { | 1425 match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) { |
1252 if (C1 == C2) { | 1426 if (C1 == C2) { |
1253 Constant *Z1=0, *Z2=0; | 1427 Constant *Z1=nullptr, *Z2=nullptr; |
1254 Value *A, *B, *C=C1; | 1428 Value *A, *B, *C=C1; |
1255 if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) { | 1429 if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) { |
1256 Z1 = dyn_cast<Constant>(A1); A = A2; | 1430 Z1 = dyn_cast<Constant>(A1); A = A2; |
1257 Z2 = dyn_cast<Constant>(B2); B = B1; | 1431 Z2 = dyn_cast<Constant>(B2); B = B1; |
1258 } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) { | 1432 } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) { |
1272 if (I.hasUnsafeAlgebra()) { | 1446 if (I.hasUnsafeAlgebra()) { |
1273 if (Value *V = FAddCombine(Builder).simplify(&I)) | 1447 if (Value *V = FAddCombine(Builder).simplify(&I)) |
1274 return ReplaceInstUsesWith(I, V); | 1448 return ReplaceInstUsesWith(I, V); |
1275 } | 1449 } |
1276 | 1450 |
1277 return Changed ? &I : 0; | 1451 return Changed ? &I : nullptr; |
1278 } | 1452 } |
1279 | 1453 |
1280 | 1454 |
1281 /// Optimize pointer differences into the same array into a size. Consider: | 1455 /// Optimize pointer differences into the same array into a size. Consider: |
1282 /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer | 1456 /// &A[10] - &A[0]: we should compile this to "10". LHS/RHS are the pointer |
1283 /// operands to the ptrtoint instructions for the LHS/RHS of the subtract. | 1457 /// operands to the ptrtoint instructions for the LHS/RHS of the subtract. |
1284 /// | 1458 /// |
1285 Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS, | 1459 Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS, |
1286 Type *Ty) { | 1460 Type *Ty) { |
1287 assert(TD && "Must have target data info for this"); | 1461 assert(DL && "Must have target data info for this"); |
1288 | 1462 |
1289 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize | 1463 // If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize |
1290 // this. | 1464 // this. |
1291 bool Swapped = false; | 1465 bool Swapped = false; |
1292 GEPOperator *GEP1 = 0, *GEP2 = 0; | 1466 GEPOperator *GEP1 = nullptr, *GEP2 = nullptr; |
1293 | 1467 |
1294 // For now we require one side to be the base pointer "A" or a constant | 1468 // For now we require one side to be the base pointer "A" or a constant |
1295 // GEP derived from it. | 1469 // GEP derived from it. |
1296 if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { | 1470 if (GEPOperator *LHSGEP = dyn_cast<GEPOperator>(LHS)) { |
1297 // (gep X, ...) - X | 1471 // (gep X, ...) - X |
1325 } | 1499 } |
1326 } | 1500 } |
1327 | 1501 |
1328 // Avoid duplicating the arithmetic if GEP2 has non-constant indices and | 1502 // Avoid duplicating the arithmetic if GEP2 has non-constant indices and |
1329 // multiple users. | 1503 // multiple users. |
1330 if (GEP1 == 0 || | 1504 if (!GEP1 || |
1331 (GEP2 != 0 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse())) | 1505 (GEP2 && !GEP2->hasAllConstantIndices() && !GEP2->hasOneUse())) |
1332 return 0; | 1506 return nullptr; |
1333 | 1507 |
1334 // Emit the offset of the GEP and an intptr_t. | 1508 // Emit the offset of the GEP and an intptr_t. |
1335 Value *Result = EmitGEPOffset(GEP1); | 1509 Value *Result = EmitGEPOffset(GEP1); |
1336 | 1510 |
1337 // If we had a constant expression GEP on the other side offsetting the | 1511 // If we had a constant expression GEP on the other side offsetting the |
1350 | 1524 |
1351 | 1525 |
1352 Instruction *InstCombiner::visitSub(BinaryOperator &I) { | 1526 Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
1353 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 1527 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
1354 | 1528 |
1529 if (Value *V = SimplifyVectorOp(I)) | |
1530 return ReplaceInstUsesWith(I, V); | |
1531 | |
1355 if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(), | 1532 if (Value *V = SimplifySubInst(Op0, Op1, I.hasNoSignedWrap(), |
1356 I.hasNoUnsignedWrap(), TD)) | 1533 I.hasNoUnsignedWrap(), DL, TLI, DT, AT)) |
1357 return ReplaceInstUsesWith(I, V); | 1534 return ReplaceInstUsesWith(I, V); |
1358 | 1535 |
1359 // (A*B)-(A*C) -> A*(B-C) etc | 1536 // (A*B)-(A*C) -> A*(B-C) etc |
1360 if (Value *V = SimplifyUsingDistributiveLaws(I)) | 1537 if (Value *V = SimplifyUsingDistributiveLaws(I)) |
1361 return ReplaceInstUsesWith(I, V); | 1538 return ReplaceInstUsesWith(I, V); |
1362 | 1539 |
1363 // If this is a 'B = x-(-A)', change to B = x+A. This preserves NSW/NUW. | 1540 // If this is a 'B = x-(-A)', change to B = x+A. |
1364 if (Value *V = dyn_castNegVal(Op1)) { | 1541 if (Value *V = dyn_castNegVal(Op1)) { |
1365 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); | 1542 BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); |
1366 Res->setHasNoSignedWrap(I.hasNoSignedWrap()); | 1543 |
1367 Res->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | 1544 if (const auto *BO = dyn_cast<BinaryOperator>(Op1)) { |
1545 assert(BO->getOpcode() == Instruction::Sub && | |
1546 "Expected a subtraction operator!"); | |
1547 if (BO->hasNoSignedWrap() && I.hasNoSignedWrap()) | |
1548 Res->setHasNoSignedWrap(true); | |
1549 } else { | |
1550 if (cast<Constant>(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap()) | |
1551 Res->setHasNoSignedWrap(true); | |
1552 } | |
1553 | |
1368 return Res; | 1554 return Res; |
1369 } | 1555 } |
1370 | 1556 |
1371 if (I.getType()->isIntegerTy(1)) | 1557 if (I.getType()->isIntegerTy(1)) |
1372 return BinaryOperator::CreateXor(Op0, Op1); | 1558 return BinaryOperator::CreateXor(Op0, Op1); |
1373 | 1559 |
1374 // Replace (-1 - A) with (~A). | 1560 // Replace (-1 - A) with (~A). |
1375 if (match(Op0, m_AllOnes())) | 1561 if (match(Op0, m_AllOnes())) |
1376 return BinaryOperator::CreateNot(Op1); | 1562 return BinaryOperator::CreateNot(Op1); |
1377 | 1563 |
1378 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { | 1564 if (Constant *C = dyn_cast<Constant>(Op0)) { |
1379 // C - ~X == X + (1+C) | 1565 // C - ~X == X + (1+C) |
1380 Value *X = 0; | 1566 Value *X = nullptr; |
1381 if (match(Op1, m_Not(m_Value(X)))) | 1567 if (match(Op1, m_Not(m_Value(X)))) |
1382 return BinaryOperator::CreateAdd(X, AddOne(C)); | 1568 return BinaryOperator::CreateAdd(X, AddOne(C)); |
1383 | 1569 |
1570 // Try to fold constant sub into select arguments. | |
1571 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | |
1572 if (Instruction *R = FoldOpIntoSelect(I, SI)) | |
1573 return R; | |
1574 | |
1575 // C-(X+C2) --> (C-C2)-X | |
1576 Constant *C2; | |
1577 if (match(Op1, m_Add(m_Value(X), m_Constant(C2)))) | |
1578 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X); | |
1579 | |
1580 if (SimplifyDemandedInstructionBits(I)) | |
1581 return &I; | |
1582 | |
1583 // Fold (sub 0, (zext bool to B)) --> (sext bool to B) | |
1584 if (C->isNullValue() && match(Op1, m_ZExt(m_Value(X)))) | |
1585 if (X->getType()->getScalarType()->isIntegerTy(1)) | |
1586 return CastInst::CreateSExtOrBitCast(X, Op1->getType()); | |
1587 | |
1588 // Fold (sub 0, (sext bool to B)) --> (zext bool to B) | |
1589 if (C->isNullValue() && match(Op1, m_SExt(m_Value(X)))) | |
1590 if (X->getType()->getScalarType()->isIntegerTy(1)) | |
1591 return CastInst::CreateZExtOrBitCast(X, Op1->getType()); | |
1592 } | |
1593 | |
1594 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { | |
1384 // -(X >>u 31) -> (X >>s 31) | 1595 // -(X >>u 31) -> (X >>s 31) |
1385 // -(X >>s 31) -> (X >>u 31) | 1596 // -(X >>s 31) -> (X >>u 31) |
1386 if (C->isZero()) { | 1597 if (C->isZero()) { |
1387 Value *X; ConstantInt *CI; | 1598 Value *X; ConstantInt *CI; |
1388 if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) && | 1599 if (match(Op1, m_LShr(m_Value(X), m_ConstantInt(CI))) && |
1393 if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) && | 1604 if (match(Op1, m_AShr(m_Value(X), m_ConstantInt(CI))) && |
1394 // Verify we are shifting out everything but the sign bit. | 1605 // Verify we are shifting out everything but the sign bit. |
1395 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1) | 1606 CI->getValue() == I.getType()->getPrimitiveSizeInBits()-1) |
1396 return BinaryOperator::CreateLShr(X, CI); | 1607 return BinaryOperator::CreateLShr(X, CI); |
1397 } | 1608 } |
1398 | |
1399 // Try to fold constant sub into select arguments. | |
1400 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | |
1401 if (Instruction *R = FoldOpIntoSelect(I, SI)) | |
1402 return R; | |
1403 | |
1404 // C-(X+C2) --> (C-C2)-X | |
1405 ConstantInt *C2; | |
1406 if (match(Op1, m_Add(m_Value(X), m_ConstantInt(C2)))) | |
1407 return BinaryOperator::CreateSub(ConstantExpr::getSub(C, C2), X); | |
1408 | |
1409 if (SimplifyDemandedInstructionBits(I)) | |
1410 return &I; | |
1411 | |
1412 // Fold (sub 0, (zext bool to B)) --> (sext bool to B) | |
1413 if (C->isZero() && match(Op1, m_ZExt(m_Value(X)))) | |
1414 if (X->getType()->isIntegerTy(1)) | |
1415 return CastInst::CreateSExtOrBitCast(X, Op1->getType()); | |
1416 | |
1417 // Fold (sub 0, (sext bool to B)) --> (zext bool to B) | |
1418 if (C->isZero() && match(Op1, m_SExt(m_Value(X)))) | |
1419 if (X->getType()->isIntegerTy(1)) | |
1420 return CastInst::CreateZExtOrBitCast(X, Op1->getType()); | |
1421 } | 1609 } |
1422 | 1610 |
1423 | 1611 |
1424 { Value *Y; | 1612 { Value *Y; |
1425 // X-(X+Y) == -Y X-(Y+X) == -Y | 1613 // X-(X+Y) == -Y X-(Y+X) == -Y |
1431 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y)))) | 1619 if (match(Op0, m_Sub(m_Specific(Op1), m_Value(Y)))) |
1432 return BinaryOperator::CreateNeg(Y); | 1620 return BinaryOperator::CreateNeg(Y); |
1433 } | 1621 } |
1434 | 1622 |
1435 if (Op1->hasOneUse()) { | 1623 if (Op1->hasOneUse()) { |
1436 Value *X = 0, *Y = 0, *Z = 0; | 1624 Value *X = nullptr, *Y = nullptr, *Z = nullptr; |
1437 Constant *C = 0; | 1625 Constant *C = nullptr; |
1438 ConstantInt *CI = 0; | 1626 Constant *CI = nullptr; |
1439 | 1627 |
1440 // (X - (Y - Z)) --> (X + (Z - Y)). | 1628 // (X - (Y - Z)) --> (X + (Z - Y)). |
1441 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z)))) | 1629 if (match(Op1, m_Sub(m_Value(Y), m_Value(Z)))) |
1442 return BinaryOperator::CreateAdd(Op0, | 1630 return BinaryOperator::CreateAdd(Op0, |
1443 Builder->CreateSub(Z, Y, Op1->getName())); | 1631 Builder->CreateSub(Z, Y, Op1->getName())); |
1447 if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) || | 1635 if (match(Op1, m_And(m_Value(Y), m_Specific(Op0))) || |
1448 match(Op1, m_And(m_Specific(Op0), m_Value(Y)))) | 1636 match(Op1, m_And(m_Specific(Op0), m_Value(Y)))) |
1449 return BinaryOperator::CreateAnd(Op0, | 1637 return BinaryOperator::CreateAnd(Op0, |
1450 Builder->CreateNot(Y, Y->getName() + ".not")); | 1638 Builder->CreateNot(Y, Y->getName() + ".not")); |
1451 | 1639 |
1452 // 0 - (X sdiv C) -> (X sdiv -C) | 1640 // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow. |
1453 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && | 1641 if (match(Op1, m_SDiv(m_Value(X), m_Constant(C))) && match(Op0, m_Zero()) && |
1454 match(Op0, m_Zero())) | 1642 C->isNotMinSignedValue() && !C->isOneValue()) |
1455 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C)); | 1643 return BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(C)); |
1456 | 1644 |
1457 // 0 - (X << Y) -> (-X << Y) when X is freely negatable. | 1645 // 0 - (X << Y) -> (-X << Y) when X is freely negatable. |
1458 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero())) | 1646 if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero())) |
1459 if (Value *XNeg = dyn_castNegVal(X)) | 1647 if (Value *XNeg = dyn_castNegVal(X)) |
1460 return BinaryOperator::CreateShl(XNeg, Y); | 1648 return BinaryOperator::CreateShl(XNeg, Y); |
1461 | |
1462 // X - X*C --> X * (1-C) | |
1463 if (match(Op1, m_Mul(m_Specific(Op0), m_ConstantInt(CI)))) { | |
1464 Constant *CP1 = ConstantExpr::getSub(ConstantInt::get(I.getType(),1), CI); | |
1465 return BinaryOperator::CreateMul(Op0, CP1); | |
1466 } | |
1467 | |
1468 // X - X<<C --> X * (1-(1<<C)) | |
1469 if (match(Op1, m_Shl(m_Specific(Op0), m_ConstantInt(CI)))) { | |
1470 Constant *One = ConstantInt::get(I.getType(), 1); | |
1471 C = ConstantExpr::getSub(One, ConstantExpr::getShl(One, CI)); | |
1472 return BinaryOperator::CreateMul(Op0, C); | |
1473 } | |
1474 | 1649 |
1475 // X - A*-B -> X + A*B | 1650 // X - A*-B -> X + A*B |
1476 // X - -A*B -> X + A*B | 1651 // X - -A*B -> X + A*B |
1477 Value *A, *B; | 1652 Value *A, *B; |
1478 if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) || | 1653 if (match(Op1, m_Mul(m_Value(A), m_Neg(m_Value(B)))) || |
1479 match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B)))) | 1654 match(Op1, m_Mul(m_Neg(m_Value(A)), m_Value(B)))) |
1480 return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B)); | 1655 return BinaryOperator::CreateAdd(Op0, Builder->CreateMul(A, B)); |
1481 | 1656 |
1482 // X - A*CI -> X + A*-CI | 1657 // X - A*CI -> X + A*-CI |
1483 // X - CI*A -> X + A*-CI | 1658 // X - CI*A -> X + A*-CI |
1484 if (match(Op1, m_Mul(m_Value(A), m_ConstantInt(CI))) || | 1659 if (match(Op1, m_Mul(m_Value(A), m_Constant(CI))) || |
1485 match(Op1, m_Mul(m_ConstantInt(CI), m_Value(A)))) { | 1660 match(Op1, m_Mul(m_Constant(CI), m_Value(A)))) { |
1486 Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI)); | 1661 Value *NewMul = Builder->CreateMul(A, ConstantExpr::getNeg(CI)); |
1487 return BinaryOperator::CreateAdd(Op0, NewMul); | 1662 return BinaryOperator::CreateAdd(Op0, NewMul); |
1488 } | 1663 } |
1489 } | 1664 } |
1490 | 1665 |
1491 ConstantInt *C1; | |
1492 if (Value *X = dyn_castFoldableMul(Op0, C1)) { | |
1493 if (X == Op1) // X*C - X --> X * (C-1) | |
1494 return BinaryOperator::CreateMul(Op1, SubOne(C1)); | |
1495 | |
1496 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) | |
1497 if (X == dyn_castFoldableMul(Op1, C2)) | |
1498 return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); | |
1499 } | |
1500 | |
1501 // Optimize pointer differences into the same array into a size. Consider: | 1666 // Optimize pointer differences into the same array into a size. Consider: |
1502 // &A[10] - &A[0]: we should compile this to "10". | 1667 // &A[10] - &A[0]: we should compile this to "10". |
1503 if (TD) { | 1668 if (DL) { |
1504 Value *LHSOp, *RHSOp; | 1669 Value *LHSOp, *RHSOp; |
1505 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && | 1670 if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && |
1506 match(Op1, m_PtrToInt(m_Value(RHSOp)))) | 1671 match(Op1, m_PtrToInt(m_Value(RHSOp)))) |
1507 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) | 1672 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
1508 return ReplaceInstUsesWith(I, Res); | 1673 return ReplaceInstUsesWith(I, Res); |
1510 // trunc(p)-trunc(q) -> trunc(p-q) | 1675 // trunc(p)-trunc(q) -> trunc(p-q) |
1511 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && | 1676 if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && |
1512 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) | 1677 match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) |
1513 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) | 1678 if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) |
1514 return ReplaceInstUsesWith(I, Res); | 1679 return ReplaceInstUsesWith(I, Res); |
1515 } | 1680 } |
1516 | 1681 |
1517 return 0; | 1682 bool Changed = false; |
1683 if (!I.hasNoSignedWrap() && WillNotOverflowSignedSub(Op0, Op1, &I)) { | |
1684 Changed = true; | |
1685 I.setHasNoSignedWrap(true); | |
1686 } | |
1687 if (!I.hasNoUnsignedWrap() && WillNotOverflowUnsignedSub(Op0, Op1, &I)) { | |
1688 Changed = true; | |
1689 I.setHasNoUnsignedWrap(true); | |
1690 } | |
1691 | |
1692 return Changed ? &I : nullptr; | |
1518 } | 1693 } |
1519 | 1694 |
1520 Instruction *InstCombiner::visitFSub(BinaryOperator &I) { | 1695 Instruction *InstCombiner::visitFSub(BinaryOperator &I) { |
1521 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | 1696 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
1522 | 1697 |
1523 if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), TD)) | 1698 if (Value *V = SimplifyVectorOp(I)) |
1699 return ReplaceInstUsesWith(I, V); | |
1700 | |
1701 if (Value *V = SimplifyFSubInst(Op0, Op1, I.getFastMathFlags(), DL, | |
1702 TLI, DT, AT)) | |
1524 return ReplaceInstUsesWith(I, V); | 1703 return ReplaceInstUsesWith(I, V); |
1525 | 1704 |
1526 if (isa<Constant>(Op0)) | 1705 if (isa<Constant>(Op0)) |
1527 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | 1706 if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
1528 if (Instruction *NV = FoldOpIntoSelect(I, SI)) | 1707 if (Instruction *NV = FoldOpIntoSelect(I, SI)) |
1554 if (I.hasUnsafeAlgebra()) { | 1733 if (I.hasUnsafeAlgebra()) { |
1555 if (Value *V = FAddCombine(Builder).simplify(&I)) | 1734 if (Value *V = FAddCombine(Builder).simplify(&I)) |
1556 return ReplaceInstUsesWith(I, V); | 1735 return ReplaceInstUsesWith(I, V); |
1557 } | 1736 } |
1558 | 1737 |
1559 return 0; | 1738 return nullptr; |
1560 } | 1739 } |