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/ScopDetection.h" 168a846610SHongbin Zheng #include "polly/CodeGen/LoopGenerators.h" 173081b0f5STobias Grosser #include "llvm/Analysis/LoopInfo.h" 18535d52c7SChandler Carruth #include "llvm/IR/DataLayout.h" 19e87c6a81SChandler Carruth #include "llvm/IR/Dominators.h" 2083628182STobias Grosser #include "llvm/IR/Module.h" 21609089f2SHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 22609089f2SHongbin Zheng 23609089f2SHongbin Zheng using namespace llvm; 244ac4e155SHongbin Zheng using namespace polly; 25609089f2SHongbin Zheng 26dd5c1442SJohannes Doerfert // We generate a loop of either of the following structures: 275db6ffd7STobias Grosser // 28dd5c1442SJohannes Doerfert // BeforeBB BeforeBB 29dd5c1442SJohannes Doerfert // | | 30dd5c1442SJohannes Doerfert // v v 31dd5c1442SJohannes Doerfert // GuardBB PreHeaderBB 32dd5c1442SJohannes Doerfert // / | | _____ 33dd5c1442SJohannes Doerfert // __ PreHeaderBB | v \/ | 34dd5c1442SJohannes Doerfert // / \ / | HeaderBB latch 35dd5c1442SJohannes Doerfert // latch HeaderBB | |\ | 36dd5c1442SJohannes Doerfert // \ / \ / | \------/ 37dd5c1442SJohannes Doerfert // < \ / | 38dd5c1442SJohannes Doerfert // \ / v 39dd5c1442SJohannes Doerfert // ExitBB ExitBB 405db6ffd7STobias Grosser // 41dd5c1442SJohannes Doerfert // depending on whether or not we know that it is executed at least once. If 42dd5c1442SJohannes Doerfert // not, GuardBB checks if the loop is executed at least once. If this is the 43dd5c1442SJohannes Doerfert // case we branch to PreHeaderBB and subsequently to the HeaderBB, which 44dd5c1442SJohannes Doerfert // contains the loop iv 'polly.indvar', the incremented loop iv 45dd5c1442SJohannes Doerfert // 'polly.indvar_next' as well as the condition to check if we execute another 46dd5c1442SJohannes Doerfert // iteration of the loop. After the loop has finished, we branch to ExitBB. 474ac4e155SHongbin Zheng Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, 482ef3f4fdSJohannes Doerfert PollyIRBuilder &Builder, Pass *P, LoopInfo &LI, 492ef3f4fdSJohannes Doerfert DominatorTree &DT, BasicBlock *&ExitBB, 5037c9b8e0STobias Grosser ICmpInst::Predicate Predicate, 51dd5c1442SJohannes Doerfert LoopAnnotator *Annotator, bool Parallel, 52dd5c1442SJohannes Doerfert bool UseGuard) { 534ac4e155SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 54609089f2SHongbin Zheng LLVMContext &Context = F->getContext(); 55609089f2SHongbin Zheng 565db6ffd7STobias Grosser assert(LB->getType() == UB->getType() && "Types of loop bounds do not match"); 57609089f2SHongbin Zheng IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); 58609089f2SHongbin Zheng assert(LoopIVType && "UB is not integer?"); 59609089f2SHongbin Zheng 605db6ffd7STobias Grosser BasicBlock *BeforeBB = Builder.GetInsertBlock(); 61dd5c1442SJohannes Doerfert BasicBlock *GuardBB = 62dd5c1442SJohannes Doerfert UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr; 635db6ffd7STobias Grosser BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); 645db6ffd7STobias Grosser BasicBlock *PreHeaderBB = 655db6ffd7STobias Grosser BasicBlock::Create(Context, "polly.loop_preheader", F); 66609089f2SHongbin Zheng 673081b0f5STobias Grosser // Update LoopInfo 683081b0f5STobias Grosser Loop *OuterLoop = LI.getLoopFor(BeforeBB); 693081b0f5STobias Grosser Loop *NewLoop = new Loop(); 703081b0f5STobias Grosser 71dd5c1442SJohannes Doerfert if (OuterLoop) 723081b0f5STobias Grosser OuterLoop->addChildLoop(NewLoop); 73dd5c1442SJohannes Doerfert else 743081b0f5STobias Grosser LI.addTopLevelLoop(NewLoop); 753081b0f5STobias Grosser 76dd5c1442SJohannes Doerfert if (OuterLoop && GuardBB) 773081b0f5STobias Grosser OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase()); 78dd5c1442SJohannes Doerfert else if (OuterLoop) 793081b0f5STobias Grosser OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase()); 803081b0f5STobias Grosser 813081b0f5STobias Grosser NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase()); 823081b0f5STobias Grosser 83*c7b719fcSJohannes Doerfert // Notify the annotator (if present) that we have a new loop, but only 84*c7b719fcSJohannes Doerfert // after the header block is set. 85*c7b719fcSJohannes Doerfert if (Annotator) 86*c7b719fcSJohannes Doerfert Annotator->pushLoop(NewLoop, Parallel); 87*c7b719fcSJohannes Doerfert 885db6ffd7STobias Grosser // ExitBB 895db6ffd7STobias Grosser ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P); 905db6ffd7STobias Grosser ExitBB->setName("polly.loop_exit"); 91609089f2SHongbin Zheng 925db6ffd7STobias Grosser // BeforeBB 93dd5c1442SJohannes Doerfert if (GuardBB) { 945db6ffd7STobias Grosser BeforeBB->getTerminator()->setSuccessor(0, GuardBB); 95dd5c1442SJohannes Doerfert DT.addNewBlock(GuardBB, BeforeBB); 96609089f2SHongbin Zheng 975db6ffd7STobias Grosser // GuardBB 985db6ffd7STobias Grosser Builder.SetInsertPoint(GuardBB); 995db6ffd7STobias Grosser Value *LoopGuard; 1005db6ffd7STobias Grosser LoopGuard = Builder.CreateICmp(Predicate, LB, UB); 1015db6ffd7STobias Grosser LoopGuard->setName("polly.loop_guard"); 1025db6ffd7STobias Grosser Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); 103dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, GuardBB); 104dd5c1442SJohannes Doerfert } else { 105dd5c1442SJohannes Doerfert BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); 106dd5c1442SJohannes Doerfert DT.addNewBlock(PreHeaderBB, BeforeBB); 107dd5c1442SJohannes Doerfert } 108609089f2SHongbin Zheng 1095db6ffd7STobias Grosser // PreHeaderBB 1105db6ffd7STobias Grosser Builder.SetInsertPoint(PreHeaderBB); 1114ac4e155SHongbin Zheng Builder.CreateBr(HeaderBB); 112609089f2SHongbin Zheng 1135db6ffd7STobias Grosser // HeaderBB 1145db6ffd7STobias Grosser DT.addNewBlock(HeaderBB, PreHeaderBB); 1155db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB); 1165db6ffd7STobias Grosser PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); 1175db6ffd7STobias Grosser IV->addIncoming(LB, PreHeaderBB); 1185db6ffd7STobias Grosser Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); 1195db6ffd7STobias Grosser Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); 1205db6ffd7STobias Grosser Value *LoopCondition; 1215db6ffd7STobias Grosser UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub"); 1225db6ffd7STobias Grosser LoopCondition = Builder.CreateICmp(Predicate, IV, UB); 1235db6ffd7STobias Grosser LoopCondition->setName("polly.loop_cond"); 124*c7b719fcSJohannes Doerfert 125*c7b719fcSJohannes Doerfert // Create the loop latch and annotate it as such. 126*c7b719fcSJohannes Doerfert BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); 127*c7b719fcSJohannes Doerfert if (Annotator) 128*c7b719fcSJohannes Doerfert Annotator->annotateLoopLatch(B, NewLoop, Parallel); 129*c7b719fcSJohannes Doerfert 1305db6ffd7STobias Grosser IV->addIncoming(IncrementedIV, HeaderBB); 131dd5c1442SJohannes Doerfert if (GuardBB) 1325db6ffd7STobias Grosser DT.changeImmediateDominator(ExitBB, GuardBB); 133dd5c1442SJohannes Doerfert else 134f8a678d2STobias Grosser DT.changeImmediateDominator(ExitBB, HeaderBB); 135609089f2SHongbin Zheng 1365db6ffd7STobias Grosser // The loop body should be added here. 1375db6ffd7STobias Grosser Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); 138609089f2SHongbin Zheng return IV; 139609089f2SHongbin Zheng } 140609089f2SHongbin Zheng 141c14582f2STobias Grosser void OMPGenerator::createCallParallelLoopStart( 142c14582f2STobias Grosser Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads, 143c14582f2STobias Grosser Value *LowerBound, Value *UpperBound, Value *Stride) { 144609089f2SHongbin Zheng Module *M = getModule(); 145609089f2SHongbin Zheng const char *Name = "GOMP_parallel_loop_runtime_start"; 146609089f2SHongbin Zheng Function *F = M->getFunction(Name); 147609089f2SHongbin Zheng 148609089f2SHongbin Zheng // If F is not available, declare it. 149609089f2SHongbin Zheng if (!F) { 150609089f2SHongbin Zheng Type *LongTy = getIntPtrTy(); 151609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 152609089f2SHongbin Zheng 153c14582f2STobias Grosser Type *Params[] = {PointerType::getUnqual(FunctionType::get( 154c14582f2STobias Grosser Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), 155c14582f2STobias Grosser Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy, 15645bac0d9STobias Grosser LongTy, LongTy}; 157609089f2SHongbin Zheng 158609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); 159609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 160609089f2SHongbin Zheng } 161609089f2SHongbin Zheng 1624c932b8aSTobias Grosser Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads, 1634c932b8aSTobias Grosser LowerBound, UpperBound, Stride}; 164609089f2SHongbin Zheng 165609089f2SHongbin Zheng Builder.CreateCall(F, Args); 166609089f2SHongbin Zheng } 167609089f2SHongbin Zheng 168e602a076STobias Grosser Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr, 169e602a076STobias Grosser Value *UpperBoundPtr) { 170609089f2SHongbin Zheng Module *M = getModule(); 171609089f2SHongbin Zheng const char *Name = "GOMP_loop_runtime_next"; 172609089f2SHongbin Zheng Function *F = M->getFunction(Name); 173609089f2SHongbin Zheng 174609089f2SHongbin Zheng // If F is not available, declare it. 175609089f2SHongbin Zheng if (!F) { 176609089f2SHongbin Zheng Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy()); 177609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 178609089f2SHongbin Zheng 17945bac0d9STobias Grosser Type *Params[] = {LongPtrTy, LongPtrTy}; 180609089f2SHongbin Zheng 181609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); 182609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 183609089f2SHongbin Zheng } 184609089f2SHongbin Zheng 18545bac0d9STobias Grosser Value *Args[] = {LowerBoundPtr, UpperBoundPtr}; 186609089f2SHongbin Zheng 187609089f2SHongbin Zheng Value *Return = Builder.CreateCall(F, Args); 188c14582f2STobias Grosser Return = Builder.CreateICmpNE( 189c14582f2STobias Grosser Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); 190609089f2SHongbin Zheng return Return; 191609089f2SHongbin Zheng } 192609089f2SHongbin Zheng 193609089f2SHongbin Zheng void OMPGenerator::createCallParallelEnd() { 194609089f2SHongbin Zheng const char *Name = "GOMP_parallel_end"; 195609089f2SHongbin Zheng Module *M = getModule(); 196609089f2SHongbin Zheng Function *F = M->getFunction(Name); 197609089f2SHongbin Zheng 198609089f2SHongbin Zheng // If F is not available, declare it. 199609089f2SHongbin Zheng if (!F) { 200609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 201609089f2SHongbin Zheng 202609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 203609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 204609089f2SHongbin Zheng } 205609089f2SHongbin Zheng 206609089f2SHongbin Zheng Builder.CreateCall(F); 207609089f2SHongbin Zheng } 208609089f2SHongbin Zheng 209609089f2SHongbin Zheng void OMPGenerator::createCallLoopEndNowait() { 210609089f2SHongbin Zheng const char *Name = "GOMP_loop_end_nowait"; 211609089f2SHongbin Zheng Module *M = getModule(); 212609089f2SHongbin Zheng Function *F = M->getFunction(Name); 213609089f2SHongbin Zheng 214609089f2SHongbin Zheng // If F is not available, declare it. 215609089f2SHongbin Zheng if (!F) { 216609089f2SHongbin Zheng GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; 217609089f2SHongbin Zheng 218609089f2SHongbin Zheng FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); 219609089f2SHongbin Zheng F = Function::Create(Ty, Linkage, Name, M); 220609089f2SHongbin Zheng } 221609089f2SHongbin Zheng 222609089f2SHongbin Zheng Builder.CreateCall(F); 223609089f2SHongbin Zheng } 224609089f2SHongbin Zheng 225609089f2SHongbin Zheng IntegerType *OMPGenerator::getIntPtrTy() { 226c5d1689bSRafael Espindola return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType( 227c5d1689bSRafael Espindola Builder.getContext()); 228609089f2SHongbin Zheng } 229609089f2SHongbin Zheng 230609089f2SHongbin Zheng Module *OMPGenerator::getModule() { 231609089f2SHongbin Zheng return Builder.GetInsertBlock()->getParent()->getParent(); 232609089f2SHongbin Zheng } 233609089f2SHongbin Zheng 234609089f2SHongbin Zheng Function *OMPGenerator::createSubfunctionDefinition() { 235609089f2SHongbin Zheng Module *M = getModule(); 236609089f2SHongbin Zheng Function *F = Builder.GetInsertBlock()->getParent(); 237609089f2SHongbin Zheng std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); 238609089f2SHongbin Zheng FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); 239609089f2SHongbin Zheng Function *FN = Function::Create(FT, Function::InternalLinkage, 240609089f2SHongbin Zheng F->getName() + ".omp_subfn", M); 241609089f2SHongbin Zheng // Do not run any polly pass on the new function. 24243e1eadfSJohannes Doerfert FN->addFnAttr(PollySkipFnAttr); 243609089f2SHongbin Zheng 244609089f2SHongbin Zheng Function::arg_iterator AI = FN->arg_begin(); 245609089f2SHongbin Zheng AI->setName("omp.userContext"); 246609089f2SHongbin Zheng 247609089f2SHongbin Zheng return FN; 248609089f2SHongbin Zheng } 249609089f2SHongbin Zheng 250609089f2SHongbin Zheng Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) { 251609089f2SHongbin Zheng std::vector<Type *> Members; 252609089f2SHongbin Zheng 25391f5b262STobias Grosser for (Value *V : Values) 25491f5b262STobias Grosser Members.push_back(V->getType()); 255609089f2SHongbin Zheng 256609089f2SHongbin Zheng StructType *Ty = StructType::get(Builder.getContext(), Members); 257609089f2SHongbin Zheng Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext"); 258609089f2SHongbin Zheng 259609089f2SHongbin Zheng for (unsigned i = 0; i < Values.size(); i++) { 260609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 261609089f2SHongbin Zheng Builder.CreateStore(Values[i], Address); 262609089f2SHongbin Zheng } 263609089f2SHongbin Zheng 264609089f2SHongbin Zheng return Struct; 265609089f2SHongbin Zheng } 266609089f2SHongbin Zheng 267e602a076STobias Grosser void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues, 268e602a076STobias Grosser Value *Struct, 269e602a076STobias Grosser ValueToValueMapTy &Map) { 270609089f2SHongbin Zheng for (unsigned i = 0; i < OldValues.size(); i++) { 271609089f2SHongbin Zheng Value *Address = Builder.CreateStructGEP(Struct, i); 272609089f2SHongbin Zheng Value *NewValue = Builder.CreateLoad(Address); 27336e6ca01SAndy Gibbs Map.insert(std::make_pair(OldValues[i], NewValue)); 274609089f2SHongbin Zheng } 275609089f2SHongbin Zheng } 276609089f2SHongbin Zheng 277e602a076STobias Grosser Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData, 278e602a076STobias Grosser SetVector<Value *> Data, 279e602a076STobias Grosser ValueToValueMapTy &Map, 280e602a076STobias Grosser Function **SubFunction) { 281609089f2SHongbin Zheng Function *FN = createSubfunctionDefinition(); 282609089f2SHongbin Zheng 283609089f2SHongbin Zheng BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB, 284609089f2SHongbin Zheng *AfterBB; 285609089f2SHongbin Zheng Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule, 286609089f2SHongbin Zheng *LowerBound, *UpperBound, *IV; 287609089f2SHongbin Zheng Type *IntPtrTy = getIntPtrTy(); 288609089f2SHongbin Zheng LLVMContext &Context = FN->getContext(); 289609089f2SHongbin Zheng 290609089f2SHongbin Zheng // Store the previous basic block. 291609089f2SHongbin Zheng PrevBB = Builder.GetInsertBlock(); 292609089f2SHongbin Zheng 293609089f2SHongbin Zheng // Create basic blocks. 294609089f2SHongbin Zheng HeaderBB = BasicBlock::Create(Context, "omp.setup", FN); 295609089f2SHongbin Zheng ExitBB = BasicBlock::Create(Context, "omp.exit", FN); 296609089f2SHongbin Zheng CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN); 297609089f2SHongbin Zheng LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN); 298609089f2SHongbin Zheng 29942aff30dSTobias Grosser DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 300609089f2SHongbin Zheng DT.addNewBlock(HeaderBB, PrevBB); 301609089f2SHongbin Zheng DT.addNewBlock(ExitBB, HeaderBB); 302609089f2SHongbin Zheng DT.addNewBlock(CheckNextBB, HeaderBB); 303609089f2SHongbin Zheng DT.addNewBlock(LoadIVBoundsBB, HeaderBB); 304609089f2SHongbin Zheng 305609089f2SHongbin Zheng // Fill up basic block HeaderBB. 306609089f2SHongbin Zheng Builder.SetInsertPoint(HeaderBB); 307609089f2SHongbin Zheng LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr"); 308609089f2SHongbin Zheng UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr"); 309609089f2SHongbin Zheng UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(), 310609089f2SHongbin Zheng "omp.userContext"); 311609089f2SHongbin Zheng 312609089f2SHongbin Zheng extractValuesFromStruct(Data, UserContext, Map); 313609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 314609089f2SHongbin Zheng 315609089f2SHongbin Zheng // Add code to check if another set of iterations will be executed. 316609089f2SHongbin Zheng Builder.SetInsertPoint(CheckNextBB); 317609089f2SHongbin Zheng Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr); 318609089f2SHongbin Zheng HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), 319609089f2SHongbin Zheng "omp.hasNextScheduleBlock"); 320609089f2SHongbin Zheng Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB); 321609089f2SHongbin Zheng 322609089f2SHongbin Zheng // Add code to to load the iv bounds for this set of iterations. 323609089f2SHongbin Zheng Builder.SetInsertPoint(LoadIVBoundsBB); 324609089f2SHongbin Zheng LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound"); 325609089f2SHongbin Zheng UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound"); 326609089f2SHongbin Zheng 327609089f2SHongbin Zheng // Subtract one as the upper bound provided by openmp is a < comparison 328609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 329609089f2SHongbin Zheng UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1), 330609089f2SHongbin Zheng "omp.upperBoundAdjusted"); 331609089f2SHongbin Zheng 332609089f2SHongbin Zheng Builder.CreateBr(CheckNextBB); 333609089f2SHongbin Zheng Builder.SetInsertPoint(--Builder.GetInsertPoint()); 3342ef3f4fdSJohannes Doerfert LoopInfo &LI = P->getAnalysis<LoopInfo>(); 3352ef3f4fdSJohannes Doerfert IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, LI, DT, AfterBB, 336dd5c1442SJohannes Doerfert ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false); 337609089f2SHongbin Zheng 338609089f2SHongbin Zheng BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); 339609089f2SHongbin Zheng Builder.SetInsertPoint(AfterBB->begin()); 340609089f2SHongbin Zheng 341609089f2SHongbin Zheng // Add code to terminate this openmp subfunction. 342609089f2SHongbin Zheng Builder.SetInsertPoint(ExitBB); 343609089f2SHongbin Zheng createCallLoopEndNowait(); 344609089f2SHongbin Zheng Builder.CreateRetVoid(); 345609089f2SHongbin Zheng 346609089f2SHongbin Zheng Builder.SetInsertPoint(LoopBody); 347609089f2SHongbin Zheng *SubFunction = FN; 348609089f2SHongbin Zheng 349609089f2SHongbin Zheng return IV; 350609089f2SHongbin Zheng } 351609089f2SHongbin Zheng 352e602a076STobias Grosser Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound, 353e602a076STobias Grosser Value *Stride, 354e602a076STobias Grosser SetVector<Value *> &Values, 355e602a076STobias Grosser ValueToValueMapTy &Map, 356609089f2SHongbin Zheng BasicBlock::iterator *LoopBody) { 357609089f2SHongbin Zheng Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads; 358609089f2SHongbin Zheng Function *SubFunction; 359609089f2SHongbin Zheng 360609089f2SHongbin Zheng Struct = loadValuesIntoStruct(Values); 361609089f2SHongbin Zheng 362609089f2SHongbin Zheng BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint(); 363609089f2SHongbin Zheng IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction); 364609089f2SHongbin Zheng *LoopBody = Builder.GetInsertPoint(); 365609089f2SHongbin Zheng Builder.SetInsertPoint(PrevInsertPoint); 366609089f2SHongbin Zheng 367609089f2SHongbin Zheng // Create call for GOMP_parallel_loop_runtime_start. 368c14582f2STobias Grosser SubfunctionParam = 369c14582f2STobias Grosser Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data"); 370609089f2SHongbin Zheng 371609089f2SHongbin Zheng NumberOfThreads = Builder.getInt32(0); 372609089f2SHongbin Zheng 373609089f2SHongbin Zheng // Add one as the upper bound provided by openmp is a < comparison 374609089f2SHongbin Zheng // whereas the codegenForSequential function creates a <= comparison. 375c14582f2STobias Grosser UpperBound = 376c14582f2STobias Grosser Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1)); 377609089f2SHongbin Zheng 378609089f2SHongbin Zheng createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads, 379609089f2SHongbin Zheng LowerBound, UpperBound, Stride); 380609089f2SHongbin Zheng Builder.CreateCall(SubFunction, SubfunctionParam); 381609089f2SHongbin Zheng createCallParallelEnd(); 382609089f2SHongbin Zheng 383609089f2SHongbin Zheng return IV; 384609089f2SHongbin Zheng } 385