Assume Microsoft ABI on Windows

The SUBZERO_USE_MICROSOFT_ABI macro definition was used to indicate that
we want to use the Microsoft x86-64 calling convention, instead of
the System V one which PNaCl assumes (even on Windows). Using the
standard _WIN64 macro instead makes us not require defining the custom
one as part of our build.

SUBZERO_USE_MICROSOFT_ABI was also being used to decide whether to emit
stack probes. For 32-bit Windows targets, use the _WIN32 macro instead.
Note that when _WIN64 is defined, _WIN32 is also defined, but to avoid
confusion we use _WIN64 in the X8664 backend.

Bug: b/179832693
Change-Id: Ic2e62d3dc26543876673c07b9ccc01e9d92762bf
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/55528
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Reactor/BUILD.gn b/src/Reactor/BUILD.gn
index 4d9ddaa..b8093ce 100644
--- a/src/Reactor/BUILD.gn
+++ b/src/Reactor/BUILD.gn
@@ -72,10 +72,6 @@
       "ICE_THREAD_LOCAL_HACK=0",
     ]
 
-    if (is_win) {
-      defines += [ "SUBZERO_USE_MICROSOFT_ABI" ]
-    }
-
     if (current_cpu == "x64") {
       defines += [
         "SZTARGET=X8664",
diff --git a/third_party/subzero/CMakeLists.txt b/third_party/subzero/CMakeLists.txt
index 40c2735..0460010 100644
--- a/third_party/subzero/CMakeLists.txt
+++ b/third_party/subzero/CMakeLists.txt
@@ -119,8 +119,6 @@
         "ALLOW_MINIMAL_BUILD=0"
         "ALLOW_WASM=0"
         "ICE_THREAD_LOCAL_HACK=0"
-    PRIVATE
-        $<$<BOOL:${WIN32}>:"SUBZERO_USE_MICROSOFT_ABI">
 )
 
 target_link_libraries(subzero
diff --git a/third_party/subzero/src/IceInstX8664.def b/third_party/subzero/src/IceInstX8664.def
index cc9b10b..e07bef6 100644
--- a/third_party/subzero/src/IceInstX8664.def
+++ b/third_party/subzero/src/IceInstX8664.def
@@ -211,7 +211,7 @@
 //          sboxres, isGPR, is64, is32, is16, is8, isXmm, is64To8, is32To8,
 //          is16To8, isTrunc8Rcvr, isAhRcvr, aliases)
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)  // Microsoft x86-64 ABI
+#if defined(_WIN64)  // Microsoft x86-64 ABI
 #define REGX8664_BYTEREG_TABLE REGX8664_BYTEREG_TABLE2(0, 1)
 #define REGX8664_GPR_TABLE REGX8664_GPR_TABLE2(0, 1)
 #else  // System V AMD64 ABI
@@ -264,7 +264,7 @@
 //          sboxres, isGPR, is64, is32, is16, is8, isXmm, is64To8, is32To8,
 //          is16To8, isTrunc8Rcvr, isAhRcvr, aliases)
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)  // Microsoft x86-64 ABI
+#if defined(_WIN64)  // Microsoft x86-64 ABI
 #define REGX8664_XMM_TABLE REGX8664_XMM_TABLE2(0, 1)
 #else  // System V AMD64 ABI
 #define REGX8664_XMM_TABLE REGX8664_XMM_TABLE2(1, 0)
diff --git a/third_party/subzero/src/IceTargetLoweringX8632.cpp b/third_party/subzero/src/IceTargetLoweringX8632.cpp
index 904d1ce..a1434f1 100644
--- a/third_party/subzero/src/IceTargetLoweringX8632.cpp
+++ b/third_party/subzero/src/IceTargetLoweringX8632.cpp
@@ -17,7 +17,7 @@
 
 #include "IceTargetLoweringX8632Traits.h"
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN32)
 extern "C" void _chkstk();
 #endif
 
@@ -115,7 +115,7 @@
 const size_t TargetX8632Traits::TableTypeX8632AttributesSize =
     llvm::array_lengthof(TableTypeX8632Attributes);
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN32)
 // Windows 32-bit only guarantees 4 byte stack alignment
 const uint32_t TargetX8632Traits::X86_STACK_ALIGNMENT_BYTES = 4;
 #else
