1 //===--- Utils.cpp - Utility functions for the code generation --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains utility functions for the code generation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "polly/CodeGen/Utils.h"
15 #include "polly/CodeGen/IRBuilder.h"
16 #include "polly/ScopInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21 
22 using namespace llvm;
23 
24 BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) {
25   BasicBlock *StartBlock, *SplitBlock, *NewBlock;
26   Region &R = S.getRegion();
27   PollyIRBuilder Builder(R.getEntry());
28   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
29   RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo();
30   LoopInfo &LI = P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
31 
32   // Split the entry edge of the region and generate a new basic block on this
33   // edge. This function also updates ScopInfo and RegionInfo.
34   NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), &DT, &LI);
35   if (DT.dominates(R.getEntry(), NewBlock)) {
36     BasicBlock *OldBlock = R.getEntry();
37     std::string OldName = OldBlock->getName();
38 
39     // Update ScopInfo.
40     for (auto &Stmt : S)
41       if (Stmt->getBasicBlock() == OldBlock) {
42         Stmt->setBasicBlock(NewBlock);
43         break;
44       }
45 
46     // Update RegionInfo.
47     SplitBlock = OldBlock;
48     OldBlock->setName("polly.split");
49     NewBlock->setName(OldName);
50     R.replaceEntryRecursive(NewBlock);
51     RI.setRegionFor(NewBlock, &R);
52   } else {
53     RI.setRegionFor(NewBlock, R.getParent());
54     SplitBlock = NewBlock;
55   }
56 
57   SplitBlock->setName("polly.split_new_and_old");
58   Function *F = SplitBlock->getParent();
59   StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
60   SplitBlock->getTerminator()->eraseFromParent();
61   Builder.SetInsertPoint(SplitBlock);
62   Builder.CreateCondBr(RTC, StartBlock, R.getEntry());
63   if (Loop *L = LI.getLoopFor(SplitBlock))
64     L->addBasicBlockToLoop(StartBlock, LI);
65   DT.addNewBlock(StartBlock, SplitBlock);
66   Builder.SetInsertPoint(StartBlock);
67 
68   BasicBlock *MergeBlock;
69 
70   if (R.getExit()->getSinglePredecessor())
71     // No splitEdge required.  A block with a single predecessor cannot have
72     // PHI nodes that would complicate life.
73     MergeBlock = R.getExit();
74   else {
75     MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), &DT, &LI);
76     // SplitEdge will never split R.getExit(), as R.getExit() has more than
77     // one predecessor. Hence, mergeBlock is always a newly generated block.
78     R.replaceExitRecursive(MergeBlock);
79     RI.setRegionFor(MergeBlock, &R);
80   }
81 
82   Builder.CreateBr(MergeBlock);
83   MergeBlock->setName("polly.merge_new_and_old");
84 
85   if (DT.dominates(SplitBlock, MergeBlock))
86     DT.changeImmediateDominator(MergeBlock, SplitBlock);
87   return StartBlock;
88 }
89