[llvm-commits] [polly] r150339 - /polly/trunk/lib/CodeGeneration.cpp

Tobias Grosser grosser at fim.uni-passau.de
Sun Feb 12 04:09:53 PST 2012


Author: grosser
Date: Sun Feb 12 06:09:53 2012
New Revision: 150339

URL: http://llvm.org/viewvc/llvm-project?rev=150339&view=rev
Log:
CodeGen: Maintain a valid CFG during code generation

Before this change we built the CFG such that it was only valid after code was
fully generated. During code generation itself, it was often incomplete. After
this change always maintain a valid CFG. This will later allow us to use the
SCEVExpander during code generation. This is the first step to get rid of the
independent blocks pass.

Modified:
    polly/trunk/lib/CodeGeneration.cpp

Modified: polly/trunk/lib/CodeGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/CodeGeneration.cpp?rev=150339&r1=150338&r2=150339&view=diff
==============================================================================
--- polly/trunk/lib/CodeGeneration.cpp (original)
+++ polly/trunk/lib/CodeGeneration.cpp Sun Feb 12 06:09:53 2012
@@ -98,18 +98,18 @@
 // @param UB      The upper bound of the loop iv.
 // @param Stride  The number by which the loop iv is incremented after every
 //                iteration.
-static void createLoop(IRBuilder<> *Builder, Value *LB, Value *UB, APInt Stride,
-                PHINode*& IV, BasicBlock*& AfterBB, Value*& IncrementedIV,
-                DominatorTree *DT) {
+static Value *createLoop(IRBuilder<> *Builder, Value *LB, Value *UB,
+                         APInt Stride, DominatorTree *DT, Pass *P) {
   Function *F = Builder->GetInsertBlock()->getParent();
   LLVMContext &Context = F->getContext();
 
   BasicBlock *PreheaderBB = Builder->GetInsertBlock();
   BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
   BasicBlock *BodyBB = BasicBlock::Create(Context, "polly.loop_body", F);
-  AfterBB = BasicBlock::Create(Context, "polly.after_loop", F);
+  BasicBlock *AfterBB = SplitBlock(PreheaderBB, Builder->GetInsertPoint()++, P);
+  AfterBB->setName("polly.loop_after");
 
-  Builder->CreateBr(HeaderBB);
+  PreheaderBB->getTerminator()->setSuccessor(0, HeaderBB);
   DT->addNewBlock(HeaderBB, PreheaderBB);
 
   Builder->SetInsertPoint(HeaderBB);
@@ -122,27 +122,35 @@
   assert(LoopIVType && "UB is not integer?");
 
   // IV
-  IV = Builder->CreatePHI(LoopIVType, 2, "polly.loopiv");
+  PHINode *IV = Builder->CreatePHI(LoopIVType, 2, "polly.loopiv");
   IV->addIncoming(LB, PreheaderBB);
 
   // IV increment.
   Value *StrideValue = ConstantInt::get(LoopIVType,
                                         Stride.zext(LoopIVType->getBitWidth()));
-  IncrementedIV = Builder->CreateAdd(IV, StrideValue, "polly.next_loopiv");
+  Value *IncrementedIV = Builder->CreateAdd(IV, StrideValue,
+                                            "polly.next_loopiv");
 
   // Exit condition.
+  Value *CMP;
   if (AtLeastOnce) { // At least on iteration.
     UB = Builder->CreateAdd(UB, Builder->getInt64(1));
-    Value *CMP = Builder->CreateICmpEQ(IV, UB);
-    Builder->CreateCondBr(CMP, AfterBB, BodyBB);
+    CMP = Builder->CreateICmpNE(IV, UB);
   } else { // Maybe not executed at all.
-    Value *CMP = Builder->CreateICmpSLE(IV, UB);
-    Builder->CreateCondBr(CMP, BodyBB, AfterBB);
+    CMP = Builder->CreateICmpSLE(IV, UB);
   }
+
+  Builder->CreateCondBr(CMP, BodyBB, AfterBB);
   DT->addNewBlock(BodyBB, HeaderBB);
-  DT->addNewBlock(AfterBB, HeaderBB);
 
   Builder->SetInsertPoint(BodyBB);
+  Builder->CreateBr(HeaderBB);
+  IV->addIncoming(IncrementedIV, BodyBB);
+  DT->changeImmediateDominator(AfterBB, HeaderBB);
+
+  Builder->SetInsertPoint(BodyBB->begin());
+
+  return IV;
 }
 
 class BlockGenerator {
@@ -264,7 +272,7 @@
   //                is used to update the operands of the statements.
   //                For new statements a relation old->new is inserted in this
   //                map.
-  void copyBB(BasicBlock *BB, DominatorTree *DT);
+  void copyBB(BasicBlock *BB, Pass *P);
 };
 
 BlockGenerator::BlockGenerator(IRBuilder<> &B, ValueMapT &vmap,
@@ -694,15 +702,11 @@
   copyInstScalar(Inst, BBMap);
 }
 
-void BlockGenerator::copyBB(BasicBlock *BB, DominatorTree *DT) {
-  Function *F = Builder.GetInsertBlock()->getParent();
-  LLVMContext &Context = F->getContext();
-  BasicBlock *CopyBB = BasicBlock::Create(Context,
-                                          "polly." + BB->getName() + ".stmt",
-                                          F);
-  Builder.CreateBr(CopyBB);
-  DT->addNewBlock(CopyBB, Builder.GetInsertBlock());
-  Builder.SetInsertPoint(CopyBB);
+void BlockGenerator::copyBB(BasicBlock *BB, Pass *P) {
+  BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(),
+                                  Builder.GetInsertPoint(), P);
+  CopyBB->setName("polly." + BB->getName() + ".stmt");
+  Builder.SetInsertPoint(CopyBB->begin());
 
   // Create two maps that store the mapping from the original instructions of
   // the old basic block to their copies in the new basic block. Those maps
@@ -892,6 +896,7 @@
   ScopDetection *SD;
   Dependences *DP;
   TargetData *TD;
+  Pass *P;
 
   // The Builder specifies the current location to code generate at.
   IRBuilder<> &Builder;
@@ -1001,7 +1006,7 @@
 
   ClastStmtCodeGen(Scop *scop, ScalarEvolution &se, DominatorTree *dt,
                    ScopDetection *sd, Dependences *dp, TargetData *td,
-                   IRBuilder<> &B);
+                   IRBuilder<> &B, Pass *P);
 };
 }
 
