Page MenuHomePhabricator

[InstSimplify] Simplify to vector constants when possible
ClosedPublic

Authored by aeubanks on Aug 13 2020, 4:45 PM.

Details

Summary

InstSimplify should do all transformations that ConstProp does, but
one thing that ConstProp does that InstSimplify wouldn't is inline
vector instructions that are constants, e.g. into a ret.

Previously vector instructions wouldn't be inlined in InstSimplify
because llvm::Simplify*Instruction() would return nullptr for specific
instructions, such as vector instructions that were actually constants,
if it couldn't simplify them.

This changes SimplifyInsertElementInst, SimplifyExtractElementInst, and
SimplifyShuffleVectorInst to return a vector constant when possible.

Diff Detail

Event Timeline

aeubanks created this revision.Aug 13 2020, 4:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 13 2020, 4:45 PM
aeubanks requested review of this revision.Aug 13 2020, 4:45 PM

The other possibility is to change SimplifyExtractValueInst() (and related functions) to return a vector constant even if the element can't be folded?

aeubanks retitled this revision from [InstSimplify] Inline vector constants to [InstSimplify] Inline vector constants from vector instructions.Aug 13 2020, 4:49 PM
aeubanks added reviewers: efriedma, nikic.

Many Simplify* operations (at least, all the ones that call foldOrCommuteConstant) will return a constant expression if possible. I think it would make sense to extend that behavior to all of them.

aeubanks updated this revision to Diff 287801.Aug 25 2020, 5:15 PM

Move constant folding to Simplify*Inst()

aeubanks retitled this revision from [InstSimplify] Inline vector constants from vector instructions to [InstSimplify] Simplify to vector constants when possible.Aug 25 2020, 5:16 PM
aeubanks edited the summary of this revision. (Show Details)
efriedma added inline comments.Aug 25 2020, 5:37 PM
llvm/lib/Analysis/InstructionSimplify.cpp
4306

ConstantExpr::getInsertElement calls ConstantFoldInsertElementInstruction in its implementation.

4614

This is redundant, I think?

aeubanks updated this revision to Diff 288024.Aug 26 2020, 10:13 AM

Skip ConstantFoldInsertElementInstruction() since it's called by ConstantExpr::getInsertElement()

llvm/lib/Analysis/InstructionSimplify.cpp
4306

Fixed.

4614

What do you mean? Removing this makes the newly added insertelement_shufflevector_inline_to_ret() fail.

aeubanks added inline comments.Aug 26 2020, 10:18 AM
llvm/lib/Analysis/InstructionSimplify.cpp
4614

Oh you mean similar to ConstantExpr::getInsertElement()? The check above also checks for if (!Scalable) which is why it doesn't trigger here. Maybe above instead of

if (!Scalable && Op0Const && Op1Const)
  return ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask);

it should be

if (Op0Const && Op1Const)
  if (auto* Res = ConstantFoldShuffleVectorInstruction(Op0Const, Op1Const, Mask))
    return Res;

?

aeubanks updated this revision to Diff 288031.Aug 26 2020, 10:22 AM

Use ConstantExpr::get*() in place of all ConstantFold*()
Conditionally check result in shufflevector due to Scalable

efriedma accepted this revision.Aug 26 2020, 11:24 AM

LGTM with one minor comment.

llvm/lib/Analysis/InstructionSimplify.cpp
4569

ConstantExpr::getShuffleVector can't return null.

This revision is now accepted and ready to land.Aug 26 2020, 11:24 AM
aeubanks updated this revision to Diff 288054.Aug 26 2020, 11:39 AM

ConstantExpr::getShuffleVector can't return null here

This revision was landed with ongoing or failed builds.Aug 26 2020, 11:42 AM
This revision was automatically updated to reflect the committed changes.