]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/ipa-polymorphic-call.c
[Ada] Fix documentation for GNAT.Command_Line.Exit_From_Command_Line
[thirdparty/gcc.git] / gcc / ipa-polymorphic-call.c
index 8d9f22af0559ac4e74fddcc62c73049007f0aa89..705af03d20cebbe24234be717a76ffa285fb7066 100644 (file)
@@ -1,5 +1,5 @@
 /* Analysis of polymorphic call context.
-   Copyright (C) 2013-2016 Free Software Foundation, Inc.
+   Copyright (C) 2013-2019 Free Software Foundation, Inc.
    Contributed by Jan Hubicka
 
 This file is part of GCC.
@@ -112,12 +112,12 @@ possible_placement_new (tree type, tree expected_type,
    If the same is produced by multiple inheritance, we end up with A and offset
    sizeof(int). 
 
-   If we can not find corresponding class, give up by setting
+   If we cannot find corresponding class, give up by setting
    THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL. 
    Return true when lookup was sucesful.
 
    When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
-   valid only via alocation of new polymorphic type inside by means
+   valid only via allocation of new polymorphic type inside by means
    of placement new.
 
    When CONSIDER_BASES is false, only look for actual fields, not base types
@@ -146,7 +146,7 @@ ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
     derived from OUTER_TYPE.
 
     Because the instance type may contain field whose type is of OUTER_TYPE,
-    we can not derive any effective information about it.
+    we cannot derive any effective information about it.
 
     TODO: In the case we know all derrived types, we can definitely do better
     here.  */
@@ -191,7 +191,7 @@ ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
       tree fld;
 
       /* If we do not know size of TYPE, we need to be more conservative
-         about accepting cases where we can not find EXPECTED_TYPE.
+         about accepting cases where we cannot find EXPECTED_TYPE.
         Generally the types that do matter here are of constant size.
         Size_unknown case should be very rare.  */
       if (TYPE_SIZE (type)
@@ -233,7 +233,7 @@ ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
                  && type_known_to_have_no_derivations_p (outer_type))
                maybe_derived_type = false;
 
-             /* Type can not contain itself on an non-zero offset.  In that case
+             /* Type cannot contain itself on an non-zero offset.  In that case
                 just give up.  Still accept the case where size is now known.
                 Either the second copy may appear past the end of type or within
                 the non-POD buffer located inside the variably sized type
@@ -267,7 +267,8 @@ ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
        {
          for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
            {
-             if (TREE_CODE (fld) != FIELD_DECL)
+             if (TREE_CODE (fld) != FIELD_DECL
+                 || TREE_TYPE (fld) == error_mark_node)
                continue;
 
              pos = int_bit_position (fld);
@@ -286,7 +287,7 @@ ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
              size = tree_to_uhwi (DECL_SIZE (fld));
 
              /* We can always skip types smaller than pointer size:
-                those can not contain a virtual table pointer.
+                those cannot contain a virtual table pointer.
 
                 Disqualifying fields that are too small to fit OTR_TYPE
                 saves work needed to walk them for no benefit.
@@ -463,12 +464,12 @@ contains_type_p (tree outer_type, HOST_WIDE_INT offset,
   /* Check that type is within range.  */
   if (offset < 0)
     return false;
-  if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
-      && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
-      && TREE_CODE (TYPE_SIZE (otr_type)) == INTEGER_CST
-      && wi::ltu_p (wi::to_offset (TYPE_SIZE (outer_type)),
-                   (wi::to_offset (TYPE_SIZE (otr_type)) + offset)))
-    return false;
+
+  /* PR ipa/71207
+     As OUTER_TYPE can be a type which has a diamond virtual inheritance,
+     it's not necessary that INNER_TYPE will fit within OUTER_TYPE with
+     a given offset.  It can happen that INNER_TYPE also contains a base object,
+     however it would point to the same instance in the OUTER_TYPE.  */
 
   context.offset = offset;
   context.outer_type = TYPE_MAIN_VARIANT (outer_type);
