[llvm-commits] [llvm-gcc-4.2] r78738 - /llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp

Dan Gohman gohman at apple.com
Tue Aug 11 15:39:58 PDT 2009


Author: djg
Date: Tue Aug 11 17:39:57 2009
New Revision: 78738

URL: http://llvm.org/viewvc/llvm-project?rev=78738&view=rev
Log:
Use the new "exact" flag for sdiv to model EXACT_DIV_EXPR. This
obviates the need for the hack that recognizes division by
constant powers of 2.

Modified:
    llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp

Modified: llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp?rev=78738&r1=78737&r2=78738&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-convert.cpp Tue Aug 11 17:39:57 2009
@@ -3604,6 +3604,7 @@
   bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
   bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1)));
   bool TyIsSigned  = !TYPE_UNSIGNED(TREE_TYPE(exp));
+  bool IsExactDiv  = TREE_CODE(exp) == EXACT_DIV_EXPR;
 
   LHS = CastToAnyType(LHS, LHSIsSigned, Ty, TyIsSigned);
   RHS = CastToAnyType(RHS, RHSIsSigned, Ty, TyIsSigned);
@@ -3622,7 +3623,11 @@
     RHS = BitCastToType(RHS, Ty);
   }
 
-  Value *V = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
+  Value *V;
+  if (Opc == Instruction::SDiv && IsExactDiv)
+    V = Builder.CreateExactSDiv(LHS, RHS);
+  else
+    V = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
   if (ResTy != Ty)
     V = BitCastToType(V, ResTy);
   return V;
@@ -3767,22 +3772,7 @@
   // Unsigned EXACT_DIV_EXPR -> normal udiv.
   if (TYPE_UNSIGNED(TREE_TYPE(exp)))
     return EmitBinOp(exp, DestLoc, Instruction::UDiv);
-  
-  // If this is a signed EXACT_DIV_EXPR by a constant, and we know that
-  // the RHS is a multiple of two, we strength reduce the result to use
-  // a signed SHR here.  We have no way in LLVM to represent EXACT_DIV_EXPR
-  // precisely, so this transform can't currently be performed at the LLVM
-  // level.  This is commonly used for pointer subtraction. 
-  if (TREE_CODE(TREE_OPERAND(exp, 1)) == INTEGER_CST) {
-    uint64_t IntValue = getINTEGER_CSTVal(TREE_OPERAND(exp, 1));
-    if (isPowerOf2_64(IntValue)) {
-      // Create an ashr instruction, by the log of the division amount.
-      Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
-      return Builder.CreateAShr(LHS, ConstantInt::get(LHS->getType(),
-                                                      Log2_64(IntValue)));
-    }
-  }
-  
+
   // Otherwise, emit this as a normal signed divide.
   return EmitBinOp(exp, DestLoc, Instruction::SDiv);
 }





More information about the llvm-commits mailing list