blob: 982d7e7546f79d08d2a97902778540acb52183dc [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
Hongbin Zheng8a846612012-04-25 13:18:28 +000016#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000018#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser86bc93a2015-09-06 08:47:57 +000019#include "polly/CodeGen/RuntimeDebugBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000020#include "polly/Options.h"
Tobias Grosser5624d3c2015-12-21 12:38:56 +000021#include "polly/ScopInfo.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000022#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000023#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000024#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000025#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000026#include "llvm/Analysis/RegionInfo.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000027#include "llvm/Analysis/ScalarEvolution.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000028#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000029#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Tobias Grosser1b9d25a2015-09-27 11:17:22 +000031#include "llvm/Transforms/Utils/Local.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000032#include "isl/aff.h"
33#include "isl/ast.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000034#include "isl/ast_build.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000035#include "isl/set.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000036#include <deque>
37
Hongbin Zheng3b11a162012-04-25 13:16:49 +000038using namespace llvm;
39using namespace polly;
40
Tobias Grosser878aba42014-10-22 23:22:41 +000041static cl::opt<bool> Aligned("enable-polly-aligned",
42 cl::desc("Assumed aligned memory accesses."),
43 cl::Hidden, cl::init(false), cl::ZeroOrMore,
44 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000045
Tobias Grosser86bc93a2015-09-06 08:47:57 +000046static cl::opt<bool> DebugPrinting(
47 "polly-codegen-add-debug-printing",
48 cl::desc("Add printf calls that show the values loaded/stored."),
49 cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
50
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000051BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
52 ScalarEvolution &SE, DominatorTree &DT,
Johannes Doerfertecff11d2015-05-22 23:43:58 +000053 ScalarAllocaMapTy &ScalarMap,
54 ScalarAllocaMapTy &PHIOpMap,
55 EscapeUsersAllocaMapTy &EscapeMap,
Tobias Grosserf4bb7a62015-10-04 10:18:32 +000056 ValueMapT &GlobalMap,
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000057 IslExprBuilder *ExprBuilder)
Johannes Doerfertecff11d2015-05-22 23:43:58 +000058 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
59 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
Tobias Grosserbc132602015-09-05 09:56:54 +000060 EscapeMap(EscapeMap), GlobalMap(GlobalMap) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000061
Tobias Grosser2f1acac2015-10-04 10:18:45 +000062Value *BlockGenerator::trySynthesizeNewValue(ScopStmt &Stmt, Value *Old,
Tobias Grosser33cb9f92015-09-30 11:56:19 +000063 ValueMapT &BBMap,
64 LoopToScevMapT &LTS,
65 Loop *L) const {
Johannes Doerfert42df8d12015-12-22 19:08:01 +000066 if (!SE.isSCEVable(Old->getType()))
67 return nullptr;
Johannes Doerferte69e1142015-08-18 11:56:00 +000068
Johannes Doerfert42df8d12015-12-22 19:08:01 +000069 const SCEV *Scev = SE.getSCEVAtScope(Old, L);
70 if (!Scev)
71 return nullptr;
Johannes Doerferte69e1142015-08-18 11:56:00 +000072
Johannes Doerfert42df8d12015-12-22 19:08:01 +000073 if (isa<SCEVCouldNotCompute>(Scev))
74 return nullptr;
Tobias Grossere71c6ab2012-04-27 16:36:14 +000075
Sanjoy Das03bcb912016-05-29 07:33:16 +000076 const SCEV *NewScev = SCEVLoopAddRecRewriter::rewrite(Scev, LTS, SE);
Johannes Doerfert42df8d12015-12-22 19:08:01 +000077 ValueMapT VTV;
78 VTV.insert(BBMap.begin(), BBMap.end());
79 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Tobias Grossere71c6ab2012-04-27 16:36:14 +000080
Johannes Doerfert42df8d12015-12-22 19:08:01 +000081 Scop &S = *Stmt.getParent();
Johannes Doerfert3f52e352016-05-23 12:38:05 +000082 const DataLayout &DL = S.getFunction().getParent()->getDataLayout();
Johannes Doerfert42df8d12015-12-22 19:08:01 +000083 auto IP = Builder.GetInsertPoint();
84
85 assert(IP != Builder.GetInsertBlock()->end() &&
86 "Only instructions can be insert points for SCEVExpander");
87 Value *Expanded =
88 expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), &*IP, &VTV);
89
90 BBMap[Old] = Expanded;
91 return Expanded;
Tobias Grosser33cb9f92015-09-30 11:56:19 +000092}
93
Tobias Grosser2f1acac2015-10-04 10:18:45 +000094Value *BlockGenerator::getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap,
95 LoopToScevMapT &LTS, Loop *L) const {
Tobias Grosser9bd0dad2015-12-14 16:19:59 +000096 // Constants that do not reference any named value can always remain
Michael Kruse5bbc0e12015-12-14 23:41:32 +000097 // unchanged. Handle them early to avoid expensive map lookups. We do not take
Tobias Grosser9bd0dad2015-12-14 16:19:59 +000098 // the fast-path for external constants which are referenced through globals
99 // as these may need to be rewritten when distributing code accross different
100 // LLVM modules.
101 if (isa<Constant>(Old) && !isa<GlobalValue>(Old))
Tobias Grosser6f764bb2015-12-14 16:19:54 +0000102 return Old;
Tobias Grosser33cb9f92015-09-30 11:56:19 +0000103
Johannes Doerfert28f8ac12015-12-22 19:08:24 +0000104 // Inline asm is like a constant to us.
105 if (isa<InlineAsm>(Old))
106 return Old;
107
Tobias Grosser33cb9f92015-09-30 11:56:19 +0000108 if (Value *New = GlobalMap.lookup(Old)) {
109 if (Value *NewRemapped = GlobalMap.lookup(New))
110 New = NewRemapped;
111 if (Old->getType()->getScalarSizeInBits() <
112 New->getType()->getScalarSizeInBits())
113 New = Builder.CreateTruncOrBitCast(New, Old->getType());
114
115 return New;
116 }
117
118 if (Value *New = BBMap.lookup(Old))
119 return New;
120
121 if (Value *New = trySynthesizeNewValue(Stmt, Old, BBMap, LTS, L))
122 return New;
123
Tobias Grosser16371ac2014-11-05 20:48:56 +0000124 // A scop-constant value defined by a global or a function parameter.
125 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
Tobias Grosser6f764bb2015-12-14 16:19:54 +0000126 return Old;
Tobias Grosser16371ac2014-11-05 20:48:56 +0000127
128 // A scop-constant value defined by an instruction executed outside the scop.
129 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfert952b5302016-05-23 12:40:48 +0000130 if (!Stmt.getParent()->contains(Inst->getParent()))
Tobias Grosser6f764bb2015-12-14 16:19:54 +0000131 return Old;
Tobias Grosser16371ac2014-11-05 20:48:56 +0000132
133 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000134 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000135 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000136}
137
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000138void BlockGenerator::copyInstScalar(ScopStmt &Stmt, Instruction *Inst,
Tobias Grosserbc132602015-09-05 09:56:54 +0000139 ValueMapT &BBMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000140 // We do not generate debug intrinsics as we did not investigate how to
141 // copy them correctly. At the current state, they just crash the code
142 // generation as the meta-data operands are not correctly copied.
143 if (isa<DbgInfoIntrinsic>(Inst))
144 return;
145
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000146 Instruction *NewInst = Inst->clone();
147
148 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000149 for (Value *OldOperand : Inst->operands()) {
Tobias Grosserbc132602015-09-05 09:56:54 +0000150 Value *NewOperand =
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000151 getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForStmt(Stmt));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000152
153 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000154 assert(!isa<StoreInst>(NewInst) &&
155 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000156 delete NewInst;
157 return;
158 }
159
160 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
161 }
162
163 Builder.Insert(NewInst);
164 BBMap[Inst] = NewInst;
165
166 if (!NewInst->getType()->isVoidTy())
167 NewInst->setName("p_" + Inst->getName());
168}
169
Michael Kruse70131d32016-01-27 17:09:17 +0000170Value *
171BlockGenerator::generateLocationAccessed(ScopStmt &Stmt, MemAccInst Inst,
172 ValueMapT &BBMap, LoopToScevMapT &LTS,
173 isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser09214772015-12-15 23:49:53 +0000174 const MemoryAccess &MA = Stmt.getArrayAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000175
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000176 isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000177
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000178 if (AccessExpr) {
179 AccessExpr = isl_ast_expr_address_of(AccessExpr);
Tobias Grosser98b3ee52015-09-29 06:44:38 +0000180 auto Address = ExprBuilder->create(AccessExpr);
181
182 // Cast the address of this memory access to a pointer type that has the
183 // same element type as the original access, but uses the address space of
184 // the newly generated pointer.
185 auto OldPtrTy = MA.getAccessValue()->getType()->getPointerTo();
186 auto NewPtrTy = Address->getType();
187 OldPtrTy = PointerType::get(OldPtrTy->getElementType(),
188 NewPtrTy->getPointerAddressSpace());
189
Tobias Grosserd840fc72016-02-04 13:18:42 +0000190 if (OldPtrTy != NewPtrTy)
Tobias Grosser98b3ee52015-09-29 06:44:38 +0000191 Address = Builder.CreateBitOrPointerCast(Address, OldPtrTy);
Tobias Grosser98b3ee52015-09-29 06:44:38 +0000192 return Address;
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000193 }
194
Michael Kruse70131d32016-01-27 17:09:17 +0000195 return getNewValue(Stmt, Inst.getPointerOperand(), BBMap, LTS,
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000196 getLoopForStmt(Stmt));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197}
198
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000199Loop *BlockGenerator::getLoopForStmt(const ScopStmt &Stmt) const {
Michael Kruse375cb5f2016-02-24 22:08:24 +0000200 auto *StmtBB = Stmt.getEntryBlock();
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000201 return LI.getLoopFor(StmtBB);
Tobias Grosser369430f2013-03-22 23:42:53 +0000202}
203
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000204Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, LoadInst *Load,
Tobias Grosserbc132602015-09-05 09:56:54 +0000205 ValueMapT &BBMap, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000206 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertc1db67e2015-09-29 23:47:21 +0000207 if (Value *PreloadLoad = GlobalMap.lookup(Load))
208 return PreloadLoad;
209
Tobias Grosserbc132602015-09-05 09:56:54 +0000210 Value *NewPointer =
Michael Kruse70131d32016-01-27 17:09:17 +0000211 generateLocationAccessed(Stmt, Load, BBMap, LTS, NewAccesses);
Johannes Doerfert87901452014-10-02 16:22:19 +0000212 Value *ScalarLoad = Builder.CreateAlignedLoad(
213 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Tobias Grosser86bc93a2015-09-06 08:47:57 +0000214
215 if (DebugPrinting)
216 RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer,
217 ": ", ScalarLoad, "\n");
218
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000219 return ScalarLoad;
220}
221
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000222void BlockGenerator::generateScalarStore(ScopStmt &Stmt, StoreInst *Store,
Tobias Grosserbc132602015-09-05 09:56:54 +0000223 ValueMapT &BBMap, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000224 isl_id_to_ast_expr *NewAccesses) {
Tobias Grosserbc132602015-09-05 09:56:54 +0000225 Value *NewPointer =
Michael Kruse70131d32016-01-27 17:09:17 +0000226 generateLocationAccessed(Stmt, Store, BBMap, LTS, NewAccesses);
Tobias Grosserbc132602015-09-05 09:56:54 +0000227 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, LTS,
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000228 getLoopForStmt(Stmt));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000229
Tobias Grosser86bc93a2015-09-06 08:47:57 +0000230 if (DebugPrinting)
231 RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to ", NewPointer,
232 ": ", ValueOperand, "\n");
233
Tobias Grosserc186ac72015-08-11 08:13:15 +0000234 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000235}
236
Johannes Doerfert5dced262015-12-22 19:08:49 +0000237bool BlockGenerator::canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst) {
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000238 Loop *L = getLoopForStmt(Stmt);
Johannes Doerfert5dced262015-12-22 19:08:49 +0000239 return (Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
Johannes Doerfert0f0d209b2016-05-23 12:47:09 +0000240 canSynthesize(Inst, *Stmt.getParent(), &LI, &SE, L);
Johannes Doerfert5dced262015-12-22 19:08:49 +0000241}
242
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000243void BlockGenerator::copyInstruction(ScopStmt &Stmt, Instruction *Inst,
Tobias Grosserbc132602015-09-05 09:56:54 +0000244 ValueMapT &BBMap, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000245 isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000246 // Terminator instructions control the control flow. They are explicitly
247 // expressed in the clast and do not need to be copied.
248 if (Inst->isTerminator())
249 return;
250
Johannes Doerfert5dced262015-12-22 19:08:49 +0000251 // Synthesizable statements will be generated on-demand.
252 if (canSyntheziseInStmt(Stmt, Inst))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000253 return;
254
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000255 if (auto *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosserbc132602015-09-05 09:56:54 +0000256 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, LTS, NewAccesses);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000257 // Compute NewLoad before its insertion in BBMap to make the insertion
258 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000259 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000260 return;
261 }
262
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000263 if (auto *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosserbc132602015-09-05 09:56:54 +0000264 generateScalarStore(Stmt, Store, BBMap, LTS, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000265 return;
266 }
267
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000268 if (auto *PHI = dyn_cast<PHINode>(Inst)) {
Tobias Grosserbc132602015-09-05 09:56:54 +0000269 copyPHIInstruction(Stmt, PHI, BBMap, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000270 return;
271 }
272
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000273 // Skip some special intrinsics for which we do not adjust the semantics to
274 // the new schedule. All others are handled like every other instruction.
Tobias Grosser9c0ffe32015-08-30 16:57:15 +0000275 if (isIgnoredIntrinsic(Inst))
276 return;
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000277
Tobias Grosserbc132602015-09-05 09:56:54 +0000278 copyInstScalar(Stmt, Inst, BBMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000279}
280
Tobias Grosser9d12d8a2016-07-21 11:48:36 +0000281void BlockGenerator::removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap) {
Tobias Grosserc59b3ce2016-08-09 08:59:05 +0000282 auto NewBB = Builder.GetInsertBlock();
283 for (auto I = NewBB->rbegin(); I != NewBB->rend(); I++) {
284 Instruction *NewInst = &*I;
Tobias Grosser9d12d8a2016-07-21 11:48:36 +0000285
286 if (!isInstructionTriviallyDead(NewInst))
287 continue;
288
Tobias Grosserc59b3ce2016-08-09 08:59:05 +0000289 for (auto Pair : BBMap)
290 if (Pair.second == NewInst) {
291 BBMap.erase(Pair.first);
292 }
293
Tobias Grosser9d12d8a2016-07-21 11:48:36 +0000294 NewInst->eraseFromParent();
Tobias Grosserc59b3ce2016-08-09 08:59:05 +0000295 I = NewBB->rbegin();
Tobias Grosser9d12d8a2016-07-21 11:48:36 +0000296 }
297}
298
Tobias Grosserbc132602015-09-05 09:56:54 +0000299void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000300 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000301 assert(Stmt.isBlockStmt() &&
302 "Only block statements can be copied by the block generator");
303
304 ValueMapT BBMap;
305
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000306 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserbc132602015-09-05 09:56:54 +0000307 copyBB(Stmt, BB, BBMap, LTS, NewAccesses);
Tobias Grosser9d12d8a2016-07-21 11:48:36 +0000308 removeDeadInstructions(BB, BBMap);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000309}
310
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000311BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000312 BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(),
313 &*Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000314 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000315 return CopyBB;
316}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000317
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000318BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
Tobias Grosserbc132602015-09-05 09:56:54 +0000319 ValueMapT &BBMap, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000320 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000321 BasicBlock *CopyBB = splitBB(BB);
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000322 Builder.SetInsertPoint(&CopyBB->front());
Michael Kruse225f0d12015-10-17 21:36:00 +0000323 generateScalarLoads(Stmt, BBMap);
324
Tobias Grosserbc132602015-09-05 09:56:54 +0000325 copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses);
Michael Kruse225f0d12015-10-17 21:36:00 +0000326
327 // After a basic block was copied store all scalars that escape this block in
328 // their alloca.
329 generateScalarStores(Stmt, LTS, BBMap);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000330 return CopyBB;
331}
332
333void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
Tobias Grosserbc132602015-09-05 09:56:54 +0000334 ValueMapT &BBMap, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000335 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000336 EntryBB = &CopyBB->getParent()->getEntryBlock();
337
Tobias Grosser91f5b262014-06-04 08:06:40 +0000338 for (Instruction &Inst : *BB)
Tobias Grosserbc132602015-09-05 09:56:54 +0000339 copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000340}
341
Tobias Grosser64c0ff42015-08-31 05:52:24 +0000342Value *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
343 ScalarAllocaMapTy &Map,
Tobias Grosser64c0ff42015-08-31 05:52:24 +0000344 const char *NameExt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000345 // If no alloca was found create one and insert it in the entry block.
Tobias Grossere9cb5a02015-10-03 17:19:49 +0000346 if (!Map.count(ScalarBase)) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000347 auto *Ty = ScalarBase->getType();
Tobias Grosserb09455d2015-09-18 06:01:11 +0000348 auto NewAddr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
Tobias Grosser64c0ff42015-08-31 05:52:24 +0000349 EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000350 NewAddr->insertBefore(&*EntryBB->getFirstInsertionPt());
Tobias Grossere9cb5a02015-10-03 17:19:49 +0000351 Map[ScalarBase] = NewAddr;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000352 }
353
Tobias Grossere9cb5a02015-10-03 17:19:49 +0000354 auto Addr = Map[ScalarBase];
355
Tobias Grosser5c7f16b2016-01-24 14:16:59 +0000356 if (auto NewAddr = GlobalMap.lookup(Addr))
357 return NewAddr;
Tobias Grosser64c0ff42015-08-31 05:52:24 +0000358
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000359 return Addr;
360}
361
Johannes Doerfert800e17a2016-02-02 14:16:01 +0000362Value *BlockGenerator::getOrCreateAlloca(const MemoryAccess &Access) {
Tobias Grosser3216f852016-08-04 06:55:53 +0000363 assert(!Access.isArrayKind() && "Trying to get alloca for array kind");
364
Tobias Grossera535dff2015-12-13 19:59:01 +0000365 if (Access.isPHIKind())
Tobias Grosserb8d27aa2015-10-18 00:51:13 +0000366 return getOrCreatePHIAlloca(Access.getBaseAddr());
367 else
368 return getOrCreateScalarAlloca(Access.getBaseAddr());
Tobias Grossera4f09882015-10-17 22:16:00 +0000369}
370
371Value *BlockGenerator::getOrCreateAlloca(const ScopArrayInfo *Array) {
Tobias Grosser3216f852016-08-04 06:55:53 +0000372 assert(!Array->isArrayKind() && "Trying to get alloca for array kind");
373
Tobias Grossera535dff2015-12-13 19:59:01 +0000374 if (Array->isPHIKind())
Tobias Grossera4f09882015-10-17 22:16:00 +0000375 return getOrCreatePHIAlloca(Array->getBasePtr());
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000376 else
Tobias Grossera4f09882015-10-17 22:16:00 +0000377 return getOrCreateScalarAlloca(Array->getBasePtr());
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000378}
379
Tobias Grosserbc132602015-09-05 09:56:54 +0000380Value *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) {
381 return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
Tobias Grosserb79a67d2015-08-28 08:23:35 +0000382}
383
Tobias Grosserbc132602015-09-05 09:56:54 +0000384Value *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) {
385 return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
Tobias Grosserb79a67d2015-08-28 08:23:35 +0000386}
387
Johannes Doerfert38a012c2016-05-23 09:14:07 +0000388void BlockGenerator::handleOutsideUsers(const Scop &S, Instruction *Inst) {
Tobias Grosser29854002015-08-30 15:03:59 +0000389 // If there are escape users we get the alloca for this instruction and put it
390 // in the EscapeMap for later finalization. Lastly, if the instruction was
391 // copied multiple times we already did this and can exit.
Michael Kruseacb6ade2015-08-18 17:25:48 +0000392 if (EscapeMap.count(Inst))
393 return;
Johannes Doerferte69e1142015-08-18 11:56:00 +0000394
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000395 EscapeUserVectorTy EscapeUsers;
396 for (User *U : Inst->users()) {
397
398 // Non-instruction user will never escape.
399 Instruction *UI = dyn_cast<Instruction>(U);
400 if (!UI)
401 continue;
402
Johannes Doerfert952b5302016-05-23 12:40:48 +0000403 if (S.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000404 continue;
405
406 EscapeUsers.push_back(UI);
407 }
408
409 // Exit if no escape uses were found.
410 if (EscapeUsers.empty())
411 return;
412
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000413 // Get or create an escape alloca for this instruction.
Johannes Doerfert38a012c2016-05-23 09:14:07 +0000414 auto *ScalarAddr = getOrCreateScalarAlloca(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000415
416 // Remember that this instruction has escape uses and the escape alloca.
417 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000418}
419
Michael Kruse225f0d12015-10-17 21:36:00 +0000420void BlockGenerator::generateScalarLoads(ScopStmt &Stmt, ValueMapT &BBMap) {
421 for (MemoryAccess *MA : Stmt) {
Tobias Grossera535dff2015-12-13 19:59:01 +0000422 if (MA->isArrayKind() || MA->isWrite())
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000423 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000424
Michael Krusee2bccbb2015-09-18 19:59:43 +0000425 auto *Address = getOrCreateAlloca(*MA);
Michael Kruseeac97262016-02-24 22:08:14 +0000426 assert((!isa<Instruction>(Address) ||
427 DT.dominates(cast<Instruction>(Address)->getParent(),
428 Builder.GetInsertBlock())) &&
429 "Domination violation");
Michael Krusee2bccbb2015-09-18 19:59:43 +0000430 BBMap[MA->getBaseAddr()] =
Tobias Grosser2fc50df2015-08-30 19:51:01 +0000431 Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000432 }
433}
434
Michael Kruse225f0d12015-10-17 21:36:00 +0000435void BlockGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT &LTS,
Tobias Grosserbc132602015-09-05 09:56:54 +0000436 ValueMapT &BBMap) {
Tobias Grosserf2cdd142016-01-26 10:01:35 +0000437 Loop *L = LI.getLoopFor(Stmt.getBasicBlock());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000438
Michael Kruse225f0d12015-10-17 21:36:00 +0000439 assert(Stmt.isBlockStmt() && "Region statements need to use the "
440 "generateScalarStores() function in the "
441 "RegionGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000442
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000443 for (MemoryAccess *MA : Stmt) {
Tobias Grossera535dff2015-12-13 19:59:01 +0000444 if (MA->isArrayKind() || MA->isRead())
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000445 continue;
446
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000447 Value *Val = MA->getAccessValue();
Michael Kruseee6a4fc2016-01-26 13:33:27 +0000448 if (MA->isAnyPHIKind()) {
449 assert(MA->getIncoming().size() >= 1 &&
450 "Block statements have exactly one exiting block, or multiple but "
451 "with same incoming block and value");
452 assert(std::all_of(MA->getIncoming().begin(), MA->getIncoming().end(),
453 [&](std::pair<BasicBlock *, Value *> p) -> bool {
454 return p.first == Stmt.getBasicBlock();
455 }) &&
456 "Incoming block must be statement's block");
457 Val = MA->getIncoming()[0].second;
458 }
Tobias Grosserbc132602015-09-05 09:56:54 +0000459 auto *Address = getOrCreateAlloca(*MA);
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000460
Tobias Grosserf2cdd142016-01-26 10:01:35 +0000461 Val = getNewValue(Stmt, Val, BBMap, LTS, L);
Michael Kruseeac97262016-02-24 22:08:14 +0000462 assert((!isa<Instruction>(Val) ||
463 DT.dominates(cast<Instruction>(Val)->getParent(),
464 Builder.GetInsertBlock())) &&
465 "Domination violation");
466 assert((!isa<Instruction>(Address) ||
467 DT.dominates(cast<Instruction>(Address)->getParent(),
468 Builder.GetInsertBlock())) &&
469 "Domination violation");
Tobias Grosser92245222015-07-28 14:53:44 +0000470 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000471 }
472}
473
Tobias Grosserc0091a72015-08-30 19:19:34 +0000474void BlockGenerator::createScalarInitialization(Scop &S) {
Johannes Doerfertef744432016-05-23 12:42:38 +0000475 BasicBlock *ExitBB = S.getExit();
Johannes Doerfert188542f2015-11-09 00:21:21 +0000476
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000477 // The split block __just before__ the region and optimized region.
Johannes Doerfertef744432016-05-23 12:42:38 +0000478 BasicBlock *SplitBB = S.getEnteringBlock();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000479 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
480 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
481
482 // Get the start block of the __optimized__ region.
483 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
Johannes Doerfertef744432016-05-23 12:42:38 +0000484 if (StartBB == S.getEntry())
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000485 StartBB = SplitBBTerm->getSuccessor(1);
486
Tobias Grosser776700d2016-08-09 15:34:59 +0000487 Builder.SetInsertPoint(&*StartBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000488
Roman Gareevd7754a12016-07-30 09:25:51 +0000489 for (auto &Array : S.arrays()) {
Tobias Grosserc0091a72015-08-30 19:19:34 +0000490 if (Array->getNumberOfDimensions() != 0)
491 continue;
Tobias Grossera535dff2015-12-13 19:59:01 +0000492 if (Array->isPHIKind()) {
Tobias Grosserc0091a72015-08-30 19:19:34 +0000493 // For PHI nodes, the only values we need to store are the ones that
494 // reach the PHI node from outside the region. In general there should
495 // only be one such incoming edge and this edge should enter through
496 // 'SplitBB'.
497 auto PHI = cast<PHINode>(Array->getBasePtr());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000498
Tobias Grosserc0091a72015-08-30 19:19:34 +0000499 for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++)
Johannes Doerfert952b5302016-05-23 12:40:48 +0000500 if (!S.contains(*BI) && *BI != SplitBB)
Tobias Grosserc0091a72015-08-30 19:19:34 +0000501 llvm_unreachable("Incoming edges from outside the scop should always "
502 "come from SplitBB");
503
504 int Idx = PHI->getBasicBlockIndex(SplitBB);
505 if (Idx < 0)
506 continue;
507
508 Value *ScalarValue = PHI->getIncomingValue(Idx);
509
Tobias Grosserbc132602015-09-05 09:56:54 +0000510 Builder.CreateStore(ScalarValue, getOrCreatePHIAlloca(PHI));
Tobias Grosserc0091a72015-08-30 19:19:34 +0000511 continue;
512 }
513
514 auto *Inst = dyn_cast<Instruction>(Array->getBasePtr());
515
Johannes Doerfert952b5302016-05-23 12:40:48 +0000516 if (Inst && S.contains(Inst))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000517 continue;
518
Johannes Doerfert188542f2015-11-09 00:21:21 +0000519 // PHI nodes that are not marked as such in their SAI object are either exit
520 // PHI nodes we model as common scalars but without initialization, or
521 // incoming phi nodes that need to be initialized. Check if the first is the
522 // case for Inst and do not create and initialize memory if so.
523 if (auto *PHI = dyn_cast_or_null<PHINode>(Inst))
524 if (!S.hasSingleExitEdge() && PHI->getBasicBlockIndex(ExitBB) >= 0)
525 continue;
Johannes Doerfert717b8662015-09-08 21:44:27 +0000526
Tobias Grosserc0091a72015-08-30 19:19:34 +0000527 Builder.CreateStore(Array->getBasePtr(),
Tobias Grosserbc132602015-09-05 09:56:54 +0000528 getOrCreateScalarAlloca(Array->getBasePtr()));
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000529 }
530}
531
Johannes Doerfertef744432016-05-23 12:42:38 +0000532void BlockGenerator::createScalarFinalization(Scop &S) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000533 // The exit block of the __unoptimized__ region.
Johannes Doerfertef744432016-05-23 12:42:38 +0000534 BasicBlock *ExitBB = S.getExitingBlock();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000535 // The merge block __just after__ the region and the optimized region.
Johannes Doerfertef744432016-05-23 12:42:38 +0000536 BasicBlock *MergeBB = S.getExit();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000537
538 // The exit block of the __optimized__ region.
539 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
540 if (OptExitBB == ExitBB)
541 OptExitBB = *(++pred_begin(MergeBB));
542
543 Builder.SetInsertPoint(OptExitBB->getTerminator());
544 for (const auto &EscapeMapping : EscapeMap) {
545 // Extract the escaping instruction and the escaping users as well as the
546 // alloca the instruction was demoted to.
Michael Krusefbde4352016-08-05 16:45:51 +0000547 Instruction *EscapeInst = EscapeMapping.first;
548 const auto &EscapeMappingValue = EscapeMapping.second;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000549 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
Tobias Grosser64c0ff42015-08-31 05:52:24 +0000550 Value *ScalarAddr = EscapeMappingValue.first;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000551
552 // Reload the demoted instruction in the optimized version of the SCoP.
Johannes Doerfertd8b6ad22015-10-18 12:36:42 +0000553 Value *EscapeInstReload =
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000554 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
Johannes Doerfertd8b6ad22015-10-18 12:36:42 +0000555 EscapeInstReload =
556 Builder.CreateBitOrPointerCast(EscapeInstReload, EscapeInst->getType());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000557
558 // Create the merge PHI that merges the optimized and unoptimized version.
559 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
560 EscapeInst->getName() + ".merge");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000561 MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000562
563 // Add the respective values to the merge PHI.
564 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
565 MergePHI->addIncoming(EscapeInst, ExitBB);
566
567 // The information of scalar evolution about the escaping instruction needs
568 // to be revoked so the new merged instruction will be used.
569 if (SE.isSCEVable(EscapeInst->getType()))
570 SE.forgetValue(EscapeInst);
571
572 // Replace all uses of the demoted instruction with the merge PHI.
573 for (Instruction *EUser : EscapeUsers)
574 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
575 }
576}
577
Tobias Grosserffa24462015-10-17 08:54:13 +0000578void BlockGenerator::findOutsideUsers(Scop &S) {
Roman Gareevd7754a12016-07-30 09:25:51 +0000579 for (auto &Array : S.arrays()) {
Tobias Grosserffa24462015-10-17 08:54:13 +0000580
581 if (Array->getNumberOfDimensions() != 0)
582 continue;
583
Tobias Grossera535dff2015-12-13 19:59:01 +0000584 if (Array->isPHIKind())
Tobias Grosserffa24462015-10-17 08:54:13 +0000585 continue;
586
587 auto *Inst = dyn_cast<Instruction>(Array->getBasePtr());
588
589 if (!Inst)
590 continue;
591
592 // Scop invariant hoisting moves some of the base pointers out of the scop.
593 // We can ignore these, as the invariant load hoisting already registers the
594 // relevant outside users.
Johannes Doerfert952b5302016-05-23 12:40:48 +0000595 if (!S.contains(Inst))
Tobias Grosserffa24462015-10-17 08:54:13 +0000596 continue;
597
Johannes Doerfert38a012c2016-05-23 09:14:07 +0000598 handleOutsideUsers(S, Inst);
Tobias Grosserffa24462015-10-17 08:54:13 +0000599 }
600}
601
Tobias Grosser27d742d2015-10-24 17:41:29 +0000602void BlockGenerator::createExitPHINodeMerges(Scop &S) {
603 if (S.hasSingleExitEdge())
604 return;
605
Johannes Doerfertef744432016-05-23 12:42:38 +0000606 auto *ExitBB = S.getExitingBlock();
607 auto *MergeBB = S.getExit();
Tobias Grosser27d742d2015-10-24 17:41:29 +0000608 auto *AfterMergeBB = MergeBB->getSingleSuccessor();
609 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
610 if (OptExitBB == ExitBB)
611 OptExitBB = *(++pred_begin(MergeBB));
612
613 Builder.SetInsertPoint(OptExitBB->getTerminator());
614
Roman Gareevd7754a12016-07-30 09:25:51 +0000615 for (auto &SAI : S.arrays()) {
Tobias Grosser27d742d2015-10-24 17:41:29 +0000616 auto *Val = SAI->getBasePtr();
617
Michael Krusefaedfcb2016-03-03 17:20:43 +0000618 // Only Value-like scalars need a merge PHI. Exit block PHIs receive either
619 // the original PHI's value or the reloaded incoming values from the
620 // generated code. An llvm::Value is merged between the original code's
621 // value or the generated one.
622 if (!SAI->isValueKind() && !SAI->isExitPHIKind())
623 continue;
624
Tobias Grosser27d742d2015-10-24 17:41:29 +0000625 PHINode *PHI = dyn_cast<PHINode>(Val);
626 if (!PHI)
627 continue;
628
Tobias Grossera3f6eda2015-10-24 19:01:09 +0000629 if (PHI->getParent() != AfterMergeBB)
Tobias Grosser27d742d2015-10-24 17:41:29 +0000630 continue;
Tobias Grosser27d742d2015-10-24 17:41:29 +0000631
632 std::string Name = PHI->getName();
633 Value *ScalarAddr = getOrCreateScalarAlloca(PHI);
634 Value *Reload = Builder.CreateLoad(ScalarAddr, Name + ".ph.final_reload");
635 Reload = Builder.CreateBitOrPointerCast(Reload, PHI->getType());
636 Value *OriginalValue = PHI->getIncomingValueForBlock(MergeBB);
Michael Krusefaedfcb2016-03-03 17:20:43 +0000637 assert((!isa<Instruction>(OriginalValue) ||
638 cast<Instruction>(OriginalValue)->getParent() != MergeBB) &&
639 "Original value must no be one we just generated.");
Tobias Grosser27d742d2015-10-24 17:41:29 +0000640 auto *MergePHI = PHINode::Create(PHI->getType(), 2, Name + ".ph.merge");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +0000641 MergePHI->insertBefore(&*MergeBB->getFirstInsertionPt());
Tobias Grosser27d742d2015-10-24 17:41:29 +0000642 MergePHI->addIncoming(Reload, OptExitBB);
643 MergePHI->addIncoming(OriginalValue, ExitBB);
644 int Idx = PHI->getBasicBlockIndex(MergeBB);
645 PHI->setIncomingValue(Idx, MergePHI);
646 }
647}
648
Tobias Grosserffa24462015-10-17 08:54:13 +0000649void BlockGenerator::finalizeSCoP(Scop &S) {
650 findOutsideUsers(S);
Tobias Grosserc0091a72015-08-30 19:19:34 +0000651 createScalarInitialization(S);
Tobias Grosser27d742d2015-10-24 17:41:29 +0000652 createExitPHINodeMerges(S);
Johannes Doerfertef744432016-05-23 12:42:38 +0000653 createScalarFinalization(S);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000654}
655
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000656VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000657 std::vector<LoopToScevMapT> &VLTS,
658 isl_map *Schedule)
Tobias Grosserbc132602015-09-05 09:56:54 +0000659 : BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000660 assert(Schedule && "No statement domain provided");
661}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000662
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000663Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000664 ValueMapT &VectorMap,
665 VectorValueMapT &ScalarMaps,
666 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000667 if (Value *NewValue = VectorMap.lookup(Old))
668 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000669
670 int Width = getVectorWidth();
671
672 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
673
674 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000675 Vector = Builder.CreateInsertElement(
Tobias Grosserbc132602015-09-05 09:56:54 +0000676 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000677 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000678
679 VectorMap[Old] = Vector;
680
681 return Vector;
682}
683
684Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
685 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
686 assert(PointerTy && "PointerType expected");
687
688 Type *ScalarType = PointerTy->getElementType();
689 VectorType *VectorType = VectorType::get(ScalarType, Width);
690
691 return PointerType::getUnqual(VectorType);
692}
693
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000694Value *VectorBlockGenerator::generateStrideOneLoad(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000695 ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000696 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000697 unsigned VectorWidth = getVectorWidth();
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000698 auto *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000699 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
700 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
701
Michael Kruse8f25b0c2016-02-25 15:52:43 +0000702 Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[Offset],
703 VLTS[Offset], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000704 Value *VectorPtr =
705 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
706 LoadInst *VecLoad =
707 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000708 if (!Aligned)
709 VecLoad->setAlignment(8);
710
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000711 if (NegativeStride) {
712 SmallVector<Constant *, 16> Indices;
713 for (int i = VectorWidth - 1; i >= 0; i--)
714 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
715 Constant *SV = llvm::ConstantVector::get(Indices);
716 Value *RevVecLoad = Builder.CreateShuffleVector(
717 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
718 return RevVecLoad;
719 }
720
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000721 return VecLoad;
722}
723
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000724Value *VectorBlockGenerator::generateStrideZeroLoad(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000725 ScopStmt &Stmt, LoadInst *Load, ValueMapT &BBMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000726 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000727 auto *Pointer = Load->getPointerOperand();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000728 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Michael Kruse70131d32016-01-27 17:09:17 +0000729 Value *NewPointer =
730 generateLocationAccessed(Stmt, Load, BBMap, VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000731 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
732 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000733 LoadInst *ScalarLoad =
734 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000735
736 if (!Aligned)
737 ScalarLoad->setAlignment(8);
738
Tobias Grosserc14582f2013-02-05 18:01:29 +0000739 Constant *SplatVector = Constant::getNullValue(
740 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000741
Tobias Grosserc14582f2013-02-05 18:01:29 +0000742 Value *VectorLoad = Builder.CreateShuffleVector(
743 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000744 return VectorLoad;
745}
746
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000747Value *VectorBlockGenerator::generateUnknownStrideLoad(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000748 ScopStmt &Stmt, LoadInst *Load, VectorValueMapT &ScalarMaps,
Johannes Doerfert09e36972015-10-07 20:17:36 +0000749 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000750 int VectorWidth = getVectorWidth();
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000751 auto *Pointer = Load->getPointerOperand();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000752 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000753 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000754
755 Value *Vector = UndefValue::get(VectorType);
756
757 for (int i = 0; i < VectorWidth; i++) {
Michael Kruse70131d32016-01-27 17:09:17 +0000758 Value *NewPointer = generateLocationAccessed(Stmt, Load, ScalarMaps[i],
759 VLTS[i], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000760 Value *ScalarLoad =
761 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
762 Vector = Builder.CreateInsertElement(
763 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000764 }
765
766 return Vector;
767}
768
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000769void VectorBlockGenerator::generateLoad(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000770 ScopStmt &Stmt, LoadInst *Load, ValueMapT &VectorMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000771 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertc1db67e2015-09-29 23:47:21 +0000772 if (Value *PreloadLoad = GlobalMap.lookup(Load)) {
773 VectorMap[Load] = Builder.CreateVectorSplat(getVectorWidth(), PreloadLoad,
774 Load->getName() + "_p");
775 return;
776 }
777
Tobias Grosser28736452015-03-23 07:00:36 +0000778 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000779 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserbc132602015-09-05 09:56:54 +0000780 ScalarMaps[i][Load] =
781 generateScalarLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000782 return;
783 }
784
Tobias Grosser184a4922015-12-15 23:50:01 +0000785 const MemoryAccess &Access = Stmt.getArrayAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000786
Tobias Grosser95493982014-04-18 09:46:35 +0000787 // Make sure we have scalar values available to access the pointer to
788 // the data location.
789 extractScalarValues(Load, VectorMap, ScalarMaps);
790
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000791 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000792 if (Access.isStrideZero(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000793 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
Sebastian Popa00a0292012-12-18 07:46:06 +0000794 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000795 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000796 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000797 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000798 else
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000799 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000800
801 VectorMap[Load] = NewLoad;
802}
803
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000804void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000805 ValueMapT &VectorMap,
806 VectorValueMapT &ScalarMaps) {
807 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000808 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000809 ScalarMaps, getLoopForStmt(Stmt));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000810
811 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
812
813 const CastInst *Cast = dyn_cast<CastInst>(Inst);
814 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
815 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
816}
817
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000818void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000819 ValueMapT &VectorMap,
820 VectorValueMapT &ScalarMaps) {
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000821 Loop *L = getLoopForStmt(Stmt);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000822 Value *OpZero = Inst->getOperand(0);
823 Value *OpOne = Inst->getOperand(1);
824
825 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000826 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
827 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000828
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000829 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000830 Inst->getName() + "p_vec");
831 VectorMap[Inst] = NewInst;
832}
833
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000834void VectorBlockGenerator::copyStore(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000835 ScopStmt &Stmt, StoreInst *Store, ValueMapT &VectorMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000836 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser184a4922015-12-15 23:50:01 +0000837 const MemoryAccess &Access = Stmt.getArrayAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000838
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000839 auto *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000840 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +0000841 ScalarMaps, getLoopForStmt(Stmt));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000842
Tobias Grosser50fd7012014-04-17 23:13:49 +0000843 // Make sure we have scalar values available to access the pointer to
844 // the data location.
845 extractScalarValues(Store, VectorMap, ScalarMaps);
846
Sebastian Popa00a0292012-12-18 07:46:06 +0000847 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000848 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Michael Kruse70131d32016-01-27 17:09:17 +0000849 Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[0],
850 VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000851
Tobias Grosserc14582f2013-02-05 18:01:29 +0000852 Value *VectorPtr =
853 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000854 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
855
856 if (!Aligned)
857 Store->setAlignment(8);
858 } else {
859 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000860 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Michael Kruse70131d32016-01-27 17:09:17 +0000861 Value *NewPointer = generateLocationAccessed(Stmt, Store, ScalarMaps[i],
862 VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000863 Builder.CreateStore(Scalar, NewPointer);
864 }
865 }
866}
867
868bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
869 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000870 for (Value *Operand : Inst->operands())
871 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000872 return true;
873 return false;
874}
875
876bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
877 ValueMapT &VectorMap,
878 VectorValueMapT &ScalarMaps) {
879 bool HasVectorOperand = false;
880 int VectorWidth = getVectorWidth();
881
Tobias Grosser91f5b262014-06-04 08:06:40 +0000882 for (Value *Operand : Inst->operands()) {
883 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000884
885 if (VecOp == VectorMap.end())
886 continue;
887
888 HasVectorOperand = true;
889 Value *NewVector = VecOp->second;
890
891 for (int i = 0; i < VectorWidth; ++i) {
892 ValueMapT &SM = ScalarMaps[i];
893
894 // If there is one scalar extracted, all scalar elements should have
895 // already been extracted by the code here. So no need to check for the
Tobias Grosser2219d152016-08-03 05:28:09 +0000896 // existence of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000897 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000898 break;
899
Tobias Grosser91f5b262014-06-04 08:06:40 +0000900 SM[Operand] =
901 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000902 }
903 }
904
905 return HasVectorOperand;
906}
907
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000908void VectorBlockGenerator::copyInstScalarized(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000909 ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000910 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000911 bool HasVectorOperand;
912 int VectorWidth = getVectorWidth();
913
914 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
915
916 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000917 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Tobias Grosserbc132602015-09-05 09:56:54 +0000918 VLTS[VectorLane], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000919
920 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
921 return;
922
923 // Make the result available as vector value.
924 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
925 Value *Vector = UndefValue::get(VectorType);
926
927 for (int i = 0; i < VectorWidth; i++)
928 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
929 Builder.getInt32(i));
930
931 VectorMap[Inst] = Vector;
932}
933
Tobias Grosserbc132602015-09-05 09:56:54 +0000934int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000935
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000936void VectorBlockGenerator::copyInstruction(
Tobias Grosser2f1acac2015-10-04 10:18:45 +0000937 ScopStmt &Stmt, Instruction *Inst, ValueMapT &VectorMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000938 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000939 // Terminator instructions control the control flow. They are explicitly
940 // expressed in the clast and do not need to be copied.
941 if (Inst->isTerminator())
942 return;
943
Johannes Doerfert5dced262015-12-22 19:08:49 +0000944 if (canSyntheziseInStmt(Stmt, Inst))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000945 return;
946
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000947 if (auto *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000948 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000949 return;
950 }
951
952 if (hasVectorOperands(Inst, VectorMap)) {
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000953 if (auto *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000954 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000955 return;
956 }
957
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000958 if (auto *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000959 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000960 return;
961 }
962
Tobias Grosser9646e3f2015-10-04 10:18:39 +0000963 if (auto *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000964 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000965 return;
966 }
967
968 // Falltrough: We generate scalar instructions, if we don't know how to
969 // generate vector code.
970 }
971
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000972 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000973}
974
Tobias Grossera69d4f02015-12-15 23:49:58 +0000975void VectorBlockGenerator::generateScalarVectorLoads(
976 ScopStmt &Stmt, ValueMapT &VectorBlockMap) {
977 for (MemoryAccess *MA : Stmt) {
978 if (MA->isArrayKind() || MA->isWrite())
979 continue;
980
981 auto *Address = getOrCreateAlloca(*MA);
982 Type *VectorPtrType = getVectorPtrTy(Address, 1);
983 Value *VectorPtr = Builder.CreateBitCast(Address, VectorPtrType,
984 Address->getName() + "_p_vec_p");
985 auto *Val = Builder.CreateLoad(VectorPtr, Address->getName() + ".reload");
986 Constant *SplatVector = Constant::getNullValue(
987 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
988
989 Value *VectorVal = Builder.CreateShuffleVector(
990 Val, Val, SplatVector, Address->getName() + "_p_splat");
991 VectorBlockMap[MA->getBaseAddr()] = VectorVal;
Tobias Grossera69d4f02015-12-15 23:49:58 +0000992 }
993}
994
995void VectorBlockGenerator::verifyNoScalarStores(ScopStmt &Stmt) {
996 for (MemoryAccess *MA : Stmt) {
997 if (MA->isArrayKind() || MA->isRead())
998 continue;
999
1000 llvm_unreachable("Scalar stores not expected in vector loop");
1001 }
1002}
1003
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +00001004void VectorBlockGenerator::copyStmt(
1005 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +00001006 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
1007 "the vector block generator");
1008
Johannes Doerfertbe9c9112015-02-06 21:39:31 +00001009 BasicBlock *BB = Stmt.getBasicBlock();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001010 BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(),
1011 &*Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001012 CopyBB->setName("polly.stmt." + BB->getName());
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001013 Builder.SetInsertPoint(&CopyBB->front());
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001014
1015 // Create two maps that store the mapping from the original instructions of
1016 // the old basic block to their copies in the new basic block. Those maps
1017 // are basic block local.
1018 //
1019 // As vector code generation is supported there is one map for scalar values
1020 // and one for vector values.
1021 //
1022 // In case we just do scalar code generation, the vectorMap is not used and
1023 // the scalarMap has just one dimension, which contains the mapping.
1024 //
1025 // In case vector code generation is done, an instruction may either appear
1026 // in the vector map once (as it is calculating >vectorwidth< values at a
1027 // time. Or (if the values are calculated using scalar operations), it
1028 // appears once in every dimension of the scalarMap.
1029 VectorValueMapT ScalarBlockMap(getVectorWidth());
1030 ValueMapT VectorBlockMap;
1031
Tobias Grossera69d4f02015-12-15 23:49:58 +00001032 generateScalarVectorLoads(Stmt, VectorBlockMap);
1033
Tobias Grosser91f5b262014-06-04 08:06:40 +00001034 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +00001035 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
Tobias Grossera69d4f02015-12-15 23:49:58 +00001036
1037 verifyNoScalarStores(Stmt);
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001038}
Johannes Doerfert275a1752015-02-24 16:16:32 +00001039
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001040BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
1041 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001042
1043 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
1044 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
1045
1046 if (BBCopyIDom)
1047 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
1048
1049 return BBCopyIDom;
1050}
1051
Michael Krusef33c1252016-02-25 14:08:48 +00001052// This is to determine whether an llvm::Value (defined in @p BB) is usable when
1053// leaving a subregion. The straight-forward DT.dominates(BB, R->getExitBlock())
1054// does not work in cases where the exit block has edges from outside the
1055// region. In that case the llvm::Value would never be usable in in the exit
1056// block. The RegionGenerator however creates an new exit block ('ExitBBCopy')
1057// for the subregion's exiting edges only. We need to determine whether an
1058// llvm::Value is usable in there. We do this by checking whether it dominates
1059// all exiting blocks individually.
1060static bool isDominatingSubregionExit(const DominatorTree &DT, Region *R,
1061 BasicBlock *BB) {
1062 for (auto ExitingBB : predecessors(R->getExit())) {
1063 // Check for non-subregion incoming edges.
1064 if (!R->contains(ExitingBB))
1065 continue;
1066
1067 if (!DT.dominates(BB, ExitingBB))
1068 return false;
1069 }
1070
1071 return true;
1072}
1073
1074// Find the direct dominator of the subregion's exit block if the subregion was
1075// simplified.
1076static BasicBlock *findExitDominator(DominatorTree &DT, Region *R) {
1077 BasicBlock *Common = nullptr;
1078 for (auto ExitingBB : predecessors(R->getExit())) {
1079 // Check for non-subregion incoming edges.
1080 if (!R->contains(ExitingBB))
1081 continue;
1082
1083 // First exiting edge.
1084 if (!Common) {
1085 Common = ExitingBB;
1086 continue;
1087 }
1088
1089 Common = DT.findNearestCommonDominator(Common, ExitingBB);
1090 }
1091
1092 assert(Common && R->contains(Common));
1093 return Common;
1094}
1095
Tobias Grosserbc132602015-09-05 09:56:54 +00001096void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +00001097 isl_id_to_ast_expr *IdToAstExp) {
Johannes Doerfert275a1752015-02-24 16:16:32 +00001098 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +00001099 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +00001100
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001101 // Forget all old mappings.
1102 BlockMap.clear();
1103 RegionMaps.clear();
1104 IncompletePHINodeMap.clear();
1105
Michael Kruse225f0d12015-10-17 21:36:00 +00001106 // Collection of all values related to this subregion.
1107 ValueMapT ValueMap;
1108
Johannes Doerfert275a1752015-02-24 16:16:32 +00001109 // The region represented by the statement.
1110 Region *R = Stmt.getRegion();
1111
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001112 // Create a dedicated entry for the region where we can reload all demoted
1113 // inputs.
1114 BasicBlock *EntryBB = R->getEntry();
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001115 BasicBlock *EntryBBCopy = SplitBlock(Builder.GetInsertBlock(),
1116 &*Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001117 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001118 Builder.SetInsertPoint(&EntryBBCopy->front());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001119
Michael Krusec9937392015-11-09 23:33:40 +00001120 ValueMapT &EntryBBMap = RegionMaps[EntryBBCopy];
1121 generateScalarLoads(Stmt, EntryBBMap);
Michael Kruse225f0d12015-10-17 21:36:00 +00001122
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001123 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
1124 if (!R->contains(*PI))
1125 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +00001126
1127 // Iterate over all blocks in the region in a breadth-first search.
1128 std::deque<BasicBlock *> Blocks;
1129 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001130 Blocks.push_back(EntryBB);
1131 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001132
1133 while (!Blocks.empty()) {
1134 BasicBlock *BB = Blocks.front();
1135 Blocks.pop_front();
1136
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001137 // First split the block and update dominance information.
1138 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001139 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
1140
Michael Krusec9937392015-11-09 23:33:40 +00001141 // Get the mapping for this block and initialize it with either the scalar
1142 // loads from the generated entering block (which dominates all blocks of
1143 // this subregion) or the maps of the immediate dominator, if part of the
1144 // subregion. The latter necessarily includes the former.
1145 ValueMapT *InitBBMap;
1146 if (BBCopyIDom) {
1147 assert(RegionMaps.count(BBCopyIDom));
1148 InitBBMap = &RegionMaps[BBCopyIDom];
1149 } else
1150 InitBBMap = &EntryBBMap;
1151 auto Inserted = RegionMaps.insert(std::make_pair(BBCopy, *InitBBMap));
1152 ValueMapT &RegionMap = Inserted.first->second;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001153
Johannes Doerfert275a1752015-02-24 16:16:32 +00001154 // Copy the block with the BlockGenerator.
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001155 Builder.SetInsertPoint(&BBCopy->front());
Tobias Grosserbc132602015-09-05 09:56:54 +00001156 copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001157
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001158 // In order to remap PHI nodes we store also basic block mappings.
1159 BlockMap[BB] = BBCopy;
1160
1161 // Add values to incomplete PHI nodes waiting for this block to be copied.
1162 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
Tobias Grosserbc132602015-09-05 09:56:54 +00001163 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001164 IncompletePHINodeMap[BB].clear();
1165
Johannes Doerfert275a1752015-02-24 16:16:32 +00001166 // And continue with new successors inside the region.
1167 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1168 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1169 Blocks.push_back(*SI);
Michael Kruseebffcbe2015-11-09 22:37:29 +00001170
1171 // Remember value in case it is visible after this subregion.
Michael Krusef33c1252016-02-25 14:08:48 +00001172 if (isDominatingSubregionExit(DT, R, BB))
Michael Kruseebffcbe2015-11-09 22:37:29 +00001173 ValueMap.insert(RegionMap.begin(), RegionMap.end());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001174 }
1175
1176 // Now create a new dedicated region exit block and add it to the region map.
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001177 BasicBlock *ExitBBCopy = SplitBlock(Builder.GetInsertBlock(),
1178 &*Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001179 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001180 BlockMap[R->getExit()] = ExitBBCopy;
1181
Michael Krusef33c1252016-02-25 14:08:48 +00001182 BasicBlock *ExitDomBBCopy = BlockMap.lookup(findExitDominator(DT, R));
1183 assert(ExitDomBBCopy && "Common exit dominator must be within region; at "
1184 "least the entry node must match");
1185 DT.changeImmediateDominator(ExitBBCopy, ExitDomBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001186
1187 // As the block generator doesn't handle control flow we need to add the
1188 // region control flow by hand after all blocks have been copied.
1189 for (BasicBlock *BB : SeenBlocks) {
1190
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001191 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerferte114dc02015-09-14 11:15:58 +00001192 TerminatorInst *TI = BB->getTerminator();
1193 if (isa<UnreachableInst>(TI)) {
1194 while (!BBCopy->empty())
1195 BBCopy->begin()->eraseFromParent();
1196 new UnreachableInst(BBCopy->getContext(), BBCopy);
1197 continue;
1198 }
1199
Johannes Doerfert275a1752015-02-24 16:16:32 +00001200 Instruction *BICopy = BBCopy->getTerminator();
1201
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001202 ValueMapT &RegionMap = RegionMaps[BBCopy];
1203 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1204
Tobias Grosser45e79442015-08-01 09:07:57 +00001205 Builder.SetInsertPoint(BICopy);
Johannes Doerfert9a132f32015-09-28 09:33:22 +00001206 copyInstScalar(Stmt, TI, RegionMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001207 BICopy->eraseFromParent();
1208 }
1209
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001210 // Add counting PHI nodes to all loops in the region that can be used as
1211 // replacement for SCEVs refering to the old loop.
1212 for (BasicBlock *BB : SeenBlocks) {
1213 Loop *L = LI.getLoopFor(BB);
Tobias Grosserbc29e0b2015-11-12 07:34:09 +00001214 if (L == nullptr || L->getHeader() != BB || !R->contains(L))
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001215 continue;
1216
1217 BasicBlock *BBCopy = BlockMap[BB];
1218 Value *NullVal = Builder.getInt32(0);
1219 PHINode *LoopPHI =
1220 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1221 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1222 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001223 LoopPHI->insertBefore(&BBCopy->front());
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001224 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1225
1226 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1227 if (!R->contains(PredBB))
1228 continue;
1229 if (L->contains(PredBB))
1230 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1231 else
1232 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1233 }
1234
1235 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1236 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1237 LoopPHI->addIncoming(NullVal, PredBBCopy);
1238
1239 LTS[L] = SE.getUnknown(LoopPHI);
1240 }
1241
Michael Kruse225f0d12015-10-17 21:36:00 +00001242 // Continue generating code in the exit block.
Duncan P. N. Exon Smithb8f58b52015-11-06 22:56:54 +00001243 Builder.SetInsertPoint(&*ExitBBCopy->getFirstInsertionPt());
Michael Kruse225f0d12015-10-17 21:36:00 +00001244
1245 // Write values visible to other statements.
1246 generateScalarStores(Stmt, LTS, ValueMap);
Tobias Grosser3e956022015-10-26 20:41:53 +00001247 BlockMap.clear();
1248 RegionMaps.clear();
1249 IncompletePHINodeMap.clear();
Johannes Doerfert275a1752015-02-24 16:16:32 +00001250}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001251
Michael Kruseee6a4fc2016-01-26 13:33:27 +00001252PHINode *RegionGenerator::buildExitPHI(MemoryAccess *MA, LoopToScevMapT &LTS,
1253 ValueMapT &BBMap, Loop *L) {
1254 ScopStmt *Stmt = MA->getStatement();
1255 Region *SubR = Stmt->getRegion();
1256 auto Incoming = MA->getIncoming();
1257
1258 PollyIRBuilder::InsertPointGuard IPGuard(Builder);
1259 PHINode *OrigPHI = cast<PHINode>(MA->getAccessInstruction());
1260 BasicBlock *NewSubregionExit = Builder.GetInsertBlock();
1261
1262 // This can happen if the subregion is simplified after the ScopStmts
1263 // have been created; simplification happens as part of CodeGeneration.
1264 if (OrigPHI->getParent() != SubR->getExit()) {
1265 BasicBlock *FormerExit = SubR->getExitingBlock();
1266 if (FormerExit)
1267 NewSubregionExit = BlockMap.lookup(FormerExit);
1268 }
1269
1270 PHINode *NewPHI = PHINode::Create(OrigPHI->getType(), Incoming.size(),
1271 "polly." + OrigPHI->getName(),
1272 NewSubregionExit->getFirstNonPHI());
1273
1274 // Add the incoming values to the PHI.
1275 for (auto &Pair : Incoming) {
1276 BasicBlock *OrigIncomingBlock = Pair.first;
1277 BasicBlock *NewIncomingBlock = BlockMap.lookup(OrigIncomingBlock);
1278 Builder.SetInsertPoint(NewIncomingBlock->getTerminator());
1279 assert(RegionMaps.count(NewIncomingBlock));
1280 ValueMapT *LocalBBMap = &RegionMaps[NewIncomingBlock];
1281
1282 Value *OrigIncomingValue = Pair.second;
1283 Value *NewIncomingValue =
1284 getNewValue(*Stmt, OrigIncomingValue, *LocalBBMap, LTS, L);
1285 NewPHI->addIncoming(NewIncomingValue, NewIncomingBlock);
1286 }
1287
1288 return NewPHI;
1289}
1290
1291Value *RegionGenerator::getExitScalar(MemoryAccess *MA, LoopToScevMapT &LTS,
1292 ValueMapT &BBMap) {
1293 ScopStmt *Stmt = MA->getStatement();
1294
1295 // TODO: Add some test cases that ensure this is really the right choice.
1296 Loop *L = LI.getLoopFor(Stmt->getRegion()->getExit());
1297
1298 if (MA->isAnyPHIKind()) {
1299 auto Incoming = MA->getIncoming();
1300 assert(!Incoming.empty() &&
1301 "PHI WRITEs must have originate from at least one incoming block");
1302
1303 // If there is only one incoming value, we do not need to create a PHI.
1304 if (Incoming.size() == 1) {
1305 Value *OldVal = Incoming[0].second;
1306 return getNewValue(*Stmt, OldVal, BBMap, LTS, L);
1307 }
1308
1309 return buildExitPHI(MA, LTS, BBMap, L);
1310 }
1311
1312 // MK_Value accesses leaving the subregion must dominate the exit block; just
1313 // pass the copied value
1314 Value *OldVal = MA->getAccessValue();
1315 return getNewValue(*Stmt, OldVal, BBMap, LTS, L);
1316}
1317
Michael Kruse225f0d12015-10-17 21:36:00 +00001318void RegionGenerator::generateScalarStores(ScopStmt &Stmt, LoopToScevMapT &LTS,
Tobias Grosserbc132602015-09-05 09:56:54 +00001319 ValueMapT &BBMap) {
Tobias Grosser75296902015-08-21 19:23:21 +00001320 assert(Stmt.getRegion() &&
1321 "Block statements need to use the generateScalarStores() "
1322 "function in the BlockGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001323
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001324 for (MemoryAccess *MA : Stmt) {
Tobias Grossera535dff2015-12-13 19:59:01 +00001325 if (MA->isArrayKind() || MA->isRead())
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001326 continue;
1327
Michael Kruseee6a4fc2016-01-26 13:33:27 +00001328 Value *NewVal = getExitScalar(MA, LTS, BBMap);
1329 Value *Address = getOrCreateAlloca(*MA);
Michael Kruseeac97262016-02-24 22:08:14 +00001330 assert((!isa<Instruction>(NewVal) ||
1331 DT.dominates(cast<Instruction>(NewVal)->getParent(),
1332 Builder.GetInsertBlock())) &&
1333 "Domination violation");
1334 assert((!isa<Instruction>(Address) ||
1335 DT.dominates(cast<Instruction>(Address)->getParent(),
1336 Builder.GetInsertBlock())) &&
1337 "Domination violation");
Michael Kruseee6a4fc2016-01-26 13:33:27 +00001338 Builder.CreateStore(NewVal, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001339 }
1340}
1341
1342void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1343 PHINode *PHICopy, BasicBlock *IncomingBB,
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001344 LoopToScevMapT &LTS) {
1345 Region *StmtR = Stmt.getRegion();
1346
1347 // If the incoming block was not yet copied mark this PHI as incomplete.
1348 // Once the block will be copied the incoming value will be added.
1349 BasicBlock *BBCopy = BlockMap[IncomingBB];
1350 if (!BBCopy) {
1351 assert(StmtR->contains(IncomingBB) &&
1352 "Bad incoming block for PHI in non-affine region");
1353 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1354 return;
1355 }
1356
1357 Value *OpCopy = nullptr;
1358 if (StmtR->contains(IncomingBB)) {
1359 assert(RegionMaps.count(BBCopy) &&
1360 "Incoming PHI block did not have a BBMap");
1361 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1362
1363 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
Michael Krusedc122222015-10-19 09:19:25 +00001364
Johannes Doerfert6ba92712016-04-01 11:25:47 +00001365 // If the current insert block is different from the PHIs incoming block
1366 // change it, otherwise do not.
1367 auto IP = Builder.GetInsertPoint();
1368 if (IP->getParent() != BBCopy)
1369 Builder.SetInsertPoint(BBCopy->getTerminator());
Johannes Doerfert2c3ffc02016-02-16 12:36:14 +00001370 OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForStmt(Stmt));
Johannes Doerfert6ba92712016-04-01 11:25:47 +00001371 if (IP->getParent() != BBCopy)
1372 Builder.SetInsertPoint(&*IP);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001373 } else {
1374
1375 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1376 return;
1377
Tobias Grosserbc132602015-09-05 09:56:54 +00001378 Value *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001379 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1380 BlockMap[IncomingBB]->getTerminator());
1381 }
1382
1383 assert(OpCopy && "Incoming PHI value was not copied properly");
1384 assert(BBCopy && "Incoming PHI block was not copied properly");
1385 PHICopy->addIncoming(OpCopy, BBCopy);
1386}
1387
Tobias Grosser2b809d12016-02-21 15:44:34 +00001388void RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, PHINode *PHI,
1389 ValueMapT &BBMap,
1390 LoopToScevMapT &LTS) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001391 unsigned NumIncoming = PHI->getNumIncomingValues();
1392 PHINode *PHICopy =
1393 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1394 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1395 BBMap[PHI] = PHICopy;
1396
1397 for (unsigned u = 0; u < NumIncoming; u++)
Tobias Grosserbc132602015-09-05 09:56:54 +00001398 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001399}