This is an archive of the discontinued LLVM Phabricator instance.

[GuardWidening] Insert fictive guards to loop preheader to increase chances of widening
AbandonedPublic

Authored by mkazantsev on Aug 3 2018, 6:01 AM.

Details

Summary

Sometimes we have something like:

preheader:
  guard(%c1)[];
  %c2 =<some cond>;
  br i1 %loop

loop:
  ...
  guard(%c2)
  ...

Despite %c2 being loop invariant, we cannot widen the 2nd guard into
the 1st one in case if %c2 is not available at the point of 1st guard. To
solve that situation, we can insert the fictive guard with true condition
like this:

preheader:
  guard(%c1)[];
  %c2 =<some cond>;
  guard(true)[];
  br i1 %loop

loop:
  ...
  guard(%c2)
  ...

And then widen the condition of guard in loop into this fictive guard. In case
if it didn't help, we can just safely delete the fictive guard we've just inserted.

Diff Detail

Event Timeline

sanjoy added a comment.Aug 3 2018, 6:48 AM

Did you consider phrasing this as an "LICM" on guard? It is a bit of a code smell in LLVM to do an operation speculatively and then undo it immediately.

reames requested changes to this revision.Aug 3 2018, 10:09 AM

I think this is the wrong approach. I'd suggest tackling the example in two ways:

  1. Try to hoist c2 above the existing guard. We have lots of precedent for such things, look for makeAvailableAt.
  2. Hoisting a guard from within a loop to the preheader is worthwhile. I've been cleaning up the pieces of LICM needed to make this easy. We can chat offline if you want to pick that up and finish it.
This revision now requires changes to proceed.Aug 3 2018, 10:09 AM
mkazantsev planned changes to this revision.Aug 8 2018, 8:25 PM

After some discussion, I'll try making some variation of this in LICM.