This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fix visitSwitchInst to use right operand types for sub cstexpr.
ClosedPublic

Authored by bruno on Dec 12 2014, 11:01 AM.

Details

Summary

visitSwitchInst generates SUB constant expressions to recompute the switch
condition. When truncating the condition to a smaller type, SUB expressions
should use the previous type (before trunc) for both operands. This fixes an
assertion crash.

Diff Detail

Event Timeline

bruno updated this revision to Diff 17240.Dec 12 2014, 11:01 AM
bruno retitled this revision from to [InstCombine] Fix visitSwitchInst to use right operand types for sub cstexpr..
bruno updated this object.
bruno edited the test plan for this revision. (Show Details)
bruno added a reviewer: majnemer.
bruno set the repository for this revision to rL LLVM.
bruno added a subscriber: Unknown Object (MLST).
majnemer added inline comments.Dec 14 2014, 2:44 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
2107–2108

InstCombine methods are supposed to return the original instruction if they modify the instruction. It seems visitSwitchInst isn't doing this here.

2120–2123

Isn't this equivalent to:
Constant *LHS = ConstantExpr::getSExt(CaseVal, BitWidth);

2124

Might as well make this Constant * since you are changing the right hand side.

2124

Might as well put the pointer on the right hand side.

bruno added inline comments.Dec 15 2014, 9:51 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
2107–2108

Good catch, in case where Trunc instruction is generated but no ADD instruction is found we're returning null. Will address that in a next patch since this is not related to this fix.

2120–2123

Likely, but only after a series of constant fold lookups/calls for the common cases, I guess the current version is uglier but faster.

2124

Ok!

bruno updated this revision to Diff 17295.Dec 15 2014, 9:55 AM
bruno updated this object.
bruno removed rL LLVM as the repository for this revision.

Updated patch!

majnemer added inline comments.Dec 15 2014, 10:04 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
2120

Is there any particular reason why you chose to sext instead of zext?

bruno added inline comments.Dec 15 2014, 10:21 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
2120

Being conservative to perform the right extension when coming back from the truncated negative condition cases. Would that be somehow redundant in this scenario?

majnemer added inline comments.Dec 15 2014, 1:48 PM
lib/Transforms/InstCombine/InstructionCombining.cpp
2120

I don't think it will actually be faster.

ConstantExpr::getSExt(CaseVal, Cond->getType()) will call getFoldedCast which will call ConstantFoldCastInstruction which will do the following:

case Instruction::SExt:
  if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
    uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
    return ConstantInt::get(V->getContext(),
                            CI->getValue().sext(BitWidth));
  }

Seems like it will do the exact same thing. No table lookups involved.

bruno added inline comments.Dec 16 2014, 4:16 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
2120

As I said before, it takes additional calls for every case statement. We don't need to bikeshed on that though, I guess that won't make that much difference either.

bruno updated this revision to Diff 17331.Dec 16 2014, 4:18 AM

Updated patch after David's comments/suggestions.

majnemer accepted this revision.Dec 16 2014, 3:31 PM
majnemer edited edge metadata.

LGTM with the above changes and one comment (I'd make it inline but parts of the diff are missing).

Would it be reasonable to zext instead of sext if the bits you trimmed off were all zeros?

lib/Transforms/InstCombine/InstructionCombining.cpp
2119

ConstantExpr::getSExt returns a Constant *, the cast<Constant> should be redundant.

2120–2121

Likewise. Also, please stick the pointer on the RHS.

This revision is now accepted and ready to land.Dec 16 2014, 3:31 PM
bruno closed this revision.Dec 19 2014, 6:28 AM

Use zext when appropriate as suggested and committed in r224574.

Thanks David.