change initialization of ptrdiff_type_node

Message ID CAAgBjM=uUTFhCK5w4KXq3uaRVB4oA+8fWAvgbYuRY+c0RKpi6Q@mail.gmail.com
State New
Headers show

Commit Message

Prathamesh Kulkarni Nov. 25, 2016, 9:11 a.m.
On 25 November 2016 at 13:43, Richard Biener <rguenther@suse.de> wrote:
> On Fri, 25 Nov 2016, Jakub Jelinek wrote:

>

>> On Fri, Nov 25, 2016 at 01:28:06PM +0530, Prathamesh Kulkarni wrote:

>> > --- a/gcc/lto/lto-lang.c

>> > +++ b/gcc/lto/lto-lang.c

>> > @@ -1271,8 +1271,30 @@ lto_init (void)

>> >    gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)

>> >           == const_ptr_type_node);

>> >

>> > -  ptrdiff_type_node = integer_type_node;

>> > +  if (strcmp (PTRDIFF_TYPE, "int") == 0)

>> > +    ptrdiff_type_node = integer_type_node;

>> > +  else if (strcmp (PTRDIFF_TYPE, "long int") == 0)

>> > +    ptrdiff_type_node = long_integer_type_node;

>> > +  else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)

>> > +    ptrdiff_type_node = long_long_integer_type_node;

>> > +  else if (strcmp (PTRDIFF_TYPE, "short int") == 0)

>> > +    ptrdiff_type_node = short_integer_type_node;

>> > +  else

>> > +    {

>> > +      ptrdiff_type_node = NULL_TREE;

>> > +      for (int i = 0; i < NUM_INT_N_ENTS; i++)

>> > +   if (int_n_enabled_p[i])

>> > +     {

>> > +       char name[50];

>> > +       sprintf (name, "__int%d", int_n_data[i].bitsize);

>> > +       if (strcmp (name, PTRDIFF_TYPE) == 0)

>> > +         ptrdiff_type_node = int_n_trees[i].signed_type;

>> > +     }

>> > +      if (ptrdiff_type_node == NULL_TREE)

>> > +   gcc_unreachable ();

>> > +    }

>>

>> This looks ok to me.

>

> But I'd like to see this in build_common_tree_nodes alongside

> the initialization of size_type_node (and thus removed from

> c_common_nodes_and_builtins).  This way you can simply remove

> the lto-lang.c code as well.

>

> Please then also remove the ptrdiff_type_node re-set from

> free_lang_data ().

Hi Richard,
Does this version look OK ?
Validation in progress.

Thanks,
Prathamesh
>

>> >

>> > +  unsigned_ptrdiff_type_node = unsigned_type_for (ptrdiff_type_node);

>> >    lto_build_c_type_nodes ();

>> >    gcc_assert (va_list_type_node);

>>

>> But why this and the remaining hunks?  Nothing in the middle-end

>> needs it, IMHO it should be kept in c-family/.

>

> Yeah, this change looks unnecessary to me.

>

>> > diff --git a/gcc/tree-core.h b/gcc/tree-core.h

>> > index eec2d4f..6c52387 100644

>> > --- a/gcc/tree-core.h

>> > +++ b/gcc/tree-core.h

