Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3125,11 +3125,64 @@ assert(!Op1C->isMinValue(true)); // A >=s MIN -> TRUE return new ICmpInst(ICmpInst::ICMP_SGT, Op0, Builder.getInt(Op1Val - 1)); default: - break; + return nullptr; } } - // TODO: Handle vectors. + // The usual vector types are ConstantDataVector. Exotic vector types are + // ConstantVector. They both derive from Constant. + if (isa(Op1) || isa(Op1)) { + Constant *Op1C = cast(Op1); + Type *Op1Type = Op1->getType(); + unsigned NumElts = Op1Type->getVectorNumElements(); + + // Set the new comparison predicate and splat a vector of 1 or -1 to + // increment or decrement the vector constants. But first, check that no + // elements of the constant vector would overflow/underflow when we + // increment/decrement the constants. + // + // TODO? If the edge cases for vectors were guaranteed to be handled as they + // are for scalar, we could remove the min/max checks here. However, to do + // that, we would have to use insertelement/shufflevector to replace edge + // values. + + CmpInst::Predicate NewPred; + Constant *OneOrNegOne = nullptr; + switch (I.getPredicate()) { + case ICmpInst::ICMP_ULE: + for (unsigned i = 0; i != NumElts; ++i) + if (cast(Op1C->getAggregateElement(i))->isMaxValue(false)) + return nullptr; + NewPred = ICmpInst::ICMP_ULT; + OneOrNegOne = ConstantInt::get(Op1Type, 1); + break; + case ICmpInst::ICMP_SLE: + for (unsigned i = 0; i != NumElts; ++i) + if (cast(Op1C->getAggregateElement(i))->isMaxValue(true)) + return nullptr; + NewPred = ICmpInst::ICMP_SLT; + OneOrNegOne = ConstantInt::get(Op1Type, 1); + break; + case ICmpInst::ICMP_UGE: + for (unsigned i = 0; i != NumElts; ++i) + if (cast(Op1C->getAggregateElement(i))->isMinValue(false)) + return nullptr; + NewPred = ICmpInst::ICMP_UGT; + OneOrNegOne = ConstantInt::get(Op1Type, -1); + break; + case ICmpInst::ICMP_SGE: + for (unsigned i = 0; i != NumElts; ++i) + if (cast(Op1C->getAggregateElement(i))->isMinValue(true)) + return nullptr; + NewPred = ICmpInst::ICMP_SGT; + OneOrNegOne = ConstantInt::get(Op1Type, -1); + break; + default: + return nullptr; + } + + return new ICmpInst(NewPred, Op0, ConstantExpr::getAdd(Op1C, OneOrNegOne)); + } return nullptr; } Index: llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll =================================================================== --- llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll +++ llvm/trunk/test/Transforms/InstCombine/icmp-vec.ll @@ -0,0 +1,124 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + +; Canonicalize vector ge/le comparisons with constants to gt/lt. + +; Normal types are ConstantDataVectors. Test the constant values adjacent to the +; min/max values that we're not allowed to transform. + +define <2 x i1> @sge(<2 x i8> %x) { +; CHECK-LABEL: @sge( +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i8> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp sge <2 x i8> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @uge(<2 x i8> %x) { +; CHECK-LABEL: @uge( +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <2 x i8> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp uge <2 x i8> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @sle(<2 x i8> %x) { +; CHECK-LABEL: @sle( +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i8> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp sle <2 x i8> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @ule(<2 x i8> %x) { +; CHECK-LABEL: @ule( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <2 x i8> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp ule <2 x i8> %x, + ret <2 x i1> %cmp +} + +; Weird types are ConstantVectors, not ConstantDataVectors. For an i3 type: +; Signed min = -4 +; Unsigned min = 0 +; Signed max = 3 +; Unsigned max = 7 + +define <3 x i1> @sge_weird(<3 x i3> %x) { +; CHECK-LABEL: @sge_weird( +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <3 x i3> %x, +; CHECK-NEXT: ret <3 x i1> [[CMP]] +; + %cmp = icmp sge <3 x i3> %x, + ret <3 x i1> %cmp +} + +define <3 x i1> @uge_weird(<3 x i3> %x) { +; CHECK-LABEL: @uge_weird( +; CHECK-NEXT: [[CMP:%.*]] = icmp ugt <3 x i3> %x, +; CHECK-NEXT: ret <3 x i1> [[CMP]] +; + %cmp = icmp uge <3 x i3> %x, + ret <3 x i1> %cmp +} + +define <3 x i1> @sle_weird(<3 x i3> %x) { +; CHECK-LABEL: @sle_weird( +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <3 x i3> %x, +; CHECK-NEXT: ret <3 x i1> [[CMP]] +; + %cmp = icmp sle <3 x i3> %x, + ret <3 x i1> %cmp +} + +define <3 x i1> @ule_weird(<3 x i3> %x) { +; CHECK-LABEL: @ule_weird( +; CHECK-NEXT: [[CMP:%.*]] = icmp ult <3 x i3> %x, +; CHECK-NEXT: ret <3 x i1> [[CMP]] +; + %cmp = icmp ule <3 x i3> %x, + ret <3 x i1> %cmp +} + +; We can't do the transform if any constants are already at the limits. + +define <2 x i1> @sge_min(<2 x i3> %x) { +; CHECK-LABEL: @sge_min( +; CHECK-NEXT: [[CMP:%.*]] = icmp sge <2 x i3> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp sge <2 x i3> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @uge_min(<2 x i3> %x) { +; CHECK-LABEL: @uge_min( +; CHECK-NEXT: [[CMP:%.*]] = icmp uge <2 x i3> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp uge <2 x i3> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @sle_max(<2 x i3> %x) { +; CHECK-LABEL: @sle_max( +; CHECK-NEXT: [[CMP:%.*]] = icmp sle <2 x i3> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp sle <2 x i3> %x, + ret <2 x i1> %cmp +} + +define <2 x i1> @ule_max(<2 x i3> %x) { +; CHECK-LABEL: @ule_max( +; CHECK-NEXT: [[CMP:%.*]] = icmp ule <2 x i3> %x, +; CHECK-NEXT: ret <2 x i1> [[CMP]] +; + %cmp = icmp ule <2 x i3> %x, + ret <2 x i1> %cmp +} + Index: llvm/trunk/test/Transforms/LoopVectorize/if-conversion.ll =================================================================== --- llvm/trunk/test/Transforms/LoopVectorize/if-conversion.ll +++ llvm/trunk/test/Transforms/LoopVectorize/if-conversion.ll @@ -73,7 +73,7 @@ ;CHECK-LABEL: @reduction_func( ;CHECK: load <4 x i32> ;CHECK: add <4 x i32> -;CHECK: icmp sle <4 x i32> +;CHECK: icmp slt <4 x i32> ;CHECK: select <4 x i1> ;CHECK: ret i32 define i32 @reduction_func(i32* nocapture %A, i32 %n) nounwind uwtable readonly ssp {