Blame SOURCES/gcc48-rh1535655-2.patch

fab8f6
commit 60281b40f9b28b1b1b912f3157547d6b4f50669c
fab8f6
Author: root <root@lenovo-x3950-01.khw.lab.eng.bos.redhat.com>
fab8f6
Date:   Thu Jan 18 17:50:46 2018 -0500
fab8f6
fab8f6
    HJ patch #1
fab8f6
fab8f6
diff --git a/gcc/config/i386/i386-opts.h b/gcc/config/i386/i386-opts.h
fab8f6
index 11c0845..139d23c 100644
fab8f6
--- a/gcc/config/i386/i386-opts.h
fab8f6
+++ b/gcc/config/i386/i386-opts.h
fab8f6
@@ -85,4 +85,16 @@ enum ix86_veclibabi {
fab8f6
   ix86_veclibabi_type_acml
fab8f6
 };
fab8f6
 
fab8f6
+/* This is used to mitigate variant #2 of the speculative execution
fab8f6
+   vulnerabilities on x86 processors identified by CVE-2017-5715, aka
fab8f6
+   Spectre.  They convert indirect branches and function returns to
fab8f6
+   call and return thunks to avoid speculative execution via indirect
fab8f6
+   call, jmp and ret.  */
fab8f6
+enum indirect_branch {
fab8f6
+  indirect_branch_unset = 0,
fab8f6
+  indirect_branch_keep,
fab8f6
+  indirect_branch_thunk,
fab8f6
+  indirect_branch_thunk_inline,
fab8f6
+  indirect_branch_thunk_extern
fab8f6
+};
fab8f6
 #endif
fab8f6
diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h
fab8f6
index 96e7c5c..ecdf108 100644
fab8f6
--- a/gcc/config/i386/i386-protos.h
fab8f6
+++ b/gcc/config/i386/i386-protos.h
fab8f6
@@ -306,6 +306,7 @@ extern enum attr_cpu ix86_schedule;
fab8f6
 #endif
fab8f6
 
fab8f6
 extern const char * ix86_output_call_insn (rtx insn, rtx call_op);
fab8f6
+extern const char * ix86_output_indirect_jmp (rtx call_op, bool ret_p);
fab8f6
 
fab8f6
 #ifdef RTX_CODE
fab8f6
 /* Target data for multipass lookahead scheduling.
fab8f6
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
fab8f6
index b91a456..ebc9a90 100644
fab8f6
--- a/gcc/config/i386/i386.c
fab8f6
+++ b/gcc/config/i386/i386.c
fab8f6
@@ -2572,12 +2572,23 @@ struct rtl_opt_pass pass_insert_vzeroupper =
fab8f6
  }
fab8f6
 };
fab8f6
 
fab8f6
-/* Return true if a red-zone is in use.  */
fab8f6
+/* Return true if a red-zone is in use.  We can't use red-zone when
fab8f6
+   there are local indirect jumps, like "indirect_jump" or "tablejump",
fab8f6
+   which jumps to another place in the function, since "call" in the
fab8f6
+   indirect thunk pushes the return address onto stack, destroying
fab8f6
+   red-zone.
fab8f6
+
fab8f6
+   TODO: If we can reserve the first 2 WORDs, for PUSH and, another
fab8f6
+   for CALL, in red-zone, we can allow local indirect jumps with
fab8f6
+   indirect thunk.  */
fab8f6
 
fab8f6
 static inline bool
fab8f6
 ix86_using_red_zone (void)
fab8f6
 {
fab8f6
-  return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
fab8f6
+  return (TARGET_RED_ZONE
fab8f6
+	  && !TARGET_64BIT_MS_ABI
fab8f6
+	  && (!cfun->machine->has_local_indirect_jump
fab8f6
+	      || cfun->machine->indirect_branch_type == indirect_branch_keep));
fab8f6
 }
fab8f6
 
fab8f6
 /* Return a string that documents the current -m options.  The caller is
fab8f6
@@ -4595,6 +4606,37 @@ ix86_can_inline_p (tree caller, tree callee)
fab8f6
 }
fab8f6
 
fab8f6
 
fab8f6
+/* Set the indirect_branch_type field from the function FNDECL.  */
fab8f6
+
fab8f6
+static void
fab8f6
+ix86_set_indirect_branch_type (tree fndecl)
fab8f6
+{
fab8f6
+  if (cfun->machine->indirect_branch_type == indirect_branch_unset)
fab8f6
+    {
fab8f6
+      tree attr = lookup_attribute ("indirect_branch",
fab8f6
+				    DECL_ATTRIBUTES (fndecl));
fab8f6
+      if (attr != NULL)
fab8f6
+	{
fab8f6
+	  tree args = TREE_VALUE (attr);
fab8f6
+	  if (args == NULL)
fab8f6
+	    gcc_unreachable ();
fab8f6
+	  tree cst = TREE_VALUE (args);
fab8f6
+	  if (strcmp (TREE_STRING_POINTER (cst), "keep") == 0)
fab8f6
+	    cfun->machine->indirect_branch_type = indirect_branch_keep;
fab8f6
+	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk") == 0)
fab8f6
+	    cfun->machine->indirect_branch_type = indirect_branch_thunk;
fab8f6
+	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-inline") == 0)
fab8f6
+	    cfun->machine->indirect_branch_type = indirect_branch_thunk_inline;
fab8f6
+	  else if (strcmp (TREE_STRING_POINTER (cst), "thunk-extern") == 0)
fab8f6
+	    cfun->machine->indirect_branch_type = indirect_branch_thunk_extern;
fab8f6
+	  else
fab8f6
+	    gcc_unreachable ();
fab8f6
+	}
fab8f6
+      else
fab8f6
+	cfun->machine->indirect_branch_type = ix86_indirect_branch;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
 /* Remember the last target of ix86_set_current_function.  */
fab8f6
 static GTY(()) tree ix86_previous_fndecl;
fab8f6
 
fab8f6
@@ -4609,6 +4651,9 @@ ix86_set_current_function (tree fndecl)
fab8f6
      slow things down too much or call target_reinit when it isn't safe.  */
fab8f6
   if (fndecl && fndecl != ix86_previous_fndecl)
fab8f6
     {
fab8f6
+      if (cfun && cfun->machine && fndecl)
fab8f6
+	ix86_set_indirect_branch_type (fndecl);
fab8f6
+
fab8f6
       tree old_tree = (ix86_previous_fndecl
fab8f6
 		       ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
fab8f6
 		       : NULL_TREE);
fab8f6
@@ -4637,6 +4682,8 @@ ix86_set_current_function (tree fndecl)
fab8f6
 	  target_reinit ();
fab8f6
 	}
fab8f6
     }
fab8f6
+  if (cfun && cfun->machine && fndecl)
fab8f6
+    ix86_set_indirect_branch_type (fndecl);
fab8f6
 }
