56d343
diff -Nrup gcc/config/s390/s390.c gcc/config/s390/s390.c
56d343
--- gcc/config/s390/s390.c	2018-03-27 09:33:20.158140823 -0600
56d343
+++ gcc/config/s390/s390.c	2018-03-27 09:33:58.826861609 -0600
56d343
@@ -958,6 +958,35 @@ s390_expand_builtin (tree exp, rtx targe
56d343
 }
56d343
 
56d343
 
56d343
+/* Masks per jump target register indicating which thunk need to be
56d343
+   generated.  */
56d343
+static GTY(()) int indirect_branch_prez10thunk_mask = 0;
56d343
+static GTY(()) int indirect_branch_z10thunk_mask = 0;
56d343
+
56d343
+#define INDIRECT_BRANCH_NUM_OPTIONS 4
56d343
+
56d343
+enum s390_indirect_branch_option
56d343
+  {
56d343
+    s390_opt_indirect_branch_jump = 0,
56d343
+    s390_opt_indirect_branch_call,
56d343
+    s390_opt_function_return_reg,
56d343
+    s390_opt_function_return_mem
56d343
+  };
56d343
+
56d343
+static GTY(()) int indirect_branch_table_label_no[INDIRECT_BRANCH_NUM_OPTIONS] = { 0 };
56d343
+const char *indirect_branch_table_label[INDIRECT_BRANCH_NUM_OPTIONS] = \
56d343
+  { "LJUMP", "LCALL", "LRETREG", "LRETMEM" };
56d343
+const char *indirect_branch_table_name[INDIRECT_BRANCH_NUM_OPTIONS] =	\
56d343
+  { ".s390_indirect_jump", ".s390_indirect_call",
56d343
+    ".s390_return_reg", ".s390_return_mem" };
56d343
+
56d343
+bool
56d343
+s390_return_addr_from_memory ()
56d343
+{
56d343
+  return (cfun_frame_layout.first_save_gpr <= RETURN_REGNUM
56d343
+	  && cfun_frame_layout.last_save_gpr >= RETURN_REGNUM);
56d343
+}
56d343
+
56d343
 static const int s390_hotpatch_hw_max = 1000000;
56d343
 static int s390_hotpatch_hw_before_label = 0;
56d343
 static int s390_hotpatch_hw_after_label = 0;
56d343
@@ -2669,6 +2698,34 @@ s390_option_override (void)
56d343
   if (TARGET_64BIT && !TARGET_ZARCH)
56d343
     error ("64-bit ABI not supported in ESA/390 mode");
56d343
 
56d343
+  if (s390_indirect_branch != indirect_branch_keep)
56d343
+    {
56d343
+      if (!global_options_set.x_s390_indirect_branch_call)
56d343
+	s390_indirect_branch_call = s390_indirect_branch;
56d343
+
56d343
+      if (!global_options_set.x_s390_indirect_branch_jump)
56d343
+	s390_indirect_branch_jump = s390_indirect_branch;
56d343
+    }
56d343
+
56d343
+  if (s390_function_return != indirect_branch_keep)
56d343
+    {
56d343
+      if (!global_options_set.x_s390_function_return_reg)
56d343
+	s390_function_return_reg = s390_function_return;
56d343
+
56d343
+      if (!global_options_set.x_s390_function_return_mem)
56d343
+	s390_function_return_mem = s390_function_return;
56d343
+    }
56d343
+
56d343
+  if (!TARGET_CPU_ZARCH)
56d343
+    {
56d343
+      if (s390_indirect_branch_call != indirect_branch_keep
56d343
+	  || s390_indirect_branch_jump != indirect_branch_keep)
56d343
+	error ("-mindirect-branch* options require -march=z900 or higher");
56d343
+      if (s390_function_return_reg != indirect_branch_keep
56d343
+	  || s390_function_return_mem != indirect_branch_keep)
56d343
+	error ("-mfunction-return* options require -march=z900 or higher");
56d343
+    }
56d343
+
56d343
   /* Use hardware DFP if available and not explicitly disabled by
56d343
      user. E.g. with -m31 -march=z10 -mzarch   */
56d343
   if (!(target_flags_explicit & MASK_HARD_DFP) && TARGET_DFP)
56d343
@@ -10873,7 +10930,6 @@ s390_emit_epilogue (bool sibcall)
56d343
   rtx frame_pointer, return_reg, cfa_restores = NULL_RTX;
56d343
   int area_bottom, area_top, offset = 0;
56d343
   int next_offset;
56d343
-  rtvec p;
56d343
   int i;
56d343
 
56d343
   if (TARGET_TPF_PROFILING)
56d343
@@ -11023,8 +11079,14 @@ s390_emit_epilogue (bool sibcall)
56d343
 		  && cfun_frame_layout.last_restore_gpr > RETURN_REGNUM))
56d343
 	    {
56d343
 	      int return_regnum = find_unused_clobbered_reg();
56d343
-	      if (!return_regnum)
56d343
-		return_regnum = 4;
56d343
+	      if (!return_regnum
56d343
+		  || (TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION
56d343
+		      && !TARGET_CPU_Z10
56d343
+		      && return_regnum == INDIRECT_BRANCH_THUNK_REGNUM))
56d343
+		{
56d343
+		  gcc_assert (INDIRECT_BRANCH_THUNK_REGNUM != 4);
56d343
+		  return_regnum = 4;
56d343
+		}
56d343
 	      return_reg = gen_rtx_REG (Pmode, return_regnum);
56d343
 
56d343
 	      addr = plus_constant (Pmode, frame_pointer,
56d343
@@ -11054,16 +11116,7 @@ s390_emit_epilogue (bool sibcall)
56d343
     }
56d343
 
56d343
   if (! sibcall)
56d343
-    {
56d343
-
56d343
-      /* Return to caller.  */
56d343
-
56d343
-      p = rtvec_alloc (2);
56d343
-
56d343
-      RTVEC_ELT (p, 0) = ret_rtx;
56d343
-      RTVEC_ELT (p, 1) = gen_rtx_USE (VOIDmode, return_reg);
56d343
-      emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, p));
56d343
-    }
56d343
+    emit_jump_insn (gen_return_use (return_reg));
56d343
 }
56d343
 
56d343
 
56d343
@@ -12371,6 +12424,84 @@ s390_output_mi_thunk (FILE *file, tree t
56d343
   final_end_function ();
56d343
 }
56d343
 
56d343
+/* Output either an indirect jump or a an indirect call
56d343
+   (RETURN_ADDR_REGNO != INVALID_REGNUM) with target register REGNO
56d343
+   using a branch trampoline disabling branch target prediction.  */
56d343
+
56d343
+void
56d343
+s390_indirect_branch_via_thunk (unsigned int regno,
56d343
+				unsigned int return_addr_regno,
56d343
+				rtx comparison_operator,
56d343
+				enum s390_indirect_branch_type type)
56d343
+{
56d343
+  enum s390_indirect_branch_option option;
56d343
+
56d343
+  if (type == s390_indirect_branch_type_return)
56d343
+    {
56d343
+      if (s390_function_return_reg != indirect_branch_keep
56d343
+	  && !s390_return_addr_from_memory ())
56d343
+	option = s390_opt_function_return_reg;
56d343
+
56d343
+      if (s390_function_return_mem != indirect_branch_keep
56d343
+	  && s390_return_addr_from_memory ())
56d343
+	option = s390_opt_function_return_mem;
56d343
+    }
56d343
+  else if (type == s390_indirect_branch_type_jump)
56d343
+    option = s390_opt_indirect_branch_jump;
56d343
+  else if (type == s390_indirect_branch_type_call)
56d343
+    option = s390_opt_indirect_branch_call;
56d343
+  else
56d343
+    gcc_unreachable ();
56d343
+
56d343
+  if (TARGET_INDIRECT_BRANCH_TABLE)
56d343
+    {
56d343
+      char label[32];
56d343
+
56d343
+      ASM_GENERATE_INTERNAL_LABEL (label,
56d343
+				   indirect_branch_table_label[option],
56d343
+				   indirect_branch_table_label_no[option]++);
56d343
+      ASM_OUTPUT_LABEL (asm_out_file, label);
56d343
+    }
56d343
+
56d343
+  if (return_addr_regno != INVALID_REGNUM)
56d343
+    {
56d343
+      gcc_assert (comparison_operator == NULL_RTX);
56d343
+      fprintf (asm_out_file, " \tbrasl\t%%r%d,", return_addr_regno);
56d343
+    }
56d343
+  else
56d343
+    {
56d343
+      fputs (" \tjg", asm_out_file);
56d343
+      if (comparison_operator != NULL_RTX)
56d343
+	print_operand (asm_out_file, comparison_operator, 'C');
56d343
+
56d343
+      fputs ("\t", asm_out_file);
56d343
+    }
56d343
+
56d343
+  if (TARGET_CPU_Z10)
56d343
+    fprintf (asm_out_file,
56d343
+	     TARGET_INDIRECT_BRANCH_THUNK_NAME_EXRL "\n",
56d343
+	     regno);
56d343
+  else
56d343
+    fprintf (asm_out_file,
56d343
+	     TARGET_INDIRECT_BRANCH_THUNK_NAME_EX "\n",
56d343
+	     INDIRECT_BRANCH_THUNK_REGNUM, regno);
56d343
+
56d343
+  if ((option == s390_opt_indirect_branch_jump
56d343
+       && s390_indirect_branch_jump == indirect_branch_thunk)
56d343
+      || (option == s390_opt_indirect_branch_call
56d343
+	  && s390_indirect_branch_call == indirect_branch_thunk)
56d343
+      || (option == s390_opt_function_return_reg
56d343
+	  && s390_function_return_reg == indirect_branch_thunk)
56d343
+      || (option == s390_opt_function_return_mem
56d343
+	  && s390_function_return_mem == indirect_branch_thunk))
56d343
+    {
56d343
+      if (TARGET_CPU_Z10)
56d343
+	indirect_branch_z10thunk_mask |= (1 << regno);
56d343
+      else
56d343
+	indirect_branch_prez10thunk_mask |= (1 << regno);
56d343
+    }
56d343
+}
56d343
+
56d343
 static bool
