@@ -679,15 +679,16 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
679
679
680
680
// Simplify BinOps with their identity values first. They are no-ops and we
681
681
// can always return the other value, including undef or poison values.
682
- // FIXME: remove unnecessary duplicated identity patterns below.
683
- // FIXME: Use AllowRHSConstant with getBinOpIdentity to handle additional ops,
684
- // like X << 0 = X.
685
- Constant *Identity = ConstantExpr::getBinOpIdentity (Opcode, C1->getType ());
686
- if (Identity) {
682
+ if (Constant *Identity = ConstantExpr::getBinOpIdentity (
683
+ Opcode, C1->getType (), /* AllowRHSIdentity*/ false )) {
687
684
if (C1 == Identity)
688
685
return C2;
689
686
if (C2 == Identity)
690
687
return C1;
688
+ } else if (Constant *Identity = ConstantExpr::getBinOpIdentity (
689
+ Opcode, C1->getType (), /* AllowRHSIdentity*/ true )) {
690
+ if (C2 == Identity)
691
+ return C1;
691
692
}
692
693
693
694
// Binary operations propagate poison.
@@ -734,9 +735,6 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
734
735
// X / 0 -> poison
735
736
if (match (C2, m_CombineOr (m_Undef (), m_Zero ())))
736
737
return PoisonValue::get (C2->getType ());
737
- // undef / 1 -> undef
738
- if (match (C2, m_One ()))
739
- return C1;
740
738
// undef / X -> 0 otherwise
741
739
return Constant::getNullValue (C1->getType ());
742
740
case Instruction::URem:
@@ -755,28 +753,19 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
755
753
// X >>l undef -> poison
756
754
if (isa<UndefValue>(C2))
757
755
return PoisonValue::get (C2->getType ());
758
- // undef >>l 0 -> undef
759
- if (match (C2, m_Zero ()))
760
- return C1;
761
756
// undef >>l X -> 0
762
757
return Constant::getNullValue (C1->getType ());
763
758
case Instruction::AShr:
764
759
// X >>a undef -> poison
765
760
if (isa<UndefValue>(C2))
766
761
return PoisonValue::get (C2->getType ());
767
- // undef >>a 0 -> undef
768
- if (match (C2, m_Zero ()))
769
- return C1;
770
762
// TODO: undef >>a X -> poison if the shift is exact
771
763
// undef >>a X -> 0
772
764
return Constant::getNullValue (C1->getType ());
773
765
case Instruction::Shl:
774
766
// X << undef -> undef
775
767
if (isa<UndefValue>(C2))
776
768
return PoisonValue::get (C2->getType ());
777
- // undef << 0 -> undef
778
- if (match (C2, m_Zero ()))
779
- return C1;
780
769
// undef << X -> 0
781
770
return Constant::getNullValue (C1->getType ());
782
771
case Instruction::FSub:
@@ -810,21 +799,12 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
810
799
// Handle simplifications when the RHS is a constant int.
811
800
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
812
801
switch (Opcode) {
813
- case Instruction::Add:
814
- if (CI2->isZero ()) return C1; // X + 0 == X
815
- break ;
816
- case Instruction::Sub:
817
- if (CI2->isZero ()) return C1; // X - 0 == X
818
- break ;
819
802
case Instruction::Mul:
820
- if (CI2->isZero ()) return C2; // X * 0 == 0
821
- if (CI2->isOne ())
822
- return C1; // X * 1 == X
803
+ if (CI2->isZero ())
804
+ return C2; // X * 0 == 0
823
805
break ;
824
806
case Instruction::UDiv:
825
807
case Instruction::SDiv:
826
- if (CI2->isOne ())
827
- return C1; // X / 1 == X
828
808
if (CI2->isZero ())
829
809
return PoisonValue::get (CI2->getType ()); // X / 0 == poison
830
810
break ;
@@ -836,9 +816,8 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
836
816
return PoisonValue::get (CI2->getType ()); // X % 0 == poison
837
817
break ;
838
818
case Instruction::And:
839
- if (CI2->isZero ()) return C2; // X & 0 == 0
840
- if (CI2->isMinusOne ())
841
- return C1; // X & -1 == X
819
+ if (CI2->isZero ())
820
+ return C2; // X & 0 == 0
842
821
843
822
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
844
823
// If and'ing the address of a global with a constant, fold it.
@@ -880,16 +859,14 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
880
859
}
881
860
break ;
882
861
case Instruction::Or:
883
- if (CI2->isZero ()) return C1; // X | 0 == X
884
862
if (CI2->isMinusOne ())
885
- return C2; // X | -1 == -1
863
+ return C2; // X | -1 == -1
886
864
break ;
887
865
case Instruction::Xor:
888
- if (CI2->isZero ()) return C1; // X ^ 0 == X
889
-
890
866
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
891
867
switch (CE1->getOpcode ()) {
892
- default : break ;
868
+ default :
869
+ break ;
893
870
case Instruction::ICmp:
894
871
case Instruction::FCmp:
895
872
// cmp pred ^ true -> cmp !pred
0 commit comments