fab8f6
 
fab8f6
 
fab8f6
@@ -8668,6 +8715,196 @@ ix86_setup_frame_addresses (void)
fab8f6
 # endif
fab8f6
 #endif
fab8f6
 
fab8f6
+/* Label count for call and return thunks.  It is used to make unique
fab8f6
+   labels in call and return thunks.  */
fab8f6
+static int indirectlabelno;
fab8f6
+
fab8f6
+/* True if call and return thunk functions are needed.  */
fab8f6
+static bool indirect_thunk_needed = false;
fab8f6
+
fab8f6
+/* Bit masks of integer registers, which contain branch target, used
fab8f6
+   by call and return thunks functions.  */
fab8f6
+static int indirect_thunks_used;
fab8f6
+
fab8f6
+#ifndef INDIRECT_LABEL
fab8f6
+# define INDIRECT_LABEL "LIND"
fab8f6
+#endif
fab8f6
+
fab8f6
+/* Fills in the label name that should be used for the indirect thunk.  */
fab8f6
+
fab8f6
+static void
fab8f6
+indirect_thunk_name (char name[32], int regno)
fab8f6
+{
fab8f6
+  if (USE_HIDDEN_LINKONCE)
fab8f6
+    {
fab8f6
+      if (regno >= 0)
fab8f6
+	{
fab8f6
+	  const char *reg_prefix;
fab8f6
+	  if (LEGACY_INT_REGNO_P (regno))
fab8f6
+	    reg_prefix = TARGET_64BIT ? "r" : "e";
fab8f6
+	  else
fab8f6
+	    reg_prefix = "";
fab8f6
+	  sprintf (name, "__x86_indirect_thunk_%s%s",
fab8f6
+		   reg_prefix, reg_names[regno]);
fab8f6
+	}
fab8f6
+      else
fab8f6
+	sprintf (name, "__x86_indirect_thunk");
fab8f6
+    }
fab8f6
+  else
fab8f6
+    {
fab8f6
+      if (regno >= 0)
fab8f6
+	ASM_GENERATE_INTERNAL_LABEL (name, "LITR", regno);
fab8f6
+      else
fab8f6
+	ASM_GENERATE_INTERNAL_LABEL (name, "LIT", 0);
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* Output a call and return thunk for indirect branch.  If REGNO != -1,
fab8f6
+   the function address is in REGNO and the call and return thunk looks like:
fab8f6
+
fab8f6
+	call	L2
fab8f6
+   L1:
fab8f6
+	pause
fab8f6
+	jmp	L1
fab8f6
+   L2:
fab8f6
+	mov	%REG, (%sp)
fab8f6
+	ret
fab8f6
+
fab8f6
+   Otherwise, the function address is on the top of stack and the
fab8f6
+   call and return thunk looks like:
fab8f6
+
fab8f6
+	call L2
fab8f6
+  L1:
fab8f6
+	pause
fab8f6
+	jmp L1
fab8f6
+  L2:
fab8f6
+	lea WORD_SIZE(%sp), %sp
fab8f6
+	ret
fab8f6
+ */
fab8f6
+
fab8f6
+static void
fab8f6
+output_indirect_thunk (int regno)
fab8f6
+{
fab8f6
+  char indirectlabel1[32];
fab8f6
+  char indirectlabel2[32];
fab8f6
+
fab8f6
+  ASM_GENERATE_INTERNAL_LABEL (indirectlabel1, INDIRECT_LABEL,
fab8f6
+			       indirectlabelno++);
fab8f6
+  ASM_GENERATE_INTERNAL_LABEL (indirectlabel2, INDIRECT_LABEL,
fab8f6
+			       indirectlabelno++);
fab8f6
+
fab8f6
+  /* Call */
fab8f6
+  fputs ("\tcall\t", asm_out_file);
fab8f6
+  assemble_name_raw (asm_out_file, indirectlabel2);
fab8f6
+  fputc ('\n', asm_out_file);
fab8f6
+
fab8f6
+  ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel1);
fab8f6
+
fab8f6
+  /* Pause + lfence.  */
fab8f6
+  fprintf (asm_out_file, "\tpause\n\tlfence\n");
fab8f6
+
fab8f6
+  /* Jump.  */
fab8f6
+  fputs ("\tjmp\t", asm_out_file);
fab8f6
+  assemble_name_raw (asm_out_file, indirectlabel1);
fab8f6
+  fputc ('\n', asm_out_file);
fab8f6
+
fab8f6
+  ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel2);
fab8f6
+
fab8f6
+  if (regno >= 0)
fab8f6
+    {
fab8f6
+      /* MOV.  */
fab8f6
+      rtx xops[2];
fab8f6
+      xops[0] = gen_rtx_MEM (word_mode, stack_pointer_rtx);
fab8f6
+      xops[1] = gen_rtx_REG (word_mode, regno);
fab8f6
+      output_asm_insn ("mov\t{%1, %0|%0, %1}", xops);
fab8f6
+    }
fab8f6
+  else
fab8f6
+    {
fab8f6
+      /* LEA.  */
fab8f6
+      rtx xops[2];
fab8f6
+      xops[0] = stack_pointer_rtx;
fab8f6
+      xops[1] = plus_constant (Pmode, stack_pointer_rtx, UNITS_PER_WORD);
fab8f6
+      output_asm_insn ("lea\t{%E1, %0|%0, %E1}", xops);
fab8f6
+    }
fab8f6
+
fab8f6
+  fputs ("\tret\n", asm_out_file);
fab8f6
+}
fab8f6
+
fab8f6
+/* Output a funtion with a call and return thunk for indirect branch.
fab8f6
+   If REGNO != -1, the function address is in REGNO.  Otherwise, the
fab8f6
+   function address is on the top of stack.  */
fab8f6
+
fab8f6
+static void
fab8f6
+output_indirect_thunk_function (int regno)
fab8f6
+{
fab8f6
+  char name[32];
fab8f6
+  tree decl;
fab8f6
+
fab8f6
+  /* Create __x86_indirect_thunk.  */
fab8f6
+  indirect_thunk_name (name, regno);
fab8f6
+  decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
fab8f6
+		     get_identifier (name),
fab8f6
+		     build_function_type_list (void_type_node, NULL_TREE));
fab8f6
+  DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
fab8f6
+				   NULL_TREE, void_type_node);
fab8f6
+  TREE_PUBLIC (decl) = 1;
fab8f6
+  TREE_STATIC (decl) = 1;
fab8f6
+  DECL_IGNORED_P (decl) = 1;
fab8f6
+
fab8f6
+#if TARGET_MACHO
fab8f6
+  if (TARGET_MACHO)
fab8f6
+    {
fab8f6
+      switch_to_section (darwin_sections[picbase_thunk_section]);
fab8f6
+      fputs ("\t.weak_definition\t", asm_out_file);
fab8f6
+      assemble_name (asm_out_file, name);
fab8f6
+      fputs ("\n\t.private_extern\t", asm_out_file);
fab8f6
+      assemble_name (asm_out_file, name);
fab8f6
+      putc ('\n', asm_out_file);
fab8f6
+      ASM_OUTPUT_LABEL (asm_out_file, name);
fab8f6
+      DECL_WEAK (decl) = 1;
fab8f6
+    }
fab8f6
+  else
fab8f6
+#endif
fab8f6
+    if (USE_HIDDEN_LINKONCE)
fab8f6
+      {
fab8f6
+	DECL_COMDAT (decl) = 1;
fab8f6
+	make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
fab8f6
+
fab8f6
+	targetm.asm_out.unique_section (decl, 0);
fab8f6
+	switch_to_section (get_named_section (decl, NULL, 0));
fab8f6
+
fab8f6
+	targetm.asm_out.globalize_label (asm_out_file, name);
fab8f6
+	fputs ("\t.hidden\t", asm_out_file);
fab8f6
+	assemble_name (asm_out_file, name);
fab8f6
+	putc ('\n', asm_out_file);
fab8f6
+	ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
fab8f6
+      }
fab8f6
+    else
fab8f6
+      {
fab8f6
+	switch_to_section (text_section);
fab8f6
+	ASM_OUTPUT_LABEL (asm_out_file, name);
fab8f6
+      }
fab8f6
+
fab8f6
+  DECL_INITIAL (decl) = make_node (BLOCK);
fab8f6
+  current_function_decl = decl;
fab8f6
+  allocate_struct_function (decl, false);
fab8f6
+  init_function_start (decl);
fab8f6
+  /* We're about to hide the function body from callees of final_* by
fab8f6
+     emitting it directly; tell them we're a thunk, if they care.  */
fab8f6
+  cfun->is_thunk = true;
fab8f6
+  first_function_block_is_cold = false;
fab8f6
+  /* Make sure unwind info is emitted for the thunk if needed.  */
fab8f6
+  final_start_function (emit_barrier (), asm_out_file, 1);
fab8f6
+
fab8f6
+  output_indirect_thunk (regno);
fab8f6
+
fab8f6
+  final_end_function ();
fab8f6
+  init_insn_lengths ();
fab8f6
+  free_after_compilation (cfun);
fab8f6
+  set_cfun (NULL);
fab8f6
+  current_function_decl = NULL;
fab8f6
+}
fab8f6
+
fab8f6
 static int pic_labels_used;