56d343
 s390_valid_pointer_mode (enum machine_mode mode)
56d343
 {
56d343
@@ -12476,6 +12607,14 @@ s390_function_ok_for_sibcall (tree decl,
56d343
   if (!TARGET_64BIT && flag_pic && decl && !targetm.binds_local_p (decl))
56d343
     return false;
56d343
 
56d343
+  /* The thunks for indirect branches require r1 if no exrl is
56d343
+     available.  r1 might not be available when doing a sibling
56d343
+     call.  */
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+      && !TARGET_CPU_Z10
56d343
+      && !decl)
56d343
+    return false;
56d343
+
56d343
   /* Register 6 on s390 is available as an argument register but unfortunately
56d343
      "caller saved". This makes functions needing this register for arguments
56d343
      not suitable for sibcalls.  */
56d343
@@ -12509,9 +12648,13 @@ s390_emit_call (rtx addr_location, rtx t
56d343
 {
56d343
   bool plt_call = false;
56d343
   rtx insn;
56d343
-  rtx call;
56d343
-  rtx clobber;
56d343
-  rtvec vec;
56d343
+  rtx vec[4] = { NULL_RTX };
56d343
+  int elts = 0;
56d343
+  rtx *call = &vec[0];
56d343
+  rtx *clobber_ret_reg = &vec[1];
56d343
+  rtx *use = &vec[2];
56d343
+  rtx *clobber_thunk_reg = &vec[3];
56d343
+  int i;
56d343
 
56d343
   /* Direct function calls need special treatment.  */
56d343
   if (GET_CODE (addr_location) == SYMBOL_REF)
56d343
@@ -12520,7 +12663,7 @@ s390_emit_call (rtx addr_location, rtx t
56d343
          replace the symbol itself with the PLT stub.  */
56d343
       if (flag_pic && !SYMBOL_REF_LOCAL_P (addr_location))
56d343
         {
56d343
-	  if (retaddr_reg != NULL_RTX)
56d343
+	  if (TARGET_64BIT || retaddr_reg != NULL_RTX)
56d343
 	    {
56d343
 	      addr_location = gen_rtx_UNSPEC (Pmode,
56d343
 					      gen_rtvec (1, addr_location),
56d343
@@ -12563,26 +12706,57 @@ s390_emit_call (rtx addr_location, rtx t
56d343
       addr_location = gen_rtx_REG (Pmode, SIBCALL_REGNUM);
56d343
     }
56d343
 
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+      && GET_CODE (addr_location) != SYMBOL_REF
56d343
+      && !plt_call)
56d343
+    {
56d343
+      /* Indirect branch thunks require the target to be a single GPR.  */
56d343
+      addr_location = force_reg (Pmode, addr_location);
56d343
+
56d343
+      /* Without exrl the indirect branch thunks need an additional
56d343
+	 register for larl;ex */
56d343
+      if (!TARGET_CPU_Z10)
56d343
+	{
56d343
+	  *clobber_thunk_reg = gen_rtx_REG (Pmode, INDIRECT_BRANCH_THUNK_REGNUM);
56d343
+	  *clobber_thunk_reg = gen_rtx_CLOBBER (VOIDmode, *clobber_thunk_reg);
56d343
+	}
56d343
+    }
56d343
+
56d343
   addr_location = gen_rtx_MEM (QImode, addr_location);
56d343
-  call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
56d343
+  *call = gen_rtx_CALL (VOIDmode, addr_location, const0_rtx);
56d343
 
56d343
   if (result_reg != NULL_RTX)
56d343
-    call = gen_rtx_SET (VOIDmode, result_reg, call);
56d343
+    *call = gen_rtx_SET (VOIDmode, result_reg, *call);
56d343
 
56d343
   if (retaddr_reg != NULL_RTX)
56d343
     {
56d343
-      clobber = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
56d343
+      *clobber_ret_reg = gen_rtx_CLOBBER (VOIDmode, retaddr_reg);
56d343
 
56d343
       if (tls_call != NULL_RTX)
56d343
-	vec = gen_rtvec (3, call, clobber,
56d343
-			 gen_rtx_USE (VOIDmode, tls_call));
56d343
-      else
56d343
-	vec = gen_rtvec (2, call, clobber);
56d343
+	*use = gen_rtx_USE (VOIDmode, tls_call);
56d343
+    }
56d343
+
56d343
+  for (i = 0; i < 4; i++)
56d343
+    if (vec[i] != NULL_RTX)
56d343
+      elts++;
56d343
 
56d343
-      call = gen_rtx_PARALLEL (VOIDmode, vec);
56d343
+  if (elts > 1)
56d343
+    {
56d343
+      rtvec v;
56d343
+      int e = 0;
56d343
+
56d343
+      v = rtvec_alloc (elts);
56d343
+      for (i = 0; i < 4; i++)
56d343
+	if (vec[i] != NULL_RTX)
56d343
+	  {
56d343
+	    RTVEC_ELT (v, e) = vec[i];
56d343
+	    e++;
56d343
+	  }
56d343
+
56d343
+      *call = gen_rtx_PARALLEL (VOIDmode, v);
56d343
     }
56d343
 
56d343
-  insn = emit_call_insn (call);
56d343
+  insn = emit_call_insn (*call);
56d343
 
56d343
   /* 31-bit PLT stubs and tls calls use the GOT register implicitly.  */
56d343
   if ((!TARGET_64BIT && plt_call) || tls_call != NULL_RTX)
56d343
@@ -13819,6 +13993,190 @@ s390_asm_file_end (void)
56d343
   file_end_indicate_exec_stack ();
56d343
 }
56d343
 
56d343
+#ifdef HAVE_GAS_HIDDEN
56d343
+# define USE_HIDDEN_LINKONCE 1
56d343
+#else
56d343
+# define USE_HIDDEN_LINKONCE 0
56d343
+#endif
56d343
+
56d343
+/* Output an indirect branch trampoline for target register REGNO.  */
56d343
+
56d343
+static void
56d343
+s390_output_indirect_thunk_function (unsigned int regno, bool z10_p)
56d343
+{
56d343
+  tree decl;
56d343
+  char thunk_label[32];
56d343
+
56d343
+  int i;
56d343
+
56d343
+  if (z10_p)
56d343
+    sprintf (thunk_label, TARGET_INDIRECT_BRANCH_THUNK_NAME_EXRL, regno);
56d343
+  else
56d343
+    sprintf (thunk_label, TARGET_INDIRECT_BRANCH_THUNK_NAME_EX,
56d343
+	     INDIRECT_BRANCH_THUNK_REGNUM, regno);
56d343
+
56d343
+  decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
56d343
+		     get_identifier (thunk_label),
56d343
+		     build_function_type_list (void_type_node, NULL_TREE));
56d343
+  DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
56d343
+				   NULL_TREE, void_type_node);
56d343
+  TREE_PUBLIC (decl) = 1;
56d343
+  TREE_STATIC (decl) = 1;
56d343
+  DECL_IGNORED_P (decl) = 1;
56d343
+
56d343
+  if (USE_HIDDEN_LINKONCE)
56d343
+    {
56d343
+      DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
56d343
+
56d343
+      targetm.asm_out.unique_section (decl, 0);
56d343
+      switch_to_section (get_named_section (decl, NULL, 0));
56d343
+
56d343
+      targetm.asm_out.globalize_label (asm_out_file, thunk_label);
56d343
+      fputs ("\t.hidden\t", asm_out_file);
56d343
+      assemble_name (asm_out_file, thunk_label);
56d343
+      putc ('\n', asm_out_file);
56d343
+      ASM_DECLARE_FUNCTION_NAME (asm_out_file, thunk_label, decl);
56d343
+    }
56d343
+  else
56d343
+    {
56d343
+      switch_to_section (text_section);
56d343
+      ASM_OUTPUT_LABEL (asm_out_file, thunk_label);
56d343
+    }
56d343
+
56d343
+  DECL_INITIAL (decl) = make_node (BLOCK);
56d343
+  current_function_decl = decl;
56d343
+  allocate_struct_function (decl, false);
56d343
+  init_function_start (decl);
56d343
+  cfun->is_thunk = true;
56d343
+  first_function_block_is_cold = false;
56d343
+  final_start_function (emit_barrier (), asm_out_file, 1);
56d343
+
56d343
+  /* This makes CFI at least usable for indirect jumps.
56d343
+
56d343
+     jumps: stopping in the thunk: backtrace will point to the thunk
56d343
+     target is if it was interrupted by a signal
56d343
+
56d343
+     calls: Instead of caller->thunk the backtrace will be
56d343
+     caller->callee->thunk   */
56d343
+  if (flag_asynchronous_unwind_tables)
56d343
+    {
56d343
+      fputs ("\t.cfi_signal_frame\n", asm_out_file);
56d343
+      fprintf (asm_out_file, "\t.cfi_return_column %d\n", regno);
56d343
+      for (i = 0; i < FPR15_REGNUM; i++)
56d343
+	fprintf (asm_out_file, "\t.cfi_same_value %s\n", reg_names[i]);
56d343
+    }
56d343
+
56d343
+  if (z10_p)
56d343
+    {
56d343
+      /* exrl  0,1f  */
56d343
+
56d343
+      /* We generate a thunk for z10 compiled code although z10 is
56d343
+	 currently not enabled.  Tell the assembler to accept the
56d343
+	 instruction.  */
56d343
+      if (!TARGET_CPU_Z10)
56d343
+	{
56d343
+	  fputs ("\t.machine push\n", asm_out_file);
56d343
+	  fputs ("\t.machine z10\n", asm_out_file);
56d343
+	}
56d343
+      /* We use exrl even if -mzarch hasn't been specified on the
56d343
+	 command line so we have to tell the assembler to accept
56d343
+	 it.  */
56d343
+      if (!TARGET_ZARCH)
56d343
+	fputs ("\t.machinemode zarch\n", asm_out_file);
56d343
+
56d343
+      fputs ("\texrl\t0,1f\n", asm_out_file);
56d343
+
56d343
+      if (!TARGET_ZARCH)
56d343
+	fputs ("\t.machinemode esa\n", asm_out_file);
56d343
+
56d343
+      if (!TARGET_CPU_Z10)
56d343
+	fputs ("\t.machine pop\n", asm_out_file);
56d343
+    }
56d343
+  else if (TARGET_CPU_ZARCH)
56d343
+    {
56d343
+      /* larl %r1,1f  */
56d343
+      fprintf (asm_out_file, "\tlarl\t%%r%d,1f\n",
56d343
+	       INDIRECT_BRANCH_THUNK_REGNUM);
56d343
+
56d343
+      /* ex 0,0(%r1)  */
56d343
+      fprintf (asm_out_file, "\tex\t0,0(%%r%d)\n",
56d343
+	       INDIRECT_BRANCH_THUNK_REGNUM);
56d343
+    }
56d343
+  else
56d343
+    gcc_unreachable ();
56d343
+
56d343
+  /* 0:    j 0b  */
56d343
+  fputs ("0:\tj\t0b\n", asm_out_file);
56d343
+
56d343
+  /* 1:    br <regno>  */
56d343
+  fprintf (asm_out_file, "1:\tbr\t%%r%d\n", regno);
56d343
+
56d343
+  final_end_function ();
56d343
+  init_insn_lengths ();
56d343
+  free_after_compilation (cfun);
56d343
+  set_cfun (NULL);
56d343
+  current_function_decl = NULL;
56d343
+}
56d343
+
56d343
+/* Implement the asm.code_end target hook.  */
56d343
+
56d343
+static void
56d343
+s390_code_end (void)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  for (i = 1; i < 16; i++)
56d343
+    {
56d343
+      if (indirect_branch_z10thunk_mask & (1 << i))
56d343
+	s390_output_indirect_thunk_function (i, true);
56d343
+
56d343
+      if (indirect_branch_prez10thunk_mask & (1 << i))
56d343
+	s390_output_indirect_thunk_function (i, false);
56d343
+    }
56d343
+
56d343
+  if (TARGET_INDIRECT_BRANCH_TABLE)
56d343
+    {
56d343
+      int o;
56d343
+      int i;
56d343
+
56d343
+      for (o = 0; o < INDIRECT_BRANCH_NUM_OPTIONS; o++)
56d343
+	{
56d343
+	  if (indirect_branch_table_label_no[o] == 0)
56d343
+	    continue;
56d343
+
56d343
+	  switch_to_section (get_section (indirect_branch_table_name[o],
56d343
+					  0,
56d343
+					  NULL_TREE));
56d343
+	  for (i = 0; i < indirect_branch_table_label_no[o]; i++)
56d343
+	    {
56d343
+	      char label_start[32];
56d343
+
56d343
+	      ASM_GENERATE_INTERNAL_LABEL (label_start,
56d343
+					   indirect_branch_table_label[o], i);
56d343
+
56d343
+	      fputs ("\t.long\t", asm_out_file);
56d343
+	      assemble_name_raw (asm_out_file, label_start);
56d343
+	      fputs ("-.\n", asm_out_file);
56d343
+	    }
56d343
+	  switch_to_section (current_function_section ());
56d343
+	}
56d343
+    }
56d343
+}
56d343
+
56d343
+/* Implement the TARGET_CASE_VALUES_THRESHOLD target hook.  */
56d343
+
56d343
+unsigned int
56d343
+s390_case_values_threshold (void)
56d343
+{
56d343
+  /* Disabling branch prediction for indirect jumps makes jump table
56d343
+     much more expensive.  */
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_JUMP)
56d343
+    return 20;
56d343
+
56d343
+  return default_case_values_threshold ();
56d343
+}
56d343
+
56d343
+
56d343
 /* Initialize GCC target structure.  */
56d343
 
56d343
 #undef  TARGET_ASM_ALIGNED_HI_OP
56d343
@@ -14015,6 +14373,12 @@ s390_asm_file_end (void)
56d343
 #undef TARGET_ASM_FILE_END
56d343
 #define TARGET_ASM_FILE_END s390_asm_file_end
56d343
 
56d343
+#undef TARGET_ASM_CODE_END
56d343
+#define TARGET_ASM_CODE_END s390_code_end
56d343
+
56d343
+#undef TARGET_CASE_VALUES_THRESHOLD
56d343
+#define TARGET_CASE_VALUES_THRESHOLD s390_case_values_threshold
56d343
+
56d343
 struct gcc_target targetm = TARGET_INITIALIZER;
56d343
 
56d343
 #include "gt-s390.h"
56d343
diff -Nrup gcc/config/s390/s390.h gcc/config/s390/s390.h
56d343
--- gcc/config/s390/s390.h	2018-03-27 09:33:19.762143683 -0600
56d343
+++ gcc/config/s390/s390.h	2018-03-27 09:33:58.827861602 -0600
56d343
@@ -1006,4 +1006,37 @@ extern const int processor_flags_table[]
56d343
     s390_register_target_pragmas ();		\
56d343
   } while (0)
