@@ -5807,15 +5807,13 @@ bool InstCombinerImpl::OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp,
5807
5807
// / \returns Instruction which must replace the compare instruction, NULL if no
5808
5808
// / replacement required.
5809
5809
static Instruction *processUMulZExtIdiom (ICmpInst &I, Value *MulVal,
5810
- Value *OtherVal,
5810
+ const APInt *OtherVal,
5811
5811
InstCombinerImpl &IC) {
5812
5812
// Don't bother doing this transformation for pointers, don't do it for
5813
5813
// vectors.
5814
5814
if (!isa<IntegerType>(MulVal->getType ()))
5815
5815
return nullptr ;
5816
5816
5817
- assert (I.getOperand (0 ) == MulVal || I.getOperand (1 ) == MulVal);
5818
- assert (I.getOperand (0 ) == OtherVal || I.getOperand (1 ) == OtherVal);
5819
5817
auto *MulInstr = dyn_cast<Instruction>(MulVal);
5820
5818
if (!MulInstr)
5821
5819
return nullptr ;
@@ -5875,28 +5873,26 @@ static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
5875
5873
5876
5874
// Recognize patterns
5877
5875
switch (I.getPredicate ()) {
5878
- case ICmpInst::ICMP_UGT:
5876
+ case ICmpInst::ICMP_UGT: {
5879
5877
// Recognize pattern:
5880
5878
// mulval = mul(zext A, zext B)
5881
5879
// cmp ugt mulval, max
5882
- if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
5883
- APInt MaxVal = APInt::getMaxValue (MulWidth);
5884
- MaxVal = MaxVal.zext (CI->getBitWidth ());
5885
- if (MaxVal.eq (CI->getValue ()))
5886
- break ; // Recognized
5887
- }
5880
+ APInt MaxVal = APInt::getMaxValue (MulWidth);
5881
+ MaxVal = MaxVal.zext (OtherVal->getBitWidth ());
5882
+ if (MaxVal.eq (*OtherVal))
5883
+ break ; // Recognized
5888
5884
return nullptr ;
5885
+ }
5889
5886
5890
- case ICmpInst::ICMP_ULT:
5887
+ case ICmpInst::ICMP_ULT: {
5891
5888
// Recognize pattern:
5892
5889
// mulval = mul(zext A, zext B)
5893
5890
// cmp ule mulval, max + 1
5894
- if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
5895
- APInt MaxVal = APInt::getOneBitSet (CI->getBitWidth (), MulWidth);
5896
- if (MaxVal.eq (CI->getValue ()))
5897
- break ; // Recognized
5898
- }
5891
+ APInt MaxVal = APInt::getOneBitSet (OtherVal->getBitWidth (), MulWidth);
5892
+ if (MaxVal.eq (*OtherVal))
5893
+ break ; // Recognized
5899
5894
return nullptr ;
5895
+ }
5900
5896
5901
5897
default :
5902
5898
return nullptr ;
@@ -5922,7 +5918,7 @@ static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
5922
5918
if (MulVal->hasNUsesOrMore (2 )) {
5923
5919
Value *Mul = Builder.CreateExtractValue (Call, 0 , " umul.value" );
5924
5920
for (User *U : make_early_inc_range (MulVal->users ())) {
5925
- if (U == &I || U == OtherVal )
5921
+ if (U == &I)
5926
5922
continue ;
5927
5923
if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
5928
5924
if (TI->getType ()->getPrimitiveSizeInBits () == MulWidth)
@@ -5943,27 +5939,10 @@ static Instruction *processUMulZExtIdiom(ICmpInst &I, Value *MulVal,
5943
5939
IC.addToWorklist (cast<Instruction>(U));
5944
5940
}
5945
5941
}
5946
- if (isa<Instruction>(OtherVal))
5947
- IC.addToWorklist (cast<Instruction>(OtherVal));
5948
5942
5949
5943
// The original icmp gets replaced with the overflow value, maybe inverted
5950
5944
// depending on predicate.
5951
- bool Inverse = false ;
5952
- switch (I.getPredicate ()) {
5953
- case ICmpInst::ICMP_UGT:
5954
- if (I.getOperand (0 ) == MulVal)
5955
- break ;
5956
- Inverse = true ;
5957
- break ;
5958
- case ICmpInst::ICMP_ULT:
5959
- if (I.getOperand (1 ) == MulVal)
5960
- break ;
5961
- Inverse = true ;
5962
- break ;
5963
- default :
5964
- llvm_unreachable (" Unexpected predicate" );
5965
- }
5966
- if (Inverse) {
5945
+ if (I.getPredicate () == ICmpInst::ICMP_ULT) {
5967
5946
Value *Res = Builder.CreateExtractValue (Call, 1 );
5968
5947
return BinaryOperator::CreateNot (Res);
5969
5948
}
@@ -7083,12 +7062,9 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
7083
7062
}
7084
7063
7085
7064
// (zext a) * (zext b) --> llvm.umul.with.overflow.
7086
- if (match (Op0, m_NUWMul (m_ZExt (m_Value (A)), m_ZExt (m_Value (B))))) {
7087
- if (Instruction *R = processUMulZExtIdiom (I, Op0, Op1, *this ))
7088
- return R;
7089
- }
7090
- if (match (Op1, m_NUWMul (m_ZExt (m_Value (A)), m_ZExt (m_Value (B))))) {
7091
- if (Instruction *R = processUMulZExtIdiom (I, Op1, Op0, *this ))
7065
+ if (match (Op0, m_NUWMul (m_ZExt (m_Value (A)), m_ZExt (m_Value (B)))) &&
7066
+ match (Op1, m_APInt (C))) {
7067
+ if (Instruction *R = processUMulZExtIdiom (I, Op0, C, *this ))
7092
7068
return R;
7093
7069
}
7094
7070
0 commit comments