blob: 3f9e1b0f535e6e6dae72efac4a3795e6ef8d6df6 [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 Grosserc967d8e2012-10-16 07:29:13 +000051 ICmpInst::Predicate Predicate) {
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000052
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 Grosser3081b0f2013-05-16 06:40:24 +000068 // Update LoopInfo
69 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
70 Loop *NewLoop = new Loop();
71
72 if (OuterLoop) {
73 OuterLoop->addChildLoop(NewLoop);
74 } else {
75 LI.addTopLevelLoop(NewLoop);
76 }
77
78 if (OuterLoop) {
79 OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase());
80 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase());
81 }
82
83 NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase());
84
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000085 // ExitBB
86 ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P);
87 ExitBB->setName("polly.loop_exit");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000088
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000089 // BeforeBB
90 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000091
Tobias Grosser5db6ffd2013-05-16 06:40:06 +000092 // GuardBB
93 DT.addNewBlock(GuardBB, BeforeBB);
94 Builder.SetInsertPoint(GuardBB);
95 Value *LoopGuard;
96 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
97 LoopGuard->setName("polly.loop_guard");
98 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000099
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000100 // PreHeaderBB
101 DT.addNewBlock(PreHeaderBB, GuardBB);
102 Builder.SetInsertPoint(PreHeaderBB);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000103 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000104
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000105 // HeaderBB
106 DT.addNewBlock(HeaderBB, PreHeaderBB);
107 Builder.SetInsertPoint(HeaderBB);
108 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar");
109 IV->addIncoming(LB, PreHeaderBB);
110 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
111 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next");
112 Value *LoopCondition;
113 UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub");
114 LoopCondition = Builder.CreateICmp(Predicate, IV, UB);
115 LoopCondition->setName("polly.loop_cond");
116 Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
117 IV->addIncoming(IncrementedIV, HeaderBB);
118 DT.changeImmediateDominator(ExitBB, GuardBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000119
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000120 // The loop body should be added here.
121 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000122 return IV;
123}
124
Tobias Grosserc14582f2013-02-05 18:01:29 +0000125void OMPGenerator::createCallParallelLoopStart(
126 Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads,
127 Value *LowerBound, Value *UpperBound, Value *Stride) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000128 Module *M = getModule();
129 const char *Name = "GOMP_parallel_loop_runtime_start";
130 Function *F = M->getFunction(Name);
131
132 // If F is not available, declare it.
133 if (!F) {
134 Type *LongTy = getIntPtrTy();
135 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
136
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000137 Type *Params[] = {PointerType::getUnqual(FunctionType::get(
138 Builder.getVoidTy(), Builder.getInt8PtrTy(), false)),
139 Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy,
140 LongTy, LongTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000141
142 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
143 F = Function::Create(Ty, Linkage, Name, M);
144 }
145
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000146 Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads,
147 LowerBound, UpperBound, Stride};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000148
149 Builder.CreateCall(F, Args);
150}
151
Tobias Grossere602a072013-05-07 07:30:56 +0000152Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
153 Value *UpperBoundPtr) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000154 Module *M = getModule();
155 const char *Name = "GOMP_loop_runtime_next";
156 Function *F = M->getFunction(Name);
157
158 // If F is not available, declare it.
159 if (!F) {
160 Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
161 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
162
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000163 Type *Params[] = {LongPtrTy, LongPtrTy};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000164
165 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
166 F = Function::Create(Ty, Linkage, Name, M);
167 }
168
Tobias Grosser45bac0d2014-03-02 17:05:21 +0000169 Value *Args[] = {LowerBoundPtr, UpperBoundPtr};
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000170
171 Value *Return = Builder.CreateCall(F, Args);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000172 Return = Builder.CreateICmpNE(
173 Return, Builder.CreateZExt(Builder.getFalse(), Return->getType()));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000174 return Return;
175}
176
177void OMPGenerator::createCallParallelEnd() {
178 const char *Name = "GOMP_parallel_end";
179 Module *M = getModule();
180 Function *F = M->getFunction(Name);
181
182 // If F is not available, declare it.
183 if (!F) {
184 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
185
186 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
187 F = Function::Create(Ty, Linkage, Name, M);
188 }
189
190 Builder.CreateCall(F);
191}
192
193void OMPGenerator::createCallLoopEndNowait() {
194 const char *Name = "GOMP_loop_end_nowait";
195 Module *M = getModule();
196 Function *F = M->getFunction(Name);
197
198 // If F is not available, declare it.
199 if (!F) {
200 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
201
202 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
203 F = Function::Create(Ty, Linkage, Name, M);
204 }
205
206 Builder.CreateCall(F);
207}
208
209IntegerType *OMPGenerator::getIntPtrTy() {
Rafael Espindolac5d16892014-02-25 19:17:57 +0000210 return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType(
211 Builder.getContext());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000212}
213
214Module *OMPGenerator::getModule() {
215 return Builder.GetInsertBlock()->getParent()->getParent();
216}
217
218Function *OMPGenerator::createSubfunctionDefinition() {
219 Module *M = getModule();
220 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000221 std::vector<Type *> Arguments(1, Builder.getInt8PtrTy());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000222 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
223 Function *FN = Function::Create(FT, Function::InternalLinkage,
224 F->getName() + ".omp_subfn", M);
225 // Do not run any polly pass on the new function.
226 P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN);
227
228 Function::arg_iterator AI = FN->arg_begin();
229 AI->setName("omp.userContext");
230
231 return FN;
232}
233
Tobias Grosserc14582f2013-02-05 18:01:29 +0000234Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) {
235 std::vector<Type *> Members;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000236
237 for (unsigned i = 0; i < Values.size(); i++)
238 Members.push_back(Values[i]->getType());
239
240 StructType *Ty = StructType::get(Builder.getContext(), Members);
241 Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
242
243 for (unsigned i = 0; i < Values.size(); i++) {
244 Value *Address = Builder.CreateStructGEP(Struct, i);
245 Builder.CreateStore(Values[i], Address);
246 }
247
248 return Struct;
249}
250
Tobias Grossere602a072013-05-07 07:30:56 +0000251void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues,
252 Value *Struct,
253 ValueToValueMapTy &Map) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000254 for (unsigned i = 0; i < OldValues.size(); i++) {
255 Value *Address = Builder.CreateStructGEP(Struct, i);
256 Value *NewValue = Builder.CreateLoad(Address);
Andy Gibbs36e6ca02013-03-20 15:42:59 +0000257 Map.insert(std::make_pair(OldValues[i], NewValue));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000258 }
259}
260
Tobias Grossere602a072013-05-07 07:30:56 +0000261Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
262 SetVector<Value *> Data,
263 ValueToValueMapTy &Map,
264 Function **SubFunction) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000265 Function *FN = createSubfunctionDefinition();
266
267 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000268 *AfterBB;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000269 Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000270 *LowerBound, *UpperBound, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000271 Type *IntPtrTy = getIntPtrTy();
272 LLVMContext &Context = FN->getContext();
273
274 // Store the previous basic block.
275 PrevBB = Builder.GetInsertBlock();
276
277 // Create basic blocks.
278 HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
279 ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
280 CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
281 LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
282
Tobias Grosser42aff302014-01-13 22:29:56 +0000283 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000284 DT.addNewBlock(HeaderBB, PrevBB);
285 DT.addNewBlock(ExitBB, HeaderBB);
286 DT.addNewBlock(CheckNextBB, HeaderBB);
287 DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
288
289 // Fill up basic block HeaderBB.
290 Builder.SetInsertPoint(HeaderBB);
291 LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
292 UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
293 UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
294 "omp.userContext");
295
296 extractValuesFromStruct(Data, UserContext, Map);
297 Builder.CreateBr(CheckNextBB);
298
299 // Add code to check if another set of iterations will be executed.
300 Builder.SetInsertPoint(CheckNextBB);
301 Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
302 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
303 "omp.hasNextScheduleBlock");
304 Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
305
306 // Add code to to load the iv bounds for this set of iterations.
307 Builder.SetInsertPoint(LoadIVBoundsBB);
308 LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
309 UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
310
311 // Subtract one as the upper bound provided by openmp is a < comparison
312 // whereas the codegenForSequential function creates a <= comparison.
313 UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
314 "omp.upperBoundAdjusted");
315
316 Builder.CreateBr(CheckNextBB);
317 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000318 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
319 ICmpInst::ICMP_SLE);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000320
321 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
322 Builder.SetInsertPoint(AfterBB->begin());
323
324 // Add code to terminate this openmp subfunction.
325 Builder.SetInsertPoint(ExitBB);
326 createCallLoopEndNowait();
327 Builder.CreateRetVoid();
328
329 Builder.SetInsertPoint(LoopBody);
330 *SubFunction = FN;
331
332 return IV;
333}
334
Tobias Grossere602a072013-05-07 07:30:56 +0000335Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
336 Value *Stride,
337 SetVector<Value *> &Values,
338 ValueToValueMapTy &Map,
339 BasicBlock::iterator *LoopBody) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000340 Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
341 Function *SubFunction;
342
343 Struct = loadValuesIntoStruct(Values);
344
345 BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
346 IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
347 *LoopBody = Builder.GetInsertPoint();
348 Builder.SetInsertPoint(PrevInsertPoint);
349
350 // Create call for GOMP_parallel_loop_runtime_start.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000351 SubfunctionParam =
352 Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000353
354 NumberOfThreads = Builder.getInt32(0);
355
356 // Add one as the upper bound provided by openmp is a < comparison
357 // whereas the codegenForSequential function creates a <= comparison.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000358 UpperBound =
359 Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1));
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000360
361 createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
362 LowerBound, UpperBound, Stride);
363 Builder.CreateCall(SubFunction, SubfunctionParam);
364 createCallParallelEnd();
365
366 return IV;
367}