teach instcombine to optimize pointer difference idioms involving constant
authorChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 22:29:12 +0000 (22:29 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 1 Jan 2010 22:29:12 +0000 (22:29 +0000)
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
lib/Transforms/Scalar/InstructionCombining.cpp
test/Transforms/InstCombine/sub.ll

index c0b6a6b98c09d32236d1b6266cea22949526d246..23daad9239298ce2950dd22229519e74c68b0b4e 100644 (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
index ee0b9374cb14e70d76667793d845f61cb47906cb..6c5a16c361559c3438a3dda85eaecb36a5dfc614 100644 (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;
index ba28910f4c039527ef002f54ead0602ea226c2d5..85ee64ec43ff0abd5e93fb5a40060f014155aca3 100644 (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 
+}
+