2013-04-20 05:18:51 +00:00
|
|
|
//===- VecUtils.h - Vectorization Utilities -------------------------------===//
|
2013-04-09 19:44:35 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This family of classes and functions manipulate vectors and chains of
|
|
|
|
// vectors.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-04-14 09:33:08 +00:00
|
|
|
#ifndef LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
|
|
|
|
#define LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
|
2013-04-09 19:44:35 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-05-22 19:47:32 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2013-04-09 19:44:35 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2013-05-22 19:47:32 +00:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-04-09 19:44:35 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class BasicBlock; class Instruction; class Type;
|
|
|
|
class VectorType; class StoreInst; class Value;
|
|
|
|
class ScalarEvolution; class DataLayout;
|
|
|
|
class TargetTransformInfo; class AliasAnalysis;
|
2013-04-20 06:13:47 +00:00
|
|
|
class Loop;
|
2013-04-09 19:44:35 +00:00
|
|
|
|
|
|
|
/// Bottom Up SLP vectorization utility class.
|
|
|
|
struct BoUpSLP {
|
|
|
|
typedef SmallVector<Value*, 8> ValueList;
|
|
|
|
typedef SmallPtrSet<Value*, 16> ValueSet;
|
|
|
|
typedef SmallVector<StoreInst*, 8> StoreList;
|
|
|
|
static const int max_cost = 1<<20;
|
|
|
|
|
|
|
|
// \brief C'tor.
|
|
|
|
BoUpSLP(BasicBlock *Bb, ScalarEvolution *Se, DataLayout *Dl,
|
2013-04-20 06:13:47 +00:00
|
|
|
TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp);
|
2013-04-09 19:44:35 +00:00
|
|
|
|
2013-04-12 21:16:54 +00:00
|
|
|
/// \brief Take the pointer operand from the Load/Store instruction.
|
|
|
|
/// \returns NULL if this is not a valid Load/Store instruction.
|
|
|
|
static Value *getPointerOperand(Value *I);
|
|
|
|
|
|
|
|
/// \brief Take the address space operand from the Load/Store instruction.
|
|
|
|
/// \returns -1 if this is not a valid Load/Store instruction.
|
|
|
|
static unsigned getAddressSpaceOperand(Value *I);
|
|
|
|
|
2013-04-09 19:44:35 +00:00
|
|
|
/// \returns true if the memory operations A and B are consecutive.
|
|
|
|
bool isConsecutiveAccess(Value *A, Value *B);
|
|
|
|
|
|
|
|
/// \brief Vectorize the tree that starts with the elements in \p VL.
|
|
|
|
/// \returns the vectorized value.
|
2013-04-20 09:49:10 +00:00
|
|
|
Value *vectorizeTree(ArrayRef<Value *> VL, int VF);
|
2013-04-09 19:44:35 +00:00
|
|
|
|
|
|
|
/// \returns the vectorization cost of the subtree that starts at \p VL.
|
|
|
|
/// A negative number means that this is profitable.
|
2013-04-20 09:49:10 +00:00
|
|
|
int getTreeCost(ArrayRef<Value *> VL);
|
2013-04-09 19:44:35 +00:00
|
|
|
|
2013-04-20 09:49:10 +00:00
|
|
|
/// \returns the scalarization cost for this list of values. Assuming that
|
|
|
|
/// this subtree gets vectorized, we may need to extract the values from the
|
2013-04-14 05:15:53 +00:00
|
|
|
/// roots. This method calculates the cost of extracting the values.
|
2013-04-20 09:49:10 +00:00
|
|
|
int getScalarizationCost(ArrayRef<Value *> VL);
|
2013-04-14 05:15:53 +00:00
|
|
|
|
2013-04-09 19:44:35 +00:00
|
|
|
/// \brief Attempts to order and vectorize a sequence of stores. This
|
|
|
|
/// function does a quadratic scan of the given stores.
|
|
|
|
/// \returns true if the basic block was modified.
|
2013-04-20 09:49:10 +00:00
|
|
|
bool vectorizeStores(ArrayRef<StoreInst *> Stores, int costThreshold);
|
2013-04-09 19:44:35 +00:00
|
|
|
|
2013-04-14 03:22:20 +00:00
|
|
|
/// \brief Vectorize a group of scalars into a vector tree.
|
2013-04-20 09:49:10 +00:00
|
|
|
void vectorizeArith(ArrayRef<Value *> Operands);
|
2013-04-14 03:22:20 +00:00
|
|
|
|
2013-04-15 22:00:26 +00:00
|
|
|
/// \returns the list of new instructions that were added in order to collect
|
|
|
|
/// scalars into vectors. This list can be used to further optimize the gather
|
|
|
|
/// sequences.
|
|
|
|
ValueList &getGatherSeqInstructions() {return GatherInstructions; }
|
|
|
|
|
2013-04-12 21:16:54 +00:00
|
|
|
private:
|
2013-04-14 09:33:08 +00:00
|
|
|
/// \brief This method contains the recursive part of getTreeCost.
|
2013-04-20 09:49:10 +00:00
|
|
|
int getTreeCost_rec(ArrayRef<Value *> VL, unsigned Depth);
|
2013-04-12 21:16:54 +00:00
|
|
|
|
2013-04-14 09:33:08 +00:00
|
|
|
/// \brief This recursive method looks for vectorization hazards such as
|
2013-04-12 21:16:54 +00:00
|
|
|
/// values that are used by multiple users and checks that values are used
|
|
|
|
/// by only one vector lane. It updates the variables LaneMap, MultiUserVals.
|
2013-04-20 09:49:10 +00:00
|
|
|
void getTreeUses_rec(ArrayRef<Value *> VL, unsigned Depth);
|
2013-04-12 21:16:54 +00:00
|
|
|
|
|
|
|
/// \brief This method contains the recursive part of vectorizeTree.
|
2013-04-20 09:49:10 +00:00
|
|
|
Value *vectorizeTree_rec(ArrayRef<Value *> VL, int VF);
|
2013-04-12 21:16:54 +00:00
|
|
|
|
2013-04-09 19:44:35 +00:00
|
|
|
/// \brief Number all of the instructions in the block.
|
|
|
|
void numberInstructions();
|
|
|
|
|
2013-04-12 21:16:54 +00:00
|
|
|
/// \brief Vectorize a sorted sequence of stores.
|
2013-04-20 09:49:10 +00:00
|
|
|
bool vectorizeStoreChain(ArrayRef<Value *> Chain, int CostThreshold);
|
2013-04-12 21:16:54 +00:00
|
|
|
|
2013-04-09 19:44:35 +00:00
|
|
|
/// \returns the scalarization cost for this type. Scalarization in this
|
|
|
|
/// context means the creation of vectors from a group of scalars.
|
|
|
|
int getScalarizationCost(Type *Ty);
|
|
|
|
|
|
|
|
/// \returns the AA location that is being access by the instruction.
|
|
|
|
AliasAnalysis::Location getLocation(Instruction *I);
|
|
|
|
|
|
|
|
/// \brief Checks if it is possible to sink an instruction from
|
|
|
|
/// \p Src to \p Dst.
|
|
|
|
/// \returns the pointer to the barrier instruction if we can't sink.
|
|
|
|
Value *isUnsafeToSink(Instruction *Src, Instruction *Dst);
|
|
|
|
|
2013-05-22 19:47:32 +00:00
|
|
|
/// \returns the index of the last instrucion in the BB from \p VL.
|
2013-04-09 19:44:35 +00:00
|
|
|
/// Only consider the first \p VF elements.
|
2013-05-22 19:47:32 +00:00
|
|
|
int getLastIndex(ArrayRef<Value *> VL, unsigned VF);
|
|
|
|
|
|
|
|
/// \returns the index of the first User of \p VL.
|
|
|
|
/// Only consider the first \p VF elements.
|
|
|
|
int getFirstUserIndex(ArrayRef<Value *> VL, unsigned VF);
|
|
|
|
|
|
|
|
/// \returns the instruction \p I or \p Jt hat appears last in the BB .
|
|
|
|
int getLastIndex(Instruction *I, Instruction *J);
|
|
|
|
|
|
|
|
/// \returns the insertion point for \p Index.
|
|
|
|
Instruction *getInsertionPoint(unsigned Index);
|
2013-04-09 19:44:35 +00:00
|
|
|
|
|
|
|
/// \returns a vector from a collection of scalars in \p VL.
|
2013-04-20 09:49:10 +00:00
|
|
|
Value *Scalarize(ArrayRef<Value *> VL, VectorType *Ty);
|
2013-04-20 06:13:47 +00:00
|
|
|
|
2013-04-12 21:16:54 +00:00
|
|
|
private:
|
2013-04-15 22:00:26 +00:00
|
|
|
/// Maps instructions to numbers and back.
|
2013-04-09 19:44:35 +00:00
|
|
|
SmallDenseMap<Value*, int> InstrIdx;
|
2013-04-15 22:00:26 +00:00
|
|
|
/// Maps integers to Instructions.
|
2013-04-09 19:44:35 +00:00
|
|
|
std::vector<Instruction*> InstrVec;
|
2013-04-12 21:16:54 +00:00
|
|
|
|
|
|
|
// -- containers that are used during getTreeCost -- //
|
|
|
|
|
|
|
|
/// Contains values that must be scalarized because they are used
|
|
|
|
/// by multiple lanes, or by users outside the tree.
|
|
|
|
/// NOTICE: The vectorization methods also use this set.
|
|
|
|
ValueSet MustScalarize;
|
2013-04-14 05:15:53 +00:00
|
|
|
|
2013-05-10 22:59:33 +00:00
|
|
|
/// Contains values that have users outside of the vectorized graph.
|
|
|
|
/// We need to generate extract instructions for these values.
|
|
|
|
/// NOTICE: The vectorization methods also use this set.
|
2013-05-22 19:47:32 +00:00
|
|
|
SetVector<Value*> MustExtract;
|
2013-05-10 22:59:33 +00:00
|
|
|
|
2013-04-15 22:00:26 +00:00
|
|
|
/// Contains a list of values that are used outside the current tree. This
|
|
|
|
/// set must be reset between runs.
|
2013-05-22 19:47:32 +00:00
|
|
|
SetVector<Value*> MultiUserVals;
|
2013-04-15 22:00:26 +00:00
|
|
|
/// Maps values in the tree to the vector lanes that uses them. This map must
|
|
|
|
/// be reset between runs of getCost.
|
2013-04-12 21:16:54 +00:00
|
|
|
std::map<Value*, int> LaneMap;
|
2013-04-15 22:00:26 +00:00
|
|
|
/// A list of instructions to ignore while sinking
|
|
|
|
/// memory instructions. This map must be reset between runs of getCost.
|
2013-05-22 19:47:32 +00:00
|
|
|
ValueSet MemBarrierIgnoreList;
|
2013-04-12 21:16:54 +00:00
|
|
|
|
2013-04-15 22:00:26 +00:00
|
|
|
// -- Containers that are used during vectorizeTree -- //
|
|
|
|
|
|
|
|
/// Maps between the first scalar to the vector. This map must be reset
|
|
|
|
///between runs.
|
2013-04-12 21:16:54 +00:00
|
|
|
DenseMap<Value*, Value*> VectorizedValues;
|
|
|
|
|
2013-04-15 22:00:26 +00:00
|
|
|
// -- Containers that are used after vectorization by the caller -- //
|
|
|
|
|
|
|
|
/// A list of instructions that are used when gathering scalars into vectors.
|
|
|
|
/// In many cases these instructions can be hoisted outside of the BB.
|
|
|
|
/// Iterating over this list is faster than calling LICM.
|
|
|
|
ValueList GatherInstructions;
|
|
|
|
|
2013-05-22 19:47:32 +00:00
|
|
|
/// Instruction builder to construct the vectorized tree.
|
|
|
|
IRBuilder<> Builder;
|
|
|
|
|
2013-04-09 19:44:35 +00:00
|
|
|
// Analysis and block reference.
|
|
|
|
BasicBlock *BB;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
DataLayout *DL;
|
|
|
|
TargetTransformInfo *TTI;
|
|
|
|
AliasAnalysis *AA;
|
2013-04-20 06:13:47 +00:00
|
|
|
Loop *L;
|
2013-04-09 19:44:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace
|
|
|
|
|
2013-04-14 09:33:08 +00:00
|
|
|
#endif // LLVM_TRANSFORMS_VECTORIZE_VECUTILS_H
|