Skip to content

Commit b6b2875

Browse files
committed
Address comments
* Return iterator to SelectLikeInfo position after processing * Move isDebugOrPseudoInst check position * Add comments
1 parent ce5e430 commit b6b2875

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,9 @@ static Value *getTrueOrFalseValue(
498498

499499
unsigned OtherIdx = 1 - CondIdx;
500500
if (auto *IV = dyn_cast<Instruction>(CBO->getOperand(OtherIdx))) {
501-
if (OptSelects.count(IV)) {
501+
if (OptSelects.count(IV))
502502
CBO->setOperand(OtherIdx,
503503
isTrue ? OptSelects[IV].first : OptSelects[IV].second);
504-
}
505504
}
506505
CBO->insertBefore(B->getTerminator());
507506
return CBO;
@@ -763,84 +762,88 @@ void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
763762

764763
std::map<Value *, SelectLikeInfo> SelectInfo;
765764

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) {
767769
Value *Cond;
768770
if (match(I, m_OneUse(m_ZExt(m_Value(Cond)))) &&
769771
Cond->getType()->isIntegerTy(1)) {
770772
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;
773774
}
774775

775776
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;
778778
}
779779

780780
// Select instruction are what we are usually looking for.
781781
if (match(I, m_Select(m_Value(Cond), m_Value(), m_Value()))) {
782782
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;
785784
}
786785

787786
// An Or(zext(i1 X), Y) can also be treated like a select, with condition X
788787
// and values Y|1 and Y.
789788
if (auto *BO = dyn_cast<BinaryOperator>(I)) {
790789
if (BO->getType()->isIntegerTy(1) || BO->getOpcode() != Instruction::Or)
791-
return;
790+
return SelectInfo.end();
792791

793792
for (unsigned Idx = 0; Idx < 2; Idx++) {
794793
auto *Op = BO->getOperand(Idx);
795794
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;
800799
}
801800
}
801+
return SelectInfo.end();
802802
};
803803

804804
bool AlreadyProcessed = false;
805805
BasicBlock::iterator BBIt = BB.begin();
806+
std::map<Value *, SelectLikeInfo>::iterator It;
806807
while (BBIt != BB.end()) {
807808
Instruction *I = &*BBIt++;
809+
if (I->isDebugOrPseudoInst())
810+
continue;
811+
808812
if (!AlreadyProcessed)
809-
ProcessSelectInfo(I);
813+
It = ProcessSelectInfo(I);
810814
else
811815
AlreadyProcessed = false;
812816

813-
auto It = SelectInfo.find(I);
814817
if (It == SelectInfo.end() || It->second.IsAuxiliary)
815818
continue;
816819

817820
if (!TTI->shouldTreatInstructionLikeSelect(I))
818821
continue;
819822

820823
Value *Cond = It->second.Cond;
824+
// Vector conditions are not supported.
825+
if (!Cond->getType()->isIntegerTy(1))
826+
continue;
827+
821828
SelectGroup SIGroup{Cond};
822829
SIGroup.Selects.emplace_back(I, It->second.IsInverted,
823830
It->second.ConditionIdx);
824831

825-
if (!Cond->getType()->isIntegerTy(1))
826-
continue;
827-
828832
// If the select type is not supported, no point optimizing it.
829833
// Instruction selection will take care of it.
830834
if (!isSelectKindSupported(SIGroup.Selects.front()))
831835
continue;
832836

833837
while (BBIt != BB.end()) {
834838
Instruction *NI = &*BBIt;
835-
ProcessSelectInfo(NI);
836839
// Debug/pseudo instructions should be skipped and not prevent the
837840
// formation of a select group.
838841
if (NI->isDebugOrPseudoInst()) {
839842
++BBIt;
840843
continue;
841844
}
842845

843-
It = SelectInfo.find(NI);
846+
It = ProcessSelectInfo(NI);
844847
if (It == SelectInfo.end()) {
845848
AlreadyProcessed = true;
846849
break;

0 commit comments

Comments
 (0)