[llvm-branch-commits] [llvm] 94f6d36 - [InstCombine] avoid crash on phi with unreachable incoming block (PR48369)

Sanjay Patel via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Dec 6 06:36:31 PST 2020


Author: Sanjay Patel
Date: 2020-12-06T09:31:47-05:00
New Revision: 94f6d365e4be0cf05930df0eedd2bfb23f6fce51

URL: https://github.com/llvm/llvm-project/commit/94f6d365e4be0cf05930df0eedd2bfb23f6fce51
DIFF: https://github.com/llvm/llvm-project/commit/94f6d365e4be0cf05930df0eedd2bfb23f6fce51.diff

LOG: [InstCombine] avoid crash on phi with unreachable incoming block (PR48369)

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 92504da01cbf..cab6f1e5632f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1083,9 +1083,11 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
   // operation in that block.  However, if this is a critical edge, we would be
   // inserting the computation on some other paths (e.g. inside a loop).  Only
   // do this if the pred block is unconditionally branching into the phi block.
+  // Also, make sure that the pred block is not dead code.
   if (NonConstBB != nullptr) {
     BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
-    if (!BI || !BI->isUnconditional()) return nullptr;
+    if (!BI || !BI->isUnconditional() || !DT.isReachableFromEntry(NonConstBB))
+      return nullptr;
   }
 
   // Okay, we can do the transformation: create the new PHI node.

diff  --git a/llvm/test/Transforms/InstCombine/phi-select-constant.ll b/llvm/test/Transforms/InstCombine/phi-select-constant.ll
index 9d1c973925bb..c65be75c0b4a 100644
--- a/llvm/test/Transforms/InstCombine/phi-select-constant.ll
+++ b/llvm/test/Transforms/InstCombine/phi-select-constant.ll
@@ -77,16 +77,16 @@ final:
 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-NEXT:  entry:
-; CHECK-NEXT:    [[PHITMP1:%.*]] = shufflevector <2 x i8> [[Z:%.*]], <2 x i8> [[Y:%.*]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT:    [[PHI_SEL1:%.*]] = shufflevector <2 x i8> [[Z:%.*]], <2 x i8> [[Y:%.*]], <2 x i32> <i32 0, i32 3>
 ; CHECK-NEXT:    br i1 [[COND1:%.*]], label [[IF1:%.*]], label [[ELSE:%.*]]
 ; CHECK:       if1:
-; CHECK-NEXT:    [[PHITMP2:%.*]] = shufflevector <2 x i8> [[Y]], <2 x i8> [[Z]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT:    [[PHI_SEL2:%.*]] = shufflevector <2 x i8> [[Y]], <2 x i8> [[Z]], <2 x i32> <i32 0, i32 3>
 ; CHECK-NEXT:    br i1 [[COND2:%.*]], label [[IF2:%.*]], label [[ELSE]]
 ; CHECK:       if2:
-; CHECK-NEXT:    [[PHITMP:%.*]] = select <2 x i1> [[X:%.*]], <2 x i8> [[Y]], <2 x i8> [[Z]]
+; CHECK-NEXT:    [[PHI_SEL:%.*]] = select <2 x i1> [[X:%.*]], <2 x i8> [[Y]], <2 x i8> [[Z]]
 ; CHECK-NEXT:    br label [[ELSE]]
 ; CHECK:       else:
-; CHECK-NEXT:    [[PHI:%.*]] = phi <2 x i8> [ [[PHITMP]], [[IF2]] ], [ [[PHITMP1]], [[ENTRY:%.*]] ], [ [[PHITMP2]], [[IF1]] ]
+; CHECK-NEXT:    [[PHI:%.*]] = phi <2 x i8> [ [[PHI_SEL]], [[IF2]] ], [ [[PHI_SEL1]], [[ENTRY:%.*]] ], [ [[PHI_SEL2]], [[IF1]] ]
 ; CHECK-NEXT:    ret <2 x i8> [[PHI]]
 ;
 entry:
@@ -103,3 +103,37 @@ else:
   %sel = select <2 x i1> %phi, <2 x i8> %y, <2 x i8> %z
   ret <2 x i8> %sel
 }
+
+; Don't crash on unreachable IR.
+
+define void @PR48369(i32 %a, i32* %p) {
+; CHECK-LABEL: @PR48369(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[PHI_CMP:%.*]] = icmp sgt i32 [[A:%.*]], 0
+; CHECK-NEXT:    br label [[BB1:%.*]]
+; CHECK:       bb1:
+; CHECK-NEXT:    [[CMP:%.*]] = phi i1 [ [[PHI_CMP]], [[DEADBB:%.*]] ], [ true, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[SHL:%.*]] = select i1 [[CMP]], i32 256, i32 0
+; CHECK-NEXT:    store i32 [[SHL]], i32* [[P:%.*]], align 4
+; CHECK-NEXT:    br label [[END:%.*]]
+; CHECK:       deadbb:
+; CHECK-NEXT:    br label [[BB1]]
+; CHECK:       end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %phi.cmp = icmp sgt i32 %a, 0
+  br label %bb1
+
+bb1:
+  %cmp = phi i1 [ %phi.cmp, %deadbb ], [ true, %entry ]
+  %shl = select i1 %cmp, i32 256, i32 0
+  store i32 %shl, i32* %p
+  br label %end
+
+deadbb:
+  br label %bb1
+
+end:
+  ret void
+}


        


More information about the llvm-branch-commits mailing list