]> granicus.if.org Git - llvm/commitdiff
[DAG] Teach computeKnownBits and ComputeNumSignBits in SelectionDAG to look through...
authorBjorn Pettersson <bjorn.a.pettersson@ericsson.com>
Wed, 5 Oct 2016 17:40:27 +0000 (17:40 +0000)
committerBjorn Pettersson <bjorn.a.pettersson@ericsson.com>
Wed, 5 Oct 2016 17:40:27 +0000 (17:40 +0000)
Summary: Both computeKnownBits and ComputeNumSignBits can now do a simple
look-through of EXTRACT_VECTOR_ELT. It will compute the result based
on the known bits (or known sign bits) for the vector that the element
is extracted from.

Reviewers: bogner, tstellarAMD, mkuper

Subscribers: wdng, RKSimon, jyknight, llvm-commits, nhaehnle

Differential Revision: https://reviews.llvm.org/D25007

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283347 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/SelectionDAG.cpp
test/CodeGen/AMDGPU/amdgpu.private-memory.ll
test/CodeGen/SPARC/vector-extract-elt.ll [new file with mode: 0644]
test/CodeGen/X86/pr21792.ll

index e9680ff521de7ef5d952a29d4ba1f017db3c9a6a..6ed8999766b53035396ce96561ced9deca90b156 100644 (file)
@@ -2448,6 +2448,26 @@ void SelectionDAG::computeKnownBits(SDValue Op, APInt &KnownZero,
     KnownOne = KnownOne.trunc(BitWidth);
     break;
   }
+  case ISD::EXTRACT_VECTOR_ELT: {
+    // At the moment we keep this simple and skip tracking the specific
+    // element. This way we get the lowest common denominator for all elements
+    // of the vector.
+    // TODO: get information for given vector element
+    const unsigned BitWidth = Op.getValueSizeInBits();
+    const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
+    // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
+    // anything about the extended bits.
+    if (BitWidth > EltBitWidth) {
+      KnownZero = KnownZero.trunc(EltBitWidth);
+      KnownOne = KnownOne.trunc(EltBitWidth);
+    }
+    computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
+    if (BitWidth > EltBitWidth) {
+      KnownZero = KnownZero.zext(BitWidth);
+      KnownOne = KnownOne.zext(BitWidth);
+    }
+    break;
+  }
   case ISD::BSWAP: {
     computeKnownBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
     KnownZero = KnownZero2.byteSwap();
@@ -2715,6 +2735,20 @@ unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, unsigned Depth) const {
     // result. Otherwise it gives either negative or > bitwidth result
     return std::max(std::min(KnownSign - rIndex * BitWidth, BitWidth), 0);
   }
+  case ISD::EXTRACT_VECTOR_ELT: {
+    // At the moment we keep this simple and skip tracking the specific
+    // element. This way we get the lowest common denominator for all elements
+    // of the vector.
+    // TODO: get information for given vector element
+    const unsigned BitWidth = Op.getValueSizeInBits();
+    const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
+    // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
+    // anything about sign bits. But if the sizes match we can derive knowledge
+    // about sign bits from the vector operand.
+    if (BitWidth == EltBitWidth)
+      return ComputeNumSignBits(Op.getOperand(0), Depth+1);
+    break;
+  }
   }
 
   // If we are looking at the loaded value of the SDNode.
index 655212105a72f50457befecffc2266e559d336a9..aedec231c655be7fb3a739bb18d573e238bced31 100644 (file)
@@ -229,7 +229,8 @@ for.end:
 
 ; SI-PROMOTE-DAG: buffer_store_short v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ; encoding: [0x00,0x10,0x68,0xe0
 ; SI-PROMOTE-DAG: buffer_store_short v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:2 ; encoding: [0x02,0x10,0x68,0xe0
-; SI-PROMOTE: buffer_load_sshort v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}}
+; Loaded value is 0 or 1, so sext will become zext, so we get buffer_load_ushort instead of buffer_load_sshort.
+; SI-PROMOTE: buffer_load_ushort v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}}
 define void @short_array(i32 addrspace(1)* %out, i32 %index) #0 {
 entry:
   %0 = alloca [2 x i16]
diff --git a/test/CodeGen/SPARC/vector-extract-elt.ll b/test/CodeGen/SPARC/vector-extract-elt.ll
new file mode 100644 (file)
index 0000000..702f063
--- /dev/null
@@ -0,0 +1,19 @@
+; RUN: llc -march=sparc < %s | FileCheck %s
+
+
+; If computeKnownSignBits (in SelectionDAG) can do a simple
+; look-thru for extractelement then we we know that the add will yield a
+; non-negative result.
+define i1 @test1(<4 x i16>* %in) {
+; CHECK-LABEL: ! BB#0:
+; CHECK-NEXT:        retl
+; CHECK-NEXT:        sethi 0, %o0
+  %vec2 = load <4 x i16>, <4 x i16>* %in, align 1
+  %vec3 = lshr <4 x i16> %vec2, <i16 2, i16 2, i16 2, i16 2>
+  %vec4 = sext <4 x i16> %vec3 to <4 x i32>
+  %elt0 = extractelement <4 x i32> %vec4, i32 0
+  %elt1 = extractelement <4 x i32> %vec4, i32 1
+  %sum = add i32 %elt0, %elt1
+  %bool = icmp slt i32 %sum, 0
+  ret i1 %bool
+}
index f6dca609bc05a8d63ed5ea45dfed4b01e4dfe2d4..c222b196b1d6925b598d5cf51955cb71b6e9604d 100644 (file)
@@ -34,8 +34,8 @@ entry:
 ; CHECK-LABEL: func:
 ; CHECK: pextrq  $1, %xmm0,
 ; CHECK-NEXT: movd    %xmm0, %r[[AX:..]]
-; CHECK-NEXT: movslq  %e[[AX]],
-; CHECK-NEXT: sarq    $32, %r[[AX]]
+; CHECK-NEXT: movq    %r[[AX]],
+; CHECK-NEXT: shrq    $32, %r9
 }
 
 declare void @toto(double*, double*, double*, double*, double*, double*)