Polly 23.0.0git
BlockGenerators.h
Go to the documentation of this file.
1//===-BlockGenerators.h - Helper to generate code for statements-*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the BlockGenerator and VectorBlockGenerator classes, which
10// generate sequential code and vectorized code for a polyhedral statement,
11// respectively.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef POLLY_BLOCK_GENERATORS_H
16#define POLLY_BLOCK_GENERATORS_H
17
20#include "llvm/Analysis/ScalarEvolutionExpressions.h"
22
23namespace polly {
24using llvm::AllocaInst;
25using llvm::ArrayRef;
26using llvm::AssertingVH;
27using llvm::BasicBlock;
28using llvm::BinaryOperator;
29using llvm::CmpInst;
30using llvm::DataLayout;
31using llvm::DenseMap;
32using llvm::DominatorTree;
33using llvm::Function;
34using llvm::Instruction;
35using llvm::LoadInst;
36using llvm::Loop;
37using llvm::LoopInfo;
38using llvm::MapVector;
39using llvm::PHINode;
40using llvm::ScalarEvolution;
41using llvm::SetVector;
42using llvm::SmallVector;
43using llvm::StoreInst;
44using llvm::StringRef;
45using llvm::Type;
46using llvm::UnaryInstruction;
47using llvm::Value;
48
49class MemoryAccess;
50class ScopArrayInfo;
51class IslExprBuilder;
52
53/// Generate a new basic block for a polyhedral statement.
55public:
56 typedef llvm::SmallVector<ValueMapT, 8> VectorValueMapT;
57
58 /// Map types to resolve scalar dependences.
59 ///
60 ///@{
61 using AllocaMapTy = DenseMap<const ScopArrayInfo *, AssertingVH<AllocaInst>>;
62
63 /// Simple vector of instructions to store escape users.
64 using EscapeUserVectorTy = SmallVector<Instruction *, 4>;
65
66 /// Map type to resolve escaping users for scalar instructions.
67 ///
68 /// @see The EscapeMap member.
70 MapVector<Instruction *,
71 std::pair<AssertingVH<Value>, EscapeUserVectorTy>>;
72
73 ///@}
74
75 /// Create a generator for basic blocks.
76 ///
77 /// @param Builder The LLVM-IR Builder used to generate the statement. The
78 /// code is generated at the location, the Builder points
79 /// to.
80 /// @param LI The loop info for the current function
81 /// @param SE The scalar evolution info for the current function
82 /// @param DT The dominator tree of this function.
83 /// @param ScalarMap Map from scalars to their demoted location.
84 /// @param EscapeMap Map from scalars to their escape users and locations.
85 /// @param GlobalMap A mapping from llvm::Values used in the original scop
86 /// region to a new set of llvm::Values. Each reference to
87 /// an original value appearing in this mapping is replaced
88 /// with the new value it is mapped to.
89 /// @param ExprBuilder An expression builder to generate new access functions.
90 /// @param StartBlock The first basic block after the RTC.
91 BlockGenerator(PollyIRBuilder &Builder, LoopInfo &LI, ScalarEvolution &SE,
92 DominatorTree &DT, AllocaMapTy &ScalarMap,
95
96 /// Copy the basic block.
97 ///
98 /// This copies the entire basic block and updates references to old values
99 /// with references to new values, as defined by GlobalMap.
100 ///
101 /// @param Stmt The block statement to code generate.
102 /// @param LTS A map from old loops to new induction variables as
103 /// SCEVs.
104 /// @param NewAccesses A map from memory access ids to new ast expressions,
105 /// which may contain new access expressions for certain
106 /// memory accesses.
107 void copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
108 isl_id_to_ast_expr *NewAccesses);
109
110 /// Remove a ScopArrayInfo's allocation from the ScalarMap.
111 ///
112 /// This function allows to remove values from the ScalarMap. This is useful
113 /// if the corresponding alloca instruction will be deleted (or moved into
114 /// another module), as without removing these values the underlying
115 /// AssertingVH will trigger due to us still keeping reference to this
116 /// scalar.
117 ///
118 /// @param Array The array for which the alloca was generated.
120
121 /// Return the alloca for @p Access.
122 ///
123 /// If no alloca was mapped for @p Access a new one is created.
124 ///
125 /// @param Access The memory access for which to generate the alloca.
126 ///
127 /// @returns The alloca for @p Access or a replacement value taken from
128 /// GlobalMap.
129 Value *getOrCreateAlloca(const MemoryAccess &Access);
130
131 /// Return the alloca for @p Array.
132 ///
133 /// If no alloca was mapped for @p Array a new one is created.
134 ///
135 /// @param Array The array for which to generate the alloca.
136 ///
137 /// @returns The alloca for @p Array or a replacement value taken from
138 /// GlobalMap.
140
141 /// Finalize the code generation for the SCoP @p S.
142 ///
143 /// This will initialize and finalize the scalar variables we demoted during
144 /// the code generation.
145 ///
146 /// @see createScalarInitialization(Scop &)
147 /// @see createScalarFinalization(Region &)
148 void finalizeSCoP(Scop &S);
149
150 /// An empty destructor
151 virtual ~BlockGenerator() {}
152
153 BlockGenerator(const BlockGenerator &) = default;
154
155protected:
157 LoopInfo &LI;
158 ScalarEvolution &SE;
160
161 /// The dominator tree of this function.
162 DominatorTree &DT;
163
164 /// Relates to the region where the code is emitted into.
165 /// @{
166 DominatorTree *GenDT;
167 LoopInfo *GenLI;
168 ScalarEvolution *GenSE;
169 /// @}
170
171public:
172 /// Map to resolve scalar dependences for PHI operands and scalars.
173 ///
174 /// When translating code that contains scalar dependences as they result from
175 /// inter-block scalar dependences (including the use of data carrying PHI
176 /// nodes), we do not directly regenerate in-register SSA code, but instead
177 /// allocate some stack memory through which these scalar values are passed.
178 /// Only a later pass of -mem2reg will then (re)introduce in-register
179 /// computations.
180 ///
181 /// To keep track of the memory location(s) used to store the data computed by
182 /// a given SSA instruction, we use the map 'ScalarMap'. ScalarMap maps a
183 /// given ScopArrayInfo to the junk of stack allocated memory, that is
184 /// used for code generation.
185 ///
186 /// Up to two different ScopArrayInfo objects are associated with each
187 /// llvm::Value:
188 ///
189 /// MemoryType::Value objects are used for normal scalar dependences that go
190 /// from a scalar definition to its use. Such dependences are lowered by
191 /// directly writing the value an instruction computes into the corresponding
192 /// chunk of memory and reading it back from this chunk of memory right before
193 /// every use of this original scalar value. The memory allocations for
194 /// MemoryType::Value objects end with '.s2a'.
195 ///
196 /// MemoryType::PHI (and MemoryType::ExitPHI) objects are used to model PHI
197 /// nodes. For each PHI nodes we introduce, besides the Array of type
198 /// MemoryType::Value, a second chunk of memory into which we write at the end
199 /// of each basic block preceding the PHI instruction the value passed
200 /// through this basic block. At the place where the PHI node is executed, we
201 /// replace the PHI node with a load from the corresponding MemoryType::PHI
202 /// memory location. The memory allocations for MemoryType::PHI end with
203 /// '.phiops'.
204 ///
205 /// Example:
206 ///
207 /// Input C Code
208 /// ============
209 ///
210 /// S1: x1 = ...
211 /// for (i=0...N) {
212 /// S2: x2 = phi(x1, add)
213 /// S3: add = x2 + 42;
214 /// }
215 /// S4: print(x1)
216 /// print(x2)
217 /// print(add)
218 ///
219 ///
220 /// Unmodified IR IR After expansion
221 /// ============= ==================
222 ///
223 /// S1: x1 = ... S1: x1 = ...
224 /// x1.s2a = s1
225 /// x2.phiops = s1
226 /// | |
227 /// | <--<--<--<--< | <--<--<--<--<
228 /// | / \ | / \ .
229 /// V V \ V V \ .
230 /// S2: x2 = phi (x1, add) | S2: x2 = x2.phiops |
231 /// | x2.s2a = x2 |
232 /// | |
233 /// S3: add = x2 + 42 | S3: add = x2 + 42 |
234 /// | add.s2a = add |
235 /// | x2.phiops = add |
236 /// | \ / | \ /
237 /// | \ / | \ /
238 /// | >-->-->-->--> | >-->-->-->-->
239 /// V V
240 ///
241 /// S4: x1 = x1.s2a
242 /// S4: ... = x1 ... = x1
243 /// x2 = x2.s2a
244 /// ... = x2 ... = x2
245 /// add = add.s2a
246 /// ... = add ... = add
247 ///
248 /// ScalarMap = { x1:Value -> x1.s2a, x2:Value -> x2.s2a,
249 /// add:Value -> add.s2a, x2:PHI -> x2.phiops }
250 ///
251 /// ??? Why does a PHI-node require two memory chunks ???
252 ///
253 /// One may wonder why a PHI node requires two memory chunks and not just
254 /// all data is stored in a single location. The following example tries
255 /// to store all data in .s2a and drops the .phiops location:
256 ///
257 /// S1: x1 = ...
258 /// x1.s2a = s1
259 /// x2.s2a = s1 // use .s2a instead of .phiops
260 /// |
261 /// | <--<--<--<--<
262 /// | / \ .
263 /// V V \ .
264 /// S2: x2 = x2.s2a | // value is same as above, but read
265 /// | // from .s2a
266 /// |
267 /// x2.s2a = x2 | // store into .s2a as normal
268 /// |
269 /// S3: add = x2 + 42 |
270 /// add.s2a = add |
271 /// x2.s2a = add | // use s2a instead of .phiops
272 /// | \ / // !!! This is wrong, as x2.s2a now
273 /// | >-->-->-->--> // contains add instead of x2.
274 /// V
275 ///
276 /// S4: x1 = x1.s2a
277 /// ... = x1
278 /// x2 = x2.s2a // !!! We now read 'add' instead of
279 /// ... = x2 // 'x2'
280 /// add = add.s2a
281 /// ... = add
282 ///
283 /// As visible in the example, the SSA value of the PHI node may still be
284 /// needed _after_ the basic block, which could conceptually branch to the
285 /// PHI node, has been run and has overwritten the PHI's old value. Hence, a
286 /// single memory location is not enough to code-generate a PHI node.
287 ///
288 /// Memory locations used for the special PHI node modeling.
290
291 /// Map from instructions to their escape users as well as the alloca.
293
294 /// A map from llvm::Values referenced in the old code to a new set of
295 /// llvm::Values, which is used to replace these old values during
296 /// code generation.
298
299 /// The first basic block after the RTC.
300 BasicBlock *StartBlock;
301
302 /// Split @p BB to create a new one we can use to clone @p BB in.
303 BasicBlock *splitBB(BasicBlock *BB);
304
305 /// Change the function that code is emitted into.
306 void switchGeneratedFunc(Function *GenFn, DominatorTree *GenDT,
307 LoopInfo *GenLI, ScalarEvolution *GenSE);
308
309 /// Copy the given basic block.
310 ///
311 /// @param Stmt The statement to code generate.
312 /// @param BB The basic block to code generate.
313 /// @param BBMap A mapping from old values to their new values in this
314 /// block.
315 /// @param LTS A map from old loops to new induction variables as
316 /// SCEVs.
317 /// @param NewAccesses A map from memory access ids to new ast expressions,
318 /// which may contain new access expressions for certain
319 /// memory accesses.
320 ///
321 /// @returns The copy of the basic block.
322 BasicBlock *copyBB(ScopStmt &Stmt, BasicBlock *BB, ValueMapT &BBMap,
323 LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses);
324
325 /// Copy the given basic block.
326 ///
327 /// @param Stmt The statement to code generate.
328 /// @param BB The basic block to code generate.
329 /// @param BBCopy The new basic block to generate code in.
330 /// @param BBMap A mapping from old values to their new values in this
331 /// block.
332 /// @param LTS A map from old loops to new induction variables as
333 /// SCEVs.
334 /// @param NewAccesses A map from memory access ids to new ast expressions,
335 /// which may contain new access expressions for certain
336 /// memory accesses.
337 void copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *BBCopy,
338 ValueMapT &BBMap, LoopToScevMapT &LTS,
339 isl_id_to_ast_expr *NewAccesses);
340
341 /// Generate reload of scalars demoted to memory and needed by @p Stmt.
342 ///
343 /// @param Stmt The statement we generate code for.
344 /// @param LTS A mapping from loops virtual canonical induction
345 /// variable to their new values.
346 /// @param BBMap A mapping from old values to their new values in this block.
347 /// @param NewAccesses A map from memory access ids to new ast expressions.
349 ValueMapT &BBMap,
350 __isl_keep isl_id_to_ast_expr *NewAccesses);
351
352 /// When statement tracing is enabled, build the print instructions for
353 /// printing the current statement instance.
354 ///
355 /// The printed output looks like:
356 ///
357 /// Stmt1(0)
358 ///
359 /// If printing of scalars is enabled, it also appends the value of each
360 /// scalar to the line:
361 ///
362 /// Stmt1(0) %i=1 %sum=5
363 ///
364 /// @param Stmt The statement we generate code for.
365 /// @param LTS A mapping from loops virtual canonical induction
366 /// variable to their new values.
367 /// @param BBMap A mapping from old values to their new values in this block.
369 ValueMapT &BBMap);
370
371 /// Generate instructions that compute whether one instance of @p Set is
372 /// executed.
373 ///
374 /// @param Stmt The statement we generate code for.
375 /// @param Subdomain A set in the space of @p Stmt's domain. Elements not in
376 /// @p Stmt's domain are ignored.
377 ///
378 /// @return An expression of type i1, generated into the current builder
379 /// position, that evaluates to 1 if the executed instance is part of
380 /// @p Set.
381 Value *buildContainsCondition(ScopStmt &Stmt, const isl::set &Subdomain);
382
383 /// Generate code that executes in a subset of @p Stmt's domain.
384 ///
385 /// @param Stmt The statement we generate code for.
386 /// @param Subdomain The condition for some code to be executed.
387 /// @param Subject A name for the code that is executed
388 /// conditionally. Used to name new basic blocks and
389 /// instructions.
390 /// @param GenThenFunc Callback which generates the code to be executed
391 /// when the current executed instance is in @p Set. The
392 /// IRBuilder's position is moved to within the block that
393 /// executes conditionally for this callback.
394 void generateConditionalExecution(ScopStmt &Stmt, const isl::set &Subdomain,
395 StringRef Subject,
396 const std::function<void()> &GenThenFunc);
397
398 /// Generate the scalar stores for the given statement.
399 ///
400 /// After the statement @p Stmt was copied all inner-SCoP scalar dependences
401 /// starting in @p Stmt (hence all scalar write accesses in @p Stmt) need to
402 /// be demoted to memory.
403 ///
404 /// @param Stmt The statement we generate code for.
405 /// @param LTS A mapping from loops virtual canonical induction
406 /// variable to their new values
407 /// (for values recalculated in the new ScoP, but not
408 /// within this basic block)
409 /// @param BBMap A mapping from old values to their new values in this block.
410 /// @param NewAccesses A map from memory access ids to new ast expressions.
411 virtual void generateScalarStores(ScopStmt &Stmt, LoopToScevMapT &LTS,
412 ValueMapT &BBMap,
413 __isl_keep isl_id_to_ast_expr *NewAccesses);
414
415 /// Handle users of @p Array outside the SCoP.
416 ///
417 /// @param S The current SCoP.
418 /// @param Inst The ScopArrayInfo to handle.
420
421 /// Find scalar statements that have outside users.
422 ///
423 /// We register these scalar values to later update subsequent scalar uses of
424 /// these values to either use the newly computed value from within the scop
425 /// (if the scop was executed) or the unchanged original code (if the run-time
426 /// check failed).
427 ///
428 /// @param S The scop for which to find the outside users.
429 void findOutsideUsers(Scop &S);
430
431 /// Initialize the memory of demoted scalars.
432 ///
433 /// @param S The scop for which to generate the scalar initializers.
435
436 /// Create exit PHI node merges for PHI nodes with more than two edges
437 /// from inside the scop.
438 ///
439 /// For scops which have a PHI node in the exit block that has more than two
440 /// incoming edges from inside the scop region, we require some special
441 /// handling to understand which of the possible values will be passed to the
442 /// PHI node from inside the optimized version of the scop. To do so ScopInfo
443 /// models the possible incoming values as write accesses of the ScopStmts.
444 ///
445 /// This function creates corresponding code to reload the computed outgoing
446 /// value from the stack slot it has been stored into and to pass it on to the
447 /// PHI node in the original exit block.
448 ///
449 /// @param S The scop for which to generate the exiting PHI nodes.
451
452 /// Promote the values of demoted scalars after the SCoP.
453 ///
454 /// If a scalar value was used outside the SCoP we need to promote the value
455 /// stored in the memory cell allocated for that scalar and combine it with
456 /// the original value in the non-optimized SCoP.
458
459 /// Try to synthesize a new value
460 ///
461 /// Given an old value, we try to synthesize it in a new context from its
462 /// original SCEV expression. We start from the original SCEV expression,
463 /// then replace outdated parameter and loop references, and finally
464 /// expand it to code that computes this updated expression.
465 ///
466 /// @param Stmt The statement to code generate
467 /// @param Old The old Value
468 /// @param BBMap A mapping from old values to their new values
469 /// (for values recalculated within this basic block)
470 /// @param LTS A mapping from loops virtual canonical induction
471 /// variable to their new values
472 /// (for values recalculated in the new ScoP, but not
473 /// within this basic block)
474 /// @param L The loop that surrounded the instruction that referenced
475 /// this value in the original code. This loop is used to
476 /// evaluate the scalar evolution at the right scope.
477 ///
478 /// @returns o A newly synthesized value.
479 /// o NULL, if synthesizing the value failed.
481 LoopToScevMapT &LTS, Loop *L) const;
482
483 /// Get the new version of a value.
484 ///
485 /// Given an old value, we first check if a new version of this value is
486 /// available in the BBMap or GlobalMap. In case it is not and the value can
487 /// be recomputed using SCEV, we do so. If we can not recompute a value
488 /// using SCEV, but we understand that the value is constant within the scop,
489 /// we return the old value. If the value can still not be derived, this
490 /// function will assert.
491 ///
492 /// @param Stmt The statement to code generate.
493 /// @param Old The old Value.
494 /// @param BBMap A mapping from old values to their new values
495 /// (for values recalculated within this basic block).
496 /// @param LTS A mapping from loops virtual canonical induction
497 /// variable to their new values
498 /// (for values recalculated in the new ScoP, but not
499 /// within this basic block).
500 /// @param L The loop that surrounded the instruction that referenced
501 /// this value in the original code. This loop is used to
502 /// evaluate the scalar evolution at the right scope.
503 ///
504 /// @returns o The old value, if it is still valid.
505 /// o The new value, if available.
506 /// o NULL, if no value is found.
507 Value *getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap,
508 LoopToScevMapT &LTS, Loop *L) const;
509
510 void copyInstScalar(ScopStmt &Stmt, Instruction *Inst, ValueMapT &BBMap,
511 LoopToScevMapT &LTS);
512
513 /// Get the innermost loop that surrounds the statement @p Stmt.
514 Loop *getLoopForStmt(const ScopStmt &Stmt) const;
515
516 /// Generate the operand address
517 /// @param NewAccesses A map from memory access ids to new ast expressions,
518 /// which may contain new access expressions for certain
519 /// memory accesses.
521 ValueMapT &BBMap, LoopToScevMapT &LTS,
522 isl_id_to_ast_expr *NewAccesses);
523
524 /// Generate the operand address.
525 ///
526 /// @param Stmt The statement to generate code for.
527 /// @param L The innermost loop that surrounds the statement.
528 /// @param Pointer If the access expression is not changed (ie. not found
529 /// in @p LTS), use this Pointer from the original code
530 /// instead.
531 /// @param BBMap A mapping from old values to their new values.
532 /// @param LTS A mapping from loops virtual canonical induction
533 /// variable to their new values.
534 /// @param NewAccesses Ahead-of-time generated access expressions.
535 /// @param Id Identifier of the MemoryAccess to generate.
536 /// @param ExpectedType The type the returned value should have.
537 ///
538 /// @return The generated address.
539 Value *generateLocationAccessed(ScopStmt &Stmt, Loop *L, Value *Pointer,
540 ValueMapT &BBMap, LoopToScevMapT &LTS,
541 isl_id_to_ast_expr *NewAccesses,
542 __isl_take isl_id *Id, Type *ExpectedType);
543
544 /// Generate the pointer value that is accesses by @p Access.
545 ///
546 /// For write accesses, generate the target address. For read accesses,
547 /// generate the source address.
548 /// The access can be either an array access or a scalar access. In the first
549 /// case, the returned address will point to an element into that array. In
550 /// the scalar case, an alloca is used.
551 /// If a new AccessRelation is set for the MemoryAccess, the new relation will
552 /// be used.
553 ///
554 /// @param Access The access to generate a pointer for.
555 /// @param L The innermost loop that surrounds the statement.
556 /// @param LTS A mapping from loops virtual canonical induction
557 /// variable to their new values.
558 /// @param BBMap A mapping from old values to their new values.
559 /// @param NewAccesses A map from memory access ids to new ast expressions.
560 ///
561 /// @return The generated address.
562 Value *getImplicitAddress(MemoryAccess &Access, Loop *L, LoopToScevMapT &LTS,
563 ValueMapT &BBMap,
564 __isl_keep isl_id_to_ast_expr *NewAccesses);
565
566 /// @param NewAccesses A map from memory access ids to new ast expressions,
567 /// which may contain new access expressions for certain
568 /// memory accesses.
569 Value *generateArrayLoad(ScopStmt &Stmt, LoadInst *load, ValueMapT &BBMap,
570 LoopToScevMapT &LTS,
571 isl_id_to_ast_expr *NewAccesses);
572
573 /// @param NewAccesses A map from memory access ids to new ast expressions,
574 /// which may contain new access expressions for certain
575 /// memory accesses.
576 void generateArrayStore(ScopStmt &Stmt, StoreInst *store, ValueMapT &BBMap,
577 LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses);
578
579 /// Copy a single PHI instruction.
580 ///
581 /// The implementation in the BlockGenerator is trivial, however it allows
582 /// subclasses to handle PHIs different.
583 virtual void copyPHIInstruction(ScopStmt &, PHINode *, ValueMapT &,
584 LoopToScevMapT &) {}
585
586 /// Copy a single Instruction.
587 ///
588 /// This copies a single Instruction and updates references to old values
589 /// with references to new values, as defined by GlobalMap and BBMap.
590 ///
591 /// @param Stmt The statement to code generate.
592 /// @param Inst The instruction to copy.
593 /// @param BBMap A mapping from old values to their new values
594 /// (for values recalculated within this basic block).
595 /// @param GlobalMap A mapping from old values to their new values
596 /// (for values recalculated in the new ScoP, but not
597 /// within this basic block).
598 /// @param LTS A mapping from loops virtual canonical induction
599 /// variable to their new values
600 /// (for values recalculated in the new ScoP, but not
601 /// within this basic block).
602 /// @param NewAccesses A map from memory access ids to new ast expressions,
603 /// which may contain new access expressions for certain
604 /// memory accesses.
605 void copyInstruction(ScopStmt &Stmt, Instruction *Inst, ValueMapT &BBMap,
606 LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses);
607
608 /// Helper to determine if @p Inst can be synthesized in @p Stmt.
609 ///
610 /// @returns false, iff @p Inst can be synthesized in @p Stmt.
611 bool canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst);
612
613 /// Remove dead instructions generated for BB
614 ///
615 /// @param BB The basic block code for which code has been generated.
616 /// @param BBMap A local map from old to new instructions.
617 void removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap);
618
619 /// Invalidate the scalar evolution expressions for a scop.
620 ///
621 /// This function invalidates the scalar evolution results for all
622 /// instructions that are part of a given scop, and the loops
623 /// surrounding the users of merge blocks. This is necessary to ensure that
624 /// later scops do not obtain scalar evolution expressions that reference
625 /// values that earlier dominated the later scop, but have been moved in the
626 /// conditional part of an earlier scop and consequently do not any more
627 /// dominate the later scop.
628 ///
629 /// @param S The scop to invalidate.
631};
632
633/// Generator for new versions of polyhedral region statements.
634class RegionGenerator final : public BlockGenerator {
635public:
636 /// Create a generator for regions.
637 ///
638 /// @param BlockGen A generator for basic blocks.
640
641 virtual ~RegionGenerator() {}
642
643 /// Copy the region statement @p Stmt.
644 ///
645 /// This copies the entire region represented by @p Stmt and updates
646 /// references to old values with references to new values, as defined by
647 /// GlobalMap.
648 ///
649 /// @param Stmt The statement to code generate.
650 /// @param LTS A map from old loops to new induction variables as SCEVs.
651 void copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS,
652 __isl_keep isl_id_to_ast_expr *IdToAstExp);
653
654private:
655 /// A map from old to the first new block in the region, that was created to
656 /// model the old basic block.
657 DenseMap<BasicBlock *, BasicBlock *> StartBlockMap;
658
659 /// A map from old to the last new block in the region, that was created to
660 /// model the old basic block.
661 DenseMap<BasicBlock *, BasicBlock *> EndBlockMap;
662
663 /// The "BBMaps" for the whole region (one for each block). In case a basic
664 /// block is code generated to multiple basic blocks (e.g., for partial
665 /// writes), the StartBasic is used as index for the RegionMap.
666 DenseMap<BasicBlock *, ValueMapT> RegionMaps;
667
668 /// Mapping to remember PHI nodes that still need incoming values.
669 using PHINodePairTy = std::pair<PHINode *, PHINode *>;
670 DenseMap<BasicBlock *, SmallVector<PHINodePairTy, 4>> IncompletePHINodeMap;
671
672 /// Repair the dominance tree after we created a copy block for @p BB.
673 ///
674 /// @returns The immediate dominator in the DT for @p BBCopy if in the region.
675 BasicBlock *repairDominance(BasicBlock *BB, BasicBlock *BBCopy);
676
677 /// Add the new operand from the copy of @p IncomingBB to @p PHICopy.
678 ///
679 /// PHI nodes, which may have (multiple) edges that enter from outside the
680 /// non-affine subregion and even from outside the scop, are code generated as
681 /// follows:
682 ///
683 /// # Original
684 ///
685 /// Region: %A-> %exit
686 /// NonAffine Stmt: %nonaffB -> %D (includes %nonaffB, %nonaffC)
687 ///
688 /// pre:
689 /// %val = add i64 1, 1
690 ///
691 /// A:
692 /// br label %nonaff
693 ///
694 /// nonaffB:
695 /// %phi = phi i64 [%val, %A], [%valC, %nonAffC], [%valD, %D]
696 /// %cmp = <nonaff>
697 /// br i1 %cmp, label %C, label %nonaffC
698 ///
699 /// nonaffC:
700 /// %valC = add i64 1, 1
701 /// br i1 undef, label %D, label %nonaffB
702 ///
703 /// D:
704 /// %valD = ...
705 /// %exit_cond = <loopexit>
706 /// br i1 %exit_cond, label %nonaffB, label %exit
707 ///
708 /// exit:
709 /// ...
710 ///
711 /// - %start and %C enter from outside the non-affine region.
712 /// - %nonaffC enters from within the non-affine region.
713 ///
714 /// # New
715 ///
716 /// polly.A:
717 /// store i64 %val, i64* %phi.phiops
718 /// br label %polly.nonaffA.entry
719 ///
720 /// polly.nonaffB.entry:
721 /// %phi.phiops.reload = load i64, i64* %phi.phiops
722 /// br label %nonaffB
723 ///
724 /// polly.nonaffB:
725 /// %polly.phi = [%phi.phiops.reload, %nonaffB.entry],
726 /// [%p.valC, %polly.nonaffC]
727 ///
728 /// polly.nonaffC:
729 /// %p.valC = add i64 1, 1
730 /// br i1 undef, label %polly.D, label %polly.nonaffB
731 ///
732 /// polly.D:
733 /// %p.valD = ...
734 /// store i64 %p.valD, i64* %phi.phiops
735 /// %p.exit_cond = <loopexit>
736 /// br i1 %p.exit_cond, label %polly.nonaffB, label %exit
737 ///
738 /// Values that enter the PHI from outside the non-affine region are stored
739 /// into the stack slot %phi.phiops by statements %polly.A and %polly.D and
740 /// reloaded in %polly.nonaffB.entry, a basic block generated before the
741 /// actual non-affine region.
742 ///
743 /// When generating the PHI node of the non-affine region in %polly.nonaffB,
744 /// incoming edges from outside the region are combined into a single branch
745 /// from %polly.nonaffB.entry which has as incoming value the value reloaded
746 /// from the %phi.phiops stack slot. Incoming edges from within the region
747 /// refer to the copied instructions (%p.valC) and basic blocks
748 /// (%polly.nonaffC) of the non-affine region.
749 ///
750 /// @param Stmt The statement to code generate.
751 /// @param PHI The original PHI we copy.
752 /// @param PHICopy The copy of @p PHI.
753 /// @param IncomingBB An incoming block of @p PHI.
754 /// @param LTS A map from old loops to new induction variables as
755 /// SCEVs.
756 void addOperandToPHI(ScopStmt &Stmt, PHINode *PHI, PHINode *PHICopy,
757 BasicBlock *IncomingBB, LoopToScevMapT &LTS);
758
759 /// Create a PHI that combines the incoming values from all incoming blocks
760 /// that are in the subregion.
761 ///
762 /// PHIs in the subregion's exit block can have incoming edges from within and
763 /// outside the subregion. This function combines the incoming values from
764 /// within the subregion to appear as if there is only one incoming edge from
765 /// the subregion (an additional exit block is created by RegionGenerator).
766 /// This is to avoid that a value is written to the .phiops location without
767 /// leaving the subregion because the exiting block as an edge back into the
768 /// subregion.
769 ///
770 /// @param MA The WRITE of MemoryKind::PHI/MemoryKind::ExitPHI for a PHI in
771 /// the subregion's exit block.
772 /// @param LTS Virtual induction variable mapping.
773 /// @param BBMap A mapping from old values to their new values in this block.
774 /// @param L Loop surrounding this region statement.
775 ///
776 /// @returns The constructed PHI node.
777 PHINode *buildExitPHI(MemoryAccess *MA, LoopToScevMapT &LTS, ValueMapT &BBMap,
778 Loop *L);
779
780 /// @param Return the new value of a scalar write, creating a PHINode if
781 /// necessary.
782 ///
783 /// @param MA A scalar WRITE MemoryAccess.
784 /// @param LTS Virtual induction variable mapping.
785 /// @param BBMap A mapping from old values to their new values in this block.
786 ///
787 /// @returns The effective value of @p MA's written value when leaving the
788 /// subregion.
789 /// @see buildExitPHI
791
792 /// Generate the scalar stores for the given statement.
793 ///
794 /// After the statement @p Stmt was copied all inner-SCoP scalar dependences
795 /// starting in @p Stmt (hence all scalar write accesses in @p Stmt) need to
796 /// be demoted to memory.
797 ///
798 /// @param Stmt The statement we generate code for.
799 /// @param LTS A mapping from loops virtual canonical induction variable to
800 /// their new values (for values recalculated in the new ScoP,
801 /// but not within this basic block)
802 /// @param BBMap A mapping from old values to their new values in this block.
803 /// @param LTS A mapping from loops virtual canonical induction variable to
804 /// their new values.
805 void
807 __isl_keep isl_id_to_ast_expr *NewAccesses) override;
808
809 /// Copy a single PHI instruction.
810 ///
811 /// This copies a single PHI instruction and updates references to old values
812 /// with references to new values, as defined by GlobalMap and BBMap.
813 ///
814 /// @param Stmt The statement to code generate.
815 /// @param PHI The PHI instruction to copy.
816 /// @param BBMap A mapping from old values to their new values
817 /// (for values recalculated within this basic block).
818 /// @param LTS A map from old loops to new induction variables as SCEVs.
819 void copyPHIInstruction(ScopStmt &Stmt, PHINode *Inst, ValueMapT &BBMap,
820 LoopToScevMapT &LTS) override;
821};
822} // namespace polly
823#endif
virtual ~BlockGenerator()
An empty destructor.
Loop * getLoopForStmt(const ScopStmt &Stmt) const
Get the innermost loop that surrounds the statement Stmt.
EscapeUsersAllocaMapTy & EscapeMap
Map from instructions to their escape users as well as the alloca.
Value * getImplicitAddress(MemoryAccess &Access, Loop *L, LoopToScevMapT &LTS, ValueMapT &BBMap, __isl_keep isl_id_to_ast_expr *NewAccesses)
Generate the pointer value that is accesses by Access.
DominatorTree & DT
The dominator tree of this function.
llvm::SmallVector< ValueMapT, 8 > VectorValueMapT
BasicBlock * splitBB(BasicBlock *BB)
Split BB to create a new one we can use to clone BB in.
void generateBeginStmtTrace(ScopStmt &Stmt, LoopToScevMapT &LTS, ValueMapT &BBMap)
When statement tracing is enabled, build the print instructions for printing the current statement in...
Value * trySynthesizeNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap, LoopToScevMapT &LTS, Loop *L) const
Try to synthesize a new value.
void generateScalarLoads(ScopStmt &Stmt, LoopToScevMapT &LTS, ValueMapT &BBMap, __isl_keep isl_id_to_ast_expr *NewAccesses)
Generate reload of scalars demoted to memory and needed by Stmt.
AllocaMapTy & ScalarMap
Map to resolve scalar dependences for PHI operands and scalars.
PollyIRBuilder & Builder
DenseMap< const ScopArrayInfo *, AssertingVH< AllocaInst > > AllocaMapTy
Map types to resolve scalar dependences.
void createExitPHINodeMerges(Scop &S)
Create exit PHI node merges for PHI nodes with more than two edges from inside the scop.
void copyInstScalar(ScopStmt &Stmt, Instruction *Inst, ValueMapT &BBMap, LoopToScevMapT &LTS)
Value * generateArrayLoad(ScopStmt &Stmt, LoadInst *load, ValueMapT &BBMap, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
Value * buildContainsCondition(ScopStmt &Stmt, const isl::set &Subdomain)
Generate instructions that compute whether one instance of Set is executed.
void finalizeSCoP(Scop &S)
Finalize the code generation for the SCoP S.
void createScalarInitialization(Scop &S)
Initialize the memory of demoted scalars.
SmallVector< Instruction *, 4 > EscapeUserVectorTy
Simple vector of instructions to store escape users.
bool canSyntheziseInStmt(ScopStmt &Stmt, Instruction *Inst)
Helper to determine if Inst can be synthesized in Stmt.
virtual void generateScalarStores(ScopStmt &Stmt, LoopToScevMapT &LTS, ValueMapT &BBMap, __isl_keep isl_id_to_ast_expr *NewAccesses)
Generate the scalar stores for the given statement.
ScalarEvolution & SE
IslExprBuilder * ExprBuilder
void handleOutsideUsers(const Scop &S, ScopArrayInfo *Array)
Handle users of Array outside the SCoP.
void createScalarFinalization(Scop &S)
Promote the values of demoted scalars after the SCoP.
void switchGeneratedFunc(Function *GenFn, DominatorTree *GenDT, LoopInfo *GenLI, ScalarEvolution *GenSE)
Change the function that code is emitted into.
Value * getNewValue(ScopStmt &Stmt, Value *Old, ValueMapT &BBMap, LoopToScevMapT &LTS, Loop *L) const
Get the new version of a value.
ValueMapT & GlobalMap
A map from llvm::Values referenced in the old code to a new set of llvm::Values, which is used to rep...
void findOutsideUsers(Scop &S)
Find scalar statements that have outside users.
void generateArrayStore(ScopStmt &Stmt, StoreInst *store, ValueMapT &BBMap, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
DominatorTree * GenDT
Relates to the region where the code is emitted into.
MapVector< Instruction *, std::pair< AssertingVH< Value >, EscapeUserVectorTy > > EscapeUsersAllocaMapTy
Map type to resolve escaping users for scalar instructions.
BasicBlock * copyBB(ScopStmt &Stmt, BasicBlock *BB, ValueMapT &BBMap, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
Copy the given basic block.
virtual void copyPHIInstruction(ScopStmt &, PHINode *, ValueMapT &, LoopToScevMapT &)
Copy a single PHI instruction.
void copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
Copy the basic block.
BasicBlock * StartBlock
The first basic block after the RTC.
void freeScalarAlloc(ScopArrayInfo *Array)
Remove a ScopArrayInfo's allocation from the ScalarMap.
BlockGenerator(const BlockGenerator &)=default
void copyInstruction(ScopStmt &Stmt, Instruction *Inst, ValueMapT &BBMap, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
Copy a single Instruction.
void invalidateScalarEvolution(Scop &S)
Invalidate the scalar evolution expressions for a scop.
BlockGenerator(PollyIRBuilder &Builder, LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT, AllocaMapTy &ScalarMap, EscapeUsersAllocaMapTy &EscapeMap, ValueMapT &GlobalMap, IslExprBuilder *ExprBuilder, BasicBlock *StartBlock)
Create a generator for basic blocks.
ScalarEvolution * GenSE
void removeDeadInstructions(BasicBlock *BB, ValueMapT &BBMap)
Remove dead instructions generated for BB.
Value * generateLocationAccessed(ScopStmt &Stmt, MemAccInst Inst, ValueMapT &BBMap, LoopToScevMapT &LTS, isl_id_to_ast_expr *NewAccesses)
Generate the operand address.
Value * getOrCreateAlloca(const MemoryAccess &Access)
Return the alloca for Access.
void generateConditionalExecution(ScopStmt &Stmt, const isl::set &Subdomain, StringRef Subject, const std::function< void()> &GenThenFunc)
Generate code that executes in a subset of Stmt's domain.
LLVM-IR generator for isl_ast_expr[essions].
Utility proxy to wrap the common members of LoadInst and StoreInst.
Definition ScopHelper.h:141
Represent memory accesses in statements.
Definition ScopInfo.h:426
std::pair< PHINode *, PHINode * > PHINodePairTy
Mapping to remember PHI nodes that still need incoming values.
RegionGenerator(BlockGenerator &BlockGen)
Create a generator for regions.
void copyStmt(ScopStmt &Stmt, LoopToScevMapT &LTS, __isl_keep isl_id_to_ast_expr *IdToAstExp)
Copy the region statement Stmt.
DenseMap< BasicBlock *, SmallVector< PHINodePairTy, 4 > > IncompletePHINodeMap
void addOperandToPHI(ScopStmt &Stmt, PHINode *PHI, PHINode *PHICopy, BasicBlock *IncomingBB, LoopToScevMapT &LTS)
Add the new operand from the copy of IncomingBB to PHICopy.
void copyPHIInstruction(ScopStmt &Stmt, PHINode *Inst, ValueMapT &BBMap, LoopToScevMapT &LTS) override
Copy a single PHI instruction.
DenseMap< BasicBlock *, BasicBlock * > EndBlockMap
A map from old to the last new block in the region, that was created to model the old basic block.
Value * getExitScalar(MemoryAccess *MA, LoopToScevMapT &LTS, ValueMapT &BBMap)
DenseMap< BasicBlock *, BasicBlock * > StartBlockMap
A map from old to the first new block in the region, that was created to model the old basic block.
void generateScalarStores(ScopStmt &Stmt, LoopToScevMapT &LTS, ValueMapT &BBMAp, __isl_keep isl_id_to_ast_expr *NewAccesses) override
Generate the scalar stores for the given statement.
DenseMap< BasicBlock *, ValueMapT > RegionMaps
The "BBMaps" for the whole region (one for each block).
PHINode * buildExitPHI(MemoryAccess *MA, LoopToScevMapT &LTS, ValueMapT &BBMap, Loop *L)
Create a PHI that combines the incoming values from all incoming blocks that are in the subregion.
BasicBlock * repairDominance(BasicBlock *BB, BasicBlock *BBCopy)
Repair the dominance tree after we created a copy block for BB.
A class to store information about arrays in the SCoP.
Definition ScopInfo.h:214
Statement of the Scop.
Definition ScopInfo.h:1135
Static Control Part.
Definition ScopInfo.h:1625
#define __isl_take
Definition ctx.h:23
#define __isl_keep
Definition ctx.h:26
#define S(TYPE, NAME)
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
Definition ScopInfo.h:110
@ Value
MemoryKind::Value: Models an llvm::Value.
Definition ScopInfo.h:149
@ PHI
MemoryKind::PHI: Models PHI nodes within the SCoP.
Definition ScopInfo.h:186
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
Definition IRBuilder.h:143
llvm::DenseMap< const llvm::Loop *, llvm::SCEVUse > LoopToScevMapT
Same as llvm/Analysis/ScalarEvolutionExpressions.h.
Definition ScopHelper.h:41
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > ValueMapT
Type to remap values.
Definition ScopHelper.h:106