summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@intel.com>2018-05-03 05:48:49 +0000
committerCraig Topper <craig.topper@intel.com>2018-05-03 05:48:49 +0000
commit856fd686908aac7edb37f1013874cb79c84de5bc (patch)
tree1c1e08c22c2e6ad5456d83bbf2caa837f0e74c3f
parenta0cba89f861da4cba80c28a1d3cf30c8e0fb436e (diff)
downloadbcm5719-llvm-856fd686908aac7edb37f1013874cb79c84de5bc.tar.gz
bcm5719-llvm-856fd686908aac7edb37f1013874cb79c84de5bc.zip
[LoopIdiomRecognize] When looking for 'x & (x -1)' for popcnt, make sure the left hand side of the 'and' matches the left hand side of the 'subtract'
llvm-svn: 331437
-rw-r--r--llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp2
-rw-r--r--llvm/test/Transforms/LoopIdiom/X86/popcnt.ll16
2 files changed, 7 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
index fc64058fd4a..59753579bc3 100644
--- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
@@ -1196,7 +1196,7 @@ static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
VarX1 = DefX2->getOperand(0);
SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
}
- if (!SubOneOp)
+ if (!SubOneOp || SubOneOp->getOperand(0) != VarX1)
return false;
ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1));
diff --git a/llvm/test/Transforms/LoopIdiom/X86/popcnt.ll b/llvm/test/Transforms/LoopIdiom/X86/popcnt.ll
index 94e836b7440..4c8920358d6 100644
--- a/llvm/test/Transforms/LoopIdiom/X86/popcnt.ll
+++ b/llvm/test/Transforms/LoopIdiom/X86/popcnt.ll
@@ -139,29 +139,25 @@ while.end: ; preds = %while.body, %entry
ret i32 %c.0.lcssa
}
-; The a & (a - 1) in the loop is a & (b - 1) in this code.
-; FIXME: We shouldn't emit ctpop for this.
+; The a & (a - 1) in the loop is a & (b - 1) in this code. Make sure we don't
+; convert it.
define i32 @popcount_bad(i64 %a, i64 %b) nounwind uwtable readnone ssp {
; CHECK-LABEL: @popcount_bad(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.ctpop.i64(i64 [[A:%.*]])
-; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[TMP0]] to i32
-; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0
-; CHECK-NEXT: br i1 [[TMP2]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
+; CHECK-NEXT: [[TOBOOL3:%.*]] = icmp eq i64 [[A:%.*]], 0
+; CHECK-NEXT: br i1 [[TOBOOL3]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
; CHECK: while.body.preheader:
; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
; CHECK: while.body:
-; CHECK-NEXT: [[TCPHI:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY_PREHEADER]] ], [ [[TCDEC:%.*]], [[WHILE_BODY]] ]
; CHECK-NEXT: [[C_05:%.*]] = phi i32 [ [[INC:%.*]], [[WHILE_BODY]] ], [ 0, [[WHILE_BODY_PREHEADER]] ]
; CHECK-NEXT: [[A_ADDR_04:%.*]] = phi i64 [ [[AND:%.*]], [[WHILE_BODY]] ], [ [[A]], [[WHILE_BODY_PREHEADER]] ]
; CHECK-NEXT: [[INC]] = add nsw i32 [[C_05]], 1
; CHECK-NEXT: [[SUB:%.*]] = add i64 [[B:%.*]], -1
; CHECK-NEXT: [[AND]] = and i64 [[SUB]], [[A_ADDR_04]]
-; CHECK-NEXT: [[TCDEC]] = sub nsw i32 [[TCPHI]], 1
-; CHECK-NEXT: [[TOBOOL:%.*]] = icmp sle i32 [[TCDEC]], 0
+; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i64 [[AND]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[WHILE_END_LOOPEXIT:%.*]], label [[WHILE_BODY]]
; CHECK: while.end.loopexit:
-; CHECK-NEXT: [[INC_LCSSA:%.*]] = phi i32 [ [[TMP1]], [[WHILE_BODY]] ]
+; CHECK-NEXT: [[INC_LCSSA:%.*]] = phi i32 [ [[INC]], [[WHILE_BODY]] ]
; CHECK-NEXT: br label [[WHILE_END]]
; CHECK: while.end:
; CHECK-NEXT: [[C_0_LCSSA:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC_LCSSA]], [[WHILE_END_LOOPEXIT]] ]
OpenPOWER on IntegriCloud