blob: 2e569392efd6003f919115bb4b519fbe31aee9a1 [file] [log] [blame]
Tobias Grosser3a275d22012-05-29 09:11:54 +00001//===--- 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
16#include "polly/ScopInfo.h"
17
Chandler Carruth535d52c2013-01-02 11:47:44 +000018#include "llvm/IR/IRBuilder.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000019#include "llvm/Support/Debug.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21
22using namespace llvm;
23
24BasicBlock *polly::executeScopConditionally(Scop &S, Pass *PassInfo) {
25 BasicBlock *StartBlock, *SplitBlock, *NewBlock;
26 Region &R = S.getRegion();
27 IRBuilder<> Builder(R.getEntry());
28 DominatorTree &DT = PassInfo->getAnalysis<DominatorTree>();
29 RegionInfo &RI = PassInfo->getAnalysis<RegionInfo>();
30
31 // Split the entry edge of the region and generate a new basic block on this
32 // edge. This function also updates ScopInfo and RegionInfo.
33 NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), PassInfo);
34 if (DT.dominates(R.getEntry(), NewBlock)) {
35 BasicBlock *OldBlock = R.getEntry();
36 std::string OldName = OldBlock->getName();
37
38 // Update ScopInfo.
39 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
40 if ((*SI)->getBasicBlock() == OldBlock) {
41 (*SI)->setBasicBlock(NewBlock);
42 break;
43 }
44
45 // Update RegionInfo.
46 SplitBlock = OldBlock;
47 OldBlock->setName("polly.split");
48 NewBlock->setName(OldName);
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000049 R.replaceEntryRecursive(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000050 RI.setRegionFor(NewBlock, &R);
51 } else {
52 RI.setRegionFor(NewBlock, R.getParent());
53 SplitBlock = NewBlock;
54 }
55
56 SplitBlock->setName("polly.split_new_and_old");
57 Function *F = SplitBlock->getParent();
58 StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
59 SplitBlock->getTerminator()->eraseFromParent();
60 Builder.SetInsertPoint(SplitBlock);
61 Builder.CreateCondBr(Builder.getTrue(), StartBlock, R.getEntry());
62 DT.addNewBlock(StartBlock, SplitBlock);
63 Builder.SetInsertPoint(StartBlock);
64
65 BasicBlock *MergeBlock;
66
67 if (R.getExit()->getSinglePredecessor())
68 // No splitEdge required. A block with a single predecessor cannot have
69 // PHI nodes that would complicate life.
70 MergeBlock = R.getExit();
71 else {
72 MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), PassInfo);
73 // SplitEdge will never split R.getExit(), as R.getExit() has more than
74 // one predecessor. Hence, mergeBlock is always a newly generated block.
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000075 R.replaceExitRecursive(MergeBlock);
76 RI.setRegionFor(MergeBlock, &R);
Tobias Grosser3a275d22012-05-29 09:11:54 +000077 }
78
79 Builder.CreateBr(MergeBlock);
80 MergeBlock->setName("polly.merge_new_and_old");
81
82 if (DT.dominates(SplitBlock, MergeBlock))
83 DT.changeImmediateDominator(MergeBlock, SplitBlock);
84 return StartBlock;
85}