Skip to content

Commit

Permalink
[InferAttrs] Add writeonly to all the math functions
Browse files Browse the repository at this point in the history
All of these functions would be `readnone`, but can't be on platforms
where they can set `errno`. A `writeonly` function with no pointer
arguments can only write (but never read) global state.

Writeonly theoretically allows these calls to be CSE'd (a writeonly call
with the same arguments will always result in the same global stores) or
hoisted out of loops, but that's not implemented currently.

There are a few functions in this list that could be `readnone` instead
of `writeonly`, if someone is interested.

Differential Revision: https://reviews.llvm.org/D116426
  • Loading branch information
d0k committed Jan 4, 2022
1 parent 8aea5d5 commit ea75be3
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 115 deletions.
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/Utils/BuildLibCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ STATISTIC(NumReadNone, "Number of functions inferred as readnone");
STATISTIC(NumInaccessibleMemOnly,
"Number of functions inferred as inaccessiblememonly");
STATISTIC(NumReadOnly, "Number of functions inferred as readonly");
STATISTIC(NumWriteOnly, "Number of functions inferred as writeonly");
STATISTIC(NumArgMemOnly, "Number of functions inferred as argmemonly");
STATISTIC(NumInaccessibleMemOrArgMemOnly,
"Number of functions inferred as inaccessiblemem_or_argmemonly");
Expand Down Expand Up @@ -71,6 +72,14 @@ static bool setOnlyReadsMemory(Function &F) {
return true;
}

static bool setDoesNotReadMemory(Function &F) {
if (F.doesNotReadMemory()) // writeonly or readnone
return false;
F.setDoesNotReadMemory();
++NumWriteOnly;
return true;
}

static bool setOnlyAccessesArgMemory(Function &F) {
if (F.onlyAccessesArgMemory())
return false;
Expand Down Expand Up @@ -1171,6 +1180,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
case LibFunc_truncl:
Changed |= setDoesNotThrow(F);
Changed |= setDoesNotFreeMemory(F);
Changed |= setDoesNotReadMemory(F);
Changed |= setWillReturn(F);
return Changed;
default:
Expand Down
Loading

0 comments on commit ea75be3

Please sign in to comment.