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 //
10609089f2SHongbin Zheng // This file contains functions to create scalar and OpenMP parallel loops
11609089f2SHongbin Zheng // as LLVM-IR.
12609089f2SHongbin Zheng //
13609089f2SHongbin Zheng //===----------------------------------------------------------------------===//
14609089f2SHongbin Zheng 
15609089f2SHongbin Zheng #include "polly/ScopDetection.h"
168a846610SHongbin Zheng #include "polly/CodeGen/LoopGenerators.h"
173081b0f5STobias Grosser #include "llvm/Analysis/LoopInfo.h"
18535d52c7SChandler Carruth #include "llvm/IR/DataLayout.h"
19e87c6a81SChandler Carruth #include "llvm/IR/Dominators.h"
2083628182STobias Grosser #include "llvm/IR/Module.h"
21609089f2SHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h"
22609089f2SHongbin Zheng 
23609089f2SHongbin Zheng using namespace llvm;
244ac4e155SHongbin Zheng using namespace polly;
25609089f2SHongbin Zheng 
265db6ffd7STobias Grosser // We generate a loop of the following structure
275db6ffd7STobias Grosser //
285db6ffd7STobias Grosser //              BeforeBB
295db6ffd7STobias Grosser //                 |
305db6ffd7STobias Grosser //                 v
315db6ffd7STobias Grosser //              GuardBB
32*0c9de71eSSebastian Pop //              /      |
33*0c9de71eSSebastian Pop //     __  PreHeaderBB  |
345db6ffd7STobias Grosser //    /  \    /         |
355db6ffd7STobias Grosser // latch  HeaderBB      |
365db6ffd7STobias Grosser //    \  /    \         /
375db6ffd7STobias Grosser //     <       \       /
385db6ffd7STobias Grosser //              \     /
395db6ffd7STobias Grosser //              ExitBB
405db6ffd7STobias Grosser //
415db6ffd7STobias Grosser // GuardBB checks if the loop is executed at least once. If this is the case
425db6ffd7STobias Grosser // we branch to PreHeaderBB and subsequently to the HeaderBB, which contains the
435db6ffd7STobias Grosser // loop iv 'polly.indvar', the incremented loop iv 'polly.indvar_next' as well
445db6ffd7STobias Grosser // as the condition to check if we execute another iteration of the loop. After
455db6ffd7STobias Grosser // the loop has finished, we branch to ExitBB.
465db6ffd7STobias Grosser //
475db6ffd7STobias Grosser // TODO: We currently always create the GuardBB. If we can prove the loop is
485db6ffd7STobias Grosser //       always executed at least once, we can get rid of this branch.
494ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
505103ba7cSTobias Grosser                          PollyIRBuilder &Builder, Pass *P, BasicBlock *&ExitBB,
5137c9b8e0STobias Grosser                          ICmpInst::Predicate Predicate,
5237c9b8e0STobias Grosser                          LoopAnnotator *Annotator, bool Parallel) {
535db6ffd7STobias Grosser 
5442aff30dSTobias Grosser   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
553081b0f5STobias Grosser   LoopInfo &LI = P->getAnalysis<LoopInfo>();
564ac4e155SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
57609089f2SHongbin Zheng   LLVMContext &Context = F->getContext();
58609089f2SHongbin Zheng 
595db6ffd7STobias Grosser   assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
60609089f2SHongbin Zheng   IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
61609089f2SHongbin Zheng   assert(LoopIVType && "UB is not integer?");
62609089f2SHongbin Zheng 
635db6ffd7STobias Grosser   BasicBlock *BeforeBB = Builder.GetInsertBlock();
645db6ffd7STobias Grosser   BasicBlock *GuardBB = BasicBlock::Create(Context, "polly.loop_if", F);
655db6ffd7STobias Grosser   BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
665db6ffd7STobias Grosser   BasicBlock *PreHeaderBB =
675db6ffd7STobias Grosser       BasicBlock::Create(Context, "polly.loop_preheader", F);
68609089f2SHongbin Zheng 
6937c9b8e0STobias Grosser   if (Annotator) {
7037c9b8e0STobias Grosser     Annotator->Begin(HeaderBB);
7137c9b8e0STobias Grosser     if (Parallel)
7237c9b8e0STobias Grosser       Annotator->SetCurrentParallel();
7337c9b8e0STobias Grosser   }
7437c9b8e0STobias Grosser 
753081b0f5STobias Grosser   // Update LoopInfo
763081b0f5STobias Grosser   Loop *OuterLoop = LI.getLoopFor(BeforeBB);
773081b0f5STobias Grosser   Loop *NewLoop = new Loop();
783081b0f5STobias Grosser 
793081b0f5STobias Grosser   if (OuterLoop) {
803081b0f5STobias Grosser     OuterLoop->addChildLoop(NewLoop);
813081b0f5STobias Grosser   } else {
823081b0f5STobias Grosser     LI.addTopLevelLoop(NewLoop);
833081b0f5STobias Grosser   }
843081b0f5STobias Grosser 
853081b0f5STobias Grosser   if (OuterLoop) {
863081b0f5STobias Grosser     OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
873081b0f5STobias Grosser     OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
883081b0f5STobias Grosser   }
893081b0f5STobias Grosser 
903081b0f5STobias Grosser   NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
913081b0f5STobias Grosser 
925db6ffd7STobias Grosser   // ExitBB
935db6ffd7STobias Grosser   ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
945db6ffd7STobias Grosser   ExitBB->setName("polly.loop_exit");
95609089f2SHongbin Zheng 
965db6ffd7STobias Grosser   // BeforeBB
975db6ffd7STobias Grosser   BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
98609089f2SHongbin Zheng 
995db6ffd7STobias Grosser   // GuardBB
1005db6ffd7STobias Grosser   DT.addNewBlock(GuardBB, BeforeBB);
1015db6ffd7STobias Grosser   Builder.SetInsertPoint(GuardBB);
1025db6ffd7STobias Grosser   Value *LoopGuard;
1035db6ffd7STobias Grosser   LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
1045db6ffd7STobias Grosser   LoopGuard->setName("polly.loop_guard");
1055db6ffd7STobias Grosser   Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
106609089f2SHongbin Zheng 
1075db6ffd7STobias Grosser   // PreHeaderBB
1085db6ffd7STobias Grosser   DT.addNewBlock(PreHeaderBB, GuardBB);
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");
1235db6ffd7STobias Grosser   Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
1245db6ffd7STobias Grosser   IV->addIncoming(IncrementedIV, HeaderBB);
1255db6ffd7STobias Grosser   DT.changeImmediateDominator(ExitBB, GuardBB);
126609089f2SHongbin Zheng 
1275db6ffd7STobias Grosser   // The loop body should be added here.
1285db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
129609089f2SHongbin Zheng   return IV;
130609089f2SHongbin Zheng }
131609089f2SHongbin Zheng 
132c14582f2STobias Grosser void OMPGenerator::createCallParallelLoopStart(
133c14582f2STobias Grosser     Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
134c14582f2STobias Grosser     Value *LowerBound, Value *UpperBound, Value *Stride) {
135609089f2SHongbin Zheng   Module *M = getModule();
136609089f2SHongbin Zheng   const char *Name = "GOMP_parallel_loop_runtime_start";
137609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
138609089f2SHongbin Zheng 
139609089f2SHongbin Zheng   // If F is not available, declare it.
140609089f2SHongbin Zheng   if (!F) {
141609089f2SHongbin Zheng     Type *LongTy = getIntPtrTy();
142609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
143609089f2SHongbin Zheng 
144c14582f2STobias Grosser     Type *Params[] = {PointerType::getUnqual(FunctionType::get(
145c14582f2STobias Grosser                           Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
146c14582f2STobias Grosser                       Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
14745bac0d9STobias Grosser                       LongTy, LongTy};
148609089f2SHongbin Zheng 
149609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
150609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
151609089f2SHongbin Zheng   }
152609089f2SHongbin Zheng 
1534c932b8aSTobias Grosser   Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads,
1544c932b8aSTobias Grosser                    LowerBound,  UpperBound,       Stride};
155609089f2SHongbin Zheng 
156609089f2SHongbin Zheng   Builder.CreateCall(F, Args);
157609089f2SHongbin Zheng }
158609089f2SHongbin Zheng 
159e602a076STobias Grosser Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
160e602a076STobias Grosser                                         Value *UpperBoundPtr) {
161609089f2SHongbin Zheng   Module *M = getModule();
162609089f2SHongbin Zheng   const char *Name = "GOMP_loop_runtime_next";
163609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
164609089f2SHongbin Zheng 
165609089f2SHongbin Zheng   // If F is not available, declare it.
166609089f2SHongbin Zheng   if (!F) {
167609089f2SHongbin Zheng     Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
168609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
169609089f2SHongbin Zheng 
17045bac0d9STobias Grosser     Type *Params[] = {LongPtrTy, LongPtrTy};
171609089f2SHongbin Zheng 
172609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
173609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
174609089f2SHongbin Zheng   }
175609089f2SHongbin Zheng 
17645bac0d9STobias Grosser   Value *Args[] = {LowerBoundPtr, UpperBoundPtr};
177609089f2SHongbin Zheng 
178609089f2SHongbin Zheng   Value *Return = Builder.CreateCall(F, Args);
179c14582f2STobias Grosser   Return = Builder.CreateICmpNE(
180c14582f2STobias Grosser       Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
181609089f2SHongbin Zheng   return Return;
182609089f2SHongbin Zheng }
183609089f2SHongbin Zheng 
184609089f2SHongbin Zheng void OMPGenerator::createCallParallelEnd() {
185609089f2SHongbin Zheng   const char *Name = "GOMP_parallel_end";
186609089f2SHongbin Zheng   Module *M = getModule();
187609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
188609089f2SHongbin Zheng 
189609089f2SHongbin Zheng   // If F is not available, declare it.
190609089f2SHongbin Zheng   if (!F) {
191609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
192609089f2SHongbin Zheng 
193609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
194609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
195609089f2SHongbin Zheng   }
196609089f2SHongbin Zheng 
197609089f2SHongbin Zheng   Builder.CreateCall(F);
198609089f2SHongbin Zheng }
199609089f2SHongbin Zheng 
200609089f2SHongbin Zheng void OMPGenerator::createCallLoopEndNowait() {
201609089f2SHongbin Zheng   const char *Name = "GOMP_loop_end_nowait";
202609089f2SHongbin Zheng   Module *M = getModule();
203609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
204609089f2SHongbin Zheng 
205609089f2SHongbin Zheng   // If F is not available, declare it.
206609089f2SHongbin Zheng   if (!F) {
207609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
208609089f2SHongbin Zheng 
209609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
210609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
211609089f2SHongbin Zheng   }
212609089f2SHongbin Zheng 
213609089f2SHongbin Zheng   Builder.CreateCall(F);
214609089f2SHongbin Zheng }
215609089f2SHongbin Zheng 
216609089f2SHongbin Zheng IntegerType *OMPGenerator::getIntPtrTy() {
217c5d1689bSRafael Espindola   return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType(
218c5d1689bSRafael Espindola       Builder.getContext());
219609089f2SHongbin Zheng }
220609089f2SHongbin Zheng 
221609089f2SHongbin Zheng Module *OMPGenerator::getModule() {
222609089f2SHongbin Zheng   return Builder.GetInsertBlock()->getParent()->getParent();
223609089f2SHongbin Zheng }
224609089f2SHongbin Zheng 
225609089f2SHongbin Zheng Function *OMPGenerator::createSubfunctionDefinition() {
226609089f2SHongbin Zheng   Module *M = getModule();
227609089f2SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
228609089f2SHongbin Zheng   std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
229609089f2SHongbin Zheng   FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
230609089f2SHongbin Zheng   Function *FN = Function::Create(FT, Function::InternalLinkage,
231609089f2SHongbin Zheng                                   F->getName() + ".omp_subfn", M);
232609089f2SHongbin Zheng   // Do not run any polly pass on the new function.
233609089f2SHongbin Zheng   P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN);
234609089f2SHongbin Zheng 
235609089f2SHongbin Zheng   Function::arg_iterator AI = FN->arg_begin();
236609089f2SHongbin Zheng   AI->setName("omp.userContext");
237609089f2SHongbin Zheng 
238609089f2SHongbin Zheng   return FN;
239609089f2SHongbin Zheng }
240609089f2SHongbin Zheng 
241609089f2SHongbin Zheng Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
242609089f2SHongbin Zheng   std::vector<Type *> Members;
243609089f2SHongbin Zheng 
244609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++)
245609089f2SHongbin Zheng     Members.push_back(Values[i]->getType());
246609089f2SHongbin Zheng 
247609089f2SHongbin Zheng   StructType *Ty = StructType::get(Builder.getContext(), Members);
248609089f2SHongbin Zheng   Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
249609089f2SHongbin Zheng 
250609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++) {
251609089f2SHongbin Zheng     Value *Address = Builder.CreateStructGEP(Struct, i);
252609089f2SHongbin Zheng     Builder.CreateStore(Values[i], Address);
253609089f2SHongbin Zheng   }
254609089f2SHongbin Zheng 
255609089f2SHongbin Zheng   return Struct;
256609089f2SHongbin Zheng }
257609089f2SHongbin Zheng 
258e602a076STobias Grosser void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
259e602a076STobias Grosser                                            Value *Struct,
260e602a076STobias Grosser                                            ValueToValueMapTy &Map) {
261609089f2SHongbin Zheng   for (unsigned i = 0; i < OldValues.size(); i++) {
262609089f2SHongbin Zheng     Value *Address = Builder.CreateStructGEP(Struct, i);
263609089f2SHongbin Zheng     Value *NewValue = Builder.CreateLoad(Address);
26436e6ca01SAndy Gibbs     Map.insert(std::make_pair(OldValues[i], NewValue));
265609089f2SHongbin Zheng   }
266609089f2SHongbin Zheng }
267609089f2SHongbin Zheng 
268e602a076STobias Grosser Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
269e602a076STobias Grosser                                        SetVector<Value *> Data,
270e602a076STobias Grosser                                        ValueToValueMapTy &Map,
271e602a076STobias Grosser                                        Function **SubFunction) {
272609089f2SHongbin Zheng   Function *FN = createSubfunctionDefinition();
273609089f2SHongbin Zheng 
274609089f2SHongbin Zheng   BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
275609089f2SHongbin Zheng       *AfterBB;
276609089f2SHongbin Zheng   Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
277609089f2SHongbin Zheng       *LowerBound, *UpperBound, *IV;
278609089f2SHongbin Zheng   Type *IntPtrTy = getIntPtrTy();
279609089f2SHongbin Zheng   LLVMContext &Context = FN->getContext();
280609089f2SHongbin Zheng 
281609089f2SHongbin Zheng   // Store the previous basic block.
282609089f2SHongbin Zheng   PrevBB = Builder.GetInsertBlock();
283609089f2SHongbin Zheng 
284609089f2SHongbin Zheng   // Create basic blocks.
285609089f2SHongbin Zheng   HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
286609089f2SHongbin Zheng   ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
287609089f2SHongbin Zheng   CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
288609089f2SHongbin Zheng   LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
289609089f2SHongbin Zheng 
29042aff30dSTobias Grosser   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
291609089f2SHongbin Zheng   DT.addNewBlock(HeaderBB, PrevBB);
292609089f2SHongbin Zheng   DT.addNewBlock(ExitBB, HeaderBB);
293609089f2SHongbin Zheng   DT.addNewBlock(CheckNextBB, HeaderBB);
294609089f2SHongbin Zheng   DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
295609089f2SHongbin Zheng 
296609089f2SHongbin Zheng   // Fill up basic block HeaderBB.
297609089f2SHongbin Zheng   Builder.SetInsertPoint(HeaderBB);
298609089f2SHongbin Zheng   LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
299609089f2SHongbin Zheng   UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
300609089f2SHongbin Zheng   UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
301609089f2SHongbin Zheng                                       "omp.userContext");
302609089f2SHongbin Zheng 
303609089f2SHongbin Zheng   extractValuesFromStruct(Data, UserContext, Map);
304609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
305609089f2SHongbin Zheng 
306609089f2SHongbin Zheng   // Add code to check if another set of iterations will be executed.
307609089f2SHongbin Zheng   Builder.SetInsertPoint(CheckNextBB);
308609089f2SHongbin Zheng   Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
309609089f2SHongbin Zheng   HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
310609089f2SHongbin Zheng                                         "omp.hasNextScheduleBlock");
311609089f2SHongbin Zheng   Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
312609089f2SHongbin Zheng 
313609089f2SHongbin Zheng   // Add code to to load the iv bounds for this set of iterations.
314609089f2SHongbin Zheng   Builder.SetInsertPoint(LoadIVBoundsBB);
315609089f2SHongbin Zheng   LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
316609089f2SHongbin Zheng   UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
317609089f2SHongbin Zheng 
318609089f2SHongbin Zheng   // Subtract one as the upper bound provided by openmp is a < comparison
319609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
320609089f2SHongbin Zheng   UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
321609089f2SHongbin Zheng                                  "omp.upperBoundAdjusted");
322609089f2SHongbin Zheng 
323609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
324609089f2SHongbin Zheng   Builder.SetInsertPoint(--Builder.GetInsertPoint());
325c967d8e6STobias Grosser   IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
326c967d8e6STobias Grosser                   ICmpInst::ICMP_SLE);
327609089f2SHongbin Zheng 
328609089f2SHongbin Zheng   BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
329609089f2SHongbin Zheng   Builder.SetInsertPoint(AfterBB->begin());
330609089f2SHongbin Zheng 
331609089f2SHongbin Zheng   // Add code to terminate this openmp subfunction.
332609089f2SHongbin Zheng   Builder.SetInsertPoint(ExitBB);
333609089f2SHongbin Zheng   createCallLoopEndNowait();
334609089f2SHongbin Zheng   Builder.CreateRetVoid();
335609089f2SHongbin Zheng 
336609089f2SHongbin Zheng   Builder.SetInsertPoint(LoopBody);
337609089f2SHongbin Zheng   *SubFunction = FN;
338609089f2SHongbin Zheng 
339609089f2SHongbin Zheng   return IV;
340609089f2SHongbin Zheng }
341609089f2SHongbin Zheng 
342e602a076STobias Grosser Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
343e602a076STobias Grosser                                         Value *Stride,
344e602a076STobias Grosser                                         SetVector<Value *> &Values,
345e602a076STobias Grosser                                         ValueToValueMapTy &Map,
346609089f2SHongbin Zheng                                         BasicBlock::iterator *LoopBody) {
347609089f2SHongbin Zheng   Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
348609089f2SHongbin Zheng   Function *SubFunction;
349609089f2SHongbin Zheng 
350609089f2SHongbin Zheng   Struct = loadValuesIntoStruct(Values);
351609089f2SHongbin Zheng 
352609089f2SHongbin Zheng   BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
353609089f2SHongbin Zheng   IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
354609089f2SHongbin Zheng   *LoopBody = Builder.GetInsertPoint();
355609089f2SHongbin Zheng   Builder.SetInsertPoint(PrevInsertPoint);
356609089f2SHongbin Zheng 
357609089f2SHongbin Zheng   // Create call for GOMP_parallel_loop_runtime_start.
358c14582f2STobias Grosser   SubfunctionParam =
359c14582f2STobias Grosser       Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
360609089f2SHongbin Zheng 
361609089f2SHongbin Zheng   NumberOfThreads = Builder.getInt32(0);
362609089f2SHongbin Zheng 
363609089f2SHongbin Zheng   // Add one as the upper bound provided by openmp is a < comparison
364609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
365c14582f2STobias Grosser   UpperBound =
366c14582f2STobias Grosser       Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
367609089f2SHongbin Zheng 
368609089f2SHongbin Zheng   createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
369609089f2SHongbin Zheng                               LowerBound, UpperBound, Stride);
370609089f2SHongbin Zheng   Builder.CreateCall(SubFunction, SubfunctionParam);
371609089f2SHongbin Zheng   createCallParallelEnd();
372609089f2SHongbin Zheng 
373609089f2SHongbin Zheng   return IV;
374609089f2SHongbin Zheng }
375