b5061d4
From a6fa10c14649c18d299cddf3e823b032460cb6f5 Mon Sep 17 00:00:00 2001
b5061d4
From: Pirama Arumuga Nainar <pirama@google.com>
b5061d4
Date: Thu, 23 Mar 2017 16:47:47 +0000
b5061d4
Subject: [PATCH] Fix computeKnownBits for ARMISD::CMOV
b5061d4
b5061d4
Summary:
b5061d4
The true and false operands for the CMOV are operands 0 and 1.
b5061d4
ARMISelLowering.cpp::computeKnownBits was looking at operands 1 and 2
b5061d4
instead.  This can cause CMOV instructions to be incorrectly folded into
b5061d4
BFI if value set by the CMOV is another CMOV, whose known bits are
b5061d4
computed incorrectly.
b5061d4
b5061d4
This patch fixes the issue and adds a test case.
b5061d4
b5061d4
Reviewers: kristof.beyls, jmolloy
b5061d4
b5061d4
Subscribers: llvm-commits, aemerson, srhines, rengolin
b5061d4
b5061d4
Differential Revision: https://reviews.llvm.org/D31265
b5061d4
b5061d4
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298624 91177308-0d34-0410-b5e6-96231b3b80d8
b5061d4
---
b5061d4
 lib/Target/ARM/ARMISelLowering.cpp |  4 ++--
b5061d4
 test/CodeGen/ARM/no-cmov2bfi.ll    | 19 +++++++++++++++++++
b5061d4
 2 files changed, 21 insertions(+), 2 deletions(-)
b5061d4
 create mode 100644 test/CodeGen/ARM/no-cmov2bfi.ll
b5061d4
b5061d4
diff --git a/lib/Target/ARM/ARMISelLowering.cpp b/lib/Target/ARM/ARMISelLowering.cpp
b5061d4
index 4a227a3cd7b1..cf98e60c0657 100644
b5061d4
--- a/lib/Target/ARM/ARMISelLowering.cpp
b5061d4
+++ b/lib/Target/ARM/ARMISelLowering.cpp
b5061d4
@@ -10806,8 +10806,8 @@ static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
b5061d4
   if (Op.getOpcode() == ARMISD::CMOV) {
b5061d4
     APInt KZ2(KnownZero.getBitWidth(), 0);
b5061d4
     APInt KO2(KnownOne.getBitWidth(), 0);
b5061d4
-    computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
b5061d4
-    computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
b5061d4
+    computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
b5061d4
+    computeKnownBits(DAG, Op.getOperand(1), KZ2, KO2);
b5061d4
 
b5061d4
     KnownZero &= KZ2;
b5061d4
     KnownOne &= KO2;
b5061d4
diff --git a/test/CodeGen/ARM/no-cmov2bfi.ll b/test/CodeGen/ARM/no-cmov2bfi.ll
b5061d4
new file mode 100644
b5061d4
index 000000000000..c8b512048905
b5061d4
--- /dev/null
b5061d4
+++ b/test/CodeGen/ARM/no-cmov2bfi.ll
b5061d4
@@ -0,0 +1,19 @@
b5061d4
+; RUN: llc < %s -mtriple=thumbv7 | FileCheck --check-prefix=CHECK-NOBFI %s
b5061d4
+
b5061d4
+declare zeroext i1 @dummy()
b5061d4
+
b5061d4
+define i8 @test(i8 %a1, i1 %c) {
b5061d4
+; CHECK-NOBFI-NOT: bfi
b5061d4
+; CHECK-NOBFI: bl      dummy
b5061d4
+; CHECK-NOBFI: cmp     r0, #0
b5061d4
+; CHECK-NOBFI: it      ne
b5061d4
+; CHECK-NOBFI: orrne   [[REG:r[0-9]+]], [[REG]], #8
b5061d4
+; CHECK-NOBFI: mov     r0, [[REG]]
b5061d4
+
b5061d4
+  %1 = and i8 %a1, -9
b5061d4
+  %2 = select i1 %c, i8 %1, i8 %a1
b5061d4
+  %3 = tail call zeroext i1 @dummy()
b5061d4
+  %4 = or i8 %2, 8
b5061d4
+  %ret = select i1 %3, i8 %4, i8 %2
b5061d4
+  ret i8 %ret
b5061d4
+}
b5061d4
-- 
b5061d4
2.9.3
b5061d4