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 
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"
20990cd4c2SJohannes Doerfert #include "llvm/Support/CommandLine.h"
21ba0d0922STobias Grosser #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22609089f2SHongbin Zheng 
23609089f2SHongbin Zheng using namespace llvm;
244ac4e155SHongbin Zheng using namespace polly;
25609089f2SHongbin Zheng 
26990cd4c2SJohannes Doerfert static cl::opt<int>
27990cd4c2SJohannes Doerfert     PollyNumThreads("polly-num-threads",
28990cd4c2SJohannes Doerfert                     cl::desc("Number of threads to use (0 = auto)"), cl::Hidden,
29990cd4c2SJohannes Doerfert                     cl::init(0));
30990cd4c2SJohannes Doerfert 
31dd5c1442SJohannes Doerfert // We generate a loop of either of the following structures:
325db6ffd7STobias Grosser //
33dd5c1442SJohannes Doerfert //              BeforeBB                      BeforeBB
34dd5c1442SJohannes Doerfert //                 |                             |
35dd5c1442SJohannes Doerfert //                 v                             v
36dd5c1442SJohannes Doerfert //              GuardBB                      PreHeaderBB
37dd5c1442SJohannes Doerfert //              /      |                         |   _____
38dd5c1442SJohannes Doerfert //     __  PreHeaderBB  |                        v  \/    |
39dd5c1442SJohannes Doerfert //    /  \    /         |                     HeaderBB  latch
40dd5c1442SJohannes Doerfert // latch  HeaderBB      |                        |\       |
41dd5c1442SJohannes Doerfert //    \  /    \         /                        | \------/
42dd5c1442SJohannes Doerfert //     <       \       /                         |
43dd5c1442SJohannes Doerfert //              \     /                          v
44dd5c1442SJohannes Doerfert //              ExitBB                         ExitBB
455db6ffd7STobias Grosser //
46dd5c1442SJohannes Doerfert // depending on whether or not we know that it is executed at least once. If
47dd5c1442SJohannes Doerfert // not, GuardBB checks if the loop is executed at least once. If this is the
48dd5c1442SJohannes Doerfert // case we branch to PreHeaderBB and subsequently to the HeaderBB, which
49dd5c1442SJohannes Doerfert // contains the loop iv 'polly.indvar', the incremented loop iv
50dd5c1442SJohannes Doerfert // 'polly.indvar_next' as well as the condition to check if we execute another
51dd5c1442SJohannes Doerfert // iteration of the loop. After the loop has finished, we branch to ExitBB.
524ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
532ef3f4fdSJohannes Doerfert                          PollyIRBuilder &Builder, Pass *P, LoopInfo &LI,
542ef3f4fdSJohannes Doerfert                          DominatorTree &DT, BasicBlock *&ExitBB,
5537c9b8e0STobias Grosser                          ICmpInst::Predicate Predicate,
5651d1c74dSJohannes Doerfert                          ScopAnnotator *Annotator, bool Parallel,
57dd5c1442SJohannes Doerfert                          bool UseGuard) {
584ac4e155SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
59609089f2SHongbin Zheng   LLVMContext &Context = F->getContext();
60609089f2SHongbin Zheng 
615db6ffd7STobias Grosser   assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
62609089f2SHongbin Zheng   IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
63609089f2SHongbin Zheng   assert(LoopIVType && "UB is not integer?");
64609089f2SHongbin Zheng 
655db6ffd7STobias Grosser   BasicBlock *BeforeBB = Builder.GetInsertBlock();
66dd5c1442SJohannes Doerfert   BasicBlock *GuardBB =
67dd5c1442SJohannes Doerfert       UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
685db6ffd7STobias Grosser   BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
695db6ffd7STobias Grosser   BasicBlock *PreHeaderBB =
705db6ffd7STobias Grosser       BasicBlock::Create(Context, "polly.loop_preheader", F);
71609089f2SHongbin Zheng 
723081b0f5STobias Grosser   // Update LoopInfo
733081b0f5STobias Grosser   Loop *OuterLoop = LI.getLoopFor(BeforeBB);
743081b0f5STobias Grosser   Loop *NewLoop = new Loop();
753081b0f5STobias Grosser 
76dd5c1442SJohannes Doerfert   if (OuterLoop)
773081b0f5STobias Grosser     OuterLoop->addChildLoop(NewLoop);
78dd5c1442SJohannes Doerfert   else
793081b0f5STobias Grosser     LI.addTopLevelLoop(NewLoop);
803081b0f5STobias Grosser 
81154d9469STobias Grosser   if (OuterLoop) {
82154d9469STobias Grosser     if (GuardBB)
836adcf56bSChandler Carruth       OuterLoop->addBasicBlockToLoop(GuardBB, LI);
846adcf56bSChandler Carruth     OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI);
85154d9469STobias Grosser   }
863081b0f5STobias Grosser 
876adcf56bSChandler Carruth   NewLoop->addBasicBlockToLoop(HeaderBB, LI);
883081b0f5STobias Grosser 
89c7b719fcSJohannes Doerfert   // Notify the annotator (if present) that we have a new loop, but only
90c7b719fcSJohannes Doerfert   // after the header block is set.
91c7b719fcSJohannes Doerfert   if (Annotator)
92c7b719fcSJohannes Doerfert     Annotator->pushLoop(NewLoop, Parallel);
93c7b719fcSJohannes Doerfert 
945db6ffd7STobias Grosser   // ExitBB
955ec3333dSChandler Carruth   ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, &DT, &LI);
965db6ffd7STobias Grosser   ExitBB->setName("polly.loop_exit");
97609089f2SHongbin Zheng 
985db6ffd7STobias Grosser   // BeforeBB
99dd5c1442SJohannes Doerfert   if (GuardBB) {
1005db6ffd7STobias Grosser     BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
101dd5c1442SJohannes Doerfert     DT.addNewBlock(GuardBB, BeforeBB);
102609089f2SHongbin Zheng 
1035db6ffd7STobias Grosser     // GuardBB
1045db6ffd7STobias Grosser     Builder.SetInsertPoint(GuardBB);
1055db6ffd7STobias Grosser     Value *LoopGuard;
1065db6ffd7STobias Grosser     LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
1075db6ffd7STobias Grosser     LoopGuard->setName("polly.loop_guard");
1085db6ffd7STobias Grosser     Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
109dd5c1442SJohannes Doerfert     DT.addNewBlock(PreHeaderBB, GuardBB);
110dd5c1442SJohannes Doerfert   } else {
111dd5c1442SJohannes Doerfert     BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
112dd5c1442SJohannes Doerfert     DT.addNewBlock(PreHeaderBB, BeforeBB);
113dd5c1442SJohannes Doerfert   }
114609089f2SHongbin Zheng 
1155db6ffd7STobias Grosser   // PreHeaderBB
1165db6ffd7STobias Grosser   Builder.SetInsertPoint(PreHeaderBB);
1174ac4e155SHongbin Zheng   Builder.CreateBr(HeaderBB);
118609089f2SHongbin Zheng 
1195db6ffd7STobias Grosser   // HeaderBB
1205db6ffd7STobias Grosser   DT.addNewBlock(HeaderBB, PreHeaderBB);
1215db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB);
1225db6ffd7STobias Grosser   PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
1235db6ffd7STobias Grosser   IV->addIncoming(LB, PreHeaderBB);
1245db6ffd7STobias Grosser   Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
1255db6ffd7STobias Grosser   Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
1265db6ffd7STobias Grosser   Value *LoopCondition;
1275db6ffd7STobias Grosser   UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
1285db6ffd7STobias Grosser   LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
1295db6ffd7STobias Grosser   LoopCondition->setName("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)
134c7b719fcSJohannes Doerfert     Annotator->annotateLoopLatch(B, NewLoop, Parallel);
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,
14912b355a2SJohannes Doerfert     ValueToValueMapTy &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();
15612b355a2SJohannes Doerfert   Builder.SetInsertPoint(BeforeLoop);
15712b355a2SJohannes Doerfert 
158f0e3d50dSDavid Blaikie   Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
15912b355a2SJohannes Doerfert                                             "polly.par.userContext");
16012b355a2SJohannes Doerfert 
16112b355a2SJohannes Doerfert   // 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 
1701356ac75SJohannes Doerfert   // Mark the end of the lifetime for the parameter struct.
1711356ac75SJohannes Doerfert   Type *Ty = Struct->getType();
1721356ac75SJohannes Doerfert   ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
1731356ac75SJohannes Doerfert   Builder.CreateLifetimeEnd(Struct, SizeOf);
1741356ac75SJohannes Doerfert 
17512b355a2SJohannes Doerfert   return IV;
17612b355a2SJohannes Doerfert }
17712b355a2SJohannes Doerfert 
17812b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn,
17912b355a2SJohannes Doerfert                                                    Value *SubFnParam, Value *LB,
18012b355a2SJohannes Doerfert                                                    Value *UB, Value *Stride) {
18112b355a2SJohannes Doerfert   const std::string Name = "GOMP_parallel_loop_runtime_start";
18212b355a2SJohannes Doerfert 
183609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
184609089f2SHongbin Zheng 
185609089f2SHongbin Zheng   // If F is not available, declare it.
186609089f2SHongbin Zheng   if (!F) {
187609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
188609089f2SHongbin Zheng 
189c14582f2STobias Grosser     Type *Params[] = {PointerType::getUnqual(FunctionType::get(
190c14582f2STobias Grosser                           Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
191a4417835SJohannes Doerfert                       Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongType,
192a4417835SJohannes Doerfert                       LongType, LongType};
193609089f2SHongbin Zheng 
194609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
195609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
196609089f2SHongbin Zheng   }
197609089f2SHongbin Zheng 
198a4417835SJohannes Doerfert   Value *NumberOfThreads = Builder.getInt32(PollyNumThreads);
199*d4ea2f48STobias Grosser   Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride};
200609089f2SHongbin Zheng 
201609089f2SHongbin Zheng   Builder.CreateCall(F, Args);
202609089f2SHongbin Zheng }
203609089f2SHongbin Zheng 
20412b355a2SJohannes Doerfert Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr,
20512b355a2SJohannes Doerfert                                                     Value *UBPtr) {
20612b355a2SJohannes Doerfert   const std::string Name = "GOMP_loop_runtime_next";
20712b355a2SJohannes Doerfert 
208609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
209609089f2SHongbin Zheng 
210609089f2SHongbin Zheng   // If F is not available, declare it.
211609089f2SHongbin Zheng   if (!F) {
212609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
21312b355a2SJohannes Doerfert     Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()};
214609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
215609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
216609089f2SHongbin Zheng   }
217609089f2SHongbin Zheng 
21812b355a2SJohannes Doerfert   Value *Args[] = {LBPtr, UBPtr};
219609089f2SHongbin Zheng   Value *Return = Builder.CreateCall(F, Args);
220c14582f2STobias Grosser   Return = Builder.CreateICmpNE(
221c14582f2STobias Grosser       Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
222609089f2SHongbin Zheng   return Return;
223609089f2SHongbin Zheng }
224609089f2SHongbin Zheng 
22512b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallJoinThreads() {
22612b355a2SJohannes Doerfert   const std::string Name = "GOMP_parallel_end";
22712b355a2SJohannes Doerfert 
228609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
229609089f2SHongbin Zheng 
230609089f2SHongbin Zheng   // If F is not available, declare it.
231609089f2SHongbin Zheng   if (!F) {
232609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
233609089f2SHongbin Zheng 
234609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
235609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
236609089f2SHongbin Zheng   }
237609089f2SHongbin Zheng 
238609089f2SHongbin Zheng   Builder.CreateCall(F);
239609089f2SHongbin Zheng }
240609089f2SHongbin Zheng 
24112b355a2SJohannes Doerfert void ParallelLoopGenerator::createCallCleanupThread() {
24212b355a2SJohannes Doerfert   const std::string Name = "GOMP_loop_end_nowait";
24312b355a2SJohannes Doerfert 
244609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
245609089f2SHongbin Zheng 
246609089f2SHongbin Zheng   // If F is not available, declare it.
247609089f2SHongbin Zheng   if (!F) {
248609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
249609089f2SHongbin Zheng 
250609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
251609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
252609089f2SHongbin Zheng   }
253609089f2SHongbin Zheng 
254609089f2SHongbin Zheng   Builder.CreateCall(F);
255609089f2SHongbin Zheng }
256609089f2SHongbin Zheng 
25712b355a2SJohannes Doerfert Function *ParallelLoopGenerator::createSubFnDefinition() {
258609089f2SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
259609089f2SHongbin Zheng   std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
260609089f2SHongbin Zheng   FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
26112b355a2SJohannes Doerfert   Function *SubFn = Function::Create(FT, Function::InternalLinkage,
26212b355a2SJohannes Doerfert                                      F->getName() + ".polly.subfn", M);
26312b355a2SJohannes Doerfert 
264609089f2SHongbin Zheng   // Do not run any polly pass on the new function.
26512b355a2SJohannes Doerfert   SubFn->addFnAttr(PollySkipFnAttr);
266609089f2SHongbin Zheng 
26712b355a2SJohannes Doerfert   Function::arg_iterator AI = SubFn->arg_begin();
26812b355a2SJohannes Doerfert   AI->setName("polly.par.userContext");
269609089f2SHongbin Zheng 
27012b355a2SJohannes Doerfert   return SubFn;
271609089f2SHongbin Zheng }
272609089f2SHongbin Zheng 
273f0e3d50dSDavid Blaikie AllocaInst *
27412b355a2SJohannes Doerfert ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) {
27512b355a2SJohannes Doerfert   SmallVector<Type *, 8> Members;
276609089f2SHongbin Zheng 
27791f5b262STobias Grosser   for (Value *V : Values)
27891f5b262STobias Grosser     Members.push_back(V->getType());
279609089f2SHongbin Zheng 
2801356ac75SJohannes Doerfert   // We do not want to allocate the alloca inside any loop, thus we allocate it
2811356ac75SJohannes Doerfert   // in the entry block of the function and use annotations to denote the actual
2821356ac75SJohannes Doerfert   // live span (similar to clang).
2831356ac75SJohannes Doerfert   BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock();
2841356ac75SJohannes Doerfert   Instruction *IP = EntryBB.getFirstInsertionPt();
285609089f2SHongbin Zheng   StructType *Ty = StructType::get(Builder.getContext(), Members);
286f0e3d50dSDavid Blaikie   AllocaInst *Struct = new AllocaInst(Ty, 0, "polly.par.userContext", IP);
2871356ac75SJohannes Doerfert 
2881356ac75SJohannes Doerfert   // Mark the start of the lifetime for the parameter struct.
2891356ac75SJohannes Doerfert   ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
2901356ac75SJohannes Doerfert   Builder.CreateLifetimeStart(Struct, SizeOf);
291609089f2SHongbin Zheng 
292609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++) {
293f0e3d50dSDavid Blaikie     Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
294609089f2SHongbin Zheng     Builder.CreateStore(Values[i], Address);
295609089f2SHongbin Zheng   }
296609089f2SHongbin Zheng 
297609089f2SHongbin Zheng   return Struct;
298609089f2SHongbin Zheng }
299609089f2SHongbin Zheng 
30012b355a2SJohannes Doerfert void ParallelLoopGenerator::extractValuesFromStruct(
301f0e3d50dSDavid Blaikie     SetVector<Value *> OldValues, Type *Ty, Value *Struct,
302f0e3d50dSDavid Blaikie     ValueToValueMapTy &Map) {
303609089f2SHongbin Zheng   for (unsigned i = 0; i < OldValues.size(); i++) {
304f0e3d50dSDavid Blaikie     Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
305609089f2SHongbin Zheng     Value *NewValue = Builder.CreateLoad(Address);
30612b355a2SJohannes Doerfert     Map[OldValues[i]] = NewValue;
307609089f2SHongbin Zheng   }
308609089f2SHongbin Zheng }
309609089f2SHongbin Zheng 
310f0e3d50dSDavid Blaikie Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData,
311e602a076STobias Grosser                                           SetVector<Value *> Data,
312e602a076STobias Grosser                                           ValueToValueMapTy &Map,
31312b355a2SJohannes Doerfert                                           Function **SubFnPtr) {
31412b355a2SJohannes Doerfert   BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB;
31512b355a2SJohannes Doerfert   Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV;
31612b355a2SJohannes Doerfert   Function *SubFn = createSubFnDefinition();
31712b355a2SJohannes Doerfert   LLVMContext &Context = SubFn->getContext();
318609089f2SHongbin Zheng 
319609089f2SHongbin Zheng   // Store the previous basic block.
320609089f2SHongbin Zheng   PrevBB = Builder.GetInsertBlock();
321609089f2SHongbin Zheng 
322609089f2SHongbin Zheng   // Create basic blocks.
32312b355a2SJohannes Doerfert   HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn);
32412b355a2SJohannes Doerfert   ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn);
32512b355a2SJohannes Doerfert   CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn);
32612b355a2SJohannes Doerfert   PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn);
327609089f2SHongbin Zheng 
328609089f2SHongbin Zheng   DT.addNewBlock(HeaderBB, PrevBB);
329609089f2SHongbin Zheng   DT.addNewBlock(ExitBB, HeaderBB);
330609089f2SHongbin Zheng   DT.addNewBlock(CheckNextBB, HeaderBB);
33112b355a2SJohannes Doerfert   DT.addNewBlock(PreHeaderBB, HeaderBB);
332609089f2SHongbin Zheng 
333609089f2SHongbin Zheng   // Fill up basic block HeaderBB.
334609089f2SHongbin Zheng   Builder.SetInsertPoint(HeaderBB);
33512b355a2SJohannes Doerfert   LBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.LBPtr");
33612b355a2SJohannes Doerfert   UBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.UBPtr");
33712b355a2SJohannes Doerfert   UserContext = Builder.CreateBitCast(SubFn->arg_begin(), StructData->getType(),
33812b355a2SJohannes Doerfert                                       "polly.par.userContext");
339609089f2SHongbin Zheng 
340f0e3d50dSDavid Blaikie   extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext,
341f0e3d50dSDavid Blaikie                           Map);
342609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
343609089f2SHongbin Zheng 
344609089f2SHongbin Zheng   // Add code to check if another set of iterations will be executed.
345609089f2SHongbin Zheng   Builder.SetInsertPoint(CheckNextBB);
34612b355a2SJohannes Doerfert   Ret1 = createCallGetWorkItem(LBPtr, UBPtr);
347609089f2SHongbin Zheng   HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
34812b355a2SJohannes Doerfert                                         "polly.par.hasNextScheduleBlock");
34912b355a2SJohannes Doerfert   Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB);
350609089f2SHongbin Zheng 
351609089f2SHongbin Zheng   // Add code to to load the iv bounds for this set of iterations.
35212b355a2SJohannes Doerfert   Builder.SetInsertPoint(PreHeaderBB);
35312b355a2SJohannes Doerfert   LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
35412b355a2SJohannes Doerfert   UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
355609089f2SHongbin Zheng 
356609089f2SHongbin Zheng   // Subtract one as the upper bound provided by openmp is a < comparison
357609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
35812b355a2SJohannes Doerfert   UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1),
35912b355a2SJohannes Doerfert                          "polly.par.UBAdjusted");
360609089f2SHongbin Zheng 
361609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
362609089f2SHongbin Zheng   Builder.SetInsertPoint(--Builder.GetInsertPoint());
36312b355a2SJohannes Doerfert   IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB,
364dd5c1442SJohannes Doerfert                   ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false);
365609089f2SHongbin Zheng 
366609089f2SHongbin Zheng   BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
367609089f2SHongbin Zheng 
36812b355a2SJohannes Doerfert   // Add code to terminate this subfunction.
369609089f2SHongbin Zheng   Builder.SetInsertPoint(ExitBB);
37012b355a2SJohannes Doerfert   createCallCleanupThread();
371609089f2SHongbin Zheng   Builder.CreateRetVoid();
372609089f2SHongbin Zheng 
373609089f2SHongbin Zheng   Builder.SetInsertPoint(LoopBody);
37412b355a2SJohannes Doerfert   *SubFnPtr = SubFn;
375609089f2SHongbin Zheng 
376609089f2SHongbin Zheng   return IV;
377609089f2SHongbin Zheng }
378