blob: 81b1986c97d7e8c0608ee50bfdfec424d3fb2f99 [file] [log] [blame]
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001//===- VPlan.h - Represent A Vectorizer Plan --------------------*- C++ -*-===//
Ayal Zaks1f58dda2017-08-27 12:55:46 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenko6cadde72017-10-17 21:27:42 +00009//
Ayal Zaks1f58dda2017-08-27 12:55:46 +000010/// \file
11/// This file contains the declarations of the Vectorization Plan base classes:
12/// 1. VPBasicBlock and VPRegionBlock that inherit from a common pure virtual
13/// VPBlockBase, together implementing a Hierarchical CFG;
14/// 2. Specializations of GraphTraits that allow VPBlockBase graphs to be
15/// treated as proper graphs for generic algorithms;
16/// 3. Pure virtual VPRecipeBase serving as the base class for recipes contained
17/// within VPBasicBlocks;
Gil Rapaport8b9d1f32017-11-20 12:01:47 +000018/// 4. VPInstruction, a concrete Recipe and VPUser modeling a single planned
19/// instruction;
20/// 5. The VPlan class holding a candidate for vectorization;
21/// 6. The VPlanPrinter class providing a way to print a plan in dot format;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000022/// These are documented in docs/VectorizationPlan.rst.
Eugene Zelenko6cadde72017-10-17 21:27:42 +000023//
Ayal Zaks1f58dda2017-08-27 12:55:46 +000024//===----------------------------------------------------------------------===//
25
26#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
27#define LLVM_TRANSFORMS_VECTORIZE_VPLAN_H
28
Diego Caballero35871502018-07-31 01:57:29 +000029#include "VPlanLoopInfo.h"
Gil Rapaport8b9d1f32017-11-20 12:01:47 +000030#include "VPlanValue.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000031#include "llvm/ADT/DenseMap.h"
Diego Caballero2a34ac82018-07-30 21:33:31 +000032#include "llvm/ADT/DepthFirstIterator.h"
Ayal Zaks1f58dda2017-08-27 12:55:46 +000033#include "llvm/ADT/GraphTraits.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000034#include "llvm/ADT/Optional.h"
Florian Hahna1cc8482018-06-12 11:16:56 +000035#include "llvm/ADT/SmallPtrSet.h"
Ayal Zaks1f58dda2017-08-27 12:55:46 +000036#include "llvm/ADT/SmallSet.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000037#include "llvm/ADT/SmallVector.h"
38#include "llvm/ADT/Twine.h"
Ayal Zaks1f58dda2017-08-27 12:55:46 +000039#include "llvm/ADT/ilist.h"
40#include "llvm/ADT/ilist_node.h"
41#include "llvm/IR/IRBuilder.h"
Eugene Zelenko6cadde72017-10-17 21:27:42 +000042#include <algorithm>
43#include <cassert>
44#include <cstddef>
45#include <map>
46#include <string>
Ayal Zaks1f58dda2017-08-27 12:55:46 +000047
48namespace llvm {
49
Hal Finkel0f1314c2018-01-07 16:02:58 +000050class LoopVectorizationLegality;
51class LoopVectorizationCostModel;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000052class BasicBlock;
Eugene Zelenko6cadde72017-10-17 21:27:42 +000053class DominatorTree;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000054class InnerLoopVectorizer;
Hal Finkel7333aa92017-12-16 01:12:50 +000055class InterleaveGroup;
Eugene Zelenko6cadde72017-10-17 21:27:42 +000056class raw_ostream;
57class Value;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000058class VPBasicBlock;
Eugene Zelenko6cadde72017-10-17 21:27:42 +000059class VPRegionBlock;
Florian Hahn45e5d5b2018-06-08 17:30:45 +000060class VPlan;
61
62/// A range of powers-of-2 vectorization factors with fixed start and
63/// adjustable end. The range includes start and excludes end, e.g.,:
64/// [1, 9) = {1, 2, 4, 8}
65struct VFRange {
66 // A power of 2.
67 const unsigned Start;
68
69 // Need not be a power of 2. If End <= Start range is empty.
70 unsigned End;
71};
72
73using VPlanPtr = std::unique_ptr<VPlan>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000074
75/// In what follows, the term "input IR" refers to code that is fed into the
76/// vectorizer whereas the term "output IR" refers to code that is generated by
77/// the vectorizer.
78
79/// VPIteration represents a single point in the iteration space of the output
80/// (vectorized and/or unrolled) IR loop.
81struct VPIteration {
Eugene Zelenko6cadde72017-10-17 21:27:42 +000082 /// in [0..UF)
83 unsigned Part;
84
85 /// in [0..VF)
86 unsigned Lane;
Ayal Zaks1f58dda2017-08-27 12:55:46 +000087};
88
89/// This is a helper struct for maintaining vectorization state. It's used for
90/// mapping values from the original loop to their corresponding values in
91/// the new loop. Two mappings are maintained: one for vectorized values and
92/// one for scalarized values. Vectorized values are represented with UF
93/// vector values in the new loop, and scalarized values are represented with
94/// UF x VF scalar values in the new loop. UF and VF are the unroll and
95/// vectorization factors, respectively.
96///
97/// Entries can be added to either map with setVectorValue and setScalarValue,
98/// which assert that an entry was not already added before. If an entry is to
99/// replace an existing one, call resetVectorValue and resetScalarValue. This is
100/// currently needed to modify the mapped values during "fix-up" operations that
101/// occur once the first phase of widening is complete. These operations include
102/// type truncation and the second phase of recurrence widening.
103///
104/// Entries from either map can be retrieved using the getVectorValue and
105/// getScalarValue functions, which assert that the desired value exists.
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000106struct VectorizerValueMap {
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000107 friend struct VPTransformState;
108
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000109private:
110 /// The unroll factor. Each entry in the vector map contains UF vector values.
111 unsigned UF;
112
113 /// The vectorization factor. Each entry in the scalar map contains UF x VF
114 /// scalar values.
115 unsigned VF;
116
117 /// The vector and scalar map storage. We use std::map and not DenseMap
118 /// because insertions to DenseMap invalidate its iterators.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000119 using VectorParts = SmallVector<Value *, 2>;
120 using ScalarParts = SmallVector<SmallVector<Value *, 4>, 2>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000121 std::map<Value *, VectorParts> VectorMapStorage;
122 std::map<Value *, ScalarParts> ScalarMapStorage;
123
124public:
125 /// Construct an empty map with the given unroll and vectorization factors.
126 VectorizerValueMap(unsigned UF, unsigned VF) : UF(UF), VF(VF) {}
127
128 /// \return True if the map has any vector entry for \p Key.
129 bool hasAnyVectorValue(Value *Key) const {
130 return VectorMapStorage.count(Key);
131 }
132
133 /// \return True if the map has a vector entry for \p Key and \p Part.
134 bool hasVectorValue(Value *Key, unsigned Part) const {
135 assert(Part < UF && "Queried Vector Part is too large.");
136 if (!hasAnyVectorValue(Key))
137 return false;
138 const VectorParts &Entry = VectorMapStorage.find(Key)->second;
139 assert(Entry.size() == UF && "VectorParts has wrong dimensions.");
140 return Entry[Part] != nullptr;
141 }
142
143 /// \return True if the map has any scalar entry for \p Key.
144 bool hasAnyScalarValue(Value *Key) const {
145 return ScalarMapStorage.count(Key);
146 }
147
148 /// \return True if the map has a scalar entry for \p Key and \p Instance.
149 bool hasScalarValue(Value *Key, const VPIteration &Instance) const {
150 assert(Instance.Part < UF && "Queried Scalar Part is too large.");
151 assert(Instance.Lane < VF && "Queried Scalar Lane is too large.");
152 if (!hasAnyScalarValue(Key))
153 return false;
154 const ScalarParts &Entry = ScalarMapStorage.find(Key)->second;
155 assert(Entry.size() == UF && "ScalarParts has wrong dimensions.");
156 assert(Entry[Instance.Part].size() == VF &&
157 "ScalarParts has wrong dimensions.");
158 return Entry[Instance.Part][Instance.Lane] != nullptr;
159 }
160
161 /// Retrieve the existing vector value that corresponds to \p Key and
162 /// \p Part.
163 Value *getVectorValue(Value *Key, unsigned Part) {
164 assert(hasVectorValue(Key, Part) && "Getting non-existent value.");
165 return VectorMapStorage[Key][Part];
166 }
167
168 /// Retrieve the existing scalar value that corresponds to \p Key and
169 /// \p Instance.
170 Value *getScalarValue(Value *Key, const VPIteration &Instance) {
171 assert(hasScalarValue(Key, Instance) && "Getting non-existent value.");
172 return ScalarMapStorage[Key][Instance.Part][Instance.Lane];
173 }
174
175 /// Set a vector value associated with \p Key and \p Part. Assumes such a
176 /// value is not already set. If it is, use resetVectorValue() instead.
177 void setVectorValue(Value *Key, unsigned Part, Value *Vector) {
178 assert(!hasVectorValue(Key, Part) && "Vector value already set for part");
179 if (!VectorMapStorage.count(Key)) {
180 VectorParts Entry(UF);
181 VectorMapStorage[Key] = Entry;
182 }
183 VectorMapStorage[Key][Part] = Vector;
184 }
185
186 /// Set a scalar value associated with \p Key and \p Instance. Assumes such a
187 /// value is not already set.
188 void setScalarValue(Value *Key, const VPIteration &Instance, Value *Scalar) {
189 assert(!hasScalarValue(Key, Instance) && "Scalar value already set");
190 if (!ScalarMapStorage.count(Key)) {
191 ScalarParts Entry(UF);
192 // TODO: Consider storing uniform values only per-part, as they occupy
193 // lane 0 only, keeping the other VF-1 redundant entries null.
194 for (unsigned Part = 0; Part < UF; ++Part)
195 Entry[Part].resize(VF, nullptr);
196 ScalarMapStorage[Key] = Entry;
197 }
198 ScalarMapStorage[Key][Instance.Part][Instance.Lane] = Scalar;
199 }
200
201 /// Reset the vector value associated with \p Key for the given \p Part.
202 /// This function can be used to update values that have already been
203 /// vectorized. This is the case for "fix-up" operations including type
204 /// truncation and the second phase of recurrence vectorization.
205 void resetVectorValue(Value *Key, unsigned Part, Value *Vector) {
206 assert(hasVectorValue(Key, Part) && "Vector value not set for part");
207 VectorMapStorage[Key][Part] = Vector;
208 }
209
210 /// Reset the scalar value associated with \p Key for \p Part and \p Lane.
211 /// This function can be used to update values that have already been
212 /// scalarized. This is the case for "fix-up" operations including scalar phi
213 /// nodes for scalarized and predicated instructions.
214 void resetScalarValue(Value *Key, const VPIteration &Instance,
215 Value *Scalar) {
216 assert(hasScalarValue(Key, Instance) &&
217 "Scalar value not set for part and lane");
218 ScalarMapStorage[Key][Instance.Part][Instance.Lane] = Scalar;
219 }
220};
221
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000222/// This class is used to enable the VPlan to invoke a method of ILV. This is
223/// needed until the method is refactored out of ILV and becomes reusable.
224struct VPCallback {
225 virtual ~VPCallback() {}
226 virtual Value *getOrCreateVectorValues(Value *V, unsigned Part) = 0;
227};
228
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000229/// VPTransformState holds information passed down when "executing" a VPlan,
230/// needed for generating the output IR.
231struct VPTransformState {
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000232 VPTransformState(unsigned VF, unsigned UF, LoopInfo *LI, DominatorTree *DT,
233 IRBuilder<> &Builder, VectorizerValueMap &ValueMap,
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000234 InnerLoopVectorizer *ILV, VPCallback &Callback)
235 : VF(VF), UF(UF), Instance(), LI(LI), DT(DT), Builder(Builder),
236 ValueMap(ValueMap), ILV(ILV), Callback(Callback) {}
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000237
238 /// The chosen Vectorization and Unroll Factors of the loop being vectorized.
239 unsigned VF;
240 unsigned UF;
241
242 /// Hold the indices to generate specific scalar instructions. Null indicates
243 /// that all instances are to be generated, using either scalar or vector
244 /// instructions.
245 Optional<VPIteration> Instance;
246
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000247 struct DataState {
248 /// A type for vectorized values in the new loop. Each value from the
249 /// original loop, when vectorized, is represented by UF vector values in
250 /// the new unrolled loop, where UF is the unroll factor.
251 typedef SmallVector<Value *, 2> PerPartValuesTy;
252
253 DenseMap<VPValue *, PerPartValuesTy> PerPartOutput;
254 } Data;
255
256 /// Get the generated Value for a given VPValue and a given Part. Note that
257 /// as some Defs are still created by ILV and managed in its ValueMap, this
258 /// method will delegate the call to ILV in such cases in order to provide
259 /// callers a consistent API.
260 /// \see set.
261 Value *get(VPValue *Def, unsigned Part) {
262 // If Values have been set for this Def return the one relevant for \p Part.
263 if (Data.PerPartOutput.count(Def))
264 return Data.PerPartOutput[Def][Part];
265 // Def is managed by ILV: bring the Values from ValueMap.
266 return Callback.getOrCreateVectorValues(VPValue2Value[Def], Part);
267 }
268
269 /// Set the generated Value for a given VPValue and a given Part.
270 void set(VPValue *Def, Value *V, unsigned Part) {
271 if (!Data.PerPartOutput.count(Def)) {
272 DataState::PerPartValuesTy Entry(UF);
273 Data.PerPartOutput[Def] = Entry;
274 }
275 Data.PerPartOutput[Def][Part] = V;
276 }
277
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000278 /// Hold state information used when constructing the CFG of the output IR,
279 /// traversing the VPBasicBlocks and generating corresponding IR BasicBlocks.
280 struct CFGState {
281 /// The previous VPBasicBlock visited. Initially set to null.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000282 VPBasicBlock *PrevVPBB = nullptr;
283
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000284 /// The previous IR BasicBlock created or used. Initially set to the new
285 /// header BasicBlock.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000286 BasicBlock *PrevBB = nullptr;
287
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000288 /// The last IR BasicBlock in the output IR. Set to the new latch
289 /// BasicBlock, used for placing the newly created BasicBlocks.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000290 BasicBlock *LastBB = nullptr;
291
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000292 /// A mapping of each VPBasicBlock to the corresponding BasicBlock. In case
293 /// of replication, maps the BasicBlock of the last replica created.
294 SmallDenseMap<VPBasicBlock *, BasicBlock *> VPBB2IRBB;
295
Hideki Saitoea7f3032018-09-14 00:36:00 +0000296 /// Vector of VPBasicBlocks whose terminator instruction needs to be fixed
297 /// up at the end of vector code generation.
298 SmallVector<VPBasicBlock *, 8> VPBBsToFix;
299
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000300 CFGState() = default;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000301 } CFG;
302
303 /// Hold a pointer to LoopInfo to register new basic blocks in the loop.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000304 LoopInfo *LI;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000305
306 /// Hold a pointer to Dominator Tree to register new basic blocks in the loop.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000307 DominatorTree *DT;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000308
309 /// Hold a reference to the IRBuilder used to generate output IR code.
310 IRBuilder<> &Builder;
311
312 /// Hold a reference to the Value state information used when generating the
313 /// Values of the output IR.
314 VectorizerValueMap &ValueMap;
315
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000316 /// Hold a reference to a mapping between VPValues in VPlan and original
317 /// Values they correspond to.
318 VPValue2ValueTy VPValue2Value;
319
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000320 /// Hold a pointer to InnerLoopVectorizer to reuse its IR generation methods.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000321 InnerLoopVectorizer *ILV;
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000322
323 VPCallback &Callback;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000324};
325
326/// VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
327/// A VPBlockBase can be either a VPBasicBlock or a VPRegionBlock.
328class VPBlockBase {
Diego Caballero168d04d2018-05-21 18:14:23 +0000329 friend class VPBlockUtils;
330
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000331private:
332 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
333
334 /// An optional name for the block.
335 std::string Name;
336
337 /// The immediate VPRegionBlock which this VPBlockBase belongs to, or null if
338 /// it is a topmost VPBlockBase.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000339 VPRegionBlock *Parent = nullptr;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000340
341 /// List of predecessor blocks.
342 SmallVector<VPBlockBase *, 1> Predecessors;
343
344 /// List of successor blocks.
345 SmallVector<VPBlockBase *, 1> Successors;
346
Diego Caballerod0953012018-07-09 15:57:09 +0000347 /// Successor selector, null for zero or single successor blocks.
348 VPValue *CondBit = nullptr;
349
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000350 /// Add \p Successor as the last successor to this block.
351 void appendSuccessor(VPBlockBase *Successor) {
352 assert(Successor && "Cannot add nullptr successor!");
353 Successors.push_back(Successor);
354 }
355
356 /// Add \p Predecessor as the last predecessor to this block.
357 void appendPredecessor(VPBlockBase *Predecessor) {
358 assert(Predecessor && "Cannot add nullptr predecessor!");
359 Predecessors.push_back(Predecessor);
360 }
361
362 /// Remove \p Predecessor from the predecessors of this block.
363 void removePredecessor(VPBlockBase *Predecessor) {
364 auto Pos = std::find(Predecessors.begin(), Predecessors.end(), Predecessor);
365 assert(Pos && "Predecessor does not exist");
366 Predecessors.erase(Pos);
367 }
368
369 /// Remove \p Successor from the successors of this block.
370 void removeSuccessor(VPBlockBase *Successor) {
371 auto Pos = std::find(Successors.begin(), Successors.end(), Successor);
372 assert(Pos && "Successor does not exist");
373 Successors.erase(Pos);
374 }
375
376protected:
377 VPBlockBase(const unsigned char SC, const std::string &N)
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000378 : SubclassID(SC), Name(N) {}
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000379
380public:
381 /// An enumeration for keeping track of the concrete subclass of VPBlockBase
382 /// that are actually instantiated. Values of this enumeration are kept in the
383 /// SubclassID field of the VPBlockBase objects. They are used for concrete
384 /// type identification.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000385 using VPBlockTy = enum { VPBasicBlockSC, VPRegionBlockSC };
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000386
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000387 using VPBlocksTy = SmallVectorImpl<VPBlockBase *>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000388
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000389 virtual ~VPBlockBase() = default;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000390
391 const std::string &getName() const { return Name; }
392
393 void setName(const Twine &newName) { Name = newName.str(); }
394
395 /// \return an ID for the concrete type of this object.
396 /// This is used to implement the classof checks. This should not be used
397 /// for any other purpose, as the values may change as LLVM evolves.
398 unsigned getVPBlockID() const { return SubclassID; }
399
Diego Caballero168d04d2018-05-21 18:14:23 +0000400 VPRegionBlock *getParent() { return Parent; }
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000401 const VPRegionBlock *getParent() const { return Parent; }
402
403 void setParent(VPRegionBlock *P) { Parent = P; }
404
405 /// \return the VPBasicBlock that is the entry of this VPBlockBase,
406 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
407 /// VPBlockBase is a VPBasicBlock, it is returned.
408 const VPBasicBlock *getEntryBasicBlock() const;
409 VPBasicBlock *getEntryBasicBlock();
410
411 /// \return the VPBasicBlock that is the exit of this VPBlockBase,
412 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
413 /// VPBlockBase is a VPBasicBlock, it is returned.
414 const VPBasicBlock *getExitBasicBlock() const;
415 VPBasicBlock *getExitBasicBlock();
416
417 const VPBlocksTy &getSuccessors() const { return Successors; }
418 VPBlocksTy &getSuccessors() { return Successors; }
419
420 const VPBlocksTy &getPredecessors() const { return Predecessors; }
421 VPBlocksTy &getPredecessors() { return Predecessors; }
422
423 /// \return the successor of this VPBlockBase if it has a single successor.
424 /// Otherwise return a null pointer.
425 VPBlockBase *getSingleSuccessor() const {
426 return (Successors.size() == 1 ? *Successors.begin() : nullptr);
427 }
428
429 /// \return the predecessor of this VPBlockBase if it has a single
430 /// predecessor. Otherwise return a null pointer.
431 VPBlockBase *getSinglePredecessor() const {
432 return (Predecessors.size() == 1 ? *Predecessors.begin() : nullptr);
433 }
434
Diego Caballero168d04d2018-05-21 18:14:23 +0000435 size_t getNumSuccessors() const { return Successors.size(); }
436 size_t getNumPredecessors() const { return Predecessors.size(); }
437
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000438 /// An Enclosing Block of a block B is any block containing B, including B
439 /// itself. \return the closest enclosing block starting from "this", which
440 /// has successors. \return the root enclosing block if all enclosing blocks
441 /// have no successors.
442 VPBlockBase *getEnclosingBlockWithSuccessors();
443
444 /// \return the closest enclosing block starting from "this", which has
445 /// predecessors. \return the root enclosing block if all enclosing blocks
446 /// have no predecessors.
447 VPBlockBase *getEnclosingBlockWithPredecessors();
448
449 /// \return the successors either attached directly to this VPBlockBase or, if
450 /// this VPBlockBase is the exit block of a VPRegionBlock and has no
451 /// successors of its own, search recursively for the first enclosing
452 /// VPRegionBlock that has successors and return them. If no such
453 /// VPRegionBlock exists, return the (empty) successors of the topmost
454 /// VPBlockBase reached.
455 const VPBlocksTy &getHierarchicalSuccessors() {
456 return getEnclosingBlockWithSuccessors()->getSuccessors();
457 }
458
459 /// \return the hierarchical successor of this VPBlockBase if it has a single
460 /// hierarchical successor. Otherwise return a null pointer.
461 VPBlockBase *getSingleHierarchicalSuccessor() {
462 return getEnclosingBlockWithSuccessors()->getSingleSuccessor();
463 }
464
465 /// \return the predecessors either attached directly to this VPBlockBase or,
466 /// if this VPBlockBase is the entry block of a VPRegionBlock and has no
467 /// predecessors of its own, search recursively for the first enclosing
468 /// VPRegionBlock that has predecessors and return them. If no such
469 /// VPRegionBlock exists, return the (empty) predecessors of the topmost
470 /// VPBlockBase reached.
471 const VPBlocksTy &getHierarchicalPredecessors() {
472 return getEnclosingBlockWithPredecessors()->getPredecessors();
473 }
474
475 /// \return the hierarchical predecessor of this VPBlockBase if it has a
476 /// single hierarchical predecessor. Otherwise return a null pointer.
477 VPBlockBase *getSingleHierarchicalPredecessor() {
478 return getEnclosingBlockWithPredecessors()->getSinglePredecessor();
479 }
480
Diego Caballerod0953012018-07-09 15:57:09 +0000481 /// \return the condition bit selecting the successor.
482 VPValue *getCondBit() { return CondBit; }
483
484 const VPValue *getCondBit() const { return CondBit; }
485
486 void setCondBit(VPValue *CV) { CondBit = CV; }
487
Diego Caballero168d04d2018-05-21 18:14:23 +0000488 /// Set a given VPBlockBase \p Successor as the single successor of this
489 /// VPBlockBase. This VPBlockBase is not added as predecessor of \p Successor.
490 /// This VPBlockBase must have no successors.
491 void setOneSuccessor(VPBlockBase *Successor) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000492 assert(Successors.empty() && "Setting one successor when others exist.");
493 appendSuccessor(Successor);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000494 }
495
Diego Caballero168d04d2018-05-21 18:14:23 +0000496 /// Set two given VPBlockBases \p IfTrue and \p IfFalse to be the two
Diego Caballerod0953012018-07-09 15:57:09 +0000497 /// successors of this VPBlockBase. \p Condition is set as the successor
498 /// selector. This VPBlockBase is not added as predecessor of \p IfTrue or \p
499 /// IfFalse. This VPBlockBase must have no successors.
500 void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
501 VPValue *Condition) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000502 assert(Successors.empty() && "Setting two successors when others exist.");
Diego Caballerod0953012018-07-09 15:57:09 +0000503 assert(Condition && "Setting two successors without condition!");
504 CondBit = Condition;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000505 appendSuccessor(IfTrue);
506 appendSuccessor(IfFalse);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000507 }
508
Diego Caballero168d04d2018-05-21 18:14:23 +0000509 /// Set each VPBasicBlock in \p NewPreds as predecessor of this VPBlockBase.
510 /// This VPBlockBase must have no predecessors. This VPBlockBase is not added
511 /// as successor of any VPBasicBlock in \p NewPreds.
512 void setPredecessors(ArrayRef<VPBlockBase *> NewPreds) {
513 assert(Predecessors.empty() && "Block predecessors already set.");
514 for (auto *Pred : NewPreds)
515 appendPredecessor(Pred);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000516 }
517
518 /// The method which generates the output IR that correspond to this
519 /// VPBlockBase, thereby "executing" the VPlan.
520 virtual void execute(struct VPTransformState *State) = 0;
521
522 /// Delete all blocks reachable from a given VPBlockBase, inclusive.
523 static void deleteCFG(VPBlockBase *Entry);
Diego Caballero2a34ac82018-07-30 21:33:31 +0000524
525 void printAsOperand(raw_ostream &OS, bool PrintType) const {
526 OS << getName();
527 }
528
529 void print(raw_ostream &OS) const {
530 // TODO: Only printing VPBB name for now since we only have dot printing
531 // support for VPInstructions/Recipes.
532 printAsOperand(OS, false);
533 }
Diego Caballero35871502018-07-31 01:57:29 +0000534
535 /// Return true if it is legal to hoist instructions into this block.
536 bool isLegalToHoistInto() {
537 // There are currently no constraints that prevent an instruction to be
538 // hoisted into a VPBlockBase.
539 return true;
540 }
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000541};
542
543/// VPRecipeBase is a base class modeling a sequence of one or more output IR
544/// instructions.
545class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock> {
546 friend VPBasicBlock;
547
548private:
549 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
550
551 /// Each VPRecipe belongs to a single VPBasicBlock.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000552 VPBasicBlock *Parent = nullptr;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000553
554public:
555 /// An enumeration for keeping track of the concrete subclass of VPRecipeBase
556 /// that is actually instantiated. Values of this enumeration are kept in the
557 /// SubclassID field of the VPRecipeBase objects. They are used for concrete
558 /// type identification.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000559 using VPRecipeTy = enum {
Gil Rapaport848581c2017-11-14 12:09:30 +0000560 VPBlendSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000561 VPBranchOnMaskSC,
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000562 VPInstructionSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000563 VPInterleaveSC,
564 VPPredInstPHISC,
565 VPReplicateSC,
566 VPWidenIntOrFpInductionSC,
Gil Rapaport848581c2017-11-14 12:09:30 +0000567 VPWidenMemoryInstructionSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000568 VPWidenPHISC,
569 VPWidenSC,
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000570 };
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000571
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000572 VPRecipeBase(const unsigned char SC) : SubclassID(SC) {}
573 virtual ~VPRecipeBase() = default;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000574
575 /// \return an ID for the concrete type of this object.
576 /// This is used to implement the classof checks. This should not be used
577 /// for any other purpose, as the values may change as LLVM evolves.
578 unsigned getVPRecipeID() const { return SubclassID; }
579
580 /// \return the VPBasicBlock which this VPRecipe belongs to.
581 VPBasicBlock *getParent() { return Parent; }
582 const VPBasicBlock *getParent() const { return Parent; }
583
584 /// The method which generates the output IR instructions that correspond to
585 /// this VPRecipe, thereby "executing" the VPlan.
586 virtual void execute(struct VPTransformState &State) = 0;
587
588 /// Each recipe prints itself.
589 virtual void print(raw_ostream &O, const Twine &Indent) const = 0;
Florian Hahn7591e4e2018-06-18 11:34:17 +0000590
591 /// Insert an unlinked recipe into a basic block immediately before
592 /// the specified recipe.
593 void insertBefore(VPRecipeBase *InsertPos);
Florian Hahn63cbcf92018-06-18 15:18:48 +0000594
595 /// This method unlinks 'this' from the containing basic block and deletes it.
596 ///
597 /// \returns an iterator pointing to the element after the erased one
598 iplist<VPRecipeBase>::iterator eraseFromParent();
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000599};
600
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000601/// This is a concrete Recipe that models a single VPlan-level instruction.
602/// While as any Recipe it may generate a sequence of IR instructions when
603/// executed, these instructions would always form a single-def expression as
604/// the VPInstruction is also a single def-use vertex.
605class VPInstruction : public VPUser, public VPRecipeBase {
Florian Hahn3385caa2018-06-18 18:28:49 +0000606 friend class VPlanHCFGTransforms;
607
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000608public:
609 /// VPlan opcodes, extending LLVM IR with idiomatics instructions.
610 enum { Not = Instruction::OtherOpsEnd + 1 };
611
612private:
613 typedef unsigned char OpcodeTy;
614 OpcodeTy Opcode;
615
616 /// Utility method serving execute(): generates a single instance of the
617 /// modeled instruction.
618 void generateInstruction(VPTransformState &State, unsigned Part);
619
620public:
Diego Caballero168d04d2018-05-21 18:14:23 +0000621 VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands)
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000622 : VPUser(VPValue::VPInstructionSC, Operands),
623 VPRecipeBase(VPRecipeBase::VPInstructionSC), Opcode(Opcode) {}
624
Diego Caballero168d04d2018-05-21 18:14:23 +0000625 VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands)
626 : VPInstruction(Opcode, ArrayRef<VPValue *>(Operands)) {}
627
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000628 /// Method to support type inquiry through isa, cast, and dyn_cast.
629 static inline bool classof(const VPValue *V) {
630 return V->getVPValueID() == VPValue::VPInstructionSC;
631 }
632
633 /// Method to support type inquiry through isa, cast, and dyn_cast.
634 static inline bool classof(const VPRecipeBase *R) {
635 return R->getVPRecipeID() == VPRecipeBase::VPInstructionSC;
636 }
637
638 unsigned getOpcode() const { return Opcode; }
639
640 /// Generate the instruction.
641 /// TODO: We currently execute only per-part unless a specific instance is
642 /// provided.
643 void execute(VPTransformState &State) override;
644
645 /// Print the Recipe.
646 void print(raw_ostream &O, const Twine &Indent) const override;
647
648 /// Print the VPInstruction.
649 void print(raw_ostream &O) const;
650};
651
Hal Finkel7333aa92017-12-16 01:12:50 +0000652/// VPWidenRecipe is a recipe for producing a copy of vector type for each
653/// Instruction in its ingredients independently, in order. This recipe covers
654/// most of the traditional vectorization cases where each ingredient transforms
655/// into a vectorized version of itself.
656class VPWidenRecipe : public VPRecipeBase {
657private:
658 /// Hold the ingredients by pointing to their original BasicBlock location.
659 BasicBlock::iterator Begin;
660 BasicBlock::iterator End;
661
662public:
663 VPWidenRecipe(Instruction *I) : VPRecipeBase(VPWidenSC) {
664 End = I->getIterator();
665 Begin = End++;
666 }
667
668 ~VPWidenRecipe() override = default;
669
670 /// Method to support type inquiry through isa, cast, and dyn_cast.
671 static inline bool classof(const VPRecipeBase *V) {
672 return V->getVPRecipeID() == VPRecipeBase::VPWidenSC;
673 }
674
675 /// Produce widened copies of all Ingredients.
676 void execute(VPTransformState &State) override;
677
678 /// Augment the recipe to include Instr, if it lies at its End.
679 bool appendInstruction(Instruction *Instr) {
680 if (End != Instr->getIterator())
681 return false;
682 End++;
683 return true;
684 }
685
686 /// Print the recipe.
687 void print(raw_ostream &O, const Twine &Indent) const override;
688};
689
690/// A recipe for handling phi nodes of integer and floating-point inductions,
691/// producing their vector and scalar values.
692class VPWidenIntOrFpInductionRecipe : public VPRecipeBase {
693private:
694 PHINode *IV;
695 TruncInst *Trunc;
696
697public:
698 VPWidenIntOrFpInductionRecipe(PHINode *IV, TruncInst *Trunc = nullptr)
699 : VPRecipeBase(VPWidenIntOrFpInductionSC), IV(IV), Trunc(Trunc) {}
700 ~VPWidenIntOrFpInductionRecipe() override = default;
701
702 /// Method to support type inquiry through isa, cast, and dyn_cast.
703 static inline bool classof(const VPRecipeBase *V) {
704 return V->getVPRecipeID() == VPRecipeBase::VPWidenIntOrFpInductionSC;
705 }
706
707 /// Generate the vectorized and scalarized versions of the phi node as
708 /// needed by their users.
709 void execute(VPTransformState &State) override;
710
711 /// Print the recipe.
712 void print(raw_ostream &O, const Twine &Indent) const override;
713};
714
715/// A recipe for handling all phi nodes except for integer and FP inductions.
716class VPWidenPHIRecipe : public VPRecipeBase {
717private:
718 PHINode *Phi;
719
720public:
721 VPWidenPHIRecipe(PHINode *Phi) : VPRecipeBase(VPWidenPHISC), Phi(Phi) {}
722 ~VPWidenPHIRecipe() override = default;
723
724 /// Method to support type inquiry through isa, cast, and dyn_cast.
725 static inline bool classof(const VPRecipeBase *V) {
726 return V->getVPRecipeID() == VPRecipeBase::VPWidenPHISC;
727 }
728
729 /// Generate the phi/select nodes.
730 void execute(VPTransformState &State) override;
731
732 /// Print the recipe.
733 void print(raw_ostream &O, const Twine &Indent) const override;
734};
735
736/// A recipe for vectorizing a phi-node as a sequence of mask-based select
737/// instructions.
738class VPBlendRecipe : public VPRecipeBase {
739private:
740 PHINode *Phi;
741
742 /// The blend operation is a User of a mask, if not null.
743 std::unique_ptr<VPUser> User;
744
745public:
746 VPBlendRecipe(PHINode *Phi, ArrayRef<VPValue *> Masks)
747 : VPRecipeBase(VPBlendSC), Phi(Phi) {
748 assert((Phi->getNumIncomingValues() == 1 ||
749 Phi->getNumIncomingValues() == Masks.size()) &&
750 "Expected the same number of incoming values and masks");
751 if (!Masks.empty())
752 User.reset(new VPUser(Masks));
753 }
754
755 /// Method to support type inquiry through isa, cast, and dyn_cast.
756 static inline bool classof(const VPRecipeBase *V) {
757 return V->getVPRecipeID() == VPRecipeBase::VPBlendSC;
758 }
759
760 /// Generate the phi/select nodes.
761 void execute(VPTransformState &State) override;
762
763 /// Print the recipe.
764 void print(raw_ostream &O, const Twine &Indent) const override;
765};
766
767/// VPInterleaveRecipe is a recipe for transforming an interleave group of load
768/// or stores into one wide load/store and shuffles.
769class VPInterleaveRecipe : public VPRecipeBase {
770private:
771 const InterleaveGroup *IG;
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000772 std::unique_ptr<VPUser> User;
Hal Finkel7333aa92017-12-16 01:12:50 +0000773
774public:
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000775 VPInterleaveRecipe(const InterleaveGroup *IG, VPValue *Mask)
776 : VPRecipeBase(VPInterleaveSC), IG(IG) {
777 if (Mask) // Create a VPInstruction to register as a user of the mask.
778 User.reset(new VPUser({Mask}));
779 }
Hal Finkel7333aa92017-12-16 01:12:50 +0000780 ~VPInterleaveRecipe() override = default;
781
782 /// Method to support type inquiry through isa, cast, and dyn_cast.
783 static inline bool classof(const VPRecipeBase *V) {
784 return V->getVPRecipeID() == VPRecipeBase::VPInterleaveSC;
785 }
786
787 /// Generate the wide load or store, and shuffles.
788 void execute(VPTransformState &State) override;
789
790 /// Print the recipe.
791 void print(raw_ostream &O, const Twine &Indent) const override;
792
793 const InterleaveGroup *getInterleaveGroup() { return IG; }
794};
795
796/// VPReplicateRecipe replicates a given instruction producing multiple scalar
797/// copies of the original scalar type, one per lane, instead of producing a
798/// single copy of widened type for all lanes. If the instruction is known to be
799/// uniform only one copy, per lane zero, will be generated.
800class VPReplicateRecipe : public VPRecipeBase {
801private:
802 /// The instruction being replicated.
803 Instruction *Ingredient;
804
805 /// Indicator if only a single replica per lane is needed.
806 bool IsUniform;
807
808 /// Indicator if the replicas are also predicated.
809 bool IsPredicated;
810
811 /// Indicator if the scalar values should also be packed into a vector.
812 bool AlsoPack;
813
814public:
815 VPReplicateRecipe(Instruction *I, bool IsUniform, bool IsPredicated = false)
816 : VPRecipeBase(VPReplicateSC), Ingredient(I), IsUniform(IsUniform),
817 IsPredicated(IsPredicated) {
818 // Retain the previous behavior of predicateInstructions(), where an
819 // insert-element of a predicated instruction got hoisted into the
820 // predicated basic block iff it was its only user. This is achieved by
821 // having predicated instructions also pack their values into a vector by
822 // default unless they have a replicated user which uses their scalar value.
823 AlsoPack = IsPredicated && !I->use_empty();
824 }
825
826 ~VPReplicateRecipe() override = default;
827
828 /// Method to support type inquiry through isa, cast, and dyn_cast.
829 static inline bool classof(const VPRecipeBase *V) {
830 return V->getVPRecipeID() == VPRecipeBase::VPReplicateSC;
831 }
832
833 /// Generate replicas of the desired Ingredient. Replicas will be generated
834 /// for all parts and lanes unless a specific part and lane are specified in
835 /// the \p State.
836 void execute(VPTransformState &State) override;
837
838 void setAlsoPack(bool Pack) { AlsoPack = Pack; }
839
840 /// Print the recipe.
841 void print(raw_ostream &O, const Twine &Indent) const override;
842};
843
844/// A recipe for generating conditional branches on the bits of a mask.
845class VPBranchOnMaskRecipe : public VPRecipeBase {
846private:
847 std::unique_ptr<VPUser> User;
848
849public:
850 VPBranchOnMaskRecipe(VPValue *BlockInMask) : VPRecipeBase(VPBranchOnMaskSC) {
851 if (BlockInMask) // nullptr means all-one mask.
852 User.reset(new VPUser({BlockInMask}));
853 }
854
855 /// Method to support type inquiry through isa, cast, and dyn_cast.
856 static inline bool classof(const VPRecipeBase *V) {
857 return V->getVPRecipeID() == VPRecipeBase::VPBranchOnMaskSC;
858 }
859
860 /// Generate the extraction of the appropriate bit from the block mask and the
861 /// conditional branch.
862 void execute(VPTransformState &State) override;
863
864 /// Print the recipe.
865 void print(raw_ostream &O, const Twine &Indent) const override {
866 O << " +\n" << Indent << "\"BRANCH-ON-MASK ";
867 if (User)
868 O << *User->getOperand(0);
869 else
870 O << " All-One";
871 O << "\\l\"";
872 }
873};
874
875/// VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when
876/// control converges back from a Branch-on-Mask. The phi nodes are needed in
877/// order to merge values that are set under such a branch and feed their uses.
878/// The phi nodes can be scalar or vector depending on the users of the value.
879/// This recipe works in concert with VPBranchOnMaskRecipe.
880class VPPredInstPHIRecipe : public VPRecipeBase {
881private:
882 Instruction *PredInst;
883
884public:
885 /// Construct a VPPredInstPHIRecipe given \p PredInst whose value needs a phi
886 /// nodes after merging back from a Branch-on-Mask.
887 VPPredInstPHIRecipe(Instruction *PredInst)
888 : VPRecipeBase(VPPredInstPHISC), PredInst(PredInst) {}
889 ~VPPredInstPHIRecipe() override = default;
890
891 /// Method to support type inquiry through isa, cast, and dyn_cast.
892 static inline bool classof(const VPRecipeBase *V) {
893 return V->getVPRecipeID() == VPRecipeBase::VPPredInstPHISC;
894 }
895
896 /// Generates phi nodes for live-outs as needed to retain SSA form.
897 void execute(VPTransformState &State) override;
898
899 /// Print the recipe.
900 void print(raw_ostream &O, const Twine &Indent) const override;
901};
902
903/// A Recipe for widening load/store operations.
904/// TODO: We currently execute only per-part unless a specific instance is
905/// provided.
906class VPWidenMemoryInstructionRecipe : public VPRecipeBase {
907private:
908 Instruction &Instr;
909 std::unique_ptr<VPUser> User;
910
911public:
912 VPWidenMemoryInstructionRecipe(Instruction &Instr, VPValue *Mask)
913 : VPRecipeBase(VPWidenMemoryInstructionSC), Instr(Instr) {
914 if (Mask) // Create a VPInstruction to register as a user of the mask.
915 User.reset(new VPUser({Mask}));
916 }
917
918 /// Method to support type inquiry through isa, cast, and dyn_cast.
919 static inline bool classof(const VPRecipeBase *V) {
920 return V->getVPRecipeID() == VPRecipeBase::VPWidenMemoryInstructionSC;
921 }
922
923 /// Generate the wide load/store.
924 void execute(VPTransformState &State) override;
925
926 /// Print the recipe.
927 void print(raw_ostream &O, const Twine &Indent) const override;
928};
929
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000930/// VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph. It
931/// holds a sequence of zero or more VPRecipe's each representing a sequence of
932/// output IR instructions.
933class VPBasicBlock : public VPBlockBase {
934public:
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000935 using RecipeListTy = iplist<VPRecipeBase>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000936
937private:
938 /// The VPRecipes held in the order of output instructions to generate.
939 RecipeListTy Recipes;
940
941public:
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000942 VPBasicBlock(const Twine &Name = "", VPRecipeBase *Recipe = nullptr)
943 : VPBlockBase(VPBasicBlockSC, Name.str()) {
944 if (Recipe)
945 appendRecipe(Recipe);
946 }
947
948 ~VPBasicBlock() override { Recipes.clear(); }
949
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000950 /// Instruction iterators...
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000951 using iterator = RecipeListTy::iterator;
952 using const_iterator = RecipeListTy::const_iterator;
953 using reverse_iterator = RecipeListTy::reverse_iterator;
954 using const_reverse_iterator = RecipeListTy::const_reverse_iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000955
956 //===--------------------------------------------------------------------===//
957 /// Recipe iterator methods
958 ///
959 inline iterator begin() { return Recipes.begin(); }
960 inline const_iterator begin() const { return Recipes.begin(); }
961 inline iterator end() { return Recipes.end(); }
962 inline const_iterator end() const { return Recipes.end(); }
963
964 inline reverse_iterator rbegin() { return Recipes.rbegin(); }
965 inline const_reverse_iterator rbegin() const { return Recipes.rbegin(); }
966 inline reverse_iterator rend() { return Recipes.rend(); }
967 inline const_reverse_iterator rend() const { return Recipes.rend(); }
968
969 inline size_t size() const { return Recipes.size(); }
970 inline bool empty() const { return Recipes.empty(); }
971 inline const VPRecipeBase &front() const { return Recipes.front(); }
972 inline VPRecipeBase &front() { return Recipes.front(); }
973 inline const VPRecipeBase &back() const { return Recipes.back(); }
974 inline VPRecipeBase &back() { return Recipes.back(); }
975
Florian Hahn7591e4e2018-06-18 11:34:17 +0000976 /// Returns a reference to the list of recipes.
977 RecipeListTy &getRecipeList() { return Recipes; }
978
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000979 /// Returns a pointer to a member of the recipe list.
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000980 static RecipeListTy VPBasicBlock::*getSublistAccess(VPRecipeBase *) {
981 return &VPBasicBlock::Recipes;
982 }
983
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000984 /// Method to support type inquiry through isa, cast, and dyn_cast.
985 static inline bool classof(const VPBlockBase *V) {
986 return V->getVPBlockID() == VPBlockBase::VPBasicBlockSC;
987 }
988
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000989 void insert(VPRecipeBase *Recipe, iterator InsertPt) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000990 assert(Recipe && "No recipe to append.");
991 assert(!Recipe->Parent && "Recipe already in VPlan");
992 Recipe->Parent = this;
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000993 Recipes.insert(InsertPt, Recipe);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000994 }
995
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000996 /// Augment the existing recipes of a VPBasicBlock with an additional
997 /// \p Recipe as the last recipe.
998 void appendRecipe(VPRecipeBase *Recipe) { insert(Recipe, end()); }
999
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001000 /// The method which generates the output IR instructions that correspond to
1001 /// this VPBasicBlock, thereby "executing" the VPlan.
1002 void execute(struct VPTransformState *State) override;
1003
1004private:
1005 /// Create an IR BasicBlock to hold the output instructions generated by this
1006 /// VPBasicBlock, and return it. Update the CFGState accordingly.
1007 BasicBlock *createEmptyBasicBlock(VPTransformState::CFGState &CFG);
1008};
1009
1010/// VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks
1011/// which form a Single-Entry-Single-Exit subgraph of the output IR CFG.
1012/// A VPRegionBlock may indicate that its contents are to be replicated several
1013/// times. This is designed to support predicated scalarization, in which a
1014/// scalar if-then code structure needs to be generated VF * UF times. Having
1015/// this replication indicator helps to keep a single model for multiple
1016/// candidate VF's. The actual replication takes place only once the desired VF
1017/// and UF have been determined.
1018class VPRegionBlock : public VPBlockBase {
1019private:
1020 /// Hold the Single Entry of the SESE region modelled by the VPRegionBlock.
1021 VPBlockBase *Entry;
1022
1023 /// Hold the Single Exit of the SESE region modelled by the VPRegionBlock.
1024 VPBlockBase *Exit;
1025
1026 /// An indicator whether this region is to generate multiple replicated
1027 /// instances of output IR corresponding to its VPBlockBases.
1028 bool IsReplicator;
1029
1030public:
1031 VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exit,
1032 const std::string &Name = "", bool IsReplicator = false)
1033 : VPBlockBase(VPRegionBlockSC, Name), Entry(Entry), Exit(Exit),
1034 IsReplicator(IsReplicator) {
1035 assert(Entry->getPredecessors().empty() && "Entry block has predecessors.");
1036 assert(Exit->getSuccessors().empty() && "Exit block has successors.");
1037 Entry->setParent(this);
1038 Exit->setParent(this);
1039 }
Diego Caballero168d04d2018-05-21 18:14:23 +00001040 VPRegionBlock(const std::string &Name = "", bool IsReplicator = false)
1041 : VPBlockBase(VPRegionBlockSC, Name), Entry(nullptr), Exit(nullptr),
1042 IsReplicator(IsReplicator) {}
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001043
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001044 ~VPRegionBlock() override {
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001045 if (Entry)
1046 deleteCFG(Entry);
1047 }
1048
1049 /// Method to support type inquiry through isa, cast, and dyn_cast.
1050 static inline bool classof(const VPBlockBase *V) {
1051 return V->getVPBlockID() == VPBlockBase::VPRegionBlockSC;
1052 }
1053
1054 const VPBlockBase *getEntry() const { return Entry; }
1055 VPBlockBase *getEntry() { return Entry; }
1056
Diego Caballero168d04d2018-05-21 18:14:23 +00001057 /// Set \p EntryBlock as the entry VPBlockBase of this VPRegionBlock. \p
1058 /// EntryBlock must have no predecessors.
1059 void setEntry(VPBlockBase *EntryBlock) {
1060 assert(EntryBlock->getPredecessors().empty() &&
1061 "Entry block cannot have predecessors.");
1062 Entry = EntryBlock;
1063 EntryBlock->setParent(this);
1064 }
1065
Diego Caballero2a34ac82018-07-30 21:33:31 +00001066 // FIXME: DominatorTreeBase is doing 'A->getParent()->front()'. 'front' is a
1067 // specific interface of llvm::Function, instead of using
1068 // GraphTraints::getEntryNode. We should add a new template parameter to
1069 // DominatorTreeBase representing the Graph type.
1070 VPBlockBase &front() const { return *Entry; }
1071
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001072 const VPBlockBase *getExit() const { return Exit; }
1073 VPBlockBase *getExit() { return Exit; }
1074
Diego Caballero168d04d2018-05-21 18:14:23 +00001075 /// Set \p ExitBlock as the exit VPBlockBase of this VPRegionBlock. \p
1076 /// ExitBlock must have no successors.
1077 void setExit(VPBlockBase *ExitBlock) {
1078 assert(ExitBlock->getSuccessors().empty() &&
1079 "Exit block cannot have successors.");
1080 Exit = ExitBlock;
1081 ExitBlock->setParent(this);
1082 }
1083
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001084 /// An indicator whether this region is to generate multiple replicated
1085 /// instances of output IR corresponding to its VPBlockBases.
1086 bool isReplicator() const { return IsReplicator; }
1087
1088 /// The method which generates the output IR instructions that correspond to
1089 /// this VPRegionBlock, thereby "executing" the VPlan.
1090 void execute(struct VPTransformState *State) override;
1091};
1092
1093/// VPlan models a candidate for vectorization, encoding various decisions take
1094/// to produce efficient output IR, including which branches, basic-blocks and
1095/// output IR instructions to generate, and their cost. VPlan holds a
1096/// Hierarchical-CFG of VPBasicBlocks and VPRegionBlocks rooted at an Entry
1097/// VPBlock.
1098class VPlan {
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001099 friend class VPlanPrinter;
1100
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001101private:
1102 /// Hold the single entry to the Hierarchical CFG of the VPlan.
1103 VPBlockBase *Entry;
1104
1105 /// Holds the VFs applicable to this VPlan.
1106 SmallSet<unsigned, 2> VFs;
1107
1108 /// Holds the name of the VPlan, for printing.
1109 std::string Name;
1110
Diego Caballero168d04d2018-05-21 18:14:23 +00001111 /// Holds all the external definitions created for this VPlan.
1112 // TODO: Introduce a specific representation for external definitions in
1113 // VPlan. External definitions must be immutable and hold a pointer to its
1114 // underlying IR that will be used to implement its structural comparison
1115 // (operators '==' and '<').
Craig Topper61998282018-06-09 05:04:20 +00001116 SmallPtrSet<VPValue *, 16> VPExternalDefs;
Diego Caballero168d04d2018-05-21 18:14:23 +00001117
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001118 /// Holds a mapping between Values and their corresponding VPValue inside
1119 /// VPlan.
1120 Value2VPValueTy Value2VPValue;
1121
Diego Caballero35871502018-07-31 01:57:29 +00001122 /// Holds the VPLoopInfo analysis for this VPlan.
1123 VPLoopInfo VPLInfo;
1124
Hideki Saitod19851a2018-09-14 02:02:57 +00001125 /// Holds the condition bit values built during VPInstruction to VPRecipe transformation.
1126 SmallVector<VPValue *, 4> VPCBVs;
1127
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001128public:
1129 VPlan(VPBlockBase *Entry = nullptr) : Entry(Entry) {}
1130
1131 ~VPlan() {
1132 if (Entry)
1133 VPBlockBase::deleteCFG(Entry);
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001134 for (auto &MapEntry : Value2VPValue)
1135 delete MapEntry.second;
Diego Caballero168d04d2018-05-21 18:14:23 +00001136 for (VPValue *Def : VPExternalDefs)
1137 delete Def;
Hideki Saitod19851a2018-09-14 02:02:57 +00001138 for (VPValue *CBV : VPCBVs)
1139 delete CBV;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001140 }
1141
1142 /// Generate the IR code for this VPlan.
1143 void execute(struct VPTransformState *State);
1144
1145 VPBlockBase *getEntry() { return Entry; }
1146 const VPBlockBase *getEntry() const { return Entry; }
1147
1148 VPBlockBase *setEntry(VPBlockBase *Block) { return Entry = Block; }
1149
1150 void addVF(unsigned VF) { VFs.insert(VF); }
1151
1152 bool hasVF(unsigned VF) { return VFs.count(VF); }
1153
1154 const std::string &getName() const { return Name; }
1155
1156 void setName(const Twine &newName) { Name = newName.str(); }
1157
Diego Caballero168d04d2018-05-21 18:14:23 +00001158 /// Add \p VPVal to the pool of external definitions if it's not already
1159 /// in the pool.
1160 void addExternalDef(VPValue *VPVal) {
1161 VPExternalDefs.insert(VPVal);
1162 }
1163
Hideki Saitod19851a2018-09-14 02:02:57 +00001164 /// Add \p CBV to the vector of condition bit values.
1165 void addCBV(VPValue *CBV) {
1166 VPCBVs.push_back(CBV);
1167 }
1168
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001169 void addVPValue(Value *V) {
1170 assert(V && "Trying to add a null Value to VPlan");
1171 assert(!Value2VPValue.count(V) && "Value already exists in VPlan");
1172 Value2VPValue[V] = new VPValue();
1173 }
1174
1175 VPValue *getVPValue(Value *V) {
1176 assert(V && "Trying to get the VPValue of a null Value");
1177 assert(Value2VPValue.count(V) && "Value does not exist in VPlan");
1178 return Value2VPValue[V];
1179 }
1180
Diego Caballero35871502018-07-31 01:57:29 +00001181 /// Return the VPLoopInfo analysis for this VPlan.
1182 VPLoopInfo &getVPLoopInfo() { return VPLInfo; }
1183 const VPLoopInfo &getVPLoopInfo() const { return VPLInfo; }
1184
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001185private:
1186 /// Add to the given dominator tree the header block and every new basic block
1187 /// that was created between it and the latch block, inclusive.
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001188 static void updateDominatorTree(DominatorTree *DT,
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001189 BasicBlock *LoopPreHeaderBB,
1190 BasicBlock *LoopLatchBB);
1191};
1192
1193/// VPlanPrinter prints a given VPlan to a given output stream. The printing is
1194/// indented and follows the dot format.
1195class VPlanPrinter {
1196 friend inline raw_ostream &operator<<(raw_ostream &OS, VPlan &Plan);
1197 friend inline raw_ostream &operator<<(raw_ostream &OS,
1198 const struct VPlanIngredient &I);
1199
1200private:
1201 raw_ostream &OS;
1202 VPlan &Plan;
1203 unsigned Depth;
1204 unsigned TabWidth = 2;
1205 std::string Indent;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001206 unsigned BID = 0;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001207 SmallDenseMap<const VPBlockBase *, unsigned> BlockID;
1208
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001209 VPlanPrinter(raw_ostream &O, VPlan &P) : OS(O), Plan(P) {}
1210
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001211 /// Handle indentation.
1212 void bumpIndent(int b) { Indent = std::string((Depth += b) * TabWidth, ' '); }
1213
1214 /// Print a given \p Block of the Plan.
1215 void dumpBlock(const VPBlockBase *Block);
1216
1217 /// Print the information related to the CFG edges going out of a given
1218 /// \p Block, followed by printing the successor blocks themselves.
1219 void dumpEdges(const VPBlockBase *Block);
1220
1221 /// Print a given \p BasicBlock, including its VPRecipes, followed by printing
1222 /// its successor blocks.
1223 void dumpBasicBlock(const VPBasicBlock *BasicBlock);
1224
1225 /// Print a given \p Region of the Plan.
1226 void dumpRegion(const VPRegionBlock *Region);
1227
1228 unsigned getOrCreateBID(const VPBlockBase *Block) {
1229 return BlockID.count(Block) ? BlockID[Block] : BlockID[Block] = BID++;
1230 }
1231
1232 const Twine getOrCreateName(const VPBlockBase *Block);
1233
1234 const Twine getUID(const VPBlockBase *Block);
1235
1236 /// Print the information related to a CFG edge between two VPBlockBases.
1237 void drawEdge(const VPBlockBase *From, const VPBlockBase *To, bool Hidden,
1238 const Twine &Label);
1239
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001240 void dump();
1241
1242 static void printAsIngredient(raw_ostream &O, Value *V);
1243};
1244
1245struct VPlanIngredient {
1246 Value *V;
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001247
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001248 VPlanIngredient(Value *V) : V(V) {}
1249};
1250
1251inline raw_ostream &operator<<(raw_ostream &OS, const VPlanIngredient &I) {
1252 VPlanPrinter::printAsIngredient(OS, I.V);
1253 return OS;
1254}
1255
1256inline raw_ostream &operator<<(raw_ostream &OS, VPlan &Plan) {
1257 VPlanPrinter Printer(OS, Plan);
1258 Printer.dump();
1259 return OS;
1260}
1261
Diego Caballero2a34ac82018-07-30 21:33:31 +00001262//===----------------------------------------------------------------------===//
1263// GraphTraits specializations for VPlan Hierarchical Control-Flow Graphs //
1264//===----------------------------------------------------------------------===//
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001265
Diego Caballero2a34ac82018-07-30 21:33:31 +00001266// The following set of template specializations implement GraphTraits to treat
1267// any VPBlockBase as a node in a graph of VPBlockBases. It's important to note
1268// that VPBlockBase traits don't recurse into VPRegioBlocks, i.e., if the
1269// VPBlockBase is a VPRegionBlock, this specialization provides access to its
1270// successors/predecessors but not to the blocks inside the region.
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001271
1272template <> struct GraphTraits<VPBlockBase *> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001273 using NodeRef = VPBlockBase *;
1274 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001275
1276 static NodeRef getEntryNode(NodeRef N) { return N; }
1277
1278 static inline ChildIteratorType child_begin(NodeRef N) {
1279 return N->getSuccessors().begin();
1280 }
1281
1282 static inline ChildIteratorType child_end(NodeRef N) {
1283 return N->getSuccessors().end();
1284 }
1285};
1286
1287template <> struct GraphTraits<const VPBlockBase *> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001288 using NodeRef = const VPBlockBase *;
1289 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::const_iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001290
1291 static NodeRef getEntryNode(NodeRef N) { return N; }
1292
1293 static inline ChildIteratorType child_begin(NodeRef N) {
1294 return N->getSuccessors().begin();
1295 }
1296
1297 static inline ChildIteratorType child_end(NodeRef N) {
1298 return N->getSuccessors().end();
1299 }
1300};
1301
Diego Caballero2a34ac82018-07-30 21:33:31 +00001302// Inverse order specialization for VPBasicBlocks. Predecessors are used instead
1303// of successors for the inverse traversal.
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001304template <> struct GraphTraits<Inverse<VPBlockBase *>> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001305 using NodeRef = VPBlockBase *;
1306 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001307
Diego Caballero2a34ac82018-07-30 21:33:31 +00001308 static NodeRef getEntryNode(Inverse<NodeRef> B) { return B.Graph; }
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001309
1310 static inline ChildIteratorType child_begin(NodeRef N) {
1311 return N->getPredecessors().begin();
1312 }
1313
1314 static inline ChildIteratorType child_end(NodeRef N) {
1315 return N->getPredecessors().end();
1316 }
1317};
1318
Diego Caballero2a34ac82018-07-30 21:33:31 +00001319// The following set of template specializations implement GraphTraits to
1320// treat VPRegionBlock as a graph and recurse inside its nodes. It's important
1321// to note that the blocks inside the VPRegionBlock are treated as VPBlockBases
1322// (i.e., no dyn_cast is performed, VPBlockBases specialization is used), so
1323// there won't be automatic recursion into other VPBlockBases that turn to be
1324// VPRegionBlocks.
1325
1326template <>
1327struct GraphTraits<VPRegionBlock *> : public GraphTraits<VPBlockBase *> {
1328 using GraphRef = VPRegionBlock *;
1329 using nodes_iterator = df_iterator<NodeRef>;
1330
1331 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
1332
1333 static nodes_iterator nodes_begin(GraphRef N) {
1334 return nodes_iterator::begin(N->getEntry());
1335 }
1336
1337 static nodes_iterator nodes_end(GraphRef N) {
1338 // df_iterator::end() returns an empty iterator so the node used doesn't
1339 // matter.
1340 return nodes_iterator::end(N);
1341 }
1342};
1343
1344template <>
1345struct GraphTraits<const VPRegionBlock *>
1346 : public GraphTraits<const VPBlockBase *> {
1347 using GraphRef = const VPRegionBlock *;
1348 using nodes_iterator = df_iterator<NodeRef>;
1349
1350 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
1351
1352 static nodes_iterator nodes_begin(GraphRef N) {
1353 return nodes_iterator::begin(N->getEntry());
1354 }
1355
1356 static nodes_iterator nodes_end(GraphRef N) {
1357 // df_iterator::end() returns an empty iterator so the node used doesn't
1358 // matter.
1359 return nodes_iterator::end(N);
1360 }
1361};
1362
1363template <>
1364struct GraphTraits<Inverse<VPRegionBlock *>>
1365 : public GraphTraits<Inverse<VPBlockBase *>> {
1366 using GraphRef = VPRegionBlock *;
1367 using nodes_iterator = df_iterator<NodeRef>;
1368
1369 static NodeRef getEntryNode(Inverse<GraphRef> N) {
1370 return N.Graph->getExit();
1371 }
1372
1373 static nodes_iterator nodes_begin(GraphRef N) {
1374 return nodes_iterator::begin(N->getExit());
1375 }
1376
1377 static nodes_iterator nodes_end(GraphRef N) {
1378 // df_iterator::end() returns an empty iterator so the node used doesn't
1379 // matter.
1380 return nodes_iterator::end(N);
1381 }
1382};
1383
Diego Caballero168d04d2018-05-21 18:14:23 +00001384//===----------------------------------------------------------------------===//
1385// VPlan Utilities
1386//===----------------------------------------------------------------------===//
1387
1388/// Class that provides utilities for VPBlockBases in VPlan.
1389class VPBlockUtils {
1390public:
1391 VPBlockUtils() = delete;
1392
1393 /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
Diego Caballerod0953012018-07-09 15:57:09 +00001394 /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
1395 /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. If \p BlockPtr
1396 /// has more than one successor, its conditional bit is propagated to \p
1397 /// NewBlock. \p NewBlock must have neither successors nor predecessors.
Diego Caballero168d04d2018-05-21 18:14:23 +00001398 static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
1399 assert(NewBlock->getSuccessors().empty() &&
1400 "Can't insert new block with successors.");
1401 // TODO: move successors from BlockPtr to NewBlock when this functionality
1402 // is necessary. For now, setBlockSingleSuccessor will assert if BlockPtr
1403 // already has successors.
1404 BlockPtr->setOneSuccessor(NewBlock);
1405 NewBlock->setPredecessors({BlockPtr});
1406 NewBlock->setParent(BlockPtr->getParent());
1407 }
1408
1409 /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
1410 /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
1411 /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
Diego Caballerod0953012018-07-09 15:57:09 +00001412 /// parent to \p IfTrue and \p IfFalse. \p Condition is set as the successor
1413 /// selector. \p BlockPtr must have no successors and \p IfTrue and \p IfFalse
1414 /// must have neither successors nor predecessors.
Diego Caballero168d04d2018-05-21 18:14:23 +00001415 static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
Diego Caballerod0953012018-07-09 15:57:09 +00001416 VPValue *Condition, VPBlockBase *BlockPtr) {
Diego Caballero168d04d2018-05-21 18:14:23 +00001417 assert(IfTrue->getSuccessors().empty() &&
1418 "Can't insert IfTrue with successors.");
1419 assert(IfFalse->getSuccessors().empty() &&
1420 "Can't insert IfFalse with successors.");
Diego Caballerod0953012018-07-09 15:57:09 +00001421 BlockPtr->setTwoSuccessors(IfTrue, IfFalse, Condition);
Diego Caballero168d04d2018-05-21 18:14:23 +00001422 IfTrue->setPredecessors({BlockPtr});
1423 IfFalse->setPredecessors({BlockPtr});
1424 IfTrue->setParent(BlockPtr->getParent());
1425 IfFalse->setParent(BlockPtr->getParent());
1426 }
1427
1428 /// Connect VPBlockBases \p From and \p To bi-directionally. Append \p To to
1429 /// the successors of \p From and \p From to the predecessors of \p To. Both
1430 /// VPBlockBases must have the same parent, which can be null. Both
1431 /// VPBlockBases can be already connected to other VPBlockBases.
1432 static void connectBlocks(VPBlockBase *From, VPBlockBase *To) {
1433 assert((From->getParent() == To->getParent()) &&
1434 "Can't connect two block with different parents");
1435 assert(From->getNumSuccessors() < 2 &&
1436 "Blocks can't have more than two successors.");
1437 From->appendSuccessor(To);
1438 To->appendPredecessor(From);
1439 }
1440
1441 /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
1442 /// from the successors of \p From and \p From from the predecessors of \p To.
1443 static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
1444 assert(To && "Successor to disconnect is null.");
1445 From->removeSuccessor(To);
1446 To->removePredecessor(From);
1447 }
1448};
Florian Hahn45e5d5b2018-06-08 17:30:45 +00001449
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001450} // end namespace llvm
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001451
1452#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_H