@@ -551,7 +552,7 @@ decl_maybe_in_construction_p (tree base, tree outer_type,
   if (DECL_STRUCT_FUNCTION (function)->after_inlining)
     return true;
 
-  /* Pure functions can not do any changes on the dynamic type;
+  /* Pure functions cannot do any changes on the dynamic type;
      that require writting to memory.  */
   if ((!base || !auto_var_in_fn_p (base, function))
       && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
@@ -687,8 +688,8 @@ ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
 /* Stream in the context from IB and DATA_IN.  */
 
 void
-ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
-                                        struct data_in *data_in)
+ipa_polymorphic_call_context::stream_in (class lto_input_block *ib,
+                                        class data_in *data_in)
 {
   struct bitpack_d bp = streamer_read_bitpack (ib);
 
@@ -758,7 +759,7 @@ ipa_polymorphic_call_context::set_by_invariant (tree cst,
                                                tree otr_type,
                                                HOST_WIDE_INT off)
 {
-  HOST_WIDE_INT offset2, size, max_size;
+  poly_int64 offset2, size, max_size;
   bool reverse;
   tree base;
 
@@ -771,7 +772,7 @@ ipa_polymorphic_call_context::set_by_invariant (tree cst,
 
   cst = TREE_OPERAND (cst, 0);
   base = get_ref_base_and_extent (cst, &offset2, &size, &max_size, &reverse);
-  if (!DECL_P (base) || max_size == -1 || max_size != size)
+  if (!DECL_P (base) || !known_size_p (max_size) || maybe_ne (max_size, size))
     return false;
 
   /* Only type inconsistent programs can have otr_type that is
@@ -898,31 +899,35 @@ ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
       base_pointer = walk_ssa_copies (base_pointer, &visited);
       if (TREE_CODE (base_pointer) == ADDR_EXPR)
        {
-         HOST_WIDE_INT size, max_size;
-         HOST_WIDE_INT offset2;
+         HOST_WIDE_INT offset2, size;
          bool reverse;
          tree base
-           = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
-                                      &offset2, &size, &max_size, &reverse);
+           = get_ref_base_and_extent_hwi (TREE_OPERAND (base_pointer, 0),
+                                          &offset2, &size, &reverse);
+         if (!base)
+           break;
 
-         if (max_size != -1 && max_size == size)
-           combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
-                                     offset + offset2,
-                                     true,
-                                     NULL /* Do not change outer type.  */);
+         combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
+                                   offset + offset2,
+                                   true,
+                                   NULL /* Do not change outer type.  */);
 
          /* If this is a varying address, punt.  */
-         if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
-             && max_size != -1
-             && max_size == size)
+         if (TREE_CODE (base) == MEM_REF || DECL_P (base))
            {
              /* We found dereference of a pointer.  Type of the pointer
                 and MEM_REF is meaningless, but we can look futher.  */
-             if (TREE_CODE (base) == MEM_REF)
+             offset_int mem_offset;
+             if (TREE_CODE (base) == MEM_REF
+                 && mem_ref_offset (base).is_constant (&mem_offset))
                {
+                 offset_int o = mem_offset * BITS_PER_UNIT;
+                 o += offset;
+                 o += offset2;
+                 if (!wi::fits_shwi_p (o))
+                   break;
                  base_pointer = TREE_OPERAND (base, 0);
-                 offset
-                   += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
+                 offset = o.to_shwi ();
                  outer_type = NULL;
                }
              /* We found base object.  In this case the outer_type
@@ -960,10 +965,16 @@ ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
            break;
        }
       else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
-              && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
+              && TREE_CODE (TREE_OPERAND (base_pointer, 1)) == INTEGER_CST)
        {
-         offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
-                   * BITS_PER_UNIT;
+         offset_int o
+           = offset_int::from (wi::to_wide (TREE_OPERAND (base_pointer, 1)),
+                               SIGNED);
+         o *= BITS_PER_UNIT;
+         o += offset;
+         if (!wi::fits_shwi_p (o))
+           break;
+         offset = o.to_shwi ();
          base_pointer = TREE_OPERAND (base_pointer, 0);
        }
       else
@@ -984,9 +995,22 @@ ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
        {
          outer_type
             = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
+         cgraph_node *node = cgraph_node::get (current_function_decl);
          gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
                      || TREE_CODE (outer_type) == UNION_TYPE);
 
+         /* Handle the case we inlined into a thunk.  In this case
+            thunk has THIS pointer of type bar, but it really receives
+            address to its base type foo which sits in bar at 
+            0-thunk.fixed_offset.  It starts with code that adds
+            think.fixed_offset to the pointer to compensate for this.
+
+            Because we walked all the way to the begining of thunk, we now
+            see pointer &bar-thunk.fixed_offset and need to compensate
+            for it.  */
+         if (node->thunk.fixed_offset)
+           offset -= node->thunk.fixed_offset * BITS_PER_UNIT;
+
          /* Dynamic casting has possibly upcasted the type
             in the hiearchy.  In this case outer type is less
             informative than inner type and we should forget
@@ -994,7 +1018,11 @@ ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
          if ((otr_type
               && !contains_type_p (outer_type, offset,
                                    otr_type))
-             || !contains_polymorphic_type_p (outer_type))
+             || !contains_polymorphic_type_p (outer_type)
+             /* If we compile thunk with virtual offset, the THIS pointer
+                is adjusted by unknown value.  We can't thus use outer info
+                at all.  */
+             || node->thunk.virtual_offset_p)
            {
              outer_type = NULL;
              if (instance)
@@ -1019,7 +1047,15 @@ ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
              maybe_in_construction = false;
            }
          if (instance)
-           *instance = base_pointer;
+           {
+             /* If method is expanded thunk, we need to apply thunk offset
+                to instance pointer.  */
+             if (node->thunk.virtual_offset_p
+                 || node->thunk.fixed_offset)
+               *instance = NULL;
+             else
+               *instance = base_pointer;
+           }
          return;
        }
       /* Non-PODs passed by value are really passed by invisible
@@ -1138,7 +1174,7 @@ noncall_stmt_may_be_vtbl_ptr_store (gimple *stmt)
          if (TREE_CODE (lhs) == COMPONENT_REF
              && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
            return false;
-         /* In the future we might want to use get_base_ref_and_offset to find
+         /* In the future we might want to use get_ref_base_and_extent to find
             if there is a field corresponding to the offset and if so, proceed
             almost like if it was a component ref.  */
        }
