diff options
Diffstat (limited to 'gcc/d/dmd/optimize.d')
-rw-r--r-- | gcc/d/dmd/optimize.d | 106 |
1 files changed, 66 insertions, 40 deletions
diff --git a/gcc/d/dmd/optimize.d b/gcc/d/dmd/optimize.d index 2c89a58..66b8c6a 100644 --- a/gcc/d/dmd/optimize.d +++ b/gcc/d/dmd/optimize.d @@ -1,12 +1,12 @@ /** * Perform constant folding. * - * Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved + * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) - * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/optimize.d, _optimize.d) + * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/optimize.d, _optimize.d) * Documentation: https://dlang.org/phobos/dmd_optimize.html - * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/optimize.d + * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/optimize.d */ module dmd.optimize; @@ -84,7 +84,7 @@ Expression expandVar(int result, VarDeclaration v) { Type tb = v.type.toBasetype(); if (v.storage_class & STC.manifest || - tb.isscalar() || + tb.isScalar() || ((result & WANTexpand) && (tb.ty != Tsarray && tb.ty != Tstruct))) { if (v._init) @@ -110,7 +110,7 @@ Expression expandVar(int result, VarDeclaration v) } if (ei.op == EXP.construct || ei.op == EXP.blit) { - AssignExp ae = cast(AssignExp)ei; + auto ae = cast(AssignExp)ei; ei = ae.e2; if (ei.isConst() == 1) { @@ -183,31 +183,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; } @@ -372,10 +370,10 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) void visitStructLiteral(StructLiteralExp e) { - if (e.stageflags & stageOptimize) + if (e.stageflags & StructLiteralExp.StageFlags.optimize) return; const old = e.stageflags; - e.stageflags |= stageOptimize; + e.stageflags |= StructLiteralExp.StageFlags.optimize; if (e.elements) { foreach (ref ex; (*e.elements)[]) @@ -753,6 +751,7 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) void visitNew(NewExp e) { + expOptimize(e.placement, WANTvalue); expOptimize(e.thisexp, WANTvalue); // Optimize parameters if (e.arguments) @@ -1004,7 +1003,7 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) } } - extern (D) void shift_optimize(BinExp e, UnionExp function(const ref Loc, Type, Expression, Expression) shift) + extern (D) void shift_optimize(BinExp e, UnionExp function(Loc, Type, Expression, Expression) shift) { if (binOptimize(e, result)) return; @@ -1071,7 +1070,7 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) if (binOptimize(e, result)) return; // All negative integral powers are illegal. - if (e.e1.type.isintegral() && (e.e2.op == EXP.int64) && cast(sinteger_t)e.e2.toInteger() < 0) + if (e.e1.type.isIntegral() && (e.e2.op == EXP.int64) && cast(sinteger_t)e.e2.toInteger() < 0) { error(e.loc, "cannot raise `%s` to a negative integer power. Did you mean `(cast(real)%s)^^%s` ?", e.e1.type.toBasetype().toChars(), e.e1.toChars(), e.e2.toChars()); return errorReturn(); @@ -1080,7 +1079,7 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) if (e.e2.op == EXP.float64 && e.e2.toReal() == real_t(cast(sinteger_t)e.e2.toReal())) { // This only applies to floating point, or positive integral powers. - if (e.e1.type.isfloating() || cast(sinteger_t)e.e2.toInteger() >= 0) + if (e.e1.type.isFloating() || cast(sinteger_t)e.e2.toInteger() >= 0) e.e2 = new IntegerExp(e.loc, e.e2.toInteger(), Type.tint64); } if (e.e1.isConst() == 1 && e.e2.isConst() == 1) @@ -1238,18 +1237,21 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) if (expOptimize(e.e1, WANTvalue)) return; const oror = e.op == EXP.orOr; - if (e.e1.toBool().hasValue(oror)) + void returnE_e1() { - // Replace with (e1, oror) - ret = IntegerExp.createBool(oror); - ret = Expression.combine(e.e1, ret); - if (e.type.toBasetype().ty == Tvoid) + ret = e.e1; + if (!e.e1.type.equals(e.type)) { - ret = new CastExp(e.loc, ret, Type.tvoid); + ret = new CastExp(e.loc, ret, e.type); ret.type = e.type; + ret = optimize(ret, result, false); } - ret = optimize(ret, result, false); - return; + } + if (e.e1.toBool().hasValue(oror)) + { + // e_true || e2 -> e_true + // e_false && e2 -> e_false + return returnE_e1(); } expOptimize(e.e2, WANTvalue); if (e.e1.isConst()) @@ -1263,6 +1265,7 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) } else if (e1Opt.hasValue(!oror)) { + if (e.type.toBasetype().ty == Tvoid) ret = e.e2; else @@ -1272,6 +1275,29 @@ Expression optimize(Expression e, int result, bool keepLvalue = false) } } } + else if (e.e2.isConst()) + { + const e2Opt = e.e2.toBool(); + if (e2Opt.hasValue(oror)) + { + // e1 || true -> (e1, true) + // e1 && false -> (e1, false) + ret = IntegerExp.createBool(oror); + ret = Expression.combine(e.e1, ret); + if (e.type.toBasetype().ty == Tvoid) + { + ret = new CastExp(e.loc, ret, Type.tvoid); + ret.type = e.type; + } + ret = optimize(ret, result, false); + } + else if (e2Opt.hasValue(!oror)) + { + // e1 || false -> e1 + // e1 && true -> e1 + return returnE_e1(); + } + } } void visitCmp(CmpExp e) |