[PATCH] D34162: [InstCombine] Set correct insertion point for selects generated while folding phis

Anna Thomas via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 13 13:04:35 PDT 2017


anna created this revision.

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).


https://reviews.llvm.org/D34162

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/phi-select-constant.ll


Index: test/Transforms/InstCombine/phi-select-constant.ll
===================================================================
--- test/Transforms/InstCombine/phi-select-constant.ll
+++ test/Transforms/InstCombine/phi-select-constant.ll
@@ -55,3 +55,47 @@
  %sel = select <4 x i1> %phinode, <4 x i64> zeroinitializer, <4 x i64> <i64 124, i64 125, i64 126, i64 127>
  ret <4 x i64> %sel
 }
+
+; Test PR33364
+; Insert the generated select into the same block as the incoming phi value.
+; The select for loadA (generated through foldOpIntoPhi) should be inserted
+; into bb itself. Later passes of instcombine correctly converts this select 
+; to an insert in the zero'th element of loadA.
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py and modified.
+ at globalA = global <2 x i8> <i8 42, i8 24>, align 2
+ at globalB = global <2 x i8> zeroinitializer, align 2
+define i8 @vec3(i1 %cond) {
+; CHECK-LABEL: vec3
+; CHECK-LABEL: bb:
+; CHECK-NEXT:    [[LOADA:%.*]] = load <2 x i8>, <2 x i8>* @globalA,
+; CHECK-NEXT:    [[PHITMP23:%.*]] = insertelement <2 x i8> [[LOADA]], i8 0, i32 0
+; CHECK-NEXT:    br i1 %cond, label %bb13_crit_edge, label %bb13
+bb:
+  %loadA = load <2 x i8>, <2 x i8>* @globalA, align 2
+  br i1 %cond, label %bb13_crit_edge, label %bb13
+
+; CHECK:       bb13_crit_edge:
+; CHECK-NEXT:    [[LOADB:%.*]] = load <2 x i8>, <2 x i8>* @globalB
+; CHECK-NEXT:    [[PHITMP:%.*]] = icmp slt <2 x i8> [[LOADB]], <i8 1, i8 1>
+; CHECK-NEXT:    [[PHITMP1:%.*]] = select <2 x i1> [[PHITMP]], <2 x i8> [[LOADA]], <2 x i8> zeroinitializer
+; CHECK-NEXT:    br label %bb13
+bb13_crit_edge:                               ; preds = %bb15
+  %loadB = load <2 x i8>, <2 x i8>* @globalB, align 2
+  br label %bb13
+
+
+; CHECK:       bb13:
+; CHECK-NEXT:    [[PHI:%.*]] = phi <2 x i8> [ [[PHITMP1]], [[BB13_CRIT_EDGE]] ], [ [[PHITMP23]], [[BB:%.*]] ]
+; CHECK-NEXT:    [[EX1:%.*]] = extractelement <2 x i8> [[PHI]], i32 0
+; CHECK-NEXT:    [[EX2:%.*]] = extractelement <2 x i8> [[PHI]], i32 1
+; CHECK-NEXT:    [[SUM:%.*]] = add nsw i8 [[EX1]], [[EX2]]
+; CHECK-NEXT:    ret i8 [[SUM]]
+bb13:                                             ; preds = %bb13_crit_edge, %bb
+  %phi = phi <2 x i8> [ %loadB, %bb13_crit_edge ], [ <i8 1, i8 0>, %bb ]
+  %cmp = icmp slt <2 x i8> %phi, <i8 1, i8 1>
+  %sel = select <2 x i1> %cmp, <2 x i8> %loadA, <2 x i8> zeroinitializer
+  %ex1 = extractelement <2 x i8> %sel, i32 0
+  %ex2 = extractelement <2 x i8> %sel, i32 1
+  %sum = add nsw i8  %ex1, %ex2
+  ret i8 %sum
+}
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -939,9 +939,14 @@
       // `TrueVInPred`.
       if (InC && !isa<ConstantExpr>(InC) && isa<ConstantInt>(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.
+        Builder->SetInsertPoint(ThisBB->getTerminator());
         InV = Builder->CreateSelect(PN->getIncomingValue(i),
                                     TrueVInPred, FalseVInPred, "phitmp");
+      }
       NewPN->addIncoming(InV, ThisBB);
     }
   } else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34162.102388.patch
Type: text/x-patch
Size: 3502 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170613/98382bf0/attachment.bin>


More information about the llvm-commits mailing list