Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 1 | //===------ 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 Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 15 | #include "polly/ScopDetection.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 16 | #include "polly/CodeGen/LoopGenerators.h" |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 18 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | e87c6a8 | 2014-01-13 09:56:11 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Dominators.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 22 | |
| 23 | using namespace llvm; |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 24 | using namespace polly; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 25 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 26 | // We generate a loop of either of the following structures: |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 27 | // |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 28 | // BeforeBB BeforeBB |
| 29 | // | | |
| 30 | // v v |
| 31 | // GuardBB PreHeaderBB |
| 32 | // / | | _____ |
| 33 | // __ PreHeaderBB | v \/ | |
| 34 | // / \ / | HeaderBB latch |
| 35 | // latch HeaderBB | |\ | |
| 36 | // \ / \ / | \------/ |
| 37 | // < \ / | |
| 38 | // \ / v |
| 39 | // ExitBB ExitBB |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 40 | // |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 41 | // depending on whether or not we know that it is executed at least once. If |
| 42 | // not, GuardBB checks if the loop is executed at least once. If this is the |
| 43 | // case we branch to PreHeaderBB and subsequently to the HeaderBB, which |
| 44 | // contains the loop iv 'polly.indvar', the incremented loop iv |
| 45 | // 'polly.indvar_next' as well as the condition to check if we execute another |
| 46 | // iteration of the loop. After the loop has finished, we branch to ExitBB. |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 47 | Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 48 | PollyIRBuilder &Builder, Pass *P, LoopInfo &LI, |
| 49 | DominatorTree &DT, BasicBlock *&ExitBB, |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 50 | ICmpInst::Predicate Predicate, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 51 | LoopAnnotator *Annotator, bool Parallel, |
| 52 | bool UseGuard) { |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 53 | Function *F = Builder.GetInsertBlock()->getParent(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 54 | LLVMContext &Context = F->getContext(); |
| 55 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 56 | assert(LB->getType() == UB->getType() && "Types of loop bounds do not match"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 57 | IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); |
| 58 | assert(LoopIVType && "UB is not integer?"); |
| 59 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 60 | BasicBlock *BeforeBB = Builder.GetInsertBlock(); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 61 | BasicBlock *GuardBB = |
| 62 | UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr; |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 63 | BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); |
| 64 | BasicBlock *PreHeaderBB = |
| 65 | BasicBlock::Create(Context, "polly.loop_preheader", F); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 66 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 67 | if (Annotator) { |
| 68 | Annotator->Begin(HeaderBB); |
| 69 | if (Parallel) |
| 70 | Annotator->SetCurrentParallel(); |
| 71 | } |
| 72 | |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 73 | // Update LoopInfo |
| 74 | Loop *OuterLoop = LI.getLoopFor(BeforeBB); |
| 75 | Loop *NewLoop = new Loop(); |
| 76 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 77 | if (OuterLoop) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 78 | OuterLoop->addChildLoop(NewLoop); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 79 | else |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 80 | LI.addTopLevelLoop(NewLoop); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 81 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 82 | if (OuterLoop && GuardBB) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 83 | OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase()); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 84 | else if (OuterLoop) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 85 | OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase()); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 86 | |
| 87 | NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase()); |
| 88 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 89 | // ExitBB |
| 90 | ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P); |
| 91 | ExitBB->setName("polly.loop_exit"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 92 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 93 | // BeforeBB |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 94 | if (GuardBB) { |
| 95 | BeforeBB->getTerminator()->setSuccessor(0, GuardBB); |
| 96 | DT.addNewBlock(GuardBB, BeforeBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 97 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 98 | // GuardBB |
| 99 | Builder.SetInsertPoint(GuardBB); |
| 100 | Value *LoopGuard; |
| 101 | LoopGuard = Builder.CreateICmp(Predicate, LB, UB); |
| 102 | LoopGuard->setName("polly.loop_guard"); |
| 103 | Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); |
| 104 | DT.addNewBlock(PreHeaderBB, GuardBB); |
| 105 | } else { |
| 106 | BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); |
| 107 | DT.addNewBlock(PreHeaderBB, BeforeBB); |
| 108 | } |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 109 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 110 | // PreHeaderBB |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 111 | Builder.SetInsertPoint(PreHeaderBB); |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 112 | Builder.CreateBr(HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 113 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 114 | // HeaderBB |
| 115 | DT.addNewBlock(HeaderBB, PreHeaderBB); |
| 116 | Builder.SetInsertPoint(HeaderBB); |
| 117 | PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); |
| 118 | IV->addIncoming(LB, PreHeaderBB); |
| 119 | Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); |
| 120 | Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); |
| 121 | Value *LoopCondition; |
| 122 | UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub"); |
| 123 | LoopCondition = Builder.CreateICmp(Predicate, IV, UB); |
| 124 | LoopCondition->setName("polly.loop_cond"); |
| 125 | Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); |
| 126 | IV->addIncoming(IncrementedIV, HeaderBB); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 127 | if (GuardBB) |
| 128 | DT.changeImmediateDominator(ExitBB, GuardBB); |
| 129 | else |
| 130 | DT.changeImmediateDominator(ExitBB, BeforeBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 131 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 132 | // The loop body should be added here. |
| 133 | Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 134 | return IV; |
| 135 | } |
| 136 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 137 | void OMPGenerator::createCallParallelLoopStart( |
| 138 | Value *SubFunction, Value *SubfunctionParam, Value *NumberOfThreads, |
| 139 | Value *LowerBound, Value *UpperBound, Value *Stride) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 140 | Module *M = getModule(); |
| 141 | const char *Name = "GOMP_parallel_loop_runtime_start"; |
| 142 | Function *F = M->getFunction(Name); |
| 143 | |
| 144 | // If F is not available, declare it. |
| 145 | if (!F) { |
| 146 | Type *LongTy = getIntPtrTy(); |
| 147 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 148 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 149 | Type *Params[] = {PointerType::getUnqual(FunctionType::get( |
| 150 | Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), |
| 151 | Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongTy, |
| 152 | LongTy, LongTy}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 153 | |
| 154 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); |
| 155 | F = Function::Create(Ty, Linkage, Name, M); |
| 156 | } |
| 157 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 158 | Value *Args[] = {SubFunction, SubfunctionParam, NumberOfThreads, |
| 159 | LowerBound, UpperBound, Stride}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 160 | |
| 161 | Builder.CreateCall(F, Args); |
| 162 | } |
| 163 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 164 | Value *OMPGenerator::createCallLoopNext(Value *LowerBoundPtr, |
| 165 | Value *UpperBoundPtr) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 166 | Module *M = getModule(); |
| 167 | const char *Name = "GOMP_loop_runtime_next"; |
| 168 | Function *F = M->getFunction(Name); |
| 169 | |
| 170 | // If F is not available, declare it. |
| 171 | if (!F) { |
| 172 | Type *LongPtrTy = PointerType::getUnqual(getIntPtrTy()); |
| 173 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 174 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 175 | Type *Params[] = {LongPtrTy, LongPtrTy}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 176 | |
| 177 | FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); |
| 178 | F = Function::Create(Ty, Linkage, Name, M); |
| 179 | } |
| 180 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 181 | Value *Args[] = {LowerBoundPtr, UpperBoundPtr}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 182 | |
| 183 | Value *Return = Builder.CreateCall(F, Args); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 184 | Return = Builder.CreateICmpNE( |
| 185 | Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 186 | return Return; |
| 187 | } |
| 188 | |
| 189 | void OMPGenerator::createCallParallelEnd() { |
| 190 | const char *Name = "GOMP_parallel_end"; |
| 191 | Module *M = getModule(); |
| 192 | Function *F = M->getFunction(Name); |
| 193 | |
| 194 | // If F is not available, declare it. |
| 195 | if (!F) { |
| 196 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 197 | |
| 198 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 199 | F = Function::Create(Ty, Linkage, Name, M); |
| 200 | } |
| 201 | |
| 202 | Builder.CreateCall(F); |
| 203 | } |
| 204 | |
| 205 | void OMPGenerator::createCallLoopEndNowait() { |
| 206 | const char *Name = "GOMP_loop_end_nowait"; |
| 207 | Module *M = getModule(); |
| 208 | Function *F = M->getFunction(Name); |
| 209 | |
| 210 | // If F is not available, declare it. |
| 211 | if (!F) { |
| 212 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 213 | |
| 214 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 215 | F = Function::Create(Ty, Linkage, Name, M); |
| 216 | } |
| 217 | |
| 218 | Builder.CreateCall(F); |
| 219 | } |
| 220 | |
| 221 | IntegerType *OMPGenerator::getIntPtrTy() { |
Rafael Espindola | c5d1689 | 2014-02-25 19:17:57 +0000 | [diff] [blame] | 222 | return P->getAnalysis<DataLayoutPass>().getDataLayout().getIntPtrType( |
| 223 | Builder.getContext()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | Module *OMPGenerator::getModule() { |
| 227 | return Builder.GetInsertBlock()->getParent()->getParent(); |
| 228 | } |
| 229 | |
| 230 | Function *OMPGenerator::createSubfunctionDefinition() { |
| 231 | Module *M = getModule(); |
| 232 | Function *F = Builder.GetInsertBlock()->getParent(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 233 | std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 234 | FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); |
| 235 | Function *FN = Function::Create(FT, Function::InternalLinkage, |
| 236 | F->getName() + ".omp_subfn", M); |
| 237 | // Do not run any polly pass on the new function. |
Johannes Doerfert | 43e1ead | 2014-07-15 21:06:48 +0000 | [diff] [blame] | 238 | FN->addFnAttr(PollySkipFnAttr); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 239 | |
| 240 | Function::arg_iterator AI = FN->arg_begin(); |
| 241 | AI->setName("omp.userContext"); |
| 242 | |
| 243 | return FN; |
| 244 | } |
| 245 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 246 | Value *OMPGenerator::loadValuesIntoStruct(SetVector<Value *> &Values) { |
| 247 | std::vector<Type *> Members; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 248 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 249 | for (Value *V : Values) |
| 250 | Members.push_back(V->getType()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 251 | |
| 252 | StructType *Ty = StructType::get(Builder.getContext(), Members); |
| 253 | Value *Struct = Builder.CreateAlloca(Ty, 0, "omp.userContext"); |
| 254 | |
| 255 | for (unsigned i = 0; i < Values.size(); i++) { |
| 256 | Value *Address = Builder.CreateStructGEP(Struct, i); |
| 257 | Builder.CreateStore(Values[i], Address); |
| 258 | } |
| 259 | |
| 260 | return Struct; |
| 261 | } |
| 262 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 263 | void OMPGenerator::extractValuesFromStruct(SetVector<Value *> OldValues, |
| 264 | Value *Struct, |
| 265 | ValueToValueMapTy &Map) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 266 | for (unsigned i = 0; i < OldValues.size(); i++) { |
| 267 | Value *Address = Builder.CreateStructGEP(Struct, i); |
| 268 | Value *NewValue = Builder.CreateLoad(Address); |
Andy Gibbs | 36e6ca0 | 2013-03-20 15:42:59 +0000 | [diff] [blame] | 269 | Map.insert(std::make_pair(OldValues[i], NewValue)); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 273 | Value *OMPGenerator::createSubfunction(Value *Stride, Value *StructData, |
| 274 | SetVector<Value *> Data, |
| 275 | ValueToValueMapTy &Map, |
| 276 | Function **SubFunction) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 277 | Function *FN = createSubfunctionDefinition(); |
| 278 | |
| 279 | BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *LoadIVBoundsBB, |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 280 | *AfterBB; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 281 | Value *LowerBoundPtr, *UpperBoundPtr, *UserContext, *Ret1, *HasNextSchedule, |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 282 | *LowerBound, *UpperBound, *IV; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 283 | Type *IntPtrTy = getIntPtrTy(); |
| 284 | LLVMContext &Context = FN->getContext(); |
| 285 | |
| 286 | // Store the previous basic block. |
| 287 | PrevBB = Builder.GetInsertBlock(); |
| 288 | |
| 289 | // Create basic blocks. |
| 290 | HeaderBB = BasicBlock::Create(Context, "omp.setup", FN); |
| 291 | ExitBB = BasicBlock::Create(Context, "omp.exit", FN); |
| 292 | CheckNextBB = BasicBlock::Create(Context, "omp.checkNext", FN); |
| 293 | LoadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", FN); |
| 294 | |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 295 | DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 296 | DT.addNewBlock(HeaderBB, PrevBB); |
| 297 | DT.addNewBlock(ExitBB, HeaderBB); |
| 298 | DT.addNewBlock(CheckNextBB, HeaderBB); |
| 299 | DT.addNewBlock(LoadIVBoundsBB, HeaderBB); |
| 300 | |
| 301 | // Fill up basic block HeaderBB. |
| 302 | Builder.SetInsertPoint(HeaderBB); |
| 303 | LowerBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.lowerBoundPtr"); |
| 304 | UpperBoundPtr = Builder.CreateAlloca(IntPtrTy, 0, "omp.upperBoundPtr"); |
| 305 | UserContext = Builder.CreateBitCast(FN->arg_begin(), StructData->getType(), |
| 306 | "omp.userContext"); |
| 307 | |
| 308 | extractValuesFromStruct(Data, UserContext, Map); |
| 309 | Builder.CreateBr(CheckNextBB); |
| 310 | |
| 311 | // Add code to check if another set of iterations will be executed. |
| 312 | Builder.SetInsertPoint(CheckNextBB); |
| 313 | Ret1 = createCallLoopNext(LowerBoundPtr, UpperBoundPtr); |
| 314 | HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), |
| 315 | "omp.hasNextScheduleBlock"); |
| 316 | Builder.CreateCondBr(HasNextSchedule, LoadIVBoundsBB, ExitBB); |
| 317 | |
| 318 | // Add code to to load the iv bounds for this set of iterations. |
| 319 | Builder.SetInsertPoint(LoadIVBoundsBB); |
| 320 | LowerBound = Builder.CreateLoad(LowerBoundPtr, "omp.lowerBound"); |
| 321 | UpperBound = Builder.CreateLoad(UpperBoundPtr, "omp.upperBound"); |
| 322 | |
| 323 | // Subtract one as the upper bound provided by openmp is a < comparison |
| 324 | // whereas the codegenForSequential function creates a <= comparison. |
| 325 | UpperBound = Builder.CreateSub(UpperBound, ConstantInt::get(IntPtrTy, 1), |
| 326 | "omp.upperBoundAdjusted"); |
| 327 | |
| 328 | Builder.CreateBr(CheckNextBB); |
| 329 | Builder.SetInsertPoint(--Builder.GetInsertPoint()); |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 330 | LoopInfo &LI = P->getAnalysis<LoopInfo>(); |
| 331 | IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, LI, DT, AfterBB, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame^] | 332 | ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 333 | |
| 334 | BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); |
| 335 | Builder.SetInsertPoint(AfterBB->begin()); |
| 336 | |
| 337 | // Add code to terminate this openmp subfunction. |
| 338 | Builder.SetInsertPoint(ExitBB); |
| 339 | createCallLoopEndNowait(); |
| 340 | Builder.CreateRetVoid(); |
| 341 | |
| 342 | Builder.SetInsertPoint(LoopBody); |
| 343 | *SubFunction = FN; |
| 344 | |
| 345 | return IV; |
| 346 | } |
| 347 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 348 | Value *OMPGenerator::createParallelLoop(Value *LowerBound, Value *UpperBound, |
| 349 | Value *Stride, |
| 350 | SetVector<Value *> &Values, |
| 351 | ValueToValueMapTy &Map, |
| 352 | BasicBlock::iterator *LoopBody) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 353 | Value *Struct, *IV, *SubfunctionParam, *NumberOfThreads; |
| 354 | Function *SubFunction; |
| 355 | |
| 356 | Struct = loadValuesIntoStruct(Values); |
| 357 | |
| 358 | BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint(); |
| 359 | IV = createSubfunction(Stride, Struct, Values, Map, &SubFunction); |
| 360 | *LoopBody = Builder.GetInsertPoint(); |
| 361 | Builder.SetInsertPoint(PrevInsertPoint); |
| 362 | |
| 363 | // Create call for GOMP_parallel_loop_runtime_start. |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 364 | SubfunctionParam = |
| 365 | Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), "omp_data"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 366 | |
| 367 | NumberOfThreads = Builder.getInt32(0); |
| 368 | |
| 369 | // Add one as the upper bound provided by openmp is a < comparison |
| 370 | // whereas the codegenForSequential function creates a <= comparison. |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 371 | UpperBound = |
| 372 | Builder.CreateAdd(UpperBound, ConstantInt::get(getIntPtrTy(), 1)); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 373 | |
| 374 | createCallParallelLoopStart(SubFunction, SubfunctionParam, NumberOfThreads, |
| 375 | LowerBound, UpperBound, Stride); |
| 376 | Builder.CreateCall(SubFunction, SubfunctionParam); |
| 377 | createCallParallelEnd(); |
| 378 | |
| 379 | return IV; |
| 380 | } |