2012-02-01 11:51:43 +08:00
|
|
|
//===- BBVectorize.cpp - A Basic-Block Vectorizer -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a basic-block vectorization pass. The algorithm was
|
|
|
|
// inspired by that used by the Vienna MAP Vectorizor by Franchetti and Kral,
|
|
|
|
// et al. It works by looking for chains of pairable operations and then
|
|
|
|
// pairing them.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define BBV_NAME "bb-vectorize"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Vectorize.h"
|
2012-02-01 11:51:43 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-11-13 05:21:02 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2012-02-01 11:51:43 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/AliasSetTracker.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2013-01-07 11:08:10 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2012-02-01 11:51:43 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
2014-03-04 19:17:44 +08:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2012-02-01 11:51:43 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-06-28 13:42:42 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2012-02-01 11:51:43 +08:00
|
|
|
#include <algorithm>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE BBV_NAME
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
IgnoreTargetInfo("bb-vectorize-ignore-target-info", cl::init(false),
|
|
|
|
cl::Hidden, cl::desc("Ignore target information"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
ReqChainDepth("bb-vectorize-req-chain-depth", cl::init(6), cl::Hidden,
|
|
|
|
cl::desc("The required chain depth for vectorization"));
|
|
|
|
|
2012-11-02 05:50:12 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
UseChainDepthWithTI("bb-vectorize-use-chain-depth", cl::init(false),
|
|
|
|
cl::Hidden, cl::desc("Use the chain depth requirement with"
|
|
|
|
" target information"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
SearchLimit("bb-vectorize-search-limit", cl::init(400), cl::Hidden,
|
|
|
|
cl::desc("The maximum search distance for instruction pairs"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
SplatBreaksChain("bb-vectorize-splat-breaks-chain", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Replicating one element to a pair breaks the chain"));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
VectorBits("bb-vectorize-vector-bits", cl::init(128), cl::Hidden,
|
|
|
|
cl::desc("The size of the native vector registers"));
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxIter("bb-vectorize-max-iter", cl::init(0), cl::Hidden,
|
|
|
|
cl::desc("The maximum number of pairing iterations"));
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
Pow2LenOnly("bb-vectorize-pow2-len-only", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to form non-2^n-length vectors"));
|
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxInsts("bb-vectorize-max-instr-per-group", cl::init(500), cl::Hidden,
|
|
|
|
cl::desc("The maximum number of pairable instructions per group"));
|
|
|
|
|
BBVectorize: Cap the number of candidate pairs in each instruction group
For some basic blocks, it is possible to generate many candidate pairs for
relatively few pairable instructions. When many (tens of thousands) of these pairs
are generated for a single instruction group, the time taken to generate and
rank the different vectorization plans can become quite large. As a result, we now
cap the number of candidate pairs within each instruction group. This is done by
closing out the group once the threshold is reached (set now at 3000 pairs).
Although this will limit the overall compile-time impact, this may not be the best
way to achieve this result. It might be better, for example, to prune excessive
candidate pairs after the fact the prevent the generation of short, but highly-connected
groups. We can experiment with this in the future.
This change reduces the overall compile-time slowdown of the csa.ll test case in
PR15222 to ~5x. If 5x is still considered too large, a lower limit can be
used as the default.
This represents a functionality change, but only for very large inputs
(thus, there is no regression test).
llvm-svn: 175251
2013-02-15 12:28:42 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxPairs("bb-vectorize-max-pairs-per-group", cl::init(3000), cl::Hidden,
|
|
|
|
cl::desc("The maximum number of candidate instruction pairs per group"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
MaxCandPairsForCycleCheck("bb-vectorize-max-cycle-check-pairs", cl::init(200),
|
|
|
|
cl::Hidden, cl::desc("The maximum number of candidate pairs with which to use"
|
|
|
|
" a full cycle check"));
|
|
|
|
|
2012-06-24 21:28:01 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoBools("bb-vectorize-no-bools", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize boolean (i1) values"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoInts("bb-vectorize-no-ints", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize integer values"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
NoFloats("bb-vectorize-no-floats", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize floating-point values"));
|
|
|
|
|
2012-10-26 08:05:26 +08:00
|
|
|
// FIXME: This should default to false once pointer vector support works.
|
2012-04-14 15:32:43 +08:00
|
|
|
static cl::opt<bool>
|
2012-10-26 08:05:26 +08:00
|
|
|
NoPointers("bb-vectorize-no-pointers", cl::init(/*false*/ true), cl::Hidden,
|
2012-04-14 15:32:43 +08:00
|
|
|
cl::desc("Don't try to vectorize pointer values"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoCasts("bb-vectorize-no-casts", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize casting (conversion) operations"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
NoMath("bb-vectorize-no-math", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize floating-point math intrinsics"));
|
|
|
|
|
2014-04-25 11:33:48 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoBitManipulation("bb-vectorize-no-bitmanip", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize BitManipulation intrinsics"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoFMA("bb-vectorize-no-fma", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize the fused-multiply-add intrinsic"));
|
|
|
|
|
2012-04-14 04:45:45 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoSelect("bb-vectorize-no-select", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize select instructions"));
|
|
|
|
|
2012-06-24 05:52:50 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoCmp("bb-vectorize-no-cmp", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize comparison instructions"));
|
|
|
|
|
2012-04-14 15:32:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoGEP("bb-vectorize-no-gep", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize getelementptr instructions"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoMemOps("bb-vectorize-no-mem-ops", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't try to vectorize loads and stores"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
AlignedOnly("bb-vectorize-aligned-only", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Only generate aligned loads and stores"));
|
|
|
|
|
2012-02-04 12:14:04 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
NoMemOpBoost("bb-vectorize-no-mem-op-boost",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Don't boost the chain-depth contribution of loads and stores"));
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
FastDep("bb-vectorize-fast-dep", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Use a fast instruction dependency analysis"));
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<bool>
|
|
|
|
DebugInstructionExamination("bb-vectorize-debug-instruction-examination",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("When debugging is enabled, output information on the"
|
|
|
|
" instruction-examination process"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
DebugCandidateSelection("bb-vectorize-debug-candidate-selection",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("When debugging is enabled, output information on the"
|
|
|
|
" candidate-selection process"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
DebugPairSelection("bb-vectorize-debug-pair-selection",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("When debugging is enabled, output information on the"
|
|
|
|
" pair-selection process"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
DebugCycleCheck("bb-vectorize-debug-cycle-check",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("When debugging is enabled, output information on the"
|
|
|
|
" cycle-checking process"));
|
2012-10-31 23:17:07 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
PrintAfterEveryPair("bb-vectorize-debug-print-after-every-pair",
|
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("When debugging is enabled, dump the basic block after"
|
|
|
|
" every pair is fused"));
|
2012-02-01 11:51:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
STATISTIC(NumFusedOps, "Number of operations fused by bb-vectorize");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct BBVectorize : public BasicBlockPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2012-04-05 23:46:55 +08:00
|
|
|
|
2012-04-06 00:07:49 +08:00
|
|
|
const VectorizeConfig Config;
|
2012-04-05 23:46:55 +08:00
|
|
|
|
|
|
|
BBVectorize(const VectorizeConfig &C = VectorizeConfig())
|
|
|
|
: BasicBlockPass(ID), Config(C) {
|
2012-02-01 11:51:43 +08:00
|
|
|
initializeBBVectorizePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2015-02-01 20:01:35 +08:00
|
|
|
BBVectorize(Pass *P, Function &F, const VectorizeConfig &C)
|
2012-04-05 23:46:55 +08:00
|
|
|
: BasicBlockPass(ID), Config(C) {
|
2012-04-05 16:05:16 +08:00
|
|
|
AA = &P->getAnalysis<AliasAnalysis>();
|
2014-01-13 21:07:17 +08:00
|
|
|
DT = &P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2012-04-05 16:05:16 +08:00
|
|
|
SE = &P->getAnalysis<ScalarEvolution>();
|
[PM] Change the core design of the TTI analysis to use a polymorphic
type erased interface and a single analysis pass rather than an
extremely complex analysis group.
The end result is that the TTI analysis can contain a type erased
implementation that supports the polymorphic TTI interface. We can build
one from a target-specific implementation or from a dummy one in the IR.
I've also factored all of the code into "mix-in"-able base classes,
including CRTP base classes to facilitate calling back up to the most
specialized form when delegating horizontally across the surface. These
aren't as clean as I would like and I'm planning to work on cleaning
some of this up, but I wanted to start by putting into the right form.
There are a number of reasons for this change, and this particular
design. The first and foremost reason is that an analysis group is
complete overkill, and the chaining delegation strategy was so opaque,
confusing, and high overhead that TTI was suffering greatly for it.
Several of the TTI functions had failed to be implemented in all places
because of the chaining-based delegation making there be no checking of
this. A few other functions were implemented with incorrect delegation.
The message to me was very clear working on this -- the delegation and
analysis group structure was too confusing to be useful here.
The other reason of course is that this is *much* more natural fit for
the new pass manager. This will lay the ground work for a type-erased
per-function info object that can look up the correct subtarget and even
cache it.
Yet another benefit is that this will significantly simplify the
interaction of the pass managers and the TargetMachine. See the future
work below.
The downside of this change is that it is very, very verbose. I'm going
to work to improve that, but it is somewhat an implementation necessity
in C++ to do type erasure. =/ I discussed this design really extensively
with Eric and Hal prior to going down this path, and afterward showed
them the result. No one was really thrilled with it, but there doesn't
seem to be a substantially better alternative. Using a base class and
virtual method dispatch would make the code much shorter, but as
discussed in the update to the programmer's manual and elsewhere,
a polymorphic interface feels like the more principled approach even if
this is perhaps the least compelling example of it. ;]
Ultimately, there is still a lot more to be done here, but this was the
huge chunk that I couldn't really split things out of because this was
the interface change to TTI. I've tried to minimize all the other parts
of this. The follow up work should include at least:
1) Improving the TargetMachine interface by having it directly return
a TTI object. Because we have a non-pass object with value semantics
and an internal type erasure mechanism, we can narrow the interface
of the TargetMachine to *just* do what we need: build and return
a TTI object that we can then insert into the pass pipeline.
2) Make the TTI object be fully specialized for a particular function.
This will include splitting off a minimal form of it which is
sufficient for the inliner and the old pass manager.
3) Add a new pass manager analysis which produces TTI objects from the
target machine for each function. This may actually be done as part
of #2 in order to use the new analysis to implement #2.
4) Work on narrowing the API between TTI and the targets so that it is
easier to understand and less verbose to type erase.
5) Work on narrowing the API between TTI and its clients so that it is
easier to understand and less verbose to forward.
6) Try to improve the CRTP-based delegation. I feel like this code is
just a bit messy and exacerbating the complexity of implementing
the TTI in each target.
Many thanks to Eric and Hal for their help here. I ended up blocked on
this somewhat more abruptly than I expected, and so I appreciate getting
it sorted out very quickly.
Differential Revision: http://reviews.llvm.org/D7293
llvm-svn: 227669
2015-01-31 11:43:40 +08:00
|
|
|
TTI = IgnoreTargetInfo
|
|
|
|
? nullptr
|
2015-02-01 20:01:35 +08:00
|
|
|
: &P->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
|
2012-04-05 16:05:16 +08:00
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
typedef std::pair<Value *, Value *> ValuePair;
|
2012-10-26 05:12:23 +08:00
|
|
|
typedef std::pair<ValuePair, int> ValuePairWithCost;
|
2012-02-01 11:51:43 +08:00
|
|
|
typedef std::pair<ValuePair, size_t> ValuePairWithDepth;
|
|
|
|
typedef std::pair<ValuePair, ValuePair> VPPair; // A ValuePair pair
|
2012-10-31 23:17:07 +08:00
|
|
|
typedef std::pair<VPPair, unsigned> VPPairWithType;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
AliasAnalysis *AA;
|
2012-10-23 02:00:55 +08:00
|
|
|
DominatorTree *DT;
|
2012-02-01 11:51:43 +08:00
|
|
|
ScalarEvolution *SE;
|
2013-01-05 18:05:28 +08:00
|
|
|
const TargetTransformInfo *TTI;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// FIXME: const correct?
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
bool vectorizePairs(BasicBlock &BB, bool NonPow2Len = false);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
bool getCandidatePairs(BasicBlock &BB,
|
|
|
|
BasicBlock::iterator &Start,
|
2013-02-15 06:37:09 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
2012-10-31 04:17:37 +08:00
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
2012-10-26 05:12:23 +08:00
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
2012-06-28 13:42:42 +08:00
|
|
|
std::vector<Value *> &PairableInsts, bool NonPow2Len);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-11-02 05:50:12 +08:00
|
|
|
// FIXME: The current implementation does not account for pairs that
|
|
|
|
// are connected in multiple ways. For example:
|
|
|
|
// C1 = A1 / A2; C2 = A2 / A1 (which may be both direct and a swap)
|
2012-10-31 23:17:07 +08:00
|
|
|
enum PairConnectionType {
|
|
|
|
PairConnectionDirect,
|
|
|
|
PairConnectionSwap,
|
|
|
|
PairConnectionSplat
|
|
|
|
};
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
void computeConnectedPairs(
|
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void buildDepMap(BasicBlock &BB,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-15 06:37:09 +08:00
|
|
|
void choosePairs(DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<Value *, Value *>& ChosenPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void fuseChosenPairs(BasicBlock &BB,
|
2013-02-15 06:38:04 +08:00
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<Value *, Value *>& ChosenPairs,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps);
|
2012-10-31 23:17:07 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
bool isInstVectorizable(Instruction *I, bool &IsSimpleLoadStore);
|
|
|
|
|
|
|
|
bool areInstsCompatible(Instruction *I, Instruction *J,
|
2012-10-26 05:12:23 +08:00
|
|
|
bool IsSimpleLoadStore, bool NonPow2Len,
|
2012-10-31 04:17:37 +08:00
|
|
|
int &CostSavings, int &FixedOrder);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
bool trackUsesOfI(DenseSet<Value *> &Users,
|
|
|
|
AliasSetTracker &WriteSet, Instruction *I,
|
|
|
|
Instruction *J, bool UpdateUsers = true,
|
2014-04-25 13:29:35 +08:00
|
|
|
DenseSet<ValuePair> *LoadMoveSetPairs = nullptr);
|
2012-02-06 13:29:32 +08:00
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
void computePairsConnectedTo(
|
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
ValuePair P);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
bool pairsConflict(ValuePair P, ValuePair Q,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >
|
2014-04-25 13:29:35 +08:00
|
|
|
*PairableInstUserMap = nullptr,
|
|
|
|
DenseSet<VPPair> *PairableInstUserPairSet = nullptr);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
bool pairWillFormCycle(ValuePair P,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUsers,
|
|
|
|
DenseSet<ValuePair> &CurrentPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
void pruneDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
|
|
|
|
DenseSet<VPPair> &PairableInstUserPairSet,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t> &DAG,
|
|
|
|
DenseSet<ValuePair> &PrunedDAG, ValuePair J,
|
2013-02-15 06:38:04 +08:00
|
|
|
bool UseCycleCheck);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
void buildInitialDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t> &DAG, ValuePair J);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
void findBestDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
|
|
|
|
DenseSet<VPPair> &PairableInstUserPairSet,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseSet<ValuePair> &BestDAG, size_t &BestMaxDepth,
|
2013-02-15 06:38:04 +08:00
|
|
|
int &BestEffSize, Value *II, std::vector<Value *>&JJ,
|
|
|
|
bool UseCycleCheck);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
Value *getReplacementPointerInput(LLVMContext& Context, Instruction *I,
|
2012-10-31 03:35:29 +08:00
|
|
|
Instruction *J, unsigned o);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void fillNewShuffleMask(LLVMContext& Context, Instruction *J,
|
2012-06-28 13:42:42 +08:00
|
|
|
unsigned MaskOffset, unsigned NumInElem,
|
|
|
|
unsigned NumInElem1, unsigned IdxOffset,
|
|
|
|
std::vector<Constant*> &Mask);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
Value *getReplacementShuffleMask(LLVMContext& Context, Instruction *I,
|
|
|
|
Instruction *J);
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
bool expandIEChain(LLVMContext& Context, Instruction *I, Instruction *J,
|
|
|
|
unsigned o, Value *&LOp, unsigned numElemL,
|
2012-10-31 23:17:07 +08:00
|
|
|
Type *ArgTypeL, Type *ArgTypeR, bool IBeforeJ,
|
2012-06-28 13:42:42 +08:00
|
|
|
unsigned IdxOff = 0);
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
Value *getReplacementInput(LLVMContext& Context, Instruction *I,
|
2012-10-31 23:17:07 +08:00
|
|
|
Instruction *J, unsigned o, bool IBeforeJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void getReplacementInputsForPair(LLVMContext& Context, Instruction *I,
|
2013-07-14 12:42:23 +08:00
|
|
|
Instruction *J, SmallVectorImpl<Value *> &ReplacedOperands,
|
2012-10-31 23:17:07 +08:00
|
|
|
bool IBeforeJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void replaceOutputsOfPair(LLVMContext& Context, Instruction *I,
|
|
|
|
Instruction *J, Instruction *K,
|
|
|
|
Instruction *&InsertionPt, Instruction *&K1,
|
2012-10-31 03:35:29 +08:00
|
|
|
Instruction *&K2);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
void collectPairLoadMoveSet(BasicBlock &BB,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *I);
|
|
|
|
|
|
|
|
void collectLoadMoveSet(BasicBlock &BB,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
bool canMoveUsesOfIAfterJ(BasicBlock &BB,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *I, Instruction *J);
|
|
|
|
|
|
|
|
void moveUsesOfIAfterJ(BasicBlock &BB,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *&InsertionPt,
|
|
|
|
Instruction *I, Instruction *J);
|
|
|
|
|
2012-04-05 16:05:16 +08:00
|
|
|
bool vectorizeBB(BasicBlock &BB) {
|
2014-02-06 08:07:05 +08:00
|
|
|
if (skipOptnoneFunction(BB))
|
|
|
|
return false;
|
2012-10-23 02:00:55 +08:00
|
|
|
if (!DT->isReachableFromEntry(&BB)) {
|
|
|
|
DEBUG(dbgs() << "BBV: skipping unreachable " << BB.getName() <<
|
|
|
|
" in " << BB.getParent()->getName() << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
DEBUG(if (TTI) dbgs() << "BBV: using target information\n");
|
2012-10-26 05:12:23 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
bool changed = false;
|
|
|
|
// Iterate a sufficient number of times to merge types of size 1 bit,
|
|
|
|
// then 2 bits, then 4, etc. up to half of the target vector width of the
|
|
|
|
// target vector register.
|
2012-06-28 13:42:42 +08:00
|
|
|
unsigned n = 1;
|
|
|
|
for (unsigned v = 2;
|
2013-01-05 18:05:28 +08:00
|
|
|
(TTI || v <= Config.VectorBits) &&
|
2012-10-26 05:12:23 +08:00
|
|
|
(!Config.MaxIter || n <= Config.MaxIter);
|
2012-02-01 11:51:43 +08:00
|
|
|
v *= 2, ++n) {
|
2012-04-05 23:46:55 +08:00
|
|
|
DEBUG(dbgs() << "BBV: fusing loop #" << n <<
|
2012-02-01 11:51:43 +08:00
|
|
|
" for " << BB.getName() << " in " <<
|
|
|
|
BB.getParent()->getName() << "...\n");
|
|
|
|
if (vectorizePairs(BB))
|
|
|
|
changed = true;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
if (changed && !Pow2LenOnly) {
|
|
|
|
++n;
|
|
|
|
for (; !Config.MaxIter || n <= Config.MaxIter; ++n) {
|
|
|
|
DEBUG(dbgs() << "BBV: fusing for non-2^n-length vectors loop #: " <<
|
|
|
|
n << " for " << BB.getName() << " in " <<
|
|
|
|
BB.getParent()->getName() << "...\n");
|
|
|
|
if (!vectorizePairs(BB, true)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(dbgs() << "BBV: done!\n");
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB) override {
|
2014-02-06 08:07:05 +08:00
|
|
|
// OptimizeNone check deferred to vectorizeBB().
|
|
|
|
|
2012-04-05 16:05:16 +08:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
2014-01-13 21:07:17 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2012-04-05 16:05:16 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
[PM] Change the core design of the TTI analysis to use a polymorphic
type erased interface and a single analysis pass rather than an
extremely complex analysis group.
The end result is that the TTI analysis can contain a type erased
implementation that supports the polymorphic TTI interface. We can build
one from a target-specific implementation or from a dummy one in the IR.
I've also factored all of the code into "mix-in"-able base classes,
including CRTP base classes to facilitate calling back up to the most
specialized form when delegating horizontally across the surface. These
aren't as clean as I would like and I'm planning to work on cleaning
some of this up, but I wanted to start by putting into the right form.
There are a number of reasons for this change, and this particular
design. The first and foremost reason is that an analysis group is
complete overkill, and the chaining delegation strategy was so opaque,
confusing, and high overhead that TTI was suffering greatly for it.
Several of the TTI functions had failed to be implemented in all places
because of the chaining-based delegation making there be no checking of
this. A few other functions were implemented with incorrect delegation.
The message to me was very clear working on this -- the delegation and
analysis group structure was too confusing to be useful here.
The other reason of course is that this is *much* more natural fit for
the new pass manager. This will lay the ground work for a type-erased
per-function info object that can look up the correct subtarget and even
cache it.
Yet another benefit is that this will significantly simplify the
interaction of the pass managers and the TargetMachine. See the future
work below.
The downside of this change is that it is very, very verbose. I'm going
to work to improve that, but it is somewhat an implementation necessity
in C++ to do type erasure. =/ I discussed this design really extensively
with Eric and Hal prior to going down this path, and afterward showed
them the result. No one was really thrilled with it, but there doesn't
seem to be a substantially better alternative. Using a base class and
virtual method dispatch would make the code much shorter, but as
discussed in the update to the programmer's manual and elsewhere,
a polymorphic interface feels like the more principled approach even if
this is perhaps the least compelling example of it. ;]
Ultimately, there is still a lot more to be done here, but this was the
huge chunk that I couldn't really split things out of because this was
the interface change to TTI. I've tried to minimize all the other parts
of this. The follow up work should include at least:
1) Improving the TargetMachine interface by having it directly return
a TTI object. Because we have a non-pass object with value semantics
and an internal type erasure mechanism, we can narrow the interface
of the TargetMachine to *just* do what we need: build and return
a TTI object that we can then insert into the pass pipeline.
2) Make the TTI object be fully specialized for a particular function.
This will include splitting off a minimal form of it which is
sufficient for the inliner and the old pass manager.
3) Add a new pass manager analysis which produces TTI objects from the
target machine for each function. This may actually be done as part
of #2 in order to use the new analysis to implement #2.
4) Work on narrowing the API between TTI and the targets so that it is
easier to understand and less verbose to type erase.
5) Work on narrowing the API between TTI and its clients so that it is
easier to understand and less verbose to forward.
6) Try to improve the CRTP-based delegation. I feel like this code is
just a bit messy and exacerbating the complexity of implementing
the TTI in each target.
Many thanks to Eric and Hal for their help here. I ended up blocked on
this somewhat more abruptly than I expected, and so I appreciate getting
it sorted out very quickly.
Differential Revision: http://reviews.llvm.org/D7293
llvm-svn: 227669
2015-01-31 11:43:40 +08:00
|
|
|
TTI = IgnoreTargetInfo
|
|
|
|
? nullptr
|
2015-02-01 20:01:35 +08:00
|
|
|
: &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
|
|
|
|
*BB.getParent());
|
2012-04-05 16:05:16 +08:00
|
|
|
|
|
|
|
return vectorizeBB(BB);
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2012-02-01 11:51:43 +08:00
|
|
|
BasicBlockPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<AliasAnalysis>();
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2012-02-01 11:51:43 +08:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
[PM] Change the core design of the TTI analysis to use a polymorphic
type erased interface and a single analysis pass rather than an
extremely complex analysis group.
The end result is that the TTI analysis can contain a type erased
implementation that supports the polymorphic TTI interface. We can build
one from a target-specific implementation or from a dummy one in the IR.
I've also factored all of the code into "mix-in"-able base classes,
including CRTP base classes to facilitate calling back up to the most
specialized form when delegating horizontally across the surface. These
aren't as clean as I would like and I'm planning to work on cleaning
some of this up, but I wanted to start by putting into the right form.
There are a number of reasons for this change, and this particular
design. The first and foremost reason is that an analysis group is
complete overkill, and the chaining delegation strategy was so opaque,
confusing, and high overhead that TTI was suffering greatly for it.
Several of the TTI functions had failed to be implemented in all places
because of the chaining-based delegation making there be no checking of
this. A few other functions were implemented with incorrect delegation.
The message to me was very clear working on this -- the delegation and
analysis group structure was too confusing to be useful here.
The other reason of course is that this is *much* more natural fit for
the new pass manager. This will lay the ground work for a type-erased
per-function info object that can look up the correct subtarget and even
cache it.
Yet another benefit is that this will significantly simplify the
interaction of the pass managers and the TargetMachine. See the future
work below.
The downside of this change is that it is very, very verbose. I'm going
to work to improve that, but it is somewhat an implementation necessity
in C++ to do type erasure. =/ I discussed this design really extensively
with Eric and Hal prior to going down this path, and afterward showed
them the result. No one was really thrilled with it, but there doesn't
seem to be a substantially better alternative. Using a base class and
virtual method dispatch would make the code much shorter, but as
discussed in the update to the programmer's manual and elsewhere,
a polymorphic interface feels like the more principled approach even if
this is perhaps the least compelling example of it. ;]
Ultimately, there is still a lot more to be done here, but this was the
huge chunk that I couldn't really split things out of because this was
the interface change to TTI. I've tried to minimize all the other parts
of this. The follow up work should include at least:
1) Improving the TargetMachine interface by having it directly return
a TTI object. Because we have a non-pass object with value semantics
and an internal type erasure mechanism, we can narrow the interface
of the TargetMachine to *just* do what we need: build and return
a TTI object that we can then insert into the pass pipeline.
2) Make the TTI object be fully specialized for a particular function.
This will include splitting off a minimal form of it which is
sufficient for the inliner and the old pass manager.
3) Add a new pass manager analysis which produces TTI objects from the
target machine for each function. This may actually be done as part
of #2 in order to use the new analysis to implement #2.
4) Work on narrowing the API between TTI and the targets so that it is
easier to understand and less verbose to type erase.
5) Work on narrowing the API between TTI and its clients so that it is
easier to understand and less verbose to forward.
6) Try to improve the CRTP-based delegation. I feel like this code is
just a bit messy and exacerbating the complexity of implementing
the TTI in each target.
Many thanks to Eric and Hal for their help here. I ended up blocked on
this somewhat more abruptly than I expected, and so I appreciate getting
it sorted out very quickly.
Differential Revision: http://reviews.llvm.org/D7293
llvm-svn: 227669
2015-01-31 11:43:40 +08:00
|
|
|
AU.addRequired<TargetTransformInfoWrapperPass>();
|
2012-02-01 11:51:43 +08:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2012-02-01 11:51:43 +08:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2012-02-01 13:51:45 +08:00
|
|
|
AU.setPreservesCFG();
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
static inline VectorType *getVecTypeForPair(Type *ElemTy, Type *Elem2Ty) {
|
|
|
|
assert(ElemTy->getScalarType() == Elem2Ty->getScalarType() &&
|
|
|
|
"Cannot form vector from incompatible scalar types");
|
|
|
|
Type *STy = ElemTy->getScalarType();
|
|
|
|
|
|
|
|
unsigned numElem;
|
2012-02-01 11:51:43 +08:00
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(ElemTy)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
numElem = VTy->getNumElements();
|
|
|
|
} else {
|
|
|
|
numElem = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VectorType *VTy = dyn_cast<VectorType>(Elem2Ty)) {
|
|
|
|
numElem += VTy->getNumElements();
|
|
|
|
} else {
|
|
|
|
numElem += 1;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
2012-02-01 13:51:45 +08:00
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
return VectorType::get(STy, numElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void getInstructionTypes(Instruction *I,
|
|
|
|
Type *&T1, Type *&T2) {
|
2013-02-11 13:29:48 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
// For stores, it is the value type, not the pointer type that matters
|
|
|
|
// because the value is what will come from a vector register.
|
|
|
|
|
2013-02-11 13:29:48 +08:00
|
|
|
Value *IVal = SI->getValueOperand();
|
2012-06-28 13:42:42 +08:00
|
|
|
T1 = IVal->getType();
|
|
|
|
} else {
|
|
|
|
T1 = I->getType();
|
|
|
|
}
|
|
|
|
|
2013-02-11 13:29:48 +08:00
|
|
|
if (CastInst *CI = dyn_cast<CastInst>(I))
|
|
|
|
T2 = CI->getSrcTy();
|
2012-06-28 13:42:42 +08:00
|
|
|
else
|
|
|
|
T2 = T1;
|
2012-10-26 05:12:23 +08:00
|
|
|
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
|
|
|
|
T2 = SI->getCondition()->getType();
|
2012-11-12 22:50:59 +08:00
|
|
|
} else if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(I)) {
|
|
|
|
T2 = SI->getOperand(0)->getType();
|
2012-11-13 03:41:38 +08:00
|
|
|
} else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
|
|
|
|
T2 = CI->getOperand(0)->getType();
|
2012-10-26 05:12:23 +08:00
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the weight associated with the provided value. A chain of
|
|
|
|
// candidate pairs has a length given by the sum of the weights of its
|
|
|
|
// members (one weight per pair; the weight of each member of the pair
|
|
|
|
// is assumed to be the same). This length is then compared to the
|
|
|
|
// chain-length threshold to determine if a given chain is significant
|
|
|
|
// enough to be vectorized. The length is also used in comparing
|
|
|
|
// candidate chains where longer chains are considered to be better.
|
|
|
|
// Note: when this function returns 0, the resulting instructions are
|
|
|
|
// not actually fused.
|
2012-04-05 23:46:55 +08:00
|
|
|
inline size_t getDepthFactor(Value *V) {
|
2012-02-01 11:51:43 +08:00
|
|
|
// InsertElement and ExtractElement have a depth factor of zero. This is
|
|
|
|
// for two reasons: First, they cannot be usefully fused. Second, because
|
|
|
|
// the pass generates a lot of these, they can confuse the simple metric
|
2013-02-16 01:20:54 +08:00
|
|
|
// used to compare the dags in the next iteration. Thus, giving them a
|
2012-02-01 11:51:43 +08:00
|
|
|
// weight of zero allows the pass to essentially ignore them in
|
|
|
|
// subsequent iterations when looking for vectorization opportunities
|
|
|
|
// while still tracking dependency chains that flow through those
|
|
|
|
// instructions.
|
|
|
|
if (isa<InsertElementInst>(V) || isa<ExtractElementInst>(V))
|
|
|
|
return 0;
|
|
|
|
|
2012-02-04 12:14:04 +08:00
|
|
|
// Give a load or store half of the required depth so that load/store
|
|
|
|
// pairs will vectorize.
|
2012-04-05 23:46:55 +08:00
|
|
|
if (!Config.NoMemOpBoost && (isa<LoadInst>(V) || isa<StoreInst>(V)))
|
|
|
|
return Config.ReqChainDepth/2;
|
2012-02-04 12:14:04 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
// Returns the cost of the provided instruction using TTI.
|
2012-10-27 12:33:48 +08:00
|
|
|
// This does not handle loads and stores.
|
2014-02-13 07:43:47 +08:00
|
|
|
unsigned getInstrCost(unsigned Opcode, Type *T1, Type *T2,
|
|
|
|
TargetTransformInfo::OperandValueKind Op1VK =
|
|
|
|
TargetTransformInfo::OK_AnyValue,
|
|
|
|
TargetTransformInfo::OperandValueKind Op2VK =
|
|
|
|
TargetTransformInfo::OK_AnyValue) {
|
2012-10-27 12:33:48 +08:00
|
|
|
switch (Opcode) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
// We mark this instruction as zero-cost because scalar GEPs are usually
|
2013-09-28 19:46:15 +08:00
|
|
|
// lowered to the instruction addressing mode. At the moment we don't
|
2012-10-27 12:33:48 +08:00
|
|
|
// generate vector GEPs.
|
|
|
|
return 0;
|
|
|
|
case Instruction::Br:
|
2013-01-05 18:05:28 +08:00
|
|
|
return TTI->getCFInstrCost(Opcode);
|
2012-10-27 12:33:48 +08:00
|
|
|
case Instruction::PHI:
|
|
|
|
return 0;
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::FAdd:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::FSub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::FMul:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::FRem:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::And:
|
|
|
|
case Instruction::Or:
|
|
|
|
case Instruction::Xor:
|
2014-02-13 07:43:47 +08:00
|
|
|
return TTI->getArithmeticInstrCost(Opcode, T1, Op1VK, Op2VK);
|
2012-10-27 12:33:48 +08:00
|
|
|
case Instruction::Select:
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
2013-01-05 18:05:28 +08:00
|
|
|
return TTI->getCmpSelInstrCost(Opcode, T1, T2);
|
2012-10-27 12:33:48 +08:00
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::BitCast:
|
2012-11-01 14:26:34 +08:00
|
|
|
case Instruction::ShuffleVector:
|
2013-01-05 18:05:28 +08:00
|
|
|
return TTI->getCastInstrCost(Opcode, T1, T2);
|
2012-10-27 12:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// This determines the relative offset of two loads or stores, returning
|
|
|
|
// true if the offset could be determined to be some constant value.
|
|
|
|
// For example, if OffsetInElmts == 1, then J accesses the memory directly
|
|
|
|
// after I; if OffsetInElmts == -1 then I accesses the memory
|
2012-06-28 13:42:42 +08:00
|
|
|
// directly after J.
|
2012-02-01 11:51:43 +08:00
|
|
|
bool getPairPtrInfo(Instruction *I, Instruction *J,
|
|
|
|
Value *&IPtr, Value *&JPtr, unsigned &IAlignment, unsigned &JAlignment,
|
2012-10-26 05:12:23 +08:00
|
|
|
unsigned &IAddressSpace, unsigned &JAddressSpace,
|
2012-10-31 02:55:49 +08:00
|
|
|
int64_t &OffsetInElmts, bool ComputeOffset = true) {
|
2012-02-01 11:51:43 +08:00
|
|
|
OffsetInElmts = 0;
|
2012-10-26 05:12:23 +08:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
|
|
|
LoadInst *LJ = cast<LoadInst>(J);
|
|
|
|
IPtr = LI->getPointerOperand();
|
|
|
|
JPtr = LJ->getPointerOperand();
|
|
|
|
IAlignment = LI->getAlignment();
|
|
|
|
JAlignment = LJ->getAlignment();
|
|
|
|
IAddressSpace = LI->getPointerAddressSpace();
|
|
|
|
JAddressSpace = LJ->getPointerAddressSpace();
|
2012-02-01 11:51:43 +08:00
|
|
|
} else {
|
2012-10-26 05:12:23 +08:00
|
|
|
StoreInst *SI = cast<StoreInst>(I), *SJ = cast<StoreInst>(J);
|
|
|
|
IPtr = SI->getPointerOperand();
|
|
|
|
JPtr = SJ->getPointerOperand();
|
|
|
|
IAlignment = SI->getAlignment();
|
|
|
|
JAlignment = SJ->getAlignment();
|
|
|
|
IAddressSpace = SI->getPointerAddressSpace();
|
|
|
|
JAddressSpace = SJ->getPointerAddressSpace();
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2012-10-31 02:55:49 +08:00
|
|
|
if (!ComputeOffset)
|
|
|
|
return true;
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
const SCEV *IPtrSCEV = SE->getSCEV(IPtr);
|
|
|
|
const SCEV *JPtrSCEV = SE->getSCEV(JPtr);
|
|
|
|
|
|
|
|
// If this is a trivial offset, then we'll get something like
|
|
|
|
// 1*sizeof(type). With target data, which we need anyway, this will get
|
|
|
|
// constant folded into a number.
|
|
|
|
const SCEV *OffsetSCEV = SE->getMinusSCEV(JPtrSCEV, IPtrSCEV);
|
|
|
|
if (const SCEVConstant *ConstOffSCEV =
|
|
|
|
dyn_cast<SCEVConstant>(OffsetSCEV)) {
|
|
|
|
ConstantInt *IntOff = ConstOffSCEV->getValue();
|
|
|
|
int64_t Offset = IntOff->getSExtValue();
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = I->getModule()->getDataLayout();
|
2013-10-22 03:43:56 +08:00
|
|
|
Type *VTy = IPtr->getType()->getPointerElementType();
|
2015-03-10 10:37:25 +08:00
|
|
|
int64_t VTyTSS = (int64_t)DL.getTypeStoreSize(VTy);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-10-22 03:43:56 +08:00
|
|
|
Type *VTy2 = JPtr->getType()->getPointerElementType();
|
2012-06-28 13:42:42 +08:00
|
|
|
if (VTy != VTy2 && Offset < 0) {
|
2015-03-10 10:37:25 +08:00
|
|
|
int64_t VTy2TSS = (int64_t)DL.getTypeStoreSize(VTy2);
|
2012-06-28 13:42:42 +08:00
|
|
|
OffsetInElmts = Offset/VTy2TSS;
|
2015-03-10 04:20:16 +08:00
|
|
|
return (std::abs(Offset) % VTy2TSS) == 0;
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
OffsetInElmts = Offset/VTyTSS;
|
2015-03-10 04:20:16 +08:00
|
|
|
return (std::abs(Offset) % VTyTSS) == 0;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if the provided CallInst represents an intrinsic that can
|
|
|
|
// be vectorized.
|
|
|
|
bool isVectorizableIntrinsic(CallInst* I) {
|
|
|
|
Function *F = I->getCalledFunction();
|
|
|
|
if (!F) return false;
|
|
|
|
|
2012-12-26 09:36:57 +08:00
|
|
|
Intrinsic::ID IID = (Intrinsic::ID) F->getIntrinsicID();
|
2012-02-01 11:51:43 +08:00
|
|
|
if (!IID) return false;
|
|
|
|
|
|
|
|
switch(IID) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
case Intrinsic::sqrt:
|
|
|
|
case Intrinsic::powi:
|
|
|
|
case Intrinsic::sin:
|
|
|
|
case Intrinsic::cos:
|
|
|
|
case Intrinsic::log:
|
|
|
|
case Intrinsic::log2:
|
|
|
|
case Intrinsic::log10:
|
|
|
|
case Intrinsic::exp:
|
|
|
|
case Intrinsic::exp2:
|
|
|
|
case Intrinsic::pow:
|
2014-04-24 15:29:55 +08:00
|
|
|
case Intrinsic::round:
|
|
|
|
case Intrinsic::copysign:
|
|
|
|
case Intrinsic::ceil:
|
|
|
|
case Intrinsic::nearbyint:
|
|
|
|
case Intrinsic::rint:
|
|
|
|
case Intrinsic::trunc:
|
|
|
|
case Intrinsic::floor:
|
|
|
|
case Intrinsic::fabs:
|
2014-10-22 07:00:20 +08:00
|
|
|
case Intrinsic::minnum:
|
|
|
|
case Intrinsic::maxnum:
|
2012-04-07 11:56:23 +08:00
|
|
|
return Config.VectorizeMath;
|
2014-04-25 11:33:48 +08:00
|
|
|
case Intrinsic::bswap:
|
|
|
|
case Intrinsic::ctpop:
|
|
|
|
case Intrinsic::ctlz:
|
|
|
|
case Intrinsic::cttz:
|
|
|
|
return Config.VectorizeBitManipulations;
|
2012-02-01 11:51:43 +08:00
|
|
|
case Intrinsic::fma:
|
2012-12-26 06:36:08 +08:00
|
|
|
case Intrinsic::fmuladd:
|
2012-04-07 11:56:23 +08:00
|
|
|
return Config.VectorizeFMA;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 07:55:36 +08:00
|
|
|
bool isPureIEChain(InsertElementInst *IE) {
|
|
|
|
InsertElementInst *IENext = IE;
|
|
|
|
do {
|
|
|
|
if (!isa<UndefValue>(IENext->getOperand(0)) &&
|
|
|
|
!isa<InsertElementInst>(IENext->getOperand(0))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while ((IENext =
|
|
|
|
dyn_cast<InsertElementInst>(IENext->getOperand(0))));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This function implements one vectorization iteration on the provided
|
|
|
|
// basic block. It returns true if the block is changed.
|
2012-06-28 13:42:42 +08:00
|
|
|
bool BBVectorize::vectorizePairs(BasicBlock &BB, bool NonPow2Len) {
|
2012-02-02 14:14:56 +08:00
|
|
|
bool ShouldContinue;
|
|
|
|
BasicBlock::iterator Start = BB.getFirstInsertionPt();
|
|
|
|
|
|
|
|
std::vector<Value *> AllPairableInsts;
|
|
|
|
DenseMap<Value *, Value *> AllChosenPairs;
|
2012-10-31 04:17:37 +08:00
|
|
|
DenseSet<ValuePair> AllFixedOrderPairs;
|
2012-10-31 23:17:07 +08:00
|
|
|
DenseMap<VPPair, unsigned> AllPairConnectionTypes;
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > AllConnectedPairs,
|
|
|
|
AllConnectedPairDeps;
|
2012-02-02 14:14:56 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
std::vector<Value *> PairableInsts;
|
2013-02-15 06:37:09 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > CandidatePairs;
|
2012-10-31 04:17:37 +08:00
|
|
|
DenseSet<ValuePair> FixedOrderPairs;
|
2012-10-26 05:12:23 +08:00
|
|
|
DenseMap<ValuePair, int> CandidatePairCostSavings;
|
2012-02-02 14:14:56 +08:00
|
|
|
ShouldContinue = getCandidatePairs(BB, Start, CandidatePairs,
|
2012-10-31 04:17:37 +08:00
|
|
|
FixedOrderPairs,
|
2012-10-26 05:12:23 +08:00
|
|
|
CandidatePairCostSavings,
|
2012-06-28 13:42:42 +08:00
|
|
|
PairableInsts, NonPow2Len);
|
2012-02-02 14:14:56 +08:00
|
|
|
if (PairableInsts.empty()) continue;
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2013-02-12 01:19:34 +08:00
|
|
|
// Build the candidate pair set for faster lookups.
|
|
|
|
DenseSet<ValuePair> CandidatePairsSet;
|
2013-02-15 06:37:09 +08:00
|
|
|
for (DenseMap<Value *, std::vector<Value *> >::iterator I =
|
|
|
|
CandidatePairs.begin(), E = CandidatePairs.end(); I != E; ++I)
|
|
|
|
for (std::vector<Value *>::iterator J = I->second.begin(),
|
|
|
|
JE = I->second.end(); J != JE; ++J)
|
|
|
|
CandidatePairsSet.insert(ValuePair(I->first, *J));
|
2013-02-12 01:19:34 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
// Now we have a map of all of the pairable instructions and we need to
|
|
|
|
// select the best possible pairing. A good pairing is one such that the
|
|
|
|
// users of the pair are also paired. This defines a (directed) forest
|
2012-09-27 18:14:43 +08:00
|
|
|
// over the pairs such that two pairs are connected iff the second pair
|
2012-02-02 14:14:56 +08:00
|
|
|
// uses the first.
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
// Note that it only matters that both members of the second pair use some
|
|
|
|
// element of the first pair (to allow for splatting).
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > ConnectedPairs,
|
|
|
|
ConnectedPairDeps;
|
2012-10-31 23:17:07 +08:00
|
|
|
DenseMap<VPPair, unsigned> PairConnectionTypes;
|
2013-02-12 01:19:34 +08:00
|
|
|
computeConnectedPairs(CandidatePairs, CandidatePairsSet,
|
|
|
|
PairableInsts, ConnectedPairs, PairConnectionTypes);
|
2012-02-02 14:14:56 +08:00
|
|
|
if (ConnectedPairs.empty()) continue;
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator
|
2012-10-31 23:17:07 +08:00
|
|
|
I = ConnectedPairs.begin(), IE = ConnectedPairs.end();
|
2013-02-15 06:38:04 +08:00
|
|
|
I != IE; ++I)
|
|
|
|
for (std::vector<ValuePair>::iterator J = I->second.begin(),
|
|
|
|
JE = I->second.end(); J != JE; ++J)
|
|
|
|
ConnectedPairDeps[*J].push_back(I->first);
|
2012-10-31 23:17:07 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
// Build the pairable-instruction dependency map
|
|
|
|
DenseSet<ValuePair> PairableInstUsers;
|
|
|
|
buildDepMap(BB, CandidatePairs, PairableInsts, PairableInstUsers);
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-02-03 01:29:39 +08:00
|
|
|
// There is now a graph of the connected pairs. For each variable, pick
|
2013-02-16 01:20:54 +08:00
|
|
|
// the pairing with the largest dag meeting the depth requirement on at
|
|
|
|
// least one branch. Then select all pairings that are part of that dag
|
2012-02-03 01:29:39 +08:00
|
|
|
// and remove them from the list of available pairings and pairable
|
|
|
|
// variables.
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
DenseMap<Value *, Value *> ChosenPairs;
|
2013-02-12 01:19:34 +08:00
|
|
|
choosePairs(CandidatePairs, CandidatePairsSet,
|
|
|
|
CandidatePairCostSavings,
|
2012-11-01 14:26:34 +08:00
|
|
|
PairableInsts, FixedOrderPairs, PairConnectionTypes,
|
|
|
|
ConnectedPairs, ConnectedPairDeps,
|
2012-02-02 14:14:56 +08:00
|
|
|
PairableInstUsers, ChosenPairs);
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
if (ChosenPairs.empty()) continue;
|
|
|
|
AllPairableInsts.insert(AllPairableInsts.end(), PairableInsts.begin(),
|
|
|
|
PairableInsts.end());
|
|
|
|
AllChosenPairs.insert(ChosenPairs.begin(), ChosenPairs.end());
|
2012-10-31 04:17:37 +08:00
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
// Only for the chosen pairs, propagate information on fixed-order pairs,
|
|
|
|
// pair connections, and their types to the data structures used by the
|
|
|
|
// pair fusion procedures.
|
2012-10-31 04:17:37 +08:00
|
|
|
for (DenseMap<Value *, Value *>::iterator I = ChosenPairs.begin(),
|
|
|
|
IE = ChosenPairs.end(); I != IE; ++I) {
|
|
|
|
if (FixedOrderPairs.count(*I))
|
|
|
|
AllFixedOrderPairs.insert(*I);
|
|
|
|
else if (FixedOrderPairs.count(ValuePair(I->second, I->first)))
|
|
|
|
AllFixedOrderPairs.insert(ValuePair(I->second, I->first));
|
2012-10-31 23:17:07 +08:00
|
|
|
|
|
|
|
for (DenseMap<Value *, Value *>::iterator J = ChosenPairs.begin();
|
|
|
|
J != IE; ++J) {
|
|
|
|
DenseMap<VPPair, unsigned>::iterator K =
|
|
|
|
PairConnectionTypes.find(VPPair(*I, *J));
|
|
|
|
if (K != PairConnectionTypes.end()) {
|
|
|
|
AllPairConnectionTypes.insert(*K);
|
|
|
|
} else {
|
|
|
|
K = PairConnectionTypes.find(VPPair(*J, *I));
|
|
|
|
if (K != PairConnectionTypes.end())
|
|
|
|
AllPairConnectionTypes.insert(*K);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator
|
2012-10-31 23:17:07 +08:00
|
|
|
I = ConnectedPairs.begin(), IE = ConnectedPairs.end();
|
2013-02-15 06:38:04 +08:00
|
|
|
I != IE; ++I)
|
|
|
|
for (std::vector<ValuePair>::iterator J = I->second.begin(),
|
|
|
|
JE = I->second.end(); J != JE; ++J)
|
|
|
|
if (AllPairConnectionTypes.count(VPPair(I->first, *J))) {
|
|
|
|
AllConnectedPairs[I->first].push_back(*J);
|
|
|
|
AllConnectedPairDeps[*J].push_back(I->first);
|
|
|
|
}
|
2012-02-02 14:14:56 +08:00
|
|
|
} while (ShouldContinue);
|
|
|
|
|
|
|
|
if (AllChosenPairs.empty()) return false;
|
|
|
|
NumFusedOps += AllChosenPairs.size();
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// A set of pairs has now been selected. It is now necessary to replace the
|
|
|
|
// paired instructions with vector instructions. For this procedure each
|
2012-03-06 01:39:47 +08:00
|
|
|
// operand must be replaced with a vector operand. This vector is formed
|
2012-02-01 11:51:43 +08:00
|
|
|
// by using build_vector on the old operands. The replaced values are then
|
|
|
|
// replaced with a vector_extract on the result. Subsequent optimization
|
|
|
|
// passes should coalesce the build/extract combinations.
|
2012-03-06 01:39:45 +08:00
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
fuseChosenPairs(BB, AllPairableInsts, AllChosenPairs, AllFixedOrderPairs,
|
|
|
|
AllPairConnectionTypes,
|
|
|
|
AllConnectedPairs, AllConnectedPairDeps);
|
2012-06-28 13:42:42 +08:00
|
|
|
|
|
|
|
// It is important to cleanup here so that future iterations of this
|
|
|
|
// function have less work to do.
|
2015-03-10 10:37:25 +08:00
|
|
|
(void)SimplifyInstructionsInBlock(&BB, AA->getTargetLibraryInfo());
|
2012-02-01 11:51:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns true if the provided instruction is capable of being
|
|
|
|
// fused into a vector instruction. This determination is based only on the
|
|
|
|
// type and other attributes of the instruction.
|
|
|
|
bool BBVectorize::isInstVectorizable(Instruction *I,
|
|
|
|
bool &IsSimpleLoadStore) {
|
|
|
|
IsSimpleLoadStore = false;
|
|
|
|
|
|
|
|
if (CallInst *C = dyn_cast<CallInst>(I)) {
|
|
|
|
if (!isVectorizableIntrinsic(C))
|
|
|
|
return false;
|
|
|
|
} else if (LoadInst *L = dyn_cast<LoadInst>(I)) {
|
|
|
|
// Vectorize simple loads if possbile:
|
|
|
|
IsSimpleLoadStore = L->isSimple();
|
2012-04-07 11:56:23 +08:00
|
|
|
if (!IsSimpleLoadStore || !Config.VectorizeMemOps)
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
} else if (StoreInst *S = dyn_cast<StoreInst>(I)) {
|
|
|
|
// Vectorize simple stores if possbile:
|
|
|
|
IsSimpleLoadStore = S->isSimple();
|
2012-04-07 11:56:23 +08:00
|
|
|
if (!IsSimpleLoadStore || !Config.VectorizeMemOps)
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
} else if (CastInst *C = dyn_cast<CastInst>(I)) {
|
|
|
|
// We can vectorize casts, but not casts of pointer types, etc.
|
2012-04-07 11:56:23 +08:00
|
|
|
if (!Config.VectorizeCasts)
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Type *SrcTy = C->getSrcTy();
|
2012-04-14 15:32:43 +08:00
|
|
|
if (!SrcTy->isSingleValueType())
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Type *DestTy = C->getDestTy();
|
2012-04-14 15:32:43 +08:00
|
|
|
if (!DestTy->isSingleValueType())
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
2012-04-14 04:45:45 +08:00
|
|
|
} else if (isa<SelectInst>(I)) {
|
|
|
|
if (!Config.VectorizeSelect)
|
|
|
|
return false;
|
2012-06-24 05:52:50 +08:00
|
|
|
} else if (isa<CmpInst>(I)) {
|
|
|
|
if (!Config.VectorizeCmp)
|
|
|
|
return false;
|
2012-04-14 15:32:43 +08:00
|
|
|
} else if (GetElementPtrInst *G = dyn_cast<GetElementPtrInst>(I)) {
|
|
|
|
if (!Config.VectorizeGEP)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Currently, vector GEPs exist only with one index.
|
|
|
|
if (G->getNumIndices() != 1)
|
|
|
|
return false;
|
2012-02-01 11:51:43 +08:00
|
|
|
} else if (!(I->isBinaryOp() || isa<ShuffleVectorInst>(I) ||
|
|
|
|
isa<ExtractElementInst>(I) || isa<InsertElementInst>(I))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type *T1, *T2;
|
2012-06-28 13:42:42 +08:00
|
|
|
getInstructionTypes(I, T1, T2);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Not every type can be vectorized...
|
|
|
|
if (!(VectorType::isValidElementType(T1) || T1->isVectorTy()) ||
|
|
|
|
!(VectorType::isValidElementType(T2) || T2->isVectorTy()))
|
|
|
|
return false;
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
if (T1->getScalarSizeInBits() == 1) {
|
2012-06-24 21:28:01 +08:00
|
|
|
if (!Config.VectorizeBools)
|
|
|
|
return false;
|
|
|
|
} else {
|
2012-10-26 05:12:23 +08:00
|
|
|
if (!Config.VectorizeInts && T1->isIntOrIntVectorTy())
|
2012-06-24 21:28:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-26 05:12:23 +08:00
|
|
|
|
|
|
|
if (T2->getScalarSizeInBits() == 1) {
|
|
|
|
if (!Config.VectorizeBools)
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!Config.VectorizeInts && T2->isIntOrIntVectorTy())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-07 11:56:23 +08:00
|
|
|
if (!Config.VectorizeFloats
|
|
|
|
&& (T1->isFPOrFPVectorTy() || T2->isFPOrFPVectorTy()))
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
|
2012-04-28 03:34:00 +08:00
|
|
|
// Don't vectorize target-specific types.
|
|
|
|
if (T1->isX86_FP80Ty() || T1->isPPC_FP128Ty() || T1->isX86_MMXTy())
|
|
|
|
return false;
|
|
|
|
if (T2->isX86_FP80Ty() || T2->isPPC_FP128Ty() || T2->isX86_MMXTy())
|
|
|
|
return false;
|
|
|
|
|
2015-03-10 10:37:25 +08:00
|
|
|
if (!Config.VectorizePointers && (T1->getScalarType()->isPointerTy() ||
|
|
|
|
T2->getScalarType()->isPointerTy()))
|
2012-04-14 15:32:43 +08:00
|
|
|
return false;
|
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
if (!TTI && (T1->getPrimitiveSizeInBits() >= Config.VectorBits ||
|
|
|
|
T2->getPrimitiveSizeInBits() >= Config.VectorBits))
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function returns true if the two provided instructions are compatible
|
|
|
|
// (meaning that they can be fused into a vector instruction). This assumes
|
|
|
|
// that I has already been determined to be vectorizable and that J is not
|
2013-02-16 01:20:54 +08:00
|
|
|
// in the use dag of I.
|
2012-02-01 11:51:43 +08:00
|
|
|
bool BBVectorize::areInstsCompatible(Instruction *I, Instruction *J,
|
2012-10-26 05:12:23 +08:00
|
|
|
bool IsSimpleLoadStore, bool NonPow2Len,
|
2012-10-31 04:17:37 +08:00
|
|
|
int &CostSavings, int &FixedOrder) {
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(if (DebugInstructionExamination) dbgs() << "BBV: looking at " << *I <<
|
|
|
|
" <-> " << *J << "\n");
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
CostSavings = 0;
|
2012-10-31 04:17:37 +08:00
|
|
|
FixedOrder = 0;
|
2012-10-26 05:12:23 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// Loads and stores can be merged if they have different alignments,
|
|
|
|
// but are otherwise the same.
|
2012-06-28 13:42:42 +08:00
|
|
|
if (!J->isSameOperationAs(I, Instruction::CompareIgnoringAlignment |
|
|
|
|
(NonPow2Len ? Instruction::CompareUsingScalarTypes : 0)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Type *IT1, *IT2, *JT1, *JT2;
|
|
|
|
getInstructionTypes(I, IT1, IT2);
|
|
|
|
getInstructionTypes(J, JT1, JT2);
|
|
|
|
unsigned MaxTypeBits = std::max(
|
|
|
|
IT1->getPrimitiveSizeInBits() + JT1->getPrimitiveSizeInBits(),
|
|
|
|
IT2->getPrimitiveSizeInBits() + JT2->getPrimitiveSizeInBits());
|
2013-01-05 18:05:28 +08:00
|
|
|
if (!TTI && MaxTypeBits > Config.VectorBits)
|
2012-02-01 11:51:43 +08:00
|
|
|
return false;
|
2012-06-28 13:42:26 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// FIXME: handle addsub-type operations!
|
|
|
|
|
|
|
|
if (IsSimpleLoadStore) {
|
|
|
|
Value *IPtr, *JPtr;
|
2012-10-26 05:12:23 +08:00
|
|
|
unsigned IAlignment, JAlignment, IAddressSpace, JAddressSpace;
|
2012-02-01 11:51:43 +08:00
|
|
|
int64_t OffsetInElmts = 0;
|
|
|
|
if (getPairPtrInfo(I, J, IPtr, JPtr, IAlignment, JAlignment,
|
2015-03-10 04:20:16 +08:00
|
|
|
IAddressSpace, JAddressSpace, OffsetInElmts) &&
|
|
|
|
std::abs(OffsetInElmts) == 1) {
|
2012-10-31 04:17:37 +08:00
|
|
|
FixedOrder = (int) OffsetInElmts;
|
2012-10-26 05:12:23 +08:00
|
|
|
unsigned BottomAlignment = IAlignment;
|
|
|
|
if (OffsetInElmts < 0) BottomAlignment = JAlignment;
|
|
|
|
|
|
|
|
Type *aTypeI = isa<StoreInst>(I) ?
|
|
|
|
cast<StoreInst>(I)->getValueOperand()->getType() : I->getType();
|
|
|
|
Type *aTypeJ = isa<StoreInst>(J) ?
|
|
|
|
cast<StoreInst>(J)->getValueOperand()->getType() : J->getType();
|
|
|
|
Type *VType = getVecTypeForPair(aTypeI, aTypeJ);
|
2012-06-28 13:42:42 +08:00
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
if (Config.AlignedOnly) {
|
2012-02-01 11:51:43 +08:00
|
|
|
// An aligned load or store is possible only if the instruction
|
|
|
|
// with the lower offset has an alignment suitable for the
|
|
|
|
// vector type.
|
2015-03-10 10:37:25 +08:00
|
|
|
const DataLayout &DL = I->getModule()->getDataLayout();
|
|
|
|
unsigned VecAlignment = DL.getPrefTypeAlignment(VType);
|
2012-02-01 11:51:43 +08:00
|
|
|
if (BottomAlignment < VecAlignment)
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-26 05:12:23 +08:00
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
if (TTI) {
|
|
|
|
unsigned ICost = TTI->getMemoryOpCost(I->getOpcode(), aTypeI,
|
|
|
|
IAlignment, IAddressSpace);
|
|
|
|
unsigned JCost = TTI->getMemoryOpCost(J->getOpcode(), aTypeJ,
|
|
|
|
JAlignment, JAddressSpace);
|
|
|
|
unsigned VCost = TTI->getMemoryOpCost(I->getOpcode(), VType,
|
|
|
|
BottomAlignment,
|
|
|
|
IAddressSpace);
|
2013-02-09 05:13:39 +08:00
|
|
|
|
|
|
|
ICost += TTI->getAddressComputationCost(aTypeI);
|
|
|
|
JCost += TTI->getAddressComputationCost(aTypeJ);
|
|
|
|
VCost += TTI->getAddressComputationCost(VType);
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
if (VCost > ICost + JCost)
|
|
|
|
return false;
|
2012-10-26 07:47:16 +08:00
|
|
|
|
2012-10-26 12:28:06 +08:00
|
|
|
// We don't want to fuse to a type that will be split, even
|
2012-10-26 07:47:16 +08:00
|
|
|
// if the two input types will also be split and there is no other
|
2012-10-26 12:28:06 +08:00
|
|
|
// associated cost.
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned VParts = TTI->getNumberOfParts(VType);
|
2012-10-26 12:28:06 +08:00
|
|
|
if (VParts > 1)
|
|
|
|
return false;
|
|
|
|
else if (!VParts && VCost == ICost + JCost)
|
2012-10-26 07:47:16 +08:00
|
|
|
return false;
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
CostSavings = ICost + JCost - VCost;
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-05 18:05:28 +08:00
|
|
|
} else if (TTI) {
|
2012-10-27 12:33:48 +08:00
|
|
|
unsigned ICost = getInstrCost(I->getOpcode(), IT1, IT2);
|
|
|
|
unsigned JCost = getInstrCost(J->getOpcode(), JT1, JT2);
|
2012-10-26 05:12:23 +08:00
|
|
|
Type *VT1 = getVecTypeForPair(IT1, JT1),
|
|
|
|
*VT2 = getVecTypeForPair(IT2, JT2);
|
2014-02-13 07:43:47 +08:00
|
|
|
TargetTransformInfo::OperandValueKind Op1VK =
|
|
|
|
TargetTransformInfo::OK_AnyValue;
|
|
|
|
TargetTransformInfo::OperandValueKind Op2VK =
|
|
|
|
TargetTransformInfo::OK_AnyValue;
|
|
|
|
|
|
|
|
// On some targets (example X86) the cost of a vector shift may vary
|
|
|
|
// depending on whether the second operand is a Uniform or
|
|
|
|
// NonUniform Constant.
|
|
|
|
switch (I->getOpcode()) {
|
|
|
|
default : break;
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
|
|
|
|
// If both I and J are scalar shifts by constant, then the
|
|
|
|
// merged vector shift count would be either a constant splat value
|
|
|
|
// or a non-uniform vector of constants.
|
|
|
|
if (ConstantInt *CII = dyn_cast<ConstantInt>(I->getOperand(1))) {
|
|
|
|
if (ConstantInt *CIJ = dyn_cast<ConstantInt>(J->getOperand(1)))
|
|
|
|
Op2VK = CII == CIJ ? TargetTransformInfo::OK_UniformConstantValue :
|
|
|
|
TargetTransformInfo::OK_NonUniformConstantValue;
|
|
|
|
} else {
|
|
|
|
// Check for a splat of a constant or for a non uniform vector
|
|
|
|
// of constants.
|
|
|
|
Value *IOp = I->getOperand(1);
|
|
|
|
Value *JOp = J->getOperand(1);
|
2014-02-14 00:48:38 +08:00
|
|
|
if ((isa<ConstantVector>(IOp) || isa<ConstantDataVector>(IOp)) &&
|
|
|
|
(isa<ConstantVector>(JOp) || isa<ConstantDataVector>(JOp))) {
|
|
|
|
Op2VK = TargetTransformInfo::OK_NonUniformConstantValue;
|
|
|
|
Constant *SplatValue = cast<Constant>(IOp)->getSplatValue();
|
2014-04-25 13:29:35 +08:00
|
|
|
if (SplatValue != nullptr &&
|
2014-02-14 00:48:38 +08:00
|
|
|
SplatValue == cast<Constant>(JOp)->getSplatValue())
|
|
|
|
Op2VK = TargetTransformInfo::OK_UniformConstantValue;
|
2014-02-13 07:43:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-27 00:49:04 +08:00
|
|
|
|
|
|
|
// Note that this procedure is incorrect for insert and extract element
|
|
|
|
// instructions (because combining these often results in a shuffle),
|
|
|
|
// but this cost is ignored (because insert and extract element
|
|
|
|
// instructions are assigned a zero depth factor and are not really
|
|
|
|
// fused in general).
|
2014-02-13 07:43:47 +08:00
|
|
|
unsigned VCost = getInstrCost(I->getOpcode(), VT1, VT2, Op1VK, Op2VK);
|
2012-10-26 05:12:23 +08:00
|
|
|
|
|
|
|
if (VCost > ICost + JCost)
|
|
|
|
return false;
|
2012-10-26 07:47:16 +08:00
|
|
|
|
2012-10-26 12:28:06 +08:00
|
|
|
// We don't want to fuse to a type that will be split, even
|
2012-10-26 07:47:16 +08:00
|
|
|
// if the two input types will also be split and there is no other
|
2012-10-26 12:28:06 +08:00
|
|
|
// associated cost.
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned VParts1 = TTI->getNumberOfParts(VT1),
|
|
|
|
VParts2 = TTI->getNumberOfParts(VT2);
|
2012-11-12 22:50:59 +08:00
|
|
|
if (VParts1 > 1 || VParts2 > 1)
|
2012-10-26 12:28:06 +08:00
|
|
|
return false;
|
2012-11-12 22:50:59 +08:00
|
|
|
else if ((!VParts1 || !VParts2) && VCost == ICost + JCost)
|
2012-10-26 07:47:16 +08:00
|
|
|
return false;
|
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
CostSavings = ICost + JCost - VCost;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-25 11:33:48 +08:00
|
|
|
// The powi,ctlz,cttz intrinsics are special because only the first
|
|
|
|
// argument is vectorized, the second arguments must be equal.
|
2012-03-31 11:38:40 +08:00
|
|
|
CallInst *CI = dyn_cast<CallInst>(I);
|
|
|
|
Function *FI;
|
2012-12-26 09:36:57 +08:00
|
|
|
if (CI && (FI = CI->getCalledFunction())) {
|
|
|
|
Intrinsic::ID IID = (Intrinsic::ID) FI->getIntrinsicID();
|
2014-04-25 11:33:48 +08:00
|
|
|
if (IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
|
|
|
|
IID == Intrinsic::cttz) {
|
2012-12-26 09:36:57 +08:00
|
|
|
Value *A1I = CI->getArgOperand(1),
|
|
|
|
*A1J = cast<CallInst>(J)->getArgOperand(1);
|
|
|
|
const SCEV *A1ISCEV = SE->getSCEV(A1I),
|
|
|
|
*A1JSCEV = SE->getSCEV(A1J);
|
|
|
|
return (A1ISCEV == A1JSCEV);
|
|
|
|
}
|
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
if (IID && TTI) {
|
2012-12-26 09:36:57 +08:00
|
|
|
SmallVector<Type*, 4> Tys;
|
|
|
|
for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i)
|
|
|
|
Tys.push_back(CI->getArgOperand(i)->getType());
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned ICost = TTI->getIntrinsicInstrCost(IID, IT1, Tys);
|
2012-12-26 09:36:57 +08:00
|
|
|
|
|
|
|
Tys.clear();
|
|
|
|
CallInst *CJ = cast<CallInst>(J);
|
|
|
|
for (unsigned i = 0, ie = CJ->getNumArgOperands(); i != ie; ++i)
|
|
|
|
Tys.push_back(CJ->getArgOperand(i)->getType());
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned JCost = TTI->getIntrinsicInstrCost(IID, JT1, Tys);
|
2012-12-26 09:36:57 +08:00
|
|
|
|
|
|
|
Tys.clear();
|
|
|
|
assert(CI->getNumArgOperands() == CJ->getNumArgOperands() &&
|
|
|
|
"Intrinsic argument counts differ");
|
|
|
|
for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i) {
|
2014-04-25 11:33:48 +08:00
|
|
|
if ((IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
|
|
|
|
IID == Intrinsic::cttz) && i == 1)
|
2012-12-26 09:36:57 +08:00
|
|
|
Tys.push_back(CI->getArgOperand(i)->getType());
|
|
|
|
else
|
|
|
|
Tys.push_back(getVecTypeForPair(CI->getArgOperand(i)->getType(),
|
|
|
|
CJ->getArgOperand(i)->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Type *RetTy = getVecTypeForPair(IT1, JT1);
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned VCost = TTI->getIntrinsicInstrCost(IID, RetTy, Tys);
|
2012-12-26 09:36:57 +08:00
|
|
|
|
|
|
|
if (VCost > ICost + JCost)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We don't want to fuse to a type that will be split, even
|
|
|
|
// if the two input types will also be split and there is no other
|
|
|
|
// associated cost.
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned RetParts = TTI->getNumberOfParts(RetTy);
|
2012-12-26 09:36:57 +08:00
|
|
|
if (RetParts > 1)
|
|
|
|
return false;
|
|
|
|
else if (!RetParts && VCost == ICost + JCost)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i) {
|
|
|
|
if (!Tys[i]->isVectorTy())
|
|
|
|
continue;
|
|
|
|
|
2013-01-05 18:05:28 +08:00
|
|
|
unsigned NumParts = TTI->getNumberOfParts(Tys[i]);
|
2012-12-26 09:36:57 +08:00
|
|
|
if (NumParts > 1)
|
|
|
|
return false;
|
|
|
|
else if (!NumParts && VCost == ICost + JCost)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CostSavings = ICost + JCost - VCost;
|
|
|
|
}
|
2012-03-31 11:38:40 +08:00
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out whether or not J uses I and update the users and write-set
|
|
|
|
// structures associated with I. Specifically, Users represents the set of
|
|
|
|
// instructions that depend on I. WriteSet represents the set
|
|
|
|
// of memory locations that are dependent on I. If UpdateUsers is true,
|
|
|
|
// and J uses I, then Users is updated to contain J and WriteSet is updated
|
|
|
|
// to contain any memory locations to which J writes. The function returns
|
|
|
|
// true if J uses I. By default, alias analysis is used to determine
|
|
|
|
// whether J reads from memory that overlaps with a location in WriteSet.
|
2013-02-15 06:38:04 +08:00
|
|
|
// If LoadMoveSet is not null, then it is a previously-computed map
|
2012-02-01 11:51:43 +08:00
|
|
|
// where the key is the memory-based user instruction and the value is
|
|
|
|
// the instruction to be compared with I. So, if LoadMoveSet is provided,
|
|
|
|
// then the alias analysis is not used. This is necessary because this
|
|
|
|
// function is called during the process of moving instructions during
|
|
|
|
// vectorization and the results of the alias analysis are not stable during
|
|
|
|
// that process.
|
|
|
|
bool BBVectorize::trackUsesOfI(DenseSet<Value *> &Users,
|
|
|
|
AliasSetTracker &WriteSet, Instruction *I,
|
|
|
|
Instruction *J, bool UpdateUsers,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> *LoadMoveSetPairs) {
|
2012-02-01 11:51:43 +08:00
|
|
|
bool UsesI = false;
|
|
|
|
|
|
|
|
// This instruction may already be marked as a user due, for example, to
|
|
|
|
// being a member of a selected pair.
|
|
|
|
if (Users.count(J))
|
|
|
|
UsesI = true;
|
|
|
|
|
|
|
|
if (!UsesI)
|
2012-02-01 13:51:45 +08:00
|
|
|
for (User::op_iterator JU = J->op_begin(), JE = J->op_end();
|
|
|
|
JU != JE; ++JU) {
|
2012-02-01 11:51:43 +08:00
|
|
|
Value *V = *JU;
|
|
|
|
if (I == V || Users.count(V)) {
|
|
|
|
UsesI = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!UsesI && J->mayReadFromMemory()) {
|
2013-02-11 13:29:49 +08:00
|
|
|
if (LoadMoveSetPairs) {
|
|
|
|
UsesI = LoadMoveSetPairs->count(ValuePair(J, I));
|
2012-02-01 11:51:43 +08:00
|
|
|
} else {
|
|
|
|
for (AliasSetTracker::iterator W = WriteSet.begin(),
|
|
|
|
WE = WriteSet.end(); W != WE; ++W) {
|
2012-02-10 23:52:40 +08:00
|
|
|
if (W->aliasesUnknownInst(J, *AA)) {
|
|
|
|
UsesI = true;
|
|
|
|
break;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UsesI && UpdateUsers) {
|
|
|
|
if (J->mayWriteToMemory()) WriteSet.add(J);
|
|
|
|
Users.insert(J);
|
|
|
|
}
|
|
|
|
|
|
|
|
return UsesI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function iterates over all instruction pairs in the provided
|
|
|
|
// basic block and collects all candidate pairs for vectorization.
|
2012-02-02 14:14:56 +08:00
|
|
|
bool BBVectorize::getCandidatePairs(BasicBlock &BB,
|
|
|
|
BasicBlock::iterator &Start,
|
2013-02-15 06:37:09 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
2012-10-31 04:17:37 +08:00
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
2012-10-26 05:12:23 +08:00
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
2012-06-28 13:42:42 +08:00
|
|
|
std::vector<Value *> &PairableInsts, bool NonPow2Len) {
|
BBVectorize: Cap the number of candidate pairs in each instruction group
For some basic blocks, it is possible to generate many candidate pairs for
relatively few pairable instructions. When many (tens of thousands) of these pairs
are generated for a single instruction group, the time taken to generate and
rank the different vectorization plans can become quite large. As a result, we now
cap the number of candidate pairs within each instruction group. This is done by
closing out the group once the threshold is reached (set now at 3000 pairs).
Although this will limit the overall compile-time impact, this may not be the best
way to achieve this result. It might be better, for example, to prune excessive
candidate pairs after the fact the prevent the generation of short, but highly-connected
groups. We can experiment with this in the future.
This change reduces the overall compile-time slowdown of the csa.ll test case in
PR15222 to ~5x. If 5x is still considered too large, a lower limit can be
used as the default.
This represents a functionality change, but only for very large inputs
(thus, there is no regression test).
llvm-svn: 175251
2013-02-15 12:28:42 +08:00
|
|
|
size_t TotalPairs = 0;
|
2012-02-01 11:51:43 +08:00
|
|
|
BasicBlock::iterator E = BB.end();
|
2012-02-02 14:14:56 +08:00
|
|
|
if (Start == E) return false;
|
|
|
|
|
|
|
|
bool ShouldContinue = false, IAfterStart = false;
|
|
|
|
for (BasicBlock::iterator I = Start++; I != E; ++I) {
|
|
|
|
if (I == Start) IAfterStart = true;
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
bool IsSimpleLoadStore;
|
|
|
|
if (!isInstVectorizable(I, IsSimpleLoadStore)) continue;
|
|
|
|
|
|
|
|
// Look for an instruction with which to pair instruction *I...
|
|
|
|
DenseSet<Value *> Users;
|
|
|
|
AliasSetTracker WriteSet(*AA);
|
2013-08-14 07:34:32 +08:00
|
|
|
if (I->mayWriteToMemory()) WriteSet.add(I);
|
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
bool JAfterStart = IAfterStart;
|
2014-03-02 20:27:27 +08:00
|
|
|
BasicBlock::iterator J = std::next(I);
|
2012-04-05 23:46:55 +08:00
|
|
|
for (unsigned ss = 0; J != E && ss <= Config.SearchLimit; ++J, ++ss) {
|
2012-02-02 14:14:56 +08:00
|
|
|
if (J == Start) JAfterStart = true;
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// Determine if J uses I, if so, exit the loop.
|
2012-04-05 23:46:55 +08:00
|
|
|
bool UsesI = trackUsesOfI(Users, WriteSet, I, J, !Config.FastDep);
|
|
|
|
if (Config.FastDep) {
|
2012-02-01 11:51:43 +08:00
|
|
|
// Note: For this heuristic to be effective, independent operations
|
|
|
|
// must tend to be intermixed. This is likely to be true from some
|
|
|
|
// kinds of grouped loop unrolling (but not the generic LLVM pass),
|
|
|
|
// but otherwise may require some kind of reordering pass.
|
|
|
|
|
|
|
|
// When using fast dependency analysis,
|
|
|
|
// stop searching after first use:
|
|
|
|
if (UsesI) break;
|
|
|
|
} else {
|
|
|
|
if (UsesI) continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// J does not use I, and comes before the first use of I, so it can be
|
|
|
|
// merged with I if the instructions are compatible.
|
2012-10-31 04:17:37 +08:00
|
|
|
int CostSavings, FixedOrder;
|
2012-10-26 05:12:23 +08:00
|
|
|
if (!areInstsCompatible(I, J, IsSimpleLoadStore, NonPow2Len,
|
2012-10-31 04:17:37 +08:00
|
|
|
CostSavings, FixedOrder)) continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// J is a candidate for merging with I.
|
2015-01-15 19:41:30 +08:00
|
|
|
if (PairableInsts.empty() ||
|
2012-02-01 11:51:43 +08:00
|
|
|
PairableInsts[PairableInsts.size()-1] != I) {
|
|
|
|
PairableInsts.push_back(I);
|
|
|
|
}
|
2012-02-02 14:14:56 +08:00
|
|
|
|
2013-02-15 06:37:09 +08:00
|
|
|
CandidatePairs[I].push_back(J);
|
BBVectorize: Cap the number of candidate pairs in each instruction group
For some basic blocks, it is possible to generate many candidate pairs for
relatively few pairable instructions. When many (tens of thousands) of these pairs
are generated for a single instruction group, the time taken to generate and
rank the different vectorization plans can become quite large. As a result, we now
cap the number of candidate pairs within each instruction group. This is done by
closing out the group once the threshold is reached (set now at 3000 pairs).
Although this will limit the overall compile-time impact, this may not be the best
way to achieve this result. It might be better, for example, to prune excessive
candidate pairs after the fact the prevent the generation of short, but highly-connected
groups. We can experiment with this in the future.
This change reduces the overall compile-time slowdown of the csa.ll test case in
PR15222 to ~5x. If 5x is still considered too large, a lower limit can be
used as the default.
This represents a functionality change, but only for very large inputs
(thus, there is no regression test).
llvm-svn: 175251
2013-02-15 12:28:42 +08:00
|
|
|
++TotalPairs;
|
2013-01-05 18:05:28 +08:00
|
|
|
if (TTI)
|
2012-10-26 05:12:23 +08:00
|
|
|
CandidatePairCostSavings.insert(ValuePairWithCost(ValuePair(I, J),
|
|
|
|
CostSavings));
|
2012-02-02 14:14:56 +08:00
|
|
|
|
2012-10-31 04:17:37 +08:00
|
|
|
if (FixedOrder == 1)
|
|
|
|
FixedOrderPairs.insert(ValuePair(I, J));
|
|
|
|
else if (FixedOrder == -1)
|
|
|
|
FixedOrderPairs.insert(ValuePair(J, I));
|
|
|
|
|
2012-02-02 14:14:56 +08:00
|
|
|
// The next call to this function must start after the last instruction
|
|
|
|
// selected during this invocation.
|
|
|
|
if (JAfterStart) {
|
2014-03-02 20:27:27 +08:00
|
|
|
Start = std::next(J);
|
2012-02-02 14:14:56 +08:00
|
|
|
IAfterStart = JAfterStart = false;
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(if (DebugCandidateSelection) dbgs() << "BBV: candidate pair "
|
2012-10-26 05:12:23 +08:00
|
|
|
<< *I << " <-> " << *J << " (cost savings: " <<
|
|
|
|
CostSavings << ")\n");
|
2012-02-02 14:14:56 +08:00
|
|
|
|
|
|
|
// If we have already found too many pairs, break here and this function
|
|
|
|
// will be called again starting after the last instruction selected
|
|
|
|
// during this invocation.
|
BBVectorize: Cap the number of candidate pairs in each instruction group
For some basic blocks, it is possible to generate many candidate pairs for
relatively few pairable instructions. When many (tens of thousands) of these pairs
are generated for a single instruction group, the time taken to generate and
rank the different vectorization plans can become quite large. As a result, we now
cap the number of candidate pairs within each instruction group. This is done by
closing out the group once the threshold is reached (set now at 3000 pairs).
Although this will limit the overall compile-time impact, this may not be the best
way to achieve this result. It might be better, for example, to prune excessive
candidate pairs after the fact the prevent the generation of short, but highly-connected
groups. We can experiment with this in the future.
This change reduces the overall compile-time slowdown of the csa.ll test case in
PR15222 to ~5x. If 5x is still considered too large, a lower limit can be
used as the default.
This represents a functionality change, but only for very large inputs
(thus, there is no regression test).
llvm-svn: 175251
2013-02-15 12:28:42 +08:00
|
|
|
if (PairableInsts.size() >= Config.MaxInsts ||
|
|
|
|
TotalPairs >= Config.MaxPairs) {
|
2012-02-02 14:14:56 +08:00
|
|
|
ShouldContinue = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
2012-02-02 14:14:56 +08:00
|
|
|
|
|
|
|
if (ShouldContinue)
|
|
|
|
break;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: found " << PairableInsts.size()
|
|
|
|
<< " instructions with candidate pairs\n");
|
2012-02-02 14:14:56 +08:00
|
|
|
|
|
|
|
return ShouldContinue;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finds candidate pairs connected to the pair P = <PI, PJ>. This means that
|
|
|
|
// it looks for pairs such that both members have an input which is an
|
|
|
|
// output of PI or PJ.
|
|
|
|
void BBVectorize::computePairsConnectedTo(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
ValuePair P) {
|
2012-04-14 15:32:50 +08:00
|
|
|
StoreInst *SI, *SJ;
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// For each possible pairing for this variable, look at the uses of
|
|
|
|
// the first value...
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator I = P.first->user_begin(),
|
|
|
|
E = P.first->user_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
User *UI = *I;
|
|
|
|
if (isa<LoadInst>(UI)) {
|
2012-04-14 15:32:50 +08:00
|
|
|
// A pair cannot be connected to a load because the load only takes one
|
|
|
|
// operand (the address) and it is a scalar even after vectorization.
|
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
} else if ((SI = dyn_cast<StoreInst>(UI)) &&
|
2012-04-14 15:32:50 +08:00
|
|
|
P.first == SI->getPointerOperand()) {
|
|
|
|
// Similarly, a pair cannot be connected to a store through its
|
|
|
|
// pointer operand.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// For each use of the first variable, look for uses of the second
|
|
|
|
// variable...
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *UJ : P.second->users()) {
|
|
|
|
if ((SJ = dyn_cast<StoreInst>(UJ)) &&
|
2012-04-14 15:32:50 +08:00
|
|
|
P.second == SJ->getPointerOperand())
|
|
|
|
continue;
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// Look for <I, J>:
|
2014-03-09 11:16:01 +08:00
|
|
|
if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
|
|
|
|
VPPair VP(P, ValuePair(UI, UJ));
|
2013-02-15 06:38:04 +08:00
|
|
|
ConnectedPairs[VP.first].push_back(VP.second);
|
2012-10-31 23:17:07 +08:00
|
|
|
PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionDirect));
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Look for <J, I>:
|
2014-03-09 11:16:01 +08:00
|
|
|
if (CandidatePairsSet.count(ValuePair(UJ, UI))) {
|
|
|
|
VPPair VP(P, ValuePair(UJ, UI));
|
2013-02-15 06:38:04 +08:00
|
|
|
ConnectedPairs[VP.first].push_back(VP.second);
|
2012-10-31 23:17:07 +08:00
|
|
|
PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSwap));
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2012-04-05 23:46:55 +08:00
|
|
|
if (Config.SplatBreaksChain) continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
// Look for cases where just the first value in the pair is used by
|
|
|
|
// both members of another pair (splatting).
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator J = P.first->user_begin(); J != E; ++J) {
|
|
|
|
User *UJ = *J;
|
|
|
|
if ((SJ = dyn_cast<StoreInst>(UJ)) &&
|
2012-04-14 15:32:50 +08:00
|
|
|
P.first == SJ->getPointerOperand())
|
|
|
|
continue;
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
|
|
|
|
VPPair VP(P, ValuePair(UI, UJ));
|
2013-02-15 06:38:04 +08:00
|
|
|
ConnectedPairs[VP.first].push_back(VP.second);
|
2012-10-31 23:17:07 +08:00
|
|
|
PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSplat));
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-05 23:46:55 +08:00
|
|
|
if (Config.SplatBreaksChain) return;
|
2012-02-01 11:51:43 +08:00
|
|
|
// Look for cases where just the second value in the pair is used by
|
|
|
|
// both members of another pair (splatting).
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator I = P.second->user_begin(),
|
|
|
|
E = P.second->user_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
User *UI = *I;
|
|
|
|
if (isa<LoadInst>(UI))
|
2012-04-14 15:32:50 +08:00
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
else if ((SI = dyn_cast<StoreInst>(UI)) &&
|
2012-04-14 15:32:50 +08:00
|
|
|
P.second == SI->getPointerOperand())
|
|
|
|
continue;
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Value::user_iterator J = P.second->user_begin(); J != E; ++J) {
|
|
|
|
User *UJ = *J;
|
|
|
|
if ((SJ = dyn_cast<StoreInst>(UJ)) &&
|
2012-04-14 15:32:50 +08:00
|
|
|
P.second == SJ->getPointerOperand())
|
|
|
|
continue;
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
if (CandidatePairsSet.count(ValuePair(UI, UJ))) {
|
|
|
|
VPPair VP(P, ValuePair(UI, UJ));
|
2013-02-15 06:38:04 +08:00
|
|
|
ConnectedPairs[VP.first].push_back(VP.second);
|
2012-10-31 23:17:07 +08:00
|
|
|
PairConnectionTypes.insert(VPPairWithType(VP, PairConnectionSplat));
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function figures out which pairs are connected. Two pairs are
|
|
|
|
// connected if some output of the first pair forms an input to both members
|
|
|
|
// of the second pair.
|
|
|
|
void BBVectorize::computeConnectedPairs(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes) {
|
2012-02-01 11:51:43 +08:00
|
|
|
for (std::vector<Value *>::iterator PI = PairableInsts.begin(),
|
|
|
|
PE = PairableInsts.end(); PI != PE; ++PI) {
|
2013-02-15 06:37:09 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> >::iterator PP =
|
|
|
|
CandidatePairs.find(*PI);
|
|
|
|
if (PP == CandidatePairs.end())
|
|
|
|
continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator P = PP->second.begin(),
|
|
|
|
E = PP->second.end(); P != E; ++P)
|
2013-02-12 01:19:34 +08:00
|
|
|
computePairsConnectedTo(CandidatePairs, CandidatePairsSet,
|
|
|
|
PairableInsts, ConnectedPairs,
|
2013-02-15 06:37:09 +08:00
|
|
|
PairConnectionTypes, ValuePair(*PI, *P));
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
DEBUG(size_t TotalPairs = 0;
|
|
|
|
for (DenseMap<ValuePair, std::vector<ValuePair> >::iterator I =
|
|
|
|
ConnectedPairs.begin(), IE = ConnectedPairs.end(); I != IE; ++I)
|
|
|
|
TotalPairs += I->second.size();
|
|
|
|
dbgs() << "BBV: found " << TotalPairs
|
2012-02-01 11:51:43 +08:00
|
|
|
<< " pair connections.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function builds a set of use tuples such that <A, B> is in the set
|
2013-02-16 01:20:54 +08:00
|
|
|
// if B is in the use dag of A. If B is in the use dag of A, then B
|
2012-02-01 11:51:43 +08:00
|
|
|
// depends on the output of A.
|
|
|
|
void BBVectorize::buildDepMap(
|
|
|
|
BasicBlock &BB,
|
2013-02-15 06:37:09 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers) {
|
|
|
|
DenseSet<Value *> IsInPair;
|
2013-02-15 06:37:09 +08:00
|
|
|
for (DenseMap<Value *, std::vector<Value *> >::iterator C =
|
|
|
|
CandidatePairs.begin(), E = CandidatePairs.end(); C != E; ++C) {
|
2012-02-01 11:51:43 +08:00
|
|
|
IsInPair.insert(C->first);
|
2013-02-15 06:37:09 +08:00
|
|
|
IsInPair.insert(C->second.begin(), C->second.end());
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-01-27 00:49:03 +08:00
|
|
|
// Iterate through the basic block, recording all users of each
|
2012-02-01 11:51:43 +08:00
|
|
|
// pairable instruction.
|
|
|
|
|
2013-02-12 07:02:17 +08:00
|
|
|
BasicBlock::iterator E = BB.end(), EL =
|
|
|
|
BasicBlock::iterator(cast<Instruction>(PairableInsts.back()));
|
2012-02-01 11:51:43 +08:00
|
|
|
for (BasicBlock::iterator I = BB.getFirstInsertionPt(); I != E; ++I) {
|
|
|
|
if (IsInPair.find(I) == IsInPair.end()) continue;
|
|
|
|
|
|
|
|
DenseSet<Value *> Users;
|
|
|
|
AliasSetTracker WriteSet(*AA);
|
2013-08-14 07:34:32 +08:00
|
|
|
if (I->mayWriteToMemory()) WriteSet.add(I);
|
|
|
|
|
2014-03-02 20:27:27 +08:00
|
|
|
for (BasicBlock::iterator J = std::next(I); J != E; ++J) {
|
2012-02-01 11:51:43 +08:00
|
|
|
(void) trackUsesOfI(Users, WriteSet, I, J);
|
|
|
|
|
2013-02-12 07:02:17 +08:00
|
|
|
if (J == EL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
for (DenseSet<Value *>::iterator U = Users.begin(), E = Users.end();
|
2013-02-12 07:02:09 +08:00
|
|
|
U != E; ++U) {
|
|
|
|
if (IsInPair.find(*U) == IsInPair.end()) continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
PairableInstUsers.insert(ValuePair(I, *U));
|
2013-02-12 07:02:09 +08:00
|
|
|
}
|
2013-02-12 07:02:17 +08:00
|
|
|
|
|
|
|
if (I == EL)
|
|
|
|
break;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if an input to pair P is an output of pair Q and also an
|
|
|
|
// input of pair Q is an output of pair P. If this is the case, then these
|
|
|
|
// two pairs cannot be simultaneously fused.
|
|
|
|
bool BBVectorize::pairsConflict(ValuePair P, ValuePair Q,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > *PairableInstUserMap,
|
|
|
|
DenseSet<VPPair> *PairableInstUserPairSet) {
|
2012-02-01 11:51:43 +08:00
|
|
|
// Two pairs are in conflict if they are mutual Users of eachother.
|
|
|
|
bool QUsesP = PairableInstUsers.count(ValuePair(P.first, Q.first)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(P.first, Q.second)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(P.second, Q.first)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(P.second, Q.second));
|
|
|
|
bool PUsesQ = PairableInstUsers.count(ValuePair(Q.first, P.first)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(Q.first, P.second)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(Q.second, P.first)) ||
|
|
|
|
PairableInstUsers.count(ValuePair(Q.second, P.second));
|
|
|
|
if (PairableInstUserMap) {
|
|
|
|
// FIXME: The expensive part of the cycle check is not so much the cycle
|
|
|
|
// check itself but this edge insertion procedure. This needs some
|
2013-02-15 06:38:04 +08:00
|
|
|
// profiling and probably a different data structure.
|
2012-02-01 11:51:43 +08:00
|
|
|
if (PUsesQ) {
|
2013-02-11 13:29:41 +08:00
|
|
|
if (PairableInstUserPairSet->insert(VPPair(Q, P)).second)
|
2013-02-15 06:38:04 +08:00
|
|
|
(*PairableInstUserMap)[Q].push_back(P);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
if (QUsesP) {
|
2013-02-11 13:29:41 +08:00
|
|
|
if (PairableInstUserPairSet->insert(VPPair(P, Q)).second)
|
2013-02-15 06:38:04 +08:00
|
|
|
(*PairableInstUserMap)[P].push_back(Q);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (QUsesP && PUsesQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function walks the use graph of current pairs to see if, starting
|
|
|
|
// from P, the walk returns to P.
|
|
|
|
bool BBVectorize::pairWillFormCycle(ValuePair P,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
|
|
|
|
DenseSet<ValuePair> &CurrentPairs) {
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(if (DebugCycleCheck)
|
|
|
|
dbgs() << "BBV: starting cycle check for : " << *P.first << " <-> "
|
|
|
|
<< *P.second << "\n");
|
|
|
|
// A lookup table of visisted pairs is kept because the PairableInstUserMap
|
|
|
|
// contains non-direct associations.
|
|
|
|
DenseSet<ValuePair> Visited;
|
2012-02-03 01:29:39 +08:00
|
|
|
SmallVector<ValuePair, 32> Q;
|
2012-02-01 11:51:43 +08:00
|
|
|
// General depth-first post-order traversal:
|
|
|
|
Q.push_back(P);
|
2012-02-03 01:29:39 +08:00
|
|
|
do {
|
|
|
|
ValuePair QTop = Q.pop_back_val();
|
2012-02-01 11:51:43 +08:00
|
|
|
Visited.insert(QTop);
|
|
|
|
|
|
|
|
DEBUG(if (DebugCycleCheck)
|
|
|
|
dbgs() << "BBV: cycle check visiting: " << *QTop.first << " <-> "
|
|
|
|
<< *QTop.second << "\n");
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
|
|
|
|
PairableInstUserMap.find(QTop);
|
|
|
|
if (QQ == PairableInstUserMap.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (std::vector<ValuePair>::iterator C = QQ->second.begin(),
|
|
|
|
CE = QQ->second.end(); C != CE; ++C) {
|
|
|
|
if (*C == P) {
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(dbgs()
|
|
|
|
<< "BBV: rejected to prevent non-trivial cycle formation: "
|
2013-02-15 06:38:04 +08:00
|
|
|
<< QTop.first << " <-> " << C->second << "\n");
|
2012-02-01 11:51:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
if (CurrentPairs.count(*C) && !Visited.count(*C))
|
|
|
|
Q.push_back(*C);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
2012-02-03 01:29:39 +08:00
|
|
|
} while (!Q.empty());
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// This function builds the initial dag of connected pairs with the
|
2012-02-01 11:51:43 +08:00
|
|
|
// pair J at the root.
|
2013-02-16 01:20:54 +08:00
|
|
|
void BBVectorize::buildInitialDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t> &DAG, ValuePair J) {
|
|
|
|
// Each of these pairs is viewed as the root node of a DAG. The DAG
|
2012-02-01 11:51:43 +08:00
|
|
|
// is then walked (depth-first). As this happens, we keep track of
|
2013-02-16 01:20:54 +08:00
|
|
|
// the pairs that compose the DAG and the maximum depth of the DAG.
|
2012-02-03 01:29:39 +08:00
|
|
|
SmallVector<ValuePairWithDepth, 32> Q;
|
2012-02-01 11:51:43 +08:00
|
|
|
// General depth-first post-order traversal:
|
|
|
|
Q.push_back(ValuePairWithDepth(J, getDepthFactor(J.first)));
|
2012-02-03 01:29:39 +08:00
|
|
|
do {
|
2012-02-01 11:51:43 +08:00
|
|
|
ValuePairWithDepth QTop = Q.back();
|
|
|
|
|
|
|
|
// Push each child onto the queue:
|
|
|
|
bool MoreChildren = false;
|
|
|
|
size_t MaxChildDepth = QTop.second;
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
|
|
|
|
ConnectedPairs.find(QTop.first);
|
|
|
|
if (QQ != ConnectedPairs.end())
|
|
|
|
for (std::vector<ValuePair>::iterator k = QQ->second.begin(),
|
|
|
|
ke = QQ->second.end(); k != ke; ++k) {
|
|
|
|
// Make sure that this child pair is still a candidate:
|
|
|
|
if (CandidatePairsSet.count(*k)) {
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t>::iterator C = DAG.find(*k);
|
|
|
|
if (C == DAG.end()) {
|
2013-02-15 06:38:04 +08:00
|
|
|
size_t d = getDepthFactor(k->first);
|
|
|
|
Q.push_back(ValuePairWithDepth(*k, QTop.second+d));
|
|
|
|
MoreChildren = true;
|
|
|
|
} else {
|
|
|
|
MaxChildDepth = std::max(MaxChildDepth, C->second);
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MoreChildren) {
|
2013-02-16 01:20:54 +08:00
|
|
|
// Record the current pair as part of the DAG:
|
|
|
|
DAG.insert(ValuePairWithDepth(QTop.first, MaxChildDepth));
|
2012-02-01 11:51:43 +08:00
|
|
|
Q.pop_back();
|
|
|
|
}
|
2012-02-03 01:29:39 +08:00
|
|
|
} while (!Q.empty());
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// Given some initial dag, prune it by removing conflicting pairs (pairs
|
2012-02-01 11:51:43 +08:00
|
|
|
// that cannot be simultaneously chosen for vectorization).
|
2013-02-16 01:20:54 +08:00
|
|
|
void BBVectorize::pruneDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
|
|
|
|
DenseSet<VPPair> &PairableInstUserPairSet,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t> &DAG,
|
|
|
|
DenseSet<ValuePair> &PrunedDAG, ValuePair J,
|
2013-02-15 06:38:04 +08:00
|
|
|
bool UseCycleCheck) {
|
2012-02-03 01:29:39 +08:00
|
|
|
SmallVector<ValuePairWithDepth, 32> Q;
|
2012-02-01 11:51:43 +08:00
|
|
|
// General depth-first post-order traversal:
|
|
|
|
Q.push_back(ValuePairWithDepth(J, getDepthFactor(J.first)));
|
2012-02-03 01:29:39 +08:00
|
|
|
do {
|
|
|
|
ValuePairWithDepth QTop = Q.pop_back_val();
|
2013-02-16 01:20:54 +08:00
|
|
|
PrunedDAG.insert(QTop.first);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Visit each child, pruning as necessary...
|
2012-11-15 03:53:27 +08:00
|
|
|
SmallVector<ValuePairWithDepth, 8> BestChildren;
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator QQ =
|
|
|
|
ConnectedPairs.find(QTop.first);
|
|
|
|
if (QQ == ConnectedPairs.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (std::vector<ValuePair>::iterator K = QQ->second.begin(),
|
|
|
|
KE = QQ->second.end(); K != KE; ++K) {
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t>::iterator C = DAG.find(*K);
|
|
|
|
if (C == DAG.end()) continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// This child is in the DAG, now we need to make sure it is the
|
2012-02-01 11:51:43 +08:00
|
|
|
// best of any conflicting children. There could be multiple
|
|
|
|
// conflicting children, so first, determine if we're keeping
|
|
|
|
// this child, then delete conflicting children as necessary.
|
|
|
|
|
|
|
|
// It is also necessary to guard against pairing-induced
|
|
|
|
// dependencies. Consider instructions a .. x .. y .. b
|
|
|
|
// such that (a,b) are to be fused and (x,y) are to be fused
|
|
|
|
// but a is an input to x and b is an output from y. This
|
|
|
|
// means that y cannot be moved after b but x must be moved
|
|
|
|
// after b for (a,b) to be fused. In other words, after
|
|
|
|
// fusing (a,b) we have y .. a/b .. x where y is an input
|
|
|
|
// to a/b and x is an output to a/b: x and y can no longer
|
|
|
|
// be legally fused. To prevent this condition, we must
|
2013-02-16 01:20:54 +08:00
|
|
|
// make sure that a child pair added to the DAG is not
|
2012-02-01 11:51:43 +08:00
|
|
|
// both an input and output of an already-selected pair.
|
|
|
|
|
|
|
|
// Pairing-induced dependencies can also form from more complicated
|
|
|
|
// cycles. The pair vs. pair conflicts are easy to check, and so
|
|
|
|
// that is done explicitly for "fast rejection", and because for
|
|
|
|
// child vs. child conflicts, we may prefer to keep the current
|
|
|
|
// pair in preference to the already-selected child.
|
|
|
|
DenseSet<ValuePair> CurrentPairs;
|
|
|
|
|
|
|
|
bool CanAdd = true;
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<ValuePairWithDepth>::iterator C2
|
2012-03-06 01:39:47 +08:00
|
|
|
= BestChildren.begin(), E2 = BestChildren.end();
|
2012-02-01 11:51:43 +08:00
|
|
|
C2 != E2; ++C2) {
|
|
|
|
if (C2->first.first == C->first.first ||
|
|
|
|
C2->first.first == C->first.second ||
|
|
|
|
C2->first.second == C->first.first ||
|
|
|
|
C2->first.second == C->first.second ||
|
|
|
|
pairsConflict(C2->first, C->first, PairableInstUsers,
|
2014-04-25 13:29:35 +08:00
|
|
|
UseCycleCheck ? &PairableInstUserMap : nullptr,
|
|
|
|
UseCycleCheck ? &PairableInstUserPairSet
|
|
|
|
: nullptr)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
if (C2->second >= C->second) {
|
|
|
|
CanAdd = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentPairs.insert(C2->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!CanAdd) continue;
|
|
|
|
|
|
|
|
// Even worse, this child could conflict with another node already
|
2013-02-16 01:20:54 +08:00
|
|
|
// selected for the DAG. If that is the case, ignore this child.
|
|
|
|
for (DenseSet<ValuePair>::iterator T = PrunedDAG.begin(),
|
|
|
|
E2 = PrunedDAG.end(); T != E2; ++T) {
|
2012-02-01 11:51:43 +08:00
|
|
|
if (T->first == C->first.first ||
|
|
|
|
T->first == C->first.second ||
|
|
|
|
T->second == C->first.first ||
|
|
|
|
T->second == C->first.second ||
|
|
|
|
pairsConflict(*T, C->first, PairableInstUsers,
|
2014-04-25 13:29:35 +08:00
|
|
|
UseCycleCheck ? &PairableInstUserMap : nullptr,
|
|
|
|
UseCycleCheck ? &PairableInstUserPairSet
|
|
|
|
: nullptr)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
CanAdd = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentPairs.insert(*T);
|
|
|
|
}
|
|
|
|
if (!CanAdd) continue;
|
|
|
|
|
|
|
|
// And check the queue too...
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<ValuePairWithDepth>::iterator C2 = Q.begin(),
|
2012-02-01 11:51:43 +08:00
|
|
|
E2 = Q.end(); C2 != E2; ++C2) {
|
|
|
|
if (C2->first.first == C->first.first ||
|
|
|
|
C2->first.first == C->first.second ||
|
|
|
|
C2->first.second == C->first.first ||
|
|
|
|
C2->first.second == C->first.second ||
|
|
|
|
pairsConflict(C2->first, C->first, PairableInstUsers,
|
2014-04-25 13:29:35 +08:00
|
|
|
UseCycleCheck ? &PairableInstUserMap : nullptr,
|
|
|
|
UseCycleCheck ? &PairableInstUserPairSet
|
|
|
|
: nullptr)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
CanAdd = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentPairs.insert(C2->first);
|
|
|
|
}
|
|
|
|
if (!CanAdd) continue;
|
|
|
|
|
|
|
|
// Last but not least, check for a conflict with any of the
|
|
|
|
// already-chosen pairs.
|
|
|
|
for (DenseMap<Value *, Value *>::iterator C2 =
|
|
|
|
ChosenPairs.begin(), E2 = ChosenPairs.end();
|
|
|
|
C2 != E2; ++C2) {
|
|
|
|
if (pairsConflict(*C2, C->first, PairableInstUsers,
|
2014-04-25 13:29:35 +08:00
|
|
|
UseCycleCheck ? &PairableInstUserMap : nullptr,
|
|
|
|
UseCycleCheck ? &PairableInstUserPairSet
|
|
|
|
: nullptr)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
CanAdd = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentPairs.insert(*C2);
|
|
|
|
}
|
|
|
|
if (!CanAdd) continue;
|
|
|
|
|
2012-02-06 13:29:32 +08:00
|
|
|
// To check for non-trivial cycles formed by the addition of the
|
|
|
|
// current pair we've formed a list of all relevant pairs, now use a
|
|
|
|
// graph walk to check for a cycle. We start from the current pair and
|
2013-02-16 01:20:54 +08:00
|
|
|
// walk the use dag to see if we again reach the current pair. If we
|
2012-02-06 13:29:32 +08:00
|
|
|
// do, then the current pair is rejected.
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// FIXME: It may be more efficient to use a topological-ordering
|
|
|
|
// algorithm to improve the cycle check. This should be investigated.
|
|
|
|
if (UseCycleCheck &&
|
|
|
|
pairWillFormCycle(C->first, PairableInstUserMap, CurrentPairs))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// This child can be added, but we may have chosen it in preference
|
|
|
|
// to an already-selected child. Check for this here, and if a
|
|
|
|
// conflict is found, then remove the previously-selected child
|
|
|
|
// before adding this one in its place.
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<ValuePairWithDepth>::iterator C2
|
2012-03-06 01:39:47 +08:00
|
|
|
= BestChildren.begin(); C2 != BestChildren.end();) {
|
2012-02-01 11:51:43 +08:00
|
|
|
if (C2->first.first == C->first.first ||
|
|
|
|
C2->first.first == C->first.second ||
|
|
|
|
C2->first.second == C->first.first ||
|
|
|
|
C2->first.second == C->first.second ||
|
|
|
|
pairsConflict(C2->first, C->first, PairableInstUsers))
|
2012-11-15 02:38:11 +08:00
|
|
|
C2 = BestChildren.erase(C2);
|
2012-02-01 11:51:43 +08:00
|
|
|
else
|
|
|
|
++C2;
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:38:11 +08:00
|
|
|
BestChildren.push_back(ValuePairWithDepth(C->first, C->second));
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 09:31:24 +08:00
|
|
|
for (SmallVectorImpl<ValuePairWithDepth>::iterator C
|
2012-03-06 01:39:47 +08:00
|
|
|
= BestChildren.begin(), E2 = BestChildren.end();
|
2012-02-01 11:51:43 +08:00
|
|
|
C != E2; ++C) {
|
|
|
|
size_t DepthF = getDepthFactor(C->first.first);
|
|
|
|
Q.push_back(ValuePairWithDepth(C->first, QTop.second+DepthF));
|
|
|
|
}
|
2012-02-03 01:29:39 +08:00
|
|
|
} while (!Q.empty());
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// This function finds the best dag of mututally-compatible connected
|
2012-02-01 11:51:43 +08:00
|
|
|
// pairs, given the choice of root pairs as an iterator range.
|
2013-02-16 01:20:54 +08:00
|
|
|
void BBVectorize::findBestDAGFor(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &PairableInstUserMap,
|
|
|
|
DenseSet<VPPair> &PairableInstUserPairSet,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseSet<ValuePair> &BestDAG, size_t &BestMaxDepth,
|
2013-02-15 06:38:04 +08:00
|
|
|
int &BestEffSize, Value *II, std::vector<Value *>&JJ,
|
|
|
|
bool UseCycleCheck) {
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator J = JJ.begin(), JE = JJ.end();
|
|
|
|
J != JE; ++J) {
|
|
|
|
ValuePair IJ(II, *J);
|
|
|
|
if (!CandidatePairsSet.count(IJ))
|
|
|
|
continue;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Before going any further, make sure that this pair does not
|
|
|
|
// conflict with any already-selected pairs (see comment below
|
2013-02-16 01:20:54 +08:00
|
|
|
// near the DAG pruning for more details).
|
2012-02-01 11:51:43 +08:00
|
|
|
DenseSet<ValuePair> ChosenPairSet;
|
|
|
|
bool DoesConflict = false;
|
|
|
|
for (DenseMap<Value *, Value *>::iterator C = ChosenPairs.begin(),
|
|
|
|
E = ChosenPairs.end(); C != E; ++C) {
|
2013-02-15 06:37:09 +08:00
|
|
|
if (pairsConflict(*C, IJ, PairableInstUsers,
|
2014-04-25 13:29:35 +08:00
|
|
|
UseCycleCheck ? &PairableInstUserMap : nullptr,
|
|
|
|
UseCycleCheck ? &PairableInstUserPairSet : nullptr)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
DoesConflict = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChosenPairSet.insert(*C);
|
|
|
|
}
|
|
|
|
if (DoesConflict) continue;
|
|
|
|
|
|
|
|
if (UseCycleCheck &&
|
2013-02-15 06:37:09 +08:00
|
|
|
pairWillFormCycle(IJ, PairableInstUserMap, ChosenPairSet))
|
2012-02-01 11:51:43 +08:00
|
|
|
continue;
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseMap<ValuePair, size_t> DAG;
|
|
|
|
buildInitialDAGFor(CandidatePairs, CandidatePairsSet,
|
2013-02-12 01:19:34 +08:00
|
|
|
PairableInsts, ConnectedPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
PairableInstUsers, ChosenPairs, DAG, IJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Because we'll keep the child with the largest depth, the largest
|
2013-02-16 01:20:54 +08:00
|
|
|
// depth is still the same in the unpruned DAG.
|
|
|
|
size_t MaxDepth = DAG.lookup(IJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "BBV: found DAG for pair {"
|
2013-03-11 04:57:42 +08:00
|
|
|
<< *IJ.first << " <-> " << *IJ.second << "} of depth " <<
|
2013-02-16 01:20:54 +08:00
|
|
|
MaxDepth << " and size " << DAG.size() << "\n");
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// At this point the DAG has been constructed, but, may contain
|
2012-02-01 11:51:43 +08:00
|
|
|
// contradictory children (meaning that different children of
|
2013-02-16 01:20:54 +08:00
|
|
|
// some dag node may be attempting to fuse the same instruction).
|
|
|
|
// So now we walk the dag again, in the case of a conflict,
|
2012-02-01 11:51:43 +08:00
|
|
|
// keep only the child with the largest depth. To break a tie,
|
|
|
|
// favor the first child.
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseSet<ValuePair> PrunedDAG;
|
|
|
|
pruneDAGFor(CandidatePairs, PairableInsts, ConnectedPairs,
|
2013-02-12 01:19:34 +08:00
|
|
|
PairableInstUsers, PairableInstUserMap,
|
|
|
|
PairableInstUserPairSet,
|
2013-02-16 01:20:54 +08:00
|
|
|
ChosenPairs, DAG, PrunedDAG, IJ, UseCycleCheck);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-10-26 05:12:23 +08:00
|
|
|
int EffSize = 0;
|
2013-01-05 18:05:28 +08:00
|
|
|
if (TTI) {
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseSet<Value *> PrunedDAGInstrs;
|
|
|
|
for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
|
|
|
|
E = PrunedDAG.end(); S != E; ++S) {
|
|
|
|
PrunedDAGInstrs.insert(S->first);
|
|
|
|
PrunedDAGInstrs.insert(S->second);
|
2012-11-02 05:50:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The set of pairs that have already contributed to the total cost.
|
|
|
|
DenseSet<ValuePair> IncomingPairs;
|
|
|
|
|
2012-11-13 11:12:40 +08:00
|
|
|
// If the cost model were perfect, this might not be necessary; but we
|
|
|
|
// need to make sure that we don't get stuck vectorizing our own
|
|
|
|
// shuffle chains.
|
|
|
|
bool HasNontrivialInsts = false;
|
|
|
|
|
2012-11-01 14:26:34 +08:00
|
|
|
// The node weights represent the cost savings associated with
|
|
|
|
// fusing the pair of instructions.
|
2013-02-16 01:20:54 +08:00
|
|
|
for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
|
|
|
|
E = PrunedDAG.end(); S != E; ++S) {
|
2012-11-13 11:12:40 +08:00
|
|
|
if (!isa<ShuffleVectorInst>(S->first) &&
|
|
|
|
!isa<InsertElementInst>(S->first) &&
|
|
|
|
!isa<ExtractElementInst>(S->first))
|
|
|
|
HasNontrivialInsts = true;
|
|
|
|
|
2012-11-02 05:50:12 +08:00
|
|
|
bool FlipOrder = false;
|
|
|
|
|
|
|
|
if (getDepthFactor(S->first)) {
|
|
|
|
int ESContrib = CandidatePairCostSavings.find(*S)->second;
|
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "\tweight {"
|
|
|
|
<< *S->first << " <-> " << *S->second << "} = " <<
|
|
|
|
ESContrib << "\n");
|
|
|
|
EffSize += ESContrib;
|
|
|
|
}
|
2012-11-01 14:26:34 +08:00
|
|
|
|
2012-11-02 05:50:12 +08:00
|
|
|
// The edge weights contribute in a negative sense: they represent
|
|
|
|
// the cost of shuffles.
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator SS =
|
|
|
|
ConnectedPairDeps.find(*S);
|
|
|
|
if (SS != ConnectedPairDeps.end()) {
|
2012-11-01 14:26:34 +08:00
|
|
|
unsigned NumDepsDirect = 0, NumDepsSwap = 0;
|
2013-02-15 06:38:04 +08:00
|
|
|
for (std::vector<ValuePair>::iterator T = SS->second.begin(),
|
|
|
|
TE = SS->second.end(); T != TE; ++T) {
|
|
|
|
VPPair Q(*S, *T);
|
2013-02-16 01:20:54 +08:00
|
|
|
if (!PrunedDAG.count(Q.second))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
2012-11-01 14:26:34 +08:00
|
|
|
DenseMap<VPPair, unsigned>::iterator R =
|
2013-02-15 06:38:04 +08:00
|
|
|
PairConnectionTypes.find(VPPair(Q.second, Q.first));
|
2012-11-01 14:26:34 +08:00
|
|
|
assert(R != PairConnectionTypes.end() &&
|
|
|
|
"Cannot find pair connection type");
|
|
|
|
if (R->second == PairConnectionDirect)
|
|
|
|
++NumDepsDirect;
|
|
|
|
else if (R->second == PairConnectionSwap)
|
|
|
|
++NumDepsSwap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more swaps than direct connections, then
|
|
|
|
// the pair order will be flipped during fusion. So the real
|
|
|
|
// number of swaps is the minimum number.
|
2012-11-02 05:50:12 +08:00
|
|
|
FlipOrder = !FixedOrderPairs.count(*S) &&
|
2012-11-01 14:26:34 +08:00
|
|
|
((NumDepsSwap > NumDepsDirect) ||
|
|
|
|
FixedOrderPairs.count(ValuePair(S->second, S->first)));
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
for (std::vector<ValuePair>::iterator T = SS->second.begin(),
|
|
|
|
TE = SS->second.end(); T != TE; ++T) {
|
|
|
|
VPPair Q(*S, *T);
|
2013-02-16 01:20:54 +08:00
|
|
|
if (!PrunedDAG.count(Q.second))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
2012-11-01 14:26:34 +08:00
|
|
|
DenseMap<VPPair, unsigned>::iterator R =
|
2013-02-15 06:38:04 +08:00
|
|
|
PairConnectionTypes.find(VPPair(Q.second, Q.first));
|
2012-11-01 14:26:34 +08:00
|
|
|
assert(R != PairConnectionTypes.end() &&
|
|
|
|
"Cannot find pair connection type");
|
2013-02-15 06:38:04 +08:00
|
|
|
Type *Ty1 = Q.second.first->getType(),
|
|
|
|
*Ty2 = Q.second.second->getType();
|
2012-11-01 14:26:34 +08:00
|
|
|
Type *VTy = getVecTypeForPair(Ty1, Ty2);
|
|
|
|
if ((R->second == PairConnectionDirect && FlipOrder) ||
|
|
|
|
(R->second == PairConnectionSwap && !FlipOrder) ||
|
2012-11-02 05:50:12 +08:00
|
|
|
R->second == PairConnectionSplat) {
|
|
|
|
int ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
VTy, VTy);
|
2013-01-28 04:07:01 +08:00
|
|
|
|
|
|
|
if (VTy->getVectorNumElements() == 2) {
|
|
|
|
if (R->second == PairConnectionSplat)
|
|
|
|
ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
|
|
|
|
TargetTransformInfo::SK_Broadcast, VTy));
|
|
|
|
else
|
|
|
|
ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
|
|
|
|
TargetTransformInfo::SK_Reverse, VTy));
|
|
|
|
}
|
|
|
|
|
2012-11-02 05:50:12 +08:00
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
|
2013-02-15 06:38:04 +08:00
|
|
|
*Q.second.first << " <-> " << *Q.second.second <<
|
2012-11-02 05:50:12 +08:00
|
|
|
"} -> {" <<
|
|
|
|
*S->first << " <-> " << *S->second << "} = " <<
|
|
|
|
ESContrib << "\n");
|
|
|
|
EffSize -= ESContrib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the cost of outgoing edges. We assume that edges outgoing
|
|
|
|
// to shuffles, inserts or extracts can be merged, and so contribute
|
|
|
|
// no additional cost.
|
|
|
|
if (!S->first->getType()->isVoidTy()) {
|
|
|
|
Type *Ty1 = S->first->getType(),
|
|
|
|
*Ty2 = S->second->getType();
|
|
|
|
Type *VTy = getVecTypeForPair(Ty1, Ty2);
|
|
|
|
|
|
|
|
bool NeedsExtraction = false;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : S->first->users()) {
|
|
|
|
if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(U)) {
|
2012-11-13 05:21:02 +08:00
|
|
|
// Shuffle can be folded if it has no other input
|
|
|
|
if (isa<UndefValue>(SI->getOperand(1)))
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-09 11:16:01 +08:00
|
|
|
if (isa<ExtractElementInst>(U))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
if (PrunedDAGInstrs.count(U))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
|
|
|
NeedsExtraction = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedsExtraction) {
|
|
|
|
int ESContrib;
|
2013-01-28 04:07:01 +08:00
|
|
|
if (Ty1->isVectorTy()) {
|
2012-11-02 05:50:12 +08:00
|
|
|
ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
Ty1, VTy);
|
2013-01-28 04:07:01 +08:00
|
|
|
ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
|
|
|
|
TargetTransformInfo::SK_ExtractSubvector, VTy, 0, Ty1));
|
|
|
|
} else
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib = (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::ExtractElement, VTy, 0);
|
|
|
|
|
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
|
|
|
|
*S->first << "} = " << ESContrib << "\n");
|
|
|
|
EffSize -= ESContrib;
|
|
|
|
}
|
|
|
|
|
|
|
|
NeedsExtraction = false;
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : S->second->users()) {
|
|
|
|
if (ShuffleVectorInst *SI = dyn_cast<ShuffleVectorInst>(U)) {
|
2012-11-13 05:21:02 +08:00
|
|
|
// Shuffle can be folded if it has no other input
|
|
|
|
if (isa<UndefValue>(SI->getOperand(1)))
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-09 11:16:01 +08:00
|
|
|
if (isa<ExtractElementInst>(U))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
2014-03-09 11:16:01 +08:00
|
|
|
if (PrunedDAGInstrs.count(U))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
|
|
|
NeedsExtraction = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeedsExtraction) {
|
|
|
|
int ESContrib;
|
2013-01-28 04:07:01 +08:00
|
|
|
if (Ty2->isVectorTy()) {
|
2012-11-02 05:50:12 +08:00
|
|
|
ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
Ty2, VTy);
|
2013-01-28 04:07:01 +08:00
|
|
|
ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
|
|
|
|
TargetTransformInfo::SK_ExtractSubvector, VTy,
|
|
|
|
Ty1->isVectorTy() ? Ty1->getVectorNumElements() : 1, Ty2));
|
|
|
|
} else
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib = (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::ExtractElement, VTy, 1);
|
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "\tcost {" <<
|
|
|
|
*S->second << "} = " << ESContrib << "\n");
|
|
|
|
EffSize -= ESContrib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the cost of incoming edges.
|
|
|
|
if (!isa<LoadInst>(S->first) && !isa<StoreInst>(S->first)) {
|
|
|
|
Instruction *S1 = cast<Instruction>(S->first),
|
|
|
|
*S2 = cast<Instruction>(S->second);
|
|
|
|
for (unsigned o = 0; o < S1->getNumOperands(); ++o) {
|
|
|
|
Value *O1 = S1->getOperand(o), *O2 = S2->getOperand(o);
|
|
|
|
|
|
|
|
// Combining constants into vector constants (or small vector
|
|
|
|
// constants into larger ones are assumed free).
|
|
|
|
if (isa<Constant>(O1) && isa<Constant>(O2))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (FlipOrder)
|
|
|
|
std::swap(O1, O2);
|
|
|
|
|
|
|
|
ValuePair VP = ValuePair(O1, O2);
|
|
|
|
ValuePair VPR = ValuePair(O2, O1);
|
|
|
|
|
|
|
|
// Internal edges are not handled here.
|
2013-02-16 01:20:54 +08:00
|
|
|
if (PrunedDAG.count(VP) || PrunedDAG.count(VPR))
|
2012-11-02 05:50:12 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Type *Ty1 = O1->getType(),
|
|
|
|
*Ty2 = O2->getType();
|
|
|
|
Type *VTy = getVecTypeForPair(Ty1, Ty2);
|
|
|
|
|
|
|
|
// Combining vector operations of the same type is also assumed
|
|
|
|
// folded with other operations.
|
2012-11-13 05:21:02 +08:00
|
|
|
if (Ty1 == Ty2) {
|
|
|
|
// If both are insert elements, then both can be widened.
|
2012-11-13 07:55:36 +08:00
|
|
|
InsertElementInst *IEO1 = dyn_cast<InsertElementInst>(O1),
|
|
|
|
*IEO2 = dyn_cast<InsertElementInst>(O2);
|
|
|
|
if (IEO1 && IEO2 && isPureIEChain(IEO1) && isPureIEChain(IEO2))
|
2012-11-13 05:21:02 +08:00
|
|
|
continue;
|
|
|
|
// If both are extract elements, and both have the same input
|
|
|
|
// type, then they can be replaced with a shuffle
|
|
|
|
ExtractElementInst *EIO1 = dyn_cast<ExtractElementInst>(O1),
|
|
|
|
*EIO2 = dyn_cast<ExtractElementInst>(O2);
|
|
|
|
if (EIO1 && EIO2 &&
|
|
|
|
EIO1->getOperand(0)->getType() ==
|
|
|
|
EIO2->getOperand(0)->getType())
|
|
|
|
continue;
|
|
|
|
// If both are a shuffle with equal operand types and only two
|
|
|
|
// unqiue operands, then they can be replaced with a single
|
|
|
|
// shuffle
|
|
|
|
ShuffleVectorInst *SIO1 = dyn_cast<ShuffleVectorInst>(O1),
|
|
|
|
*SIO2 = dyn_cast<ShuffleVectorInst>(O2);
|
|
|
|
if (SIO1 && SIO2 &&
|
|
|
|
SIO1->getOperand(0)->getType() ==
|
|
|
|
SIO2->getOperand(0)->getType()) {
|
|
|
|
SmallSet<Value *, 4> SIOps;
|
|
|
|
SIOps.insert(SIO1->getOperand(0));
|
|
|
|
SIOps.insert(SIO1->getOperand(1));
|
|
|
|
SIOps.insert(SIO2->getOperand(0));
|
|
|
|
SIOps.insert(SIO2->getOperand(1));
|
|
|
|
if (SIOps.size() <= 2)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 05:50:12 +08:00
|
|
|
|
|
|
|
int ESContrib;
|
|
|
|
// This pair has already been formed.
|
|
|
|
if (IncomingPairs.count(VP)) {
|
|
|
|
continue;
|
|
|
|
} else if (IncomingPairs.count(VPR)) {
|
|
|
|
ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
VTy, VTy);
|
2013-01-28 04:07:01 +08:00
|
|
|
|
|
|
|
if (VTy->getVectorNumElements() == 2)
|
|
|
|
ESContrib = std::min(ESContrib, (int) TTI->getShuffleCost(
|
|
|
|
TargetTransformInfo::SK_Reverse, VTy));
|
2012-11-02 05:50:12 +08:00
|
|
|
} else if (!Ty1->isVectorTy() && !Ty2->isVectorTy()) {
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib = (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::InsertElement, VTy, 0);
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib += (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::InsertElement, VTy, 1);
|
|
|
|
} else if (!Ty1->isVectorTy()) {
|
|
|
|
// O1 needs to be inserted into a vector of size O2, and then
|
|
|
|
// both need to be shuffled together.
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib = (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::InsertElement, Ty2, 0);
|
|
|
|
ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
VTy, Ty2);
|
|
|
|
} else if (!Ty2->isVectorTy()) {
|
|
|
|
// O2 needs to be inserted into a vector of size O1, and then
|
|
|
|
// both need to be shuffled together.
|
2013-01-05 18:05:28 +08:00
|
|
|
ESContrib = (int) TTI->getVectorInstrCost(
|
2012-11-02 05:50:12 +08:00
|
|
|
Instruction::InsertElement, Ty1, 0);
|
|
|
|
ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
VTy, Ty1);
|
|
|
|
} else {
|
|
|
|
Type *TyBig = Ty1, *TySmall = Ty2;
|
|
|
|
if (Ty2->getVectorNumElements() > Ty1->getVectorNumElements())
|
|
|
|
std::swap(TyBig, TySmall);
|
|
|
|
|
|
|
|
ESContrib = (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
VTy, TyBig);
|
|
|
|
if (TyBig != TySmall)
|
|
|
|
ESContrib += (int) getInstrCost(Instruction::ShuffleVector,
|
|
|
|
TyBig, TySmall);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(if (DebugPairSelection) dbgs() << "\tcost {"
|
|
|
|
<< *O1 << " <-> " << *O2 << "} = " <<
|
|
|
|
ESContrib << "\n");
|
|
|
|
EffSize -= ESContrib;
|
|
|
|
IncomingPairs.insert(VP);
|
2012-11-01 14:26:34 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-26 05:12:23 +08:00
|
|
|
}
|
2012-11-13 11:12:40 +08:00
|
|
|
|
|
|
|
if (!HasNontrivialInsts) {
|
|
|
|
DEBUG(if (DebugPairSelection) dbgs() <<
|
2013-02-16 01:20:54 +08:00
|
|
|
"\tNo non-trivial instructions in DAG;"
|
2012-11-13 11:12:40 +08:00
|
|
|
" override to zero effective size\n");
|
|
|
|
EffSize = 0;
|
|
|
|
}
|
2012-10-26 05:12:23 +08:00
|
|
|
} else {
|
2013-02-16 01:20:54 +08:00
|
|
|
for (DenseSet<ValuePair>::iterator S = PrunedDAG.begin(),
|
|
|
|
E = PrunedDAG.end(); S != E; ++S)
|
2012-10-26 05:12:23 +08:00
|
|
|
EffSize += (int) getDepthFactor(S->first);
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
DEBUG(if (DebugPairSelection)
|
2013-02-16 01:20:54 +08:00
|
|
|
dbgs() << "BBV: found pruned DAG for pair {"
|
2013-03-11 04:57:42 +08:00
|
|
|
<< *IJ.first << " <-> " << *IJ.second << "} of depth " <<
|
2013-02-16 01:20:54 +08:00
|
|
|
MaxDepth << " and size " << PrunedDAG.size() <<
|
2012-02-01 11:51:43 +08:00
|
|
|
" (effective size: " << EffSize << ")\n");
|
2013-01-05 18:05:28 +08:00
|
|
|
if (((TTI && !UseChainDepthWithTI) ||
|
2012-11-02 05:50:12 +08:00
|
|
|
MaxDepth >= Config.ReqChainDepth) &&
|
2012-10-26 05:12:23 +08:00
|
|
|
EffSize > 0 && EffSize > BestEffSize) {
|
2012-02-01 11:51:43 +08:00
|
|
|
BestMaxDepth = MaxDepth;
|
|
|
|
BestEffSize = EffSize;
|
2013-02-16 01:20:54 +08:00
|
|
|
BestDAG = PrunedDAG;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given the list of candidate pairs, this function selects those
|
|
|
|
// that will be fused into vector instructions.
|
|
|
|
void BBVectorize::choosePairs(
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &CandidatePairs,
|
|
|
|
DenseSet<ValuePair> &CandidatePairsSet,
|
|
|
|
DenseMap<ValuePair, int> &CandidatePairCostSavings,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps,
|
|
|
|
DenseSet<ValuePair> &PairableInstUsers,
|
|
|
|
DenseMap<Value *, Value *>& ChosenPairs) {
|
2012-04-05 23:46:55 +08:00
|
|
|
bool UseCycleCheck =
|
2013-02-15 06:37:09 +08:00
|
|
|
CandidatePairsSet.size() <= Config.MaxCandPairsForCycleCheck;
|
|
|
|
|
|
|
|
DenseMap<Value *, std::vector<Value *> > CandidatePairs2;
|
|
|
|
for (DenseSet<ValuePair>::iterator I = CandidatePairsSet.begin(),
|
|
|
|
E = CandidatePairsSet.end(); I != E; ++I) {
|
|
|
|
std::vector<Value *> &JJ = CandidatePairs2[I->second];
|
|
|
|
if (JJ.empty()) JJ.reserve(32);
|
|
|
|
JJ.push_back(I->first);
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > PairableInstUserMap;
|
2013-02-11 13:29:41 +08:00
|
|
|
DenseSet<VPPair> PairableInstUserPairSet;
|
2012-02-01 11:51:43 +08:00
|
|
|
for (std::vector<Value *>::iterator I = PairableInsts.begin(),
|
|
|
|
E = PairableInsts.end(); I != E; ++I) {
|
|
|
|
// The number of possible pairings for this variable:
|
2013-02-15 06:37:09 +08:00
|
|
|
size_t NumChoices = CandidatePairs.lookup(*I).size();
|
2012-02-01 11:51:43 +08:00
|
|
|
if (!NumChoices) continue;
|
|
|
|
|
2013-02-15 06:37:09 +08:00
|
|
|
std::vector<Value *> &JJ = CandidatePairs[*I];
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// The best pair to choose and its dag:
|
2012-10-26 05:12:23 +08:00
|
|
|
size_t BestMaxDepth = 0;
|
|
|
|
int BestEffSize = 0;
|
2013-02-16 01:20:54 +08:00
|
|
|
DenseSet<ValuePair> BestDAG;
|
|
|
|
findBestDAGFor(CandidatePairs, CandidatePairsSet,
|
2013-02-12 01:19:34 +08:00
|
|
|
CandidatePairCostSavings,
|
2012-11-01 14:26:34 +08:00
|
|
|
PairableInsts, FixedOrderPairs, PairConnectionTypes,
|
|
|
|
ConnectedPairs, ConnectedPairDeps,
|
2013-02-11 13:29:41 +08:00
|
|
|
PairableInstUsers, PairableInstUserMap,
|
|
|
|
PairableInstUserPairSet, ChosenPairs,
|
2013-02-16 01:20:54 +08:00
|
|
|
BestDAG, BestMaxDepth, BestEffSize, *I, JJ,
|
2012-02-01 11:51:43 +08:00
|
|
|
UseCycleCheck);
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
if (BestDAG.empty())
|
2013-02-15 06:37:09 +08:00
|
|
|
continue;
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// A dag has been chosen (or not) at this point. If no dag was
|
2012-02-01 11:51:43 +08:00
|
|
|
// chosen, then this instruction, I, cannot be paired (and is no longer
|
|
|
|
// considered).
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
DEBUG(dbgs() << "BBV: selected pairs in the best DAG for: "
|
2013-02-15 06:37:09 +08:00
|
|
|
<< *cast<Instruction>(*I) << "\n");
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
for (DenseSet<ValuePair>::iterator S = BestDAG.begin(),
|
|
|
|
SE2 = BestDAG.end(); S != SE2; ++S) {
|
|
|
|
// Insert the members of this dag into the list of chosen pairs.
|
2012-02-01 11:51:43 +08:00
|
|
|
ChosenPairs.insert(ValuePair(S->first, S->second));
|
|
|
|
DEBUG(dbgs() << "BBV: selected pair: " << *S->first << " <-> " <<
|
|
|
|
*S->second << "\n");
|
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// Remove all candidate pairs that have values in the chosen dag.
|
2013-02-17 23:59:26 +08:00
|
|
|
std::vector<Value *> &KK = CandidatePairs[S->first];
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator K = KK.begin(), KE = KK.end();
|
|
|
|
K != KE; ++K) {
|
|
|
|
if (*K == S->second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CandidatePairsSet.erase(ValuePair(S->first, *K));
|
|
|
|
}
|
2013-02-17 23:59:26 +08:00
|
|
|
|
|
|
|
std::vector<Value *> &LL = CandidatePairs2[S->second];
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator L = LL.begin(), LE = LL.end();
|
|
|
|
L != LE; ++L) {
|
|
|
|
if (*L == S->first)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CandidatePairsSet.erase(ValuePair(*L, S->second));
|
|
|
|
}
|
2013-02-17 23:59:26 +08:00
|
|
|
|
|
|
|
std::vector<Value *> &MM = CandidatePairs[S->second];
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator M = MM.begin(), ME = MM.end();
|
|
|
|
M != ME; ++M) {
|
|
|
|
assert(*M != S->first && "Flipped pair in candidate list?");
|
|
|
|
CandidatePairsSet.erase(ValuePair(S->second, *M));
|
|
|
|
}
|
2013-02-17 23:59:26 +08:00
|
|
|
|
|
|
|
std::vector<Value *> &NN = CandidatePairs2[S->first];
|
2013-02-15 06:37:09 +08:00
|
|
|
for (std::vector<Value *>::iterator N = NN.begin(), NE = NN.end();
|
|
|
|
N != NE; ++N) {
|
|
|
|
assert(*N != S->second && "Flipped pair in candidate list?");
|
|
|
|
CandidatePairsSet.erase(ValuePair(*N, S->first));
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: selected " << ChosenPairs.size() << " pairs.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getReplacementName(Instruction *I, bool IsInput, unsigned o,
|
|
|
|
unsigned n = 0) {
|
|
|
|
if (!I->hasName())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return (I->getName() + (IsInput ? ".v.i" : ".v.r") + utostr(o) +
|
|
|
|
(n > 0 ? "." + utostr(n) : "")).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the value that is to be used as the pointer input to the vector
|
|
|
|
// instruction that fuses I with J.
|
|
|
|
Value *BBVectorize::getReplacementPointerInput(LLVMContext& Context,
|
2012-10-31 03:35:29 +08:00
|
|
|
Instruction *I, Instruction *J, unsigned o) {
|
2012-02-01 11:51:43 +08:00
|
|
|
Value *IPtr, *JPtr;
|
2012-10-26 05:12:23 +08:00
|
|
|
unsigned IAlignment, JAlignment, IAddressSpace, JAddressSpace;
|
2012-02-01 11:51:43 +08:00
|
|
|
int64_t OffsetInElmts;
|
2012-06-28 13:42:45 +08:00
|
|
|
|
2012-10-31 03:35:29 +08:00
|
|
|
// Note: the analysis might fail here, that is why the pair order has
|
2012-06-28 13:42:45 +08:00
|
|
|
// been precomputed (OffsetInElmts must be unused here).
|
2012-02-01 11:51:43 +08:00
|
|
|
(void) getPairPtrInfo(I, J, IPtr, JPtr, IAlignment, JAlignment,
|
2012-10-26 05:12:23 +08:00
|
|
|
IAddressSpace, JAddressSpace,
|
2012-10-31 02:55:49 +08:00
|
|
|
OffsetInElmts, false);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// The pointer value is taken to be the one with the lowest offset.
|
2012-10-31 03:35:29 +08:00
|
|
|
Value *VPtr = IPtr;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-10-22 03:43:56 +08:00
|
|
|
Type *ArgTypeI = IPtr->getType()->getPointerElementType();
|
|
|
|
Type *ArgTypeJ = JPtr->getType()->getPointerElementType();
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
|
2013-10-22 03:43:56 +08:00
|
|
|
Type *VArgPtrType
|
|
|
|
= PointerType::get(VArgType,
|
|
|
|
IPtr->getType()->getPointerAddressSpace());
|
2012-02-01 11:51:43 +08:00
|
|
|
return new BitCastInst(VPtr, VArgPtrType, getReplacementName(I, true, o),
|
2012-10-31 03:35:29 +08:00
|
|
|
/* insert before */ I);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BBVectorize::fillNewShuffleMask(LLVMContext& Context, Instruction *J,
|
2012-06-28 13:42:42 +08:00
|
|
|
unsigned MaskOffset, unsigned NumInElem,
|
|
|
|
unsigned NumInElem1, unsigned IdxOffset,
|
|
|
|
std::vector<Constant*> &Mask) {
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned NumElem1 = J->getType()->getVectorNumElements();
|
2012-06-28 13:42:42 +08:00
|
|
|
for (unsigned v = 0; v < NumElem1; ++v) {
|
2012-02-01 11:51:43 +08:00
|
|
|
int m = cast<ShuffleVectorInst>(J)->getMaskValue(v);
|
|
|
|
if (m < 0) {
|
|
|
|
Mask[v+MaskOffset] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
} else {
|
|
|
|
unsigned mm = m + (int) IdxOffset;
|
2012-06-28 13:42:42 +08:00
|
|
|
if (m >= (int) NumInElem1)
|
2012-02-01 11:51:43 +08:00
|
|
|
mm += (int) NumInElem;
|
|
|
|
|
|
|
|
Mask[v+MaskOffset] =
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context), mm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the value that is to be used as the vector-shuffle mask to the
|
|
|
|
// vector instruction that fuses I with J.
|
|
|
|
Value *BBVectorize::getReplacementShuffleMask(LLVMContext& Context,
|
|
|
|
Instruction *I, Instruction *J) {
|
|
|
|
// This is the shuffle mask. We need to append the second
|
|
|
|
// mask to the first, and the numbers need to be adjusted.
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *ArgTypeI = I->getType();
|
|
|
|
Type *ArgTypeJ = J->getType();
|
|
|
|
Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
|
|
|
|
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned NumElemI = ArgTypeI->getVectorNumElements();
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Get the total number of elements in the fused vector type.
|
|
|
|
// By definition, this must equal the number of elements in
|
|
|
|
// the final mask.
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned NumElem = VArgType->getVectorNumElements();
|
2012-02-01 11:51:43 +08:00
|
|
|
std::vector<Constant*> Mask(NumElem);
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *OpTypeI = I->getOperand(0)->getType();
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned NumInElemI = OpTypeI->getVectorNumElements();
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *OpTypeJ = J->getOperand(0)->getType();
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned NumInElemJ = OpTypeJ->getVectorNumElements();
|
2012-06-28 13:42:42 +08:00
|
|
|
|
|
|
|
// The fused vector will be:
|
|
|
|
// -----------------------------------------------------
|
|
|
|
// | NumInElemI | NumInElemJ | NumInElemI | NumInElemJ |
|
|
|
|
// -----------------------------------------------------
|
|
|
|
// from which we'll extract NumElem total elements (where the first NumElemI
|
|
|
|
// of them come from the mask in I and the remainder come from the mask
|
|
|
|
// in J.
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// For the mask from the first pair...
|
2012-06-28 13:42:42 +08:00
|
|
|
fillNewShuffleMask(Context, I, 0, NumInElemJ, NumInElemI,
|
|
|
|
0, Mask);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// For the mask from the second pair...
|
2012-06-28 13:42:42 +08:00
|
|
|
fillNewShuffleMask(Context, J, NumElemI, NumInElemI, NumInElemJ,
|
|
|
|
NumInElemI, Mask);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
return ConstantVector::get(Mask);
|
|
|
|
}
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
bool BBVectorize::expandIEChain(LLVMContext& Context, Instruction *I,
|
|
|
|
Instruction *J, unsigned o, Value *&LOp,
|
|
|
|
unsigned numElemL,
|
|
|
|
Type *ArgTypeL, Type *ArgTypeH,
|
2012-10-31 23:17:07 +08:00
|
|
|
bool IBeforeJ, unsigned IdxOff) {
|
2012-06-28 13:42:42 +08:00
|
|
|
bool ExpandedIEChain = false;
|
|
|
|
if (InsertElementInst *LIE = dyn_cast<InsertElementInst>(LOp)) {
|
|
|
|
// If we have a pure insertelement chain, then this can be rewritten
|
|
|
|
// into a chain that directly builds the larger type.
|
2012-11-13 07:55:36 +08:00
|
|
|
if (isPureIEChain(LIE)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
SmallVector<Value *, 8> VectElemts(numElemL,
|
|
|
|
UndefValue::get(ArgTypeL->getScalarType()));
|
|
|
|
InsertElementInst *LIENext = LIE;
|
|
|
|
do {
|
|
|
|
unsigned Idx =
|
|
|
|
cast<ConstantInt>(LIENext->getOperand(2))->getSExtValue();
|
|
|
|
VectElemts[Idx] = LIENext->getOperand(1);
|
|
|
|
} while ((LIENext =
|
|
|
|
dyn_cast<InsertElementInst>(LIENext->getOperand(0))));
|
|
|
|
|
2014-04-25 13:29:35 +08:00
|
|
|
LIENext = nullptr;
|
2012-06-28 13:42:42 +08:00
|
|
|
Value *LIEPrev = UndefValue::get(ArgTypeH);
|
|
|
|
for (unsigned i = 0; i < numElemL; ++i) {
|
|
|
|
if (isa<UndefValue>(VectElemts[i])) continue;
|
|
|
|
LIENext = InsertElementInst::Create(LIEPrev, VectElemts[i],
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context),
|
|
|
|
i + IdxOff),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, i+1));
|
|
|
|
LIENext->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
LIEPrev = LIENext;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOp = LIENext ? (Value*) LIENext : UndefValue::get(ArgTypeH);
|
|
|
|
ExpandedIEChain = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExpandedIEChain;
|
|
|
|
}
|
|
|
|
|
2013-10-01 08:01:14 +08:00
|
|
|
static unsigned getNumScalarElements(Type *Ty) {
|
|
|
|
if (VectorType *VecTy = dyn_cast<VectorType>(Ty))
|
|
|
|
return VecTy->getNumElements();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
// Returns the value to be used as the specified operand of the vector
|
|
|
|
// instruction that fuses I with J.
|
|
|
|
Value *BBVectorize::getReplacementInput(LLVMContext& Context, Instruction *I,
|
2012-10-31 23:17:07 +08:00
|
|
|
Instruction *J, unsigned o, bool IBeforeJ) {
|
2012-02-01 11:51:43 +08:00
|
|
|
Value *CV0 = ConstantInt::get(Type::getInt32Ty(Context), 0);
|
|
|
|
Value *CV1 = ConstantInt::get(Type::getInt32Ty(Context), 1);
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
// Compute the fused vector type for this operand
|
|
|
|
Type *ArgTypeI = I->getOperand(o)->getType();
|
|
|
|
Type *ArgTypeJ = J->getOperand(o)->getType();
|
|
|
|
VectorType *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
Instruction *L = I, *H = J;
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *ArgTypeL = ArgTypeI, *ArgTypeH = ArgTypeJ;
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-10-01 08:01:14 +08:00
|
|
|
unsigned numElemL = getNumScalarElements(ArgTypeL);
|
|
|
|
unsigned numElemH = getNumScalarElements(ArgTypeH);
|
2012-06-28 13:42:42 +08:00
|
|
|
|
|
|
|
Value *LOp = L->getOperand(o);
|
|
|
|
Value *HOp = H->getOperand(o);
|
|
|
|
unsigned numElem = VArgType->getNumElements();
|
|
|
|
|
|
|
|
// First, we check if we can reuse the "original" vector outputs (if these
|
|
|
|
// exist). We might need a shuffle.
|
|
|
|
ExtractElementInst *LEE = dyn_cast<ExtractElementInst>(LOp);
|
|
|
|
ExtractElementInst *HEE = dyn_cast<ExtractElementInst>(HOp);
|
|
|
|
ShuffleVectorInst *LSV = dyn_cast<ShuffleVectorInst>(LOp);
|
|
|
|
ShuffleVectorInst *HSV = dyn_cast<ShuffleVectorInst>(HOp);
|
|
|
|
|
|
|
|
// FIXME: If we're fusing shuffle instructions, then we can't apply this
|
|
|
|
// optimization. The input vectors to the shuffle might be a different
|
|
|
|
// length from the shuffle outputs. Unfortunately, the replacement
|
|
|
|
// shuffle mask has already been formed, and the mask entries are sensitive
|
|
|
|
// to the sizes of the inputs.
|
|
|
|
bool IsSizeChangeShuffle =
|
|
|
|
isa<ShuffleVectorInst>(L) &&
|
|
|
|
(LOp->getType() != L->getType() || HOp->getType() != H->getType());
|
|
|
|
|
|
|
|
if ((LEE || LSV) && (HEE || HSV) && !IsSizeChangeShuffle) {
|
|
|
|
// We can have at most two unique vector inputs.
|
|
|
|
bool CanUseInputs = true;
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *I1, *I2 = nullptr;
|
2012-06-28 13:42:42 +08:00
|
|
|
if (LEE) {
|
|
|
|
I1 = LEE->getOperand(0);
|
|
|
|
} else {
|
|
|
|
I1 = LSV->getOperand(0);
|
|
|
|
I2 = LSV->getOperand(1);
|
|
|
|
if (I2 == I1 || isa<UndefValue>(I2))
|
2014-04-25 13:29:35 +08:00
|
|
|
I2 = nullptr;
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HEE) {
|
|
|
|
Value *I3 = HEE->getOperand(0);
|
|
|
|
if (!I2 && I3 != I1)
|
|
|
|
I2 = I3;
|
|
|
|
else if (I3 != I1 && I3 != I2)
|
|
|
|
CanUseInputs = false;
|
|
|
|
} else {
|
|
|
|
Value *I3 = HSV->getOperand(0);
|
|
|
|
if (!I2 && I3 != I1)
|
|
|
|
I2 = I3;
|
|
|
|
else if (I3 != I1 && I3 != I2)
|
|
|
|
CanUseInputs = false;
|
|
|
|
|
|
|
|
if (CanUseInputs) {
|
|
|
|
Value *I4 = HSV->getOperand(1);
|
|
|
|
if (!isa<UndefValue>(I4)) {
|
|
|
|
if (!I2 && I4 != I1)
|
|
|
|
I2 = I4;
|
|
|
|
else if (I4 != I1 && I4 != I2)
|
|
|
|
CanUseInputs = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CanUseInputs) {
|
|
|
|
unsigned LOpElem =
|
2013-10-22 03:43:56 +08:00
|
|
|
cast<Instruction>(LOp)->getOperand(0)->getType()
|
|
|
|
->getVectorNumElements();
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
unsigned HOpElem =
|
2013-10-22 03:43:56 +08:00
|
|
|
cast<Instruction>(HOp)->getOperand(0)->getType()
|
|
|
|
->getVectorNumElements();
|
2012-06-28 13:42:42 +08:00
|
|
|
|
|
|
|
// We have one or two input vectors. We need to map each index of the
|
|
|
|
// operands to the index of the original vector.
|
|
|
|
SmallVector<std::pair<int, int>, 8> II(numElem);
|
|
|
|
for (unsigned i = 0; i < numElemL; ++i) {
|
|
|
|
int Idx, INum;
|
|
|
|
if (LEE) {
|
|
|
|
Idx =
|
|
|
|
cast<ConstantInt>(LEE->getOperand(1))->getSExtValue();
|
|
|
|
INum = LEE->getOperand(0) == I1 ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
Idx = LSV->getMaskValue(i);
|
|
|
|
if (Idx < (int) LOpElem) {
|
|
|
|
INum = LSV->getOperand(0) == I1 ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
Idx -= LOpElem;
|
|
|
|
INum = LSV->getOperand(1) == I1 ? 0 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
II[i] = std::pair<int, int>(Idx, INum);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < numElemH; ++i) {
|
|
|
|
int Idx, INum;
|
|
|
|
if (HEE) {
|
|
|
|
Idx =
|
|
|
|
cast<ConstantInt>(HEE->getOperand(1))->getSExtValue();
|
|
|
|
INum = HEE->getOperand(0) == I1 ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
Idx = HSV->getMaskValue(i);
|
|
|
|
if (Idx < (int) HOpElem) {
|
|
|
|
INum = HSV->getOperand(0) == I1 ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
Idx -= HOpElem;
|
|
|
|
INum = HSV->getOperand(1) == I1 ? 0 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
II[i + numElemL] = std::pair<int, int>(Idx, INum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now have an array which tells us from which index of which
|
|
|
|
// input vector each element of the operand comes.
|
|
|
|
VectorType *I1T = cast<VectorType>(I1->getType());
|
|
|
|
unsigned I1Elem = I1T->getNumElements();
|
|
|
|
|
|
|
|
if (!I2) {
|
|
|
|
// In this case there is only one underlying vector input. Check for
|
|
|
|
// the trivial case where we can use the input directly.
|
|
|
|
if (I1Elem == numElem) {
|
|
|
|
bool ElemInOrder = true;
|
|
|
|
for (unsigned i = 0; i < numElem; ++i) {
|
|
|
|
if (II[i].first != (int) i && II[i].first != -1) {
|
|
|
|
ElemInOrder = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ElemInOrder)
|
|
|
|
return I1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A shuffle is needed.
|
|
|
|
std::vector<Constant *> Mask(numElem);
|
|
|
|
for (unsigned i = 0; i < numElem; ++i) {
|
|
|
|
int Idx = II[i].first;
|
|
|
|
if (Idx == -1)
|
|
|
|
Mask[i] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
else
|
|
|
|
Mask[i] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *S =
|
|
|
|
new ShuffleVectorInst(I1, UndefValue::get(I1T),
|
|
|
|
ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o));
|
|
|
|
S->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorType *I2T = cast<VectorType>(I2->getType());
|
|
|
|
unsigned I2Elem = I2T->getNumElements();
|
|
|
|
|
|
|
|
// This input comes from two distinct vectors. The first step is to
|
|
|
|
// make sure that both vectors are the same length. If not, the
|
|
|
|
// smaller one will need to grow before they can be shuffled together.
|
|
|
|
if (I1Elem < I2Elem) {
|
|
|
|
std::vector<Constant *> Mask(I2Elem);
|
|
|
|
unsigned v = 0;
|
|
|
|
for (; v < I1Elem; ++v)
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
for (; v < I2Elem; ++v)
|
|
|
|
Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
|
|
|
|
Instruction *NewI1 =
|
|
|
|
new ShuffleVectorInst(I1, UndefValue::get(I1T),
|
|
|
|
ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
|
|
|
NewI1->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
I1 = NewI1;
|
|
|
|
I1Elem = I2Elem;
|
|
|
|
} else if (I1Elem > I2Elem) {
|
|
|
|
std::vector<Constant *> Mask(I1Elem);
|
|
|
|
unsigned v = 0;
|
|
|
|
for (; v < I2Elem; ++v)
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
for (; v < I1Elem; ++v)
|
|
|
|
Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
|
|
|
|
Instruction *NewI2 =
|
|
|
|
new ShuffleVectorInst(I2, UndefValue::get(I2T),
|
|
|
|
ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
|
|
|
NewI2->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
I2 = NewI2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that both I1 and I2 are the same length we can shuffle them
|
|
|
|
// together (and use the result).
|
|
|
|
std::vector<Constant *> Mask(numElem);
|
|
|
|
for (unsigned v = 0; v < numElem; ++v) {
|
|
|
|
if (II[v].first == -1) {
|
|
|
|
Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
} else {
|
|
|
|
int Idx = II[v].first + II[v].second * I1Elem;
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *NewOp =
|
|
|
|
new ShuffleVectorInst(I1, I2, ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J, true, o));
|
|
|
|
NewOp->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
return NewOp;
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *ArgType = ArgTypeL;
|
|
|
|
if (numElemL < numElemH) {
|
|
|
|
if (numElemL == 1 && expandIEChain(Context, I, J, o, HOp, numElemH,
|
2012-10-31 23:17:07 +08:00
|
|
|
ArgTypeL, VArgType, IBeforeJ, 1)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
// This is another short-circuit case: we're combining a scalar into
|
|
|
|
// a vector that is formed by an IE chain. We've just expanded the IE
|
|
|
|
// chain, now insert the scalar and we're done.
|
|
|
|
|
|
|
|
Instruction *S = InsertElementInst::Create(HOp, LOp, CV0,
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J, true, o));
|
|
|
|
S->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
return S;
|
|
|
|
} else if (!expandIEChain(Context, I, J, o, LOp, numElemL, ArgTypeL,
|
2012-10-31 23:17:07 +08:00
|
|
|
ArgTypeH, IBeforeJ)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
// The two vector inputs to the shuffle must be the same length,
|
|
|
|
// so extend the smaller vector to be the same length as the larger one.
|
|
|
|
Instruction *NLOp;
|
|
|
|
if (numElemL > 1) {
|
|
|
|
|
|
|
|
std::vector<Constant *> Mask(numElemH);
|
|
|
|
unsigned v = 0;
|
|
|
|
for (; v < numElemL; ++v)
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
for (; v < numElemH; ++v)
|
|
|
|
Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
|
|
|
|
NLOp = new ShuffleVectorInst(LOp, UndefValue::get(ArgTypeL),
|
|
|
|
ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
2012-06-28 13:42:42 +08:00
|
|
|
} else {
|
|
|
|
NLOp = InsertElementInst::Create(UndefValue::get(ArgTypeH), LOp, CV0,
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
NLOp->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
LOp = NLOp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgType = ArgTypeH;
|
|
|
|
} else if (numElemL > numElemH) {
|
|
|
|
if (numElemH == 1 && expandIEChain(Context, I, J, o, LOp, numElemL,
|
2012-10-31 23:17:07 +08:00
|
|
|
ArgTypeH, VArgType, IBeforeJ)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
Instruction *S =
|
|
|
|
InsertElementInst::Create(LOp, HOp,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(Context),
|
|
|
|
numElemL),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o));
|
|
|
|
S->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
return S;
|
|
|
|
} else if (!expandIEChain(Context, I, J, o, HOp, numElemH, ArgTypeH,
|
2012-10-31 23:17:07 +08:00
|
|
|
ArgTypeL, IBeforeJ)) {
|
2012-06-28 13:42:42 +08:00
|
|
|
Instruction *NHOp;
|
|
|
|
if (numElemH > 1) {
|
|
|
|
std::vector<Constant *> Mask(numElemL);
|
|
|
|
unsigned v = 0;
|
|
|
|
for (; v < numElemH; ++v)
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
for (; v < numElemL; ++v)
|
|
|
|
Mask[v] = UndefValue::get(Type::getInt32Ty(Context));
|
|
|
|
|
|
|
|
NHOp = new ShuffleVectorInst(HOp, UndefValue::get(ArgTypeH),
|
|
|
|
ConstantVector::get(Mask),
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
2012-06-28 13:42:42 +08:00
|
|
|
} else {
|
|
|
|
NHOp = InsertElementInst::Create(UndefValue::get(ArgTypeL), HOp, CV0,
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
2013-10-22 03:43:56 +08:00
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
NHOp->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
HOp = NHOp;
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
if (ArgType->isVectorTy()) {
|
2013-10-22 03:43:56 +08:00
|
|
|
unsigned numElem = VArgType->getVectorNumElements();
|
2012-06-28 13:42:42 +08:00
|
|
|
std::vector<Constant*> Mask(numElem);
|
|
|
|
for (unsigned v = 0; v < numElem; ++v) {
|
|
|
|
unsigned Idx = v;
|
|
|
|
// If the low vector was expanded, we need to skip the extra
|
|
|
|
// undefined entries.
|
|
|
|
if (v >= numElemL && numElemH > numElemL)
|
|
|
|
Idx += (numElemH - numElemL);
|
|
|
|
Mask[v] = ConstantInt::get(Type::getInt32Ty(Context), Idx);
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
Instruction *BV = new ShuffleVectorInst(LOp, HOp,
|
2012-10-31 23:17:07 +08:00
|
|
|
ConstantVector::get(Mask),
|
|
|
|
getReplacementName(IBeforeJ ? I : J, true, o));
|
|
|
|
BV->insertBefore(IBeforeJ ? J : I);
|
2012-02-01 11:51:43 +08:00
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *BV1 = InsertElementInst::Create(
|
2012-06-28 13:42:42 +08:00
|
|
|
UndefValue::get(VArgType), LOp, CV0,
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 1));
|
|
|
|
BV1->insertBefore(IBeforeJ ? J : I);
|
2012-06-28 13:42:42 +08:00
|
|
|
Instruction *BV2 = InsertElementInst::Create(BV1, HOp, CV1,
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementName(IBeforeJ ? I : J,
|
|
|
|
true, o, 2));
|
|
|
|
BV2->insertBefore(IBeforeJ ? J : I);
|
2012-02-01 11:51:43 +08:00
|
|
|
return BV2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function creates an array of values that will be used as the inputs
|
|
|
|
// to the vector instruction that fuses I with J.
|
|
|
|
void BBVectorize::getReplacementInputsForPair(LLVMContext& Context,
|
|
|
|
Instruction *I, Instruction *J,
|
2013-07-14 12:42:23 +08:00
|
|
|
SmallVectorImpl<Value *> &ReplacedOperands,
|
2012-10-31 23:17:07 +08:00
|
|
|
bool IBeforeJ) {
|
2012-02-01 11:51:43 +08:00
|
|
|
unsigned NumOperands = I->getNumOperands();
|
|
|
|
|
|
|
|
for (unsigned p = 0, o = NumOperands-1; p < NumOperands; ++p, --o) {
|
|
|
|
// Iterate backward so that we look at the store pointer
|
|
|
|
// first and know whether or not we need to flip the inputs.
|
|
|
|
|
|
|
|
if (isa<LoadInst>(I) || (o == 1 && isa<StoreInst>(I))) {
|
|
|
|
// This is the pointer for a load/store instruction.
|
2012-10-31 03:35:29 +08:00
|
|
|
ReplacedOperands[o] = getReplacementPointerInput(Context, I, J, o);
|
2012-02-01 11:51:43 +08:00
|
|
|
continue;
|
2012-03-31 11:38:40 +08:00
|
|
|
} else if (isa<CallInst>(I)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
Function *F = cast<CallInst>(I)->getCalledFunction();
|
2012-12-26 09:36:57 +08:00
|
|
|
Intrinsic::ID IID = (Intrinsic::ID) F->getIntrinsicID();
|
2012-03-31 11:38:40 +08:00
|
|
|
if (o == NumOperands-1) {
|
|
|
|
BasicBlock &BB = *I->getParent();
|
2012-04-05 23:46:55 +08:00
|
|
|
|
2012-03-31 11:38:40 +08:00
|
|
|
Module *M = BB.getParent()->getParent();
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *ArgTypeI = I->getType();
|
|
|
|
Type *ArgTypeJ = J->getType();
|
|
|
|
Type *VArgType = getVecTypeForPair(ArgTypeI, ArgTypeJ);
|
2012-04-05 23:46:55 +08:00
|
|
|
|
2012-12-26 09:36:57 +08:00
|
|
|
ReplacedOperands[o] = Intrinsic::getDeclaration(M, IID, VArgType);
|
2012-03-31 11:38:40 +08:00
|
|
|
continue;
|
2014-04-25 11:33:48 +08:00
|
|
|
} else if ((IID == Intrinsic::powi || IID == Intrinsic::ctlz ||
|
|
|
|
IID == Intrinsic::cttz) && o == 1) {
|
|
|
|
// The second argument of powi/ctlz/cttz is a single integer/constant
|
|
|
|
// and we've already checked that both arguments are equal.
|
|
|
|
// As a result, we just keep I's second argument.
|
2012-03-31 11:38:40 +08:00
|
|
|
ReplacedOperands[o] = I->getOperand(o);
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
} else if (isa<ShuffleVectorInst>(I) && o == NumOperands-1) {
|
|
|
|
ReplacedOperands[o] = getReplacementShuffleMask(Context, I, J);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
ReplacedOperands[o] = getReplacementInput(Context, I, J, o, IBeforeJ);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function creates two values that represent the outputs of the
|
|
|
|
// original I and J instructions. These are generally vector shuffles
|
|
|
|
// or extracts. In many cases, these will end up being unused and, thus,
|
|
|
|
// eliminated by later passes.
|
|
|
|
void BBVectorize::replaceOutputsOfPair(LLVMContext& Context, Instruction *I,
|
|
|
|
Instruction *J, Instruction *K,
|
|
|
|
Instruction *&InsertionPt,
|
2012-10-31 03:35:29 +08:00
|
|
|
Instruction *&K1, Instruction *&K2) {
|
2012-02-01 11:51:43 +08:00
|
|
|
if (isa<StoreInst>(I)) {
|
|
|
|
AA->replaceWithNewValue(I, K);
|
|
|
|
AA->replaceWithNewValue(J, K);
|
|
|
|
} else {
|
|
|
|
Type *IType = I->getType();
|
2012-06-28 13:42:42 +08:00
|
|
|
Type *JType = J->getType();
|
|
|
|
|
|
|
|
VectorType *VType = getVecTypeForPair(IType, JType);
|
|
|
|
unsigned numElem = VType->getNumElements();
|
|
|
|
|
2013-10-01 08:01:14 +08:00
|
|
|
unsigned numElemI = getNumScalarElements(IType);
|
|
|
|
unsigned numElemJ = getNumScalarElements(JType);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
if (IType->isVectorTy()) {
|
2012-06-28 13:42:42 +08:00
|
|
|
std::vector<Constant*> Mask1(numElemI), Mask2(numElemI);
|
|
|
|
for (unsigned v = 0; v < numElemI; ++v) {
|
|
|
|
Mask1[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
Mask2[v] = ConstantInt::get(Type::getInt32Ty(Context), numElemJ+v);
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2012-06-28 13:42:42 +08:00
|
|
|
K1 = new ShuffleVectorInst(K, UndefValue::get(VType),
|
2012-10-31 03:35:29 +08:00
|
|
|
ConstantVector::get( Mask1),
|
2012-06-28 13:42:42 +08:00
|
|
|
getReplacementName(K, false, 1));
|
2012-02-01 11:51:43 +08:00
|
|
|
} else {
|
2012-06-28 13:42:42 +08:00
|
|
|
Value *CV0 = ConstantInt::get(Type::getInt32Ty(Context), 0);
|
2012-10-31 03:35:29 +08:00
|
|
|
K1 = ExtractElementInst::Create(K, CV0,
|
2012-02-01 11:51:43 +08:00
|
|
|
getReplacementName(K, false, 1));
|
2012-06-28 13:42:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (JType->isVectorTy()) {
|
|
|
|
std::vector<Constant*> Mask1(numElemJ), Mask2(numElemJ);
|
|
|
|
for (unsigned v = 0; v < numElemJ; ++v) {
|
|
|
|
Mask1[v] = ConstantInt::get(Type::getInt32Ty(Context), v);
|
|
|
|
Mask2[v] = ConstantInt::get(Type::getInt32Ty(Context), numElemI+v);
|
|
|
|
}
|
|
|
|
|
|
|
|
K2 = new ShuffleVectorInst(K, UndefValue::get(VType),
|
2012-10-31 03:35:29 +08:00
|
|
|
ConstantVector::get( Mask2),
|
2012-06-28 13:42:42 +08:00
|
|
|
getReplacementName(K, false, 2));
|
|
|
|
} else {
|
|
|
|
Value *CV1 = ConstantInt::get(Type::getInt32Ty(Context), numElem-1);
|
2012-10-31 03:35:29 +08:00
|
|
|
K2 = ExtractElementInst::Create(K, CV1,
|
2012-02-01 11:51:43 +08:00
|
|
|
getReplacementName(K, false, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
K1->insertAfter(K);
|
|
|
|
K2->insertAfter(K1);
|
|
|
|
InsertionPt = K2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move all uses of the function I (including pairing-induced uses) after J.
|
|
|
|
bool BBVectorize::canMoveUsesOfIAfterJ(BasicBlock &BB,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *I, Instruction *J) {
|
|
|
|
// Skip to the first instruction past I.
|
2014-03-02 20:27:27 +08:00
|
|
|
BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
DenseSet<Value *> Users;
|
|
|
|
AliasSetTracker WriteSet(*AA);
|
2013-08-14 07:34:32 +08:00
|
|
|
if (I->mayWriteToMemory()) WriteSet.add(I);
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
for (; cast<Instruction>(L) != J; ++L)
|
2013-02-11 13:29:49 +08:00
|
|
|
(void) trackUsesOfI(Users, WriteSet, I, L, true, &LoadMoveSetPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
assert(cast<Instruction>(L) == J &&
|
|
|
|
"Tracking has not proceeded far enough to check for dependencies");
|
|
|
|
// If J is now in the use set of I, then trackUsesOfI will return true
|
|
|
|
// and we have a dependency cycle (and the fusing operation must abort).
|
2013-02-11 13:29:49 +08:00
|
|
|
return !trackUsesOfI(Users, WriteSet, I, J, true, &LoadMoveSetPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move all uses of the function I (including pairing-induced uses) after J.
|
|
|
|
void BBVectorize::moveUsesOfIAfterJ(BasicBlock &BB,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *&InsertionPt,
|
|
|
|
Instruction *I, Instruction *J) {
|
|
|
|
// Skip to the first instruction past I.
|
2014-03-02 20:27:27 +08:00
|
|
|
BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
DenseSet<Value *> Users;
|
|
|
|
AliasSetTracker WriteSet(*AA);
|
2013-08-14 07:34:32 +08:00
|
|
|
if (I->mayWriteToMemory()) WriteSet.add(I);
|
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
for (; cast<Instruction>(L) != J;) {
|
2013-02-11 13:29:49 +08:00
|
|
|
if (trackUsesOfI(Users, WriteSet, I, L, true, &LoadMoveSetPairs)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
// Move this instruction
|
|
|
|
Instruction *InstToMove = L; ++L;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: moving: " << *InstToMove <<
|
|
|
|
" to after " << *InsertionPt << "\n");
|
|
|
|
InstToMove->removeFromParent();
|
|
|
|
InstToMove->insertAfter(InsertionPt);
|
|
|
|
InsertionPt = InstToMove;
|
|
|
|
} else {
|
|
|
|
++L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all load instruction that are in the move set of a given first
|
|
|
|
// pair member. These loads depend on the first instruction, I, and so need
|
|
|
|
// to be moved after J (the second instruction) when the pair is fused.
|
|
|
|
void BBVectorize::collectPairLoadMoveSet(BasicBlock &BB,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs,
|
2012-02-01 11:51:43 +08:00
|
|
|
Instruction *I) {
|
|
|
|
// Skip to the first instruction past I.
|
2014-03-02 20:27:27 +08:00
|
|
|
BasicBlock::iterator L = std::next(BasicBlock::iterator(I));
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
DenseSet<Value *> Users;
|
|
|
|
AliasSetTracker WriteSet(*AA);
|
2013-08-14 07:34:32 +08:00
|
|
|
if (I->mayWriteToMemory()) WriteSet.add(I);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Note: We cannot end the loop when we reach J because J could be moved
|
|
|
|
// farther down the use chain by another instruction pairing. Also, J
|
|
|
|
// could be before I if this is an inverted input.
|
|
|
|
for (BasicBlock::iterator E = BB.end(); cast<Instruction>(L) != E; ++L) {
|
|
|
|
if (trackUsesOfI(Users, WriteSet, I, L)) {
|
2013-02-11 13:29:49 +08:00
|
|
|
if (L->mayReadFromMemory()) {
|
2013-02-15 06:38:04 +08:00
|
|
|
LoadMoveSet[L].push_back(I);
|
2013-02-11 13:29:49 +08:00
|
|
|
LoadMoveSetPairs.insert(ValuePair(L, I));
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In cases where both load/stores and the computation of their pointers
|
|
|
|
// are chosen for vectorization, we can end up in a situation where the
|
|
|
|
// aliasing analysis starts returning different query results as the
|
|
|
|
// process of fusing instruction pairs continues. Because the algorithm
|
2013-02-16 01:20:54 +08:00
|
|
|
// relies on finding the same use dags here as were found earlier, we'll
|
2012-02-01 11:51:43 +08:00
|
|
|
// need to precompute the necessary aliasing information here and then
|
|
|
|
// manually update it during the fusion process.
|
|
|
|
void BBVectorize::collectLoadMoveSet(BasicBlock &BB,
|
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > &LoadMoveSet,
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> &LoadMoveSetPairs) {
|
2012-02-01 11:51:43 +08:00
|
|
|
for (std::vector<Value *>::iterator PI = PairableInsts.begin(),
|
|
|
|
PIE = PairableInsts.end(); PI != PIE; ++PI) {
|
|
|
|
DenseMap<Value *, Value *>::iterator P = ChosenPairs.find(*PI);
|
|
|
|
if (P == ChosenPairs.end()) continue;
|
|
|
|
|
|
|
|
Instruction *I = cast<Instruction>(P->first);
|
2013-02-11 13:29:49 +08:00
|
|
|
collectPairLoadMoveSet(BB, ChosenPairs, LoadMoveSet,
|
|
|
|
LoadMoveSetPairs, I);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function fuses the chosen instruction pairs into vector instructions,
|
|
|
|
// taking care preserve any needed scalar outputs and, then, it reorders the
|
|
|
|
// remaining instructions as needed (users of the first member of the pair
|
|
|
|
// need to be moved to after the location of the second member of the pair
|
|
|
|
// because the vector instruction is inserted in the location of the pair's
|
|
|
|
// second member).
|
|
|
|
void BBVectorize::fuseChosenPairs(BasicBlock &BB,
|
2013-02-15 06:38:04 +08:00
|
|
|
std::vector<Value *> &PairableInsts,
|
|
|
|
DenseMap<Value *, Value *> &ChosenPairs,
|
|
|
|
DenseSet<ValuePair> &FixedOrderPairs,
|
|
|
|
DenseMap<VPPair, unsigned> &PairConnectionTypes,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairs,
|
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> > &ConnectedPairDeps) {
|
2012-02-01 11:51:43 +08:00
|
|
|
LLVMContext& Context = BB.getContext();
|
|
|
|
|
|
|
|
// During the vectorization process, the order of the pairs to be fused
|
|
|
|
// could be flipped. So we'll add each pair, flipped, into the ChosenPairs
|
|
|
|
// list. After a pair is fused, the flipped pair is removed from the list.
|
2012-10-31 23:17:07 +08:00
|
|
|
DenseSet<ValuePair> FlippedPairs;
|
2012-02-01 11:51:43 +08:00
|
|
|
for (DenseMap<Value *, Value *>::iterator P = ChosenPairs.begin(),
|
|
|
|
E = ChosenPairs.end(); P != E; ++P)
|
2012-10-31 23:17:07 +08:00
|
|
|
FlippedPairs.insert(ValuePair(P->second, P->first));
|
|
|
|
for (DenseSet<ValuePair>::iterator P = FlippedPairs.begin(),
|
2012-02-01 11:51:43 +08:00
|
|
|
E = FlippedPairs.end(); P != E; ++P)
|
|
|
|
ChosenPairs.insert(*P);
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> > LoadMoveSet;
|
2013-02-11 13:29:49 +08:00
|
|
|
DenseSet<ValuePair> LoadMoveSetPairs;
|
|
|
|
collectLoadMoveSet(BB, PairableInsts, ChosenPairs,
|
|
|
|
LoadMoveSet, LoadMoveSetPairs);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: initial: \n" << BB << "\n");
|
|
|
|
|
|
|
|
for (BasicBlock::iterator PI = BB.getFirstInsertionPt(); PI != BB.end();) {
|
|
|
|
DenseMap<Value *, Value *>::iterator P = ChosenPairs.find(PI);
|
|
|
|
if (P == ChosenPairs.end()) {
|
|
|
|
++PI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getDepthFactor(P->first) == 0) {
|
|
|
|
// These instructions are not really fused, but are tracked as though
|
|
|
|
// they are. Any case in which it would be interesting to fuse them
|
|
|
|
// will be taken care of by InstCombine.
|
|
|
|
--NumFusedOps;
|
|
|
|
++PI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *I = cast<Instruction>(P->first),
|
|
|
|
*J = cast<Instruction>(P->second);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: fusing: " << *I <<
|
|
|
|
" <-> " << *J << "\n");
|
|
|
|
|
|
|
|
// Remove the pair and flipped pair from the list.
|
|
|
|
DenseMap<Value *, Value *>::iterator FP = ChosenPairs.find(P->second);
|
|
|
|
assert(FP != ChosenPairs.end() && "Flipped pair not found in list");
|
|
|
|
ChosenPairs.erase(FP);
|
|
|
|
ChosenPairs.erase(P);
|
|
|
|
|
2013-02-11 13:29:49 +08:00
|
|
|
if (!canMoveUsesOfIAfterJ(BB, LoadMoveSetPairs, I, J)) {
|
2012-02-01 11:51:43 +08:00
|
|
|
DEBUG(dbgs() << "BBV: fusion of: " << *I <<
|
|
|
|
" <-> " << *J <<
|
|
|
|
" aborted because of non-trivial dependency cycle\n");
|
|
|
|
--NumFusedOps;
|
|
|
|
++PI;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-31 04:17:37 +08:00
|
|
|
// If the pair must have the other order, then flip it.
|
|
|
|
bool FlipPairOrder = FixedOrderPairs.count(ValuePair(J, I));
|
2012-10-31 23:17:07 +08:00
|
|
|
if (!FlipPairOrder && !FixedOrderPairs.count(ValuePair(I, J))) {
|
|
|
|
// This pair does not have a fixed order, and so we might want to
|
|
|
|
// flip it if that will yield fewer shuffles. We count the number
|
|
|
|
// of dependencies connected via swaps, and those directly connected,
|
|
|
|
// and flip the order if the number of swaps is greater.
|
|
|
|
bool OrigOrder = true;
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator IJ =
|
|
|
|
ConnectedPairDeps.find(ValuePair(I, J));
|
|
|
|
if (IJ == ConnectedPairDeps.end()) {
|
|
|
|
IJ = ConnectedPairDeps.find(ValuePair(J, I));
|
2012-10-31 23:17:07 +08:00
|
|
|
OrigOrder = false;
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:38:04 +08:00
|
|
|
if (IJ != ConnectedPairDeps.end()) {
|
2012-10-31 23:17:07 +08:00
|
|
|
unsigned NumDepsDirect = 0, NumDepsSwap = 0;
|
2013-02-15 06:38:04 +08:00
|
|
|
for (std::vector<ValuePair>::iterator T = IJ->second.begin(),
|
|
|
|
TE = IJ->second.end(); T != TE; ++T) {
|
|
|
|
VPPair Q(IJ->first, *T);
|
2012-10-31 23:17:07 +08:00
|
|
|
DenseMap<VPPair, unsigned>::iterator R =
|
2013-02-15 06:38:04 +08:00
|
|
|
PairConnectionTypes.find(VPPair(Q.second, Q.first));
|
2012-10-31 23:17:07 +08:00
|
|
|
assert(R != PairConnectionTypes.end() &&
|
|
|
|
"Cannot find pair connection type");
|
|
|
|
if (R->second == PairConnectionDirect)
|
|
|
|
++NumDepsDirect;
|
|
|
|
else if (R->second == PairConnectionSwap)
|
|
|
|
++NumDepsSwap;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OrigOrder)
|
|
|
|
std::swap(NumDepsDirect, NumDepsSwap);
|
|
|
|
|
|
|
|
if (NumDepsSwap > NumDepsDirect) {
|
|
|
|
FlipPairOrder = true;
|
|
|
|
DEBUG(dbgs() << "BBV: reordering pair: " << *I <<
|
|
|
|
" <-> " << *J << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 13:42:45 +08:00
|
|
|
|
2012-10-31 03:35:29 +08:00
|
|
|
Instruction *L = I, *H = J;
|
2012-10-31 04:17:37 +08:00
|
|
|
if (FlipPairOrder)
|
2012-10-31 03:35:29 +08:00
|
|
|
std::swap(H, L);
|
|
|
|
|
2012-10-31 23:17:07 +08:00
|
|
|
// If the pair being fused uses the opposite order from that in the pair
|
|
|
|
// connection map, then we need to flip the types.
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<ValuePair, std::vector<ValuePair> >::iterator HL =
|
|
|
|
ConnectedPairs.find(ValuePair(H, L));
|
|
|
|
if (HL != ConnectedPairs.end())
|
|
|
|
for (std::vector<ValuePair>::iterator T = HL->second.begin(),
|
|
|
|
TE = HL->second.end(); T != TE; ++T) {
|
|
|
|
VPPair Q(HL->first, *T);
|
|
|
|
DenseMap<VPPair, unsigned>::iterator R = PairConnectionTypes.find(Q);
|
|
|
|
assert(R != PairConnectionTypes.end() &&
|
|
|
|
"Cannot find pair connection type");
|
|
|
|
if (R->second == PairConnectionDirect)
|
|
|
|
R->second = PairConnectionSwap;
|
|
|
|
else if (R->second == PairConnectionSwap)
|
|
|
|
R->second = PairConnectionDirect;
|
|
|
|
}
|
2012-10-31 23:17:07 +08:00
|
|
|
|
|
|
|
bool LBeforeH = !FlipPairOrder;
|
2012-02-01 11:51:43 +08:00
|
|
|
unsigned NumOperands = I->getNumOperands();
|
|
|
|
SmallVector<Value *, 3> ReplacedOperands(NumOperands);
|
2012-10-31 23:17:07 +08:00
|
|
|
getReplacementInputsForPair(Context, L, H, ReplacedOperands,
|
|
|
|
LBeforeH);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
// Make a copy of the original operation, change its type to the vector
|
|
|
|
// type and replace its operands with the vector operands.
|
2012-10-31 23:17:07 +08:00
|
|
|
Instruction *K = L->clone();
|
|
|
|
if (L->hasName())
|
|
|
|
K->takeName(L);
|
|
|
|
else if (H->hasName())
|
|
|
|
K->takeName(H);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2015-04-24 05:36:23 +08:00
|
|
|
if (auto CS = CallSite(K)) {
|
|
|
|
SmallVector<Type *, 3> Tys;
|
|
|
|
FunctionType *Old = CS.getFunctionType();
|
|
|
|
unsigned NumOld = Old->getNumParams();
|
|
|
|
assert(NumOld <= ReplacedOperands.size());
|
|
|
|
for (unsigned i = 0; i != NumOld; ++i)
|
|
|
|
Tys.push_back(ReplacedOperands[i]->getType());
|
|
|
|
CS.mutateFunctionType(
|
|
|
|
FunctionType::get(getVecTypeForPair(L->getType(), H->getType()),
|
|
|
|
Tys, Old->isVarArg()));
|
|
|
|
} else if (!isa<StoreInst>(K))
|
2012-10-31 03:35:29 +08:00
|
|
|
K->mutateType(getVecTypeForPair(L->getType(), H->getType()));
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2014-08-15 23:46:38 +08:00
|
|
|
unsigned KnownIDs[] = {
|
|
|
|
LLVMContext::MD_tbaa,
|
|
|
|
LLVMContext::MD_alias_scope,
|
|
|
|
LLVMContext::MD_noalias,
|
|
|
|
LLVMContext::MD_fpmath
|
|
|
|
};
|
|
|
|
combineMetadata(K, H, KnownIDs);
|
2012-11-28 11:04:10 +08:00
|
|
|
K->intersectOptionalDataWith(H);
|
2012-06-17 04:34:06 +08:00
|
|
|
|
2012-02-01 11:51:43 +08:00
|
|
|
for (unsigned o = 0; o < NumOperands; ++o)
|
|
|
|
K->setOperand(o, ReplacedOperands[o]);
|
|
|
|
|
|
|
|
K->insertAfter(J);
|
|
|
|
|
|
|
|
// Instruction insertion point:
|
|
|
|
Instruction *InsertionPt = K;
|
2014-04-25 13:29:35 +08:00
|
|
|
Instruction *K1 = nullptr, *K2 = nullptr;
|
2012-10-31 03:35:29 +08:00
|
|
|
replaceOutputsOfPair(Context, L, H, K, InsertionPt, K1, K2);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
2013-02-16 01:20:54 +08:00
|
|
|
// The use dag of the first original instruction must be moved to after
|
|
|
|
// the location of the second instruction. The entire use dag of the
|
|
|
|
// first instruction is disjoint from the input dag of the second
|
2012-02-01 11:51:43 +08:00
|
|
|
// (by definition), and so commutes with it.
|
|
|
|
|
2013-02-11 13:29:49 +08:00
|
|
|
moveUsesOfIAfterJ(BB, LoadMoveSetPairs, InsertionPt, I, J);
|
2012-02-01 11:51:43 +08:00
|
|
|
|
|
|
|
if (!isa<StoreInst>(I)) {
|
2012-10-31 23:17:07 +08:00
|
|
|
L->replaceAllUsesWith(K1);
|
|
|
|
H->replaceAllUsesWith(K2);
|
|
|
|
AA->replaceWithNewValue(L, K1);
|
|
|
|
AA->replaceWithNewValue(H, K2);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instructions that may read from memory may be in the load move set.
|
|
|
|
// Once an instruction is fused, we no longer need its move set, and so
|
|
|
|
// the values of the map never need to be updated. However, when a load
|
|
|
|
// is fused, we need to merge the entries from both instructions in the
|
|
|
|
// pair in case those instructions were in the move set of some other
|
|
|
|
// yet-to-be-fused pair. The loads in question are the keys of the map.
|
|
|
|
if (I->mayReadFromMemory()) {
|
|
|
|
std::vector<ValuePair> NewSetMembers;
|
2013-02-15 06:38:04 +08:00
|
|
|
DenseMap<Value *, std::vector<Value *> >::iterator II =
|
|
|
|
LoadMoveSet.find(I);
|
|
|
|
if (II != LoadMoveSet.end())
|
|
|
|
for (std::vector<Value *>::iterator N = II->second.begin(),
|
|
|
|
NE = II->second.end(); N != NE; ++N)
|
|
|
|
NewSetMembers.push_back(ValuePair(K, *N));
|
|
|
|
DenseMap<Value *, std::vector<Value *> >::iterator JJ =
|
|
|
|
LoadMoveSet.find(J);
|
|
|
|
if (JJ != LoadMoveSet.end())
|
|
|
|
for (std::vector<Value *>::iterator N = JJ->second.begin(),
|
|
|
|
NE = JJ->second.end(); N != NE; ++N)
|
|
|
|
NewSetMembers.push_back(ValuePair(K, *N));
|
2012-02-01 11:51:43 +08:00
|
|
|
for (std::vector<ValuePair>::iterator A = NewSetMembers.begin(),
|
2013-02-11 13:29:49 +08:00
|
|
|
AE = NewSetMembers.end(); A != AE; ++A) {
|
2013-02-15 06:38:04 +08:00
|
|
|
LoadMoveSet[A->first].push_back(A->second);
|
2013-02-11 13:29:49 +08:00
|
|
|
LoadMoveSetPairs.insert(*A);
|
|
|
|
}
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Before removing I, set the iterator to the next instruction.
|
2014-03-02 20:27:27 +08:00
|
|
|
PI = std::next(BasicBlock::iterator(I));
|
2012-02-01 11:51:43 +08:00
|
|
|
if (cast<Instruction>(PI) == J)
|
|
|
|
++PI;
|
|
|
|
|
|
|
|
SE->forgetValue(I);
|
|
|
|
SE->forgetValue(J);
|
|
|
|
I->eraseFromParent();
|
|
|
|
J->eraseFromParent();
|
2012-10-31 23:17:07 +08:00
|
|
|
|
|
|
|
DEBUG(if (PrintAfterEveryPair) dbgs() << "BBV: block is now: \n" <<
|
|
|
|
BB << "\n");
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "BBV: final: \n" << BB << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char BBVectorize::ID = 0;
|
|
|
|
static const char bb_vectorize_name[] = "Basic-Block Vectorization";
|
|
|
|
INITIALIZE_PASS_BEGIN(BBVectorize, BBV_NAME, bb_vectorize_name, false, false)
|
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
[PM] Change the core design of the TTI analysis to use a polymorphic
type erased interface and a single analysis pass rather than an
extremely complex analysis group.
The end result is that the TTI analysis can contain a type erased
implementation that supports the polymorphic TTI interface. We can build
one from a target-specific implementation or from a dummy one in the IR.
I've also factored all of the code into "mix-in"-able base classes,
including CRTP base classes to facilitate calling back up to the most
specialized form when delegating horizontally across the surface. These
aren't as clean as I would like and I'm planning to work on cleaning
some of this up, but I wanted to start by putting into the right form.
There are a number of reasons for this change, and this particular
design. The first and foremost reason is that an analysis group is
complete overkill, and the chaining delegation strategy was so opaque,
confusing, and high overhead that TTI was suffering greatly for it.
Several of the TTI functions had failed to be implemented in all places
because of the chaining-based delegation making there be no checking of
this. A few other functions were implemented with incorrect delegation.
The message to me was very clear working on this -- the delegation and
analysis group structure was too confusing to be useful here.
The other reason of course is that this is *much* more natural fit for
the new pass manager. This will lay the ground work for a type-erased
per-function info object that can look up the correct subtarget and even
cache it.
Yet another benefit is that this will significantly simplify the
interaction of the pass managers and the TargetMachine. See the future
work below.
The downside of this change is that it is very, very verbose. I'm going
to work to improve that, but it is somewhat an implementation necessity
in C++ to do type erasure. =/ I discussed this design really extensively
with Eric and Hal prior to going down this path, and afterward showed
them the result. No one was really thrilled with it, but there doesn't
seem to be a substantially better alternative. Using a base class and
virtual method dispatch would make the code much shorter, but as
discussed in the update to the programmer's manual and elsewhere,
a polymorphic interface feels like the more principled approach even if
this is perhaps the least compelling example of it. ;]
Ultimately, there is still a lot more to be done here, but this was the
huge chunk that I couldn't really split things out of because this was
the interface change to TTI. I've tried to minimize all the other parts
of this. The follow up work should include at least:
1) Improving the TargetMachine interface by having it directly return
a TTI object. Because we have a non-pass object with value semantics
and an internal type erasure mechanism, we can narrow the interface
of the TargetMachine to *just* do what we need: build and return
a TTI object that we can then insert into the pass pipeline.
2) Make the TTI object be fully specialized for a particular function.
This will include splitting off a minimal form of it which is
sufficient for the inliner and the old pass manager.
3) Add a new pass manager analysis which produces TTI objects from the
target machine for each function. This may actually be done as part
of #2 in order to use the new analysis to implement #2.
4) Work on narrowing the API between TTI and the targets so that it is
easier to understand and less verbose to type erase.
5) Work on narrowing the API between TTI and its clients so that it is
easier to understand and less verbose to forward.
6) Try to improve the CRTP-based delegation. I feel like this code is
just a bit messy and exacerbating the complexity of implementing
the TTI in each target.
Many thanks to Eric and Hal for their help here. I ended up blocked on
this somewhat more abruptly than I expected, and so I appreciate getting
it sorted out very quickly.
Differential Revision: http://reviews.llvm.org/D7293
llvm-svn: 227669
2015-01-31 11:43:40 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
|
2014-01-13 21:07:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2012-02-01 11:51:43 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
|
|
|
INITIALIZE_PASS_END(BBVectorize, BBV_NAME, bb_vectorize_name, false, false)
|
|
|
|
|
2012-04-05 23:46:55 +08:00
|
|
|
BasicBlockPass *llvm::createBBVectorizePass(const VectorizeConfig &C) {
|
|
|
|
return new BBVectorize(C);
|
2012-02-01 11:51:43 +08:00
|
|
|
}
|
|
|
|
|
2012-04-05 23:46:55 +08:00
|
|
|
bool
|
|
|
|
llvm::vectorizeBasicBlock(Pass *P, BasicBlock &BB, const VectorizeConfig &C) {
|
2015-02-01 20:01:35 +08:00
|
|
|
BBVectorize BBVectorizer(P, *BB.getParent(), C);
|
2012-04-05 16:05:16 +08:00
|
|
|
return BBVectorizer.vectorizeBB(BB);
|
|
|
|
}
|
2012-04-05 23:46:55 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
VectorizeConfig::VectorizeConfig() {
|
|
|
|
VectorBits = ::VectorBits;
|
2012-06-24 21:28:01 +08:00
|
|
|
VectorizeBools = !::NoBools;
|
2012-04-07 11:56:23 +08:00
|
|
|
VectorizeInts = !::NoInts;
|
|
|
|
VectorizeFloats = !::NoFloats;
|
2012-04-14 15:32:43 +08:00
|
|
|
VectorizePointers = !::NoPointers;
|
2012-04-07 11:56:23 +08:00
|
|
|
VectorizeCasts = !::NoCasts;
|
|
|
|
VectorizeMath = !::NoMath;
|
2014-04-25 11:33:48 +08:00
|
|
|
VectorizeBitManipulations = !::NoBitManipulation;
|
2012-04-07 11:56:23 +08:00
|
|
|
VectorizeFMA = !::NoFMA;
|
2012-04-14 04:45:45 +08:00
|
|
|
VectorizeSelect = !::NoSelect;
|
2012-06-24 05:52:50 +08:00
|
|
|
VectorizeCmp = !::NoCmp;
|
2012-04-14 15:32:43 +08:00
|
|
|
VectorizeGEP = !::NoGEP;
|
2012-04-07 11:56:23 +08:00
|
|
|
VectorizeMemOps = !::NoMemOps;
|
2012-04-05 23:46:55 +08:00
|
|
|
AlignedOnly = ::AlignedOnly;
|
|
|
|
ReqChainDepth= ::ReqChainDepth;
|
|
|
|
SearchLimit = ::SearchLimit;
|
|
|
|
MaxCandPairsForCycleCheck = ::MaxCandPairsForCycleCheck;
|
|
|
|
SplatBreaksChain = ::SplatBreaksChain;
|
|
|
|
MaxInsts = ::MaxInsts;
|
BBVectorize: Cap the number of candidate pairs in each instruction group
For some basic blocks, it is possible to generate many candidate pairs for
relatively few pairable instructions. When many (tens of thousands) of these pairs
are generated for a single instruction group, the time taken to generate and
rank the different vectorization plans can become quite large. As a result, we now
cap the number of candidate pairs within each instruction group. This is done by
closing out the group once the threshold is reached (set now at 3000 pairs).
Although this will limit the overall compile-time impact, this may not be the best
way to achieve this result. It might be better, for example, to prune excessive
candidate pairs after the fact the prevent the generation of short, but highly-connected
groups. We can experiment with this in the future.
This change reduces the overall compile-time slowdown of the csa.ll test case in
PR15222 to ~5x. If 5x is still considered too large, a lower limit can be
used as the default.
This represents a functionality change, but only for very large inputs
(thus, there is no regression test).
llvm-svn: 175251
2013-02-15 12:28:42 +08:00
|
|
|
MaxPairs = ::MaxPairs;
|
2012-04-05 23:46:55 +08:00
|
|
|
MaxIter = ::MaxIter;
|
2012-06-28 13:42:42 +08:00
|
|
|
Pow2LenOnly = ::Pow2LenOnly;
|
2012-04-05 23:46:55 +08:00
|
|
|
NoMemOpBoost = ::NoMemOpBoost;
|
|
|
|
FastDep = ::FastDep;
|
|
|
|
}
|