[PATCH] Allow the IslExprBuilder to compare pointers

Andreas Simbuerger simbuerg at googlemail.com
Wed Aug 13 04:26:31 PDT 2014


From: Johannes Doerfert <doerfert at cs.uni-saarland.de>

What do you think about this one here? A 'bit' overkill to use a map&vector
here, but it should be safe against reordering in the enum and unsupported
enum elements without using magic numbers.

Cheers,
Andreas

---
 lib/CodeGen/IslExprBuilder.cpp | 48 +++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/lib/CodeGen/IslExprBuilder.cpp b/lib/CodeGen/IslExprBuilder.cpp
index e78b4c0..4234f4e 100644
--- a/lib/CodeGen/IslExprBuilder.cpp
+++ b/lib/CodeGen/IslExprBuilder.cpp
@@ -255,6 +255,15 @@ Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
   return Builder.CreateSelect(Cond, LHS, RHS);
 }
 
+
+static std::map<isl_ast_op_type, std::vector<CmpInst::Predicate> > Predicates {
+  { isl_ast_op_eq, { CmpInst::ICMP_EQ , CmpInst::ICMP_EQ  } },
+  { isl_ast_op_le, { CmpInst::ICMP_SLE, CmpInst::ICMP_ULE } },
+  { isl_ast_op_lt, { CmpInst::ICMP_SLT, CmpInst::ICMP_ULT } },
+  { isl_ast_op_ge, { CmpInst::ICMP_SGE, CmpInst::ICMP_UGE } },
+  { isl_ast_op_gt, { CmpInst::ICMP_SGT, CmpInst::ICMP_UGT } }
+};
+
 Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
          "Expected an isl_ast_expr_op expression");
@@ -264,35 +273,26 @@ Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
   LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
   RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
 
-  Type *MaxType = LHS->getType();
-  MaxType = getWidestType(MaxType, RHS->getType());
+  bool IsPtrType = LHS->getType()->isPointerTy();
+  assert((!IsPtrType || RHS->getType()->isPointerTy()) &&
+         "Both ICmp operators should be pointer types or none of them");
 
-  if (MaxType != RHS->getType())
-    RHS = Builder.CreateSExt(RHS, MaxType);
+  if (!IsPtrType) {
+    Type *MaxType = LHS->getType();
+    MaxType = getWidestType(MaxType, RHS->getType());
 
-  if (MaxType != LHS->getType())
-    LHS = Builder.CreateSExt(LHS, MaxType);
+    if (MaxType != RHS->getType())
+      RHS = Builder.CreateSExt(RHS, MaxType);
 
-  switch (isl_ast_expr_get_op_type(Expr)) {
-  default:
-    llvm_unreachable("Unsupported ICmp isl ast expression");
-  case isl_ast_op_eq:
-    Res = Builder.CreateICmpEQ(LHS, RHS);
-    break;
-  case isl_ast_op_le:
-    Res = Builder.CreateICmpSLE(LHS, RHS);
-    break;
-  case isl_ast_op_lt:
-    Res = Builder.CreateICmpSLT(LHS, RHS);
-    break;
-  case isl_ast_op_ge:
-    Res = Builder.CreateICmpSGE(LHS, RHS);
-    break;
-  case isl_ast_op_gt:
-    Res = Builder.CreateICmpSGT(LHS, RHS);
-    break;
+    if (MaxType != LHS->getType())
+      LHS = Builder.CreateSExt(LHS, MaxType);
   }
 
+  isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr);
+  assert(Predicates.count(OpType) && "Unsupported ICmp isl ast expression");
+
+  Res = Builder.CreateICmp(Predicates[OpType][IsPtrType], LHS, RHS);
+
   isl_ast_expr_free(Expr);
   return Res;
 }
-- 
2.0.4




More information about the llvm-commits mailing list