diff --git a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads.def b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads.def
index 39d9b748d52..ada13e6f77c 100644
--- a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads.def
+++ b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads.def
@@ -15,14 +15,17 @@ class X{};
 class Large { public: int arr[10]; };
 class Incomplete;
 
+/* Using `true_def` in order to account for the fact that if this target
+ * doesn't support __builtin_speculation_safe_value at all everything fails to
+ * substitute.  */
 #define SPECULATION_ASSERTS                                                    \
-  MAKE_SPECULATION_ASSERT (int, true)                                          \
+  MAKE_SPECULATION_ASSERT (int, true_def)                                      \
   MAKE_SPECULATION_ASSERT (float, false)                                       \
   MAKE_SPECULATION_ASSERT (X, false)                                           \
   MAKE_SPECULATION_ASSERT (Large, false)                                       \
   MAKE_SPECULATION_ASSERT (Incomplete, false)                                  \
-  MAKE_SPECULATION_ASSERT (int *, true)                                        \
-  MAKE_SPECULATION_ASSERT (long, true)
+  MAKE_SPECULATION_ASSERT (int *, true_def)                                    \
+  MAKE_SPECULATION_ASSERT (long, true_def)
 
 int main() {
     SPECULATION_ASSERTS
diff --git a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads1.C b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads1.C
index bc8f1083a99..4c50d4aa6f5 100644
--- a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads1.C
+++ b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads1.C
@@ -1,5 +1,7 @@
 /* Check that overloaded builtins can be used in templates with SFINAE.  */
 // { dg-do compile { target c++17 } }
+// { dg-additional-options "-Dtrue_def=true" { target speculation_barrier_defined } }
+// { dg-additional-options "-Dtrue_def=false" { target { ! speculation_barrier_defined } } }
 
 /* Checks performed here:
    Various types (some that work, some that don't).  */
diff --git a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads4.C b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads4.C
index c024a21fa18..cc0b3131af7 100644
--- a/gcc/testsuite/g++.dg/template/builtin-speculation-overloads4.C
+++ b/gcc/testsuite/g++.dg/template/builtin-speculation-overloads4.C
@@ -1,5 +1,7 @@
 /* Check that overloaded builtins can be used in templates with SFINAE.  */
 // { dg-do compile { target c++17 } }
+// { dg-additional-options "-Dtrue_def=true" { target speculation_barrier_defined } }
+// { dg-additional-options "-Dtrue_def=false" { target { ! speculation_barrier_defined } } }
 
 /* Checks performed here:
    Optional parameter missing works same as with optional parameter specified.  */
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
index ff95b88f3c4..b8a667d9187 100644
--- a/gcc/testsuite/lib/target-supports.exp
+++ b/gcc/testsuite/lib/target-supports.exp
@@ -180,8 +180,51 @@ proc clear_effective_target_cache { } {
     array unset et_cache
 }
 
+# Like check_no_compiler_messages_nocache, but returns true if the compiler
+# printed a known set of messages.  `messages` is a list of messages to expect
+# *after* pruning with `prune_gcc_output` and removing empty lines.  Each entry
+# in the list should be a regexp pattern matching each line of the output in
+# turn.
+proc check_known_compiler_messages_nocache { messages args } {
+    verbose "Arguments: $args" 3
+    verbose "messages: \"$messages\"" 3
+    initialize_prune_notes
+    set result [eval check_compile $args]
+    set tool_output [lindex $result 0]
+    set output_file [lindex $result 1]
+    remote_file build delete $output_file
+    verbose "pruning output of: \"$tool_output\"" 3
+    set tool_output [prune_gcc_output $tool_output]
+    set lines [split $tool_output "\n"]
+    # Remove empty lines to make interface to this procedure nice.
+    verbose "Comparing \"$lines\" against \"$messages\"" 3
+    set lines [lmap value $lines {
+	if { ! [string match "" $value] } {
+	    string cat $value
+	} else {
+	    continue
+	}}]
+    verbose "After removing empty lines \"$lines\" against \"$messages\"" 3
+    if { [llength $lines] != [llength $messages] } {
+	verbose "message lengths different" 3
+	return 0
+    }
+    verbose "message lengths same" 3
+    foreach msg $messages line $lines {
+	verbose "Comparing \"$msg\" against \"$line\"" 3
+	if { ! [regexp $msg $line] } {
+	    verbose "line did not compare equal" 3
+	    return 0
+	}
+    }
+    verbose "message values same" 3
+    return 1
+}
+
 # Like check_compile, but delete the output file and return true if the
 # compiler printed no messages.
+# Do not implement in terms of `check_known_compiler_messages_nocach` since
+# that procedure includes pruning of messages.
 proc check_no_compiler_messages_nocache {args} {
     set result [eval check_compile $args]
     set lines [lindex $result 0]
@@ -14304,3 +14347,22 @@ proc check_effective_target_alarm { } {
 	}
     }]
 }
+
+# Return true if a speculation barrier is defined on the target.
+proc check_effective_target_speculation_barrier_defined { } {
+    # N.b. not using the existing abstractions around `gcc_warning_prefix`
+    # since that's set depending on the tool that's getting tested while this
+    # effective target is used for all tools.  Moreover this effective target
+    # is always compiling C while different tools are often testing different
+    # languages.  Hence we always know the warning prefix that would be used
+    # for this test.
+    return [check_cached_effective_target speculation_barrier_defined {
+	expr ! [check_known_compiler_messages_nocache \
+	    [list "warning: this target does not define a speculation barrier.*"] \
+	    specbar assembly {
+		int main (int argc, char **argv) { 
+		    return __builtin_speculation_safe_value (argc);
+		}
+	    }]
+    }]
+}
