From c2ae76737753a55f18a290f1e64f8bca60423449 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Fri, 26 Jun 2015 14:51:49 +0000 Subject: [PATCH] [DAGCombiner] Preserve the exact bit when simplifying SRA to SRL. Allows more aggressive folding of ashr/shl pairs. llvm-svn: 240788 --- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 11 +++++++---- llvm/test/CodeGen/X86/shift-combine.ll | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp index c70c3a27..b40025b 100644 --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -771,10 +771,13 @@ bool TargetLowering::SimplifyDemandedBits(SDValue Op, // 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(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) { diff --git a/llvm/test/CodeGen/X86/shift-combine.ll b/llvm/test/CodeGen/X86/shift-combine.ll index 7fb19a6..4330104 100644 --- a/llvm/test/CodeGen/X86/shift-combine.ll +++ b/llvm/test/CodeGen/X86/shift-combine.ll @@ -37,6 +37,16 @@ define i32* @test_exact2(i32 %a, i32 %b, i32* %x) { 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 % -- 2.7.4