2012-05-29 17:11:54 +08:00
|
|
|
//===--- Utils.cpp - Utility functions for the code generation --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains utility functions for the code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/Utils.h"
|
|
|
|
#include "polly/ScopInfo.h"
|
2013-01-02 19:47:44 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2012-05-29 17:11:54 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
BasicBlock *polly::executeScopConditionally(Scop &S, Pass *PassInfo) {
|
|
|
|
BasicBlock *StartBlock, *SplitBlock, *NewBlock;
|
|
|
|
Region &R = S.getRegion();
|
|
|
|
IRBuilder<> Builder(R.getEntry());
|
2014-01-14 06:29:56 +08:00
|
|
|
DominatorTree &DT =
|
|
|
|
PassInfo->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2012-05-29 17:11:54 +08:00
|
|
|
RegionInfo &RI = PassInfo->getAnalysis<RegionInfo>();
|
|
|
|
|
|
|
|
// Split the entry edge of the region and generate a new basic block on this
|
|
|
|
// edge. This function also updates ScopInfo and RegionInfo.
|
|
|
|
NewBlock = SplitEdge(R.getEnteringBlock(), R.getEntry(), PassInfo);
|
|
|
|
if (DT.dominates(R.getEntry(), NewBlock)) {
|
|
|
|
BasicBlock *OldBlock = R.getEntry();
|
|
|
|
std::string OldName = OldBlock->getName();
|
|
|
|
|
|
|
|
// Update ScopInfo.
|
|
|
|
for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
|
|
|
|
if ((*SI)->getBasicBlock() == OldBlock) {
|
|
|
|
(*SI)->setBasicBlock(NewBlock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update RegionInfo.
|
|
|
|
SplitBlock = OldBlock;
|
|
|
|
OldBlock->setName("polly.split");
|
|
|
|
NewBlock->setName(OldName);
|
2013-04-10 14:55:20 +08:00
|
|
|
R.replaceEntryRecursive(NewBlock);
|
2012-05-29 17:11:54 +08:00
|
|
|
RI.setRegionFor(NewBlock, &R);
|
|
|
|
} else {
|
|
|
|
RI.setRegionFor(NewBlock, R.getParent());
|
|
|
|
SplitBlock = NewBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
SplitBlock->setName("polly.split_new_and_old");
|
|
|
|
Function *F = SplitBlock->getParent();
|
|
|
|
StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
|
|
|
|
SplitBlock->getTerminator()->eraseFromParent();
|
|
|
|
Builder.SetInsertPoint(SplitBlock);
|
|
|
|
Builder.CreateCondBr(Builder.getTrue(), StartBlock, R.getEntry());
|
|
|
|
DT.addNewBlock(StartBlock, SplitBlock);
|
|
|
|
Builder.SetInsertPoint(StartBlock);
|
|
|
|
|
|
|
|
BasicBlock *MergeBlock;
|
|
|
|
|
|
|
|
if (R.getExit()->getSinglePredecessor())
|
|
|
|
// No splitEdge required. A block with a single predecessor cannot have
|
|
|
|
// PHI nodes that would complicate life.
|
|
|
|
MergeBlock = R.getExit();
|
|
|
|
else {
|
|
|
|
MergeBlock = SplitEdge(R.getExitingBlock(), R.getExit(), PassInfo);
|
|
|
|
// SplitEdge will never split R.getExit(), as R.getExit() has more than
|
|
|
|
// one predecessor. Hence, mergeBlock is always a newly generated block.
|
2013-04-10 14:55:20 +08:00
|
|
|
R.replaceExitRecursive(MergeBlock);
|
|
|
|
RI.setRegionFor(MergeBlock, &R);
|
2012-05-29 17:11:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Builder.CreateBr(MergeBlock);
|
|
|
|
MergeBlock->setName("polly.merge_new_and_old");
|
|
|
|
|
|
|
|
if (DT.dominates(SplitBlock, MergeBlock))
|
|
|
|
DT.changeImmediateDominator(MergeBlock, SplitBlock);
|
|
|
|
return StartBlock;
|
|
|
|
}
|