>> > @@ -617,6 +617,7 @@ enum tree_index {

>> >    TI_SIZE_TYPE,

>> >    TI_PID_TYPE,

>> >    TI_PTRDIFF_TYPE,

>> > +  TI_UNSIGNED_PTRDIFF_TYPE,

>> >    TI_VA_LIST_TYPE,

>> >    TI_VA_LIST_GPR_COUNTER_FIELD,

>> >    TI_VA_LIST_FPR_COUNTER_FIELD,

>> > diff --git a/gcc/tree.h b/gcc/tree.h

>> > index 62cd7bb..ae69d0d 100644

>> > --- a/gcc/tree.h

>> > +++ b/gcc/tree.h

>> > @@ -3667,6 +3667,7 @@ tree_operand_check_code (const_tree __t, enum tree_code __code, int __i,

>> >  #define size_type_node                  global_trees[TI_SIZE_TYPE]

>> >  #define pid_type_node                   global_trees[TI_PID_TYPE]

>> >  #define ptrdiff_type_node          global_trees[TI_PTRDIFF_TYPE]

>> > +#define unsigned_ptrdiff_type_node global_trees[TI_UNSIGNED_PTRDIFF_TYPE]

>> >  #define va_list_type_node          global_trees[TI_VA_LIST_TYPE]

>> >  #define va_list_gpr_counter_field  global_trees[TI_VA_LIST_GPR_COUNTER_FIELD]

>> >  #define va_list_fpr_counter_field  global_trees[TI_VA_LIST_FPR_COUNTER_FIELD]

>>

>>

>>       Jakub

>>

>>

>

> --

> Richard Biener <rguenther@suse.de>

> SUSE LINUX GmbH, GF: Felix Imendoerffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nuernberg)
2016-11-25  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

	* tree.c (build_common_tree_nodes): Initialize ptrdiff_type_node.
	(free_lang_data): Remove assignment to ptrdiff_type_node.
c-family/
	* c-common.c (c_common_nodes_and_builtins): Remove initialization of
	ptrdiff_type_node.
lto/
	* lto-lang.c (lto_init): Remove initialization of ptrdiff_type_node.

Comments

Richard Biener Nov. 25, 2016, 9:18 a.m. | #1
On Fri, 25 Nov 2016, Prathamesh Kulkarni wrote:

> On 25 November 2016 at 13:43, Richard Biener <rguenther@suse.de> wrote:

> > On Fri, 25 Nov 2016, Jakub Jelinek wrote:

> >

> >> On Fri, Nov 25, 2016 at 01:28:06PM +0530, Prathamesh Kulkarni wrote:

> >> > --- a/gcc/lto/lto-lang.c

> >> > +++ b/gcc/lto/lto-lang.c

> >> > @@ -1271,8 +1271,30 @@ lto_init (void)

> >> >    gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)

> >> >           == const_ptr_type_node);

> >> >

> >> > -  ptrdiff_type_node = integer_type_node;

> >> > +  if (strcmp (PTRDIFF_TYPE, "int") == 0)

> >> > +    ptrdiff_type_node = integer_type_node;

> >> > +  else if (strcmp (PTRDIFF_TYPE, "long int") == 0)

> >> > +    ptrdiff_type_node = long_integer_type_node;

> >> > +  else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)

> >> > +    ptrdiff_type_node = long_long_integer_type_node;

> >> > +  else if (strcmp (PTRDIFF_TYPE, "short int") == 0)

> >> > +    ptrdiff_type_node = short_integer_type_node;

> >> > +  else

> >> > +    {

> >> > +      ptrdiff_type_node = NULL_TREE;

> >> > +      for (int i = 0; i < NUM_INT_N_ENTS; i++)

> >> > +   if (int_n_enabled_p[i])

> >> > +     {

> >> > +       char name[50];

> >> > +       sprintf (name, "__int%d", int_n_data[i].bitsize);

> >> > +       if (strcmp (name, PTRDIFF_TYPE) == 0)

> >> > +         ptrdiff_type_node = int_n_trees[i].signed_type;

> >> > +     }

> >> > +      if (ptrdiff_type_node == NULL_TREE)

> >> > +   gcc_unreachable ();

> >> > +    }

> >>

> >> This looks ok to me.

> >

> > But I'd like to see this in build_common_tree_nodes alongside

> > the initialization of size_type_node (and thus removed from

> > c_common_nodes_and_builtins).  This way you can simply remove

> > the lto-lang.c code as well.

> >

> > Please then also remove the ptrdiff_type_node re-set from

> > free_lang_data ().

> Hi Richard,

> Does this version look OK ?

> Validation in progress.


Yes, patch is ok if testing succeeds.

Thanks,
Richard.
Prathamesh Kulkarni Nov. 26, 2016, 10:44 a.m. | #2
On 25 November 2016 at 14:48, Richard Biener <rguenther@suse.de> wrote:
> On Fri, 25 Nov 2016, Prathamesh Kulkarni wrote:

>

>> On 25 November 2016 at 13:43, Richard Biener <rguenther@suse.de> wrote:

>> > On Fri, 25 Nov 2016, Jakub Jelinek wrote:

>> >

>> >> On Fri, Nov 25, 2016 at 01:28:06PM +0530, Prathamesh Kulkarni wrote:

>> >> > --- a/gcc/lto/lto-lang.c

>> >> > +++ b/gcc/lto/lto-lang.c

>> >> > @@ -1271,8 +1271,30 @@ lto_init (void)

>> >> >    gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)

>> >> >           == const_ptr_type_node);

>> >> >

>> >> > -  ptrdiff_type_node = integer_type_node;

