diff mbox

User directed Function Multiversioning via Function Overloading (issue5752064)

Message ID CAAs8HmyKPG_LxxxP2ngkGTFAKUPXQDJsbbsrOB_oAP0ViyER3g@mail.gmail.com
State New
Headers show

Commit Message

Sriraman Tallam Aug. 25, 2012, 12:34 a.m. UTC
Hi Jason,

   I have created a new patch to use target hooks for all the
functionality and make the front-end just call the target hooks at the
appropriate places. This is more like what you suggested in a previous
mail. In particular, target hooks address the following questions:

* Determine if two function decls with the same signature are versions.
* Determine the new assembler name of a function version.
* Generate the dispatcher function for a set of function versions.
* Compare versions to see if one has a higher priority over the other.

Patch attached and also available for review at:

http://codereview.appspot.com/5752064/

Hope this is more along the lines of what you had in mind, please let
me know what you think.

Thanks,
-Sri.


On Mon, Jul 30, 2012 at 12:01 PM, Sriraman Tallam <tmsriram@google.com> wrote:
> On Thu, Jul 19, 2012 at 1:39 PM, Jason Merrill <jason@redhat.com> wrote:
>>
>> On 07/10/2012 03:14 PM, Sriraman Tallam wrote:
>>>
>>> I am using the questions you asked previously
>>> to explain how I solved each of them. When working on this patch, these
>>> are the exact questions I had and tried to address it.
>>>
>>> * Does this attribute affect a function signature?
>>>
>>> The function signature should be changed when there is more than one
>>> definition/declaration of foo distinguished by unique target attributes.
>>
>> >[...]
>>
>> I agree.  I was trying to suggest that these questions are what the front end needs to care about, not about versioning specifically.  If these questions are turned into target hooks, all of the logic specific to versioning can be contained in the target.
>>
>> My only question intended to be answered by humans is, do people think moving the versioning logic behind more generic target hooks is worthwhile?
>
> I have  some comments related
>
> For the example below,
>
> // Default version.
> int foo ()
> {
>   .....
> }
>
> // Version  XXX feature supported by Target ABC.
> int foo __attribute__ ((target ("XXX")))
> {
>    ....
> }
>
> How should the second version of foo be treated for targets where
> feature XXX is not supported? Right now, I am working on having my
> patch completely ignore such function versions when compiled for
> targets that do not understand the attribute. I could move this check
> into a generic target hook so that a function definition that does not
> make sense for the current target is ignored.
>
> Also, currently the patch uses target hooks to do the following:
>
> - Find if a particular version can be called directly, rather than go
> through the dispatcher.
> - Determine what the dispatcher body should be.
> - Determining the order in which function versions must be dispatched.
>
> I do not have a strong opinion on whether the entire logic should be
> based on target hooks.
>
> Thanks,
> -Sri.
>
>>
>>
>>
>> Jason
Overview of the patch which adds support to specify function versions.  This is
only enabled for target i386.

Example:

int foo ();  /* Default version */
int foo () __attribute__ ((target("avx,popcnt")));/*Specialized for avx and popcnt */
int foo () __attribute__ ((target("arch=core2,ssse3")));/*Specialized for core2 and ssse3*/

int main ()
{
 int (*p)() = &foo;
 return foo () + (*p)();
}

int foo ()
{
 return 0;
}

int __attribute__ ((target("avx,popcnt")))
foo ()
{
 return 0;
}

int __attribute__ ((target("arch=core2,ssse3")))
foo ()
{
 return 0;
}

The above example has foo defined 3 times, but all 3 definitions of foo are
different versions of the same function. The call to foo in main, directly and
via a pointer, are calls to the multi-versioned function foo which is dispatched
to the right foo at run-time.

Front-end changes:

The front-end changes are calls at appropriate places to target hooks that
determine the following:

* Determine if two function decls with the same signature are versions.
* Determine the assembler name of a function version.
* Generate the dispatcher function for a set of function versions.
* Compare versions to see if one has a higher priority over the other.

All the implementation happens in the target-specific config/i386/i386.c.

What does the patch do?

* Tracking decls that correspond to function versions of function
name, say "foo":

When the front-end sees more than one decl for "foo", it calls a target hook to
determine if they are versions. To prevent duplicate definition errors with other
 versions of "foo", "decls_match" function in cp/decl.c is made to return false
 when 2 decls have are deemed versions by the target. This will make all function
versions of "foo" to be added to the overload list of "foo".

* Change the assembler names of the function versions.

For i386, the target changes the assembler names of the function versions by
 suffixing the sorted list of args to "target" to the function name of "foo". For
example, he assembler name of "void foo () __attribute__ ((target ("sse4")))" will
become _Z3foov.sse4.

* Overload resolution:

 Function "build_over_call" in cp/call.c sees a call to function
"foo", which is multi-versioned. The overload resolution happens in
function "joust" in "cp/call.c". Here, the call to "foo" has all
possible versions of "foo" as candidates. All the candidates of "foo" are
stored in the cgraph data structures. Each version of foo is chained in a 
doubly-linked list with the default function as the first element.  This allows
any pass to access all the semantically identical versions. A call to a
multi-versioned function will be replaced by a call to a dispatcher function,
determined by a target hook, to execute the right function version at run-time.

Optimization to directly call a version when possible:
Also, in joust, where overload resolution happens, a multiversioned function
resolution is made to return the most specialized version.  This is the version
that will be checked for dispatching first and is determined by the target.
Now, if the caller can inline this function version then a direct call is made
to this function version rather than go through the dispatcher. When a direct
call cannot be made, a call to the dispatcher function is created.

* Creating the dispatcher body.

The dispatcher body, called the resolver is made only when there is a call to a
multiversioned function dispatcher or the address of a function is taken. This
is generated during build_cgraph_edges for a call or cgraph_mark_address_taken
for a pointer reference. This is done by another target hook.

* Dispatch ordering.

The order in which the function versions are checked during dispatch is based
on a priority value assigned for the ISA that is catered. More specialized 
versions are checked for dispatching first.  This is to mitigate the ambiguity
that can arise when more than one function version is valid for execution on
a particular platform.  This is not a perfect solution and in future, the user
should be allowed to assign a dispatching priority value to each version.



	* doc/tm.texi.in (TARGET_OPTION_FUNCTION_VERSIONS): New hook description.
	* (TARGET_COMPARE_VERSION_PRIORITY): New hook description.
	* (TARGET_SET_VERSION_ASSEMBLER_NAME): New hook description.
	* (TARGET_GET_FUNCTION_VERSIONS_DISPATCHER): New hook description.
	* (TARGET_GENERATE_VERSION_DISPATCHER_BODY): New hook description.
	* doc/tm.texi: Regenerate.
	* cgraphbuild.c (build_cgraph_edges): Generate body of multiversion
	function dispatcher.
	* c-family/c-common.c (handle_target_attribute): Warn for invalid attributes.
	* target.def (compare_version_priority): New target hook.
	* (set_version_assembler_name): New target hook.
	* (generate_version_dispatcher_body): New target hook.
	* (get_function_versions_dispatcher): New target hook.
	* (function_versions): New target hook.
	* cgraph.c (cgraph_mark_address_taken_node): Generate body of multiversion
	function dispatcher.
	* cgraph.h (cgraph_node): New members version_dispatcher_decl,
	prev_function_version, next_function_version, dispatcher_function.
	* tree.h (DECL_FUNCTION_VERSIONED): New macro.
	(tree_function_decl): New bit-field versioned_function.
	* testsuite/g++.dg/mv1.C: New test.
	* testsuite/g++.dg/mv2.C: New test.
	* testsuite/g++.dg/mv3.C: New test.
	* testsuite/g++.dg/mv4.C: New test.
	* cp/class.c:
	(add_method): Change assembler names of function versions.
	(resolve_address_of_overloaded_function): Save all function
	version candidates. Create dispatcher decl and return address of
	dispatcher instead.
	* cp/decl.c (decls_match): Make decls unmatched for versioned
	functions.
	(duplicate_decls): Remove ambiguity for versioned functions. 
	(cxx_comdat_group): Make comdat group of versioned functions be the
	same.
	* cp/error.c (dump_exception_spec): Dump assembler name for function
	versions.
	* cp/semantics.c (expand_or_defer_fn_1): Mark as needed versioned
	functions that are also marked inline.
	* cp/decl2.c:(check_classfn): Check attributes of versioned functions
	for match.
	* cp/call.c: (build_new_function_call): Check if versioned functions
	have a default version.
	(build_over_call): Make calls to multiversioned functions
	to call the dispatcher.
	(joust): For calls to multi-versioned functions, make the most
	specialized function version win.
	(tourney): Generate dispatcher decl for function versions.
	* cp/mangle.c (write_unqualified_name): Use assembler name for
	versioned functions.
	* Makefile.in: Add multiversion.o
	* config/i386/i386.c (add_condition_to_bb): New function.
	(get_builtin_code_for_version): New function.
	(ix86_compare_version_priority): New function.
	(feature_compare): New function.
	(dispatch_function_versions): New function.
	* (ix86_function_versions):New function.
	* (attr_strcmp):New function.
	* (sorted_attr_string):New function.
	* (ix86_set_version_assembler_name):New function.
	* (make_name):New function.
	* (make_dispatcher_decl):New function.
	* (is_function_default_version):New function.
	* (ix86_get_function_versions_dispatcher):New function.
	* (make_attribute):New function.
	* (make_resolver_func):New function.
	* (ix86_generate_version_dispatcher_body):New function.
	* (TARGET_COMPARE_VERSION_PRIORITY):New macro.
	* (TARGET_SET_VERSION_ASSEMBLER_NAME):New macro.
	* (TARGET_GET_FUNCTION_VERSIONS_DISPATCHER):New macro.
	* (TARGET_GENERATE_VERSION_DISPATCHER_BODY):New macro.
	* (TARGET_OPTION_FUNCTION_VERSIONS):New macro.

Comments

Sriraman Tallam Sept. 18, 2012, 4:29 p.m. UTC | #1
Ping.

On Fri, Aug 24, 2012 at 5:34 PM, Sriraman Tallam <tmsriram@google.com> wrote:
> Hi Jason,
>
>    I have created a new patch to use target hooks for all the
> functionality and make the front-end just call the target hooks at the
> appropriate places. This is more like what you suggested in a previous
> mail. In particular, target hooks address the following questions:
>
> * Determine if two function decls with the same signature are versions.
> * Determine the new assembler name of a function version.
> * Generate the dispatcher function for a set of function versions.
> * Compare versions to see if one has a higher priority over the other.
>
> Patch attached and also available for review at:
>
> http://codereview.appspot.com/5752064/
>
> Hope this is more along the lines of what you had in mind, please let
> me know what you think.
>
> Thanks,
> -Sri.
>
>
> On Mon, Jul 30, 2012 at 12:01 PM, Sriraman Tallam <tmsriram@google.com> wrote:
>> On Thu, Jul 19, 2012 at 1:39 PM, Jason Merrill <jason@redhat.com> wrote:
>>>
>>> On 07/10/2012 03:14 PM, Sriraman Tallam wrote:
>>>>
>>>> I am using the questions you asked previously
>>>> to explain how I solved each of them. When working on this patch, these
>>>> are the exact questions I had and tried to address it.
>>>>
>>>> * Does this attribute affect a function signature?
>>>>
>>>> The function signature should be changed when there is more than one
>>>> definition/declaration of foo distinguished by unique target attributes.
>>>
>>> >[...]
>>>
>>> I agree.  I was trying to suggest that these questions are what the front end needs to care about, not about versioning specifically.  If these questions are turned into target hooks, all of the logic specific to versioning can be contained in the target.
>>>
>>> My only question intended to be answered by humans is, do people think moving the versioning logic behind more generic target hooks is worthwhile?
>>
>> I have  some comments related
>>
>> For the example below,
>>
>> // Default version.
>> int foo ()
>> {
>>   .....
>> }
>>
>> // Version  XXX feature supported by Target ABC.
>> int foo __attribute__ ((target ("XXX")))
>> {
>>    ....
>> }
>>
>> How should the second version of foo be treated for targets where
>> feature XXX is not supported? Right now, I am working on having my
>> patch completely ignore such function versions when compiled for
>> targets that do not understand the attribute. I could move this check
>> into a generic target hook so that a function definition that does not
>> make sense for the current target is ignored.
>>
>> Also, currently the patch uses target hooks to do the following:
>>
>> - Find if a particular version can be called directly, rather than go
>> through the dispatcher.
>> - Determine what the dispatcher body should be.
>> - Determining the order in which function versions must be dispatched.
>>
>> I do not have a strong opinion on whether the entire logic should be
>> based on target hooks.
>>
>> Thanks,
>> -Sri.
>>
>>>
>>>
>>>
>>> Jason
Xinliang David Li Oct. 5, 2012, 5:07 p.m. UTC | #2
Hi Jason, Sri has addressed the comments you had on FE part. Can you
take a look if it is ok?   Stage-1 is going to be closed soon, and we
hope to get this major feature in 4.8.

