From e380208d4f59bda2794a09f78fa0face3c6daa76 Mon Sep 17 00:00:00 2001 From: Nadav Rotem Date: Sun, 14 Apr 2013 05:15:53 +0000 Subject: [PATCH] SLPVectorizer: Add support for trees that don't start at binary operators, and add the cost of extracting values from the roots of the tree. llvm-svn: 179475 --- lib/Transforms/Vectorize/SLPVectorizer.cpp | 15 +++++--- lib/Transforms/Vectorize/VecUtils.cpp | 10 +++++ lib/Transforms/Vectorize/VecUtils.h | 7 +++- .../SLPVectorizer/X86/reduction2.ll | 37 +++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 test/Transforms/SLPVectorizer/X86/reduction2.ll diff --git a/lib/Transforms/Vectorize/SLPVectorizer.cpp b/lib/Transforms/Vectorize/SLPVectorizer.cpp index 2f55a007f24..d94b2b2a0e3 100644 --- a/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -85,14 +85,16 @@ struct SLPVectorizer : public BasicBlockPass { return true; } - bool tryToVectorizePair(BinaryOperator *A, BinaryOperator *B, BoUpSLP &R) { + bool tryToVectorizePair(Value *A, Value *B, BoUpSLP &R) { if (!A || !B) return false; BoUpSLP::ValueList VL; VL.push_back(A); VL.push_back(B); int Cost = R.getTreeCost(VL); - DEBUG(dbgs()<<"SLP: Cost of pair:" << Cost << ".\n"); - if (Cost >= -SLPCostThreshold) return false; + int ExtrCost = R.getScalarizationCost(VL); + DEBUG(dbgs()<<"SLP: Cost of pair:" << Cost << + " Cost of extract:" << ExtrCost << ".\n"); + if ((Cost+ExtrCost) >= -SLPCostThreshold) return false; DEBUG(dbgs()<<"SLP: Vectorizing pair.\n"); R.vectorizeArith(VL); return true; @@ -100,11 +102,12 @@ struct SLPVectorizer : public BasicBlockPass { bool tryToVectorizeCandidate(BinaryOperator *V, BoUpSLP &R) { if (!V) return false; + // Try to vectorize V. + if (tryToVectorizePair(V->getOperand(0), V->getOperand(1), R)) + return true; + BinaryOperator *A = dyn_cast(V->getOperand(0)); BinaryOperator *B = dyn_cast(V->getOperand(1)); - // Try to vectorize V. - if (tryToVectorizePair(A, B, R)) return true; - // Try to skip B. if (B && B->hasOneUse()) { BinaryOperator *B0 = dyn_cast(B->getOperand(0)); diff --git a/lib/Transforms/Vectorize/VecUtils.cpp b/lib/Transforms/Vectorize/VecUtils.cpp index 4d075c505d2..584f3d97788 100644 --- a/lib/Transforms/Vectorize/VecUtils.cpp +++ b/lib/Transforms/Vectorize/VecUtils.cpp @@ -173,6 +173,16 @@ bool BoUpSLP::vectorizeStores(StoreList &Stores, int costThreshold) { return Changed; } +int BoUpSLP::getScalarizationCost(ValueList &VL) { + Type *ScalarTy = VL[0]->getType(); + + if (StoreInst *SI = dyn_cast(VL[0])) + ScalarTy = SI->getValueOperand()->getType(); + + VectorType *VecTy = VectorType::get(ScalarTy, VL.size()); + return getScalarizationCost(VecTy); +} + int BoUpSLP::getScalarizationCost(Type *Ty) { int Cost = 0; for (unsigned i = 0, e = cast(Ty)->getNumElements(); i < e; ++i) diff --git a/lib/Transforms/Vectorize/VecUtils.h b/lib/Transforms/Vectorize/VecUtils.h index f865236ff88..edebcb3e275 100644 --- a/lib/Transforms/Vectorize/VecUtils.h +++ b/lib/Transforms/Vectorize/VecUtils.h @@ -61,6 +61,11 @@ struct BoUpSLP { /// A negative number means that this is profitable. int getTreeCost(ValueList &VL); + /// \returns the scalarization cost for this ValueList. Assuming that this + /// subtree gets vectorized, we may need to extract the values from the + /// roots. This method calculates the cost of extracting the values. + int getScalarizationCost(ValueList &VL); + /// \brief Attempts to order and vectorize a sequence of stores. This /// function does a quadratic scan of the given stores. /// \returns true if the basic block was modified. @@ -118,7 +123,7 @@ private: /// by multiple lanes, or by users outside the tree. /// NOTICE: The vectorization methods also use this set. ValueSet MustScalarize; - + // Contains a list of values that are used outside the current tree. This // set must be reset between runs. ValueSet MultiUserVals; diff --git a/test/Transforms/SLPVectorizer/X86/reduction2.ll b/test/Transforms/SLPVectorizer/X86/reduction2.ll new file mode 100644 index 00000000000..9b5d5f701df --- /dev/null +++ b/test/Transforms/SLPVectorizer/X86/reduction2.ll @@ -0,0 +1,37 @@ +; RUN: opt < %s -basicaa -slp-vectorizer -dce -S -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx | FileCheck %s + +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32-S128" +target triple = "i386-apple-macosx10.8.0" + +;CHECK: @foo +;CHECK: load <2 x double> +;CHECK: ret +define double @foo(double* nocapture %D) #0 { + br label %1 + +;