1 //===------ CodeGeneration.cpp - Code generate the Scops using ISL. ----======// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // The CodeGeneration pass takes a Scop created by ScopInfo and translates it 11 // back to LLVM-IR using the ISL code generator. 12 // 13 // The Scop describes the high level memory behaviour of a control flow region. 14 // Transformation passes can update the schedule (execution order) of statements 15 // in the Scop. ISL is used to generate an abstract syntax tree that reflects 16 // the updated execution order. This clast is used to create new LLVM-IR that is 17 // computationally equivalent to the original control flow region, but executes 18 // its code in the new execution order defined by the changed schedule. 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "polly/CodeGen/IslNodeBuilder.h" 23 #include "polly/CodeGen/IslAst.h" 24 #include "polly/CodeGen/Utils.h" 25 #include "polly/DependenceInfo.h" 26 #include "polly/LinkAllPasses.h" 27 #include "polly/ScopInfo.h" 28 #include "polly/Support/ScopHelper.h" 29 #include "polly/TempScopInfo.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/IR/Verifier.h" 32 #include "llvm/Support/Debug.h" 33 34 using namespace polly; 35 using namespace llvm; 36 37 #define DEBUG_TYPE "polly-codegen" 38 39 namespace { 40 class CodeGeneration : public ScopPass { 41 public: 42 static char ID; 43 44 CodeGeneration() : ScopPass(ID) {} 45 46 /// @brief The datalayout used 47 const DataLayout *DL; 48 49 /// @name The analysis passes we need to generate code. 50 /// 51 ///{ 52 LoopInfo *LI; 53 IslAstInfo *AI; 54 DominatorTree *DT; 55 ScalarEvolution *SE; 56 RegionInfo *RI; 57 ///} 58 59 /// @brief The loop annotator to generate llvm.loop metadata. 60 ScopAnnotator Annotator; 61 62 /// @brief Build the runtime condition. 63 /// 64 /// Build the condition that evaluates at run-time to true iff all 65 /// assumptions taken for the SCoP hold, and to false otherwise. 66 /// 67 /// @return A value evaluating to true/false if execution is save/unsafe. 68 Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) { 69 Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator()); 70 Value *RTC = ExprBuilder.create(AI->getRunCondition()); 71 if (!RTC->getType()->isIntegerTy(1)) 72 RTC = Builder.CreateIsNotNull(RTC); 73 return RTC; 74 } 75 76 bool verifyGeneratedFunction(Scop &S, Function &F) { 77 if (!verifyFunction(F)) 78 return false; 79 80 DEBUG({ 81 errs() << "== ISL Codegen created an invalid function ==\n\n== The " 82 "SCoP ==\n"; 83 S.print(errs()); 84 errs() << "\n== The isl AST ==\n"; 85 AI->printScop(errs(), S); 86 errs() << "\n== The invalid function ==\n"; 87 F.print(errs()); 88 errs() << "\n== The errors ==\n"; 89 verifyFunction(F, &errs()); 90 }); 91 92 return true; 93 } 94 95 // CodeGeneration adds a lot of BBs without updating the RegionInfo 96 // We make all created BBs belong to the scop's parent region without any 97 // nested structure to keep the RegionInfo verifier happy. 98 void fixRegionInfo(Function *F, Region *ParentRegion) { 99 for (BasicBlock &BB : *F) { 100 if (RI->getRegionFor(&BB)) 101 continue; 102 103 RI->setRegionFor(&BB, ParentRegion); 104 } 105 } 106 107 bool runOnScop(Scop &S) override { 108 AI = &getAnalysis<IslAstInfo>(); 109 110 // Check if we created an isl_ast root node, otherwise exit. 111 isl_ast_node *AstRoot = AI->getAst(); 112 if (!AstRoot) 113 return false; 114 115 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 116 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 117 SE = &getAnalysis<ScalarEvolution>(); 118 DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); 119 RI = &getAnalysis<RegionInfoPass>().getRegionInfo(); 120 Region *R = &S.getRegion(); 121 assert(!R->isTopLevelRegion() && "Top level regions are not supported"); 122 123 Annotator.buildAliasScopes(S); 124 125 simplifyRegion(R, DT, LI, RI); 126 assert(R->isSimple()); 127 BasicBlock *EnteringBB = S.getRegion().getEnteringBlock(); 128 assert(EnteringBB); 129 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator); 130 131 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S); 132 133 // Only build the run-time condition and parameters _after_ having 134 // introduced the conditional branch. This is important as the conditional 135 // branch will guard the original scop from new induction variables that 136 // the SCEVExpander may introduce while code generating the parameters and 137 // which may introduce scalar dependences that prevent us from correctly 138 // code generating this scop. 139 BasicBlock *StartBlock = 140 executeScopConditionally(S, this, Builder.getTrue()); 141 auto SplitBlock = StartBlock->getSinglePredecessor(); 142 Builder.SetInsertPoint(SplitBlock->getTerminator()); 143 NodeBuilder.addParameters(S.getContext()); 144 Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder()); 145 SplitBlock->getTerminator()->setOperand(0, RTC); 146 Builder.SetInsertPoint(StartBlock->begin()); 147 148 NodeBuilder.create(AstRoot); 149 150 NodeBuilder.finalizeSCoP(S); 151 fixRegionInfo(EnteringBB->getParent(), R->getParent()); 152 153 assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) && 154 "Verification of generated function failed"); 155 return true; 156 } 157 158 void printScop(raw_ostream &, Scop &) const override {} 159 160 void getAnalysisUsage(AnalysisUsage &AU) const override { 161 AU.addRequired<DominatorTreeWrapperPass>(); 162 AU.addRequired<IslAstInfo>(); 163 AU.addRequired<RegionInfoPass>(); 164 AU.addRequired<ScalarEvolution>(); 165 AU.addRequired<ScopDetection>(); 166 AU.addRequired<ScopInfo>(); 167 AU.addRequired<LoopInfoWrapperPass>(); 168 169 AU.addPreserved<DependenceInfo>(); 170 171 AU.addPreserved<LoopInfoWrapperPass>(); 172 AU.addPreserved<DominatorTreeWrapperPass>(); 173 AU.addPreserved<IslAstInfo>(); 174 AU.addPreserved<ScopDetection>(); 175 AU.addPreserved<ScalarEvolution>(); 176 177 // FIXME: We do not yet add regions for the newly generated code to the 178 // region tree. 179 AU.addPreserved<RegionInfoPass>(); 180 AU.addPreserved<TempScopInfo>(); 181 AU.addPreserved<ScopInfo>(); 182 AU.addPreservedID(IndependentBlocksID); 183 } 184 }; 185 } 186 187 char CodeGeneration::ID = 1; 188 189 Pass *polly::createCodeGenerationPass() { return new CodeGeneration(); } 190 191 INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", 192 "Polly - Create LLVM-IR from SCoPs", false, false); 193 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 194 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 195 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 196 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 197 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); 198 INITIALIZE_PASS_DEPENDENCY(ScopDetection); 199 INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", 200 "Polly - Create LLVM-IR from SCoPs", false, false) 201