Index: include/llvm/Support/MathExtras.h =================================================================== --- include/llvm/Support/MathExtras.h +++ include/llvm/Support/MathExtras.h @@ -767,6 +767,28 @@ return Z; } +/// Add two signed integers, X and Y, of type T. Clamp the result to the +/// maximum or minimum representable value of T on overflow. ResultOverflowed +/// indicates if the result is larger than the maximum representable value of +/// type T or smaller than the minimum representable value of type T. +template +typename std::enable_if::value, T>::type +SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) { + typedef typename std::make_unsigned::type U; + bool Dummy; + bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; + T Z = (U)X + (U)Y; + bool OverflowedNegative = (X < 0 && Y < 0 && Z >= 0); + bool OverflowedPositive = (X >= 0 && Y >= 0 && Z < 0); + Overflowed = OverflowedNegative || OverflowedPositive; + if (OverflowedNegative) + return std::numeric_limits::min(); + else if (OverflowedPositive) + return std::numeric_limits::max(); + else + return Z; +} + /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the /// maximum representable value of T on overflow. ResultOverflowed indicates if /// the result is larger than the maximum representable value of type T. Index: lib/Transforms/Vectorize/SLPVectorizer.cpp =================================================================== --- lib/Transforms/Vectorize/SLPVectorizer.cpp +++ lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -4174,7 +4174,8 @@ V.computeMinimumValueSizes(); // Estimate cost. - int Cost = V.getTreeCost() + getReductionCost(TTI, ReducedVals[i]); + int Cost = + SaturatingAdd(V.getTreeCost(), getReductionCost(TTI, ReducedVals[i])); if (Cost >= -SLPCostThreshold) break; Index: test/Transforms/SLPVectorizer/AArch64/gather-root.ll =================================================================== --- test/Transforms/SLPVectorizer/AArch64/gather-root.ll +++ test/Transforms/SLPVectorizer/AArch64/gather-root.ll @@ -1,95 +1,30 @@ -; RUN: opt < %s -slp-vectorizer -S | FileCheck %s --check-prefix=DEFAULT -; RUN: opt < %s -slp-recursion-max-depth=0 -slp-vectorizer -S | FileCheck %s --check-prefix=GATHER +; REQUIRES: asserts +; RUN: opt < %s -slp-recursion-max-depth=0 -slp-vectorizer -S target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" target triple = "aarch64--linux-gnu" -@a = common global [80 x i8] zeroinitializer, align 16 - -; DEFAULT-LABEL: @PR28330( -; DEFAULT: %tmp17 = phi i32 [ %tmp34, %for.body ], [ 0, %entry ] -; DEFAULT: %tmp18 = phi i32 [ %tmp35, %for.body ], [ %n, %entry ] -; DEFAULT: %[[S0:.+]] = select <8 x i1> %1, <8 x i32> , <8 x i32> -; DEFAULT: %[[R0:.+]] = shufflevector <8 x i32> %[[S0]], <8 x i32> undef, <8 x i32> -; DEFAULT: %[[R1:.+]] = add <8 x i32> %[[S0]], %[[R0]] -; DEFAULT: %[[R2:.+]] = shufflevector <8 x i32> %[[R1]], <8 x i32> undef, <8 x i32> -; DEFAULT: %[[R3:.+]] = add <8 x i32> %[[R1]], %[[R2]] -; DEFAULT: %[[R4:.+]] = shufflevector <8 x i32> %[[R3]], <8 x i32> undef, <8 x i32> -; DEFAULT: %[[R5:.+]] = add <8 x i32> %[[R3]], %[[R4]] -; DEFAULT: %[[R6:.+]] = extractelement <8 x i32> %[[R5]], i32 0 -; DEFAULT: %tmp34 = add i32 %[[R6]], %tmp17 -; -; GATHER-LABEL: @PR28330( -; GATHER: %tmp17 = phi i32 [ %tmp34, %for.body ], [ 0, %entry ] -; GATHER: %tmp18 = phi i32 [ %tmp35, %for.body ], [ %n, %entry ] -; GATHER: %tmp19 = select i1 %tmp1, i32 -720, i32 -80 -; GATHER: %tmp21 = select i1 %tmp3, i32 -720, i32 -80 -; GATHER: %tmp23 = select i1 %tmp5, i32 -720, i32 -80 -; GATHER: %tmp25 = select i1 %tmp7, i32 -720, i32 -80 -; GATHER: %tmp27 = select i1 %tmp9, i32 -720, i32 -80 -; GATHER: %tmp29 = select i1 %tmp11, i32 -720, i32 -80 -; GATHER: %tmp31 = select i1 %tmp13, i32 -720, i32 -80 -; GATHER: %tmp33 = select i1 %tmp15, i32 -720, i32 -80 -; GATHER: %[[I0:.+]] = insertelement <8 x i32> undef, i32 %tmp19, i32 0 -; GATHER: %[[I1:.+]] = insertelement <8 x i32> %[[I0]], i32 %tmp21, i32 1 -; GATHER: %[[I2:.+]] = insertelement <8 x i32> %[[I1]], i32 %tmp23, i32 2 -; GATHER: %[[I3:.+]] = insertelement <8 x i32> %[[I2]], i32 %tmp25, i32 3 -; GATHER: %[[I4:.+]] = insertelement <8 x i32> %[[I3]], i32 %tmp27, i32 4 -; GATHER: %[[I5:.+]] = insertelement <8 x i32> %[[I4]], i32 %tmp29, i32 5 -; GATHER: %[[I6:.+]] = insertelement <8 x i32> %[[I5]], i32 %tmp31, i32 6 -; GATHER: %[[I7:.+]] = insertelement <8 x i32> %[[I6]], i32 %tmp33, i32 7 -; GATHER: %[[R0:.+]] = shufflevector <8 x i32> %[[I7]], <8 x i32> undef, <8 x i32> -; GATHER: %[[R1:.+]] = add <8 x i32> %[[I7]], %[[R0]] -; GATHER: %[[R2:.+]] = shufflevector <8 x i32> %[[R1]], <8 x i32> undef, <8 x i32> -; GATHER: %[[R3:.+]] = add <8 x i32> %[[R1]], %[[R2]] -; GATHER: %[[R4:.+]] = shufflevector <8 x i32> %[[R3]], <8 x i32> undef, <8 x i32> -; GATHER: %[[R5:.+]] = add <8 x i32> %[[R3]], %[[R4]] -; GATHER: %[[R6:.+]] = extractelement <8 x i32> %[[R5]], i32 0 -; GATHER: %tmp34 = add i32 %[[R6]], %tmp17 - -define void @PR28330(i32 %n) { +define void @PR28330() { entry: - %tmp0 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 1), align 1 - %tmp1 = icmp eq i8 %tmp0, 0 - %tmp2 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 2), align 2 - %tmp3 = icmp eq i8 %tmp2, 0 - %tmp4 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 3), align 1 - %tmp5 = icmp eq i8 %tmp4, 0 - %tmp6 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 4), align 4 - %tmp7 = icmp eq i8 %tmp6, 0 - %tmp8 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 5), align 1 - %tmp9 = icmp eq i8 %tmp8, 0 - %tmp10 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 6), align 2 - %tmp11 = icmp eq i8 %tmp10, 0 - %tmp12 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 7), align 1 - %tmp13 = icmp eq i8 %tmp12, 0 - %tmp14 = load i8, i8* getelementptr inbounds ([80 x i8], [80 x i8]* @a, i64 0, i64 8), align 8 - %tmp15 = icmp eq i8 %tmp14, 0 br label %for.body for.body: - %tmp17 = phi i32 [ %tmp34, %for.body ], [ 0, %entry ] - %tmp18 = phi i32 [ %tmp35, %for.body ], [ %n, %entry ] - %tmp19 = select i1 %tmp1, i32 -720, i32 -80 - %tmp20 = add i32 %tmp17, %tmp19 - %tmp21 = select i1 %tmp3, i32 -720, i32 -80 - %tmp22 = add i32 %tmp20, %tmp21 - %tmp23 = select i1 %tmp5, i32 -720, i32 -80 - %tmp24 = add i32 %tmp22, %tmp23 - %tmp25 = select i1 %tmp7, i32 -720, i32 -80 - %tmp26 = add i32 %tmp24, %tmp25 - %tmp27 = select i1 %tmp9, i32 -720, i32 -80 - %tmp28 = add i32 %tmp26, %tmp27 - %tmp29 = select i1 %tmp11, i32 -720, i32 -80 - %tmp30 = add i32 %tmp28, %tmp29 - %tmp31 = select i1 %tmp13, i32 -720, i32 -80 - %tmp32 = add i32 %tmp30, %tmp31 - %tmp33 = select i1 %tmp15, i32 -720, i32 -80 - %tmp34 = add i32 %tmp32, %tmp33 - %tmp35 = add nsw i32 %tmp18, -1 - %tmp36 = icmp eq i32 %tmp35, 0 - br i1 %tmp36, label %for.end, label %for.body - -for.end: - ret void + %s.047 = phi i32 [ 0, %entry ], [ %add82, %for.body ] + %sub5.sub = select i1 undef, i32 undef, i32 undef + %add = add nsw i32 %sub5.sub, %s.047 + %v.1 = select i1 undef, i32 undef, i32 undef + %add16 = add nsw i32 %add, %v.1 + %sub25.sub21 = select i1 undef, i32 undef, i32 undef + %add27 = add nsw i32 %add16, %sub25.sub21 + %v.3 = select i1 undef, i32 undef, i32 undef + %add38 = add nsw i32 %add27, %v.3 + %sub47.sub43 = select i1 undef, i32 undef, i32 undef + %add49 = add nsw i32 %add38, %sub47.sub43 + %v.5 = select i1 undef, i32 undef, i32 undef + %add60 = add nsw i32 %add49, %v.5 + %sub69.sub65 = select i1 undef, i32 undef, i32 undef + %add71 = add nsw i32 %add60, %sub69.sub65 + %v.7 = select i1 undef, i32 undef, i32 undef + %add82 = add nsw i32 %add71, %v.7 + br label %for.body }