Skip to content

Commit 014f5bc

Browse files
committed
[FunctionAttrs] Replace MemoryAccessKind with FMRB.
Update FunctionAttrs to use FunctionModRefBehavior instead MemoryAccessKind. This allows for adding support for inferring argmemonly and others, see D121415. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D121460
1 parent 8bd0055 commit 014f5bc

File tree

4 files changed

+25
-45
lines changed

4 files changed

+25
-45
lines changed

llvm/include/llvm/Transforms/IPO/FunctionAttrs.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H
1616
#define LLVM_TRANSFORMS_IPO_FUNCTIONATTRS_H
1717

18+
#include "llvm/Analysis/AliasAnalysis.h"
1819
#include "llvm/Analysis/CGSCCPassManager.h"
1920
#include "llvm/Analysis/LazyCallGraph.h"
2021
#include "llvm/IR/ModuleSummaryIndex.h"
@@ -27,17 +28,9 @@ class Function;
2728
class Module;
2829
class Pass;
2930

30-
/// The three kinds of memory access relevant to 'readonly' and
31-
/// 'readnone' attributes.
32-
enum MemoryAccessKind {
33-
MAK_ReadNone = 0,
34-
MAK_ReadOnly = 1,
35-
MAK_MayWrite = 2,
36-
MAK_WriteOnly = 3
37-
};
38-
3931
/// Returns the memory access properties of this copy of the function.
40-
MemoryAccessKind computeFunctionBodyMemoryAccess(Function &F, AAResults &AAR);
32+
FunctionModRefBehavior computeFunctionBodyMemoryAccess(Function &F,
33+
AAResults &AAR);
4134

4235
/// Propagate function attributes for function summaries along the index's
4336
/// callgraph during thinlink

llvm/lib/Transforms/IPO/FunctionAttrs.cpp

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,16 @@ using SCCNodeSet = SmallSetVector<Function *, 8>;
121121
/// result will be based only on AA results for the function declaration; it
122122
/// will be assumed that some other (perhaps less optimized) version of the
123123
/// function may be selected at link time.
124-
static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
125-
AAResults &AAR,
126-
const SCCNodeSet &SCCNodes) {
124+
static FunctionModRefBehavior
125+
checkFunctionMemoryAccess(Function &F, bool ThisBody, AAResults &AAR,
126+
const SCCNodeSet &SCCNodes) {
127127
FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
128128
if (MRB == FMRB_DoesNotAccessMemory)
129129
// Already perfect!
130-
return MAK_ReadNone;
130+
return MRB;
131131

132-
if (!ThisBody) {
133-
if (AliasAnalysis::onlyReadsMemory(MRB))
134-
return MAK_ReadOnly;
135-
136-
if (AliasAnalysis::onlyWritesMemory(MRB))
137-
return MAK_WriteOnly;
138-
139-
// Conservatively assume it reads and writes to memory.
140-
return MAK_MayWrite;
141-
}
132+
if (!ThisBody)
133+
return MRB;
142134

143135
// Scan the function body for instructions that may read or write memory.
144136
bool ReadsMemory = false;
@@ -232,18 +224,18 @@ static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
232224
ReadsMemory |= I.mayReadFromMemory();
233225
}
234226

235-
if (WritesMemory) {
227+
if (WritesMemory) {
236228
if (!ReadsMemory)
237-
return MAK_WriteOnly;
229+
return FMRB_OnlyWritesMemory;
238230
else
239-
return MAK_MayWrite;
231+
return FMRB_UnknownModRefBehavior;
240232
}
241233

242-
return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
234+
return ReadsMemory ? FMRB_OnlyReadsMemory : FMRB_DoesNotAccessMemory;
243235
}
244236

245-
MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
246-
AAResults &AAR) {
237+
FunctionModRefBehavior llvm::computeFunctionBodyMemoryAccess(Function &F,
238+
AAResults &AAR) {
247239
return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
248240
}
249241

@@ -262,20 +254,14 @@ static void addMemoryAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter,
262254
// Non-exact function definitions may not be selected at link time, and an
263255
// alternative version that writes to memory may be selected. See the
264256
// comment on GlobalValue::isDefinitionExact for more details.
265-
switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
266-
AAR, SCCNodes)) {
267-
case MAK_MayWrite:
257+
FunctionModRefBehavior FMRB =
258+
checkFunctionMemoryAccess(*F, F->hasExactDefinition(), AAR, SCCNodes);
259+
if (isModAndRefSet(createModRefInfo(FMRB)))
268260
return;
269-
case MAK_ReadOnly:
270-
ReadsMemory = true;
271-
break;
272-
case MAK_WriteOnly:
273-
WritesMemory = true;
274-
break;
275-
case MAK_ReadNone:
276-
// Nothing to do!
277-
break;
278-
}
261+
if (FMRB == FMRB_DoesNotAccessMemory)
262+
continue;
263+
ReadsMemory |= AliasAnalysis::onlyReadsMemory(FMRB);
264+
WritesMemory |= AliasAnalysis::onlyWritesMemory(FMRB);
279265
}
280266

281267
// If the SCC contains both functions that read and functions that write, then

llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ void splitAndWriteThinLTOBitcode(
311311
return;
312312
}
313313
if (!F->isDeclaration() &&
314-
computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone)
314+
computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) ==
315+
FMRB_DoesNotAccessMemory)
315316
EligibleVirtualFns.insert(F);
316317
});
317318
}

llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ bool DevirtModule::tryVirtualConstProp(
17401740
for (VirtualCallTarget &Target : TargetsForSlot) {
17411741
if (Target.Fn->isDeclaration() ||
17421742
computeFunctionBodyMemoryAccess(*Target.Fn, AARGetter(*Target.Fn)) !=
1743-
MAK_ReadNone ||
1743+
FMRB_DoesNotAccessMemory ||
17441744
Target.Fn->arg_empty() || !Target.Fn->arg_begin()->use_empty() ||
17451745
Target.Fn->getReturnType() != RetType)
17461746
return false;

0 commit comments

Comments
 (0)