Skip to content

Commit 67896f4

Browse files
committed
Returning poison from a function w/ noundef return attribute is UB
This does for readability of returns within said function as what we do for the caller side when reasoning about what might be poison. Differential Revision: https://reviews.llvm.org/D111180
1 parent f53d051 commit 67896f4

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

llvm/lib/Analysis/ValueTracking.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -5418,7 +5418,10 @@ void llvm::getGuaranteedWellDefinedOps(
54185418
}
54195419
break;
54205420
}
5421-
5421+
case Instruction::Ret:
5422+
if (I->getFunction()->hasRetAttribute(Attribute::NoUndef))
5423+
Operands.insert(I->getOperand(0));
5424+
break;
54225425
default:
54235426
break;
54245427
}

llvm/test/Analysis/ScalarEvolution/flags-from-poison.ll

+5-17
Original file line numberDiff line numberDiff line change
@@ -1621,49 +1621,37 @@ cont6: ; preds = %cont1, %if.then
16211621
br label %for.cond
16221622
}
16231623

1624-
; TODO: once D111180 lands, remove the udiv from these *-basic tests.
1625-
; noundef really should be enough
1626-
16271624
define noundef i32 @add-basic(i32 %a, i32 %b) {
16281625
; CHECK-LABEL: 'add-basic'
16291626
; CHECK-NEXT: Classifying expressions for: @add-basic
16301627
; CHECK-NEXT: %res = add nuw nsw i32 %a, %b
16311628
; CHECK-NEXT: --> (%a + %b)<nuw><nsw> U: full-set S: full-set
1632-
; CHECK-NEXT: %res2 = udiv i32 255, %res
1633-
; CHECK-NEXT: --> (255 /u (%a + %b)<nuw><nsw>) U: [0,256) S: [0,256)
16341629
; CHECK-NEXT: Determining loop execution counts for: @add-basic
16351630
;
16361631
%res = add nuw nsw i32 %a, %b
1637-
%res2 = udiv i32 255, %res
1638-
ret i32 %res2
1632+
ret i32 %res
16391633
}
16401634

16411635
define noundef i32 @sub-basic(i32 %a, i32 %b) {
16421636
; CHECK-LABEL: 'sub-basic'
16431637
; CHECK-NEXT: Classifying expressions for: @sub-basic
16441638
; CHECK-NEXT: %res = sub nuw nsw i32 %a, %b
16451639
; CHECK-NEXT: --> ((-1 * %b) + %a) U: full-set S: full-set
1646-
; CHECK-NEXT: %res2 = udiv i32 255, %res
1647-
; CHECK-NEXT: --> (255 /u ((-1 * %b) + %a)) U: [0,256) S: [0,256)
16481640
; CHECK-NEXT: Determining loop execution counts for: @sub-basic
16491641
;
16501642
%res = sub nuw nsw i32 %a, %b
1651-
%res2 = udiv i32 255, %res
1652-
ret i32 %res2
1643+
ret i32 %res
16531644
}
16541645

16551646
define noundef i32 @mul-basic(i32 %a, i32 %b) {
16561647
; CHECK-LABEL: 'mul-basic'
16571648
; CHECK-NEXT: Classifying expressions for: @mul-basic
16581649
; CHECK-NEXT: %res = mul nuw nsw i32 %a, %b
16591650
; CHECK-NEXT: --> (%a * %b)<nuw><nsw> U: full-set S: full-set
1660-
; CHECK-NEXT: %res2 = udiv i32 255, %res
1661-
; CHECK-NEXT: --> (255 /u (%a * %b)<nuw><nsw>) U: [0,256) S: [0,256)
16621651
; CHECK-NEXT: Determining loop execution counts for: @mul-basic
16631652
;
16641653
%res = mul nuw nsw i32 %a, %b
1665-
%res2 = udiv i32 255, %res
1666-
ret i32 %res2
1654+
ret i32 %res
16671655
}
16681656

16691657
@gA = external global i32
@@ -1746,9 +1734,9 @@ define noundef i32 @mul-recurse() {
17461734
; CHECK-NEXT: %d = load i32, i32* @gD, align 4
17471735
; CHECK-NEXT: --> %d U: full-set S: full-set
17481736
; CHECK-NEXT: %x = mul nuw i32 %a, %b
1749-
; CHECK-NEXT: --> (%a * %b) U: full-set S: full-set
1737+
; CHECK-NEXT: --> (%a * %b)<nuw> U: full-set S: full-set
17501738
; CHECK-NEXT: %y = mul nuw i32 %c, %d
1751-
; CHECK-NEXT: --> (%c * %d) U: full-set S: full-set
1739+
; CHECK-NEXT: --> (%c * %d)<nuw> U: full-set S: full-set
17521740
; CHECK-NEXT: %res = mul nuw i32 %x, %y
17531741
; CHECK-NEXT: --> (%a * %b * %c * %d) U: full-set S: full-set
17541742
; CHECK-NEXT: Determining loop execution counts for: @mul-recurse

0 commit comments

Comments
 (0)