diff --git a/gcc/doc/sourcebuild.texi b/gcc/doc/sourcebuild.texi
index b0001247795..d3200a42e44 100644
--- a/gcc/doc/sourcebuild.texi
+++ b/gcc/doc/sourcebuild.texi
@@ -1301,6 +1301,8 @@ A selector is:
 @item one or more target triplets, possibly including wildcard characters;
 use @samp{*-*-*} to match any target
 @item a single effective-target keyword (@pxref{Effective-Target Keywords})
+@item a list of compiler options that should be included or excluded
+(as described in more detail below)
 @item a logical expression
 @end itemize
 
@@ -1313,14 +1315,96 @@ test to fail for targets that match @var{selector2}.
 
 A selector expression appears within curly braces and uses a single
 logical operator: one of @samp{!}, @samp{&&}, or @samp{||}.  An
-operand is another selector expression, an effective-target keyword,
-a single target triplet, or a list of target triplets within quotes or
-curly braces.  For example:
+operand is one of the following:
+
+@itemize @bullet
+@item
+another selector expression, in curly braces
+
+@item
+an effective-target keyword, such as @code{lp64}
+
+@item
+a single target triplet
+
+@item
+a list of target triplets within quotes or curly braces
+
+@item
+one of the following:
+
+@table @samp
+@item @{ any-opts @var{opt1} @dots{} @var{optn} @}
+Each of @var{opt1} to @var{optn} is a space-separated list of option globs.
+The selector expression evaluates to true if, for one of these strings,
+every glob in the string matches an option that was passed to the compiler.
+For example:
+
+@smallexample
+@{ any-opts "-O3 -flto" "-O[2g]" @}
+@end smallexample
+
+is true if any of the following are true:
+
+@itemize @bullet
+@item
+@option{-O2} was passed to the compiler
+
+@item
+@option{-Og} was passed to the compiler
+
+@item
+both @option{-O3} and @option{-flto} were passed to the compiler
+@end itemize
+
+This kind of selector can only be used within @code{dg-final} directives.
+Use @code{dg-skip-if}, @code{dg-xfail-if} or @code{dg-xfail-run-if} to
+skip whole tests based on options, or to mark them as expected to fail
+with certain options.
+
+@item @{ no-opts @var{opt1} @dots{} @var{optn} @}
+As for @code{any-opts} above, each of @var{opt1} to @var{optn} is a
+space-separated list of option globs.  The selector expression
+evaluates to true if, for all of these strings, there is at least
+one glob that does not match an option that was passed to the compiler.
+It is shorthand for:
+
+@smallexample
+@{ ! @{ any-opts @var{opt1} @dots{} @var{optn} @} @}
+@end smallexample
+
+For example:
+
+@smallexample
+@{ no-opts "-O3 -flto" "-O[2g]" @}
+@end smallexample
+
+is true if all of the following are true:
+
+@itemize @bullet
+@item
+@option{-O2} was not passed to the compiler
+
+@item
+@option{-Og} was not passed to the compiler
+
+@item
+at least one of @option{-O3} or @option{-flto} was not passed to the compiler
+@end itemize
+
+Like @code{any-opts}, this kind of selector can only be used within
+@code{dg-final} directives.
+
+@end table
+@end itemize
+
+Here are some examples of full target selectors:
 
 @smallexample
 @{ target @{ ! "hppa*-*-* ia64*-*-*" @} @}
 @{ target @{ powerpc*-*-* && lp64 @} @}
 @{ xfail @{ lp64 || vect_no_align @} @}
+@{ xfail @{ aarch64*-*-* && @{ any-opts "-O2" @} @} @}
 @end smallexample
 
 @node Effective-Target Keywords
