Fold &MEM[0 + CST]->a.b.c to a constant
authorRichard Biener <rguenther@suse.de>
Tue, 14 Jan 2020 14:25:26 +0000 (15:25 +0100)
committerRichard Biener <rguenther@suse.de>
Tue, 12 May 2020 06:27:59 +0000 (08:27 +0200)
This canonicalizes those to a constant literal.

2020-05-12  Richard Biener  <rguenther@suse.de>

* gimple-fold.c (maybe_canonicalize_mem_ref_addr): Canonicalize
literal constant &MEM[..] to a constant literal.

gcc/ChangeLog
gcc/gimple-fold.c

index 9b8b5aee9f6779f2ec1e5f3cb453086fcdc9ccb6..bd84f8f73f07e4f8e1b3049bb3e17b057638948c 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-12  Richard Biener  <rguenther@suse.de>
+
+       * gimple-fold.c (maybe_canonicalize_mem_ref_addr): Canonicalize
+       literal constant &MEM[..] to a constant literal.
+
 2020-05-12  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/95045
index 55b78fa284f101df3f5e9a1b59e887f5a5fdc323..4e3de95d2d211cbe9e652af108364f6b4db3bfa9 100644 (file)
@@ -4840,6 +4840,7 @@ static bool
 maybe_canonicalize_mem_ref_addr (tree *t)
 {
   bool res = false;
+  tree *orig_t = t;
 
   if (TREE_CODE (*t) == ADDR_EXPR)
     t = &TREE_OPERAND (*t, 0);
@@ -4940,6 +4941,31 @@ maybe_canonicalize_mem_ref_addr (tree *t)
        }
     }
 
+  else if (TREE_CODE (*orig_t) == ADDR_EXPR
+          && TREE_CODE (*t) == MEM_REF
+          && TREE_CODE (TREE_OPERAND (*t, 0)) == INTEGER_CST)
+    {
+      tree base;
+      poly_int64 coffset;
+      base = get_addr_base_and_unit_offset (TREE_OPERAND (*orig_t, 0),
+                                           &coffset);
+      if (base)
+       {
+         gcc_assert (TREE_CODE (base) == MEM_REF);
+         poly_int64 moffset;
+         if (mem_ref_offset (base).to_shwi (&moffset))
+           {
+             coffset += moffset;
+             if (wi::to_poly_wide (TREE_OPERAND (base, 0)).to_shwi (&moffset))
+               {
+                 coffset += moffset;
+                 *orig_t = build_int_cst (TREE_TYPE (*orig_t), coffset);
+                 return true;
+               }
+           }
+       }
+    }
+
   /* Canonicalize TARGET_MEM_REF in particular with respect to
      the indexes becoming constant.  */
   else if (TREE_CODE (*t) == TARGET_MEM_REF)