This is an archive of the discontinued LLVM Phabricator instance.

[Reassociate] Skip reassociation for IV if its def instruction contains NSW/NUW flags to enable better IndVar and LSR
Needs ReviewPublic

Authored by wmi on Oct 31 2016, 3:14 PM.

Details

Summary

Reassociation will remove nsw/nuw flags after change the order of computation. It may block IndVar from doing IV use widening and also block the optimization of LSR.

For %sub1 = %i.0 + %size - 1 in the following testcase, reassociation will convert it to %sub1 = %size - 1 + %i.0 and remove all the nsw flags. Then IndVar cannot do widening for %idxprom and cannot remove the sext. In addition, LSR cannot regard %arrayidx as an induction variable and it may produce suboptimal result.

for.body:

%add = add nsw i32 %i.0, %size
%sub1 = add nsw i32 %add, -1
%idxprom = sext i32 %sub1 to i64
%arrayidx = getelementptr inbounds [1000 x i32], [1000 x i32]* @maxarray, i64 0, i64 %idxprom
%tmp0 = load i32, i32* %arrayidx, align 4
%tmp1 = load i32, i32* @total, align 4
%add2 = add nsw i32 %tmp1, %tmp0
store i32 %add2, i32* @total, align 4
%inc = add nsw i32 %i.0, 1
br label %for.cond

The patch skips reassociation for IV use when it contains NSW/NUW flags so as to let IndVar and LSR do better jobs. Because LSR will also do reassociation work in its Formula canonicalization, we won't lose performance opportunity mostly.

Diff Detail

Repository
rL LLVM

Event Timeline

wmi updated this revision to Diff 76476.Oct 31 2016, 3:14 PM
wmi retitled this revision from to [Reassociate] Skip reassociation for IV if its def instruction contains NSW/NUW flags to enable better IndVar and LSR.
wmi updated this object.
wmi added reviewers: sanjoy, dberlin, davidxl.
wmi set the repository for this revision to rL LLVM.
wmi added a subscriber: llvm-commits.
wmi updated this revision to Diff 77262.Nov 8 2016, 2:29 PM

Switch the order of reassociate and looprotate so reassociate will know more precisely whether the loop will be in simplified form and suitable for LSR. I expect the pass order change will not have noticeable impact.

sanjoy requested changes to this revision.Nov 8 2016, 6:56 PM
sanjoy edited edge metadata.

This can potentially be a compile time issue. I think the SCEV dependency itself is fine, since SCEV will do things lazily, but computing the LoopInfo can be a problem.

Can this be solved by some changes to our pass order? I.e. first run indvars and then re-associate?

This revision now requires changes to proceed.Nov 8 2016, 6:56 PM

What about having two modes for reassociate: one where we don't do any transformation that drop nsw/nuw flag, and another where it is permitted. Depending on the position in the pipeline we allow or not to drop the flags.

sanjoy resigned from this revision.Jan 29 2022, 5:27 PM
This revision now requires review to proceed.Jan 29 2022, 5:27 PM