32#include "llvm/ADT/Statistic.h"
33#include "llvm/Analysis/LoopInfo.h"
34#include "llvm/Analysis/RegionInfo.h"
35#include "llvm/IR/BasicBlock.h"
36#include "llvm/IR/Dominators.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/PassManager.h"
39#include "llvm/IR/Verifier.h"
40#include "llvm/InitializePasses.h"
41#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/raw_ostream.h"
51#define DEBUG_TYPE "polly-codegen"
53static cl::opt<bool>
Verify(
"polly-codegen-verify",
54 cl::desc(
"Verify the function generated by Polly"),
59static cl::opt<bool, true>
61 cl::desc(
"Add run-time performance monitoring"), cl::Hidden,
65STATISTIC(ScopsProcessed,
"Number of SCoP processed");
66STATISTIC(CodegenedScops,
"Number of successfully generated SCoPs");
68 "Number of original affine loops in SCoPs that have been generated");
70 "Number of original boxed loops in SCoPs that have been generated");
79 auto *OrigTerminator = Block.getTerminator();
80 Builder.SetInsertPoint(OrigTerminator);
81 Builder.CreateUnreachable();
82 OrigTerminator->eraseFromParent();
87 if (!
Verify || !verifyFunction(F, &errs()))
91 errs() <<
"== ISL Codegen created an invalid function ==\n\n== The "
94 errs() <<
"\n== The isl AST ==\n";
96 errs() <<
"\n== The invalid function ==\n";
100 llvm_unreachable(
"Polly generated function could not be verified. Add "
101 "-polly-codegen-verify=false to disable this assertion.");
107static void fixRegionInfo(Function &F, Region &ParentRegion, RegionInfo &RI) {
108 for (BasicBlock &BB : F) {
109 if (RI.getRegionFor(&BB))
112 RI.setRegionFor(&BB, &ParentRegion);
145 for (
auto *BB : R->blocks()) {
146 auto InstIt = BB->begin();
147 auto InstEnd = BB->end();
149 while (InstIt != InstEnd) {
150 auto NextIt = InstIt;
153 if (
auto *IT = dyn_cast<IntrinsicInst>(&*InstIt)) {
154 switch (IT->getIntrinsicID()) {
155 case Intrinsic::lifetime_start:
156 case Intrinsic::lifetime_end:
157 IT->eraseFromParent();
170 DominatorTree &DT, ScalarEvolution &SE,
187 POLLY_DEBUG(dbgs() <<
"Got an IstAst for a different Scop/isl_ctx\n");
198 auto ScopStats =
S.getStatistics();
201 auto &DL =
S.getFunction().getDataLayout();
202 Region *R = &
S.getRegion();
203 assert(!R->isTopLevelRegion() &&
"Top level regions are not supported");
209 BasicBlock *EnteringBB =
S.getEnteringBlock();
211 PollyIRBuilder Builder(EnteringBB->getContext(), ConstantFolder(),
213 Builder.SetInsertPoint(EnteringBB->getTerminator());
223 BasicBlock *StartBlock = std::get<0>(StartExitBlocks);
224 BasicBlock *ExitBlock = std::get<1>(StartExitBlocks);
227 auto *SplitBlock = StartBlock->getSinglePredecessor();
229 IslNodeBuilder NodeBuilder(Builder, Annotator, DL, LI, SE, DT,
S, StartBlock);
241 BasicBlock *MergeBlock = ExitBlock->getUniqueSuccessor();
250 Builder.SetInsertPoint(SplitBlock->getTerminator());
254 auto *FalseI1 = Builder.getFalse();
255 auto *SplitBBTerm = Builder.GetInsertBlock()->getTerminator();
256 SplitBBTerm->setOperand(0, FalseI1);
260 auto *ExitingBlock = StartBlock->getUniqueSuccessor();
262 auto *MergeBlock = ExitingBlock->getUniqueSuccessor();
266 auto *ExitingBB =
S.getExitingBlock();
268 DT.changeImmediateDominator(MergeBlock, ExitingBB);
269 DT.eraseNode(ExitingBlock);
274 Builder.GetInsertBlock()->getTerminator()->setOperand(0, RTC);
282 Builder.SetInsertPoint(StartBlock->getTerminator());
286 fixRegionInfo(*EnteringBB->getParent(), *R->getParent(), RI);
289 CodegenedAffineLoops += ScopStats.NumAffineLoops;
290 CodegenedBoxedLoops += ScopStats.NumBoxedLoops;
293 Function *F = EnteringBB->getParent();
300 F->addFnAttr(
"polly-optimized");
306class CodeGeneration final :
public ScopPass {
311 const DataLayout *DL;
327 AI = &getAnalysis<IslAstInfoWrapperPass>().getAI();
328 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
329 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
330 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
331 DL = &
S.getFunction().getDataLayout();
332 RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
340 AU.addRequired<DominatorTreeWrapperPass>();
342 AU.addRequired<RegionInfoPass>();
343 AU.addRequired<ScalarEvolutionWrapperPass>();
346 AU.addRequired<LoopInfoWrapperPass>();
363 return PreservedAnalyses::none();
366 return PreservedAnalyses::all();
369char CodeGeneration::ID = 1;
374 "Polly - Create LLVM-IR from SCoPs",
false,
false);
382 "Polly - Create LLVM-IR from SCoPs",
false,
false)
static bool generateCode(Scop &S, IslAstInfo &AI, LoopInfo &LI, DominatorTree &DT, ScalarEvolution &SE, RegionInfo &RI)
STATISTIC(ScopsProcessed, "Number of SCoP processed")
static cl::opt< bool, true > XPerfMonitoring("polly-codegen-perf-monitoring", cl::desc("Add run-time performance monitoring"), cl::Hidden, cl::location(polly::PerfMonitoring), cl::cat(PollyCategory))
static void verifyGeneratedFunction(Scop &S, Function &F, IslAstInfo &AI)
static void fixRegionInfo(Function &F, Region &ParentRegion, RegionInfo &RI)
static cl::opt< bool > Verify("polly-codegen-verify", cl::desc("Verify the function generated by Polly"), cl::Hidden, cl::cat(PollyCategory))
static void removeLifetimeMarkers(Region *R)
Remove all lifetime markers (llvm.lifetime.start, llvm.lifetime.end) from @R.
INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences", "Polly - Calculate dependences", false, false)
INITIALIZE_PASS_END(DependenceInfo, "polly-dependences", "Polly - Calculate dependences", false, false) namespace
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass)
polly dump Polly Dump Function
llvm::cl::OptionCategory PollyCategory
__isl_give isl_ast_expr * release()
__isl_give isl_ast_node * release()
Add Polly specifics when running IRBuilder.
isl::ast_expr getRunCondition()
Get the run condition.
void print(raw_ostream &O)
IslAst & getIslAst()
Return the isl AST computed by this IslAstInfo.
const std::shared_ptr< isl_ctx > getSharedIslCtx() const
void addParameters(__isl_take isl_set *Context)
void create(__isl_take isl_ast_node *Node)
virtual void finalize()
Finalize code generation.
bool preloadInvariantLoads()
Preload all memory loads that are invariant.
const ArrayRef< Function * > getParallelSubfunctions() const
Return the parallel subfunctions that have been created.
void allocateNewArrays(BBPair StartExitBlocks)
Allocate memory for all new arrays created by Polly.
Value * createRTC(isl_ast_expr *Condition)
Generate code that evaluates Condition at run-time.
void initialize()
Initialize the performance monitor.
void insertRegionEnd(llvm::Instruction *InsertBefore)
Mark the end of a timing region.
void insertRegionStart(llvm::Instruction *InsertBefore)
Mark the beginning of a timing region.
void invalidateScop(Scop &S)
Helper class to annotate newly generated SCoPs with metadata.
void buildAliasScopes(Scop &S)
Build all alias scopes for the given SCoP.
The legacy pass manager's analysis pass to compute scop information for a region.
ScopPass - This class adapts the RegionPass interface to allow convenient creation of passes that ope...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnScop(Scop &S)=0
runOnScop - This method must be overloaded to perform the desired Polyhedral transformation or analys...
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
std::pair< llvm::BasicBlock *, llvm::BasicBlock * > BBPair
Type to hold region delimiters (entry & exit block).
void markBlockUnreachable(BasicBlock &Block, PollyIRBuilder &Builder)
Mark a basic block unreachable.
@ Value
MemoryKind::Value: Models an llvm::Value.
llvm::Pass * createCodeGenerationPass()
std::pair< BBPair, llvm::BranchInst * > executeScopConditionally(Scop &S, llvm::Value *RTC, llvm::DominatorTree &DT, llvm::RegionInfo &RI, llvm::LoopInfo &LI)
Execute a Scop conditionally wrt RTC.
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
AnalysisManager< Scop, ScopStandardAnalysisResults & > ScopAnalysisManager
void simplifyRegion(llvm::Region *R, llvm::DominatorTree *DT, llvm::LoopInfo *LI, llvm::RegionInfo *RI)
Simplify the region to have a single unconditional entry edge and a single exit edge.
PreservedAnalyses run(Scop &S, ScopAnalysisManager &SAM, ScopStandardAnalysisResults &AR, SPMUpdater &U)