blob: c0994b3d643959b2df2611eb49aba998bb8da750 [file] [log] [blame]
Tobias Grosserf74a4cd2012-03-23 10:35:18 +00001//===------ LoopGenerators.cpp - IR helper to create loops ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Johannes Doerfert12b355a2014-10-03 19:10:13 +000010// This file contains functions to create scalar and parallel loops as LLVM-IR.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000011//
12//===----------------------------------------------------------------------===//
13
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000014#include "polly/ScopDetection.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000015#include "polly/CodeGen/LoopGenerators.h"
Tobias Grosser3081b0f2013-05-16 06:40:24 +000016#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth535d52c2013-01-02 11:47:44 +000017#include "llvm/IR/DataLayout.h"
Chandler Carruthe87c6a82014-01-13 09:56:11 +000018#include "llvm/IR/Dominators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000019#include "llvm/IR/Module.h"
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Johannes Doerfert990cd4c2014-10-03 19:11:10 +000021#include "llvm/Support/CommandLine.h"
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000022
23using namespace llvm;
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000024using namespace polly;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000025
Johannes Doerfert990cd4c2014-10-03 19:11:10 +000026static cl::opt<int>
27 PollyNumThreads("polly-num-threads",
28 cl::desc("Number of threads to use (0 = auto)"), cl::Hidden,
29 cl::init(0));
30
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000031// We generate a loop of either of the following structures:
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000032//
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000033// BeforeBB BeforeBB
34// | |
35// v v
36// GuardBB PreHeaderBB
37// / | | _____
38// __ PreHeaderBB | v \/ |
39// / \ / | HeaderBB latch
40// latch HeaderBB | |\ |
41// \ / \ / | \------/
42// < \ / |
43// \ / v
44// ExitBB ExitBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000045//
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000046// depending on whether or not we know that it is executed at least once. If
47// not, GuardBB checks if the loop is executed at least once. If this is the
48// case we branch to PreHeaderBB and subsequently to the HeaderBB, which
49// contains the loop iv 'polly.indvar', the incremented loop iv
50// 'polly.indvar_next' as well as the condition to check if we execute another
51// iteration of the loop. After the loop has finished, we branch to ExitBB.
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000052Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000053 PollyIRBuilder &Builder, Pass *P, LoopInfo &LI,
54 DominatorTree &DT, BasicBlock *&ExitBB,
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000055 ICmpInst::Predicate Predicate,
Johannes Doerfert51d1c742014-10-02 15:32:17 +000056 ScopAnnotator *Annotator, bool Parallel,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000057 bool UseGuard) {
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000058 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000059 LLVMContext &Context = F->getContext();
60
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000061 assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000062 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
63 assert(LoopIVType && "UB is not integer?");
64
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000065 BasicBlock *BeforeBB = Builder.GetInsertBlock();
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000066 BasicBlock *GuardBB =
67 UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000068 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
69 BasicBlock *PreHeaderBB =
70 BasicBlock::Create(Context, "polly.loop_preheader", F);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000071
Tobias Grosser3081b0f2013-05-16 06:40:24 +000072 // Update LoopInfo
73 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
74 Loop *NewLoop = new Loop();
75
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000076 if (OuterLoop)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000077 OuterLoop->addChildLoop(NewLoop);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000078 else
Tobias Grosser3081b0f2013-05-16 06:40:24 +000079 LI.addTopLevelLoop(NewLoop);
Tobias Grosser3081b0f2013-05-16 06:40:24 +000080
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000081 if (OuterLoop && GuardBB)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000082 OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000083 else if (OuterLoop)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000084 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
Tobias Grosser3081b0f2013-05-16 06:40:24 +000085
86 NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
87
Johannes Doerfertc7b719f2014-10-01 20:10:44 +000088 // Notify the annotator (if present) that we have a new loop, but only
89 // after the header block is set.
90 if (Annotator)
91 Annotator->pushLoop(NewLoop, Parallel);
92
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000093 // ExitBB
94 ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
95 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000096
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000097 // BeforeBB
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000098 if (GuardBB) {
99 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
100 DT.addNewBlock(GuardBB, BeforeBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000101
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000102 // GuardBB
103 Builder.SetInsertPoint(GuardBB);
104 Value *LoopGuard;
105 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
106 LoopGuard->setName("polly.loop_guard");
107 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
108 DT.addNewBlock(PreHeaderBB, GuardBB);
109 } else {
110 BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
111 DT.addNewBlock(PreHeaderBB, BeforeBB);
112 }
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000113
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000114 // PreHeaderBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000115 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000116 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000117
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000118 // HeaderBB
119 DT.addNewBlock(HeaderBB, PreHeaderBB);
120 Builder.SetInsertPoint(HeaderBB);
121 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
122 IV->addIncoming(LB, PreHeaderBB);
123 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
124 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
125 Value *LoopCondition;
126 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
127 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
128 LoopCondition->setName("polly.loop_cond");
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000129
130 // Create the loop latch and annotate it as such.
131 BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
132 if (Annotator)
133 Annotator->annotateLoopLatch(B, NewLoop, Parallel);
134
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000135 IV->addIncoming(IncrementedIV, HeaderBB);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000136 if (GuardBB)
137 DT.changeImmediateDominator(ExitBB, GuardBB);
138 else
Tobias Grosserf8a678d2014-09-28 22:40:36 +0000139 DT.changeImmediateDominator(ExitBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000140
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000141 // The loop body should be added here.
142 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000143 return IV;
144}
145
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000146Value *ParallelLoopGenerator::createParallelLoop(
147 Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues,
148 ValueToValueMapTy &Map, BasicBlock::iterator *LoopBody) {
149 Value *Struct, *IV, *SubFnParam;
150 Function *SubFn;
151
152 Struct = storeValuesIntoStruct(UsedValues);
153
154 BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint();
155 IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn);
156 *LoopBody = Builder.GetInsertPoint();
157 Builder.SetInsertPoint(BeforeLoop);
158
159 SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
160 "polly.par.userContext");
161
162 // Add one as the upper bound provided by openmp is a < comparison
163 // whereas the codegenForSequential function creates a <= comparison.
164 UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1));
165
166 // Tell the runtime we start a parallel loop
167 createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride);
168 Builder.CreateCall(SubFn, SubFnParam);
169 createCallJoinThreads();
170
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000171 // Mark the end of the lifetime for the parameter struct.
172 Type *Ty = Struct->getType();
173 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
174 Builder.CreateLifetimeEnd(Struct, SizeOf);
175
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000176 return IV;
177}
178
179void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn,
180 Value *SubFnParam, Value *LB,
181 Value *UB, Value *Stride) {
182 const std::string Name = "GOMP_parallel_loop_runtime_start";
183
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000184 Function *F = M->getFunction(Name);
185
186 // If F is not available, declare it.
187 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000188 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
189
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000190 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
191 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000192 Builder.getInt8PtrTy(), LongType, LongType, LongType,
193 LongType};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000194
195 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
196 F = Function::Create(Ty, Linkage, Name, M);
197 }
198
Johannes Doerfert990cd4c2014-10-03 19:11:10 +0000199 Value *NumberOfThreads = ConstantInt::get(LongType, PollyNumThreads);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000200 Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000201
202 Builder.CreateCall(F, Args);
203}
204
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000205Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr,
206 Value *UBPtr) {
207 const std::string Name = "GOMP_loop_runtime_next";
208
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000209 Function *F = M->getFunction(Name);
210
211 // If F is not available, declare it.
212 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000213 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000214 Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000215 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
216 F = Function::Create(Ty, Linkage, Name, M);
217 }
218
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000219 Value *Args[] = {LBPtr, UBPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000220 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000221 Return = Builder.CreateICmpNE(
222 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000223 return Return;
224}
225
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000226void ParallelLoopGenerator::createCallJoinThreads() {
227 const std::string Name = "GOMP_parallel_end";
228
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000229 Function *F = M->getFunction(Name);
230
231 // If F is not available, declare it.
232 if (!F) {
233 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
234
235 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
236 F = Function::Create(Ty, Linkage, Name, M);
237 }
238
239 Builder.CreateCall(F);
240}
241
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000242void ParallelLoopGenerator::createCallCleanupThread() {
243 const std::string Name = "GOMP_loop_end_nowait";
244
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000245 Function *F = M->getFunction(Name);
246
247 // If F is not available, declare it.
248 if (!F) {
249 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
250
251 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
252 F = Function::Create(Ty, Linkage, Name, M);
253 }
254
255 Builder.CreateCall(F);
256}
257
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000258Function *ParallelLoopGenerator::createSubFnDefinition() {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000259 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000260 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000261 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000262 Function *SubFn = Function::Create(FT, Function::InternalLinkage,
263 F->getName() + ".polly.subfn", M);
264
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000265 // Do not run any polly pass on the new function.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000266 SubFn->addFnAttr(PollySkipFnAttr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000267
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000268 Function::arg_iterator AI = SubFn->arg_begin();
269 AI->setName("polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000270
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000271 return SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000272}
273
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000274Value *
275ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) {
276 SmallVector<Type *, 8> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277
Tobias Grosser91f5b262014-06-04 08:06:40 +0000278 for (Value *V : Values)
279 Members.push_back(V->getType());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000280
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000281 // We do not want to allocate the alloca inside any loop, thus we allocate it
282 // in the entry block of the function and use annotations to denote the actual
283 // live span (similar to clang).
284 BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock();
285 Instruction *IP = EntryBB.getFirstInsertionPt();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000286 StructType *Ty = StructType::get(Builder.getContext(), Members);
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000287 Value *Struct = new AllocaInst(Ty, 0, "polly.par.userContext", IP);
288
289 // Mark the start of the lifetime for the parameter struct.
290 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
291 Builder.CreateLifetimeStart(Struct, SizeOf);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000292
293 for (unsigned i = 0; i < Values.size(); i++) {
294 Value *Address = Builder.CreateStructGEP(Struct, i);
295 Builder.CreateStore(Values[i], Address);
296 }
297
298 return Struct;
299}
300
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000301void ParallelLoopGenerator::extractValuesFromStruct(
302 SetVector<Value *> OldValues, Value *Struct, ValueToValueMapTy &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000303 for (unsigned i = 0; i < OldValues.size(); i++) {
304 Value *Address = Builder.CreateStructGEP(Struct, i);
305 Value *NewValue = Builder.CreateLoad(Address);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000306 Map[OldValues[i]] = NewValue;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000307 }
308}
309
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000310Value *ParallelLoopGenerator::createSubFn(Value *Stride, Value *StructData,
311 SetVector<Value *> Data,
312 ValueToValueMapTy &Map,
313 Function **SubFnPtr) {
314 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB;
315 Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV;
316 Function *SubFn = createSubFnDefinition();
317 LLVMContext &Context = SubFn->getContext();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000318
319 // Store the previous basic block.
320 PrevBB = Builder.GetInsertBlock();
321
322 // Create basic blocks.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000323 HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn);
324 ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn);
325 CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn);
326 PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000327
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000328 DT.addNewBlock(HeaderBB, PrevBB);
329 DT.addNewBlock(ExitBB, HeaderBB);
330 DT.addNewBlock(CheckNextBB, HeaderBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000331 DT.addNewBlock(PreHeaderBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000332
333 // Fill up basic block HeaderBB.
334 Builder.SetInsertPoint(HeaderBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000335 LBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.LBPtr");
336 UBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.UBPtr");
337 UserContext = Builder.CreateBitCast(SubFn->arg_begin(), StructData->getType(),
338 "polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000339
340 extractValuesFromStruct(Data, UserContext, Map);
341 Builder.CreateBr(CheckNextBB);
342
343 // Add code to check if another set of iterations will be executed.
344 Builder.SetInsertPoint(CheckNextBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000345 Ret1 = createCallGetWorkItem(LBPtr, UBPtr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000346 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000347 "polly.par.hasNextScheduleBlock");
348 Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000349
350 // Add code to to load the iv bounds for this set of iterations.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000351 Builder.SetInsertPoint(PreHeaderBB);
352 LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
353 UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000354
355 // Subtract one as the upper bound provided by openmp is a < comparison
356 // whereas the codegenForSequential function creates a <= comparison.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000357 UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1),
358 "polly.par.UBAdjusted");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000359
360 Builder.CreateBr(CheckNextBB);
361 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000362 IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000363 ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000364
365 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000366
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000367 // Add code to terminate this subfunction.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000368 Builder.SetInsertPoint(ExitBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000369 createCallCleanupThread();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000370 Builder.CreateRetVoid();
371
372 Builder.SetInsertPoint(LoopBody);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000373 *SubFnPtr = SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000374
375 return IV;
376}