blob: 9f64fdd35995098b7ac0b915d4cd67344b7a5e3f [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
Tobias Grosser655a4572015-08-30 17:32:39 +0000469void BlockGenerator::createScalarInitialization(Region &R) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000470 // The split block __just before__ the region and optimized region.
471 BasicBlock *SplitBB = R.getEnteringBlock();
472 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
473 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
474
475 // Get the start block of the __optimized__ region.
476 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
477 if (StartBB == R.getEntry())
478 StartBB = SplitBBTerm->getSuccessor(1);
479
480 // For each PHI predecessor outside the region store the incoming operand
481 // value prior to entering the optimized region.
482 Builder.SetInsertPoint(StartBB->getTerminator());
483
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000484 for (const auto &PHIOpMapping : PHIOpMap) {
485 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
486
487 // Check if this PHI has the split block as predecessor (that is the only
488 // possible predecessor outside the SCoP).
489 int idx = PHI->getBasicBlockIndex(SplitBB);
490 if (idx < 0)
491 continue;
492
493 Value *ScalarValue = PHI->getIncomingValue(idx);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000494
495 // If the split block is the predecessor initialize the PHI operator alloca.
496 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
497 }
498}
499
500void BlockGenerator::createScalarFinalization(Region &R) {
501 // The exit block of the __unoptimized__ region.
502 BasicBlock *ExitBB = R.getExitingBlock();
503 // The merge block __just after__ the region and the optimized region.
504 BasicBlock *MergeBB = R.getExit();
505
506 // The exit block of the __optimized__ region.
507 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
508 if (OptExitBB == ExitBB)
509 OptExitBB = *(++pred_begin(MergeBB));
510
511 Builder.SetInsertPoint(OptExitBB->getTerminator());
512 for (const auto &EscapeMapping : EscapeMap) {
513 // Extract the escaping instruction and the escaping users as well as the
514 // alloca the instruction was demoted to.
515 Instruction *EscapeInst = EscapeMapping.getFirst();
516 const auto &EscapeMappingValue = EscapeMapping.getSecond();
517 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
518 AllocaInst *ScalarAddr = EscapeMappingValue.first;
519
520 // Reload the demoted instruction in the optimized version of the SCoP.
521 Instruction *EscapeInstReload =
522 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
523
524 // Create the merge PHI that merges the optimized and unoptimized version.
525 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
526 EscapeInst->getName() + ".merge");
527 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
528
529 // Add the respective values to the merge PHI.
530 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
531 MergePHI->addIncoming(EscapeInst, ExitBB);
532
533 // The information of scalar evolution about the escaping instruction needs
534 // to be revoked so the new merged instruction will be used.
535 if (SE.isSCEVable(EscapeInst->getType()))
536 SE.forgetValue(EscapeInst);
537
538 // Replace all uses of the demoted instruction with the merge PHI.
539 for (Instruction *EUser : EscapeUsers)
540 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
541 }
542}
543
Tobias Grosser655a4572015-08-30 17:32:39 +0000544void BlockGenerator::finalizeSCoP(Scop &S) {
545 createScalarInitialization(S.getRegion());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000546 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000547}
548
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000549VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
550 VectorValueMapT &GlobalMaps,
551 std::vector<LoopToScevMapT> &VLTS,
552 isl_map *Schedule)
553 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
554 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000555 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
556 assert(Schedule && "No statement domain provided");
557}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000558
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000559Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000560 ValueMapT &VectorMap,
561 VectorValueMapT &ScalarMaps,
562 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000563 if (Value *NewValue = VectorMap.lookup(Old))
564 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000565
566 int Width = getVectorWidth();
567
568 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
569
570 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000571 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000572 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
573 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000574 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000575
576 VectorMap[Old] = Vector;
577
578 return Vector;
579}
580
581Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
582 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
583 assert(PointerTy && "PointerType expected");
584
585 Type *ScalarType = PointerTy->getElementType();
586 VectorType *VectorType = VectorType::get(ScalarType, Width);
587
588 return PointerType::getUnqual(VectorType);
589}
590
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000591Value *VectorBlockGenerator::generateStrideOneLoad(
592 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000593 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000594 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000595 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000596 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
597 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
598
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000599 Value *NewPointer = nullptr;
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000600 NewPointer =
601 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
602 GlobalMaps[Offset], VLTS[Offset], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000603 Value *VectorPtr =
604 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
605 LoadInst *VecLoad =
606 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000607 if (!Aligned)
608 VecLoad->setAlignment(8);
609
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000610 if (NegativeStride) {
611 SmallVector<Constant *, 16> Indices;
612 for (int i = VectorWidth - 1; i >= 0; i--)
613 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
614 Constant *SV = llvm::ConstantVector::get(Indices);
615 Value *RevVecLoad = Builder.CreateShuffleVector(
616 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
617 return RevVecLoad;
618 }
619
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000620 return VecLoad;
621}
622
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000623Value *VectorBlockGenerator::generateStrideZeroLoad(
624 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap,
625 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000626 const Value *Pointer = Load->getPointerOperand();
627 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000628 Value *NewPointer = generateLocationAccessed(
629 Stmt, Load, Pointer, BBMap, GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000630 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
631 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000632 LoadInst *ScalarLoad =
633 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000634
635 if (!Aligned)
636 ScalarLoad->setAlignment(8);
637
Tobias Grosserc14582f2013-02-05 18:01:29 +0000638 Constant *SplatVector = Constant::getNullValue(
639 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000640
Tobias Grosserc14582f2013-02-05 18:01:29 +0000641 Value *VectorLoad = Builder.CreateShuffleVector(
642 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000643 return VectorLoad;
644}
645
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000646Value *VectorBlockGenerator::generateUnknownStrideLoad(
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000647 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
648 __isl_keep isl_id_to_ast_expr *NewAccesses
649
650 ) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000651 int VectorWidth = getVectorWidth();
652 const Value *Pointer = Load->getPointerOperand();
653 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000654 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000655
656 Value *Vector = UndefValue::get(VectorType);
657
658 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000659 Value *NewPointer =
660 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[i],
661 GlobalMaps[i], VLTS[i], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000662 Value *ScalarLoad =
663 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
664 Vector = Builder.CreateInsertElement(
665 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000666 }
667
668 return Vector;
669}
670
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000671void VectorBlockGenerator::generateLoad(
672 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap,
673 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser28736452015-03-23 07:00:36 +0000674 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000675 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000676 ScalarMaps[i][Load] = generateScalarLoad(
677 Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000678 return;
679 }
680
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000681 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000682
Tobias Grosser95493982014-04-18 09:46:35 +0000683 // Make sure we have scalar values available to access the pointer to
684 // the data location.
685 extractScalarValues(Load, VectorMap, ScalarMaps);
686
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000687 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000688 if (Access.isStrideZero(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000689 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
Sebastian Popa00a0292012-12-18 07:46:06 +0000690 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000691 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000692 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000693 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000694 else
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000695 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000696
697 VectorMap[Load] = NewLoad;
698}
699
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000700void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
701 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000702 ValueMapT &VectorMap,
703 VectorValueMapT &ScalarMaps) {
704 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000705 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
706 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000707
708 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
709
710 const CastInst *Cast = dyn_cast<CastInst>(Inst);
711 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
712 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
713}
714
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000715void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
716 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000717 ValueMapT &VectorMap,
718 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000719 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000720 Value *OpZero = Inst->getOperand(0);
721 Value *OpOne = Inst->getOperand(1);
722
723 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000724 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
725 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000726
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000727 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000728 Inst->getName() + "p_vec");
729 VectorMap[Inst] = NewInst;
730}
731
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000732void VectorBlockGenerator::copyStore(
733 ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap,
734 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000735 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000736
737 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000738 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000739 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000740
Tobias Grosser50fd7012014-04-17 23:13:49 +0000741 // Make sure we have scalar values available to access the pointer to
742 // the data location.
743 extractScalarValues(Store, VectorMap, ScalarMaps);
744
Sebastian Popa00a0292012-12-18 07:46:06 +0000745 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000746 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000747 Value *NewPointer =
748 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[0],
749 GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000750
Tobias Grosserc14582f2013-02-05 18:01:29 +0000751 Value *VectorPtr =
752 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000753 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
754
755 if (!Aligned)
756 Store->setAlignment(8);
757 } else {
758 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000759 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000760 Value *NewPointer =
761 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[i],
762 GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000763 Builder.CreateStore(Scalar, NewPointer);
764 }
765 }
766}
767
768bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
769 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000770 for (Value *Operand : Inst->operands())
771 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000772 return true;
773 return false;
774}
775
776bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
777 ValueMapT &VectorMap,
778 VectorValueMapT &ScalarMaps) {
779 bool HasVectorOperand = false;
780 int VectorWidth = getVectorWidth();
781
Tobias Grosser91f5b262014-06-04 08:06:40 +0000782 for (Value *Operand : Inst->operands()) {
783 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000784
785 if (VecOp == VectorMap.end())
786 continue;
787
788 HasVectorOperand = true;
789 Value *NewVector = VecOp->second;
790
791 for (int i = 0; i < VectorWidth; ++i) {
792 ValueMapT &SM = ScalarMaps[i];
793
794 // If there is one scalar extracted, all scalar elements should have
795 // already been extracted by the code here. So no need to check for the
796 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000797 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000798 break;
799
Tobias Grosser91f5b262014-06-04 08:06:40 +0000800 SM[Operand] =
801 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000802 }
803 }
804
805 return HasVectorOperand;
806}
807
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000808void VectorBlockGenerator::copyInstScalarized(
809 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
810 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000811 bool HasVectorOperand;
812 int VectorWidth = getVectorWidth();
813
814 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
815
816 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000817 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000818 GlobalMaps[VectorLane], VLTS[VectorLane],
819 NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000820
821 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
822 return;
823
824 // Make the result available as vector value.
825 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
826 Value *Vector = UndefValue::get(VectorType);
827
828 for (int i = 0; i < VectorWidth; i++)
829 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
830 Builder.getInt32(i));
831
832 VectorMap[Inst] = Vector;
833}
834
Tobias Grosserc14582f2013-02-05 18:01:29 +0000835int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000836
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000837void VectorBlockGenerator::copyInstruction(
838 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
839 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000840 // Terminator instructions control the control flow. They are explicitly
841 // expressed in the clast and do not need to be copied.
842 if (Inst->isTerminator())
843 return;
844
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000845 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000846 return;
847
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000848 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000849 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000850 return;
851 }
852
853 if (hasVectorOperands(Inst, VectorMap)) {
854 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000855 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000856 return;
857 }
858
859 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000860 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000861 return;
862 }
863
864 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000865 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000866 return;
867 }
868
869 // Falltrough: We generate scalar instructions, if we don't know how to
870 // generate vector code.
871 }
872
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000873 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000874}
875
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000876void VectorBlockGenerator::copyStmt(
877 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000878 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
879 "the vector block generator");
880
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000881 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000882 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000883 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000884 CopyBB->setName("polly.stmt." + BB->getName());
885 Builder.SetInsertPoint(CopyBB->begin());
886
887 // Create two maps that store the mapping from the original instructions of
888 // the old basic block to their copies in the new basic block. Those maps
889 // are basic block local.
890 //
891 // As vector code generation is supported there is one map for scalar values
892 // and one for vector values.
893 //
894 // In case we just do scalar code generation, the vectorMap is not used and
895 // the scalarMap has just one dimension, which contains the mapping.
896 //
897 // In case vector code generation is done, an instruction may either appear
898 // in the vector map once (as it is calculating >vectorwidth< values at a
899 // time. Or (if the values are calculated using scalar operations), it
900 // appears once in every dimension of the scalarMap.
901 VectorValueMapT ScalarBlockMap(getVectorWidth());
902 ValueMapT VectorBlockMap;
903
Tobias Grosser91f5b262014-06-04 08:06:40 +0000904 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000905 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000906}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000907
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000908BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
909 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000910
911 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
912 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
913
914 if (BBCopyIDom)
915 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
916
917 return BBCopyIDom;
918}
919
Johannes Doerfert275a1752015-02-24 16:16:32 +0000920void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000921 LoopToScevMapT &LTS,
922 isl_id_to_ast_expr *IdToAstExp) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000923 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000924 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000925
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000926 // Forget all old mappings.
927 BlockMap.clear();
928 RegionMaps.clear();
929 IncompletePHINodeMap.clear();
930
Johannes Doerfert275a1752015-02-24 16:16:32 +0000931 // The region represented by the statement.
932 Region *R = Stmt.getRegion();
933
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000934 // Create a dedicated entry for the region where we can reload all demoted
935 // inputs.
936 BasicBlock *EntryBB = R->getEntry();
937 BasicBlock *EntryBBCopy =
938 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
939 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
940 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000941
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000942 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
943 if (!R->contains(*PI))
944 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000945
946 // Iterate over all blocks in the region in a breadth-first search.
947 std::deque<BasicBlock *> Blocks;
948 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000949 Blocks.push_back(EntryBB);
950 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000951
952 while (!Blocks.empty()) {
953 BasicBlock *BB = Blocks.front();
954 Blocks.pop_front();
955
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000956 // First split the block and update dominance information.
957 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000958 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
959
960 // In order to remap PHI nodes we store also basic block mappings.
961 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000962
963 // Get the mapping for this block and initialize it with the mapping
964 // available at its immediate dominator (in the new region).
965 ValueMapT &RegionMap = RegionMaps[BBCopy];
966 RegionMap = RegionMaps[BBCopyIDom];
967
Johannes Doerfert275a1752015-02-24 16:16:32 +0000968 // Copy the block with the BlockGenerator.
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000969 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS, IdToAstExp);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000970
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000971 // In order to remap PHI nodes we store also basic block mappings.
972 BlockMap[BB] = BBCopy;
973
974 // Add values to incomplete PHI nodes waiting for this block to be copied.
975 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
976 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
977 GlobalMap, LTS);
978 IncompletePHINodeMap[BB].clear();
979
Johannes Doerfert275a1752015-02-24 16:16:32 +0000980 // And continue with new successors inside the region.
981 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
982 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
983 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000984 }
985
986 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000987 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000988 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000989 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000990 BlockMap[R->getExit()] = ExitBBCopy;
991
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000992 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000993
994 // As the block generator doesn't handle control flow we need to add the
995 // region control flow by hand after all blocks have been copied.
996 for (BasicBlock *BB : SeenBlocks) {
997
998 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
999
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001000 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001001 Instruction *BICopy = BBCopy->getTerminator();
1002
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001003 ValueMapT &RegionMap = RegionMaps[BBCopy];
1004 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1005
Tobias Grosser45e79442015-08-01 09:07:57 +00001006 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001007 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1008 BICopy->eraseFromParent();
1009 }
1010
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001011 // Add counting PHI nodes to all loops in the region that can be used as
1012 // replacement for SCEVs refering to the old loop.
1013 for (BasicBlock *BB : SeenBlocks) {
1014 Loop *L = LI.getLoopFor(BB);
1015 if (L == nullptr || L->getHeader() != BB)
1016 continue;
1017
1018 BasicBlock *BBCopy = BlockMap[BB];
1019 Value *NullVal = Builder.getInt32(0);
1020 PHINode *LoopPHI =
1021 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1022 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1023 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1024 LoopPHI->insertBefore(BBCopy->begin());
1025 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1026
1027 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1028 if (!R->contains(PredBB))
1029 continue;
1030 if (L->contains(PredBB))
1031 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1032 else
1033 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1034 }
1035
1036 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1037 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1038 LoopPHI->addIncoming(NullVal, PredBBCopy);
1039
1040 LTS[L] = SE.getUnknown(LoopPHI);
1041 }
1042
1043 // Add all mappings from the region to the global map so outside uses will use
1044 // the copied instructions.
1045 for (auto &BBMap : RegionMaps)
1046 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1047
Johannes Doerfert275a1752015-02-24 16:16:32 +00001048 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001049 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001050}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001051
1052void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1053 const Instruction *Inst,
1054 ValueMapT &BBMap) {
1055
1056 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1057 // phi is copied it will reload all inputs from outside the region, hence
1058 // we do not need to generate code for the read access of the operands of a
1059 // PHI.
1060 if (isa<PHINode>(Inst))
1061 return;
1062
1063 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1064}
1065
1066void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1067 ValueMapT &BBMap,
1068 ValueMapT &GlobalMap) {
1069 const Region &R = Stmt.getParent()->getRegion();
1070
Tobias Grosser75296902015-08-21 19:23:21 +00001071 assert(Stmt.getRegion() &&
1072 "Block statements need to use the generateScalarStores() "
1073 "function in the BlockGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001074
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001075 for (MemoryAccess *MA : Stmt) {
1076
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001077 if (!MA->isScalar() || MA->isRead())
1078 continue;
1079
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001080 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001081
Tobias Grosser62139132015-08-02 16:17:41 +00001082 // Only generate accesses that belong to this basic block.
1083 if (ScalarInst->getParent() != BB)
1084 continue;
1085
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001086 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001087
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001088 auto Address = getOrCreateAlloca(*MA);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001089
Tobias Grosser92245222015-07-28 14:53:44 +00001090 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001091 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001092 }
1093}
1094
1095void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1096 PHINode *PHICopy, BasicBlock *IncomingBB,
1097 ValueMapT &GlobalMap,
1098 LoopToScevMapT &LTS) {
1099 Region *StmtR = Stmt.getRegion();
1100
1101 // If the incoming block was not yet copied mark this PHI as incomplete.
1102 // Once the block will be copied the incoming value will be added.
1103 BasicBlock *BBCopy = BlockMap[IncomingBB];
1104 if (!BBCopy) {
1105 assert(StmtR->contains(IncomingBB) &&
1106 "Bad incoming block for PHI in non-affine region");
1107 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1108 return;
1109 }
1110
1111 Value *OpCopy = nullptr;
1112 if (StmtR->contains(IncomingBB)) {
1113 assert(RegionMaps.count(BBCopy) &&
1114 "Incoming PHI block did not have a BBMap");
1115 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1116
1117 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1118 OpCopy =
1119 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1120 } else {
1121
1122 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1123 return;
1124
Tobias Grosserb79a67d2015-08-28 08:23:35 +00001125 AllocaInst *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001126 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1127 BlockMap[IncomingBB]->getTerminator());
1128 }
1129
1130 assert(OpCopy && "Incoming PHI value was not copied properly");
1131 assert(BBCopy && "Incoming PHI block was not copied properly");
1132 PHICopy->addIncoming(OpCopy, BBCopy);
1133}
1134
1135Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1136 ValueMapT &BBMap,
1137 ValueMapT &GlobalMap,
1138 LoopToScevMapT &LTS) {
1139 unsigned NumIncoming = PHI->getNumIncomingValues();
1140 PHINode *PHICopy =
1141 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1142 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1143 BBMap[PHI] = PHICopy;
1144
1145 for (unsigned u = 0; u < NumIncoming; u++)
1146 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1147 LTS);
1148 return PHICopy;
1149}