@@ -408,7 +408,7 @@
 }
 
 void TargetX8632::emitStackProbe(size_t StackSizeBytes) {
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN32)
   if (StackSizeBytes >= 4096) {
     // _chkstk on Win32 is actually __alloca_probe, which adjusts ESP by the
     // stack amount specified in EAX, so we save ESP in ECX, and restore them
diff --git a/third_party/subzero/src/IceTargetLoweringX8664.cpp b/third_party/subzero/src/IceTargetLoweringX8664.cpp
index 13884ea..df10d4d 100644
--- a/third_party/subzero/src/IceTargetLoweringX8664.cpp
+++ b/third_party/subzero/src/IceTargetLoweringX8664.cpp
@@ -17,7 +17,7 @@
 #include "IceDefs.h"
 #include "IceTargetLoweringX8664Traits.h"
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN64)
 extern "C" void __chkstk();
 #endif
 
@@ -716,7 +716,7 @@
       // System V: force r11 when calling a variadic function so that rax isn't
       // used, since rax stores the number of FP args (see NumVariadicFpArgs
       // usage below).
-#if !defined(SUBZERO_USE_MICROSOFT_ABI)
+#if !defined(_WIN64)
       if (NumVariadicFpArgs > 0)
         TargetReg = Traits::RegisterSet::Reg_r11;
 #endif
@@ -732,7 +732,7 @@
     }
 
     // System V: store number of FP args in RAX for variadic calls
-#if !defined(SUBZERO_USE_MICROSOFT_ABI)
+#if !defined(_WIN64)
     if (NumVariadicFpArgs > 0) {
       // Store number of FP args (stored in XMM registers) in RAX for variadic
       // calls
@@ -783,7 +783,7 @@
 }
 
 void TargetX8664::emitStackProbe(size_t StackSizeBytes) {
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN64)
   // Mirroring the behavior of MSVC here, which emits a _chkstk when locals are
   // >= 4KB, rather than the 8KB claimed by the docs.
   if (StackSizeBytes >= 4096) {
diff --git a/third_party/subzero/src/IceTargetLoweringX8664Traits.h b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
index ac0fa78..0d94a40 100644
--- a/third_party/subzero/src/IceTargetLoweringX8664Traits.h
+++ b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
@@ -640,7 +640,7 @@
 
   static RegNumT getRdxOrDie() { return RegisterSet::Reg_rdx; }
 
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
+#if defined(_WIN64)
   // Microsoft x86-64 calling convention:
   //
   // * The first four arguments of vector/fp type, regardless of their
diff --git a/third_party/subzero/src/IceTargetLoweringX86BaseImpl.h b/third_party/subzero/src/IceTargetLoweringX86BaseImpl.h
index f6885bc..0fe65d9 100644
--- a/third_party/subzero/src/IceTargetLoweringX86BaseImpl.h
+++ b/third_party/subzero/src/IceTargetLoweringX86BaseImpl.h
@@ -89,14 +89,12 @@
 
 namespace X86NAMESPACE {
 
-// The Microsoft x64 ABI requires the caller to allocate a minimum 32 byte
+// The Microsoft x64 ABI requires the caller to allocate a 32 byte
 // "shadow store" (aka "home space") so that the callee may copy the 4
 // register args to it.
-template <typename Traits> SizeT getShadowStoreSize() {
-#if defined(SUBZERO_USE_MICROSOFT_ABI)
-  static const SizeT ShadowStoreSize =
-      Traits::Is64Bit ? 4 * typeWidthInBytes(Traits::WordType) : 0;
-  return ShadowStoreSize;
+constexpr SizeT getShadowStoreSize() {
+#if defined(_WIN64)
+  return 4 * sizeof(int64_t);
 #else
   return 0;
 #endif
@@ -1049,7 +1047,7 @@
   // space on the frame for globals (variables with multi-block lifetime), and
   // one block to share for locals (single-block lifetime).
 
-  const SizeT ShadowStoreSize = getShadowStoreSize<Traits>();
+  const SizeT ShadowStoreSize = getShadowStoreSize();
 
   // StackPointer: points just past return address of calling function
 
@@ -2681,7 +2679,7 @@
   OperandList StackArgs, StackArgLocations;
   uint32_t ParameterAreaSizeBytes = 0;
 
-  ParameterAreaSizeBytes += getShadowStoreSize<Traits>();
+  ParameterAreaSizeBytes += getShadowStoreSize();
 
   // Classify each argument operand according to the location where the argument
   // is passed.
@@ -7693,7 +7691,7 @@
   Variable *Dest = Instr->getDest();
   if (Dest != nullptr)
     ReturnType = Dest->getType();
-  return getShadowStoreSize<Traits>() +
+  return getShadowStoreSize() +
          getCallStackArgumentsSizeBytes(ArgTypes, ReturnType);
 }