From: Amara Emerson Date: Fri, 4 Aug 2017 20:19:46 +0000 (+0000) Subject: [SCEV] Preserve NSW information for sext(subtract). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=edab1e126cf15e45bdd3b179180a7d0c706484fe;p=llvm [SCEV] Preserve NSW information for sext(subtract). Pushes the sext onto the operands of a Sub if NSW is present. Also adds support for propagating the nowrap flags of the llvm.ssub.with.overflow intrinsic during analysis. Differential Revision: https://reviews.llvm.org/D35256 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310117 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index d3baf532045..0d1c6d16f41 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -4168,10 +4168,21 @@ static Optional MatchBinaryOp(Value *V, DominatorTree &DT) { } case Intrinsic::ssub_with_overflow: - case Intrinsic::usub_with_overflow: - return BinaryOp(Instruction::Sub, CI->getArgOperand(0), - CI->getArgOperand(1)); + case Intrinsic::usub_with_overflow: { + if (!isOverflowIntrinsicNoWrap(cast(CI), DT)) + return BinaryOp(Instruction::Sub, CI->getArgOperand(0), + CI->getArgOperand(1)); + // The same reasoning as sadd/uadd above. + if (F->getIntrinsicID() == Intrinsic::ssub_with_overflow) + return BinaryOp(Instruction::Sub, CI->getArgOperand(0), + CI->getArgOperand(1), /* IsNSW = */ true, + /* IsNUW = */ false); + else + return BinaryOp(Instruction::Sub, CI->getArgOperand(0), + CI->getArgOperand(1), /* IsNSW = */ false, + /* IsNUW = */ true); + } case Intrinsic::smul_with_overflow: case Intrinsic::umul_with_overflow: return BinaryOp(Instruction::Mul, CI->getArgOperand(0), @@ -5952,6 +5963,21 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); case Instruction::SExt: + if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) { + // The NSW flag of a subtract does not always survive the conversion to + // A + (-1)*B. By pushing sign extension onto its operands we are much + // more likely to preserve NSW and allow later AddRec optimisations. + // + // NOTE: This is effectively duplicating this logic from getSignExtend: + // sext((A + B + ...)) --> (sext(A) + sext(B) + ...) + // but by that point the NSW information has potentially been lost. + if (BO->Opcode == Instruction::Sub && BO->IsNSW) { + Type *Ty = U->getType(); + auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty); + auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty); + return getMinusSCEV(V1, V2, SCEV::FlagNSW); + } + } return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); case Instruction::BitCast: diff --git a/test/Analysis/ScalarEvolution/flags-from-poison.ll b/test/Analysis/ScalarEvolution/flags-from-poison.ll index 15c679a5f10..8733a857cba 100644 --- a/test/Analysis/ScalarEvolution/flags-from-poison.ll +++ b/test/Analysis/ScalarEvolution/flags-from-poison.ll @@ -609,7 +609,7 @@ loop: %index32 = sub nsw i32 %i, %sub ; CHECK: %index64 = -; CHECK: --> {(sext i32 (-1 * %sub) to i64),+,1} +; CHECK: --> {(-1 * (sext i32 %sub to i64)),+,1} {(-1 * %sub),+,1} + %ssub = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %i, i32 %sub) + %val = extractvalue { i32, i1 } %ssub, 0 + %ovfl = extractvalue { i32, i1 } %ssub, 1 + br i1 %ovfl, label %trap, label %cont + +trap: + tail call void @llvm.trap() + unreachable + +cont: +; CHECK: %index64 = +; CHECK: --> {(-1 * (sext i32 %sub to i64)),+,1}