@@ -923,8 +923,7 @@ static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
923
923
return Remainder.isMinValue ();
924
924
}
925
925
926
- static Instruction *foldIDivShl (BinaryOperator &I,
927
- InstCombiner::BuilderTy &Builder) {
926
+ static Value *foldIDivShl (BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
928
927
assert ((I.getOpcode () == Instruction::SDiv ||
929
928
I.getOpcode () == Instruction::UDiv) &&
930
929
" Expected integer divide" );
@@ -933,7 +932,6 @@ static Instruction *foldIDivShl(BinaryOperator &I,
933
932
Value *Op0 = I.getOperand (0 ), *Op1 = I.getOperand (1 );
934
933
Type *Ty = I.getType ();
935
934
936
- Instruction *Ret = nullptr ;
937
935
Value *X, *Y, *Z;
938
936
939
937
// With appropriate no-wrap constraints, remove a common factor in the
@@ -948,12 +946,12 @@ static Instruction *foldIDivShl(BinaryOperator &I,
948
946
949
947
// (X * Y) u/ (X << Z) --> Y u>> Z
950
948
if (!IsSigned && HasNUW)
951
- Ret = BinaryOperator:: CreateLShr (Y, Z);
949
+ return Builder. CreateLShr (Y, Z, " " , I. isExact () );
952
950
953
951
// (X * Y) s/ (X << Z) --> Y s/ (1 << Z)
954
952
if (IsSigned && HasNSW && (Op0->hasOneUse () || Op1->hasOneUse ())) {
955
953
Value *Shl = Builder.CreateShl (ConstantInt::get (Ty, 1 ), Z);
956
- Ret = BinaryOperator:: CreateSDiv (Y, Shl);
954
+ return Builder. CreateSDiv (Y, Shl, " " , I. isExact () );
957
955
}
958
956
}
959
957
@@ -971,13 +969,13 @@ static Instruction *foldIDivShl(BinaryOperator &I,
971
969
((Shl0->hasNoUnsignedWrap () && Shl1->hasNoUnsignedWrap ()) ||
972
970
(Shl0->hasNoUnsignedWrap () && Shl0->hasNoSignedWrap () &&
973
971
Shl1->hasNoSignedWrap ())))
974
- Ret = BinaryOperator:: CreateUDiv (X, Y);
972
+ return Builder. CreateUDiv (X, Y, " " , I. isExact () );
975
973
976
974
// For signed div, we need 'nsw' on both shifts + 'nuw' on the divisor.
977
975
// (X << Z) / (Y << Z) --> X / Y
978
976
if (IsSigned && Shl0->hasNoSignedWrap () && Shl1->hasNoSignedWrap () &&
979
977
Shl1->hasNoUnsignedWrap ())
980
- Ret = BinaryOperator:: CreateSDiv (X, Y);
978
+ return Builder. CreateSDiv (X, Y, " " , I. isExact () );
981
979
}
982
980
983
981
// If X << Y and X << Z does not overflow, then:
@@ -998,15 +996,11 @@ static Instruction *foldIDivShl(BinaryOperator &I,
998
996
/* HasNSW*/
999
997
IsSigned ? (Shl0->hasNoUnsignedWrap () || Shl1->hasNoUnsignedWrap ())
1000
998
: Shl0->hasNoSignedWrap ());
1001
- Ret = BinaryOperator:: CreateLShr (Dividend, Z);
999
+ return Builder. CreateLShr (Dividend, Z, " " , I. isExact () );
1002
1000
}
1003
1001
}
1004
1002
1005
- if (!Ret)
1006
- return nullptr ;
1007
-
1008
- Ret->setIsExact (I.isExact ());
1009
- return Ret;
1003
+ return nullptr ;
1010
1004
}
1011
1005
1012
1006
// / This function implements the transforms common to both integer division
@@ -1183,8 +1177,8 @@ Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
1183
1177
return NewDiv;
1184
1178
}
1185
1179
1186
- if (Instruction *R = foldIDivShl (I, Builder))
1187
- return R ;
1180
+ if (Value *R = foldIDivShl (I, Builder))
1181
+ return replaceInstUsesWith (I, R) ;
1188
1182
1189
1183
// With the appropriate no-wrap constraint, remove a multiply by the divisor
1190
1184
// after peeking through another divide:
0 commit comments