56d343
 
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION			\
56d343
+  (s390_function_return_reg != indirect_branch_keep		\
56d343
+   || s390_function_return_mem != indirect_branch_keep)
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_RET					\
56d343
+  ((s390_function_return_reg != indirect_branch_keep			\
56d343
+    && !s390_return_addr_from_memory ())				\
56d343
+   || (s390_function_return_mem != indirect_branch_keep			\
56d343
+       && s390_return_addr_from_memory ()))
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_JUMP		\
56d343
+  (s390_indirect_branch_jump != indirect_branch_keep)
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK				\
56d343
+  (s390_indirect_branch_jump == indirect_branch_thunk			\
56d343
+   || s390_indirect_branch_jump == indirect_branch_thunk_extern)
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_JUMP_INLINE_THUNK		\
56d343
+  (s390_indirect_branch_jump == indirect_branch_thunk_inline)
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_NOBP_CALL		\
56d343
+  (s390_indirect_branch_call != indirect_branch_keep)
56d343
+
56d343
+#ifndef TARGET_DEFAULT_INDIRECT_BRANCH_TABLE
56d343
+#define TARGET_DEFAULT_INDIRECT_BRANCH_TABLE 0
56d343
+#endif
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_THUNK_NAME_EXRL "__s390_indirect_jump_r%d"
56d343
+#define TARGET_INDIRECT_BRANCH_THUNK_NAME_EX   "__s390_indirect_jump_r%duse_r%d"
56d343
+
56d343
+#define TARGET_INDIRECT_BRANCH_TABLE s390_indirect_branch_table
56d343
+
56d343
 #endif /* S390_H */
56d343
diff -Nrup gcc/config/s390/s390.md gcc/config/s390/s390.md
56d343
--- gcc/config/s390/s390.md	2018-03-27 09:33:19.763143675 -0600
56d343
+++ gcc/config/s390/s390.md	2018-03-27 09:33:58.831861573 -0600
56d343
@@ -285,6 +285,8 @@
56d343
   [
56d343
    ; Sibling call register.
56d343
    (SIBCALL_REGNUM		 1)
56d343
+   ; A call-clobbered reg which can be used in indirect branch thunks
56d343
+   (INDIRECT_BRANCH_THUNK_REGNUM 1)
56d343
    ; Literal pool base register.
56d343
    (BASE_REGNUM			13)
56d343
    ; Return address register.
56d343
@@ -304,6 +306,7 @@
56d343
    ; Floating point registers.
56d343
    (FPR0_REGNUM                 16)
56d343
    (FPR2_REGNUM                 18)
56d343
+   (FPR15_REGNUM                31)
56d343
    (VR0_REGNUM                  16)
56d343
    (VR16_REGNUM                 38)
56d343
    (VR23_REGNUM                 45)
56d343
@@ -402,7 +405,10 @@
56d343
                          z196_cracked"
56d343
              (const_string "none"))
56d343
 
56d343
-(define_attr "mnemonic" "bcr_flush,unknown" (const_string "unknown"))
56d343
+; mnemonics which only get defined through if_then_else currently
56d343
+; don't get added to the list values automatically and hence need to
56d343
+; be listed here.
56d343
+(define_attr "mnemonic" "b,br,bas,bc,bcr,bcr_flush,unknown" (const_string "unknown"))
56d343
 
56d343
 ;; Length in bytes.
56d343
 
56d343
@@ -8436,7 +8442,7 @@
56d343
           (match_operator 1 "s390_comparison" [(reg CC_REGNUM) (const_int 0)])
56d343
           (match_operand 0 "address_operand" "ZQZR")
