@@ -88,7 +88,7 @@ bool SCCPSolver::tryToReplaceWithConstant(Value *V) {
8888// / Try to use \p Inst's value range from \p Solver to infer the NUW flag.
8989static bool refineInstruction (SCCPSolver &Solver,
9090 const SmallPtrSetImpl<Value *> &InsertedValues,
91- Instruction &Inst, Statistic &InstRemovedStat ) {
91+ Instruction &Inst) {
9292 bool Changed = false ;
9393 auto GetRange = [&Solver, &InsertedValues](Value *Op) {
9494 if (auto *Const = dyn_cast<Constant>(Op))
@@ -101,9 +101,6 @@ static bool refineInstruction(SCCPSolver &Solver,
101101 Op->getType (), /* UndefAllowed=*/ false );
102102 };
103103
104- Value *X;
105- const APInt *RHSC;
106-
107104 if (isa<OverflowingBinaryOperator>(Inst)) {
108105 if (Inst.hasNoSignedWrap () && Inst.hasNoUnsignedWrap ())
109106 return false ;
@@ -162,15 +159,6 @@ static bool refineInstruction(SCCPSolver &Solver,
162159 GEPNoWrapFlags::noUnsignedWrap ());
163160 Changed = true ;
164161 }
165- } else if (match (&Inst, m_And (m_Value (X), m_LowBitMask (RHSC)))) {
166- ConstantRange LRange = GetRange (Inst.getOperand (0 ));
167- if (!LRange.getUnsignedMax ().ule (*RHSC))
168- return false ;
169-
170- Inst.replaceAllUsesWith (X);
171- Inst.eraseFromParent ();
172- ++InstRemovedStat;
173- Changed = true ;
174162 }
175163
176164 return Changed;
@@ -246,6 +234,33 @@ static bool replaceSignedInst(SCCPSolver &Solver,
246234 return true ;
247235}
248236
237+ // / Try to use \p Inst's value range from \p Solver to simplify it.
238+ static Value *simplifyInstruction (SCCPSolver &Solver,
239+ SmallPtrSetImpl<Value *> &InsertedValues,
240+ Instruction &Inst) {
241+ auto GetRange = [&Solver, &InsertedValues](Value *Op) {
242+ if (auto *Const = dyn_cast<Constant>(Op))
243+ return Const->toConstantRange ();
244+ if (InsertedValues.contains (Op)) {
245+ unsigned Bitwidth = Op->getType ()->getScalarSizeInBits ();
246+ return ConstantRange::getFull (Bitwidth);
247+ }
248+ return Solver.getLatticeValueFor (Op).asConstantRange (
249+ Op->getType (), /* UndefAllowed=*/ false );
250+ };
251+
252+ Value *X;
253+ const APInt *RHSC;
254+ // Remove masking operations.
255+ if (match (&Inst, m_And (m_Value (X), m_LowBitMask (RHSC)))) {
256+ ConstantRange LRange = GetRange (Inst.getOperand (0 ));
257+ if (LRange.getUnsignedMax ().ule (*RHSC))
258+ return X;
259+ }
260+
261+ return nullptr ;
262+ }
263+
249264bool SCCPSolver::simplifyInstsInBlock (BasicBlock &BB,
250265 SmallPtrSetImpl<Value *> &InsertedValues,
251266 Statistic &InstRemovedStat,
@@ -263,8 +278,12 @@ bool SCCPSolver::simplifyInstsInBlock(BasicBlock &BB,
263278 } else if (replaceSignedInst (*this , InsertedValues, Inst)) {
264279 MadeChanges = true ;
265280 ++InstReplacedStat;
266- } else if (refineInstruction (*this , InsertedValues, Inst,
267- InstRemovedStat)) {
281+ } else if (refineInstruction (*this , InsertedValues, Inst)) {
282+ MadeChanges = true ;
283+ } else if (auto *V = simplifyInstruction (*this , InsertedValues, Inst)) {
284+ Inst.replaceAllUsesWith (V);
285+ Inst.eraseFromParent ();
286+ ++InstRemovedStat;
268287 MadeChanges = true ;
269288 }
270289 }
0 commit comments