@@ -1072,7 +1077,7 @@
 
   BlockGenerator Generator(Builder, ValueMap, VectorValueMap, *Statement,
                            scatteringDomain);
-  Generator.copyBB(BB, DT);
+  Generator.copyBB(BB, P);
 }
 
 void ClastStmtCodeGen::codegen(const clast_block *b) {
@@ -1084,9 +1089,6 @@
                                             Value *LowerBound,
                                             Value *UpperBound) {
   APInt Stride;
-  PHINode *IV;
-  Value *IncrementedIV;
-  BasicBlock *AfterBB, *HeaderBB, *LastBodyBB;
   Type *IntPtrTy;
 
   Stride = APInt_from_MPZ(f->stride);
@@ -1102,8 +1104,7 @@
     UpperBound = ExpGen.codegen(f->UB, IntPtrTy);
   }
 
-  createLoop(&Builder, LowerBound, UpperBound, Stride, IV, AfterBB,
-             IncrementedIV, DT);
+  Value *IV = createLoop(&Builder, LowerBound, UpperBound, Stride, DT, P);
 
   // Add loop iv to symbols.
   (*clastVars)[f->iterator] = IV;
@@ -1113,12 +1114,6 @@
 
   // Loop is finished, so remove its iv from the live symbols.
   clastVars->erase(f->iterator);
