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