[llvm] [SLP] NFC. Refactor getSameOpcode and reduce for loop iterations. (PR #122241)
Han-Kuan Chen via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 9 06:41:24 PST 2025
https://github.com/HanKuanChen updated https://github.com/llvm/llvm-project/pull/122241
>From 2a78d8bb9f9c5ce23088223bbf9330c061c824a1 Mon Sep 17 00:00:00 2001
From: Han-Kuan Chen <hankuan.chen at sifive.com>
Date: Wed, 8 Jan 2025 05:36:42 -0800
Subject: [PATCH 1/5] rename IBase to MainOp
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index c4582df89213d8..1a062a61761e09 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -916,6 +916,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
return InstructionsState::invalid();
Value *V = *It;
+ Instruction *MainOp = cast<Instruction>(V);
unsigned InstCnt = std::count_if(It, VL.end(), IsaPred<Instruction>);
if ((VL.size() > 2 && !isa<PHINode>(V) && InstCnt < VL.size() / 2) ||
(VL.size() == 2 && InstCnt < 2))
@@ -955,10 +956,9 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
}();
// Check for one alternate opcode from another BinaryOperator.
// TODO - generalize to support all operators (types, calls etc.).
- auto *IBase = cast<Instruction>(V);
Intrinsic::ID BaseID = 0;
SmallVector<VFInfo> BaseMappings;
- if (auto *CallBase = dyn_cast<CallInst>(IBase)) {
+ if (auto *CallBase = dyn_cast<CallInst>(MainOp)) {
BaseID = getVectorIntrinsicIDForCall(CallBase, &TLI);
BaseMappings = VFDatabase(*CallBase).getMappings(*CallBase);
if (!isTriviallyVectorizable(BaseID) && BaseMappings.empty())
@@ -986,7 +986,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
continue;
}
} else if (IsCastOp && isa<CastInst>(I)) {
- Value *Op0 = IBase->getOperand(0);
+ Value *Op0 = MainOp->getOperand(0);
Type *Ty0 = Op0->getType();
Value *Op1 = I->getOperand(0);
Type *Ty1 = Op1->getType();
@@ -1045,17 +1045,17 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
"CastInst.");
if (auto *Gep = dyn_cast<GetElementPtrInst>(I)) {
if (Gep->getNumOperands() != 2 ||
- Gep->getOperand(0)->getType() != IBase->getOperand(0)->getType())
+ Gep->getOperand(0)->getType() != MainOp->getOperand(0)->getType())
return InstructionsState::invalid();
} else if (auto *EI = dyn_cast<ExtractElementInst>(I)) {
if (!isVectorLikeInstWithConstOps(EI))
return InstructionsState::invalid();
} else if (auto *LI = dyn_cast<LoadInst>(I)) {
- auto *BaseLI = cast<LoadInst>(IBase);
+ auto *BaseLI = cast<LoadInst>(MainOp);
if (!LI->isSimple() || !BaseLI->isSimple())
return InstructionsState::invalid();
} else if (auto *Call = dyn_cast<CallInst>(I)) {
- auto *CallBase = cast<CallInst>(IBase);
+ auto *CallBase = cast<CallInst>(MainOp);
if (Call->getCalledFunction() != CallBase->getCalledFunction())
return InstructionsState::invalid();
if (Call->hasOperandBundles() &&
>From 13628159e420214125edd0c51790a47c6b6d19e3 Mon Sep 17 00:00:00 2001
From: Han-Kuan Chen <hankuan.chen at sifive.com>
Date: Wed, 8 Jan 2025 05:37:56 -0800
Subject: [PATCH 2/5] rename V to MainOp
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 22 +++++++++----------
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 1a062a61761e09..ddd155af160574 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -915,19 +915,18 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
if (It == VL.end())
return InstructionsState::invalid();
- Value *V = *It;
- Instruction *MainOp = cast<Instruction>(V);
+ Instruction *MainOp = cast<Instruction>(*It);
unsigned InstCnt = std::count_if(It, VL.end(), IsaPred<Instruction>);
- if ((VL.size() > 2 && !isa<PHINode>(V) && InstCnt < VL.size() / 2) ||
+ if ((VL.size() > 2 && !isa<PHINode>(MainOp) && InstCnt < VL.size() / 2) ||
(VL.size() == 2 && InstCnt < 2))
return InstructionsState::invalid();
- bool IsCastOp = isa<CastInst>(V);
- bool IsBinOp = isa<BinaryOperator>(V);
- bool IsCmpOp = isa<CmpInst>(V);
- CmpInst::Predicate BasePred =
- IsCmpOp ? cast<CmpInst>(V)->getPredicate() : CmpInst::BAD_ICMP_PREDICATE;
- unsigned Opcode = cast<Instruction>(V)->getOpcode();
+ bool IsCastOp = isa<CastInst>(MainOp);
+ bool IsBinOp = isa<BinaryOperator>(MainOp);
+ bool IsCmpOp = isa<CmpInst>(MainOp);
+ CmpInst::Predicate BasePred = IsCmpOp ? cast<CmpInst>(MainOp)->getPredicate()
+ : CmpInst::BAD_ICMP_PREDICATE;
+ unsigned Opcode = MainOp->getOpcode();
unsigned AltOpcode = Opcode;
unsigned AltIndex = std::distance(VL.begin(), It);
@@ -1003,7 +1002,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
}
}
} else if (auto *Inst = dyn_cast<CmpInst>(VL[Cnt]); Inst && IsCmpOp) {
- auto *BaseInst = cast<CmpInst>(V);
+ auto *BaseInst = cast<CmpInst>(MainOp);
Type *Ty0 = BaseInst->getOperand(0)->getType();
Type *Ty1 = Inst->getOperand(0)->getType();
if (Ty0 == Ty1) {
@@ -1085,8 +1084,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
return InstructionsState::invalid();
}
- return InstructionsState(cast<Instruction>(V),
- cast<Instruction>(VL[AltIndex]));
+ return InstructionsState(MainOp, cast<Instruction>(VL[AltIndex]));
}
/// \returns true if all of the values in \p VL have the same type or false
>From de4aee23bbb97f53ea1b8fcc8846f7beaf78f645 Mon Sep 17 00:00:00 2001
From: Han-Kuan Chen <hankuan.chen at sifive.com>
Date: Wed, 8 Jan 2025 05:50:53 -0800
Subject: [PATCH 3/5] move IsCmpOp out of lambda
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index ddd155af160574..041b11868e987c 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -930,9 +930,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
unsigned AltOpcode = Opcode;
unsigned AltIndex = std::distance(VL.begin(), It);
- bool SwappedPredsCompatible = [&]() {
- if (!IsCmpOp)
- return false;
+ bool SwappedPredsCompatible = IsCmpOp && [&]() {
SetVector<unsigned> UniquePreds, UniqueNonSwappedPreds;
UniquePreds.insert(BasePred);
UniqueNonSwappedPreds.insert(BasePred);
>From caacc2e01b00e7d7714e960892696225fb9e7bbb Mon Sep 17 00:00:00 2001
From: Han-Kuan Chen <hankuan.chen at sifive.com>
Date: Thu, 9 Jan 2025 01:33:00 -0800
Subject: [PATCH 4/5] replace AltIndex with AltOp
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 041b11868e987c..b9999055bf011a 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -926,9 +926,9 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
bool IsCmpOp = isa<CmpInst>(MainOp);
CmpInst::Predicate BasePred = IsCmpOp ? cast<CmpInst>(MainOp)->getPredicate()
: CmpInst::BAD_ICMP_PREDICATE;
+ Instruction *AltOp = MainOp;
unsigned Opcode = MainOp->getOpcode();
unsigned AltOpcode = Opcode;
- unsigned AltIndex = std::distance(VL.begin(), It);
bool SwappedPredsCompatible = IsCmpOp && [&]() {
SetVector<unsigned> UniquePreds, UniqueNonSwappedPreds;
@@ -979,7 +979,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
if (Opcode == AltOpcode && isValidForAlternation(InstOpcode) &&
isValidForAlternation(Opcode)) {
AltOpcode = InstOpcode;
- AltIndex = Cnt;
+ AltOp = I;
continue;
}
} else if (IsCastOp && isa<CastInst>(I)) {
@@ -995,7 +995,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
isValidForAlternation(InstOpcode) &&
"Cast isn't safe for alternation, logic needs to be updated!");
AltOpcode = InstOpcode;
- AltIndex = Cnt;
+ AltOp = I;
continue;
}
}
@@ -1020,15 +1020,15 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
if (isCmpSameOrSwapped(BaseInst, Inst, TLI))
continue;
- auto *AltInst = cast<CmpInst>(VL[AltIndex]);
- if (AltIndex) {
+ auto *AltInst = cast<CmpInst>(AltOp);
+ if (MainOp != AltOp) {
if (isCmpSameOrSwapped(AltInst, Inst, TLI))
continue;
} else if (BasePred != CurrentPred) {
assert(
isValidForAlternation(InstOpcode) &&
"CmpInst isn't safe for alternation, logic needs to be updated!");
- AltIndex = Cnt;
+ AltOp = I;
continue;
}
CmpInst::Predicate AltPred = AltInst->getPredicate();
@@ -1082,7 +1082,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
return InstructionsState::invalid();
}
- return InstructionsState(MainOp, cast<Instruction>(VL[AltIndex]));
+ return InstructionsState(MainOp, AltOp);
}
/// \returns true if all of the values in \p VL have the same type or false
>From 565384cd07a76077d676e5fc13d0faece2e5b68c Mon Sep 17 00:00:00 2001
From: Han-Kuan Chen <hankuan.chen at sifive.com>
Date: Wed, 8 Jan 2025 06:10:38 -0800
Subject: [PATCH 5/5] skip the beginning PoisonValue
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index b9999055bf011a..97780cf8dbbbf8 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -962,8 +962,9 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
return InstructionsState::invalid();
}
bool AnyPoison = InstCnt != VL.size();
- for (int Cnt = 0, E = VL.size(); Cnt < E; Cnt++) {
- auto *I = dyn_cast<Instruction>(VL[Cnt]);
+ // Skip MainOp.
+ while (++It != VL.end()) {
+ auto *I = dyn_cast<Instruction>(*It);
if (!I)
continue;
@@ -999,7 +1000,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
continue;
}
}
- } else if (auto *Inst = dyn_cast<CmpInst>(VL[Cnt]); Inst && IsCmpOp) {
+ } else if (auto *Inst = dyn_cast<CmpInst>(I); Inst && IsCmpOp) {
auto *BaseInst = cast<CmpInst>(MainOp);
Type *Ty0 = BaseInst->getOperand(0)->getType();
Type *Ty1 = Inst->getOperand(0)->getType();
@@ -1014,7 +1015,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
CmpInst::Predicate SwappedCurrentPred =
CmpInst::getSwappedPredicate(CurrentPred);
- if ((E == 2 || SwappedPredsCompatible) &&
+ if ((VL.size() == 2 || SwappedPredsCompatible) &&
(BasePred == CurrentPred || BasePred == SwappedCurrentPred))
continue;
More information about the llvm-commits
mailing list