@@ -905,6 +905,34 @@ static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) {
905
905
return nullptr ;
906
906
}
907
907
908
+ // / Given a predicate and two operands, return true if the comparison is true.
909
+ // / This is a helper for div/rem simplification where we return some other value
910
+ // / when we can prove a relationship between the operands.
911
+ static bool isICmpTrue (ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
912
+ const SimplifyQuery &Q, unsigned MaxRecurse) {
913
+ Value *V = SimplifyICmpInst (Pred, LHS, RHS, Q, MaxRecurse);
914
+ Constant *C = dyn_cast_or_null<Constant>(V);
915
+ return (C && C->isAllOnesValue ());
916
+ }
917
+
918
+ // / Return true if we can simplify X / Y to 0. Remainder can adapt that answer
919
+ // / to simplify X % Y to X.
920
+ static bool isDivZero (Value *Op0, Value *Op1, const SimplifyQuery &Q,
921
+ unsigned MaxRecurse, bool IsSigned) {
922
+ // Recursion is always used, so bail out at once if we already hit the limit.
923
+ if (!MaxRecurse--)
924
+ return false ;
925
+
926
+ if (IsSigned) {
927
+ // TODO: Handle signed.
928
+ return false ;
929
+ }
930
+
931
+ // IsSigned == false.
932
+ // Is the quotient unsigned less than the divisor?
933
+ return isICmpTrue (ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse);
934
+ }
935
+
908
936
// / These are simplifications common to SDiv and UDiv.
909
937
static Value *simplifyDiv (Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
910
938
const SimplifyQuery &Q, unsigned MaxRecurse) {
@@ -914,16 +942,16 @@ static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
914
942
if (Value *V = simplifyDivRem (Op0, Op1, true ))
915
943
return V;
916
944
917
- bool isSigned = Opcode == Instruction::SDiv;
945
+ bool IsSigned = Opcode == Instruction::SDiv;
918
946
919
947
// (X * Y) / Y -> X if the multiplication does not overflow.
920
948
Value *X = nullptr , *Y = nullptr ;
921
949
if (match (Op0, m_Mul (m_Value (X), m_Value (Y))) && (X == Op1 || Y == Op1)) {
922
950
if (Y != Op1) std::swap (X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
923
951
OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0);
924
952
// If the Mul knows it does not overflow, then we are good to go.
925
- if ((isSigned && Mul->hasNoSignedWrap ()) ||
926
- (!isSigned && Mul->hasNoUnsignedWrap ()))
953
+ if ((IsSigned && Mul->hasNoSignedWrap ()) ||
954
+ (!IsSigned && Mul->hasNoUnsignedWrap ()))
927
955
return X;
928
956
// If X has the form X = A / Y then X * Y cannot overflow.
929
957
if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
@@ -932,13 +960,13 @@ static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
932
960
}
933
961
934
962
// (X rem Y) / Y -> 0
935
- if ((isSigned && match (Op0, m_SRem (m_Value (), m_Specific (Op1)))) ||
936
- (!isSigned && match (Op0, m_URem (m_Value (), m_Specific (Op1)))))
963
+ if ((IsSigned && match (Op0, m_SRem (m_Value (), m_Specific (Op1)))) ||
964
+ (!IsSigned && match (Op0, m_URem (m_Value (), m_Specific (Op1)))))
937
965
return Constant::getNullValue (Op0->getType ());
938
966
939
967
// (X /u C1) /u C2 -> 0 if C1 * C2 overflow
940
968
ConstantInt *C1, *C2;
941
- if (!isSigned && match (Op0, m_UDiv (m_Value (X), m_ConstantInt (C1))) &&
969
+ if (!IsSigned && match (Op0, m_UDiv (m_Value (X), m_ConstantInt (C1))) &&
942
970
match (Op1, m_ConstantInt (C2))) {
943
971
bool Overflow;
944
972
(void )C1->getValue ().umul_ov (C2->getValue (), Overflow);
@@ -958,6 +986,9 @@ static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
958
986
if (Value *V = ThreadBinOpOverPHI (Opcode, Op0, Op1, Q, MaxRecurse))
959
987
return V;
960
988
989
+ if (isDivZero (Op0, Op1, Q, MaxRecurse, IsSigned))
990
+ return Constant::getNullValue (Op0->getType ());
991
+
961
992
return nullptr ;
962
993
}
963
994
@@ -989,32 +1020,9 @@ static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
989
1020
if (Value *V = ThreadBinOpOverPHI (Opcode, Op0, Op1, Q, MaxRecurse))
990
1021
return V;
991
1022
992
- return nullptr ;
993
- }
994
-
995
- // / Given a predicate and two operands, return true if the comparison is true.
996
- // / This is a helper for div/rem simplification where we return some other value
997
- // / when we can prove a relationship between the operands.
998
- static bool isICmpTrue (ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
999
- const SimplifyQuery &Q, unsigned MaxRecurse) {
1000
- Value *V = SimplifyICmpInst (Pred, LHS, RHS, Q, MaxRecurse);
1001
- Constant *C = dyn_cast_or_null<Constant>(V);
1002
- return (C && C->isAllOnesValue ());
1003
- }
1004
-
1005
- static Value *simplifyUnsignedDivRem (Value *Op0, Value *Op1,
1006
- const SimplifyQuery &Q,
1007
- unsigned MaxRecurse, bool IsDiv) {
1008
- // Recursion is always used, so bail out at once if we already hit the limit.
1009
- if (!MaxRecurse--)
1010
- return nullptr ;
1011
-
1012
- // If we can prove that the quotient is unsigned less than the divisor, then
1013
- // we know the answer:
1014
- // X / Y --> 0
1015
- // X % Y --> X
1016
- if (isICmpTrue (ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse))
1017
- return IsDiv ? Constant::getNullValue (Op0->getType ()) : Op0;
1023
+ // If X / Y == 0, then X % Y == X.
1024
+ if (isDivZero (Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
1025
+ return Op0;
1018
1026
1019
1027
return nullptr ;
1020
1028
}
@@ -1023,10 +1031,7 @@ static Value *simplifyUnsignedDivRem(Value *Op0, Value *Op1,
1023
1031
// / If not, this returns null.
1024
1032
static Value *SimplifySDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q,
1025
1033
unsigned MaxRecurse) {
1026
- if (Value *V = simplifyDiv (Instruction::SDiv, Op0, Op1, Q, MaxRecurse))
1027
- return V;
1028
-
1029
- return nullptr ;
1034
+ return simplifyDiv (Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
1030
1035
}
1031
1036
1032
1037
Value *llvm::SimplifySDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q) {
@@ -1037,13 +1042,7 @@ Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1037
1042
// / If not, this returns null.
1038
1043
static Value *SimplifyUDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q,
1039
1044
unsigned MaxRecurse) {
1040
- if (Value *V = simplifyDiv (Instruction::UDiv, Op0, Op1, Q, MaxRecurse))
1041
- return V;
1042
-
1043
- if (Value *V = simplifyUnsignedDivRem (Op0, Op1, Q, MaxRecurse, true ))
1044
- return V;
1045
-
1046
- return nullptr ;
1045
+ return simplifyDiv (Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
1047
1046
}
1048
1047
1049
1048
Value *llvm::SimplifyUDivInst (Value *Op0, Value *Op1, const SimplifyQuery &Q) {
@@ -1054,10 +1053,7 @@ Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1054
1053
// / If not, this returns null.
1055
1054
static Value *SimplifySRemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q,
1056
1055
unsigned MaxRecurse) {
1057
- if (Value *V = simplifyRem (Instruction::SRem, Op0, Op1, Q, MaxRecurse))
1058
- return V;
1059
-
1060
- return nullptr ;
1056
+ return simplifyRem (Instruction::SRem, Op0, Op1, Q, MaxRecurse);
1061
1057
}
1062
1058
1063
1059
Value *llvm::SimplifySRemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q) {
@@ -1068,13 +1064,7 @@ Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
1068
1064
// / If not, this returns null.
1069
1065
static Value *SimplifyURemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q,
1070
1066
unsigned MaxRecurse) {
1071
- if (Value *V = simplifyRem (Instruction::URem, Op0, Op1, Q, MaxRecurse))
1072
- return V;
1073
-
1074
- if (Value *V = simplifyUnsignedDivRem (Op0, Op1, Q, MaxRecurse, false ))
1075
- return V;
1076
-
1077
- return nullptr ;
1067
+ return simplifyRem (Instruction::URem, Op0, Op1, Q, MaxRecurse);
1078
1068
}
1079
1069
1080
1070
Value *llvm::SimplifyURemInst (Value *Op0, Value *Op1, const SimplifyQuery &Q) {
0 commit comments