[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / Support / Windows / WindowsSupport.h
blob2e2e97430b76ed29984847c8aeec4179208e6bee
1 //===- WindowsSupport.h - Common Windows Include File -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines things specific to Windows implementations. In addition to
10 // providing some helpers for working with win32 APIs, this header wraps
11 // <windows.h> with some portability macros. Always include WindowsSupport.h
12 // instead of including <windows.h> directly.
14 //===----------------------------------------------------------------------===//
16 //===----------------------------------------------------------------------===//
17 //=== WARNING: Implementation here must contain only generic Win32 code that
18 //=== is guaranteed to work on *all* Win32 variants.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
22 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24 // mingw-w64 tends to define it as 0x0502 in its headers.
25 #undef _WIN32_WINNT
26 #undef _WIN32_IE
28 // Require at least Windows 7 API.
29 #define _WIN32_WINNT 0x0601
30 #define _WIN32_IE 0x0800 // MinGW at it again. FIXME: verify if still needed.
31 #define WIN32_LEAN_AND_MEAN
32 #ifndef NOMINMAX
33 #define NOMINMAX
34 #endif
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/ADT/Twine.h"
40 #include "llvm/Config/config.h" // Get build system configuration settings
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/VersionTuple.h"
45 #include <cassert>
46 #include <string>
47 #include <system_error>
48 #include <windows.h>
50 // Must be included after windows.h
51 #include <wincrypt.h>
53 namespace llvm {
55 /// Determines if the program is running on Windows 8 or newer. This
56 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
57 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
58 /// yet have VersionHelpers.h, so we have our own helper.
59 bool RunningWindows8OrGreater();
61 /// Returns the Windows version as Major.Minor.0.BuildNumber. Uses
62 /// RtlGetVersion or GetVersionEx under the hood depending on what is available.
63 /// GetVersionEx is deprecated, but this API exposes the build number which can
64 /// be useful for working around certain kernel bugs.
65 llvm::VersionTuple GetWindowsOSVersion();
67 bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix);
69 template <typename HandleTraits>
70 class ScopedHandle {
71 typedef typename HandleTraits::handle_type handle_type;
72 handle_type Handle;
74 ScopedHandle(const ScopedHandle &other) = delete;
75 void operator=(const ScopedHandle &other) = delete;
76 public:
77 ScopedHandle()
78 : Handle(HandleTraits::GetInvalid()) {}
80 explicit ScopedHandle(handle_type h)
81 : Handle(h) {}
83 ~ScopedHandle() {
84 if (HandleTraits::IsValid(Handle))
85 HandleTraits::Close(Handle);
88 handle_type take() {
89 handle_type t = Handle;
90 Handle = HandleTraits::GetInvalid();
91 return t;
94 ScopedHandle &operator=(handle_type h) {
95 if (HandleTraits::IsValid(Handle))
96 HandleTraits::Close(Handle);
97 Handle = h;
98 return *this;
101 // True if Handle is valid.
102 explicit operator bool() const {
103 return HandleTraits::IsValid(Handle) ? true : false;
106 operator handle_type() const {
107 return Handle;
111 struct CommonHandleTraits {
112 typedef HANDLE handle_type;
114 static handle_type GetInvalid() {
115 return INVALID_HANDLE_VALUE;
118 static void Close(handle_type h) {
119 ::CloseHandle(h);
122 static bool IsValid(handle_type h) {
123 return h != GetInvalid();
127 struct JobHandleTraits : CommonHandleTraits {
128 static handle_type GetInvalid() {
129 return NULL;
133 struct CryptContextTraits : CommonHandleTraits {
134 typedef HCRYPTPROV handle_type;
136 static handle_type GetInvalid() {
137 return 0;
140 static void Close(handle_type h) {
141 ::CryptReleaseContext(h, 0);
144 static bool IsValid(handle_type h) {
145 return h != GetInvalid();
149 struct RegTraits : CommonHandleTraits {
150 typedef HKEY handle_type;
152 static handle_type GetInvalid() {
153 return NULL;
156 static void Close(handle_type h) {
157 ::RegCloseKey(h);
160 static bool IsValid(handle_type h) {
161 return h != GetInvalid();
165 struct FindHandleTraits : CommonHandleTraits {
166 static void Close(handle_type h) {
167 ::FindClose(h);
171 struct FileHandleTraits : CommonHandleTraits {};
173 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
174 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
175 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
176 typedef ScopedHandle<RegTraits> ScopedRegHandle;
177 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
178 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
180 template <class T>
181 class SmallVectorImpl;
183 template <class T>
184 typename SmallVectorImpl<T>::const_pointer
185 c_str(SmallVectorImpl<T> &str) {
186 str.push_back(0);
187 str.pop_back();
188 return str.data();
191 namespace sys {
193 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
194 ULARGE_INTEGER TimeInteger;
195 TimeInteger.LowPart = Time.dwLowDateTime;
196 TimeInteger.HighPart = Time.dwHighDateTime;
198 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
199 return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
202 inline TimePoint<> toTimePoint(FILETIME Time) {
203 ULARGE_INTEGER TimeInteger;
204 TimeInteger.LowPart = Time.dwLowDateTime;
205 TimeInteger.HighPart = Time.dwHighDateTime;
207 // Adjust for different epoch
208 TimeInteger.QuadPart -= 11644473600ll * 10000000;
210 // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
211 return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
214 inline FILETIME toFILETIME(TimePoint<> TP) {
215 ULARGE_INTEGER TimeInteger;
216 TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
217 TimeInteger.QuadPart += 11644473600ll * 10000000;
219 FILETIME Time;
220 Time.dwLowDateTime = TimeInteger.LowPart;
221 Time.dwHighDateTime = TimeInteger.HighPart;
222 return Time;
225 namespace windows {
226 // Returns command line arguments. Unlike arguments given to main(),
227 // this function guarantees that the returned arguments are encoded in
228 // UTF-8 regardless of the current code page setting.
229 std::error_code GetCommandLineArguments(SmallVectorImpl<const char *> &Args,
230 BumpPtrAllocator &Alloc);
231 } // end namespace windows
232 } // end namespace sys
233 } // end namespace llvm.
235 #endif