Skip to content

Commit f84712f

Browse files
committed
[Attributor] Teach checkForAllUses to follow returns into callers
If we can determine all call sites we can follow a use in a return instruction into the caller. AAPointerInfo utilizes this feature.
1 parent 4f2ccdd commit f84712f

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,21 @@ bool Attributor::checkForAllUses(
13331333
SmallVector<const Use *, 16> Worklist;
13341334
SmallPtrSet<const Use *, 16> Visited;
13351335

1336-
for (const Use &U : V.uses())
1337-
Worklist.push_back(&U);
1336+
auto AddUsers = [&](const Value &V, const Use *OldUse) {
1337+
for (const Use &UU : V.uses()) {
1338+
if (OldUse && EquivalentUseCB && !EquivalentUseCB(*OldUse, UU)) {
1339+
LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1340+
"rejected by the equivalence call back: "
1341+
<< *UU << "!\n");
1342+
return false;
1343+
}
1344+
1345+
Worklist.push_back(&UU);
1346+
}
1347+
return true;
1348+
};
1349+
1350+
AddUsers(V, /* OldUse */ nullptr);
13381351

13391352
LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
13401353
<< " initial uses to check\n");
@@ -1380,15 +1393,8 @@ bool Attributor::checkForAllUses(
13801393
<< PotentialCopies.size()
13811394
<< " potential copies instead!\n");
13821395
for (Value *PotentialCopy : PotentialCopies)
1383-
for (const Use &CopyUse : PotentialCopy->uses()) {
1384-
if (EquivalentUseCB && !EquivalentUseCB(*U, CopyUse)) {
1385-
LLVM_DEBUG(dbgs() << "[Attributor] Potential copy was "
1386-
"rejected by the equivalence call back: "
1387-
<< *CopyUse << "!\n");
1388-
return false;
1389-
}
1390-
Worklist.push_back(&CopyUse);
1391-
}
1396+
if (!AddUsers(*PotentialCopy, U))
1397+
return false;
13921398
continue;
13931399
}
13941400
}
@@ -1399,8 +1405,25 @@ bool Attributor::checkForAllUses(
13991405
return false;
14001406
if (!Follow)
14011407
continue;
1402-
for (const Use &UU : U->getUser()->uses())
1403-
Worklist.push_back(&UU);
1408+
1409+
User &Usr = *U->getUser();
1410+
AddUsers(Usr, /* OldUse */ nullptr);
1411+
1412+
auto *RI = dyn_cast<ReturnInst>(&Usr);
1413+
if (!RI)
1414+
continue;
1415+
1416+
Function &F = *RI->getFunction();
1417+
auto CallSitePred = [&](AbstractCallSite ACS) {
1418+
return AddUsers(*ACS.getInstruction(), U);
1419+
};
1420+
if (!checkForAllCallSites(CallSitePred, F, /* RequireAllCallSites */ true,
1421+
&QueryingAA, UsedAssumedInformation)) {
1422+
LLVM_DEBUG(dbgs() << "[Attributor] Could not follow return instruction "
1423+
"to all call sites: "
1424+
<< *RI << "\n");
1425+
return false;
1426+
}
14041427
}
14051428

14061429
return true;

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ struct AAPointerInfoFloating : public AAPointerInfoImpl {
13011301
Follow = true;
13021302
return true;
13031303
}
1304-
if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
1304+
if (isa<CastInst>(Usr) || isa<SelectInst>(Usr) || isa<ReturnInst>(Usr))
13051305
return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
13061306

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

73657365
// By default we follow all uses assuming UserI might leak information on U,

llvm/test/Transforms/Attributor/IPConstantProp/return-argument.ll

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
2-
; 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
3-
; 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
2+
; 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
3+
; 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
44
; 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
55
; 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
66

