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"
17609089f2SHongbin Zheng #include "llvm/Analysis/Dominators.h"
18535d52c7SChandler Carruth #include "llvm/IR/DataLayout.h"
1983628182STobias Grosser #include "llvm/IR/Module.h"
20609089f2SHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21609089f2SHongbin Zheng 
22609089f2SHongbin Zheng using namespace llvm;
234ac4e155SHongbin Zheng using namespace polly;
24609089f2SHongbin Zheng 
25*5db6ffd7STobias Grosser // We generate a loop of the following structure
26*5db6ffd7STobias Grosser //
27*5db6ffd7STobias Grosser //              BeforeBB
28*5db6ffd7STobias Grosser //                 |
29*5db6ffd7STobias Grosser //                 v
30*5db6ffd7STobias Grosser //              GuardBB
31*5db6ffd7STobias Grosser //              /      \
32*5db6ffd7STobias Grosser //     __  PreHeaderBB  \
33*5db6ffd7STobias Grosser //    /  \    /         |
34*5db6ffd7STobias Grosser // latch  HeaderBB      |
35*5db6ffd7STobias Grosser //    \  /    \         /
36*5db6ffd7STobias Grosser //     <       \       /
37*5db6ffd7STobias Grosser //              \     /
38*5db6ffd7STobias Grosser //              ExitBB
39*5db6ffd7STobias Grosser //
40*5db6ffd7STobias Grosser // GuardBB checks if the loop is executed at least once. If this is the case
41*5db6ffd7STobias Grosser // we branch to PreHeaderBB and subsequently to the HeaderBB, which contains the
42*5db6ffd7STobias Grosser // loop iv 'polly.indvar', the incremented loop iv 'polly.indvar_next' as well
43*5db6ffd7STobias Grosser // as the condition to check if we execute another iteration of the loop. After
44*5db6ffd7STobias Grosser // the loop has finished, we branch to ExitBB.
45*5db6ffd7STobias Grosser //
46*5db6ffd7STobias Grosser // TODO: We currently always create the GuardBB. If we can prove the loop is
47*5db6ffd7STobias Grosser //       always executed at least once, we can get rid of this branch.
484ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
49*5db6ffd7STobias Grosser                          IRBuilder<> &Builder, Pass *P, BasicBlock *&ExitBB,
50c967d8e6STobias Grosser                          ICmpInst::Predicate Predicate) {
51*5db6ffd7STobias Grosser 
52609089f2SHongbin Zheng   DominatorTree &DT = P->getAnalysis<DominatorTree>();
534ac4e155SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
54609089f2SHongbin Zheng   LLVMContext &Context = F->getContext();
55609089f2SHongbin Zheng 
56*5db6ffd7STobias Grosser   assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
57609089f2SHongbin Zheng   IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
58609089f2SHongbin Zheng   assert(LoopIVType && "UB is not integer?");
59609089f2SHongbin Zheng 
60*5db6ffd7STobias Grosser   BasicBlock *BeforeBB = Builder.GetInsertBlock();
61*5db6ffd7STobias Grosser   BasicBlock *GuardBB = BasicBlock::Create(Context, "polly.loop_if", F);
62*5db6ffd7STobias Grosser   BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
63*5db6ffd7STobias Grosser   BasicBlock *PreHeaderBB =
64*5db6ffd7STobias Grosser       BasicBlock::Create(Context, "polly.loop_preheader", F);
65609089f2SHongbin Zheng 
66*5db6ffd7STobias Grosser   // ExitBB
67*5db6ffd7STobias Grosser   ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
68*5db6ffd7STobias Grosser   ExitBB->setName("polly.loop_exit");
69609089f2SHongbin Zheng 
70*5db6ffd7STobias Grosser   // BeforeBB
71*5db6ffd7STobias Grosser   BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
72609089f2SHongbin Zheng 
73*5db6ffd7STobias Grosser   // GuardBB
74*5db6ffd7STobias Grosser   DT.addNewBlock(GuardBB, BeforeBB);
75*5db6ffd7STobias Grosser   Builder.SetInsertPoint(GuardBB);
76*5db6ffd7STobias Grosser   Value *LoopGuard;
77*5db6ffd7STobias Grosser   LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
78*5db6ffd7STobias Grosser   LoopGuard->setName("polly.loop_guard");
79*5db6ffd7STobias Grosser   Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
80609089f2SHongbin Zheng 
81*5db6ffd7STobias Grosser   // PreHeaderBB
82*5db6ffd7STobias Grosser   DT.addNewBlock(PreHeaderBB, GuardBB);
83*5db6ffd7STobias Grosser   Builder.SetInsertPoint(PreHeaderBB);
844ac4e155SHongbin Zheng   Builder.CreateBr(HeaderBB);
85609089f2SHongbin Zheng 
86*5db6ffd7STobias Grosser   // HeaderBB
87*5db6ffd7STobias Grosser   DT.addNewBlock(HeaderBB, PreHeaderBB);
88*5db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB);
89*5db6ffd7STobias Grosser   PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
90*5db6ffd7STobias Grosser   IV->addIncoming(LB, PreHeaderBB);
91*5db6ffd7STobias Grosser   Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
92*5db6ffd7STobias Grosser   Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
93*5db6ffd7STobias Grosser   Value *LoopCondition;
94*5db6ffd7STobias Grosser   UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
95*5db6ffd7STobias Grosser   LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
96*5db6ffd7STobias Grosser   LoopCondition->setName("polly.loop_cond");
97*5db6ffd7STobias Grosser   Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
98*5db6ffd7STobias Grosser   IV->addIncoming(IncrementedIV, HeaderBB);
99*5db6ffd7STobias Grosser   DT.changeImmediateDominator(ExitBB, GuardBB);
100609089f2SHongbin Zheng 
101*5db6ffd7STobias Grosser   // The loop body should be added here.
102*5db6ffd7STobias Grosser   Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
103609089f2SHongbin Zheng   return IV;
104609089f2SHongbin Zheng }
105609089f2SHongbin Zheng 
106c14582f2STobias Grosser void OMPGenerator::createCallParallelLoopStart(
107c14582f2STobias Grosser     Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
108c14582f2STobias Grosser     Value *LowerBound, Value *UpperBound, Value *Stride) {
109609089f2SHongbin Zheng   Module *M = getModule();
110609089f2SHongbin Zheng   const char *Name = "GOMP_parallel_loop_runtime_start";
111609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
112609089f2SHongbin Zheng 
113609089f2SHongbin Zheng   // If F is not available, declare it.
114609089f2SHongbin Zheng   if (!F) {
115609089f2SHongbin Zheng     Type *LongTy = getIntPtrTy();
116609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
117609089f2SHongbin Zheng 
118c14582f2STobias Grosser     Type *Params[] = { PointerType::getUnqual(FunctionType::get(
119c14582f2STobias Grosser                            Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
120c14582f2STobias Grosser                        Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
121c14582f2STobias Grosser                        LongTy, LongTy, };
122609089f2SHongbin Zheng 
123609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
124609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
125609089f2SHongbin Zheng   }
126609089f2SHongbin Zheng 
127c14582f2STobias Grosser   Value *Args[] = { SubFunction, SubfunctionParam, NumberOfThreads, LowerBound,
128c14582f2STobias Grosser                     UpperBound, Stride, };
129609089f2SHongbin Zheng 
130609089f2SHongbin Zheng   Builder.CreateCall(F, Args);
131609089f2SHongbin Zheng }
132609089f2SHongbin Zheng 
133e602a076STobias Grosser Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
134e602a076STobias Grosser                                         Value *UpperBoundPtr) {
135609089f2SHongbin Zheng   Module *M = getModule();
136609089f2SHongbin Zheng   const char *Name = "GOMP_loop_runtime_next";
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 *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
142609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
143609089f2SHongbin Zheng 
144c14582f2STobias Grosser     Type *Params[] = { LongPtrTy, LongPtrTy, };
145609089f2SHongbin Zheng 
146609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
147609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
148609089f2SHongbin Zheng   }
149609089f2SHongbin Zheng 
150c14582f2STobias Grosser   Value *Args[] = { LowerBoundPtr, UpperBoundPtr, };
151609089f2SHongbin Zheng 
152609089f2SHongbin Zheng   Value *Return = Builder.CreateCall(F, Args);
153c14582f2STobias Grosser   Return = Builder.CreateICmpNE(
154c14582f2STobias Grosser       Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
155609089f2SHongbin Zheng   return Return;
156609089f2SHongbin Zheng }
157609089f2SHongbin Zheng 
158609089f2SHongbin Zheng void OMPGenerator::createCallParallelEnd() {
159609089f2SHongbin Zheng   const char *Name = "GOMP_parallel_end";
160609089f2SHongbin Zheng   Module *M = getModule();
161609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
162609089f2SHongbin Zheng 
163609089f2SHongbin Zheng   // If F is not available, declare it.
164609089f2SHongbin Zheng   if (!F) {
165609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
166609089f2SHongbin Zheng 
167609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
168609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
169609089f2SHongbin Zheng   }
170609089f2SHongbin Zheng 
171609089f2SHongbin Zheng   Builder.CreateCall(F);
172609089f2SHongbin Zheng }
173609089f2SHongbin Zheng 
174609089f2SHongbin Zheng void OMPGenerator::createCallLoopEndNowait() {
175609089f2SHongbin Zheng   const char *Name = "GOMP_loop_end_nowait";
176609089f2SHongbin Zheng   Module *M = getModule();
177609089f2SHongbin Zheng   Function *F = M->getFunction(Name);
178609089f2SHongbin Zheng 
179609089f2SHongbin Zheng   // If F is not available, declare it.
180609089f2SHongbin Zheng   if (!F) {
181609089f2SHongbin Zheng     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
182609089f2SHongbin Zheng 
183609089f2SHongbin Zheng     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
184609089f2SHongbin Zheng     F = Function::Create(Ty, Linkage, Name, M);
185609089f2SHongbin Zheng   }
186609089f2SHongbin Zheng 
187609089f2SHongbin Zheng   Builder.CreateCall(F);
188609089f2SHongbin Zheng }
189609089f2SHongbin Zheng 
190609089f2SHongbin Zheng IntegerType *OMPGenerator::getIntPtrTy() {
1915d01691dSTobias Grosser   return P->getAnalysis<DataLayout>().getIntPtrType(Builder.getContext());
192609089f2SHongbin Zheng }
193609089f2SHongbin Zheng 
194609089f2SHongbin Zheng Module *OMPGenerator::getModule() {
195609089f2SHongbin Zheng   return Builder.GetInsertBlock()->getParent()->getParent();
196609089f2SHongbin Zheng }
197609089f2SHongbin Zheng 
198609089f2SHongbin Zheng Function *OMPGenerator::createSubfunctionDefinition() {
199609089f2SHongbin Zheng   Module *M = getModule();
200609089f2SHongbin Zheng   Function *F = Builder.GetInsertBlock()->getParent();
201609089f2SHongbin Zheng   std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
202609089f2SHongbin Zheng   FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
203609089f2SHongbin Zheng   Function *FN = Function::Create(FT, Function::InternalLinkage,
204609089f2SHongbin Zheng                                   F->getName() + ".omp_subfn", M);
205609089f2SHongbin Zheng   // Do not run any polly pass on the new function.
206609089f2SHongbin Zheng   P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN);
207609089f2SHongbin Zheng 
208609089f2SHongbin Zheng   Function::arg_iterator AI = FN->arg_begin();
209609089f2SHongbin Zheng   AI->setName("omp.userContext");
210609089f2SHongbin Zheng 
211609089f2SHongbin Zheng   return FN;
212609089f2SHongbin Zheng }
213609089f2SHongbin Zheng 
214609089f2SHongbin Zheng Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
215609089f2SHongbin Zheng   std::vector<Type *> Members;
216609089f2SHongbin Zheng 
217609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++)
218609089f2SHongbin Zheng     Members.push_back(Values[i]->getType());
219609089f2SHongbin Zheng 
220609089f2SHongbin Zheng   StructType *Ty = StructType::get(Builder.getContext(), Members);
221609089f2SHongbin Zheng   Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
222609089f2SHongbin Zheng 
223609089f2SHongbin Zheng   for (unsigned i = 0; i < Values.size(); i++) {
224609089f2SHongbin Zheng     Value *Address = Builder.CreateStructGEP(Struct, i);
225609089f2SHongbin Zheng     Builder.CreateStore(Values[i], Address);
226609089f2SHongbin Zheng   }
227609089f2SHongbin Zheng 
228609089f2SHongbin Zheng   return Struct;
229609089f2SHongbin Zheng }
230609089f2SHongbin Zheng 
231e602a076STobias Grosser void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
232e602a076STobias Grosser                                            Value *Struct,
233e602a076STobias Grosser                                            ValueToValueMapTy &Map) {
234609089f2SHongbin Zheng   for (unsigned i = 0; i < OldValues.size(); i++) {
235609089f2SHongbin Zheng     Value *Address = Builder.CreateStructGEP(Struct, i);
236609089f2SHongbin Zheng     Value *NewValue = Builder.CreateLoad(Address);
23736e6ca01SAndy Gibbs     Map.insert(std::make_pair(OldValues[i], NewValue));
238609089f2SHongbin Zheng   }
239609089f2SHongbin Zheng }
240609089f2SHongbin Zheng 
241e602a076STobias Grosser Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
242e602a076STobias Grosser                                        SetVector<Value *> Data,
243e602a076STobias Grosser                                        ValueToValueMapTy &Map,
244e602a076STobias Grosser                                        Function **SubFunction) {
245609089f2SHongbin Zheng   Function *FN = createSubfunctionDefinition();
246609089f2SHongbin Zheng 
247609089f2SHongbin Zheng   BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
248609089f2SHongbin Zheng       *AfterBB;
249609089f2SHongbin Zheng   Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
250609089f2SHongbin Zheng       *LowerBound, *UpperBound, *IV;
251609089f2SHongbin Zheng   Type *IntPtrTy = getIntPtrTy();
252609089f2SHongbin Zheng   LLVMContext &Context = FN->getContext();
253609089f2SHongbin Zheng 
254609089f2SHongbin Zheng   // Store the previous basic block.
255609089f2SHongbin Zheng   PrevBB = Builder.GetInsertBlock();
256609089f2SHongbin Zheng 
257609089f2SHongbin Zheng   // Create basic blocks.
258609089f2SHongbin Zheng   HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
259609089f2SHongbin Zheng   ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
260609089f2SHongbin Zheng   CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
261609089f2SHongbin Zheng   LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
262609089f2SHongbin Zheng 
263609089f2SHongbin Zheng   DominatorTree &DT = P->getAnalysis<DominatorTree>();
264609089f2SHongbin Zheng   DT.addNewBlock(HeaderBB, PrevBB);
265609089f2SHongbin Zheng   DT.addNewBlock(ExitBB, HeaderBB);
266609089f2SHongbin Zheng   DT.addNewBlock(CheckNextBB, HeaderBB);
267609089f2SHongbin Zheng   DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
268609089f2SHongbin Zheng 
269609089f2SHongbin Zheng   // Fill up basic block HeaderBB.
270609089f2SHongbin Zheng   Builder.SetInsertPoint(HeaderBB);
271609089f2SHongbin Zheng   LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
272609089f2SHongbin Zheng   UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
273609089f2SHongbin Zheng   UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
274609089f2SHongbin Zheng                                       "omp.userContext");
275609089f2SHongbin Zheng 
276609089f2SHongbin Zheng   extractValuesFromStruct(Data, UserContext, Map);
277609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
278609089f2SHongbin Zheng 
279609089f2SHongbin Zheng   // Add code to check if another set of iterations will be executed.
280609089f2SHongbin Zheng   Builder.SetInsertPoint(CheckNextBB);
281609089f2SHongbin Zheng   Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
282609089f2SHongbin Zheng   HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
283609089f2SHongbin Zheng                                         "omp.hasNextScheduleBlock");
284609089f2SHongbin Zheng   Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
285609089f2SHongbin Zheng 
286609089f2SHongbin Zheng   // Add code to to load the iv bounds for this set of iterations.
287609089f2SHongbin Zheng   Builder.SetInsertPoint(LoadIVBoundsBB);
288609089f2SHongbin Zheng   LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
289609089f2SHongbin Zheng   UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
290609089f2SHongbin Zheng 
291609089f2SHongbin Zheng   // Subtract one as the upper bound provided by openmp is a < comparison
292609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
293609089f2SHongbin Zheng   UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
294609089f2SHongbin Zheng                                  "omp.upperBoundAdjusted");
295609089f2SHongbin Zheng 
296609089f2SHongbin Zheng   Builder.CreateBr(CheckNextBB);
297609089f2SHongbin Zheng   Builder.SetInsertPoint(--Builder.GetInsertPoint());
298c967d8e6STobias Grosser   IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
299c967d8e6STobias Grosser                   ICmpInst::ICMP_SLE);
300609089f2SHongbin Zheng 
301609089f2SHongbin Zheng   BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
302609089f2SHongbin Zheng   Builder.SetInsertPoint(AfterBB->begin());
303609089f2SHongbin Zheng 
304609089f2SHongbin Zheng   // Add code to terminate this openmp subfunction.
305609089f2SHongbin Zheng   Builder.SetInsertPoint(ExitBB);
306609089f2SHongbin Zheng   createCallLoopEndNowait();
307609089f2SHongbin Zheng   Builder.CreateRetVoid();
308609089f2SHongbin Zheng 
309609089f2SHongbin Zheng   Builder.SetInsertPoint(LoopBody);
310609089f2SHongbin Zheng   *SubFunction = FN;
311609089f2SHongbin Zheng 
312609089f2SHongbin Zheng   return IV;
313609089f2SHongbin Zheng }
314609089f2SHongbin Zheng 
315e602a076STobias Grosser Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
316e602a076STobias Grosser                                         Value *Stride,
317e602a076STobias Grosser                                         SetVector<Value *> &Values,
318e602a076STobias Grosser                                         ValueToValueMapTy &Map,
319609089f2SHongbin Zheng                                         BasicBlock::iterator *LoopBody) {
320609089f2SHongbin Zheng   Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
321609089f2SHongbin Zheng   Function *SubFunction;
322609089f2SHongbin Zheng 
323609089f2SHongbin Zheng   Struct = loadValuesIntoStruct(Values);
324609089f2SHongbin Zheng 
325609089f2SHongbin Zheng   BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
326609089f2SHongbin Zheng   IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
327609089f2SHongbin Zheng   *LoopBody = Builder.GetInsertPoint();
328609089f2SHongbin Zheng   Builder.SetInsertPoint(PrevInsertPoint);
329609089f2SHongbin Zheng 
330609089f2SHongbin Zheng   // Create call for GOMP_parallel_loop_runtime_start.
331c14582f2STobias Grosser   SubfunctionParam =
332c14582f2STobias Grosser       Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
333609089f2SHongbin Zheng 
334609089f2SHongbin Zheng   NumberOfThreads = Builder.getInt32(0);
335609089f2SHongbin Zheng 
336609089f2SHongbin Zheng   // Add one as the upper bound provided by openmp is a < comparison
337609089f2SHongbin Zheng   // whereas the codegenForSequential function creates a <= comparison.
338c14582f2STobias Grosser   UpperBound =
339c14582f2STobias Grosser       Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
340609089f2SHongbin Zheng 
341609089f2SHongbin Zheng   createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
342609089f2SHongbin Zheng                               LowerBound, UpperBound, Stride);
343609089f2SHongbin Zheng   Builder.CreateCall(SubFunction, SubfunctionParam);
344609089f2SHongbin Zheng   createCallParallelEnd();
345609089f2SHongbin Zheng 
346609089f2SHongbin Zheng   return IV;
347609089f2SHongbin Zheng }
348