From e9722fc850108a7b27c96dfd86ffc6f3c326f318 Mon Sep 17 00:00:00 2001 From: Andreas Bolka Date: Tue, 30 Jun 2009 02:12:10 +0000 Subject: [PATCH] Array accesses are independent if the underlying arrays differ. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74499 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/LoopDependenceAnalysis.cpp | 47 +++++++++++++++++++ .../LoopDependenceAnalysis/local-array.ll | 24 ++++++++++ .../LoopDependenceAnalysis/no-array.ll | 23 +++++++++ .../LoopDependenceAnalysis/siv-strong1.ll | 2 +- .../LoopDependenceAnalysis/siv-strong2.ll | 2 +- 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 test/Analysis/LoopDependenceAnalysis/local-array.ll create mode 100644 test/Analysis/LoopDependenceAnalysis/no-array.ll diff --git a/lib/Analysis/LoopDependenceAnalysis.cpp b/lib/Analysis/LoopDependenceAnalysis.cpp index 020a8c77ccc..c1168eccaad 100644 --- a/lib/Analysis/LoopDependenceAnalysis.cpp +++ b/lib/Analysis/LoopDependenceAnalysis.cpp @@ -22,6 +22,7 @@ #include "llvm/Analysis/LoopPass.h" #include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Instructions.h" +#include "llvm/Support/Debug.h" using namespace llvm; LoopPass *llvm::createLoopDependenceAnalysisPass() { @@ -51,6 +52,20 @@ static void GetMemRefInstrs( memrefs.push_back(i); } +static bool IsLoadOrStoreInst(Value *I) { + return isa(I) || isa(I); +} + +static Value *GetPointerOperand(Value *I) { + if (LoadInst *i = dyn_cast(I)) + return i->getPointerOperand(); + if (StoreInst *i = dyn_cast(I)) + return i->getPointerOperand(); + assert(0 && "Value is no load or store instruction!"); + // Never reached. + return 0; +} + //===----------------------------------------------------------------------===// // Dependence Testing //===----------------------------------------------------------------------===// @@ -65,6 +80,38 @@ bool LoopDependenceAnalysis::isDependencePair(const Value *x, bool LoopDependenceAnalysis::depends(Value *src, Value *dst) { assert(isDependencePair(src, dst) && "Values form no dependence pair!"); + DOUT << "== LDA test ==\n" << *src << *dst; + + // We only analyse loads and stores; for possible memory accesses by e.g. + // free, call, or invoke instructions we conservatively assume dependence. + if (!IsLoadOrStoreInst(src) || !IsLoadOrStoreInst(dst)) + return true; + + Value *srcPtr = GetPointerOperand(src); + Value *dstPtr = GetPointerOperand(dst); + const Value *srcObj = srcPtr->getUnderlyingObject(); + const Value *dstObj = dstPtr->getUnderlyingObject(); + const Type *srcTy = srcObj->getType(); + const Type *dstTy = dstObj->getType(); + + // For now, we only work on (pointers to) global or stack-allocated array + // values, as we know that their underlying memory areas will not overlap. + // MAYBE: relax this and test for aliasing? + if (!((isa(srcObj) || isa(srcObj)) && + (isa(dstObj) || isa(dstObj)) && + isa(srcTy) && + isa(dstTy) && + isa(cast(srcTy)->getElementType()) && + isa(cast(dstTy)->getElementType()))) + return true; + + // If the arrays are different, the underlying memory areas do not overlap + // and the memory accesses are therefore independent. + if (srcObj != dstObj) + return false; + + // We couldn't establish a more precise result, so we have to conservatively + // assume full dependence. return true; } diff --git a/test/Analysis/LoopDependenceAnalysis/local-array.ll b/test/Analysis/LoopDependenceAnalysis/local-array.ll new file mode 100644 index 00000000000..c42e0fad438 --- /dev/null +++ b/test/Analysis/LoopDependenceAnalysis/local-array.ll @@ -0,0 +1,24 @@ +; RUN: llvm-as < %s | opt -disable-output -analyze -lda > %t +; RUN: grep {instructions: 2} %t | count 1 +; RUN: grep {0,1: dependent} %t | count 1 + +; x[5] = x[6] // with x being an array on the stack + +define void @foo(...) nounwind { +entry: + %xptr = alloca [256 x i32], align 4 + %x.ld.addr = getelementptr [256 x i32]* %xptr, i64 0, i64 6 + %x.st.addr = getelementptr [256 x i32]* %xptr, i64 0, i64 5 + br label %for.body + +for.body: + %i = phi i64 [ 0, %entry ], [ %i.next, %for.body ] + %x = load i32* %x.ld.addr + store i32 %x, i32* %x.st.addr + %i.next = add i64 %i, 1 + %exitcond = icmp eq i64 %i.next, 256 + br i1 %exitcond, label %for.end, label %for.body + +for.end: + ret void +} diff --git a/test/Analysis/LoopDependenceAnalysis/no-array.ll b/test/Analysis/LoopDependenceAnalysis/no-array.ll new file mode 100644 index 00000000000..03b9f4d8b8d --- /dev/null +++ b/test/Analysis/LoopDependenceAnalysis/no-array.ll @@ -0,0 +1,23 @@ +; RUN: llvm-as < %s | opt -disable-output -analyze -lda > %t +; RUN: grep {instructions: 2} %t | count 1 +; RUN: grep {0,1: dependent} %t | count 1 + +; x[5] = x[6] // with x being a pointer passed as argument + +define void @foo(i32* nocapture %xptr) nounwind { +entry: + %x.ld.addr = getelementptr i32* %xptr, i64 6 + %x.st.addr = getelementptr i32* %xptr, i64 5 + br label %for.body + +for.body: + %i = phi i64 [ 0, %entry ], [ %i.next, %for.body ] + %x = load i32* %x.ld.addr + store i32 %x, i32* %x.st.addr + %i.next = add i64 %i, 1 + %exitcond = icmp eq i64 %i.next, 256 + br i1 %exitcond, label %for.end, label %for.body + +for.end: + ret void +} diff --git a/test/Analysis/LoopDependenceAnalysis/siv-strong1.ll b/test/Analysis/LoopDependenceAnalysis/siv-strong1.ll index 01f11137d78..c416ff377fb 100644 --- a/test/Analysis/LoopDependenceAnalysis/siv-strong1.ll +++ b/test/Analysis/LoopDependenceAnalysis/siv-strong1.ll @@ -1,7 +1,7 @@ ; RUN: llvm-as < %s | opt -disable-output -analyze -lda > %t ; RUN: grep {instructions: 3} %t | count 1 ; RUN: grep {0,2: dependent} %t | count 1 -; RUN: grep {1,2: dependent} %t | count 1 +; RUN: grep {1,2: independent} %t | count 1 ; for (i = 0; i < 256; i++) ; x[i] = x[i] + y[i] diff --git a/test/Analysis/LoopDependenceAnalysis/siv-strong2.ll b/test/Analysis/LoopDependenceAnalysis/siv-strong2.ll index e64b92c3e76..7295863e378 100644 --- a/test/Analysis/LoopDependenceAnalysis/siv-strong2.ll +++ b/test/Analysis/LoopDependenceAnalysis/siv-strong2.ll @@ -1,7 +1,7 @@ ; RUN: llvm-as < %s | opt -disable-output -analyze -lda > %t ; RUN: grep {instructions: 3} %t | count 1 ; RUN: grep {0,2: dependent} %t | count 1 -; RUN: grep {1,2: dependent} %t | count 1 +; RUN: grep {1,2: independent} %t | count 1 ; for (i = 0; i < 256; i++) ; x[i+1] = x[i] + y[i]