This is an archive of the discontinued LLVM Phabricator instance.

[LICM] Check a heuristic case to hoist load
Changes PlannedPublic

Authored by jaykang10 on Nov 11 2021, 2:01 AM.

Details

Summary

Let's see an example with MemorySSA.

loop.header:
; 2 = MemoryPhi({entry,liveOnEntry},{loop.body,1})
  %i.0 = phi i32 [ 0, %entry ], [ %inc, %loop.body ]
...

loop.body:
; MemoryUse(2) MayAlias
  %0 = load i32**, i32*** @a, align 8, !tbaa !8
  %idxprom = zext i32 %i.0 to i64
  %arrayidx = getelementptr inbounds i32*, i32** %0, i64 %idxprom
; 1 = MemoryDef(2)
  store i32* null, i32** %arrayidx, align 8, !tbaa !8
...

As you can see, there is a dependence with MayAlias between %0 = load of MemoryUse(2) and store of 1 = MemoryDef(2). However, it is WAR dependence at first iteration of the loop and there is no dependence at rest of iteration. In this case, we can hoist the '%0 = load` to preheader.

It fixes https://bugs.llvm.org/show_bug.cgi?id=52143.

Diff Detail

Event Timeline

jaykang10 created this revision.Nov 11 2021, 2:01 AM
jaykang10 requested review of this revision.Nov 11 2021, 2:01 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 11 2021, 2:01 AM
fhahn added a subscriber: fhahn.Nov 11 2021, 2:26 AM
fhahn added inline comments.
llvm/lib/Transforms/Scalar/LICM.cpp
2407

is !tbaa required here?

Also, would it be possible that @a contains the address of @a before we enter the loop? Could the first store in the loop modify @a that way?

2466

is this sufficient? An addrec could have an unknown step I think, which could be 0.

nikic requested changes to this revision.Nov 11 2021, 2:29 AM

I don't get what you're doing here. @a can store a pointer to @a, so the store will modify @a, resulting in a different loaded value on the next iteration.

This revision now requires changes to proceed.Nov 11 2021, 2:29 AM
jaykang10 planned changes to this revision.Nov 11 2021, 3:38 AM

Ah... @a could contains the address of @a... Thanks for pointing out it. @fhahn @nikic

It is fundamental problem with alias between different pointer types like long **, long***... I need to find the clear background reason to distinguish them...

Let me think about it more.

llvm/lib/Transforms/Scalar/LICM.cpp
2407

is !tbaa required here?

The same !tbaa on load and store is intended to show that it causes MayAlias on TBAA.

Also, would it be possible that @a contains the address of @a before we enter the loop? Could the first store in the loop modify @a that way?

You are right! I missed it...

2466

Ah, step could be 0... Thanks for pointing out it.