Index: lib/Analysis/ScalarEvolution.cpp =================================================================== --- lib/Analysis/ScalarEvolution.cpp +++ lib/Analysis/ScalarEvolution.cpp @@ -5250,28 +5250,55 @@ break; case Instruction::AShr: - // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. - if (ConstantInt *CI = dyn_cast(BO->RHS)) - if (Operator *L = dyn_cast(BO->LHS)) - if (L->getOpcode() == Instruction::Shl && - L->getOperand(1) == BO->RHS) { - uint64_t BitWidth = getTypeSizeInBits(BO->LHS->getType()); - - // If the shift count is not less than the bitwidth, the result of - // the shift is undefined. Don't try to analyze it, because the - // resolution chosen here may differ from the resolution chosen in - // other parts of the compiler. - if (CI->getValue().uge(BitWidth)) - break; + ConstantInt *CI = dyn_cast(BO->RHS); + Operator *L = dyn_cast(BO->LHS); + if (CI && L) { + if (L->getOpcode() == Instruction::Shl) { + uint64_t BitWidth = getTypeSizeInBits(BO->LHS->getType()); + // If the shift count is not less than the bitwidth, the result of + // the shift is undefined. Don't try to analyze it, because the + // resolution chosen here may differ from the resolution chosen in + // other parts of the compiler. + if (CI->getValue().uge(BitWidth)) + break; + + uint64_t Amt = BitWidth - CI->getZExtValue(); + Type *TruncTy = IntegerType::get(getContext(), Amt); + const SCEV *LOp0Trunc = + getTruncateExpr(getSCEV(L->getOperand(0)), TruncTy); - uint64_t Amt = BitWidth - CI->getZExtValue(); + Value *LOp1 = L->getOperand(1); + Type *SExtTy = BO->LHS->getType(); + // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV + // expression. + if (LOp1 == BO->RHS) { if (Amt == BitWidth) return getSCEV(L->getOperand(0)); // shift by zero --> noop - return getSignExtendExpr( - getTruncateExpr(getSCEV(L->getOperand(0)), - IntegerType::get(getContext(), Amt)), - BO->LHS->getType()); + return getSignExtendExpr(LOp0Trunc, SExtTy); } + + // Hanlde below case: + // %y = shl %x, n + // %z = ashr %y, m + // where n != m + ConstantInt *LCI = dyn_cast(LOp1); + if (!LCI) + break; + if (LCI->getValue().uge(BitWidth)) + break; + + uint64_t AShrAmt = CI->getZExtValue(); + uint64_t LShAmt = LCI->getZExtValue(); + //TODO: when n < m, sext(udiv(trunc(x), 2^(m-n))) + if (AShrAmt > LShAmt) + break; + // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV + // expression. + uint64_t Mul = 1 << (LShAmt - AShrAmt); + const SCEV *MulSCEV = getSCEV(ConstantInt::get(TruncTy, Mul)); + return getSignExtendExpr(getMulExpr(LOp0Trunc, MulSCEV), SExtTy); + } + } break; } } Index: test/Analysis/ScalarEvolution/sext-mul.ll =================================================================== --- /dev/null +++ test/Analysis/ScalarEvolution/sext-mul.ll @@ -0,0 +1,42 @@ +; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s + +; CHECK: %12 = ashr exact i64 %11, 32 +; CHECK: --> (sext i32 {0,+,2}<%9> to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483647) Exits: (sext i32 (-2 + (2 * %2)) to i64) LoopDispositions: { %9: Computable } +; CHECK: %13 = getelementptr inbounds i32, i32* %0, i64 %12 +; CHECK: --> ((4 * (sext i32 {0,+,2}<%9> to i64)) + %0) U: full-set S: full-set Exits: ((4 * (sext i32 (-2 + (2 * %2)) to i64)) + %0) LoopDispositions: { %9: Computable } +; CHECK: %16 = or i64 %12, 1 +; CHECK: --> (1 + (sext i32 {0,+,2}<%9> to i64)) U: [-2147483647,2147483649) S: [-2147483647,2147483648) Exits: (1 + (sext i32 (-2 + (2 * %2)) to i64)) LoopDispositions: { %9: Computable } +; CHECK: %17 = getelementptr inbounds i32, i32* %0, i64 %16 +; CHECK: --> (4 + (4 * (sext i32 {0,+,2}<%9> to i64)) + %0) U: full-set S: full-set Exits: (4 + (4 * (sext i32 (-2 + (2 * %2)) to i64)) + %0) LoopDispositions: { %9: Computable } + +define void @foo(i32* nocapture, i32, i32) { + %4 = icmp sgt i32 %2, 0 + br i1 %4, label %5, label %8 + +;