Commit 11bffdc5 authored by Tobias Grosser's avatar Tobias Grosser
Browse files

Update LoopInfo correctly



When the Polly code generation was written we did not correctly update the
LoopInfo data, but still claimed that the loop information is correct. This
does not only lead to missed optimizations, but it can also cause
miscompilations in case passes such as LoopSimplify are run after Polly.

Reported-by: default avatarSergei Larin <slarin@codeaurora.org>

Merged from: https://llvm.org/svn/llvm-project/polly/trunk@181987

llvm-svn: 183374
parent 5dbed5dc
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -924,6 +924,13 @@ void ClastStmtCodeGen::codegen(const clast_guard *g) {
  Builder.CreateBr(MergeBB);
  Builder.SetInsertPoint(ThenBB->begin());

  LoopInfo &LI = P->getAnalysis<LoopInfo>();
  Loop *L = LI.getLoopFor(CondBB);
  if (L) {
    L->addBasicBlockToLoop(ThenBB, LI.getBase());
    L->addBasicBlockToLoop(MergeBB, LI.getBase());
  }

  codegen(g->then);

  Builder.SetInsertPoint(MergeBB->begin());
@@ -1030,8 +1037,6 @@ public:

    AU.addPreserved<CloogInfo>();
    AU.addPreserved<Dependences>();

    // FIXME: We do not create LoopInfo for the newly generated loops.
    AU.addPreserved<LoopInfo>();
    AU.addPreserved<DominatorTree>();
    AU.addPreserved<ScopDetection>();
+7 −1
Original line number Diff line number Diff line
@@ -860,6 +860,13 @@ void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
  DT.addNewBlock(ElseBB, CondBB);
  DT.changeImmediateDominator(MergeBB, CondBB);

  LoopInfo &LI = P->getAnalysis<LoopInfo>();
  Loop *L = LI.getLoopFor(CondBB);
  if (L) {
    L->addBasicBlockToLoop(ThenBB, LI.getBase());
    L->addBasicBlockToLoop(ElseBB, LI.getBase());
  }

  CondBB->getTerminator()->eraseFromParent();

  Builder.SetInsertPoint(CondBB);
@@ -1051,7 +1058,6 @@ public:

    AU.addPreserved<Dependences>();

    // FIXME: We do not create LoopInfo for the newly generated loops.
    AU.addPreserved<LoopInfo>();
    AU.addPreserved<DominatorTree>();
    AU.addPreserved<IslAstInfo>();
+19 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
#include "polly/ScopDetection.h"
#include "polly/CodeGen/LoopGenerators.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -50,6 +51,7 @@ Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
                         ICmpInst::Predicate Predicate) {

  DominatorTree &DT = P->getAnalysis<DominatorTree>();
  LoopInfo &LI = P->getAnalysis<LoopInfo>();
  Function *F = Builder.GetInsertBlock()->getParent();
  LLVMContext &Context = F->getContext();

@@ -63,6 +65,23 @@ Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
  BasicBlock *PreHeaderBB =
      BasicBlock::Create(Context, "polly.loop_preheader", F);

  // Update LoopInfo
  Loop *OuterLoop = LI.getLoopFor(BeforeBB);
  Loop *NewLoop = new Loop();

  if (OuterLoop) {
    OuterLoop->addChildLoop(NewLoop);
  } else {
    LI.addTopLevelLoop(NewLoop);
  }

  if (OuterLoop) {
    OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
    OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
  }

  NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());

  // ExitBB
  ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
  ExitBB->setName("polly.loop_exit");
+6 −1
Original line number Diff line number Diff line
; RUN: opt %loadPolly %defaultOpts -polly-cloog -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -basicaa -polly-cloog -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -basicaa -polly-codegen -loops -analyze < %s | FileCheck %s -check-prefix=LOOPS

;#include <string.h>
;#define N 1024
@@ -214,3 +215,7 @@ declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
; CHECK:     Stmt_9(c2);
; CHECK: }


; LOOPS: Loop at depth 1 containing: %polly.loop_header<header>,%polly.stmt.,%polly.stmt.3<latch><exiting>
; LOOPS: Loop at depth 1 containing: %polly.loop_header5<header>,%polly.stmt.11,%polly.stmt.12<latch><exiting>
; LOOPS: Loop at depth 1 containing: %polly.loop_header15<header>,%polly.stmt.21<latch><exiting>
+8 −1
Original line number Diff line number Diff line
; RUN: opt %loadPolly %defaultOpts -polly-ast -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -basicaa -polly-ast -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -basicaa -polly-codegen-isl -loops -analyze < %s | FileCheck %s -check-prefix=LOOPS


;#include <string.h>
;#define N 1024
@@ -209,3 +211,8 @@ declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
; CHECK:     Stmt_6(c1);
; CHECK:   Stmt_9(c1);
; CHECK: }

; LOOPS: Printing analysis 'Natural Loop Information' for function 'loop_with_condition':
; LOOPS: Loop at depth 1 containing: %1<header><exiting>,%2,%4,%7,%6,%8,%9,%10<latch>
; LOOPS: Loop at depth 1 containing:
; LOOPS: %polly.loop_header<header>,%polly.cond,%polly.merge,%polly.then,%polly.else,%polly.stmt.,%polly.cond3,%polly.merge4,%polly.then5,%polly.else6,%polly.stmt.7,%polly.stmt.8<latch><exiting>
Loading