[llvm-commits] [llvm] r45169 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2007-12-18-AddSelCmpSub.ll
Chris Lattner
clattner at apple.com
Wed Dec 19 17:56:15 PST 2007
> URL: http://llvm.org/viewvc/llvm-project?rev=45169&view=rev
> Log:
> Remove an orthogonal transformation of the selection condition from
> my most recent submission.
Thanks for splitting this out, it makes it much easier for me to see
the forest through the trees :)
> +++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Tue
> Dec 18 14:30:28 2007
> // add (select X 0 (sub n A)) A ->
> // select X A n
> {
> SelectInst *SI = dyn_cast<SelectInst>(LHS);
> Value *Other = RHS;
> if (!SI) {
> SI = dyn_cast<SelectInst>(RHS);
> Other = LHS;
> }
> if (SI) {
> Value *TV = SI->getTrueValue();
> Value *FV = SI->getFalseValue();
> Value *A;
>
> // Can we fold the add into the argument of the select?
> // We check both true and false select arguments for a
> matching subtract.
> ConstantInt *C1, *C2;
> if (match(FV, m_ConstantInt(C1)) && C1->getValue() == 0 &&
> match(TV, m_Sub(m_ConstantInt(C2), m_Value(A))) &&
> A == Other) {
> // We managed to fold the add into the true select value.
> return new SelectInst(SI->getCondition(), C2, A);
> } else if (match(TV, m_ConstantInt(C1)) && C1->getValue() ==
> 0 &&
> match(FV, m_Sub(m_ConstantInt(C2), m_Value(A))) &&
> A == Other) {
> // We managed to fold the add into the false select value.
> return new SelectInst(SI->getCondition(), A, C2);
> }
> }
> }
This looks very nice. The one bug I see is that it should only keep
in if the select hasOneUse(). Changing it to "if (SI && SI->hasOneUse
())" should fix this. The reason for the check is that we don't want
to create multiple select instructions if the add isn't the only
thing that uses the select.
This also inspires me to add a m_Zero() matching function, so I'll
take care of the update :)
Thanks Christopher, nice job,
-Chris
More information about the llvm-commits
mailing list