From f2ebc682d1ee008782d44261f28e92bf982790c2 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 1 Jan 2010 22:29:12 +0000 Subject: [PATCH] teach instcombine to optimize pointer difference idioms involving constant expressions. This is a step towards comment #4 in PR3351. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92401 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/PatternMatch.h | 23 ++++++++++++++----- .../Scalar/InstructionCombining.cpp | 16 +++++-------- test/Transforms/InstCombine/sub.ll | 12 ++++++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index c0b6a6b98c0..23daad92392 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -437,7 +437,7 @@ m_SelectCst(const Cond &C) { // Matchers for CastInst classes // -template +template struct CastClass_match { Op_t Op; @@ -445,17 +445,28 @@ struct CastClass_match { template bool match(OpTy *V) { - if (Class *I = dyn_cast(V)) - return Op.match(I->getOperand(0)); + if (CastInst *I = dyn_cast(V)) + return I->getOpcode() == Opcode && Op.match(I->getOperand(0)); + if (ConstantExpr *CE = dyn_cast(V)) + return CE->getOpcode() == Opcode && Op.match(CE->getOperand(0)); return false; } }; -template -inline CastClass_match m_Cast(const OpTy &Op) { - return CastClass_match(Op); +/// m_PtrToInt +template +inline CastClass_match +m_PtrToInt(const OpTy &Op) { + return CastClass_match(Op); } +/// m_Trunc +template +inline CastClass_match +m_Trunc(const OpTy &Op) { + return CastClass_match(Op); +} + //===----------------------------------------------------------------------===// // Matchers for unary operators diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index ee0b9374cb1..6c5a16c3615 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2950,20 +2950,16 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { // &A[10] - &A[0]: we should compile this to "10". if (TD) { Value *LHSOp, *RHSOp; - if (match(Op0, m_Cast(m_Value(LHSOp))) && - match(Op1, m_Cast(m_Value(RHSOp)))) + if (match(Op0, m_PtrToInt(m_Value(LHSOp))) && + match(Op1, m_PtrToInt(m_Value(RHSOp)))) if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) return ReplaceInstUsesWith(I, Res); // trunc(p)-trunc(q) -> trunc(p-q) - if (TruncInst *LHST = dyn_cast(Op0)) - if (TruncInst *RHST = dyn_cast(Op1)) - if (PtrToIntInst *LHS = dyn_cast(LHST->getOperand(0))) - if (PtrToIntInst *RHS = dyn_cast(RHST->getOperand(0))) - if (Value *Res = OptimizePointerDifference(LHS->getOperand(0), - RHS->getOperand(0), - I.getType())) - return ReplaceInstUsesWith(I, Res); + if (match(Op0, m_Trunc(m_PtrToInt(m_Value(LHSOp)))) && + match(Op1, m_Trunc(m_PtrToInt(m_Value(RHSOp))))) + if (Value *Res = OptimizePointerDifference(LHSOp, RHSOp, I.getType())) + return ReplaceInstUsesWith(I, Res); } return 0; diff --git a/test/Transforms/InstCombine/sub.ll b/test/Transforms/InstCombine/sub.ll index ba28910f4c0..85ee64ec43f 100644 --- a/test/Transforms/InstCombine/sub.ll +++ b/test/Transforms/InstCombine/sub.ll @@ -248,3 +248,15 @@ define i64 @test24a(i8* %P, i64 %A){ ; CHECK-NEXT: ret i64 } +@Arr = external global [42 x i16] + +define i64 @test24b(i8* %P, i64 %A){ + %B = getelementptr inbounds [42 x i16]* @Arr, i64 0, i64 %A + %C = ptrtoint i16* %B to i64 + %G = sub i64 %C, ptrtoint ([42 x i16]* @Arr to i64) + ret i64 %G +; CHECK: @test24b +; CHECK-NEXT: shl i64 %A, 1 +; CHECK-NEXT: ret i64 +} + -- 2.34.1