blob: 5815879ecc267ed79ed1753866b468cc7dcd41cd [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
32// / \
33// __ PreHeaderBB \
34// / \ / |
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 Grosser5db6ffd2013-05-16 06:40:06 +000053
Tobias Grosser42aff302014-01-13 22:29:56 +000054 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser3081b0f2013-05-16 06:40:24 +000055 LoopInfo &LI = P->getAnalysis<LoopInfo>();
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000056 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000057 LLVMContext &Context = F->getContext();
58
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000059 assert(LB->getType() == UB->getType() && "Types of loop bounds do not match");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000060 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
61 assert(LoopIVType && "UB is not integer?");
62
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000063 BasicBlock *BeforeBB = Builder.GetInsertBlock();
64 BasicBlock *GuardBB = BasicBlock::Create(Context, "polly.loop_if", F);
65 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
66 BasicBlock *PreHeaderBB =
67 BasicBlock::Create(Context, "polly.loop_preheader", F);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000068
Tobias Grosser37c9b8e2014-03-04 14:59:00 +000069 if (Annotator) {
70 Annotator->Begin(HeaderBB);
71 if (Parallel)
72 Annotator->SetCurrentParallel();
73 }
74
Tobias Grosser3081b0f2013-05-16 06:40:24 +000075 // Update LoopInfo
76 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
77 Loop *NewLoop = new Loop();
78
79 if (OuterLoop) {
80 OuterLoop->addChildLoop(NewLoop);
81 } else {
82 LI.addTopLevelLoop(NewLoop);
83 }
84
85 if (OuterLoop) {
86 OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
87 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
88 }
89
90 NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
91
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000092 // ExitBB
93 ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
94 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000095
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000096 // BeforeBB
97 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000098
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000099 // GuardBB
100 DT.addNewBlock(GuardBB, BeforeBB);
101 Builder.SetInsertPoint(GuardBB);
102 Value *LoopGuard;
103 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
104 LoopGuard->setName("polly.loop_guard");
105 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000106
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000107 // PreHeaderBB
108 DT.addNewBlock(PreHeaderBB, GuardBB);
109 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000110 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000111
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000112 // HeaderBB
113 DT.addNewBlock(HeaderBB, PreHeaderBB);
114 Builder.SetInsertPoint(HeaderBB);
115 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
116 IV->addIncoming(LB, PreHeaderBB);
117 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
118 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
119 Value *LoopCondition;
120 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
121 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
122 LoopCondition->setName("polly.loop_cond");
123 Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
124 IV->addIncoming(IncrementedIV, HeaderBB);
125 DT.changeImmediateDominator(ExitBB, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000126
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000127 // The loop body should be added here.
128 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000129 return IV;
130}
131
Tobias Grosserc14582f2013-02-05 18:01:29 +0000132void OMPGenerator::createCallParallelLoopStart(
133 Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
134 Value *LowerBound, Value *UpperBound, Value *Stride) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000135 Module *M = getModule();
136 const char *Name = "GOMP_parallel_loop_runtime_start";
137 Function *F = M->getFunction(Name);
138
139 // If F is not available, declare it.
140 if (!F) {
141 Type *LongTy = getIntPtrTy();
142 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
143
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000144 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
145 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
146 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
147 LongTy, LongTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000148
149 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
150 F = Function::Create(Ty, Linkage, Name, M);
151 }
152
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000153 Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads,
154 LowerBound, UpperBound, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000155
156 Builder.CreateCall(F, Args);
157}
158
Tobias Grossere602a072013-05-07 07:30:56 +0000159Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
160 Value *UpperBoundPtr) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000161 Module *M = getModule();
162 const char *Name = "GOMP_loop_runtime_next";
163 Function *F = M->getFunction(Name);
164
165 // If F is not available, declare it.
166 if (!F) {
167 Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
168 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
169
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000170 Type *Params[] = {LongPtrTy, LongPtrTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000171
172 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
173 F = Function::Create(Ty, Linkage, Name, M);
174 }
175
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000176 Value *Args[] = {LowerBoundPtr, UpperBoundPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000177
178 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000179 Return = Builder.CreateICmpNE(
180 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000181 return Return;
182}
183
184void OMPGenerator::createCallParallelEnd() {
185 const char *Name = "GOMP_parallel_end";
186 Module *M = getModule();
187 Function *F = M->getFunction(Name);
188
189 // If F is not available, declare it.
190 if (!F) {
191 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
192
193 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
194 F = Function::Create(Ty, Linkage, Name, M);
195 }
196
197 Builder.CreateCall(F);
198}
199
200void OMPGenerator::createCallLoopEndNowait() {
201 const char *Name = "GOMP_loop_end_nowait";
202 Module *M = getModule();
203 Function *F = M->getFunction(Name);
204
205 // If F is not available, declare it.
206 if (!F) {
207 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
208
209 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
210 F = Function::Create(Ty, Linkage, Name, M);
211 }
212
213 Builder.CreateCall(F);
214}
215
216IntegerType *OMPGenerator::getIntPtrTy() {
Rafael Espindolac5d16892014-02-25 19:17:57 +0000217 return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType(
218 Builder.getContext());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000219}
220
221Module *OMPGenerator::getModule() {
222 return Builder.GetInsertBlock()->getParent()->getParent();
223}
224
225Function *OMPGenerator::createSubfunctionDefinition() {
226 Module *M = getModule();
227 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000228 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000229 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
230 Function *FN = Function::Create(FT, Function::InternalLinkage,
231 F->getName() + ".omp_subfn", M);
232 // Do not run any polly pass on the new function.
233 P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN);
234
235 Function::arg_iterator AI = FN->arg_begin();
236 AI->setName("omp.userContext");
237
238 return FN;
239}
240
Tobias Grosserc14582f2013-02-05 18:01:29 +0000241Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
242 std::vector<Type *> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000243
244 for (unsigned i = 0; i < Values.size(); i++)
245 Members.push_back(Values[i]->getType());
246
247 StructType *Ty = StructType::get(Builder.getContext(), Members);
248 Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
249
250 for (unsigned i = 0; i < Values.size(); i++) {
251 Value *Address = Builder.CreateStructGEP(Struct, i);
252 Builder.CreateStore(Values[i], Address);
253 }
254
255 return Struct;
256}
257
Tobias Grossere602a072013-05-07 07:30:56 +0000258void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
259 Value *Struct,
260 ValueToValueMapTy &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000261 for (unsigned i = 0; i < OldValues.size(); i++) {
262 Value *Address = Builder.CreateStructGEP(Struct, i);
263 Value *NewValue = Builder.CreateLoad(Address);
Andy Gibbs36e6ca02013-03-20 15:42:59 +0000264 Map.insert(std::make_pair(OldValues[i], NewValue));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000265 }
266}
267
Tobias Grossere602a072013-05-07 07:30:56 +0000268Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
269 SetVector<Value *> Data,
270 ValueToValueMapTy &Map,
271 Function **SubFunction) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000272 Function *FN = createSubfunctionDefinition();
273
274 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000275 *AfterBB;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000276 Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000277 *LowerBound, *UpperBound, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000278 Type *IntPtrTy = getIntPtrTy();
279 LLVMContext &Context = FN->getContext();
280
281 // Store the previous basic block.
282 PrevBB = Builder.GetInsertBlock();
283
284 // Create basic blocks.
285 HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
286 ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
287 CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
288 LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
289
Tobias Grosser42aff302014-01-13 22:29:56 +0000290 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000291 DT.addNewBlock(HeaderBB, PrevBB);
292 DT.addNewBlock(ExitBB, HeaderBB);
293 DT.addNewBlock(CheckNextBB, HeaderBB);
294 DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
295
296 // Fill up basic block HeaderBB.
297 Builder.SetInsertPoint(HeaderBB);
298 LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
299 UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
300 UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
301 "omp.userContext");
302
303 extractValuesFromStruct(Data, UserContext, Map);
304 Builder.CreateBr(CheckNextBB);
305
306 // Add code to check if another set of iterations will be executed.
307 Builder.SetInsertPoint(CheckNextBB);
308 Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
309 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
310 "omp.hasNextScheduleBlock");
311 Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
312
313 // Add code to to load the iv bounds for this set of iterations.
314 Builder.SetInsertPoint(LoadIVBoundsBB);
315 LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
316 UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
317
318 // Subtract one as the upper bound provided by openmp is a < comparison
319 // whereas the codegenForSequential function creates a <= comparison.
320 UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
321 "omp.upperBoundAdjusted");
322
323 Builder.CreateBr(CheckNextBB);
324 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000325 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
326 ICmpInst::ICMP_SLE);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000327
328 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
329 Builder.SetInsertPoint(AfterBB->begin());
330
331 // Add code to terminate this openmp subfunction.
332 Builder.SetInsertPoint(ExitBB);
333 createCallLoopEndNowait();
334 Builder.CreateRetVoid();
335
336 Builder.SetInsertPoint(LoopBody);
337 *SubFunction = FN;
338
339 return IV;
340}
341
Tobias Grossere602a072013-05-07 07:30:56 +0000342Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
343 Value *Stride,
344 SetVector<Value *> &Values,
345 ValueToValueMapTy &Map,
346 BasicBlock::iterator *LoopBody) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000347 Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
348 Function *SubFunction;
349
350 Struct = loadValuesIntoStruct(Values);
351
352 BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
353 IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
354 *LoopBody = Builder.GetInsertPoint();
355 Builder.SetInsertPoint(PrevInsertPoint);
356
357 // Create call for GOMP_parallel_loop_runtime_start.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000358 SubfunctionParam =
359 Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000360
361 NumberOfThreads = Builder.getInt32(0);
362
363 // Add one as the upper bound provided by openmp is a < comparison
364 // whereas the codegenForSequential function creates a <= comparison.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000365 UpperBound =
366 Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000367
368 createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
369 LowerBound, UpperBound, Stride);
370 Builder.CreateCall(SubFunction, SubfunctionParam);
371 createCallParallelEnd();
372
373 return IV;
374}