56d343
           (pc)))]
56d343
-  ""
56d343
+  "!TARGET_INDIRECT_BRANCH_NOBP_JUMP"
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "b%C1r\t%0";
56d343
@@ -8446,6 +8452,9 @@
56d343
   [(set (attr "op_type")
56d343
         (if_then_else (match_operand 0 "register_operand" "")
56d343
                       (const_string "RR") (const_string "RX")))
56d343
+   (set (attr "mnemonic")
56d343
+        (if_then_else (match_operand 0 "register_operand" "")
56d343
+                      (const_string "bcr") (const_string "bc")))
56d343
    (set_attr "type"  "branch")
56d343
    (set_attr "atype" "agen")])
56d343
 
56d343
@@ -8499,7 +8508,7 @@
56d343
           (match_operator 1 "s390_comparison" [(reg CC_REGNUM) (const_int 0)])
56d343
           (pc)
56d343
           (match_operand 0 "address_operand" "ZQZR")))]
56d343
-  ""
56d343
+  "!TARGET_INDIRECT_BRANCH_NOBP_JUMP"
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "b%D1r\t%0";
56d343
@@ -8509,6 +8518,9 @@
56d343
   [(set (attr "op_type")
56d343
         (if_then_else (match_operand 0 "register_operand" "")
56d343
                       (const_string "RR") (const_string "RX")))
56d343
+   (set (attr "mnemonic")
56d343
+        (if_then_else (match_operand 0 "register_operand" "")
56d343
+                      (const_string "bcr") (const_string "bc")))
56d343
    (set_attr "type"  "branch")
56d343
    (set_attr "atype" "agen")])
56d343
 
56d343
@@ -9005,29 +9017,125 @@
56d343
 ; indirect-jump instruction pattern(s).
56d343
 ;
56d343
 
56d343
-(define_insn "indirect_jump"
56d343
- [(set (pc) (match_operand 0 "address_operand" "ZQZR"))]
56d343
-  ""
56d343
+(define_expand "indirect_jump"
56d343
+  [(set (pc) (match_operand 0 "address_operand" "ZQZR"))]
56d343
+   ""
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK)
56d343
+    {
56d343
+      operands[0] = force_reg (Pmode, operands[0]);
56d343
+      if (TARGET_CPU_Z10)
56d343
+	{
56d343
+	  if (TARGET_64BIT)
56d343
+	    emit_jump_insn (gen_indirect_jump_via_thunkdi_z10 (operands[0]));
56d343
+	  else
56d343
+	    emit_jump_insn (gen_indirect_jump_via_thunksi_z10 (operands[0]));
56d343
+	}
56d343
+      else
56d343
+	{
56d343
+	  if (TARGET_64BIT)
56d343
+	    emit_jump_insn (gen_indirect_jump_via_thunkdi (operands[0]));
56d343
+	  else
56d343
+	    emit_jump_insn (gen_indirect_jump_via_thunksi (operands[0]));
56d343
+	}
56d343
+      DONE;
56d343
+    }
56d343
+})
56d343
+
56d343
+(define_insn "*indirect_jump"
56d343
+  [(set (pc)
56d343
+	(match_operand 0 "address_operand" "ZR"))]
56d343
+ "!TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK"
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "br\t%0";
56d343
   else
56d343
     return "b\t%a0";
56d343
 }
56d343
-  [(set (attr "op_type")
56d343
-        (if_then_else (match_operand 0 "register_operand" "")
56d343
-                      (const_string "RR") (const_string "RX")))
56d343
-   (set_attr "type"  "branch")
56d343
-   (set_attr "atype" "agen")])
56d343
+ [(set (attr "op_type")
56d343
+       (if_then_else (match_operand 0 "register_operand" "")
56d343
+		     (const_string "RR") (const_string "RX")))
56d343
+  (set (attr "mnemonic")
56d343
+       (if_then_else (match_operand 0 "register_operand" "")
56d343
+		     (const_string "br") (const_string "b")))
56d343
+  (set_attr "type"  "branch")
56d343
+  (set_attr "atype" "agen")])
56d343
+
56d343
+(define_insn "indirect_jump_via_thunk<mode>_z10"
56d343
+  [(set (pc)
56d343
+	(match_operand:P 0 "register_operand" "a"))]
56d343
+ "TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK
56d343
+  && TARGET_CPU_Z10"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  INVALID_REGNUM,
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_jump);
56d343
+  return "";
56d343
+}
56d343
+ [(set_attr "op_type"  "RIL")
56d343
+  (set_attr "mnemonic" "jg")
56d343
+  (set_attr "type"  "branch")
56d343
+  (set_attr "atype" "agen")])
56d343
+
56d343
+(define_insn "indirect_jump_via_thunk<mode>"
56d343
+  [(set (pc)
56d343
+	(match_operand:P 0 "register_operand" " a"))
56d343
+   (clobber (reg:P INDIRECT_BRANCH_THUNK_REGNUM))]
56d343
+ "TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK
56d343
+  && !TARGET_CPU_Z10"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  INVALID_REGNUM,
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_jump);
56d343
+  return "";
56d343
+}
56d343
+ [(set_attr "op_type"  "RIL")
56d343
+  (set_attr "mnemonic" "jg")
56d343
+  (set_attr "type"  "branch")
56d343
+  (set_attr "atype" "agen")])
56d343
 
56d343
 ;
56d343
 ; casesi instruction pattern(s).
56d343
 ;
56d343
 
56d343
-(define_insn "casesi_jump"
56d343
- [(set (pc) (match_operand 0 "address_operand" "ZQZR"))
56d343
-   (use (label_ref (match_operand 1 "" "")))]
56d343
-  ""
56d343
+(define_expand "casesi_jump"
56d343
+  [(parallel
56d343
+    [(set (pc) (match_operand 0 "address_operand"))
56d343
+     (use (label_ref (match_operand 1 "")))])]
56d343
+   ""
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK)
56d343
+    {
56d343
+      operands[0] = force_reg (GET_MODE (operands[0]), operands[0]);
56d343
+
56d343
+      if (TARGET_CPU_Z10)
56d343
+	{
56d343
+	  if (TARGET_64BIT)
56d343
+	    emit_jump_insn (gen_casesi_jump_via_thunkdi_z10 (operands[0],
56d343
+							     operands[1]));
56d343
+	  else
56d343
+	    emit_jump_insn (gen_casesi_jump_via_thunksi_z10 (operands[0],
56d343
+							     operands[1]));
56d343
+	}
56d343
+      else
56d343
+	{
56d343
+	  if (TARGET_64BIT)
56d343
+	    emit_jump_insn (gen_casesi_jump_via_thunkdi (operands[0],
56d343
+							 operands[1]));
56d343
+	  else
56d343
+	    emit_jump_insn (gen_casesi_jump_via_thunksi (operands[0],
56d343
+							 operands[1]));
56d343
+	}
56d343
+      DONE;
56d343
+    }
56d343
+})
56d343
+
56d343
+(define_insn "*casesi_jump"
56d343
+ [(set (pc) (match_operand 0 "address_operand" "ZR"))
56d343
+  (use (label_ref (match_operand 1 "" "")))]
56d343
+ "!TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK"
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "br\t%0";
56d343
@@ -9035,11 +9143,50 @@
56d343
     return "b\t%a0";
56d343
 }
56d343
   [(set (attr "op_type")
56d343
+	(if_then_else (match_operand 0 "register_operand" "")
56d343
+		      (const_string "RR") (const_string "RX")))
56d343
+   (set (attr "mnemonic")
56d343
         (if_then_else (match_operand 0 "register_operand" "")
56d343
-                      (const_string "RR") (const_string "RX")))
56d343
+                      (const_string "br") (const_string "b")))
56d343
+   (set_attr "type"  "branch")
56d343
+   (set_attr "atype" "agen")])
56d343
+
56d343
+(define_insn "casesi_jump_via_thunk<mode>_z10"
56d343
+ [(set (pc) (match_operand:P 0 "register_operand" "a"))
56d343
+  (use (label_ref (match_operand 1 "" "")))]
56d343
+ "TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK
56d343
+  && TARGET_CPU_Z10"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  INVALID_REGNUM,
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_jump);
56d343
+  return "";
56d343
+}
56d343
+  [(set_attr "op_type" "RIL")
56d343
+   (set_attr "mnemonic" "jg")
56d343
    (set_attr "type"  "branch")
56d343
    (set_attr "atype" "agen")])
56d343
 
