blob: 2c6d415cf8e2623acf55d0a73a40332035ecc4c8 [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"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000017
Tobias Grosser472d3b72014-02-24 00:50:49 +000018#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000019#include "llvm/Analysis/RegionInfo.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000020#include "llvm/Support/Debug.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.h"
22
23using namespace llvm;
24
Johannes Doerfert38262242014-09-10 14:50:23 +000025BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) {
Tobias Grosser3a275d22012-05-29 09:11:54 +000026 BasicBlock *StartBlock, *SplitBlock, *NewBlock;
27 Region &R = S.getRegion();
Tobias Grosser5103ba72014-03-04 14:58:49 +000028 PollyIRBuilder Builder(R.getEntry());
Johannes Doerfert38262242014-09-10 14:50:23 +000029 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
30 RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo();
Chandler Carruthf5579872015-01-17 14:16:56 +000031 LoopInfo &LI = P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Tobias Grosser3a275d22012-05-29 09:11:54 +000032
33 // Split the entry edge of the region and generate a new basic block on this
34 // edge. This function also updates ScopInfo and RegionInfo.
Chandler Carruth62975f52015-01-19 12:37:33 +000035 NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), &DT, &LI);
Tobias Grosser3a275d22012-05-29 09:11:54 +000036 if (DT.dominates(R.getEntry(), NewBlock)) {
37 BasicBlock *OldBlock = R.getEntry();
38 std::string OldName = OldBlock->getName();
39
40 // Update ScopInfo.
Tobias Grosser083d3d32014-06-28 08:59:45 +000041 for (ScopStmt *Stmt : S)
42 if (Stmt->getBasicBlock() == OldBlock) {
43 Stmt->setBasicBlock(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000044 break;
45 }
46
47 // Update RegionInfo.
48 SplitBlock = OldBlock;
49 OldBlock->setName("polly.split");
50 NewBlock->setName(OldName);
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000051 R.replaceEntryRecursive(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000052 RI.setRegionFor(NewBlock, &R);
53 } else {
54 RI.setRegionFor(NewBlock, R.getParent());
55 SplitBlock = NewBlock;
56 }
57
58 SplitBlock->setName("polly.split_new_and_old");
59 Function *F = SplitBlock->getParent();
60 StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
61 SplitBlock->getTerminator()->eraseFromParent();
62 Builder.SetInsertPoint(SplitBlock);
Johannes Doerfert38262242014-09-10 14:50:23 +000063 Builder.CreateCondBr(RTC, StartBlock, R.getEntry());
Tobias Grosser472d3b72014-02-24 00:50:49 +000064 if (Loop *L = LI.getLoopFor(SplitBlock))
Chandler Carruth6adcf562015-01-18 01:47:30 +000065 L->addBasicBlockToLoop(StartBlock, LI);
Tobias Grosser3a275d22012-05-29 09:11:54 +000066 DT.addNewBlock(StartBlock, SplitBlock);
67 Builder.SetInsertPoint(StartBlock);
68
69 BasicBlock *MergeBlock;
70
71 if (R.getExit()->getSinglePredecessor())
72 // No splitEdge required. A block with a single predecessor cannot have
73 // PHI nodes that would complicate life.
74 MergeBlock = R.getExit();
75 else {
Chandler Carruth62975f52015-01-19 12:37:33 +000076 MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), &DT, &LI);
Tobias Grosser3a275d22012-05-29 09:11:54 +000077 // SplitEdge will never split R.getExit(), as R.getExit() has more than
78 // one predecessor. Hence, mergeBlock is always a newly generated block.
Tobias Grosser03fc9ac2013-04-10 06:55:20 +000079 R.replaceExitRecursive(MergeBlock);
80 RI.setRegionFor(MergeBlock, &R);
Tobias Grosser3a275d22012-05-29 09:11:54 +000081 }
82
83 Builder.CreateBr(MergeBlock);
84 MergeBlock->setName("polly.merge_new_and_old");
85
86 if (DT.dominates(SplitBlock, MergeBlock))
87 DT.changeImmediateDominator(MergeBlock, SplitBlock);
88 return StartBlock;
89}