blob: 230cb6369c805b5df3b8e3821f477c3d52b727ac [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 Grosserf74a4cd2012-03-23 10:35:18 +000017
18#include "llvm/Module.h"
19#include "llvm/Analysis/Dominators.h"
Micah Villmow7a3d8202012-10-08 17:26:19 +000020#include "llvm/DataLayout.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
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000026Value *polly::createLoop(Value *LB, Value *UB, Value *Stride,
Tobias Grosser1bb59b02012-12-29 23:47:38 +000027 IRBuilder<> &Builder, Pass *P, BasicBlock *&AfterBlock,
Tobias Grosserc967d8e2012-10-16 07:29:13 +000028 ICmpInst::Predicate Predicate) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000029 DominatorTree &DT = P->getAnalysis<DominatorTree>();
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000030 Function *F = Builder.GetInsertBlock()->getParent();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000031 LLVMContext &Context = F->getContext();
32
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000033 BasicBlock *PreheaderBB = Builder.GetInsertBlock();
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000034 BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F);
35 BasicBlock *BodyBB = BasicBlock::Create(Context, "polly.loop_body", F);
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000036 BasicBlock *AfterBB = SplitBlock(PreheaderBB, Builder.GetInsertPoint()++, P);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000037 AfterBB->setName("polly.loop_after");
38
39 PreheaderBB->getTerminator()->setSuccessor(0, HeaderBB);
40 DT.addNewBlock(HeaderBB, PreheaderBB);
41
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000042 Builder.SetInsertPoint(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000043
44 // Use the type of upper and lower bound.
Tobias Grosserae2d83e2012-12-29 23:57:18 +000045 assert(LB->getType() == UB->getType() &&
46 "Different types for upper and lower bound.");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000047
48 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
49 assert(LoopIVType && "UB is not integer?");
50
51 // IV
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000052 PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.loopiv");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000053 IV->addIncoming(LB, PreheaderBB);
54
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000055 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
Tobias Grosser400a4ac2012-05-29 09:11:59 +000056 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.next_loopiv");
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000057
58 // Exit condition.
59 Value *CMP;
Tobias Grosserc967d8e2012-10-16 07:29:13 +000060 CMP = Builder.CreateICmp(Predicate, IV, UB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000061
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000062 Builder.CreateCondBr(CMP, BodyBB, AfterBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000063 DT.addNewBlock(BodyBB, HeaderBB);
64
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000065 Builder.SetInsertPoint(BodyBB);
66 Builder.CreateBr(HeaderBB);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000067 IV->addIncoming(IncrementedIV, BodyBB);
68 DT.changeImmediateDominator(AfterBB, HeaderBB);
69
Hongbin Zheng4ac4e152012-04-23 13:03:56 +000070 Builder.SetInsertPoint(BodyBB->begin());
71 AfterBlock = AfterBB;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +000072
73 return IV;
74}
75
76void OMPGenerator::createCallParallelLoopStart(Value *SubFunction,
77 Value *SubfunctionParam,
78 Value *NumberOfThreads,
79 Value *LowerBound,
80 Value *UpperBound,
81 Value *Stride) {
82 Module *M = getModule();
83 const char *Name = "GOMP_parallel_loop_runtime_start";
84 Function *F = M->getFunction(Name);
85
86 // If F is not available, declare it.
87 if (!F) {
88 Type *LongTy = getIntPtrTy();
89 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
90
91 Type *Params[] = {
92 PointerType::getUnqual(FunctionType::get(Builder.getVoidTy(),
93 Builder.getInt8PtrTy(),
94 false)),
95 Builder.getInt8PtrTy(),
96 Builder.getInt32Ty(),
97 LongTy,
98 LongTy,
99 LongTy,
100 };
101
102 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false);
103 F = Function::Create(Ty, Linkage, Name, M);
104 }
105
106 Value *Args[] = {
107 SubFunction,
108 SubfunctionParam,
109 NumberOfThreads,
110 LowerBound,
111 UpperBound,
112 Stride,
113 };
114
115 Builder.CreateCall(F, Args);
116}
117
118Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr,
119 Value *UpperBoundPtr) {
120 Module *M = getModule();
121 const char *Name = "GOMP_loop_runtime_next";
122 Function *F = M->getFunction(Name);
123
124 // If F is not available, declare it.
125 if (!F) {
126 Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy());
127 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
128
129 Type *Params[] = {
130 LongPtrTy,
131 LongPtrTy,
132 };
133
134 FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false);
135 F = Function::Create(Ty, Linkage, Name, M);
136 }
137
138 Value *Args[] = {
139 LowerBoundPtr,
140 UpperBoundPtr,
141 };
142
143 Value *Return = Builder.CreateCall(F, Args);
144 Return = Builder.CreateICmpNE(Return, Builder.CreateZExt(Builder.getFalse(),
145 Return->getType()));
146 return Return;
147}
148
149void OMPGenerator::createCallParallelEnd() {
150 const char *Name = "GOMP_parallel_end";
151 Module *M = getModule();
152 Function *F = M->getFunction(Name);
153
154 // If F is not available, declare it.
155 if (!F) {
156 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
157
158 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
159 F = Function::Create(Ty, Linkage, Name, M);
160 }
161
162 Builder.CreateCall(F);
163}
164
165void OMPGenerator::createCallLoopEndNowait() {
166 const char *Name = "GOMP_loop_end_nowait";
167 Module *M = getModule();
168 Function *F = M->getFunction(Name);
169
170 // If F is not available, declare it.
171 if (!F) {
172 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
173
174 FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false);
175 F = Function::Create(Ty, Linkage, Name, M);
176 }
177
178 Builder.CreateCall(F);
179}
180
181IntegerType *OMPGenerator::getIntPtrTy() {
Tobias Grosser5d01691d2012-11-01 16:45:18 +0000182 return P->getAnalysis<DataLayout>().getIntPtrType(Builder.getContext());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000183}
184
185Module *OMPGenerator::getModule() {
186 return Builder.GetInsertBlock()->getParent()->getParent();
187}
188
189Function *OMPGenerator::createSubfunctionDefinition() {
190 Module *M = getModule();
191 Function *F = Builder.GetInsertBlock()->getParent();
192 std::vector<Type*> Arguments(1, Builder.getInt8PtrTy());
193 FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false);
194 Function *FN = Function::Create(FT, Function::InternalLinkage,
195 F->getName() + ".omp_subfn", M);
196 // Do not run any polly pass on the new function.
197 P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN);
198
199 Function::arg_iterator AI = FN->arg_begin();
200 AI->setName("omp.userContext");
201
202 return FN;
203}
204
205Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value*> &Values) {
206 std::vector<Type*> Members;
207
208 for (unsigned i = 0; i < Values.size(); i++)
209 Members.push_back(Values[i]->getType());
210
211 StructType *Ty = StructType::get(Builder.getContext(), Members);
212 Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext");
213
214 for (unsigned i = 0; i < Values.size(); i++) {
215 Value *Address = Builder.CreateStructGEP(Struct, i);
216 Builder.CreateStore(Values[i], Address);
217 }
218
219 return Struct;
220}
221
222void OMPGenerator::extractValuesFromStruct(SetVector<Value*> OldValues,
223 Value *Struct,
224 ValueToValueMapTy &Map) {
225 for (unsigned i = 0; i < OldValues.size(); i++) {
226 Value *Address = Builder.CreateStructGEP(Struct, i);
227 Value *NewValue = Builder.CreateLoad(Address);
228 Map.insert(std::make_pair<Value*, Value*>(OldValues[i], NewValue));
229 }
230}
231
232Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData,
233 SetVector<Value*> Data,
234 ValueToValueMapTy &Map,
235 Function **SubFunction) {
236 Function *FN = createSubfunctionDefinition();
237
238 BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB,
239 *AfterBB;
240 Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule,
241 *LowerBound, *UpperBound, *IV;
242 Type *IntPtrTy = getIntPtrTy();
243 LLVMContext &Context = FN->getContext();
244
245 // Store the previous basic block.
246 PrevBB = Builder.GetInsertBlock();
247
248 // Create basic blocks.
249 HeaderBB = BasicBlock::Create(Context, "omp.setup", FN);
250 ExitBB = BasicBlock::Create(Context, "omp.exit", FN);
251 CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN);
252 LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN);
253
254 DominatorTree &DT = P->getAnalysis<DominatorTree>();
255 DT.addNewBlock(HeaderBB, PrevBB);
256 DT.addNewBlock(ExitBB, HeaderBB);
257 DT.addNewBlock(CheckNextBB, HeaderBB);
258 DT.addNewBlock(LoadIVBoundsBB, HeaderBB);
259
260 // Fill up basic block HeaderBB.
261 Builder.SetInsertPoint(HeaderBB);
262 LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr");
263 UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr");
264 UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(),
265 "omp.userContext");
266
267 extractValuesFromStruct(Data, UserContext, Map);
268 Builder.CreateBr(CheckNextBB);
269
270 // Add code to check if another set of iterations will be executed.
271 Builder.SetInsertPoint(CheckNextBB);
272 Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr);
273 HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(),
274 "omp.hasNextScheduleBlock");
275 Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB);
276
277 // Add code to to load the iv bounds for this set of iterations.
278 Builder.SetInsertPoint(LoadIVBoundsBB);
279 LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound");
280 UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound");
281
282 // Subtract one as the upper bound provided by openmp is a < comparison
283 // whereas the codegenForSequential function creates a <= comparison.
284 UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1),
285 "omp.upperBoundAdjusted");
286
287 Builder.CreateBr(CheckNextBB);
288 Builder.SetInsertPoint(--Builder.GetInsertPoint());
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000289 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB,
290 ICmpInst::ICMP_SLE);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000291
292 BasicBlock::iterator LoopBody = Builder.GetInsertPoint();
293 Builder.SetInsertPoint(AfterBB->begin());
294
295 // Add code to terminate this openmp subfunction.
296 Builder.SetInsertPoint(ExitBB);
297 createCallLoopEndNowait();
298 Builder.CreateRetVoid();
299
300 Builder.SetInsertPoint(LoopBody);
301 *SubFunction = FN;
302
303 return IV;
304}
305
306Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound,
307 Value *Stride,
308 SetVector<Value*> &Values,
309 ValueToValueMapTy &Map,
310 BasicBlock::iterator *LoopBody) {
311 Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads;
312 Function *SubFunction;
313
314 Struct = loadValuesIntoStruct(Values);
315
316 BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint();
317 IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction);
318 *LoopBody = Builder.GetInsertPoint();
319 Builder.SetInsertPoint(PrevInsertPoint);
320
321 // Create call for GOMP_parallel_loop_runtime_start.
322 SubfunctionParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(),
323 "omp_data");
324
325 NumberOfThreads = Builder.getInt32(0);
326
327 // Add one as the upper bound provided by openmp is a < comparison
328 // whereas the codegenForSequential function creates a <= comparison.
329 UpperBound = Builder.CreateAdd(UpperBound,
330 ConstantInt::get(getIntPtrTy(), 1));
331
332 createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads,
333 LowerBound, UpperBound, Stride);
334 Builder.CreateCall(SubFunction, SubfunctionParam);
335 createCallParallelEnd();
336
337 return IV;
338}