[llvm] r355345 - [CodeGenPrepare] avoid crashing on non-canonical/degenerate code
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 4 14:47:13 PST 2019
Author: spatel
Date: Mon Mar 4 14:47:13 2019
New Revision: 355345
URL: http://llvm.org/viewvc/llvm-project?rev=355345&view=rev
Log:
[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?
Modified:
llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
llvm/trunk/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll
Modified: llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp?rev=355345&r1=355344&r2=355345&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp (original)
+++ llvm/trunk/lib/CodeGen/CodeGenPrepare.cpp Mon Mar 4 14:47:13 2019
@@ -1203,6 +1203,11 @@ static bool matchUAddWithOverflowConstan
// 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<Constant>(A))
+ return false;
+
ICmpInst::Predicate Pred = Cmp->getPredicate();
if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes()))
B = ConstantInt::get(B->getType(), 1);
Modified: llvm/trunk/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll?rev=355345&r1=355344&r2=355345&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll (original)
+++ llvm/trunk/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll Mon Mar 4 14:47:13 2019
@@ -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
More information about the llvm-commits
mailing list