diff --git a/gcc/generic-match-head.cc b/gcc/generic-match-head.cc
index b4b5bc88f4b5e315ba16e57e9536deed87584fa7..a71c0727b0b2f78a7772437af150403b7cad1100 100644
--- a/gcc/generic-match-head.cc
+++ b/gcc/generic-match-head.cc
@@ -41,6 +41,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tree-eh.h"
 #include "langhooks.h"
 #include "tree-pass.h"
+#include "attribs.h"
+#include "asan.h"
 
 /* Routine to determine if the types T1 and T2 are effectively
    the same for GENERIC.  If T1 or T2 is not a type, the test
diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index d795066e53e7f742d78d167f4ffb48bffff0f1d8..5d6d26d009b313eb87d8b44823b149bc8567283e 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -47,6 +47,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm.h"
 #include "gimple-range.h"
 #include "langhooks.h"
+#include "attribs.h"
+#include "asan.h"
 
 tree do_valueize (tree, tree (*)(tree), bool &);
 tree do_valueize (tree (*)(tree), tree);
diff --git a/gcc/match.pd b/gcc/match.pd
index ab7622b3bdea6e97737faef06c29c93b2d8ce32f..ee6cef6b09dd48d87b6506a438a206e3e9ab9c77 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1059,6 +1059,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
    (lshift @0 @2)))
 
+/* Shifts by precision or greater result in zero.  */
+(for shift (lshift rshift)
+ (simplify
+  (shift @0 uniform_integer_cst_p@1)
+  (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
+       /* Use a signed compare to leave negative shift counts alone.  */
+       && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
+		     element_precision (type)))
+   { build_zero_cst (type); })))
+
 /* Shifts by constants distribute over several binary operations,
    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
 (for op (plus minus)