Add convenience functions for creating exact sdiv operators, and
authorDan Gohman <gohman@apple.com>
Tue, 11 Aug 2009 17:05:24 +0000 (17:05 +0000)
committerDan Gohman <gohman@apple.com>
Tue, 11 Aug 2009 17:05:24 +0000 (17:05 +0000)
use them in CreatePtrDiff.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78682 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
include/llvm/InstrTypes.h
include/llvm/Support/ConstantFolder.h
include/llvm/Support/IRBuilder.h
include/llvm/Support/NoFolder.h
include/llvm/Support/TargetFolder.h
lib/VMCore/Constants.cpp

index df13291922c386439d842d7ed8ac78567cf66af0..1aaef8045a621b7f68ec5adfac700b7ef8eb6f8f 100644 (file)
@@ -636,6 +636,8 @@ public:
   static Constant *getIntToPtr(Constant *C, const Type *Ty);
   static Constant *getBitCast (Constant *C, const Type *Ty);
 
+  static Constant* getExactSDiv(Constant* C1, Constant* C2);
+
   /// Transparently provide more efficient getOperand methods.
   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
 
index 550849bc8a9b30a457f8f4576b7b8c053acc4342..771e9f799b3e605e10c034afdb2a49375cf0fcbe 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "llvm/Instruction.h"
 #include "llvm/OperandTraits.h"
+#include "llvm/Operator.h"
 #include "llvm/DerivedTypes.h"
 
 namespace llvm {
@@ -195,6 +196,27 @@ public:
 #include "llvm/Instruction.def"
 
 
+  /// CreateExactSDiv - Create an SDiv operator with the exact flag set.
+  ///
+  static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
+                                         const Twine &Name = "") {
+    BinaryOperator *BO = CreateSDiv(V1, V2, Name);
+    cast<SDivOperator>(BO)->setIsExact(true);
+    return BO;
+  }
+  static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
+                                         const Twine &Name, BasicBlock *BB) {
+    BinaryOperator *BO = CreateSDiv(V1, V2, Name, BB);
+    cast<SDivOperator>(BO)->setIsExact(true);
+    return BO;
+  }
+  static BinaryOperator *CreateExactSDiv(Value *V1, Value *V2,
+                                         const Twine &Name, Instruction *I) {
+    BinaryOperator *BO = CreateSDiv(V1, V2, Name, I);
+    cast<SDivOperator>(BO)->setIsExact(true);
+    return BO;
+  }
+
   /// Helper functions to construct and inspect unary operations (NEG and NOT)
   /// via binary operators SUB and XOR:
   ///
index cb9b05558477d1ec974d4c2f7c67892497ca9fae..422d1ad00d29f18f6323f297e47f30aac88987cf 100644 (file)
@@ -56,6 +56,9 @@ public:
   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
     return ConstantExpr::getSDiv(LHS, RHS);
   }
+  Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
+    return ConstantExpr::getExactSDiv(LHS, RHS);
+  }
   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
     return ConstantExpr::getFDiv(LHS, RHS);
   }
index b2619a56b6e9a7413d01ef6a9b456e5e49c7abcb..a8663d2a399bb45ea6367642276324a7c1ee12b0 100644 (file)
@@ -241,6 +241,12 @@ public:
         return Folder.CreateSDiv(LC, RC);
     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
   }
+  Value *CreateExactSDiv(Value *LHS, Value *RHS, const char *Name = "") {
+    if (Constant *LC = dyn_cast<Constant>(LHS))
+      if (Constant *RC = dyn_cast<Constant>(RHS))
+        return Folder.CreateExactSDiv(LC, RC);
+    return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
+  }
   Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
     if (Constant *LC = dyn_cast<Constant>(LHS))
       if (Constant *RC = dyn_cast<Constant>(RHS))
@@ -719,7 +725,9 @@ public:
 
   /// CreatePtrDiff - Return the i64 difference between two pointer values,
   /// dividing out the size of the pointed-to objects.  This is intended to
-  /// implement C-style pointer subtraction.
+  /// implement C-style pointer subtraction. As such, the pointers must be
+  /// appropriately aligned for their element types and pointing into the
+  /// same object.
   Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
     assert(LHS->getType() == RHS->getType() &&
            "Pointer subtraction operand types must match!");
@@ -727,9 +735,9 @@ public:
     Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
     Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
     Value *Difference = CreateSub(LHS_int, RHS_int);
-    return CreateSDiv(Difference,
-                      ConstantExpr::getSizeOf(ArgType->getElementType()),
-                      Name);
+    return CreateExactSDiv(Difference,
+                           ConstantExpr::getSizeOf(ArgType->getElementType()),
+                           Name);
   }
 };
 
index e0fb9cdb3f594e55f4cd2b1fdf170a01faf1eb38..b89c084b5862fa1d5ae05f4e4a1d322173c8e02e 100644 (file)
@@ -63,6 +63,9 @@ public:
   Value *CreateSDiv(Constant *LHS, Constant *RHS) const {
     return BinaryOperator::CreateSDiv(LHS, RHS);
   }
+  Value *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
+    return BinaryOperator::CreateExactSDiv(LHS, RHS);
+  }
   Value *CreateFDiv(Constant *LHS, Constant *RHS) const {
     return BinaryOperator::CreateFDiv(LHS, RHS);
   }
index cdcc4a8ad57ec5e195fb4ad9cd7f781d5d2cb88f..900c32688591bace1c9a641d1a48eb0ab1b4b5ee 100644 (file)
@@ -72,6 +72,9 @@ public:
   Constant *CreateSDiv(Constant *LHS, Constant *RHS) const {
     return Fold(ConstantExpr::getSDiv(LHS, RHS));
   }
+  Constant *CreateExactSDiv(Constant *LHS, Constant *RHS) const {
+    return Fold(ConstantExpr::getExactSDiv(LHS, RHS));
+  }
   Constant *CreateFDiv(Constant *LHS, Constant *RHS) const {
     return Fold(ConstantExpr::getFDiv(LHS, RHS));
   }
index 43cb39016f9de2980d058aa96d90762f81b3cbab..2670965f7341fb982d2dfa146d9b706ef6e72134 100644 (file)
@@ -605,6 +605,12 @@ Constant* ConstantVector::get(Constant* const* Vals, unsigned NumVals) {
   return get(std::vector<Constant*>(Vals, Vals+NumVals));
 }
 
+Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
+  Constant *C = getSDiv(C1, C2);
+  cast<SDivOperator>(C)->setIsExact(true);
+  return C;
+}
+
 // Utility function for determining if a ConstantExpr is a CastOp or not. This
 // can't be inline because we don't want to #include Instruction.h into
 // Constant.h