blob: 4fccbc38876a22c16ff336f3391b6fb2c75aada2 [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"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000018#include "llvm/Analysis/RegionInfo.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
Johannes Doerfert38262242014-09-10 14:50:23 +000024BasicBlock *polly::executeScopConditionally(Scop &S, Pass *P, Value *RTC) {
Tobias Grosser3a275d22012-05-29 09:11:54 +000025 BasicBlock *StartBlock, *SplitBlock, *NewBlock;
26 Region &R = S.getRegion();
Tobias Grosser5103ba72014-03-04 14:58:49 +000027 PollyIRBuilder Builder(R.getEntry());
Johannes Doerfert38262242014-09-10 14:50:23 +000028 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
29 RegionInfo &RI = P->getAnalysis<RegionInfoPass>().getRegionInfo();
Chandler Carruthf5579872015-01-17 14:16:56 +000030 LoopInfo &LI = P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
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.
Chandler Carruth62975f52015-01-19 12:37:33 +000034 NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), &DT, &LI);
Tobias Grosser3a275d22012-05-29 09:11:54 +000035 if (DT.dominates(R.getEntry(), NewBlock)) {
36 BasicBlock *OldBlock = R.getEntry();
37 std::string OldName = OldBlock->getName();
38
39 // Update ScopInfo.
Tobias Grosser7c3bad52015-05-27 05:16:57 +000040 for (ScopStmt &Stmt : S)
41 if (Stmt.getBasicBlock() == OldBlock) {
42 Stmt.setBasicBlock(NewBlock);
Tobias Grosser3a275d22012-05-29 09:11:54 +000043 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);
Johannes Doerfert38262242014-09-10 14:50:23 +000062 Builder.CreateCondBr(RTC, StartBlock, R.getEntry());
Tobias Grosser472d3b72014-02-24 00:50:49 +000063 if (Loop *L = LI.getLoopFor(SplitBlock))
Chandler Carruth6adcf562015-01-18 01:47:30 +000064 L->addBasicBlockToLoop(StartBlock, LI);
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 {
Chandler Carruth62975f52015-01-19 12:37:33 +000075 MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), &DT, &LI);
Tobias Grosser3a275d22012-05-29 09:11:54 +000076 // 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}