@@ -926,9 +926,9 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
926926 bool IsCmpOp = isa<CmpInst>(MainOp);
927927 CmpInst::Predicate BasePred = IsCmpOp ? cast<CmpInst>(MainOp)->getPredicate()
928928 : CmpInst::BAD_ICMP_PREDICATE;
929+ Instruction *AltOp = MainOp;
929930 unsigned Opcode = MainOp->getOpcode();
930931 unsigned AltOpcode = Opcode;
931- unsigned AltIndex = std::distance(VL.begin(), It);
932932
933933 bool SwappedPredsCompatible = IsCmpOp && [&]() {
934934 SetVector<unsigned> UniquePreds, UniqueNonSwappedPreds;
@@ -979,7 +979,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
979979 if (Opcode == AltOpcode && isValidForAlternation(InstOpcode) &&
980980 isValidForAlternation(Opcode)) {
981981 AltOpcode = InstOpcode;
982- AltIndex = Cnt ;
982+ AltOp = I ;
983983 continue;
984984 }
985985 } else if (IsCastOp && isa<CastInst>(I)) {
@@ -995,7 +995,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
995995 isValidForAlternation(InstOpcode) &&
996996 "Cast isn't safe for alternation, logic needs to be updated!");
997997 AltOpcode = InstOpcode;
998- AltIndex = Cnt ;
998+ AltOp = I ;
999999 continue;
10001000 }
10011001 }
@@ -1020,18 +1020,17 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
10201020
10211021 if (isCmpSameOrSwapped(BaseInst, Inst, TLI))
10221022 continue;
1023- auto *AltInst = cast<CmpInst>(VL[AltIndex]);
1024- if (AltIndex) {
1025- if (isCmpSameOrSwapped(AltInst, Inst, TLI))
1023+ if (MainOp != AltOp) {
1024+ if (isCmpSameOrSwapped(cast<CmpInst>(AltOp), Inst, TLI))
10261025 continue;
10271026 } else if (BasePred != CurrentPred) {
10281027 assert(
10291028 isValidForAlternation(InstOpcode) &&
10301029 "CmpInst isn't safe for alternation, logic needs to be updated!");
1031- AltIndex = Cnt ;
1030+ AltOp = I ;
10321031 continue;
10331032 }
1034- CmpInst::Predicate AltPred = AltInst ->getPredicate();
1033+ CmpInst::Predicate AltPred = cast<CmpInst>(AltOp) ->getPredicate();
10351034 if (BasePred == CurrentPred || BasePred == SwappedCurrentPred ||
10361035 AltPred == CurrentPred || AltPred == SwappedCurrentPred)
10371036 continue;
@@ -1082,7 +1081,7 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
10821081 return InstructionsState::invalid();
10831082 }
10841083
1085- return InstructionsState(MainOp, cast<Instruction>(VL[AltIndex] ));
1084+ return InstructionsState(MainOp, cast<Instruction>(AltOp ));
10861085}
10871086
10881087/// \returns true if all of the values in \p VL have the same type or false
0 commit comments