This is an archive of the discontinued LLVM Phabricator instance.

[LICM] Replace incoming values with undef if the incoming BB is unreachable
ClosedPublic

Authored by majnemer on Jul 2 2015, 12:23 PM.

Details

Summary

Unreachable BBs don't meaningfully contribute to the PHI node, replace
their incoming value with undef.

This fixes PR24013.

Diff Detail

Repository
rL LLVM

Event Timeline

majnemer updated this revision to Diff 28967.Jul 2 2015, 12:23 PM
majnemer retitled this revision from to [LICM] Replace incoming values with undef if the incoming BB is unreachable.
majnemer updated this object.
majnemer added a reviewer: chandlerc.
majnemer added a subscriber: llvm-commits.
chandlerc added inline comments.Jul 10 2015, 11:35 PM
lib/Transforms/Scalar/LICM.cpp
614–621 ↗(On Diff #28967)

Why do this here rather than in InstSimplify? Do we form these too late for that to clean it up? If so, should we add it to InstSimplify and call that here?

majnemer added inline comments.Jul 11 2015, 12:19 AM
lib/Transforms/Scalar/LICM.cpp
614–621 ↗(On Diff #28967)

I don't know what we could do in InstructionSimplify instead of here.

Running InstructionSimplify on the PHI won't help because it cannot create new instructions, it wouldn't be able to give us a new PHI which had the incoming value replaced with undef.

Running InstructionSimplify on the incoming value won't help because it might not be simplifiable. My trivial test case has a trivial incoming value but we cannot, in the general case, rely on simplification yielding a Constant. For example:

define i32 @f(i1 zeroext %p1) {
entry:
  br label %lbl

lbl.loopexit:                                     ; No predecessors!
  br label %lbl

lbl:                                              ; preds = %lbl.loopexit, %entry
  %phi = phi i32 [ %conv, %lbl.loopexit ], [ undef, %entry ]
  br i1 %p1, label %if.then.5, label %out

if.then.5:                                        ; preds = %if.then.5, %lbl
  %conv = zext i1 %p1 to i32
  br label %if.then.5

out:                                              ; preds = %lbl
  ret i32 %phi
}
hfinkel added inline comments.
lib/Transforms/Scalar/LICM.cpp
614–621 ↗(On Diff #28967)

InstCombine always has a DT now and can create new instructions, why not add this there?

majnemer added inline comments.Jul 11 2015, 10:37 AM
lib/Transforms/Scalar/LICM.cpp
614–621 ↗(On Diff #28967)

This fix address a miscompile, not a missed optimization. Sorry for the miscommunication, I'll update the summary to have an in-depth description of the bug and the rationale behind the fix.

hfinkel accepted this revision.Jul 11 2015, 11:03 AM
hfinkel added a reviewer: hfinkel.
hfinkel added inline comments.
lib/Transforms/Scalar/LICM.cpp
614–621 ↗(On Diff #28967)

Okay; I'd rather see a comment in the code explaining why what it is doing is a necessary part of the algorithm.

Assuming you add such a comment, LGTM.

This revision is now accepted and ready to land.Jul 11 2015, 11:03 AM
This revision was automatically updated to reflect the committed changes.