2011-04-29 14:27:02 +08:00
|
|
|
//===----- ScopDetection.cpp - Detect Scops --------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Detect the maximal Scops of a function.
|
|
|
|
//
|
|
|
|
// A static control part (Scop) is a subgraph of the control flow graph (CFG)
|
|
|
|
// that only has statically known control flow and can therefore be described
|
|
|
|
// within the polyhedral model.
|
|
|
|
//
|
|
|
|
// Every Scop fullfills these restrictions:
|
|
|
|
//
|
|
|
|
// * It is a single entry single exit region
|
|
|
|
//
|
|
|
|
// * Only affine linear bounds in the loops
|
|
|
|
//
|
|
|
|
// Every natural loop in a Scop must have a number of loop iterations that can
|
|
|
|
// be described as an affine linear function in surrounding loop iterators or
|
|
|
|
// parameters. (A parameter is a scalar that does not change its value during
|
|
|
|
// execution of the Scop).
|
|
|
|
//
|
|
|
|
// * Only comparisons of affine linear expressions in conditions
|
|
|
|
//
|
|
|
|
// * All loops and conditions perfectly nested
|
|
|
|
//
|
|
|
|
// The control flow needs to be structured such that it could be written using
|
|
|
|
// just 'for' and 'if' statements, without the need for any 'goto', 'break' or
|
|
|
|
// 'continue'.
|
|
|
|
//
|
|
|
|
// * Side effect free functions call
|
|
|
|
//
|
|
|
|
// Only function calls and intrinsics that do not have side effects are allowed
|
|
|
|
// (readnone).
|
|
|
|
//
|
|
|
|
// The Scop detection finds the largest Scops by checking if the largest
|
|
|
|
// region is a Scop. If this is not the case, its canonical subregions are
|
|
|
|
// checked until a region is a Scop. It is now tried to extend this Scop by
|
|
|
|
// creating a larger non canonical region.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/ScopDetection.h"
|
|
|
|
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/Support/ScopHelper.h"
|
2011-11-07 20:58:54 +08:00
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2013-01-02 19:47:44 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2011-11-10 06:35:00 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2011-11-10 06:35:00 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2011-11-10 20:44:50 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2012-11-02 00:45:20 +08:00
|
|
|
#include "llvm/DebugInfo.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "polly-detect"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
2011-11-08 23:41:28 +08:00
|
|
|
#include <set>
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2011-10-23 19:17:06 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OnlyFunction("polly-detect-only",
|
|
|
|
cl::desc("Only detect scops in function"), cl::Hidden,
|
|
|
|
cl::value_desc("The function name to detect scops in"),
|
|
|
|
cl::ValueRequired, cl::init(""));
|
|
|
|
|
2011-11-10 20:47:26 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
IgnoreAliasing("polly-ignore-aliasing",
|
|
|
|
cl::desc("Ignore possible aliasing of the array bases"),
|
|
|
|
cl::Hidden, cl::init(false));
|
2011-10-23 19:17:06 +08:00
|
|
|
|
2012-11-02 00:45:20 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
ReportLevel("polly-report",
|
|
|
|
cl::desc("Print information about Polly"),
|
|
|
|
cl::Hidden, cl::init(false));
|
|
|
|
|
2011-12-20 18:43:14 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AllowNonAffine("polly-allow-nonaffine",
|
|
|
|
cl::desc("Allow non affine access functions in arrays"),
|
|
|
|
cl::Hidden, cl::init(false));
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Statistics.
|
|
|
|
|
|
|
|
STATISTIC(ValidRegion, "Number of regions that a valid part of Scop");
|
|
|
|
|
|
|
|
#define BADSCOP_STAT(NAME, DESC) STATISTIC(Bad##NAME##ForScop, \
|
|
|
|
"Number of bad regions for Scop: "\
|
|
|
|
DESC)
|
|
|
|
|
2011-10-08 08:30:48 +08:00
|
|
|
#define INVALID(NAME, MESSAGE) \
|
|
|
|
do { \
|
2011-10-08 08:30:55 +08:00
|
|
|
std::string Buf; \
|
|
|
|
raw_string_ostream fmt(Buf); \
|
|
|
|
fmt << MESSAGE; \
|
|
|
|
fmt.flush(); \
|
|
|
|
LastFailure = Buf; \
|
2011-10-08 08:30:48 +08:00
|
|
|
DEBUG(dbgs() << MESSAGE); \
|
|
|
|
DEBUG(dbgs() << "\n"); \
|
2011-11-10 21:21:43 +08:00
|
|
|
assert(!Context.Verifying && #NAME); \
|
|
|
|
if (!Context.Verifying) ++Bad##NAME##ForScop; \
|
|
|
|
return false; \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
|
|
|
|
#define INVALID_NOVERIFY(NAME, MESSAGE) \
|
|
|
|
do { \
|
|
|
|
std::string Buf; \
|
|
|
|
raw_string_ostream fmt(Buf); \
|
|
|
|
fmt << MESSAGE; \
|
|
|
|
fmt.flush(); \
|
|
|
|
LastFailure = Buf; \
|
|
|
|
DEBUG(dbgs() << MESSAGE); \
|
|
|
|
DEBUG(dbgs() << "\n"); \
|
|
|
|
/* DISABLED: assert(!Context.Verifying && #NAME); */ \
|
|
|
|
if (!Context.Verifying) ++Bad##NAME##ForScop; \
|
2011-10-08 08:30:48 +08:00
|
|
|
return false; \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
BADSCOP_STAT(CFG, "CFG too complex");
|
|
|
|
BADSCOP_STAT(IndVar, "Non canonical induction variable in loop");
|
|
|
|
BADSCOP_STAT(LoopBound, "Loop bounds can not be computed");
|
|
|
|
BADSCOP_STAT(FuncCall, "Function call with side effects appeared");
|
|
|
|
BADSCOP_STAT(AffFunc, "Expression not affine");
|
|
|
|
BADSCOP_STAT(Scalar, "Found scalar dependency");
|
|
|
|
BADSCOP_STAT(Alias, "Found base address alias");
|
|
|
|
BADSCOP_STAT(SimpleRegion, "Region not simple");
|
|
|
|
BADSCOP_STAT(Other, "Others");
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ScopDetection.
|
|
|
|
bool ScopDetection::isMaxRegionInScop(const Region &R) const {
|
|
|
|
// The Region is valid only if it could be found in the set.
|
|
|
|
return ValidRegions.count(&R);
|
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:55 +08:00
|
|
|
std::string ScopDetection::regionIsInvalidBecause(const Region *R) const {
|
|
|
|
if (!InvalidRegions.count(R))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return InvalidRegions.find(R)->second;
|
|
|
|
}
|
|
|
|
|
2012-12-30 07:47:38 +08:00
|
|
|
bool ScopDetection::isValidCFG(BasicBlock &BB,
|
|
|
|
DetectionContext &Context) const {
|
2011-04-29 14:27:02 +08:00
|
|
|
Region &RefRegion = Context.CurRegion;
|
|
|
|
TerminatorInst *TI = BB.getTerminator();
|
|
|
|
|
|
|
|
// Return instructions are only valid if the region is the top level region.
|
|
|
|
if (isa<ReturnInst>(TI) && !RefRegion.getExit() && TI->getNumOperands() == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
BranchInst *Br = dyn_cast<BranchInst>(TI);
|
|
|
|
|
2011-10-08 08:30:48 +08:00
|
|
|
if (!Br)
|
2011-11-17 22:52:36 +08:00
|
|
|
INVALID(CFG, "Non branch instruction terminates BB: " + BB.getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
if (Br->isUnconditional()) return true;
|
|
|
|
|
|
|
|
Value *Condition = Br->getCondition();
|
|
|
|
|
|
|
|
// UndefValue is not allowed as condition.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (isa<UndefValue>(Condition))
|
2012-12-30 07:47:38 +08:00
|
|
|
INVALID(AffFunc, "Condition based on 'undef' value in BB: " + BB.getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Only Constant and ICmpInst are allowed as condition.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (!(isa<Constant>(Condition) || isa<ICmpInst>(Condition)))
|
2011-11-17 22:52:36 +08:00
|
|
|
INVALID(AffFunc, "Condition in BB '" + BB.getName() + "' neither "
|
2011-10-08 08:30:48 +08:00
|
|
|
"constant nor an icmp instruction");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Allow perfectly nested conditions.
|
|
|
|
assert(Br->getNumSuccessors() == 2 && "Unexpected number of successors");
|
|
|
|
|
|
|
|
if (ICmpInst *ICmp = dyn_cast<ICmpInst>(Condition)) {
|
|
|
|
// Unsigned comparisons are not allowed. They trigger overflow problems
|
|
|
|
// in the code generation.
|
|
|
|
//
|
|
|
|
// TODO: This is not sufficient and just hides bugs. However it does pretty
|
|
|
|
// well.
|
2012-12-30 07:47:38 +08:00
|
|
|
if (ICmp->isUnsigned())
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Are both operands of the ICmp affine?
|
|
|
|
if (isa<UndefValue>(ICmp->getOperand(0))
|
2011-10-08 08:30:48 +08:00
|
|
|
|| isa<UndefValue>(ICmp->getOperand(1)))
|
2011-11-17 22:52:36 +08:00
|
|
|
INVALID(AffFunc, "undef operand in branch at BB: " + BB.getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-11-10 20:45:11 +08:00
|
|
|
const SCEV *LHS = SE->getSCEV(ICmp->getOperand(0));
|
|
|
|
const SCEV *RHS = SE->getSCEV(ICmp->getOperand(1));
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-11-10 20:45:11 +08:00
|
|
|
if (!isAffineExpr(&Context.CurRegion, LHS, *SE) ||
|
|
|
|
!isAffineExpr(&Context.CurRegion, RHS, *SE))
|
2011-11-17 22:52:36 +08:00
|
|
|
INVALID(AffFunc, "Non affine branch in BB '" << BB.getName()
|
2011-11-10 20:45:11 +08:00
|
|
|
<< "' with LHS: " << *LHS << " and RHS: " << *RHS);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow loop exit conditions.
|
|
|
|
Loop *L = LI->getLoopFor(&BB);
|
|
|
|
if (L && L->getExitingBlock() == &BB)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Allow perfectly nested conditions.
|
|
|
|
Region *R = RI->getRegionFor(&BB);
|
2011-10-08 08:30:48 +08:00
|
|
|
if (R->getEntry() != &BB)
|
2011-11-17 22:52:36 +08:00
|
|
|
INVALID(CFG, "Not well structured condition at BB: " + BB.getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidCallInst(CallInst &CI) {
|
|
|
|
if (CI.mayHaveSideEffects() || CI.doesNotReturn())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CI.doesNotAccessMemory())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Function *CalledFunction = CI.getCalledFunction();
|
|
|
|
|
|
|
|
// Indirect calls are not supported.
|
|
|
|
if (CalledFunction == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// TODO: Intrinsics.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidMemoryAccess(Instruction &Inst,
|
|
|
|
DetectionContext &Context) const {
|
2011-11-10 20:45:03 +08:00
|
|
|
Value *Ptr = getPointerOperand(Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
const SCEV *AccessFunction = SE->getSCEV(Ptr);
|
2011-11-10 20:44:50 +08:00
|
|
|
const SCEVUnknown *BasePointer;
|
2011-11-10 20:45:03 +08:00
|
|
|
Value *BaseValue;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-11-10 20:44:50 +08:00
|
|
|
BasePointer = dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFunction));
|
|
|
|
|
|
|
|
if (!BasePointer)
|
|
|
|
INVALID(AffFunc, "No base pointer");
|
|
|
|
|
|
|
|
BaseValue = BasePointer->getValue();
|
|
|
|
|
|
|
|
if (isa<UndefValue>(BaseValue))
|
|
|
|
INVALID(AffFunc, "Undefined base pointer");
|
|
|
|
|
|
|
|
AccessFunction = SE->getMinusSCEV(AccessFunction, BasePointer);
|
|
|
|
|
2012-12-30 07:47:38 +08:00
|
|
|
if (!isAffineExpr(&Context.CurRegion, AccessFunction, *SE, BaseValue) &&
|
|
|
|
!AllowNonAffine)
|
2012-09-08 22:00:37 +08:00
|
|
|
INVALID(AffFunc, "Non affine access function: " << *AccessFunction);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// FIXME: Alias Analysis thinks IntToPtrInst aliases with alloca instructions
|
|
|
|
// created by IndependentBlocks Pass.
|
2011-11-10 20:45:03 +08:00
|
|
|
if (isa<IntToPtrInst>(BaseValue))
|
|
|
|
INVALID(Other, "Find bad intToptr prt: " << *BaseValue);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Check if the base pointer of the memory access does alias with
|
|
|
|
// any other pointer. This cannot be handled at the moment.
|
|
|
|
AliasSet &AS =
|
2011-11-10 20:45:03 +08:00
|
|
|
Context.AST.getAliasSetForPointer(BaseValue, AliasAnalysis::UnknownSize,
|
2011-04-29 14:27:02 +08:00
|
|
|
Inst.getMetadata(LLVMContext::MD_tbaa));
|
2011-11-10 21:21:43 +08:00
|
|
|
|
|
|
|
// INVALID triggers an assertion in verifying mode, if it detects that a SCoP
|
|
|
|
// was detected by SCoP detection and that this SCoP was invalidated by a pass
|
|
|
|
// that stated it would preserve the SCoPs.
|
|
|
|
// We disable this check as the independent blocks pass may create memory
|
|
|
|
// references which seem to alias, if -basicaa is not available. They actually
|
|
|
|
// do not, but as we can not proof this without -basicaa we would fail. We
|
|
|
|
// disable this check to not cause irrelevant verification failures.
|
2011-11-10 20:47:26 +08:00
|
|
|
if (!AS.isMustAlias() && !IgnoreAliasing)
|
2011-11-10 21:21:43 +08:00
|
|
|
INVALID_NOVERIFY(Alias,
|
2011-11-23 03:40:19 +08:00
|
|
|
"Possible aliasing for value: " << BaseValue->getName()
|
|
|
|
<< "\n");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::hasScalarDependency(Instruction &Inst,
|
|
|
|
Region &RefRegion) const {
|
|
|
|
for (Instruction::use_iterator UI = Inst.use_begin(), UE = Inst.use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
if (Instruction *Use = dyn_cast<Instruction>(*UI))
|
|
|
|
if (!RefRegion.contains(Use->getParent())) {
|
|
|
|
// DirtyHack 1: PHINode user outside the Scop is not allow, if this
|
|
|
|
// PHINode is induction variable, the scalar to array transform may
|
|
|
|
// break it and introduce a non-indvar PHINode, which is not allow in
|
|
|
|
// Scop.
|
|
|
|
// This can be fix by:
|
|
|
|
// Introduce a IndependentBlockPrepare pass, which translate all
|
|
|
|
// PHINodes not in Scop to array.
|
|
|
|
// The IndependentBlockPrepare pass can also split the entry block of
|
|
|
|
// the function to hold the alloca instruction created by scalar to
|
|
|
|
// array. and split the exit block of the Scop so the new create load
|
|
|
|
// instruction for escape users will not break other Scops.
|
|
|
|
if (isa<PHINode>(Use))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidInstruction(Instruction &Inst,
|
|
|
|
DetectionContext &Context) const {
|
|
|
|
// Only canonical IVs are allowed.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(&Inst))
|
2011-10-08 08:49:30 +08:00
|
|
|
if (!isIndVar(PN, LI))
|
|
|
|
INVALID(IndVar, "Non canonical PHI node: " << Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Scalar dependencies are not allowed.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (hasScalarDependency(Inst, Context.CurRegion))
|
|
|
|
INVALID(Scalar, "Scalar dependency found: " << Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// We only check the call instruction but not invoke instruction.
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(&Inst)) {
|
|
|
|
if (isValidCallInst(*CI))
|
|
|
|
return true;
|
|
|
|
|
2011-10-08 08:49:30 +08:00
|
|
|
INVALID(FuncCall, "Call instruction: " << Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!Inst.mayWriteToMemory() && !Inst.mayReadFromMemory()) {
|
2011-10-08 08:30:48 +08:00
|
|
|
if (isa<AllocaInst>(Inst))
|
2011-10-08 08:49:30 +08:00
|
|
|
INVALID(Other, "Alloca instruction: " << Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the access function.
|
|
|
|
if (isa<LoadInst>(Inst) || isa<StoreInst>(Inst))
|
|
|
|
return isValidMemoryAccess(Inst, Context);
|
|
|
|
|
|
|
|
// We do not know this instruction, therefore we assume it is invalid.
|
2011-10-08 08:30:48 +08:00
|
|
|
INVALID(Other, "Unknown instruction: " << Inst);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidBasicBlock(BasicBlock &BB,
|
|
|
|
DetectionContext &Context) const {
|
|
|
|
if (!isValidCFG(BB, Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check all instructions, except the terminator instruction.
|
|
|
|
for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
|
|
|
|
if (!isValidInstruction(*I, Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Loop *L = LI->getLoopFor(&BB);
|
|
|
|
if (L && L->getHeader() == &BB && !isValidLoop(L, Context))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidLoop(Loop *L, DetectionContext &Context) const {
|
|
|
|
PHINode *IndVar = L->getCanonicalInductionVariable();
|
|
|
|
// No canonical induction variable.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (!IndVar)
|
2011-10-08 08:49:30 +08:00
|
|
|
INVALID(IndVar, "No canonical IV at loop header: "
|
2011-11-17 22:52:36 +08:00
|
|
|
<< L->getHeader()->getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Is the loop count affine?
|
|
|
|
const SCEV *LoopCount = SE->getBackedgeTakenCount(L);
|
2011-11-07 20:58:54 +08:00
|
|
|
if (!isAffineExpr(&Context.CurRegion, LoopCount, *SE))
|
2011-10-26 09:27:49 +08:00
|
|
|
INVALID(LoopBound, "Non affine loop bound '" << *LoopCount << "' in loop: "
|
2011-11-17 22:52:36 +08:00
|
|
|
<< L->getHeader()->getName());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Region *ScopDetection::expandRegion(Region &R) {
|
2012-04-07 23:14:28 +08:00
|
|
|
// Initial no valid region was found (greater than R)
|
|
|
|
Region *LastValidRegion = NULL;
|
2012-12-30 07:47:38 +08:00
|
|
|
Region *ExpandedRegion = R.getExpandedRegion();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "\tExpanding " << R.getNameStr() << "\n");
|
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
while (ExpandedRegion) {
|
|
|
|
DetectionContext Context(*ExpandedRegion, *AA, false /* verifying */);
|
|
|
|
DEBUG(dbgs() << "\t\tTrying " << ExpandedRegion->getNameStr() << "\n");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Check the exit first (cheap)
|
2011-04-29 14:27:02 +08:00
|
|
|
if (isValidExit(Context)) {
|
2012-04-07 23:14:28 +08:00
|
|
|
// If the exit is valid check all blocks
|
|
|
|
// - if true, a valid region was found => store it + keep expanding
|
|
|
|
// - if false, .tbd. => stop (should this really end the loop?)
|
|
|
|
if (!allBlocksValid(Context))
|
|
|
|
break;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Delete unnecessary regions (allocated by getExpandedRegion)
|
|
|
|
if (LastValidRegion)
|
|
|
|
delete LastValidRegion;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Store this region, because it is the greatest valid (encountered so far)
|
|
|
|
LastValidRegion = ExpandedRegion;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Create and test the next greater region (if any)
|
|
|
|
ExpandedRegion = ExpandedRegion->getExpandedRegion();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
} else {
|
|
|
|
// Create and test the next greater region (if any)
|
|
|
|
Region *TmpRegion = ExpandedRegion->getExpandedRegion();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
// Delete unnecessary regions (allocated by getExpandedRegion)
|
|
|
|
delete ExpandedRegion;
|
|
|
|
|
|
|
|
ExpandedRegion = TmpRegion;
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
DEBUG(
|
|
|
|
if (LastValidRegion)
|
|
|
|
dbgs() << "\tto " << LastValidRegion->getNameStr() << "\n";
|
|
|
|
else
|
|
|
|
dbgs() << "\tExpanding " << R.getNameStr() << " failed\n";
|
|
|
|
);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 23:14:28 +08:00
|
|
|
return LastValidRegion;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::findScops(Region &R) {
|
|
|
|
DetectionContext Context(R, *AA, false /*verifying*/);
|
|
|
|
|
2011-11-10 20:45:15 +08:00
|
|
|
LastFailure = "";
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
if (isValidRegion(Context)) {
|
|
|
|
++ValidRegion;
|
|
|
|
ValidRegions.insert(&R);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-08 08:30:55 +08:00
|
|
|
InvalidRegions[&R] = LastFailure;
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
|
|
|
|
findScops(**I);
|
|
|
|
|
|
|
|
// Try to expand regions.
|
|
|
|
//
|
|
|
|
// As the region tree normally only contains canonical regions, non canonical
|
|
|
|
// regions that form a Scop are not found. Therefore, those non canonical
|
|
|
|
// regions are checked by expanding the canonical ones.
|
|
|
|
|
|
|
|
std::vector<Region*> ToExpand;
|
|
|
|
|
|
|
|
for (Region::iterator I = R.begin(), E = R.end(); I != E; ++I)
|
|
|
|
ToExpand.push_back(*I);
|
|
|
|
|
|
|
|
for (std::vector<Region*>::iterator RI = ToExpand.begin(),
|
|
|
|
RE = ToExpand.end(); RI != RE; ++RI) {
|
|
|
|
Region *CurrentRegion = *RI;
|
|
|
|
|
|
|
|
// Skip invalid regions. Regions may become invalid, if they are element of
|
|
|
|
// an already expanded region.
|
|
|
|
if (ValidRegions.find(CurrentRegion) == ValidRegions.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Region *ExpandedR = expandRegion(*CurrentRegion);
|
|
|
|
|
|
|
|
if (!ExpandedR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
R.addSubRegion(ExpandedR, true);
|
|
|
|
ValidRegions.insert(ExpandedR);
|
|
|
|
ValidRegions.erase(CurrentRegion);
|
|
|
|
|
|
|
|
for (Region::iterator I = ExpandedR->begin(), E = ExpandedR->end(); I != E;
|
|
|
|
++I)
|
|
|
|
ValidRegions.erase(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::allBlocksValid(DetectionContext &Context) const {
|
|
|
|
Region &R = Context.CurRegion;
|
|
|
|
|
|
|
|
for (Region::block_iterator I = R.block_begin(), E = R.block_end(); I != E;
|
|
|
|
++I)
|
2012-05-05 05:24:27 +08:00
|
|
|
if (!isValidBasicBlock(**I, Context))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidExit(DetectionContext &Context) const {
|
|
|
|
Region &R = Context.CurRegion;
|
|
|
|
|
|
|
|
// PHI nodes are not allowed in the exit basic block.
|
|
|
|
if (BasicBlock *Exit = R.getExit()) {
|
|
|
|
BasicBlock::iterator I = Exit->begin();
|
2012-12-30 07:47:38 +08:00
|
|
|
if (I != Exit->end() && isa<PHINode>(*I))
|
2011-10-08 08:30:48 +08:00
|
|
|
INVALID(Other, "PHI node in exit BB");
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidRegion(DetectionContext &Context) const {
|
|
|
|
Region &R = Context.CurRegion;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Checking region: " << R.getNameStr() << "\n\t");
|
|
|
|
|
|
|
|
// The toplevel region is no valid region.
|
|
|
|
if (!R.getParent()) {
|
|
|
|
DEBUG(dbgs() << "Top level region is invalid";
|
|
|
|
dbgs() << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-11 02:12:19 +08:00
|
|
|
// SCoP cannot contain the entry block of the function, because we need
|
2011-04-29 14:27:02 +08:00
|
|
|
// to insert alloca instruction there when translate scalar to array.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (R.getEntry() == &(R.getEntry()->getParent()->getEntryBlock()))
|
|
|
|
INVALID(Other, "Region containing entry block of function is invalid!");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Only a simple region is allowed.
|
2011-10-08 08:30:48 +08:00
|
|
|
if (!R.isSimple())
|
|
|
|
INVALID(SimpleRegion, "Region not simple: " << R.getNameStr());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-04-07 20:29:17 +08:00
|
|
|
if (!isValidExit(Context))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
2012-04-07 20:29:17 +08:00
|
|
|
if (!allBlocksValid(Context))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "OK\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScopDetection::isValidFunction(llvm::Function &F) {
|
2011-05-06 10:38:20 +08:00
|
|
|
return !InvalidFunctions.count(&F);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-11-02 00:45:20 +08:00
|
|
|
void ScopDetection::getDebugLocation(const Region *R, unsigned &LineBegin,
|
|
|
|
unsigned &LineEnd, std::string &FileName) {
|
|
|
|
LineBegin = -1;
|
|
|
|
LineEnd = 0;
|
|
|
|
|
|
|
|
for (Region::const_block_iterator RI = R->block_begin(), RE = R->block_end();
|
|
|
|
RI != RE; ++RI)
|
|
|
|
for (BasicBlock::iterator BI = (*RI)->begin(), BE = (*RI)->end(); BI != BE;
|
|
|
|
++BI) {
|
|
|
|
DebugLoc DL = BI->getDebugLoc();
|
|
|
|
if (DL.isUnknown())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DIScope Scope(DL.getScope(BI->getContext()));
|
|
|
|
|
|
|
|
if (FileName.empty())
|
|
|
|
FileName = Scope.getFilename();
|
|
|
|
|
|
|
|
unsigned NewLine = DL.getLine();
|
|
|
|
|
|
|
|
LineBegin = std::min(LineBegin, NewLine);
|
|
|
|
LineEnd = std::max(LineEnd, NewLine);
|
|
|
|
break;
|
2012-12-30 07:47:38 +08:00
|
|
|
}
|
2012-11-02 00:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::printLocations() {
|
|
|
|
for (iterator RI = begin(), RE = end(); RI != RE; ++RI) {
|
|
|
|
unsigned LineEntry, LineExit;
|
|
|
|
std::string FileName;
|
|
|
|
|
|
|
|
getDebugLocation(*RI, LineEntry, LineExit, FileName);
|
|
|
|
|
|
|
|
if (FileName.empty()) {
|
|
|
|
outs() << "Scop detected at unknown location. Compile with debug info "
|
|
|
|
"(-g) to get more precise information. \n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
outs() << FileName << ":" << LineEntry << ": Scop start\n";
|
|
|
|
outs() << FileName << ":" << LineExit << ": Scop end\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool ScopDetection::runOnFunction(llvm::Function &F) {
|
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
RI = &getAnalysis<RegionInfo>();
|
|
|
|
Region *TopRegion = RI->getTopLevelRegion();
|
|
|
|
|
2011-10-23 19:17:06 +08:00
|
|
|
releaseMemory();
|
|
|
|
|
2011-11-17 22:52:36 +08:00
|
|
|
if (OnlyFunction != "" && F.getName() != OnlyFunction)
|
2011-10-23 19:17:06 +08:00
|
|
|
return false;
|
|
|
|
|
2012-12-30 07:47:38 +08:00
|
|
|
if (!isValidFunction(F))
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
findScops(*TopRegion);
|
2012-11-02 00:45:20 +08:00
|
|
|
|
|
|
|
if (ReportLevel >= 1)
|
|
|
|
printLocations();
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void polly::ScopDetection::verifyRegion(const Region &R) const {
|
|
|
|
assert(isMaxRegionInScop(R) && "Expect R is a valid region.");
|
|
|
|
DetectionContext Context(const_cast<Region&>(R), *AA, true /*verifying*/);
|
|
|
|
isValidRegion(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void polly::ScopDetection::verifyAnalysis() const {
|
|
|
|
for (RegionSet::const_iterator I = ValidRegions.begin(),
|
|
|
|
E = ValidRegions.end(); I != E; ++I)
|
|
|
|
verifyRegion(**I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<PostDominatorTree>();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addRequired<ScalarEvolution>();
|
|
|
|
// We also need AA and RegionInfo when we are verifying analysis.
|
|
|
|
AU.addRequiredTransitive<AliasAnalysis>();
|
|
|
|
AU.addRequiredTransitive<RegionInfo>();
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::print(raw_ostream &OS, const Module *) const {
|
|
|
|
for (RegionSet::const_iterator I = ValidRegions.begin(),
|
|
|
|
E = ValidRegions.end(); I != E; ++I)
|
|
|
|
OS << "Valid Region for Scop: " << (*I)->getNameStr() << '\n';
|
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScopDetection::releaseMemory() {
|
|
|
|
ValidRegions.clear();
|
2011-10-08 08:30:55 +08:00
|
|
|
InvalidRegions.clear();
|
2011-05-06 10:38:20 +08:00
|
|
|
// Do not clear the invalid function set.
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char ScopDetection::ID = 0;
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(ScopDetection, "polly-detect",
|
|
|
|
"Polly - Detect static control parts (SCoPs)", false,
|
|
|
|
false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
|
|
|
INITIALIZE_PASS_END(ScopDetection, "polly-detect",
|
|
|
|
"Polly - Detect static control parts (SCoPs)", false, false)
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-08-24 06:35:08 +08:00
|
|
|
Pass *polly::createScopDetectionPass() {
|
|
|
|
return new ScopDetection();
|
|
|
|
}
|