fab8f6
 
fab8f6
 /* Fills in the label name that should be used for a pc thunk for
fab8f6
@@ -8694,11 +8931,24 @@ ix86_code_end (void)
fab8f6
   rtx xops[2];
fab8f6
   int regno;
fab8f6
 
fab8f6
+  if (indirect_thunk_needed)
fab8f6
+    output_indirect_thunk_function (-1);
fab8f6
+
fab8f6
+  for (regno = FIRST_REX_INT_REG; regno <= LAST_REX_INT_REG; regno++)
fab8f6
+    {
fab8f6
+      int i = regno - FIRST_REX_INT_REG + LAST_INT_REG + 1;
fab8f6
+      if ((indirect_thunks_used & (1 << i)))
fab8f6
+	output_indirect_thunk_function (regno);
fab8f6
+    }
fab8f6
+
fab8f6
   for (regno = AX_REG; regno <= SP_REG; regno++)
fab8f6
     {
fab8f6
       char name[32];
fab8f6
       tree decl;
fab8f6
 
fab8f6
+      if ((indirect_thunks_used & (1 << regno)))
fab8f6
+	output_indirect_thunk_function (regno);
fab8f6
+
fab8f6
       if (!(pic_labels_used & (1 << regno)))
fab8f6
 	continue;
fab8f6
 
fab8f6
@@ -24074,12 +24324,250 @@ ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
fab8f6
   return call;
fab8f6
 }
fab8f6
 
fab8f6
+/* Output indirect branch via a call and return thunk.  CALL_OP is a
fab8f6
+   register which contains the branch target.  XASM is the assembly
fab8f6
+   template for CALL_OP.  Branch is a tail call if SIBCALL_P is true.
fab8f6
+   A normal call is converted to:
fab8f6
+
fab8f6
+	call __x86_indirect_thunk_reg
fab8f6
+
fab8f6
+   and a tail call is converted to:
fab8f6
+
fab8f6
+	jmp __x86_indirect_thunk_reg
fab8f6
+ */
fab8f6
+
fab8f6
+static void
fab8f6
+ix86_output_indirect_branch_via_reg (rtx call_op, bool sibcall_p)
fab8f6
+{
fab8f6
+  char thunk_name_buf[32];
fab8f6
+  char *thunk_name;
fab8f6
+  int regno = REGNO (call_op);
fab8f6
+
fab8f6
+  if (cfun->machine->indirect_branch_type
fab8f6
+      != indirect_branch_thunk_inline)
fab8f6
+    {
fab8f6
+      if (cfun->machine->indirect_branch_type == indirect_branch_thunk)
fab8f6
+	{
fab8f6
+	  int i = regno;
fab8f6
+	  if (i >= FIRST_REX_INT_REG)
fab8f6
+	    i -= (FIRST_REX_INT_REG - LAST_INT_REG - 1);
fab8f6
+	  indirect_thunks_used |= 1 << i;
fab8f6
+	}
fab8f6
+      indirect_thunk_name (thunk_name_buf, regno);
fab8f6
+      thunk_name = thunk_name_buf;
fab8f6
+    }
fab8f6
+  else
fab8f6
+    thunk_name = NULL;
fab8f6
+
fab8f6
+  if (sibcall_p)
fab8f6
+    {
fab8f6
+      if (thunk_name != NULL)
fab8f6
+	fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name);
fab8f6
+      else
fab8f6
+	output_indirect_thunk (regno);
fab8f6
+    }
fab8f6
+  else
fab8f6
+    {
fab8f6
+      if (thunk_name != NULL)
fab8f6
+	{
fab8f6
+	  fprintf (asm_out_file, "\tcall\t%s\n", thunk_name);
fab8f6
+	  return;
fab8f6
+	}
fab8f6
+
fab8f6
+      char indirectlabel1[32];
fab8f6
+      char indirectlabel2[32];
fab8f6
+
fab8f6
+      ASM_GENERATE_INTERNAL_LABEL (indirectlabel1,
fab8f6
+				   INDIRECT_LABEL,
fab8f6
+				   indirectlabelno++);
fab8f6
+      ASM_GENERATE_INTERNAL_LABEL (indirectlabel2,
fab8f6
+				   INDIRECT_LABEL,
fab8f6
+				   indirectlabelno++);
fab8f6
+
fab8f6
+      /* Jump.  */
fab8f6
+      fputs ("\tjmp\t", asm_out_file);
fab8f6
+      assemble_name_raw (asm_out_file, indirectlabel2);
fab8f6
+      fputc ('\n', asm_out_file);
fab8f6
+
fab8f6
+      ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel1);
fab8f6
+
fab8f6
+      if (thunk_name != NULL)
fab8f6
+	fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name);
fab8f6
+      else
fab8f6
+	output_indirect_thunk (regno);
fab8f6
+
fab8f6
+      ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel2);
fab8f6
+
fab8f6
+      /* Call.  */
fab8f6
+      fputs ("\tcall\t", asm_out_file);
fab8f6
+      assemble_name_raw (asm_out_file, indirectlabel1);
fab8f6
+      fputc ('\n', asm_out_file);
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* Output indirect branch via a call and return thunk.  CALL_OP is
fab8f6
+   the branch target.  XASM is the assembly template for CALL_OP.
fab8f6
+   Branch is a tail call if SIBCALL_P is true.  A normal call is
fab8f6
+   converted to:
fab8f6
+
fab8f6
+	jmp L2
fab8f6
+   L1:
fab8f6
+	push CALL_OP
fab8f6
+	jmp __x86_indirect_thunk
fab8f6
+   L2:
fab8f6
+	call L1
fab8f6
+
fab8f6
+   and a tail call is converted to:
fab8f6
+
fab8f6
+	push CALL_OP
fab8f6
+	jmp __x86_indirect_thunk
fab8f6
+ */
fab8f6
+
fab8f6
+static void
fab8f6
+ix86_output_indirect_branch_via_push (rtx call_op, const char *xasm,
fab8f6
+				      bool sibcall_p)
fab8f6
+{
fab8f6
+  char thunk_name_buf[32];
fab8f6
+  char *thunk_name;
fab8f6
+  char push_buf[64];
fab8f6
+  int regno = -1;
fab8f6
+
fab8f6
+  if (cfun->machine->indirect_branch_type
fab8f6
+      != indirect_branch_thunk_inline)
fab8f6
+    {
fab8f6
+      if (cfun->machine->indirect_branch_type == indirect_branch_thunk)
fab8f6
+	indirect_thunk_needed = true;
fab8f6
+      indirect_thunk_name (thunk_name_buf, regno);
fab8f6
+      thunk_name = thunk_name_buf;
fab8f6
+    }
fab8f6
+  else
fab8f6
+    thunk_name = NULL;
fab8f6
+
fab8f6
+  snprintf (push_buf, sizeof (push_buf), "push{%c}\t%s",
fab8f6
+	    TARGET_64BIT ? 'q' : 'l', xasm);
fab8f6
+
fab8f6
+  if (sibcall_p)
fab8f6
+    {
fab8f6
+      output_asm_insn (push_buf, &call_op);
fab8f6
+      if (thunk_name != NULL)
fab8f6
+	fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name);
fab8f6
+      else
fab8f6
+	output_indirect_thunk (regno);
fab8f6
+    }
fab8f6
+  else
fab8f6
+    {
fab8f6
+      char indirectlabel1[32];
fab8f6
+      char indirectlabel2[32];
fab8f6
+
fab8f6
+      ASM_GENERATE_INTERNAL_LABEL (indirectlabel1,
fab8f6
+				   INDIRECT_LABEL,
fab8f6
+				   indirectlabelno++);
fab8f6
+      ASM_GENERATE_INTERNAL_LABEL (indirectlabel2,
fab8f6
+				   INDIRECT_LABEL,
fab8f6
+				   indirectlabelno++);
fab8f6
+
fab8f6
+      /* Jump.  */
fab8f6
+      fputs ("\tjmp\t", asm_out_file);
fab8f6
+      assemble_name_raw (asm_out_file, indirectlabel2);
fab8f6
+      fputc ('\n', asm_out_file);
fab8f6
+
fab8f6
+      ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel1);
fab8f6
+
fab8f6
+      /* An external function may be called via GOT, instead of PLT.  */
fab8f6
+      if (MEM_P (call_op))
fab8f6
+	{
fab8f6
+	  struct ix86_address parts;
fab8f6
+	  rtx addr = XEXP (call_op, 0);
fab8f6
+	  if (ix86_decompose_address (addr, &parts)
fab8f6
+	      && parts.base == stack_pointer_rtx)
fab8f6
+	    {
fab8f6
+	      /* Since call will adjust stack by -UNITS_PER_WORD,
fab8f6
+		 we must convert "disp(stack, index, scale)" to
fab8f6
+		 "disp+UNITS_PER_WORD(stack, index, scale)".  */
fab8f6
+	      if (parts.index)
fab8f6
+		{
fab8f6
+		  addr = gen_rtx_MULT (Pmode, parts.index,
fab8f6
+				       GEN_INT (parts.scale));
fab8f6
+		  addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
fab8f6
+				       addr);
fab8f6
+		}
fab8f6
+	      else
fab8f6
+		addr = stack_pointer_rtx;
fab8f6
+
fab8f6
+	      rtx disp;
fab8f6
+	      if (parts.disp != NULL_RTX)
fab8f6
+		disp = plus_constant (Pmode, parts.disp,
fab8f6
+				      UNITS_PER_WORD);
fab8f6
+	      else
fab8f6
+		disp = GEN_INT (UNITS_PER_WORD);
fab8f6
+
fab8f6
+	      addr = gen_rtx_PLUS (Pmode, addr, disp);
fab8f6
+	      call_op = gen_rtx_MEM (GET_MODE (call_op), addr);
fab8f6
+	    }
fab8f6
+	}
fab8f6
+
fab8f6
+      output_asm_insn (push_buf, &call_op);
fab8f6
+
fab8f6
+      if (thunk_name != NULL)
fab8f6
+	fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name);
fab8f6
+      else
fab8f6
+	output_indirect_thunk (regno);
fab8f6
+
fab8f6
+      ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, indirectlabel2);
fab8f6
+
fab8f6
+      /* Call.  */
fab8f6
+      fputs ("\tcall\t", asm_out_file);
fab8f6
+      assemble_name_raw (asm_out_file, indirectlabel1);
fab8f6
+      fputc ('\n', asm_out_file);
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* Output indirect branch via a call and return thunk.  CALL_OP is
fab8f6
+   the branch target.  XASM is the assembly template for CALL_OP.
fab8f6
+   Branch is a tail call if SIBCALL_P is true.   */
fab8f6
+
fab8f6
+static void
fab8f6
+ix86_output_indirect_branch (rtx call_op, const char *xasm,
fab8f6
+			     bool sibcall_p)
fab8f6
+{
fab8f6
+  if (REG_P (call_op))
fab8f6
+    ix86_output_indirect_branch_via_reg (call_op, sibcall_p);
fab8f6
+  else
fab8f6
+    ix86_output_indirect_branch_via_push (call_op, xasm, sibcall_p);
fab8f6
+}
fab8f6
+/* Output indirect jump.  CALL_OP is the jump target.  Jump is a
fab8f6
+   function return if RET_P is true.  */
fab8f6
+
fab8f6
+const char *
fab8f6
+ix86_output_indirect_jmp (rtx call_op, bool ret_p)
fab8f6
+{
fab8f6
+  if (cfun->machine->indirect_branch_type != indirect_branch_keep)
fab8f6
+    {
fab8f6
+      struct ix86_frame frame;
fab8f6
+      ix86_compute_frame_layout (&frame);
fab8f6
+
fab8f6
+      /* We can't have red-zone if this isn't a function return since
fab8f6
+	 "call" in the indirect thunk pushes the return address onto
fab8f6
+	 stack, destroying red-zone.  */
fab8f6
+      if (!ret_p && frame.red_zone_size != 0)
fab8f6
+	gcc_unreachable ();
fab8f6
+
fab8f6
+      ix86_output_indirect_branch (call_op, "%0", true);
fab8f6
+      return "";
fab8f6
+    }
fab8f6
+  else
fab8f6
+    return "jmp\t%A0";
fab8f6
+}
fab8f6
+
fab8f6
 /* Output the assembly for a call instruction.  */
