[PATCH] D125485: [ArgPromotion] Unify byval promotion with non-byval

Pavel Samolysov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 12 10:58:52 PDT 2022


psamolysov created this revision.
psamolysov added reviewers: aeubanks, nikic, jdoerfert.
psamolysov added a project: LLVM.
Herald added subscribers: ormris, hiraditya.
Herald added a project: All.
psamolysov requested review of this revision.
Herald added a subscriber: llvm-commits.

It makes sense to handle byval promotion in the same way as non-byval
but also allowing `store` instructions. Such `store` instructions should be
considered as dead operations during the promotion because they do
write in a temporary copy of the argument (what the `byval` attribute
means) and these writes have no side effects.

The following code

  define internal void @f(%struct.ss* byval(%struct.ss) align 4 %b) nounwind  {
  entry:
    %temp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0
    %temp1 = load i32, i32* %temp, align 4
    %temp2 = add i32 %temp1, 1
    store i32 %temp2, i32* %temp, align 4
    ret void
  }

will be aggressively optimized into:

  define internal void @f(i32 %b_0) nounwind  {
  entry:
    %temp = add i32 %b_0, 1
    ret void
  }

If the pointer (the whole argument or a part of it) is invalidated
in the function (there is a `store` into and then `load` from), the
alias analysis used in the pass prevents the promotion. The following
code won't be optimized:

  define internal void @k(%struct.ss* byval(%struct.ss) align 4 %b) nounwind  {
  entry:
    %temp = getelementptr %struct.ss, %struct.ss* %b, i32 0, i32 0
    %temp1 = load i32, i32* %temp, align 4
    %temp2 = add i32 %temp1, 1
    store i32 %temp2, i32* %temp, align 4
    %temp3 = load i32, i32* %temp, align 4
    ret void
  }

The idea comes from the following discussion:
https://reviews.llvm.org/D124514#3479676


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125485

Files:
  llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
  llvm/test/Transforms/ArgumentPromotion/attrs.ll
  llvm/test/Transforms/ArgumentPromotion/byval-2.ll
  llvm/test/Transforms/ArgumentPromotion/byval.ll
  llvm/test/Transforms/ArgumentPromotion/dbg.ll
  llvm/test/Transforms/ArgumentPromotion/fp80.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125485.429014.patch
Type: text/x-patch
Size: 28970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220512/f201b7d9/attachment-0001.bin>


More information about the llvm-commits mailing list