blob: 267caeb5001bee434748cee4c14ebd8afce3a544 [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//
10// This file contains functions to create scalar and OpenMP parallel loops
11// as LLVM-IR.
12//
13//===----------------------------------------------------------------------===//
14
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000015#include "polly/ScopDetection.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000016#include "polly/CodeGen/LoopGenerators.h"
Tobias Grosser3081b0f2013-05-16 06:40:24 +000017#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth535d52c2013-01-02 11:47:44 +000018#include "llvm/IR/DataLayout.h"
Chandler Carruthe87c6a82014-01-13 09:56:11 +000019#include "llvm/IR/Dominators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "llvm/IR/Module.h"
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000021#include "llvm/Transforms/Utils/BasicBlockUtils.h"
22
23using namespace llvm;
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000024using namespace polly;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000025
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000026// We generate a loop of either of the following structures:
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000027//
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000028// BeforeBB BeforeBB
29// | |
30// v v
31// GuardBB PreHeaderBB
32// / | | _____
33// __ PreHeaderBB | v \/ |
34// / \ / | HeaderBB latch
35// latch HeaderBB | |\ |
36// \ / \ / | \------/
37// < \ / |
38// \ / v
39// ExitBB ExitBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000040//
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000041// depending on whether or not we know that it is executed at least once. If
42// not, GuardBB checks if the loop is executed at least once. If this is the
43// case we branch to PreHeaderBB and subsequently to the HeaderBB, which
44// contains the loop iv 'polly.indvar', the incremented loop iv
45// 'polly.indvar_next' as well as the condition to check if we execute another
46// iteration of the loop. After the loop has finished, we branch to ExitBB.
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000047Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000048 PollyIRBuilder &Builder, Pass *P, LoopInfo &LI,
49 DominatorTree &DT, BasicBlock *&ExitBB,
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000050 ICmpInst::Predicate Predicate,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000051 LoopAnnotator *Annotator, bool Parallel,
52 bool UseGuard) {
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000053 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000054 LLVMContext &Context = F->getContext();
55
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000056 assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000057 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
58 assert(LoopIVType && "UB is not integer?");
59
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000060 BasicBlock *BeforeBB = Builder.GetInsertBlock();
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000061 BasicBlock *GuardBB =
62 UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000063 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
64 BasicBlock *PreHeaderBB =
65 BasicBlock::Create(Context, "polly.loop_preheader", F);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000066
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000067 if (Annotator) {
68 Annotator->Begin(HeaderBB);
69 if (Parallel)
70 Annotator->SetCurrentParallel();
71 }
72
Tobias Grosser3081b0f2013-05-16 06:40:24 +000073 // Update LoopInfo
74 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
75 Loop *NewLoop = new Loop();
76
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000077 if (OuterLoop)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000078 OuterLoop->addChildLoop(NewLoop);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000079 else
Tobias Grosser3081b0f2013-05-16 06:40:24 +000080 LI.addTopLevelLoop(NewLoop);
Tobias Grosser3081b0f2013-05-16 06:40:24 +000081
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000082 if (OuterLoop && GuardBB)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000083 OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000084 else if (OuterLoop)
Tobias Grosser3081b0f2013-05-16 06:40:24 +000085 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
Tobias Grosser3081b0f2013-05-16 06:40:24 +000086
87 NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
88
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000089 // ExitBB
90 ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
91 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000092
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000093 // BeforeBB
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000094 if (GuardBB) {
95 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
96 DT.addNewBlock(GuardBB, BeforeBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000097
Johannes Doerfertdd5c1442014-09-10 17:33:32 +000098 // GuardBB
99 Builder.SetInsertPoint(GuardBB);
100 Value *LoopGuard;
101 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
102 LoopGuard->setName("polly.loop_guard");
103 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
104 DT.addNewBlock(PreHeaderBB, GuardBB);
105 } else {
106 BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
107 DT.addNewBlock(PreHeaderBB, BeforeBB);
108 }
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000109
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000110 // PreHeaderBB
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000111 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000112 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000113
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000114 // HeaderBB
115 DT.addNewBlock(HeaderBB, PreHeaderBB);
116 Builder.SetInsertPoint(HeaderBB);
117 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
118 IV->addIncoming(LB, PreHeaderBB);
119 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
120 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
121 Value *LoopCondition;
122 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
123 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
124 LoopCondition->setName("polly.loop_cond");
125 Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
126 IV->addIncoming(IncrementedIV, HeaderBB);
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000127 if (GuardBB)
128 DT.changeImmediateDominator(ExitBB, GuardBB);
129 else
130 DT.changeImmediateDominator(ExitBB, BeforeBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000131
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000132 // The loop body should be added here.
133 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000134 return IV;
135}
136
Tobias Grosserc14582f2013-02-05 18:01:29 +0000137void OMPGenerator::createCallParallelLoopStart(
138 Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
139 Value *LowerBound, Value *UpperBound, Value *Stride) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000140 Module *M = getModule();
141 const char *Name = "GOMP_parallel_loop_runtime_start";
142 Function *F = M->getFunction(Name);
143
144 // If F is not available, declare it.
145 if (!F) {
146 Type *LongTy = getIntPtrTy();
147 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
148
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000149 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
150 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
151 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
152 LongTy, LongTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000153
154 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
155 F = Function::Create(Ty, Linkage, Name, M);
156 }
157
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000158 Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads,
159 LowerBound, UpperBound, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000160
161 Builder.CreateCall(F, Args);
162}
163
Tobias Grossere602a072013-05-07 07:30:56 +0000164Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
165 Value *UpperBoundPtr) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000166 Module *M = getModule();
167 const char *Name = "GOMP_loop_runtime_next";
168 Function *F = M->getFunction(Name);
169
170 // If F is not available, declare it.
171 if (!F) {
172 Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
173 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
174
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000175 Type *Params[] = {LongPtrTy, LongPtrTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000176
177 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
178 F = Function::Create(Ty, Linkage, Name, M);
179 }
180
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000181 Value *Args[] = {LowerBoundPtr, UpperBoundPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000182
183 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000184 Return = Builder.CreateICmpNE(
185 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000186 return Return;
187}
188
189void OMPGenerator::createCallParallelEnd() {
190 const char *Name = "GOMP_parallel_end";
191 Module *M = getModule();
192 Function *F = M->getFunction(Name);
193
194 // If F is not available, declare it.
195 if (!F) {
196 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
197
198 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
199 F = Function::Create(Ty, Linkage, Name, M);
200 }
201
202 Builder.CreateCall(F);
203}
204
205void OMPGenerator::createCallLoopEndNowait() {
206 const char *Name = "GOMP_loop_end_nowait";
207 Module *M = getModule();
208 Function *F = M->getFunction(Name);
209
210 // If F is not available, declare it.
211 if (!F) {
212 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
213
214 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
215 F = Function::Create(Ty, Linkage, Name, M);
216 }
217
218 Builder.CreateCall(F);
219}
220
221IntegerType *OMPGenerator::getIntPtrTy() {
Rafael Espindolac5d16892014-02-25 19:17:57 +0000222 return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType(
223 Builder.getContext());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000224}
225
226Module *OMPGenerator::getModule() {
227 return Builder.GetInsertBlock()->getParent()->getParent();
228}
229
230Function *OMPGenerator::createSubfunctionDefinition() {
231 Module *M = getModule();
232 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000233 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000234 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
235 Function *FN = Function::Create(FT, Function::InternalLinkage,
236 F->getName() + ".omp_subfn", M);
237 // Do not run any polly pass on the new function.
Johannes Doerfert43e1ead2014-07-15 21:06:48 +0000238 FN->addFnAttr(PollySkipFnAttr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000239
240 Function::arg_iterator AI = FN->arg_begin();
241 AI->setName("omp.userContext");
242
243 return FN;
244}
245
Tobias Grosserc14582f2013-02-05 18:01:29 +0000246Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
247 std::vector<Type *> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000248
Tobias Grosser91f5b262014-06-04 08:06:40 +0000249 for (Value *V : Values)
250 Members.push_back(V->getType());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000251
252 StructType *Ty = StructType::get(Builder.getContext(), Members);
253 Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
254
255 for (unsigned i = 0; i < Values.size(); i++) {
256 Value *Address = Builder.CreateStructGEP(Struct, i);
257 Builder.CreateStore(Values[i], Address);
258 }
259
260 return Struct;
261}
262
Tobias Grossere602a072013-05-07 07:30:56 +0000263void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
264 Value *Struct,
265 ValueToValueMapTy &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000266 for (unsigned i = 0; i < OldValues.size(); i++) {
267 Value *Address = Builder.CreateStructGEP(Struct, i);
268 Value *NewValue = Builder.CreateLoad(Address);
Andy Gibbs36e6ca02013-03-20 15:42:59 +0000269 Map.insert(std::make_pair(OldValues[i], NewValue));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000270 }
271}
272
Tobias Grossere602a072013-05-07 07:30:56 +0000273Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
274 SetVector<Value *> Data,
275 ValueToValueMapTy &Map,
276 Function **SubFunction) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277 Function *FN = createSubfunctionDefinition();
278
279 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000280 *AfterBB;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000281 Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000282 *LowerBound, *UpperBound, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000283 Type *IntPtrTy = getIntPtrTy();
284 LLVMContext &Context = FN->getContext();
285
286 // Store the previous basic block.
287 PrevBB = Builder.GetInsertBlock();
288
289 // Create basic blocks.
290 HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
291 ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
292 CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
293 LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
294
Tobias Grosser42aff302014-01-13 22:29:56 +0000295 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000296 DT.addNewBlock(HeaderBB, PrevBB);
297 DT.addNewBlock(ExitBB, HeaderBB);
298 DT.addNewBlock(CheckNextBB, HeaderBB);
299 DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
300
301 // Fill up basic block HeaderBB.
302 Builder.SetInsertPoint(HeaderBB);
303 LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
304 UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
305 UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
306 "omp.userContext");
307
308 extractValuesFromStruct(Data, UserContext, Map);
309 Builder.CreateBr(CheckNextBB);
310
311 // Add code to check if another set of iterations will be executed.
312 Builder.SetInsertPoint(CheckNextBB);
313 Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
314 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
315 "omp.hasNextScheduleBlock");
316 Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
317
318 // Add code to to load the iv bounds for this set of iterations.
319 Builder.SetInsertPoint(LoadIVBoundsBB);
320 LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
321 UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
322
323 // Subtract one as the upper bound provided by openmp is a < comparison
324 // whereas the codegenForSequential function creates a <= comparison.
325 UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
326 "omp.upperBoundAdjusted");
327
328 Builder.CreateBr(CheckNextBB);
329 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000330 LoopInfo &LI = P->getAnalysis<LoopInfo>();
331 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, LI, DT, AfterBB,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000332 ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000333
334 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
335 Builder.SetInsertPoint(AfterBB->begin());
336
337 // Add code to terminate this openmp subfunction.
338 Builder.SetInsertPoint(ExitBB);
339 createCallLoopEndNowait();
340 Builder.CreateRetVoid();
341
342 Builder.SetInsertPoint(LoopBody);
343 *SubFunction = FN;
344
345 return IV;
346}
347
Tobias Grossere602a072013-05-07 07:30:56 +0000348Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
349 Value *Stride,
350 SetVector<Value *> &Values,
351 ValueToValueMapTy &Map,
352 BasicBlock::iterator *LoopBody) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000353 Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
354 Function *SubFunction;
355
356 Struct = loadValuesIntoStruct(Values);
357
358 BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
359 IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
360 *LoopBody = Builder.GetInsertPoint();
361 Builder.SetInsertPoint(PrevInsertPoint);
362
363 // Create call for GOMP_parallel_loop_runtime_start.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000364 SubfunctionParam =
365 Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000366
367 NumberOfThreads = Builder.getInt32(0);
368
369 // Add one as the upper bound provided by openmp is a < comparison
370 // whereas the codegenForSequential function creates a <= comparison.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000371 UpperBound =
372 Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000373
374 createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
375 LowerBound, UpperBound, Stride);
376 Builder.CreateCall(SubFunction, SubfunctionParam);
377 createCallParallelEnd();
378
379 return IV;
380}