[JumpThreading][VectorUtils] avoid infinite loop on unreachable IR
authorSanjay Patel <spatel@rotateright.com>
Wed, 2 Dec 2020 18:35:05 +0000 (13:35 -0500)
committerSanjay Patel <spatel@rotateright.com>
Wed, 2 Dec 2020 18:39:33 +0000 (13:39 -0500)
https://llvm.org/PR48362

It's possible that we could stub this out sooner somewhere
within JumpThreading, but I'm not sure how to do that, and
then we would still have potential danger in other callers.

I can't find a way to trigger this using 'instsimplify',
however, because that already has a bailout on unreachable
blocks.

llvm/lib/Analysis/VectorUtils.cpp
llvm/test/Transforms/JumpThreading/unreachable-loops.ll

index bd69055..9072697 100644 (file)
@@ -290,6 +290,10 @@ Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
     if (EltNo == IIElt)
       return III->getOperand(1);
 
+    // Guard against infinite loop on malformed, unreachable IR.
+    if (III == III->getOperand(0))
+      return nullptr;
+
     // Otherwise, the insertelement doesn't modify the value, recurse on its
     // vector input.
     return findScalarElement(III->getOperand(0), EltNo);
index 3f75aea..a0f1c21 100644 (file)
@@ -1,11 +1,12 @@
 ; RUN: opt -jump-threading -S < %s | FileCheck %s
 ; RUN: opt -passes=jump-threading -S < %s | FileCheck %s
+
 ; Check the unreachable loop won't cause infinite loop
 ; in jump-threading when it tries to update the predecessors'
 ; profile metadata from a phi node.
 
 define void @unreachable_single_bb_loop() {
-; CHECK-LABEL: @unreachable_single_bb_loop()
+; CHECK-LABEL: @unreachable_single_bb_loop(
 bb:
   %tmp = call i32 @a()
   %tmp1 = icmp eq i32 %tmp, 1
@@ -15,8 +16,8 @@ bb:
 bb2:                                              ; preds = %bb2
   %tmp4 = icmp ne i32 %tmp, 1
   switch i1 %tmp4, label %bb2 [
-    i1 0, label %bb5
-    i1 1, label %bb8
+  i1 0, label %bb5
+  i1 1, label %bb8
   ]
 
 bb5:                                              ; preds = %bb2, %bb
@@ -31,7 +32,7 @@ bb8:                                              ; preds = %bb8, %bb7, %bb5, %b
 }
 
 define void @unreachable_multi_bbs_loop() {
-; CHECK-LABEL: @unreachable_multi_bbs_loop()
+; CHECK-LABEL: @unreachable_multi_bbs_loop(
 bb:
   %tmp = call i32 @a()
   %tmp1 = icmp eq i32 %tmp, 1
@@ -44,8 +45,8 @@ bb3:                                              ; preds = %bb2
 bb2:                                              ; preds = %bb3
   %tmp4 = icmp ne i32 %tmp, 1
   switch i1 %tmp4, label %bb3 [
-    i1 0, label %bb5
-    i1 1, label %bb8
+  i1 0, label %bb5
+  i1 1, label %bb8
   ]
 
 bb5:                                              ; preds = %bb2, %bb
@@ -60,4 +61,85 @@ bb8:                                              ; preds = %bb8, %bb7, %bb5, %b
 }
 declare i32 @a()
 
+; This gets into a state that could cause instruction simplify
+; to hang - an insertelement instruction has itself as an operand.
+
+define void @PR48362() {
+; CHECK-LABEL: @PR48362(
+cleanup1491:                                      ; preds = %for.body1140
+  switch i32 0, label %cleanup2343.loopexit4 [
+  i32 0, label %cleanup.cont1500
+  i32 128, label %lbl_555.loopexit
+  ]
+
+cleanup.cont1500:                                 ; preds = %cleanup1491
+  unreachable
+
+lbl_555.loopexit:                                 ; preds = %cleanup1491
+  br label %for.body1509
+
+for.body1509:                                     ; preds = %for.inc2340, %lbl_555.loopexit
+  %l_580.sroa.0.0 = phi <4 x i32> [ <i32 1684658741, i32 1684658741, i32 1684658741, i32 1684658741>, %lbl_555.loopexit ], [ %l_580.sroa.0.2, %for.inc2340 ]
+  %p_55.addr.10 = phi i16 [ 0, %lbl_555.loopexit ], [ %p_55.addr.11, %for.inc2340 ]
+  %i82 = load i32, i32* undef, align 1
+  %tobool1731.not = icmp eq i32 %i82, 0
+  br i1 %tobool1731.not, label %if.end1733, label %if.then1732
+
+if.then1732:                                      ; preds = %for.body1509
+  br label %cleanup2329
+
+if.end1733:                                       ; preds = %for.body1509
+  %tobool1735.not = icmp eq i16 %p_55.addr.10, 0
+  br i1 %tobool1735.not, label %if.then1736, label %if.else1904
+
+if.then1736:                                      ; preds = %if.end1733
+  br label %cleanup2329
+
+if.else1904:                                      ; preds = %if.end1733
+  br label %for.body1911
+
+for.body1911:                                     ; preds = %if.else1904
+  %l_580.sroa.0.4.vec.extract683 = extractelement <4 x i32> %l_580.sroa.0.0, i32 2
+  %xor2107 = xor i32 undef, %l_580.sroa.0.4.vec.extract683
+  br label %land.end2173
+
+land.end2173:                                     ; preds = %for.body1911
+  br i1 undef, label %if.end2178, label %cleanup2297
+
+if.end2178:                                       ; preds = %land.end2173
+  %l_580.sroa.0.2.vec.insert = insertelement <4 x i32> %l_580.sroa.0.0, i32 undef, i32 1
+  br label %cleanup2297
+
+cleanup2297:                                      ; preds = %if.end2178, %land.end2173
+  %l_580.sroa.0.1 = phi <4 x i32> [ %l_580.sroa.0.2.vec.insert, %if.end2178 ], [ %l_580.sroa.0.0, %land.end2173 ]
+  br label %cleanup2329
+
+cleanup2329:                                      ; preds = %cleanup2297, %if.then1736, %if.then1732
+  %l_580.sroa.0.2 = phi <4 x i32> [ %l_580.sroa.0.0, %if.then1736 ], [ %l_580.sroa.0.1, %cleanup2297 ], [ %l_580.sroa.0.0, %if.then1732 ]
+  %cleanup.dest.slot.11 = phi i32 [ 0, %if.then1736 ], [ undef, %cleanup2297 ], [ 129, %if.then1732 ]
+  %p_55.addr.11 = phi i16 [ %p_55.addr.10, %if.then1736 ], [ undef, %cleanup2297 ], [ %p_55.addr.10, %if.then1732 ]
+  switch i32 %cleanup.dest.slot.11, label %cleanup2343.loopexit [
+  i32 0, label %cleanup.cont2339
+  i32 129, label %crit_edge114
+  ]
+
+cleanup.cont2339:                                 ; preds = %cleanup2329
+  br label %for.inc2340
+
+for.inc2340:                                      ; preds = %cleanup.cont2339
+  br i1 undef, label %for.body1509, label %crit_edge115
+
+crit_edge114:                                     ; preds = %cleanup2329
+  unreachable
+
+crit_edge115:                                     ; preds = %for.inc2340
+  unreachable
+
+cleanup2343.loopexit:                             ; preds = %cleanup2329
+  unreachable
+
+cleanup2343.loopexit4:                            ; preds = %cleanup1491
+  unreachable
+}
+
 !0 = !{!"branch_weights", i32 2146410443, i32 1073205}