From 3f95fb8466a125e8abd5e80e9dfc953fe09d85ab Mon Sep 17 00:00:00 2001 From: Andy Ayers Date: Wed, 30 Aug 2017 09:01:51 -0700 Subject: [PATCH] JIT: allow nulls in gtCanOptimizeTypeEquality (#13680) This is a follow-on to #13657. I looked at the remaining calls to `Type::op_Equality` in the jit-diffs output and saw many of the calls had a null pointer argument. This pattern comes about from explicit null checks in the sources, often as part of argument validation. Such calls can also be optimized into simple pointer equality checks, so add another clause to `gtCanOptimizeTypeEquality` to look for nulls. --- src/jit/gentree.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/jit/gentree.cpp b/src/jit/gentree.cpp index 97e0453..df700a8 100644 --- a/src/jit/gentree.cpp +++ b/src/jit/gentree.cpp @@ -14718,7 +14718,16 @@ void Compiler::gtCheckQuirkAddrExposedLclVar(GenTreePtr tree, GenTreeStack* pare // the operands is: // 1) The result of Object::GetType // 2) The result of typeof(...) -// 3) Is otherwise known to have type RuntimeType +// 3) Is a null reference +// 4) Is otherwise known to have type RuntimeType +// +// The null reference case is surprisingly common because operator +// overloading turns the otherwise innocuous +// +// Type t = ....; +// if (t == null) +// +// into a method call. bool Compiler::gtCanOptimizeTypeEquality(GenTreePtr tree) { @@ -14743,6 +14752,10 @@ bool Compiler::gtCanOptimizeTypeEquality(GenTreePtr tree) { return true; } + else if ((tree->gtOper == GT_CNS_INT) && (tree->gtIntCon.gtIconVal == 0)) + { + return true; + } else { bool isExact = false; -- 2.7.4