1609089f2SHongbin Zheng //===------ LoopGenerators.cpp - IR helper to create loops ---------------===// 2609089f2SHongbin Zheng // 3609089f2SHongbin Zheng // The LLVM Compiler Infrastructure 4609089f2SHongbin Zheng // 5609089f2SHongbin Zheng // This file is distributed under the University of Illinois Open Source 6609089f2SHongbin Zheng // License. See LICENSE.TXT for details. 7609089f2SHongbin Zheng // 8609089f2SHongbin Zheng //===----------------------------------------------------------------------===// 9609089f2SHongbin Zheng // 10609089f2SHongbin Zheng // This file contains functions to create scalar and OpenMP parallel loops 11609089f2SHongbin Zheng // as LLVM-IR. 12609089f2SHongbin Zheng // 13609089f2SHongbin Zheng //===----------------------------------------------------------------------===// 14609089f2SHongbin Zheng 15609089f2SHongbin Zheng #include "polly/LoopGenerators.h" 16609089f2SHongbin Zheng #include "polly/ScopDetection.h" 17609089f2SHongbin Zheng 18609089f2SHongbin Zheng #include "llvm/Module.h" 19609089f2SHongbin Zheng #include "llvm/Analysis/Dominators.h" 20609089f2SHongbin Zheng #include "llvm/Target/TargetData.h" 21609089f2SHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 22609089f2SHongbin Zheng 23609089f2SHongbin Zheng using namespace llvm; 24*4ac4e155SHongbin Zheng using namespace polly; 25609089f2SHongbin Zheng 26*4ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, 27*4ac4e155SHongbin Zheng IRBuilder<> &Builder, Pass *P, 28*4ac4e155SHongbin Zheng BasicBlock *&AfterBlock) { 29609089f2SHongbin Zheng DominatorTree &DT = P->getAnalysis<DominatorTree>(); 30*4ac4e155SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 31609089f2SHongbin Zheng LLVMContext &Context = F->getContext(); 32609089f2SHongbin Zheng 33*4ac4e155SHongbin Zheng BasicBlock *PreheaderBB = Builder.GetInsertBlock(); 34609089f2SHongbin Zheng BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); 35609089f2SHongbin Zheng BasicBlock *BodyBB = BasicBlock::Create(Context, "polly.loop_body", F); 36*4ac4e155SHongbin Zheng BasicBlock *AfterBB = SplitBlock(PreheaderBB, Builder.GetInsertPoint()++, P); 37609089f2SHongbin Zheng AfterBB->setName("polly.loop_after"); 38609089f2SHongbin Zheng 39609089f2SHongbin Zheng PreheaderBB->getTerminator()->setSuccessor(0, HeaderBB); 40609089f2SHongbin Zheng DT.addNewBlock(HeaderBB, PreheaderBB); 41609089f2SHongbin Zheng 42*4ac4e155SHongbin Zheng Builder.SetInsertPoint(HeaderBB); 43609089f2SHongbin Zheng 44609089f2SHongbin Zheng // Use the type of upper and lower bound. 45609089f2SHongbin Zheng assert(LB->getType() == UB->getType() 46609089f2SHongbin Zheng && "Different types for upper and lower bound."); 47609089f2SHongbin Zheng 48609089f2SHongbin Zheng IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); 49609089f2SHongbin Zheng assert(LoopIVType && "UB is not integer?"); 50609089f2SHongbin Zheng 51609089f2SHongbin Zheng // IV 52*4ac4e155SHongbin Zheng PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.loopiv"); 53609089f2SHongbin Zheng IV->addIncoming(LB, PreheaderBB); 54609089f2SHongbin Zheng 55*4ac4e155SHongbin Zheng Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); 56*4ac4e155SHongbin Zheng Value *IncrementedIV = Builder.CreateAdd(IV, Stride, "polly.next_loopiv"); 57609089f2SHongbin Zheng 58609089f2SHongbin Zheng // Exit condition. 59609089f2SHongbin Zheng Value *CMP; 60*4ac4e155SHongbin Zheng CMP = Builder.CreateICmpSLE(IV, UB); 61609089f2SHongbin Zheng 62*4ac4e155SHongbin Zheng Builder.CreateCondBr(CMP, BodyBB, AfterBB); 63609089f2SHongbin Zheng DT.addNewBlock(BodyBB, HeaderBB); 64609089f2SHongbin Zheng 65*4ac4e155SHongbin Zheng Builder.SetInsertPoint(BodyBB); 66*4ac4e155SHongbin Zheng Builder.CreateBr(HeaderBB); 67609089f2SHongbin Zheng IV->addIncoming(IncrementedIV, BodyBB); 68609089f2SHongbin Zheng DT.changeImmediateDominator(AfterBB, HeaderBB); 69609089f2SHongbin Zheng 70*4ac4e155SHongbin Zheng Builder.SetInsertPoint(BodyBB->begin()); 71*4ac4e155SHongbin Zheng AfterBlock = AfterBB; 72609089f2SHongbin Zheng 73609089f2SHongbin Zheng return IV; 74609089f2SHongbin Zheng } 75609089f2SHongbin Zheng 76609089f2SHongbin Zheng void OMPGenerator::createCallParallelLoopStart(Value *SubFunction, 77609089f2SHongbin Zheng Value *SubfunctionParam, 78609089f2SHongbin Zheng Value *NumberOfThreads, 79609089f2SHongbin Zheng Value *LowerBound, 80609089f2SHongbin Zheng Value *UpperBound, 81609089f2SHongbin Zheng Value *Stride) { 82609089f2SHongbin Zheng Module *M = getModule(); 83609089f2SHongbin Zheng const char *Name = "GOMP_parallel_loop_runtime_start"; 84609089f2SHongbin Zheng Function *F = M->getFunction(Name); 85609089f2SHongbin Zheng 86609089f2SHongbin Zheng // If F is not available, declare it. 87609089f2SHongbin Zheng if (!F) { 88609089f2SHongbin Zheng Type *LongTy = getIntPtrTy(); 89609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 90609089f2SHongbin Zheng 91609089f2SHongbin Zheng Type *Params[] = { 92609089f2SHongbin Zheng PointerType::getUnqual(FunctionType::get(Builder.getVoidTy(), 93609089f2SHongbin Zheng Builder.getInt8PtrTy(), 94609089f2SHongbin Zheng false)), 95609089f2SHongbin Zheng Builder.getInt8PtrTy(), 96609089f2SHongbin Zheng Builder.getInt32Ty(), 97609089f2SHongbin Zheng LongTy, 98609089f2SHongbin Zheng LongTy, 99609089f2SHongbin Zheng LongTy, 100609089f2SHongbin Zheng }; 101609089f2SHongbin Zheng 102609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); 103609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 104609089f2SHongbin Zheng } 105609089f2SHongbin Zheng 106609089f2SHongbin Zheng Value *Args[] = { 107609089f2SHongbin Zheng SubFunction, 108609089f2SHongbin Zheng SubfunctionParam, 109609089f2SHongbin Zheng NumberOfThreads, 110609089f2SHongbin Zheng LowerBound, 111609089f2SHongbin Zheng UpperBound, 112609089f2SHongbin Zheng Stride, 113609089f2SHongbin Zheng }; 114609089f2SHongbin Zheng 115609089f2SHongbin Zheng Builder.CreateCall(F, Args); 116609089f2SHongbin Zheng } 117609089f2SHongbin Zheng 118609089f2SHongbin Zheng Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr, 119609089f2SHongbin Zheng Value *UpperBoundPtr) { 120609089f2SHongbin Zheng Module *M = getModule(); 121609089f2SHongbin Zheng const char *Name = "GOMP_loop_runtime_next"; 122609089f2SHongbin Zheng Function *F = M->getFunction(Name); 123609089f2SHongbin Zheng 124609089f2SHongbin Zheng // If F is not available, declare it. 125609089f2SHongbin Zheng if (!F) { 126609089f2SHongbin Zheng Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy()); 127609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 128609089f2SHongbin Zheng 129609089f2SHongbin Zheng Type *Params[] = { 130609089f2SHongbin Zheng LongPtrTy, 131609089f2SHongbin Zheng LongPtrTy, 132609089f2SHongbin Zheng }; 133609089f2SHongbin Zheng 134609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); 135609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 136609089f2SHongbin Zheng } 137609089f2SHongbin Zheng 138609089f2SHongbin Zheng Value *Args[] = { 139609089f2SHongbin Zheng LowerBoundPtr, 140609089f2SHongbin Zheng UpperBoundPtr, 141609089f2SHongbin Zheng }; 142609089f2SHongbin Zheng 143609089f2SHongbin Zheng Value *Return = Builder.CreateCall(F, Args); 144609089f2SHongbin Zheng Return = Builder.CreateICmpNE(Return, Builder.CreateZExt(Builder.getFalse(), 145609089f2SHongbin Zheng Return->getType())); 146609089f2SHongbin Zheng return Return; 147609089f2SHongbin Zheng } 148609089f2SHongbin Zheng 149609089f2SHongbin Zheng void OMPGenerator::createCallParallelEnd() { 150609089f2SHongbin Zheng const char *Name = "GOMP_parallel_end"; 151609089f2SHongbin Zheng Module *M = getModule(); 152609089f2SHongbin Zheng Function *F = M->getFunction(Name); 153609089f2SHongbin Zheng 154609089f2SHongbin Zheng // If F is not available, declare it. 155609089f2SHongbin Zheng if (!F) { 156609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 157609089f2SHongbin Zheng 158609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 159609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 160609089f2SHongbin Zheng } 161609089f2SHongbin Zheng 162609089f2SHongbin Zheng Builder.CreateCall(F); 163609089f2SHongbin Zheng } 164609089f2SHongbin Zheng 165609089f2SHongbin Zheng void OMPGenerator::createCallLoopEndNowait() { 166609089f2SHongbin Zheng const char *Name = "GOMP_loop_end_nowait"; 167609089f2SHongbin Zheng Module *M = getModule(); 168609089f2SHongbin Zheng Function *F = M->getFunction(Name); 169609089f2SHongbin Zheng 170609089f2SHongbin Zheng // If F is not available, declare it. 171609089f2SHongbin Zheng if (!F) { 172609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 173609089f2SHongbin Zheng 174609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 175609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 176609089f2SHongbin Zheng } 177609089f2SHongbin Zheng 178609089f2SHongbin Zheng Builder.CreateCall(F); 179609089f2SHongbin Zheng } 180609089f2SHongbin Zheng 181609089f2SHongbin Zheng IntegerType *OMPGenerator::getIntPtrTy() { 182609089f2SHongbin Zheng return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext()); 183609089f2SHongbin Zheng } 184609089f2SHongbin Zheng 185609089f2SHongbin Zheng Module *OMPGenerator::getModule() { 186609089f2SHongbin Zheng return Builder.GetInsertBlock()->getParent()->getParent(); 187609089f2SHongbin Zheng } 188609089f2SHongbin Zheng 189609089f2SHongbin Zheng Function *OMPGenerator::createSubfunctionDefinition() { 190609089f2SHongbin Zheng Module *M = getModule(); 191609089f2SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 192609089f2SHongbin Zheng std::vector<Type*> Arguments(1, Builder.getInt8PtrTy()); 193609089f2SHongbin Zheng FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); 194609089f2SHongbin Zheng Function *FN = Function::Create(FT, Function::InternalLinkage, 195609089f2SHongbin Zheng F->getName() + ".omp_subfn", M); 196609089f2SHongbin Zheng // Do not run any polly pass on the new function. 197609089f2SHongbin Zheng P->getAnalysis<polly::ScopDetection>().markFunctionAsInvalid(FN); 198609089f2SHongbin Zheng 199609089f2SHongbin Zheng Function::arg_iterator AI = FN->arg_begin(); 200609089f2SHongbin Zheng AI->setName("omp.userContext"); 201609089f2SHongbin Zheng 202609089f2SHongbin Zheng return FN; 203609089f2SHongbin Zheng } 204609089f2SHongbin Zheng 205609089f2SHongbin Zheng Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value*> &Values) { 206609089f2SHongbin Zheng std::vector<Type*> Members; 207609089f2SHongbin Zheng 208609089f2SHongbin Zheng for (unsigned i = 0; i < Values.size(); i++) 209609089f2SHongbin Zheng Members.push_back(Values[i]->getType()); 210609089f2SHongbin Zheng 211609089f2SHongbin Zheng StructType *Ty = StructType::get(Builder.getContext(), Members); 212609089f2SHongbin Zheng Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext"); 213609089f2SHongbin Zheng 214609089f2SHongbin Zheng for (unsigned i = 0; i < Values.size(); i++) { 215609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 216609089f2SHongbin Zheng Builder.CreateStore(Values[i], Address); 217609089f2SHongbin Zheng } 218609089f2SHongbin Zheng 219609089f2SHongbin Zheng return Struct; 220609089f2SHongbin Zheng } 221609089f2SHongbin Zheng 222609089f2SHongbin Zheng void OMPGenerator::extractValuesFromStruct(SetVector<Value*> OldValues, 223609089f2SHongbin Zheng Value *Struct, 224609089f2SHongbin Zheng ValueToValueMapTy &Map) { 225609089f2SHongbin Zheng for (unsigned i = 0; i < OldValues.size(); i++) { 226609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 227609089f2SHongbin Zheng Value *NewValue = Builder.CreateLoad(Address); 228609089f2SHongbin Zheng Map.insert(std::make_pair<Value*, Value*>(OldValues[i], NewValue)); 229609089f2SHongbin Zheng } 230609089f2SHongbin Zheng } 231609089f2SHongbin Zheng 232609089f2SHongbin Zheng Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData, 233609089f2SHongbin Zheng SetVector<Value*> Data, 234609089f2SHongbin Zheng ValueToValueMapTy &Map, 235609089f2SHongbin Zheng Function **SubFunction) { 236609089f2SHongbin Zheng Function *FN = createSubfunctionDefinition(); 237609089f2SHongbin Zheng 238609089f2SHongbin Zheng BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB, 239609089f2SHongbin Zheng *AfterBB; 240609089f2SHongbin Zheng Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule, 241609089f2SHongbin Zheng *LowerBound, *UpperBound, *IV; 242609089f2SHongbin Zheng Type *IntPtrTy = getIntPtrTy(); 243609089f2SHongbin Zheng LLVMContext &Context = FN->getContext(); 244609089f2SHongbin Zheng 245609089f2SHongbin Zheng // Store the previous basic block. 246609089f2SHongbin Zheng PrevBB = Builder.GetInsertBlock(); 247609089f2SHongbin Zheng 248609089f2SHongbin Zheng // Create basic blocks. 249609089f2SHongbin Zheng HeaderBB = BasicBlock::Create(Context, "omp.setup", FN); 250609089f2SHongbin Zheng ExitBB = BasicBlock::Create(Context, "omp.exit", FN); 251609089f2SHongbin Zheng CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN); 252609089f2SHongbin Zheng LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN); 253609089f2SHongbin Zheng 254609089f2SHongbin Zheng DominatorTree &DT = P->getAnalysis<DominatorTree>(); 255609089f2SHongbin Zheng DT.addNewBlock(HeaderBB, PrevBB); 256609089f2SHongbin Zheng DT.addNewBlock(ExitBB, HeaderBB); 257609089f2SHongbin Zheng DT.addNewBlock(CheckNextBB, HeaderBB); 258609089f2SHongbin Zheng DT.addNewBlock(LoadIVBoundsBB, HeaderBB); 259609089f2SHongbin Zheng 260609089f2SHongbin Zheng // Fill up basic block HeaderBB. 261609089f2SHongbin Zheng Builder.SetInsertPoint(HeaderBB); 262609089f2SHongbin Zheng LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr"); 263609089f2SHongbin Zheng UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr"); 264609089f2SHongbin Zheng UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(), 265609089f2SHongbin Zheng "omp.userContext"); 266609089f2SHongbin Zheng 267609089f2SHongbin Zheng extractValuesFromStruct(Data, UserContext, Map); 268609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 269609089f2SHongbin Zheng 270609089f2SHongbin Zheng // Add code to check if another set of iterations will be executed. 271609089f2SHongbin Zheng Builder.SetInsertPoint(CheckNextBB); 272609089f2SHongbin Zheng Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr); 273609089f2SHongbin Zheng HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), 274609089f2SHongbin Zheng "omp.hasNextScheduleBlock"); 275609089f2SHongbin Zheng Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB); 276609089f2SHongbin Zheng 277609089f2SHongbin Zheng // Add code to to load the iv bounds for this set of iterations. 278609089f2SHongbin Zheng Builder.SetInsertPoint(LoadIVBoundsBB); 279609089f2SHongbin Zheng LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound"); 280609089f2SHongbin Zheng UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound"); 281609089f2SHongbin Zheng 282609089f2SHongbin Zheng // Subtract one as the upper bound provided by openmp is a < comparison 283609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 284609089f2SHongbin Zheng UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1), 285609089f2SHongbin Zheng "omp.upperBoundAdjusted"); 286609089f2SHongbin Zheng 287609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 288609089f2SHongbin Zheng Builder.SetInsertPoint(--Builder.GetInsertPoint()); 289*4ac4e155SHongbin Zheng IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB); 290609089f2SHongbin Zheng 291609089f2SHongbin Zheng BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); 292609089f2SHongbin Zheng Builder.SetInsertPoint(AfterBB->begin()); 293609089f2SHongbin Zheng 294609089f2SHongbin Zheng // Add code to terminate this openmp subfunction. 295609089f2SHongbin Zheng Builder.SetInsertPoint(ExitBB); 296609089f2SHongbin Zheng createCallLoopEndNowait(); 297609089f2SHongbin Zheng Builder.CreateRetVoid(); 298609089f2SHongbin Zheng 299609089f2SHongbin Zheng Builder.SetInsertPoint(LoopBody); 300609089f2SHongbin Zheng *SubFunction = FN; 301609089f2SHongbin Zheng 302609089f2SHongbin Zheng return IV; 303609089f2SHongbin Zheng } 304609089f2SHongbin Zheng 305609089f2SHongbin Zheng Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound, 306609089f2SHongbin Zheng Value *Stride, 307609089f2SHongbin Zheng SetVector<Value*> &Values, 308609089f2SHongbin Zheng ValueToValueMapTy &Map, 309609089f2SHongbin Zheng BasicBlock::iterator *LoopBody) { 310609089f2SHongbin Zheng Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads; 311609089f2SHongbin Zheng Function *SubFunction; 312609089f2SHongbin Zheng 313609089f2SHongbin Zheng Struct = loadValuesIntoStruct(Values); 314609089f2SHongbin Zheng 315609089f2SHongbin Zheng BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint(); 316609089f2SHongbin Zheng IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction); 317609089f2SHongbin Zheng *LoopBody = Builder.GetInsertPoint(); 318609089f2SHongbin Zheng Builder.SetInsertPoint(PrevInsertPoint); 319609089f2SHongbin Zheng 320609089f2SHongbin Zheng // Create call for GOMP_parallel_loop_runtime_start. 321609089f2SHongbin Zheng SubfunctionParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), 322609089f2SHongbin Zheng "omp_data"); 323609089f2SHongbin Zheng 324609089f2SHongbin Zheng NumberOfThreads = Builder.getInt32(0); 325609089f2SHongbin Zheng 326609089f2SHongbin Zheng // Add one as the upper bound provided by openmp is a < comparison 327609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 328609089f2SHongbin Zheng UpperBound = Builder.CreateAdd(UpperBound, 329609089f2SHongbin Zheng ConstantInt::get(getIntPtrTy(), 1)); 330609089f2SHongbin Zheng 331609089f2SHongbin Zheng createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads, 332609089f2SHongbin Zheng LowerBound, UpperBound, Stride); 333609089f2SHongbin Zheng Builder.CreateCall(SubFunction, SubfunctionParam); 334609089f2SHongbin Zheng createCallParallelEnd(); 335609089f2SHongbin Zheng 336609089f2SHongbin Zheng return IV; 337609089f2SHongbin Zheng } 338