>> >> > +  if (strcmp (PTRDIFF_TYPE, "int") == 0)

>> >> > +    ptrdiff_type_node = integer_type_node;

>> >> > +  else if (strcmp (PTRDIFF_TYPE, "long int") == 0)

>> >> > +    ptrdiff_type_node = long_integer_type_node;

>> >> > +  else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)

>> >> > +    ptrdiff_type_node = long_long_integer_type_node;

>> >> > +  else if (strcmp (PTRDIFF_TYPE, "short int") == 0)

>> >> > +    ptrdiff_type_node = short_integer_type_node;

>> >> > +  else

>> >> > +    {

>> >> > +      ptrdiff_type_node = NULL_TREE;

>> >> > +      for (int i = 0; i < NUM_INT_N_ENTS; i++)

>> >> > +   if (int_n_enabled_p[i])

>> >> > +     {

>> >> > +       char name[50];

>> >> > +       sprintf (name, "__int%d", int_n_data[i].bitsize);

>> >> > +       if (strcmp (name, PTRDIFF_TYPE) == 0)

>> >> > +         ptrdiff_type_node = int_n_trees[i].signed_type;

>> >> > +     }

>> >> > +      if (ptrdiff_type_node == NULL_TREE)

>> >> > +   gcc_unreachable ();

>> >> > +    }

>> >>

>> >> This looks ok to me.

>> >

>> > But I'd like to see this in build_common_tree_nodes alongside

>> > the initialization of size_type_node (and thus removed from

>> > c_common_nodes_and_builtins).  This way you can simply remove

>> > the lto-lang.c code as well.

>> >

>> > Please then also remove the ptrdiff_type_node re-set from

>> > free_lang_data ().

>> Hi Richard,

>> Does this version look OK ?

>> Validation in progress.

>

> Yes, patch is ok if testing succeeds.

Thanks, the patch passes bootstrap+test on x86_64-unknown-linux-gnu
with --enable-languages=all,ada
and cross-tested on arm*-*-*, aarch64*-*-* with
--enable-languages=c,c++,fortran.

However LTO bootstrap fails with miscompares (attached)
configured with: --disable-werror --enable-stage1-checking=release
--with-build-config=bootstrap-lto
I verified that the same miscompares happen without the patch too, and
have committed it as r242888.

Thanks,
Prathamesh
>

> Thanks,

