This is an archive of the discontinued LLVM Phabricator instance.

Modify constraints in `llvm::canReplaceOperandWithVariable`
ClosedPublic

Authored by aoli on Jun 30 2017, 1:30 PM.

Details

Summary

Instruction::Switch: only first operand can be set to a non-constant value.
Instruction::InsertValue both the first and the second operand can be set to a non-constant value.
Instruction::Alloca return true for non-static allocation.

Event Timeline

aoli created this revision.Jun 30 2017, 1:30 PM
aoli retitled this revision from The second operand for `insertvalue` can be set to a non-constant value. to Bring more constraints to `llvm::canReplaceOperandWithVariable`.Jun 30 2017, 3:21 PM
aoli edited the summary of this revision. (Show Details)
aoli updated this revision to Diff 104948.Jun 30 2017, 3:22 PM

Add more constraints.

aoli updated this revision to Diff 104970.Jun 30 2017, 5:44 PM

Remove Alloca.

aoli edited the summary of this revision. (Show Details)Jun 30 2017, 6:27 PM
aoli retitled this revision from Bring more constraints to `llvm::canReplaceOperandWithVariable` to Modify constraints in `llvm::canReplaceOperandWithVariable`.Jul 5 2017, 10:13 AM
aoli edited the summary of this revision. (Show Details)
aoli added a reviewer: efriedma.
aoli added subscribers: llvm-commits, pirama, srhines.
efriedma edited edge metadata.Jul 5 2017, 12:52 PM

alloca in particular is a little tricky. It's legal to hoist a constant operand (it doesn't change the semantics), but it's a bad idea: it turns a constant-size stack allocation into a variable-size stack allocation, which is much slower. Better to avoid messing with it. (This is similar to the way the current callers of canReplaceOperandWithVariable handle calls: it's technically legal, but not a good idea.)

Other changes look fine.

aoli updated this revision to Diff 105337.Jul 5 2017, 2:13 PM

Add Instruction::Alloca back.

It would be nice to have a testcase for the InsertValue change, since it has an impact on SimplifyCFG even without D34921.

aoli updated this revision to Diff 105360.Jul 5 2017, 4:49 PM

Test sink common code for insertvalue.

efriedma accepted this revision.Jul 5 2017, 5:33 PM

LGTM

This revision is now accepted and ready to land.Jul 5 2017, 5:33 PM
aoli added a comment.Jul 5 2017, 6:07 PM

As we metioned in https://reviews.llvm.org/D34921. Should canReplaceOperandWithVariable only return false if isStaticAlloca is true for Instruction::Alloca here?

Yes, that makes sense. (Please add a testcase to sink-common-code.ll.)

aoli updated this revision to Diff 105468.Jul 6 2017, 10:24 AM

canReplaceOperandWithVariable return true for non-static alloca instruction.

aoli edited the summary of this revision. (Show Details)Jul 6 2017, 10:24 AM
aoli added a comment.EditedJul 6 2017, 11:33 AM

Hi, I changed the canReplaceOperandWithVariable but I did not add test for sink-common-code.ll. because I found in SimplifyCFG.cpp canSinkInstructions: there is a check:

for (auto *I : Insts) {
  // These instructions may change or break semantics if moved.
  if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
      I->getType()->isTokenTy())
    return false;

So it will always return false for AllocaInst.

For GVN sink, alloca instruction is also blacklisted in GVNSink::isInstructionBlacklisted.

So the logic in canReplaceOperandWithVariable will never be reached.

efriedma accepted this revision.Jul 6 2017, 11:36 AM

That's fine. LGTM.

aoli closed this revision.Jul 6 2017, 11:47 AM