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