diff --git a/gcc/config/xtensa/xtensa.md b/gcc/config/xtensa/xtensa.md
index 6a58d3e2776..c02f1a56641 100644
--- a/gcc/config/xtensa/xtensa.md
+++ b/gcc/config/xtensa/xtensa.md
@@ -1716,63 +1716,78 @@
 
 (define_insn_and_split "*masktrue_const_pow2_minus_one"
   [(set (pc)
-	(if_then_else (match_operator 3 "boolean_operator"
+	(if_then_else (match_operator 4 "boolean_operator"
 			[(and:SI (match_operand:SI 0 "register_operand" "r")
 				 (match_operand:SI 1 "const_int_operand" "i"))
-			 (const_int 0)])
-		      (label_ref (match_operand 2 "" ""))
+			 (match_operand:SI 2 "const_int_operand" "i")])
+		      (label_ref (match_operand 3 "" ""))
 		      (pc)))]
-  "IN_RANGE (exact_log2 (INTVAL (operands[1]) + 1), 17, 31)"
+  "IN_RANGE (exact_log2 (INTVAL (operands[1]) + 1), 17, 31)
+   /* && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0  // can be omitted */
+   && xtensa_b4const_or_zero (INTVAL (operands[2]) << (32 - floor_log2 (INTVAL (operands[1]) + 1)))"
   "#"
   "&& can_create_pseudo_p ()"
-  [(set (match_dup 4)
+  [(set (match_dup 5)
 	(ashift:SI (match_dup 0)
 		   (match_dup 1)))
    (set (pc)
-	(if_then_else (match_op_dup 3
-			[(match_dup 4)
-			 (const_int 0)])
-		      (label_ref (match_dup 2))
+	(if_then_else (match_op_dup 4
+			[(match_dup 5)
+			 (match_dup 2)])
+		      (label_ref (match_dup 3))
 		      (pc)))]
 {
-  operands[1] = GEN_INT (32 - floor_log2 (INTVAL (operands[1]) + 1));
-  operands[4] = gen_reg_rtx (SImode);
+  int shift = 32 - floor_log2 (INTVAL (operands[1]) + 1);
+  operands[1] = GEN_INT (shift);
+  operands[2] = GEN_INT (INTVAL (operands[2]) << shift);
+  operands[5] = gen_reg_rtx (SImode);
 }
   [(set_attr "type"	"jump")
    (set_attr "mode"	"none")
    (set (attr "length")
-	(if_then_else (match_test "TARGET_DENSITY
-				   && INTVAL (operands[1]) == 0x7FFFFFFF")
-		      (const_int 5)
-		      (const_int 6)))])
+	(if_then_else (match_test "(TARGET_DENSITY && INTVAL (operands[1]) == 0x7FFFFFFF)
+				   && INTVAL (operands[2]) == 0")
+		      (const_int 4)
+		      (if_then_else (match_test "TARGET_DENSITY
+						 && (INTVAL (operands[1]) == 0x7FFFFFFF
+						     || INTVAL (operands[2]) == 0)")
+				    (const_int 5)
+				    (const_int 6))))])
 
 (define_insn_and_split "*masktrue_const_negative_pow2"
   [(set (pc)
-	(if_then_else (match_operator 3 "boolean_operator"
+	(if_then_else (match_operator 4 "boolean_operator"
 			[(and:SI (match_operand:SI 0 "register_operand" "r")
 				 (match_operand:SI 1 "const_int_operand" "i"))
-			 (const_int 0)])
-		      (label_ref (match_operand 2 "" ""))
+			 (match_operand:SI 2 "const_int_operand" "i")])
+		      (label_ref (match_operand 3 "" ""))
 		      (pc)))]
-  "IN_RANGE (exact_log2 (-INTVAL (operands[1])), 12, 30)"
+  "IN_RANGE (exact_log2 (-INTVAL (operands[1])), 1, 30)
+   /* && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0  // can be omitted */
+   && xtensa_b4const_or_zero (INTVAL (operands[2]) >> floor_log2 (-INTVAL (operands[1])))"
   "#"
   "&& can_create_pseudo_p ()"
-  [(set (match_dup 4)
+  [(set (match_dup 5)
 	(lshiftrt:SI (match_dup 0)
 		     (match_dup 1)))
    (set (pc)
-	(if_then_else (match_op_dup 3
-			[(match_dup 4)
-			 (const_int 0)])
-		      (label_ref (match_dup 2))
+	(if_then_else (match_op_dup 4
+			[(match_dup 5)
+			 (match_dup 2)])
+		      (label_ref (match_dup 3))
 		      (pc)))]
 {
-  operands[1] = GEN_INT (floor_log2 (-INTVAL (operands[1])));
-  operands[4] = gen_reg_rtx (SImode);
+  int shift = floor_log2 (-INTVAL (operands[1]));
+  operands[1] = GEN_INT (shift);
+  operands[2] = GEN_INT (INTVAL (operands[2]) >> shift);
+  operands[5] = gen_reg_rtx (SImode);
 }
   [(set_attr "type"	"jump")
    (set_attr "mode"	"none")
-   (set_attr "length"	"6")])
+   (set (attr "length")
+	(if_then_else (match_test "TARGET_DENSITY && INTVAL (operands[2]) == 0")
+		      (const_int 5)
+		      (const_int 6)))])
 
 (define_insn_and_split "*masktrue_const_shifted_mask"
   [(set (pc)
@@ -1782,8 +1797,8 @@
 			 (match_operand:SI 2 "const_int_operand" "i")])
 		      (label_ref (match_operand 3 "" ""))
 		      (pc)))]
-  "(INTVAL (operands[2]) & ((1 << ctz_hwi (INTVAL (operands[1]))) - 1)) == 0
-   && xtensa_b4const_or_zero ((uint32_t)INTVAL (operands[2]) >> ctz_hwi (INTVAL (operands[1])))"
+  "/* (INTVAL (operands[2]) & ((1 << ctz_hwi (INTVAL (operands[1]))) - 1)) == 0  // can be omitted
+   && */ xtensa_b4const_or_zero ((uint32_t)INTVAL (operands[2]) >> ctz_hwi (INTVAL (operands[1])))"
   "#"
   "&& can_create_pseudo_p ()"
   [(set (match_dup 6)
