/// <summary> /// Check if "condition ? trueInst : falseInst" can be simplified using the null-conditional operator. /// Returns the replacement instruction, or null if no replacement is possible. /// </summary> internal ILInstruction Run(ILInstruction condition, ILInstruction trueInst, ILInstruction falseInst) { Debug.Assert(context.Settings.NullPropagation); Debug.Assert(!condition.MatchLogicNot(out _), "Caller should pass in positive condition"); if (condition is Comp comp && comp.Left.MatchLdLoc(out var testedVar) && comp.Right.MatchLdNull()) { if (comp.LiftingKind != ComparisonLiftingKind.None) { return(null); } if (comp.Kind == ComparisonKind.Equality) { // testedVar == null ? trueInst : falseInst return(TryNullPropagation(testedVar, falseInst, trueInst, Mode.ReferenceType)); } else if (comp.Kind == ComparisonKind.Inequality) { return(TryNullPropagation(testedVar, trueInst, falseInst, Mode.ReferenceType)); } }