Skip to content

ObjCARCContract: Use stripPointerCastsAndAliases #134275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 13 additions & 25 deletions llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,31 +660,19 @@ bool ObjCARCContract::run(Function &F, AAResults *A, DominatorTree *D) {
};

Value *Arg = cast<CallInst>(Inst)->getArgOperand(0);

// TODO: Change this to a do-while.
for (;;) {
ReplaceArgUses(Arg);

// If Arg is a no-op casted pointer, strip one level of casts and iterate.
if (const BitCastInst *BI = dyn_cast<BitCastInst>(Arg))
Arg = BI->getOperand(0);
else if (isa<GEPOperator>(Arg) &&
cast<GEPOperator>(Arg)->hasAllZeroIndices())
Arg = cast<GEPOperator>(Arg)->getPointerOperand();
else if (isa<GlobalAlias>(Arg) &&
!cast<GlobalAlias>(Arg)->isInterposable())
Arg = cast<GlobalAlias>(Arg)->getAliasee();
else {
// If Arg is a PHI node, get PHIs that are equivalent to it and replace
// their uses.
if (PHINode *PN = dyn_cast<PHINode>(Arg)) {
SmallVector<Value *, 1> PHIList;
getEquivalentPHIs(*PN, PHIList);
for (Value *PHI : PHIList)
ReplaceArgUses(PHI);
}
break;
}
ReplaceArgUses(Arg);

Value *Stripped = Arg->stripPointerCastsAndAliases();
if (Stripped != Arg)
ReplaceArgUses(Stripped);

// If Arg is a PHI node, get PHIs that are equivalent to it and replace
// their uses.
if (PHINode *PN = dyn_cast<PHINode>(Stripped)) {
SmallVector<Value *, 1> PHIList;
getEquivalentPHIs(*PN, PHIList);
for (Value *PHI : PHIList)
ReplaceArgUses(PHI);
}
}

Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Transforms/ObjCARC/contract.ll
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ define void @test14(ptr %a, ptr %b) {
ret void
}

define void @test15(ptr %x) {
; CHECK-LABEL: define void @test15(
; CHECK-SAME: ptr [[X:%.*]]) {
; CHECK-NEXT: [[Y:%.*]] = getelementptr inbounds ptr, ptr [[X]], i32 0
; CHECK-NEXT: [[V0:%.*]] = call ptr @llvm.objc.retain(ptr [[Y]]) #[[ATTR0:[0-9]+]]
; CHECK-NEXT: call void @use_pointer(ptr [[V0]])
; CHECK-NEXT: call void @use_pointer(ptr [[V0]])
; CHECK-NEXT: ret void
;
%y = getelementptr inbounds ptr, ptr %x, i32 0
%v0 = call ptr @llvm.objc.retain(ptr %y) nounwind
call void @use_pointer(ptr %x)
call void @use_pointer(ptr %y)
ret void
}

declare void @llvm.objc.clang.arc.use(...) nounwind
declare void @llvm.objc.clang.arc.noop.use(...) nounwind

Expand Down