Avoid some operator new null checks
authorMike Danes <onemihaid@hotmail.com>
Sat, 14 Oct 2017 11:56:24 +0000 (14:56 +0300)
committerMike Danes <onemihaid@hotmail.com>
Sun, 15 Oct 2017 10:29:48 +0000 (13:29 +0300)
src/jit/compiler.hpp
src/jit/jit.h

index 190cc2e..07b264e 100644 (file)
@@ -872,7 +872,13 @@ void* GenTree::operator new(size_t sz, Compiler* comp, genTreeOps oper)
 #endif // MEASURE_NODE_SIZE
 
     assert(size >= sz);
-    return comp->compGetMem(size, CMK_ASTNode);
+    void* mem = comp->compGetMem(size, CMK_ASTNode);
+#ifdef _MSC_VER
+    // MSVC assumes that a user defined operator new may return nullptr and inserts
+    // a null check before invoking the constructor. This can be avoided using __assume.
+    __assume(mem != nullptr);
+#endif
+    return mem;
 }
 
 // GenTree constructor
index 4315f94..5939013 100644 (file)
@@ -864,12 +864,24 @@ public:
 
 inline void* __cdecl operator new(size_t n, CompAllocator* alloc)
 {
-    return alloc->Alloc(n);
+    void* mem = alloc->Alloc(n);
+#ifdef _MSC_VER
+    // MSVC assumes that a user defined operator new may return nullptr and inserts
+    // a null check before invoking the constructor. This can be avoided using __assume.
+    __assume(mem != nullptr);
+#endif
+    return mem;
 }
 
 inline void* __cdecl operator new[](size_t n, CompAllocator* alloc)
 {
-    return alloc->Alloc(n);
+    void* mem = alloc->Alloc(n);
+#ifdef _MSC_VER
+    // MSVC assumes that a user defined operator new may return nullptr and inserts
+    // a null check before invoking the constructor. This can be avoided using __assume.
+    __assume(mem != nullptr);
+#endif
+    return mem;
 }
 
 class JitTls