21#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/Analysis/AliasSetTracker.h"
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Analysis/OptimizationRemarkEmitter.h"
29#include "llvm/Analysis/RegionInfo.h"
30#include "llvm/Analysis/ScalarEvolution.h"
31#include "llvm/IR/BasicBlock.h"
32#include "llvm/IR/CFG.h"
33#include "llvm/IR/DebugLoc.h"
34#include "llvm/IR/DiagnosticInfo.h"
35#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/raw_ostream.h"
45#define DEBUG_TYPE "polly-detect"
47#define SCOP_STAT(NAME, DESC) \
48 {"polly-detect", "NAME", "Number of rejected regions: " DESC}
52 SCOP_STAT(InvalidTerminator,
"Unsupported terminator instruction"),
53 SCOP_STAT(IrreducibleRegion,
"Irreducible loops"),
54 SCOP_STAT(UnreachableInExit,
"Unreachable in exit block"),
55 SCOP_STAT(IndirectPredecessor,
"Branch from indirect terminator"),
58 SCOP_STAT(UndefCond,
"Undefined branch condition"),
59 SCOP_STAT(InvalidCond,
"Non-integer branch condition"),
60 SCOP_STAT(UndefOperand,
"Undefined operands in comparison"),
61 SCOP_STAT(NonAffBranch,
"Non-affine branch condition"),
63 SCOP_STAT(UndefBasePtr,
"Undefined base pointer"),
64 SCOP_STAT(VariantBasePtr,
"Variant base pointer"),
65 SCOP_STAT(NonAffineAccess,
"Non-affine memory accesses"),
66 SCOP_STAT(DifferentElementSize,
"Accesses with differing sizes"),
68 SCOP_STAT(LoopBound,
"Uncomputable loop bounds"),
69 SCOP_STAT(LoopHasNoExit,
"Loop without exit"),
70 SCOP_STAT(LoopHasMultipleExits,
"Loop with multiple exits"),
71 SCOP_STAT(LoopOnlySomeLatches,
"Not all loop latches in scop"),
72 SCOP_STAT(FuncCall,
"Function call with side effects"),
74 "Compilated access semantics (volatile or atomic)"),
75 SCOP_STAT(Alias,
"Base address aliasing"),
77 SCOP_STAT(IntToPtr,
"Integer to pointer conversions"),
79 SCOP_STAT(UnknownInst,
"Unknown Instructions"),
81 SCOP_STAT(Unprofitable,
"Assumed to be unprofitable"),
88template <
typename T> std::string
operator+(Twine LHS,
const T &RHS) {
90 raw_string_ostream fmt(Buf);
93 return LHS.concat(Buf).str();
100static bool operator<(
const DebugLoc &LHS,
const DebugLoc &RHS) {
101 return LHS.getLine() < RHS.getLine() ||
102 (LHS.getLine() == RHS.getLine() && LHS.getCol() < RHS.getCol());
109 return std::make_pair(R->getEntry(), R->getExit());
113 SmallPtrSet<BasicBlock *, 32> Seen;
114 SmallVector<BasicBlock *, 32> Todo;
115 Todo.push_back(P.first);
116 while (!Todo.empty()) {
117 auto *BB = Todo.pop_back_val();
120 if (!Seen.insert(BB).second)
122 Todo.append(succ_begin(BB), succ_end(BB));
123 for (
const Instruction &Inst : *BB) {
124 DebugLoc DL = Inst.getStableDebugLoc();
128 Begin = Begin ? std::min(Begin, DL) : DL;
129 End = End ? std::max(End, DL) : DL;
135 OptimizationRemarkEmitter &ORE) {
140 OptimizationRemarkMissed(
DEBUG_TYPE,
"RejectionErrors", Begin, P.first)
141 <<
"The following errors keep this region from being a Scop.");
145 if (
const DebugLoc &Loc = RR->getDebugLoc())
146 ORE.emit(OptimizationRemarkMissed(
DEBUG_TYPE, RR->getRemarkName(), Loc,
148 << RR->getEndUserMessage());
150 ORE.emit(OptimizationRemarkMissed(
DEBUG_TYPE, RR->getRemarkName(), Begin,
152 << RR->getEndUserMessage());
158 OptimizationRemarkMissed(
DEBUG_TYPE,
"InvalidScopEnd", End, P.second)
159 <<
"Invalid Scop candidate ends here.");
162 OptimizationRemarkMissed(
DEBUG_TYPE,
"InvalidScopEnd", End, P.first)
163 <<
"Invalid Scop candidate ends here.");
184 OS.indent(level) <<
"[" << j++ <<
"] " << Reason->getMessage() <<
"\n";
201 return "InvalidTerminator";
207 return (
"Invalid instruction terminates BB: " +
BB->getName()).str();
211 return BB->getTerminator()->getDebugLoc();
222 return "UnreachableInExit";
228 std::string BBName =
BB->getName().str();
229 return "Unreachable in exit block" + BBName;
235 return "Unreachable in exit block.";
246 return "IndirectPredecessor";
251 return Inst->getParent();
257 return "Branch from indirect terminator: " + *
Inst;
266 return "Branch from indirect terminator.";
277 return "IrreducibleRegion";
281 return R->getEntry();
285 return "Irreducible region encountered: " +
R->getNameStr();
291 return "Irreducible region encountered in control flow.";
317 return (
"Condition based on 'undef' value in BB: " +
BB->getName()).str();
332 return (
"Condition in BB '" +
BB->getName()).str() +
333 "' neither constant nor an icmp instruction";
348 return (
"undef operand in branch at BB: " +
BB->getName()).str();
363 return (
"Non affine branch in BB '" +
BB->getName()).str() +
364 "' with LHS: " + *
LHS +
" and RHS: " + *
RHS;
390 return Inst->getParent();
394 return "Undefined base pointer";
405 return "VariantBasePtr";
409 return Inst->getParent();
413 return "Base address not invariant in current region:" + *
BaseValue;
417 return "The base address of this array is not invariant inside the loop";
428 return "DifferentArrayElementSize";
432 return Inst->getParent();
436 return "Access to one array through data types of different size";
444 StringRef BaseName =
BaseValue->getName();
445 std::string Name = BaseName.empty() ?
"UNKNOWN" : BaseName.str();
446 return "The array \"" + Name +
447 "\" is accessed through elements that differ "
455 return "NonAffineAccess";
459 return Inst->getParent();
471 StringRef BaseName =
BaseValue->getName();
472 std::string Name = BaseName.empty() ?
"UNKNOWN" : BaseName.str();
473 return "The array subscript of \"" + Name +
"\" is not affine";
481 Loc(L->getStartLoc()) {}
488 return "Non affine loop bound '" + *
LoopCount +
489 "' in loop: " +
L->getHeader()->getName();
499 return "Failed to derive an affine function from the loop bounds.";
506 return "LoopHasNoExit";
512 return "Loop " +
L->getHeader()->getName() +
" has no exit.";
522 return "Loop cannot be handled because it has no exit.";
529 return "ReportLoopHasMultipleExits";
533 return L->getHeader();
537 return "Loop " +
L->getHeader()->getName() +
" has multiple exits.";
547 return "Loop cannot be handled because it has multiple exits.";
554 return "LoopHasNoExit";
558 return L->getHeader();
562 return "Not all latches of loop " +
L->getHeader()->getName() +
573 return "Loop cannot be handled because not all latches are part of loop "
588 return "Call instruction: " + *
Inst;
592 return Inst->getDebugLoc();
596 return "This function call cannot be handled. "
611 return "NonSimpleMemoryAccess";
615 return Inst->getParent();
619 return "Non-simple memory access: " + *
Inst;
623 return Inst->getDebugLoc();
627 return "Volatile memory accesses or memory accesses for atomic types "
628 "are not supported.";
644 std::string Suffix)
const {
646 raw_string_ostream OS(Message);
650 for (PointerSnapshotTy::const_iterator PI =
Pointers.begin(),
653 const Value *V = *PI;
654 assert(V &&
"Diagnostic info does not match found LLVM-IR anymore.");
656 if (V->getName().empty())
657 OS <<
"\" <unknown> \"";
659 OS <<
"\"" << V->getName() <<
"\"";
684 " may access the same memory.");
719 return "Find bad intToptr prt: " + *
BaseValue;
741 return "Alloca instruction: " + *
Inst;
745 return Inst->getDebugLoc();
761 return Inst->getParent();
765 return "Unknown instruction: " + *
Inst;
769 return Inst->getDebugLoc();
787 return "Region containing entry block of function is invalid!";
791 return "Scop contains function entry (not yet supported).";
795 return BB->getTerminator()->getDebugLoc();
813 return "Region can not profitably be optimized!";
817 return "No profitable polyhedral optimization found";
821 for (
const BasicBlock *BB :
R->blocks())
822 for (
const Instruction &Inst : *BB)
823 if (
const DebugLoc &DL = Inst.getStableDebugLoc())
826 return R->getEntry()->getTerminator()->getDebugLoc();
#define SCOP_STAT(NAME, DESC)
static Statistic RejectStatistics[]
Stores all errors that occurred during the detection.
void print(raw_ostream &OS, int level=0) const
SmallVector< RejectReasonPtr, 1 > ErrorReports
Base class of all reject reasons found during Scop detection.
static const DebugLoc Unknown
RejectReason(RejectReasonKind K)
RejectReasonKind getKind() const
virtual const DebugLoc & getDebugLoc() const
Get the source location of this error.
static bool classof(const RejectReason *RR)
ReportAffFunc(const RejectReasonKind K, const Instruction *Inst)
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string formatInvalidAlias(std::string Prefix="", std::string Suffix="") const
Format an invalid alias set.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
ReportAlias(Instruction *Inst, AliasSet &AS)
PointerSnapshotTy Pointers
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
ReportAlloca(Instruction *Inst)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
static bool classof(const RejectReason *RR)
static bool classof(const RejectReason *RR)
ReportCFG(const RejectReasonKind K)
static bool classof(const RejectReason *RR)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
ReportEntry(BasicBlock *BB)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
static bool classof(const RejectReason *RR)
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
ReportFuncCall(Instruction *Inst)
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
static bool classof(const RejectReason *RR)
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
ReportIntToPtr(Instruction *BaseValue)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
static bool classof(const RejectReason *RR)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
static bool classof(const RejectReason *RR)
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
ReportLoopBound(Loop *L, const SCEV *LoopCount)
static bool classof(const RejectReason *RR)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
static bool classof(const RejectReason *RR)
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
Loop * L
The loop that has multiple exits.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
Loop * L
The loop that has no exit.
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
Loop * L
The loop for which not all loop latches are part of the scop.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
static bool classof(const RejectReason *RR)
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
static bool classof(const RejectReason *RR)
const SCEV * LHS
LHS & RHS of the failed condition.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const SCEV * AccessFunction
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
ReportNonSimpleMemoryAccess(Instruction *Inst)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
static bool classof(const RejectReason *RR)
Base class for otherwise ungrouped reject reasons.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
static bool classof(const RejectReason *RR)
ReportOther(const RejectReasonKind K)
static bool classof(const RejectReason *RR)
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
static bool classof(const RejectReason *RR)
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
static bool classof(const RejectReason *RR)
std::string getRemarkName() const override
Generate the remark name to identify this remark.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
ReportUnknownInst(Instruction *Inst)
static bool classof(const RejectReason *RR)
static bool classof(const RejectReason *RR)
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
ReportUnprofitable(Region *R)
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
static bool classof(const RejectReason *RR)
const DebugLoc & getDebugLoc() const override
Get the source location of this error.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
const Value * getRemarkBB() const override
Get the Basic Block containing this remark.
std::string getEndUserMessage() const override
Generate a message for the end-user describing this error.
static bool classof(const RejectReason *RR)
std::string getRemarkName() const override
Generate the remark name to identify this remark.
std::string getMessage() const override
Generate a reasonable diagnostic message describing this error.
static __isl_give isl_union_map * append_range(__isl_take isl_union_map *umap, int extra)
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
static bool operator<(const DebugLoc &LHS, const DebugLoc &RHS)
std::pair< llvm::BasicBlock *, llvm::BasicBlock * > BBPair
Type to hold region delimiters (entry & exit block).
std::shared_ptr< RejectReason > RejectReasonPtr
void getDebugLocations(const BBPair &P, DebugLoc &Begin, DebugLoc &End)
Set the begin and end source location for the region limited by P.
isl::pw_aff operator+(isl::pw_aff Left, isl::pw_aff Right)
Addition.
@ Value
MemoryKind::Value: Models an llvm::Value.
void emitRejectionRemarks(const BBPair &P, const RejectLog &Log, OptimizationRemarkEmitter &ORE)
Emit optimization remarks about the rejected regions to the user.
BBPair getBBPairForRegion(const Region *R)
Return the region delimiters (entry & exit block) of R.