From a50f957a6162bd56ecd83b08397e3f2175af1388 Mon Sep 17 00:00:00 2001 From: Amaury Sechet Date: Wed, 1 Mar 2017 23:44:17 +0000 Subject: [PATCH] [DAGCombiner] mulhi + 1 never overflow. Summary: This can be used to optimize large multiplications after legalization. Depends on D29565 Reviewers: mkuper, spatel, RKSimon, zvi, bkramer, aaboud, craig.topper Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D29587 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296711 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 13 +++++++++++++ test/CodeGen/X86/overflow.ll | 6 ++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 16425a08200..5d7bf27ae64 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -2775,6 +2775,19 @@ SelectionDAG::OverflowKind SelectionDAG::computeOverflowKind(SDValue N0, return OFK_Never; } + // mulhi + 1 never overflow + if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 && + (~N1Zero & 0x01) == ~N1Zero) + return OFK_Never; + + if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1) { + APInt N0Zero, N0One; + computeKnownBits(N0, N0Zero, N0One); + + if ((~N0Zero & 0x01) == ~N0Zero) + return OFK_Never; + } + return OFK_Sometime; } diff --git a/test/CodeGen/X86/overflow.ll b/test/CodeGen/X86/overflow.ll index 37ca5e1c0eb..ff25b5de493 100644 --- a/test/CodeGen/X86/overflow.ll +++ b/test/CodeGen/X86/overflow.ll @@ -50,10 +50,8 @@ define i128 @mulhioverflow(i64 %a, i64 %b, i64 %c) nounwind { ; X64-NEXT: movq %rdi, %rax ; X64-NEXT: mulq %rsi ; X64-NEXT: andl $1, %ecx -; X64-NEXT: addq %rdx, %rcx -; X64-NEXT: sbbq %rdx, %rdx -; X64-NEXT: andl $1, %edx -; X64-NEXT: movq %rcx, %rax +; X64-NEXT: leaq (%rcx,%rdx), %rax +; X64-NEXT: xorl %edx, %edx ; X64-NEXT: retq %1 = zext i64 %a to i128 %2 = zext i64 %b to i128