2011-04-29 14:27:02 +08:00
|
|
|
//===- ScopHelper.cpp - Some Helper Functions for Scop. ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Small functions that help with Scop and LLVM-IR.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/Support/ScopHelper.h"
|
2013-04-16 16:04:42 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2014-03-04 19:47:37 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2014-04-22 11:30:19 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 11:30:19 +08:00
|
|
|
#define DEBUG_TYPE "polly-scop-helper"
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
// Helper function for Scop
|
|
|
|
// TODO: Add assertion to not allow parameter to be null
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Temporary Hack for extended region tree.
|
|
|
|
// Cast the region to loop if there is a loop have the same header and exit.
|
|
|
|
Loop *polly::castToLoop(const Region &R, LoopInfo &LI) {
|
|
|
|
BasicBlock *entry = R.getEntry();
|
|
|
|
|
|
|
|
if (!LI.isLoopHeader(entry))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Loop *L = LI.getLoopFor(entry);
|
|
|
|
|
|
|
|
BasicBlock *exit = L->getExitBlock();
|
|
|
|
|
|
|
|
// Is the loop with multiple exits?
|
2013-02-15 00:42:45 +08:00
|
|
|
if (!exit)
|
|
|
|
return 0;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
if (exit != R.getExit()) {
|
|
|
|
// SubRegion/ParentRegion with the same entry.
|
2013-02-15 00:42:45 +08:00
|
|
|
assert((R.getNode(R.getEntry())->isSubRegion() ||
|
|
|
|
R.getParent()->getEntry() == entry) &&
|
|
|
|
"Expect the loop is the smaller or bigger region");
|
2011-04-29 14:27:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *polly::getPointerOperand(Instruction &Inst) {
|
|
|
|
if (LoadInst *load = dyn_cast<LoadInst>(&Inst))
|
|
|
|
return load->getPointerOperand();
|
|
|
|
else if (StoreInst *store = dyn_cast<StoreInst>(&Inst))
|
|
|
|
return store->getPointerOperand();
|
|
|
|
else if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(&Inst))
|
|
|
|
return gep->getPointerOperand();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool polly::hasInvokeEdge(const PHINode *PN) {
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(PN->getIncomingValue(i)))
|
|
|
|
if (II->getParent() == PN->getIncomingBlock(i))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *polly::createSingleExitEdge(Region *R, Pass *P) {
|
|
|
|
BasicBlock *BB = R->getExit();
|
|
|
|
|
2013-02-15 00:42:45 +08:00
|
|
|
SmallVector<BasicBlock *, 4> Preds;
|
2011-04-29 14:27:02 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI)
|
|
|
|
if (R->contains(*PI))
|
|
|
|
Preds.push_back(*PI);
|
|
|
|
|
2011-12-10 05:34:43 +08:00
|
|
|
return SplitBlockPredecessors(BB, Preds, ".region", P);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
void polly::simplifyRegion(Scop *S, Pass *P) {
|
2013-04-16 16:04:42 +08:00
|
|
|
Region *R = &S->getRegion();
|
|
|
|
|
|
|
|
// Create single entry edge if the region has multiple entry edges.
|
2013-05-07 15:30:56 +08:00
|
|
|
if (!R->getEnteringBlock()) {
|
2013-04-16 16:04:42 +08:00
|
|
|
BasicBlock *OldEntry = R->getEntry();
|
2013-05-07 15:30:56 +08:00
|
|
|
BasicBlock *NewEntry = SplitBlock(OldEntry, OldEntry->begin(), P);
|
2013-04-16 16:04:42 +08:00
|
|
|
|
2014-06-28 16:59:45 +08:00
|
|
|
for (ScopStmt *Stmt : *S)
|
|
|
|
if (Stmt->getBasicBlock() == OldEntry) {
|
|
|
|
Stmt->setBasicBlock(NewEntry);
|
2013-04-16 16:04:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
R->replaceEntryRecursive(NewEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create single exit edge if the region has multiple exit edges.
|
|
|
|
if (!R->getExitingBlock()) {
|
|
|
|
BasicBlock *NewExit = createSingleExitEdge(R, P);
|
|
|
|
|
2014-06-28 16:59:45 +08:00
|
|
|
for (auto &&SubRegion : *R)
|
|
|
|
SubRegion->replaceExitRecursive(NewExit);
|
2013-04-16 16:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
void polly::splitEntryBlockForAlloca(BasicBlock *EntryBlock, Pass *P) {
|
|
|
|
// Find first non-alloca instruction. Every basic block has a non-alloc
|
|
|
|
// instruction, as every well formed basic block has a terminator.
|
|
|
|
BasicBlock::iterator I = EntryBlock->begin();
|
2013-02-15 00:42:45 +08:00
|
|
|
while (isa<AllocaInst>(I))
|
|
|
|
++I;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// SplitBlock updates DT, DF and LI.
|
|
|
|
BasicBlock *NewEntry = SplitBlock(EntryBlock, I, P);
|
2014-07-20 02:40:17 +08:00
|
|
|
if (RegionInfoPass *RIP = P->getAnalysisIfAvailable<RegionInfoPass>())
|
|
|
|
RIP->getRegionInfo().splitBlock(NewEntry, EntryBlock);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|