diff --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp --- a/llvm/lib/CodeGen/TypePromotion.cpp +++ b/llvm/lib/CodeGen/TypePromotion.cpp @@ -484,7 +484,10 @@ continue; if (auto *Const = dyn_cast(Op)) { - Constant *NewConst = (SafeWrap.contains(I) && i == 1) + // For ICmp, the constant can be on the LHS or RHS. For Add/Sub the + // constant we checked for sext is on the RHS. + Constant *NewConst = (SafeWrap.contains(I) && + (I->getOpcode() == Instruction::ICmp || i == 1)) ? ConstantExpr::getSExt(Const, ExtTy) : ConstantExpr::getZExt(Const, ExtTy); I->setOperand(i, NewConst);