@@ -1170,7 +1206,7 @@ static tree
 extr_type_from_vtbl_ptr_store (gimple *stmt, struct type_change_info *tci,
                               HOST_WIDE_INT *type_offset)
 {
-  HOST_WIDE_INT offset, size, max_size;
+  poly_int64 offset, size, max_size;
   tree lhs, rhs, base;
   bool reverse;
 
@@ -1252,17 +1288,23 @@ extr_type_from_vtbl_ptr_store (gimple *stmt, struct type_change_info *tci,
            }
          return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
        }
-      if (offset != tci->offset
-         || size != POINTER_SIZE
-         || max_size != POINTER_SIZE)
+      if (maybe_ne (offset, tci->offset)
+         || maybe_ne (size, POINTER_SIZE)
+         || maybe_ne (max_size, POINTER_SIZE))
        {
          if (dump_file)
-           fprintf (dump_file, "    wrong offset %i!=%i or size %i\n",
-                    (int)offset, (int)tci->offset, (int)size);
-         return offset + POINTER_SIZE <= tci->offset
-                || (max_size != -1
-                    && tci->offset + POINTER_SIZE > offset + max_size)
-                ? error_mark_node : NULL;
+           {
+             fprintf (dump_file, "    wrong offset ");
+             print_dec (offset, dump_file);
+             fprintf (dump_file, "!=%i or size ", (int) tci->offset);
+             print_dec (size, dump_file);
+             fprintf (dump_file, "\n");
+           }
+         return (known_le (offset + POINTER_SIZE, tci->offset)
+                 || (known_size_p (max_size)
+                     && known_gt (tci->offset + POINTER_SIZE,
+                                  offset + max_size))
+                 ? error_mark_node : NULL);
        }
     }
 
