summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@intel.com>2017-07-06 16:24:21 +0000
committerCraig Topper <craig.topper@intel.com>2017-07-06 16:24:21 +0000
commit22795de20a10835f197587b3c7c79c962657da92 (patch)
treefaea098efe1100c9c317b9e5539fc610220292c3
parentf0f29608d052810f0a2bbcee558f8d61a6973554 (diff)
downloadbcm5719-llvm-22795de20a10835f197587b3c7c79c962657da92.tar.gz
bcm5719-llvm-22795de20a10835f197587b3c7c79c962657da92.zip
[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
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp13
-rw-r--r--llvm/test/Transforms/InstCombine/bswap-fold.ll10
2 files changed, 13 insertions, 10 deletions
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;
diff --git a/llvm/test/Transforms/InstCombine/bswap-fold.ll b/llvm/test/Transforms/InstCombine/bswap-fold.ll
index 9a2cf5590ad..260e2330996 100644
--- a/llvm/test/Transforms/InstCombine/bswap-fold.ll
+++ b/llvm/test/Transforms/InstCombine/bswap-fold.ll
@@ -285,9 +285,8 @@ define i64 @bs_and64_multiuse1(i64 %a, i64 %b) #0 {
; CHECK-LABEL: @bs_and64_multiuse1(
; CHECK-NEXT: [[TMP1:%.*]] = tail call i64 @llvm.bswap.i64(i64 [[A:%.*]])
; CHECK-NEXT: [[TMP2:%.*]] = tail call i64 @llvm.bswap.i64(i64 [[B:%.*]])
-; CHECK-NEXT: [[TMP3:%.*]] = and i64 [[A]], [[B]]
-; CHECK-NEXT: [[TMP6:%.*]] = call i64 @llvm.bswap.i64(i64 [[TMP3]])
-; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP6]], [[TMP1]]
+; CHECK-NEXT: [[TMP3:%.*]] = and i64 [[TMP1]], [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP3]], [[TMP1]]
; CHECK-NEXT: [[TMP5:%.*]] = mul i64 [[TMP4]], [[TMP2]]
; CHECK-NEXT: ret i64 [[TMP5]]
;
@@ -332,9 +331,8 @@ define i64 @bs_and64_multiuse3(i64 %a, i64 %b) #0 {
define i64 @bs_and64i_multiuse(i64 %a, i64 %b) #0 {
; CHECK-LABEL: @bs_and64i_multiuse(
; CHECK-NEXT: [[TMP1:%.*]] = tail call i64 @llvm.bswap.i64(i64 [[A:%.*]])
-; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[A]], 129085117527228416
-; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.bswap.i64(i64 [[TMP2]])
-; CHECK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP4]], [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], 1000000001
+; CHECK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP2]], [[TMP1]]
; CHECK-NEXT: ret i64 [[TMP3]]
;
%tmp1 = tail call i64 @llvm.bswap.i64(i64 %a)
OpenPOWER on IntegriCloud