@@ -8803,40 +8803,10 @@ static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
8803
8803
return matchFastFloatClamp (Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8804
8804
}
8805
8805
8806
- // / Helps to match a select pattern in case of a type mismatch.
8807
- // /
8808
- // / The function processes the case when type of true and false values of a
8809
- // / select instruction differs from type of the cmp instruction operands because
8810
- // / of a cast instruction. The function checks if it is legal to move the cast
8811
- // / operation after "select". If yes, it returns the new second value of
8812
- // / "select" (with the assumption that cast is moved):
8813
- // / 1. As operand of cast instruction when both values of "select" are same cast
8814
- // / instructions.
8815
- // / 2. As restored constant (by applying reverse cast operation) when the first
8816
- // / value of the "select" is a cast operation and the second value is a
8817
- // / constant.
8818
- // / NOTE: We return only the new second value because the first value could be
8819
- // / accessed as operand of cast instruction.
8820
- static Value *lookThroughCast (CmpInst *CmpI, Value *V1, Value *V2,
8821
- Instruction::CastOps *CastOp) {
8822
- auto *Cast1 = dyn_cast<CastInst>(V1);
8823
- if (!Cast1)
8824
- return nullptr ;
8825
-
8826
- *CastOp = Cast1->getOpcode ();
8827
- Type *SrcTy = Cast1->getSrcTy ();
8828
- if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8829
- // If V1 and V2 are both the same cast from the same type, look through V1.
8830
- if (*CastOp == Cast2->getOpcode () && SrcTy == Cast2->getSrcTy ())
8831
- return Cast2->getOperand (0 );
8832
- return nullptr ;
8833
- }
8834
-
8835
- auto *C = dyn_cast<Constant>(V2);
8836
- if (!C)
8837
- return nullptr ;
8838
-
8806
+ static Value *lookThroughCastConst (CmpInst *CmpI, Type *SrcTy, Constant *C,
8807
+ Instruction::CastOps *CastOp) {
8839
8808
const DataLayout &DL = CmpI->getDataLayout ();
8809
+
8840
8810
Constant *CastedTo = nullptr ;
8841
8811
switch (*CastOp) {
8842
8812
case Instruction::ZExt:
@@ -8912,6 +8882,63 @@ static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8912
8882
return CastedTo;
8913
8883
}
8914
8884
8885
+ // / Helps to match a select pattern in case of a type mismatch.
8886
+ // /
8887
+ // / The function processes the case when type of true and false values of a
8888
+ // / select instruction differs from type of the cmp instruction operands because
8889
+ // / of a cast instruction. The function checks if it is legal to move the cast
8890
+ // / operation after "select". If yes, it returns the new second value of
8891
+ // / "select" (with the assumption that cast is moved):
8892
+ // / 1. As operand of cast instruction when both values of "select" are same cast
8893
+ // / instructions.
8894
+ // / 2. As restored constant (by applying reverse cast operation) when the first
8895
+ // / value of the "select" is a cast operation and the second value is a
8896
+ // / constant. It is implemented in lookThroughCastConst().
8897
+ // / 3. As one operand is cast instruction and the other is not. The operands in
8898
+ // / sel(cmp) are in different type integer.
8899
+ // / NOTE: We return only the new second value because the first value could be
8900
+ // / accessed as operand of cast instruction.
8901
+ static Value *lookThroughCast (CmpInst *CmpI, Value *V1, Value *V2,
8902
+ Instruction::CastOps *CastOp) {
8903
+ auto *Cast1 = dyn_cast<CastInst>(V1);
8904
+ if (!Cast1)
8905
+ return nullptr ;
8906
+
8907
+ *CastOp = Cast1->getOpcode ();
8908
+ Type *SrcTy = Cast1->getSrcTy ();
8909
+ if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8910
+ // If V1 and V2 are both the same cast from the same type, look through V1.
8911
+ if (*CastOp == Cast2->getOpcode () && SrcTy == Cast2->getSrcTy ())
8912
+ return Cast2->getOperand (0 );
8913
+ return nullptr ;
8914
+ }
8915
+
8916
+ auto *C = dyn_cast<Constant>(V2);
8917
+ if (C)
8918
+ return lookThroughCastConst (CmpI, SrcTy, C, CastOp);
8919
+
8920
+ Value *CastedTo = nullptr ;
8921
+ if (*CastOp == Instruction::Trunc) {
8922
+ if (match (CmpI->getOperand (1 ), m_ZExtOrSExt (m_Specific (V2)))) {
8923
+ // Here we have the following case:
8924
+ // %y_ext = sext iK %y to iN
8925
+ // %cond = cmp iN %x, %y_ext
8926
+ // %tr = trunc iN %x to iK
8927
+ // %narrowsel = select i1 %cond, iK %tr, iK %y
8928
+ //
8929
+ // We can always move trunc after select operation:
8930
+ // %y_ext = sext iK %y to iN
8931
+ // %cond = cmp iN %x, %y_ext
8932
+ // %widesel = select i1 %cond, iN %x, iN %y_ext
8933
+ // %tr = trunc iN %widesel to iK
8934
+ assert (V2->getType () == Cast1->getType () &&
8935
+ " V2 and Cast1 should be the same type." );
8936
+ CastedTo = CmpI->getOperand (1 );
8937
+ }
8938
+ }
8939
+
8940
+ return CastedTo;
8941
+ }
8915
8942
SelectPatternResult llvm::matchSelectPattern (Value *V, Value *&LHS, Value *&RHS,
8916
8943
Instruction::CastOps *CastOp,
8917
8944
unsigned Depth) {
0 commit comments