| 1 |
/** |
| 2 |
* Perform constant folding. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/optimize.d, _optimize.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_optimize.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/optimize.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.optimize; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
|
| 16 |
import dmd.astenums; |
| 17 |
import dmd.constfold; |
| 18 |
import dmd.ctfeexpr; |
| 19 |
import dmd.dcast; |
| 20 |
import dmd.dclass; |
| 21 |
import dmd.declaration; |
| 22 |
import dmd.dsymbol; |
| 23 |
import dmd.dsymbolsem; |
| 24 |
import dmd.errors; |
| 25 |
import dmd.expression; |
| 26 |
import dmd.expressionsem; |
| 27 |
import dmd.globals; |
| 28 |
import dmd.hdrgen; |
| 29 |
import dmd.init; |
| 30 |
import dmd.location; |
| 31 |
import dmd.mtype; |
| 32 |
import dmd.printast; |
| 33 |
import dmd.root.ctfloat; |
| 34 |
import dmd.sideeffect; |
| 35 |
import dmd.tokens; |
| 36 |
import dmd.typesem; |
| 37 |
import dmd.visitor; |
| 38 |
|
| 39 |
/************************************* |
| 40 |
* If variable has a const initializer, |
| 41 |
* return that initializer. |
| 42 |
* Returns: |
| 43 |
* initializer if there is one, |
| 44 |
* null if not, |
| 45 |
* ErrorExp if error |
| 46 |
*/ |
| 47 |
Expression expandVar(int result, VarDeclaration v) |
| 48 |
{ |
| 49 |
//printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); |
| 50 |
|
| 51 |
/******** |
| 52 |
* Params: |
| 53 |
* e = initializer expression |
| 54 |
*/ |
| 55 |
Expression initializerReturn(Expression e) |
| 56 |
{ |
| 57 |
if (e.type != v.type) |
| 58 |
{ |
| 59 |
e = e.castTo(null, v.type); |
| 60 |
} |
| 61 |
v.inuse++; |
| 62 |
e = e.optimize(result); |
| 63 |
v.inuse--; |
| 64 |
//if (e) printf("\te = %p, %s, e.type = %d, %s\n", e, e.toChars(), e.type.ty, e.type.toChars()); |
| 65 |
return e; |
| 66 |
} |
| 67 |
|
| 68 |
static Expression nullReturn() |
| 69 |
{ |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
static Expression errorReturn() |
| 74 |
{ |
| 75 |
return ErrorExp.get(); |
| 76 |
} |
| 77 |
|
| 78 |
if (!v) |
| 79 |
return nullReturn(); |
| 80 |
if (!v.originalType && v.semanticRun < PASS.semanticdone) // semantic() not yet run |
| 81 |
v.dsymbolSemantic(null); |
| 82 |
if (v.type && |
| 83 |
(v.isConst() || v.isImmutable() || v.storage_class & STC.manifest)) |
| 84 |
{ |
| 85 |
Type tb = v.type.toBasetype(); |
| 86 |
if (v.storage_class & STC.manifest || |
| 87 |
tb.isScalar() || |
| 88 |
((result & WANTexpand) && (tb.ty != Tsarray && tb.ty != Tstruct))) |
| 89 |
{ |
| 90 |
if (v._init) |
| 91 |
{ |
| 92 |
if (v.inuse) |
| 93 |
{ |
| 94 |
if (v.storage_class & STC.manifest) |
| 95 |
{ |
| 96 |
.error(v.loc, "%s `%s` recursive initialization of constant", v.kind, v.toPrettyChars); |
| 97 |
return errorReturn(); |
| 98 |
} |
| 99 |
return nullReturn(); |
| 100 |
} |
| 101 |
Expression ei = v.getConstInitializer(); |
| 102 |
if (!ei) |
| 103 |
{ |
| 104 |
if (v.storage_class & STC.manifest) |
| 105 |
{ |
| 106 |
.error(v.loc, "%s `%s` enum cannot be initialized with `%s`", v.kind, v.toPrettyChars, dmd.hdrgen.toChars(v._init)); |
| 107 |
return errorReturn(); |
| 108 |
} |
| 109 |
return nullReturn(); |
| 110 |
} |
| 111 |
if (ei.op == EXP.construct || ei.op == EXP.blit) |
| 112 |
{ |
| 113 |
auto ae = cast(AssignExp)ei; |
| 114 |
ei = ae.e2; |
| 115 |
if (ei.isConst() == 1) |
| 116 |
{ |
| 117 |
} |
| 118 |
else if (ei.op == EXP.string_) |
| 119 |
{ |
| 120 |
// https://issues.dlang.org/show_bug.cgi?id=14459 |
| 121 |
// Do not constfold the string literal |
| 122 |
// if it's typed as a C string, because the value expansion |
| 123 |
// will drop the pointer identity. |
| 124 |
if (!(result & WANTexpand) && ei.type.toBasetype().ty == Tpointer) |
| 125 |
return nullReturn(); |
| 126 |
} |
| 127 |
else |
| 128 |
return nullReturn(); |
| 129 |
if (ei.type == v.type) |
| 130 |
{ |
| 131 |
// const variable initialized with const expression |
| 132 |
} |
| 133 |
else if (ei.implicitConvTo(v.type) >= MATCH.constant) |
| 134 |
{ |
| 135 |
// const var initialized with non-const expression |
| 136 |
ei = ei.implicitCastTo(null, v.type); |
| 137 |
ei = ei.expressionSemantic(null); |
| 138 |
} |
| 139 |
else |
| 140 |
return nullReturn(); |
| 141 |
} |
| 142 |
else if (!(v.storage_class & STC.manifest) && |
| 143 |
ei.isConst() != 1 && |
| 144 |
ei.op != EXP.string_ && |
| 145 |
ei.op != EXP.address) |
| 146 |
{ |
| 147 |
return nullReturn(); |
| 148 |
} |
| 149 |
|
| 150 |
if (!ei.type) |
| 151 |
{ |
| 152 |
return nullReturn(); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
// Should remove the copy() operation by |
| 157 |
// making all mods to expressions copy-on-write |
| 158 |
return initializerReturn(ei.copy()); |
| 159 |
} |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
// v does not have an initializer |
| 164 |
version (all) |
| 165 |
{ |
| 166 |
return nullReturn(); |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
// BUG: what if const is initialized in constructor? |
| 171 |
auto e = v.type.defaultInit(); |
| 172 |
e.loc = e1.loc; |
| 173 |
return initializerReturn(e); |
| 174 |
} |
| 175 |
} |
| 176 |
assert(0); |
| 177 |
} |
| 178 |
} |
| 179 |
return nullReturn(); |
| 180 |
} |
| 181 |
|
| 182 |
private Expression fromConstInitializer(int result, Expression e1) |
| 183 |
{ |
| 184 |
//printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); |
| 185 |
//static int xx; if (xx++ == 10) assert(0); |
| 186 |
auto ve = e1.isVarExp(); |
| 187 |
if (!ve) |
| 188 |
return e1; |
| 189 |
|
| 190 |
Expression e = e1; |
| 191 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 192 |
e = expandVar(result, v); |
| 193 |
if (!e) |
| 194 |
return e1; |
| 195 |
|
| 196 |
// If it is a comma expression involving a declaration, we mustn't |
| 197 |
// perform a copy -- we'd get two declarations of the same variable. |
| 198 |
// See https://issues.dlang.org/show_bug.cgi?id=4465. |
| 199 |
if (e.op == EXP.comma && e.isCommaExp().e1.isDeclarationExp()) |
| 200 |
e = e1; |
| 201 |
else if (e.type != e1.type && e1.type && e1.type.ty != Tident) |
| 202 |
{ |
| 203 |
// Type 'paint' operation |
| 204 |
e = e.copy(); |
| 205 |
e.type = e1.type; |
| 206 |
} |
| 207 |
e.loc = e1.loc; |
| 208 |
|
| 209 |
return e; |
| 210 |
} |
| 211 |
|
| 212 |
/*** |
| 213 |
* It is possible for constant folding to change an array expression of |
| 214 |
* unknown length, into one where the length is known. |
| 215 |
* If the expression 'arr' is a literal, set lengthVar to be its length. |
| 216 |
* Params: |
| 217 |
* lengthVar = variable declaration for the `.length` property |
| 218 |
* arr = String, ArrayLiteral, or of TypeSArray |
| 219 |
*/ |
| 220 |
package void setLengthVarIfKnown(VarDeclaration lengthVar, Expression arr) |
| 221 |
{ |
| 222 |
if (!lengthVar) |
| 223 |
return; |
| 224 |
if (lengthVar._init && !lengthVar._init.isVoidInitializer()) |
| 225 |
return; // we have previously calculated the length |
| 226 |
dinteger_t len; |
| 227 |
if (auto se = arr.isStringExp()) |
| 228 |
len = se.len; |
| 229 |
else if (auto ale = arr.isArrayLiteralExp()) |
| 230 |
len = ale.elements.length; |
| 231 |
else |
| 232 |
{ |
| 233 |
auto tsa = arr.type.toBasetype().isTypeSArray(); |
| 234 |
if (!tsa) |
| 235 |
return; // we don't know the length yet |
| 236 |
len = tsa.dim.toInteger(); |
| 237 |
} |
| 238 |
Expression dollar = new IntegerExp(Loc.initial, len, Type.tsize_t); |
| 239 |
lengthVar._init = new ExpInitializer(Loc.initial, dollar); |
| 240 |
lengthVar.storage_class |= STC.static_ | STC.const_; |
| 241 |
} |
| 242 |
|
| 243 |
/*** |
| 244 |
* Same as above, but determines the length from 'type'. |
| 245 |
* Params: |
| 246 |
* lengthVar = variable declaration for the `.length` property |
| 247 |
* type = TypeSArray |
| 248 |
*/ |
| 249 |
package void setLengthVarIfKnown(VarDeclaration lengthVar, Type type) |
| 250 |
{ |
| 251 |
if (!lengthVar) |
| 252 |
return; |
| 253 |
if (lengthVar._init && !lengthVar._init.isVoidInitializer()) |
| 254 |
return; // we have previously calculated the length |
| 255 |
auto tsa = type.toBasetype().isTypeSArray(); |
| 256 |
if (!tsa) |
| 257 |
return; // we don't know the length yet |
| 258 |
const len = tsa.dim.toInteger(); |
| 259 |
Expression dollar = new IntegerExp(Loc.initial, len, Type.tsize_t); |
| 260 |
lengthVar._init = new ExpInitializer(Loc.initial, dollar); |
| 261 |
lengthVar.storage_class |= STC.static_ | STC.const_; |
| 262 |
} |
| 263 |
|
| 264 |
/********************************* |
| 265 |
* Constant fold an Expression. |
| 266 |
* Params: |
| 267 |
* e = expression to const fold; this may get modified in-place |
| 268 |
* result = WANTvalue, WANTexpand, or both |
| 269 |
* keepLvalue = `e` is an lvalue, and keep it as an lvalue since it is |
| 270 |
* an argument to a `ref` or `out` parameter, or the operand of `&` operator |
| 271 |
* Returns: |
| 272 |
* Constant folded version of `e` |
| 273 |
*/ |
| 274 |
Expression optimize(Expression e, int result, bool keepLvalue = false) |
| 275 |
{ |
| 276 |
//printf("optimize() e: %s result: %d keepLvalue %d\n", e.toChars(), result, keepLvalue); |
| 277 |
Expression ret = e; |
| 278 |
|
| 279 |
void errorReturn() |
| 280 |
{ |
| 281 |
ret = ErrorExp.get(); |
| 282 |
} |
| 283 |
|
| 284 |
/* Returns: true if error |
| 285 |
*/ |
| 286 |
bool expOptimize(ref Expression e, int flags, bool keepLvalue = false) |
| 287 |
{ |
| 288 |
if (!e) |
| 289 |
return false; |
| 290 |
Expression ex = optimize(e, flags, keepLvalue); |
| 291 |
if (ex.op == EXP.error) |
| 292 |
{ |
| 293 |
ret = ex; // store error result |
| 294 |
return true; |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
e = ex; // modify original |
| 299 |
return false; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
bool unaOptimize(UnaExp e, int flags) |
| 304 |
{ |
| 305 |
return expOptimize(e.e1, flags); |
| 306 |
} |
| 307 |
|
| 308 |
bool binOptimize(BinExp e, int flags, bool keepLhsLvalue = false) |
| 309 |
{ |
| 310 |
return expOptimize(e.e1, flags, keepLhsLvalue) | |
| 311 |
expOptimize(e.e2, flags); |
| 312 |
} |
| 313 |
|
| 314 |
void visitExp(Expression e) |
| 315 |
{ |
| 316 |
//printf("Expression::optimize(result = x%x) %s\n", result, e.toChars()); |
| 317 |
} |
| 318 |
|
| 319 |
void visitVar(VarExp e) |
| 320 |
{ |
| 321 |
VarDeclaration v = e.var.isVarDeclaration(); |
| 322 |
|
| 323 |
if (!(keepLvalue && v && !(v.storage_class & STC.manifest))) |
| 324 |
ret = fromConstInitializer(result, e); |
| 325 |
|
| 326 |
// if unoptimized, try to optimize the dtor expression |
| 327 |
// (e.g., might be a LogicalExp with constant lhs) |
| 328 |
if (ret == e && v && v.edtor) |
| 329 |
{ |
| 330 |
// prevent infinite recursion (`<var>.~this()`) |
| 331 |
if (!v.inuse) |
| 332 |
{ |
| 333 |
v.inuse++; |
| 334 |
expOptimize(v.edtor, WANTvalue); |
| 335 |
v.inuse--; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
void visitTuple(TupleExp e) |
| 341 |
{ |
| 342 |
expOptimize(e.e0, WANTvalue); |
| 343 |
foreach (ref ex; (*e.exps)[]) |
| 344 |
{ |
| 345 |
expOptimize(ex, WANTvalue); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
void visitArrayLiteral(ArrayLiteralExp e) |
| 350 |
{ |
| 351 |
if (e.elements) |
| 352 |
{ |
| 353 |
expOptimize(e.basis, result & WANTexpand); |
| 354 |
foreach (ref ex; (*e.elements)[]) |
| 355 |
{ |
| 356 |
expOptimize(ex, result & WANTexpand); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void visitAssocArrayLiteral(AssocArrayLiteralExp e) |
| 362 |
{ |
| 363 |
assert(e.keys.length == e.values.length); |
| 364 |
foreach (i, ref ekey; (*e.keys)[]) |
| 365 |
{ |
| 366 |
expOptimize(ekey, result & WANTexpand); |
| 367 |
expOptimize((*e.values)[i], result & WANTexpand); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
void visitStructLiteral(StructLiteralExp e) |
| 372 |
{ |
| 373 |
if (e.stageflags & StructLiteralExp.StageFlags.optimize) |
| 374 |
return; |
| 375 |
const old = e.stageflags; |
| 376 |
e.stageflags |= StructLiteralExp.StageFlags.optimize; |
| 377 |
if (e.elements) |
| 378 |
{ |
| 379 |
foreach (ref ex; (*e.elements)[]) |
| 380 |
{ |
| 381 |
expOptimize(ex, result & WANTexpand); |
| 382 |
} |
| 383 |
} |
| 384 |
e.stageflags = old; |
| 385 |
} |
| 386 |
|
| 387 |
void visitUna(UnaExp e) |
| 388 |
{ |
| 389 |
//printf("UnaExp::optimize() %s\n", e.toChars()); |
| 390 |
if (unaOptimize(e, result)) |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
void visitNeg(NegExp e) |
| 395 |
{ |
| 396 |
if (unaOptimize(e, result)) |
| 397 |
return; |
| 398 |
if (e.e1.isConst() == 1) |
| 399 |
{ |
| 400 |
ret = Neg(e.type, e.e1).copy(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void visitCom(ComExp e) |
| 405 |
{ |
| 406 |
if (unaOptimize(e, result)) |
| 407 |
return; |
| 408 |
if (e.e1.isConst() == 1) |
| 409 |
{ |
| 410 |
ret = Com(e.type, e.e1).copy(); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void visitNop(NotExp e) |
| 415 |
{ |
| 416 |
if (unaOptimize(e, result)) |
| 417 |
return; |
| 418 |
if (e.e1.isConst() == 1) |
| 419 |
{ |
| 420 |
ret = Not(e.type, e.e1).copy(); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
void visitSymOff(SymOffExp e) |
| 425 |
{ |
| 426 |
assert(e.var); |
| 427 |
} |
| 428 |
|
| 429 |
void visitAddr(AddrExp e) |
| 430 |
{ |
| 431 |
//printf("AddrExp::optimize(result = %d, keepLvalue = %d) %s\n", result, keepLvalue, e.toChars()); |
| 432 |
/* Rewrite &(a,b) as (a,&b) |
| 433 |
*/ |
| 434 |
if (auto ce = e.e1.isCommaExp()) |
| 435 |
{ |
| 436 |
auto ae = new AddrExp(e.loc, ce.e2, e.type); |
| 437 |
ret = new CommaExp(ce.loc, ce.e1, ae); |
| 438 |
ret.type = e.type; |
| 439 |
return; |
| 440 |
} |
| 441 |
// Keep lvalue-ness |
| 442 |
if (expOptimize(e.e1, result, true)) |
| 443 |
return; // error return |
| 444 |
|
| 445 |
// Convert &*ex to ex |
| 446 |
if (auto pe = e.e1.isPtrExp()) |
| 447 |
{ |
| 448 |
Expression ex = pe.e1; |
| 449 |
if (e.type.equals(ex.type)) |
| 450 |
ret = ex; |
| 451 |
else if (e.type.toBasetype().equivalent(ex.type.toBasetype())) |
| 452 |
{ |
| 453 |
ret = ex.copy(); |
| 454 |
ret.type = e.type; |
| 455 |
} |
| 456 |
return; |
| 457 |
} |
| 458 |
if (auto ve = e.e1.isVarExp()) |
| 459 |
{ |
| 460 |
if (!ve.var.isReference() && !ve.var.isImportedSymbol()) |
| 461 |
{ |
| 462 |
ret = new SymOffExp(e.loc, ve.var, 0, ve.hasOverloads); |
| 463 |
ret.type = e.type; |
| 464 |
return; |
| 465 |
} |
| 466 |
} |
| 467 |
if (e.e1.isDotVarExp()) |
| 468 |
{ |
| 469 |
/****************************** |
| 470 |
* Run down the left side of the a.b.c expression to determine the |
| 471 |
* leftmost variable being addressed (`a`), and accumulate the offsets of the `.b` and `.c`. |
| 472 |
* Params: |
| 473 |
* e = the DotVarExp or VarExp |
| 474 |
* var = set to the VarExp at the end, or null if doesn't end in VarExp |
| 475 |
* eint = set to the IntegerExp at the end, or null if doesn't end in IntegerExp |
| 476 |
* offset = accumulation of all the .var offsets encountered |
| 477 |
* Returns: true on error |
| 478 |
*/ |
| 479 |
static bool getVarAndOffset(Expression e, out VarDeclaration var, out IntegerExp eint, ref uint offset) |
| 480 |
{ |
| 481 |
if (e.type.size() == SIZE_INVALID) // trigger computation of v.offset |
| 482 |
return true; |
| 483 |
|
| 484 |
if (auto dve = e.isDotVarExp()) |
| 485 |
{ |
| 486 |
auto v = dve.var.isVarDeclaration(); |
| 487 |
if (!v || !v.isField() || v.isBitFieldDeclaration()) |
| 488 |
return false; |
| 489 |
|
| 490 |
if (getVarAndOffset(dve.e1, var, eint, offset)) |
| 491 |
return true; |
| 492 |
offset += v.offset; |
| 493 |
} |
| 494 |
else if (auto ve = e.isVarExp()) |
| 495 |
{ |
| 496 |
if (!ve.var.isReference() && |
| 497 |
!ve.var.isImportedSymbol() && |
| 498 |
ve.var.isDataseg() && |
| 499 |
ve.var.isCsymbol()) |
| 500 |
{ |
| 501 |
var = ve.var.isVarDeclaration(); |
| 502 |
} |
| 503 |
} |
| 504 |
else if (auto ep = e.isPtrExp()) |
| 505 |
{ |
| 506 |
if (auto ei = ep.e1.isIntegerExp()) |
| 507 |
{ |
| 508 |
eint = ei; |
| 509 |
} |
| 510 |
else if (auto se = ep.e1.isSymOffExp()) |
| 511 |
{ |
| 512 |
if (!se.var.isReference() && |
| 513 |
!se.var.isImportedSymbol() && |
| 514 |
se.var.isDataseg()) |
| 515 |
{ |
| 516 |
var = se.var.isVarDeclaration(); |
| 517 |
offset += se.offset; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
else if (auto ei = e.isIndexExp()) |
| 522 |
{ |
| 523 |
if (auto ve = ei.e1.isVarExp()) |
| 524 |
{ |
| 525 |
if (!ve.var.isReference() && |
| 526 |
!ve.var.isImportedSymbol() && |
| 527 |
ve.var.isDataseg() && |
| 528 |
ve.var.isCsymbol()) |
| 529 |
{ |
| 530 |
if (auto ie = ei.e2.isIntegerExp()) |
| 531 |
{ |
| 532 |
var = ve.var.isVarDeclaration(); |
| 533 |
offset += ie.toInteger() * ve.type.toBasetype().nextOf().size(); |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
} |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
uint offset; |
| 542 |
VarDeclaration var; |
| 543 |
IntegerExp eint; |
| 544 |
if (getVarAndOffset(e.e1, var, eint, offset)) |
| 545 |
{ |
| 546 |
ret = ErrorExp.get(); |
| 547 |
return; |
| 548 |
} |
| 549 |
if (var) |
| 550 |
{ |
| 551 |
ret = new SymOffExp(e.loc, var, offset, false); |
| 552 |
ret.type = e.type; |
| 553 |
return; |
| 554 |
} |
| 555 |
if (eint) |
| 556 |
{ |
| 557 |
ret = new IntegerExp(e.loc, eint.toInteger() + offset, e.type); |
| 558 |
return; |
| 559 |
} |
| 560 |
} |
| 561 |
else if (auto ae = e.e1.isIndexExp()) |
| 562 |
{ |
| 563 |
if (ae.e2.isIntegerExp() && ae.e1.isIndexExp()) |
| 564 |
{ |
| 565 |
/* Rewrite `(a[i])[index]` to `(&a[i]) + index*size` |
| 566 |
*/ |
| 567 |
sinteger_t index = ae.e2.toInteger(); |
| 568 |
auto ae1 = ae.e1.isIndexExp(); // ae1 is a[i] |
| 569 |
if (auto ts = ae1.type.isTypeSArray()) |
| 570 |
{ |
| 571 |
sinteger_t dim = ts.dim.toInteger(); |
| 572 |
|
| 573 |
if (index < 0 || index > dim) |
| 574 |
{ |
| 575 |
error(e.loc, "array index %lld is out of bounds `[0..%lld]`", index, dim); |
| 576 |
return errorReturn(); |
| 577 |
} |
| 578 |
|
| 579 |
import core.checkedint : mulu; |
| 580 |
bool overflow; |
| 581 |
const offset = mulu(index, ts.nextOf().size(e.loc), overflow); // offset = index*size |
| 582 |
if (overflow) |
| 583 |
{ |
| 584 |
error(e.loc, "array offset overflow"); |
| 585 |
return errorReturn(); |
| 586 |
} |
| 587 |
|
| 588 |
Expression ex = new AddrExp(ae1.loc, ae1); // &a[i] |
| 589 |
ex.type = ae1.type.pointerTo(); |
| 590 |
|
| 591 |
Expression add = new AddExp(ae.loc, ex, new IntegerExp(ae.e2.loc, offset, ae.e2.type)); |
| 592 |
add.type = e.type; |
| 593 |
ret = optimize(add, result, keepLvalue); |
| 594 |
return; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
// Convert &array[n] to &array+n |
| 599 |
if (ae.e2.isIntegerExp() && ae.e1.isVarExp()) |
| 600 |
{ |
| 601 |
sinteger_t index = ae.e2.toInteger(); |
| 602 |
VarExp ve = ae.e1.isVarExp(); |
| 603 |
if (ve.type.isTypeSArray() && !ve.var.isImportedSymbol()) |
| 604 |
{ |
| 605 |
TypeSArray ts = ve.type.isTypeSArray(); |
| 606 |
sinteger_t dim = ts.dim.toInteger(); |
| 607 |
if (index < 0 || index >= dim) |
| 608 |
{ |
| 609 |
/* 0 for C static arrays means size is unknown, no need to check, |
| 610 |
* and address one past the end is OK, too |
| 611 |
*/ |
| 612 |
if (!((dim == 0 || dim == index) && ve.var.isCsymbol())) |
| 613 |
{ |
| 614 |
error(e.loc, "array index %lld is out of bounds `[0..%lld]`", index, dim); |
| 615 |
return errorReturn(); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
import core.checkedint : mulu; |
| 620 |
bool overflow; |
| 621 |
const offset = mulu(index, ts.nextOf().size(e.loc), overflow); |
| 622 |
if (overflow) |
| 623 |
{ |
| 624 |
error(e.loc, "array offset overflow"); |
| 625 |
return errorReturn(); |
| 626 |
} |
| 627 |
|
| 628 |
ret = new SymOffExp(e.loc, ve.var, offset); |
| 629 |
ret.type = e.type; |
| 630 |
return; |
| 631 |
} |
| 632 |
} |
| 633 |
// Convert &((a.b)[index]) to (&a.b)+index*elementsize |
| 634 |
else if (ae.e2.isIntegerExp() && ae.e1.isDotVarExp()) |
| 635 |
{ |
| 636 |
sinteger_t index = ae.e2.toInteger(); |
| 637 |
DotVarExp ve = ae.e1.isDotVarExp(); |
| 638 |
if (ve.type.isTypeSArray() && ve.var.isField() && ve.e1.isPtrExp()) |
| 639 |
{ |
| 640 |
TypeSArray ts = ve.type.isTypeSArray(); |
| 641 |
sinteger_t dim = ts.dim.toInteger(); |
| 642 |
if (index < 0 || index >= dim) |
| 643 |
{ |
| 644 |
/* 0 for C static arrays means size is unknown, no need to check, |
| 645 |
* and address one past the end is OK, too |
| 646 |
*/ |
| 647 |
if (!((dim == 0 || dim == index) && ve.var.isCsymbol())) |
| 648 |
{ |
| 649 |
error(e.loc, "array index %lld is out of bounds `[0..%lld]`", index, dim); |
| 650 |
return errorReturn(); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
import core.checkedint : mulu; |
| 655 |
bool overflow; |
| 656 |
const offset = mulu(index, ts.nextOf().size(e.loc), overflow); // index*elementsize |
| 657 |
if (overflow) |
| 658 |
{ |
| 659 |
error(e.loc, "array offset overflow"); |
| 660 |
return errorReturn(); |
| 661 |
} |
| 662 |
|
| 663 |
auto pe = new AddrExp(e.loc, ve); |
| 664 |
pe.type = e.type; |
| 665 |
ret = new AddExp(e.loc, pe, new IntegerExp(e.loc, offset, Type.tsize_t)); |
| 666 |
ret.type = e.type; |
| 667 |
return; |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
void visitPtr(PtrExp e) |
| 674 |
{ |
| 675 |
//printf("PtrExp::optimize(result = x%x) %s\n", result, e.toChars()); |
| 676 |
if (expOptimize(e.e1, result)) |
| 677 |
return; |
| 678 |
// Convert *&ex to ex |
| 679 |
// But only if there is no type punning involved |
| 680 |
if (auto ey = e.e1.isAddrExp()) |
| 681 |
{ |
| 682 |
Expression ex = ey.e1; |
| 683 |
if (e.type.equals(ex.type)) |
| 684 |
ret = ex; |
| 685 |
else if (e.type.toBasetype().equivalent(ex.type.toBasetype())) |
| 686 |
{ |
| 687 |
ret = ex.copy(); |
| 688 |
ret.type = e.type; |
| 689 |
} |
| 690 |
} |
| 691 |
if (keepLvalue) |
| 692 |
return; |
| 693 |
// Constant fold *(&structliteral + offset) |
| 694 |
if (e.e1.op == EXP.add) |
| 695 |
{ |
| 696 |
Expression ex = Ptr(e.type, e.e1).copy(); |
| 697 |
if (!CTFEExp.isCantExp(ex)) |
| 698 |
{ |
| 699 |
ret = ex; |
| 700 |
return; |
| 701 |
} |
| 702 |
} |
| 703 |
if (auto se = e.e1.isSymOffExp()) |
| 704 |
{ |
| 705 |
VarDeclaration v = se.var.isVarDeclaration(); |
| 706 |
Expression ex = expandVar(result, v); |
| 707 |
if (ex && ex.isStructLiteralExp()) |
| 708 |
{ |
| 709 |
StructLiteralExp sle = ex.isStructLiteralExp(); |
| 710 |
ex = sle.getField(e.type, cast(uint)se.offset); |
| 711 |
if (ex && !CTFEExp.isCantExp(ex)) |
| 712 |
{ |
| 713 |
ret = ex; |
| 714 |
return; |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
void visitDotVar(DotVarExp e) |
| 721 |
{ |
| 722 |
//printf("DotVarExp::optimize(result = x%x) %s\n", result, e.toChars()); |
| 723 |
if (expOptimize(e.e1, result)) |
| 724 |
return; |
| 725 |
if (keepLvalue) |
| 726 |
return; |
| 727 |
Expression ex = e.e1; |
| 728 |
if (auto ve = ex.isVarExp()) |
| 729 |
{ |
| 730 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 731 |
ex = expandVar(result, v); |
| 732 |
} |
| 733 |
if (ex && ex.isStructLiteralExp()) |
| 734 |
{ |
| 735 |
StructLiteralExp sle = ex.isStructLiteralExp(); |
| 736 |
VarDeclaration vf = e.var.isVarDeclaration(); |
| 737 |
if (vf && !vf.overlapped) |
| 738 |
{ |
| 739 |
/* https://issues.dlang.org/show_bug.cgi?id=13021 |
| 740 |
* Prevent optimization if vf has overlapped fields. |
| 741 |
*/ |
| 742 |
ex = sle.getField(e.type, vf.offset); |
| 743 |
if (ex && !CTFEExp.isCantExp(ex)) |
| 744 |
{ |
| 745 |
ret = ex; |
| 746 |
return; |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
void visitNew(NewExp e) |
| 753 |
{ |
| 754 |
expOptimize(e.placement, WANTvalue); |
| 755 |
expOptimize(e.thisexp, WANTvalue); |
| 756 |
// Optimize parameters |
| 757 |
if (e.arguments) |
| 758 |
{ |
| 759 |
foreach (ref arg; (*e.arguments)[]) |
| 760 |
{ |
| 761 |
expOptimize(arg, WANTvalue); |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
void visitCall(CallExp e) |
| 767 |
{ |
| 768 |
//printf("CallExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 769 |
// Optimize parameters with keeping lvalue-ness |
| 770 |
if (expOptimize(e.e1, result)) |
| 771 |
return; |
| 772 |
if (e.arguments) |
| 773 |
{ |
| 774 |
// t1 can apparently be void for __ArrayDtor(T) calls |
| 775 |
if (auto tf = e.calledFunctionType()) |
| 776 |
{ |
| 777 |
foreach (i, ref arg; (*e.arguments)[]) |
| 778 |
{ |
| 779 |
Parameter p = tf.parameterList[i]; |
| 780 |
bool keep = p && p.isReference(); |
| 781 |
expOptimize(arg, WANTvalue, keep); |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
void visitCast(CastExp e) |
| 788 |
{ |
| 789 |
//printf("CastExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 790 |
//printf("from %s to %s\n", e.type.toChars(), e.to.toChars()); |
| 791 |
//printf("from %s\n", e.type.toChars()); |
| 792 |
//printf("e1.type %s\n", e.e1.type.toChars()); |
| 793 |
//printf("type = %p\n", e.type); |
| 794 |
assert(e.type); |
| 795 |
const op1 = e.e1.op; |
| 796 |
Expression e1old = e.e1; |
| 797 |
if (expOptimize(e.e1, result, keepLvalue)) |
| 798 |
return; |
| 799 |
if (!keepLvalue) |
| 800 |
e.e1 = fromConstInitializer(result, e.e1); |
| 801 |
if (e.e1 == e1old && e.e1.op == EXP.arrayLiteral && e.type.toBasetype().ty == Tpointer && e.e1.type.toBasetype().ty != Tsarray) |
| 802 |
{ |
| 803 |
// Casting this will result in the same expression, and |
| 804 |
// infinite loop because of Expression::implicitCastTo() |
| 805 |
return; // no change |
| 806 |
} |
| 807 |
if ((e.e1.op == EXP.string_ || e.e1.op == EXP.arrayLiteral) && |
| 808 |
(e.type.ty == Tpointer || e.type.ty == Tarray)) |
| 809 |
{ |
| 810 |
const esz = e.type.nextOf().size(e.loc); |
| 811 |
const e1sz = e.e1.type.toBasetype().nextOf().size(e.e1.loc); |
| 812 |
if (esz == SIZE_INVALID || e1sz == SIZE_INVALID) |
| 813 |
return errorReturn(); |
| 814 |
|
| 815 |
if (e1sz == esz) |
| 816 |
{ |
| 817 |
// https://issues.dlang.org/show_bug.cgi?id=12937 |
| 818 |
// If target type is void array, trying to paint |
| 819 |
// e.e1 with that type will cause infinite recursive optimization. |
| 820 |
if (e.type.nextOf().ty == Tvoid) |
| 821 |
return; |
| 822 |
ret = e.e1.castTo(null, e.type); |
| 823 |
//printf(" returning1 %s\n", ret.toChars()); |
| 824 |
return; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
// Returning e.e1 with changing its type |
| 829 |
void returnE_e1() |
| 830 |
{ |
| 831 |
ret = (e1old == e.e1 ? e.e1.copy() : e.e1); |
| 832 |
ret.type = e.type; |
| 833 |
} |
| 834 |
|
| 835 |
if (e.e1.op == EXP.structLiteral && e.e1.type.implicitConvTo(e.type) >= MATCH.constant) |
| 836 |
{ |
| 837 |
//printf(" returning2 %s\n", e.e1.toChars()); |
| 838 |
return returnE_e1(); |
| 839 |
} |
| 840 |
/* The first test here is to prevent infinite loops |
| 841 |
*/ |
| 842 |
if (op1 != EXP.arrayLiteral && e.e1.op == EXP.arrayLiteral) |
| 843 |
{ |
| 844 |
ret = e.e1.castTo(null, e.to); |
| 845 |
return; |
| 846 |
} |
| 847 |
if (e.e1.op == EXP.null_ && (e.type.ty == Tpointer || e.type.ty == Tclass || e.type.ty == Tarray)) |
| 848 |
{ |
| 849 |
//printf(" returning3 %s\n", e.e1.toChars()); |
| 850 |
return returnE_e1(); |
| 851 |
} |
| 852 |
if (e.type.ty == Tclass && e.e1.type.ty == Tclass) |
| 853 |
{ |
| 854 |
import dmd.astenums : Sizeok; |
| 855 |
|
| 856 |
// See if we can remove an unnecessary cast |
| 857 |
ClassDeclaration cdfrom = e.e1.type.isClassHandle(); |
| 858 |
ClassDeclaration cdto = e.type.isClassHandle(); |
| 859 |
if (cdfrom.errors || cdto.errors) |
| 860 |
return errorReturn(); |
| 861 |
if (cdto == ClassDeclaration.object && !cdfrom.isInterfaceDeclaration()) |
| 862 |
return returnE_e1(); // can always convert a class to Object |
| 863 |
// Need to determine correct offset before optimizing away the cast. |
| 864 |
// https://issues.dlang.org/show_bug.cgi?id=16980 |
| 865 |
if (cdfrom.size(e.loc) == SIZE_INVALID) |
| 866 |
return errorReturn(); |
| 867 |
assert(cdfrom.sizeok == Sizeok.done); |
| 868 |
assert(cdto.sizeok == Sizeok.done || !cdto.isBaseOf(cdfrom, null)); |
| 869 |
int offset; |
| 870 |
if (cdto.isBaseOf(cdfrom, &offset) && offset == 0) |
| 871 |
{ |
| 872 |
//printf(" returning4 %s\n", e.e1.toChars()); |
| 873 |
return returnE_e1(); |
| 874 |
} |
| 875 |
} |
| 876 |
if (e.e1.type.mutableOf().unSharedOf().equals(e.to.mutableOf().unSharedOf())) |
| 877 |
{ |
| 878 |
//printf(" returning5 %s\n", e.e1.toChars()); |
| 879 |
return returnE_e1(); |
| 880 |
} |
| 881 |
if (e.e1.isConst()) |
| 882 |
{ |
| 883 |
if (e.e1.op == EXP.symbolOffset) |
| 884 |
{ |
| 885 |
if (e.type.toBasetype().ty != Tsarray) |
| 886 |
{ |
| 887 |
const esz = e.type.size(e.loc); |
| 888 |
const e1sz = e.e1.type.size(e.e1.loc); |
| 889 |
if (esz == SIZE_INVALID || |
| 890 |
e1sz == SIZE_INVALID) |
| 891 |
return errorReturn(); |
| 892 |
|
| 893 |
if (esz == e1sz) |
| 894 |
return returnE_e1(); |
| 895 |
} |
| 896 |
return; |
| 897 |
} |
| 898 |
if (e.to.toBasetype().ty != Tvoid) |
| 899 |
{ |
| 900 |
if (e.e1.type.equals(e.type) && e.type.equals(e.to)) |
| 901 |
ret = e.e1; |
| 902 |
else |
| 903 |
ret = Cast(e.loc, e.type, e.to, e.e1).copy(); |
| 904 |
} |
| 905 |
} |
| 906 |
//printf(" returning6 %s\n", ret.toChars()); |
| 907 |
} |
| 908 |
|
| 909 |
void visitBinAssign(BinAssignExp e) |
| 910 |
{ |
| 911 |
//printf("BinAssignExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 912 |
if (binOptimize(e, result, /*keepLhsLvalue*/ true)) |
| 913 |
return; |
| 914 |
if (e.op == EXP.leftShiftAssign || e.op == EXP.rightShiftAssign || e.op == EXP.unsignedRightShiftAssign) |
| 915 |
{ |
| 916 |
if (e.e2.isConst() == 1) |
| 917 |
{ |
| 918 |
sinteger_t i2 = e.e2.toInteger(); |
| 919 |
uinteger_t sz = e.e1.type.size(e.e1.loc); |
| 920 |
assert(sz != SIZE_INVALID); |
| 921 |
sz *= 8; |
| 922 |
if (i2 < 0 || i2 >= sz) |
| 923 |
{ |
| 924 |
error(e.loc, "shift assign by %lld is outside the range `0..%llu`", i2, cast(ulong)sz - 1); |
| 925 |
return errorReturn(); |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
void visitCatAssign(CatAssignExp e) |
| 932 |
{ |
| 933 |
if (auto lowering = e.lowering) |
| 934 |
optimize(lowering, result, keepLvalue); |
| 935 |
else |
| 936 |
visitBinAssign(e); |
| 937 |
} |
| 938 |
|
| 939 |
void visitBin(BinExp e) |
| 940 |
{ |
| 941 |
//printf("BinExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 942 |
const keepLhsLvalue = e.op == EXP.construct || e.op == EXP.blit || e.op == EXP.assign |
| 943 |
|| e.op == EXP.plusPlus || e.op == EXP.minusMinus |
| 944 |
|| e.op == EXP.prePlusPlus || e.op == EXP.preMinusMinus; |
| 945 |
binOptimize(e, result, keepLhsLvalue); |
| 946 |
} |
| 947 |
|
| 948 |
void visitAdd(AddExp e) |
| 949 |
{ |
| 950 |
//printf("AddExp::optimize(%s)\n", e.toChars()); |
| 951 |
if (binOptimize(e, result)) |
| 952 |
return; |
| 953 |
if (e.e1.isConst() && e.e2.isConst()) |
| 954 |
{ |
| 955 |
if (e.e1.op == EXP.symbolOffset && e.e2.op == EXP.symbolOffset) |
| 956 |
return; |
| 957 |
ret = Add(e.loc, e.type, e.e1, e.e2).copy(); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
void visitMin(MinExp e) |
| 962 |
{ |
| 963 |
//printf("MinExp::optimize(%s)\n", e.toChars()); |
| 964 |
if (binOptimize(e, result)) |
| 965 |
return; |
| 966 |
if (e.e1.isConst() && e.e2.isConst()) |
| 967 |
{ |
| 968 |
if (e.e2.op == EXP.symbolOffset) |
| 969 |
return; |
| 970 |
ret = Min(e.loc, e.type, e.e1, e.e2).copy(); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
void visitMul(MulExp e) |
| 975 |
{ |
| 976 |
//printf("MulExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 977 |
if (binOptimize(e, result)) |
| 978 |
return; |
| 979 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 980 |
{ |
| 981 |
ret = Mul(e.loc, e.type, e.e1, e.e2).copy(); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
void visitDiv(DivExp e) |
| 986 |
{ |
| 987 |
//printf("DivExp::optimize(%s)\n", e.toChars()); |
| 988 |
if (binOptimize(e, result)) |
| 989 |
return; |
| 990 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 991 |
{ |
| 992 |
ret = Div(e.loc, e.type, e.e1, e.e2).copy(); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
void visitMod(ModExp e) |
| 997 |
{ |
| 998 |
if (binOptimize(e, result)) |
| 999 |
return; |
| 1000 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 1001 |
{ |
| 1002 |
ret = Mod(e.loc, e.type, e.e1, e.e2).copy(); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
extern (D) void shift_optimize(BinExp e, UnionExp function(Loc, Type, Expression, Expression) shift) |
| 1007 |
{ |
| 1008 |
if (binOptimize(e, result)) |
| 1009 |
return; |
| 1010 |
if (e.e2.isConst() == 1) |
| 1011 |
{ |
| 1012 |
sinteger_t i2 = e.e2.toInteger(); |
| 1013 |
uinteger_t sz = e.e1.type.size(e.e1.loc); |
| 1014 |
assert(sz != SIZE_INVALID); |
| 1015 |
sz *= 8; |
| 1016 |
if (i2 < 0 || i2 >= sz) |
| 1017 |
{ |
| 1018 |
error(e.loc, "shift by %lld is outside the range `0..%llu`", i2, cast(ulong)sz - 1); |
| 1019 |
return errorReturn(); |
| 1020 |
} |
| 1021 |
if (e.e1.isConst() == 1) |
| 1022 |
ret = (*shift)(e.loc, e.type, e.e1, e.e2).copy(); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
void visitShl(ShlExp e) |
| 1027 |
{ |
| 1028 |
//printf("ShlExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1029 |
shift_optimize(e, &Shl); |
| 1030 |
} |
| 1031 |
|
| 1032 |
void visitShr(ShrExp e) |
| 1033 |
{ |
| 1034 |
//printf("ShrExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1035 |
shift_optimize(e, &Shr); |
| 1036 |
} |
| 1037 |
|
| 1038 |
void visitUshr(UshrExp e) |
| 1039 |
{ |
| 1040 |
//printf("UshrExp::optimize(result = %d) %s\n", result, toChars()); |
| 1041 |
shift_optimize(e, &Ushr); |
| 1042 |
} |
| 1043 |
|
| 1044 |
void visitAnd(AndExp e) |
| 1045 |
{ |
| 1046 |
if (binOptimize(e, result)) |
| 1047 |
return; |
| 1048 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 1049 |
ret = And(e.loc, e.type, e.e1, e.e2).copy(); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void visitOr(OrExp e) |
| 1053 |
{ |
| 1054 |
if (binOptimize(e, result)) |
| 1055 |
return; |
| 1056 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 1057 |
ret = Or(e.loc, e.type, e.e1, e.e2).copy(); |
| 1058 |
} |
| 1059 |
|
| 1060 |
void visitXor(XorExp e) |
| 1061 |
{ |
| 1062 |
if (binOptimize(e, result)) |
| 1063 |
return; |
| 1064 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 1065 |
ret = Xor(e.loc, e.type, e.e1, e.e2).copy(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
void visitPow(PowExp e) |
| 1069 |
{ |
| 1070 |
if (binOptimize(e, result)) |
| 1071 |
return; |
| 1072 |
// All negative integral powers are illegal. |
| 1073 |
if (e.e1.type.isIntegral() && (e.e2.op == EXP.int64) && cast(sinteger_t)e.e2.toInteger() < 0) |
| 1074 |
{ |
| 1075 |
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()); |
| 1076 |
return errorReturn(); |
| 1077 |
} |
| 1078 |
// If e2 *could* have been an integer, make it one. |
| 1079 |
if (e.e2.op == EXP.float64 && e.e2.toReal() == real_t(cast(sinteger_t)e.e2.toReal())) |
| 1080 |
{ |
| 1081 |
// This only applies to floating point, or positive integral powers. |
| 1082 |
if (e.e1.type.isFloating() || cast(sinteger_t)e.e2.toInteger() >= 0) |
| 1083 |
e.e2 = new IntegerExp(e.loc, e.e2.toInteger(), Type.tint64); |
| 1084 |
} |
| 1085 |
if (e.e1.isConst() == 1 && e.e2.isConst() == 1) |
| 1086 |
{ |
| 1087 |
Expression ex = Pow(e.loc, e.type, e.e1, e.e2).copy(); |
| 1088 |
if (!CTFEExp.isCantExp(ex)) |
| 1089 |
{ |
| 1090 |
ret = ex; |
| 1091 |
return; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
void visitComma(CommaExp e) |
| 1097 |
{ |
| 1098 |
//printf("CommaExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1099 |
// Comma needs special treatment, because it may |
| 1100 |
// contain compiler-generated declarations. We can interpret them, but |
| 1101 |
// otherwise we must NOT attempt to constant-fold them. |
| 1102 |
// In particular, if the comma returns a temporary variable, it needs |
| 1103 |
// to be an lvalue (this is particularly important for struct constructors) |
| 1104 |
expOptimize(e.e1, WANTvalue); |
| 1105 |
expOptimize(e.e2, result, keepLvalue); |
| 1106 |
if (ret.op == EXP.error) |
| 1107 |
return; |
| 1108 |
if (!e.e1 || e.e1.op == EXP.int64 || e.e1.op == EXP.float64 || !hasSideEffect(e.e1)) |
| 1109 |
{ |
| 1110 |
ret = e.e2; |
| 1111 |
if (ret) |
| 1112 |
ret.type = e.type; |
| 1113 |
} |
| 1114 |
//printf("-CommaExp::optimize(result = %d) %s\n", result, e.e.toChars()); |
| 1115 |
} |
| 1116 |
|
| 1117 |
void visitArrayLength(ArrayLengthExp e) |
| 1118 |
{ |
| 1119 |
//printf("ArrayLengthExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1120 |
if (unaOptimize(e, WANTexpand)) |
| 1121 |
return; |
| 1122 |
// CTFE interpret static immutable arrays (to get better diagnostics) |
| 1123 |
if (auto ve = e.e1.isVarExp()) |
| 1124 |
{ |
| 1125 |
VarDeclaration v = ve.var.isVarDeclaration(); |
| 1126 |
if (v && (v.storage_class & STC.static_) && (v.storage_class & STC.immutable_) && v._init) |
| 1127 |
{ |
| 1128 |
if (Expression ci = v.getConstInitializer()) |
| 1129 |
e.e1 = ci; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
if (e.e1.op == EXP.string_ || e.e1.op == EXP.arrayLiteral || e.e1.op == EXP.assocArrayLiteral || e.e1.type.toBasetype().ty == Tsarray || e.e1.op == EXP.null_) |
| 1133 |
{ |
| 1134 |
ret = ArrayLength(e.type, e.e1).copy(); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
void visitEqual(EqualExp e) |
| 1139 |
{ |
| 1140 |
//printf("EqualExp::optimize(result = %x) %s\n", result, e.toChars()); |
| 1141 |
if (binOptimize(e, WANTvalue)) |
| 1142 |
return; |
| 1143 |
Expression e1 = fromConstInitializer(result, e.e1); |
| 1144 |
Expression e2 = fromConstInitializer(result, e.e2); |
| 1145 |
if (e1.op == EXP.error) |
| 1146 |
{ |
| 1147 |
ret = e1; |
| 1148 |
return; |
| 1149 |
} |
| 1150 |
if (e2.op == EXP.error) |
| 1151 |
{ |
| 1152 |
ret = e2; |
| 1153 |
return; |
| 1154 |
} |
| 1155 |
ret = Equal(e.op, e.loc, e.type, e1, e2).copy(); |
| 1156 |
if (CTFEExp.isCantExp(ret)) |
| 1157 |
ret = e; |
| 1158 |
} |
| 1159 |
|
| 1160 |
void visitIdentity(IdentityExp e) |
| 1161 |
{ |
| 1162 |
//printf("IdentityExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1163 |
if (binOptimize(e, WANTvalue)) |
| 1164 |
return; |
| 1165 |
if ((e.e1.isConst() && e.e2.isConst()) || (e.e1.op == EXP.null_ && e.e2.op == EXP.null_)) |
| 1166 |
{ |
| 1167 |
ret = Identity(e.op, e.loc, e.type, e.e1, e.e2).copy(); |
| 1168 |
if (CTFEExp.isCantExp(ret)) |
| 1169 |
ret = e; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
void visitIndex(IndexExp e) |
| 1174 |
{ |
| 1175 |
//printf("IndexExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1176 |
if (expOptimize(e.e1, result & WANTexpand)) |
| 1177 |
return; |
| 1178 |
Expression ex = fromConstInitializer(result, e.e1); |
| 1179 |
// We might know $ now |
| 1180 |
setLengthVarIfKnown(e.lengthVar, ex); |
| 1181 |
if (expOptimize(e.e2, WANTvalue)) |
| 1182 |
return; |
| 1183 |
// Don't optimize to an array literal element directly in case an lvalue is requested |
| 1184 |
if (keepLvalue && ex.op == EXP.arrayLiteral) |
| 1185 |
return; |
| 1186 |
ret = Index(e.type, ex, e.e2, e.indexIsInBounds).copy(); |
| 1187 |
if (CTFEExp.isCantExp(ret) || (!ret.isErrorExp() && keepLvalue && !ret.isLvalue())) |
| 1188 |
ret = e; |
| 1189 |
} |
| 1190 |
|
| 1191 |
void visitSlice(SliceExp e) |
| 1192 |
{ |
| 1193 |
//printf("SliceExp::optimize(result = %d) %s\n", result, e.toChars()); |
| 1194 |
if (expOptimize(e.e1, result & WANTexpand)) |
| 1195 |
return; |
| 1196 |
if (!e.lwr) |
| 1197 |
{ |
| 1198 |
if (e.e1.op == EXP.string_) |
| 1199 |
{ |
| 1200 |
// Convert slice of string literal into dynamic array |
| 1201 |
Type t = e.e1.type.toBasetype(); |
| 1202 |
if (Type tn = t.nextOf()) |
| 1203 |
ret = e.e1.castTo(null, tn.arrayOf()); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
else |
| 1207 |
{ |
| 1208 |
e.e1 = fromConstInitializer(result, e.e1); |
| 1209 |
// We might know $ now |
| 1210 |
setLengthVarIfKnown(e.lengthVar, e.e1); |
| 1211 |
expOptimize(e.lwr, WANTvalue); |
| 1212 |
expOptimize(e.upr, WANTvalue); |
| 1213 |
if (ret.op == EXP.error) |
| 1214 |
return; |
| 1215 |
ret = Slice(e.type, e.e1, e.lwr, e.upr).copy(); |
| 1216 |
if (CTFEExp.isCantExp(ret)) |
| 1217 |
ret = e; |
| 1218 |
} |
| 1219 |
// https://issues.dlang.org/show_bug.cgi?id=14649 |
| 1220 |
// Leave the slice form so it might be |
| 1221 |
// a part of array operation. |
| 1222 |
// Assume that the backend codegen will handle the form `e[]` |
| 1223 |
// as an equal to `e` itself. |
| 1224 |
if (ret.op == EXP.string_) |
| 1225 |
{ |
| 1226 |
e.e1 = ret; |
| 1227 |
e.lwr = null; |
| 1228 |
e.upr = null; |
| 1229 |
ret = e; |
| 1230 |
} |
| 1231 |
//printf("-SliceExp::optimize() %s\n", ret.toChars()); |
| 1232 |
} |
| 1233 |
|
| 1234 |
void visitLogical(LogicalExp e) |
| 1235 |
{ |
| 1236 |
//printf("LogicalExp::optimize(%d) %s\n", result, e.toChars()); |
| 1237 |
if (expOptimize(e.e1, WANTvalue)) |
| 1238 |
return; |
| 1239 |
const oror = e.op == EXP.orOr; |
| 1240 |
void returnE_e1() |
| 1241 |
{ |
| 1242 |
ret = e.e1; |
| 1243 |
if (!e.e1.type.equals(e.type)) |
| 1244 |
{ |
| 1245 |
ret = new CastExp(e.loc, ret, e.type); |
| 1246 |
ret.type = e.type; |
| 1247 |
ret = optimize(ret, result, false); |
| 1248 |
} |
| 1249 |
} |
| 1250 |
if (e.e1.toBool().hasValue(oror)) |
| 1251 |
{ |
| 1252 |
// e_true || e2 -> e_true |
| 1253 |
// e_false && e2 -> e_false |
| 1254 |
return returnE_e1(); |
| 1255 |
} |
| 1256 |
expOptimize(e.e2, WANTvalue); |
| 1257 |
if (e.e1.isConst()) |
| 1258 |
{ |
| 1259 |
const e1Opt = e.e1.toBool(); |
| 1260 |
if (e.e2.isConst()) |
| 1261 |
{ |
| 1262 |
bool n1 = e1Opt.get(); |
| 1263 |
bool n2 = e.e2.toBool().get(); |
| 1264 |
ret = new IntegerExp(e.loc, oror ? (n1 || n2) : (n1 && n2), e.type); |
| 1265 |
} |
| 1266 |
else if (e1Opt.hasValue(!oror)) |
| 1267 |
{ |
| 1268 |
|
| 1269 |
if (e.type.toBasetype().ty == Tvoid) |
| 1270 |
ret = e.e2; |
| 1271 |
else |
| 1272 |
{ |
| 1273 |
ret = new CastExp(e.loc, e.e2, e.type); |
| 1274 |
ret.type = e.type; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
} |
| 1278 |
else if (e.e2.isConst()) |
| 1279 |
{ |
| 1280 |
const e2Opt = e.e2.toBool(); |
| 1281 |
if (e2Opt.hasValue(oror)) |
| 1282 |
{ |
| 1283 |
// e1 || true -> (e1, true) |
| 1284 |
// e1 && false -> (e1, false) |
| 1285 |
ret = IntegerExp.createBool(oror); |
| 1286 |
ret = Expression.combine(e.e1, ret); |
| 1287 |
if (e.type.toBasetype().ty == Tvoid) |
| 1288 |
{ |
| 1289 |
ret = new CastExp(e.loc, ret, Type.tvoid); |
| 1290 |
ret.type = e.type; |
| 1291 |
} |
| 1292 |
ret = optimize(ret, result, false); |
| 1293 |
} |
| 1294 |
else if (e2Opt.hasValue(!oror)) |
| 1295 |
{ |
| 1296 |
// e1 || false -> e1 |
| 1297 |
// e1 && true -> e1 |
| 1298 |
return returnE_e1(); |
| 1299 |
} |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
void visitCmp(CmpExp e) |
| 1304 |
{ |
| 1305 |
//printf("CmpExp::optimize() %s\n", e.toChars()); |
| 1306 |
if (binOptimize(e, WANTvalue)) |
| 1307 |
return; |
| 1308 |
Expression e1 = fromConstInitializer(result, e.e1); |
| 1309 |
Expression e2 = fromConstInitializer(result, e.e2); |
| 1310 |
ret = Cmp(e.op, e.loc, e.type, e1, e2).copy(); |
| 1311 |
if (CTFEExp.isCantExp(ret)) |
| 1312 |
ret = e; |
| 1313 |
} |
| 1314 |
|
| 1315 |
void visitCat(CatExp e) |
| 1316 |
{ |
| 1317 |
//printf("CatExp::optimize(%d) %s\n", result, e.toChars()); |
| 1318 |
if (binOptimize(e, result)) |
| 1319 |
return; |
| 1320 |
|
| 1321 |
if (e.type == Type.tstring) |
| 1322 |
if (auto ce1 = e.e1.isCatExp()) |
| 1323 |
{ |
| 1324 |
// https://issues.dlang.org/show_bug.cgi?id=12798 |
| 1325 |
// optimize ((expr ~ str1) ~ str2) |
| 1326 |
// https://issues.dlang.org/show_bug.cgi?id=24078 |
| 1327 |
// This optimization is only valid if `expr` is a string. |
| 1328 |
// Otherwise it leads to: |
| 1329 |
// `["c"] ~ "a" ~ "b"` becoming `["c"] ~ "ab"` |
| 1330 |
scope CatExp cex = new CatExp(e.loc, ce1.e2, e.e2); |
| 1331 |
cex.type = e.type; |
| 1332 |
Expression ex = optimize(cex, result, false); |
| 1333 |
if (ex != cex) |
| 1334 |
{ |
| 1335 |
e.e1 = ce1.e1; |
| 1336 |
e.e2 = ex; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
// optimize "str"[] -> "str" |
| 1340 |
if (auto se1 = e.e1.isSliceExp()) |
| 1341 |
{ |
| 1342 |
if (se1.e1.op == EXP.string_ && !se1.lwr) |
| 1343 |
e.e1 = se1.e1; |
| 1344 |
} |
| 1345 |
if (auto se2 = e.e2.isSliceExp()) |
| 1346 |
{ |
| 1347 |
if (se2.e1.op == EXP.string_ && !se2.lwr) |
| 1348 |
e.e2 = se2.e1; |
| 1349 |
} |
| 1350 |
ret = Cat(e.loc, e.type, e.e1, e.e2).copy(); |
| 1351 |
if (CTFEExp.isCantExp(ret)) |
| 1352 |
ret = e; |
| 1353 |
} |
| 1354 |
|
| 1355 |
void visitCond(CondExp e) |
| 1356 |
{ |
| 1357 |
if (expOptimize(e.econd, WANTvalue)) |
| 1358 |
return; |
| 1359 |
const opt = e.econd.toBool(); |
| 1360 |
if (opt.hasValue(true)) |
| 1361 |
ret = optimize(e.e1, result, keepLvalue); |
| 1362 |
else if (opt.hasValue(false)) |
| 1363 |
ret = optimize(e.e2, result, keepLvalue); |
| 1364 |
else |
| 1365 |
{ |
| 1366 |
expOptimize(e.e1, result, keepLvalue); |
| 1367 |
expOptimize(e.e2, result, keepLvalue); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
// Optimize the expression until it can no longer be simplified. |
| 1372 |
size_t b; |
| 1373 |
while (1) |
| 1374 |
{ |
| 1375 |
if (b++ == global.recursionLimit) |
| 1376 |
{ |
| 1377 |
error(e.loc, "infinite loop while optimizing expression"); |
| 1378 |
return ErrorExp.get(); |
| 1379 |
} |
| 1380 |
|
| 1381 |
auto ex = ret; |
| 1382 |
switch (ex.op) |
| 1383 |
{ |
| 1384 |
case EXP.variable: visitVar(ex.isVarExp()); break; |
| 1385 |
case EXP.tuple: visitTuple(ex.isTupleExp()); break; |
| 1386 |
case EXP.arrayLiteral: visitArrayLiteral(ex.isArrayLiteralExp()); break; |
| 1387 |
case EXP.assocArrayLiteral: visitAssocArrayLiteral(ex.isAssocArrayLiteralExp()); break; |
| 1388 |
case EXP.structLiteral: visitStructLiteral(ex.isStructLiteralExp()); break; |
| 1389 |
|
| 1390 |
case EXP.import_: |
| 1391 |
case EXP.assert_: |
| 1392 |
case EXP.dotIdentifier: |
| 1393 |
case EXP.dotTemplateDeclaration: |
| 1394 |
case EXP.dotTemplateInstance: |
| 1395 |
case EXP.delegate_: |
| 1396 |
case EXP.dotType: |
| 1397 |
case EXP.uadd: |
| 1398 |
case EXP.delete_: |
| 1399 |
case EXP.vector: |
| 1400 |
case EXP.vectorArray: |
| 1401 |
case EXP.array: |
| 1402 |
case EXP.delegatePointer: |
| 1403 |
case EXP.delegateFunctionPointer: |
| 1404 |
case EXP.preMinusMinus: |
| 1405 |
case EXP.prePlusPlus: visitUna(cast(UnaExp)ex); break; |
| 1406 |
|
| 1407 |
case EXP.negate: visitNeg(ex.isNegExp()); break; |
| 1408 |
case EXP.tilde: visitCom(ex.isComExp()); break; |
| 1409 |
case EXP.not: visitNop(ex.isNotExp()); break; |
| 1410 |
case EXP.symbolOffset: visitSymOff(ex.isSymOffExp()); break; |
| 1411 |
case EXP.address: visitAddr(ex.isAddrExp()); break; |
| 1412 |
case EXP.star: visitPtr(ex.isPtrExp()); break; |
| 1413 |
case EXP.dotVariable: visitDotVar(ex.isDotVarExp()); break; |
| 1414 |
case EXP.new_: visitNew(ex.isNewExp()); break; |
| 1415 |
case EXP.call: visitCall(ex.isCallExp()); break; |
| 1416 |
case EXP.cast_: visitCast(ex.isCastExp()); break; |
| 1417 |
|
| 1418 |
case EXP.addAssign: |
| 1419 |
case EXP.minAssign: |
| 1420 |
case EXP.mulAssign: |
| 1421 |
case EXP.divAssign: |
| 1422 |
case EXP.modAssign: |
| 1423 |
case EXP.andAssign: |
| 1424 |
case EXP.orAssign: |
| 1425 |
case EXP.xorAssign: |
| 1426 |
case EXP.powAssign: |
| 1427 |
case EXP.leftShiftAssign: |
| 1428 |
case EXP.rightShiftAssign: |
| 1429 |
case EXP.unsignedRightShiftAssign: |
| 1430 |
case EXP.concatenateDcharAssign: visitBinAssign(ex.isBinAssignExp()); break; |
| 1431 |
case EXP.concatenateElemAssign: |
| 1432 |
case EXP.concatenateAssign: visitCatAssign(cast(CatAssignExp) ex); break; |
| 1433 |
|
| 1434 |
case EXP.minusMinus: |
| 1435 |
case EXP.plusPlus: |
| 1436 |
case EXP.assign: |
| 1437 |
case EXP.construct: |
| 1438 |
case EXP.blit: |
| 1439 |
case EXP.in_: |
| 1440 |
case EXP.remove: |
| 1441 |
case EXP.dot: visitBin(cast(BinExp)ex); break; |
| 1442 |
|
| 1443 |
case EXP.add: visitAdd(ex.isAddExp()); break; |
| 1444 |
case EXP.min: visitMin(ex.isMinExp()); break; |
| 1445 |
case EXP.mul: visitMul(ex.isMulExp()); break; |
| 1446 |
case EXP.div: visitDiv(ex.isDivExp()); break; |
| 1447 |
case EXP.mod: visitMod(ex.isModExp()); break; |
| 1448 |
case EXP.leftShift: visitShl(ex.isShlExp()); break; |
| 1449 |
case EXP.rightShift: visitShr(ex.isShrExp()); break; |
| 1450 |
case EXP.unsignedRightShift: visitUshr(ex.isUshrExp()); break; |
| 1451 |
case EXP.and: visitAnd(ex.isAndExp()); break; |
| 1452 |
case EXP.or: visitOr(ex.isOrExp()); break; |
| 1453 |
case EXP.xor: visitXor(ex.isXorExp()); break; |
| 1454 |
case EXP.pow: visitPow(ex.isPowExp()); break; |
| 1455 |
case EXP.comma: visitComma(ex.isCommaExp()); break; |
| 1456 |
case EXP.arrayLength: visitArrayLength(ex.isArrayLengthExp()); break; |
| 1457 |
case EXP.notEqual: |
| 1458 |
case EXP.equal: visitEqual(ex.isEqualExp()); break; |
| 1459 |
case EXP.notIdentity: |
| 1460 |
case EXP.identity: visitIdentity(ex.isIdentityExp()); break; |
| 1461 |
case EXP.index: visitIndex(ex.isIndexExp()); break; |
| 1462 |
case EXP.slice: visitSlice(ex.isSliceExp()); break; |
| 1463 |
case EXP.andAnd: |
| 1464 |
case EXP.orOr: visitLogical(ex.isLogicalExp()); break; |
| 1465 |
case EXP.lessThan: |
| 1466 |
case EXP.lessOrEqual: |
| 1467 |
case EXP.greaterThan: |
| 1468 |
case EXP.greaterOrEqual: visitCmp(cast(CmpExp)ex); break; |
| 1469 |
case EXP.concatenate: visitCat(ex.isCatExp()); break; |
| 1470 |
case EXP.question: visitCond(ex.isCondExp()); break; |
| 1471 |
|
| 1472 |
default: visitExp(ex); break; |
| 1473 |
} |
| 1474 |
|
| 1475 |
if (ex == ret) |
| 1476 |
break; |
| 1477 |
} |
| 1478 |
return ret; |
| 1479 |
} |