From 4ed87e3dc1fcc1fe6146e58d539bd6fd86b3ed16 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 10 Apr 2017 16:40:00 +0000 Subject: [PATCH] [InstCombine] Support folding of add instructions with vector constants into select operations We currently only fold scalar add of constants into selects. This improves this to support vectors too. Differential Revision: https://reviews.llvm.org/D31683 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299847 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineAddSub.cpp | 9 ++------- test/Transforms/InstCombine/add.ll | 9 +++------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/lib/Transforms/InstCombine/InstCombineAddSub.cpp index c946c096821..65f72d0fb55 100644 --- a/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1128,8 +1128,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { } } - if (isa(RHS) && isa(LHS)) - if (Instruction *NV = FoldOpIntoPhi(I)) + if (isa(RHS)) + if (Instruction *NV = foldOpWithConstantIntoOperand(I)) return NV; if (I.getType()->getScalarType()->isIntegerTy(1)) @@ -1200,11 +1200,6 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { return BinaryOperator::CreateAnd(NewAdd, C2); } } - - // Try to fold constant add into select arguments. - if (SelectInst *SI = dyn_cast(LHS)) - if (Instruction *R = FoldOpIntoSelect(I, SI)) - return R; } // add (select X 0 (sub n A)) A --> select X A n diff --git a/test/Transforms/InstCombine/add.ll b/test/Transforms/InstCombine/add.ll index 84a48ece63f..648305d134c 100644 --- a/test/Transforms/InstCombine/add.ll +++ b/test/Transforms/InstCombine/add.ll @@ -272,8 +272,7 @@ define i32 @test19(i1 %C) { define <2 x i32> @test19vec(i1 %C) { ; CHECK-LABEL: @test19vec( -; CHECK-NEXT: [[A:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> -; CHECK-NEXT: [[V:%.*]] = add nuw nsw <2 x i32> [[A]], +; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[V]] ; %A = select i1 %C, <2 x i32> , <2 x i32> @@ -604,8 +603,7 @@ define i32 @test42(i1 %C) { define <2 x i32> @test42vec(i1 %C) { ; CHECK-LABEL: @test42vec( -; CHECK-NEXT: [[A:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> -; CHECK-NEXT: [[V:%.*]] = add nuw nsw <2 x i32> [[A]], +; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[V]] ; %A = select i1 %C, <2 x i32> , <2 x i32> @@ -615,8 +613,7 @@ define <2 x i32> @test42vec(i1 %C) { define <2 x i32> @test42vec2(i1 %C) { ; CHECK-LABEL: @test42vec2( -; CHECK-NEXT: [[A:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> -; CHECK-NEXT: [[V:%.*]] = add nuw nsw <2 x i32> [[A]], +; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> , <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[V]] ; %A = select i1 %C, <2 x i32> , <2 x i32> -- 2.40.0