thanks,

David



On Tue, Sep 18, 2012 at 9:29 AM, Sriraman Tallam <tmsriram@google.com> wrote:
> Ping.
>
> On Fri, Aug 24, 2012 at 5:34 PM, Sriraman Tallam <tmsriram@google.com> wrote:
>> Hi Jason,
>>
>>    I have created a new patch to use target hooks for all the
>> functionality and make the front-end just call the target hooks at the
>> appropriate places. This is more like what you suggested in a previous
>> mail. In particular, target hooks address the following questions:
>>
>> * Determine if two function decls with the same signature are versions.
>> * Determine the new assembler name of a function version.
>> * Generate the dispatcher function for a set of function versions.
>> * Compare versions to see if one has a higher priority over the other.
>>
>> Patch attached and also available for review at:
>>
>> http://codereview.appspot.com/5752064/
>>
>> Hope this is more along the lines of what you had in mind, please let
>> me know what you think.
>>
>> Thanks,
>> -Sri.
>>
>>
>> On Mon, Jul 30, 2012 at 12:01 PM, Sriraman Tallam <tmsriram@google.com> wrote:
>>> On Thu, Jul 19, 2012 at 1:39 PM, Jason Merrill <jason@redhat.com> wrote:
>>>>
>>>> On 07/10/2012 03:14 PM, Sriraman Tallam wrote:
>>>>>
>>>>> I am using the questions you asked previously
>>>>> to explain how I solved each of them. When working on this patch, these
>>>>> are the exact questions I had and tried to address it.
>>>>>
>>>>> * Does this attribute affect a function signature?
>>>>>
>>>>> The function signature should be changed when there is more than one
>>>>> definition/declaration of foo distinguished by unique target attributes.
>>>>
>>>> >[...]
>>>>
>>>> I agree.  I was trying to suggest that these questions are what the front end needs to care about, not about versioning specifically.  If these questions are turned into target hooks, all of the logic specific to versioning can be contained in the target.
>>>>
>>>> My only question intended to be answered by humans is, do people think moving the versioning logic behind more generic target hooks is worthwhile?
>>>
>>> I have  some comments related
>>>
>>> For the example below,
>>>
>>> // Default version.
>>> int foo ()
>>> {
>>>   .....
>>> }
>>>
>>> // Version  XXX feature supported by Target ABC.
>>> int foo __attribute__ ((target ("XXX")))
>>> {
>>>    ....
>>> }
>>>
>>> How should the second version of foo be treated for targets where
>>> feature XXX is not supported? Right now, I am working on having my
>>> patch completely ignore such function versions when compiled for
>>> targets that do not understand the attribute. I could move this check
>>> into a generic target hook so that a function definition that does not
>>> make sense for the current target is ignored.
>>>
>>> Also, currently the patch uses target hooks to do the following:
>>>
>>> - Find if a particular version can be called directly, rather than go
>>> through the dispatcher.
>>> - Determine what the dispatcher body should be.
>>> - Determining the order in which function versions must be dispatched.
>>>
>>> I do not have a strong opinion on whether the entire logic should be
>>> based on target hooks.
>>>
>>> Thanks,
>>> -Sri.
>>>
>>>>
>>>>
>>>>
>>>> Jason
Jason Merrill Oct. 5, 2012, 5:43 p.m. UTC | #3
On 08/24/2012 08:34 PM, Sriraman Tallam wrote:
> +  /* If the address of a multiversioned function dispatcher is taken,
> +     generate the body to dispatch the right function at run-time.  This
> +     is needed as the address can be used to do an indirect call.  */

It seems to me that you don't need a dispatcher for doing indirect 
calls; you could just take the address of the version you would choose 
if you were doing a direct call.

The only reason for a dispatcher I can think of is if you want the 
address of a function to compare equal across translation units compiled 
with different target flags.  I'm not sure that's necessary; am I 
missing something?

Continuing to look at the patch.

Jason
Jason Merrill Oct. 5, 2012, 6:14 p.m. UTC | #4
On 10/05/2012 01:43 PM, Jason Merrill wrote:
> On 08/24/2012 08:34 PM, Sriraman Tallam wrote:
>> +  /* If the address of a multiversioned function dispatcher is taken,
>> +     generate the body to dispatch the right function at run-time.  This
>> +     is needed as the address can be used to do an indirect call.  */
>
> It seems to me that you don't need a dispatcher for doing indirect
> calls; you could just take the address of the version you would choose
> if you were doing a direct call.

Oh, I see you use the dispatcher for direct calls as well.  Why is that? 
  Why do you do direct calls when the function is inlineable, but not 
otherwise?

Jason
Jason Merrill Oct. 5, 2012, 6:32 p.m. UTC | #5
On 08/24/2012 08:34 PM, Sriraman Tallam wrote:
> +	  /* For function versions, their parms and types match
> +	     but they are not duplicates.  Record function versions
> +	     as and when they are found.  */
> +	  if (TREE_CODE (fn) == FUNCTION_DECL
> +	      && TREE_CODE (method) == FUNCTION_DECL
> +	      && (DECL_FUNCTION_SPECIFIC_TARGET (fn)
> +		  || DECL_FUNCTION_SPECIFIC_TARGET (method))
> +	      && targetm.target_option.function_versions (fn, method))
> + 	    {
> +	      targetm.set_version_assembler_name (fn);
> +	      targetm.set_version_assembler_name (method);
> +	      continue;
> +	    }

This seems like an odd place to be setting assembler names; better to 
just have the existing mangle_decl_assembler_name hook add the 
appropriate suffix when it's called normally.

> +	 Also, mark this function as needed if it is marked inline but
> +	 is a multi-versioned function.  */

Why?  If it's used, it should be marked needed though the normal process.

> +	    error_at (location_of (DECL_NAME (OVL_CURRENT (fn))),
> +		      "Call to multiversioned function %<%D(%A)%> with"
> +		      " no default version", DECL_NAME (OVL_CURRENT (fn)),
> +		      build_tree_list_vec (*args));

location_of just returns input_location if you ask it for the location 
of an identifier, so you might as well use error with no explicit 
location.  And why not print candidates->fn instead of pasting the 
name/args?  Also, lowercase "call".

> +    {
> +      tree dispatcher_decl = NULL;
> +      struct cgraph_node *node = cgraph_get_node (fn);
> +      if (node != NULL)
> +        dispatcher_decl = cgraph_get_node (fn)->version_dispatcher_decl;
> +      if (dispatcher_decl == NULL)
> +	{
> +	  error_at (input_location, "Call to multiversioned function"
> +		    " without a default is not allowed");
> +	  return NULL;
> +	}
> +      retrofit_lang_decl (dispatcher_decl);
> +      gcc_assert (dispatcher_decl != NULL);
> +      fn = dispatcher_decl;
> +    }

Let's move this logic into a separate function that returns the 
dispatcher function.

> +      /* Both functions must be marked versioned.  */
> +      gcc_assert (DECL_FUNCTION_VERSIONED (cand1->fn)
> +		  && DECL_FUNCTION_VERSIONED (cand2->fn));

Why can't you compare a versioned function and a non-versioned one?

The code in joust should go further down in the function, before the 
handling of two declarations of the same function.

> +  /* For multiversioned functions, aggregate all the versions here for
> +     generating the dispatcher body later if necessary.  */
> +
> +  if (TREE_CODE (candidates->fn) == FUNCTION_DECL
> +      && DECL_FUNCTION_VERSIONED (candidates->fn))
> +    {
> +      VEC (tree, heap) *fn_ver_vec = NULL;
> +      struct z_candidate *ver = candidates;
> +      fn_ver_vec = VEC_alloc (tree, heap, 2);
> +      for (;ver; ver = ver->next)
> +        VEC_safe_push (tree, heap, fn_ver_vec, ver->fn);
> +      gcc_assert (targetm.get_function_versions_dispatcher);
> +      targetm.get_function_versions_dispatcher (fn_ver_vec);
> +      VEC_free (tree, heap, fn_ver_vec);
> +    }

This seems to assume that all the functions in the list of candidates 
are versioned, but there might be unrelated functions from different 
namespaces.  Also, doing this every time someone calls a versioned 
function seems like the wrong place; I would think it would be better to 
build up a list of versions as you seed declarations, and then use that 
list to define the dispatcher at EOF if it's needed.

> +      if (TREE_CODE (decl) == FUNCTION_DECL
> +          && DECL_FUNCTION_VERSIONED (decl)
> +	  && DECL_ASSEMBLER_NAME_SET_P (decl))
> +	write_source_name (DECL_ASSEMBLER_NAME (decl));
> +      else
> +	write_source_name (DECL_NAME (decl));

Again, I think it's better to handle the suffix via 
mangle_decl_assembler_name.

Jason
Sriraman Tallam Oct. 5, 2012, 9:57 p.m. UTC | #6
On Fri, Oct 5, 2012 at 10:43 AM, Jason Merrill <jason@redhat.com> wrote:
> On 08/24/2012 08:34 PM, Sriraman Tallam wrote:
>>
>> +  /* If the address of a multiversioned function dispatcher is taken,
>> +     generate the body to dispatch the right function at run-time.  This
>>
>> +     is needed as the address can be used to do an indirect call.  */
>
>
> It seems to me that you don't need a dispatcher for doing indirect calls;
> you could just take the address of the version you would choose if you were
> doing a direct call.
>
> The only reason for a dispatcher I can think of is if you want the address
> of a function to compare equal across translation units compiled with
> different target flags.  I'm not sure that's necessary; am I missing
> something?

In general, the dispatcher is always necessary since it is not known
what function version will be called at compile time. This is true
whether it is a direct or an indirect call.

Example:
int foo() __attribute__(sse3)
{
}

int foo () __attribute__(sse4)
{
}

int main ()
{
   foo (); // The version of foo to be called is not known at compile
time. Needs dispatcher.
   int (*p)() = &foo; // What should be the value of p?
   (*p)(); // This needs a dispatcher too.
}

Now, since a dispatcher is necessary when the address of the function
is taken, I thought I could as well make it the address of the
function.

Thanks,
-Sri.

>
> Continuing to look at the patch.
>
> Jason
>
Jason Merrill Oct. 5, 2012, 10:50 p.m. UTC | #7
On 10/05/2012 05:57 PM, Sriraman Tallam wrote:
> In general, the dispatcher is always necessary since it is not known
> what function version will be called at compile time. This is true
> whether it is a direct or an indirect call.

So you want to compile with lowest common denominator flags and then 
choose a faster version at runtime based on the running configuration? 
I see.

Jason
Sriraman Tallam Oct. 5, 2012, 11:45 p.m. UTC | #8
On Fri, Oct 5, 2012 at 3:50 PM, Jason Merrill <jason@redhat.com> wrote:
> On 10/05/2012 05:57 PM, Sriraman Tallam wrote:
>>
>> In general, the dispatcher is always necessary since it is not known
>> what function version will be called at compile time. This is true
>> whether it is a direct or an indirect call.
>
>
> So you want to compile with lowest common denominator flags and then choose
> a faster version at runtime based on the running configuration? I see.
>

Yes.

Thanks,
-Sri.

> Jason
>
diff mbox

Patch

Index: gcc/doc/tm.texi
===================================================================
--- gcc/doc/tm.texi	(revision 190493)
+++ gcc/doc/tm.texi	(working copy)
@@ -9894,6 +9894,11 @@  changed via the optimize attribute or pragma, see
 @code{TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE}
 @end deftypefn
 
+@deftypefn {Target Hook} bool TARGET_OPTION_FUNCTION_VERSIONS (tree @var{decl1}, tree @var{decl2})
+This target hook returns @code{true} if @var{FN1} and @var{FN2} are
+versions of the same function.
+@end deftypefn
+
 @deftypefn {Target Hook} bool TARGET_CAN_INLINE_P (tree @var{caller}, tree @var{callee})
 This target hook returns @code{false} if the @var{caller} function
 cannot inline @var{callee}, based on target specific information.  By
@@ -10925,6 +10930,32 @@  The result is another tree containing a simplified
 call's result.  If @var{ignore} is true the value will be ignored.
 @end deftypefn
 