fab8f6
 
fab8f6
 const char *
fab8f6
 ix86_output_call_insn (rtx insn, rtx call_op)
fab8f6
 {
fab8f6
   bool direct_p = constant_call_address_operand (call_op, VOIDmode);
fab8f6
+  bool output_indirect_p
fab8f6
+    = (!TARGET_SEH
fab8f6
+       && cfun->machine->indirect_branch_type != indirect_branch_keep);
fab8f6
   bool seh_nop_p = false;
fab8f6
   const char *xasm;
fab8f6
 
fab8f6
@@ -24092,9 +24580,17 @@ ix86_output_call_insn (rtx insn, rtx call_op)
fab8f6
       else if (TARGET_SEH)
fab8f6
 	xasm = "rex.W jmp %A0";
fab8f6
       else
fab8f6
-	xasm = "jmp\t%A0";
fab8f6
+	{
fab8f6
+	  if (output_indirect_p)
fab8f6
+	    xasm = "%0";
fab8f6
+	  else
fab8f6
+	    xasm = "jmp\t%A0";
fab8f6
+	}
fab8f6
 
fab8f6
-      output_asm_insn (xasm, &call_op);
fab8f6
+      if (output_indirect_p && !direct_p)
fab8f6
+	ix86_output_indirect_branch (call_op, xasm, true);
fab8f6
+      else
fab8f6
+	output_asm_insn (xasm, &call_op);
fab8f6
       return "";
fab8f6
     }
