From: Sanjay Patel Date: Mon, 4 Mar 2019 22:47:13 +0000 (+0000) Subject: [CodeGenPrepare] avoid crashing on non-canonical/degenerate code X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b4cc6b6cce7b53b2f33e674f93b5586ddd946d21;p=llvm [CodeGenPrepare] avoid crashing on non-canonical/degenerate code The test is reduced from an example in the post-commit thread for: rL354746 http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190304/632396.html While we must avoid dying here, the real question should be: Why is non-canonical and/or degenerate code making it to CGP when using the new pass manager? git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@355345 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index e56bd036624..3996bdff8fc 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -1203,6 +1203,11 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp, // Add = add A, 1; Cmp = icmp eq A,-1 (overflow if A is max val) // Add = add A,-1; Cmp = icmp ne A, 0 (overflow if A is non-zero) Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1); + + // We are not expecting non-canonical/degenerate code. Just bail out. + if (isa(A)) + return false; + ICmpInst::Predicate Pred = Cmp->getPredicate(); if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes())) B = ConstantInt::get(B->getType(), 1); diff --git a/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll b/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll index 0e7517132cf..9a0bf28cb9f 100644 --- a/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll +++ b/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll @@ -430,6 +430,31 @@ end: ret i1 %ov } +; Verify that crazy/non-canonical code does not crash. + +define void @bar() { +; CHECK-LABEL: @bar( +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 1, -1 +; CHECK-NEXT: [[FROMBOOL:%.*]] = zext i1 [[CMP]] to i8 +; CHECK-NEXT: unreachable +; + %cmp = icmp eq i64 1, -1 + %frombool = zext i1 %cmp to i8 + unreachable +} + +define void @foo() { +; CHECK-LABEL: @foo( +; CHECK-NEXT: [[SUB:%.*]] = add nsw i64 1, 1 +; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SUB]] to i32 +; CHECK-NEXT: unreachable +; + %sub = add nsw i64 1, 1 + %conv = trunc i64 %sub to i32 + unreachable +} + + ; Check that every instruction inserted by -codegenprepare has a debug location. ; DEBUG: CheckModuleDebugify: PASS