| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1 | //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===// | 
|  | 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 implements the BlockGenerator and VectorBlockGenerator classes, | 
|  | 11 | // which generate sequential code and vectorized code for a polyhedral | 
|  | 12 | // statement, respectively. | 
|  | 13 | // | 
|  | 14 | //===----------------------------------------------------------------------===// | 
|  | 15 |  | 
| Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 16 | #include "polly/CodeGen/BlockGenerators.h" | 
| Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 17 | #include "polly/CodeGen/CodeGeneration.h" | 
| Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 18 | #include "polly/CodeGen/IslExprBuilder.h" | 
| Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 19 | #include "polly/CodeGen/RuntimeDebugBuilder.h" | 
| Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 20 | #include "polly/Options.h" | 
| Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 21 | #include "polly/ScopInfo.h" | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 22 | #include "polly/Support/GICHelper.h" | 
| Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 23 | #include "polly/Support/SCEVValidator.h" | 
| Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 24 | #include "polly/Support/ScopHelper.h" | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/LoopInfo.h" | 
| Johannes Doerfert | f32d651 | 2015-03-01 18:45:58 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/RegionInfo.h" | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ScalarEvolution.h" | 
| Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IntrinsicInst.h" | 
| Tobias Grosser | c989506 | 2015-03-10 15:24:33 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Module.h" | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | 
| Tobias Grosser | 1b9d25a | 2015-09-27 11:17:22 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" | 
| Johannes Doerfert | f32d651 | 2015-03-01 18:45:58 +0000 | [diff] [blame] | 32 | #include "isl/aff.h" | 
|  | 33 | #include "isl/ast.h" | 
| Johannes Doerfert | f32d651 | 2015-03-01 18:45:58 +0000 | [diff] [blame] | 34 | #include "isl/ast_build.h" | 
| Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 35 | #include "isl/set.h" | 
| Johannes Doerfert | f32d651 | 2015-03-01 18:45:58 +0000 | [diff] [blame] | 36 | #include <deque> | 
|  | 37 |  | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 38 | using namespace llvm; | 
|  | 39 | using namespace polly; | 
|  | 40 |  | 
| Tobias Grosser | 878aba4 | 2014-10-22 23:22:41 +0000 | [diff] [blame] | 41 | static cl::opt<bool> Aligned("enable-polly-aligned", | 
|  | 42 | cl::desc("Assumed aligned memory accesses."), | 
|  | 43 | cl::Hidden, cl::init(false), cl::ZeroOrMore, | 
|  | 44 | cl::cat(PollyCategory)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 45 |  | 
| Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 46 | static cl::opt<bool> DebugPrinting( | 
|  | 47 | "polly-codegen-add-debug-printing", | 
|  | 48 | cl::desc("Add printf calls that show the values loaded/stored."), | 
|  | 49 | cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); | 
|  | 50 |  | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 51 | BlockGenerator::BlockGenerator( | 
|  | 52 | PollyIRBuilder &B, LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT, | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 53 | AllocaMapTy &ScalarMap, EscapeUsersAllocaMapTy &EscapeMap, | 
|  | 54 | ValueMapT &GlobalMap, IslExprBuilder *ExprBuilder, BasicBlock *StartBlock) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 55 | : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT), | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 56 | EntryBB(nullptr), ScalarMap(ScalarMap), EscapeMap(EscapeMap), | 
|  | 57 | GlobalMap(GlobalMap), StartBlock(StartBlock) {} | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 58 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 59 | Value *BlockGenerator::trySynthesizeNewValue(ScopStmt &Stmt, Value *Old, | 
| Tobias Grosser | 33cb9f9 | 2015-09-30 11:56:19 +0000 | [diff] [blame] | 60 | ValueMapT &BBMap, | 
|  | 61 | LoopToScevMapT <S, | 
|  | 62 | Loop *L) const { | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 63 | if (!SE.isSCEVable(Old->getType())) | 
|  | 64 | return nullptr; | 
| Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 65 |  | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 66 | const SCEV *Scev = SE.getSCEVAtScope(Old, L); | 
|  | 67 | if (!Scev) | 
|  | 68 | return nullptr; | 
| Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 69 |  | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 70 | if (isa<SCEVCouldNotCompute>(Scev)) | 
|  | 71 | return nullptr; | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 72 |  | 
| Sanjoy Das | 03bcb91 | 2016-05-29 07:33:16 +0000 | [diff] [blame] | 73 | const SCEV *NewScev = SCEVLoopAddRecRewriter::rewrite(Scev, LTS, SE); | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 74 | ValueMapT VTV; | 
|  | 75 | VTV.insert(BBMap.begin(), BBMap.end()); | 
|  | 76 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 77 |  | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 78 | Scop &S = *Stmt.getParent(); | 
| Johannes Doerfert | 3f52e35 | 2016-05-23 12:38:05 +0000 | [diff] [blame] | 79 | const DataLayout &DL = S.getFunction().getParent()->getDataLayout(); | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 80 | auto IP = Builder.GetInsertPoint(); | 
|  | 81 |  | 
|  | 82 | assert(IP != Builder.GetInsertBlock()->end() && | 
|  | 83 | "Only instructions can be insert points for SCEVExpander"); | 
|  | 84 | Value *Expanded = | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 85 | expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), &*IP, &VTV, | 
|  | 86 | StartBlock->getSinglePredecessor()); | 
| Johannes Doerfert | 42df8d1 | 2015-12-22 19:08:01 +0000 | [diff] [blame] | 87 |  | 
|  | 88 | BBMap[Old] = Expanded; | 
|  | 89 | return Expanded; | 
| Tobias Grosser | 33cb9f9 | 2015-09-30 11:56:19 +0000 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 92 | Value *BlockGenerator::getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap, | 
|  | 93 | LoopToScevMapT <S, Loop *L) const { | 
| Tobias Grosser | 9bd0dad | 2015-12-14 16:19:59 +0000 | [diff] [blame] | 94 | // Constants that do not reference any named value can always remain | 
| Michael Kruse | 5bbc0e1 | 2015-12-14 23:41:32 +0000 | [diff] [blame] | 95 | // unchanged. Handle them early to avoid expensive map lookups. We do not take | 
| Tobias Grosser | 9bd0dad | 2015-12-14 16:19:59 +0000 | [diff] [blame] | 96 | // the fast-path for external constants which are referenced through globals | 
|  | 97 | // as these may need to be rewritten when distributing code accross different | 
|  | 98 | // LLVM modules. | 
|  | 99 | if (isa<Constant>(Old) && !isa<GlobalValue>(Old)) | 
| Tobias Grosser | 6f764bb | 2015-12-14 16:19:54 +0000 | [diff] [blame] | 100 | return Old; | 
| Tobias Grosser | 33cb9f9 | 2015-09-30 11:56:19 +0000 | [diff] [blame] | 101 |  | 
| Johannes Doerfert | 28f8ac1 | 2015-12-22 19:08:24 +0000 | [diff] [blame] | 102 | // Inline asm is like a constant to us. | 
|  | 103 | if (isa<InlineAsm>(Old)) | 
|  | 104 | return Old; | 
|  | 105 |  | 
| Tobias Grosser | 33cb9f9 | 2015-09-30 11:56:19 +0000 | [diff] [blame] | 106 | if (Value *New = GlobalMap.lookup(Old)) { | 
|  | 107 | if (Value *NewRemapped = GlobalMap.lookup(New)) | 
|  | 108 | New = NewRemapped; | 
|  | 109 | if (Old->getType()->getScalarSizeInBits() < | 
|  | 110 | New->getType()->getScalarSizeInBits()) | 
|  | 111 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); | 
|  | 112 |  | 
|  | 113 | return New; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | if (Value *New = BBMap.lookup(Old)) | 
|  | 117 | return New; | 
|  | 118 |  | 
|  | 119 | if (Value *New = trySynthesizeNewValue(Stmt, Old, BBMap, LTS, L)) | 
|  | 120 | return New; | 
|  | 121 |  | 
| Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 122 | // A scop-constant value defined by a global or a function parameter. | 
|  | 123 | if (isa<GlobalValue>(Old) || isa<Argument>(Old)) | 
| Tobias Grosser | 6f764bb | 2015-12-14 16:19:54 +0000 | [diff] [blame] | 124 | return Old; | 
| Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 125 |  | 
|  | 126 | // A scop-constant value defined by an instruction executed outside the scop. | 
|  | 127 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) | 
| Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 128 | if (!Stmt.getParent()->contains(Inst->getParent())) | 
| Tobias Grosser | 6f764bb | 2015-12-14 16:19:54 +0000 | [diff] [blame] | 129 | return Old; | 
| Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 130 |  | 
|  | 131 | // The scalar dependence is neither available nor SCEVCodegenable. | 
| Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 132 | llvm_unreachable("Unexpected scalar dependence in region!"); | 
| Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 133 | return nullptr; | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 136 | void BlockGenerator::copyInstScalar(ScopStmt &Stmt, Instruction *Inst, | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 137 | ValueMapT &BBMap, LoopToScevMapT <S) { | 
| Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 138 | // We do not generate debug intrinsics as we did not investigate how to | 
|  | 139 | // copy them correctly. At the current state, they just crash the code | 
|  | 140 | // generation as the meta-data operands are not correctly copied. | 
|  | 141 | if (isa<DbgInfoIntrinsic>(Inst)) | 
|  | 142 | return; | 
|  | 143 |  | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 144 | Instruction *NewInst = Inst->clone(); | 
|  | 145 |  | 
|  | 146 | // Replace old operands with the new ones. | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 147 | for (Value *OldOperand : Inst->operands()) { | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 148 | Value *NewOperand = | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 149 | getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForStmt(Stmt)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 150 |  | 
|  | 151 | if (!NewOperand) { | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 152 | assert(!isa<StoreInst>(NewInst) && | 
|  | 153 | "Store instructions are always needed!"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 154 | delete NewInst; | 
|  | 155 | return; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | Builder.Insert(NewInst); | 
|  | 162 | BBMap[Inst] = NewInst; | 
|  | 163 |  | 
|  | 164 | if (!NewInst->getType()->isVoidTy()) | 
|  | 165 | NewInst->setName("p_" + Inst->getName()); | 
|  | 166 | } | 
|  | 167 |  | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 168 | Value * | 
|  | 169 | BlockGenerator::generateLocationAccessed(ScopStmt &Stmt, MemAccInst Inst, | 
|  | 170 | ValueMapT &BBMap, LoopToScevMapT <S, | 
|  | 171 | isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | 0921477 | 2015-12-15 23:49:53 +0000 | [diff] [blame] | 172 | const MemoryAccess &MA = Stmt.getArrayAccessFor(Inst); | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 173 | return generateLocationAccessed( | 
|  | 174 | Stmt, getLoopForStmt(Stmt), | 
|  | 175 | Inst.isNull() ? nullptr : Inst.getPointerOperand(), BBMap, LTS, | 
|  | 176 | NewAccesses, MA.getId(), MA.getAccessValue()->getType()); | 
|  | 177 | } | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 178 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 179 | Value *BlockGenerator::generateLocationAccessed( | 
|  | 180 | ScopStmt &Stmt, Loop *L, Value *Pointer, ValueMapT &BBMap, | 
|  | 181 | LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses, __isl_take isl_id *Id, | 
|  | 182 | Type *ExpectedType) { | 
|  | 183 | isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, Id); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 184 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 185 | if (AccessExpr) { | 
|  | 186 | AccessExpr = isl_ast_expr_address_of(AccessExpr); | 
| Tobias Grosser | 98b3ee5 | 2015-09-29 06:44:38 +0000 | [diff] [blame] | 187 | auto Address = ExprBuilder->create(AccessExpr); | 
|  | 188 |  | 
|  | 189 | // Cast the address of this memory access to a pointer type that has the | 
|  | 190 | // same element type as the original access, but uses the address space of | 
|  | 191 | // the newly generated pointer. | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 192 | auto OldPtrTy = ExpectedType->getPointerTo(); | 
| Tobias Grosser | 98b3ee5 | 2015-09-29 06:44:38 +0000 | [diff] [blame] | 193 | auto NewPtrTy = Address->getType(); | 
|  | 194 | OldPtrTy = PointerType::get(OldPtrTy->getElementType(), | 
|  | 195 | NewPtrTy->getPointerAddressSpace()); | 
|  | 196 |  | 
| Tobias Grosser | d840fc7 | 2016-02-04 13:18:42 +0000 | [diff] [blame] | 197 | if (OldPtrTy != NewPtrTy) | 
| Tobias Grosser | 98b3ee5 | 2015-09-29 06:44:38 +0000 | [diff] [blame] | 198 | Address = Builder.CreateBitOrPointerCast(Address, OldPtrTy); | 
| Tobias Grosser | 98b3ee5 | 2015-09-29 06:44:38 +0000 | [diff] [blame] | 199 | return Address; | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 200 | } | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 201 | assert( | 
|  | 202 | Pointer && | 
|  | 203 | "If expression was not generated, must use the original pointer value"); | 
|  | 204 | return getNewValue(Stmt, Pointer, BBMap, LTS, L); | 
|  | 205 | } | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 206 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 207 | Value * | 
|  | 208 | BlockGenerator::getImplicitAddress(MemoryAccess &Access, Loop *L, | 
|  | 209 | LoopToScevMapT <S, ValueMapT &BBMap, | 
|  | 210 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
|  | 211 | if (Access.isLatestArrayKind()) | 
|  | 212 | return generateLocationAccessed(*Access.getStatement(), L, nullptr, BBMap, | 
|  | 213 | LTS, NewAccesses, Access.getId(), | 
|  | 214 | Access.getAccessValue()->getType()); | 
|  | 215 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 216 | return getOrCreateAlloca(Access); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 219 | Loop *BlockGenerator::getLoopForStmt(const ScopStmt &Stmt) const { | 
| Michael Kruse | 375cb5f | 2016-02-24 22:08:24 +0000 | [diff] [blame] | 220 | auto *StmtBB = Stmt.getEntryBlock(); | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 221 | return LI.getLoopFor(StmtBB); | 
| Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Michael Kruse | 888ab55 | 2016-09-30 14:34:05 +0000 | [diff] [blame] | 224 | Value *BlockGenerator::generateArrayLoad(ScopStmt &Stmt, LoadInst *Load, | 
|  | 225 | ValueMapT &BBMap, LoopToScevMapT <S, | 
|  | 226 | isl_id_to_ast_expr *NewAccesses) { | 
| Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 227 | if (Value *PreloadLoad = GlobalMap.lookup(Load)) | 
|  | 228 | return PreloadLoad; | 
|  | 229 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 230 | Value *NewPointer = | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 231 | generateLocationAccessed(Stmt, Load, BBMap, LTS, NewAccesses); | 
| Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 232 | Value *ScalarLoad = Builder.CreateAlignedLoad( | 
|  | 233 | NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); | 
| Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 234 |  | 
|  | 235 | if (DebugPrinting) | 
|  | 236 | RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer, | 
|  | 237 | ": ", ScalarLoad, "\n"); | 
|  | 238 |  | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 239 | return ScalarLoad; | 
|  | 240 | } | 
|  | 241 |  | 
| Michael Kruse | 888ab55 | 2016-09-30 14:34:05 +0000 | [diff] [blame] | 242 | void BlockGenerator::generateArrayStore(ScopStmt &Stmt, StoreInst *Store, | 
|  | 243 | ValueMapT &BBMap, LoopToScevMapT <S, | 
|  | 244 | isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 245 | Value *NewPointer = | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 246 | generateLocationAccessed(Stmt, Store, BBMap, LTS, NewAccesses); | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 247 | Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, LTS, | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 248 | getLoopForStmt(Stmt)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 249 |  | 
| Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 250 | if (DebugPrinting) | 
|  | 251 | RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to  ", NewPointer, | 
|  | 252 | ": ", ValueOperand, "\n"); | 
|  | 253 |  | 
| Tobias Grosser | c186ac7 | 2015-08-11 08:13:15 +0000 | [diff] [blame] | 254 | Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment()); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Johannes Doerfert | 5dced26 | 2015-12-22 19:08:49 +0000 | [diff] [blame] | 257 | bool BlockGenerator::canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst) { | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 258 | Loop *L = getLoopForStmt(Stmt); | 
| Johannes Doerfert | 5dced26 | 2015-12-22 19:08:49 +0000 | [diff] [blame] | 259 | return (Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) && | 
| Michael Kruse | 11c5e079 | 2016-11-29 15:11:04 +0000 | [diff] [blame] | 260 | canSynthesize(Inst, *Stmt.getParent(), &SE, L); | 
| Johannes Doerfert | 5dced26 | 2015-12-22 19:08:49 +0000 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 263 | void BlockGenerator::copyInstruction(ScopStmt &Stmt, Instruction *Inst, | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 264 | ValueMapT &BBMap, LoopToScevMapT <S, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 265 | isl_id_to_ast_expr *NewAccesses) { | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 266 | // Terminator instructions control the control flow. They are explicitly | 
|  | 267 | // expressed in the clast and do not need to be copied. | 
|  | 268 | if (Inst->isTerminator()) | 
|  | 269 | return; | 
|  | 270 |  | 
| Johannes Doerfert | 5dced26 | 2015-12-22 19:08:49 +0000 | [diff] [blame] | 271 | // Synthesizable statements will be generated on-demand. | 
|  | 272 | if (canSyntheziseInStmt(Stmt, Inst)) | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 273 | return; | 
|  | 274 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 275 | if (auto *Load = dyn_cast<LoadInst>(Inst)) { | 
| Michael Kruse | 888ab55 | 2016-09-30 14:34:05 +0000 | [diff] [blame] | 276 | Value *NewLoad = generateArrayLoad(Stmt, Load, BBMap, LTS, NewAccesses); | 
| Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 277 | // Compute NewLoad before its insertion in BBMap to make the insertion | 
|  | 278 | // deterministic. | 
| Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 279 | BBMap[Load] = NewLoad; | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 280 | return; | 
|  | 281 | } | 
|  | 282 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 283 | if (auto *Store = dyn_cast<StoreInst>(Inst)) { | 
| Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 284 | // Identified as redundant by -polly-simplify. | 
|  | 285 | if (!Stmt.getArrayAccessOrNULLFor(Store)) | 
|  | 286 | return; | 
|  | 287 |  | 
| Michael Kruse | 888ab55 | 2016-09-30 14:34:05 +0000 | [diff] [blame] | 288 | generateArrayStore(Stmt, Store, BBMap, LTS, NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 289 | return; | 
|  | 290 | } | 
|  | 291 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 292 | if (auto *PHI = dyn_cast<PHINode>(Inst)) { | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 293 | copyPHIInstruction(Stmt, PHI, BBMap, LTS); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 294 | return; | 
|  | 295 | } | 
|  | 296 |  | 
| Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 297 | // Skip some special intrinsics for which we do not adjust the semantics to | 
|  | 298 | // the new schedule. All others are handled like every other instruction. | 
| Tobias Grosser | 9c0ffe3 | 2015-08-30 16:57:15 +0000 | [diff] [blame] | 299 | if (isIgnoredIntrinsic(Inst)) | 
|  | 300 | return; | 
| Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 301 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 302 | copyInstScalar(Stmt, Inst, BBMap, LTS); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Tobias Grosser | 9d12d8a | 2016-07-21 11:48:36 +0000 | [diff] [blame] | 305 | void BlockGenerator::removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap) { | 
| Tobias Grosser | c59b3ce | 2016-08-09 08:59:05 +0000 | [diff] [blame] | 306 | auto NewBB = Builder.GetInsertBlock(); | 
|  | 307 | for (auto I = NewBB->rbegin(); I != NewBB->rend(); I++) { | 
|  | 308 | Instruction *NewInst = &*I; | 
| Tobias Grosser | 9d12d8a | 2016-07-21 11:48:36 +0000 | [diff] [blame] | 309 |  | 
|  | 310 | if (!isInstructionTriviallyDead(NewInst)) | 
|  | 311 | continue; | 
|  | 312 |  | 
| Tobias Grosser | c59b3ce | 2016-08-09 08:59:05 +0000 | [diff] [blame] | 313 | for (auto Pair : BBMap) | 
|  | 314 | if (Pair.second == NewInst) { | 
|  | 315 | BBMap.erase(Pair.first); | 
|  | 316 | } | 
|  | 317 |  | 
| Tobias Grosser | 9d12d8a | 2016-07-21 11:48:36 +0000 | [diff] [blame] | 318 | NewInst->eraseFromParent(); | 
| Tobias Grosser | c59b3ce | 2016-08-09 08:59:05 +0000 | [diff] [blame] | 319 | I = NewBB->rbegin(); | 
| Tobias Grosser | 9d12d8a | 2016-07-21 11:48:36 +0000 | [diff] [blame] | 320 | } | 
|  | 321 | } | 
|  | 322 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 323 | void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 324 | isl_id_to_ast_expr *NewAccesses) { | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 325 | assert(Stmt.isBlockStmt() && | 
|  | 326 | "Only block statements can be copied by the block generator"); | 
|  | 327 |  | 
|  | 328 | ValueMapT BBMap; | 
|  | 329 |  | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 330 | BasicBlock *BB = Stmt.getBasicBlock(); | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 331 | copyBB(Stmt, BB, BBMap, LTS, NewAccesses); | 
| Tobias Grosser | 9d12d8a | 2016-07-21 11:48:36 +0000 | [diff] [blame] | 332 | removeDeadInstructions(BB, BBMap); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 333 | } | 
|  | 334 |  | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 335 | BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 336 | BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), | 
|  | 337 | &*Builder.GetInsertPoint(), &DT, &LI); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 338 | CopyBB->setName("polly.stmt." + BB->getName()); | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 339 | return CopyBB; | 
|  | 340 | } | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 341 |  | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 342 | BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 343 | ValueMapT &BBMap, LoopToScevMapT <S, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 344 | isl_id_to_ast_expr *NewAccesses) { | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 345 | BasicBlock *CopyBB = splitBB(BB); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 346 | Builder.SetInsertPoint(&CopyBB->front()); | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 347 | generateScalarLoads(Stmt, LTS, BBMap, NewAccesses); | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 348 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 349 | copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses); | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 350 |  | 
|  | 351 | // After a basic block was copied store all scalars that escape this block in | 
|  | 352 | // their alloca. | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 353 | generateScalarStores(Stmt, LTS, BBMap, NewAccesses); | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 354 | return CopyBB; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB, | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 358 | ValueMapT &BBMap, LoopToScevMapT <S, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 359 | isl_id_to_ast_expr *NewAccesses) { | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 360 | EntryBB = &CopyBB->getParent()->getEntryBlock(); | 
|  | 361 |  | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 362 | for (Instruction &Inst : *BB) | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 363 | copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 364 | } | 
|  | 365 |  | 
| Johannes Doerfert | 800e17a | 2016-02-02 14:16:01 +0000 | [diff] [blame] | 366 | Value *BlockGenerator::getOrCreateAlloca(const MemoryAccess &Access) { | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 367 | assert(!Access.isLatestArrayKind() && "Trying to get alloca for array kind"); | 
| Tobias Grosser | 3216f85 | 2016-08-04 06:55:53 +0000 | [diff] [blame] | 368 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 369 | return getOrCreateAlloca(Access.getLatestScopArrayInfo()); | 
| Tobias Grosser | a4f0988 | 2015-10-17 22:16:00 +0000 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
|  | 372 | Value *BlockGenerator::getOrCreateAlloca(const ScopArrayInfo *Array) { | 
| Tobias Grosser | 3216f85 | 2016-08-04 06:55:53 +0000 | [diff] [blame] | 373 | assert(!Array->isArrayKind() && "Trying to get alloca for array kind"); | 
|  | 374 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 375 | auto &Addr = ScalarMap[Array]; | 
|  | 376 |  | 
|  | 377 | if (Addr) { | 
|  | 378 | // Allow allocas to be (temporarily) redirected once by adding a new | 
|  | 379 | // old-alloca-addr to new-addr mapping to GlobalMap. This funcitionality | 
|  | 380 | // is used for example by the OpenMP code generation where a first use | 
|  | 381 | // of a scalar while still in the host code allocates a normal alloca with | 
|  | 382 | // getOrCreateAlloca. When the values of this scalar are accessed during | 
|  | 383 | // the generation of the parallel subfunction, these values are copied over | 
|  | 384 | // to the parallel subfunction and each request for a scalar alloca slot | 
|  | 385 | // must be forwared to the temporary in-subfunction slot. This mapping is | 
|  | 386 | // removed when the subfunction has been generated and again normal host | 
| Tobias Grosser | 682c511 | 2017-01-28 11:39:02 +0000 | [diff] [blame] | 387 | // code is generated. Due to the following reasons it is not possible to | 
|  | 388 | // perform the GlobalMap lookup right after creating the alloca below, but | 
|  | 389 | // instead we need to check GlobalMap at each call to getOrCreateAlloca: | 
|  | 390 | // | 
|  | 391 | //   1) GlobalMap may be changed multiple times (for each parallel loop), | 
|  | 392 | //   2) The temporary mapping is commonly only known after the initial | 
|  | 393 | //      alloca has already been generated, and | 
|  | 394 | //   3) The original alloca value must be restored after leaving the | 
|  | 395 | //      sub-function. | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 396 | if (Value *NewAddr = GlobalMap.lookup(&*Addr)) | 
|  | 397 | return NewAddr; | 
|  | 398 | return Addr; | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | Type *Ty = Array->getElementType(); | 
|  | 402 | Value *ScalarBase = Array->getBasePtr(); | 
|  | 403 | std::string NameExt; | 
| Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 404 | if (Array->isPHIKind()) | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 405 | NameExt = ".phiops"; | 
| Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 406 | else | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 407 | NameExt = ".s2a"; | 
|  | 408 |  | 
|  | 409 | Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt); | 
|  | 410 | EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); | 
|  | 411 | Addr->insertBefore(&*EntryBB->getFirstInsertionPt()); | 
|  | 412 |  | 
|  | 413 | return Addr; | 
| Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 416 | void BlockGenerator::handleOutsideUsers(const Scop &S, ScopArrayInfo *Array) { | 
|  | 417 | Instruction *Inst = cast<Instruction>(Array->getBasePtr()); | 
| Tobias Grosser | b79a67d | 2015-08-28 08:23:35 +0000 | [diff] [blame] | 418 |  | 
| Tobias Grosser | 2985400 | 2015-08-30 15:03:59 +0000 | [diff] [blame] | 419 | // If there are escape users we get the alloca for this instruction and put it | 
|  | 420 | // in the EscapeMap for later finalization. Lastly, if the instruction was | 
|  | 421 | // copied multiple times we already did this and can exit. | 
| Michael Kruse | acb6ade | 2015-08-18 17:25:48 +0000 | [diff] [blame] | 422 | if (EscapeMap.count(Inst)) | 
|  | 423 | return; | 
| Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 424 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 425 | EscapeUserVectorTy EscapeUsers; | 
|  | 426 | for (User *U : Inst->users()) { | 
|  | 427 |  | 
|  | 428 | // Non-instruction user will never escape. | 
|  | 429 | Instruction *UI = dyn_cast<Instruction>(U); | 
|  | 430 | if (!UI) | 
|  | 431 | continue; | 
|  | 432 |  | 
| Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 433 | if (S.contains(UI)) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 434 | continue; | 
|  | 435 |  | 
|  | 436 | EscapeUsers.push_back(UI); | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | // Exit if no escape uses were found. | 
|  | 440 | if (EscapeUsers.empty()) | 
|  | 441 | return; | 
|  | 442 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 443 | // Get or create an escape alloca for this instruction. | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 444 | auto *ScalarAddr = getOrCreateAlloca(Array); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 445 |  | 
|  | 446 | // Remember that this instruction has escape uses and the escape alloca. | 
|  | 447 | EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers)); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 448 | } | 
|  | 449 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 450 | void BlockGenerator::generateScalarLoads( | 
|  | 451 | ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, | 
|  | 452 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 453 | for (MemoryAccess *MA : Stmt) { | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 454 | if (MA->isOriginalArrayKind() || MA->isWrite()) | 
| Tobias Grosser | d4dd6ec | 2015-07-27 17:57:58 +0000 | [diff] [blame] | 455 | continue; | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 456 |  | 
| Michael Kruse | 77394f1 | 2016-09-30 14:01:46 +0000 | [diff] [blame] | 457 | #ifndef NDEBUG | 
|  | 458 | auto *StmtDom = Stmt.getDomain(); | 
|  | 459 | auto *AccDom = isl_map_domain(MA->getAccessRelation()); | 
|  | 460 | assert(isl_set_is_subset(StmtDom, AccDom) && | 
|  | 461 | "Scalar must be loaded in all statement instances"); | 
|  | 462 | isl_set_free(StmtDom); | 
|  | 463 | isl_set_free(AccDom); | 
|  | 464 | #endif | 
|  | 465 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 466 | auto *Address = | 
|  | 467 | getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, NewAccesses); | 
| Michael Kruse | eac9726 | 2016-02-24 22:08:14 +0000 | [diff] [blame] | 468 | assert((!isa<Instruction>(Address) || | 
|  | 469 | DT.dominates(cast<Instruction>(Address)->getParent(), | 
|  | 470 | Builder.GetInsertBlock())) && | 
|  | 471 | "Domination violation"); | 
| Tobias Grosser | 583be06 | 2017-02-09 23:54:23 +0000 | [diff] [blame] | 472 | BBMap[MA->getAccessValue()] = | 
| Tobias Grosser | 2fc50df | 2015-08-30 19:51:01 +0000 | [diff] [blame] | 473 | Builder.CreateLoad(Address, Address->getName() + ".reload"); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 474 | } | 
|  | 475 | } | 
|  | 476 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 477 | void BlockGenerator::generateScalarStores( | 
|  | 478 | ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, | 
|  | 479 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | f2cdd14 | 2016-01-26 10:01:35 +0000 | [diff] [blame] | 480 | Loop *L = LI.getLoopFor(Stmt.getBasicBlock()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 481 |  | 
| Tobias Grosser | 21a059a | 2017-01-16 14:08:10 +0000 | [diff] [blame] | 482 | assert(Stmt.isBlockStmt() && | 
|  | 483 | "Region statements need to use the generateScalarStores() function in " | 
|  | 484 | "the RegionGenerator"); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 485 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 486 | for (MemoryAccess *MA : Stmt) { | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 487 | if (MA->isOriginalArrayKind() || MA->isRead()) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 488 | continue; | 
|  | 489 |  | 
| Michael Kruse | 77394f1 | 2016-09-30 14:01:46 +0000 | [diff] [blame] | 490 | #ifndef NDEBUG | 
|  | 491 | auto *StmtDom = Stmt.getDomain(); | 
|  | 492 | auto *AccDom = isl_map_domain(MA->getAccessRelation()); | 
|  | 493 | assert(isl_set_is_subset(StmtDom, AccDom) && | 
|  | 494 | "Scalar must be stored in all statement instances"); | 
|  | 495 | isl_set_free(StmtDom); | 
|  | 496 | isl_set_free(AccDom); | 
|  | 497 | #endif | 
|  | 498 |  | 
| Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 499 | Value *Val = MA->getAccessValue(); | 
| Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 500 | if (MA->isAnyPHIKind()) { | 
|  | 501 | assert(MA->getIncoming().size() >= 1 && | 
|  | 502 | "Block statements have exactly one exiting block, or multiple but " | 
|  | 503 | "with same incoming block and value"); | 
|  | 504 | assert(std::all_of(MA->getIncoming().begin(), MA->getIncoming().end(), | 
|  | 505 | [&](std::pair<BasicBlock *, Value *> p) -> bool { | 
|  | 506 | return p.first == Stmt.getBasicBlock(); | 
|  | 507 | }) && | 
|  | 508 | "Incoming block must be statement's block"); | 
|  | 509 | Val = MA->getIncoming()[0].second; | 
|  | 510 | } | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 511 | auto Address = | 
|  | 512 | getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, NewAccesses); | 
| Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 513 |  | 
| Tobias Grosser | f2cdd14 | 2016-01-26 10:01:35 +0000 | [diff] [blame] | 514 | Val = getNewValue(Stmt, Val, BBMap, LTS, L); | 
| Michael Kruse | eac9726 | 2016-02-24 22:08:14 +0000 | [diff] [blame] | 515 | assert((!isa<Instruction>(Val) || | 
|  | 516 | DT.dominates(cast<Instruction>(Val)->getParent(), | 
|  | 517 | Builder.GetInsertBlock())) && | 
|  | 518 | "Domination violation"); | 
|  | 519 | assert((!isa<Instruction>(Address) || | 
|  | 520 | DT.dominates(cast<Instruction>(Address)->getParent(), | 
|  | 521 | Builder.GetInsertBlock())) && | 
|  | 522 | "Domination violation"); | 
| Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 523 | Builder.CreateStore(Val, Address); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 524 | } | 
|  | 525 | } | 
|  | 526 |  | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 527 | void BlockGenerator::createScalarInitialization(Scop &S) { | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 528 | BasicBlock *ExitBB = S.getExit(); | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 529 | BasicBlock *PreEntryBB = S.getEnteringBlock(); | 
| Johannes Doerfert | 188542f | 2015-11-09 00:21:21 +0000 | [diff] [blame] | 530 |  | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 531 | Builder.SetInsertPoint(&*StartBlock->begin()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 532 |  | 
| Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 533 | for (auto &Array : S.arrays()) { | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 534 | if (Array->getNumberOfDimensions() != 0) | 
|  | 535 | continue; | 
| Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 536 | if (Array->isPHIKind()) { | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 537 | // For PHI nodes, the only values we need to store are the ones that | 
|  | 538 | // reach the PHI node from outside the region. In general there should | 
|  | 539 | // only be one such incoming edge and this edge should enter through | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 540 | // 'PreEntryBB'. | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 541 | auto PHI = cast<PHINode>(Array->getBasePtr()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 542 |  | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 543 | for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++) | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 544 | if (!S.contains(*BI) && *BI != PreEntryBB) | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 545 | llvm_unreachable("Incoming edges from outside the scop should always " | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 546 | "come from PreEntryBB"); | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 547 |  | 
| Eli Friedman | acf8006 | 2016-11-02 22:32:23 +0000 | [diff] [blame] | 548 | int Idx = PHI->getBasicBlockIndex(PreEntryBB); | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 549 | if (Idx < 0) | 
|  | 550 | continue; | 
|  | 551 |  | 
|  | 552 | Value *ScalarValue = PHI->getIncomingValue(Idx); | 
|  | 553 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 554 | Builder.CreateStore(ScalarValue, getOrCreateAlloca(Array)); | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 555 | continue; | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); | 
|  | 559 |  | 
| Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 560 | if (Inst && S.contains(Inst)) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 561 | continue; | 
|  | 562 |  | 
| Johannes Doerfert | 188542f | 2015-11-09 00:21:21 +0000 | [diff] [blame] | 563 | // PHI nodes that are not marked as such in their SAI object are either exit | 
|  | 564 | // PHI nodes we model as common scalars but without initialization, or | 
|  | 565 | // incoming phi nodes that need to be initialized. Check if the first is the | 
|  | 566 | // case for Inst and do not create and initialize memory if so. | 
|  | 567 | if (auto *PHI = dyn_cast_or_null<PHINode>(Inst)) | 
|  | 568 | if (!S.hasSingleExitEdge() && PHI->getBasicBlockIndex(ExitBB) >= 0) | 
|  | 569 | continue; | 
| Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame] | 570 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 571 | Builder.CreateStore(Array->getBasePtr(), getOrCreateAlloca(Array)); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 572 | } | 
|  | 573 | } | 
|  | 574 |  | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 575 | void BlockGenerator::createScalarFinalization(Scop &S) { | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 576 | // The exit block of the __unoptimized__ region. | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 577 | BasicBlock *ExitBB = S.getExitingBlock(); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 578 | // The merge block __just after__ the region and the optimized region. | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 579 | BasicBlock *MergeBB = S.getExit(); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 580 |  | 
|  | 581 | // The exit block of the __optimized__ region. | 
|  | 582 | BasicBlock *OptExitBB = *(pred_begin(MergeBB)); | 
|  | 583 | if (OptExitBB == ExitBB) | 
|  | 584 | OptExitBB = *(++pred_begin(MergeBB)); | 
|  | 585 |  | 
|  | 586 | Builder.SetInsertPoint(OptExitBB->getTerminator()); | 
|  | 587 | for (const auto &EscapeMapping : EscapeMap) { | 
|  | 588 | // Extract the escaping instruction and the escaping users as well as the | 
|  | 589 | // alloca the instruction was demoted to. | 
| Michael Kruse | fbde435 | 2016-08-05 16:45:51 +0000 | [diff] [blame] | 590 | Instruction *EscapeInst = EscapeMapping.first; | 
|  | 591 | const auto &EscapeMappingValue = EscapeMapping.second; | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 592 | const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second; | 
| Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 593 | Value *ScalarAddr = EscapeMappingValue.first; | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 594 |  | 
|  | 595 | // Reload the demoted instruction in the optimized version of the SCoP. | 
| Johannes Doerfert | d8b6ad2 | 2015-10-18 12:36:42 +0000 | [diff] [blame] | 596 | Value *EscapeInstReload = | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 597 | Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload"); | 
| Johannes Doerfert | d8b6ad2 | 2015-10-18 12:36:42 +0000 | [diff] [blame] | 598 | EscapeInstReload = | 
|  | 599 | Builder.CreateBitOrPointerCast(EscapeInstReload, EscapeInst->getType()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 600 |  | 
|  | 601 | // Create the merge PHI that merges the optimized and unoptimized version. | 
|  | 602 | PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2, | 
|  | 603 | EscapeInst->getName() + ".merge"); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 604 | MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 605 |  | 
|  | 606 | // Add the respective values to the merge PHI. | 
|  | 607 | MergePHI->addIncoming(EscapeInstReload, OptExitBB); | 
|  | 608 | MergePHI->addIncoming(EscapeInst, ExitBB); | 
|  | 609 |  | 
|  | 610 | // The information of scalar evolution about the escaping instruction needs | 
|  | 611 | // to be revoked so the new merged instruction will be used. | 
|  | 612 | if (SE.isSCEVable(EscapeInst->getType())) | 
|  | 613 | SE.forgetValue(EscapeInst); | 
|  | 614 |  | 
|  | 615 | // Replace all uses of the demoted instruction with the merge PHI. | 
|  | 616 | for (Instruction *EUser : EscapeUsers) | 
|  | 617 | EUser->replaceUsesOfWith(EscapeInst, MergePHI); | 
|  | 618 | } | 
|  | 619 | } | 
|  | 620 |  | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 621 | void BlockGenerator::findOutsideUsers(Scop &S) { | 
| Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 622 | for (auto &Array : S.arrays()) { | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 623 |  | 
|  | 624 | if (Array->getNumberOfDimensions() != 0) | 
|  | 625 | continue; | 
|  | 626 |  | 
| Tobias Grosser | a535dff | 2015-12-13 19:59:01 +0000 | [diff] [blame] | 627 | if (Array->isPHIKind()) | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 628 | continue; | 
|  | 629 |  | 
|  | 630 | auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); | 
|  | 631 |  | 
|  | 632 | if (!Inst) | 
|  | 633 | continue; | 
|  | 634 |  | 
|  | 635 | // Scop invariant hoisting moves some of the base pointers out of the scop. | 
|  | 636 | // We can ignore these, as the invariant load hoisting already registers the | 
|  | 637 | // relevant outside users. | 
| Johannes Doerfert | 952b530 | 2016-05-23 12:40:48 +0000 | [diff] [blame] | 638 | if (!S.contains(Inst)) | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 639 | continue; | 
|  | 640 |  | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 641 | handleOutsideUsers(S, Array); | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 642 | } | 
|  | 643 | } | 
|  | 644 |  | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 645 | void BlockGenerator::createExitPHINodeMerges(Scop &S) { | 
|  | 646 | if (S.hasSingleExitEdge()) | 
|  | 647 | return; | 
|  | 648 |  | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 649 | auto *ExitBB = S.getExitingBlock(); | 
|  | 650 | auto *MergeBB = S.getExit(); | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 651 | auto *AfterMergeBB = MergeBB->getSingleSuccessor(); | 
|  | 652 | BasicBlock *OptExitBB = *(pred_begin(MergeBB)); | 
|  | 653 | if (OptExitBB == ExitBB) | 
|  | 654 | OptExitBB = *(++pred_begin(MergeBB)); | 
|  | 655 |  | 
|  | 656 | Builder.SetInsertPoint(OptExitBB->getTerminator()); | 
|  | 657 |  | 
| Roman Gareev | d7754a1 | 2016-07-30 09:25:51 +0000 | [diff] [blame] | 658 | for (auto &SAI : S.arrays()) { | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 659 | auto *Val = SAI->getBasePtr(); | 
|  | 660 |  | 
| Michael Kruse | faedfcb | 2016-03-03 17:20:43 +0000 | [diff] [blame] | 661 | // Only Value-like scalars need a merge PHI. Exit block PHIs receive either | 
|  | 662 | // the original PHI's value or the reloaded incoming values from the | 
|  | 663 | // generated code. An llvm::Value is merged between the original code's | 
|  | 664 | // value or the generated one. | 
| Michael Kruse | fa53c86 | 2016-10-12 16:31:09 +0000 | [diff] [blame] | 665 | if (!SAI->isExitPHIKind()) | 
| Michael Kruse | faedfcb | 2016-03-03 17:20:43 +0000 | [diff] [blame] | 666 | continue; | 
|  | 667 |  | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 668 | PHINode *PHI = dyn_cast<PHINode>(Val); | 
|  | 669 | if (!PHI) | 
|  | 670 | continue; | 
|  | 671 |  | 
| Tobias Grosser | a3f6eda | 2015-10-24 19:01:09 +0000 | [diff] [blame] | 672 | if (PHI->getParent() != AfterMergeBB) | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 673 | continue; | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 674 |  | 
|  | 675 | std::string Name = PHI->getName(); | 
| Tobias Grosser | 587f1f5 | 2017-01-28 07:42:10 +0000 | [diff] [blame] | 676 | Value *ScalarAddr = getOrCreateAlloca(SAI); | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 677 | Value *Reload = Builder.CreateLoad(ScalarAddr, Name + ".ph.final_reload"); | 
|  | 678 | Reload = Builder.CreateBitOrPointerCast(Reload, PHI->getType()); | 
|  | 679 | Value *OriginalValue = PHI->getIncomingValueForBlock(MergeBB); | 
| Michael Kruse | faedfcb | 2016-03-03 17:20:43 +0000 | [diff] [blame] | 680 | assert((!isa<Instruction>(OriginalValue) || | 
|  | 681 | cast<Instruction>(OriginalValue)->getParent() != MergeBB) && | 
|  | 682 | "Original value must no be one we just generated."); | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 683 | auto *MergePHI = PHINode::Create(PHI->getType(), 2, Name + ".ph.merge"); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 684 | MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt()); | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 685 | MergePHI->addIncoming(Reload, OptExitBB); | 
|  | 686 | MergePHI->addIncoming(OriginalValue, ExitBB); | 
|  | 687 | int Idx = PHI->getBasicBlockIndex(MergeBB); | 
|  | 688 | PHI->setIncomingValue(Idx, MergePHI); | 
|  | 689 | } | 
|  | 690 | } | 
|  | 691 |  | 
| Tobias Grosser | 1c18440 | 2016-08-18 10:45:57 +0000 | [diff] [blame] | 692 | void BlockGenerator::invalidateScalarEvolution(Scop &S) { | 
|  | 693 | for (auto &Stmt : S) | 
| Roman Gareev | b3224ad | 2016-09-14 06:26:09 +0000 | [diff] [blame] | 694 | if (Stmt.isCopyStmt()) | 
|  | 695 | continue; | 
|  | 696 | else if (Stmt.isBlockStmt()) | 
| Tobias Grosser | 1c18440 | 2016-08-18 10:45:57 +0000 | [diff] [blame] | 697 | for (auto &Inst : *Stmt.getBasicBlock()) | 
|  | 698 | SE.forgetValue(&Inst); | 
|  | 699 | else if (Stmt.isRegionStmt()) | 
|  | 700 | for (auto *BB : Stmt.getRegion()->blocks()) | 
|  | 701 | for (auto &Inst : *BB) | 
|  | 702 | SE.forgetValue(&Inst); | 
|  | 703 | else | 
|  | 704 | llvm_unreachable("Unexpected statement type found"); | 
|  | 705 | } | 
|  | 706 |  | 
| Tobias Grosser | ffa2446 | 2015-10-17 08:54:13 +0000 | [diff] [blame] | 707 | void BlockGenerator::finalizeSCoP(Scop &S) { | 
|  | 708 | findOutsideUsers(S); | 
| Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 709 | createScalarInitialization(S); | 
| Tobias Grosser | 27d742d | 2015-10-24 17:41:29 +0000 | [diff] [blame] | 710 | createExitPHINodeMerges(S); | 
| Johannes Doerfert | ef74443 | 2016-05-23 12:42:38 +0000 | [diff] [blame] | 711 | createScalarFinalization(S); | 
| Tobias Grosser | 1c18440 | 2016-08-18 10:45:57 +0000 | [diff] [blame] | 712 | invalidateScalarEvolution(S); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 713 | } | 
|  | 714 |  | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 715 | VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen, | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 716 | std::vector<LoopToScevMapT> &VLTS, | 
|  | 717 | isl_map *Schedule) | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 718 | : BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) { | 
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 719 | assert(Schedule && "No statement domain provided"); | 
|  | 720 | } | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 721 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 722 | Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, Value *Old, | 
| Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 723 | ValueMapT &VectorMap, | 
|  | 724 | VectorValueMapT &ScalarMaps, | 
|  | 725 | Loop *L) { | 
| Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 726 | if (Value *NewValue = VectorMap.lookup(Old)) | 
|  | 727 | return NewValue; | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 728 |  | 
|  | 729 | int Width = getVectorWidth(); | 
|  | 730 |  | 
|  | 731 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); | 
|  | 732 |  | 
|  | 733 | for (int Lane = 0; Lane < Width; Lane++) | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 734 | Vector = Builder.CreateInsertElement( | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 735 | Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L), | 
| Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 736 | Builder.getInt32(Lane)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 737 |  | 
|  | 738 | VectorMap[Old] = Vector; | 
|  | 739 |  | 
|  | 740 | return Vector; | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { | 
|  | 744 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); | 
|  | 745 | assert(PointerTy && "PointerType expected"); | 
|  | 746 |  | 
|  | 747 | Type *ScalarType = PointerTy->getElementType(); | 
|  | 748 | VectorType *VectorType = VectorType::get(ScalarType, Width); | 
|  | 749 |  | 
|  | 750 | return PointerType::getUnqual(VectorType); | 
|  | 751 | } | 
|  | 752 |  | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 753 | Value *VectorBlockGenerator::generateStrideOneLoad( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 754 | ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 755 | __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) { | 
| Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 756 | unsigned VectorWidth = getVectorWidth(); | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 757 | auto *Pointer = Load->getPointerOperand(); | 
| Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 758 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); | 
|  | 759 | unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; | 
|  | 760 |  | 
| Michael Kruse | 8f25b0c | 2016-02-25 15:52:43 +0000 | [diff] [blame] | 761 | Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[Offset], | 
|  | 762 | VLTS[Offset], NewAccesses); | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 763 | Value *VectorPtr = | 
|  | 764 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); | 
|  | 765 | LoadInst *VecLoad = | 
|  | 766 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 767 | if (!Aligned) | 
|  | 768 | VecLoad->setAlignment(8); | 
|  | 769 |  | 
| Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 770 | if (NegativeStride) { | 
|  | 771 | SmallVector<Constant *, 16> Indices; | 
|  | 772 | for (int i = VectorWidth - 1; i >= 0; i--) | 
|  | 773 | Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); | 
|  | 774 | Constant *SV = llvm::ConstantVector::get(Indices); | 
|  | 775 | Value *RevVecLoad = Builder.CreateShuffleVector( | 
|  | 776 | VecLoad, VecLoad, SV, Load->getName() + "_reverse"); | 
|  | 777 | return RevVecLoad; | 
|  | 778 | } | 
|  | 779 |  | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 780 | return VecLoad; | 
|  | 781 | } | 
|  | 782 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 783 | Value *VectorBlockGenerator::generateStrideZeroLoad( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 784 | ScopStmt &Stmt, LoadInst *Load, ValueMapT &BBMap, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 785 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 786 | auto *Pointer = Load->getPointerOperand(); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 787 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 788 | Value *NewPointer = | 
|  | 789 | generateLocationAccessed(Stmt, Load, BBMap, VLTS[0], NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 790 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, | 
|  | 791 | Load->getName() + "_p_vec_p"); | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 792 | LoadInst *ScalarLoad = | 
|  | 793 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 794 |  | 
|  | 795 | if (!Aligned) | 
|  | 796 | ScalarLoad->setAlignment(8); | 
|  | 797 |  | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 798 | Constant *SplatVector = Constant::getNullValue( | 
|  | 799 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 800 |  | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 801 | Value *VectorLoad = Builder.CreateShuffleVector( | 
|  | 802 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 803 | return VectorLoad; | 
|  | 804 | } | 
|  | 805 |  | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 806 | Value *VectorBlockGenerator::generateUnknownStrideLoad( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 807 | ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps, | 
| Johannes Doerfert | 09e3697 | 2015-10-07 20:17:36 +0000 | [diff] [blame] | 808 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 809 | int VectorWidth = getVectorWidth(); | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 810 | auto *Pointer = Load->getPointerOperand(); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 811 | VectorType *VectorType = VectorType::get( | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 812 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 813 |  | 
|  | 814 | Value *Vector = UndefValue::get(VectorType); | 
|  | 815 |  | 
|  | 816 | for (int i = 0; i < VectorWidth; i++) { | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 817 | Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[i], | 
|  | 818 | VLTS[i], NewAccesses); | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 819 | Value *ScalarLoad = | 
|  | 820 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); | 
|  | 821 | Vector = Builder.CreateInsertElement( | 
|  | 822 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 823 | } | 
|  | 824 |  | 
|  | 825 | return Vector; | 
|  | 826 | } | 
|  | 827 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 828 | void VectorBlockGenerator::generateLoad( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 829 | ScopStmt &Stmt, LoadInst *Load, ValueMapT &VectorMap, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 830 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Johannes Doerfert | c1db67e | 2015-09-29 23:47:21 +0000 | [diff] [blame] | 831 | if (Value *PreloadLoad = GlobalMap.lookup(Load)) { | 
|  | 832 | VectorMap[Load] = Builder.CreateVectorSplat(getVectorWidth(), PreloadLoad, | 
|  | 833 | Load->getName() + "_p"); | 
|  | 834 | return; | 
|  | 835 | } | 
|  | 836 |  | 
| Tobias Grosser | 2873645 | 2015-03-23 07:00:36 +0000 | [diff] [blame] | 837 | if (!VectorType::isValidElementType(Load->getType())) { | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 838 | for (int i = 0; i < getVectorWidth(); i++) | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 839 | ScalarMaps[i][Load] = | 
| Michael Kruse | 888ab55 | 2016-09-30 14:34:05 +0000 | [diff] [blame] | 840 | generateArrayLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 841 | return; | 
|  | 842 | } | 
|  | 843 |  | 
| Tobias Grosser | 184a492 | 2015-12-15 23:50:01 +0000 | [diff] [blame] | 844 | const MemoryAccess &Access = Stmt.getArrayAccessFor(Load); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 845 |  | 
| Tobias Grosser | 9549398 | 2014-04-18 09:46:35 +0000 | [diff] [blame] | 846 | // Make sure we have scalar values available to access the pointer to | 
|  | 847 | // the data location. | 
|  | 848 | extractScalarValues(Load, VectorMap, ScalarMaps); | 
|  | 849 |  | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 850 | Value *NewLoad; | 
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 851 | if (Access.isStrideZero(isl_map_copy(Schedule))) | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 852 | NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses); | 
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 853 | else if (Access.isStrideOne(isl_map_copy(Schedule))) | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 854 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses); | 
| Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 855 | else if (Access.isStrideX(isl_map_copy(Schedule), -1)) | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 856 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 857 | else | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 858 | NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 859 |  | 
|  | 860 | VectorMap[Load] = NewLoad; | 
|  | 861 | } | 
|  | 862 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 863 | void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, UnaryInstruction *Inst, | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 864 | ValueMapT &VectorMap, | 
|  | 865 | VectorValueMapT &ScalarMaps) { | 
|  | 866 | int VectorWidth = getVectorWidth(); | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 867 | Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap, | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 868 | ScalarMaps, getLoopForStmt(Stmt)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 869 |  | 
|  | 870 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); | 
|  | 871 |  | 
|  | 872 | const CastInst *Cast = dyn_cast<CastInst>(Inst); | 
|  | 873 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); | 
|  | 874 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); | 
|  | 875 | } | 
|  | 876 |  | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 877 | void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, BinaryOperator *Inst, | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 878 | ValueMapT &VectorMap, | 
|  | 879 | VectorValueMapT &ScalarMaps) { | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 880 | Loop *L = getLoopForStmt(Stmt); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 881 | Value *OpZero = Inst->getOperand(0); | 
|  | 882 | Value *OpOne = Inst->getOperand(1); | 
|  | 883 |  | 
|  | 884 | Value *NewOpZero, *NewOpOne; | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 885 | NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L); | 
|  | 886 | NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 887 |  | 
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 888 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 889 | Inst->getName() + "p_vec"); | 
|  | 890 | VectorMap[Inst] = NewInst; | 
|  | 891 | } | 
|  | 892 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 893 | void VectorBlockGenerator::copyStore( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 894 | ScopStmt &Stmt, StoreInst *Store, ValueMapT &VectorMap, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 895 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | 184a492 | 2015-12-15 23:50:01 +0000 | [diff] [blame] | 896 | const MemoryAccess &Access = Stmt.getArrayAccessFor(Store); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 897 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 898 | auto *Pointer = Store->getPointerOperand(); | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 899 | Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap, | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 900 | ScalarMaps, getLoopForStmt(Stmt)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 901 |  | 
| Tobias Grosser | 50fd701 | 2014-04-17 23:13:49 +0000 | [diff] [blame] | 902 | // Make sure we have scalar values available to access the pointer to | 
|  | 903 | // the data location. | 
|  | 904 | extractScalarValues(Store, VectorMap, ScalarMaps); | 
|  | 905 |  | 
| Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 906 | if (Access.isStrideOne(isl_map_copy(Schedule))) { | 
| Johannes Doerfert | 1947f86 | 2014-10-08 20:18:32 +0000 | [diff] [blame] | 907 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 908 | Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[0], | 
|  | 909 | VLTS[0], NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 910 |  | 
| Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 911 | Value *VectorPtr = | 
|  | 912 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 913 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); | 
|  | 914 |  | 
|  | 915 | if (!Aligned) | 
|  | 916 | Store->setAlignment(8); | 
|  | 917 | } else { | 
|  | 918 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { | 
| Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 919 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); | 
| Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 920 | Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[i], | 
|  | 921 | VLTS[i], NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 922 | Builder.CreateStore(Scalar, NewPointer); | 
|  | 923 | } | 
|  | 924 | } | 
|  | 925 | } | 
|  | 926 |  | 
|  | 927 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, | 
|  | 928 | ValueMapT &VectorMap) { | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 929 | for (Value *Operand : Inst->operands()) | 
|  | 930 | if (VectorMap.count(Operand)) | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 931 | return true; | 
|  | 932 | return false; | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, | 
|  | 936 | ValueMapT &VectorMap, | 
|  | 937 | VectorValueMapT &ScalarMaps) { | 
|  | 938 | bool HasVectorOperand = false; | 
|  | 939 | int VectorWidth = getVectorWidth(); | 
|  | 940 |  | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 941 | for (Value *Operand : Inst->operands()) { | 
|  | 942 | ValueMapT::iterator VecOp = VectorMap.find(Operand); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 943 |  | 
|  | 944 | if (VecOp == VectorMap.end()) | 
|  | 945 | continue; | 
|  | 946 |  | 
|  | 947 | HasVectorOperand = true; | 
|  | 948 | Value *NewVector = VecOp->second; | 
|  | 949 |  | 
|  | 950 | for (int i = 0; i < VectorWidth; ++i) { | 
|  | 951 | ValueMapT &SM = ScalarMaps[i]; | 
|  | 952 |  | 
|  | 953 | // If there is one scalar extracted, all scalar elements should have | 
|  | 954 | // already been extracted by the code here. So no need to check for the | 
| Tobias Grosser | 2219d15 | 2016-08-03 05:28:09 +0000 | [diff] [blame] | 955 | // existence of all of them. | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 956 | if (SM.count(Operand)) | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 957 | break; | 
|  | 958 |  | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 959 | SM[Operand] = | 
|  | 960 | Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 961 | } | 
|  | 962 | } | 
|  | 963 |  | 
|  | 964 | return HasVectorOperand; | 
|  | 965 | } | 
|  | 966 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 967 | void VectorBlockGenerator::copyInstScalarized( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 968 | ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 969 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 970 | bool HasVectorOperand; | 
|  | 971 | int VectorWidth = getVectorWidth(); | 
|  | 972 |  | 
|  | 973 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); | 
|  | 974 |  | 
|  | 975 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 976 | BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane], | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 977 | VLTS[VectorLane], NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 978 |  | 
|  | 979 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) | 
|  | 980 | return; | 
|  | 981 |  | 
|  | 982 | // Make the result available as vector value. | 
|  | 983 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); | 
|  | 984 | Value *Vector = UndefValue::get(VectorType); | 
|  | 985 |  | 
|  | 986 | for (int i = 0; i < VectorWidth; i++) | 
|  | 987 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], | 
|  | 988 | Builder.getInt32(i)); | 
|  | 989 |  | 
|  | 990 | VectorMap[Inst] = Vector; | 
|  | 991 | } | 
|  | 992 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 993 | int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); } | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 994 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 995 | void VectorBlockGenerator::copyInstruction( | 
| Tobias Grosser | 2f1acac | 2015-10-04 10:18:45 +0000 | [diff] [blame] | 996 | ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 997 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 998 | // Terminator instructions control the control flow. They are explicitly | 
|  | 999 | // expressed in the clast and do not need to be copied. | 
|  | 1000 | if (Inst->isTerminator()) | 
|  | 1001 | return; | 
|  | 1002 |  | 
| Johannes Doerfert | 5dced26 | 2015-12-22 19:08:49 +0000 | [diff] [blame] | 1003 | if (canSyntheziseInStmt(Stmt, Inst)) | 
| Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 1004 | return; | 
|  | 1005 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 1006 | if (auto *Load = dyn_cast<LoadInst>(Inst)) { | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1007 | generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1008 | return; | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | if (hasVectorOperands(Inst, VectorMap)) { | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 1012 | if (auto *Store = dyn_cast<StoreInst>(Inst)) { | 
| Michael Kruse | 0446d81 | 2017-03-10 16:05:24 +0000 | [diff] [blame] | 1013 | // Identified as redundant by -polly-simplify. | 
|  | 1014 | if (!Stmt.getArrayAccessOrNULLFor(Store)) | 
|  | 1015 | return; | 
|  | 1016 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1017 | copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1018 | return; | 
|  | 1019 | } | 
|  | 1020 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 1021 | if (auto *Unary = dyn_cast<UnaryInstruction>(Inst)) { | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 1022 | copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1023 | return; | 
|  | 1024 | } | 
|  | 1025 |  | 
| Tobias Grosser | 9646e3f | 2015-10-04 10:18:39 +0000 | [diff] [blame] | 1026 | if (auto *Binary = dyn_cast<BinaryOperator>(Inst)) { | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 1027 | copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1028 | return; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | // Falltrough: We generate scalar instructions, if we don't know how to | 
|  | 1032 | // generate vector code. | 
|  | 1033 | } | 
|  | 1034 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1035 | copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1036 | } | 
|  | 1037 |  | 
| Tobias Grosser | a69d4f0 | 2015-12-15 23:49:58 +0000 | [diff] [blame] | 1038 | void VectorBlockGenerator::generateScalarVectorLoads( | 
|  | 1039 | ScopStmt &Stmt, ValueMapT &VectorBlockMap) { | 
|  | 1040 | for (MemoryAccess *MA : Stmt) { | 
|  | 1041 | if (MA->isArrayKind() || MA->isWrite()) | 
|  | 1042 | continue; | 
|  | 1043 |  | 
|  | 1044 | auto *Address = getOrCreateAlloca(*MA); | 
|  | 1045 | Type *VectorPtrType = getVectorPtrTy(Address, 1); | 
|  | 1046 | Value *VectorPtr = Builder.CreateBitCast(Address, VectorPtrType, | 
|  | 1047 | Address->getName() + "_p_vec_p"); | 
|  | 1048 | auto *Val = Builder.CreateLoad(VectorPtr, Address->getName() + ".reload"); | 
|  | 1049 | Constant *SplatVector = Constant::getNullValue( | 
|  | 1050 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); | 
|  | 1051 |  | 
|  | 1052 | Value *VectorVal = Builder.CreateShuffleVector( | 
|  | 1053 | Val, Val, SplatVector, Address->getName() + "_p_splat"); | 
| Tobias Grosser | 583be06 | 2017-02-09 23:54:23 +0000 | [diff] [blame] | 1054 | VectorBlockMap[MA->getAccessValue()] = VectorVal; | 
| Tobias Grosser | a69d4f0 | 2015-12-15 23:49:58 +0000 | [diff] [blame] | 1055 | } | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | void VectorBlockGenerator::verifyNoScalarStores(ScopStmt &Stmt) { | 
|  | 1059 | for (MemoryAccess *MA : Stmt) { | 
|  | 1060 | if (MA->isArrayKind() || MA->isRead()) | 
|  | 1061 | continue; | 
|  | 1062 |  | 
|  | 1063 | llvm_unreachable("Scalar stores not expected in vector loop"); | 
|  | 1064 | } | 
|  | 1065 | } | 
|  | 1066 |  | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1067 | void VectorBlockGenerator::copyStmt( | 
|  | 1068 | ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | 21a059a | 2017-01-16 14:08:10 +0000 | [diff] [blame] | 1069 | assert(Stmt.isBlockStmt() && | 
|  | 1070 | "TODO: Only block statements can be copied by the vector block " | 
|  | 1071 | "generator"); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1072 |  | 
| Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 1073 | BasicBlock *BB = Stmt.getBasicBlock(); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1074 | BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), | 
|  | 1075 | &*Builder.GetInsertPoint(), &DT, &LI); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1076 | CopyBB->setName("polly.stmt." + BB->getName()); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1077 | Builder.SetInsertPoint(&CopyBB->front()); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1078 |  | 
|  | 1079 | // Create two maps that store the mapping from the original instructions of | 
|  | 1080 | // the old basic block to their copies in the new basic block. Those maps | 
|  | 1081 | // are basic block local. | 
|  | 1082 | // | 
|  | 1083 | // As vector code generation is supported there is one map for scalar values | 
|  | 1084 | // and one for vector values. | 
|  | 1085 | // | 
|  | 1086 | // In case we just do scalar code generation, the vectorMap is not used and | 
|  | 1087 | // the scalarMap has just one dimension, which contains the mapping. | 
|  | 1088 | // | 
|  | 1089 | // In case vector code generation is done, an instruction may either appear | 
|  | 1090 | // in the vector map once (as it is calculating >vectorwidth< values at a | 
|  | 1091 | // time. Or (if the values are calculated using scalar operations), it | 
|  | 1092 | // appears once in every dimension of the scalarMap. | 
|  | 1093 | VectorValueMapT ScalarBlockMap(getVectorWidth()); | 
|  | 1094 | ValueMapT VectorBlockMap; | 
|  | 1095 |  | 
| Tobias Grosser | a69d4f0 | 2015-12-15 23:49:58 +0000 | [diff] [blame] | 1096 | generateScalarVectorLoads(Stmt, VectorBlockMap); | 
|  | 1097 |  | 
| Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 1098 | for (Instruction &Inst : *BB) | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1099 | copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses); | 
| Tobias Grosser | a69d4f0 | 2015-12-15 23:49:58 +0000 | [diff] [blame] | 1100 |  | 
|  | 1101 | verifyNoScalarStores(Stmt); | 
| Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1102 | } | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1103 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1104 | BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB, | 
|  | 1105 | BasicBlock *BBCopy) { | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1106 |  | 
|  | 1107 | BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); | 
|  | 1108 | BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); | 
|  | 1109 |  | 
|  | 1110 | if (BBCopyIDom) | 
|  | 1111 | DT.changeImmediateDominator(BBCopy, BBCopyIDom); | 
|  | 1112 |  | 
|  | 1113 | return BBCopyIDom; | 
|  | 1114 | } | 
|  | 1115 |  | 
| Michael Kruse | f33c125 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 1116 | // This is to determine whether an llvm::Value (defined in @p BB) is usable when | 
|  | 1117 | // leaving a subregion. The straight-forward DT.dominates(BB, R->getExitBlock()) | 
|  | 1118 | // does not work in cases where the exit block has edges from outside the | 
|  | 1119 | // region. In that case the llvm::Value would never be usable in in the exit | 
|  | 1120 | // block. The RegionGenerator however creates an new exit block ('ExitBBCopy') | 
|  | 1121 | // for the subregion's exiting edges only. We need to determine whether an | 
|  | 1122 | // llvm::Value is usable in there. We do this by checking whether it dominates | 
|  | 1123 | // all exiting blocks individually. | 
|  | 1124 | static bool isDominatingSubregionExit(const DominatorTree &DT, Region *R, | 
|  | 1125 | BasicBlock *BB) { | 
|  | 1126 | for (auto ExitingBB : predecessors(R->getExit())) { | 
|  | 1127 | // Check for non-subregion incoming edges. | 
|  | 1128 | if (!R->contains(ExitingBB)) | 
|  | 1129 | continue; | 
|  | 1130 |  | 
|  | 1131 | if (!DT.dominates(BB, ExitingBB)) | 
|  | 1132 | return false; | 
|  | 1133 | } | 
|  | 1134 |  | 
|  | 1135 | return true; | 
|  | 1136 | } | 
|  | 1137 |  | 
|  | 1138 | // Find the direct dominator of the subregion's exit block if the subregion was | 
|  | 1139 | // simplified. | 
|  | 1140 | static BasicBlock *findExitDominator(DominatorTree &DT, Region *R) { | 
|  | 1141 | BasicBlock *Common = nullptr; | 
|  | 1142 | for (auto ExitingBB : predecessors(R->getExit())) { | 
|  | 1143 | // Check for non-subregion incoming edges. | 
|  | 1144 | if (!R->contains(ExitingBB)) | 
|  | 1145 | continue; | 
|  | 1146 |  | 
|  | 1147 | // First exiting edge. | 
|  | 1148 | if (!Common) { | 
|  | 1149 | Common = ExitingBB; | 
|  | 1150 | continue; | 
|  | 1151 | } | 
|  | 1152 |  | 
|  | 1153 | Common = DT.findNearestCommonDominator(Common, ExitingBB); | 
|  | 1154 | } | 
|  | 1155 |  | 
|  | 1156 | assert(Common && R->contains(Common)); | 
|  | 1157 | return Common; | 
|  | 1158 | } | 
|  | 1159 |  | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1160 | void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, | 
| Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 1161 | isl_id_to_ast_expr *IdToAstExp) { | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1162 | assert(Stmt.isRegionStmt() && | 
| Tobias Grosser | d3f2183 | 2015-08-01 06:26:51 +0000 | [diff] [blame] | 1163 | "Only region statements can be copied by the region generator"); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1164 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1165 | // Forget all old mappings. | 
|  | 1166 | BlockMap.clear(); | 
|  | 1167 | RegionMaps.clear(); | 
|  | 1168 | IncompletePHINodeMap.clear(); | 
|  | 1169 |  | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 1170 | // Collection of all values related to this subregion. | 
|  | 1171 | ValueMapT ValueMap; | 
|  | 1172 |  | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1173 | // The region represented by the statement. | 
|  | 1174 | Region *R = Stmt.getRegion(); | 
|  | 1175 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1176 | // Create a dedicated entry for the region where we can reload all demoted | 
|  | 1177 | // inputs. | 
|  | 1178 | BasicBlock *EntryBB = R->getEntry(); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1179 | BasicBlock *EntryBBCopy = SplitBlock(Builder.GetInsertBlock(), | 
|  | 1180 | &*Builder.GetInsertPoint(), &DT, &LI); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1181 | EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry"); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1182 | Builder.SetInsertPoint(&EntryBBCopy->front()); | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1183 |  | 
| Michael Kruse | c993739 | 2015-11-09 23:33:40 +0000 | [diff] [blame] | 1184 | ValueMapT &EntryBBMap = RegionMaps[EntryBBCopy]; | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 1185 | generateScalarLoads(Stmt, LTS, EntryBBMap, IdToAstExp); | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 1186 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1187 | for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI) | 
|  | 1188 | if (!R->contains(*PI)) | 
|  | 1189 | BlockMap[*PI] = EntryBBCopy; | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1190 |  | 
|  | 1191 | // Iterate over all blocks in the region in a breadth-first search. | 
|  | 1192 | std::deque<BasicBlock *> Blocks; | 
| Mandeep Singh Grang | 5b1abfc | 2016-10-19 17:56:49 +0000 | [diff] [blame] | 1193 | SmallSetVector<BasicBlock *, 8> SeenBlocks; | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1194 | Blocks.push_back(EntryBB); | 
|  | 1195 | SeenBlocks.insert(EntryBB); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1196 |  | 
|  | 1197 | while (!Blocks.empty()) { | 
|  | 1198 | BasicBlock *BB = Blocks.front(); | 
|  | 1199 | Blocks.pop_front(); | 
|  | 1200 |  | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1201 | // First split the block and update dominance information. | 
|  | 1202 | BasicBlock *BBCopy = splitBB(BB); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1203 | BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy); | 
|  | 1204 |  | 
| Michael Kruse | c993739 | 2015-11-09 23:33:40 +0000 | [diff] [blame] | 1205 | // Get the mapping for this block and initialize it with either the scalar | 
|  | 1206 | // loads from the generated entering block (which dominates all blocks of | 
|  | 1207 | // this subregion) or the maps of the immediate dominator, if part of the | 
|  | 1208 | // subregion. The latter necessarily includes the former. | 
|  | 1209 | ValueMapT *InitBBMap; | 
|  | 1210 | if (BBCopyIDom) { | 
|  | 1211 | assert(RegionMaps.count(BBCopyIDom)); | 
|  | 1212 | InitBBMap = &RegionMaps[BBCopyIDom]; | 
|  | 1213 | } else | 
|  | 1214 | InitBBMap = &EntryBBMap; | 
|  | 1215 | auto Inserted = RegionMaps.insert(std::make_pair(BBCopy, *InitBBMap)); | 
|  | 1216 | ValueMapT &RegionMap = Inserted.first->second; | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1217 |  | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1218 | // Copy the block with the BlockGenerator. | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1219 | Builder.SetInsertPoint(&BBCopy->front()); | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1220 | copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1221 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1222 | // In order to remap PHI nodes we store also basic block mappings. | 
|  | 1223 | BlockMap[BB] = BBCopy; | 
|  | 1224 |  | 
|  | 1225 | // Add values to incomplete PHI nodes waiting for this block to be copied. | 
|  | 1226 | for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB]) | 
| Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1227 | addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1228 | IncompletePHINodeMap[BB].clear(); | 
|  | 1229 |  | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1230 | // And continue with new successors inside the region. | 
|  | 1231 | for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++) | 
| Mandeep Singh Grang | 5b1abfc | 2016-10-19 17:56:49 +0000 | [diff] [blame] | 1232 | if (R->contains(*SI) && SeenBlocks.insert(*SI)) | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1233 | Blocks.push_back(*SI); | 
| Michael Kruse | ebffcbe | 2015-11-09 22:37:29 +0000 | [diff] [blame] | 1234 |  | 
|  | 1235 | // Remember value in case it is visible after this subregion. | 
| Michael Kruse | f33c125 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 1236 | if (isDominatingSubregionExit(DT, R, BB)) | 
| Michael Kruse | ebffcbe | 2015-11-09 22:37:29 +0000 | [diff] [blame] | 1237 | ValueMap.insert(RegionMap.begin(), RegionMap.end()); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | // Now create a new dedicated region exit block and add it to the region map. | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1241 | BasicBlock *ExitBBCopy = SplitBlock(Builder.GetInsertBlock(), | 
|  | 1242 | &*Builder.GetInsertPoint(), &DT, &LI); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1243 | ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit"); | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1244 | BlockMap[R->getExit()] = ExitBBCopy; | 
|  | 1245 |  | 
| Michael Kruse | f33c125 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 1246 | BasicBlock *ExitDomBBCopy = BlockMap.lookup(findExitDominator(DT, R)); | 
| Tobias Grosser | 21a059a | 2017-01-16 14:08:10 +0000 | [diff] [blame] | 1247 | assert(ExitDomBBCopy && | 
|  | 1248 | "Common exit dominator must be within region; at least the entry node " | 
|  | 1249 | "must match"); | 
| Michael Kruse | f33c125 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 1250 | DT.changeImmediateDominator(ExitBBCopy, ExitDomBBCopy); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1251 |  | 
|  | 1252 | // As the block generator doesn't handle control flow we need to add the | 
|  | 1253 | // region control flow by hand after all blocks have been copied. | 
|  | 1254 | for (BasicBlock *BB : SeenBlocks) { | 
|  | 1255 |  | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1256 | BasicBlock *BBCopy = BlockMap[BB]; | 
| Johannes Doerfert | e114dc0 | 2015-09-14 11:15:58 +0000 | [diff] [blame] | 1257 | TerminatorInst *TI = BB->getTerminator(); | 
|  | 1258 | if (isa<UnreachableInst>(TI)) { | 
|  | 1259 | while (!BBCopy->empty()) | 
|  | 1260 | BBCopy->begin()->eraseFromParent(); | 
|  | 1261 | new UnreachableInst(BBCopy->getContext(), BBCopy); | 
|  | 1262 | continue; | 
|  | 1263 | } | 
|  | 1264 |  | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1265 | Instruction *BICopy = BBCopy->getTerminator(); | 
|  | 1266 |  | 
| Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1267 | ValueMapT &RegionMap = RegionMaps[BBCopy]; | 
|  | 1268 | RegionMap.insert(BlockMap.begin(), BlockMap.end()); | 
|  | 1269 |  | 
| Tobias Grosser | 45e7944 | 2015-08-01 09:07:57 +0000 | [diff] [blame] | 1270 | Builder.SetInsertPoint(BICopy); | 
| Johannes Doerfert | 9a132f3 | 2015-09-28 09:33:22 +0000 | [diff] [blame] | 1271 | copyInstScalar(Stmt, TI, RegionMap, LTS); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1272 | BICopy->eraseFromParent(); | 
|  | 1273 | } | 
|  | 1274 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1275 | // Add counting PHI nodes to all loops in the region that can be used as | 
|  | 1276 | // replacement for SCEVs refering to the old loop. | 
|  | 1277 | for (BasicBlock *BB : SeenBlocks) { | 
|  | 1278 | Loop *L = LI.getLoopFor(BB); | 
| Tobias Grosser | bc29e0b | 2015-11-12 07:34:09 +0000 | [diff] [blame] | 1279 | if (L == nullptr || L->getHeader() != BB || !R->contains(L)) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1280 | continue; | 
|  | 1281 |  | 
|  | 1282 | BasicBlock *BBCopy = BlockMap[BB]; | 
|  | 1283 | Value *NullVal = Builder.getInt32(0); | 
|  | 1284 | PHINode *LoopPHI = | 
|  | 1285 | PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv"); | 
|  | 1286 | Instruction *LoopPHIInc = BinaryOperator::CreateAdd( | 
|  | 1287 | LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc"); | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1288 | LoopPHI->insertBefore(&BBCopy->front()); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1289 | LoopPHIInc->insertBefore(BBCopy->getTerminator()); | 
|  | 1290 |  | 
|  | 1291 | for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) { | 
|  | 1292 | if (!R->contains(PredBB)) | 
|  | 1293 | continue; | 
|  | 1294 | if (L->contains(PredBB)) | 
|  | 1295 | LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]); | 
|  | 1296 | else | 
|  | 1297 | LoopPHI->addIncoming(NullVal, BlockMap[PredBB]); | 
|  | 1298 | } | 
|  | 1299 |  | 
|  | 1300 | for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy))) | 
|  | 1301 | if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0) | 
|  | 1302 | LoopPHI->addIncoming(NullVal, PredBBCopy); | 
|  | 1303 |  | 
|  | 1304 | LTS[L] = SE.getUnknown(LoopPHI); | 
|  | 1305 | } | 
|  | 1306 |  | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 1307 | // Continue generating code in the exit block. | 
| Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 1308 | Builder.SetInsertPoint(&*ExitBBCopy->getFirstInsertionPt()); | 
| Michael Kruse | 225f0d1 | 2015-10-17 21:36:00 +0000 | [diff] [blame] | 1309 |  | 
|  | 1310 | // Write values visible to other statements. | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 1311 | generateScalarStores(Stmt, LTS, ValueMap, IdToAstExp); | 
| Tobias Grosser | 3e95602 | 2015-10-26 20:41:53 +0000 | [diff] [blame] | 1312 | BlockMap.clear(); | 
|  | 1313 | RegionMaps.clear(); | 
|  | 1314 | IncompletePHINodeMap.clear(); | 
| Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1315 | } | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1316 |  | 
| Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1317 | PHINode *RegionGenerator::buildExitPHI(MemoryAccess *MA, LoopToScevMapT <S, | 
|  | 1318 | ValueMapT &BBMap, Loop *L) { | 
|  | 1319 | ScopStmt *Stmt = MA->getStatement(); | 
|  | 1320 | Region *SubR = Stmt->getRegion(); | 
|  | 1321 | auto Incoming = MA->getIncoming(); | 
|  | 1322 |  | 
|  | 1323 | PollyIRBuilder::InsertPointGuard IPGuard(Builder); | 
|  | 1324 | PHINode *OrigPHI = cast<PHINode>(MA->getAccessInstruction()); | 
|  | 1325 | BasicBlock *NewSubregionExit = Builder.GetInsertBlock(); | 
|  | 1326 |  | 
|  | 1327 | // This can happen if the subregion is simplified after the ScopStmts | 
|  | 1328 | // have been created; simplification happens as part of CodeGeneration. | 
|  | 1329 | if (OrigPHI->getParent() != SubR->getExit()) { | 
|  | 1330 | BasicBlock *FormerExit = SubR->getExitingBlock(); | 
|  | 1331 | if (FormerExit) | 
|  | 1332 | NewSubregionExit = BlockMap.lookup(FormerExit); | 
|  | 1333 | } | 
|  | 1334 |  | 
|  | 1335 | PHINode *NewPHI = PHINode::Create(OrigPHI->getType(), Incoming.size(), | 
|  | 1336 | "polly." + OrigPHI->getName(), | 
|  | 1337 | NewSubregionExit->getFirstNonPHI()); | 
|  | 1338 |  | 
|  | 1339 | // Add the incoming values to the PHI. | 
|  | 1340 | for (auto &Pair : Incoming) { | 
|  | 1341 | BasicBlock *OrigIncomingBlock = Pair.first; | 
|  | 1342 | BasicBlock *NewIncomingBlock = BlockMap.lookup(OrigIncomingBlock); | 
|  | 1343 | Builder.SetInsertPoint(NewIncomingBlock->getTerminator()); | 
|  | 1344 | assert(RegionMaps.count(NewIncomingBlock)); | 
|  | 1345 | ValueMapT *LocalBBMap = &RegionMaps[NewIncomingBlock]; | 
|  | 1346 |  | 
|  | 1347 | Value *OrigIncomingValue = Pair.second; | 
|  | 1348 | Value *NewIncomingValue = | 
|  | 1349 | getNewValue(*Stmt, OrigIncomingValue, *LocalBBMap, LTS, L); | 
|  | 1350 | NewPHI->addIncoming(NewIncomingValue, NewIncomingBlock); | 
|  | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | return NewPHI; | 
|  | 1354 | } | 
|  | 1355 |  | 
|  | 1356 | Value *RegionGenerator::getExitScalar(MemoryAccess *MA, LoopToScevMapT <S, | 
|  | 1357 | ValueMapT &BBMap) { | 
|  | 1358 | ScopStmt *Stmt = MA->getStatement(); | 
|  | 1359 |  | 
|  | 1360 | // TODO: Add some test cases that ensure this is really the right choice. | 
|  | 1361 | Loop *L = LI.getLoopFor(Stmt->getRegion()->getExit()); | 
|  | 1362 |  | 
|  | 1363 | if (MA->isAnyPHIKind()) { | 
|  | 1364 | auto Incoming = MA->getIncoming(); | 
|  | 1365 | assert(!Incoming.empty() && | 
|  | 1366 | "PHI WRITEs must have originate from at least one incoming block"); | 
|  | 1367 |  | 
|  | 1368 | // If there is only one incoming value, we do not need to create a PHI. | 
|  | 1369 | if (Incoming.size() == 1) { | 
|  | 1370 | Value *OldVal = Incoming[0].second; | 
|  | 1371 | return getNewValue(*Stmt, OldVal, BBMap, LTS, L); | 
|  | 1372 | } | 
|  | 1373 |  | 
|  | 1374 | return buildExitPHI(MA, LTS, BBMap, L); | 
|  | 1375 | } | 
|  | 1376 |  | 
| Tobias Grosser | 4d5a917 | 2017-01-14 20:25:44 +0000 | [diff] [blame] | 1377 | // MemoryKind::Value accesses leaving the subregion must dominate the exit | 
|  | 1378 | // block; just pass the copied value. | 
| Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1379 | Value *OldVal = MA->getAccessValue(); | 
|  | 1380 | return getNewValue(*Stmt, OldVal, BBMap, LTS, L); | 
|  | 1381 | } | 
|  | 1382 |  | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 1383 | void RegionGenerator::generateScalarStores( | 
|  | 1384 | ScopStmt &Stmt, LoopToScevMapT <S, ValueMapT &BBMap, | 
|  | 1385 | __isl_keep isl_id_to_ast_expr *NewAccesses) { | 
| Tobias Grosser | 7529690 | 2015-08-21 19:23:21 +0000 | [diff] [blame] | 1386 | assert(Stmt.getRegion() && | 
|  | 1387 | "Block statements need to use the generateScalarStores() " | 
|  | 1388 | "function in the BlockGenerator"); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1389 |  | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1390 | for (MemoryAccess *MA : Stmt) { | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 1391 | if (MA->isOriginalArrayKind() || MA->isRead()) | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1392 | continue; | 
|  | 1393 |  | 
| Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1394 | Value *NewVal = getExitScalar(MA, LTS, BBMap); | 
| Michael Kruse | 2fa3519 | 2016-09-01 19:53:31 +0000 | [diff] [blame] | 1395 | Value *Address = | 
|  | 1396 | getImplicitAddress(*MA, getLoopForStmt(Stmt), LTS, BBMap, NewAccesses); | 
| Michael Kruse | eac9726 | 2016-02-24 22:08:14 +0000 | [diff] [blame] | 1397 | assert((!isa<Instruction>(NewVal) || | 
|  | 1398 | DT.dominates(cast<Instruction>(NewVal)->getParent(), | 
|  | 1399 | Builder.GetInsertBlock())) && | 
|  | 1400 | "Domination violation"); | 
|  | 1401 | assert((!isa<Instruction>(Address) || | 
|  | 1402 | DT.dominates(cast<Instruction>(Address)->getParent(), | 
|  | 1403 | Builder.GetInsertBlock())) && | 
|  | 1404 | "Domination violation"); | 
| Michael Kruse | ee6a4fc | 2016-01-26 13:33:27 +0000 | [diff] [blame] | 1405 | Builder.CreateStore(NewVal, Address); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1406 | } | 
|  | 1407 | } | 
|  | 1408 |  | 
| Tobias Grosser | 943c369 | 2017-01-19 13:25:52 +0000 | [diff] [blame] | 1409 | void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, PHINode *PHI, | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1410 | PHINode *PHICopy, BasicBlock *IncomingBB, | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1411 | LoopToScevMapT <S) { | 
|  | 1412 | Region *StmtR = Stmt.getRegion(); | 
|  | 1413 |  | 
|  | 1414 | // If the incoming block was not yet copied mark this PHI as incomplete. | 
|  | 1415 | // Once the block will be copied the incoming value will be added. | 
|  | 1416 | BasicBlock *BBCopy = BlockMap[IncomingBB]; | 
|  | 1417 | if (!BBCopy) { | 
|  | 1418 | assert(StmtR->contains(IncomingBB) && | 
|  | 1419 | "Bad incoming block for PHI in non-affine region"); | 
|  | 1420 | IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy)); | 
|  | 1421 | return; | 
|  | 1422 | } | 
|  | 1423 |  | 
| Tobias Grosser | 75dfaa1 | 2017-01-19 14:12:45 +0000 | [diff] [blame] | 1424 | assert(RegionMaps.count(BBCopy) && "Incoming PHI block did not have a BBMap"); | 
|  | 1425 | ValueMapT &BBCopyMap = RegionMaps[BBCopy]; | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1426 |  | 
| Tobias Grosser | 75dfaa1 | 2017-01-19 14:12:45 +0000 | [diff] [blame] | 1427 | Value *OpCopy = nullptr; | 
|  | 1428 |  | 
|  | 1429 | if (StmtR->contains(IncomingBB)) { | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1430 | Value *Op = PHI->getIncomingValueForBlock(IncomingBB); | 
| Michael Kruse | dc12222 | 2015-10-19 09:19:25 +0000 | [diff] [blame] | 1431 |  | 
| Johannes Doerfert | 6ba9271 | 2016-04-01 11:25:47 +0000 | [diff] [blame] | 1432 | // If the current insert block is different from the PHIs incoming block | 
|  | 1433 | // change it, otherwise do not. | 
|  | 1434 | auto IP = Builder.GetInsertPoint(); | 
|  | 1435 | if (IP->getParent() != BBCopy) | 
|  | 1436 | Builder.SetInsertPoint(BBCopy->getTerminator()); | 
| Johannes Doerfert | 2c3ffc0 | 2016-02-16 12:36:14 +0000 | [diff] [blame] | 1437 | OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForStmt(Stmt)); | 
| Johannes Doerfert | 6ba9271 | 2016-04-01 11:25:47 +0000 | [diff] [blame] | 1438 | if (IP->getParent() != BBCopy) | 
|  | 1439 | Builder.SetInsertPoint(&*IP); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1440 | } else { | 
| Tobias Grosser | 75dfaa1 | 2017-01-19 14:12:45 +0000 | [diff] [blame] | 1441 | // All edges from outside the non-affine region become a single edge | 
|  | 1442 | // in the new copy of the non-affine region. Make sure to only add the | 
|  | 1443 | // corresponding edge the first time we encounter a basic block from | 
|  | 1444 | // outside the non-affine region. | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1445 | if (PHICopy->getBasicBlockIndex(BBCopy) >= 0) | 
|  | 1446 | return; | 
|  | 1447 |  | 
| Tobias Grosser | 75dfaa1 | 2017-01-19 14:12:45 +0000 | [diff] [blame] | 1448 | // Get the reloaded value. | 
|  | 1449 | OpCopy = getNewValue(Stmt, PHI, BBCopyMap, LTS, getLoopForStmt(Stmt)); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | assert(OpCopy && "Incoming PHI value was not copied properly"); | 
|  | 1453 | assert(BBCopy && "Incoming PHI block was not copied properly"); | 
|  | 1454 | PHICopy->addIncoming(OpCopy, BBCopy); | 
|  | 1455 | } | 
|  | 1456 |  | 
| Tobias Grosser | 2b809d1 | 2016-02-21 15:44:34 +0000 | [diff] [blame] | 1457 | void RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, PHINode *PHI, | 
|  | 1458 | ValueMapT &BBMap, | 
|  | 1459 | LoopToScevMapT <S) { | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1460 | unsigned NumIncoming = PHI->getNumIncomingValues(); | 
|  | 1461 | PHINode *PHICopy = | 
|  | 1462 | Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName()); | 
|  | 1463 | PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI()); | 
|  | 1464 | BBMap[PHI] = PHICopy; | 
|  | 1465 |  | 
| Tobias Grosser | 97b8490 | 2017-01-19 05:09:23 +0000 | [diff] [blame] | 1466 | for (BasicBlock *IncomingBB : PHI->blocks()) | 
|  | 1467 | addOperandToPHI(Stmt, PHI, PHICopy, IncomingBB, LTS); | 
| Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1468 | } |