fab8f6
 
fab8f6
@@ -24131,9 +24627,17 @@ ix86_output_call_insn (rtx insn, rtx call_op)
fab8f6
   if (direct_p)
fab8f6
     xasm = "call\t%P0";
fab8f6
   else
fab8f6
-    xasm = "call\t%A0";
fab8f6
+    {
fab8f6
+      if (output_indirect_p)
fab8f6
+	xasm = "%0";
fab8f6
+      else
fab8f6
+	xasm = "call\t%A0";
fab8f6
+    }
fab8f6
 
fab8f6
-  output_asm_insn (xasm, &call_op);
fab8f6
+  if (output_indirect_p && !direct_p)
fab8f6
+    ix86_output_indirect_branch (call_op, xasm, false);
fab8f6
+  else
fab8f6
+    output_asm_insn (xasm, &call_op);
fab8f6
 
fab8f6
   if (seh_nop_p)
fab8f6
     return "nop";
fab8f6
@@ -35436,7 +35940,7 @@ ix86_handle_struct_attribute (tree *node, tree name,
fab8f6
 
fab8f6
 static tree
fab8f6
 ix86_handle_fndecl_attribute (tree *node, tree name,
fab8f6
-                              tree args ATTRIBUTE_UNUSED,
fab8f6
+                              tree args,
fab8f6
                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
fab8f6
 {
fab8f6
   if (TREE_CODE (*node) != FUNCTION_DECL)
fab8f6
@@ -35445,6 +35949,29 @@ ix86_handle_fndecl_attribute (tree *node, tree name,
fab8f6
                name);
fab8f6
       *no_add_attrs = true;
fab8f6
     }
fab8f6
+
fab8f6
+  if (is_attribute_p ("indirect_branch", name))
fab8f6
+    {
fab8f6
+      tree cst = TREE_VALUE (args);
fab8f6
+      if (TREE_CODE (cst) != STRING_CST)
fab8f6
+	{
fab8f6
+	  warning (OPT_Wattributes,
fab8f6
+		   "%qE attribute requires a string constant argument",
fab8f6
+		   name);
fab8f6
+	  *no_add_attrs = true;
fab8f6
+	}
fab8f6
+      else if (strcmp (TREE_STRING_POINTER (cst), "keep") != 0
fab8f6
+	       && strcmp (TREE_STRING_POINTER (cst), "thunk") != 0
fab8f6
+	       && strcmp (TREE_STRING_POINTER (cst), "thunk-inline") != 0
fab8f6
+	       && strcmp (TREE_STRING_POINTER (cst), "thunk-extern") != 0)
fab8f6
+	{
fab8f6
+	  warning (OPT_Wattributes,
fab8f6
+		   "argument to %qE attribute is not "
fab8f6
+		   "(keep|thunk|thunk-inline|thunk-extern)", name);
fab8f6
+	  *no_add_attrs = true;
fab8f6
+	}
fab8f6
+    }
fab8f6
+
fab8f6
   return NULL_TREE;
fab8f6
 }
fab8f6
 
fab8f6
@@ -38963,6 +39490,8 @@ static const struct attribute_spec ix86_attribute_table[] =
fab8f6
     false },
fab8f6
   { "callee_pop_aggregate_return", 1, 1, false, true, true,
fab8f6
     ix86_handle_callee_pop_aggregate_return, true },
fab8f6
+  { "indirect_branch", 1, 1, true, false, false,
fab8f6
+    ix86_handle_fndecl_attribute, false },
fab8f6
   /* End element.  */
fab8f6
   { NULL,        0, 0, false, false, false, NULL, false }
fab8f6
 };
