Skip to content

Commit

Permalink
[Attributor] Teach checkForAllUses to follow returns into callers
Browse files Browse the repository at this point in the history
If we can determine all call sites we can follow a use in a return
instruction into the caller. AAPointerInfo utilizes this feature.
  • Loading branch information
jdoerfert committed Jul 19, 2022
1 parent 4f2ccdd commit f84712f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
49 changes: 36 additions & 13 deletions llvm/lib/Transforms/IPO/Attributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,8 +1333,21 @@ bool Attributor::checkForAllUses(
SmallVector<const Use *, 16> Worklist;
SmallPtrSet<const Use *, 16> Visited;

for (const Use &U : V.uses())
Worklist.push_back(&U);
auto AddUsers = [&](const Value &V, const Use *OldUse) {
for (const Use &UU : V.uses()) {
if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
"rejected by the equivalence call back: "
<< *UU << "!\n");
return false;
}

Worklist.push_back(&UU);
}
return true;
};

AddUsers(V, /* OldUse */ nullptr);

LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
<< " initial uses to check\n");
Expand Down Expand Up @@ -1380,15 +1393,8 @@ bool Attributor::checkForAllUses(
<< PotentialCopies.size()
<< " potential copies instead!\n");
for (Value *PotentialCopy : PotentialCopies)
for (const Use &CopyUse : PotentialCopy->uses()) {
if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) {
LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
"rejected by the equivalence call back: "
<< *CopyUse << "!\n");
return false;
}
Worklist.push_back(&CopyUse);
}
if (!AddUsers(*PotentialCopy, U))
return false;
continue;
}
}
Expand All @@ -1399,8 +1405,25 @@ bool Attributor::checkForAllUses(
return false;
if (!Follow)
continue;
for (const Use &UU : U->getUser()->uses())
Worklist.push_back(&UU);

User &Usr = *U->getUser();
AddUsers(Usr, /* OldUse */ nullptr);

auto *RI = dyn_cast<ReturnInst>(&Usr);
if (!RI)
continue;

Function &F = *RI->getFunction();
auto CallSitePred = [&](AbstractCallSite ACS) {
return AddUsers(*ACS.getInstruction(), U);
};
if (!checkForAllCallSites(CallSitePred, F, /* RequireAllCallSites */ true,
&QueryingAA, UsedAssumedInformation)) {
LLVM_DEBUG(dbgs() << "[Attributor] Could not follow return instruction "
"to all call sites: "
<< *RI << "\n");
return false;
}
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ struct AAPointerInfoFloating : public AAPointerInfoImpl {
Follow = true;
return true;
}
if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
if (isa<CastInst>(Usr) || isa<SelectInst>(Usr) || isa<ReturnInst>(Usr))
return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);

