From c6bb54c8ca5d629d094987ce603efaa1ee901538 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Sun, 22 Sep 2024 02:27:27 +0800 Subject: [PATCH] [optimize.d] use early return --- compiler/src/dmd/optimize.d | 197 ++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 98 deletions(-) diff --git a/compiler/src/dmd/optimize.d b/compiler/src/dmd/optimize.d index e409a7907e9..43aa76f68a6 100644 --- a/compiler/src/dmd/optimize.d +++ b/compiler/src/dmd/optimize.d @@ -79,103 +79,106 @@ Expression expandVar(int result, VarDeclaration v) return nullReturn(); if (!v.originalType && v.semanticRun < PASS.semanticdone) // semantic() not yet run v.dsymbolSemantic(null); - if (v.type && - (v.isConst() || v.isImmutable() || v.storage_class & STC.manifest)) + if (!v.type) + nullReturn(); + + if ((!v.isConst() && !v.isImmutable() && !(v.storage_class & STC.manifest))) + nullReturn(); + + Type tb = v.type.toBasetype(); + if (v.storage_class & STC.manifest || + tb.isScalar() || + ((result & WANTexpand) && (tb.ty != Tsarray && tb.ty != Tstruct))) { - Type tb = v.type.toBasetype(); - if (v.storage_class & STC.manifest || - tb.isScalar() || - ((result & WANTexpand) && (tb.ty != Tsarray && tb.ty != Tstruct))) + if (v._init) { - if (v._init) + if (v.inuse) { - if (v.inuse) + if (v.storage_class & STC.manifest) { - if (v.storage_class & STC.manifest) - { - .error(v.loc, "%s `%s` recursive initialization of constant", v.kind, v.toPrettyChars); - return errorReturn(); - } - return nullReturn(); + .error(v.loc, "%s `%s` recursive initialization of constant", v.kind, v.toPrettyChars); + return errorReturn(); } - Expression ei = v.getConstInitializer(); - if (!ei) + return nullReturn(); + } + Expression ei = v.getConstInitializer(); + if (!ei) + { + if (v.storage_class & STC.manifest) + { + .error(v.loc, "%s `%s` enum cannot be initialized with `%s`", v.kind, v.toPrettyChars, dmd.hdrgen.toChars(v._init)); + return errorReturn(); + } + return nullReturn(); + } + if (ei.op == EXP.construct || ei.op == EXP.blit) + { + auto ae = cast(AssignExp)ei; + ei = ae.e2; + if (ei.isConst() == 1) { - if (v.storage_class & STC.manifest) - { - .error(v.loc, "%s `%s` enum cannot be initialized with `%s`", v.kind, v.toPrettyChars, dmd.hdrgen.toChars(v._init)); - return errorReturn(); - } - return nullReturn(); } - if (ei.op == EXP.construct || ei.op == EXP.blit) + else if (ei.op == EXP.string_) { - auto ae = cast(AssignExp)ei; - ei = ae.e2; - if (ei.isConst() == 1) - { - } - else if (ei.op == EXP.string_) - { - // https://issues.dlang.org/show_bug.cgi?id=14459 - // Do not constfold the string literal - // if it's typed as a C string, because the value expansion - // will drop the pointer identity. - if (!(result & WANTexpand) && ei.type.toBasetype().ty == Tpointer) - return nullReturn(); - } - else - return nullReturn(); - if (ei.type == v.type) - { - // const variable initialized with const expression - } - else if (ei.implicitConvTo(v.type) >= MATCH.constant) - { - // const var initialized with non-const expression - ei = ei.implicitCastTo(null, v.type); - ei = ei.expressionSemantic(null); - } - else + // https://issues.dlang.org/show_bug.cgi?id=14459 + // Do not constfold the string literal + // if it's typed as a C string, because the value expansion + // will drop the pointer identity. + if (!(result & WANTexpand) && ei.type.toBasetype().ty == Tpointer) return nullReturn(); } - else if (!(v.storage_class & STC.manifest) && - ei.isConst() != 1 && - ei.op != EXP.string_ && - ei.op != EXP.address) - { + else return nullReturn(); - } - - if (!ei.type) + if (ei.type == v.type) { - return nullReturn(); + // const variable initialized with const expression } - else + else if (ei.implicitConvTo(v.type) >= MATCH.constant) { - // Should remove the copy() operation by - // making all mods to expressions copy-on-write - return initializerReturn(ei.copy()); + // const var initialized with non-const expression + ei = ei.implicitCastTo(null, v.type); + ei = ei.expressionSemantic(null); } + else + return nullReturn(); + } + else if (!(v.storage_class & STC.manifest) && + ei.isConst() != 1 && + ei.op != EXP.string_ && + ei.op != EXP.address) + { + return nullReturn(); + } + + if (!ei.type) + { + return nullReturn(); } else { - // v does not have an initializer - version (all) - { - return nullReturn(); - } - else - { - // BUG: what if const is initialized in constructor? - auto e = v.type.defaultInit(); - e.loc = e1.loc; - return initializerReturn(e); - } + // Should remove the copy() operation by + // making all mods to expressions copy-on-write + return initializerReturn(ei.copy()); + } + } + else + { + // v does not have an initializer + version (all) + { + return nullReturn(); + } + else + { + // BUG: what if const is initialized in constructor? + auto e = v.type.defaultInit(); + e.loc = e1.loc; + return initializerReturn(e); } - assert(0); } + assert(0); } + return nullReturn(); } @@ -183,31 +186,29 @@ private Expression fromConstInitializer(int result, Expression e1) { //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); //static int xx; if (xx++ == 10) assert(0); + auto ve = e1.isVarExp(); + if (!ve) + return e1; + Expression e = e1; - if (auto ve = e1.isVarExp()) + VarDeclaration v = ve.var.isVarDeclaration(); + e = expandVar(result, v); + if (!e) + return e1; + + // If it is a comma expression involving a declaration, we mustn't + // perform a copy -- we'd get two declarations of the same variable. + // See https://issues.dlang.org/show_bug.cgi?id=4465. + if (e.op == EXP.comma && e.isCommaExp().e1.isDeclarationExp()) + e = e1; + else if (e.type != e1.type && e1.type && e1.type.ty != Tident) { - VarDeclaration v = ve.var.isVarDeclaration(); - e = expandVar(result, v); - if (e) - { - // If it is a comma expression involving a declaration, we mustn't - // perform a copy -- we'd get two declarations of the same variable. - // See https://issues.dlang.org/show_bug.cgi?id=4465. - if (e.op == EXP.comma && e.isCommaExp().e1.isDeclarationExp()) - e = e1; - else if (e.type != e1.type && e1.type && e1.type.ty != Tident) - { - // Type 'paint' operation - e = e.copy(); - e.type = e1.type; - } - e.loc = e1.loc; - } - else - { - e = e1; - } + // Type 'paint' operation + e = e.copy(); + e.type = e1.type; } + e.loc = e1.loc; + return e; }