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
This commit is contained in:
Chris Lattner 2010-01-01 22:29:12 +00:00
parent 3376718505
commit f2ebc682d1
3 changed files with 35 additions and 16 deletions

View File

@ -437,7 +437,7 @@ m_SelectCst(const Cond &C) {
// Matchers for CastInst classes
//
template<typename Op_t, typename Class>
template<typename Op_t, unsigned Opcode>
struct CastClass_match {
Op_t Op;
@ -445,17 +445,28 @@ struct CastClass_match {
template<typename OpTy>
bool match(OpTy *V) {
if (Class *I = dyn_cast<Class>(V))
return Op.match(I->getOperand(0));
if (CastInst *I = dyn_cast<CastInst>(V))
return I->getOpcode() == Opcode && Op.match(I->getOperand(0));
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
return CE->getOpcode() == Opcode && Op.match(CE->getOperand(0));
return false;
}
};
template<typename Class, typename OpTy>
inline CastClass_match<OpTy, Class> m_Cast(const OpTy &Op) {
return CastClass_match<OpTy, Class>(Op);
/// m_PtrToInt
template<typename OpTy>
inline CastClass_match<OpTy, Instruction::PtrToInt>
m_PtrToInt(const OpTy &Op) {
return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
}
/// m_Trunc
template<typename OpTy>
inline CastClass_match<OpTy, Instruction::Trunc>
m_Trunc(const OpTy &Op) {
return CastClass_match<OpTy, Instruction::Trunc>(Op);
}
//===----------------------------------------------------------------------===//
// Matchers for unary operators

View File

@ -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<PtrToIntInst>(m_Value(LHSOp))) &&
match(Op1, m_Cast<PtrToIntInst>(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<TruncInst>(Op0))
if (TruncInst *RHST = dyn_cast<TruncInst>(Op1))
if (PtrToIntInst *LHS = dyn_cast<PtrToIntInst>(LHST->getOperand(0)))
if (PtrToIntInst *RHS = dyn_cast<PtrToIntInst>(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;

View File

@ -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
}