fab8f6
diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h
fab8f6
index 87fd381..8183cee7 100644
fab8f6
--- a/gcc/config/i386/i386.h
fab8f6
+++ b/gcc/config/i386/i386.h
fab8f6
@@ -2322,6 +2322,13 @@ struct GTY(()) machine_function {
fab8f6
      stack below the return address.  */
fab8f6
   BOOL_BITFIELD static_chain_on_stack : 1;
fab8f6
 
fab8f6
+  /* How to generate indirec branch.  */
fab8f6
+  ENUM_BITFIELD(indirect_branch) indirect_branch_type : 3;
fab8f6
+
fab8f6
+  /* If true, the current function has local indirect jumps, like
fab8f6
+     "indirect_jump" or "tablejump".  */
fab8f6
+  BOOL_BITFIELD has_local_indirect_jump : 1;
fab8f6
+
fab8f6
   /* During prologue/epilogue generation, the current frame state.
fab8f6
      Otherwise, the frame state at the end of the prologue.  */
fab8f6
   struct machine_frame_state fs;
fab8f6
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
fab8f6
index e09e961..b943849 100644
fab8f6
--- a/gcc/config/i386/i386.md
fab8f6
+++ b/gcc/config/i386/i386.md
fab8f6
@@ -11276,13 +11276,18 @@
fab8f6
 {
fab8f6
   if (TARGET_X32)
fab8f6
     operands[0] = convert_memory_address (word_mode, operands[0]);
fab8f6
+  cfun->machine->has_local_indirect_jump = true;
fab8f6
 })
fab8f6
 
fab8f6
 (define_insn "*indirect_jump"
fab8f6
   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rw"))]
fab8f6
   ""
fab8f6
-  "jmp\t%A0"
fab8f6
-  [(set_attr "type" "ibr")
fab8f6
+  "* return ix86_output_indirect_jmp (operands[0], false);"
fab8f6
+  [(set (attr "type")
fab8f6
+     (if_then_else (match_test "(cfun->machine->indirect_branch_type
fab8f6
+				 != indirect_branch_keep)")
fab8f6
+	(const_string "multi")
fab8f6
+	(const_string "ibr")))
fab8f6
    (set_attr "length_immediate" "0")])
fab8f6
 
fab8f6
 (define_expand "tablejump"
fab8f6
@@ -11324,14 +11329,19 @@
fab8f6
 
fab8f6
   if (TARGET_X32)
fab8f6
     operands[0] = convert_memory_address (word_mode, operands[0]);
fab8f6
+  cfun->machine->has_local_indirect_jump = true;
fab8f6
 })
fab8f6
 
fab8f6
 (define_insn "*tablejump_1"
fab8f6
   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rw"))
fab8f6
    (use (label_ref (match_operand 1)))]
fab8f6
   ""
fab8f6
-  "jmp\t%A0"
fab8f6
-  [(set_attr "type" "ibr")
fab8f6
+  "* return ix86_output_indirect_jmp (operands[0], false);"
fab8f6
+  [(set (attr "type")
fab8f6
+     (if_then_else (match_test "(cfun->machine->indirect_branch_type
fab8f6
+				 != indirect_branch_keep)")
fab8f6
+	(const_string "multi")
fab8f6
+	(const_string "ibr")))
fab8f6
    (set_attr "length_immediate" "0")])
fab8f6
 
fab8f6
 ;; Convert setcc + movzbl to xor + setcc if operands don't overlap.
fab8f6
@@ -11773,8 +11783,12 @@
fab8f6
   [(simple_return)
fab8f6
    (use (match_operand:SI 0 "register_operand" "r"))]
fab8f6
   "reload_completed"
fab8f6
-  "jmp\t%A0"
fab8f6
-  [(set_attr "type" "ibr")
fab8f6
+  "* return ix86_output_indirect_jmp (operands[0], true);"
fab8f6
+  [(set (attr "type")
fab8f6
+     (if_then_else (match_test "(cfun->machine->indirect_branch_type
fab8f6
+				 != indirect_branch_keep)")
fab8f6
+	(const_string "multi")
fab8f6
+	(const_string "ibr")))
fab8f6
    (set_attr "length_immediate" "0")])
fab8f6
 
fab8f6
 (define_insn "nop"
fab8f6
diff --git a/gcc/config/i386/i386.opt b/gcc/config/i386/i386.opt
fab8f6
index e93aa5a..0f6965a 100644
fab8f6
--- a/gcc/config/i386/i386.opt
fab8f6
+++ b/gcc/config/i386/i386.opt
fab8f6
@@ -630,3 +630,23 @@ Support RTM built-in functions and code generation
fab8f6
 mpku
fab8f6
 Target Report Mask(ISA_PKU) Var(ix86_isa_flags) Save
fab8f6
 Support PKU built-in functions and code generation
fab8f6
+
fab8f6
+mindirect-branch=
fab8f6
+Target Report RejectNegative Joined Enum(indirect_branch) Var(ix86_indirect_branch) Init(indirect_branch_keep)
fab8f6
+Convert indirect call and jump to call and return thunks.
fab8f6
+
fab8f6
+Enum
fab8f6
+Name(indirect_branch) Type(enum indirect_branch)
fab8f6
+Known indirect branch choices (for use with the -mindirect-branch= option):
fab8f6
+
fab8f6
+EnumValue
fab8f6
+Enum(indirect_branch) String(keep) Value(indirect_branch_keep)
fab8f6
+
fab8f6
+EnumValue
fab8f6
+Enum(indirect_branch) String(thunk) Value(indirect_branch_thunk)
fab8f6
+
fab8f6
+EnumValue
fab8f6
+Enum(indirect_branch) String(thunk-inline) Value(indirect_branch_thunk_inline)
fab8f6
+
fab8f6
+EnumValue
fab8f6
+Enum(indirect_branch) String(thunk-extern) Value(indirect_branch_thunk_extern)
fab8f6
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
fab8f6
index e495af5..847991c 100644
fab8f6
--- a/gcc/doc/extend.texi
fab8f6
+++ b/gcc/doc/extend.texi
fab8f6
@@ -3811,6 +3811,16 @@ Specify which floating-point unit to use.  The
fab8f6
 @code{target("fpmath=sse,387")} option must be specified as
fab8f6
 @code{target("fpmath=sse+387")} because the comma would separate
fab8f6
 different options.
fab8f6
+
fab8f6
+@item indirect_branch("@var{choice}")
fab8f6
+@cindex @code{indirect_branch} function attribute, x86
fab8f6
+On x86 targets, the @code{indirect_branch} attribute causes the compiler
fab8f6
+to convert indirect call and jump with @var{choice}.  @samp{keep}
fab8f6
+keeps indirect call and jump unmodified.  @samp{thunk} converts indirect
fab8f6
+call and jump to call and return thunk.  @samp{thunk-inline} converts
fab8f6
+indirect call and jump to inlined call and return thunk.
fab8f6
+@samp{thunk-extern} converts indirect call and jump to external call
fab8f6
+and return thunk provided in a separate object file.
fab8f6
 @end table
fab8f6
 
fab8f6
 On the PowerPC, the following options are allowed:
fab8f6
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
fab8f6
index 313a6c5..b299fbf 100644
fab8f6
--- a/gcc/doc/invoke.texi
fab8f6
+++ b/gcc/doc/invoke.texi
fab8f6
@@ -657,7 +657,8 @@ Objective-C and Objective-C++ Dialects}.
fab8f6
 -mcmodel=@var{code-model} -mabi=@var{name} -maddress-mode=@var{mode} @gol