+@deftypefn {Target Hook} int TARGET_COMPARE_VERSION_PRIORITY (tree @var{decl1}, tree @var{decl2})
+This hook is used to compare the target attributes in two functions to
+figure out which function's features get higher priority.  This is used
+during multi-versioning to figure out the order of dispatching. @var{decl1}
+and @var{decl2} are the two function decls that will be compared.
+@end deftypefn
+
+@deftypefn {Target Hook} void TARGET_SET_VERSION_ASSEMBLER_NAME (tree @var{decl})
+This hook is for getting the new assembler name of a function that is
+a version.
+@end deftypefn
+
+@deftypefn {Target Hook} tree TARGET_GET_FUNCTION_VERSIONS_DISPATCHER (void *@var{arglist})
+This hook is used to get the dispatcher function for a set of function
+versions.  The dispatcher function is called to invoke the rignt function
+version at run-time. @var{arglist} is the vector of function versions
+that should be considered for dispatch.
+@end deftypefn
+
+@deftypefn {Target Hook} tree TARGET_GENERATE_VERSION_DISPATCHER_BODY (void *@var{arg})
+This hook is used to generate the dispatcher logic to invoke the right
+function version at runtime for a given set of function versions.
+@var{arg} points to the callgraph node of the dispatcher function whose
+body must be generated.
+@end deftypefn
+
 @deftypefn {Target Hook} {const char *} TARGET_INVALID_WITHIN_DOLOOP (const_rtx @var{insn})
 
 Take an instruction in @var{insn} and return NULL if it is valid within a
Index: gcc/doc/tm.texi.in
===================================================================
--- gcc/doc/tm.texi.in	(revision 190493)
+++ gcc/doc/tm.texi.in	(working copy)
@@ -9763,6 +9763,11 @@  changed via the optimize attribute or pragma, see
 @code{TARGET_OVERRIDE_OPTIONS_AFTER_CHANGE}
 @end deftypefn
 
+@hook TARGET_OPTION_FUNCTION_VERSIONS
+This target hook returns @code{true} if @var{FN1} and @var{FN2} are
+versions of the same function.
+@end deftypefn
+
 @hook TARGET_CAN_INLINE_P
 This target hook returns @code{false} if the @var{caller} function
 cannot inline @var{callee}, based on target specific information.  By
@@ -10783,6 +10788,32 @@  The result is another tree containing a simplified
 call's result.  If @var{ignore} is true the value will be ignored.
 @end deftypefn
 
+@hook TARGET_COMPARE_VERSION_PRIORITY
+This hook is used to compare the target attributes in two functions to
+figure out which function's features get higher priority.  This is used
+during multi-versioning to figure out the order of dispatching. @var{decl1}
+and @var{decl2} are the two function decls that will be compared.
+@end deftypefn
+
+@hook TARGET_SET_VERSION_ASSEMBLER_NAME
+This hook is for getting the new assembler name of a function that is
+a version.
+@end deftypefn
+
+@hook TARGET_GET_FUNCTION_VERSIONS_DISPATCHER
+This hook is used to get the dispatcher function for a set of function
+versions.  The dispatcher function is called to invoke the rignt function
+version at run-time. @var{arglist} is the vector of function versions
+that should be considered for dispatch.
+@end deftypefn
+
+@hook TARGET_GENERATE_VERSION_DISPATCHER_BODY
+This hook is used to generate the dispatcher logic to invoke the right
+function version at runtime for a given set of function versions.
+@var{arg} points to the callgraph node of the dispatcher function whose
+body must be generated.
+@end deftypefn
+
 @hook TARGET_INVALID_WITHIN_DOLOOP
 
 Take an instruction in @var{insn} and return NULL if it is valid within a
Index: gcc/cgraphbuild.c
===================================================================
--- gcc/cgraphbuild.c	(revision 190493)
+++ gcc/cgraphbuild.c	(working copy)
@@ -34,6 +34,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "ipa-utils.h"
 #include "except.h"
 #include "ipa-inline.h"
+#include "target.h"
 
 /* Context of record_reference.  */
 struct record_reference_ctx
@@ -288,7 +289,6 @@  mark_store (gimple stmt, tree t, void *data)
      }
   return false;
 }
-
 /* Create cgraph edges for function calls.
    Also look for functions and variables having addresses taken.  */
 
@@ -317,8 +317,21 @@  build_cgraph_edges (void)
 							 bb);
 	      decl = gimple_call_fndecl (stmt);
 	      if (decl)
-		cgraph_create_edge (node, cgraph_get_create_node (decl),
-				    stmt, bb->count, freq);
+		{
+		  struct cgraph_node *callee = cgraph_get_create_node (decl);
+	          /* If a call to a multiversioned function dispatcher is
+		     found, generate the body to dispatch the right function
+		     at run-time.  */
+		  if (callee->dispatcher_function)
+		    {
+		      tree resolver_decl;
+		      gcc_assert (callee->next_function_version);
+		      resolver_decl
+			 = targetm.generate_version_dispatcher_body (callee);
+		      gcc_assert (resolver_decl != NULL_TREE);
+		    }
+		  cgraph_create_edge (node, callee, stmt, bb->count, freq);
+	        }
 	      else
 		cgraph_create_indirect_edge (node, stmt,
 					     gimple_call_flags (stmt),
Index: gcc/c-family/c-common.c
===================================================================
--- gcc/c-family/c-common.c	(revision 190493)
+++ gcc/c-family/c-common.c	(working copy)
@@ -8502,9 +8502,22 @@  handle_target_attribute (tree *node, tree name, tr
       warning (OPT_Wattributes, "%qE attribute ignored", name);
       *no_add_attrs = true;
     }
-  else if (! targetm.target_option.valid_attribute_p (*node, name, args,
-						      flags))
-    *no_add_attrs = true;
+  else
+    {
+      /* When a target attribute is invalid, it may also be because the
+	 target for the compilation unit and the attribute match.  For
+         instance, target attribute "xxx" is invalid when -mxxx is used.
+         When used with multiversioning, removing the attribute will lead
+         to duplicate definitions if a default version is provided.
+	 So, generate a warning here and remove the attribute.  */
+      if (!targetm.target_option.valid_attribute_p (*node, name, args, flags))
+	{
+	  warning (OPT_Wattributes,
+		   "Invalid target attribute in function %qE, ignored.",
+		   *node);
+	  *no_add_attrs = true;
+	}
+    }
 
   return NULL_TREE;
 }
Index: gcc/target.def
===================================================================
--- gcc/target.def	(revision 190493)
+++ gcc/target.def	(working copy)
@@ -1298,6 +1298,39 @@  DEFHOOK
  tree, (tree fndecl, int n_args, tree *argp, bool ignore),
  hook_tree_tree_int_treep_bool_null)
 
+/* Target hook to compare the priority of target features in function DECL1
+   and DECL2.  It returns positive value if DECL1 is higher priority,
+   negative value if DECL2 is higher priority and 0 if they are the
+   same.  */
+DEFHOOK
+(compare_version_priority,
+ "",
+ int, (tree decl1, tree decl2), NULL)
+
+/* Target hook to get the new assembler name of DECL which is a function
+   version.  */
+
+DEFHOOK
+(set_version_assembler_name,
+ "",
+ void, (tree decl), NULL)
+
+/* Target hook to generate the dispatcher body for a function version
+   dispatcher ARG, which is a cgraph_node pointer.  */
+
+DEFHOOK
+(generate_version_dispatcher_body,
+ "",
+ tree, (void *arg), NULL) 
+
+/* Target hook to generate a function version dispatcher DECL for the list
+   of function versions in arglist, which is a vector of decls.  */
+
+DEFHOOK
+(get_function_versions_dispatcher,
+ "",
+ tree, (void *arglist), NULL)
+
 /* Returns a code for a target-specific builtin that implements
    reciprocal of the function, or NULL_TREE if not available.  */
 DEFHOOK
@@ -2705,6 +2738,14 @@  DEFHOOK
  void, (void),
  hook_void_void)
 
+/* Returns true if DECL1 and DECL2 are versions of the same function.  */
+
+DEFHOOK
+(function_versions,
+ "",
+ bool, (tree decl1, tree decl2),
+ hook_bool_tree_tree_false)
+
 /* Function to determine if one function can inline another function.  */
 #undef HOOK_PREFIX
 #define HOOK_PREFIX "TARGET_"
Index: gcc/cgraph.c
===================================================================
--- gcc/cgraph.c	(revision 190493)
+++ gcc/cgraph.c	(working copy)
@@ -1281,6 +1281,14 @@  cgraph_mark_address_taken_node (struct cgraph_node
   node->symbol.address_taken = 1;
   node = cgraph_function_or_thunk_node (node, NULL);
   node->symbol.address_taken = 1;
+  /* If the address of a multiversioned function dispatcher is taken,
+     generate the body to dispatch the right function at run-time.  This
+     is needed as the address can be used to do an indirect call.  */
+  if (node->dispatcher_function)
+    {
+      gcc_assert (node->next_function_version);
+      targetm.generate_version_dispatcher_body (node);
+    }
 }
 
 /* Return local info for the compiled function.  */