diff --git a/gcc/testsuite/lib/target-supports-dg.exp b/gcc/testsuite/lib/target-supports-dg.exp
index c014abce4f5..94ba79eb4ae 100644
--- a/gcc/testsuite/lib/target-supports-dg.exp
+++ b/gcc/testsuite/lib/target-supports-dg.exp
@@ -570,7 +570,15 @@ if { [info procs saved-dg-process-target] == [list] } {
 
     # Evaluate a selector expression.
     proc selector_expression { exp } {
-	if { [llength $exp] == 2 } {
+	if { [llength $exp] >= 2
+	     && [string match "any-opts" [lindex $exp 0]] } {
+	    set args [list "" { *-*-* } [lrange $exp 1 end] ""]
+	    set answer [check_conditional_xfail $args]
+	} elseif { [llength $exp] >= 2
+		   && [string match "no-opts" [lindex $exp 0]] } {
+	    set args [list "" { *-*-* } "*" [lrange $exp 1 end]]
+	    set answer [check_conditional_xfail $args]
+	} elseif { [llength $exp] == 2 } {
 	    if [string match "!" [lindex $exp 0]] {
 		set op1 [lindex $exp 1]
 		set answer [expr { ! [selector_opd $op1] }]
diff --git a/gcc/testsuite/gcc.dg/guality/pr41353-1.c b/gcc/testsuite/gcc.dg/guality/pr41353-1.c
index cd306328fda..6639a524f0f 100644
--- a/gcc/testsuite/gcc.dg/guality/pr41353-1.c
+++ b/gcc/testsuite/gcc.dg/guality/pr41353-1.c
@@ -22,7 +22,7 @@ f2 (int i, int j)
 {
   j += i;
   /* { dg-final { gdb-test .+4 "i" "37" } } */
-  /* { dg-final { gdb-test .+3 "j" "28 + 37" { xfail *-*-* } } } */
+  /* { dg-final { gdb-test .+3 "j" "28 + 37" { xfail { no-opts "-O0" } } } } */
   int i1 = 2 * i; /* { dg-final { gdb-test .+2 "i1" "2 * 37" } } */
   int i2 = 3 * i; /* { dg-final { gdb-test .+1 "i2" "3 * 37" } } */
   return j;
diff --git a/gcc/testsuite/gcc.dg/guality/pr54970.c b/gcc/testsuite/gcc.dg/guality/pr54970.c
index 2e0bc5784a9..e60cc043fc9 100644
--- a/gcc/testsuite/gcc.dg/guality/pr54970.c
+++ b/gcc/testsuite/gcc.dg/guality/pr54970.c
@@ -8,39 +8,39 @@
 int
 main ()
 {
-  int a[] = { 1, 2, 3 };	/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { *-*-* } } } } */
+  int a[] = { 1, 2, 3 };	/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { no-opts "-O0" "-Og" } } } } */
   int *p = a + 2;		/* { dg-final { gdb-test .+3 "a\[1\]" "2" } } */
   int *q = a + 1;		/* { dg-final { gdb-test .+2 "a\[2\]" "3" } } */
 				/* { dg-final { gdb-test .+1 "*p" "3" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "2" } } */
-  *p += 10;			/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { *-*-* } } } } */
+  *p += 10;			/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+3 "a\[1\]" "2" } } */
 				/* { dg-final { gdb-test .+2 "a\[2\]" "13" } } */
 				/* { dg-final { gdb-test .+1 "*p" "13" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "2" } } */
-  *q += 10;			/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { *-*-* } } } } */
+  *q += 10;			/* { dg-final { gdb-test .+4 "a\[0\]" "1" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+3 "a\[1\]" "12" } } */
 				/* { dg-final { gdb-test .+2 "a\[2\]" "13" } } */
 				/* { dg-final { gdb-test .+1 "*p" "13" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "12" } } */
   __builtin_memcpy (&a, (int [3]) { 4, 5, 6 }, sizeof (a));
