Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -80,6 +80,12 @@ APInt AndMask; bool CreateAnd = false; ICmpInst::Predicate Pred = IC->getPredicate(); + + bool ShouldNotVal = !TrueVal.isNullValue(); + ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; + if (ShouldNotVal) + return nullptr; + if (ICmpInst::isEquality(Pred)) { if (!match(IC->getOperand(1), m_Zero())) return nullptr; @@ -146,13 +152,6 @@ } else V = Builder.CreateZExtOrTrunc(V, SelType); - // Okay, now we know that everything is set up, we just don't know whether we - // have a icmp_ne or icmp_eq and whether the true or false val is the zero. - bool ShouldNotVal = !TrueVal.isNullValue(); - ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; - if (ShouldNotVal) - V = Builder.CreateXor(V, ValC); - // Apply an offset if needed. if (!Offset.isNullValue()) V = Builder.CreateAdd(V, ConstantInt::get(V->getType(), Offset)); @@ -795,42 +794,6 @@ Value *CmpLHS = ICI->getOperand(0); Value *CmpRHS = ICI->getOperand(1); - // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1 - // and (X ((X >>s 31) & (C2 - C1)) + C1 - // FIXME: Type and constness constraints could be lifted, but we have to - // watch code size carefully. We should consider xor instead of - // sub/add when we decide to do that. - // TODO: Merge this with foldSelectICmpAnd somehow. - if (CmpLHS->getType()->isIntOrIntVectorTy() && - CmpLHS->getType() == TrueVal->getType()) { - const APInt *C1, *C2; - if (match(TrueVal, m_APInt(C1)) && match(FalseVal, m_APInt(C2))) { - ICmpInst::Predicate Pred = ICI->getPredicate(); - Value *X; - APInt Mask; - if (decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask, false)) { - if (Mask.isSignMask()) { - assert(X == CmpLHS && "Expected to use the compare input directly"); - assert(ICmpInst::isEquality(Pred) && "Expected equality predicate"); - - if (Pred == ICmpInst::ICMP_NE) - std::swap(C1, C2); - - // This shift results in either -1 or 0. - Value *AShr = Builder.CreateAShr(X, Mask.getBitWidth() - 1); - - // Check if we can express the operation with a single or. - if (C2->isAllOnesValue()) - return replaceInstUsesWith(SI, Builder.CreateOr(AShr, *C1)); - - Value *And = Builder.CreateAnd(AShr, *C2 - *C1); - return replaceInstUsesWith(SI, Builder.CreateAdd(And, - ConstantInt::get(And->getType(), *C1))); - } - } - } - } - { const APInt *TrueValC, *FalseValC; if (match(TrueVal, m_APInt(TrueValC)) && Index: test/Transforms/InstCombine/rem.ll =================================================================== --- test/Transforms/InstCombine/rem.ll +++ test/Transforms/InstCombine/rem.ll @@ -354,12 +354,11 @@ define i32 @test18(i16 %x, i32 %y) { ; CHECK-LABEL: @test18( -; CHECK-NEXT: [[TMP1:%.*]] = shl i16 %x, 3 -; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], 32 -; CHECK-NEXT: [[TMP3:%.*]] = xor i16 [[TMP2]], 63 -; CHECK-NEXT: [[TMP4:%.*]] = zext i16 [[TMP3]] to i32 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], %y -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP1:%.*]] = and i16 %x, 4 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i16 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 63, i32 3 +; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP3]], %y +; CHECK-NEXT: ret i32 [[TMP4]] ; %1 = and i16 %x, 4 %2 = icmp ne i16 %1, 0 Index: test/Transforms/InstCombine/select-icmp-and.ll =================================================================== --- test/Transforms/InstCombine/select-icmp-and.ll +++ test/Transforms/InstCombine/select-icmp-and.ll @@ -29,10 +29,9 @@ define i32 @test35(i32 %x) { ; CHECK-LABEL: @test35( -; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X:%.*]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 40 -; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], 60 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 60, i32 100 +; CHECK-NEXT: ret i32 [[COND]] ; %cmp = icmp sge i32 %x, 0 %cond = select i1 %cmp, i32 60, i32 100 @@ -41,10 +40,9 @@ define <2 x i32> @test35vec(<2 x i32> %x) { ; CHECK-LABEL: @test35vec( -; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw <2 x i32> [[TMP2]], -; CHECK-NEXT: ret <2 x i32> [[TMP3]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[CMP]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[COND]] ; %cmp = icmp sge <2 x i32> %x, %cond = select <2 x i1> %cmp, <2 x i32> , <2 x i32> @@ -55,10 +53,9 @@ define i32 @test35_with_trunc(i64 %x) { ; CHECK-LABEL: @test35_with_trunc( ; CHECK-NEXT: [[X1:%.*]] = trunc i64 [[X:%.*]] to i32 -; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X1]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 40 -; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], 60 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X1]], -1 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 60, i32 100 +; CHECK-NEXT: ret i32 [[COND]] ; %x1 = trunc i64 %x to i32 %cmp = icmp sge i32 %x1, 0 @@ -68,10 +65,9 @@ define i32 @test36(i32 %x) { ; CHECK-LABEL: @test36( -; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X:%.*]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -40 -; CHECK-NEXT: [[TMP3:%.*]] = add nsw i32 [[TMP2]], 100 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 0 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 60, i32 100 +; CHECK-NEXT: ret i32 [[COND]] ; %cmp = icmp slt i32 %x, 0 %cond = select i1 %cmp, i32 60, i32 100 @@ -80,10 +76,9 @@ define <2 x i32> @test36vec(<2 x i32> %x) { ; CHECK-LABEL: @test36vec( -; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = add nsw <2 x i32> [[TMP2]], -; CHECK-NEXT: ret <2 x i32> [[TMP3]] +; CHECK-NEXT: [[CMP:%.*]] = icmp slt <2 x i32> [[X:%.*]], zeroinitializer +; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[CMP]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[COND]] ; %cmp = icmp slt <2 x i32> %x, %cond = select <2 x i1> %cmp, <2 x i32> , <2 x i32> @@ -92,9 +87,9 @@ define i32 @test37(i32 %x) { ; CHECK-LABEL: @test37( -; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X:%.*]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = or i32 [[TMP1]], 1 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1 +; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 1, i32 -1 +; CHECK-NEXT: ret i32 [[COND]] ; %cmp = icmp sgt i32 %x, -1 %cond = select i1 %cmp, i32 1, i32 -1 @@ -103,9 +98,9 @@ define <2 x i32> @test37vec(<2 x i32> %x) { ; CHECK-LABEL: @test37vec( -; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = or <2 x i32> [[TMP1]], -; CHECK-NEXT: ret <2 x i32> [[TMP2]] +; CHECK-NEXT: [[CMP:%.*]] = icmp sgt <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[CMP]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[COND]] ; %cmp = icmp sgt <2 x i32> %x, %cond = select <2 x i1> %cmp, <2 x i32> , <2 x i32> @@ -114,11 +109,10 @@ define i32 @test65(i64 %x) { ; CHECK-LABEL: @test65( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[X:%.*]], 3 -; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], 2 -; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], 42 -; CHECK-NEXT: ret i32 [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], 16 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 42, i32 40 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = and i64 %x, 16 %2 = icmp ne i64 %1, 0 @@ -128,11 +122,10 @@ define <2 x i32> @test65vec(<2 x i64> %x) { ; CHECK-LABEL: @test65vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i64> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32> -; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP2]], -; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i32> [[TMP3]], -; CHECK-NEXT: ret <2 x i32> [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i64> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = and <2 x i64> %x, %2 = icmp ne <2 x i64> %1, zeroinitializer @@ -142,11 +135,10 @@ define i32 @test66(i64 %x) { ; CHECK-LABEL: @test66( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[X:%.*]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], 2 -; CHECK-NEXT: [[TMP4:%.*]] = xor i32 [[TMP3]], 42 -; CHECK-NEXT: ret i32 [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], 4294967296 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i64 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 42, i32 40 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = and i64 %x, 4294967296 %2 = icmp ne i64 %1, 0 @@ -156,11 +148,10 @@ define <2 x i32> @test66vec(<2 x i64> %x) { ; CHECK-LABEL: @test66vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i64> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32> -; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP2]], -; CHECK-NEXT: [[TMP4:%.*]] = xor <2 x i32> [[TMP3]], -; CHECK-NEXT: ret <2 x i32> [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i64> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i64> [[TMP1]], zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = and <2 x i64> %x, %2 = icmp ne <2 x i64> %1, zeroinitializer @@ -184,11 +175,10 @@ define i32 @test67(i16 %x) { ; CHECK-LABEL: @test67( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i16 [[X:%.*]], 1 -; CHECK-NEXT: [[TMP2:%.*]] = and i16 [[TMP1]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = xor i16 [[TMP2]], 42 -; CHECK-NEXT: [[TMP4:%.*]] = zext i16 [[TMP3]] to i32 -; CHECK-NEXT: ret i32 [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[X:%.*]], 4 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i16 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 42, i32 40 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = and i16 %x, 4 %2 = icmp ne i16 %1, 0 @@ -198,11 +188,10 @@ define <2 x i32> @test67vec(<2 x i16> %x) { ; CHECK-LABEL: @test67vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i16> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i16> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i16> [[TMP2]], -; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i16> [[TMP3]] to <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[TMP4]] +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i16> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i16> [[TMP1]], zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = and <2 x i16> %x, %2 = icmp ne <2 x i16> %1, zeroinitializer @@ -212,9 +201,9 @@ define i32 @test71(i32 %x) { ; CHECK-LABEL: @test71( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 6 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[TMP2]], 42 +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 128 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 42, i32 40 ; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = and i32 %x, 128 @@ -225,9 +214,9 @@ define <2 x i32> @test71vec(<2 x i32> %x) { ; CHECK-LABEL: @test71vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = xor <2 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = and <2 x i32> %x, @@ -238,9 +227,9 @@ define i32 @test72(i32 %x) { ; CHECK-LABEL: @test72( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 6 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP2]], 40 +; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 128 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 40, i32 42 ; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = and i32 %x, 128 @@ -251,9 +240,9 @@ define <2 x i32> @test72vec(<2 x i32> %x) { ; CHECK-LABEL: @test72vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = and <2 x i32> %x, @@ -264,9 +253,9 @@ define i32 @test73(i32 %x) { ; CHECK-LABEL: @test73( -; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 6 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP2]], 40 +; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[X:%.*]] to i8 +; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt i8 [[TMP1]], -1 +; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP2]], i32 40, i32 42 ; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = trunc i32 %x to i8 @@ -277,9 +266,9 @@ define <2 x i32> @test73vec(<2 x i32> %x) { ; CHECK-LABEL: @test73vec( -; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP2]], +; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8> +; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt <2 x i8> [[TMP1]], +; CHECK-NEXT: [[TMP3:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = trunc <2 x i32> %x to <2 x i8> @@ -290,10 +279,9 @@ define i32 @test74(i32 %x) { ; CHECK-LABEL: @test74( -; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[X:%.*]], 31 -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = or i32 [[TMP2]], 40 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -1 +; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 40, i32 42 +; CHECK-NEXT: ret i32 [[TMP2]] ; %1 = icmp sgt i32 %x, -1 %2 = select i1 %1, i32 40, i32 42 @@ -302,10 +290,9 @@ define <2 x i32> @test74vec(<2 x i32> %x) { ; CHECK-LABEL: @test74vec( -; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP1]], -; CHECK-NEXT: [[TMP3:%.*]] = or <2 x i32> [[TMP2]], -; CHECK-NEXT: ret <2 x i32> [[TMP3]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> , <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP2]] ; %1 = icmp sgt <2 x i32> %x, %2 = select <2 x i1> %1, <2 x i32> , <2 x i32> @@ -328,8 +315,9 @@ define i32 @test15b(i32 %X) { ; CHECK-LABEL: @test15b( ; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 32 -; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[T1]], 32 -; CHECK-NEXT: ret i32 [[TMP1]] +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 32, i32 0 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 32 %t2 = icmp eq i32 %t1, 0 @@ -377,10 +365,10 @@ ;; (a & 128) ? 0 : 256 define i32 @test15f(i32 %X) { ; CHECK-LABEL: @test15f( -; CHECK-NEXT: [[T1:%.*]] = shl i32 [[X:%.*]], 1 -; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[T1]], 256 -; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1]], 256 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 128 +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 256, i32 0 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 128 %t2 = icmp ne i32 %t1, 0 @@ -391,8 +379,10 @@ ;; (a & 8) ? -1 : -9 define i32 @test15g(i32 %X) { ; CHECK-LABEL: @test15g( -; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[X:%.*]], -9 -; CHECK-NEXT: ret i32 [[TMP1]] +; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 8 +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 -9, i32 -1 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 8 %t2 = icmp ne i32 %t1, 0 @@ -403,9 +393,10 @@ ;; (a & 8) ? -9 : -1 define i32 @test15h(i32 %X) { ; CHECK-LABEL: @test15h( -; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[X:%.*]], -9 -; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1]], 8 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 8 +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 -1, i32 -9 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 8 %t2 = icmp ne i32 %t1, 0 @@ -416,11 +407,10 @@ ;; (a & 2) ? 577 : 1089 define i32 @test15i(i32 %X) { ; CHECK-LABEL: @test15i( -; CHECK-NEXT: [[T1:%.*]] = shl i32 [[X:%.*]], 8 -; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[T1]], 512 -; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1]], 512 -; CHECK-NEXT: [[TMP3:%.*]] = add nuw nsw i32 [[TMP2]], 577 -; CHECK-NEXT: ret i32 [[TMP3]] +; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 2 +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 1089, i32 577 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 2 %t2 = icmp ne i32 %t1, 0 @@ -431,10 +421,10 @@ ;; (a & 2) ? 1089 : 577 define i32 @test15j(i32 %X) { ; CHECK-LABEL: @test15j( -; CHECK-NEXT: [[T1:%.*]] = shl i32 [[X:%.*]], 8 -; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[T1]], 512 -; CHECK-NEXT: [[TMP2:%.*]] = add nuw nsw i32 [[TMP1]], 577 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[T1:%.*]] = and i32 [[X:%.*]], 2 +; CHECK-NEXT: [[T2:%.*]] = icmp eq i32 [[T1]], 0 +; CHECK-NEXT: [[T3:%.*]] = select i1 [[T2]], i32 577, i32 1089 +; CHECK-NEXT: ret i32 [[T3]] ; %t1 = and i32 %X, 2 %t2 = icmp ne i32 %t1, 0 Index: test/Transforms/InstCombine/three-way-comparison.ll =================================================================== --- /dev/null +++ test/Transforms/InstCombine/three-way-comparison.ll @@ -0,0 +1,470 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; Test that various patterns of three-ways comparison are recognized. + +; RUN: opt < %s -instcombine -S | FileCheck %s + +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" + +declare void @foo(i32 %x) + +define i32 @compare_against_arbitrary_value(i32 %x, i32 %c) { +; TODO: We can prove that if %x s> %c then %x != c, so there should be no actual +; calculations in callfoo block. @foo can be invoked with 1. We only do it +; for constants that are not 0 currently while it could be generalized. +; CHECK-LABEL: @compare_against_arbitrary_value( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], [[C:%.*]] +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[X]], [[C]] +; CHECK-NEXT: [[SELECT2:%.*]] = zext i1 [[CMP1]] to i32 +; CHECK-NEXT: call void @foo(i32 [[SELECT2]]) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, %c + %cmp2 = icmp slt i32 %x, %c + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero(i32 %x) { +; CHECK-LABEL: @compare_against_zero( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 0 + %cmp2 = icmp slt i32 %x, 0 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_one(i32 %x) { +; CHECK-LABEL: @compare_against_one( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 1 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 1 + %cmp2 = icmp slt i32 %x, 1 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_two(i32 %x) { +; CHECK-LABEL: @compare_against_two( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 2 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 2 + %cmp2 = icmp slt i32 %x, 2 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_three(i32 %x) { +; CHECK-LABEL: @compare_against_three( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 3 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 3 + %cmp2 = icmp slt i32 %x, 3 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_four(i32 %x) { +; CHECK-LABEL: @compare_against_four( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 4 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 4 + %cmp2 = icmp slt i32 %x, 4 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_five(i32 %x) { +; CHECK-LABEL: @compare_against_five( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 5 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 5 + %cmp2 = icmp slt i32 %x, 5 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_six(i32 %x) { +; CHECK-LABEL: @compare_against_six( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 6 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 6 + %cmp2 = icmp slt i32 %x, 6 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +; Same as @compare_against_arbitrary_value, but now the three-way comparison +; returns not idiomatic comparator's result (-1, 0, 1) but some other constants. +define i32 @compare_against_arbitrary_value_non_idiomatic_1(i32 %x, i32 %c) { +; CHECK-LABEL: @compare_against_arbitrary_value_non_idiomatic_1( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], [[C:%.*]] +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 425) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, %c + %cmp2 = icmp slt i32 %x, %c + %select1 = select i1 %cmp2, i32 -6, i32 425 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero_non_idiomatic_add(i32 %x) { +; CHECK-LABEL: @compare_against_zero_non_idiomatic_add( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 425) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 0 + %cmp2 = icmp slt i32 %x, 0 + %select1 = select i1 %cmp2, i32 -6, i32 425 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +; Same as @compare_against_arbitrary_value, but now the three-way comparison +; returns not idiomatic comparator's result (-1, 0, 1) but some other constants. +define i32 @compare_against_arbitrary_value_non_idiomatic_2(i32 %x, i32 %c) { +; CHECK-LABEL: @compare_against_arbitrary_value_non_idiomatic_2( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], [[C:%.*]] +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 425) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, %c + %cmp2 = icmp slt i32 %x, %c + %select1 = select i1 %cmp2, i32 -5, i32 425 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero_non_idiomatic_or(i32 %x) { +; CHECK-LABEL: @compare_against_zero_non_idiomatic_or( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 425) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i32 %x, 0 + %cmp2 = icmp slt i32 %x, 0 + %select1 = select i1 %cmp2, i32 -5, i32 425 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_arbitrary_value_type_mismatch(i64 %x, i64 %c) { +; TODO: We can prove that if %x s> %c then %x != c, so there should be no actual +; calculations in callfoo block. @foo can be invoked with 1. We only do it +; for constants that are not 0 currently while it could be generalized. +; CHECK-LABEL: @compare_against_arbitrary_value_type_mismatch( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i64 [[X:%.*]], [[C:%.*]] +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i64 [[X]], [[C]] +; CHECK-NEXT: [[SELECT2:%.*]] = zext i1 [[CMP1]] to i32 +; CHECK-NEXT: call void @foo(i32 [[SELECT2]]) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i64 %x, %c + %cmp2 = icmp slt i64 %x, %c + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero_type_mismatch_idiomatic(i64 %x) { +; CHECK-LABEL: @compare_against_zero_type_mismatch_idiomatic( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i64 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i64 %x, 0 + %cmp2 = icmp slt i64 %x, 0 + %select1 = select i1 %cmp2, i32 -1, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero_type_mismatch_non_idiomatic_1(i64 %x) { +; CHECK-LABEL: @compare_against_zero_type_mismatch_non_idiomatic_1( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i64 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i64 %x, 0 + %cmp2 = icmp slt i64 %x, 0 + %select1 = select i1 %cmp2, i32 -7, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +} + +define i32 @compare_against_zero_type_mismatch_non_idiomatic_2(i64 %x) { +; CHECK-LABEL: @compare_against_zero_type_mismatch_non_idiomatic_2( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i64 [[X:%.*]], 0 +; CHECK-NEXT: br i1 [[TMP0]], label [[CALLFOO:%.*]], label [[EXIT:%.*]] +; CHECK: callfoo: +; CHECK-NEXT: call void @foo(i32 1) +; CHECK-NEXT: br label [[EXIT]] +; CHECK: exit: +; CHECK-NEXT: ret i32 42 +; + +entry: + %cmp1 = icmp eq i64 %x, 0 + %cmp2 = icmp slt i64 %x, 0 + %select1 = select i1 %cmp2, i32 -6, i32 1 + %select2 = select i1 %cmp1, i32 0, i32 %select1 + %cond = icmp sgt i32 %select2, 0 + br i1 %cond, label %callfoo, label %exit + +callfoo: + call void @foo(i32 %select2) + br label %exit + +exit: + ret i32 42 +}