From 22795de20a10835f197587b3c7c79c962657da92 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Thu, 6 Jul 2017 16:24:21 +0000 Subject: [InstCombine] Add single use checks to SimplifyBSwap to ensure we are really saving instructions Bswap isn't a simple operation so we need to make sure we are really removing a call to it before doing these simplifications. For the case when both LHS and RHS are bswaps I've allowed it to be moved if either LHS or RHS has a single use since that at least allows us to move it later where it might find another bswap to combine with and it decreases the use count on the other side so maybe the other user can be optimized. Differential Revision: https://reviews.llvm.org/D34974 llvm-svn: 307273 --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp index 55486f15ae0..a23cad40a38 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -82,20 +82,25 @@ static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS, Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) { assert(I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying"); - // TODO We should probably check for single use of the bswap. + Value *OldLHS = I.getOperand(0); + Value *OldRHS = I.getOperand(1); Value *NewLHS; - if (!match(I.getOperand(0), m_BSwap(m_Value(NewLHS)))) + if (!match(OldLHS, m_BSwap(m_Value(NewLHS)))) return nullptr; Value *NewRHS; const APInt *C; - if (match(I.getOperand(1), m_BSwap(m_Value(NewRHS)))) { + if (match(OldRHS, m_BSwap(m_Value(NewRHS)))) { // OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) ) + if (!OldLHS->hasOneUse() && !OldRHS->hasOneUse()) + return nullptr; // NewRHS initialized by the matcher. - } else if (match(I.getOperand(1), m_APInt(C))) { + } else if (match(OldRHS, m_APInt(C))) { // OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) ) + if (!OldLHS->hasOneUse()) + return nullptr; NewRHS = ConstantInt::get(I.getType(), C->byteSwap()); } else return nullptr; -- cgit v1.2.3