Skip to content

Commit 3b2d0bc

Browse files
committedMar 4, 2019
[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? llvm-svn: 355345
1 parent caf62b1 commit 3b2d0bc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎llvm/lib/CodeGen/CodeGenPrepare.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,11 @@ static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp,
12031203
// Add = add A, 1; Cmp = icmp eq A,-1 (overflow if A is max val)
12041204
// Add = add A,-1; Cmp = icmp ne A, 0 (overflow if A is non-zero)
12051205
Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1);
1206+
1207+
// We are not expecting non-canonical/degenerate code. Just bail out.
1208+
if (isa<Constant>(A))
1209+
return false;
1210+
12061211
ICmpInst::Predicate Pred = Cmp->getPredicate();
12071212
if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes()))
12081213
B = ConstantInt::get(B->getType(), 1);

‎llvm/test/Transforms/CodeGenPrepare/X86/overflow-intrinsics.ll

+25
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,31 @@ end:
430430
ret i1 %ov
431431
}
432432

433+
; Verify that crazy/non-canonical code does not crash.
434+
435+
define void @bar() {
436+
; CHECK-LABEL: @bar(
437+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 1, -1
438+
; CHECK-NEXT: [[FROMBOOL:%.*]] = zext i1 [[CMP]] to i8
439+
; CHECK-NEXT: unreachable
440+
;
441+
%cmp = icmp eq i64 1, -1
442+
%frombool = zext i1 %cmp to i8
443+
unreachable
444+
}
445+
446+
define void @foo() {
447+
; CHECK-LABEL: @foo(
448+
; CHECK-NEXT: [[SUB:%.*]] = add nsw i64 1, 1
449+
; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SUB]] to i32
450+
; CHECK-NEXT: unreachable
451+
;
452+
%sub = add nsw i64 1, 1
453+
%conv = trunc i64 %sub to i32
454+
unreachable
455+
}
456+
457+
433458
; Check that every instruction inserted by -codegenprepare has a debug location.
434459
; DEBUG: CheckModuleDebugify: PASS
435460

0 commit comments

Comments
 (0)
Please sign in to comment.