> Richard.
gcc/tree-ssa-phiopt.o differs
gcc/sanopt.o differs
gcc/tree-ssa-loop-ivcanon.o differs
gcc/gcc.o differs
gcc/lra.o differs
gcc/tree-ssa-loop-manip.o differs
gcc/tree.o differs
gcc/tree-ssa-dce.o differs
gcc/gcse.o differs
gcc/gimple-ssa-strength-reduction.o differs
gcc/ipa-split.o differs
gcc/ipa.o differs
gcc/cfgexpand.o differs
gcc/recog.o differs
gcc/tree-ssa-loop-niter.o differs
gcc/loop-doloop.o differs
gcc/combine.o differs
gcc/predict.o differs
gcc/dce.o differs
gcc/graphds.o differs
gcc/asan.o differs
gcc/tree-ssa.o differs
gcc/tree-ssa-loop-im.o differs
gcc/ipa-devirt.o differs
gcc/dbxout.o differs
gcc/combine-stack-adj.o differs
gcc/tree-ssa-live.o differs
gcc/sched-rgn.o differs
gcc/trans-mem.o differs
gcc/tree-ssa-loop-unswitch.o differs
gcc/haifa-sched.o differs
gcc/tree-diagnostic.o differs
gcc/tree-vect-stmts.o differs
gcc/collect2.o differs
gcc/tree-vect-data-refs.o differs
gcc/tree-ssa-operands.o differs
gcc/ipa-icf.o differs
gcc/tree-ssa-sccvn.o differs
gcc/tree-ssa-forwprop.o differs
gcc/tsan.o differs
gcc/gimple-ssa-store-merging.o differs
gcc/tree-parloops.o differs
gcc/tree-complex.o differs
gcc/tracer.o differs
gcc/tree-vect-slp.o differs
gcc/diagnostic-show-locus.o differs
gcc/hsa-gen.o differs
gcc/hsa.o differs
gcc/df-scan.o differs
gcc/gcse-common.o differs
gcc/tree-object-size.o differs
gcc/build/genextract.o differs
gcc/build/genpreds.o differs
gcc/build/read-rtl.o differs
gcc/build/gengtype-state.o differs
gcc/build/genattr.o differs
gcc/build/genopinit.o differs
gcc/build/genmatch.o differs
gcc/build/genrecog.o differs
gcc/build/gensupport.o differs
gcc/build/genautomata.o differs
gcc/tree-loop-distribution.o differs
gcc/gimplify.o differs
gcc/symtab.o differs
gcc/lto-wrapper.o differs
gcc/rtlanal.o differs
gcc/dse.o differs
gcc/cfgrtl.o differs
gcc/dwarf2out.o differs
gcc/ifcvt.o differs
gcc/ipa-inline-analysis.o differs
gcc/ira.o differs
gcc/hsa-brig.o differs
gcc/dwarf2cfi.o differs
gcc/gimple.o differs
gcc/sel-sched-dump.o differs
gcc/tree-ssa-uncprop.o differs
gcc/tree-eh.o differs
gcc/reg-stack.o differs
gcc/ggc-none.o differs
gcc/tree-ssa-tail-merge.o differs
gcc/loop-init.o differs
gcc/tree-vrp.o differs
gcc/emit-rtl.o differs
gcc/tree-vect-patterns.o differs
gcc/ipa-pure-const.o differs
gcc/ipa-prop.o differs
gcc/function.o differs
gcc/tree-vect-loop-manip.o differs
gcc/vtable-verify.o differs
gcc/tree-outof-ssa.o differs
gcc/edit-context.o differs
gcc/dominance.o differs
gcc/alias.o differs
gcc/df-core.o differs
gcc/tree-scalar-evolution.o differs
gcc/tree-ssa-pre.o differs
gcc/sese.o differs
gcc/ipa-icf-gimple.o differs
gcc/lra-constraints.o differs
gcc/calls.o differs
gcc/tree-into-ssa.o differs
gcc/loop-unroll.o differs
gcc/reload1.o differs
gcc/gengtype-state.o differs
gcc/opts-common.o differs
gcc/ipa-profile.o differs
gcc/bt-load.o differs
gcc/tree-chkp.o differs
gcc/cfghooks.o differs
gcc/tree-ssa-strlen.o differs
gcc/sched-deps.o differs
gcc/ipa-visibility.o differs
gcc/tree-ssa-loop.o differs
gcc/tree-vect-loop.o differs
gcc/ira-color.o differs
gcc/sel-sched-ir.o differs
gcc/omp-simd-clone.o differs
gcc/cp/constexpr.o differs
gcc/cp/vtable-class-hierarchy.o differs
gcc/cp/parser.o differs
gcc/cp/cp-array-notation.o differs
gcc/cp/class.o differs
gcc/cp/search.o differs
gcc/cp/semantics.o differs
gcc/cp/mangle.o differs
gcc/cp/name-lookup.o differs
gcc/cp/cp-gimplify.o differs
gcc/tree-stdarg.o differs
gcc/loop-iv.o differs
gcc/tree-ssa-loop-split.o differs
gcc/tree-ssa-threadupdate.o differs
gcc/tree-switch-conversion.o differs
gcc/graphite-poly.o differs
gcc/cgraphclones.o differs
gcc/tree-chkp-opt.o differs
gcc/tree-sra.o differs
gcc/tree-cfg.o differs
gcc/fortran/scanner.o differs
gcc/cfganal.o differs
gcc/c/c-parser.o differs
gcc/c/gimple-parser.o differs
gcc/c/c-decl.o differs
gcc/c/c-typeck.o differs
gcc/c/c-array-notation.o differs
gcc/tree-ssa-coalesce.o differs
gcc/shrink-wrap.o differs
gcc/tree-inline.o differs
gcc/tree-ssa-structalias.o differs
gcc/lto-streamer-out.o differs
gcc/sel-sched.o differs
gcc/tree-vectorizer.o differs
gcc/passes.o differs
gcc/tree-ssa-threadbackward.o differs
gcc/tree-ssa-alias.o differs
gcc/tree-dfa.o differs
gcc/lto-streamer-in.o differs
gcc/cfgloopmanip.o differs
gcc/cgraphunit.o differs
gcc/ggc-common.o differs
gcc/expr.o differs
gcc/value-prof.o differs
gcc/graphite-scop-detection.o differs
gcc/lto-section-out.o differs
gcc/ipa-cp.o differs
gcc/profile.o differs
gcc/cfgloop.o differs
gcc/tree-ssa-loop-ivopts.o differs
gcc/tree-ssa-loop-prefetch.o differs
gcc/lto-cgraph.o differs
gcc/ree.o differs
gcc/params.o differs
gcc/tree-ssa-threadedge.o differs
gcc/except.o differs
gcc/tree-ssa-loop-ch.o differs
gcc/objc/objc-act.o differs
gcc/lower-subreg.o differs
gcc/tree-data-ref.o differs
gcc/var-tracking.o differs
gcc/lto/lto.o differs
gcc/lto/lto-partition.o differs
gcc/ipa-inline.o differs
gcc/dumpfile.o differs
gcc/tree-if-conv.o differs
gcc/tree-ssa-reassoc.o differs
gcc/reginfo.o differs
gcc/tlink.o differs
gcc/input.o differs
gcc/tree-ssa-propagate.o differs
gcc/tree-call-cdce.o differs
gcc/cgraph.o differs
gcc/ddg.o differs
gcc/tree-predcom.o differs
gcc/tree-ssa-dom.o differs
gcc/ipa-utils.o differs
gcc/lra-lives.o differs
gcc/varasm.o differs
gcc/gimple-ssa-backprop.o differs
gcc/store-motion.o differs
gcc/i386.o differs
gcc/modulo-sched.o differs
gcc/tree-emutls.o differs
gcc/tree-ssa-uninit.o differs
gcc/opts-global.o differs
gcc/spellcheck.o differs
gcc/loop-invariant.o differs
gcc/coverage.o differs
gcc/ipa-reference.o differs
gcc/ira-build.o differs
gcc/opts.o differs
gcc/gcov-dump.o differs
gcc/c-family/cilk.o differs
gcc/c-family/c-omp.o differs
gcc/c-family/c-pragma.o differs
gcc/c-family/c-ada-spec.o differs
gcc/c-family/c-warn.o differs
gcc/c-family/array-notation-common.o differs
gcc/c-family/c-common.o differs
gcc/c-family/c-opts.o differs
gcc/c-family/c-format.o differs
gcc/libgcov-driver-tool.o differs
gcc/hsa-regalloc.o differs
gcc/diagnostic.o differs
gcc/tree-ssa-math-opts.o differs
gcc/cfgloopanal.o differs
gcc/gimple-low.o differs
gcc/ira-emit.o differs
libcpp/charset.o differs
Richard Biener Nov. 28, 2016, 11:48 a.m. | #3
On Sat, 26 Nov 2016, Prathamesh Kulkarni wrote:

