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