77
;; This function returns its second argument on all return statements
88
define internal i32* @incdec(i1 %C, i32* %V) {
9-
; IS__TUNIT____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn
9+
; IS__TUNIT____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn writeonly
1010
; IS__TUNIT____-LABEL: define {{[^@]+}}@incdec
11-
; IS__TUNIT____-SAME: (i1 [[C:%.*]], i32* noalias nofree noundef nonnull returned align 4 dereferenceable(4) "no-capture-maybe-returned" [[V:%.*]]) #[[ATTR0:[0-9]+]] {
12-
; IS__TUNIT____-NEXT: [[X:%.*]] = load i32, i32* [[V]], align 4
11+
; IS__TUNIT____-SAME: (i1 [[C:%.*]], i32* noalias nofree noundef nonnull returned writeonly align 4 dereferenceable(4) "no-capture-maybe-returned" [[V:%.*]]) #[[ATTR0:[0-9]+]] {
1312
; IS__TUNIT____-NEXT: br i1 [[C]], label [[T:%.*]], label [[F:%.*]]
1413
; IS__TUNIT____: T:
15-
; IS__TUNIT____-NEXT: [[X1:%.*]] = add i32 [[X]], 1
16-
; IS__TUNIT____-NEXT: store i32 [[X1]], i32* [[V]], align 4
1714
; IS__TUNIT____-NEXT: ret i32* [[V]]
1815
; IS__TUNIT____: F:
19-
; IS__TUNIT____-NEXT: [[X2:%.*]] = sub i32 [[X]], 1
20-
; IS__TUNIT____-NEXT: store i32 [[X2]], i32* [[V]], align 4
2116
; IS__TUNIT____-NEXT: ret i32* [[V]]
2217
;
2318
; IS__CGSCC____: Function Attrs: argmemonly nofree norecurse nosync nounwind willreturn
@@ -51,13 +46,13 @@ F: ; preds = %0
5146
;; This function returns its first argument as a part of a multiple return
5247
;; value
5348
define internal { i32, i32 } @foo(i32 %A, i32 %B) {
54-
; CHECK: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
55-
; CHECK-LABEL: define {{[^@]+}}@foo
56-
; CHECK-SAME: (i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR1:[0-9]+]] {
57-
; CHECK-NEXT: [[X:%.*]] = add i32 [[A]], [[B]]
58-
; CHECK-NEXT: [[Y:%.*]] = insertvalue { i32, i32 } undef, i32 [[A]], 0
59-
; CHECK-NEXT: [[Z:%.*]] = insertvalue { i32, i32 } [[Y]], i32 [[X]], 1
60-
; CHECK-NEXT: ret { i32, i32 } [[Z]]
49+
; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
50+
; IS__CGSCC____-LABEL: define {{[^@]+}}@foo
51+
; IS__CGSCC____-SAME: (i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) #[[ATTR1:[0-9]+]] {
52+
; IS__CGSCC____-NEXT: [[X:%.*]] = add i32 [[A]], [[B]]
53+
; IS__CGSCC____-NEXT: [[Y:%.*]] = insertvalue { i32, i32 } undef, i32 [[A]], 0
54+
; IS__CGSCC____-NEXT: [[Z:%.*]] = insertvalue { i32, i32 } [[Y]], i32 [[X]], 1
55+
; IS__CGSCC____-NEXT: ret { i32, i32 } [[Z]]
6156
;
6257
%X = add i32 %A, %B
6358
%Y = insertvalue { i32, i32 } undef, i32 %A, 0
@@ -68,17 +63,11 @@ define internal { i32, i32 } @foo(i32 %A, i32 %B) {
6863
define void @caller(i1 %C) personality i32 (...)* @__gxx_personality_v0 {
6964
; IS__TUNIT____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn
7065
; IS__TUNIT____-LABEL: define {{[^@]+}}@caller
71-
; IS__TUNIT____-SAME: (i1 [[C:%.*]]) #[[ATTR1]] personality i32 (...)* @__gxx_personality_v0 {
66+
; IS__TUNIT____-SAME: (i1 [[C:%.*]]) #[[ATTR1:[0-9]+]] personality i32 (...)* @__gxx_personality_v0 {
7267
; IS__TUNIT____-NEXT: [[Q:%.*]] = alloca i32, align 4
73-
; 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]+]]
74-
; IS__TUNIT____-NEXT: [[S1:%.*]] = call { i32, i32 } @foo(i32 noundef 1, i32 noundef 2) #[[ATTR3:[0-9]+]]
75-
; IS__TUNIT____-NEXT: [[X1:%.*]] = extractvalue { i32, i32 } [[S1]], 0
76-
; IS__TUNIT____-NEXT: [[S2:%.*]] = call { i32, i32 } @foo(i32 noundef 3, i32 noundef 4) #[[ATTR3]]
68+
; 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]+]]
7769
; IS__TUNIT____-NEXT: br label [[OK:%.*]]
7870
; IS__TUNIT____: OK:
79-
; IS__TUNIT____-NEXT: [[X2:%.*]] = extractvalue { i32, i32 } [[S2]], 0
80-
; IS__TUNIT____-NEXT: [[Z:%.*]] = add i32 [[X1]], [[X2]]
81-
; IS__TUNIT____-NEXT: store i32 [[Z]], i32* [[Q]], align 4
8271
; IS__TUNIT____-NEXT: br label [[RET:%.*]]
8372
; IS__TUNIT____: LPAD:
8473
; IS__TUNIT____-NEXT: unreachable
@@ -116,7 +105,6 @@ define void @caller(i1 %C) personality i32 (...)* @__gxx_personality_v0 {
116105

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

133121
declare i32 @__gxx_personality_v0(...)
134122
;.
135-
; IS__TUNIT____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn }
123+
; IS__TUNIT____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn writeonly }
136124
; IS__TUNIT____: attributes #[[ATTR1]] = { nofree norecurse nosync nounwind readnone willreturn }
137-
; IS__TUNIT____: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn }
138-
; IS__TUNIT____: attributes #[[ATTR3]] = { nofree nosync nounwind readnone willreturn }
125+
; IS__TUNIT____: attributes #[[ATTR2]] = { nofree nosync nounwind willreturn writeonly }
139126
;.
140127
; IS__CGSCC____: attributes #[[ATTR0]] = { argmemonly nofree norecurse nosync nounwind willreturn }
141128
; IS__CGSCC____: attributes #[[ATTR1]] = { nofree norecurse nosync nounwind readnone willreturn }

0 commit comments

Comments
 (0)