1609089f2SHongbin Zheng //===------ LoopGenerators.cpp -  IR helper to create loops ---------------===//
2609089f2SHongbin Zheng //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6609089f2SHongbin Zheng //
7609089f2SHongbin Zheng //===----------------------------------------------------------------------===//
8609089f2SHongbin Zheng //
912b355a2SJohannes Doerfert // This file contains functions to create scalar and parallel loops as LLVM-IR.
10609089f2SHongbin Zheng //
11609089f2SHongbin Zheng //===----------------------------------------------------------------------===//
12609089f2SHongbin Zheng 
138a846610SHongbin Zheng #include "polly/CodeGen/LoopGenerators.h"
145624d3c9STobias Grosser #include "polly/ScopDetection.h"
153081b0f5STobias Grosser #include "llvm/Analysis/LoopInfo.h"
16535d52c7SChandler Carruth #include "llvm/IR/DataLayout.h"
17e87c6a81SChandler Carruth #include "llvm/IR/Dominators.h"
1883628182STobias Grosser #include "llvm/IR/Module.h"
19990cd4c2SJohannes Doerfert #include "llvm/Support/CommandLine.h"
20ba0d0922STobias Grosser #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21609089f2SHongbin Zheng 
22609089f2SHongbin Zheng using namespace llvm;
234ac4e155SHongbin Zheng using namespace polly;
24609089f2SHongbin Zheng 
25990cd4c2SJohannes Doerfert static cl::opt<int>
26990cd4c2SJohannes Doerfert     PollyNumThreads("polly-num-threads",
27990cd4c2SJohannes Doerfert                     cl::desc("Number of threads to use (0 = auto)"), cl::Hidden,
28990cd4c2SJohannes Doerfert                     cl::init(0));
29990cd4c2SJohannes Doerfert 
30dd5c1442SJohannes Doerfert // We generate a loop of either of the following structures:
315db6ffd7STobias Grosser //
32dd5c1442SJohannes Doerfert //              BeforeBB                      BeforeBB
33dd5c1442SJohannes Doerfert //                 |                             |
34dd5c1442SJohannes Doerfert //                 v                             v
35dd5c1442SJohannes Doerfert //              GuardBB                      PreHeaderBB
36dd5c1442SJohannes Doerfert //              /      |                         |   _____
37dd5c1442SJohannes Doerfert //     __  PreHeaderBB  |                        v  \/    |
38dd5c1442SJohannes Doerfert //    /  \    /         |                     HeaderBB  latch
39dd5c1442SJohannes Doerfert // latch  HeaderBB      |                        |\       |
40dd5c1442SJohannes Doerfert //    \  /    \         /                        | \------/
41dd5c1442SJohannes Doerfert //     <       \       /                         |
42dd5c1442SJohannes Doerfert //              \     /                          v
43dd5c1442SJohannes Doerfert //              ExitBB                         ExitBB
445db6ffd7STobias Grosser //
45dd5c1442SJohannes Doerfert // depending on whether or not we know that it is executed at least once. If
46dd5c1442SJohannes Doerfert // not, GuardBB checks if the loop is executed at least once. If this is the
47dd5c1442SJohannes Doerfert // case we branch to PreHeaderBB and subsequently to the HeaderBB, which
48dd5c1442SJohannes Doerfert // contains the loop iv 'polly.indvar', the incremented loop iv
49dd5c1442SJohannes Doerfert // 'polly.indvar_next' as well as the condition to check if we execute another
50dd5c1442SJohannes Doerfert // iteration of the loop. After the loop has finished, we branch to ExitBB.
514fe342cbSHongbin Zheng // We expect the type of UB, LB, UB+Stride to be large enough for values that
524fe342cbSHongbin Zheng // UB may take throughout the execution of the loop, including the computation
534fe342cbSHongbin Zheng // of indvar + Stride before the final abort.
544ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
552d950f36SPhilip Pfaffe                          PollyIRBuilder &Builder, LoopInfo &LI,
562ef3f4fdSJohannes Doerfert                          DominatorTree &DT, BasicBlock *&ExitBB,
5737c9b8e0STobias Grosser                          ICmpInst::Predicate Predicate,
580956a606SRoman Gareev                          ScopAnnotator *Annotator, bool Parallel, bool UseGuard,
590956a606SRoman Gareev                          bool LoopVectDisabled) {
604ac4e155SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
61609089f2SHongbin Zheng   LLVMContext &Context = F->getContext();
62609089f2SHongbin Zheng 
635db6ffd7STobias Grosser   assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
64609089f2SHongbin Zheng   IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
65609089f2SHongbin Zheng   assert(LoopIVType && "UB is not integer?");
66609089f2SHongbin Zheng 
675db6ffd7STobias Grosser   BasicBlock *BeforeBB = Builder.GetInsertBlock();
68dd5c1442SJohannes Doerfert   BasicBlock *GuardBB =
69dd5c1442SJohannes Doerfert       UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
705db6ffd7STobias Grosser   BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
715db6ffd7STobias Grosser   BasicBlock *PreHeaderBB =
725db6ffd7STobias Grosser       BasicBlock::Create(Context, "polly.loop_preheader", F);
73609089f2SHongbin Zheng 
743081b0f5STobias Grosser   // Update LoopInfo
753081b0f5STobias Grosser   Loop *OuterLoop = LI.getLoopFor(BeforeBB);
76859ef1c0SPhilip Pfaffe   Loop *NewLoop = LI.AllocateLoop();
773081b0f5STobias Grosser 
78dd5c1442SJohannes Doerfert   if (OuterLoop)
793081b0f5STobias Grosser     OuterLoop->addChildLoop(NewLoop);
80dd5c1442SJohannes Doerfert   else
813081b0f5STobias Grosser     LI.addTopLevelLoop(NewLoop);
823081b0f5STobias Grosser 
83154d9469STobias Grosser   if (OuterLoop) {
84154d9469STobias Grosser     if (GuardBB)
856adcf56bSChandler Carruth       OuterLoop->addBasicBlockToLoop(GuardBB, LI);
866adcf56bSChandler Carruth     OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI);
87154d9469STobias Grosser   }
883081b0f5STobias Grosser 
896adcf56bSChandler Carruth   NewLoop->addBasicBlockToLoop(HeaderBB, LI);
903081b0f5STobias Grosser 
91c7b719fcSJohannes Doerfert   // Notify the annotator (if present) that we have a new loop, but only
92c7b719fcSJohannes Doerfert   // after the header block is set.
93c7b719fcSJohannes Doerfert   if (Annotator)
94c7b719fcSJohannes Doerfert     Annotator->pushLoop(NewLoop, Parallel);
95c7b719fcSJohannes Doerfert 
965db6ffd7STobias Grosser   // ExitBB
97b8f58b53SDuncan P. N. Exon Smith   ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI);
985db6ffd7STobias Grosser   ExitBB->setName("polly.loop_exit");
99609089f2SHongbin Zheng 
1005db6ffd7STobias Grosser   // BeforeBB
101dd5c1442SJohannes Doerfert   if (GuardBB) {
1025db6ffd7STobias Grosser     BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
103dd5c1442SJohannes Doerfert     DT.addNewBlock(GuardBB, BeforeBB);
104609089f2SHongbin Zheng 
1055db6ffd7STobias Grosser     // GuardBB
1065db6ffd7STobias Grosser     Builder.SetInsertPoint(GuardBB);
1075db6ffd7STobias Grosser     Value *LoopGuard;
1085db6ffd7STobias Grosser     LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
1095db6ffd7STobias Grosser     LoopGuard->setName("polly.loop_guard");
1105db6ffd7STobias Grosser     Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
111dd5c1442SJohannes Doerfert     DT.addNewBlock(PreHeaderBB, GuardBB);
112dd5c1442SJohannes Doerfert   } else {
113dd5c1442SJohannes Doerfert     BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
114dd5c1442SJohannes Doerfert     DT.addNewBlock(PreHeaderBB, BeforeBB);
115dd5c1442SJohannes Doerfert   }
116609089f2SHongbin Zheng 
1175db6ffd7STobias Grosser   // PreHeaderBB
1185db6ffd7STobias Grosser   Builder.SetInsertPoint(PreHeaderBB);
1194ac4e155SHongbin Zheng   Builder.CreateBr(HeaderBB);
120609089f2SHongbin Zheng 
1215db6ffd7STobias Grosser   // HeaderBB
1225db6ffd7STobias Grosser   DT.addNewBlock(HeaderBB, PreHeaderBB);
1235db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB);
1245db6ffd7STobias Grosser   PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
1255db6ffd7STobias Grosser   IV->addIncoming(LB, PreHeaderBB);
1263717aa5dSTobias Grosser   Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
1275db6ffd7STobias Grosser   Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
1284fe342cbSHongbin Zheng   Value *LoopCondition =
1294fe342cbSHongbin Zheng       Builder.CreateICmp(Predicate, IncrementedIV, UB, "polly.loop_cond");
130c7b719fcSJohannes Doerfert 
131c7b719fcSJohannes Doerfert   // Create the loop latch and annotate it as such.
132c7b719fcSJohannes Doerfert   BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
133c7b719fcSJohannes Doerfert   if (Annotator)
1340956a606SRoman Gareev     Annotator->annotateLoopLatch(B, NewLoop, Parallel, LoopVectDisabled);
135c7b719fcSJohannes Doerfert 
1365db6ffd7STobias Grosser   IV->addIncoming(IncrementedIV, HeaderBB);
137dd5c1442SJohannes Doerfert   if (GuardBB)
1385db6ffd7STobias Grosser     DT.changeImmediateDominator(ExitBB, GuardBB);
139dd5c1442SJohannes Doerfert   else
140f8a678d2STobias Grosser     DT.changeImmediateDominator(ExitBB, HeaderBB);
141609089f2SHongbin Zheng 
1425db6ffd7STobias Grosser   // The loop body should be added here.
1435db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
144609089f2SHongbin Zheng   return IV;
145609089f2SHongbin Zheng }
146609089f2SHongbin Zheng 
14712b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createParallelLoop(
14812b355a2SJohannes Doerfert     Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues,
149521dd584SJohannes Doerfert     ValueMapT &Map, BasicBlock::iterator *LoopBody) {
15012b355a2SJohannes Doerfert   Function *SubFn;
15112b355a2SJohannes Doerfert 
152f0e3d50dSDavid Blaikie   AllocaInst *Struct = storeValuesIntoStruct(UsedValues);
15312b355a2SJohannes Doerfert   BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint();
154f0e3d50dSDavid Blaikie   Value *IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn);
15512b355a2SJohannes Doerfert   *LoopBody = Builder.GetInsertPoint();
156b8f58b53SDuncan P. N. Exon Smith   Builder.SetInsertPoint(&*BeforeLoop);
15712b355a2SJohannes Doerfert 
158f0e3d50dSDavid Blaikie   Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
15912b355a2SJohannes Doerfert                                             "polly.par.userContext");
16012b355a2SJohannes Doerfert 
161a6d48f59SMichael Kruse   // Add one as the upper bound provided by OpenMP is a < comparison
16212b355a2SJohannes Doerfert   // whereas the codegenForSequential function creates a <= comparison.
16312b355a2SJohannes Doerfert   UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1));
16412b355a2SJohannes Doerfert 
16512b355a2SJohannes Doerfert   // Tell the runtime we start a parallel loop
16612b355a2SJohannes Doerfert   createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride);
16712b355a2SJohannes Doerfert   Builder.CreateCall(SubFn, SubFnParam);
16812b355a2SJohannes Doerfert   createCallJoinThreads();
16912b355a2SJohannes Doerfert 
17012b355a2SJohannes Doerfert   return IV;
17112b355a2SJohannes Doerfert }
17212b355a2SJohannes Doerfert 
17312b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn,
17412b355a2SJohannes Doerfert                                                    Value *SubFnParam, Value *LB,
17512b355a2SJohannes Doerfert                                                    Value *UB, Value *Stride) {
17612b355a2SJohannes Doerfert   const std::string Name = "GOMP_parallel_loop_runtime_start";
17712b355a2SJohannes Doerfert 
178609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
179609089f2SHongbin Zheng 
180609089f2SHongbin Zheng   // If F is not available, declare it.
181609089f2SHongbin Zheng   if (!F) {
182609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
183609089f2SHongbin Zheng 
184c14582f2STobias Grosser     Type *Params[] = {PointerType::getUnqual(FunctionType::get(
185c14582f2STobias Grosser                           Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
186b6945e33STobias Grosser                       Builder.getInt8PtrTy(),
187b6945e33STobias Grosser                       Builder.getInt32Ty(),
188b6945e33STobias Grosser                       LongType,
189b6945e33STobias Grosser                       LongType,
190b6945e33STobias Grosser                       LongType};
191609089f2SHongbin Zheng 
192609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
193609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
194609089f2SHongbin Zheng   }
195609089f2SHongbin Zheng 
196a4417835SJohannes Doerfert   Value *NumberOfThreads = Builder.getInt32(PollyNumThreads);
197d4ea2f48STobias Grosser   Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride};
198609089f2SHongbin Zheng 
199609089f2SHongbin Zheng   Builder.CreateCall(F, Args);
200609089f2SHongbin Zheng }
201609089f2SHongbin Zheng 
20212b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr,
20312b355a2SJohannes Doerfert                                                     Value *UBPtr) {
20412b355a2SJohannes Doerfert   const std::string Name = "GOMP_loop_runtime_next";
20512b355a2SJohannes Doerfert 
206609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
207609089f2SHongbin Zheng 
208609089f2SHongbin Zheng   // If F is not available, declare it.
209609089f2SHongbin Zheng   if (!F) {
210609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
21112b355a2SJohannes Doerfert     Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()};
212609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
213609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
214609089f2SHongbin Zheng   }
215609089f2SHongbin Zheng 
21612b355a2SJohannes Doerfert   Value *Args[] = {LBPtr, UBPtr};
217609089f2SHongbin Zheng   Value *Return = Builder.CreateCall(F, Args);
218c14582f2STobias Grosser   Return = Builder.CreateICmpNE(
219c14582f2STobias Grosser       Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
220609089f2SHongbin Zheng   return Return;
221609089f2SHongbin Zheng }
222609089f2SHongbin Zheng 
22312b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallJoinThreads() {
22412b355a2SJohannes Doerfert   const std::string Name = "GOMP_parallel_end";
22512b355a2SJohannes Doerfert 
226609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
227609089f2SHongbin Zheng 
228609089f2SHongbin Zheng   // If F is not available, declare it.
229609089f2SHongbin Zheng   if (!F) {
230609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
231609089f2SHongbin Zheng 
232609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
233609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
234609089f2SHongbin Zheng   }
235609089f2SHongbin Zheng 
2361128b365STobias Grosser   Builder.CreateCall(F, {});
237609089f2SHongbin Zheng }
238609089f2SHongbin Zheng 
23912b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallCleanupThread() {
24012b355a2SJohannes Doerfert   const std::string Name = "GOMP_loop_end_nowait";
24112b355a2SJohannes Doerfert 
242609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
243609089f2SHongbin Zheng 
244609089f2SHongbin Zheng   // If F is not available, declare it.
245609089f2SHongbin Zheng   if (!F) {
246609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
247609089f2SHongbin Zheng 
248609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
249609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
250609089f2SHongbin Zheng   }
251609089f2SHongbin Zheng 
2521128b365STobias Grosser   Builder.CreateCall(F, {});
253609089f2SHongbin Zheng }
254609089f2SHongbin Zheng 
25512b355a2SJohannes Doerfert Function *ParallelLoopGenerator::createSubFnDefinition() {
256609089f2SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
257609089f2SHongbin Zheng   std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
258609089f2SHongbin Zheng   FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
25912b355a2SJohannes Doerfert   Function *SubFn = Function::Create(FT, Function::InternalLinkage,
260a89dc57bSTobias Grosser                                      F->getName() + "_polly_subfn", M);
261a89dc57bSTobias Grosser 
262a89dc57bSTobias Grosser   // Certain backends (e.g., NVPTX) do not support '.'s in function names.
263a89dc57bSTobias Grosser   // Hence, we ensure that all '.'s are replaced by '_'s.
264a89dc57bSTobias Grosser   std::string FunctionName = SubFn->getName();
265a89dc57bSTobias Grosser   std::replace(FunctionName.begin(), FunctionName.end(), '.', '_');
266a89dc57bSTobias Grosser   SubFn->setName(FunctionName);
26712b355a2SJohannes Doerfert 
268609089f2SHongbin Zheng   // Do not run any polly pass on the new function.
26912b355a2SJohannes Doerfert   SubFn->addFnAttr(PollySkipFnAttr);
270609089f2SHongbin Zheng 
27112b355a2SJohannes Doerfert   Function::arg_iterator AI = SubFn->arg_begin();
27212b355a2SJohannes Doerfert   AI->setName("polly.par.userContext");
273609089f2SHongbin Zheng 
27412b355a2SJohannes Doerfert   return SubFn;
275609089f2SHongbin Zheng }
276609089f2SHongbin Zheng 
277f0e3d50dSDavid Blaikie AllocaInst *
27812b355a2SJohannes Doerfert ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) {
27912b355a2SJohannes Doerfert   SmallVector<Type *, 8> Members;
280609089f2SHongbin Zheng 
28191f5b262STobias Grosser   for (Value *V : Values)
28291f5b262STobias Grosser     Members.push_back(V->getType());
283609089f2SHongbin Zheng 
2847b5a4dfdSTobias Grosser   const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout();
285b3e30c32SMatt Arsenault 
2861356ac75SJohannes Doerfert   // We do not want to allocate the alloca inside any loop, thus we allocate it
2871356ac75SJohannes Doerfert   // in the entry block of the function and use annotations to denote the actual
2881356ac75SJohannes Doerfert   // live span (similar to clang).
2891356ac75SJohannes Doerfert   BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock();
290b8f58b53SDuncan P. N. Exon Smith   Instruction *IP = &*EntryBB.getFirstInsertionPt();
291609089f2SHongbin Zheng   StructType *Ty = StructType::get(Builder.getContext(), Members);
292b3e30c32SMatt Arsenault   AllocaInst *Struct = new AllocaInst(Ty, DL.getAllocaAddrSpace(), nullptr,
293b3e30c32SMatt Arsenault                                       "polly.par.userContext", IP);
2941356ac75SJohannes Doerfert 
295609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++) {
296f0e3d50dSDavid Blaikie     Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
29795e59aaaSTobias Grosser     Address->setName("polly.subfn.storeaddr." + Values[i]->getName());
298609089f2SHongbin Zheng     Builder.CreateStore(Values[i], Address);
299609089f2SHongbin Zheng   }
300609089f2SHongbin Zheng 
301609089f2SHongbin Zheng   return Struct;
302609089f2SHongbin Zheng }
303609089f2SHongbin Zheng 
30412b355a2SJohannes Doerfert void ParallelLoopGenerator::extractValuesFromStruct(
305521dd584SJohannes Doerfert     SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) {
306609089f2SHongbin Zheng   for (unsigned i = 0; i < OldValues.size(); i++) {
307f0e3d50dSDavid Blaikie     Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
308609089f2SHongbin Zheng     Value *NewValue = Builder.CreateLoad(Address);
30972b80672STobias Grosser     NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName());
31012b355a2SJohannes Doerfert     Map[OldValues[i]] = NewValue;
311609089f2SHongbin Zheng   }
312609089f2SHongbin Zheng }
313609089f2SHongbin Zheng 
314f0e3d50dSDavid Blaikie Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData,
315e602a076STobias Grosser                                           SetVector<Value *> Data,
316521dd584SJohannes Doerfert                                           ValueMapT &Map, Function **SubFnPtr) {
31712b355a2SJohannes Doerfert   BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB;
31812b355a2SJohannes Doerfert   Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV;
31912b355a2SJohannes Doerfert   Function *SubFn = createSubFnDefinition();
32012b355a2SJohannes Doerfert   LLVMContext &Context = SubFn->getContext();
321609089f2SHongbin Zheng 
322609089f2SHongbin Zheng   // Store the previous basic block.
323609089f2SHongbin Zheng   PrevBB = Builder.GetInsertBlock();
324609089f2SHongbin Zheng 
325609089f2SHongbin Zheng   // Create basic blocks.
32612b355a2SJohannes Doerfert   HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn);
32712b355a2SJohannes Doerfert   ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn);
32812b355a2SJohannes Doerfert   CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn);
32912b355a2SJohannes Doerfert   PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn);
330609089f2SHongbin Zheng 
331609089f2SHongbin Zheng   DT.addNewBlock(HeaderBB, PrevBB);
332609089f2SHongbin Zheng   DT.addNewBlock(ExitBB, HeaderBB);
333609089f2SHongbin Zheng   DT.addNewBlock(CheckNextBB, HeaderBB);
33412b355a2SJohannes Doerfert   DT.addNewBlock(PreHeaderBB, HeaderBB);
335609089f2SHongbin Zheng 
336609089f2SHongbin Zheng   // Fill up basic block HeaderBB.
337609089f2SHongbin Zheng   Builder.SetInsertPoint(HeaderBB);
3388dd653d9STobias Grosser   LBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.LBPtr");
3398dd653d9STobias Grosser   UBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.UBPtr");
340b8f58b53SDuncan P. N. Exon Smith   UserContext = Builder.CreateBitCast(
341b8f58b53SDuncan P. N. Exon Smith       &*SubFn->arg_begin(), StructData->getType(), "polly.par.userContext");
342609089f2SHongbin Zheng 
343f0e3d50dSDavid Blaikie   extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext,
344f0e3d50dSDavid Blaikie                           Map);
345609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
346609089f2SHongbin Zheng 
347609089f2SHongbin Zheng   // Add code to check if another set of iterations will be executed.
348609089f2SHongbin Zheng   Builder.SetInsertPoint(CheckNextBB);
34912b355a2SJohannes Doerfert   Ret1 = createCallGetWorkItem(LBPtr, UBPtr);
350609089f2SHongbin Zheng   HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
35112b355a2SJohannes Doerfert                                         "polly.par.hasNextScheduleBlock");
35212b355a2SJohannes Doerfert   Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB);
353609089f2SHongbin Zheng 
354d8e3c8c6STobias Grosser   // Add code to load the iv bounds for this set of iterations.
35512b355a2SJohannes Doerfert   Builder.SetInsertPoint(PreHeaderBB);
35612b355a2SJohannes Doerfert   LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
35712b355a2SJohannes Doerfert   UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
358609089f2SHongbin Zheng 
359a6d48f59SMichael Kruse   // Subtract one as the upper bound provided by OpenMP is a < comparison
360609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
36112b355a2SJohannes Doerfert   UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1),
36212b355a2SJohannes Doerfert                          "polly.par.UBAdjusted");
363609089f2SHongbin Zheng 
364609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
365b8f58b53SDuncan P. N. Exon Smith   Builder.SetInsertPoint(&*--Builder.GetInsertPoint());
366447f175eSPhilip Pfaffe   IV = createLoop(LB, UB, Stride, Builder, LI, DT, AfterBB, ICmpInst::ICMP_SLE,
367447f175eSPhilip Pfaffe                   nullptr, true, /* UseGuard */ false);
368609089f2SHongbin Zheng 
369609089f2SHongbin Zheng   BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
370609089f2SHongbin Zheng 
37112b355a2SJohannes Doerfert   // Add code to terminate this subfunction.
372609089f2SHongbin Zheng   Builder.SetInsertPoint(ExitBB);
37312b355a2SJohannes Doerfert   createCallCleanupThread();
374609089f2SHongbin Zheng   Builder.CreateRetVoid();
375609089f2SHongbin Zheng 
376b8f58b53SDuncan P. N. Exon Smith   Builder.SetInsertPoint(&*LoopBody);
37712b355a2SJohannes Doerfert   *SubFnPtr = SubFn;
378609089f2SHongbin Zheng 
379609089f2SHongbin Zheng   return IV;
380609089f2SHongbin Zheng }
381