> On 25 November 2016 at 14:48, Richard Biener <rguenther@suse.de> wrote:

> > On Fri, 25 Nov 2016, Prathamesh Kulkarni wrote:

> >

> >> On 25 November 2016 at 13:43, Richard Biener <rguenther@suse.de> wrote:

> >> > On Fri, 25 Nov 2016, Jakub Jelinek wrote:

> >> >

> >> >> On Fri, Nov 25, 2016 at 01:28:06PM +0530, Prathamesh Kulkarni wrote:

> >> >> > --- a/gcc/lto/lto-lang.c

> >> >> > +++ b/gcc/lto/lto-lang.c

> >> >> > @@ -1271,8 +1271,30 @@ lto_init (void)

> >> >> >    gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)

> >> >> >           == const_ptr_type_node);

> >> >> >

> >> >> > -  ptrdiff_type_node = integer_type_node;

> >> >> > +  if (strcmp (PTRDIFF_TYPE, "int") == 0)

> >> >> > +    ptrdiff_type_node = integer_type_node;

> >> >> > +  else if (strcmp (PTRDIFF_TYPE, "long int") == 0)

> >> >> > +    ptrdiff_type_node = long_integer_type_node;

> >> >> > +  else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)

> >> >> > +    ptrdiff_type_node = long_long_integer_type_node;

> >> >> > +  else if (strcmp (PTRDIFF_TYPE, "short int") == 0)

> >> >> > +    ptrdiff_type_node = short_integer_type_node;

> >> >> > +  else

> >> >> > +    {

> >> >> > +      ptrdiff_type_node = NULL_TREE;

> >> >> > +      for (int i = 0; i < NUM_INT_N_ENTS; i++)

> >> >> > +   if (int_n_enabled_p[i])

> >> >> > +     {

> >> >> > +       char name[50];

> >> >> > +       sprintf (name, "__int%d", int_n_data[i].bitsize);

> >> >> > +       if (strcmp (name, PTRDIFF_TYPE) == 0)

> >> >> > +         ptrdiff_type_node = int_n_trees[i].signed_type;

> >> >> > +     }

> >> >> > +      if (ptrdiff_type_node == NULL_TREE)

> >> >> > +   gcc_unreachable ();

> >> >> > +    }

