Skip to content

Commit d1f19c5

Browse files
Dinar TemirbulatovDinar Temirbulatov
authored andcommitted
Resolved remarks.
1 parent 250942c commit d1f19c5

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

llvm/include/llvm/Analysis/IVDescriptors.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,24 @@ class RecurrenceDescriptor {
7676
RecurKind K, FastMathFlags FMF, Instruction *ExactFP,
7777
Type *RT, bool Signed, bool Ordered,
7878
SmallPtrSetImpl<Instruction *> &CI,
79-
unsigned MinWidthCastToRecurTy, Instruction *Cmp)
79+
unsigned MinWidthCastToRecurTy)
8080
: IntermediateStore(Store), StartValue(Start), LoopExitInstr(Exit),
8181
Kind(K), FMF(FMF), ExactFPMathInst(ExactFP), RecurrenceType(RT),
8282
IsSigned(Signed), IsOrdered(Ordered),
83-
MinWidthCastToRecurrenceType(MinWidthCastToRecurTy), MultiCmp(Cmp) {
83+
MinWidthCastToRecurrenceType(MinWidthCastToRecurTy) {
8484
CastInsts.insert(CI.begin(), CI.end());
8585
}
8686

8787
/// This POD struct holds information about a potential recurrence operation.
8888
class InstDesc {
8989
public:
9090
InstDesc(bool IsRecur, Instruction *I, Instruction *ExactFP = nullptr)
91-
: IsRecurrence(IsRecur), PatternLastInst(I), RecKind(RecurKind::None),
92-
ExactFPMathInst(ExactFP), Cmp(nullptr) {}
91+
: IsRecurrence(IsRecur), PatternLastInst(I),
92+
RecKind(RecurKind::None), ExactFPMathInst(ExactFP) {}
9393

94-
InstDesc(Instruction *I, RecurKind K, Instruction *ExactFP = nullptr,
95-
Instruction *MultiCmp = nullptr)
94+
InstDesc(Instruction *I, RecurKind K, Instruction *ExactFP = nullptr)
9695
: IsRecurrence(true), PatternLastInst(I), RecKind(K),
97-
ExactFPMathInst(ExactFP), Cmp(MultiCmp) {}
96+
ExactFPMathInst(ExactFP) {}
9897

9998
bool isRecurrence() const { return IsRecurrence; }
10099

@@ -106,8 +105,6 @@ class RecurrenceDescriptor {
106105

107106
Instruction *getPatternInst() const { return PatternLastInst; }
108107

109-
Instruction *getMultiCmp() const { return Cmp; }
110-
111108
private:
112109
// Is this instruction a recurrence candidate.
113110
bool IsRecurrence;
@@ -118,8 +115,6 @@ class RecurrenceDescriptor {
118115
RecurKind RecKind;
119116
// Recurrence does not allow floating-point reassociation.
120117
Instruction *ExactFPMathInst;
121-
// Mult-user compare instruction.
122-
Instruction *Cmp;
123118
};
124119

125120
/// Returns a struct describing if the instruction 'I' can be a recurrence
@@ -275,8 +270,6 @@ class RecurrenceDescriptor {
275270
cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::fmuladd;
276271
}
277272

278-
Instruction *getMultiCmp() const { return MultiCmp; }
279-
280273
/// Reductions may store temporary or final result to an invariant address.
281274
/// If there is such a store in the loop then, after successfull run of
282275
/// AddReductionVar method, this field will be assigned the last met store.
@@ -307,7 +300,6 @@ class RecurrenceDescriptor {
307300
SmallPtrSet<Instruction *, 8> CastInsts;
308301
// The minimum width used by the recurrence.
309302
unsigned MinWidthCastToRecurrenceType;
310-
Instruction *MultiCmp = nullptr;
311303
};
312304

313305
/// A struct for saving information about induction variables.

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ bool RecurrenceDescriptor::AddReductionVar(
256256
SmallPtrSet<Instruction *, 4> CastInsts;
257257
unsigned MinWidthCastToRecurrenceType;
258258
Instruction *Start = Phi;
259-
Instruction *MultiCMP = nullptr;
260259
bool IsSigned = false;
261260

262261
SmallPtrSet<Instruction *, 8> VisitedInsts;
@@ -401,8 +400,6 @@ bool RecurrenceDescriptor::AddReductionVar(
401400
}
402401

403402
bool IsASelect = isa<SelectInst>(Cur);
404-
if (IsASelect)
405-
MultiCMP = ReduxDesc.getMultiCmp();
406403

407404
// A conditional reduction operation must only have 2 or less uses in
408405
// VisitedInsts.
@@ -600,8 +597,7 @@ bool RecurrenceDescriptor::AddReductionVar(
600597
// Save the description of this reduction variable.
601598
RecurrenceDescriptor RD(RdxStart, ExitInstruction, IntermediateStore, Kind,
602599
FMF, ExactFPMathInst, RecurrenceType, IsSigned,
603-
IsOrdered, CastInsts, MinWidthCastToRecurrenceType,
604-
MultiCMP);
600+
IsOrdered, CastInsts, MinWidthCastToRecurrenceType);
605601
RedDes = RD;
606602

607603
return true;
@@ -639,11 +635,9 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
639635
return InstDesc(Select, Prev.getRecKind());
640636
}
641637

642-
// Find the compare instruction that is associated with OrigPhi, i.e
643-
// recurrent-reduction. And determine that SelectInst and CmpInst multiple
644-
// instructions usage are safe to vectorise.
645638
SelectInst *SI = dyn_cast<SelectInst>(I);
646639
Instruction *Cmp = nullptr;
640+
647641
if (SI) {
648642
bool HasOrigPhiUser = false;
649643
bool SelectNonPHIUserInLoop = false;
@@ -655,8 +649,6 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
655649
if (Inst == OrigPhi) {
656650
HasOrigPhiUser = true;
657651
} else {
658-
// If we found SelectInstr usage in the loop then the reduction stops
659-
// to be recurrent and it is not safe to procede further.
660652
if (std::find(Blocks.begin(), Blocks.end(), Inst->getParent()) !=
661653
Blocks.end())
662654
SelectNonPHIUserInLoop = true;
@@ -687,8 +679,6 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
687679
}
688680
if (!IsSafeCMP)
689681
Cmp = nullptr;
690-
} else {
691-
Cmp = nullptr;
692682
}
693683
}
694684

@@ -711,10 +701,8 @@ RecurrenceDescriptor::isAnyOfPattern(Loop *Loop, PHINode *OrigPhi,
711701
if (!Loop->isLoopInvariant(NonPhi))
712702
return InstDesc(false, I);
713703

714-
return InstDesc(I,
715-
isa<ICmpInst>(I->getOperand(0)) ? RecurKind::IAnyOf
716-
: RecurKind::FAnyOf,
717-
nullptr, Cmp);
704+
return InstDesc(I, isa<ICmpInst>(I->getOperand(0)) ? RecurKind::IAnyOf
705+
: RecurKind::FAnyOf);
718706
}
719707

720708
RecurrenceDescriptor::InstDesc

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,16 @@ bool LoopVectorizationLegality::canVectorizeInstrs() {
831831
Requirements->addExactFPMathInst(RedDes.getExactFPMathInst());
832832
AllowedExit.insert(RedDes.getLoopExitInstr());
833833
Reductions[Phi] = RedDes;
834-
Instruction *Cmp = RedDes.getMultiCmp();
835-
if (Cmp) {
834+
CmpInst *Cmp = nullptr;
835+
for (Value *V :
836+
{Phi->getIncomingValue(0), Phi->getIncomingValue(1)}) {
837+
if (Instruction *SI = dyn_cast<SelectInst>(V))
838+
Cmp = dyn_cast<CmpInst>(SI->getOperand(0));
839+
}
840+
if (Cmp && !Cmp->hasOneUse()) {
841+
RecurKind Kind = RedDes.getRecurrenceKind();
842+
assert((Kind == RecurKind::IAnyOf || Kind == RecurKind::FAnyOf) &&
843+
"Unexpected type of recurrence");
836844
if (MultiCmpsRed.contains(Cmp))
837845
MultiCmpsRed[Cmp]++;
838846
else
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt < %s -passes=loop-vectorize -debug-only=loop-vectorize -S 2>&1 | FileCheck %s
2+
3+
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
4+
target triple = "aarch64-unknown-linux-gnu"
5+
6+
define i32 @multi_user_cmp(ptr readonly %a, i32 noundef %n) {
7+
; CHECK: LV: Found an estimated cost of 0 for VF 4 For instruction: %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
8+
; CHECK-NEXT: LV: Found an estimated cost of 0 for VF 4 For instruction: %all.0.off010 = phi i1 [ true, %entry ], [ %all.0.off0., %for.body ]
9+
; CHECK-NEXT: LV: Found an estimated cost of 0 for VF 4 For instruction: %any.0.off09 = phi i1 [ false, %entry ], [ %.any.0.off0, %for.body ]
10+
; CHECK-NEXT: LV: Found an estimated cost of 0 for VF 4 For instruction: %arrayidx = getelementptr inbounds float, ptr %a, i64 %indvars.iv
11+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %load1 = load float, ptr %arrayidx, align 4
12+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %cmp1 = fcmp olt float %load1, 0.000000e+00
13+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %.any.0.off0 = select i1 %cmp1, i1 true, i1 %any.0.off09
14+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %all.0.off0. = select i1 %cmp1, i1 %all.0.off010, i1 false
15+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
16+
; CHECK-NEXT: LV: Found an estimated cost of 1 for VF 4 For instruction: %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
17+
; CHECK-NEXT: LV: Found an estimated cost of 0 for VF 4 For instruction: br i1 %exitcond.not, label %exit, label %for.body
18+
entry:
19+
%wide.trip.count = zext nneg i32 %n to i64
20+
br label %for.body
21+
22+
for.body:
23+
%indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
24+
%all.0.off010 = phi i1 [ true, %entry ], [ %all.0.off0., %for.body ]
25+
%any.0.off09 = phi i1 [ false, %entry ], [ %.any.0.off0, %for.body ]
26+
%arrayidx = getelementptr inbounds float, ptr %a, i64 %indvars.iv
27+
%load1 = load float, ptr %arrayidx, align 4
28+
%cmp1 = fcmp olt float %load1, 0.000000e+00
29+
%.any.0.off0 = select i1 %cmp1, i1 true, i1 %any.0.off09
30+
%all.0.off0. = select i1 %cmp1, i1 %all.0.off010, i1 false
31+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
32+
%exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
33+
br i1 %exitcond.not, label %exit, label %for.body
34+
35+
exit:
36+
%0 = select i1 %.any.0.off0, i32 2, i32 3
37+
%1 = select i1 %all.0.off0., i32 1, i32 %0
38+
ret i32 %1
39+
}

0 commit comments

Comments
 (0)