Skip to content

Commit

Permalink
[ObjC][ARC] Fix non-deterministic behavior in ProvenanceAnalysis
Browse files Browse the repository at this point in the history
If the second value passed to relatedSelect is a select, check whether
neither arm of the select is related to the first value.
  • Loading branch information
ahatanaka committed Jan 5, 2023
1 parent 87b2c76 commit 665e477
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion llvm/lib/Transforms/ObjCARC/ProvenanceAnalysis.cpp
Expand Up @@ -42,11 +42,17 @@ bool ProvenanceAnalysis::relatedSelect(const SelectInst *A,
const Value *B) {
// If the values are Selects with the same condition, we can do a more precise
// check: just check for relations between the values on corresponding arms.
if (const SelectInst *SB = dyn_cast<SelectInst>(B))
if (const SelectInst *SB = dyn_cast<SelectInst>(B)) {
if (A->getCondition() == SB->getCondition())
return related(A->getTrueValue(), SB->getTrueValue()) ||
related(A->getFalseValue(), SB->getFalseValue());

// Check both arms of B individually. Return false if neither arm is related
// to A.
if (!(related(SB->getTrueValue(), A) || related(SB->getFalseValue(), A)))
return false;
}

// Check both arms of the Select node individually.
return related(A->getTrueValue(), B) || related(A->getFalseValue(), B);
}
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/ObjCARC/related-check.ll
Expand Up @@ -132,6 +132,19 @@ define ptr @foo() {
ret ptr %call78
}

; CHECK-LABEL: define void @test_select(
; CHECK: call ptr @llvm.objc.retain(
; CHECK: call void @llvm.objc.release(

define void @test_select(i1 %c0, i1 %c1, ptr %p0, ptr %p1) {
%cond = select i1 %c0, ptr %p0, ptr %p1
%cond5 = select i1 %c0, ptr %p1, ptr %p0
%cond14 = select i1 %c1, ptr %cond5, ptr null
call ptr @llvm.objc.retain(ptr %cond14)
call void @llvm.objc.release(ptr %cond)
ret void
}

declare ptr @bar(ptr)

declare ptr @llvm.objc.retain(ptr)
Expand Down

0 comments on commit 665e477

Please sign in to comment.