From bf3f49ac797216b8ebc72d5ece95a8c95743f418 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Wed, 18 Dec 2024 16:51:23 -0800 Subject: [PATCH] optimize s=s where s is a struct --- compiler/src/dmd/backend/cgelem.d | 7 +++++++ compiler/test/runnable/mars1.d | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/compiler/src/dmd/backend/cgelem.d b/compiler/src/dmd/backend/cgelem.d index 9576733e120..9cb055ae44d 100644 --- a/compiler/src/dmd/backend/cgelem.d +++ b/compiler/src/dmd/backend/cgelem.d @@ -3521,6 +3521,13 @@ elem * elstruct(elem *e, Goal goal) return optelem(e, goal); } + // Replace (e = e) with (e, e) + if (e.Eoper == OPstreq && el_match(e.E1, e.E2)) + { + e.Eoper = OPcomma; + return optelem(e, goal); + } + if (!e.ET) return e; //printf("\tnumbytes = %d\n", cast(int)type_size(e.ET)); diff --git a/compiler/test/runnable/mars1.d b/compiler/test/runnable/mars1.d index 8352f4d4fa9..7de4372237f 100644 --- a/compiler/test/runnable/mars1.d +++ b/compiler/test/runnable/mars1.d @@ -2507,6 +2507,26 @@ void test20574() //////////////////////////////////////////////////////////////////////// +struct S8 +{ + int x,y,z; +} + +int test8x(S8 s) +{ + s = s; + return s.y; +} + +void test8() +{ + S8 s; + s.y = 2; + assert(test8x(s) == 2); +} + +//////////////////////////////////////////////////////////////////////// + int main() { // All the various integer divide tests @@ -2607,6 +2627,7 @@ int main() test21835(); testDoWhileContinue(); test20574(); + test8(); printf("Success\n"); return 0;