56d343
+(define_insn "casesi_jump_via_thunk<mode>"
56d343
+ [(set (pc) (match_operand:P 0 "register_operand" "a"))
56d343
+  (use (label_ref (match_operand 1 "" "")))
56d343
+  (clobber (reg:P INDIRECT_BRANCH_THUNK_REGNUM))]
56d343
+ "TARGET_INDIRECT_BRANCH_NOBP_JUMP_THUNK
56d343
+  && !TARGET_CPU_Z10"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  INVALID_REGNUM,
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_jump);
56d343
+  return "";
56d343
+}
56d343
+ [(set_attr "op_type" "RIL")
56d343
+  (set_attr "mnemonic" "jg")
56d343
+  (set_attr "type"  "branch")
56d343
+  (set_attr "atype" "agen")])
56d343
+
56d343
+
56d343
 (define_expand "casesi"
56d343
   [(match_operand:SI 0 "general_operand" "")
56d343
    (match_operand:SI 1 "general_operand" "")
56d343
@@ -9141,11 +9288,30 @@
56d343
 
56d343
 (define_insn "*sibcall_br"
56d343
   [(call (mem:QI (reg SIBCALL_REGNUM))
56d343
-         (match_operand 0 "const_int_operand" "n"))]
56d343
+	 (match_operand 0 "const_int_operand" "n"))]
56d343
   "SIBLING_CALL_P (insn)
56d343
-   && GET_MODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == Pmode"
56d343
-  "br\t%%r1"
56d343
-  [(set_attr "op_type" "RR")
56d343
+    && GET_MODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == Pmode"
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_CALL)
56d343
+    {
56d343
+      gcc_assert (TARGET_CPU_Z10);
56d343
+      s390_indirect_branch_via_thunk (SIBCALL_REGNUM,
56d343
+				      INVALID_REGNUM,
56d343
+				      NULL_RTX,
56d343
+				      s390_indirect_branch_type_call);
56d343
+      return "";
56d343
+    }
56d343
+  else
56d343
+    return "br\t%%r1";
56d343
+}
56d343
+  [(set (attr "op_type")
56d343
+	(if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_CALL")
56d343
+		      (const_string "RIL")
56d343
+		      (const_string "RR")))
56d343
+   (set (attr "mnemonic")
56d343
+	(if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_CALL")
56d343
+		      (const_string "jg")
56d343
+		      (const_string "br")))
56d343
    (set_attr "type"  "branch")
56d343
    (set_attr "atype" "agen")])
56d343
 
56d343
@@ -9185,8 +9351,27 @@
56d343
 	      (match_operand 1 "const_int_operand" "n")))]
56d343
   "SIBLING_CALL_P (insn)
56d343
    && GET_MODE (XEXP (XEXP (XEXP (PATTERN (insn), 1), 0), 0)) == Pmode"
56d343
-  "br\t%%r1"
56d343
-  [(set_attr "op_type" "RR")
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_CALL)
56d343
+    {
56d343
+      gcc_assert (TARGET_CPU_Z10);
56d343
+      s390_indirect_branch_via_thunk (SIBCALL_REGNUM,
56d343
+				      INVALID_REGNUM,
56d343
+				      NULL_RTX,
56d343
+				      s390_indirect_branch_type_call);
56d343
+      return "";
56d343
+    }
56d343
+  else
56d343
+    return "br\t%%r1";
56d343
+}
56d343
+  [(set (attr "op_type")
56d343
+	(if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_CALL")
56d343
+		      (const_string "RIL")
56d343
+		      (const_string "RR")))
56d343
+   (set (attr "mnemonic")
56d343
+	(if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_CALL")
56d343
+		      (const_string "jg")
56d343
+		      (const_string "br")))
56d343
    (set_attr "type"  "branch")
56d343
    (set_attr "atype" "agen")])
56d343
 
56d343
@@ -9252,7 +9437,9 @@
56d343
   [(call (mem:QI (match_operand 0 "address_operand" "ZQZR"))
56d343
          (match_operand 1 "const_int_operand" "n"))
56d343
    (clobber (match_operand 2 "register_operand" "=r"))]
56d343
-  "!SIBLING_CALL_P (insn) && GET_MODE (operands[2]) == Pmode"
56d343
+  "!TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && !SIBLING_CALL_P (insn)
56d343
+   && GET_MODE (operands[2]) == Pmode"
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "basr\t%2,%0";
56d343
@@ -9262,6 +9449,50 @@
56d343
   [(set (attr "op_type")
56d343
         (if_then_else (match_operand 0 "register_operand" "")
56d343
                       (const_string "RR") (const_string "RX")))
56d343
+   (set (attr "mnemonic")
56d343
+        (if_then_else (match_operand 0 "register_operand" "")
56d343
+		      (const_string "basr") (const_string "bas")))
56d343
+   (set_attr "type"  "jsr")
56d343
+   (set_attr "atype" "agen")
56d343
+   (set_attr "z196prop" "z196_cracked")])
56d343
+
56d343
+(define_insn "*basr_via_thunk<mode>_z10"
56d343
+  [(call (mem:QI (match_operand:P 0 "register_operand" "a"))
56d343
+         (match_operand 1 "const_int_operand"          "n"))
56d343
+   (clobber (match_operand:P 2 "register_operand"    "=&r"))]
56d343
+  "TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && TARGET_CPU_Z10
56d343
+   && !SIBLING_CALL_P (insn)"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  REGNO (operands[2]),
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_call);
56d343
+  return "";
56d343
+}
56d343
+  [(set_attr "op_type" "RIL")
56d343
+   (set_attr "mnemonic" "brasl")
56d343
+   (set_attr "type"  "jsr")
56d343
+   (set_attr "atype" "agen")
56d343
+   (set_attr "z196prop" "z196_cracked")])
56d343
+
56d343
+(define_insn "*basr_via_thunk<mode>"
56d343
+  [(call (mem:QI (match_operand:P 0 "register_operand" "a"))
56d343
+         (match_operand 1 "const_int_operand"          "n"))
56d343
+   (clobber (match_operand:P 2 "register_operand"    "=&r"))
56d343
+   (clobber (reg:P INDIRECT_BRANCH_THUNK_REGNUM))]
56d343
+  "TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && !TARGET_CPU_Z10
56d343
+   && !SIBLING_CALL_P (insn)"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+				  REGNO (operands[2]),
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_call);
56d343
+  return "";
56d343
+}
56d343
+  [(set_attr "op_type" "RIL")
56d343
+   (set_attr "mnemonic" "brasl")
56d343
    (set_attr "type"  "jsr")
56d343
    (set_attr "atype" "agen")
56d343
    (set_attr "z196prop" "z196_cracked")])
56d343
@@ -9313,7 +9544,10 @@
56d343
         (call (mem:QI (match_operand 1 "address_operand" "ZQZR"))
56d343
               (match_operand 2 "const_int_operand" "n")))
56d343
    (clobber (match_operand 3 "register_operand" "=r"))]
56d343
-  "!SIBLING_CALL_P (insn) && GET_MODE (operands[3]) == Pmode"
56d343
+  "!TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && !SIBLING_CALL_P (insn)
56d343
+   && GET_MODE (operands[3]) == Pmode"
56d343
+
56d343
 {
56d343
   if (get_attr_op_type (insn) == OP_TYPE_RR)
56d343
     return "basr\t%3,%1";
56d343
@@ -9323,6 +9557,54 @@
56d343
   [(set (attr "op_type")
56d343
         (if_then_else (match_operand 1 "register_operand" "")
56d343
                       (const_string "RR") (const_string "RX")))
56d343
+   (set (attr "mnemonic")
56d343
+        (if_then_else (match_operand 1 "register_operand" "")
56d343
+                      (const_string "basr") (const_string "bas")))
56d343
+   (set_attr "type"  "jsr")
56d343
+   (set_attr "atype" "agen")
56d343
+   (set_attr "z196prop" "z196_cracked")])
56d343
+
56d343
+(define_insn "*basr_r_via_thunk_z10"
56d343
+  [(set (match_operand 0 "" "")
56d343
+        (call (mem:QI (match_operand 1 "register_operand" "a"))
56d343
+              (match_operand 2 "const_int_operand"        "n")))
56d343
+   (clobber (match_operand 3 "register_operand"         "=&r"))]
56d343
+  "TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && TARGET_CPU_Z10
56d343
+   && !SIBLING_CALL_P (insn)
56d343
+   && GET_MODE (operands[3]) == Pmode"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[1]),
56d343
+				  REGNO (operands[3]),
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_call);
56d343
+  return "";
56d343
+}
56d343
+  [(set_attr "op_type" "RIL")
56d343
+   (set_attr "mnemonic" "brasl")
56d343
+   (set_attr "type"  "jsr")
56d343
+   (set_attr "atype" "agen")
56d343
+   (set_attr "z196prop" "z196_cracked")])
56d343
+
56d343
+(define_insn "*basr_r_via_thunk"
56d343
+  [(set (match_operand 0 "" "")
56d343
+        (call (mem:QI (match_operand 1 "register_operand" "a"))
56d343
+              (match_operand 2 "const_int_operand"        "n")))
56d343
+   (clobber (match_operand 3 "register_operand"         "=&r"))
56d343
+   (clobber (reg:P INDIRECT_BRANCH_THUNK_REGNUM))]
56d343
+  "TARGET_INDIRECT_BRANCH_NOBP_CALL
56d343
+   && !TARGET_CPU_Z10
56d343
+   && !SIBLING_CALL_P (insn)
56d343
+   && GET_MODE (operands[3]) == Pmode"
56d343
+{
56d343
+  s390_indirect_branch_via_thunk (REGNO (operands[1]),
56d343
+				  REGNO (operands[3]),
56d343
+				  NULL_RTX,
56d343
+				  s390_indirect_branch_type_call);
56d343
+  return "";
56d343
+}
56d343
+  [(set_attr "op_type" "RIL")
56d343
+   (set_attr "mnemonic"  "brasl")
56d343
    (set_attr "type"  "jsr")
56d343
    (set_attr "atype" "agen")
56d343
    (set_attr "z196prop" "z196_cracked")])
56d343
@@ -10056,15 +10338,78 @@
56d343
   ""
56d343
   "s390_emit_epilogue (true); DONE;")
56d343
 
