[IRCE] Bail in case of pointer types. PR40539
authorMax Kazantsev <mkazantsev@azul.com>
Mon, 12 Sep 2022 08:50:28 +0000 (15:50 +0700)
committerMax Kazantsev <mkazantsev@azul.com>
Mon, 12 Sep 2022 09:01:25 +0000 (16:01 +0700)
We should not unconditionally expect that SCEVable types are all integers
because SCEV can also be computed for pointers. Bail in this case.

llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
llvm/test/Transforms/IRCE/pr40539.ll [new file with mode: 0644]

index 9d18f32..e429aaf 100644 (file)
@@ -1583,8 +1583,11 @@ InductiveRangeCheck::computeSafeIterationSpace(
     bool IsLatchSigned) const {
   // We can deal when types of latch check and range checks don't match in case
   // if latch check is more narrow.
-  auto *IVType = cast<IntegerType>(IndVar->getType());
-  auto *RCType = cast<IntegerType>(getBegin()->getType());
+  auto *IVType = dyn_cast<IntegerType>(IndVar->getType());
+  auto *RCType = dyn_cast<IntegerType>(getBegin()->getType());
+  // Do not work with pointer types.
+  if (!IVType || !RCType)
+    return None;
   if (IVType->getBitWidth() > RCType->getBitWidth())
     return None;
   // IndVar is of the form "A + B * I" (where "I" is the canonical induction
diff --git a/llvm/test/Transforms/IRCE/pr40539.ll b/llvm/test/Transforms/IRCE/pr40539.ll
new file mode 100644 (file)
index 0000000..a8d6a37
--- /dev/null
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -irce -S < %s | FileCheck %s
+; RUN: opt -passes='require<branch-prob>,irce' -S < %s | FileCheck %s
+
+@array = external global [1528 x i16], align 1
+
+; Make sure we do not crash here.
+define void @test_01() {
+; CHECK-LABEL: @test_01(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[CONT48:%.*]]
+; CHECK:       cont48:
+; CHECK-NEXT:    [[A2_0121:%.*]] = phi i16* [ [[ADD_PTR74:%.*]], [[CONT76:%.*]] ], [ getelementptr inbounds ([1528 x i16], [1528 x i16]* @array, i16 0, i16 0), [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[I41_0119:%.*]] = phi i16 [ [[ADD73:%.*]], [[CONT76]] ], [ 0, [[ENTRY]] ]
+; CHECK-NEXT:    [[ADD73]] = add nuw nsw i16 [[I41_0119]], 2
+; CHECK-NEXT:    [[ADD_PTR74]] = getelementptr inbounds i16, i16* [[A2_0121]], i16 2
+; CHECK-NEXT:    [[TMP0:%.*]] = icmp ult i16* [[A2_0121]], inttoptr (i16 -2 to i16*)
+; CHECK-NEXT:    br i1 [[TMP0]], label [[CONT76]], label [[HANDLER_POINTER_OVERFLOW75:%.*]]
+; CHECK:       handler.pointer_overflow75:
+; CHECK-NEXT:    unreachable
+; CHECK:       cont76:
+; CHECK-NEXT:    [[CMP43:%.*]] = icmp ult i16 [[ADD73]], 16
+; CHECK-NEXT:    br i1 [[CMP43]], label [[CONT48]], label [[FOR_END77:%.*]]
+; CHECK:       for.end77:
+; CHECK-NEXT:    unreachable
+;
+entry:
+  br label %cont48
+
+cont48:                                           ; preds = %cont76, %entry
+  %a2.0121 = phi i16* [ %add.ptr74, %cont76 ], [ getelementptr inbounds ([1528 x i16], [1528 x i16]* @array, i16 0, i16 0), %entry ]
+  %i41.0119 = phi i16 [ %add73, %cont76 ], [ 0, %entry ]
+  %add73 = add nuw nsw i16 %i41.0119, 2
+  %add.ptr74 = getelementptr inbounds i16, i16* %a2.0121, i16 2
+  %0 = icmp ult i16* %a2.0121, inttoptr (i16 -2 to i16*)
+  br i1 %0, label %cont76, label %handler.pointer_overflow75
+
+handler.pointer_overflow75:                       ; preds = %cont48
+  unreachable
+
+cont76:                                           ; preds = %cont48
+  %cmp43 = icmp ult i16 %add73, 16
+  br i1 %cmp43, label %cont48, label %for.end77
+
+for.end77:                                        ; preds = %cont76
+  unreachable
+}