[PATCH] D100717: [InstCombine] Transform memcpy to ptr load/stores if TBAA says so

Juneyoung Lee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 18 01:38:58 PDT 2021


aqjune created this revision.
aqjune added reviewers: lebedev.ri, nikic, fhahn, spatel, nlopes.
Herald added subscribers: dexonsmith, pengfei, kosarev, hiraditya, arichardson.
aqjune requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Folding `inttoptr(ptrtoint p) -> p` has been known as a source of miscompilation (http://llvm.org/pr34548)
because it swaps the object that some pointer is based-on.
Recently I've been playing with `CastInst::isEliminableCastPair` to see how removal of it
affects performance, and got very nice results.

On x86-64, compiling single amalgamated file benchmarks (bzip2, gzip, oggenc, ph7, sqlite3, gcc)[1, 2, 3]
didn't show a single difference in assembly even after the folding is removed.
I guess this is highly related to the recent efforts (D88788 <https://reviews.llvm.org/D88788>, D88789 <https://reviews.llvm.org/D88789>, D88979 <https://reviews.llvm.org/D88979>) in reducing the number of
inttoptr casts unnecessarily generated by middle-end transformations.

The int->ptr casts used in the source programs are either

(1) int->ptr casts of a constant, e.g. `(int*)-1`
(2) manipulating bits using constants, e.g. `(int*)((intptr_t)x | 0x3`

.. both of which cannot gain benefit from `inttoptr(ptrtoint p) -> p` folding anyway.
For the latter one, we can suggest using `llvm.ptrmask` as well for better performance.

For larger benchmark, I chose LLVM (as suggested by @nlopes). 
Compiling LLVM with/without this folding showed quite a few differences in assembly, however.
But they are still mostly from inttoptrs generated by the optimizations.

This patch fixes LLVM by using load/store i8* (which is `char*` in C/C++) instead if !tbaa_struct
says the element had a character type.
This patch reduces the number of different assembly files from 1255 to 845 (-32%).
This also reduces the total number of inttoptrs after `-O3` significantly for some files.

[1] https://people.csail.mit.edu/smcc/projects/single-file-programs/
[2] https://www.sqlite.org/amalgamation.html
[3] https://ph7.symisc.net/downloads.html


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100717

Files:
  llvm/include/llvm/IR/Metadata.h
  llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/memcpy-tbaa.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100717.338363.patch
Type: text/x-patch
Size: 5341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210418/5fb727ee/attachment.bin>


More information about the llvm-commits mailing list