56d343
-(define_insn "*return"
56d343
+(define_expand "return_use"
56d343
+  [(parallel
56d343
+    [(return)
56d343
+     (use (match_operand 0 "register_operand" "a"))])]
56d343
+  ""
56d343
+{
56d343
+  if (!TARGET_CPU_Z10
56d343
+      && TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION)
56d343
+    {
56d343
+      if (TARGET_64BIT)
56d343
+        emit_jump_insn (gen_returndi_prez10 (operands[0]));
56d343
+      else
56d343
+        emit_jump_insn (gen_returnsi_prez10 (operands[0]));
56d343
+      DONE;
56d343
+    }
56d343
+})
56d343
+
56d343
+(define_insn "*return<mode>"
56d343
   [(return)
56d343
-   (use (match_operand 0 "register_operand" "a"))]
56d343
-  "GET_MODE (operands[0]) == Pmode"
56d343
-  "br\t%0"
56d343
-  [(set_attr "op_type" "RR")
56d343
+   (use (match_operand:P 0 "register_operand" "a"))]
56d343
+  "TARGET_CPU_Z10 || !TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION"
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_RET)
56d343
+    {
56d343
+      s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+                                      INVALID_REGNUM,
56d343
+                                      NULL_RTX,
56d343
+                                      s390_indirect_branch_type_return);
56d343
+      return "";
56d343
+    }
56d343
+  else
56d343
+    return "br\t%0";
56d343
+}
56d343
+  [(set (attr "op_type")
56d343
+       (if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_RET")
56d343
+                     (const_string "RIL")
56d343
+                     (const_string "RR")))
56d343
+   (set (attr "mnemonic")
56d343
+       (if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_RET")
56d343
+                     (const_string "jg")
56d343
+                     (const_string "br")))
56d343
    (set_attr "type"    "jsr")
56d343
    (set_attr "atype"   "agen")])
56d343
 
56d343
+(define_insn "return<mode>_prez10"
56d343
+  [(return)
56d343
+   (use (match_operand:P 0 "register_operand" "a"))
56d343
+   (clobber (reg:P INDIRECT_BRANCH_THUNK_REGNUM))]
56d343
+  "!TARGET_CPU_Z10 && TARGET_INDIRECT_BRANCH_NOBP_RET_OPTION"
56d343
+{
56d343
+  if (TARGET_INDIRECT_BRANCH_NOBP_RET)
56d343
+    {
56d343
+      s390_indirect_branch_via_thunk (REGNO (operands[0]),
56d343
+                                      INVALID_REGNUM,
56d343
+                                      NULL_RTX,
56d343
+                                      s390_indirect_branch_type_return);
56d343
+      return "";
56d343
+    }
56d343
+  else
56d343
+    return "br\t%0";
56d343
+}
56d343
+  [(set (attr "op_type")
56d343
+       (if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_RET")
56d343
+                     (const_string "RIL")
56d343
+                     (const_string "RR")))
56d343
+   (set (attr "mnemonic")
56d343
+       (if_then_else (match_test "TARGET_INDIRECT_BRANCH_NOBP_RET")
56d343
+                     (const_string "jg")
56d343
+                     (const_string "br")))
56d343
+    (set_attr "type"    "jsr")
56d343
+    (set_attr "atype"   "agen")])
56d343
+
56d343
 
56d343
 ;; Instruction definition to extend a 31-bit pointer into a 64-bit
56d343
 ;; pointer. This is used for compatibility.
56d343
diff -Nrup gcc/config/s390/s390.opt gcc/config/s390/s390.opt
56d343
--- gcc/config/s390/s390.opt	2018-03-27 09:33:19.763143675 -0600
56d343
+++ gcc/config/s390/s390.opt	2018-03-27 09:33:58.832861566 -0600
56d343
@@ -175,3 +175,59 @@ Target Report Joined RejectNegative UInt
56d343
 Set the branch costs for conditional branch instructions.  Reasonable
56d343
 values are small, non-negative integers.  The default branch cost is
56d343
 1.
56d343
+
56d343
+mindirect-branch=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_indirect_branch) Init(indirect_branch_keep)
56d343
+Wrap all indirect branches into execute in order to disable branch
56d343
+prediction.
56d343
+
56d343
+mindirect-branch-jump=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_indirect_branch_jump) Init(indirect_branch_keep)
56d343
+Wrap indirect table jumps and computed gotos into execute in order to
56d343
+disable branch prediction.  Using thunk or thunk-extern with this
56d343
+option requires the thunks to be considered signal handlers to order to
56d343
+generate correct CFI.  For environments where unwinding (e.g. for
56d343
+exceptions) is required please use thunk-inline instead.
56d343
+
56d343
+mindirect-branch-call=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_indirect_branch_call) Init(indirect_branch_keep)
56d343
+Wrap all indirect calls into execute in order to disable branch prediction.
56d343
+
56d343
+mfunction-return=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_function_return) Init(indirect_branch_keep)
56d343
+Wrap all indirect return branches into execute in order to disable branch
56d343
+prediction.
56d343
+
56d343
+mfunction-return-mem=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_function_return_mem) Init(indirect_branch_keep)
56d343
+Wrap indirect return branches into execute in order to disable branch
56d343
+prediction. This affects only branches where the return address is
56d343
+going to be restored from memory.
56d343
+
56d343
+mfunction-return-reg=
56d343
+Target Report RejectNegative Joined Enum(indirect_branch) Var(s390_function_return_reg) Init(indirect_branch_keep)
56d343
+Wrap indirect return branches into execute in order to disable branch
56d343
+prediction. This affects only branches where the return address
56d343
+doesn't need to be restored from memory.
56d343
+
56d343
+Enum
56d343
+Name(indirect_branch) Type(enum indirect_branch)
56d343
+Known indirect branch choices (for use with the -mindirect-branch=/-mfunction-return= options):
56d343
+
56d343
+EnumValue
56d343
+Enum(indirect_branch) String(keep) Value(indirect_branch_keep)
56d343
+
56d343
+EnumValue
56d343
+Enum(indirect_branch) String(thunk) Value(indirect_branch_thunk)
56d343
+
56d343
+EnumValue
56d343
+Enum(indirect_branch) String(thunk-extern) Value(indirect_branch_thunk_extern)
56d343
+
56d343
+mindirect-branch-table
56d343
+Target Report Var(s390_indirect_branch_table) Init(TARGET_DEFAULT_INDIRECT_BRANCH_TABLE)
56d343
+Generate sections .s390_indirect_jump, .s390_indirect_call,
56d343
+.s390_return_reg, and .s390_return_mem to contain the indirect branch
56d343
+locations which have been patched as part of using one of the
56d343
+-mindirect-branch* or -mfunction-return* options.  The sections
56d343
+consist of an array of 32 bit elements. Each entry holds the offset
56d343
+from the entry to the patched location.
56d343
diff -Nrup gcc/config/s390/s390-opts.h gcc/config/s390/s390-opts.h
56d343
--- gcc/config/s390/s390-opts.h	2018-03-27 09:33:19.764143668 -0600
56d343
+++ gcc/config/s390/s390-opts.h	2018-03-27 09:33:58.821861645 -0600
56d343
@@ -39,4 +39,12 @@ enum processor_type
56d343
   PROCESSOR_max
56d343
 };
56d343
 
56d343
+/* Values for -mindirect-branch and -mfunction-return options.  */
56d343
+enum indirect_branch {
56d343
+  indirect_branch_unset = 0,
56d343
+  indirect_branch_keep,
56d343
+  indirect_branch_thunk,
56d343
+  indirect_branch_thunk_extern
56d343
+};
56d343
+
56d343
 #endif
