blob: 432960d2b85ae98a2823c0371561557e36832e89 [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
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000026// We generate a loop of the following structure
27//
28// BeforeBB
29// |
30// v
31// GuardBB
Sebastian Pop0c9de712014-03-13 16:28:02 +000032// / |
33// __ PreHeaderBB |
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000034// / \ / |
35// latch HeaderBB |
36// \ / \ /
37// < \ /
38// \ /
39// ExitBB
40//
41// GuardBB checks if the loop is executed at least once. If this is the case
42// we branch to PreHeaderBB and subsequently to the HeaderBB, which contains the
43// loop iv 'polly.indvar', the incremented loop iv 'polly.indvar_next' as well
44// as the condition to check if we execute another iteration of the loop. After
45// the loop has finished, we branch to ExitBB.
46//
47// TODO: We currently always create the GuardBB. If we can prove the loop is
48// always executed at least once, we can get rid of this branch.
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000049Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
Tobias Grosser5103ba72014-03-04 14:58:49 +000050 PollyIRBuilder &Builder, Pass *P, BasicBlock *&ExitBB,
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000051 ICmpInst::Predicate Predicate,
52 LoopAnnotator *Annotator, bool Parallel) {
Tobias Grosser42aff302014-01-13 22:29:56 +000053 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser3081b0f2013-05-16 06:40:24 +000054 LoopInfo &LI = P->getAnalysis<LoopInfo>();
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000055 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000056 LLVMContext &Context = F->getContext();
57
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000058 assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000059 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
60 assert(LoopIVType && "UB is not integer?");
61
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000062 BasicBlock *BeforeBB = Builder.GetInsertBlock();
63 BasicBlock *GuardBB = BasicBlock::Create(Context, "polly.loop_if", F);
64 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
65 BasicBlock *PreHeaderBB =
66 BasicBlock::Create(Context, "polly.loop_preheader", F);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000067
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000068 if (Annotator) {
69 Annotator->Begin(HeaderBB);
70 if (Parallel)
71 Annotator->SetCurrentParallel();
72 }
73
Tobias Grosser3081b0f2013-05-16 06:40:24 +000074 // Update LoopInfo
75 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
76 Loop *NewLoop = new Loop();
77
78 if (OuterLoop) {
79 OuterLoop->addChildLoop(NewLoop);
80 } else {
81 LI.addTopLevelLoop(NewLoop);
82 }
83
84 if (OuterLoop) {
85 OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
86 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
87 }
88
89 NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
90
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000091 // ExitBB
92 ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
93 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000094
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000095 // BeforeBB
96 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000097
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000098 // GuardBB
99 DT.addNewBlock(GuardBB, BeforeBB);
100 Builder.SetInsertPoint(GuardBB);
101 Value *LoopGuard;
102 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
103 LoopGuard->setName("polly.loop_guard");
104 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000105
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000106 // PreHeaderBB
107 DT.addNewBlock(PreHeaderBB, GuardBB);
108 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000109 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000110
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000111 // HeaderBB
112 DT.addNewBlock(HeaderBB, PreHeaderBB);
113 Builder.SetInsertPoint(HeaderBB);
114 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
115 IV->addIncoming(LB, PreHeaderBB);
116 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
117 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
118 Value *LoopCondition;
119 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
120 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
121 LoopCondition->setName("polly.loop_cond");
122 Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
123 IV->addIncoming(IncrementedIV, HeaderBB);
124 DT.changeImmediateDominator(ExitBB, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000125
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000126 // The loop body should be added here.
127 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000128 return IV;
129}
130
Tobias Grosserc14582f2013-02-05 18:01:29 +0000131void OMPGenerator::createCallParallelLoopStart(
132 Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
133 Value *LowerBound, Value *UpperBound, Value *Stride) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000134 Module *M = getModule();
135 const char *Name = "GOMP_parallel_loop_runtime_start";
136 Function *F = M->getFunction(Name);
137
138 // If F is not available, declare it.
139 if (!F) {
140 Type *LongTy = getIntPtrTy();
141 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
142
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000143 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
144 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
145 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
146 LongTy, LongTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000147
148 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
149 F = Function::Create(Ty, Linkage, Name, M);
150 }
151
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000152 Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads,
153 LowerBound, UpperBound, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000154
155 Builder.CreateCall(F, Args);
156}
157
Tobias Grossere602a072013-05-07 07:30:56 +0000158Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
159 Value *UpperBoundPtr) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000160 Module *M = getModule();
161 const char *Name = "GOMP_loop_runtime_next";
162 Function *F = M->getFunction(Name);
163
164 // If F is not available, declare it.
165 if (!F) {
166 Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
167 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
168
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000169 Type *Params[] = {LongPtrTy, LongPtrTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000170
171 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
172 F = Function::Create(Ty, Linkage, Name, M);
173 }
174
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000175 Value *Args[] = {LowerBoundPtr, UpperBoundPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000176
177 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000178 Return = Builder.CreateICmpNE(
179 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000180 return Return;
181}
182
183void OMPGenerator::createCallParallelEnd() {
184 const char *Name = "GOMP_parallel_end";
185 Module *M = getModule();
186 Function *F = M->getFunction(Name);
187
188 // If F is not available, declare it.
189 if (!F) {
190 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
191
192 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
193 F = Function::Create(Ty, Linkage, Name, M);
194 }
195
196 Builder.CreateCall(F);
197}
198
199void OMPGenerator::createCallLoopEndNowait() {
200 const char *Name = "GOMP_loop_end_nowait";
201 Module *M = getModule();
202 Function *F = M->getFunction(Name);
203
204 // If F is not available, declare it.
205 if (!F) {
206 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
207
208 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
209 F = Function::Create(Ty, Linkage, Name, M);
210 }
211
212 Builder.CreateCall(F);
213}
214
215IntegerType *OMPGenerator::getIntPtrTy() {
Rafael Espindolac5d16892014-02-25 19:17:57 +0000216 return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType(
217 Builder.getContext());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000218}
219
220Module *OMPGenerator::getModule() {
221 return Builder.GetInsertBlock()->getParent()->getParent();
222}
223
224Function *OMPGenerator::createSubfunctionDefinition() {
225 Module *M = getModule();
226 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000227 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000228 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
229 Function *FN = Function::Create(FT, Function::InternalLinkage,
230 F->getName() + ".omp_subfn", M);
231 // Do not run any polly pass on the new function.
Johannes Doerfert43e1ead2014-07-15 21:06:48 +0000232 FN->addFnAttr(PollySkipFnAttr);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000233
234 Function::arg_iterator AI = FN->arg_begin();
235 AI->setName("omp.userContext");
236
237 return FN;
238}
239
Tobias Grosserc14582f2013-02-05 18:01:29 +0000240Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
241 std::vector<Type *> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000242
Tobias Grosser91f5b262014-06-04 08:06:40 +0000243 for (Value *V : Values)
244 Members.push_back(V->getType());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000245
246 StructType *Ty = StructType::get(Builder.getContext(), Members);
247 Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
248
249 for (unsigned i = 0; i < Values.size(); i++) {
250 Value *Address = Builder.CreateStructGEP(Struct, i);
251 Builder.CreateStore(Values[i], Address);
252 }
253
254 return Struct;
255}
256
Tobias Grossere602a072013-05-07 07:30:56 +0000257void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
258 Value *Struct,
259 ValueToValueMapTy &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000260 for (unsigned i = 0; i < OldValues.size(); i++) {
261 Value *Address = Builder.CreateStructGEP(Struct, i);
262 Value *NewValue = Builder.CreateLoad(Address);
Andy Gibbs36e6ca02013-03-20 15:42:59 +0000263 Map.insert(std::make_pair(OldValues[i], NewValue));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000264 }
265}
266
Tobias Grossere602a072013-05-07 07:30:56 +0000267Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
268 SetVector<Value *> Data,
269 ValueToValueMapTy &Map,
270 Function **SubFunction) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000271 Function *FN = createSubfunctionDefinition();
272
273 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000274 *AfterBB;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000275 Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000276 *LowerBound, *UpperBound, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277 Type *IntPtrTy = getIntPtrTy();
278 LLVMContext &Context = FN->getContext();
279
280 // Store the previous basic block.
281 PrevBB = Builder.GetInsertBlock();
282
283 // Create basic blocks.
284 HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
285 ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
286 CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
287 LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
288
Tobias Grosser42aff302014-01-13 22:29:56 +0000289 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000290 DT.addNewBlock(HeaderBB, PrevBB);
291 DT.addNewBlock(ExitBB, HeaderBB);
292 DT.addNewBlock(CheckNextBB, HeaderBB);
293 DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
294
295 // Fill up basic block HeaderBB.
296 Builder.SetInsertPoint(HeaderBB);
297 LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
298 UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
299 UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
300 "omp.userContext");
301
302 extractValuesFromStruct(Data, UserContext, Map);
303 Builder.CreateBr(CheckNextBB);
304
305 // Add code to check if another set of iterations will be executed.
306 Builder.SetInsertPoint(CheckNextBB);
307 Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
308 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
309 "omp.hasNextScheduleBlock");
310 Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
311
312 // Add code to to load the iv bounds for this set of iterations.
313 Builder.SetInsertPoint(LoadIVBoundsBB);
314 LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
315 UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
316
317 // Subtract one as the upper bound provided by openmp is a < comparison
318 // whereas the codegenForSequential function creates a <= comparison.
319 UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
320 "omp.upperBoundAdjusted");
321
322 Builder.CreateBr(CheckNextBB);
323 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000324 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
325 ICmpInst::ICMP_SLE);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000326
327 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
328 Builder.SetInsertPoint(AfterBB->begin());
329
330 // Add code to terminate this openmp subfunction.
331 Builder.SetInsertPoint(ExitBB);
332 createCallLoopEndNowait();
333 Builder.CreateRetVoid();
334
335 Builder.SetInsertPoint(LoopBody);
336 *SubFunction = FN;
337
338 return IV;
339}
340
Tobias Grossere602a072013-05-07 07:30:56 +0000341Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
342 Value *Stride,
343 SetVector<Value *> &Values,
344 ValueToValueMapTy &Map,
345 BasicBlock::iterator *LoopBody) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000346 Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
347 Function *SubFunction;
348
349 Struct = loadValuesIntoStruct(Values);
350
351 BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
352 IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
353 *LoopBody = Builder.GetInsertPoint();
354 Builder.SetInsertPoint(PrevInsertPoint);
355
356 // Create call for GOMP_parallel_loop_runtime_start.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000357 SubfunctionParam =
358 Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000359
360 NumberOfThreads = Builder.getInt32(0);
361
362 // Add one as the upper bound provided by openmp is a < comparison
363 // whereas the codegenForSequential function creates a <= comparison.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000364 UpperBound =
365 Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000366
367 createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
368 LowerBound, UpperBound, Stride);
369 Builder.CreateCall(SubFunction, SubfunctionParam);
370 createCallParallelEnd();
371
372 return IV;
373}