isl-codegen: Support '<' and '>'

Previously isl always generated '<=' or '>='. However, in many cases '<' or '>'
leads to simpler code. This commit updates isl and adds the relevant code
generation support to Polly.

llvm-svn: 166020
diff --git a/polly/lib/CodeGen/CodeGeneration.cpp b/polly/lib/CodeGen/CodeGeneration.cpp
index f1d1487..804ff9f 100644
--- a/polly/lib/CodeGen/CodeGeneration.cpp
+++ b/polly/lib/CodeGen/CodeGeneration.cpp
@@ -448,7 +448,8 @@
   UpperBound = ExpGen.codegen(f->UB, IntPtrTy);
   Stride = Builder.getInt(APInt_from_MPZ(f->stride));
 
-  IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB);
+  IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
+                  CmpInst::ICMP_SLE);
 
   // Add loop iv to symbols.
   ClastVars[f->iterator] = IV;
diff --git a/polly/lib/CodeGen/IslCodeGeneration.cpp b/polly/lib/CodeGen/IslCodeGeneration.cpp
index 48e2eba..e0856ea 100644
--- a/polly/lib/CodeGen/IslCodeGeneration.cpp
+++ b/polly/lib/CodeGen/IslCodeGeneration.cpp
@@ -380,9 +380,15 @@
   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;
   }
 
   isl_ast_expr_free(Expr);
@@ -464,7 +470,9 @@
     return createOpBoolean(Expr);
   case isl_ast_op_eq:
   case isl_ast_op_le:
+  case isl_ast_op_lt:
   case isl_ast_op_ge:
+  case isl_ast_op_gt:
     return createOpICmp(Expr);
   }
 
@@ -570,7 +578,8 @@
   //    of loop iterations.
   //
   // 3. With the existing code, upper bounds have been easier to implement.
-  __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For);
+  __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
+                                         CmpInst::Predicate &Predicate);
 
   void createFor(__isl_take isl_ast_node *For);
   void createIf(__isl_take isl_ast_node *If);
@@ -579,18 +588,28 @@
 };
 
 __isl_give isl_ast_expr *IslNodeBuilder::getUpperBound(
-  __isl_keep isl_ast_node *For) {
+  __isl_keep isl_ast_node *For, ICmpInst::Predicate &Predicate) {
   isl_id *UBID, *IteratorID;
   isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
+  isl_ast_op_type Type;
 
   Cond = isl_ast_node_for_get_cond(For);
   Iterator = isl_ast_node_for_get_iterator(For);
+  Type = isl_ast_expr_get_op_type(Cond);
 
   assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op
          && "conditional expression is not an atomic upper bound");
 
-  assert(isl_ast_expr_get_op_type(Cond) == isl_ast_op_le
-         && "conditional expression is not an atomic upper bound");
+  switch (Type) {
+    case isl_ast_op_le:
+      Predicate = ICmpInst::ICMP_SLE;
+      break;
+    case isl_ast_op_lt:
+      Predicate = ICmpInst::ICMP_SLT;
+      break;
+    default:
+      llvm_unreachable("Unexpected comparision type in loop conditon");
+  }
 
   Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
 
@@ -626,6 +645,7 @@
   Type *MaxType;
   BasicBlock *AfterBlock;
   Value *IV;
+  CmpInst::Predicate Predicate;
 
   Body = isl_ast_node_for_get_body(For);
 
@@ -639,7 +659,7 @@
   Inc = isl_ast_node_for_get_inc(For);
   Iterator = isl_ast_node_for_get_iterator(For);
   IteratorID = isl_ast_expr_get_id(Iterator);
-  UB = getUpperBound(For);
+  UB = getUpperBound(For, Predicate);
 
   ValueLB = ExprBuilder.create(Init);
   ValueUB = ExprBuilder.create(UB);
@@ -663,7 +683,8 @@
   //       executed at least once, which will enable a lot of loop invariant
   //       code motion.
 
-  IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock);
+  IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock,
+                  Predicate);
   IDToValue[IteratorID] = IV;
 
   create(Body);
diff --git a/polly/lib/CodeGen/LoopGenerators.cpp b/polly/lib/CodeGen/LoopGenerators.cpp
index 2b7e58d..4edd01d 100644
--- a/polly/lib/CodeGen/LoopGenerators.cpp
+++ b/polly/lib/CodeGen/LoopGenerators.cpp
@@ -25,7 +25,8 @@
 
 Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
                          IRBuilder<> &Builder, Pass *P,
-                         BasicBlock *&AfterBlock) {
+                         BasicBlock *&AfterBlock,
+                         ICmpInst::Predicate Predicate) {
   DominatorTree &DT = P->getAnalysis<DominatorTree>();
   Function *F = Builder.GetInsertBlock()->getParent();
   LLVMContext &Context = F->getContext();
@@ -57,7 +58,7 @@
 
   // Exit condition.
   Value *CMP;
-  CMP = Builder.CreateICmpSLE(IV, UB);
+  CMP = Builder.CreateICmp(Predicate, IV, UB);
 
   Builder.CreateCondBr(CMP, BodyBB, AfterBB);
   DT.addNewBlock(BodyBB, HeaderBB);
@@ -286,7 +287,8 @@
 
   Builder.CreateBr(CheckNextBB);
   Builder.SetInsertPoint(--Builder.GetInsertPoint());
-  IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB);
+  IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
+                  ICmpInst::ICMP_SLE);
 
   BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
   Builder.SetInsertPoint(AfterBB->begin());