@@ -1392,26 +1434,27 @@ check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
       {
        tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
        tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
-       HOST_WIDE_INT offset = 0, size, max_size;
+       HOST_WIDE_INT offset = 0;
        bool reverse;
 
        if (dump_file)
          {
            fprintf (dump_file, "  Checking constructor call: ");
-           print_gimple_stmt (dump_file, stmt, 0, 0);
+           print_gimple_stmt (dump_file, stmt, 0);
          }
 
        /* See if THIS parameter seems like instance pointer.  */
        if (TREE_CODE (op) == ADDR_EXPR)
          {
-           op = get_ref_base_and_extent (TREE_OPERAND (op, 0), &offset,
-                                         &size, &max_size, &reverse);
-           if (size != max_size || max_size == -1)
+           HOST_WIDE_INT size;
+           op = get_ref_base_and_extent_hwi (TREE_OPERAND (op, 0),
+                                             &offset, &size, &reverse);
+           if (!op)
              {
                 tci->speculative++;
                return csftc_abort_walking_p (tci->speculative);
              }
-           if (op && TREE_CODE (op) == MEM_REF)
+           if (TREE_CODE (op) == MEM_REF)
              {
                if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
                  {
@@ -1460,7 +1503,7 @@ check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
      if (dump_file)
        {
           fprintf (dump_file, "  Function call may change dynamic type:");
-         print_gimple_stmt (dump_file, stmt, 0, 0);
+         print_gimple_stmt (dump_file, stmt, 0);
        }
      tci->speculative++;
      return csftc_abort_walking_p (tci->speculative);
@@ -1473,7 +1516,7 @@ check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
       if (dump_file)
        {
          fprintf (dump_file, "  Checking vtbl store: ");
-         print_gimple_stmt (dump_file, stmt, 0, 0);
+         print_gimple_stmt (dump_file, stmt, 0);
        }
 
       type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
@@ -1511,13 +1554,18 @@ check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
 
    We do not include this analysis in the context analysis itself, because
    it needs memory SSA to be fully built and the walk may be expensive.
-   So it is not suitable for use withing fold_stmt and similar uses.  */
+   So it is not suitable for use withing fold_stmt and similar uses.
+
+   AA_WALK_BUDGET_P, if not NULL, is how statements we should allow
+   walk_aliased_vdefs to examine.  The value should be decremented by the
+   number of stetements we examined or set to zero if exhausted.  */
 
 bool
 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
                                                tree otr_object,
                                                tree otr_type,
-                                               gimple *call)
+                                               gimple *call,
+                                               unsigned *aa_walk_budget_p)
 {
   struct type_change_info tci;
   ao_ref ao;
@@ -1529,6 +1577,9 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
   HOST_WIDE_INT instance_offset = offset;
   tree instance_outer_type = outer_type;
 
+  if (!instance)
+    return false;
+
   if (otr_type)
     otr_type = TYPE_MAIN_VARIANT (otr_type);
 
@@ -1567,7 +1618,6 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
   if (gimple_code (call) == GIMPLE_CALL)
     {
       tree ref = gimple_call_fn (call);
-      HOST_WIDE_INT offset2, size, max_size;
       bool reverse;
 
       if (TREE_CODE (ref) == OBJ_TYPE_REF)
@@ -1597,10 +1647,11 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
                  && !SSA_NAME_IS_DEFAULT_DEF (ref)
                  && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
                {
+                 HOST_WIDE_INT offset2, size;
                  tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
                  tree base_ref
-                   = get_ref_base_and_extent (ref_exp, &offset2, &size,
-                                              &max_size, &reverse);
+                   = get_ref_base_and_extent_hwi (ref_exp, &offset2,
+                                                  &size, &reverse);
 
                  /* Finally verify that what we found looks like read from
                     OTR_OBJECT or from INSTANCE with offset OFFSET.  */
@@ -1653,9 +1704,9 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
   if (dump_file)
     {
       fprintf (dump_file, "Determining dynamic type for call: ");
-      print_gimple_stmt (dump_file, call, 0, 0);
+      print_gimple_stmt (dump_file, call, 0);
       fprintf (dump_file, "  Starting walk at: ");
-      print_gimple_stmt (dump_file, stmt, 0, 0);
+      print_gimple_stmt (dump_file, stmt, 0);
       fprintf (dump_file, "  instance pointer: ");
       print_generic_expr (dump_file, otr_object, TDF_SLIM);
       fprintf (dump_file, "  Outer instance pointer: ");
@@ -1677,8 +1728,13 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
   tci.speculative = 0;
   tci.seen_unanalyzed_store = false;
 
-  walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
-                     &tci, NULL, &function_entry_reached);
+  unsigned aa_walk_budget = 0;
+  if (aa_walk_budget_p)
+    aa_walk_budget = *aa_walk_budget_p + 1;
+
+  int walked
+   = walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
+                        &tci, NULL, &function_entry_reached, aa_walk_budget);
 
   /* If we did not find any type changing statements, we may still drop
      maybe_in_construction flag if the context already have outer type. 
@@ -1699,7 +1755,7 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
      sub-objects and the code written by the user is run.  Only this may
      include calling virtual functions, directly or indirectly.
 
-     4) placement new can not be used to change type of non-POD statically
+     4) placement new cannot be used to change type of non-POD statically
      allocated variables.
 
      There is no way to call a constructor of an ancestor sub-object in any
@@ -1726,6 +1782,16 @@ ipa_polymorphic_call_context::get_dynamic_type (tree instance,
      only if there was dyanmic type store that may affect given variable
      (seen_unanalyzed_store)  */
 
+  if (walked < 0)
+    {
+      if (dump_file)
+       fprintf (dump_file, "  AA walk budget exhausted.\n");
+      *aa_walk_budget_p = 0;
+      return false;
+    }
+  else if (aa_walk_budget_p)
+    *aa_walk_budget_p -= walked;
+
   if (!tci.type_maybe_changed
       || (outer_type
          && !dynamic
@@ -2156,8 +2222,8 @@ ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
          updated = true;
        }
 
-      /* If we do not know how the context is being used, we can
-        not clear MAYBE_IN_CONSTRUCTION because it may be offseted
+      /* If we do not know how the context is being used, we cannot
+        clear MAYBE_IN_CONSTRUCTION because it may be offseted
         to other component of OUTER_TYPE later and we know nothing
         about it.  */
       if (otr_type && maybe_in_construction
@@ -2287,7 +2353,7 @@ ipa_polymorphic_call_context::make_speculative (tree otr_type)
                            otr_type);
 }
 
-/* Use when we can not track dynamic type change.  This speculatively assume
+/* Use when we cannot track dynamic type change.  This speculatively assume
    type change is not happening.  */
 
 void