blob: 93978f72f1740be56d45372327e05e7a4ff4ae49 [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"
Tobias Grosser3a275d22012-05-29 09:11:54 +000015#include "polly/ScopInfo.h"
Tobias Grosser472d3b72014-02-24 00:50:49 +000016#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth535d52c2013-01-02 11:47:44 +000017#include "llvm/IR/IRBuilder.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000018#include "llvm/Support/Debug.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
20
21using namespace llvm;
22
23BasicBlock *polly::executeScopConditionally(Scop &S, Pass *PassInfo) {
24 BasicBlock *StartBlock, *SplitBlock, *NewBlock;
25 Region &R = S.getRegion();
26 IRBuilder<> Builder(R.getEntry());
Tobias Grosser42aff302014-01-13 22:29:56 +000027 DominatorTree &DT =
28 PassInfo->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser3a275d22012-05-29 09:11:54 +000029 RegionInfo &RI = PassInfo->getAnalysis<RegionInfo>();
Tobias Grosser472d3b72014-02-24 00:50:49 +000030 LoopInfo &LI = PassInfo->getAnalysis<LoopInfo>();
Tobias Grosser3a275d22012-05-29 09:11:54 +000031
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(), PassInfo);
35 if (DT.dominates(R.getEntry(), NewBlock)) {
36 BasicBlock *OldBlock = R.getEntry();
37 std::string OldName = OldBlock->getName();
38
39 // Update ScopInfo.
40 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
41 if ((*SI)->getBasicBlock() == OldBlock) {
42 (*SI)->setBasicBlock(NewBlock);
43 break;
44 }
45
46 // Update RegionInfo.
47 SplitBlock = OldBlock;
48 OldBlock->setName("polly.split");
49 NewBlock->setName(OldName);
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000050 R.replaceEntryRecursive(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000051 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(Builder.getTrue(), StartBlock, R.getEntry());
Tobias Grosser472d3b72014-02-24 00:50:49 +000063 if (Loop *L = LI.getLoopFor(SplitBlock))
64 L->addBasicBlockToLoop(StartBlock, LI.getBase());
Tobias Grosser3a275d22012-05-29 09:11:54 +000065 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(), PassInfo);
76 // SplitEdge will never split R.getExit(), as R.getExit() has more than
77 // one predecessor. Hence, mergeBlock is always a newly generated block.
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000078 R.replaceExitRecursive(MergeBlock);
79 RI.setRegionFor(MergeBlock, &R);
Tobias Grosser3a275d22012-05-29 09:11:54 +000080 }
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}