1609089f2SHongbin Zheng //===------ LoopGenerators.cpp - IR helper to create loops ---------------===// 2609089f2SHongbin Zheng // 3609089f2SHongbin Zheng // The LLVM Compiler Infrastructure 4609089f2SHongbin Zheng // 5609089f2SHongbin Zheng // This file is distributed under the University of Illinois Open Source 6609089f2SHongbin Zheng // License. See LICENSE.TXT for details. 7609089f2SHongbin Zheng // 8609089f2SHongbin Zheng //===----------------------------------------------------------------------===// 9609089f2SHongbin Zheng // 10*12b355a2SJohannes Doerfert // This file contains functions to create scalar and parallel loops as LLVM-IR. 11609089f2SHongbin Zheng // 12609089f2SHongbin Zheng //===----------------------------------------------------------------------===// 13609089f2SHongbin Zheng 14609089f2SHongbin Zheng #include "polly/ScopDetection.h" 158a846610SHongbin Zheng #include "polly/CodeGen/LoopGenerators.h" 163081b0f5STobias Grosser #include "llvm/Analysis/LoopInfo.h" 17535d52c7SChandler Carruth #include "llvm/IR/DataLayout.h" 18e87c6a81SChandler Carruth #include "llvm/IR/Dominators.h" 1983628182STobias Grosser #include "llvm/IR/Module.h" 20609089f2SHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 21609089f2SHongbin Zheng 22609089f2SHongbin Zheng using namespace llvm; 234ac4e155SHongbin Zheng using namespace polly; 24609089f2SHongbin Zheng 25dd5c1442SJohannes Doerfert // We generate a loop of either of the following structures: 265db6ffd7STobias Grosser // 27dd5c1442SJohannes Doerfert // BeforeBB BeforeBB 28dd5c1442SJohannes Doerfert // | | 29dd5c1442SJohannes Doerfert // v v 30dd5c1442SJohannes Doerfert // GuardBB PreHeaderBB 31dd5c1442SJohannes Doerfert // / | | _____ 32dd5c1442SJohannes Doerfert // __ PreHeaderBB | v \/ | 33dd5c1442SJohannes Doerfert // / \ / | HeaderBB latch 34dd5c1442SJohannes Doerfert // latch HeaderBB | |\ | 35dd5c1442SJohannes Doerfert // \ / \ / | \------/ 36dd5c1442SJohannes Doerfert // < \ / | 37dd5c1442SJohannes Doerfert // \ / v 38dd5c1442SJohannes Doerfert // ExitBB ExitBB 395db6ffd7STobias Grosser // 40dd5c1442SJohannes Doerfert // depending on whether or not we know that it is executed at least once. If 41dd5c1442SJohannes Doerfert // not, GuardBB checks if the loop is executed at least once. If this is the 42dd5c1442SJohannes Doerfert // case we branch to PreHeaderBB and subsequently to the HeaderBB, which 43dd5c1442SJohannes Doerfert // contains the loop iv 'polly.indvar', the incremented loop iv 44dd5c1442SJohannes Doerfert // 'polly.indvar_next' as well as the condition to check if we execute another 45dd5c1442SJohannes Doerfert // iteration of the loop. After the loop has finished, we branch to ExitBB. 464ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, 472ef3f4fdSJohannes Doerfert PollyIRBuilder &Builder, Pass *P, LoopInfo &LI, 482ef3f4fdSJohannes Doerfert DominatorTree &DT, BasicBlock *&ExitBB, 4937c9b8e0STobias Grosser ICmpInst::Predicate Predicate, 5051d1c74dSJohannes Doerfert ScopAnnotator *Annotator, bool Parallel, 51dd5c1442SJohannes Doerfert bool UseGuard) { 524ac4e155SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 53609089f2SHongbin Zheng LLVMContext &Context = F->getContext(); 54609089f2SHongbin Zheng 555db6ffd7STobias Grosser assert(LB->getType() == UB->getType() && "Types of loop bounds do not match"); 56609089f2SHongbin Zheng IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); 57609089f2SHongbin Zheng assert(LoopIVType && "UB is not integer?"); 58609089f2SHongbin Zheng 595db6ffd7STobias Grosser BasicBlock *BeforeBB = Builder.GetInsertBlock(); 60dd5c1442SJohannes Doerfert BasicBlock *GuardBB = 61dd5c1442SJohannes Doerfert UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr; 625db6ffd7STobias Grosser BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); 635db6ffd7STobias Grosser BasicBlock *PreHeaderBB = 645db6ffd7STobias Grosser BasicBlock::Create(Context, "polly.loop_preheader", F); 65609089f2SHongbin Zheng 663081b0f5STobias Grosser // Update LoopInfo 673081b0f5STobias Grosser Loop *OuterLoop = LI.getLoopFor(BeforeBB); 683081b0f5STobias Grosser Loop *NewLoop = new Loop(); 693081b0f5STobias Grosser 70dd5c1442SJohannes Doerfert if (OuterLoop) 713081b0f5STobias Grosser OuterLoop->addChildLoop(NewLoop); 72dd5c1442SJohannes Doerfert else 733081b0f5STobias Grosser LI.addTopLevelLoop(NewLoop); 743081b0f5STobias Grosser 75dd5c1442SJohannes Doerfert if (OuterLoop && GuardBB) 763081b0f5STobias Grosser OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase()); 77dd5c1442SJohannes Doerfert else if (OuterLoop) 783081b0f5STobias Grosser OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase()); 793081b0f5STobias Grosser 803081b0f5STobias Grosser NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase()); 813081b0f5STobias Grosser 82c7b719fcSJohannes Doerfert // Notify the annotator (if present) that we have a new loop, but only 83c7b719fcSJohannes Doerfert // after the header block is set. 84c7b719fcSJohannes Doerfert if (Annotator) 85c7b719fcSJohannes Doerfert Annotator->pushLoop(NewLoop, Parallel); 86c7b719fcSJohannes Doerfert 875db6ffd7STobias Grosser // ExitBB 885db6ffd7STobias Grosser ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P); 895db6ffd7STobias Grosser ExitBB->setName("polly.loop_exit"); 90609089f2SHongbin Zheng 915db6ffd7STobias Grosser // BeforeBB 92dd5c1442SJohannes Doerfert if (GuardBB) { 935db6ffd7STobias Grosser BeforeBB->getTerminator()->setSuccessor(0, GuardBB); 94dd5c1442SJohannes Doerfert DT.addNewBlock(GuardBB, BeforeBB); 95609089f2SHongbin Zheng 965db6ffd7STobias Grosser // GuardBB 975db6ffd7STobias Grosser Builder.SetInsertPoint(GuardBB); 985db6ffd7STobias Grosser Value *LoopGuard; 995db6ffd7STobias Grosser LoopGuard = Builder.CreateICmp(Predicate, LB, UB); 1005db6ffd7STobias Grosser LoopGuard->setName("polly.loop_guard"); 1015db6ffd7STobias Grosser Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); 102dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, GuardBB); 103dd5c1442SJohannes Doerfert } else { 104dd5c1442SJohannes Doerfert BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); 105dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, BeforeBB); 106dd5c1442SJohannes Doerfert } 107609089f2SHongbin Zheng 1085db6ffd7STobias Grosser // PreHeaderBB 1095db6ffd7STobias Grosser Builder.SetInsertPoint(PreHeaderBB); 1104ac4e155SHongbin Zheng Builder.CreateBr(HeaderBB); 111609089f2SHongbin Zheng 1125db6ffd7STobias Grosser // HeaderBB 1135db6ffd7STobias Grosser DT.addNewBlock(HeaderBB, PreHeaderBB); 1145db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB); 1155db6ffd7STobias Grosser PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); 1165db6ffd7STobias Grosser IV->addIncoming(LB, PreHeaderBB); 1175db6ffd7STobias Grosser Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); 1185db6ffd7STobias Grosser Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); 1195db6ffd7STobias Grosser Value *LoopCondition; 1205db6ffd7STobias Grosser UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub"); 1215db6ffd7STobias Grosser LoopCondition = Builder.CreateICmp(Predicate, IV, UB); 1225db6ffd7STobias Grosser LoopCondition->setName("polly.loop_cond"); 123c7b719fcSJohannes Doerfert 124c7b719fcSJohannes Doerfert // Create the loop latch and annotate it as such. 125c7b719fcSJohannes Doerfert BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); 126c7b719fcSJohannes Doerfert if (Annotator) 127c7b719fcSJohannes Doerfert Annotator->annotateLoopLatch(B, NewLoop, Parallel); 128c7b719fcSJohannes Doerfert 1295db6ffd7STobias Grosser IV->addIncoming(IncrementedIV, HeaderBB); 130dd5c1442SJohannes Doerfert if (GuardBB) 1315db6ffd7STobias Grosser DT.changeImmediateDominator(ExitBB, GuardBB); 132dd5c1442SJohannes Doerfert else 133f8a678d2STobias Grosser DT.changeImmediateDominator(ExitBB, HeaderBB); 134609089f2SHongbin Zheng 1355db6ffd7STobias Grosser // The loop body should be added here. 1365db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); 137609089f2SHongbin Zheng return IV; 138609089f2SHongbin Zheng } 139609089f2SHongbin Zheng 140*12b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createParallelLoop( 141*12b355a2SJohannes Doerfert Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues, 142*12b355a2SJohannes Doerfert ValueToValueMapTy &Map, BasicBlock::iterator *LoopBody) { 143*12b355a2SJohannes Doerfert Value *Struct, *IV, *SubFnParam; 144*12b355a2SJohannes Doerfert Function *SubFn; 145*12b355a2SJohannes Doerfert 146*12b355a2SJohannes Doerfert Struct = storeValuesIntoStruct(UsedValues); 147*12b355a2SJohannes Doerfert 148*12b355a2SJohannes Doerfert BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint(); 149*12b355a2SJohannes Doerfert IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn); 150*12b355a2SJohannes Doerfert *LoopBody = Builder.GetInsertPoint(); 151*12b355a2SJohannes Doerfert Builder.SetInsertPoint(BeforeLoop); 152*12b355a2SJohannes Doerfert 153*12b355a2SJohannes Doerfert SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), 154*12b355a2SJohannes Doerfert "polly.par.userContext"); 155*12b355a2SJohannes Doerfert 156*12b355a2SJohannes Doerfert // Add one as the upper bound provided by openmp is a < comparison 157*12b355a2SJohannes Doerfert // whereas the codegenForSequential function creates a <= comparison. 158*12b355a2SJohannes Doerfert UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1)); 159*12b355a2SJohannes Doerfert 160*12b355a2SJohannes Doerfert // Tell the runtime we start a parallel loop 161*12b355a2SJohannes Doerfert createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride); 162*12b355a2SJohannes Doerfert Builder.CreateCall(SubFn, SubFnParam); 163*12b355a2SJohannes Doerfert createCallJoinThreads(); 164*12b355a2SJohannes Doerfert 165*12b355a2SJohannes Doerfert return IV; 166*12b355a2SJohannes Doerfert } 167*12b355a2SJohannes Doerfert 168*12b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn, 169*12b355a2SJohannes Doerfert Value *SubFnParam, Value *LB, 170*12b355a2SJohannes Doerfert Value *UB, Value *Stride) { 171*12b355a2SJohannes Doerfert const std::string Name = "GOMP_parallel_loop_runtime_start"; 172*12b355a2SJohannes Doerfert 173609089f2SHongbin Zheng Function *F = M->getFunction(Name); 174609089f2SHongbin Zheng 175609089f2SHongbin Zheng // If F is not available, declare it. 176609089f2SHongbin Zheng if (!F) { 177609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 178609089f2SHongbin Zheng 179c14582f2STobias Grosser Type *Params[] = {PointerType::getUnqual(FunctionType::get( 180c14582f2STobias Grosser Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), 181*12b355a2SJohannes Doerfert Builder.getInt8PtrTy(), LongType, LongType, LongType, 182*12b355a2SJohannes Doerfert LongType}; 183609089f2SHongbin Zheng 184609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); 185609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 186609089f2SHongbin Zheng } 187609089f2SHongbin Zheng 188*12b355a2SJohannes Doerfert Value *NumberOfThreads = ConstantInt::get(LongType, 0); 189*12b355a2SJohannes Doerfert Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride}; 190609089f2SHongbin Zheng 191609089f2SHongbin Zheng Builder.CreateCall(F, Args); 192609089f2SHongbin Zheng } 193609089f2SHongbin Zheng 194*12b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr, 195*12b355a2SJohannes Doerfert Value *UBPtr) { 196*12b355a2SJohannes Doerfert const std::string Name = "GOMP_loop_runtime_next"; 197*12b355a2SJohannes Doerfert 198609089f2SHongbin Zheng Function *F = M->getFunction(Name); 199609089f2SHongbin Zheng 200609089f2SHongbin Zheng // If F is not available, declare it. 201609089f2SHongbin Zheng if (!F) { 202609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 203*12b355a2SJohannes Doerfert Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()}; 204609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); 205609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 206609089f2SHongbin Zheng } 207609089f2SHongbin Zheng 208*12b355a2SJohannes Doerfert Value *Args[] = {LBPtr, UBPtr}; 209609089f2SHongbin Zheng Value *Return = Builder.CreateCall(F, Args); 210c14582f2STobias Grosser Return = Builder.CreateICmpNE( 211c14582f2STobias Grosser Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); 212609089f2SHongbin Zheng return Return; 213609089f2SHongbin Zheng } 214609089f2SHongbin Zheng 215*12b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallJoinThreads() { 216*12b355a2SJohannes Doerfert const std::string Name = "GOMP_parallel_end"; 217*12b355a2SJohannes Doerfert 218609089f2SHongbin Zheng Function *F = M->getFunction(Name); 219609089f2SHongbin Zheng 220609089f2SHongbin Zheng // If F is not available, declare it. 221609089f2SHongbin Zheng if (!F) { 222609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 223609089f2SHongbin Zheng 224609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 225609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 226609089f2SHongbin Zheng } 227609089f2SHongbin Zheng 228609089f2SHongbin Zheng Builder.CreateCall(F); 229609089f2SHongbin Zheng } 230609089f2SHongbin Zheng 231*12b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallCleanupThread() { 232*12b355a2SJohannes Doerfert const std::string Name = "GOMP_loop_end_nowait"; 233*12b355a2SJohannes Doerfert 234609089f2SHongbin Zheng Function *F = M->getFunction(Name); 235609089f2SHongbin Zheng 236609089f2SHongbin Zheng // If F is not available, declare it. 237609089f2SHongbin Zheng if (!F) { 238609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 239609089f2SHongbin Zheng 240609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 241609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 242609089f2SHongbin Zheng } 243609089f2SHongbin Zheng 244609089f2SHongbin Zheng Builder.CreateCall(F); 245609089f2SHongbin Zheng } 246609089f2SHongbin Zheng 247*12b355a2SJohannes Doerfert Function *ParallelLoopGenerator::createSubFnDefinition() { 248609089f2SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 249609089f2SHongbin Zheng std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); 250609089f2SHongbin Zheng FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); 251*12b355a2SJohannes Doerfert Function *SubFn = Function::Create(FT, Function::InternalLinkage, 252*12b355a2SJohannes Doerfert F->getName() + ".polly.subfn", M); 253*12b355a2SJohannes Doerfert 254609089f2SHongbin Zheng // Do not run any polly pass on the new function. 255*12b355a2SJohannes Doerfert SubFn->addFnAttr(PollySkipFnAttr); 256609089f2SHongbin Zheng 257*12b355a2SJohannes Doerfert Function::arg_iterator AI = SubFn->arg_begin(); 258*12b355a2SJohannes Doerfert AI->setName("polly.par.userContext"); 259609089f2SHongbin Zheng 260*12b355a2SJohannes Doerfert return SubFn; 261609089f2SHongbin Zheng } 262609089f2SHongbin Zheng 263*12b355a2SJohannes Doerfert Value * 264*12b355a2SJohannes Doerfert ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) { 265*12b355a2SJohannes Doerfert SmallVector<Type *, 8> Members; 266609089f2SHongbin Zheng 26791f5b262STobias Grosser for (Value *V : Values) 26891f5b262STobias Grosser Members.push_back(V->getType()); 269609089f2SHongbin Zheng 270609089f2SHongbin Zheng StructType *Ty = StructType::get(Builder.getContext(), Members); 271*12b355a2SJohannes Doerfert Value *Struct = 272*12b355a2SJohannes Doerfert new AllocaInst(Ty, 0, "polly.par.userContext", Builder.GetInsertPoint()); 273609089f2SHongbin Zheng 274609089f2SHongbin Zheng for (unsigned i = 0; i < Values.size(); i++) { 275609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 276609089f2SHongbin Zheng Builder.CreateStore(Values[i], Address); 277609089f2SHongbin Zheng } 278609089f2SHongbin Zheng 279609089f2SHongbin Zheng return Struct; 280609089f2SHongbin Zheng } 281609089f2SHongbin Zheng 282*12b355a2SJohannes Doerfert void ParallelLoopGenerator::extractValuesFromStruct( 283*12b355a2SJohannes Doerfert SetVector<Value *> OldValues, Value *Struct, ValueToValueMapTy &Map) { 284609089f2SHongbin Zheng for (unsigned i = 0; i < OldValues.size(); i++) { 285609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 286609089f2SHongbin Zheng Value *NewValue = Builder.CreateLoad(Address); 287*12b355a2SJohannes Doerfert Map[OldValues[i]] = NewValue; 288609089f2SHongbin Zheng } 289609089f2SHongbin Zheng } 290609089f2SHongbin Zheng 291*12b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createSubFn(Value *Stride, Value *StructData, 292e602a076STobias Grosser SetVector<Value *> Data, 293e602a076STobias Grosser ValueToValueMapTy &Map, 294*12b355a2SJohannes Doerfert Function **SubFnPtr) { 295*12b355a2SJohannes Doerfert BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB; 296*12b355a2SJohannes Doerfert Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV; 297*12b355a2SJohannes Doerfert Function *SubFn = createSubFnDefinition(); 298*12b355a2SJohannes Doerfert LLVMContext &Context = SubFn->getContext(); 299609089f2SHongbin Zheng 300609089f2SHongbin Zheng // Store the previous basic block. 301609089f2SHongbin Zheng PrevBB = Builder.GetInsertBlock(); 302609089f2SHongbin Zheng 303609089f2SHongbin Zheng // Create basic blocks. 304*12b355a2SJohannes Doerfert HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn); 305*12b355a2SJohannes Doerfert ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn); 306*12b355a2SJohannes Doerfert CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn); 307*12b355a2SJohannes Doerfert PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn); 308609089f2SHongbin Zheng 309609089f2SHongbin Zheng DT.addNewBlock(HeaderBB, PrevBB); 310609089f2SHongbin Zheng DT.addNewBlock(ExitBB, HeaderBB); 311609089f2SHongbin Zheng DT.addNewBlock(CheckNextBB, HeaderBB); 312*12b355a2SJohannes Doerfert DT.addNewBlock(PreHeaderBB, HeaderBB); 313609089f2SHongbin Zheng 314609089f2SHongbin Zheng // Fill up basic block HeaderBB. 315609089f2SHongbin Zheng Builder.SetInsertPoint(HeaderBB); 316*12b355a2SJohannes Doerfert LBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.LBPtr"); 317*12b355a2SJohannes Doerfert UBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.UBPtr"); 318*12b355a2SJohannes Doerfert UserContext = Builder.CreateBitCast(SubFn->arg_begin(), StructData->getType(), 319*12b355a2SJohannes Doerfert "polly.par.userContext"); 320609089f2SHongbin Zheng 321609089f2SHongbin Zheng extractValuesFromStruct(Data, UserContext, Map); 322609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 323609089f2SHongbin Zheng 324609089f2SHongbin Zheng // Add code to check if another set of iterations will be executed. 325609089f2SHongbin Zheng Builder.SetInsertPoint(CheckNextBB); 326*12b355a2SJohannes Doerfert Ret1 = createCallGetWorkItem(LBPtr, UBPtr); 327609089f2SHongbin Zheng HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), 328*12b355a2SJohannes Doerfert "polly.par.hasNextScheduleBlock"); 329*12b355a2SJohannes Doerfert Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB); 330609089f2SHongbin Zheng 331609089f2SHongbin Zheng // Add code to to load the iv bounds for this set of iterations. 332*12b355a2SJohannes Doerfert Builder.SetInsertPoint(PreHeaderBB); 333*12b355a2SJohannes Doerfert LB = Builder.CreateLoad(LBPtr, "polly.par.LB"); 334*12b355a2SJohannes Doerfert UB = Builder.CreateLoad(UBPtr, "polly.par.UB"); 335609089f2SHongbin Zheng 336609089f2SHongbin Zheng // Subtract one as the upper bound provided by openmp is a < comparison 337609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 338*12b355a2SJohannes Doerfert UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1), 339*12b355a2SJohannes Doerfert "polly.par.UBAdjusted"); 340609089f2SHongbin Zheng 341609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 342609089f2SHongbin Zheng Builder.SetInsertPoint(--Builder.GetInsertPoint()); 343*12b355a2SJohannes Doerfert IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB, 344dd5c1442SJohannes Doerfert ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false); 345609089f2SHongbin Zheng 346609089f2SHongbin Zheng BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); 347609089f2SHongbin Zheng 348*12b355a2SJohannes Doerfert // Add code to terminate this subfunction. 349609089f2SHongbin Zheng Builder.SetInsertPoint(ExitBB); 350*12b355a2SJohannes Doerfert createCallCleanupThread(); 351609089f2SHongbin Zheng Builder.CreateRetVoid(); 352609089f2SHongbin Zheng 353609089f2SHongbin Zheng Builder.SetInsertPoint(LoopBody); 354*12b355a2SJohannes Doerfert *SubFnPtr = SubFn; 355609089f2SHongbin Zheng 356609089f2SHongbin Zheng return IV; 357609089f2SHongbin Zheng } 358