[llvm] r240788 - [DAGCombiner] Preserve the exact bit when simplifying SRA to SRL.

Benjamin Kramer benny.kra at googlemail.com
Fri Jun 26 07:51:49 PDT 2015


Author: d0k
Date: Fri Jun 26 09:51:49 2015
New Revision: 240788

URL: http://llvm.org/viewvc/llvm-project?rev=240788&view=rev
Log:
[DAGCombiner] Preserve the exact bit when simplifying SRA to SRL.

Allows more aggressive folding of ashr/shl pairs.

Modified:
    llvm/trunk/lib/CodeGen/SelectionDAG/TargetLowering.cpp
    llvm/trunk/test/CodeGen/X86/shift-combine.ll

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/TargetLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/TargetLowering.cpp?rev=240788&r1=240787&r2=240788&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/TargetLowering.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/TargetLowering.cpp Fri Jun 26 09:51:49 2015
@@ -771,10 +771,13 @@ bool TargetLowering::SimplifyDemandedBit
 
       // If the input sign bit is known to be zero, or if none of the top bits
       // are demanded, turn this into an unsigned shift right.
-      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits)
-        return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::SRL, dl, VT,
-                                                 Op.getOperand(0),
-                                                 Op.getOperand(1)));
+      if (KnownZero.intersects(SignBit) || (HighBits & ~NewMask) == HighBits) {
+        SDNodeFlags Flags;
+        Flags.setExact(cast<BinaryWithFlagsSDNode>(Op)->Flags.hasExact());
+        return TLO.CombineTo(Op,
+                             TLO.DAG.getNode(ISD::SRL, dl, VT, Op.getOperand(0),
+                                             Op.getOperand(1), &Flags));
+      }
 
       int Log2 = NewMask.exactLogBase2();
       if (Log2 >= 0) {

Modified: llvm/trunk/test/CodeGen/X86/shift-combine.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/shift-combine.ll?rev=240788&r1=240787&r2=240788&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/shift-combine.ll (original)
+++ llvm/trunk/test/CodeGen/X86/shift-combine.ll Fri Jun 26 09:51:49 2015
@@ -37,6 +37,16 @@ define i32* @test_exact2(i32 %a, i32 %b,
   ret i32* %gep
 }
 
+define i32* @test_exact3(i32 %a, i32 %b, i32* %x)  {
+; CHECK-LABEL: test_exact3:
+; CHECK-NOT: sarl
+
+  %sub = sub i32 %b, %a
+  %shr = ashr exact i32 %sub, 2
+  %gep = getelementptr inbounds i32, i32* %x, i32 %shr
+  ret i32* %gep
+}
+
 define i32* @test_exact4(i32 %a, i32 %b, i32* %x)  {
 ; CHECK-LABEL: test_exact4:
 ; CHECK: shrl %





More information about the llvm-commits mailing list