Skip to content

Commit

Permalink
[FIX] Handle pointer-pointer comparisons
Browse files Browse the repository at this point in the history
  This should fix a problem introduced by r225464.

llvm-svn: 227404
  • Loading branch information
Johannes Doerfert committed Jan 29, 2015
1 parent 72b5e5f commit ef61def
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
28 changes: 26 additions & 2 deletions polly/lib/CodeGen/IslExprBuilder.cpp
Expand Up @@ -175,9 +175,33 @@ Value *IslExprBuilder::createOpBin(__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 *LHSType = LHS->getType();
Type *RHSType = RHS->getType();

MaxType = LHS->getType();
MaxType = getWidestType(MaxType, RHS->getType());
// Handle <pointer> +/- <integer> and <integer> +/- <pointer>
if (LHSType->isPointerTy() || RHSType->isPointerTy()) {
isl_ast_expr_free(Expr);

assert((LHSType->isIntegerTy() || RHSType->isIntegerTy()) &&
"Arithmetic operations might only performed on one but not two "
"pointer types.");

if (LHSType->isIntegerTy())
std::swap(LHS, RHS);

switch (OpType) {
default:
llvm_unreachable(
"Only additive binary operations are allowed on pointer types.");
case isl_ast_op_sub:
RHS = Builder.CreateNeg(RHS);
// Fall through
case isl_ast_op_add:
return Builder.CreateGEP(LHS, RHS);
}
}

MaxType = getWidestType(LHSType, RHSType);

// Take the result into account when calculating the widest type.
//
Expand Down
47 changes: 47 additions & 0 deletions polly/test/Isl/CodeGen/pointer-type-pointer-type-comparison.ll
@@ -0,0 +1,47 @@
; RUN: opt %loadPolly -polly-ast -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -polly-codegen-isl -S < %s | FileCheck %s -check-prefix=CODEGEN
;
; void f(int a[], int N, float *P, float *Q) {
; int i;
; for (i = 0; i < N; ++i)
; if ((P + 1) != Q)
; a[i] = i;
; }
;
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

define void @f(i64* nocapture %a, i64 %N, float * %P, float * %Q) nounwind {
entry:
br label %bb

bb:
%i = phi i64 [ 0, %entry ], [ %i.inc, %bb.backedge ]
%brcond = icmp ne float* %P, %Q
br i1 %brcond, label %store, label %bb.backedge

store:
%scevgep = getelementptr i64* %a, i64 %i
store i64 %i, i64* %scevgep
br label %bb.backedge

bb.backedge:
%i.inc = add nsw i64 %i, 1
%exitcond = icmp eq i64 %i.inc, %N
br i1 %exitcond, label %return, label %bb

return:
ret void
}

; CHECK: if (Q >= P + 1) {
; CHECK: for (int c0 = 0; c0 < N; c0 += 1)
; CHECK: Stmt_store(c0);
; CHECK: } else if (P >= Q + 1)
; CHECK: for (int c0 = 0; c0 < N; c0 += 1)
; CHECK: Stmt_store(c0);
; CHECK: }

; CODEGEN: %[[Pinc:[_a-zA-Z0-9]+]] = getelementptr float* %P, i64 1
; CODEGEN-NEXT: icmp uge float* %Q, %[[Pinc]]
; CODEGEN: %[[Qinc:[_a-zA-Z0-9]+]] = getelementptr float* %Q, i64 1
; CODEGEN-NEXT: icmp uge float* %P, %[[Qinc]]

0 comments on commit ef61def

Please sign in to comment.