This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Improve isKnownNonZero for PHI of non-zero constants
ClosedPublic

Authored by junbuml on Jan 29 2016, 1:01 PM.

Details

Summary

It is clear that a PHI is a non-zero if all incoming values are non-zero constants.

Diff Detail

Event Timeline

junbuml updated this revision to Diff 46418.Jan 29 2016, 1:01 PM
junbuml retitled this revision from to [ValueTracking] Improve isKnownNonZero for PHI of non-zero constants.
junbuml updated this object.
junbuml added reviewers: majnemer, sanjoy, jmolloy, mcrosier.
junbuml added a subscriber: llvm-commits.
sanjoy requested changes to this revision.Jan 29 2016, 1:15 PM
sanjoy edited edge metadata.

Minor comments inline.

lib/Analysis/ValueTracking.cpp
2065

A cleaner way of writing this would be:

// llvm::all_of from STLExtras.h
bool AllNonZeroConstants = all_of(PN->operands(), [](Value *V) {
  return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZeroValue();
});
if (AllNonZeroConstants)
  return true;
test/Transforms/InstCombine/phi.ll
768

Minor: you can simplify the branch to br i1 %c, label %if.then, label %if.else.

774

Minor: I'd add a call to an unknown function in one of the if branches. Without it simplifycfg changes the PHI to a select (and elides the control flow), after which LLVM is able to optimize this function in -O3, but with a call void @foo() in one of the branches, the phi->select conversion does not happen, and this test case demonstrates a truly missed optimization in LLVM today.

This revision now requires changes to proceed.Jan 29 2016, 1:15 PM
junbuml updated this revision to Diff 46420.Jan 29 2016, 1:29 PM
junbuml edited edge metadata.
junbuml marked 2 inline comments as done.

Thanks Sanjoy for the review.

junbuml updated this revision to Diff 46424.Jan 29 2016, 1:42 PM
junbuml edited edge metadata.
junbuml marked an inline comment as done.

Fixed Sanjoy's comment again.

sanjoy accepted this revision.Jan 29 2016, 1:48 PM
sanjoy edited edge metadata.
This revision is now accepted and ready to land.Jan 29 2016, 1:48 PM

Landed in r259370. Thanks Sanjoy for the review.

mcrosier closed this revision.Feb 1 2016, 11:09 AM