This is an archive of the discontinued LLVM Phabricator instance.

[SCEV] Search operand tree for scope bound when inferring flags from IR
ClosedPublic

Authored by reames on Oct 5 2021, 3:27 PM.

Details

Summary

This does the obvious thing, and recurses through operands searching for a tighter bound on the defining scope. I'm honestly surprised by how little this seems to mater. I think it's worth doing for completeness, but I have to admit the test diffs are unconvincing.

Diff Detail

Event Timeline

reames created this revision.Oct 5 2021, 3:27 PM
reames requested review of this revision.Oct 5 2021, 3:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 5 2021, 3:28 PM
reames updated this revision to Diff 377637.Oct 6 2021, 12:20 PM
reames edited the summary of this revision. (Show Details)
reames added reviewers: mkazantsev, lebedev.ri.

rebase over previous changes and tests

nikic added a comment.Oct 6 2021, 12:37 PM

Why does this not recover this regression? https://reviews.llvm.org/D109789#inline-1056586 I would have thought that looking through the mul/zext would have helped there.

reames added a comment.EditedOct 6 2021, 1:26 PM

Why does this not recover this regression? https://reviews.llvm.org/D109789#inline-1056586 I would have thought that looking through the mul/zext would have helped there.

Not sure, but it looks to be some negative interaction of PSE and LoopAccessAnalysis. The actual SCEV's look fine.

$ ./opt -enable-new-pm=0 -analyze -scalar-evolution scev-reduced.ll
Printing analysis 'Scalar Evolution Analysis' for function 'test1':
Classifying expressions for: @test1
....

%arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom
-->  ((4 * (zext i32 {1,+,1}<%for.body> to i64))<nuw><nsw> + %a)<nuw> U: full-set S: full-set		Exits: <<Unknown>>		LoopDispositions: { %for.body: Computable }

edit: actually, it does fix it. However lit does a prefix match not an exact match and the way the test line is written, that matches both regressed and unregressed. I'll update the test to show the more precise form. Good catch!

reames updated this revision to Diff 377666.Oct 6 2021, 1:31 PM

hand tweak test to show improvement noted by @nikic

nikic added inline comments.Oct 6 2021, 2:30 PM
llvm/lib/Analysis/ScalarEvolution.cpp
6605

Is an else missing here? We probably don't want to recurse through addrecs here, as the addrec will already have the smallest scope.

6613

Huh, I'm surprised we don't have a simpler way to write this.

reames added inline comments.Oct 6 2021, 2:41 PM
llvm/lib/Analysis/ScalarEvolution.cpp
6605

Er, yes, yes it is. Good catch. :)

6613

Me too honestly. Closest alternative I could find was to use a visitor to recurse through all the operands, but that seemed worse.

nikic accepted this revision.Oct 6 2021, 3:04 PM

LGTM

llvm/lib/Analysis/ScalarEvolution.cpp
6630

nit: This newline probably shouldn't be here.

This revision is now accepted and ready to land.Oct 6 2021, 3:04 PM
xbolva00 added inline comments.
llvm/test/Analysis/LoopAccessAnalysis/memcheck-wrapping-pointers.ll
19

Here we say nsw

48

And here nuw..

Suspicous.

This revision was landed with ongoing or failed builds.Oct 6 2021, 3:10 PM
This revision was automatically updated to reflect the committed changes.
nikic added inline comments.Oct 6 2021, 3:12 PM
llvm/test/Analysis/LoopAccessAnalysis/memcheck-wrapping-pointers.ll
48

The comment above is indeed outdated, but nuw is correct here. This is because inbounds only guarantees no overflow in the unsigned address space (despite the index being signed).