> >> >>

> >> >> This looks ok to me.

> >> >

> >> > But I'd like to see this in build_common_tree_nodes alongside

> >> > the initialization of size_type_node (and thus removed from

> >> > c_common_nodes_and_builtins).  This way you can simply remove

> >> > the lto-lang.c code as well.

> >> >

> >> > Please then also remove the ptrdiff_type_node re-set from

> >> > free_lang_data ().

> >> Hi Richard,

> >> Does this version look OK ?

> >> Validation in progress.

> >

> > Yes, patch is ok if testing succeeds.

> Thanks, the patch passes bootstrap+test on x86_64-unknown-linux-gnu

> with --enable-languages=all,ada

> and cross-tested on arm*-*-*, aarch64*-*-* with

> --enable-languages=c,c++,fortran.

> 

> However LTO bootstrap fails with miscompares (attached)

> configured with: --disable-werror --enable-stage1-checking=release

> --with-build-config=bootstrap-lto

> I verified that the same miscompares happen without the patch too, and

> have committed it as r242888.


LTO bootstrap works fine for me (with checking enabled).

Richard.

Patch hide | download patch | download mbox

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 62174a9..0749361 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -4475,8 +4475,6 @@  c_common_nodes_and_builtins (void)
 
   default_function_type
     = build_varargs_function_type_list (integer_type_node, NULL_TREE);
-  ptrdiff_type_node
-    = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
   unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
 
   lang_hooks.decls.pushdecl
diff --git a/gcc/lto/lto-lang.c b/gcc/lto/lto-lang.c
index a5f04ba..58f6e0c 100644
--- a/gcc/lto/lto-lang.c
+++ b/gcc/lto/lto-lang.c
@@ -1271,8 +1271,6 @@  lto_init (void)
   gcc_assert (TYPE_MAIN_VARIANT (const_tm_ptr_type_node)
 	      == const_ptr_type_node);
 
-  ptrdiff_type_node = integer_type_node;
-
   lto_build_c_type_nodes ();
   gcc_assert (va_list_type_node);
 
diff --git a/gcc/tree.c b/gcc/tree.c
index 11e0abc..57f2e9a 100644
--- a/gcc/tree.c
+++ b/gcc/tree.c
@@ -6006,7 +6006,6 @@  free_lang_data (void)
   free_lang_data_in_cgraph ();
 
   /* Create gimple variants for common types.  */
-  ptrdiff_type_node = integer_type_node;
   fileptr_type_node = ptr_type_node;
   const_tm_ptr_type_node = const_ptr_type_node;
 
@@ -10314,6 +10313,30 @@  build_common_tree_nodes (bool signed_char)
 	gcc_unreachable ();
     }
 
+  /* Define what type to use for ptrdiff_t.  */
+  if (strcmp (PTRDIFF_TYPE, "int") == 0)
+    ptrdiff_type_node = integer_type_node;
+  else if (strcmp (PTRDIFF_TYPE, "long int") == 0)
+    ptrdiff_type_node = long_integer_type_node;
+  else if (strcmp (PTRDIFF_TYPE, "long long int") == 0)
+    ptrdiff_type_node = long_long_integer_type_node;
+  else if (strcmp (PTRDIFF_TYPE, "short int") == 0)
+    ptrdiff_type_node = short_integer_type_node;
+  else
+    {
+      ptrdiff_type_node = NULL_TREE;
+      for (int i = 0; i < NUM_INT_N_ENTS; i++)
+	if (int_n_enabled_p[i])
+	  {
+	    char name[50];
+	    sprintf (name, "__int%d", int_n_data[i].bitsize);
+	    if (strcmp (name, PTRDIFF_TYPE) == 0)
+	      ptrdiff_type_node = int_n_trees[i].signed_type;
+	  }
+      if (ptrdiff_type_node == NULL_TREE)
+	gcc_unreachable ();
+    }
+
   /* Fill in the rest of the sized types.  Reuse existing type nodes
      when possible.  */
   intQI_type_node = make_or_reuse_type (GET_MODE_BITSIZE (QImode), 0);