diff options
-rw-r--r-- | llvm/lib/CodeGen/CodeGenPrepare.cpp | 5 | ||||
-rw-r--r-- | llvm/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll | 25 |
2 files changed, 30 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index e56bd036624..3996bdff8fc 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/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<Constant>(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/llvm/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll b/llvm/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll index 0e7517132cf..9a0bf28cb9f 100644 --- a/llvm/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll +++ b/llvm/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 |