blob: 960e0cca903a98335fa9fac31c5621bc9e873fed [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?");
Johannes Doerfertd3655372016-06-02 11:15:57 +000064 assert((LoopIVType == LB->getType() && LoopIVType == Stride->getType()) &&
65 "LB, UB and Stride should have equal types.");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000066
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000067 BasicBlock *BeforeBB = Builder.GetInsertBlock();
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000068 BasicBlock *GuardBB =
69 UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000070 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
71 BasicBlock *PreHeaderBB =
72 BasicBlock::Create(Context, "polly.loop_preheader", F);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000073
Tobias Grosser3081b0f2013-05-16 06:40:24 +000074 // Update LoopInfo
75 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
76 Loop *NewLoop = new Loop();
77
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000078 if (OuterLoop)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000079 OuterLoop->addChildLoop(NewLoop);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000080 else
Tobias Grosser3081b0f2013-05-16 06:40:24 +000081 LI.addTopLevelLoop(NewLoop);
Tobias Grosser3081b0f2013-05-16 06:40:24 +000082
Tobias Grosser154d9462014-11-25 17:09:21 +000083 if (OuterLoop) {
84 if (GuardBB)
Chandler Carruth6adcf562015-01-18 01:47:30 +000085 OuterLoop->addBasicBlockToLoop(GuardBB, LI);
86 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI);
Tobias Grosser154d9462014-11-25 17:09:21 +000087 }
Tobias Grosser3081b0f2013-05-16 06:40:24 +000088
Chandler Carruth6adcf562015-01-18 01:47:30 +000089 NewLoop->addBasicBlockToLoop(HeaderBB, LI);
Tobias Grosser3081b0f2013-05-16 06:40:24 +000090
Johannes Doerfertc7b719f2014-10-01 20:10:44 +000091 // Notify the annotator (if present) that we have a new loop, but only
92 // after the header block is set.
93 if (Annotator)
94 Annotator->pushLoop(NewLoop, Parallel);
95
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000096 // ExitBB
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +000097 ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI);
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000098 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000099
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000100 // BeforeBB
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000101 if (GuardBB) {
102 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
103 DT.addNewBlock(GuardBB, BeforeBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000104
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000105 // GuardBB
106 Builder.SetInsertPoint(GuardBB);
107 Value *LoopGuard;
108 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
109 LoopGuard->setName("polly.loop_guard");
110 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
111 DT.addNewBlock(PreHeaderBB, GuardBB);
112 } else {
113 BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
114 DT.addNewBlock(PreHeaderBB, BeforeBB);
115 }
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000116
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000117 // PreHeaderBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000118 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000119 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000120
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000121 // HeaderBB
122 DT.addNewBlock(HeaderBB, PreHeaderBB);
123 Builder.SetInsertPoint(HeaderBB);
124 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
125 IV->addIncoming(LB, PreHeaderBB);
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000126 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
127 Value *LoopCondition;
128 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
129 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
130 LoopCondition->setName("polly.loop_cond");
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000131
132 // Create the loop latch and annotate it as such.
133 BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
134 if (Annotator)
135 Annotator->annotateLoopLatch(B, NewLoop, Parallel);
136
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000137 IV->addIncoming(IncrementedIV, HeaderBB);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000138 if (GuardBB)
139 DT.changeImmediateDominator(ExitBB, GuardBB);
140 else
Tobias Grosserf8a678d2014-09-28 22:40:36 +0000141 DT.changeImmediateDominator(ExitBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000142
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000143 // The loop body should be added here.
144 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000145 return IV;
146}
147
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000148Value *ParallelLoopGenerator::createParallelLoop(
149 Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues,
Johannes Doerfert521dd582015-10-07 20:15:56 +0000150 ValueMapT &Map, BasicBlock::iterator *LoopBody) {
Johannes Doerfertd3655372016-06-02 11:15:57 +0000151
152 // Adjust the types to match the GOMP API.
153 LB = Builder.CreateSExt(LB, LongType);
154 UB = Builder.CreateSExt(UB, LongType);
155 Stride = Builder.CreateSExt(Stride, LongType);
156
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000157 Function *SubFn;
158
David Blaikief0e3d502015-04-05 22:51:12 +0000159 AllocaInst *Struct = storeValuesIntoStruct(UsedValues);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000160 BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint();
David Blaikief0e3d502015-04-05 22:51:12 +0000161 Value *IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000162 *LoopBody = Builder.GetInsertPoint();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000163 Builder.SetInsertPoint(&*BeforeLoop);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000164
David Blaikief0e3d502015-04-05 22:51:12 +0000165 Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
166 "polly.par.userContext");
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000167
168 // Add one as the upper bound provided by openmp is a < comparison
169 // whereas the codegenForSequential function creates a <= comparison.
170 UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1));
171
172 // Tell the runtime we start a parallel loop
173 createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride);
174 Builder.CreateCall(SubFn, SubFnParam);
175 createCallJoinThreads();
176
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000177 // Mark the end of the lifetime for the parameter struct.
178 Type *Ty = Struct->getType();
179 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
180 Builder.CreateLifetimeEnd(Struct, SizeOf);
181
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000182 return IV;
183}
184
185void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn,
186 Value *SubFnParam, Value *LB,
187 Value *UB, Value *Stride) {
188 const std::string Name = "GOMP_parallel_loop_runtime_start";
189
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000190 Function *F = M->getFunction(Name);
191
192 // If F is not available, declare it.
193 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000194 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
195
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000196 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
197 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
Johannes Doerferta4417832014-10-03 20:40:24 +0000198 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongType,
199 LongType, LongType};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000200
201 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
202 F = Function::Create(Ty, Linkage, Name, M);
203 }
204
Johannes Doerferta4417832014-10-03 20:40:24 +0000205 Value *NumberOfThreads = Builder.getInt32(PollyNumThreads);
Tobias Grosserd4ea2f42015-05-11 13:43:04 +0000206 Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000207
208 Builder.CreateCall(F, Args);
209}
210
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000211Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr,
212 Value *UBPtr) {
213 const std::string Name = "GOMP_loop_runtime_next";
214
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000215 Function *F = M->getFunction(Name);
216
217 // If F is not available, declare it.
218 if (!F) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000219 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000220 Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000221 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
222 F = Function::Create(Ty, Linkage, Name, M);
223 }
224
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000225 Value *Args[] = {LBPtr, UBPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000226 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000227 Return = Builder.CreateICmpNE(
228 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000229 return Return;
230}
231
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000232void ParallelLoopGenerator::createCallJoinThreads() {
233 const std::string Name = "GOMP_parallel_end";
234
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000235 Function *F = M->getFunction(Name);
236
237 // If F is not available, declare it.
238 if (!F) {
239 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
240
241 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
242 F = Function::Create(Ty, Linkage, Name, M);
243 }
244
Tobias Grosser1128b362015-05-19 06:25:02 +0000245 Builder.CreateCall(F, {});
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000246}
247
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000248void ParallelLoopGenerator::createCallCleanupThread() {
249 const std::string Name = "GOMP_loop_end_nowait";
250
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000251 Function *F = M->getFunction(Name);
252
253 // If F is not available, declare it.
254 if (!F) {
255 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
256
257 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
258 F = Function::Create(Ty, Linkage, Name, M);
259 }
260
Tobias Grosser1128b362015-05-19 06:25:02 +0000261 Builder.CreateCall(F, {});
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000262}
263
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000264Function *ParallelLoopGenerator::createSubFnDefinition() {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000265 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000266 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000267 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000268 Function *SubFn = Function::Create(FT, Function::InternalLinkage,
Tobias Grossera89dc572015-09-08 06:22:17 +0000269 F->getName() + "_polly_subfn", M);
270
271 // Certain backends (e.g., NVPTX) do not support '.'s in function names.
272 // Hence, we ensure that all '.'s are replaced by '_'s.
273 std::string FunctionName = SubFn->getName();
274 std::replace(FunctionName.begin(), FunctionName.end(), '.', '_');
275 SubFn->setName(FunctionName);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000276
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277 // Do not run any polly pass on the new function.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000278 SubFn->addFnAttr(PollySkipFnAttr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000279
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000280 Function::arg_iterator AI = SubFn->arg_begin();
281 AI->setName("polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000282
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000283 return SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000284}
285
David Blaikief0e3d502015-04-05 22:51:12 +0000286AllocaInst *
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000287ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) {
288 SmallVector<Type *, 8> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000289
Tobias Grosser91f5b262014-06-04 08:06:40 +0000290 for (Value *V : Values)
291 Members.push_back(V->getType());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000292
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000293 // We do not want to allocate the alloca inside any loop, thus we allocate it
294 // in the entry block of the function and use annotations to denote the actual
295 // live span (similar to clang).
296 BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000297 Instruction *IP = &*EntryBB.getFirstInsertionPt();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000298 StructType *Ty = StructType::get(Builder.getContext(), Members);
David Blaikief0e3d502015-04-05 22:51:12 +0000299 AllocaInst *Struct = new AllocaInst(Ty, 0, "polly.par.userContext", IP);
Johannes Doerfert1356ac72014-10-03 19:12:05 +0000300
301 // Mark the start of the lifetime for the parameter struct.
302 ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty));
303 Builder.CreateLifetimeStart(Struct, SizeOf);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000304
305 for (unsigned i = 0; i < Values.size(); i++) {
David Blaikief0e3d502015-04-05 22:51:12 +0000306 Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
Tobias Grosser95e59aa2015-09-28 16:46:38 +0000307 Address->setName("polly.subfn.storeaddr." + Values[i]->getName());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000308 Builder.CreateStore(Values[i], Address);
309 }
310
311 return Struct;
312}
313
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000314void ParallelLoopGenerator::extractValuesFromStruct(
Johannes Doerfert521dd582015-10-07 20:15:56 +0000315 SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000316 for (unsigned i = 0; i < OldValues.size(); i++) {
David Blaikief0e3d502015-04-05 22:51:12 +0000317 Value *Address = Builder.CreateStructGEP(Ty, Struct, i);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000318 Value *NewValue = Builder.CreateLoad(Address);
Tobias Grosser72b80672015-09-05 10:41:19 +0000319 NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName());
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000320 Map[OldValues[i]] = NewValue;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000321 }
322}
323
David Blaikief0e3d502015-04-05 22:51:12 +0000324Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData,
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000325 SetVector<Value *> Data,
Johannes Doerfert521dd582015-10-07 20:15:56 +0000326 ValueMapT &Map, Function **SubFnPtr) {
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000327 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB;
328 Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV;
329 Function *SubFn = createSubFnDefinition();
330 LLVMContext &Context = SubFn->getContext();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000331
332 // Store the previous basic block.
333 PrevBB = Builder.GetInsertBlock();
334
335 // Create basic blocks.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000336 HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn);
337 ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn);
338 CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn);
339 PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000340
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000341 DT.addNewBlock(HeaderBB, PrevBB);
342 DT.addNewBlock(ExitBB, HeaderBB);
343 DT.addNewBlock(CheckNextBB, HeaderBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000344 DT.addNewBlock(PreHeaderBB, HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000345
346 // Fill up basic block HeaderBB.
347 Builder.SetInsertPoint(HeaderBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000348 LBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.LBPtr");
349 UBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.UBPtr");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000350 UserContext = Builder.CreateBitCast(
351 &*SubFn->arg_begin(), StructData->getType(), "polly.par.userContext");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000352
David Blaikief0e3d502015-04-05 22:51:12 +0000353 extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext,
354 Map);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000355 Builder.CreateBr(CheckNextBB);
356
357 // Add code to check if another set of iterations will be executed.
358 Builder.SetInsertPoint(CheckNextBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000359 Ret1 = createCallGetWorkItem(LBPtr, UBPtr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000360 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000361 "polly.par.hasNextScheduleBlock");
362 Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000363
Tobias Grosserd8e3c8c2015-08-19 14:22:48 +0000364 // Add code to load the iv bounds for this set of iterations.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000365 Builder.SetInsertPoint(PreHeaderBB);
366 LB = Builder.CreateLoad(LBPtr, "polly.par.LB");
367 UB = Builder.CreateLoad(UBPtr, "polly.par.UB");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000368
369 // Subtract one as the upper bound provided by openmp is a < comparison
370 // whereas the codegenForSequential function creates a <= comparison.
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000371 UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1),
372 "polly.par.UBAdjusted");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000373
374 Builder.CreateBr(CheckNextBB);
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000375 Builder.SetInsertPoint(&*--Builder.GetInsertPoint());
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000376 IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000377 ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000378
379 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000380
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000381 // Add code to terminate this subfunction.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000382 Builder.SetInsertPoint(ExitBB);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000383 createCallCleanupThread();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000384 Builder.CreateRetVoid();
385
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000386 Builder.SetInsertPoint(&*LoopBody);
Johannes Doerfert12b355a2014-10-03 19:10:13 +0000387 *SubFnPtr = SubFn;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000388
389 return IV;
390}