From 512a2c924aa92c5c6663bb2d03e60811ea33a020 Mon Sep 17 00:00:00 2001 From: Anna Thomas Date: Fri, 16 Jun 2017 21:08:37 +0000 Subject: [PATCH] [InstCombine] Set correct insertion point for selects generated while folding phis Summary: When we fold vector constants that are operands of phi's that feed into select, we need to set the correct insertion point for the *new* selects that get generated. The correct insertion point is the incoming block for the phi. Such cases can occur with patch r298845, which fixed folding of vector constants, but the new selects could be inserted incorrectly (as the added test case shows). Reviewers: majnemer, spatel, sanjoy Reviewed by: spatel Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D34162 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305591 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstructionCombining.cpp | 12 +++++++- .../InstCombine/phi-select-constant.ll | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 65e6d2e3590..a2dd27d282a 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -939,9 +939,19 @@ Instruction *InstCombiner::foldOpIntoPhi(Instruction &I, PHINode *PN) { // `TrueVInPred`. if (InC && !isa(InC) && isa(InC)) InV = InC->isNullValue() ? FalseVInPred : TrueVInPred; - else + else { + // Generate the select in the same block as PN's current incoming block. + // Note: ThisBB need not be the NonConstBB because vector constants + // which are constants by definition are handled here. + // FIXME: This can lead to an increase in IR generation because we might + // generate selects for vector constant phi operand, that could not be + // folded to TrueVInPred or FalseVInPred as done for ConstantInt. For + // non-vector phis, this transformation was always profitable because + // the select would be generated exactly once in the NonConstBB. + Builder->SetInsertPoint(ThisBB->getTerminator()); InV = Builder->CreateSelect(PN->getIncomingValue(i), TrueVInPred, FalseVInPred, "phitmp"); + } NewPN->addIncoming(InV, ThisBB); } } else if (CmpInst *CI = dyn_cast(&I)) { diff --git a/test/Transforms/InstCombine/phi-select-constant.ll b/test/Transforms/InstCombine/phi-select-constant.ll index 272594d7f4f..83c4efb2a78 100644 --- a/test/Transforms/InstCombine/phi-select-constant.ll +++ b/test/Transforms/InstCombine/phi-select-constant.ll @@ -55,3 +55,32 @@ final: %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> ret <4 x i64> %sel } + +; Test PR33364 +; Insert the generated select into the same block as the incoming phi value. +; phi has constant vectors along with a single non-constant vector as operands. +define <2 x i8> @vec3(i1 %cond1, i1 %cond2, <2 x i1> %x, <2 x i8> %y, <2 x i8> %z) { +; CHECK-LABEL: @vec3 +; CHECK-LABEL: entry: +; CHECK-NEXT: [[PHITMP1:%.*]] = shufflevector <2 x i8> %y, <2 x i8> %z, <2 x i32> +entry: + br i1 %cond1, label %if1, label %else + +; CHECK-LABEL: if1: +; CHECK-NEXT: [[PHITMP2:%.*]] = shufflevector <2 x i8> %y, <2 x i8> %z, <2 x i32> +if1: + br i1 %cond2, label %if2, label %else + +; CHECK-LABEL: if2: +; CHECK-NEXT: [[PHITMP3:%.*]] = select <2 x i1> %x, <2 x i8> %y, <2 x i8> %z +if2: + br label %else + +; CHECK-LABEL: else: +; CHECK-NEXT: [[PHITMP4:%.*]] = phi <2 x i8> [ [[PHITMP3]], %if2 ], [ [[PHITMP1]], %entry ], [ [[PHITMP2]], %if1 ] +; CHECK-NEXT: ret <2 x i8> [[PHITMP4]] +else: + %phi = phi <2 x i1> [ %x, %if2 ], [ , %entry ], [ , %if1 ] + %sel = select <2 x i1> %phi, <2 x i8> %y, <2 x i8> %z + ret <2 x i8> %sel +} -- 2.40.0