diff mbox series

[16/17] RFC: allow expansion of accessed constants if 'static const'

Message ID 20191210225921.94897-17-luc.vanoostenryck@gmail.com (mailing list archive)
State Deferred, archived
Headers show
Series improve expansion of constant symbols | expand

Commit Message

Luc Van Oostenryck Dec. 10, 2019, 10:59 p.m. UTC
Currently, constant_symbol_value() safely refuses to convert
a symbol that is accessed.

But accessing a 'static const' object should be fine since
it should never be modified.

So, allow to also expand symbol marked as accessed but being
'static const'.

Note: a simple 'const' should be enough and 'static'
      doesn't make it 'more const'. In both cases, the
      object can be modified via a cast to a non-const
      pointer (which is UB). The only thing that 'static'
      changes is that *if* the object's address is not
      taken (so all accesses are const-expanded) then
      it's not needed to allocate memory for thsi object.

      OTOH, GCC also seems to do some these expansions only
      if the object is static const. So, I dunno.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c                                |  7 +++++--
 validation/expand/constant-init-array.c |  4 ++--
 validation/expand/constant-static.c     | 16 ++++++++++++++++
 validation/expand/default-init-array.c  |  1 -
 4 files changed, 23 insertions(+), 5 deletions(-)
 create mode 100644 validation/expand/constant-static.c
diff mbox series

Patch

diff --git a/expand.c b/expand.c
index 36612c8672dd..1885a4eaad2a 100644
--- a/expand.c
+++ b/expand.c
@@ -689,9 +689,12 @@  redo:
 static struct expression *constant_symbol_value(struct symbol *sym, int offset)
 {
 	struct expression *value;
+	unsigned long mods = sym->ctype.modifiers;
 
-	if (sym->ctype.modifiers & MOD_ACCESS)
-		return NULL;
+	if (mods & MOD_ACCESS) {
+		if ((mods & (MOD_STATIC | MOD_CONST)) != (MOD_STATIC | MOD_CONST))
+			return NULL;
+	}
 	value = sym->initializer;
 	if (!value)
 		return NULL;
diff --git a/validation/expand/constant-init-array.c b/validation/expand/constant-init-array.c
index 94949be54244..861885630f18 100644
--- a/validation/expand/constant-init-array.c
+++ b/validation/expand/constant-init-array.c
@@ -10,6 +10,6 @@  int test_array(int i)
  * check-command: test-linearize -Wno-decl -fdump-ir $file
  *
  * check-output-ignore
- * check-output-excludes: phisrc\\..*return.*\\$2
- * check-output-contains: load\\.
+ * check-output-contains: phisrc\\..*return.*\\$2
+ * check-output-excludes: load\\.
  */
diff --git a/validation/expand/constant-static.c b/validation/expand/constant-static.c
new file mode 100644
index 000000000000..df6673eeef47
--- /dev/null
+++ b/validation/expand/constant-static.c
@@ -0,0 +1,16 @@ 
+static const int a = 3;
+
+static int foo(void)
+{
+	int *p = (int*) &a;
+	return a;
+}
+
+/*
+ * check-name: constant-static
+ * check-command: test-linearize $file
+ *
+ * check-output-ignore
+ * check-output-contains: ret\\.32  *\\$3
+ * check-output-excludes: load\\.
+ */
diff --git a/validation/expand/default-init-array.c b/validation/expand/default-init-array.c
index b372ea09534b..5f2e44ef21a3 100644
--- a/validation/expand/default-init-array.c
+++ b/validation/expand/default-init-array.c
@@ -8,7 +8,6 @@  int test_array(int i)
 /*
  * check-name: default-init-array
  * check-command: test-linearize -Wno-decl -fdump-ir $file
- * check-known-to-fail
  *
  * check-output-ignore
  * check-output-contains: phisrc\\..*return.*\\$0