-
-  HeaderBB = *pred_begin(AfterBB);
-  LastBodyBB = Builder.GetInsertBlock();
-  Builder.CreateBr(HeaderBB);
-  IV->addIncoming(IncrementedIV, LastBodyBB);
-  Builder.SetInsertPoint(AfterBB);
 }
 
 Function *ClastStmtCodeGen::addOpenMPSubfunction(Module *M) {
@@ -1205,6 +1200,7 @@
   IntegerType *intPtrTy = TD->getIntPtrType(Context);
 
   // Store the previous basic block.
+  BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
   BasicBlock *PrevBB = Builder.GetInsertBlock();
 
   // Create basic blocks.
@@ -1258,22 +1254,22 @@
   clastVars = &clastVarsOMP;
   ExpGen.setIVS(&clastVarsOMP);
 
+  Builder.CreateBr(checkNextBB);
+  Builder.SetInsertPoint(--Builder.GetInsertPoint());
   codegenForSequential(f, lowerBound, upperBound);
 
   // Restore the old clastVars.
   clastVars = oldClastVars;
   ExpGen.setIVS(oldClastVars);
 
-  Builder.CreateBr(checkNextBB);
-
   // Add code to terminate this openmp subfunction.
   Builder.SetInsertPoint(ExitBB);
   Function *endnowaitFunction = M->getFunction("GOMP_loop_end_nowait");
   Builder.CreateCall(endnowaitFunction);
   Builder.CreateRetVoid();
 
-  // Restore the builder back to previous basic block.
-  Builder.SetInsertPoint(PrevBB);
+  // Restore the previous insert point.
+  Builder.SetInsertPoint(PrevInsertPoint);
 }
 
 void ClastStmtCodeGen::codegenForOpenMP(const clast_for *f) {
@@ -1447,10 +1443,20 @@
 void ClastStmtCodeGen::codegen(const clast_guard *g) {
   Function *F = Builder.GetInsertBlock()->getParent();
   LLVMContext &Context = F->getContext();
+
+  BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
+                                      Builder.GetInsertPoint(), P);
+  CondBB->setName("polly.cond");
+  BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
+  MergeBB->setName("polly.merge");
   BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
-  BasicBlock *MergeBB = BasicBlock::Create(Context, "polly.merge", F);
-  DT->addNewBlock(ThenBB, Builder.GetInsertBlock());
-  DT->addNewBlock(MergeBB, Builder.GetInsertBlock());
+
+  DT->addNewBlock(ThenBB, CondBB);
+  DT->changeImmediateDominator(MergeBB, CondBB);
+
+  CondBB->getTerminator()->eraseFromParent();
+
+  Builder.SetInsertPoint(CondBB);
 
   Value *Predicate = codegen(&(g->eq[0]));
 
@@ -1461,11 +1467,10 @@
 
   Builder.CreateCondBr(Predicate, ThenBB, MergeBB);
   Builder.SetInsertPoint(ThenBB);
+  Builder.CreateBr(MergeBB);
+  Builder.SetInsertPoint(ThenBB->begin());
 
   codegen(g->then);
-
-  Builder.CreateBr(MergeBB);
-  Builder.SetInsertPoint(MergeBB);
 }
 
 void ClastStmtCodeGen::codegen(const clast_stmt *stmt) {
@@ -1489,12 +1494,6 @@
 void ClastStmtCodeGen::addParameters(const CloogNames *names) {
   SCEVExpander Rewriter(SE, "polly");
 
-  // Create an instruction that specifies the location where the parameters
-  // are expanded.
-  CastInst::CreateIntegerCast(ConstantInt::getTrue(Builder.getContext()),
-                              Builder.getInt16Ty(), false, "insertInst",
-                              Builder.GetInsertBlock());
-
   int i = 0;
   for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end();
        PI != PE; ++PI) {
@@ -1528,8 +1527,8 @@
 ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, ScalarEvolution &se,
                                    DominatorTree *dt, ScopDetection *sd,
                                    Dependences *dp, TargetData *td,
-                                  IRBuilder<> &B) :
-    S(scop), SE(se), DT(dt), SD(sd), DP(dp), TD(td), Builder(B),
+                                  IRBuilder<> &B, Pass *P) :
+    S(scop), SE(se), DT(dt), SD(sd), DP(dp), TD(td), P(P), Builder(B),
     ExpGen(Builder, NULL) {}
 
 namespace {
@@ -1725,8 +1724,12 @@
 
     // The builder will be set to startBlock.
     BasicBlock *splitBlock = addSplitAndStartBlock(&builder);
+    BasicBlock *StartBlock = builder.GetInsertBlock();
+
+    mergeControlFlow(splitBlock, &builder);
+    builder.SetInsertPoint(StartBlock->begin());
 
-    ClastStmtCodeGen CodeGen(S, *SE, DT, SD, DP, TD, builder);
+    ClastStmtCodeGen CodeGen(S, *SE, DT, SD, DP, TD, builder, this);
     CloogInfo &C = getAnalysis<CloogInfo>();
     CodeGen.codegen(C.getClast());
 
@@ -1734,8 +1737,6 @@
                          CodeGen.getParallelLoops().begin(),
                          CodeGen.getParallelLoops().end());
 
-    mergeControlFlow(splitBlock, &builder);
-
     return true;
   }
 





More information about the llvm-commits mailing list