-				/* { dg-final { gdb-test .+4 "a\[0\]" "4" { xfail { *-*-* } } } } */
+				/* { dg-final { gdb-test .+4 "a\[0\]" "4" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+3 "a\[1\]" "5" } } */
 				/* { dg-final { gdb-test .+2 "a\[2\]" "6" } } */
 				/* { dg-final { gdb-test .+1 "*p" "6" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "5" } } */
-  *p += 20;			/* { dg-final { gdb-test .+4 "a\[0\]" "4" { xfail { *-*-* } } } } */
+  *p += 20;			/* { dg-final { gdb-test .+4 "a\[0\]" "4" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+3 "a\[1\]" "5" } } */
 				/* { dg-final { gdb-test .+2 "a\[2\]" "26" } } */
 				/* { dg-final { gdb-test .+1 "*p" "26" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "5" } } */
-  *q += 20;			/* { dg-final { gdb-test .+8 "a\[0\]" "4" { xfail { *-*-* } } } } */
+  *q += 20;			/* { dg-final { gdb-test .+8 "a\[0\]" "4" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+7 "a\[1\]" "25" } } */
 				/* { dg-final { gdb-test .+6 "a\[2\]" "26" } } */
 				/* { dg-final { gdb-test .+5 "*p" "26" } } */
 				/* { dg-final { gdb-test .+4 "p\[-1\]" "25" } } */
-				/* { dg-final { gdb-test .+3 "p\[-2\]" "4" { xfail { *-*-* } } } } */
-				/* { dg-final { gdb-test .+2 "q\[-1\]" "4" { xfail { *-*-* } } } } */
+				/* { dg-final { gdb-test .+3 "p\[-2\]" "4" { xfail { no-opts "-O0" "-Og" } } } } */
+				/* { dg-final { gdb-test .+2 "q\[-1\]" "4" { xfail { no-opts "-O0" "-Og" } } } } */
 				/* { dg-final { gdb-test .+1 "q\[1\]" "26" } } */
   asm volatile (NOP);		/* { dg-final { gdb-test . "*q" "25" } } */
   return 0;
diff --git a/gcc/testsuite/gcc.dg/guality/pr59776.c b/gcc/testsuite/gcc.dg/guality/pr59776.c
index 7c95a9f9416..9777f6229de 100644
--- a/gcc/testsuite/gcc.dg/guality/pr59776.c
+++ b/gcc/testsuite/gcc.dg/guality/pr59776.c
@@ -12,11 +12,11 @@ foo (struct S *p)
   struct S s1, s2;			/* { dg-final { gdb-test pr59776.c:17 "s1.f" "5.0" } } */
   s1 = *p;				/* { dg-final { gdb-test pr59776.c:17 "s1.g" "6.0" } } */
   s2 = s1;				/* { dg-final { gdb-test pr59776.c:17 "s2.f" "0.0" } } */
-  *(int *) &s2.f = 0;			/* { dg-final { gdb-test pr59776.c:17 "s2.g" "6.0" { xfail *-*-* } } } */
+  *(int *) &s2.f = 0;			/* { dg-final { gdb-test pr59776.c:17 "s2.g" "6.0" { xfail { no-opts "-O0" } } } } */
   asm volatile (NOP : : : "memory");	/* { dg-final { gdb-test pr59776.c:20 "s1.f" "5.0" } } */
   asm volatile (NOP : : : "memory");	/* { dg-final { gdb-test pr59776.c:20 "s1.g" "6.0" } } */
   s2 = s1;				/* { dg-final { gdb-test pr59776.c:20 "s2.f" "5.0" } } */
-  asm volatile (NOP : : : "memory");	/* { dg-final { gdb-test pr59776.c:20 "s2.g" "6.0" { xfail *-*-* } } } */
+  asm volatile (NOP : : : "memory");	/* { dg-final { gdb-test pr59776.c:20 "s2.g" "6.0" { xfail { no-opts "-O0" } } } } */
   asm volatile (NOP : : : "memory");
 }
 
