Skip to content

Commit 9f9b586

Browse files
committed
Reorder so that matching occurs before any data extraction
Fixes bug in optimizing: ``` define <2 x i64> @php_url_encode_impl(i32 %0, ptr %p) { %2 = load <2 x i64>, ptr %p, align 16 %.not = icmp eq i32 %0, 0 %spec.select = select i1 %.not, <2 x i64> zeroinitializer, <2 x i64> %2 ret <2 x i64> %spec.select } ``` One side effect of the matching is that it garuntees that the types of the TrueV and the Conditional constant match which is assumed by the later code.
1 parent 5879158 commit 9f9b586

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,19 @@ static Instruction *foldSelectZeroOrFixedOp(SelectInst &SI,
907907
if (TrueValC == nullptr || !isa<Instruction>(FalseVal))
908908
return nullptr;
909909

910+
if (!(match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
911+
match(FalseVal, m_c_And(m_Specific(X), m_Value(Y))) ||
912+
match(FalseVal, m_Shl(m_Specific(X), m_Value(Y))) ||
913+
match(FalseVal, m_AShr(m_Specific(X), m_Value(Y))) ||
914+
match(FalseVal, m_LShr(m_Specific(X), m_Value(Y))) ||
915+
match(FalseVal, m_FShl(m_Specific(X), m_Specific(X), m_Value(Y))) ||
916+
match(FalseVal, m_FShr(m_Specific(X), m_Specific(X), m_Value(Y))) ||
917+
match(FalseVal, m_SDiv(m_Specific(X), m_Value(Y))) ||
918+
match(FalseVal, m_UDiv(m_Specific(X), m_Value(Y))) ||
919+
match(FalseVal, m_c_UMin(m_Specific(X), m_Value(Y))))) {
920+
return nullptr;
921+
}
922+
910923
auto *ZeroC = cast<Constant>(cast<Instruction>(CondVal)->getOperand(1));
911924
auto *MergedC = Constant::mergeUndefsWith(TrueValC, ZeroC);
912925
// If X is compared with 0 then TrueVal could be either zero or undef.
@@ -915,28 +928,15 @@ static Instruction *foldSelectZeroOrFixedOp(SelectInst &SI,
915928
if (!match(MergedC, m_Zero()) && !match(MergedC, m_Undef()))
916929
return nullptr;
917930

918-
if (match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
919-
match(FalseVal, m_c_And(m_Specific(X), m_Value(Y))) ||
920-
match(FalseVal, m_Shl(m_Specific(X), m_Value(Y))) ||
921-
match(FalseVal, m_AShr(m_Specific(X), m_Value(Y))) ||
922-
match(FalseVal, m_LShr(m_Specific(X), m_Value(Y))) ||
923-
match(FalseVal, m_FShl(m_Specific(X), m_Specific(X), m_Value(Y))) ||
924-
match(FalseVal, m_FShr(m_Specific(X), m_Specific(X), m_Value(Y))) ||
925-
match(FalseVal, m_SDiv(m_Specific(X), m_Value(Y))) ||
926-
match(FalseVal, m_UDiv(m_Specific(X), m_Value(Y))) ||
927-
match(FalseVal, m_c_UMin(m_Specific(X), m_Value(Y)))) {
928-
auto *FalseValI = cast<Instruction>(FalseVal);
929-
auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
930-
FalseValI->getIterator());
931-
IC.replaceOperand(*FalseValI,
932-
FalseValI->getOperand(0) == Y
933-
? 0
934-
: (FalseValI->getOperand(1) == Y ? 1 : 2),
935-
FrY);
936-
return IC.replaceInstUsesWith(SI, FalseValI);
937-
}
938-
939-
return nullptr;
931+
auto *FalseValI = cast<Instruction>(FalseVal);
932+
auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
933+
FalseValI->getIterator());
934+
IC.replaceOperand(*FalseValI,
935+
FalseValI->getOperand(0) == Y
936+
? 0
937+
: (FalseValI->getOperand(1) == Y ? 1 : 2),
938+
FrY);
939+
return IC.replaceInstUsesWith(SI, FalseValI);
940940
}
941941

942942
/// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).

0 commit comments

Comments
 (0)