// For PHIs we need to take care of the recurrence explicitly as the value
Expand Down Expand Up @@ -7359,7 +7359,7 @@ bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
const Instruction *UserI) {
// The loaded value is unrelated to the pointer argument, no need to
// follow the users of the load.
if (isa<LoadInst>(UserI))
if (isa<LoadInst>(UserI) || isa<ReturnInst>(UserI))
return false;

// By default we follow all uses assuming UserI might leak information on U,
Expand Down
43 changes: 15 additions & 28 deletions llvm/test/Transforms/Attributor/IPConstantProp/return-argument.ll
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=11 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=11 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
; RUN: opt -attributor-cgscc -enable-new-pm=0 -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM

;; This function returns its second argument on all return statements
define internal i32* @incdec(i1 %C, i32* %V) {
; IS__TUNIT____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn
; IS__TUNIT____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn writeonly
; IS__TUNIT____-LABEL: define {{[^@]+}}@incdec
; IS__TUNIT____-SAME: (i1 [[C:%.*]], i32* noalias nofree noundef nonnull returned align 4 dereferenceable(4) "no-capture-maybe-returned" [[V:%.*]]) #[[ATTR0:[0-9]+]] {
; IS__TUNIT____-NEXT: [[X:%.*]] = load i32, i32* [[V]], align 4
; IS__TUNIT____-SAME: (i1 [[C:%.*]], i32* noalias nofree noundef nonnull returned writeonly align 4 dereferenceable(4) "no-capture-maybe-returned" [[V:%.*]]) #[[ATTR0:[0-9]+]] {
; IS__TUNIT____-NEXT: br i1 [[C]], label [[T:%.*]], label [[F:%.*]]
; IS__TUNIT____: T:
; IS__TUNIT____-NEXT: [[X1:%.*]] = add i32 [[X]], 1
; IS__TUNIT____-NEXT: store i32 [[X1]], i32* [[V]], align 4
; IS__TUNIT____-NEXT: ret i32* [[V]]
; IS__TUNIT____: F:
; IS__TUNIT____-NEXT: [[X2:%.*]] = sub i32 [[X]], 1
; IS__TUNIT____-NEXT: store i32 [[X2]], i32* [[V]], align 4
; IS__TUNIT____-NEXT: ret i32* [[V]]
;
; IS__CGSCC____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn
Expand Down Expand Up @@ -51,13 +46,13 @@ F: ; preds = %0
;; This function returns its first argument as a part of a multiple return
;; value
define internal { i32, i32 } @foo(i32 %A, i32 %B) {
; CHECK: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; CHECK-LABEL: define {{[^@]+}}@foo
; CHECK-SAME: (i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR1:[0-9]+]] {
; CHECK-NEXT: [[X:%.*]] = add i32 [[A]], [[B]]
; CHECK-NEXT: [[Y:%.*]] = insertvalue { i32, i32 } undef, i32 [[A]], 0
; CHECK-NEXT: [[Z:%.*]] = insertvalue { i32, i32 } [[Y]], i32 [[X]], 1
; CHECK-NEXT: ret { i32, i32 } [[Z]]
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; IS__CGSCC____-LABEL: define {{[^@]+}}@foo
; IS__CGSCC____-SAME: (i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR1:[0-9]+]] {
; IS__CGSCC____-NEXT: [[X:%.*]] = add i32 [[A]], [[B]]
; IS__CGSCC____-NEXT: [[Y:%.*]] = insertvalue { i32, i32 } undef, i32 [[A]], 0
; IS__CGSCC____-NEXT: [[Z:%.*]] = insertvalue { i32, i32 } [[Y]], i32 [[X]], 1
; IS__CGSCC____-NEXT: ret { i32, i32 } [[Z]]
;
%X = add i32 %A, %B
%Y = insertvalue { i32, i32 } undef, i32 %A, 0
Expand All @@ -68,17 +63,11 @@ define internal { i32, i32 } @foo(i32 %A, i32 %B) {
define void @caller(i1 %C) personality i32 (...)* @__gxx_personality_v0 {
; IS__TUNIT____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
; IS__TUNIT____-LABEL: define {{[^@]+}}@caller
; IS__TUNIT____-SAME: (i1 [[C:%.*]]) #[[ATTR1]] personality i32 (...)* @__gxx_personality_v0 {
; IS__TUNIT____-SAME: (i1 [[C:%.*]]) #[[ATTR1:[0-9]+]] personality i32 (...)* @__gxx_personality_v0 {
; IS__TUNIT____-NEXT: [[Q:%.*]] = alloca i32, align 4
; IS__TUNIT____-NEXT: [[W:%.*]] = call align 4 i32* @incdec(i1 [[C]], i32* noalias nofree noundef nonnull align 4 dereferenceable(4) "no-capture-maybe-returned" [[Q]]) #[[ATTR2:[0-9]+]]
; IS__TUNIT____-NEXT: [[S1:%.*]] = call { i32, i32 } @foo(i32 noundef 1, i32 noundef 2) #[[ATTR3:[0-9]+]]
; IS__TUNIT____-NEXT: [[X1:%.*]] = extractvalue { i32, i32 } [[S1]], 0
; IS__TUNIT____-NEXT: [[S2:%.*]] = call { i32, i32 } @foo(i32 noundef 3, i32 noundef 4) #[[ATTR3]]
; IS__TUNIT____-NEXT: [[W:%.*]] = call align 4 i32* @incdec(i1 [[C]], i32* noalias nofree noundef nonnull writeonly align 4 dereferenceable(4) "no-capture-maybe-returned" [[Q]]) #[[ATTR2:[0-9]+]]
; IS__TUNIT____-NEXT: br label [[OK:%.*]]
; IS__TUNIT____: OK:
; IS__TUNIT____-NEXT: [[X2:%.*]] = extractvalue { i32, i32 } [[S2]], 0
; IS__TUNIT____-NEXT: [[Z:%.*]] = add i32 [[X1]], [[X2]]
; IS__TUNIT____-NEXT: store i32 [[Z]], i32* [[Q]], align 4
; IS__TUNIT____-NEXT: br label [[RET:%.*]]
; IS__TUNIT____: LPAD:
; IS__TUNIT____-NEXT: unreachable
Expand Down Expand Up @@ -116,7 +105,6 @@ define void @caller(i1 %C) personality i32 (...)* @__gxx_personality_v0 {

OK:
%X2 = extractvalue { i32, i32 } %S2, 0
;; Do some stuff with the returned values which we can grep for
%Z = add i32 %X1, %X2
store i32 %Z, i32* %W
br label %RET
Expand All @@ -132,10 +120,9 @@ RET:

declare i32 @__gxx_personality_v0(...)
;.
; IS__TUNIT____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn }
; IS__TUNIT____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn writeonly }
; IS__TUNIT____: attributes #[[ATTR1]] = { nofree norecurse nosync nounwind readnone willreturn }
; IS__TUNIT____: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn }
; IS__TUNIT____: attributes #[[ATTR3]] = { nofree nosync nounwind readnone willreturn }
; IS__TUNIT____: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn writeonly }
;.
; IS__CGSCC____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn }
; IS__CGSCC____: attributes #[[ATTR1]] = { nofree norecurse nosync nounwind readnone willreturn }
Expand Down

0 comments on commit f84712f

Please sign in to comment.