From a7bbf63f800a8976d3636d5d9f55fbee24f0baf7 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 13 Oct 2017 17:47:25 +0000 Subject: [PATCH] [InstCombine] add hasOneUse check to add-zext-add fold to prevent increasing instructions git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315718 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineAddSub.cpp | 6 ++---- test/Transforms/InstCombine/add.ll | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/lib/Transforms/InstCombine/InstCombineAddSub.cpp index 92452d7df73..3921ab4374e 100644 --- a/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -988,10 +988,8 @@ Instruction *InstCombiner::foldAddWithConstant(BinaryOperator &Add) { return CastInst::Create(Instruction::SExt, X, Ty); // (add (zext (add nuw X, C2)), C) --> (zext (add nuw X, C2 + C)) - // FIXME: This should check hasOneUse to not increase the instruction count? - if (C->isNegative() && - match(Op0, m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2)))) && - C->sge(-C2->sext(C->getBitWidth()))) { + if (match(Op0, m_OneUse(m_ZExt(m_NUWAdd(m_Value(X), m_APInt(C2))))) && + C->isNegative() && C->sge(-C2->sext(C->getBitWidth()))) { Constant *NewC = ConstantInt::get(X->getType(), *C2 + C->trunc(C2->getBitWidth())); return new ZExtInst(Builder.CreateNUWAdd(X, NewC), Ty); diff --git a/test/Transforms/InstCombine/add.ll b/test/Transforms/InstCombine/add.ll index 728262a208a..e1fcbe0b1af 100644 --- a/test/Transforms/InstCombine/add.ll +++ b/test/Transforms/InstCombine/add.ll @@ -633,9 +633,8 @@ define <2 x i64> @test41vec_and_multiuse(<2 x i32> %a) { ; CHECK-LABEL: @test41vec_and_multiuse( ; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i32> %a, ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i32> [[ADD]] to <2 x i64> -; CHECK-NEXT: [[TMP1:%.*]] = add nuw <2 x i32> %a, -; CHECK-NEXT: [[SUB:%.*]] = zext <2 x i32> [[TMP1]] to <2 x i64> -; CHECK-NEXT: [[EXTRAUSE:%.*]] = add nuw nsw <2 x i64> [[ZEXT]], [[SUB]] +; CHECK-NEXT: [[SUB:%.*]] = add nsw <2 x i64> [[ZEXT]], +; CHECK-NEXT: [[EXTRAUSE:%.*]] = add nsw <2 x i64> [[SUB]], [[ZEXT]] ; CHECK-NEXT: ret <2 x i64> [[EXTRAUSE]] ; %add = add nuw <2 x i32> %a, -- 2.50.1