blob: b87c53a8b78f4ec326e565cedf192960f675db5b [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- 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
16#include "polly/ScopInfo.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000017#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000018#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000019#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000020#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000021#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000022#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000023#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000024#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000025#include "llvm/Analysis/RegionInfo.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000026#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000027#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000028#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000029#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000030#include "isl/aff.h"
31#include "isl/ast.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000032#include "isl/ast_build.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000033#include "isl/set.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000034#include <deque>
35
Hongbin Zheng3b11a162012-04-25 13:16:49 +000036using namespace llvm;
37using namespace polly;
38
Tobias Grosser878aba42014-10-22 23:22:41 +000039static cl::opt<bool> Aligned("enable-polly-aligned",
40 cl::desc("Assumed aligned memory accesses."),
41 cl::Hidden, cl::init(false), cl::ZeroOrMore,
42 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000043
Tobias Grosserdcc3b432015-08-04 13:54:20 +000044bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI,
Tobias Grosserecfe21b2013-03-20 18:03:18 +000045 ScalarEvolution *SE, const Region *R) {
Tobias Grosserdcc3b432015-08-04 13:54:20 +000046 if (!V || !SE->isSCEVable(V->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000047 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048
Tobias Grosserdcc3b432015-08-04 13:54:20 +000049 if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V)))
Tobias Grosser683b8e42014-11-30 14:33:31 +000050 if (!isa<SCEVCouldNotCompute>(Scev))
51 if (!hasScalarDepsInsideRegion(Scev, R))
52 return true;
53
54 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000055}
56
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000057bool polly::isIgnoredIntrinsic(const Value *V) {
58 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
59 switch (IT->getIntrinsicID()) {
60 // Lifetime markers are supported/ignored.
61 case llvm::Intrinsic::lifetime_start:
62 case llvm::Intrinsic::lifetime_end:
63 // Invariant markers are supported/ignored.
64 case llvm::Intrinsic::invariant_start:
65 case llvm::Intrinsic::invariant_end:
66 // Some misc annotations are supported/ignored.
67 case llvm::Intrinsic::var_annotation:
68 case llvm::Intrinsic::ptr_annotation:
69 case llvm::Intrinsic::annotation:
70 case llvm::Intrinsic::donothing:
71 case llvm::Intrinsic::assume:
72 case llvm::Intrinsic::expect:
Tobias Grossere83a3962015-08-30 16:57:20 +000073 // Some debug info intrisics are supported/ignored.
74 case llvm::Intrinsic::dbg_value:
75 case llvm::Intrinsic::dbg_declare:
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000076 return true;
77 default:
78 break;
79 }
80 }
81 return false;
82}
83
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000084BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
85 ScalarEvolution &SE, DominatorTree &DT,
Johannes Doerfertecff11d2015-05-22 23:43:58 +000086 ScalarAllocaMapTy &ScalarMap,
87 ScalarAllocaMapTy &PHIOpMap,
88 EscapeUsersAllocaMapTy &EscapeMap,
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000089 IslExprBuilder *ExprBuilder)
Johannes Doerfertecff11d2015-05-22 23:43:58 +000090 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
91 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
92 EscapeMap(EscapeMap) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000093
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000094Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
95 ValueMapT &BBMap, ValueMapT &GlobalMap,
96 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000097 // We assume constants never change.
98 // This avoids map lookups for many calls to this function.
99 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000100 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000101
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000102 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000103 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000104 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000105 New = Builder.CreateTruncOrBitCast(New, Old->getType());
106
107 return New;
108 }
109
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000110 if (Value *New = BBMap.lookup(Old))
111 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000112
Tobias Grosser683b8e42014-11-30 14:33:31 +0000113 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000114 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000115 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000116 const SCEV *NewScev = apply(Scev, LTS, SE);
117 ValueToValueMap VTV;
118 VTV.insert(BBMap.begin(), BBMap.end());
119 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000120 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Johannes Doerferte69e1142015-08-18 11:56:00 +0000121
122 Scop &S = *Stmt.getParent();
123 const DataLayout &DL =
124 S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
125 auto IP = Builder.GetInsertPoint();
126
127 assert(IP != Builder.GetInsertBlock()->end() &&
Tobias Grosser45e79442015-08-01 09:07:57 +0000128 "Only instructions can be insert points for SCEVExpander");
Johannes Doerferte69e1142015-08-18 11:56:00 +0000129 Value *Expanded =
130 expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), IP);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000131
132 BBMap[Old] = Expanded;
133 return Expanded;
134 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000135 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000136
Tobias Grosser16371ac2014-11-05 20:48:56 +0000137 // A scop-constant value defined by a global or a function parameter.
138 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
139 return const_cast<Value *>(Old);
140
141 // A scop-constant value defined by an instruction executed outside the scop.
142 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000143 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000144 return const_cast<Value *>(Old);
145
146 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000147 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000148 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149}
150
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000151void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
152 ValueMapT &BBMap, ValueMapT &GlobalMap,
153 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000154 // We do not generate debug intrinsics as we did not investigate how to
155 // copy them correctly. At the current state, they just crash the code
156 // generation as the meta-data operands are not correctly copied.
157 if (isa<DbgInfoIntrinsic>(Inst))
158 return;
159
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000160 Instruction *NewInst = Inst->clone();
161
162 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000163 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000164 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
165 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000166
167 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000168 assert(!isa<StoreInst>(NewInst) &&
169 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000170 delete NewInst;
171 return;
172 }
173
174 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
175 }
176
177 Builder.Insert(NewInst);
178 BBMap[Inst] = NewInst;
179
180 if (!NewInst->getType()->isVoidTy())
181 NewInst->setName("p_" + Inst->getName());
182}
183
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000184Value *BlockGenerator::generateLocationAccessed(
185 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000186 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS,
187 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000188 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000190 isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000191
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000192 if (AccessExpr) {
193 AccessExpr = isl_ast_expr_address_of(AccessExpr);
194 return ExprBuilder->create(AccessExpr);
195 }
196
197 return getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS,
198 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000199}
200
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000201Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000202 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000203}
204
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000205Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000206 ValueMapT &BBMap,
207 ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000208 LoopToScevMapT &LTS,
209 isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000211 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
212 GlobalMap, LTS, NewAccesses);
Johannes Doerfert87901452014-10-02 16:22:19 +0000213 Value *ScalarLoad = Builder.CreateAlignedLoad(
214 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000215 return ScalarLoad;
216}
217
Tobias Grosserc186ac72015-08-11 08:13:15 +0000218void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
219 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000220 LoopToScevMapT &LTS,
221 isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000223 Value *NewPointer = generateLocationAccessed(Stmt, Store, Pointer, BBMap,
224 GlobalMap, LTS, NewAccesses);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000225 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
226 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227
Tobias Grosserc186ac72015-08-11 08:13:15 +0000228 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000229}
230
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000231void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
232 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000233 LoopToScevMapT &LTS,
234 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000235
236 // First check for possible scalar dependences for this instruction.
237 generateScalarLoads(Stmt, Inst, BBMap);
238
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000239 // Terminator instructions control the control flow. They are explicitly
240 // expressed in the clast and do not need to be copied.
241 if (Inst->isTerminator())
242 return;
243
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000244 Loop *L = getLoopForInst(Inst);
245 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
246 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
247 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
248 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000249 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000250 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000251
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000252 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000253 Value *NewLoad =
254 generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS, NewAccesses);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000255 // Compute NewLoad before its insertion in BBMap to make the insertion
256 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000257 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000258 return;
259 }
260
261 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000262 generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000263 return;
264 }
265
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000266 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
267 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
268 return;
269 }
270
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000271 // Skip some special intrinsics for which we do not adjust the semantics to
272 // the new schedule. All others are handled like every other instruction.
Tobias Grosser9c0ffe32015-08-30 16:57:15 +0000273 if (isIgnoredIntrinsic(Inst))
274 return;
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000275
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000276 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000277}
278
Johannes Doerfert275a1752015-02-24 16:16:32 +0000279void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000280 LoopToScevMapT &LTS,
281 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000282 assert(Stmt.isBlockStmt() &&
283 "Only block statements can be copied by the block generator");
284
285 ValueMapT BBMap;
286
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000287 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000288 copyBB(Stmt, BB, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000289}
290
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000291BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000292 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000293 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000294 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000295 return CopyBB;
296}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000297
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000298BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
299 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000300 LoopToScevMapT &LTS,
301 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000302 BasicBlock *CopyBB = splitBB(BB);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000303 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000304 return CopyBB;
305}
306
307void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
308 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000309 LoopToScevMapT &LTS,
310 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000311 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000312 EntryBB = &CopyBB->getParent()->getEntryBlock();
313
Tobias Grosser91f5b262014-06-04 08:06:40 +0000314 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000315 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000316
317 // After a basic block was copied store all scalars that escape this block
318 // in their alloca. First the scalars that have dependences inside the SCoP,
319 // then the ones that might escape the SCoP.
320 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
321
322 const Region &R = Stmt.getParent()->getRegion();
323 for (Instruction &Inst : *BB)
324 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
325}
326
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000327AllocaInst *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000328 ScalarAllocaMapTy &Map,
Tobias Grosser29854002015-08-30 15:03:59 +0000329 const char *NameExt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000330 // Check if an alloca was cached for the base instruction.
331 AllocaInst *&Addr = Map[ScalarBase];
332
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000333 // If no alloca was found create one and insert it in the entry block.
334 if (!Addr) {
335 auto *Ty = ScalarBase->getType();
336 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
337 Addr->insertBefore(EntryBB->getFirstInsertionPt());
338 }
339
340 return Addr;
341}
342
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000343AllocaInst *BlockGenerator::getOrCreateAlloca(MemoryAccess &Access) {
344 if (Access.getScopArrayInfo()->isPHI())
345 return getOrCreatePHIAlloca(Access.getBaseAddr());
346 else
347 return getOrCreateScalarAlloca(Access.getBaseAddr());
348}
349
Tobias Grosserb79a67d2015-08-28 08:23:35 +0000350AllocaInst *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) {
351 return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
352}
353
354AllocaInst *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) {
355 return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
356}
357
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000358void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
359 Value *InstCopy) {
Tobias Grosser29854002015-08-30 15:03:59 +0000360 // If there are escape users we get the alloca for this instruction and put it
361 // in the EscapeMap for later finalization. Lastly, if the instruction was
362 // copied multiple times we already did this and can exit.
Michael Kruseacb6ade2015-08-18 17:25:48 +0000363 if (EscapeMap.count(Inst))
364 return;
Johannes Doerferte69e1142015-08-18 11:56:00 +0000365
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000366 EscapeUserVectorTy EscapeUsers;
367 for (User *U : Inst->users()) {
368
369 // Non-instruction user will never escape.
370 Instruction *UI = dyn_cast<Instruction>(U);
371 if (!UI)
372 continue;
373
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000374 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000375 continue;
376
377 EscapeUsers.push_back(UI);
378 }
379
380 // Exit if no escape uses were found.
381 if (EscapeUsers.empty())
382 return;
383
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000384 // Get or create an escape alloca for this instruction.
Tobias Grosser29854002015-08-30 15:03:59 +0000385 AllocaInst *ScalarAddr = getOrCreateScalarAlloca(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000386
387 // Remember that this instruction has escape uses and the escape alloca.
388 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000389}
390
391void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
392 const Instruction *Inst,
393 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000394 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000395
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000396 if (!MAL)
397 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000398
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000399 for (MemoryAccess &MA : *MAL) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000400 if (!MA.isScalar() || !MA.isRead())
401 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000402
Tobias Grosserfcfac082015-08-30 16:01:58 +0000403 auto *Address = getOrCreateAlloca(MA);
404 auto Name = Address->getName() + ".reload";
405 BBMap[MA.getBaseAddr()] = Builder.CreateLoad(Address, Name);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000406 }
407}
408
409Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
410 ScalarAllocaMapTy &ReloadMap,
411 ValueMapT &BBMap,
412 ValueMapT &GlobalMap) {
413 // If the value we want to store is an instruction we might have demoted it
414 // in order to make it accessible here. In such a case a reload is
415 // necessary. If it is no instruction it will always be a value that
416 // dominates the current point and we can just use it. In total there are 4
417 // options:
418 // (1) The value is no instruction ==> use the value.
419 // (2) The value is an instruction that was split out of the region prior to
420 // code generation ==> use the instruction as it dominates the region.
421 // (3) The value is an instruction:
422 // (a) The value was defined in the current block, thus a copy is in
423 // the BBMap ==> use the mapped value.
424 // (b) The value was defined in a previous block, thus we demoted it
425 // earlier ==> use the reloaded value.
426 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
427 if (!ScalarValueInst)
428 return ScalarValue;
429
430 if (!R.contains(ScalarValueInst)) {
431 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
432 return /* Case (3a) */ ScalarValueCopy;
433 else
434 return /* Case 2 */ ScalarValue;
435 }
436
437 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
438 return /* Case (3a) */ ScalarValueCopy;
439
440 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000441 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000442 ScalarValue =
443 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
444
445 return ScalarValue;
446}
447
448void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
449 ValueMapT &BBMap,
450 ValueMapT &GlobalMap) {
451 const Region &R = Stmt.getParent()->getRegion();
452
453 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
454 "Region statements need to use the generateScalarStores() "
455 "function in the RegionGenerator");
456
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000457 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000458 if (!MA->isScalar() || MA->isRead())
459 continue;
460
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000461 Value *Val = MA->getAccessValue();
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000462 auto *Address = getOrCreateAlloca(*MA);
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000463
Tobias Grosser92245222015-07-28 14:53:44 +0000464 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
465 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000466 }
467}
468
469void BlockGenerator::createScalarInitialization(Region &R,
470 ValueMapT &GlobalMap) {
471 // The split block __just before__ the region and optimized region.
472 BasicBlock *SplitBB = R.getEnteringBlock();
473 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
474 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
475
476 // Get the start block of the __optimized__ region.
477 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
478 if (StartBB == R.getEntry())
479 StartBB = SplitBBTerm->getSuccessor(1);
480
481 // For each PHI predecessor outside the region store the incoming operand
482 // value prior to entering the optimized region.
483 Builder.SetInsertPoint(StartBB->getTerminator());
484
485 ScalarAllocaMapTy EmptyMap;
486 for (const auto &PHIOpMapping : PHIOpMap) {
487 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
488
489 // Check if this PHI has the split block as predecessor (that is the only
490 // possible predecessor outside the SCoP).
491 int idx = PHI->getBasicBlockIndex(SplitBB);
492 if (idx < 0)
493 continue;
494
495 Value *ScalarValue = PHI->getIncomingValue(idx);
496 ScalarValue =
497 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
498
499 // If the split block is the predecessor initialize the PHI operator alloca.
500 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
501 }
502}
503
504void BlockGenerator::createScalarFinalization(Region &R) {
505 // The exit block of the __unoptimized__ region.
506 BasicBlock *ExitBB = R.getExitingBlock();
507 // The merge block __just after__ the region and the optimized region.
508 BasicBlock *MergeBB = R.getExit();
509
510 // The exit block of the __optimized__ region.
511 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
512 if (OptExitBB == ExitBB)
513 OptExitBB = *(++pred_begin(MergeBB));
514
515 Builder.SetInsertPoint(OptExitBB->getTerminator());
516 for (const auto &EscapeMapping : EscapeMap) {
517 // Extract the escaping instruction and the escaping users as well as the
518 // alloca the instruction was demoted to.
519 Instruction *EscapeInst = EscapeMapping.getFirst();
520 const auto &EscapeMappingValue = EscapeMapping.getSecond();
521 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
522 AllocaInst *ScalarAddr = EscapeMappingValue.first;
523
524 // Reload the demoted instruction in the optimized version of the SCoP.
525 Instruction *EscapeInstReload =
526 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
527
528 // Create the merge PHI that merges the optimized and unoptimized version.
529 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
530 EscapeInst->getName() + ".merge");
531 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
532
533 // Add the respective values to the merge PHI.
534 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
535 MergePHI->addIncoming(EscapeInst, ExitBB);
536
537 // The information of scalar evolution about the escaping instruction needs
538 // to be revoked so the new merged instruction will be used.
539 if (SE.isSCEVable(EscapeInst->getType()))
540 SE.forgetValue(EscapeInst);
541
542 // Replace all uses of the demoted instruction with the merge PHI.
543 for (Instruction *EUser : EscapeUsers)
544 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
545 }
546}
547
548void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
549 createScalarInitialization(S.getRegion(), GlobalMap);
550 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000551}
552
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000553VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
554 VectorValueMapT &GlobalMaps,
555 std::vector<LoopToScevMapT> &VLTS,
556 isl_map *Schedule)
557 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
558 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000559 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
560 assert(Schedule && "No statement domain provided");
561}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000562
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000563Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000564 ValueMapT &VectorMap,
565 VectorValueMapT &ScalarMaps,
566 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000567 if (Value *NewValue = VectorMap.lookup(Old))
568 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000569
570 int Width = getVectorWidth();
571
572 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
573
574 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000575 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000576 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
577 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000578 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000579
580 VectorMap[Old] = Vector;
581
582 return Vector;
583}
584
585Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
586 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
587 assert(PointerTy && "PointerType expected");
588
589 Type *ScalarType = PointerTy->getElementType();
590 VectorType *VectorType = VectorType::get(ScalarType, Width);
591
592 return PointerType::getUnqual(VectorType);
593}
594
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000595Value *VectorBlockGenerator::generateStrideOneLoad(
596 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000597 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000598 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000599 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000600 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
601 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
602
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000603 Value *NewPointer = nullptr;
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000604 NewPointer =
605 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
606 GlobalMaps[Offset], VLTS[Offset], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000607 Value *VectorPtr =
608 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
609 LoadInst *VecLoad =
610 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000611 if (!Aligned)
612 VecLoad->setAlignment(8);
613
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000614 if (NegativeStride) {
615 SmallVector<Constant *, 16> Indices;
616 for (int i = VectorWidth - 1; i >= 0; i--)
617 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
618 Constant *SV = llvm::ConstantVector::get(Indices);
619 Value *RevVecLoad = Builder.CreateShuffleVector(
620 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
621 return RevVecLoad;
622 }
623
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000624 return VecLoad;
625}
626
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000627Value *VectorBlockGenerator::generateStrideZeroLoad(
628 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap,
629 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000630 const Value *Pointer = Load->getPointerOperand();
631 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000632 Value *NewPointer = generateLocationAccessed(
633 Stmt, Load, Pointer, BBMap, GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000634 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
635 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000636 LoadInst *ScalarLoad =
637 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000638
639 if (!Aligned)
640 ScalarLoad->setAlignment(8);
641
Tobias Grosserc14582f2013-02-05 18:01:29 +0000642 Constant *SplatVector = Constant::getNullValue(
643 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000644
Tobias Grosserc14582f2013-02-05 18:01:29 +0000645 Value *VectorLoad = Builder.CreateShuffleVector(
646 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000647 return VectorLoad;
648}
649
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000650Value *VectorBlockGenerator::generateUnknownStrideLoad(
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000651 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
652 __isl_keep isl_id_to_ast_expr *NewAccesses
653
654 ) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000655 int VectorWidth = getVectorWidth();
656 const Value *Pointer = Load->getPointerOperand();
657 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000658 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000659
660 Value *Vector = UndefValue::get(VectorType);
661
662 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000663 Value *NewPointer =
664 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[i],
665 GlobalMaps[i], VLTS[i], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000666 Value *ScalarLoad =
667 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
668 Vector = Builder.CreateInsertElement(
669 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000670 }
671
672 return Vector;
673}
674
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000675void VectorBlockGenerator::generateLoad(
676 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap,
677 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser28736452015-03-23 07:00:36 +0000678 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000679 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000680 ScalarMaps[i][Load] = generateScalarLoad(
681 Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000682 return;
683 }
684
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000685 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000686
Tobias Grosser95493982014-04-18 09:46:35 +0000687 // Make sure we have scalar values available to access the pointer to
688 // the data location.
689 extractScalarValues(Load, VectorMap, ScalarMaps);
690
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000691 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000692 if (Access.isStrideZero(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000693 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
Sebastian Popa00a0292012-12-18 07:46:06 +0000694 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000695 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000696 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000697 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000698 else
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000699 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000700
701 VectorMap[Load] = NewLoad;
702}
703
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000704void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
705 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000706 ValueMapT &VectorMap,
707 VectorValueMapT &ScalarMaps) {
708 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000709 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
710 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000711
712 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
713
714 const CastInst *Cast = dyn_cast<CastInst>(Inst);
715 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
716 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
717}
718
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000719void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
720 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000721 ValueMapT &VectorMap,
722 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000723 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000724 Value *OpZero = Inst->getOperand(0);
725 Value *OpOne = Inst->getOperand(1);
726
727 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000728 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
729 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000730
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000731 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000732 Inst->getName() + "p_vec");
733 VectorMap[Inst] = NewInst;
734}
735
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000736void VectorBlockGenerator::copyStore(
737 ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap,
738 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000739 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000740
741 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000742 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000743 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000744
Tobias Grosser50fd7012014-04-17 23:13:49 +0000745 // Make sure we have scalar values available to access the pointer to
746 // the data location.
747 extractScalarValues(Store, VectorMap, ScalarMaps);
748
Sebastian Popa00a0292012-12-18 07:46:06 +0000749 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000750 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000751 Value *NewPointer =
752 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[0],
753 GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000754
Tobias Grosserc14582f2013-02-05 18:01:29 +0000755 Value *VectorPtr =
756 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000757 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
758
759 if (!Aligned)
760 Store->setAlignment(8);
761 } else {
762 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000763 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000764 Value *NewPointer =
765 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[i],
766 GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000767 Builder.CreateStore(Scalar, NewPointer);
768 }
769 }
770}
771
772bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
773 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000774 for (Value *Operand : Inst->operands())
775 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000776 return true;
777 return false;
778}
779
780bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
781 ValueMapT &VectorMap,
782 VectorValueMapT &ScalarMaps) {
783 bool HasVectorOperand = false;
784 int VectorWidth = getVectorWidth();
785
Tobias Grosser91f5b262014-06-04 08:06:40 +0000786 for (Value *Operand : Inst->operands()) {
787 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000788
789 if (VecOp == VectorMap.end())
790 continue;
791
792 HasVectorOperand = true;
793 Value *NewVector = VecOp->second;
794
795 for (int i = 0; i < VectorWidth; ++i) {
796 ValueMapT &SM = ScalarMaps[i];
797
798 // If there is one scalar extracted, all scalar elements should have
799 // already been extracted by the code here. So no need to check for the
800 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000801 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000802 break;
803
Tobias Grosser91f5b262014-06-04 08:06:40 +0000804 SM[Operand] =
805 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000806 }
807 }
808
809 return HasVectorOperand;
810}
811
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000812void VectorBlockGenerator::copyInstScalarized(
813 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
814 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000815 bool HasVectorOperand;
816 int VectorWidth = getVectorWidth();
817
818 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
819
820 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000821 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000822 GlobalMaps[VectorLane], VLTS[VectorLane],
823 NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000824
825 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
826 return;
827
828 // Make the result available as vector value.
829 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
830 Value *Vector = UndefValue::get(VectorType);
831
832 for (int i = 0; i < VectorWidth; i++)
833 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
834 Builder.getInt32(i));
835
836 VectorMap[Inst] = Vector;
837}
838
Tobias Grosserc14582f2013-02-05 18:01:29 +0000839int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000840
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000841void VectorBlockGenerator::copyInstruction(
842 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
843 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000844 // Terminator instructions control the control flow. They are explicitly
845 // expressed in the clast and do not need to be copied.
846 if (Inst->isTerminator())
847 return;
848
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000849 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000850 return;
851
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000852 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000853 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000854 return;
855 }
856
857 if (hasVectorOperands(Inst, VectorMap)) {
858 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000859 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000860 return;
861 }
862
863 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000864 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000865 return;
866 }
867
868 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000869 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000870 return;
871 }
872
873 // Falltrough: We generate scalar instructions, if we don't know how to
874 // generate vector code.
875 }
876
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000877 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000878}
879
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000880void VectorBlockGenerator::copyStmt(
881 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000882 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
883 "the vector block generator");
884
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000885 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000886 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000887 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000888 CopyBB->setName("polly.stmt." + BB->getName());
889 Builder.SetInsertPoint(CopyBB->begin());
890
891 // Create two maps that store the mapping from the original instructions of
892 // the old basic block to their copies in the new basic block. Those maps
893 // are basic block local.
894 //
895 // As vector code generation is supported there is one map for scalar values
896 // and one for vector values.
897 //
898 // In case we just do scalar code generation, the vectorMap is not used and
899 // the scalarMap has just one dimension, which contains the mapping.
900 //
901 // In case vector code generation is done, an instruction may either appear
902 // in the vector map once (as it is calculating >vectorwidth< values at a
903 // time. Or (if the values are calculated using scalar operations), it
904 // appears once in every dimension of the scalarMap.
905 VectorValueMapT ScalarBlockMap(getVectorWidth());
906 ValueMapT VectorBlockMap;
907
Tobias Grosser91f5b262014-06-04 08:06:40 +0000908 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000909 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000910}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000911
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000912BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
913 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000914
915 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
916 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
917
918 if (BBCopyIDom)
919 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
920
921 return BBCopyIDom;
922}
923
Johannes Doerfert275a1752015-02-24 16:16:32 +0000924void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000925 LoopToScevMapT &LTS,
926 isl_id_to_ast_expr *IdToAstExp) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000927 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000928 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000929
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000930 // Forget all old mappings.
931 BlockMap.clear();
932 RegionMaps.clear();
933 IncompletePHINodeMap.clear();
934
Johannes Doerfert275a1752015-02-24 16:16:32 +0000935 // The region represented by the statement.
936 Region *R = Stmt.getRegion();
937
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000938 // Create a dedicated entry for the region where we can reload all demoted
939 // inputs.
940 BasicBlock *EntryBB = R->getEntry();
941 BasicBlock *EntryBBCopy =
942 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
943 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
944 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000945
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000946 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
947 if (!R->contains(*PI))
948 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000949
950 // Iterate over all blocks in the region in a breadth-first search.
951 std::deque<BasicBlock *> Blocks;
952 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000953 Blocks.push_back(EntryBB);
954 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000955
956 while (!Blocks.empty()) {
957 BasicBlock *BB = Blocks.front();
958 Blocks.pop_front();
959
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000960 // First split the block and update dominance information.
961 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000962 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
963
964 // In order to remap PHI nodes we store also basic block mappings.
965 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000966
967 // Get the mapping for this block and initialize it with the mapping
968 // available at its immediate dominator (in the new region).
969 ValueMapT &RegionMap = RegionMaps[BBCopy];
970 RegionMap = RegionMaps[BBCopyIDom];
971
Johannes Doerfert275a1752015-02-24 16:16:32 +0000972 // Copy the block with the BlockGenerator.
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000973 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS, IdToAstExp);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000974
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000975 // In order to remap PHI nodes we store also basic block mappings.
976 BlockMap[BB] = BBCopy;
977
978 // Add values to incomplete PHI nodes waiting for this block to be copied.
979 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
980 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
981 GlobalMap, LTS);
982 IncompletePHINodeMap[BB].clear();
983
Johannes Doerfert275a1752015-02-24 16:16:32 +0000984 // And continue with new successors inside the region.
985 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
986 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
987 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000988 }
989
990 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000991 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000992 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000993 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000994 BlockMap[R->getExit()] = ExitBBCopy;
995
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000996 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000997
998 // As the block generator doesn't handle control flow we need to add the
999 // region control flow by hand after all blocks have been copied.
1000 for (BasicBlock *BB : SeenBlocks) {
1001
1002 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1003
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001004 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001005 Instruction *BICopy = BBCopy->getTerminator();
1006
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001007 ValueMapT &RegionMap = RegionMaps[BBCopy];
1008 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1009
Tobias Grosser45e79442015-08-01 09:07:57 +00001010 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001011 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1012 BICopy->eraseFromParent();
1013 }
1014
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001015 // Add counting PHI nodes to all loops in the region that can be used as
1016 // replacement for SCEVs refering to the old loop.
1017 for (BasicBlock *BB : SeenBlocks) {
1018 Loop *L = LI.getLoopFor(BB);
1019 if (L == nullptr || L->getHeader() != BB)
1020 continue;
1021
1022 BasicBlock *BBCopy = BlockMap[BB];
1023 Value *NullVal = Builder.getInt32(0);
1024 PHINode *LoopPHI =
1025 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1026 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1027 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1028 LoopPHI->insertBefore(BBCopy->begin());
1029 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1030
1031 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1032 if (!R->contains(PredBB))
1033 continue;
1034 if (L->contains(PredBB))
1035 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1036 else
1037 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1038 }
1039
1040 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1041 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1042 LoopPHI->addIncoming(NullVal, PredBBCopy);
1043
1044 LTS[L] = SE.getUnknown(LoopPHI);
1045 }
1046
1047 // Add all mappings from the region to the global map so outside uses will use
1048 // the copied instructions.
1049 for (auto &BBMap : RegionMaps)
1050 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1051
Johannes Doerfert275a1752015-02-24 16:16:32 +00001052 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001053 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001054}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001055
1056void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1057 const Instruction *Inst,
1058 ValueMapT &BBMap) {
1059
1060 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1061 // phi is copied it will reload all inputs from outside the region, hence
1062 // we do not need to generate code for the read access of the operands of a
1063 // PHI.
1064 if (isa<PHINode>(Inst))
1065 return;
1066
1067 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1068}
1069
1070void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1071 ValueMapT &BBMap,
1072 ValueMapT &GlobalMap) {
1073 const Region &R = Stmt.getParent()->getRegion();
1074
Tobias Grosser75296902015-08-21 19:23:21 +00001075 assert(Stmt.getRegion() &&
1076 "Block statements need to use the generateScalarStores() "
1077 "function in the BlockGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001078
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001079 for (MemoryAccess *MA : Stmt) {
1080
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001081 if (!MA->isScalar() || MA->isRead())
1082 continue;
1083
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001084 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001085
Tobias Grosser62139132015-08-02 16:17:41 +00001086 // Only generate accesses that belong to this basic block.
1087 if (ScalarInst->getParent() != BB)
1088 continue;
1089
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001090 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001091
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001092 auto Address = getOrCreateAlloca(*MA);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001093
Tobias Grosser92245222015-07-28 14:53:44 +00001094 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001095 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001096 }
1097}
1098
1099void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1100 PHINode *PHICopy, BasicBlock *IncomingBB,
1101 ValueMapT &GlobalMap,
1102 LoopToScevMapT &LTS) {
1103 Region *StmtR = Stmt.getRegion();
1104
1105 // If the incoming block was not yet copied mark this PHI as incomplete.
1106 // Once the block will be copied the incoming value will be added.
1107 BasicBlock *BBCopy = BlockMap[IncomingBB];
1108 if (!BBCopy) {
1109 assert(StmtR->contains(IncomingBB) &&
1110 "Bad incoming block for PHI in non-affine region");
1111 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1112 return;
1113 }
1114
1115 Value *OpCopy = nullptr;
1116 if (StmtR->contains(IncomingBB)) {
1117 assert(RegionMaps.count(BBCopy) &&
1118 "Incoming PHI block did not have a BBMap");
1119 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1120
1121 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1122 OpCopy =
1123 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1124 } else {
1125
1126 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1127 return;
1128
Tobias Grosserb79a67d2015-08-28 08:23:35 +00001129 AllocaInst *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001130 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1131 BlockMap[IncomingBB]->getTerminator());
1132 }
1133
1134 assert(OpCopy && "Incoming PHI value was not copied properly");
1135 assert(BBCopy && "Incoming PHI block was not copied properly");
1136 PHICopy->addIncoming(OpCopy, BBCopy);
1137}
1138
1139Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1140 ValueMapT &BBMap,
1141 ValueMapT &GlobalMap,
1142 LoopToScevMapT &LTS) {
1143 unsigned NumIncoming = PHI->getNumIncomingValues();
1144 PHINode *PHICopy =
1145 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1146 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1147 BBMap[PHI] = PHICopy;
1148
1149 for (unsigned u = 0; u < NumIncoming; u++)
1150 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1151 LTS);
1152 return PHICopy;
1153}