56d343
diff -Nrup gcc/config/s390/s390-protos.h gcc/config/s390/s390-protos.h
56d343
--- gcc/config/s390/s390-protos.h	2018-03-27 09:33:19.764143668 -0600
56d343
+++ gcc/config/s390/s390-protos.h	2018-03-27 09:33:58.821861645 -0600
56d343
@@ -41,6 +41,7 @@ extern void s390_set_has_landing_pad_p (
56d343
 extern bool s390_hard_regno_mode_ok (unsigned int, enum machine_mode);
56d343
 extern bool s390_hard_regno_rename_ok (unsigned int, unsigned int);
56d343
 extern int s390_class_max_nregs (enum reg_class, enum machine_mode);
56d343
+extern bool s390_return_addr_from_memory(void);
56d343
 extern int s390_cannot_change_mode_class (enum machine_mode, enum machine_mode,
56d343
 					  enum reg_class);
56d343
 extern bool s390_function_arg_vector (enum machine_mode, const_tree);
56d343
@@ -124,6 +125,18 @@ extern int s390_compare_and_branch_condi
56d343
 extern bool s390_extzv_shift_ok (int, int, unsigned HOST_WIDE_INT);
56d343
 extern void s390_asm_output_function_label (FILE *, const char *, tree);
56d343
 
56d343
+enum s390_indirect_branch_type
56d343
+  {
56d343
+    s390_indirect_branch_type_jump = 0,
56d343
+    s390_indirect_branch_type_call,
56d343
+    s390_indirect_branch_type_return
56d343
+  };
56d343
+extern void s390_indirect_branch_via_thunk (unsigned int regno,
56d343
+					    unsigned int return_addr_regno,
56d343
+					    rtx comparison_operator,
56d343
+					    enum s390_indirect_branch_type type);
56d343
+extern void s390_indirect_branch_via_inline_thunk (rtx execute_target);
56d343
+
56d343
 #endif /* RTX_CODE */
56d343
 
56d343
 /* s390-c.c routines */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-function-pointer-nothunk.c gcc/testsuite/gcc.target/s390/nobp-function-pointer-nothunk.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-function-pointer-nothunk.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-function-pointer-nothunk.c	2018-03-27 09:33:58.832861566 -0600
56d343
@@ -0,0 +1,59 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O3  -march=z10 --save-temps -mindirect-branch-call=thunk-extern -mindirect-branch-table" } */
56d343
+
56d343
+int gl;
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  gl = a + 40;
56d343
+}
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+foo_value (int a)
56d343
+{
56d343
+  return a + 40;
56d343
+}
56d343
+
56d343
+void*  __attribute__((noinline,noclone))
56d343
+get_fptr (int a)
56d343
+{
56d343
+  switch (a)
56d343
+    {
56d343
+    case 0: return &foo; break;
56d343
+    case 1: return &foo_value; break;
56d343
+    default: __builtin_abort ();
56d343
+    }
56d343
+}
56d343
+
56d343
+void (*f) (int);
56d343
+int (*g) (int);
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  int res;
56d343
+
56d343
+  f = get_fptr(0);
56d343
+  f (2);
56d343
+  if (gl != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  g = get_fptr(1);
56d343
+  if (g (2) != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2 x main
56d343
+/* { dg-final { scan-assembler-times "brasl\t%r\[0-9\]*,__s390_indirect_jump" 2 } } */
56d343
+
56d343
+/* No thunks due to thunk-extern.  */
56d343
+/* { dg-final { scan-assembler-not "exrl" } } */
56d343
+/* { dg-final { scan-assembler-not ".globl __s390_indirect_jump" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-function-pointer-z10.c gcc/testsuite/gcc.target/s390/nobp-function-pointer-z10.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-function-pointer-z10.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-function-pointer-z10.c	2018-03-27 09:33:58.833861558 -0600
56d343
@@ -0,0 +1,56 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3  -march=z10 --save-temps -mindirect-branch-call=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl;
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  gl = a + 40;
56d343
+}
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+foo_value (int a)
56d343
+{
56d343
+  return a + 40;
56d343
+}
56d343
+
56d343
+void*  __attribute__((noinline,noclone))
56d343
+get_fptr (int a)
56d343
+{
56d343
+  switch (a)
56d343
+    {
56d343
+    case 0: return &foo; break;
56d343
+    case 1: return &foo_value; break;
56d343
+    default: __builtin_abort ();
56d343
+    }
56d343
+}
56d343
+
56d343
+void (*f) (int);
56d343
+int (*g) (int);
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  int res;
56d343
+
56d343
+  f = get_fptr(0);
56d343
+  f (2);
56d343
+  if (gl != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  g = get_fptr(1);
56d343
+  if (g (2) != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2 x main
56d343
+/* { dg-final { scan-assembler-times "brasl\t%r\[0-9\]*,__s390_indirect_jump" 2 } } */
56d343
+/* { dg-final { scan-assembler "exrl" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-function-pointer-z900.c gcc/testsuite/gcc.target/s390/nobp-function-pointer-z900.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-function-pointer-z900.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-function-pointer-z900.c	2018-03-27 09:33:58.833861558 -0600
56d343
@@ -0,0 +1,56 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3  -march=z900 --save-temps -mindirect-branch-call=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl;
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  gl = a + 40;
56d343
+}
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+foo_value (int a)
56d343
+{
56d343
+  return a + 40;
56d343
+}
56d343
+
56d343
+void*  __attribute__((noinline,noclone))
56d343
+get_fptr (int a)
56d343
+{
56d343
+  switch (a)
56d343
+    {
56d343
+    case 0: return &foo; break;
56d343
+    case 1: return &foo_value; break;
56d343
+    default: __builtin_abort ();
56d343
+    }
56d343
+}
56d343
+
56d343
+void (*f) (int);
56d343
+int (*g) (int);
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  int res;
56d343
+
56d343
+  f = get_fptr(0);
56d343
+  f (2);
56d343
+  if (gl != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  g = get_fptr(1);
56d343
+  if (g (2) != 42)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2 x main
56d343
+/* { dg-final { scan-assembler-times "brasl\t%r\[0-9\]*,__s390_indirect_jump" 2 } } */
56d343
+/* { dg-final { scan-assembler "ex\t" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-indirect-jump-nothunk.c gcc/testsuite/gcc.target/s390/nobp-indirect-jump-nothunk.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-indirect-jump-nothunk.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-indirect-jump-nothunk.c	2018-03-27 09:33:58.833861558 -0600
56d343
@@ -0,0 +1,45 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O3 -march=z10 --save-temps -mindirect-branch-jump=thunk-extern -mindirect-branch-table" } */
56d343
+
56d343
+/* This is a copy of the gcc.c-torture/execute/20040302-1.c
56d343
+   testcase.  */
56d343
+
56d343
+int code[]={0,0,0,0,1};
56d343
+
56d343
+void
56d343
+foo(int x) {
56d343
+  volatile int b;
56d343
+  b = 0xffffffff;
56d343
+}
56d343
+
56d343
+void
56d343
+bar(int *pc) {
56d343
+  static const void *l[] = {&&lab0, &&end;;
56d343
+
56d343
+  foo(0);
56d343
+  goto *l[*pc];
56d343
+ lab0:
56d343
+  foo(0);
56d343
+  pc++;
56d343
+  goto *l[*pc];
56d343
+ end:
56d343
+  return;
56d343
+}
56d343
+
56d343
+int
56d343
+main() {
56d343
+  bar(code);
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2 x bar
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+
56d343
+/* No thunks due to thunk-extern.  */
56d343
+/* { dg-final { scan-assembler-not "exrl" } } */
56d343
+/* { dg-final { scan-assembler-not ".globl __s390_indirect_jump" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z10.c gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z10.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z10.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z10.c	2018-03-27 09:33:58.834861551 -0600
56d343
@@ -0,0 +1,42 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z10 --save-temps -mindirect-branch-jump=thunk -mindirect-branch-table" } */
56d343
+
56d343
+/* This is a copy of the gcc.c-torture/execute/20040302-1.c
56d343
+   testcase.  */
56d343
+
56d343
+int code[]={0,0,0,0,1};
56d343
+
56d343
+void
56d343
+foo(int x) {
56d343
+  volatile int b;
56d343
+  b = 0xffffffff;
56d343
+}
56d343
+
56d343
+void
56d343
+bar(int *pc) {
56d343
+  static const void *l[] = {&&lab0, &&end;;
56d343
+
56d343
+  foo(0);
56d343
+  goto *l[*pc];
56d343
+ lab0:
56d343
+  foo(0);
56d343
+  pc++;
56d343
+  goto *l[*pc];
56d343
+ end:
56d343
+  return;
56d343
+}
56d343
+
56d343
+int
56d343
+main() {
56d343
+  bar(code);
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2x bar */
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+/* { dg-final { scan-assembler "exrl" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z900.c gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z900.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z900.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-indirect-jump-z900.c	2018-03-27 09:33:58.834861551 -0600
56d343
@@ -0,0 +1,42 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z900 --save-temps -mindirect-branch-jump=thunk -mindirect-branch-table" } */
56d343
+
56d343
+/* This is a copy of the gcc.c-torture/execute/20040302-1.c
56d343
+   testcase.  */
56d343
+
56d343
+int code[]={0,0,0,0,1};
56d343
+
56d343
+void
56d343
+foo(int x) {
56d343
+  volatile int b;
56d343
+  b = 0xffffffff;
56d343
+}
56d343
+
56d343
+void
56d343
+bar(int *pc) {
56d343
+  static const void *l[] = {&&lab0, &&end;;
56d343
+
56d343
+  foo(0);
56d343
+  goto *l[*pc];
56d343
+ lab0:
56d343
+  foo(0);
56d343
+  pc++;
56d343
+  goto *l[*pc];
56d343
+ end:
56d343
+  return;
56d343
+}
56d343
+
56d343
+int
56d343
+main() {
56d343
+  bar(code);
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 2 x bar
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+/* { dg-final { scan-assembler "ex\t" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-mem-nothunk.c gcc/testsuite/gcc.target/s390/nobp-return-mem-nothunk.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-mem-nothunk.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-mem-nothunk.c	2018-03-27 09:33:58.834861551 -0600
56d343
@@ -0,0 +1,44 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O3 -march=z10 -mzarch --save-temps -mfunction-return-mem=thunk-extern -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x foo, 1 x main
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+
56d343
+/* No thunks due to thunk-extern.  */
56d343
+/* { dg-final { scan-assembler-not "exrl" } } */
56d343
+/* { dg-final { scan-assembler-not ".globl __s390_indirect_jump" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-mem-z10.c gcc/testsuite/gcc.target/s390/nobp-return-mem-z10.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-mem-z10.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-mem-z10.c	2018-03-27 09:33:58.835861544 -0600
56d343
@@ -0,0 +1,41 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z10 -mzarch --save-temps -mfunction-return-mem=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x foo, 1 x main
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+/* { dg-final { scan-assembler "exrl" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-mem-z900.c gcc/testsuite/gcc.target/s390/nobp-return-mem-z900.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-mem-z900.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-mem-z900.c	2018-03-27 09:33:58.835861544 -0600
56d343
@@ -0,0 +1,42 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z900 --save-temps -mfunction-return-mem=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x foo, 1 x main
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 2 } } */
56d343
+
56d343
+/* { dg-final { scan-assembler "ex\t" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-reg-nothunk.c gcc/testsuite/gcc.target/s390/nobp-return-reg-nothunk.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-reg-nothunk.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-reg-nothunk.c	2018-03-27 09:33:58.835861544 -0600
56d343
@@ -0,0 +1,44 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O3 -march=z10 --save-temps -mfunction-return-reg=thunk-extern -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x bar
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 1 } } */
56d343
+
56d343
+/* No thunks due to thunk-extern.  */
56d343
+/* { dg-final { scan-assembler-not "exrl" } } */
56d343
+/* { dg-final { scan-assembler-not ".globl __s390_indirect_jump" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-reg-z10.c gcc/testsuite/gcc.target/s390/nobp-return-reg-z10.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-reg-z10.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-reg-z10.c	2018-03-27 09:33:58.836861537 -0600
56d343
@@ -0,0 +1,41 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z10 --save-temps -mfunction-return-reg=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x bar
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 1 } } */
56d343
+/* { dg-final { scan-assembler "exrl" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-return-reg-z900.c gcc/testsuite/gcc.target/s390/nobp-return-reg-z900.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-return-reg-z900.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-return-reg-z900.c	2018-03-27 09:33:58.836861537 -0600
56d343
@@ -0,0 +1,41 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z900 --save-temps -mfunction-return-reg=thunk -mindirect-branch-table" } */
56d343
+
56d343
+int gl = 0;
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  return a + 2;
56d343
+}
56d343
+
56d343
+void __attribute__((noinline,noclone))
56d343
+foo (int a)
56d343
+{
56d343
+  int i;
56d343
+
56d343
+  if (a == 42)
56d343
+    return;
56d343
+
56d343
+  for (i = 0; i < a; i++)
56d343
+    gl += bar (i);
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  foo (3);
56d343
+  if (gl != 9)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x bar
56d343
+/* { dg-final { scan-assembler-times "jg\t__s390_indirect_jump" 1 } } */
56d343
+/* { dg-final { scan-assembler "ex\t" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler     "section\t.s390_return_reg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_mem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-table-jump-z10.c gcc/testsuite/gcc.target/s390/nobp-table-jump-z10.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-table-jump-z10.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-table-jump-z10.c	2018-03-27 09:33:58.836861537 -0600
56d343
@@ -0,0 +1,77 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z10 -mzarch --save-temps -mindirect-branch-jump=thunk -mindirect-branch-table" } */
56d343
+/* case-values-threshold will be set to 20 by the back-end when jump
56d343
+   thunk are requested.  */
56d343
+
56d343
+int __attribute__((noinline,noclone)) foo1 (void) { return 1; }
56d343
+int __attribute__((noinline,noclone)) foo2 (void) { return 2; }
56d343
+int __attribute__((noinline,noclone)) foo3 (void) { return 3; }
56d343
+int __attribute__((noinline,noclone)) foo4 (void) { return 4; }
56d343
+int __attribute__((noinline,noclone)) foo5 (void) { return 5; }
56d343
+int __attribute__((noinline,noclone)) foo6 (void) { return 6; }
56d343
+int __attribute__((noinline,noclone)) foo7 (void) { return 7; }
56d343
+int __attribute__((noinline,noclone)) foo8 (void) { return 8; }
56d343
+int __attribute__((noinline,noclone)) foo9 (void) { return 9; }
56d343
+int __attribute__((noinline,noclone)) foo10 (void) { return 10; }
56d343
+int __attribute__((noinline,noclone)) foo11 (void) { return 11; }
56d343
+int __attribute__((noinline,noclone)) foo12 (void) { return 12; }
56d343
+int __attribute__((noinline,noclone)) foo13 (void) { return 13; }
56d343
+int __attribute__((noinline,noclone)) foo14 (void) { return 14; }
56d343
+int __attribute__((noinline,noclone)) foo15 (void) { return 15; }
56d343
+int __attribute__((noinline,noclone)) foo16 (void) { return 16; }
56d343
+int __attribute__((noinline,noclone)) foo17 (void) { return 17; }
56d343
+int __attribute__((noinline,noclone)) foo18 (void) { return 18; }
56d343
+int __attribute__((noinline,noclone)) foo19 (void) { return 19; }
56d343
+int __attribute__((noinline,noclone)) foo20 (void) { return 20; }
56d343
+
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  int ret = 0;
56d343
+
56d343
+  switch (a)
56d343
+    {
56d343
+    case 1: ret = foo1 (); break;
56d343
+    case 2: ret = foo2 (); break;
56d343
+    case 3: ret = foo3 (); break;
56d343
+    case 4: ret = foo4 (); break;
56d343
+    case 5: ret = foo5 (); break;
56d343
+    case 6: ret = foo6 (); break;
56d343
+    case 7: ret = foo7 (); break;
56d343
+    case 8: ret = foo8 (); break;
56d343
+    case 9: ret = foo9 (); break;
56d343
+    case 10: ret = foo10 (); break;
56d343
+    case 11: ret = foo11 (); break;
56d343
+    case 12: ret = foo12 (); break;
56d343
+    case 13: ret = foo13 (); break;
56d343
+    case 14: ret = foo14 (); break;
56d343
+    case 15: ret = foo15 (); break;
56d343
+    case 16: ret = foo16 (); break;
56d343
+    case 17: ret = foo17 (); break;
56d343
+    case 18: ret = foo18 (); break;
56d343
+    case 19: ret = foo19 (); break;
56d343
+    case 20: ret = foo20 (); break;
56d343
+    default:
56d343
+      __builtin_abort ();
56d343
+    }
56d343
+
56d343
+  return ret;
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  if (bar (3) != 3)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x bar
56d343
+/* { dg-final { scan-assembler-times "exrl" 1 } } */
56d343
+
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_fromreg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_frommem" } } */
56d343
diff -Nrup gcc/testsuite/gcc.target/s390/nobp-table-jump-z900.c gcc/testsuite/gcc.target/s390/nobp-table-jump-z900.c
56d343
--- gcc/testsuite/gcc.target/s390/nobp-table-jump-z900.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ gcc/testsuite/gcc.target/s390/nobp-table-jump-z900.c	2018-03-27 09:33:58.837861529 -0600
56d343
@@ -0,0 +1,78 @@
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-O3 -march=z900 -mzarch --save-temps -mindirect-branch-jump=thunk -mindirect-branch-table" } */
56d343
+
56d343
+/* case-values-threshold will be set to 20 by the back-end when jump
56d343
+   thunk are requested.  */
56d343
+
56d343
+int __attribute__((noinline,noclone)) foo1 (void) { return 1; }
56d343
+int __attribute__((noinline,noclone)) foo2 (void) { return 2; }
56d343
+int __attribute__((noinline,noclone)) foo3 (void) { return 3; }
56d343
+int __attribute__((noinline,noclone)) foo4 (void) { return 4; }
56d343
+int __attribute__((noinline,noclone)) foo5 (void) { return 5; }
56d343
+int __attribute__((noinline,noclone)) foo6 (void) { return 6; }
56d343
+int __attribute__((noinline,noclone)) foo7 (void) { return 7; }
56d343
+int __attribute__((noinline,noclone)) foo8 (void) { return 8; }
56d343
+int __attribute__((noinline,noclone)) foo9 (void) { return 9; }
56d343
+int __attribute__((noinline,noclone)) foo10 (void) { return 10; }
56d343
+int __attribute__((noinline,noclone)) foo11 (void) { return 11; }
56d343
+int __attribute__((noinline,noclone)) foo12 (void) { return 12; }
56d343
+int __attribute__((noinline,noclone)) foo13 (void) { return 13; }
56d343
+int __attribute__((noinline,noclone)) foo14 (void) { return 14; }
56d343
+int __attribute__((noinline,noclone)) foo15 (void) { return 15; }
56d343
+int __attribute__((noinline,noclone)) foo16 (void) { return 16; }
56d343
+int __attribute__((noinline,noclone)) foo17 (void) { return 17; }
56d343
+int __attribute__((noinline,noclone)) foo18 (void) { return 18; }
56d343
+int __attribute__((noinline,noclone)) foo19 (void) { return 19; }
56d343
+int __attribute__((noinline,noclone)) foo20 (void) { return 20; }
56d343
+
56d343
+
56d343
+int __attribute__((noinline,noclone))
56d343
+bar (int a)
56d343
+{
56d343
+  int ret = 0;
56d343
+
56d343
+  switch (a)
56d343
+    {
56d343
+    case 1: ret = foo1 (); break;
56d343
+    case 2: ret = foo2 (); break;
56d343
+    case 3: ret = foo3 (); break;
56d343
+    case 4: ret = foo4 (); break;
56d343
+    case 5: ret = foo5 (); break;
56d343
+    case 6: ret = foo6 (); break;
56d343
+    case 7: ret = foo7 (); break;
56d343
+    case 8: ret = foo8 (); break;
56d343
+    case 9: ret = foo9 (); break;
56d343
+    case 10: ret = foo10 (); break;
56d343
+    case 11: ret = foo11 (); break;
56d343
+    case 12: ret = foo12 (); break;
56d343
+    case 13: ret = foo13 (); break;
56d343
+    case 14: ret = foo14 (); break;
56d343
+    case 15: ret = foo15 (); break;
56d343
+    case 16: ret = foo16 (); break;
56d343
+    case 17: ret = foo17 (); break;
56d343
+    case 18: ret = foo18 (); break;
56d343
+    case 19: ret = foo19 (); break;
56d343
+    case 20: ret = foo20 (); break;
56d343
+    default:
56d343
+      __builtin_abort ();
56d343
+    }
56d343
+
56d343
+  return ret;
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  if (bar (3) != 3)
56d343
+    __builtin_abort ();
56d343
+
56d343
+  return 0;
56d343
+}
56d343
+
56d343
+/* 1 x bar
56d343
+/* { dg-final { scan-assembler-times "ex\t" 1 } } */
56d343
+
56d343
+/* { dg-final { scan-assembler     "section\t.s390_indirect_jump" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_indirect_call" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_fromreg" } } */
56d343
+/* { dg-final { scan-assembler-not "section\t.s390_return_frommem" } } */