@@ -498,10 +498,9 @@ static Value *getTrueOrFalseValue(
498
498
499
499
unsigned OtherIdx = 1 - CondIdx;
500
500
if (auto *IV = dyn_cast<Instruction>(CBO->getOperand (OtherIdx))) {
501
- if (OptSelects.count (IV)) {
501
+ if (OptSelects.count (IV))
502
502
CBO->setOperand (OtherIdx,
503
503
isTrue ? OptSelects[IV].first : OptSelects[IV].second );
504
- }
505
504
}
506
505
CBO->insertBefore (B->getTerminator ());
507
506
return CBO;
@@ -763,84 +762,88 @@ void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
763
762
764
763
std::map<Value *, SelectLikeInfo> SelectInfo;
765
764
766
- auto ProcessSelectInfo = [&SelectInfo](Instruction *I) -> void {
765
+ // Check if the instruction is SelectLike or might be part of SelectLike
766
+ // expression, put information into SelectInfo and return the iterator to the
767
+ // inserted position.
768
+ auto ProcessSelectInfo = [&SelectInfo](Instruction *I) {
767
769
Value *Cond;
768
770
if (match (I, m_OneUse (m_ZExt (m_Value (Cond)))) &&
769
771
Cond->getType ()->isIntegerTy (1 )) {
770
772
bool Inverted = match (Cond, m_Not (m_Value (Cond)));
771
- SelectInfo[I] = {Cond, true , Inverted, 0 };
772
- return ;
773
+ return SelectInfo.insert ({I, {Cond, true , Inverted, 0 }}).first ;
773
774
}
774
775
775
776
if (match (I, m_Not (m_Value (Cond)))) {
776
- SelectInfo[I] = {Cond, true , true , 0 };
777
- return ;
777
+ return SelectInfo.insert ({I, {Cond, true , true , 0 }}).first ;
778
778
}
779
779
780
780
// Select instruction are what we are usually looking for.
781
781
if (match (I, m_Select (m_Value (Cond), m_Value (), m_Value ()))) {
782
782
bool Inverted = match (Cond, m_Not (m_Value (Cond)));
783
- SelectInfo[I] = {Cond, false , Inverted, 0 };
784
- return ;
783
+ return SelectInfo.insert ({I, {Cond, false , Inverted, 0 }}).first ;
785
784
}
786
785
787
786
// An Or(zext(i1 X), Y) can also be treated like a select, with condition X
788
787
// and values Y|1 and Y.
789
788
if (auto *BO = dyn_cast<BinaryOperator>(I)) {
790
789
if (BO->getType ()->isIntegerTy (1 ) || BO->getOpcode () != Instruction::Or)
791
- return ;
790
+ return SelectInfo. end () ;
792
791
793
792
for (unsigned Idx = 0 ; Idx < 2 ; Idx++) {
794
793
auto *Op = BO->getOperand (Idx);
795
794
auto It = SelectInfo.find (Op);
796
- if (It != SelectInfo.end () && It->second .IsAuxiliary ) {
797
- SelectInfo[I] = {It-> second . Cond , false , It-> second . IsInverted , Idx};
798
- break ;
799
- }
795
+ if (It != SelectInfo.end () && It->second .IsAuxiliary )
796
+ return SelectInfo
797
+ . insert ({I, {It-> second . Cond , false , It-> second . IsInverted , Idx}})
798
+ . first ;
800
799
}
801
800
}
801
+ return SelectInfo.end ();
802
802
};
803
803
804
804
bool AlreadyProcessed = false ;
805
805
BasicBlock::iterator BBIt = BB.begin ();
806
+ std::map<Value *, SelectLikeInfo>::iterator It;
806
807
while (BBIt != BB.end ()) {
807
808
Instruction *I = &*BBIt++;
809
+ if (I->isDebugOrPseudoInst ())
810
+ continue ;
811
+
808
812
if (!AlreadyProcessed)
809
- ProcessSelectInfo (I);
813
+ It = ProcessSelectInfo (I);
810
814
else
811
815
AlreadyProcessed = false ;
812
816
813
- auto It = SelectInfo.find (I);
814
817
if (It == SelectInfo.end () || It->second .IsAuxiliary )
815
818
continue ;
816
819
817
820
if (!TTI->shouldTreatInstructionLikeSelect (I))
818
821
continue ;
819
822
820
823
Value *Cond = It->second .Cond ;
824
+ // Vector conditions are not supported.
825
+ if (!Cond->getType ()->isIntegerTy (1 ))
826
+ continue ;
827
+
821
828
SelectGroup SIGroup{Cond};
822
829
SIGroup.Selects .emplace_back (I, It->second .IsInverted ,
823
830
It->second .ConditionIdx );
824
831
825
- if (!Cond->getType ()->isIntegerTy (1 ))
826
- continue ;
827
-
828
832
// If the select type is not supported, no point optimizing it.
829
833
// Instruction selection will take care of it.
830
834
if (!isSelectKindSupported (SIGroup.Selects .front ()))
831
835
continue ;
832
836
833
837
while (BBIt != BB.end ()) {
834
838
Instruction *NI = &*BBIt;
835
- ProcessSelectInfo (NI);
836
839
// Debug/pseudo instructions should be skipped and not prevent the
837
840
// formation of a select group.
838
841
if (NI->isDebugOrPseudoInst ()) {
839
842
++BBIt;
840
843
continue ;
841
844
}
842
845
843
- It = SelectInfo. find (NI);
846
+ It = ProcessSelectInfo (NI);
844
847
if (It == SelectInfo.end ()) {
845
848
AlreadyProcessed = true ;
846
849
break ;
0 commit comments