[llvm] r370911 - [InstSimplify] guard against unreachable code (PR43218)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 08:12:56 PDT 2019


Author: spatel
Date: Wed Sep  4 08:12:55 2019
New Revision: 370911

URL: http://llvm.org/viewvc/llvm-project?rev=370911&view=rev
Log:
[InstSimplify] guard against unreachable code (PR43218)

This would crash:
https://bugs.llvm.org/show_bug.cgi?id=43218

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstSimplifyPass.cpp
    llvm/trunk/test/Transforms/InstSimplify/insertelement.ll

Modified: llvm/trunk/lib/Transforms/Scalar/InstSimplifyPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstSimplifyPass.cpp?rev=370911&r1=370910&r2=370911&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstSimplifyPass.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstSimplifyPass.cpp Wed Sep  4 08:12:55 2019
@@ -34,6 +34,11 @@ static bool runImpl(Function &F, const S
 
   do {
     for (BasicBlock &BB : F) {
+      // Unreachable code can take on strange forms that we are not prepared to
+      // handle. For example, an instruction may have itself as an operand.
+      if (!SQ.DT->isReachableFromEntry(&BB))
+        continue;
+
       SmallVector<Instruction *, 8> DeadInstsInBB;
       for (Instruction &I : BB) {
         // The first time through the loop, ToSimplify is empty and we try to
@@ -87,7 +92,7 @@ struct InstSimplifyLegacyPass : public F
     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
   }
 
-  /// runOnFunction - Remove instructions that simplify.
+  /// Remove instructions that simplify.
   bool runOnFunction(Function &F) override {
     if (skipFunction(F))
       return false;

Modified: llvm/trunk/test/Transforms/InstSimplify/insertelement.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/insertelement.ll?rev=370911&r1=370910&r2=370911&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/insertelement.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/insertelement.ll Wed Sep  4 08:12:55 2019
@@ -67,3 +67,26 @@ define <8 x i8> @extract_insert_same_vec
   %vec = insertelement <8 x i8> %in, i8 %val, i32 %index
   ret <8 x i8> %vec
 }
+
+; The insert is in an unreachable block, so it is allowed to point to itself.
+; This would crash via stack overflow.
+
+define void @PR43218() {
+; CHECK-LABEL: @PR43218(
+; CHECK-NEXT:  end:
+; CHECK-NEXT:    ret void
+; CHECK:       unreachable_infloop:
+; CHECK-NEXT:    [[EXTRACT:%.*]] = extractelement <2 x i64> [[BOGUS:%.*]], i32 0
+; CHECK-NEXT:    [[T0:%.*]] = inttoptr i64 [[EXTRACT]] to i16****
+; CHECK-NEXT:    [[BOGUS]] = insertelement <2 x i64> [[BOGUS]], i64 undef, i32 1
+; CHECK-NEXT:    br label [[UNREACHABLE_INFLOOP:%.*]]
+;
+end:
+  ret void
+
+unreachable_infloop:
+  %extract = extractelement <2 x i64> %bogus, i32 0
+  %t0 = inttoptr i64 %extract to i16****
+  %bogus = insertelement <2 x i64> %bogus, i64 undef, i32 1
+  br label %unreachable_infloop
+}




More information about the llvm-commits mailing list