Skip to content

Commit 1d2ad83

Browse files
committed
SLPVectorizer: Improve the cost model for loop invariant broadcast values.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179930 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ef332b1 commit 1d2ad83

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct SLPVectorizer : public FunctionPass {
8383

8484
// Use the bollom up slp vectorizer to construct chains that start with
8585
// he store instructions.
86-
BoUpSLP R(BB, SE, DL, TTI, AA);
86+
BoUpSLP R(BB, SE, DL, TTI, AA, LI->getLoopFor(BB));
8787

8888
// Vectorize trees that end at reductions.
8989
BBChanged |= vectorizeReductions(BB, R);

lib/Transforms/Vectorize/VecUtils.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
1919
#include "llvm/Analysis/TargetTransformInfo.h"
2020
#include "llvm/Analysis/Verifier.h"
21+
#include "llvm/Analysis/LoopInfo.h"
2122
#include "llvm/IR/Constants.h"
2223
#include "llvm/IR/DataLayout.h"
2324
#include "llvm/IR/Function.h"
@@ -44,8 +45,8 @@ static const unsigned RecursionMaxDepth = 6;
4445
namespace llvm {
4546

4647
BoUpSLP::BoUpSLP(BasicBlock *Bb, ScalarEvolution *S, DataLayout *Dl,
47-
TargetTransformInfo *Tti, AliasAnalysis *Aa) :
48-
BB(Bb), SE(S), DL(Dl), TTI(Tti), AA(Aa) {
48+
TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp) :
49+
BB(Bb), SE(S), DL(Dl), TTI(Tti), AA(Aa), L(Lp) {
4950
numberInstructions();
5051
}
5152

@@ -121,7 +122,7 @@ bool BoUpSLP::vectorizeStoreChain(ValueList &Chain, int CostThreshold) {
121122
if (Cost < CostThreshold) {
122123
DEBUG(dbgs() << "SLP: Decided to vectorize cost=" << Cost << "\n");
123124
vectorizeTree(Operands, VF);
124-
i += VF;
125+
i += VF - 1;
125126
Changed = true;
126127
}
127128
}
@@ -381,25 +382,39 @@ int BoUpSLP::getTreeCost_rec(ValueList &VL, unsigned Depth) {
381382
// Check if all of the operands are constants.
382383
bool AllConst = true;
383384
bool AllSameScalar = true;
385+
bool MustScalarizeFlag = false;
384386
for (unsigned i = 0, e = VL.size(); i < e; ++i) {
385387
AllConst &= isa<Constant>(VL[i]);
386388
AllSameScalar &= (VL[0] == VL[i]);
387389
// Must have a single use.
388390
Instruction *I = dyn_cast<Instruction>(VL[i]);
389-
// This instruction is outside the basic block or if it is a known hazard.
390-
if (MustScalarize.count(VL[i]) || (I && I->getParent() != BB))
391+
MustScalarizeFlag |= MustScalarize.count(VL[i]);
392+
// This instruction is outside the basic block.
393+
if (I && I->getParent() != BB)
391394
return getScalarizationCost(VecTy);
392395
}
393396

394397
// Is this a simple vector constant.
395398
if (AllConst) return 0;
396399

397400
// If all of the operands are identical we can broadcast them.
398-
if (AllSameScalar)
401+
Instruction *VL0 = dyn_cast<Instruction>(VL[0]);
402+
if (AllSameScalar) {
403+
// If we are in a loop, and this is not an instruction (e.g. constant or
404+
// argument) or the instruction is defined outside the loop then assume
405+
// that the cost is zero.
406+
if (L && (!VL0 || !L->contains(VL0)))
407+
return 0;
408+
409+
// We need to broadcast the scalar.
399410
return TTI->getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy, 0);
411+
}
412+
413+
// If this is not a constant, or a scalar from outside the loop then we
414+
// need to scalarize it.
415+
if (MustScalarizeFlag)
416+
return getScalarizationCost(VecTy);
400417

401-
// Scalarize unknown structures.
402-
Instruction *VL0 = dyn_cast<Instruction>(VL[0]);
403418
if (!VL0) return getScalarizationCost(VecTy);
404419
assert(VL0->getParent() == BB && "Wrong BB");
405420

lib/Transforms/Vectorize/VecUtils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BasicBlock; class Instruction; class Type;
2727
class VectorType; class StoreInst; class Value;
2828
class ScalarEvolution; class DataLayout;
2929
class TargetTransformInfo; class AliasAnalysis;
30+
class Loop;
3031

3132
/// Bottom Up SLP vectorization utility class.
3233
struct BoUpSLP {
@@ -37,7 +38,7 @@ struct BoUpSLP {
3738

3839
// \brief C'tor.
3940
BoUpSLP(BasicBlock *Bb, ScalarEvolution *Se, DataLayout *Dl,
40-
TargetTransformInfo *Tti, AliasAnalysis *Aa);
41+
TargetTransformInfo *Tti, AliasAnalysis *Aa, Loop *Lp);
4142

4243
/// \brief Take the pointer operand from the Load/Store instruction.
4344
/// \returns NULL if this is not a valid Load/Store instruction.
@@ -112,7 +113,7 @@ struct BoUpSLP {
112113

113114
/// \returns a vector from a collection of scalars in \p VL.
114115
Value *Scalarize(ValueList &VL, VectorType *Ty);
115-
116+
116117
private:
117118
/// Maps instructions to numbers and back.
118119
SmallDenseMap<Value*, int> InstrIdx;
@@ -155,6 +156,7 @@ struct BoUpSLP {
155156
DataLayout *DL;
156157
TargetTransformInfo *TTI;
157158
AliasAnalysis *AA;
159+
Loop *L;
158160
};
159161

160162
} // end of namespace
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; RUN: opt < %s -basicaa -slp-vectorizer -dce -S -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7-avx | FileCheck %s
2+
3+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
4+
target triple = "x86_64-apple-macosx10.8.0"
5+
6+
;CHECK: @foo
7+
;CHECK: load <4 x i32>
8+
;CHECK: add <4 x i32>
9+
;CHECK: store <4 x i32>
10+
;CHECK: load <4 x i32>
11+
;CHECK: add <4 x i32>
12+
;CHECK: store <4 x i32>
13+
;CHECK: ret
14+
define i32 @foo(i32* nocapture %A, i32 %n) #0 {
15+
entry:
16+
%cmp62 = icmp sgt i32 %n, 0
17+
br i1 %cmp62, label %for.body, label %for.end
18+
19+
for.body: ; preds = %entry, %for.body
20+
%indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
21+
%arrayidx = getelementptr inbounds i32* %A, i64 %indvars.iv
22+
%0 = load i32* %arrayidx, align 4, !tbaa !0
23+
%add1 = add nsw i32 %0, %n
24+
store i32 %add1, i32* %arrayidx, align 4, !tbaa !0
25+
%1 = or i64 %indvars.iv, 1
26+
%arrayidx4 = getelementptr inbounds i32* %A, i64 %1
27+
%2 = load i32* %arrayidx4, align 4, !tbaa !0
28+
%add5 = add nsw i32 %2, %n
29+
store i32 %add5, i32* %arrayidx4, align 4, !tbaa !0
30+
%3 = or i64 %indvars.iv, 2
31+
%arrayidx8 = getelementptr inbounds i32* %A, i64 %3
32+
%4 = load i32* %arrayidx8, align 4, !tbaa !0
33+
%add9 = add nsw i32 %4, %n
34+
store i32 %add9, i32* %arrayidx8, align 4, !tbaa !0
35+
%5 = or i64 %indvars.iv, 3
36+
%arrayidx12 = getelementptr inbounds i32* %A, i64 %5
37+
%6 = load i32* %arrayidx12, align 4, !tbaa !0
38+
%add13 = add nsw i32 %6, %n
39+
store i32 %add13, i32* %arrayidx12, align 4, !tbaa !0
40+
%7 = or i64 %indvars.iv, 4
41+
%arrayidx16 = getelementptr inbounds i32* %A, i64 %7
42+
%8 = load i32* %arrayidx16, align 4, !tbaa !0
43+
%add17 = add nsw i32 %8, %n
44+
store i32 %add17, i32* %arrayidx16, align 4, !tbaa !0
45+
%9 = or i64 %indvars.iv, 5
46+
%arrayidx20 = getelementptr inbounds i32* %A, i64 %9
47+
%10 = load i32* %arrayidx20, align 4, !tbaa !0
48+
%add21 = add nsw i32 %10, %n
49+
store i32 %add21, i32* %arrayidx20, align 4, !tbaa !0
50+
%11 = or i64 %indvars.iv, 6
51+
%arrayidx24 = getelementptr inbounds i32* %A, i64 %11
52+
%12 = load i32* %arrayidx24, align 4, !tbaa !0
53+
%add25 = add nsw i32 %12, %n
54+
store i32 %add25, i32* %arrayidx24, align 4, !tbaa !0
55+
%13 = or i64 %indvars.iv, 7
56+
%arrayidx28 = getelementptr inbounds i32* %A, i64 %13
57+
%14 = load i32* %arrayidx28, align 4, !tbaa !0
58+
%add29 = add nsw i32 %14, %n
59+
store i32 %add29, i32* %arrayidx28, align 4, !tbaa !0
60+
%indvars.iv.next = add i64 %indvars.iv, 8
61+
%15 = trunc i64 %indvars.iv.next to i32
62+
%cmp = icmp slt i32 %15, %n
63+
br i1 %cmp, label %for.body, label %for.end
64+
65+
for.end: ; preds = %for.body, %entry
66+
ret i32 undef
67+
}
68+
69+
attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
70+
71+
!0 = metadata !{metadata !"int", metadata !1}
72+
!1 = metadata !{metadata !"omnipotent char", metadata !2}
73+
!2 = metadata !{metadata !"Simple C/C++ TBAA"}

0 commit comments

Comments
 (0)