fab8f6
 -m32 -m64 -mx32 -mlarge-data-threshold=@var{num} @gol
fab8f6
 -msse2avx -mfentry -m8bit-idiv @gol
fab8f6
--mavx256-split-unaligned-load -mavx256-split-unaligned-store}
fab8f6
+-mavx256-split-unaligned-load -mavx256-split-unaligned-store @gol
fab8f6
+-mindirect-branch=@var{choice}}
fab8f6
 
fab8f6
 @emph{i386 and x86-64 Windows Options}
fab8f6
 @gccoptlist{-mconsole -mcygwin -mno-cygwin -mdll @gol
fab8f6
@@ -14646,6 +14647,17 @@ to 255, 8-bit unsigned integer divide is used instead of
fab8f6
 @opindex avx256-split-unaligned-store
fab8f6
 Split 32-byte AVX unaligned load and store.
fab8f6
 
fab8f6
+@item -mindirect-branch=@var{choice}
fab8f6
+@opindex -mindirect-branch
fab8f6
+Convert indirect call and jump with @var{choice}.  The default is
fab8f6
+@samp{keep}, which keeps indirect call and jump unmodified.
fab8f6
+@samp{thunk} converts indirect call and jump to call and return thunk.
fab8f6
+@samp{thunk-inline} converts indirect call and jump to inlined call
fab8f6
+and return thunk.  @samp{thunk-extern} converts indirect call and jump
fab8f6
+to external call and return thunk provided in a separate object file.
fab8f6
+You can control this behavior for a specific function by using the
fab8f6
+function attribute @code{indirect_branch}.  @xref{Function Attributes}.
fab8f6
+
fab8f6
 @end table
fab8f6
 
fab8f6
 These @samp{-m} switches are supported in addition to the above
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-1.c
fab8f6
new file mode 100644
fab8f6
index 0000000..87f6dae
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-1.c
fab8f6
@@ -0,0 +1,22 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-2.c
fab8f6
new file mode 100644
fab8f6
index 0000000..6bc4f0a
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-2.c
fab8f6
@@ -0,0 +1,22 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-3.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-3.c
fab8f6
new file mode 100644
fab8f6
index 0000000..f20d35c
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-3.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-4.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-4.c
fab8f6
new file mode 100644
fab8f6
index 0000000..0eff8fb
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-4.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-7.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-7.c
fab8f6
new file mode 100644
fab8f6
index 0000000..afdb600
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-7.c
fab8f6
@@ -0,0 +1,44 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+void func0 (void);
fab8f6
+void func1 (void);
fab8f6
+void func2 (void);
fab8f6
+void func3 (void);
fab8f6
+void func4 (void);
fab8f6
+void func4 (void);
fab8f6
+void func5 (void);
fab8f6
+
fab8f6
+void
fab8f6
+bar (int i)
fab8f6
+{
fab8f6
+  switch (i)
fab8f6
+    {
fab8f6
+    default:
fab8f6
+      func0 ();
fab8f6
+      break;
fab8f6
+    case 1:
fab8f6
+      func1 ();
fab8f6
+      break;
fab8f6
+    case 2:
fab8f6
+      func2 ();
fab8f6
+      break;
fab8f6
+    case 3:
fab8f6
+      func3 ();
fab8f6
+      break;
fab8f6
+    case 4:
fab8f6
+      func4 ();
fab8f6
+      break;
fab8f6
+    case 5:
fab8f6
+      func5 ();
fab8f6
+      break;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*\.L\[0-9\]+\\(,%" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-1.c
fab8f6
new file mode 100644
fab8f6
index 0000000..efccdec
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-1.c
fab8f6
@@ -0,0 +1,25 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+extern void male_indirect_jump (long)
fab8f6
+  __attribute__ ((indirect_branch("thunk")));
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-2.c
fab8f6
new file mode 100644
fab8f6
index 0000000..ca3814e
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-2.c
fab8f6
@@ -0,0 +1,23 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk")))
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-3.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-3.c
fab8f6
new file mode 100644
fab8f6
index 0000000..97744d6
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-3.c
fab8f6
@@ -0,0 +1,23 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+extern int male_indirect_jump (long)
fab8f6
+  __attribute__ ((indirect_branch("thunk-inline")));
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-4.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-4.c
fab8f6
new file mode 100644
fab8f6
index 0000000..bfce3ea
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-4.c
fab8f6
@@ -0,0 +1,22 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk-inline")))
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-5.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-5.c
fab8f6
new file mode 100644
fab8f6
index 0000000..0833606
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-5.c
fab8f6
@@ -0,0 +1,22 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+extern int male_indirect_jump (long)
fab8f6
+  __attribute__ ((indirect_branch("thunk-extern")));
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-6.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-6.c
fab8f6
new file mode 100644
fab8f6
index 0000000..2eba0fb
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-6.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk-extern")))
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-7.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-7.c
fab8f6
new file mode 100644
fab8f6
index 0000000..f58427e
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-7.c
fab8f6
@@ -0,0 +1,44 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -fno-pic" } */
fab8f6
+
fab8f6
+void func0 (void);
fab8f6
+void func1 (void);
fab8f6
+void func2 (void);
fab8f6
+void func3 (void);
fab8f6
+void func4 (void);
fab8f6
+void func4 (void);
fab8f6
+void func5 (void);
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("thunk-extern")))
fab8f6
+void
fab8f6
+bar (int i)
fab8f6
+{
fab8f6
+  switch (i)
fab8f6
+    {
fab8f6
+    default:
fab8f6
+      func0 ();
fab8f6
+      break;
fab8f6
+    case 1:
fab8f6
+      func1 ();
fab8f6
+      break;
fab8f6
+    case 2:
fab8f6
+      func2 ();
fab8f6
+      break;
fab8f6
+    case 3:
fab8f6
+      func3 ();
fab8f6
+      break;
fab8f6
+    case 4:
fab8f6
+      func4 ();
fab8f6
+      break;
fab8f6
+    case 5:
fab8f6
+      func5 ();
fab8f6
+      break;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*\.L\[0-9\]+\\(,%" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-8.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-8.c
fab8f6
new file mode 100644
fab8f6
index 0000000..564ed39
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-attr-8.c
fab8f6
@@ -0,0 +1,42 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk -fno-pic" } */
fab8f6
+
fab8f6
+void func0 (void);
fab8f6
+void func1 (void);
fab8f6
+void func2 (void);
fab8f6
+void func3 (void);
fab8f6
+void func4 (void);
fab8f6
+void func4 (void);
fab8f6
+void func5 (void);
fab8f6
+
fab8f6
+__attribute__ ((indirect_branch("keep")))
fab8f6
+void
fab8f6
+bar (int i)
fab8f6
+{
fab8f6
+  switch (i)
fab8f6
+    {
fab8f6
+    default:
fab8f6
+      func0 ();
fab8f6
+      break;
fab8f6
+    case 1:
fab8f6
+      func1 ();
fab8f6
+      break;
fab8f6
+    case 2:
fab8f6
+      func2 ();
fab8f6
+      break;
fab8f6
+    case 3:
fab8f6
+      func3 ();
fab8f6
+      break;
fab8f6
+    case 4:
fab8f6
+      func4 ();
fab8f6
+      break;
fab8f6
+    case 5:
fab8f6
+      func5 ();
fab8f6
+      break;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-1.c
fab8f6
new file mode 100644
fab8f6
index 0000000..7fd01d6
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-1.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-2.c
fab8f6
new file mode 100644
fab8f6
index 0000000..825f6b2
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-2.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-3.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-3.c
fab8f6
new file mode 100644
fab8f6
index 0000000..395634e
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-3.c
fab8f6
@@ -0,0 +1,20 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-4.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-4.c
fab8f6
new file mode 100644
fab8f6
index 0000000..fd3f633
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-4.c
fab8f6
@@ -0,0 +1,20 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 1 { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-7.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-7.c
fab8f6
new file mode 100644
fab8f6
index 0000000..6652523
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-extern-7.c
fab8f6
@@ -0,0 +1,43 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-extern -fno-pic" } */
fab8f6
+
fab8f6
+void func0 (void);
fab8f6
+void func1 (void);
fab8f6
+void func2 (void);
fab8f6
+void func3 (void);
fab8f6
+void func4 (void);
fab8f6
+void func4 (void);
fab8f6
+void func5 (void);
fab8f6
+
fab8f6
+void
fab8f6
+bar (int i)
fab8f6
+{
fab8f6
+  switch (i)
fab8f6
+    {
fab8f6
+    default:
fab8f6
+      func0 ();
fab8f6
+      break;
fab8f6
+    case 1:
fab8f6
+      func1 ();
fab8f6
+      break;
fab8f6
+    case 2:
fab8f6
+      func2 ();
fab8f6
+      break;
fab8f6
+    case 3:
fab8f6
+      func3 ();
fab8f6
+      break;
fab8f6
+    case 4:
fab8f6
+      func4 ();
fab8f6
+      break;
fab8f6
+    case 5:
fab8f6
+      func5 ();
fab8f6
+      break;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*\.L\[0-9\]+\\(,%" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_indirect_thunk_(r|e)ax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler-not {\t(lfence|pause)} } } */
fab8f6
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-1.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-1.c
fab8f6
new file mode 100644
fab8f6
index 0000000..48c4dd4
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-1.c
fab8f6
@@ -0,0 +1,23 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-2.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-2.c
fab8f6
new file mode 100644
fab8f6
index 0000000..355dad5
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-2.c
fab8f6
@@ -0,0 +1,23 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+void
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+}
fab8f6
+
fab8f6
+/* Our gcc-4.8 based compiler is not as aggressive at sibcalls
fab8f6
+   where the target is in a MEM.  Thus we have to scan for different
fab8f6
+   patterns here than in newer compilers.  */
fab8f6
+/* { dg-final { scan-assembler "mov(?:l|q)\[ \t\]*_?dispatch" } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-3.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-3.c
fab8f6
new file mode 100644
fab8f6
index 0000000..244fec7
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-3.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch;
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch(offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times {\tpause} 1 } } */
fab8f6
+/* { dg-final { scan-assembler-times {\tlfence} 1 } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-4.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-4.c
fab8f6
new file mode 100644
fab8f6
index 0000000..107ebe3
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-4.c
fab8f6
@@ -0,0 +1,21 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -fno-pic" } */
fab8f6
+
fab8f6
+typedef void (*dispatch_t)(long offset);
fab8f6
+
fab8f6
+dispatch_t dispatch[256];
fab8f6
+
fab8f6
+int
fab8f6
+male_indirect_jump (long offset)
fab8f6
+{
fab8f6
+  dispatch[offset](offset);
fab8f6
+  return 0;
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*_?dispatch" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-times "jmp\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times "call\[ \t\]*\.LIND" 2 } } */
fab8f6
+/* { dg-final { scan-assembler-times {\tpause} 1 } } */
fab8f6
+/* { dg-final { scan-assembler-times {\tlfence} 1 } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
diff --git a/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-7.c b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-7.c
fab8f6
new file mode 100644
fab8f6
index 0000000..d02b1dc
fab8f6
--- /dev/null
fab8f6
+++ b/gcc/testsuite/gcc.target/i386/indirect-thunk-inline-7.c
fab8f6
@@ -0,0 +1,44 @@
fab8f6
+/* { dg-do compile } */
fab8f6
+/* { dg-options "-O2 -mindirect-branch=thunk-inline -fno-pic" } */
fab8f6
+
fab8f6
+void func0 (void);
fab8f6
+void func1 (void);
fab8f6
+void func2 (void);
fab8f6
+void func3 (void);
fab8f6
+void func4 (void);
fab8f6
+void func4 (void);
fab8f6
+void func5 (void);
fab8f6
+
fab8f6
+void
fab8f6
+bar (int i)
fab8f6
+{
fab8f6
+  switch (i)
fab8f6
+    {
fab8f6
+    default:
fab8f6
+      func0 ();
fab8f6
+      break;
fab8f6
+    case 1:
fab8f6
+      func1 ();
fab8f6
+      break;
fab8f6
+    case 2:
fab8f6
+      func2 ();
fab8f6
+      break;
fab8f6
+    case 3:
fab8f6
+      func3 ();
fab8f6
+      break;
fab8f6
+    case 4:
fab8f6
+      func4 ();
fab8f6
+      break;
fab8f6
+    case 5:
fab8f6
+      func5 ();
fab8f6
+      break;
fab8f6
+    }
fab8f6
+}
fab8f6
+
fab8f6
+/* { dg-final { scan-assembler "push(?:l|q)\[ \t\]*\.L\[0-9\]+\\(,%" { target { ! x32 } } } } */
fab8f6
+/* { dg-final { scan-assembler-not "pushq\[ \t\]%rax" { target x32 } } } */
fab8f6
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
fab8f6
+/* { dg-final { scan-assembler {\tpause} } } */
fab8f6
+/* { dg-final { scan-assembler {\tlfence} } } */
fab8f6
+/* { dg-final { scan-assembler-not "__x86_indirect_thunk" } } */