blob: 149cf2cef734bee9ad705e12dee7a7184397ae43 [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
Hongbin Zheng8a846612012-04-25 13:18:28 +000014#include "polly/CodeGen/LoopGenerators.h"
Tobias Grosser5624d3c2015-12-21 12:38:56 +000015#include "polly/ScopDetection.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"
Johannes Doerfert990cd4c2014-10-03 19:11:10 +000020#include "llvm/Support/CommandLine.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.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
Tobias Grosser154d9462014-11-25 17:09:21 +000081 if (OuterLoop) {
82 if (GuardBB)
Chandler Carruth6adcf562015-01-18 01:47:30 +000083 OuterLoop->addBasicBlockToLoop(GuardBB, LI);
84 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI);
Tobias Grosser154d9462014-11-25 17:09:21 +000085 }
Tobias Grosser3081b0f2013-05-16 06:40:24 +000086
Chandler Carruth6adcf562015-01-18 01:47:30 +000087 NewLoop->addBasicBlockToLoop(HeaderBB, LI);
Tobias Grosser3081b0f2013-05-16 06:40:24 +000088
Johannes Doerfertc7b719f2014-10-01 20:10:44 +000089 // Notify the annotator (if present) that we have a new loop, but only
90 // after the header block is set.
91 if (Annotator)
92 Annotator->pushLoop(NewLoop, Parallel);
93
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000094 // ExitBB
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +000095 ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI);
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000096 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000097
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000098 // BeforeBB
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000099 if (GuardBB) {
100 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
101 DT.addNewBlock(GuardBB, BeforeBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000102
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000103 // GuardBB
104 Builder.SetInsertPoint(GuardBB);
105 Value *LoopGuard;
106 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
107 LoopGuard->setName("polly.loop_guard");
108 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
109 DT.addNewBlock(PreHeaderBB, GuardBB);
110 } else {
111 BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
112 DT.addNewBlock(PreHeaderBB, BeforeBB);
113 }
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000114
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000115 // PreHeaderBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000116 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000117 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000118
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000119 // HeaderBB
120 DT.addNewBlock(HeaderBB, PreHeaderBB);
121 Builder.SetInsertPoint(HeaderBB);
122 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
123 IV->addIncoming(LB, PreHeaderBB);
Tobias Grosser3717aa52016-06-11 19:17:15 +0000124 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000125 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
126 Value *LoopCondition;
127 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
128 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
129 LoopCondition->setName("polly.loop_cond");
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000130
131 // Create the loop latch and annotate it as such.
132 BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
133 if (Annotator)
134 Annotator->annotateLoopLatch(B, NewLoop, Parallel);
135
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000136 IV->addIncoming(IncrementedIV, HeaderBB);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000137 if (GuardBB)
138 DT.changeImmediateDominator(ExitBB, GuardBB);
139 else
Tobias Grosserf8a678d2014-09-28 22:40:36 +0000140 DT.changeImmediateDominator(ExitBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000141
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000142 // The loop body should be added here.
143 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000144 return IV;
145}
146
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000147Value *ParallelLoopGenerator::createParallelLoop(
148 Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues,
Johannes Doerfert521dd582015-10-07 20:15:56 +0000149 ValueMapT &Map, BasicBlock::iterator *LoopBody) {
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000150 Function *SubFn;
151
David Blaikief0e3d502015-04-05 22:51:12 +0000152 AllocaInst *Struct = storeValuesIntoStruct(UsedValues);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000153 BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint();
David Blaikief0e3d502015-04-05 22:51:12 +0000154 Value *IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000155 *LoopBody = Builder.GetInsertPoint();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000156 Builder.SetInsertPoint(&*BeforeLoop);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000157
David Blaikief0e3d502015-04-05 22:51:12 +0000158 Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
159 "polly.par.userContext");
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000160
161 // Add one as the upper bound provided by openmp is a < comparison
162 // whereas the codegenForSequential function creates a <= comparison.
163 UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1));
164
165 // Tell the runtime we start a parallel loop
166 createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride);
167 Builder.CreateCall(SubFn, SubFnParam);
168 createCallJoinThreads();
169
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000170 // Mark the end of the lifetime for the parameter struct.
171 Type *Ty = Struct->getType();
172 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
173 Builder.CreateLifetimeEnd(Struct, SizeOf);
174
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000175 return IV;
176}
177
178void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn,
179 Value *SubFnParam, Value *LB,
180 Value *UB, Value *Stride) {
181 const std::string Name = "GOMP_parallel_loop_runtime_start";
182
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000183 Function *F = M->getFunction(Name);
184
185 // If F is not available, declare it.
186 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000187 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
188
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000189 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
190 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
Johannes Doerferta4417832014-10-03 20:40:24 +0000191 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongType,
192 LongType, LongType};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000193
194 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
195 F = Function::Create(Ty, Linkage, Name, M);
196 }
197
Johannes Doerferta4417832014-10-03 20:40:24 +0000198 Value *NumberOfThreads = Builder.getInt32(PollyNumThreads);
Tobias Grosserd4ea2f42015-05-11 13:43:04 +0000199 Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000200
201 Builder.CreateCall(F, Args);
202}
203
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000204Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr,
205 Value *UBPtr) {
206 const std::string Name = "GOMP_loop_runtime_next";
207
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000208 Function *F = M->getFunction(Name);
209
210 // If F is not available, declare it.
211 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000212 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000213 Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000214 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
215 F = Function::Create(Ty, Linkage, Name, M);
216 }
217
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000218 Value *Args[] = {LBPtr, UBPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000219 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000220 Return = Builder.CreateICmpNE(
221 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000222 return Return;
223}
224
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000225void ParallelLoopGenerator::createCallJoinThreads() {
226 const std::string Name = "GOMP_parallel_end";
227
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000228 Function *F = M->getFunction(Name);
229
230 // If F is not available, declare it.
231 if (!F) {
232 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
233
234 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
235 F = Function::Create(Ty, Linkage, Name, M);
236 }
237
Tobias Grosser1128b362015-05-19 06:25:02 +0000238 Builder.CreateCall(F, {});
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000239}
240
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000241void ParallelLoopGenerator::createCallCleanupThread() {
242 const std::string Name = "GOMP_loop_end_nowait";
243
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000244 Function *F = M->getFunction(Name);
245
246 // If F is not available, declare it.
247 if (!F) {
248 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
249
250 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
251 F = Function::Create(Ty, Linkage, Name, M);
252 }
253
Tobias Grosser1128b362015-05-19 06:25:02 +0000254 Builder.CreateCall(F, {});
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000255}
256
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000257Function *ParallelLoopGenerator::createSubFnDefinition() {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000258 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000259 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000260 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000261 Function *SubFn = Function::Create(FT, Function::InternalLinkage,
Tobias Grossera89dc572015-09-08 06:22:17 +0000262 F->getName() + "_polly_subfn", M);
263
264 // Certain backends (e.g., NVPTX) do not support '.'s in function names.
265 // Hence, we ensure that all '.'s are replaced by '_'s.
266 std::string FunctionName = SubFn->getName();
267 std::replace(FunctionName.begin(), FunctionName.end(), '.', '_');
268 SubFn->setName(FunctionName);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000269
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000270 // Do not run any polly pass on the new function.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000271 SubFn->addFnAttr(PollySkipFnAttr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000272
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000273 Function::arg_iterator AI = SubFn->arg_begin();
274 AI->setName("polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000275
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000276 return SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277}
278
David Blaikief0e3d502015-04-05 22:51:12 +0000279AllocaInst *
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000280ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) {
281 SmallVector<Type *, 8> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000282
Tobias Grosser91f5b262014-06-04 08:06:40 +0000283 for (Value *V : Values)
284 Members.push_back(V->getType());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000285
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000286 // We do not want to allocate the alloca inside any loop, thus we allocate it
287 // in the entry block of the function and use annotations to denote the actual
288 // live span (similar to clang).
289 BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000290 Instruction *IP = &*EntryBB.getFirstInsertionPt();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000291 StructType *Ty = StructType::get(Builder.getContext(), Members);
Tobias Grosser8dd653d2016-06-22 16:22:00 +0000292 AllocaInst *Struct = new AllocaInst(Ty, nullptr, "polly.par.userContext", IP);
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000293
294 // Mark the start of the lifetime for the parameter struct.
295 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
296 Builder.CreateLifetimeStart(Struct, SizeOf);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000297
298 for (unsigned i = 0; i < Values.size(); i++) {
David Blaikief0e3d502015-04-05 22:51:12 +0000299 Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
Tobias Grosser95e59aa2015-09-28 16:46:38 +0000300 Address->setName("polly.subfn.storeaddr." + Values[i]->getName());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000301 Builder.CreateStore(Values[i], Address);
302 }
303
304 return Struct;
305}
306
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000307void ParallelLoopGenerator::extractValuesFromStruct(
Johannes Doerfert521dd582015-10-07 20:15:56 +0000308 SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000309 for (unsigned i = 0; i < OldValues.size(); i++) {
David Blaikief0e3d502015-04-05 22:51:12 +0000310 Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000311 Value *NewValue = Builder.CreateLoad(Address);
Tobias Grosser72b80672015-09-05 10:41:19 +0000312 NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName());
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000313 Map[OldValues[i]] = NewValue;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000314 }
315}
316
David Blaikief0e3d502015-04-05 22:51:12 +0000317Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData,
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000318 SetVector<Value *> Data,
Johannes Doerfert521dd582015-10-07 20:15:56 +0000319 ValueMapT &Map, Function **SubFnPtr) {
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000320 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB;
321 Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV;
322 Function *SubFn = createSubFnDefinition();
323 LLVMContext &Context = SubFn->getContext();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000324
325 // Store the previous basic block.
326 PrevBB = Builder.GetInsertBlock();
327
328 // Create basic blocks.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000329 HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn);
330 ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn);
331 CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn);
332 PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000333
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000334 DT.addNewBlock(HeaderBB, PrevBB);
335 DT.addNewBlock(ExitBB, HeaderBB);
336 DT.addNewBlock(CheckNextBB, HeaderBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000337 DT.addNewBlock(PreHeaderBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000338
339 // Fill up basic block HeaderBB.
340 Builder.SetInsertPoint(HeaderBB);
Tobias Grosser8dd653d2016-06-22 16:22:00 +0000341 LBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.LBPtr");
342 UBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.UBPtr");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000343 UserContext = Builder.CreateBitCast(
344 &*SubFn->arg_begin(), StructData->getType(), "polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000345
David Blaikief0e3d502015-04-05 22:51:12 +0000346 extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext,
347 Map);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000348 Builder.CreateBr(CheckNextBB);
349
350 // Add code to check if another set of iterations will be executed.
351 Builder.SetInsertPoint(CheckNextBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000352 Ret1 = createCallGetWorkItem(LBPtr, UBPtr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000353 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000354 "polly.par.hasNextScheduleBlock");
355 Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000356
Tobias Grosserd8e3c8c2015-08-19 14:22:48 +0000357 // Add code to load the iv bounds for this set of iterations.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000358 Builder.SetInsertPoint(PreHeaderBB);
359 LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
360 UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000361
362 // Subtract one as the upper bound provided by openmp is a < comparison
363 // whereas the codegenForSequential function creates a <= comparison.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000364 UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1),
365 "polly.par.UBAdjusted");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000366
367 Builder.CreateBr(CheckNextBB);
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000368 Builder.SetInsertPoint(&*--Builder.GetInsertPoint());
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000369 IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000370 ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000371
372 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000373
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000374 // Add code to terminate this subfunction.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000375 Builder.SetInsertPoint(ExitBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000376 createCallCleanupThread();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000377 Builder.CreateRetVoid();
378
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000379 Builder.SetInsertPoint(&*LoopBody);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000380 *SubFnPtr = SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000381
382 return IV;
383}