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 // 1012b355a2SJohannes Doerfert // This file contains functions to create scalar and parallel loops as LLVM-IR. 11609089f2SHongbin Zheng // 12609089f2SHongbin Zheng //===----------------------------------------------------------------------===// 13609089f2SHongbin Zheng 148a846610SHongbin Zheng #include "polly/CodeGen/LoopGenerators.h" 155624d3c9STobias Grosser #include "polly/ScopDetection.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" 20*4fe342cbSHongbin Zheng #include "llvm/IR/PatternMatch.h" 21990cd4c2SJohannes Doerfert #include "llvm/Support/CommandLine.h" 22ba0d0922STobias Grosser #include "llvm/Transforms/Utils/BasicBlockUtils.h" 23609089f2SHongbin Zheng 24609089f2SHongbin Zheng using namespace llvm; 254ac4e155SHongbin Zheng using namespace polly; 26*4fe342cbSHongbin Zheng using namespace PatternMatch; 27609089f2SHongbin Zheng 28990cd4c2SJohannes Doerfert static cl::opt<int> 29990cd4c2SJohannes Doerfert PollyNumThreads("polly-num-threads", 30990cd4c2SJohannes Doerfert cl::desc("Number of threads to use (0 = auto)"), cl::Hidden, 31990cd4c2SJohannes Doerfert cl::init(0)); 32990cd4c2SJohannes Doerfert 33dd5c1442SJohannes Doerfert // We generate a loop of either of the following structures: 345db6ffd7STobias Grosser // 35dd5c1442SJohannes Doerfert // BeforeBB BeforeBB 36dd5c1442SJohannes Doerfert // | | 37dd5c1442SJohannes Doerfert // v v 38dd5c1442SJohannes Doerfert // GuardBB PreHeaderBB 39dd5c1442SJohannes Doerfert // / | | _____ 40dd5c1442SJohannes Doerfert // __ PreHeaderBB | v \/ | 41dd5c1442SJohannes Doerfert // / \ / | HeaderBB latch 42dd5c1442SJohannes Doerfert // latch HeaderBB | |\ | 43dd5c1442SJohannes Doerfert // \ / \ / | \------/ 44dd5c1442SJohannes Doerfert // < \ / | 45dd5c1442SJohannes Doerfert // \ / v 46dd5c1442SJohannes Doerfert // ExitBB ExitBB 475db6ffd7STobias Grosser // 48dd5c1442SJohannes Doerfert // depending on whether or not we know that it is executed at least once. If 49dd5c1442SJohannes Doerfert // not, GuardBB checks if the loop is executed at least once. If this is the 50dd5c1442SJohannes Doerfert // case we branch to PreHeaderBB and subsequently to the HeaderBB, which 51dd5c1442SJohannes Doerfert // contains the loop iv 'polly.indvar', the incremented loop iv 52dd5c1442SJohannes Doerfert // 'polly.indvar_next' as well as the condition to check if we execute another 53dd5c1442SJohannes Doerfert // iteration of the loop. After the loop has finished, we branch to ExitBB. 54*4fe342cbSHongbin Zheng // We expect the type of UB, LB, UB+Stride to be large enough for values that 55*4fe342cbSHongbin Zheng // UB may take throughout the execution of the loop, including the computation 56*4fe342cbSHongbin Zheng // of indvar + Stride before the final abort. 574ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, 582d950f36SPhilip Pfaffe PollyIRBuilder &Builder, LoopInfo &LI, 592ef3f4fdSJohannes Doerfert DominatorTree &DT, BasicBlock *&ExitBB, 6037c9b8e0STobias Grosser ICmpInst::Predicate Predicate, 6151d1c74dSJohannes Doerfert ScopAnnotator *Annotator, bool Parallel, 62dd5c1442SJohannes Doerfert bool UseGuard) { 634ac4e155SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 64609089f2SHongbin Zheng LLVMContext &Context = F->getContext(); 65609089f2SHongbin Zheng 665db6ffd7STobias Grosser assert(LB->getType() == UB->getType() && "Types of loop bounds do not match"); 67609089f2SHongbin Zheng IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); 68609089f2SHongbin Zheng assert(LoopIVType && "UB is not integer?"); 69609089f2SHongbin Zheng 705db6ffd7STobias Grosser BasicBlock *BeforeBB = Builder.GetInsertBlock(); 71dd5c1442SJohannes Doerfert BasicBlock *GuardBB = 72dd5c1442SJohannes Doerfert UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr; 735db6ffd7STobias Grosser BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); 745db6ffd7STobias Grosser BasicBlock *PreHeaderBB = 755db6ffd7STobias Grosser BasicBlock::Create(Context, "polly.loop_preheader", F); 76609089f2SHongbin Zheng 773081b0f5STobias Grosser // Update LoopInfo 783081b0f5STobias Grosser Loop *OuterLoop = LI.getLoopFor(BeforeBB); 793081b0f5STobias Grosser Loop *NewLoop = new Loop(); 803081b0f5STobias Grosser 81dd5c1442SJohannes Doerfert if (OuterLoop) 823081b0f5STobias Grosser OuterLoop->addChildLoop(NewLoop); 83dd5c1442SJohannes Doerfert else 843081b0f5STobias Grosser LI.addTopLevelLoop(NewLoop); 853081b0f5STobias Grosser 86154d9469STobias Grosser if (OuterLoop) { 87154d9469STobias Grosser if (GuardBB) 886adcf56bSChandler Carruth OuterLoop->addBasicBlockToLoop(GuardBB, LI); 896adcf56bSChandler Carruth OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI); 90154d9469STobias Grosser } 913081b0f5STobias Grosser 926adcf56bSChandler Carruth NewLoop->addBasicBlockToLoop(HeaderBB, LI); 933081b0f5STobias Grosser 94c7b719fcSJohannes Doerfert // Notify the annotator (if present) that we have a new loop, but only 95c7b719fcSJohannes Doerfert // after the header block is set. 96c7b719fcSJohannes Doerfert if (Annotator) 97c7b719fcSJohannes Doerfert Annotator->pushLoop(NewLoop, Parallel); 98c7b719fcSJohannes Doerfert 995db6ffd7STobias Grosser // ExitBB 100b8f58b53SDuncan P. N. Exon Smith ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI); 1015db6ffd7STobias Grosser ExitBB->setName("polly.loop_exit"); 102609089f2SHongbin Zheng 1035db6ffd7STobias Grosser // BeforeBB 104dd5c1442SJohannes Doerfert if (GuardBB) { 1055db6ffd7STobias Grosser BeforeBB->getTerminator()->setSuccessor(0, GuardBB); 106dd5c1442SJohannes Doerfert DT.addNewBlock(GuardBB, BeforeBB); 107609089f2SHongbin Zheng 1085db6ffd7STobias Grosser // GuardBB 1095db6ffd7STobias Grosser Builder.SetInsertPoint(GuardBB); 1105db6ffd7STobias Grosser Value *LoopGuard; 1115db6ffd7STobias Grosser LoopGuard = Builder.CreateICmp(Predicate, LB, UB); 1125db6ffd7STobias Grosser LoopGuard->setName("polly.loop_guard"); 1135db6ffd7STobias Grosser Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); 114dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, GuardBB); 115dd5c1442SJohannes Doerfert } else { 116dd5c1442SJohannes Doerfert BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); 117dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, BeforeBB); 118dd5c1442SJohannes Doerfert } 119609089f2SHongbin Zheng 1205db6ffd7STobias Grosser // PreHeaderBB 1215db6ffd7STobias Grosser Builder.SetInsertPoint(PreHeaderBB); 1224ac4e155SHongbin Zheng Builder.CreateBr(HeaderBB); 123609089f2SHongbin Zheng 1245db6ffd7STobias Grosser // HeaderBB 1255db6ffd7STobias Grosser DT.addNewBlock(HeaderBB, PreHeaderBB); 1265db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB); 1275db6ffd7STobias Grosser PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); 1285db6ffd7STobias Grosser IV->addIncoming(LB, PreHeaderBB); 1293717aa5dSTobias Grosser Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); 1305db6ffd7STobias Grosser Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); 131*4fe342cbSHongbin Zheng Value *LoopCondition = 132*4fe342cbSHongbin Zheng Builder.CreateICmp(Predicate, IncrementedIV, UB, "polly.loop_cond"); 133c7b719fcSJohannes Doerfert 134c7b719fcSJohannes Doerfert // Create the loop latch and annotate it as such. 135c7b719fcSJohannes Doerfert BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); 136c7b719fcSJohannes Doerfert if (Annotator) 137c7b719fcSJohannes Doerfert Annotator->annotateLoopLatch(B, NewLoop, Parallel); 138c7b719fcSJohannes Doerfert 1395db6ffd7STobias Grosser IV->addIncoming(IncrementedIV, HeaderBB); 140dd5c1442SJohannes Doerfert if (GuardBB) 1415db6ffd7STobias Grosser DT.changeImmediateDominator(ExitBB, GuardBB); 142dd5c1442SJohannes Doerfert else 143f8a678d2STobias Grosser DT.changeImmediateDominator(ExitBB, HeaderBB); 144609089f2SHongbin Zheng 1455db6ffd7STobias Grosser // The loop body should be added here. 1465db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); 147609089f2SHongbin Zheng return IV; 148609089f2SHongbin Zheng } 149609089f2SHongbin Zheng 15012b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createParallelLoop( 15112b355a2SJohannes Doerfert Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues, 152521dd584SJohannes Doerfert ValueMapT &Map, BasicBlock::iterator *LoopBody) { 15312b355a2SJohannes Doerfert Function *SubFn; 15412b355a2SJohannes Doerfert 155f0e3d50dSDavid Blaikie AllocaInst *Struct = storeValuesIntoStruct(UsedValues); 15612b355a2SJohannes Doerfert BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint(); 157f0e3d50dSDavid Blaikie Value *IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn); 15812b355a2SJohannes Doerfert *LoopBody = Builder.GetInsertPoint(); 159b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&*BeforeLoop); 16012b355a2SJohannes Doerfert 161f0e3d50dSDavid Blaikie Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), 16212b355a2SJohannes Doerfert "polly.par.userContext"); 16312b355a2SJohannes Doerfert 16412b355a2SJohannes Doerfert // Add one as the upper bound provided by openmp is a < comparison 16512b355a2SJohannes Doerfert // whereas the codegenForSequential function creates a <= comparison. 16612b355a2SJohannes Doerfert UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1)); 16712b355a2SJohannes Doerfert 16812b355a2SJohannes Doerfert // Tell the runtime we start a parallel loop 16912b355a2SJohannes Doerfert createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride); 17012b355a2SJohannes Doerfert Builder.CreateCall(SubFn, SubFnParam); 17112b355a2SJohannes Doerfert createCallJoinThreads(); 17212b355a2SJohannes Doerfert 17312b355a2SJohannes Doerfert return IV; 17412b355a2SJohannes Doerfert } 17512b355a2SJohannes Doerfert 17612b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn, 17712b355a2SJohannes Doerfert Value *SubFnParam, Value *LB, 17812b355a2SJohannes Doerfert Value *UB, Value *Stride) { 17912b355a2SJohannes Doerfert const std::string Name = "GOMP_parallel_loop_runtime_start"; 18012b355a2SJohannes Doerfert 181609089f2SHongbin Zheng Function *F = M->getFunction(Name); 182609089f2SHongbin Zheng 183609089f2SHongbin Zheng // If F is not available, declare it. 184609089f2SHongbin Zheng if (!F) { 185609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 186609089f2SHongbin Zheng 187c14582f2STobias Grosser Type *Params[] = {PointerType::getUnqual(FunctionType::get( 188c14582f2STobias Grosser Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), 189b6945e33STobias Grosser Builder.getInt8PtrTy(), 190b6945e33STobias Grosser Builder.getInt32Ty(), 191b6945e33STobias Grosser LongType, 192b6945e33STobias Grosser LongType, 193b6945e33STobias Grosser LongType}; 194609089f2SHongbin Zheng 195609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); 196609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 197609089f2SHongbin Zheng } 198609089f2SHongbin Zheng 199a4417835SJohannes Doerfert Value *NumberOfThreads = Builder.getInt32(PollyNumThreads); 200d4ea2f48STobias Grosser Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride}; 201609089f2SHongbin Zheng 202609089f2SHongbin Zheng Builder.CreateCall(F, Args); 203609089f2SHongbin Zheng } 204609089f2SHongbin Zheng 20512b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr, 20612b355a2SJohannes Doerfert Value *UBPtr) { 20712b355a2SJohannes Doerfert const std::string Name = "GOMP_loop_runtime_next"; 20812b355a2SJohannes Doerfert 209609089f2SHongbin Zheng Function *F = M->getFunction(Name); 210609089f2SHongbin Zheng 211609089f2SHongbin Zheng // If F is not available, declare it. 212609089f2SHongbin Zheng if (!F) { 213609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 21412b355a2SJohannes Doerfert Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()}; 215609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); 216609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 217609089f2SHongbin Zheng } 218609089f2SHongbin Zheng 21912b355a2SJohannes Doerfert Value *Args[] = {LBPtr, UBPtr}; 220609089f2SHongbin Zheng Value *Return = Builder.CreateCall(F, Args); 221c14582f2STobias Grosser Return = Builder.CreateICmpNE( 222c14582f2STobias Grosser Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); 223609089f2SHongbin Zheng return Return; 224609089f2SHongbin Zheng } 225609089f2SHongbin Zheng 22612b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallJoinThreads() { 22712b355a2SJohannes Doerfert const std::string Name = "GOMP_parallel_end"; 22812b355a2SJohannes Doerfert 229609089f2SHongbin Zheng Function *F = M->getFunction(Name); 230609089f2SHongbin Zheng 231609089f2SHongbin Zheng // If F is not available, declare it. 232609089f2SHongbin Zheng if (!F) { 233609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 234609089f2SHongbin Zheng 235609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 236609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 237609089f2SHongbin Zheng } 238609089f2SHongbin Zheng 2391128b365STobias Grosser Builder.CreateCall(F, {}); 240609089f2SHongbin Zheng } 241609089f2SHongbin Zheng 24212b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallCleanupThread() { 24312b355a2SJohannes Doerfert const std::string Name = "GOMP_loop_end_nowait"; 24412b355a2SJohannes Doerfert 245609089f2SHongbin Zheng Function *F = M->getFunction(Name); 246609089f2SHongbin Zheng 247609089f2SHongbin Zheng // If F is not available, declare it. 248609089f2SHongbin Zheng if (!F) { 249609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 250609089f2SHongbin Zheng 251609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 252609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 253609089f2SHongbin Zheng } 254609089f2SHongbin Zheng 2551128b365STobias Grosser Builder.CreateCall(F, {}); 256609089f2SHongbin Zheng } 257609089f2SHongbin Zheng 25812b355a2SJohannes Doerfert Function *ParallelLoopGenerator::createSubFnDefinition() { 259609089f2SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 260609089f2SHongbin Zheng std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); 261609089f2SHongbin Zheng FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); 26212b355a2SJohannes Doerfert Function *SubFn = Function::Create(FT, Function::InternalLinkage, 263a89dc57bSTobias Grosser F->getName() + "_polly_subfn", M); 264a89dc57bSTobias Grosser 265a89dc57bSTobias Grosser // Certain backends (e.g., NVPTX) do not support '.'s in function names. 266a89dc57bSTobias Grosser // Hence, we ensure that all '.'s are replaced by '_'s. 267a89dc57bSTobias Grosser std::string FunctionName = SubFn->getName(); 268a89dc57bSTobias Grosser std::replace(FunctionName.begin(), FunctionName.end(), '.', '_'); 269a89dc57bSTobias Grosser SubFn->setName(FunctionName); 27012b355a2SJohannes Doerfert 271609089f2SHongbin Zheng // Do not run any polly pass on the new function. 27212b355a2SJohannes Doerfert SubFn->addFnAttr(PollySkipFnAttr); 273609089f2SHongbin Zheng 27412b355a2SJohannes Doerfert Function::arg_iterator AI = SubFn->arg_begin(); 27512b355a2SJohannes Doerfert AI->setName("polly.par.userContext"); 276609089f2SHongbin Zheng 27712b355a2SJohannes Doerfert return SubFn; 278609089f2SHongbin Zheng } 279609089f2SHongbin Zheng 280f0e3d50dSDavid Blaikie AllocaInst * 28112b355a2SJohannes Doerfert ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) { 28212b355a2SJohannes Doerfert SmallVector<Type *, 8> Members; 283609089f2SHongbin Zheng 28491f5b262STobias Grosser for (Value *V : Values) 28591f5b262STobias Grosser Members.push_back(V->getType()); 286609089f2SHongbin Zheng 2877b5a4dfdSTobias Grosser const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout(); 288b3e30c32SMatt Arsenault 2891356ac75SJohannes Doerfert // We do not want to allocate the alloca inside any loop, thus we allocate it 2901356ac75SJohannes Doerfert // in the entry block of the function and use annotations to denote the actual 2911356ac75SJohannes Doerfert // live span (similar to clang). 2921356ac75SJohannes Doerfert BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock(); 293b8f58b53SDuncan P. N. Exon Smith Instruction *IP = &*EntryBB.getFirstInsertionPt(); 294609089f2SHongbin Zheng StructType *Ty = StructType::get(Builder.getContext(), Members); 295b3e30c32SMatt Arsenault AllocaInst *Struct = new AllocaInst(Ty, DL.getAllocaAddrSpace(), nullptr, 296b3e30c32SMatt Arsenault "polly.par.userContext", IP); 2971356ac75SJohannes Doerfert 298609089f2SHongbin Zheng for (unsigned i = 0; i < Values.size(); i++) { 299f0e3d50dSDavid Blaikie Value *Address = Builder.CreateStructGEP(Ty, Struct, i); 30095e59aaaSTobias Grosser Address->setName("polly.subfn.storeaddr." + Values[i]->getName()); 301609089f2SHongbin Zheng Builder.CreateStore(Values[i], Address); 302609089f2SHongbin Zheng } 303609089f2SHongbin Zheng 304609089f2SHongbin Zheng return Struct; 305609089f2SHongbin Zheng } 306609089f2SHongbin Zheng 30712b355a2SJohannes Doerfert void ParallelLoopGenerator::extractValuesFromStruct( 308521dd584SJohannes Doerfert SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) { 309609089f2SHongbin Zheng for (unsigned i = 0; i < OldValues.size(); i++) { 310f0e3d50dSDavid Blaikie Value *Address = Builder.CreateStructGEP(Ty, Struct, i); 311609089f2SHongbin Zheng Value *NewValue = Builder.CreateLoad(Address); 31272b80672STobias Grosser NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName()); 31312b355a2SJohannes Doerfert Map[OldValues[i]] = NewValue; 314609089f2SHongbin Zheng } 315609089f2SHongbin Zheng } 316609089f2SHongbin Zheng 317f0e3d50dSDavid Blaikie Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData, 318e602a076STobias Grosser SetVector<Value *> Data, 319521dd584SJohannes Doerfert ValueMapT &Map, Function **SubFnPtr) { 32012b355a2SJohannes Doerfert BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB; 32112b355a2SJohannes Doerfert Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV; 32212b355a2SJohannes Doerfert Function *SubFn = createSubFnDefinition(); 32312b355a2SJohannes Doerfert LLVMContext &Context = SubFn->getContext(); 324609089f2SHongbin Zheng 325609089f2SHongbin Zheng // Store the previous basic block. 326609089f2SHongbin Zheng PrevBB = Builder.GetInsertBlock(); 327609089f2SHongbin Zheng 328609089f2SHongbin Zheng // Create basic blocks. 32912b355a2SJohannes Doerfert HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn); 33012b355a2SJohannes Doerfert ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn); 33112b355a2SJohannes Doerfert CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn); 33212b355a2SJohannes Doerfert PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn); 333609089f2SHongbin Zheng 334609089f2SHongbin Zheng DT.addNewBlock(HeaderBB, PrevBB); 335609089f2SHongbin Zheng DT.addNewBlock(ExitBB, HeaderBB); 336609089f2SHongbin Zheng DT.addNewBlock(CheckNextBB, HeaderBB); 33712b355a2SJohannes Doerfert DT.addNewBlock(PreHeaderBB, HeaderBB); 338609089f2SHongbin Zheng 339609089f2SHongbin Zheng // Fill up basic block HeaderBB. 340609089f2SHongbin Zheng Builder.SetInsertPoint(HeaderBB); 3418dd653d9STobias Grosser LBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.LBPtr"); 3428dd653d9STobias Grosser UBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.UBPtr"); 343b8f58b53SDuncan P. N. Exon Smith UserContext = Builder.CreateBitCast( 344b8f58b53SDuncan P. N. Exon Smith &*SubFn->arg_begin(), StructData->getType(), "polly.par.userContext"); 345609089f2SHongbin Zheng 346f0e3d50dSDavid Blaikie extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext, 347f0e3d50dSDavid Blaikie Map); 348609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 349609089f2SHongbin Zheng 350609089f2SHongbin Zheng // Add code to check if another set of iterations will be executed. 351609089f2SHongbin Zheng Builder.SetInsertPoint(CheckNextBB); 35212b355a2SJohannes Doerfert Ret1 = createCallGetWorkItem(LBPtr, UBPtr); 353609089f2SHongbin Zheng HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), 35412b355a2SJohannes Doerfert "polly.par.hasNextScheduleBlock"); 35512b355a2SJohannes Doerfert Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB); 356609089f2SHongbin Zheng 357d8e3c8c6STobias Grosser // Add code to load the iv bounds for this set of iterations. 35812b355a2SJohannes Doerfert Builder.SetInsertPoint(PreHeaderBB); 35912b355a2SJohannes Doerfert LB = Builder.CreateLoad(LBPtr, "polly.par.LB"); 36012b355a2SJohannes Doerfert UB = Builder.CreateLoad(UBPtr, "polly.par.UB"); 361609089f2SHongbin Zheng 362609089f2SHongbin Zheng // Subtract one as the upper bound provided by openmp is a < comparison 363609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 36412b355a2SJohannes Doerfert UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1), 36512b355a2SJohannes Doerfert "polly.par.UBAdjusted"); 366609089f2SHongbin Zheng 367609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 368b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&*--Builder.GetInsertPoint()); 369447f175eSPhilip Pfaffe IV = createLoop(LB, UB, Stride, Builder, LI, DT, AfterBB, ICmpInst::ICMP_SLE, 370447f175eSPhilip Pfaffe nullptr, true, /* UseGuard */ false); 371609089f2SHongbin Zheng 372609089f2SHongbin Zheng BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); 373609089f2SHongbin Zheng 37412b355a2SJohannes Doerfert // Add code to terminate this subfunction. 375609089f2SHongbin Zheng Builder.SetInsertPoint(ExitBB); 37612b355a2SJohannes Doerfert createCallCleanupThread(); 377609089f2SHongbin Zheng Builder.CreateRetVoid(); 378609089f2SHongbin Zheng 379b8f58b53SDuncan P. N. Exon Smith Builder.SetInsertPoint(&*LoopBody); 38012b355a2SJohannes Doerfert *SubFnPtr = SubFn; 381609089f2SHongbin Zheng 382609089f2SHongbin Zheng return IV; 383609089f2SHongbin Zheng } 384