blob: 8ea9a4aa32c424e849211249ad0312a655579f0f [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 Grosser5103ba72014-03-04 14:58:49 +000015#include "polly/CodeGen/IRBuilder.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000016#include "polly/ScopInfo.h"
Tobias Grosser472d3b72014-02-24 00:50:49 +000017#include "llvm/Analysis/LoopInfo.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
Johannes Doerfert38262242014-09-10 14:50:23 +000023BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) {
Tobias Grosser3a275d22012-05-29 09:11:54 +000024 BasicBlock *StartBlock, *SplitBlock, *NewBlock;
25 Region &R = S.getRegion();
Tobias Grosser5103ba72014-03-04 14:58:49 +000026 PollyIRBuilder Builder(R.getEntry());
Johannes Doerfert38262242014-09-10 14:50:23 +000027 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
28 RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo();
29 LoopInfo &LI = P->getAnalysis<LoopInfo>();
Tobias Grosser3a275d22012-05-29 09:11:54 +000030
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.
Johannes Doerfert38262242014-09-10 14:50:23 +000033 NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), P);
Tobias Grosser3a275d22012-05-29 09:11:54 +000034 if (DT.dominates(R.getEntry(), NewBlock)) {
35 BasicBlock *OldBlock = R.getEntry();
36 std::string OldName = OldBlock->getName();
37
38 // Update ScopInfo.
Tobias Grosser083d3d32014-06-28 08:59:45 +000039 for (ScopStmt *Stmt : S)
40 if (Stmt->getBasicBlock() == OldBlock) {
41 Stmt->setBasicBlock(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000042 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);
Johannes Doerfert38262242014-09-10 14:50:23 +000061 Builder.CreateCondBr(RTC, StartBlock, R.getEntry());
Tobias Grosser472d3b72014-02-24 00:50:49 +000062 if (Loop *L = LI.getLoopFor(SplitBlock))
63 L->addBasicBlockToLoop(StartBlock, LI.getBase());
Tobias Grosser3a275d22012-05-29 09:11:54 +000064 DT.addNewBlock(StartBlock, SplitBlock);
65 Builder.SetInsertPoint(StartBlock);
66
67 BasicBlock *MergeBlock;
68
69 if (R.getExit()->getSinglePredecessor())
70 // No splitEdge required. A block with a single predecessor cannot have
71 // PHI nodes that would complicate life.
72 MergeBlock = R.getExit();
73 else {
Johannes Doerfert38262242014-09-10 14:50:23 +000074 MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), P);
Tobias Grosser3a275d22012-05-29 09:11:54 +000075 // SplitEdge will never split R.getExit(), as R.getExit() has more than
76 // one predecessor. Hence, mergeBlock is always a newly generated block.
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000077 R.replaceExitRecursive(MergeBlock);
78 RI.setRegionFor(MergeBlock, &R);
Tobias Grosser3a275d22012-05-29 09:11:54 +000079 }
80
81 Builder.CreateBr(MergeBlock);
82 MergeBlock->setName("polly.merge_new_and_old");
83
84 if (DT.dominates(SplitBlock, MergeBlock))
85 DT.changeImmediateDominator(MergeBlock, SplitBlock);
86 return StartBlock;
87}