blob: 9daaea1acdedd082396cc69d92742f38f5797b3e [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 Zaksb0b53122018-10-18 15:03:15 +0000320 /// Hold the trip count of the scalar loop.
321 Value *TripCount = nullptr;
322
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000323 /// Hold a pointer to InnerLoopVectorizer to reuse its IR generation methods.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000324 InnerLoopVectorizer *ILV;
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000325
326 VPCallback &Callback;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000327};
328
329/// VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
330/// A VPBlockBase can be either a VPBasicBlock or a VPRegionBlock.
331class VPBlockBase {
Diego Caballero168d04d2018-05-21 18:14:23 +0000332 friend class VPBlockUtils;
333
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000334private:
335 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
336
337 /// An optional name for the block.
338 std::string Name;
339
340 /// The immediate VPRegionBlock which this VPBlockBase belongs to, or null if
341 /// it is a topmost VPBlockBase.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000342 VPRegionBlock *Parent = nullptr;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000343
344 /// List of predecessor blocks.
345 SmallVector<VPBlockBase *, 1> Predecessors;
346
347 /// List of successor blocks.
348 SmallVector<VPBlockBase *, 1> Successors;
349
Diego Caballerod0953012018-07-09 15:57:09 +0000350 /// Successor selector, null for zero or single successor blocks.
351 VPValue *CondBit = nullptr;
352
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000353 /// Add \p Successor as the last successor to this block.
354 void appendSuccessor(VPBlockBase *Successor) {
355 assert(Successor && "Cannot add nullptr successor!");
356 Successors.push_back(Successor);
357 }
358
359 /// Add \p Predecessor as the last predecessor to this block.
360 void appendPredecessor(VPBlockBase *Predecessor) {
361 assert(Predecessor && "Cannot add nullptr predecessor!");
362 Predecessors.push_back(Predecessor);
363 }
364
365 /// Remove \p Predecessor from the predecessors of this block.
366 void removePredecessor(VPBlockBase *Predecessor) {
367 auto Pos = std::find(Predecessors.begin(), Predecessors.end(), Predecessor);
368 assert(Pos && "Predecessor does not exist");
369 Predecessors.erase(Pos);
370 }
371
372 /// Remove \p Successor from the successors of this block.
373 void removeSuccessor(VPBlockBase *Successor) {
374 auto Pos = std::find(Successors.begin(), Successors.end(), Successor);
375 assert(Pos && "Successor does not exist");
376 Successors.erase(Pos);
377 }
378
379protected:
380 VPBlockBase(const unsigned char SC, const std::string &N)
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000381 : SubclassID(SC), Name(N) {}
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000382
383public:
384 /// An enumeration for keeping track of the concrete subclass of VPBlockBase
385 /// that are actually instantiated. Values of this enumeration are kept in the
386 /// SubclassID field of the VPBlockBase objects. They are used for concrete
387 /// type identification.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000388 using VPBlockTy = enum { VPBasicBlockSC, VPRegionBlockSC };
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000389
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000390 using VPBlocksTy = SmallVectorImpl<VPBlockBase *>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000391
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000392 virtual ~VPBlockBase() = default;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000393
394 const std::string &getName() const { return Name; }
395
396 void setName(const Twine &newName) { Name = newName.str(); }
397
398 /// \return an ID for the concrete type of this object.
399 /// This is used to implement the classof checks. This should not be used
400 /// for any other purpose, as the values may change as LLVM evolves.
401 unsigned getVPBlockID() const { return SubclassID; }
402
Diego Caballero168d04d2018-05-21 18:14:23 +0000403 VPRegionBlock *getParent() { return Parent; }
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000404 const VPRegionBlock *getParent() const { return Parent; }
405
406 void setParent(VPRegionBlock *P) { Parent = P; }
407
408 /// \return the VPBasicBlock that is the entry of this VPBlockBase,
409 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
410 /// VPBlockBase is a VPBasicBlock, it is returned.
411 const VPBasicBlock *getEntryBasicBlock() const;
412 VPBasicBlock *getEntryBasicBlock();
413
414 /// \return the VPBasicBlock that is the exit of this VPBlockBase,
415 /// recursively, if the latter is a VPRegionBlock. Otherwise, if this
416 /// VPBlockBase is a VPBasicBlock, it is returned.
417 const VPBasicBlock *getExitBasicBlock() const;
418 VPBasicBlock *getExitBasicBlock();
419
420 const VPBlocksTy &getSuccessors() const { return Successors; }
421 VPBlocksTy &getSuccessors() { return Successors; }
422
423 const VPBlocksTy &getPredecessors() const { return Predecessors; }
424 VPBlocksTy &getPredecessors() { return Predecessors; }
425
426 /// \return the successor of this VPBlockBase if it has a single successor.
427 /// Otherwise return a null pointer.
428 VPBlockBase *getSingleSuccessor() const {
429 return (Successors.size() == 1 ? *Successors.begin() : nullptr);
430 }
431
432 /// \return the predecessor of this VPBlockBase if it has a single
433 /// predecessor. Otherwise return a null pointer.
434 VPBlockBase *getSinglePredecessor() const {
435 return (Predecessors.size() == 1 ? *Predecessors.begin() : nullptr);
436 }
437
Diego Caballero168d04d2018-05-21 18:14:23 +0000438 size_t getNumSuccessors() const { return Successors.size(); }
439 size_t getNumPredecessors() const { return Predecessors.size(); }
440
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000441 /// An Enclosing Block of a block B is any block containing B, including B
442 /// itself. \return the closest enclosing block starting from "this", which
443 /// has successors. \return the root enclosing block if all enclosing blocks
444 /// have no successors.
445 VPBlockBase *getEnclosingBlockWithSuccessors();
446
447 /// \return the closest enclosing block starting from "this", which has
448 /// predecessors. \return the root enclosing block if all enclosing blocks
449 /// have no predecessors.
450 VPBlockBase *getEnclosingBlockWithPredecessors();
451
452 /// \return the successors either attached directly to this VPBlockBase or, if
453 /// this VPBlockBase is the exit block of a VPRegionBlock and has no
454 /// successors of its own, search recursively for the first enclosing
455 /// VPRegionBlock that has successors and return them. If no such
456 /// VPRegionBlock exists, return the (empty) successors of the topmost
457 /// VPBlockBase reached.
458 const VPBlocksTy &getHierarchicalSuccessors() {
459 return getEnclosingBlockWithSuccessors()->getSuccessors();
460 }
461
462 /// \return the hierarchical successor of this VPBlockBase if it has a single
463 /// hierarchical successor. Otherwise return a null pointer.
464 VPBlockBase *getSingleHierarchicalSuccessor() {
465 return getEnclosingBlockWithSuccessors()->getSingleSuccessor();
466 }
467
468 /// \return the predecessors either attached directly to this VPBlockBase or,
469 /// if this VPBlockBase is the entry block of a VPRegionBlock and has no
470 /// predecessors of its own, search recursively for the first enclosing
471 /// VPRegionBlock that has predecessors and return them. If no such
472 /// VPRegionBlock exists, return the (empty) predecessors of the topmost
473 /// VPBlockBase reached.
474 const VPBlocksTy &getHierarchicalPredecessors() {
475 return getEnclosingBlockWithPredecessors()->getPredecessors();
476 }
477
478 /// \return the hierarchical predecessor of this VPBlockBase if it has a
479 /// single hierarchical predecessor. Otherwise return a null pointer.
480 VPBlockBase *getSingleHierarchicalPredecessor() {
481 return getEnclosingBlockWithPredecessors()->getSinglePredecessor();
482 }
483
Diego Caballerod0953012018-07-09 15:57:09 +0000484 /// \return the condition bit selecting the successor.
485 VPValue *getCondBit() { return CondBit; }
486
487 const VPValue *getCondBit() const { return CondBit; }
488
489 void setCondBit(VPValue *CV) { CondBit = CV; }
490
Diego Caballero168d04d2018-05-21 18:14:23 +0000491 /// Set a given VPBlockBase \p Successor as the single successor of this
492 /// VPBlockBase. This VPBlockBase is not added as predecessor of \p Successor.
493 /// This VPBlockBase must have no successors.
494 void setOneSuccessor(VPBlockBase *Successor) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000495 assert(Successors.empty() && "Setting one successor when others exist.");
496 appendSuccessor(Successor);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000497 }
498
Diego Caballero168d04d2018-05-21 18:14:23 +0000499 /// Set two given VPBlockBases \p IfTrue and \p IfFalse to be the two
Diego Caballerod0953012018-07-09 15:57:09 +0000500 /// successors of this VPBlockBase. \p Condition is set as the successor
501 /// selector. This VPBlockBase is not added as predecessor of \p IfTrue or \p
502 /// IfFalse. This VPBlockBase must have no successors.
503 void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
504 VPValue *Condition) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000505 assert(Successors.empty() && "Setting two successors when others exist.");
Diego Caballerod0953012018-07-09 15:57:09 +0000506 assert(Condition && "Setting two successors without condition!");
507 CondBit = Condition;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000508 appendSuccessor(IfTrue);
509 appendSuccessor(IfFalse);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000510 }
511
Diego Caballero168d04d2018-05-21 18:14:23 +0000512 /// Set each VPBasicBlock in \p NewPreds as predecessor of this VPBlockBase.
513 /// This VPBlockBase must have no predecessors. This VPBlockBase is not added
514 /// as successor of any VPBasicBlock in \p NewPreds.
515 void setPredecessors(ArrayRef<VPBlockBase *> NewPreds) {
516 assert(Predecessors.empty() && "Block predecessors already set.");
517 for (auto *Pred : NewPreds)
518 appendPredecessor(Pred);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000519 }
520
521 /// The method which generates the output IR that correspond to this
522 /// VPBlockBase, thereby "executing" the VPlan.
523 virtual void execute(struct VPTransformState *State) = 0;
524
525 /// Delete all blocks reachable from a given VPBlockBase, inclusive.
526 static void deleteCFG(VPBlockBase *Entry);
Diego Caballero2a34ac82018-07-30 21:33:31 +0000527
528 void printAsOperand(raw_ostream &OS, bool PrintType) const {
529 OS << getName();
530 }
531
532 void print(raw_ostream &OS) const {
533 // TODO: Only printing VPBB name for now since we only have dot printing
534 // support for VPInstructions/Recipes.
535 printAsOperand(OS, false);
536 }
Diego Caballero35871502018-07-31 01:57:29 +0000537
538 /// Return true if it is legal to hoist instructions into this block.
539 bool isLegalToHoistInto() {
540 // There are currently no constraints that prevent an instruction to be
541 // hoisted into a VPBlockBase.
542 return true;
543 }
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000544};
545
546/// VPRecipeBase is a base class modeling a sequence of one or more output IR
547/// instructions.
548class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock> {
549 friend VPBasicBlock;
550
551private:
552 const unsigned char SubclassID; ///< Subclass identifier (for isa/dyn_cast).
553
554 /// Each VPRecipe belongs to a single VPBasicBlock.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000555 VPBasicBlock *Parent = nullptr;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000556
557public:
558 /// An enumeration for keeping track of the concrete subclass of VPRecipeBase
559 /// that is actually instantiated. Values of this enumeration are kept in the
560 /// SubclassID field of the VPRecipeBase objects. They are used for concrete
561 /// type identification.
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000562 using VPRecipeTy = enum {
Gil Rapaport848581c2017-11-14 12:09:30 +0000563 VPBlendSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000564 VPBranchOnMaskSC,
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000565 VPInstructionSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000566 VPInterleaveSC,
567 VPPredInstPHISC,
568 VPReplicateSC,
569 VPWidenIntOrFpInductionSC,
Gil Rapaport848581c2017-11-14 12:09:30 +0000570 VPWidenMemoryInstructionSC,
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000571 VPWidenPHISC,
572 VPWidenSC,
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000573 };
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000574
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000575 VPRecipeBase(const unsigned char SC) : SubclassID(SC) {}
576 virtual ~VPRecipeBase() = default;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000577
578 /// \return an ID for the concrete type of this object.
579 /// This is used to implement the classof checks. This should not be used
580 /// for any other purpose, as the values may change as LLVM evolves.
581 unsigned getVPRecipeID() const { return SubclassID; }
582
583 /// \return the VPBasicBlock which this VPRecipe belongs to.
584 VPBasicBlock *getParent() { return Parent; }
585 const VPBasicBlock *getParent() const { return Parent; }
586
587 /// The method which generates the output IR instructions that correspond to
588 /// this VPRecipe, thereby "executing" the VPlan.
589 virtual void execute(struct VPTransformState &State) = 0;
590
591 /// Each recipe prints itself.
592 virtual void print(raw_ostream &O, const Twine &Indent) const = 0;
Florian Hahn7591e4e2018-06-18 11:34:17 +0000593
594 /// Insert an unlinked recipe into a basic block immediately before
595 /// the specified recipe.
596 void insertBefore(VPRecipeBase *InsertPos);
Florian Hahn63cbcf92018-06-18 15:18:48 +0000597
598 /// This method unlinks 'this' from the containing basic block and deletes it.
599 ///
600 /// \returns an iterator pointing to the element after the erased one
601 iplist<VPRecipeBase>::iterator eraseFromParent();
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000602};
603
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000604/// This is a concrete Recipe that models a single VPlan-level instruction.
605/// While as any Recipe it may generate a sequence of IR instructions when
606/// executed, these instructions would always form a single-def expression as
607/// the VPInstruction is also a single def-use vertex.
608class VPInstruction : public VPUser, public VPRecipeBase {
Florian Hahn3385caa2018-06-18 18:28:49 +0000609 friend class VPlanHCFGTransforms;
610
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000611public:
612 /// VPlan opcodes, extending LLVM IR with idiomatics instructions.
Ayal Zaksb0b53122018-10-18 15:03:15 +0000613 enum { Not = Instruction::OtherOpsEnd + 1, ICmpULE };
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000614
615private:
616 typedef unsigned char OpcodeTy;
617 OpcodeTy Opcode;
618
619 /// Utility method serving execute(): generates a single instance of the
620 /// modeled instruction.
621 void generateInstruction(VPTransformState &State, unsigned Part);
622
623public:
Diego Caballero168d04d2018-05-21 18:14:23 +0000624 VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands)
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000625 : VPUser(VPValue::VPInstructionSC, Operands),
626 VPRecipeBase(VPRecipeBase::VPInstructionSC), Opcode(Opcode) {}
627
Diego Caballero168d04d2018-05-21 18:14:23 +0000628 VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands)
629 : VPInstruction(Opcode, ArrayRef<VPValue *>(Operands)) {}
630
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000631 /// Method to support type inquiry through isa, cast, and dyn_cast.
632 static inline bool classof(const VPValue *V) {
633 return V->getVPValueID() == VPValue::VPInstructionSC;
634 }
635
636 /// Method to support type inquiry through isa, cast, and dyn_cast.
637 static inline bool classof(const VPRecipeBase *R) {
638 return R->getVPRecipeID() == VPRecipeBase::VPInstructionSC;
639 }
640
641 unsigned getOpcode() const { return Opcode; }
642
643 /// Generate the instruction.
644 /// TODO: We currently execute only per-part unless a specific instance is
645 /// provided.
646 void execute(VPTransformState &State) override;
647
648 /// Print the Recipe.
649 void print(raw_ostream &O, const Twine &Indent) const override;
650
651 /// Print the VPInstruction.
652 void print(raw_ostream &O) const;
653};
654
Hal Finkel7333aa92017-12-16 01:12:50 +0000655/// VPWidenRecipe is a recipe for producing a copy of vector type for each
656/// Instruction in its ingredients independently, in order. This recipe covers
657/// most of the traditional vectorization cases where each ingredient transforms
658/// into a vectorized version of itself.
659class VPWidenRecipe : public VPRecipeBase {
660private:
661 /// Hold the ingredients by pointing to their original BasicBlock location.
662 BasicBlock::iterator Begin;
663 BasicBlock::iterator End;
664
665public:
666 VPWidenRecipe(Instruction *I) : VPRecipeBase(VPWidenSC) {
667 End = I->getIterator();
668 Begin = End++;
669 }
670
671 ~VPWidenRecipe() override = default;
672
673 /// Method to support type inquiry through isa, cast, and dyn_cast.
674 static inline bool classof(const VPRecipeBase *V) {
675 return V->getVPRecipeID() == VPRecipeBase::VPWidenSC;
676 }
677
678 /// Produce widened copies of all Ingredients.
679 void execute(VPTransformState &State) override;
680
681 /// Augment the recipe to include Instr, if it lies at its End.
682 bool appendInstruction(Instruction *Instr) {
683 if (End != Instr->getIterator())
684 return false;
685 End++;
686 return true;
687 }
688
689 /// Print the recipe.
690 void print(raw_ostream &O, const Twine &Indent) const override;
691};
692
693/// A recipe for handling phi nodes of integer and floating-point inductions,
694/// producing their vector and scalar values.
695class VPWidenIntOrFpInductionRecipe : public VPRecipeBase {
696private:
697 PHINode *IV;
698 TruncInst *Trunc;
699
700public:
701 VPWidenIntOrFpInductionRecipe(PHINode *IV, TruncInst *Trunc = nullptr)
702 : VPRecipeBase(VPWidenIntOrFpInductionSC), IV(IV), Trunc(Trunc) {}
703 ~VPWidenIntOrFpInductionRecipe() override = default;
704
705 /// Method to support type inquiry through isa, cast, and dyn_cast.
706 static inline bool classof(const VPRecipeBase *V) {
707 return V->getVPRecipeID() == VPRecipeBase::VPWidenIntOrFpInductionSC;
708 }
709
710 /// Generate the vectorized and scalarized versions of the phi node as
711 /// needed by their users.
712 void execute(VPTransformState &State) override;
713
714 /// Print the recipe.
715 void print(raw_ostream &O, const Twine &Indent) const override;
716};
717
718/// A recipe for handling all phi nodes except for integer and FP inductions.
719class VPWidenPHIRecipe : public VPRecipeBase {
720private:
721 PHINode *Phi;
722
723public:
724 VPWidenPHIRecipe(PHINode *Phi) : VPRecipeBase(VPWidenPHISC), Phi(Phi) {}
725 ~VPWidenPHIRecipe() override = default;
726
727 /// Method to support type inquiry through isa, cast, and dyn_cast.
728 static inline bool classof(const VPRecipeBase *V) {
729 return V->getVPRecipeID() == VPRecipeBase::VPWidenPHISC;
730 }
731
732 /// Generate the phi/select nodes.
733 void execute(VPTransformState &State) override;
734
735 /// Print the recipe.
736 void print(raw_ostream &O, const Twine &Indent) const override;
737};
738
739/// A recipe for vectorizing a phi-node as a sequence of mask-based select
740/// instructions.
741class VPBlendRecipe : public VPRecipeBase {
742private:
743 PHINode *Phi;
744
745 /// The blend operation is a User of a mask, if not null.
746 std::unique_ptr<VPUser> User;
747
748public:
749 VPBlendRecipe(PHINode *Phi, ArrayRef<VPValue *> Masks)
750 : VPRecipeBase(VPBlendSC), Phi(Phi) {
751 assert((Phi->getNumIncomingValues() == 1 ||
752 Phi->getNumIncomingValues() == Masks.size()) &&
753 "Expected the same number of incoming values and masks");
754 if (!Masks.empty())
755 User.reset(new VPUser(Masks));
756 }
757
758 /// Method to support type inquiry through isa, cast, and dyn_cast.
759 static inline bool classof(const VPRecipeBase *V) {
760 return V->getVPRecipeID() == VPRecipeBase::VPBlendSC;
761 }
762
763 /// Generate the phi/select nodes.
764 void execute(VPTransformState &State) override;
765
766 /// Print the recipe.
767 void print(raw_ostream &O, const Twine &Indent) const override;
768};
769
770/// VPInterleaveRecipe is a recipe for transforming an interleave group of load
771/// or stores into one wide load/store and shuffles.
772class VPInterleaveRecipe : public VPRecipeBase {
773private:
774 const InterleaveGroup *IG;
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000775 std::unique_ptr<VPUser> User;
Hal Finkel7333aa92017-12-16 01:12:50 +0000776
777public:
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000778 VPInterleaveRecipe(const InterleaveGroup *IG, VPValue *Mask)
779 : VPRecipeBase(VPInterleaveSC), IG(IG) {
780 if (Mask) // Create a VPInstruction to register as a user of the mask.
781 User.reset(new VPUser({Mask}));
782 }
Hal Finkel7333aa92017-12-16 01:12:50 +0000783 ~VPInterleaveRecipe() override = default;
784
785 /// Method to support type inquiry through isa, cast, and dyn_cast.
786 static inline bool classof(const VPRecipeBase *V) {
787 return V->getVPRecipeID() == VPRecipeBase::VPInterleaveSC;
788 }
789
790 /// Generate the wide load or store, and shuffles.
791 void execute(VPTransformState &State) override;
792
793 /// Print the recipe.
794 void print(raw_ostream &O, const Twine &Indent) const override;
795
796 const InterleaveGroup *getInterleaveGroup() { return IG; }
797};
798
799/// VPReplicateRecipe replicates a given instruction producing multiple scalar
800/// copies of the original scalar type, one per lane, instead of producing a
801/// single copy of widened type for all lanes. If the instruction is known to be
802/// uniform only one copy, per lane zero, will be generated.
803class VPReplicateRecipe : public VPRecipeBase {
804private:
805 /// The instruction being replicated.
806 Instruction *Ingredient;
807
808 /// Indicator if only a single replica per lane is needed.
809 bool IsUniform;
810
811 /// Indicator if the replicas are also predicated.
812 bool IsPredicated;
813
814 /// Indicator if the scalar values should also be packed into a vector.
815 bool AlsoPack;
816
817public:
818 VPReplicateRecipe(Instruction *I, bool IsUniform, bool IsPredicated = false)
819 : VPRecipeBase(VPReplicateSC), Ingredient(I), IsUniform(IsUniform),
820 IsPredicated(IsPredicated) {
821 // Retain the previous behavior of predicateInstructions(), where an
822 // insert-element of a predicated instruction got hoisted into the
823 // predicated basic block iff it was its only user. This is achieved by
824 // having predicated instructions also pack their values into a vector by
825 // default unless they have a replicated user which uses their scalar value.
826 AlsoPack = IsPredicated && !I->use_empty();
827 }
828
829 ~VPReplicateRecipe() override = default;
830
831 /// Method to support type inquiry through isa, cast, and dyn_cast.
832 static inline bool classof(const VPRecipeBase *V) {
833 return V->getVPRecipeID() == VPRecipeBase::VPReplicateSC;
834 }
835
836 /// Generate replicas of the desired Ingredient. Replicas will be generated
837 /// for all parts and lanes unless a specific part and lane are specified in
838 /// the \p State.
839 void execute(VPTransformState &State) override;
840
841 void setAlsoPack(bool Pack) { AlsoPack = Pack; }
842
843 /// Print the recipe.
844 void print(raw_ostream &O, const Twine &Indent) const override;
845};
846
847/// A recipe for generating conditional branches on the bits of a mask.
848class VPBranchOnMaskRecipe : public VPRecipeBase {
849private:
850 std::unique_ptr<VPUser> User;
851
852public:
853 VPBranchOnMaskRecipe(VPValue *BlockInMask) : VPRecipeBase(VPBranchOnMaskSC) {
854 if (BlockInMask) // nullptr means all-one mask.
855 User.reset(new VPUser({BlockInMask}));
856 }
857
858 /// Method to support type inquiry through isa, cast, and dyn_cast.
859 static inline bool classof(const VPRecipeBase *V) {
860 return V->getVPRecipeID() == VPRecipeBase::VPBranchOnMaskSC;
861 }
862
863 /// Generate the extraction of the appropriate bit from the block mask and the
864 /// conditional branch.
865 void execute(VPTransformState &State) override;
866
867 /// Print the recipe.
868 void print(raw_ostream &O, const Twine &Indent) const override {
869 O << " +\n" << Indent << "\"BRANCH-ON-MASK ";
870 if (User)
871 O << *User->getOperand(0);
872 else
873 O << " All-One";
874 O << "\\l\"";
875 }
876};
877
878/// VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when
879/// control converges back from a Branch-on-Mask. The phi nodes are needed in
880/// order to merge values that are set under such a branch and feed their uses.
881/// The phi nodes can be scalar or vector depending on the users of the value.
882/// This recipe works in concert with VPBranchOnMaskRecipe.
883class VPPredInstPHIRecipe : public VPRecipeBase {
884private:
885 Instruction *PredInst;
886
887public:
888 /// Construct a VPPredInstPHIRecipe given \p PredInst whose value needs a phi
889 /// nodes after merging back from a Branch-on-Mask.
890 VPPredInstPHIRecipe(Instruction *PredInst)
891 : VPRecipeBase(VPPredInstPHISC), PredInst(PredInst) {}
892 ~VPPredInstPHIRecipe() override = default;
893
894 /// Method to support type inquiry through isa, cast, and dyn_cast.
895 static inline bool classof(const VPRecipeBase *V) {
896 return V->getVPRecipeID() == VPRecipeBase::VPPredInstPHISC;
897 }
898
899 /// Generates phi nodes for live-outs as needed to retain SSA form.
900 void execute(VPTransformState &State) override;
901
902 /// Print the recipe.
903 void print(raw_ostream &O, const Twine &Indent) const override;
904};
905
906/// A Recipe for widening load/store operations.
907/// TODO: We currently execute only per-part unless a specific instance is
908/// provided.
909class VPWidenMemoryInstructionRecipe : public VPRecipeBase {
910private:
911 Instruction &Instr;
912 std::unique_ptr<VPUser> User;
913
914public:
915 VPWidenMemoryInstructionRecipe(Instruction &Instr, VPValue *Mask)
916 : VPRecipeBase(VPWidenMemoryInstructionSC), Instr(Instr) {
917 if (Mask) // Create a VPInstruction to register as a user of the mask.
918 User.reset(new VPUser({Mask}));
919 }
920
921 /// Method to support type inquiry through isa, cast, and dyn_cast.
922 static inline bool classof(const VPRecipeBase *V) {
923 return V->getVPRecipeID() == VPRecipeBase::VPWidenMemoryInstructionSC;
924 }
925
926 /// Generate the wide load/store.
927 void execute(VPTransformState &State) override;
928
929 /// Print the recipe.
930 void print(raw_ostream &O, const Twine &Indent) const override;
931};
932
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000933/// VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph. It
934/// holds a sequence of zero or more VPRecipe's each representing a sequence of
935/// output IR instructions.
936class VPBasicBlock : public VPBlockBase {
937public:
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000938 using RecipeListTy = iplist<VPRecipeBase>;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000939
940private:
941 /// The VPRecipes held in the order of output instructions to generate.
942 RecipeListTy Recipes;
943
944public:
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000945 VPBasicBlock(const Twine &Name = "", VPRecipeBase *Recipe = nullptr)
946 : VPBlockBase(VPBasicBlockSC, Name.str()) {
947 if (Recipe)
948 appendRecipe(Recipe);
949 }
950
951 ~VPBasicBlock() override { Recipes.clear(); }
952
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000953 /// Instruction iterators...
Eugene Zelenko6cadde72017-10-17 21:27:42 +0000954 using iterator = RecipeListTy::iterator;
955 using const_iterator = RecipeListTy::const_iterator;
956 using reverse_iterator = RecipeListTy::reverse_iterator;
957 using const_reverse_iterator = RecipeListTy::const_reverse_iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000958
959 //===--------------------------------------------------------------------===//
960 /// Recipe iterator methods
961 ///
962 inline iterator begin() { return Recipes.begin(); }
963 inline const_iterator begin() const { return Recipes.begin(); }
964 inline iterator end() { return Recipes.end(); }
965 inline const_iterator end() const { return Recipes.end(); }
966
967 inline reverse_iterator rbegin() { return Recipes.rbegin(); }
968 inline const_reverse_iterator rbegin() const { return Recipes.rbegin(); }
969 inline reverse_iterator rend() { return Recipes.rend(); }
970 inline const_reverse_iterator rend() const { return Recipes.rend(); }
971
972 inline size_t size() const { return Recipes.size(); }
973 inline bool empty() const { return Recipes.empty(); }
974 inline const VPRecipeBase &front() const { return Recipes.front(); }
975 inline VPRecipeBase &front() { return Recipes.front(); }
976 inline const VPRecipeBase &back() const { return Recipes.back(); }
977 inline VPRecipeBase &back() { return Recipes.back(); }
978
Florian Hahn7591e4e2018-06-18 11:34:17 +0000979 /// Returns a reference to the list of recipes.
980 RecipeListTy &getRecipeList() { return Recipes; }
981
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000982 /// Returns a pointer to a member of the recipe list.
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000983 static RecipeListTy VPBasicBlock::*getSublistAccess(VPRecipeBase *) {
984 return &VPBasicBlock::Recipes;
985 }
986
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000987 /// Method to support type inquiry through isa, cast, and dyn_cast.
988 static inline bool classof(const VPBlockBase *V) {
989 return V->getVPBlockID() == VPBlockBase::VPBasicBlockSC;
990 }
991
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000992 void insert(VPRecipeBase *Recipe, iterator InsertPt) {
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000993 assert(Recipe && "No recipe to append.");
994 assert(!Recipe->Parent && "Recipe already in VPlan");
995 Recipe->Parent = this;
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000996 Recipes.insert(InsertPt, Recipe);
Ayal Zaks1f58dda2017-08-27 12:55:46 +0000997 }
998
Gil Rapaport8b9d1f32017-11-20 12:01:47 +0000999 /// Augment the existing recipes of a VPBasicBlock with an additional
1000 /// \p Recipe as the last recipe.
1001 void appendRecipe(VPRecipeBase *Recipe) { insert(Recipe, end()); }
1002
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001003 /// The method which generates the output IR instructions that correspond to
1004 /// this VPBasicBlock, thereby "executing" the VPlan.
1005 void execute(struct VPTransformState *State) override;
1006
1007private:
1008 /// Create an IR BasicBlock to hold the output instructions generated by this
1009 /// VPBasicBlock, and return it. Update the CFGState accordingly.
1010 BasicBlock *createEmptyBasicBlock(VPTransformState::CFGState &CFG);
1011};
1012
1013/// VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks
1014/// which form a Single-Entry-Single-Exit subgraph of the output IR CFG.
1015/// A VPRegionBlock may indicate that its contents are to be replicated several
1016/// times. This is designed to support predicated scalarization, in which a
1017/// scalar if-then code structure needs to be generated VF * UF times. Having
1018/// this replication indicator helps to keep a single model for multiple
1019/// candidate VF's. The actual replication takes place only once the desired VF
1020/// and UF have been determined.
1021class VPRegionBlock : public VPBlockBase {
1022private:
1023 /// Hold the Single Entry of the SESE region modelled by the VPRegionBlock.
1024 VPBlockBase *Entry;
1025
1026 /// Hold the Single Exit of the SESE region modelled by the VPRegionBlock.
1027 VPBlockBase *Exit;
1028
1029 /// An indicator whether this region is to generate multiple replicated
1030 /// instances of output IR corresponding to its VPBlockBases.
1031 bool IsReplicator;
1032
1033public:
1034 VPRegionBlock(VPBlockBase *Entry, VPBlockBase *Exit,
1035 const std::string &Name = "", bool IsReplicator = false)
1036 : VPBlockBase(VPRegionBlockSC, Name), Entry(Entry), Exit(Exit),
1037 IsReplicator(IsReplicator) {
1038 assert(Entry->getPredecessors().empty() && "Entry block has predecessors.");
1039 assert(Exit->getSuccessors().empty() && "Exit block has successors.");
1040 Entry->setParent(this);
1041 Exit->setParent(this);
1042 }
Diego Caballero168d04d2018-05-21 18:14:23 +00001043 VPRegionBlock(const std::string &Name = "", bool IsReplicator = false)
1044 : VPBlockBase(VPRegionBlockSC, Name), Entry(nullptr), Exit(nullptr),
1045 IsReplicator(IsReplicator) {}
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001046
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001047 ~VPRegionBlock() override {
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001048 if (Entry)
1049 deleteCFG(Entry);
1050 }
1051
1052 /// Method to support type inquiry through isa, cast, and dyn_cast.
1053 static inline bool classof(const VPBlockBase *V) {
1054 return V->getVPBlockID() == VPBlockBase::VPRegionBlockSC;
1055 }
1056
1057 const VPBlockBase *getEntry() const { return Entry; }
1058 VPBlockBase *getEntry() { return Entry; }
1059
Diego Caballero168d04d2018-05-21 18:14:23 +00001060 /// Set \p EntryBlock as the entry VPBlockBase of this VPRegionBlock. \p
1061 /// EntryBlock must have no predecessors.
1062 void setEntry(VPBlockBase *EntryBlock) {
1063 assert(EntryBlock->getPredecessors().empty() &&
1064 "Entry block cannot have predecessors.");
1065 Entry = EntryBlock;
1066 EntryBlock->setParent(this);
1067 }
1068
Diego Caballero2a34ac82018-07-30 21:33:31 +00001069 // FIXME: DominatorTreeBase is doing 'A->getParent()->front()'. 'front' is a
1070 // specific interface of llvm::Function, instead of using
1071 // GraphTraints::getEntryNode. We should add a new template parameter to
1072 // DominatorTreeBase representing the Graph type.
1073 VPBlockBase &front() const { return *Entry; }
1074
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001075 const VPBlockBase *getExit() const { return Exit; }
1076 VPBlockBase *getExit() { return Exit; }
1077
Diego Caballero168d04d2018-05-21 18:14:23 +00001078 /// Set \p ExitBlock as the exit VPBlockBase of this VPRegionBlock. \p
1079 /// ExitBlock must have no successors.
1080 void setExit(VPBlockBase *ExitBlock) {
1081 assert(ExitBlock->getSuccessors().empty() &&
1082 "Exit block cannot have successors.");
1083 Exit = ExitBlock;
1084 ExitBlock->setParent(this);
1085 }
1086
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001087 /// An indicator whether this region is to generate multiple replicated
1088 /// instances of output IR corresponding to its VPBlockBases.
1089 bool isReplicator() const { return IsReplicator; }
1090
1091 /// The method which generates the output IR instructions that correspond to
1092 /// this VPRegionBlock, thereby "executing" the VPlan.
1093 void execute(struct VPTransformState *State) override;
1094};
1095
1096/// VPlan models a candidate for vectorization, encoding various decisions take
1097/// to produce efficient output IR, including which branches, basic-blocks and
1098/// output IR instructions to generate, and their cost. VPlan holds a
1099/// Hierarchical-CFG of VPBasicBlocks and VPRegionBlocks rooted at an Entry
1100/// VPBlock.
1101class VPlan {
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001102 friend class VPlanPrinter;
1103
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001104private:
1105 /// Hold the single entry to the Hierarchical CFG of the VPlan.
1106 VPBlockBase *Entry;
1107
1108 /// Holds the VFs applicable to this VPlan.
1109 SmallSet<unsigned, 2> VFs;
1110
1111 /// Holds the name of the VPlan, for printing.
1112 std::string Name;
1113
Diego Caballero168d04d2018-05-21 18:14:23 +00001114 /// Holds all the external definitions created for this VPlan.
1115 // TODO: Introduce a specific representation for external definitions in
1116 // VPlan. External definitions must be immutable and hold a pointer to its
1117 // underlying IR that will be used to implement its structural comparison
1118 // (operators '==' and '<').
Craig Topper61998282018-06-09 05:04:20 +00001119 SmallPtrSet<VPValue *, 16> VPExternalDefs;
Diego Caballero168d04d2018-05-21 18:14:23 +00001120
Ayal Zaksb0b53122018-10-18 15:03:15 +00001121 /// Represents the backedge taken count of the original loop, for folding
1122 /// the tail.
1123 VPValue *BackedgeTakenCount = nullptr;
1124
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001125 /// Holds a mapping between Values and their corresponding VPValue inside
1126 /// VPlan.
1127 Value2VPValueTy Value2VPValue;
1128
Diego Caballero35871502018-07-31 01:57:29 +00001129 /// Holds the VPLoopInfo analysis for this VPlan.
1130 VPLoopInfo VPLInfo;
1131
Hideki Saitod19851a2018-09-14 02:02:57 +00001132 /// Holds the condition bit values built during VPInstruction to VPRecipe transformation.
1133 SmallVector<VPValue *, 4> VPCBVs;
1134
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001135public:
1136 VPlan(VPBlockBase *Entry = nullptr) : Entry(Entry) {}
1137
1138 ~VPlan() {
1139 if (Entry)
1140 VPBlockBase::deleteCFG(Entry);
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001141 for (auto &MapEntry : Value2VPValue)
Ayal Zaksb0b53122018-10-18 15:03:15 +00001142 if (MapEntry.second != BackedgeTakenCount)
1143 delete MapEntry.second;
1144 if (BackedgeTakenCount)
1145 delete BackedgeTakenCount; // Delete once, if in Value2VPValue or not.
Diego Caballero168d04d2018-05-21 18:14:23 +00001146 for (VPValue *Def : VPExternalDefs)
1147 delete Def;
Hideki Saitod19851a2018-09-14 02:02:57 +00001148 for (VPValue *CBV : VPCBVs)
1149 delete CBV;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001150 }
1151
1152 /// Generate the IR code for this VPlan.
1153 void execute(struct VPTransformState *State);
1154
1155 VPBlockBase *getEntry() { return Entry; }
1156 const VPBlockBase *getEntry() const { return Entry; }
1157
1158 VPBlockBase *setEntry(VPBlockBase *Block) { return Entry = Block; }
1159
Ayal Zaksb0b53122018-10-18 15:03:15 +00001160 /// The backedge taken count of the original loop.
1161 VPValue *getOrCreateBackedgeTakenCount() {
1162 if (!BackedgeTakenCount)
1163 BackedgeTakenCount = new VPValue();
1164 return BackedgeTakenCount;
1165 }
1166
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001167 void addVF(unsigned VF) { VFs.insert(VF); }
1168
1169 bool hasVF(unsigned VF) { return VFs.count(VF); }
1170
1171 const std::string &getName() const { return Name; }
1172
1173 void setName(const Twine &newName) { Name = newName.str(); }
1174
Diego Caballero168d04d2018-05-21 18:14:23 +00001175 /// Add \p VPVal to the pool of external definitions if it's not already
1176 /// in the pool.
1177 void addExternalDef(VPValue *VPVal) {
1178 VPExternalDefs.insert(VPVal);
1179 }
1180
Hideki Saitod19851a2018-09-14 02:02:57 +00001181 /// Add \p CBV to the vector of condition bit values.
1182 void addCBV(VPValue *CBV) {
1183 VPCBVs.push_back(CBV);
1184 }
1185
Gil Rapaport8b9d1f32017-11-20 12:01:47 +00001186 void addVPValue(Value *V) {
1187 assert(V && "Trying to add a null Value to VPlan");
1188 assert(!Value2VPValue.count(V) && "Value already exists in VPlan");
1189 Value2VPValue[V] = new VPValue();
1190 }
1191
1192 VPValue *getVPValue(Value *V) {
1193 assert(V && "Trying to get the VPValue of a null Value");
1194 assert(Value2VPValue.count(V) && "Value does not exist in VPlan");
1195 return Value2VPValue[V];
1196 }
1197
Diego Caballero35871502018-07-31 01:57:29 +00001198 /// Return the VPLoopInfo analysis for this VPlan.
1199 VPLoopInfo &getVPLoopInfo() { return VPLInfo; }
1200 const VPLoopInfo &getVPLoopInfo() const { return VPLInfo; }
1201
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001202private:
1203 /// Add to the given dominator tree the header block and every new basic block
1204 /// that was created between it and the latch block, inclusive.
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001205 static void updateDominatorTree(DominatorTree *DT,
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001206 BasicBlock *LoopPreHeaderBB,
1207 BasicBlock *LoopLatchBB);
1208};
1209
1210/// VPlanPrinter prints a given VPlan to a given output stream. The printing is
1211/// indented and follows the dot format.
1212class VPlanPrinter {
1213 friend inline raw_ostream &operator<<(raw_ostream &OS, VPlan &Plan);
1214 friend inline raw_ostream &operator<<(raw_ostream &OS,
1215 const struct VPlanIngredient &I);
1216
1217private:
1218 raw_ostream &OS;
1219 VPlan &Plan;
1220 unsigned Depth;
1221 unsigned TabWidth = 2;
1222 std::string Indent;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001223 unsigned BID = 0;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001224 SmallDenseMap<const VPBlockBase *, unsigned> BlockID;
1225
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001226 VPlanPrinter(raw_ostream &O, VPlan &P) : OS(O), Plan(P) {}
1227
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001228 /// Handle indentation.
1229 void bumpIndent(int b) { Indent = std::string((Depth += b) * TabWidth, ' '); }
1230
1231 /// Print a given \p Block of the Plan.
1232 void dumpBlock(const VPBlockBase *Block);
1233
1234 /// Print the information related to the CFG edges going out of a given
1235 /// \p Block, followed by printing the successor blocks themselves.
1236 void dumpEdges(const VPBlockBase *Block);
1237
1238 /// Print a given \p BasicBlock, including its VPRecipes, followed by printing
1239 /// its successor blocks.
1240 void dumpBasicBlock(const VPBasicBlock *BasicBlock);
1241
1242 /// Print a given \p Region of the Plan.
1243 void dumpRegion(const VPRegionBlock *Region);
1244
1245 unsigned getOrCreateBID(const VPBlockBase *Block) {
1246 return BlockID.count(Block) ? BlockID[Block] : BlockID[Block] = BID++;
1247 }
1248
1249 const Twine getOrCreateName(const VPBlockBase *Block);
1250
1251 const Twine getUID(const VPBlockBase *Block);
1252
1253 /// Print the information related to a CFG edge between two VPBlockBases.
1254 void drawEdge(const VPBlockBase *From, const VPBlockBase *To, bool Hidden,
1255 const Twine &Label);
1256
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001257 void dump();
1258
1259 static void printAsIngredient(raw_ostream &O, Value *V);
1260};
1261
1262struct VPlanIngredient {
1263 Value *V;
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001264
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001265 VPlanIngredient(Value *V) : V(V) {}
1266};
1267
1268inline raw_ostream &operator<<(raw_ostream &OS, const VPlanIngredient &I) {
1269 VPlanPrinter::printAsIngredient(OS, I.V);
1270 return OS;
1271}
1272
1273inline raw_ostream &operator<<(raw_ostream &OS, VPlan &Plan) {
1274 VPlanPrinter Printer(OS, Plan);
1275 Printer.dump();
1276 return OS;
1277}
1278
Diego Caballero2a34ac82018-07-30 21:33:31 +00001279//===----------------------------------------------------------------------===//
1280// GraphTraits specializations for VPlan Hierarchical Control-Flow Graphs //
1281//===----------------------------------------------------------------------===//
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001282
Diego Caballero2a34ac82018-07-30 21:33:31 +00001283// The following set of template specializations implement GraphTraits to treat
1284// any VPBlockBase as a node in a graph of VPBlockBases. It's important to note
1285// that VPBlockBase traits don't recurse into VPRegioBlocks, i.e., if the
1286// VPBlockBase is a VPRegionBlock, this specialization provides access to its
1287// successors/predecessors but not to the blocks inside the region.
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001288
1289template <> struct GraphTraits<VPBlockBase *> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001290 using NodeRef = VPBlockBase *;
1291 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001292
1293 static NodeRef getEntryNode(NodeRef N) { return N; }
1294
1295 static inline ChildIteratorType child_begin(NodeRef N) {
1296 return N->getSuccessors().begin();
1297 }
1298
1299 static inline ChildIteratorType child_end(NodeRef N) {
1300 return N->getSuccessors().end();
1301 }
1302};
1303
1304template <> struct GraphTraits<const VPBlockBase *> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001305 using NodeRef = const VPBlockBase *;
1306 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::const_iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001307
1308 static NodeRef getEntryNode(NodeRef N) { return N; }
1309
1310 static inline ChildIteratorType child_begin(NodeRef N) {
1311 return N->getSuccessors().begin();
1312 }
1313
1314 static inline ChildIteratorType child_end(NodeRef N) {
1315 return N->getSuccessors().end();
1316 }
1317};
1318
Diego Caballero2a34ac82018-07-30 21:33:31 +00001319// Inverse order specialization for VPBasicBlocks. Predecessors are used instead
1320// of successors for the inverse traversal.
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001321template <> struct GraphTraits<Inverse<VPBlockBase *>> {
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001322 using NodeRef = VPBlockBase *;
1323 using ChildIteratorType = SmallVectorImpl<VPBlockBase *>::iterator;
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001324
Diego Caballero2a34ac82018-07-30 21:33:31 +00001325 static NodeRef getEntryNode(Inverse<NodeRef> B) { return B.Graph; }
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001326
1327 static inline ChildIteratorType child_begin(NodeRef N) {
1328 return N->getPredecessors().begin();
1329 }
1330
1331 static inline ChildIteratorType child_end(NodeRef N) {
1332 return N->getPredecessors().end();
1333 }
1334};
1335
Diego Caballero2a34ac82018-07-30 21:33:31 +00001336// The following set of template specializations implement GraphTraits to
1337// treat VPRegionBlock as a graph and recurse inside its nodes. It's important
1338// to note that the blocks inside the VPRegionBlock are treated as VPBlockBases
1339// (i.e., no dyn_cast is performed, VPBlockBases specialization is used), so
1340// there won't be automatic recursion into other VPBlockBases that turn to be
1341// VPRegionBlocks.
1342
1343template <>
1344struct GraphTraits<VPRegionBlock *> : public GraphTraits<VPBlockBase *> {
1345 using GraphRef = VPRegionBlock *;
1346 using nodes_iterator = df_iterator<NodeRef>;
1347
1348 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
1349
1350 static nodes_iterator nodes_begin(GraphRef N) {
1351 return nodes_iterator::begin(N->getEntry());
1352 }
1353
1354 static nodes_iterator nodes_end(GraphRef N) {
1355 // df_iterator::end() returns an empty iterator so the node used doesn't
1356 // matter.
1357 return nodes_iterator::end(N);
1358 }
1359};
1360
1361template <>
1362struct GraphTraits<const VPRegionBlock *>
1363 : public GraphTraits<const VPBlockBase *> {
1364 using GraphRef = const VPRegionBlock *;
1365 using nodes_iterator = df_iterator<NodeRef>;
1366
1367 static NodeRef getEntryNode(GraphRef N) { return N->getEntry(); }
1368
1369 static nodes_iterator nodes_begin(GraphRef N) {
1370 return nodes_iterator::begin(N->getEntry());
1371 }
1372
1373 static nodes_iterator nodes_end(GraphRef N) {
1374 // df_iterator::end() returns an empty iterator so the node used doesn't
1375 // matter.
1376 return nodes_iterator::end(N);
1377 }
1378};
1379
1380template <>
1381struct GraphTraits<Inverse<VPRegionBlock *>>
1382 : public GraphTraits<Inverse<VPBlockBase *>> {
1383 using GraphRef = VPRegionBlock *;
1384 using nodes_iterator = df_iterator<NodeRef>;
1385
1386 static NodeRef getEntryNode(Inverse<GraphRef> N) {
1387 return N.Graph->getExit();
1388 }
1389
1390 static nodes_iterator nodes_begin(GraphRef N) {
1391 return nodes_iterator::begin(N->getExit());
1392 }
1393
1394 static nodes_iterator nodes_end(GraphRef N) {
1395 // df_iterator::end() returns an empty iterator so the node used doesn't
1396 // matter.
1397 return nodes_iterator::end(N);
1398 }
1399};
1400
Diego Caballero168d04d2018-05-21 18:14:23 +00001401//===----------------------------------------------------------------------===//
1402// VPlan Utilities
1403//===----------------------------------------------------------------------===//
1404
1405/// Class that provides utilities for VPBlockBases in VPlan.
1406class VPBlockUtils {
1407public:
1408 VPBlockUtils() = delete;
1409
1410 /// Insert disconnected VPBlockBase \p NewBlock after \p BlockPtr. Add \p
Diego Caballerod0953012018-07-09 15:57:09 +00001411 /// NewBlock as successor of \p BlockPtr and \p BlockPtr as predecessor of \p
1412 /// NewBlock, and propagate \p BlockPtr parent to \p NewBlock. If \p BlockPtr
1413 /// has more than one successor, its conditional bit is propagated to \p
1414 /// NewBlock. \p NewBlock must have neither successors nor predecessors.
Diego Caballero168d04d2018-05-21 18:14:23 +00001415 static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr) {
1416 assert(NewBlock->getSuccessors().empty() &&
1417 "Can't insert new block with successors.");
1418 // TODO: move successors from BlockPtr to NewBlock when this functionality
1419 // is necessary. For now, setBlockSingleSuccessor will assert if BlockPtr
1420 // already has successors.
1421 BlockPtr->setOneSuccessor(NewBlock);
1422 NewBlock->setPredecessors({BlockPtr});
1423 NewBlock->setParent(BlockPtr->getParent());
1424 }
1425
1426 /// Insert disconnected VPBlockBases \p IfTrue and \p IfFalse after \p
1427 /// BlockPtr. Add \p IfTrue and \p IfFalse as succesors of \p BlockPtr and \p
1428 /// BlockPtr as predecessor of \p IfTrue and \p IfFalse. Propagate \p BlockPtr
Diego Caballerod0953012018-07-09 15:57:09 +00001429 /// parent to \p IfTrue and \p IfFalse. \p Condition is set as the successor
1430 /// selector. \p BlockPtr must have no successors and \p IfTrue and \p IfFalse
1431 /// must have neither successors nor predecessors.
Diego Caballero168d04d2018-05-21 18:14:23 +00001432 static void insertTwoBlocksAfter(VPBlockBase *IfTrue, VPBlockBase *IfFalse,
Diego Caballerod0953012018-07-09 15:57:09 +00001433 VPValue *Condition, VPBlockBase *BlockPtr) {
Diego Caballero168d04d2018-05-21 18:14:23 +00001434 assert(IfTrue->getSuccessors().empty() &&
1435 "Can't insert IfTrue with successors.");
1436 assert(IfFalse->getSuccessors().empty() &&
1437 "Can't insert IfFalse with successors.");
Diego Caballerod0953012018-07-09 15:57:09 +00001438 BlockPtr->setTwoSuccessors(IfTrue, IfFalse, Condition);
Diego Caballero168d04d2018-05-21 18:14:23 +00001439 IfTrue->setPredecessors({BlockPtr});
1440 IfFalse->setPredecessors({BlockPtr});
1441 IfTrue->setParent(BlockPtr->getParent());
1442 IfFalse->setParent(BlockPtr->getParent());
1443 }
1444
1445 /// Connect VPBlockBases \p From and \p To bi-directionally. Append \p To to
1446 /// the successors of \p From and \p From to the predecessors of \p To. Both
1447 /// VPBlockBases must have the same parent, which can be null. Both
1448 /// VPBlockBases can be already connected to other VPBlockBases.
1449 static void connectBlocks(VPBlockBase *From, VPBlockBase *To) {
1450 assert((From->getParent() == To->getParent()) &&
1451 "Can't connect two block with different parents");
1452 assert(From->getNumSuccessors() < 2 &&
1453 "Blocks can't have more than two successors.");
1454 From->appendSuccessor(To);
1455 To->appendPredecessor(From);
1456 }
1457
1458 /// Disconnect VPBlockBases \p From and \p To bi-directionally. Remove \p To
1459 /// from the successors of \p From and \p From from the predecessors of \p To.
1460 static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To) {
1461 assert(To && "Successor to disconnect is null.");
1462 From->removeSuccessor(To);
1463 To->removePredecessor(From);
1464 }
1465};
Florian Hahn45e5d5b2018-06-08 17:30:45 +00001466
Eugene Zelenko6cadde72017-10-17 21:27:42 +00001467} // end namespace llvm
Ayal Zaks1f58dda2017-08-27 12:55:46 +00001468
1469#endif // LLVM_TRANSFORMS_VECTORIZE_VPLAN_H