Skip to content

Commit 598c385

Browse files
authored
Merge pull request #13670 from davezarzycki/nfc_builtin_assume_dyn_cast
2 parents 65f1b82 + 2c840a9 commit 598c385

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

include/swift/AST/Type.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/AST/PrintOptions.h"
2626
#include "swift/AST/TypeAlignments.h"
2727
#include "swift/Basic/OptionSet.h"
28+
#include "swift/Basic/Compiler.h"
2829
#include <functional>
2930
#include <string>
3031

@@ -536,7 +537,9 @@ template <class X> inline CanTypeWrapper<X> cast_or_null(CanType type) {
536537
return CanTypeWrapper<X>(cast_or_null<X>(type.getPointer()));
537538
}
538539
template <class X> inline CanTypeWrapper<X> dyn_cast(CanType type) {
539-
return CanTypeWrapper<X>(dyn_cast<X>(type.getPointer()));
540+
auto Ty = type.getPointer();
541+
SWIFT_ASSUME(Ty != nullptr);
542+
return CanTypeWrapper<X>(dyn_cast<X>(Ty));
540543
}
541544
template <class X> inline CanTypeWrapper<X> dyn_cast_or_null(CanType type) {
542545
return CanTypeWrapper<X>(dyn_cast_or_null<X>(type.getPointer()));
@@ -554,7 +557,9 @@ inline CanTypeWrapper<X> cast(CanTypeWrapper<P> type) {
554557
}
555558
template <class X, class P>
556559
inline CanTypeWrapper<X> dyn_cast(CanTypeWrapper<P> type) {
557-
return CanTypeWrapper<X>(dyn_cast<X>(type.getPointer()));
560+
auto Ty = type.getPointer();
561+
SWIFT_ASSUME(Ty != nullptr);
562+
return CanTypeWrapper<X>(dyn_cast<X>(Ty));
558563
}
559564
template <class X, class P>
560565
inline CanTypeWrapper<X> dyn_cast_or_null(CanTypeWrapper<P> type) {

include/swift/AST/Types.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
446446
template <typename T>
447447
T *getAs() {
448448
static_assert(!isSugaredType<T>(), "getAs desugars types");
449-
return dyn_cast<T>(getDesugaredType());
449+
auto Ty = getDesugaredType();
450+
SWIFT_ASSUME(Ty != nullptr);
451+
return dyn_cast<T>(Ty);
450452
}
451453

452454
template <typename T>

include/swift/Basic/Compiler.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#define SWIFT_COMPILER_IS_MSVC 0
2020
#endif
2121

22+
// Workaround non-clang compilers
23+
#ifndef __has_builtin
24+
#define __has_builtin(x) 0
25+
#endif
26+
2227
#if SWIFT_COMPILER_IS_MSVC && _MSC_VER < 1910
2328
// Work around MSVC bug: attempting to reference a deleted function
2429
// https://connect.microsoft.com/VisualStudio/feedback/details/3116505
@@ -28,4 +33,11 @@
2833
#define SWIFT_DELETE_OPERATOR_DELETED = delete;
2934
#endif
3035

36+
// __builtin_assume() is an optimization hint.
37+
#if __has_builtin(__builtin_assume)
38+
#define SWIFT_ASSUME(x) __builtin_assume(x)
39+
#else
40+
#define SWIFT_ASSUME(x)
41+
#endif
42+
3143
#endif // SWIFT_BASIC_COMPILER_H

0 commit comments

Comments
 (0)