This is an archive of the discontinued LLVM Phabricator instance.

[TailCallElim] Add handling of readonly functions
AbandonedPublic

Authored by laytonio on Apr 15 2020, 6:01 PM.

Details

Summary

We can perform accumulator recursion on values loaded from memory, if we verify that the memory isn't modified before the function would have returned.

Diff Detail

Event Timeline

laytonio created this revision.Apr 15 2020, 6:01 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2020, 6:01 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
laytonio updated this revision to Diff 257919.Apr 15 2020, 6:07 PM

noticed minor typo in the regression test

xbolva00 added a subscriber: xbolva00.

I'm concerned that hoisting instructions to the entry block isn't safe. A load can have undefined behavior, and you don't try to prove the load in question points to a valid address. Is there some way to make this transform work without hoisting the operations to the entry block? At first glance, it seems like it should be possible to instead sink the instructions into the return block. But I haven't thought about it very carefully.

llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
391

While you're in this area, the following currently crashes opt -tailcallelim:

define i32 @f() local_unnamed_addr {
entry:
  %call = call i32 @g()
  switch i32 %call, label %sw.default [
    i32 1, label %cleanup
    i32 5, label %cleanup
    i32 7, label %cleanup
  ]

sw.default:
  %call1 = call i32 @f()
  %add = add nsw i32 %call1, 1
  br label %cleanup

cleanup:
  %retval.0 = phi i32 [ %add, %sw.default ], [ %call, %entry ], [ %call, %entry ], [ %call, %entry ]
  ret i32 %retval.0
}

declare i32 @g()

Please take a look, since it seems related to what you're doing.

400

llvm::all_of.

laytonio marked 2 inline comments as done.Apr 23 2020, 3:29 PM
laytonio added inline comments.
llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
391

I created a separate diff for this fix. https://reviews.llvm.org/D78765

laytonio marked an inline comment as done.Apr 23 2020, 3:32 PM
laytonio abandoned this revision.May 2 2020, 3:25 PM