Index: gcc/cgraph.h
===================================================================
--- gcc/cgraph.h	(revision 190493)
+++ gcc/cgraph.h	(working copy)
@@ -220,6 +220,26 @@  struct GTY(()) cgraph_node {
   struct cgraph_node *prev_sibling_clone;
   struct cgraph_node *clones;
   struct cgraph_node *clone_of;
+
+  /* TODO: Put version_dispatcher_decl, prev_function_version,
+     next_function_version into a struct for readability.
+
+     If this node corresponds to a function version, this points
+     to the dispatcher function decl which is the function that must
+     be called to execute the right function version at run-time.
+
+     If this node is a dispatcher for function versions, this points
+     to resolver function, which holds  the function body for the
+     dispatcher.  */
+  tree version_dispatcher_decl;
+
+  /* Chains all the semantically identical function versions.  The
+     first function in this chain is the default function.  */
+  struct cgraph_node *prev_function_version;
+  /* If this node is a dispatcher for function versions, this also points
+     to the default function version.  */
+  struct cgraph_node *next_function_version;
+
   /* For functions with many calls sites it holds map from call expression
      to the edge to speed up cgraph_edge function.  */
   htab_t GTY((param_is (struct cgraph_edge))) call_site_hash;
@@ -271,6 +291,7 @@  struct GTY(()) cgraph_node {
   /* ?? We should be able to remove this.  We have enough bits in
      cgraph to calculate it.  */
   unsigned tm_clone : 1;
+  unsigned dispatcher_function : 1;
 };
 
 DEF_VEC_P(symtab_node);
@@ -648,6 +669,7 @@  void cgraph_rebuild_references (void);
 int compute_call_stmt_bb_frequency (tree, basic_block bb);
 void record_references_in_initializer (tree, bool);
 
+
 /* In ipa.c  */
 bool symtab_remove_unreachable_nodes (bool, FILE *);
 cgraph_node_set cgraph_node_set_new (void);
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	(revision 190493)
+++ gcc/tree.h	(working copy)
@@ -3436,6 +3436,12 @@  extern VEC(tree, gc) **decl_debug_args_insert (tre
 #define DECL_FUNCTION_SPECIFIC_OPTIMIZATION(NODE) \
    (FUNCTION_DECL_CHECK (NODE)->function_decl.function_specific_optimization)
 
+/* In FUNCTION_DECL, this is set if this function has other versions generated
+   using "target" attributes.  The default version is the one which does not
+   have any "target" attribute set. */
+#define DECL_FUNCTION_VERSIONED(NODE)\
+   (FUNCTION_DECL_CHECK (NODE)->function_decl.versioned_function)
+
 /* FUNCTION_DECL inherits from DECL_NON_COMMON because of the use of the
    arguments/result/saved_tree fields by front ends.   It was either inherit
    FUNCTION_DECL from non_common, or inherit non_common from FUNCTION_DECL,
@@ -3480,8 +3486,8 @@  struct GTY(()) tree_function_decl {
   unsigned looping_const_or_pure_flag : 1;
   unsigned has_debug_args_flag : 1;
   unsigned tm_clone_flag : 1;
-
-  /* 1 bit left */
+  unsigned versioned_function : 1;
+  /* No bits left.  */
 };
 
 /* The source language of the translation-unit.  */
Index: gcc/testsuite/g++.dg/mv2.C
===================================================================
--- gcc/testsuite/g++.dg/mv2.C	(revision 0)
+++ gcc/testsuite/g++.dg/mv2.C	(revision 0)
@@ -0,0 +1,119 @@ 
+/* Test case to check if Multiversioning chooses the correct
+   dispatching order when versions are for various ISAs.  */
+/* { dg-do run { target i?86-*-* x86_64-*-* } } */
+/* { dg-require-ifunc "" }  */
+/* { dg-options "-O2 -mno-sse -mno-mmx -mno-popcnt -mno-avx" } */
+
+#include <assert.h>
+
+/* Default version.  */
+int foo ();
+/* The dispatch checks should be in the exact reverse order of the
+   declarations below.  */
+int foo () __attribute__ ((target ("mmx")));
+int foo () __attribute__ ((target ("sse")));
+int foo () __attribute__ ((target ("sse2")));
+int foo () __attribute__ ((target ("sse3")));
+int foo () __attribute__ ((target ("ssse3")));
+int foo () __attribute__ ((target ("sse4.1")));
+int foo () __attribute__ ((target ("sse4.2")));
+int foo () __attribute__ ((target ("popcnt")));
+int foo () __attribute__ ((target ("avx")));
+int foo () __attribute__ ((target ("avx2")));
+
+int main ()
+{
+
+  int val = foo ();
+
+  if (__builtin_cpu_supports ("avx2"))
+    assert (val == 1);
+  else if (__builtin_cpu_supports ("avx"))
+    assert (val == 2);
+  else if (__builtin_cpu_supports ("popcnt"))
+    assert (val == 3);
+  else if (__builtin_cpu_supports ("sse4.2"))
+    assert (val == 4);
+  else if (__builtin_cpu_supports ("sse4.1"))
+    assert (val == 5);
+  else if (__builtin_cpu_supports ("ssse3"))
+    assert (val == 6);
+  else if (__builtin_cpu_supports ("sse3"))
+    assert (val == 7);
+  else if (__builtin_cpu_supports ("sse2"))
+    assert (val == 8);
+  else if (__builtin_cpu_supports ("sse"))
+    assert (val == 9);
+  else if (__builtin_cpu_supports ("mmx"))
+    assert (val == 10);
+  else
+    assert (val == 0);
+
+  return 0;
+}
+
+int
+foo ()
+{
+  return 0;
+}
+
+int __attribute__ ((target("mmx")))
+foo ()
+{
+  return 10;
+}
+
+int __attribute__ ((target("sse")))
+foo ()
+{
+  return 9;
+}
+
+int __attribute__ ((target("sse2")))
+foo ()
+{
+  return 8;
+}
+
+int __attribute__ ((target("sse3")))
+foo ()
+{
+  return 7;
+}
+
+int __attribute__ ((target("ssse3")))
+foo ()
+{
+  return 6;
+}
+
+int __attribute__ ((target("sse4.1")))
+foo ()
+{
+  return 5;
+}
+
+int __attribute__ ((target("sse4.2")))
+foo ()
+{
+  return 4;
+}
+
+int __attribute__ ((target("popcnt")))
+foo ()
+{
+  return 3;
+}
+
+int __attribute__ ((target("avx")))
+foo ()
+{
+  return 2;
+}
+
+int __attribute__ ((target("avx2")))
+foo ()
+{
+  return 1;
+}
Index: gcc/testsuite/g++.dg/mv4.C
===================================================================
--- gcc/testsuite/g++.dg/mv4.C	(revision 0)
+++ gcc/testsuite/g++.dg/mv4.C	(revision 0)
@@ -0,0 +1,23 @@ 
+/* Test case to check if the compiler generates an error message
+   when the default version of a multiversioned function is absent
+   and its pointer is taken.  */
+
+/* { dg-do compile { target i?86-*-* x86_64-*-* } } */
+/* { dg-options "-O2 -mno-sse -mno-popcnt" } */
+
+int __attribute__ ((target ("sse")))
+foo ()
+{
+  return 1;
+}
+int __attribute__ ((target ("popcnt")))
+foo ()
+{
+  return 0;
+}
+
+int main ()
+{
+  int (*p)() = &foo; /* { dg-error "Pointer to a multiversioned function without a default is not allowed" {} } */
+  return (*p)();
+}
Index: gcc/testsuite/g++.dg/mv1.C
===================================================================
--- gcc/testsuite/g++.dg/mv1.C	(revision 0)
+++ gcc/testsuite/g++.dg/mv1.C	(revision 0)
@@ -0,0 +1,130 @@ 
+/* Test case to check if Multiversioning works.  */
+/* { dg-do run { target i?86-*-* x86_64-*-* } } */
+/* { dg-require-ifunc "" }  */
+/* { dg-options "-O2 -fPIC -mno-avx" } */
+
+#include <assert.h>
+
+/* Default version.  */
+int foo ();
+/* The other versions of foo.  Mix up the ordering and 
+   check if the dispatching does it in the order of priority. */
+/* Check combination of target attributes.  */
+int foo () __attribute__ ((target("arch=corei7,popcnt")));
+/* The target operands in this declaration and the definition are re-ordered.
+   This should still work.  */
+int foo () __attribute__ ((target("ssse3,avx2")));
+
+/* Check for all target attributes for which dispatchers are available.  */
+/* Check arch= */
+int foo () __attribute__((target("arch=core2")));
+int foo () __attribute__((target("arch=corei7")));
+int foo () __attribute__((target("arch=atom")));
+/* Check ISAs  */
+int foo () __attribute__((target("avx")));
+int foo () __attribute__ ((target("arch=core2,sse4.2")));
+/* Check more arch=.  */
+int foo () __attribute__((target("arch=amdfam10")));
+int foo () __attribute__((target("arch=bdver1")));
+int foo () __attribute__((target("arch=bdver2")));
+
+int (*p)() = &foo;
+int main ()
+{
+  int val = foo ();
+  assert (val ==  (*p)());
+
+  /* Check in the exact same order in which the dispatching
+     is expected to happen.  */
+  if (__builtin_cpu_is ("bdver1"))
+    assert (val == 1);
+  else if (__builtin_cpu_is ("bdver2"))
+    assert (val == 2);
+  else if (__builtin_cpu_supports ("avx2")
+	   && __builtin_cpu_supports ("ssse3"))
+    assert (val == 3);
+  else if (__builtin_cpu_supports ("avx"))
+    assert (val == 4);
+  else if (__builtin_cpu_is ("corei7")
+	   && __builtin_cpu_supports ("popcnt"))
+    assert (val == 5);
+  else if (__builtin_cpu_is ("corei7"))
+    assert (val == 6);
+  else if (__builtin_cpu_is ("amdfam10h"))
+    assert (val == 7);
+  else if (__builtin_cpu_is ("core2")
+	   && __builtin_cpu_supports ("sse4.2"))
+    assert (val == 8);
+  else if (__builtin_cpu_is ("core2"))
+    assert (val == 9);
+  else if (__builtin_cpu_is ("atom"))
+    assert (val == 10);
+  else
+    assert (val == 0);
+  
+  return 0;
+}
+
+int foo ()
+{
+  return 0;
+}
+
+int __attribute__ ((target("arch=corei7,popcnt")))
+foo ()
+{
+  return 5;
+}
+int __attribute__ ((target("avx2,ssse3")))
+foo ()
+{
+  return 3;
+}
+
+int __attribute__ ((target("arch=core2")))
+foo ()
+{
+  return 9;
+}
+
+int __attribute__ ((target("arch=corei7")))
+foo ()
+{
+  return 6;
+}
+
+int __attribute__ ((target("arch=atom")))
+foo ()
+{
+  return 10;
+}
+
+int __attribute__ ((target("avx")))
+foo ()
+{
+  return 4;
+}
+
+int __attribute__ ((target("arch=core2,sse4.2")))
+foo ()
+{
+  return 8;
+}
+
+int __attribute__ ((target("arch=amdfam10")))
+foo ()
+{
+  return 7;
+}
+
+int __attribute__ ((target("arch=bdver1")))
+foo ()
+{
+  return 1;
+}
+
+int __attribute__ ((target("arch=bdver2")))
+foo ()
+{
+  return 2;
+}
Index: gcc/testsuite/g++.dg/mv3.C
===================================================================
--- gcc/testsuite/g++.dg/mv3.C	(revision 0)
+++ gcc/testsuite/g++.dg/mv3.C	(revision 0)
@@ -0,0 +1,37 @@ 
+/* Test case to check if a call to a multiversioned function
+   is replaced with a direct call to the particular version when
+   the most specialized version's target attributes match the
+   caller.  
+  
+   In this program, foo is multiversioned but there is no default
+   function.  This is an error if the call has to go through a
+   dispatcher.  However, the call to foo in bar can be replaced
+   with a direct call to the popcnt version of foo.  Hence, this
+   test should pass.  */
+
+/* { dg-do run { target i?86-*-* x86_64-*-* } } */
+/* { dg-options "-O2 -mno-sse -mno-popcnt" } */
+
+
+/* Default version.  */
+int __attribute__ ((target ("sse")))
+foo ()
+{
+  return 1;
+}
+int __attribute__ ((target ("popcnt")))
+foo ()
+{
+  return 0;
+}
+
+int __attribute__ ((target ("popcnt")))
+bar ()
+{
+  return foo ();
+}
+
+int main ()
+{
+  return bar ();
+}
Index: gcc/cp/class.c
===================================================================
--- gcc/cp/class.c	(revision 190493)
+++ gcc/cp/class.c	(working copy)
@@ -1091,7 +1091,20 @@  add_method (tree type, tree method, tree using_dec
 	      || same_type_p (TREE_TYPE (fn_type),
 			      TREE_TYPE (method_type))))
 	{
-	  if (using_decl)
+	  /* For function versions, their parms and types match
+	     but they are not duplicates.  Record function versions
+	     as and when they are found.  */
+	  if (TREE_CODE (fn) == FUNCTION_DECL
+	      && TREE_CODE (method) == FUNCTION_DECL
+	      && (DECL_FUNCTION_SPECIFIC_TARGET (fn)
+		  || DECL_FUNCTION_SPECIFIC_TARGET (method))
+	      && targetm.target_option.function_versions (fn, method))
+ 	    {
+	      targetm.set_version_assembler_name (fn);
+	      targetm.set_version_assembler_name (method);
+	      continue;
+	    }
+	  else if (using_decl)
 	    {
 	      if (DECL_CONTEXT (fn) == type)
 		/* Defer to the local function.  */
@@ -6922,6 +6935,7 @@  resolve_address_of_overloaded_function (tree targe
   tree matches = NULL_TREE;
   tree fn;
   tree target_fn_type;
+  VEC (tree, heap) *fn_ver_vec = NULL;
 
   /* By the time we get here, we should be seeing only real
      pointer-to-member types, not the internal POINTER_TYPE to
@@ -6986,9 +7000,19 @@  resolve_address_of_overloaded_function (tree targe
 	  if (DECL_ANTICIPATED (fn))
 	    continue;
 
-	  /* See if there's a match.  */
+	  /* See if there's a match.   For functions that are multi-versioned,
+	     all the versions match.  */
 	  if (same_type_p (target_fn_type, static_fn_type (fn)))
-	    matches = tree_cons (fn, NULL_TREE, matches);
+	    {
+	      matches = tree_cons (fn, NULL_TREE, matches);
+	      /*If versioned, push all possible versions into a vector.  */
+	      if (DECL_FUNCTION_VERSIONED (fn))
+		{
+		  if (fn_ver_vec == NULL)
+		   fn_ver_vec = VEC_alloc (tree, heap, 2);
+		  VEC_safe_push (tree, heap, fn_ver_vec, fn); 
+		}
+	    }
 	}
     }
 
@@ -7080,13 +7104,26 @@  resolve_address_of_overloaded_function (tree targe
     {
       /* There were too many matches.  First check if they're all
 	 the same function.  */
-      tree match;
+      tree match = NULL_TREE;
 
       fn = TREE_PURPOSE (matches);
-      for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
-	if (!decls_match (fn, TREE_PURPOSE (match)))
-	  break;
 
+      /* For multi-versioned functions, more than one match is just fine.
+	 Call decls_match to make sure they are different because they are
+	 versioned.  */
+      if (DECL_FUNCTION_VERSIONED (fn))
+	{
+          for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
+  	    if (decls_match (fn, TREE_PURPOSE (match)))
+	      break;
+	}
+      else
+	{
+          for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
+  	    if (!decls_match (fn, TREE_PURPOSE (match)))
+	      break;
+	}
+
       if (match)
 	{
 	  if (flags & tf_error)
@@ -7148,6 +7185,28 @@  resolve_address_of_overloaded_function (tree targe
       perform_or_defer_access_check (access_path, fn, fn, flags);
     }
 
+  /* If a pointer to a function that is multi-versioned is requested, the
+     pointer to the dispatcher function is returned instead.  This works
+     well because indirectly calling the function will dispatch the right
+     function version at run-time.  */
+  if (DECL_FUNCTION_VERSIONED (fn))
+    {
+      tree dispatcher_decl = NULL;
+      gcc_assert (fn_ver_vec != NULL);
+      gcc_assert (targetm.get_function_versions_dispatcher);
+      dispatcher_decl = targetm.get_function_versions_dispatcher (fn_ver_vec);
+      if (!dispatcher_decl)
+	{
+	  error_at (input_location, "Pointer to a multiversioned function"
+		    " without a default is not allowed");
+	  return error_mark_node;
+	}
+      retrofit_lang_decl (dispatcher_decl);
+      mark_used (fn);
+      VEC_free (tree, heap, fn_ver_vec);
+      fn = dispatcher_decl;
+    }
+
   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
     return cp_build_addr_expr (fn, flags);
   else
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 190493)
+++ gcc/cp/decl.c	(working copy)
@@ -53,6 +53,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "pointer-set.h"
 #include "splay-tree.h"
 #include "plugin.h"
+#include "cgraph.h"
 
 /* Possible cases of bad specifiers type used by bad_specifiers. */
 enum bad_spec_place {
@@ -972,6 +973,17 @@  decls_match (tree newdecl, tree olddecl)
       if (t1 != t2)
 	return 0;
 
+      /* The decls dont match if they correspond to two different versions
+	 of the same function.  */
+      if (compparms (p1, p2)
+	  && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)) 
+	  && targetm.target_option.function_versions (newdecl, olddecl))
+	{
+	  targetm.set_version_assembler_name(newdecl);
+	  targetm.set_version_assembler_name(olddecl);
+	  return 0;
+	}
+
       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
 	  && ! (DECL_EXTERN_C_P (newdecl)
 		&& DECL_EXTERN_C_P (olddecl)))
@@ -1490,7 +1502,11 @@  duplicate_decls (tree newdecl, tree olddecl, bool
 	      error ("previous declaration %q+#D here", olddecl);
 	      return NULL_TREE;
 	    }
-	  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
+	  /* For function versions, params and types match, but they
+	     are not ambiguous.  */
+	  else if ((!DECL_FUNCTION_VERSIONED (newdecl)
+		    && !DECL_FUNCTION_VERSIONED (olddecl))
+		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
 			      TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
 	    {
 	      error ("new declaration %q#D", newdecl);
@@ -2262,6 +2278,11 @@  duplicate_decls (tree newdecl, tree olddecl, bool
   else if (DECL_PRESERVE_P (newdecl))
     DECL_PRESERVE_P (olddecl) = 1;
 
+  /* If the olddecl is a version, so is the newdecl.  */
+  if (TREE_CODE (newdecl) == FUNCTION_DECL
+      && DECL_FUNCTION_VERSIONED (olddecl))
+    DECL_FUNCTION_VERSIONED (newdecl) = 1;
+
   if (TREE_CODE (newdecl) == FUNCTION_DECL)
     {
       int function_size;
@@ -14024,7 +14045,11 @@  cxx_comdat_group (tree decl)
 	  else
 	    break;
 	}
-      name = DECL_ASSEMBLER_NAME (decl);
+      if (TREE_CODE (decl) == FUNCTION_DECL
+	  && DECL_FUNCTION_VERSIONED (decl))
+	name = DECL_NAME (decl);
+      else
+        name = DECL_ASSEMBLER_NAME (decl);
     }
 
   return name;
Index: gcc/cp/error.c
===================================================================
--- gcc/cp/error.c	(revision 190493)
+++ gcc/cp/error.c	(working copy)
@@ -1539,8 +1539,15 @@  dump_exception_spec (tree t, int flags)
 static void
 dump_function_name (tree t, int flags)
 {
-  tree name = DECL_NAME (t);
+  tree name;
 
+  /* For function versions, use the assembler name as the decl name is
+     the same for all versions.  */
+  if (DECL_FUNCTION_VERSIONED (t))
+    name = DECL_ASSEMBLER_NAME (t);
+  else
+    name = DECL_NAME (t);
+
   /* We can get here with a decl that was synthesized by language-
      independent machinery (e.g. coverage.c) in which case it won't
      have a lang_specific structure attached and DECL_CONSTRUCTOR_P
Index: gcc/cp/semantics.c
===================================================================
--- gcc/cp/semantics.c	(revision 190493)
+++ gcc/cp/semantics.c	(working copy)
@@ -3775,8 +3775,11 @@  expand_or_defer_fn_1 (tree fn)
       /* If the user wants us to keep all inline functions, then mark
 	 this function as needed so that finish_file will make sure to
 	 output it later.  Similarly, all dllexport'd functions must
-	 be emitted; there may be callers in other DLLs.  */
-      if ((flag_keep_inline_functions
+	 be emitted; there may be callers in other DLLs.
+	 Also, mark this function as needed if it is marked inline but
+	 is a multi-versioned function.  */
+      if (((flag_keep_inline_functions
+	    || DECL_FUNCTION_VERSIONED (fn))
 	   && DECL_DECLARED_INLINE_P (fn)
 	   && !DECL_REALLY_EXTERN (fn))
 	  || (flag_keep_inline_dllexport
Index: gcc/cp/decl2.c
===================================================================
--- gcc/cp/decl2.c	(revision 190493)
+++ gcc/cp/decl2.c	(working copy)
@@ -674,9 +674,13 @@  check_classfn (tree ctype, tree function, tree tem
 	  if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL))
 	    continue;
 
+	  /* While finding a match, same types and params are not enough
+	     if the function is versioned.  Also check version ("target")
+	     attributes.  */
 	  if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
 			   TREE_TYPE (TREE_TYPE (fndecl)))
 	      && compparms (p1, p2)
+	      && !targetm.target_option.function_versions (function, fndecl)
 	      && (!is_template
 		  || comp_template_parms (template_parms,
 					  DECL_TEMPLATE_PARMS (fndecl)))
Index: gcc/cp/call.c
===================================================================
--- gcc/cp/call.c	(revision 190493)
+++ gcc/cp/call.c	(working copy)
@@ -40,6 +40,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "langhooks.h"
 #include "c-family/c-objc.h"
 #include "timevar.h"
+#include "cgraph.h"
 
 /* The various kinds of conversion.  */
 
@@ -3910,6 +3911,16 @@  build_new_function_call (tree fn, VEC(tree,gc) **a
     {
       if (complain & tf_error)
 	{
+	  /* If the call is to a multiversioned function without
+	     a default version, overload resolution will fail.  */
+	  if (candidates
+	      && TREE_CODE (candidates->fn) == FUNCTION_DECL
+	      && DECL_FUNCTION_VERSIONED (candidates->fn))
+	    error_at (location_of (DECL_NAME (OVL_CURRENT (fn))),
+		      "Call to multiversioned function %<%D(%A)%> with"
+		      " no default version", DECL_NAME (OVL_CURRENT (fn)),
+		      build_tree_list_vec (*args));
+
 	  if (!any_viable_p && candidates && ! candidates->next
 	      && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
 	    return cp_build_function_call_vec (candidates->fn, args, complain);
@@ -6858,6 +6869,30 @@  build_over_call (struct z_candidate *cand, int fla
   if (!already_used)
     mark_used (fn);
 
+  /* For calls to a multi-versioned function, overload resolution
+     returns the function with the highest target priority, that is,
+     the version that will checked for dispatching first.  If this
+     version is inlinable, a direct call can be made otherwise it
+     should go through the dispatcher.  */
+
+  if (DECL_FUNCTION_VERSIONED (fn)
+      && !targetm.target_option.can_inline_p (current_function_decl, fn))
+    {
+      tree dispatcher_decl = NULL;
+      struct cgraph_node *node = cgraph_get_node (fn);
+      if (node != NULL)
+        dispatcher_decl = cgraph_get_node (fn)->version_dispatcher_decl;
+      if (dispatcher_decl == NULL)
+	{
+	  error_at (input_location, "Call to multiversioned function"
+		    " without a default is not allowed");
+	  return NULL;
+	}
+      retrofit_lang_decl (dispatcher_decl);
+      gcc_assert (dispatcher_decl != NULL);
+      fn = dispatcher_decl;
+    }
+
   if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
     {
       tree t;
@@ -8123,6 +8158,29 @@  joust (struct z_candidate *cand1, struct z_candida
   size_t i;
   size_t len;
 
+  /* For Candidates of a multi-versioned function,  make the version with
+     the highest priority win.  This version will be checked for dispatching
+     first.  If this version can be inlined into the caller the front-end
+     will simply make a direct call to this function.  */
+
+  if ((TREE_CODE (cand1->fn) == FUNCTION_DECL
+       && DECL_FUNCTION_VERSIONED (cand1->fn))
+      ||(TREE_CODE (cand2->fn) == FUNCTION_DECL
+	 && DECL_FUNCTION_VERSIONED (cand2->fn)))
+    {
+      /* Both functions must be marked versioned.  */
+      gcc_assert (DECL_FUNCTION_VERSIONED (cand1->fn)
+		  && DECL_FUNCTION_VERSIONED (cand2->fn));
+
+      /* Always make the version with the higher priority, more
+	 specialized, win.  */
+      gcc_assert (targetm.compare_version_priority);
+      if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
+	return 1;
+      else
+	return -1;
+    }
+
   /* Candidates that involve bad conversions are always worse than those
      that don't.  */
   if (cand1->viable > cand2->viable)
@@ -8468,6 +8526,22 @@  tourney (struct z_candidate *candidates, tsubst_fl
   int fate;
   int champ_compared_to_predecessor = 0;
 
+  /* For multiversioned functions, aggregate all the versions here for
+     generating the dispatcher body later if necessary.  */
+
+  if (TREE_CODE (candidates->fn) == FUNCTION_DECL
+      && DECL_FUNCTION_VERSIONED (candidates->fn))
+    {
+      VEC (tree, heap) *fn_ver_vec = NULL;
+      struct z_candidate *ver = candidates;
+      fn_ver_vec = VEC_alloc (tree, heap, 2);
+      for (;ver; ver = ver->next)
+        VEC_safe_push (tree, heap, fn_ver_vec, ver->fn);
+      gcc_assert (targetm.get_function_versions_dispatcher);
+      targetm.get_function_versions_dispatcher (fn_ver_vec);
+      VEC_free (tree, heap, fn_ver_vec);
+    }
+
   /* Walk through the list once, comparing each current champ to the next
      candidate, knocking out a candidate or two with each comparison.  */
 
Index: gcc/cp/mangle.c
===================================================================
--- gcc/cp/mangle.c	(revision 190493)
+++ gcc/cp/mangle.c	(working copy)
@@ -1245,7 +1245,12 @@  write_unqualified_name (const tree decl)
     {
       MANGLE_TRACE_TREE ("local-source-name", decl);
       write_char ('L');
-      write_source_name (DECL_NAME (decl));
+      if (TREE_CODE (decl) == FUNCTION_DECL
+          && DECL_FUNCTION_VERSIONED (decl)
+	  && DECL_ASSEMBLER_NAME_SET_P (decl))
+	write_source_name (DECL_ASSEMBLER_NAME (decl));
+      else
+	write_source_name (DECL_NAME (decl));
       /* The default discriminator is 1, and that's all we ever use,
 	 so there's no code to output one here.  */
     }
@@ -1260,7 +1265,14 @@  write_unqualified_name (const tree decl)
                && LAMBDA_TYPE_P (type))
         write_closure_type_name (type);
       else
-        write_source_name (DECL_NAME (decl));
+	{
+	  if (TREE_CODE (decl) == FUNCTION_DECL
+	      && DECL_FUNCTION_VERSIONED (decl)
+	      && DECL_ASSEMBLER_NAME_SET_P (decl))
+	    write_source_name (DECL_ASSEMBLER_NAME (decl));
+	  else
+	    write_source_name (DECL_NAME (decl));
+	}
     }
 }
 
Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c	(revision 190493)
+++ gcc/config/i386/i386.c	(working copy)
@@ -62,6 +62,8 @@  along with GCC; see the file COPYING3.  If not see
 #include "opts.h"
 #include "diagnostic.h"
 #include "dumpfile.h"
+#include "tree-pass.h"
+#include "tree-flow.h"
 
 enum upper_128bits_state
 {
@@ -28030,6 +28032,980 @@  ix86_init_mmx_sse_builtins (void)
     }
 }
 
+
+/* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL
+   to return a pointer to VERSION_DECL if the outcome of the expression
+   formed by PREDICATE_CHAIN is true.  This function will be called during
+   version dispatch to decide which function version to execute.  It returns
+   the basic block at the end, to which more conditions can be added.  */
+
+static basic_block
+add_condition_to_bb (tree function_decl, tree version_decl,
+		     tree predicate_chain, basic_block new_bb)
+{
+  gimple return_stmt;
+  tree convert_expr, result_var;
+  gimple convert_stmt;
+  gimple call_cond_stmt;
+  gimple if_else_stmt;
+
+  basic_block bb1, bb2, bb3;
+  edge e12, e23;
+
+  tree cond_var, and_expr_var = NULL_TREE;
+  gimple_seq gseq;
+
+  tree old_current_function_decl;
+  tree predicate_decl, predicate_arg;
+
+  old_current_function_decl = current_function_decl;
+  push_cfun (DECL_STRUCT_FUNCTION (function_decl));
+  current_function_decl = function_decl;
+
+  gcc_assert (new_bb != NULL);
+  gseq = bb_seq (new_bb);
+
+
+  convert_expr = build1 (CONVERT_EXPR, ptr_type_node,
+	     		 build_fold_addr_expr (version_decl));
+  result_var = create_tmp_var (ptr_type_node, NULL);
+  convert_stmt = gimple_build_assign (result_var, convert_expr); 
+  return_stmt = gimple_build_return (result_var);
+
+  if (predicate_chain == NULL_TREE)
+    {
+      gimple_seq_add_stmt (&gseq, convert_stmt);
+      gimple_seq_add_stmt (&gseq, return_stmt);
+      set_bb_seq (new_bb, gseq);
+      gimple_set_bb (convert_stmt, new_bb);
+      gimple_set_bb (return_stmt, new_bb);
+      pop_cfun ();
+      current_function_decl = old_current_function_decl;
+      return new_bb;
+    }
+
+  while (predicate_chain != NULL)
+    {
+      cond_var = create_tmp_var (integer_type_node, NULL);
+      predicate_decl = TREE_PURPOSE (predicate_chain);
+      predicate_arg = TREE_VALUE (predicate_chain);
+      call_cond_stmt = gimple_build_call (predicate_decl, 1, predicate_arg);
+      gimple_call_set_lhs (call_cond_stmt, cond_var);
+
+      gimple_set_block (call_cond_stmt, DECL_INITIAL (function_decl));
+      gimple_set_bb (call_cond_stmt, new_bb);
+      gimple_seq_add_stmt (&gseq, call_cond_stmt);
+
+      predicate_chain = TREE_CHAIN (predicate_chain);
+      
+      if (and_expr_var == NULL)
+        and_expr_var = cond_var;
+      else
+	{
+	  gimple assign_stmt;
+	  /* Use MIN_EXPR to check if any integer is zero?.
+	     and_expr_var = min_expr <cond_var, and_expr_var>  */
+	  assign_stmt = gimple_build_assign (and_expr_var,
+			  build2 (MIN_EXPR, integer_type_node,
+				  cond_var, and_expr_var));
+
+	  gimple_set_block (assign_stmt, DECL_INITIAL (function_decl));
+	  gimple_set_bb (assign_stmt, new_bb);
+	  gimple_seq_add_stmt (&gseq, assign_stmt);
+	}
+    }
+
+  if_else_stmt = gimple_build_cond (GT_EXPR, and_expr_var,
+	  		            integer_zero_node,
+				    NULL_TREE, NULL_TREE);
+  gimple_set_block (if_else_stmt, DECL_INITIAL (function_decl));
+  gimple_set_bb (if_else_stmt, new_bb);
+  gimple_seq_add_stmt (&gseq, if_else_stmt);
+
+  gimple_seq_add_stmt (&gseq, convert_stmt);
+  gimple_seq_add_stmt (&gseq, return_stmt);
+  set_bb_seq (new_bb, gseq);
+
+  bb1 = new_bb;
+  e12 = split_block (bb1, if_else_stmt);
+  bb2 = e12->dest;
+  e12->flags &= ~EDGE_FALLTHRU;
+  e12->flags |= EDGE_TRUE_VALUE;
+
+  e23 = split_block (bb2, return_stmt);
+
+  gimple_set_bb (convert_stmt, bb2);
+  gimple_set_bb (return_stmt, bb2);
+
+  bb3 = e23->dest;
+  make_edge (bb1, bb3, EDGE_FALSE_VALUE); 
+
+  remove_edge (e23);
+  make_edge (bb2, EXIT_BLOCK_PTR, 0);
+
+  pop_cfun ();
+  current_function_decl = old_current_function_decl;
+
+  return bb3;
+}
+
+/* This parses the attribute arguments to target in DECL and determines
+   the right builtin to use to match the platform specification.
+   For now, only one target argument ("arch=" or "<-m>xxx") is allowed.
+   It returns the priority value for this version decl.  If PREDICATE_LIST
+   is not NULL, it stores the list of cpu features that need to be checked
+   before dispatching this function.  */
+
+static unsigned int
+get_builtin_code_for_version (tree decl, tree *predicate_list)
+{
+  tree attrs;
+  struct cl_target_option cur_target;
+  tree target_node;
+  struct cl_target_option *new_target;
+  const char *arg_str = NULL;
+  const char *attrs_str = NULL;
+  char *tok_str = NULL;
+  char *token;
+  unsigned int priority = 0;
+
+  /* Priority of i386 features, greater value is higher priority.   This is
+     used to decide the order in which function dispatch must happen.  For
+     instance, a version specialized for SSE4.2 should be checked for dispatch
+     before a version for SSE3, as SSE4.2 implies SSE3.  */
+  enum feature_priority
+  {
+    P_ZERO = 0,
+    P_MMX,
+    P_SSE,
+    P_SSE2,
+    P_SSE3,
+    P_SSSE3,
+    P_PROC_SSSE3,
+    P_SSE4_a,
+    P_PROC_SSE4_a,
+    P_SSE4_1,
+    P_SSE4_2,
+    P_PROC_SSE4_2,
+    P_POPCNT,
+    P_AVX,
+    P_AVX2,
+    P_FMA,
+    P_PROC_FMA
+  };
+
+  /* These are the target attribute strings for which a dispatcher is
+     available, from fold_builtin_cpu.  */
+
+  static struct _feature_list
+    {
+      const char *const name;
+      const enum feature_priority priority;
+    }
+  const feature_list[] =
+    {
+      {"mmx", P_MMX},
+      {"sse", P_SSE},
+      {"sse2", P_SSE2},
+      {"sse3", P_SSE3},
+      {"ssse3", P_SSSE3},
+      {"sse4.1", P_SSE4_1},
+      {"sse4.2", P_SSE4_2},
+      {"popcnt", P_POPCNT},
+      {"avx", P_AVX},
+      {"avx2", P_AVX2}
+    };
+
+
+  static unsigned int NUM_FEATURES
+    = sizeof (feature_list) / sizeof (struct _feature_list);
+
+  unsigned int i;
+
+  tree predicate_chain = NULL_TREE;
+  tree predicate_decl, predicate_arg;
+
+  attrs = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
+  gcc_assert (attrs != NULL);
+
+  attrs = TREE_VALUE (TREE_VALUE (attrs));
+
+  gcc_assert (TREE_CODE (attrs) == STRING_CST);
+  attrs_str = TREE_STRING_POINTER (attrs);
+
+
+  /* Handle arch= if specified.  For priority, set it to be 1 more than
+     the best instruction set the processor can handle.  For instance, if
+     there is a version for atom and a version for ssse3 (the highest ISA
+     priority for atom), the atom version must be checked for dispatch
+     before the ssse3 version. */
+  if (strstr (attrs_str, "arch=") != NULL)
+    {
+      cl_target_option_save (&cur_target, &global_options);
+      target_node = ix86_valid_target_attribute_tree (attrs);
+    
+      gcc_assert (target_node);
+      new_target = TREE_TARGET_OPTION (target_node);
+      gcc_assert (new_target);
+      
+      if (new_target->arch_specified && new_target->arch > 0)
+	{
+	  switch (new_target->arch)
+	    {
+	    case PROCESSOR_CORE2_32:
+	    case PROCESSOR_CORE2_64:
+	      arg_str = "core2";
+	      priority = P_PROC_SSSE3;
+	      break;
+	    case PROCESSOR_COREI7_32:
+	    case PROCESSOR_COREI7_64:
+	      arg_str = "corei7";
+	      priority = P_PROC_SSE4_2;
+	      break;
+	    case PROCESSOR_ATOM:
+	      arg_str = "atom";
+	      priority = P_PROC_SSSE3;
+	      break;
+	    case PROCESSOR_AMDFAM10:
+	      arg_str = "amdfam10h";
+	      priority = P_PROC_SSE4_a;
+	      break;
+	    case PROCESSOR_BDVER1:
+	      arg_str = "bdver1";
+	      priority = P_PROC_FMA;
+	      break;
+	    case PROCESSOR_BDVER2:
+	      arg_str = "bdver2";
+	      priority = P_PROC_FMA;
+	      break;
+	    }  
+	}    
+    
+      cl_target_option_restore (&global_options, &cur_target);
+	
+      if (predicate_list && arg_str == NULL)
+	{
+	  error_at (DECL_SOURCE_LOCATION (decl),
+	    	"No dispatcher found for the versioning attributes");
+	  return 0;
+	}
+    
+      if (predicate_list)
+	{
+          predicate_decl = ix86_builtins [(int) IX86_BUILTIN_CPU_IS];
+          /* For a C string literal the length includes the trailing NULL.  */
+          predicate_arg = build_string_literal (strlen (arg_str) + 1, arg_str);
+          predicate_chain = tree_cons (predicate_decl, predicate_arg,
+				       predicate_chain);
+	}
+    }
+
+  /* Process feature name.  */
+  tok_str =  (char *) xmalloc (strlen (attrs_str) + 1);
+  strcpy (tok_str, attrs_str);
+  token = strtok (tok_str, ",");
+  predicate_decl = ix86_builtins [(int) IX86_BUILTIN_CPU_SUPPORTS];
+
+  while (token != NULL)
+    {
+      /* Do not process "arch="  */
+      if (strncmp (token, "arch=", 5) == 0)
+	{
+	  token = strtok (NULL, ",");
+	  continue;
+	}
+      for (i = 0; i < NUM_FEATURES; ++i)
+	{
+	  if (strcmp (token, feature_list[i].name) == 0)
+	    {
+	      if (predicate_list)
+		{
+		  predicate_arg = build_string_literal (
+				  strlen (feature_list[i].name) + 1,
+				  feature_list[i].name);
+		  predicate_chain = tree_cons (predicate_decl, predicate_arg,
+					       predicate_chain);
+		}
+	      /* Find the maximum priority feature.  */
+	      if (feature_list[i].priority > priority)
+		priority = feature_list[i].priority;
+
+	      break;
+	    }
+	}
+      if (predicate_list && i == NUM_FEATURES)
+	{
+	  error_at (DECL_SOURCE_LOCATION (decl),
+		    "No dispatcher found for %s", token);
+	  return 0;
+	}
+      token = strtok (NULL, ",");
+    }
+  free (tok_str);
+
+  if (predicate_list && predicate_chain == NULL_TREE)
+    {
+      error_at (DECL_SOURCE_LOCATION (decl),
+	        "No dispatcher found for the versioning attributes : %s",
+	        attrs_str);
+      return 0;
+    }
+  else if (predicate_list)
+    {
+      predicate_chain = nreverse (predicate_chain);
+      *predicate_list = predicate_chain;
+    }
+
+  return priority; 
+}
+
+/* This compares the priority of target features in function DECL1
+   and DECL2.  It returns positive value if DECL1 is higher priority,
+   negative value if DECL2 is higher priority and 0 if they are the
+   same.  */
+
+static int
+ix86_compare_version_priority (tree decl1, tree decl2)
+{
+  unsigned int priority1 = 0;
+  unsigned int priority2 = 0;
+
+  if (lookup_attribute ("target", DECL_ATTRIBUTES (decl1)) != NULL)
+    priority1 = get_builtin_code_for_version (decl1, NULL);
+
+  if (lookup_attribute ("target", DECL_ATTRIBUTES (decl2)) != NULL)
+    priority2 = get_builtin_code_for_version (decl2, NULL);
+
+  return (int)priority1 - (int)priority2;
+}
+
+/* V1 and V2 point to function versions with different priorities
+   based on the target ISA.  This function compares their priorities.  */
+ 
+static int
+feature_compare (const void *v1, const void *v2)
+{
+  typedef struct _function_version_info
+    {
+      tree version_decl;
+      tree predicate_chain;
+      unsigned int dispatch_priority;
+    } function_version_info;
+
+  const function_version_info c1 = *(const function_version_info *)v1;
+  const function_version_info c2 = *(const function_version_info *)v2;
+  return (c2.dispatch_priority - c1.dispatch_priority);
+}
+
+/* This function generates the dispatch function for
+   multi-versioned functions.  DISPATCH_DECL is the function which will
+   contain the dispatch logic.  FNDECLS are the function choices for
+   dispatch, and is a tree chain.  EMPTY_BB is the basic block pointer
+   in DISPATCH_DECL in which the dispatch code is generated.  */
+
+static int
+dispatch_function_versions (tree dispatch_decl,
+			    void *fndecls_p,
+			    basic_block *empty_bb)
+{
+  tree default_decl;
+  gimple ifunc_cpu_init_stmt;
+  gimple_seq gseq;
+  tree old_current_function_decl;
+  int ix;
+  tree ele;
+  VEC (tree, heap) *fndecls;
+  unsigned int num_versions = 0;
+  unsigned int actual_versions = 0;
+  unsigned int i;
+
+  struct _function_version_info
+    {
+      tree version_decl;
+      tree predicate_chain;
+      unsigned int dispatch_priority;
+    }*function_version_info;
+
+  gcc_assert (dispatch_decl != NULL
+	      && fndecls_p != NULL
+	      && empty_bb != NULL);
+
+  /*fndecls_p is actually a vector.  */
+  fndecls = (VEC (tree, heap) *)fndecls_p;
+
+  /* Atleast one more version other than the default.  */
+  num_versions = VEC_length (tree, fndecls);
+  gcc_assert (num_versions >= 2);
+
+  function_version_info = (struct _function_version_info *)
+    xmalloc ((num_versions - 1) * sizeof (struct _function_version_info));
+
+  /* The first version in the vector is the default decl.  */
+  default_decl = VEC_index (tree, fndecls, 0);
+
+  old_current_function_decl = current_function_decl;
+  push_cfun (DECL_STRUCT_FUNCTION (dispatch_decl));
+  current_function_decl = dispatch_decl;
+
+  gseq = bb_seq (*empty_bb);
+  /* Function version dispatch is via IFUNC.  IFUNC resolvers fire before
+     constructors, so explicity call __builtin_cpu_init here.  */
+  ifunc_cpu_init_stmt = gimple_build_call_vec (
+                     ix86_builtins [(int) IX86_BUILTIN_CPU_INIT], NULL);
+  gimple_seq_add_stmt (&gseq, ifunc_cpu_init_stmt);
+  gimple_set_bb (ifunc_cpu_init_stmt, *empty_bb);
+  set_bb_seq (*empty_bb, gseq);
+
+  pop_cfun ();
+  current_function_decl = old_current_function_decl;
+
+
+  for (ix = 1; VEC_iterate (tree, fndecls, ix, ele); ++ix)
+    {
+      tree version_decl = ele;
+      tree predicate_chain = NULL_TREE;
+      unsigned int priority;
+      /* Get attribute string, parse it and find the right predicate decl.
+         The predicate function could be a lengthy combination of many
+	 features, like arch-type and various isa-variants.  */
+      priority = get_builtin_code_for_version (version_decl,
+	 			               &predicate_chain);
+
+      if (predicate_chain == NULL_TREE)
+	continue;
+
+      actual_versions++;
+      function_version_info [ix - 1].version_decl = version_decl;
+      function_version_info [ix - 1].predicate_chain = predicate_chain;
+      function_version_info [ix - 1].dispatch_priority = priority;
+    }
+
+  /* Sort the versions according to descending order of dispatch priority.  The
+     priority is based on the ISA.  This is not a perfect solution.  There
+     could still be ambiguity.  If more than one function version is suitable
+     to execute,  which one should be dispatched?  In future, allow the user
+     to specify a dispatch  priority next to the version.  */
+  qsort (function_version_info, actual_versions,
+         sizeof (struct _function_version_info), feature_compare);
+
+  for  (i = 0; i < actual_versions; ++i)
+    *empty_bb = add_condition_to_bb (dispatch_decl,
+				     function_version_info[i].version_decl,
+				     function_version_info[i].predicate_chain,
+				     *empty_bb);
+
+  /* dispatch default version at the end.  */
+  *empty_bb = add_condition_to_bb (dispatch_decl, default_decl,
+				   NULL, *empty_bb);
+
+  free (function_version_info);
+  return 0;
+}
+
+/* This function returns true if fn1 and fn2 are versions of the same function.
+   Returns false if only one of the function decls has the target attribute
+   set or if the targets of the function decls are different.  This assumes
+   the fn1 and fn2 have the same signature.  */
+
+static bool
+ix86_function_versions (tree fn1, tree fn2)
+{
+  tree attr1, attr2;
+  struct cl_target_option *target1, *target2;
+
+  if (TREE_CODE (fn1) != FUNCTION_DECL
+      || TREE_CODE (fn2) != FUNCTION_DECL)
+    return false;
+
+  attr1 = DECL_FUNCTION_SPECIFIC_TARGET (fn1);
+  attr2 = DECL_FUNCTION_SPECIFIC_TARGET (fn2);
+
+  if (attr1 == NULL_TREE && attr2 == NULL_TREE)
+    return false;
+
+  if ((attr1 == NULL_TREE && attr2 != NULL_TREE)
+      || (attr1 != NULL_TREE && attr2 == NULL_TREE))
+    return true;
+
+  target1 = TREE_TARGET_OPTION (attr1);
+  target2 = TREE_TARGET_OPTION (attr2);
+
+  if (target1->x_ix86_isa_flags == target2->x_ix86_isa_flags
+      && target1->x_target_flags == target2->x_target_flags
+      && target1->arch == target2->arch
+      && target1->tune == target2->tune
+      && target1->x_ix86_fpmath == target2->x_ix86_fpmath
+      && target1->branch_cost == target2->branch_cost)
+    return false;
+
+  return true;
+}
+
+/* Comparator function to be used in qsort routine to sort attribute
+   specification strings to "target".  */
+
+static int
+attr_strcmp (const void *v1, const void *v2)
+{
+  const char *c1 = *(char *const*)v1;
+  const char *c2 = *(char *const*)v2;
+  return strcmp (c1, c2);
+}
+
+/* STR is the argument to target attribute.  This function tokenizes
+   the comma separated arguments, sorts them and returns a string which
+   is a unique identifier for the comma separated arguments.  */
+
+static char *
+sorted_attr_string (const char *str)
+{
+  char **args = NULL;
+  char *attr_str, *ret_str;
+  char *attr = NULL;
+  unsigned int argnum = 1;
+  unsigned int i;
+
+  for (i = 0; i < strlen (str); i++)
+    if (str[i] == ',')
+      argnum++;
+
+  attr_str = (char *)xmalloc (strlen (str) + 1);
+  strcpy (attr_str, str);
+
+  for (i = 0; i < strlen (attr_str); i++)
+    if (attr_str[i] == '=' || attr_str[i]== '-')
+      attr_str[i] = '_';
+
+  if (argnum == 1)
+    return attr_str;
+
+  args = (char **)xmalloc (argnum * sizeof (char *));
+
+  i = 0;
+  attr = strtok (attr_str, ",");
+  while (attr != NULL)
+    {
+      args[i] = attr;
+      i++;
+      attr = strtok (NULL, ",");
+    }
+
+  qsort (args, argnum, sizeof (char*), attr_strcmp);
+
+  ret_str = (char *)xmalloc (strlen (str) + 1);
+  strcpy (ret_str, args[0]);
+  for (i = 1; i < argnum; i++)
+    {
+      strcat (ret_str, "_");
+      strcat (ret_str, args[i]);
+    }
+
+  free (args);
+  free (attr_str);
+  return ret_str;
+}
+
+/* This function changes the assembler name for functions that are
+   versions.  If DECL is a function version and has a "target"
+   attribute, it appends the attribute string to its assembler name.  */
+
+static void
+ix86_set_version_assembler_name (tree decl)
+{
+  tree version_attr;
+  const char *orig_name, *version_string, *attr_str;
+  char *assembler_name;
+  tree assembler_name_tree;
+
+  if (TREE_CODE (decl) != FUNCTION_DECL)
+    return;
+
+  if (DECL_FUNCTION_VERSIONED (decl)
+      && DECL_ASSEMBLER_NAME_SET_P (decl))
+    return;
+
+  DECL_FUNCTION_VERSIONED (decl) = 1;
+
+  if (DECL_DECLARED_INLINE_P (decl)
+      && lookup_attribute ("gnu_inline",
+			   DECL_ATTRIBUTES (decl)))
+    error_at (DECL_SOURCE_LOCATION (decl),
+	      "Function versions cannot be marked as gnu_inline,"
+	      " bodies have to be generated\n");
+
+  if (DECL_VIRTUAL_P (decl)
+      || DECL_VINDEX (decl))
+    error_at (DECL_SOURCE_LOCATION (decl),
+	      "Virtual function versioning not supported\n");
+
+  version_attr = lookup_attribute ("target", DECL_ATTRIBUTES (decl));
+
+  /* target attribute string is NULL for default functions.  */
+  if (version_attr == NULL_TREE)
+    return;
+
+  orig_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
+  version_string
+    = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (version_attr)));
+
+  attr_str = sorted_attr_string (version_string);
+  assembler_name = (char *) xmalloc (strlen (orig_name)
+				     + strlen (attr_str) + 2);
+
+  sprintf (assembler_name, "%s.%s", orig_name, attr_str);
+  if (dump_file)
+    fprintf (stderr, "Assembler name set to %s for function version %s\n",
+	     assembler_name, IDENTIFIER_POINTER (DECL_NAME (decl)));
+
+  assembler_name_tree = get_identifier (assembler_name);
+
+  SET_DECL_ASSEMBLER_NAME (decl, assembler_name_tree);
+  SET_DECL_RTL (decl, NULL);
+}
+
+/* Return a new name by appending SUFFIX to the DECL name.  If
+   make_unique is true, append the full path name.  */
+
+static char *
+make_name (tree decl, const char *suffix, bool make_unique)
+{
+  char *global_var_name;
+  int name_len;
+  const char *name;
+  const char *unique_name = NULL;
+
+  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
+
+  /* Get a unique name that can be used globally without any chances
+     of collision at link time.  */
+  if (make_unique)
+    unique_name = IDENTIFIER_POINTER (get_file_function_name ("\0"));
+
+  name_len = strlen (name) + strlen (suffix) + 2;
+
+  if (make_unique)
+    name_len += strlen (unique_name) + 1;
+  global_var_name = (char *) xmalloc (name_len);
+
+  /* Use '.' to concatenate names as it is demangler friendly.  */
+  if (make_unique)
+      snprintf (global_var_name, name_len, "%s.%s.%s", name,
+		unique_name, suffix);
+  else
+      snprintf (global_var_name, name_len, "%s.%s", name, suffix);
+
+  return global_var_name;
+}
+
+/* Make a dispatcher declaration for the multi-versioned function DECL.
+   Calls to DECL function will be replaced with calls to the dispatcher
+   by the front-end.  Return the decl created.  */
+
+static tree
+make_dispatcher_decl (const tree decl)
+{
+  tree func_decl;
+  char *func_name, *resolver_name;
+  tree fn_type, func_type;
+  bool is_uniq = false;
+
+  if (TREE_PUBLIC (decl) == 0)
+    is_uniq = true;
+
+  func_name = make_name (decl, "ifunc", is_uniq);
+  resolver_name = make_name (decl, "resolver", is_uniq);
+  gcc_assert (resolver_name);
+
+  fn_type = TREE_TYPE (decl);
+  func_type = build_function_type (TREE_TYPE (fn_type),
+				   TYPE_ARG_TYPES (fn_type));
+  
+  func_decl = build_fn_decl (func_name, func_type);
+  TREE_USED (func_decl) = 1;
+  DECL_CONTEXT (func_decl) = NULL_TREE;
+  DECL_INITIAL (func_decl) = error_mark_node;
+  DECL_ARTIFICIAL (func_decl) = 1;
+  /* Mark this func as external, the resolver will flip it again if
+     it gets generated.  */
+  DECL_EXTERNAL (func_decl) = 1;
+  /* This will be of type IFUNCs have to be externally visible.  */
+  TREE_PUBLIC (func_decl) = 1;
+
+  return func_decl;  
+}
+
+/* Returns true if decl is multi-versioned and DECL is the default function,
+   that is it is not tagged with target specific optimization.  */
+
+static bool
+is_function_default_version (const tree decl)
+{
+  return (TREE_CODE (decl) == FUNCTION_DECL
+	  && DECL_FUNCTION_VERSIONED (decl)
+	  && DECL_FUNCTION_SPECIFIC_TARGET (decl) == NULL_TREE);
+}
+
+/* Make a dispatcher declaration for the multi-versioned function DECL.
+   Calls to DECL function will be replaced with calls to the dispatcher
+   by the front-end.  It also chains the cgraph nodes of all the
+   semantically identical versions in vector FN_VER_VEC_P.  Returns the
+   decl of the dispatcher function.  */
+
+static tree
+ix86_get_function_versions_dispatcher (void *fn_ver_vec_p)
+{
+  struct cgraph_node *node = NULL;
+  struct cgraph_node *default_node = NULL;
+  struct cgraph_node *curr_node = NULL;
+  int ix;
+  tree ele;
+  tree dispatch_decl = NULL;
+  VEC (tree, heap) *fn_ver_vec = NULL;
+
+  fn_ver_vec = (VEC (tree,heap) *) fn_ver_vec_p;
+  gcc_assert (fn_ver_vec != NULL);
+
+  /* Find the default version.  */
+  for (ix = 0; VEC_iterate (tree, fn_ver_vec, ix, ele); ++ix)
+    {
+      if (is_function_default_version (ele))
+	{
+	  default_node = cgraph_get_create_node (ele);
+	  break;
+	}
+    }
+
+  /* If there is no default node, just return NULL.  */
+  if (!default_node)
+    return NULL;
+
+  if (default_node->version_dispatcher_decl)
+    return default_node->version_dispatcher_decl;
+
+#if defined (ASM_OUTPUT_TYPE_DIRECTIVE) && HAVE_GNU_INDIRECT_FUNCTION
+  /* Right now, the dispatching is done via ifunc.  */
+  dispatch_decl = make_dispatcher_decl (default_node->symbol.decl); 
+#else
+  error_at (DECL_SOURCE_LOCATION (default_node->symbol.decl),
+	    "Multiversioning needs ifunc which is not supported "
+	    "in this configuration");
+#endif
+  default_node->version_dispatcher_decl = dispatch_decl;
+  curr_node = cgraph_get_create_node (dispatch_decl);
+  gcc_assert (curr_node);
+  curr_node->dispatcher_function = 1;
+  cgraph_mark_address_taken_node (default_node);
+
+  for (ix = 0; VEC_iterate (tree, fn_ver_vec, ix, ele); ++ix)
+    {
+      node = cgraph_get_create_node (ele);
+      gcc_assert (node != NULL && DECL_FUNCTION_VERSIONED (ele));
+      if (node == default_node)
+	continue;
+      gcc_assert (DECL_FUNCTION_SPECIFIC_TARGET (ele) != NULL_TREE);
+
+      if (curr_node->next_function_version)
+ 	{
+	  node->next_function_version = curr_node->next_function_version;
+	  curr_node->next_function_version->prev_function_version = node;
+	}
+      curr_node->next_function_version = node;
+      node->prev_function_version = curr_node;
+      node->version_dispatcher_decl = dispatch_decl;
+    }
+
+  /* The default version should be the first node.  */
+  default_node->next_function_version = curr_node->next_function_version;
+  curr_node->next_function_version->prev_function_version = default_node;
+  curr_node->next_function_version = default_node;
+  
+  return dispatch_decl;
+}
+
+/* Makes a function attribute of the form NAME(ARG_NAME) and chains
+   it to CHAIN.  */
+
+static tree
+make_attribute (const char *name, const char *arg_name, tree chain)
+{
+  tree attr_name;
+  tree attr_arg_name;
+  tree attr_args;
+  tree attr;
+
+  attr_name = get_identifier (name);
+  attr_arg_name = build_string (strlen (arg_name), arg_name);
+  attr_args = tree_cons (NULL_TREE, attr_arg_name, NULL_TREE);
+  attr = tree_cons (attr_name, attr_args, chain);
+  return attr;
+}
+
+/* Make the resolver function decl to dispatch the versions of
+   a multi-versioned function,  DEFAULT_DECL.  Create an
+   empty basic block in the resolver and store the pointer in
+   EMPTY_BB.  Return the decl of the resolver function.  */
+
+static tree
+make_resolver_func (const tree default_decl,
+		    const tree dispatch_decl,
+		    basic_block *empty_bb)
+{
+  char *resolver_name;
+  tree decl, type, decl_name, t;
+  basic_block new_bb;
+  tree old_current_function_decl;
+  bool is_uniq = false;
+
+  /* IFUNC's have to be globally visible.  So, if the default_decl is
+     not, then the name of the IFUNC should be made unique.  */
+  if (TREE_PUBLIC (default_decl) == 0)
+    is_uniq = true;
+
+  /* Append the filename to the resolver function if the versions are
+     not externally visible.  This is because the resolver function has
+     to be externally visible for the loader to find it.  So, appending
+     the filename will prevent conflicts with a resolver function from
+     another module which is based on the same version name.  */
+  resolver_name = make_name (default_decl, "resolver", is_uniq);
+
+  /* The resolver function should return a (void *). */
+  type = build_function_type_list (ptr_type_node, NULL_TREE);
+
+  decl = build_fn_decl (resolver_name, type);
+  decl_name = get_identifier (resolver_name);
+  SET_DECL_ASSEMBLER_NAME (decl, decl_name);
+
+  DECL_NAME (decl) = decl_name;
+  TREE_USED (decl) = 1;
+  DECL_ARTIFICIAL (decl) = 1;
+  DECL_IGNORED_P (decl) = 0;
+  /* IFUNC resolvers have to be externally visible.  */
+  TREE_PUBLIC (decl) = 1;
+  DECL_UNINLINABLE (decl) = 1;
+
+  DECL_EXTERNAL (decl) = 0;
+  DECL_EXTERNAL (dispatch_decl) = 0;
+
+  DECL_CONTEXT (decl) = NULL_TREE;
+  DECL_INITIAL (decl) = make_node (BLOCK);
+  DECL_STATIC_CONSTRUCTOR (decl) = 0;
+  TREE_READONLY (decl) = 0;
+  DECL_PURE_P (decl) = 0;
+
+  if (DECL_COMDAT_GROUP (default_decl))
+    {
+      DECL_COMDAT (decl) = DECL_COMDAT (default_decl);
+      make_decl_one_only (decl, DECL_COMDAT_GROUP (default_decl));
+    }
+  else if (TREE_PUBLIC (default_decl))
+    {
+      /* In this case, each translation unit with a call to this
+	 versioned function will put out a resolver.  Ensure it
+	 is comdat to keep just one copy.  */
+      DECL_COMDAT (decl) = 1;
+      make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
+    }
+  /* Build result decl and add to function_decl. */
+  t = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, ptr_type_node);
+  DECL_ARTIFICIAL (t) = 1;
+  DECL_IGNORED_P (t) = 1;
+  DECL_RESULT (decl) = t;
+
+  gimplify_function_tree (decl);
+  old_current_function_decl = current_function_decl;
+  push_cfun (DECL_STRUCT_FUNCTION (decl));
+  current_function_decl = decl;
+  gimple_register_cfg_hooks ();
+  init_empty_tree_cfg_for_function (DECL_STRUCT_FUNCTION (decl));
+  cfun->curr_properties |=
+    (PROP_gimple_lcf | PROP_gimple_leh | PROP_cfg | PROP_ssa
+     | PROP_gimple_any);
+  cfun->curr_properties = 15;
+  new_bb = create_empty_bb (ENTRY_BLOCK_PTR);
+  make_edge (ENTRY_BLOCK_PTR, new_bb, EDGE_FALLTHRU);
+  make_edge (new_bb, EXIT_BLOCK_PTR, 0);
+  *empty_bb = new_bb;
+
+  cgraph_add_new_function (decl, true);
+  cgraph_call_function_insertion_hooks (cgraph_get_create_node (decl));
+
+  if (DECL_COMDAT_GROUP (default_decl))
+    {
+      gcc_assert (cgraph_get_node (default_decl));
+      symtab_add_to_same_comdat_group (
+	(symtab_node) cgraph_get_node (decl),
+	(symtab_node) cgraph_get_node (default_decl));
+    }
+
+  pop_cfun ();
+  current_function_decl = old_current_function_decl;
+
+  gcc_assert (dispatch_decl != NULL);
+  /* Mark dispatch_decl as "ifunc" with resolver as resolver_name.  */
+  DECL_ATTRIBUTES (dispatch_decl) 
+    = make_attribute ("ifunc", resolver_name, DECL_ATTRIBUTES (dispatch_decl));
+
+  /* Create the alias for dispatch to resolver here.  */
+  cgraph_create_function_alias (dispatch_decl, decl);
+  return decl;
+}
+
+/* Generate the dispatching code body to dispatch multi-versioned function
+   DECL.  The target hook is called to process the "target" attributes and
+   provide the code to dispatch the right function at run-time.  NODE points
+   to the dispatcher decl whose body will be created.  */
+
+static tree 
+ix86_generate_version_dispatcher_body (void *node_p)
+{
+  tree resolver_decl;
+  basic_block empty_bb;
+  VEC (tree, heap) *fn_ver_vec = NULL;
+  tree old_current_function_decl;
+  tree default_ver_decl;
+  struct cgraph_node *versn;
+  struct cgraph_node *node;
+
+  node = (cgraph_node *)node_p;
+
+  gcc_assert (node->dispatcher_function);
+
+  if (node->version_dispatcher_decl)
+    return node->version_dispatcher_decl;
+
+  default_ver_decl = node->next_function_version->symbol.decl;
+  resolver_decl = make_resolver_func (default_ver_decl,
+				      node->symbol.decl, &empty_bb);
+  node->version_dispatcher_decl = resolver_decl;
+
+  old_current_function_decl = current_function_decl;
+  push_cfun (DECL_STRUCT_FUNCTION (resolver_decl));
+  current_function_decl = resolver_decl;
+
+  fn_ver_vec = VEC_alloc (tree, heap, 2);
+
+  for (versn = node->next_function_version; versn;
+       versn = versn->next_function_version)
+    {
+      /* Check for virtual functions here again, as by this time it should
+	 have been determined if this function needs a vtable index or
+	 not.  This happens for methods in derived classes that override
+	 virtual methods in base classes but are not explicitly marked as
+	 virtual.  */
+      if (DECL_VINDEX (versn->symbol.decl))
+        error_at (DECL_SOURCE_LOCATION (versn->symbol.decl),
+		  "Virtual function multiversioning not supported");
+      VEC_safe_push (tree, heap, fn_ver_vec, versn->symbol.decl);
+    }
+
+  dispatch_function_versions(resolver_decl, fn_ver_vec, &empty_bb);
+
+  rebuild_cgraph_edges (); 
+  pop_cfun ();
+  current_function_decl = old_current_function_decl;
+  return resolver_decl;
+}
+
 /* This builds the processor_model struct type defined in
    libgcc/config/i386/cpuinfo.c  */
 
@@ -40652,6 +41628,20 @@  ix86_memmodel_check (unsigned HOST_WIDE_INT val)
 #undef TARGET_FOLD_BUILTIN
 #define TARGET_FOLD_BUILTIN ix86_fold_builtin
 
+#undef TARGET_COMPARE_VERSION_PRIORITY
+#define TARGET_COMPARE_VERSION_PRIORITY ix86_compare_version_priority
+
+#undef TARGET_SET_VERSION_ASSEMBLER_NAME
+#define TARGET_SET_VERSION_ASSEMBLER_NAME ix86_set_version_assembler_name
+
+#undef TARGET_GET_FUNCTION_VERSIONS_DISPATCHER
+#define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER \
+  ix86_get_function_versions_dispatcher
+
+#undef TARGET_GENERATE_VERSION_DISPATCHER_BODY
+#define TARGET_GENERATE_VERSION_DISPATCHER_BODY \
+  ix86_generate_version_dispatcher_body
+
 #undef TARGET_ENUM_VA_LIST_P
 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
 
@@ -40789,6 +41779,9 @@  ix86_memmodel_check (unsigned HOST_WIDE_INT val)
 #undef TARGET_OPTION_PRINT
 #define TARGET_OPTION_PRINT ix86_function_specific_print
 
+#undef TARGET_OPTION_FUNCTION_VERSIONS
+#define TARGET_OPTION_FUNCTION_VERSIONS ix86_function_versions
+
 #undef TARGET_CAN_INLINE_P
 #define TARGET_CAN_INLINE_P ix86_can_inline_p