Skip to content

Commit 0b4ba56

Browse files
committed
Optimizer: Use return true / return false for functions returning bool
Changes done with Coccinelle: @r1@ identifier fn; typedef bool; symbol false; symbol true; @@ bool fn ( ... ) { <... return ( - 0 + false | - 1 + true ) ; ...> } Coccinelle patch sourced from torvalds/linux@46b5c9b.
1 parent 8bbd59f commit 0b4ba56

File tree

11 files changed

+136
-136
lines changed

11 files changed

+136
-136
lines changed

Zend/Optimizer/block_pass.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ bool zend_optimizer_get_persistent_constant(zend_string *name, zval *result, int
4242
if (copy) {
4343
Z_TRY_ADDREF_P(result);
4444
}
45-
return 1;
45+
return true;
4646
} else {
47-
return 0;
47+
return false;
4848
}
4949
}
5050

5151
/* Special constants null/true/false can always be substituted. */
5252
c = zend_get_special_const(ZSTR_VAL(name), ZSTR_LEN(name));
5353
if (c) {
5454
ZVAL_COPY_VALUE(result, &c->value);
55-
return 1;
55+
return true;
5656
}
57-
return 0;
57+
return false;
5858
}
5959

6060
/* Data dependencies macros */

Zend/Optimizer/dce.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ typedef struct {
6262
static inline bool is_bad_mod(const zend_ssa *ssa, int use, int def) {
6363
if (def < 0) {
6464
/* This modification is not tracked by SSA, assume the worst */
65-
return 1;
65+
return true;
6666
}
6767
if (ssa->var_info[use].type & MAY_BE_REF) {
6868
/* Modification of reference may have side-effect */
69-
return 1;
69+
return true;
7070
}
71-
return 0;
71+
return false;
7272
}
7373

7474
static inline bool may_have_side_effects(
@@ -125,18 +125,18 @@ static inline bool may_have_side_effects(
125125
case ZEND_FUNC_GET_ARGS:
126126
case ZEND_ARRAY_KEY_EXISTS:
127127
/* No side effects */
128-
return 0;
128+
return false;
129129
case ZEND_FREE:
130130
return opline->extended_value == ZEND_FREE_VOID_CAST;
131131
case ZEND_ADD_ARRAY_ELEMENT:
132132
/* TODO: We can't free two vars. Keep instruction alive. <?php [0, "$a" => "$b"]; */
133133
if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) && (opline->op2_type & (IS_VAR|IS_TMP_VAR))) {
134-
return 1;
134+
return true;
135135
}
136-
return 0;
136+
return false;
137137
case ZEND_ROPE_END:
138138
/* TODO: Rope dce optimization, see #76446 */
139-
return 1;
139+
return true;
140140
case ZEND_JMP:
141141
case ZEND_JMPZ:
142142
case ZEND_JMPNZ:
@@ -149,7 +149,7 @@ static inline bool may_have_side_effects(
149149
case ZEND_BIND_INIT_STATIC_OR_JMP:
150150
case ZEND_JMP_FRAMELESS:
151151
/* For our purposes a jumps and branches are side effects. */
152-
return 1;
152+
return true;
153153
case ZEND_BEGIN_SILENCE:
154154
case ZEND_END_SILENCE:
155155
case ZEND_ECHO:
@@ -164,7 +164,7 @@ static inline bool may_have_side_effects(
164164
case ZEND_YIELD_FROM:
165165
case ZEND_VERIFY_NEVER_TYPE:
166166
/* Intrinsic side effects */
167-
return 1;
167+
return true;
168168
case ZEND_DO_FCALL:
169169
case ZEND_DO_FCALL_BY_NAME:
170170
case ZEND_DO_ICALL:
@@ -174,31 +174,31 @@ static inline bool may_have_side_effects(
174174
case ZEND_FRAMELESS_ICALL_2:
175175
case ZEND_FRAMELESS_ICALL_3:
176176
/* For now assume all calls have side effects */
177-
return 1;
177+
return true;
178178
case ZEND_RECV:
179179
case ZEND_RECV_INIT:
180180
/* Even though RECV_INIT can be side-effect free, these cannot be simply dropped
181181
* due to the prologue skipping code. */
182-
return 1;
182+
return true;
183183
case ZEND_ASSIGN_REF:
184-
return 1;
184+
return true;
185185
case ZEND_ASSIGN:
186186
{
187187
if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)) {
188-
return 1;
188+
return true;
189189
}
190190
if (!reorder_dtor_effects) {
191191
if (opline->op2_type != IS_CONST
192192
&& (OP2_INFO() & MAY_HAVE_DTOR)
193193
&& ssa->vars[ssa_op->op2_use].escape_state != ESCAPE_STATE_NO_ESCAPE) {
194194
/* DCE might shorten lifetime */
195-
return 1;
195+
return true;
196196
}
197197
}
198-
return 0;
198+
return false;
199199
}
200200
case ZEND_UNSET_VAR:
201-
return 1;
201+
return true;
202202
case ZEND_UNSET_CV:
203203
{
204204
uint32_t t1 = OP1_INFO();
@@ -207,9 +207,9 @@ static inline bool may_have_side_effects(
207207
* an unset may be considered dead even if there is a later assignment to the
208208
* variable. Removing the unset in this case would not be correct if the variable
209209
* is a reference, because unset breaks references. */
210-
return 1;
210+
return true;
211211
}
212-
return 0;
212+
return false;
213213
}
214214
case ZEND_PRE_INC:
215215
case ZEND_POST_INC:
@@ -223,41 +223,41 @@ static inline bool may_have_side_effects(
223223
case ZEND_ASSIGN_OBJ:
224224
if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
225225
|| ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
226-
return 1;
226+
return true;
227227
}
228228
if (!reorder_dtor_effects) {
229229
opline++;
230230
ssa_op++;
231231
if (opline->op1_type != IS_CONST
232232
&& (OP1_INFO() & MAY_HAVE_DTOR)) {
233233
/* DCE might shorten lifetime */
234-
return 1;
234+
return true;
235235
}
236236
}
237-
return 0;
237+
return false;
238238
case ZEND_PRE_INC_OBJ:
239239
case ZEND_PRE_DEC_OBJ:
240240
case ZEND_POST_INC_OBJ:
241241
case ZEND_POST_DEC_OBJ:
242242
if (is_bad_mod(ssa, ssa_op->op1_use, ssa_op->op1_def)
243243
|| ssa->vars[ssa_op->op1_def].escape_state != ESCAPE_STATE_NO_ESCAPE) {
244-
return 1;
244+
return true;
245245
}
246-
return 0;
246+
return false;
247247
case ZEND_BIND_STATIC:
248248
if (op_array->static_variables) {
249249
/* Implicit and Explicit bind static is effectively prologue of closure so
250250
report it has side effects like RECV, RECV_INIT; This allows us to
251251
reflect on the closure and discover used variable at runtime */
252252
if ((opline->extended_value & (ZEND_BIND_IMPLICIT|ZEND_BIND_EXPLICIT))) {
253-
return 1;
253+
return true;
254254
}
255255
/* Modifies static variables which are observable through reflection */
256256
if ((opline->extended_value & ZEND_BIND_REF) && opline->op2_type != IS_UNUSED) {
257-
return 1;
257+
return true;
258258
}
259259
}
260-
return 0;
260+
return false;
261261
case ZEND_CHECK_VAR:
262262
return (OP1_INFO() & MAY_BE_UNDEF) != 0;
263263
case ZEND_FE_RESET_R:
@@ -267,7 +267,7 @@ static inline bool may_have_side_effects(
267267
return (OP1_INFO() & MAY_BE_ANY) != MAY_BE_ARRAY;
268268
default:
269269
/* For everything we didn't handle, assume a side-effect */
270-
return 1;
270+
return true;
271271
}
272272
}
273273

@@ -340,7 +340,7 @@ static inline bool is_var_dead(context *ctx, int var_num) {
340340
// Sometimes we can mark the var as EXT_UNUSED
341341
static bool try_remove_var_def(context *ctx, int free_var, int use_chain, zend_op *opline) {
342342
if (use_chain >= 0) {
343-
return 0;
343+
return false;
344344
}
345345
zend_ssa_var *var = &ctx->ssa->vars[free_var];
346346
int def = var->definition;
@@ -381,13 +381,13 @@ static bool try_remove_var_def(context *ctx, int free_var, int use_chain, zend_o
381381
def_opline->result.var = 0;
382382
def_op->result_def = -1;
383383
var->definition = -1;
384-
return 1;
384+
return true;
385385
default:
386386
break;
387387
}
388388
}
389389
}
390-
return 0;
390+
return false;
391391
}
392392

393393
static zend_always_inline bool may_be_refcounted(uint32_t type) {
@@ -400,13 +400,13 @@ static inline bool is_free_of_live_var(context *ctx, zend_op *opline, zend_ssa_o
400400
/* It is always safe to remove FREEs of non-refcounted values, even if they are live. */
401401
if ((ctx->ssa->var_info[ssa_op->op1_use].type & (MAY_BE_REF|MAY_BE_ANY|MAY_BE_UNDEF)) != 0
402402
&& !may_be_refcounted(ctx->ssa->var_info[ssa_op->op1_use].type)) {
403-
return 0;
403+
return false;
404404
}
405405
ZEND_FALLTHROUGH;
406406
case ZEND_FE_FREE:
407407
return !is_var_dead(ctx, ssa_op->op1_use);
408408
default:
409-
return 0;
409+
return false;
410410
}
411411
}
412412

@@ -417,12 +417,12 @@ static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
417417
uint8_t free_var_type;
418418

419419
if (opline->opcode == ZEND_NOP) {
420-
return 0;
420+
return false;
421421
}
422422

423423
/* We mark FREEs as dead, but they're only really dead if the destroyed var is dead */
424424
if (is_free_of_live_var(ctx, opline, ssa_op)) {
425-
return 0;
425+
return false;
426426
}
427427

428428
if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))&& !is_var_dead(ctx, ssa_op->op1_use)) {
@@ -440,7 +440,7 @@ static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
440440
if (free_var >= 0) {
441441
// TODO: We can't free two vars. Keep instruction alive.
442442
zend_bitset_excl(ctx->instr_dead, opline - ctx->op_array->opcodes);
443-
return 0;
443+
return false;
444444
}
445445
free_var = ssa_op->op2_use;
446446
free_var_type = opline->op2_type;
@@ -459,9 +459,9 @@ static bool dce_instr(context *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
459459
ssa_op->op1_use = free_var;
460460
ssa_op->op1_use_chain = ssa->vars[free_var].use_chain;
461461
ssa->vars[free_var].use_chain = ssa_op - ssa->ops;
462-
return 0;
462+
return false;
463463
}
464-
return 1;
464+
return true;
465465
}
466466

467467
static inline int get_common_phi_source(zend_ssa *ssa, zend_ssa_phi *phi) {
@@ -507,17 +507,17 @@ static void try_remove_trivial_phi(context *ctx, zend_ssa_phi *phi) {
507507
static inline bool may_break_varargs(const zend_op_array *op_array, const zend_ssa *ssa, const zend_ssa_op *ssa_op) {
508508
if (ssa_op->op1_def >= 0
509509
&& ssa->vars[ssa_op->op1_def].var < op_array->num_args) {
510-
return 1;
510+
return true;
511511
}
512512
if (ssa_op->op2_def >= 0
513513
&& ssa->vars[ssa_op->op2_def].var < op_array->num_args) {
514-
return 1;
514+
return true;
515515
}
516516
if (ssa_op->result_def >= 0
517517
&& ssa->vars[ssa_op->result_def].var < op_array->num_args) {
518-
return 1;
518+
return true;
519519
}
520-
return 0;
520+
return false;
521521
}
522522

523523
static inline bool may_throw_dce_exception(const zend_op *opline) {

Zend/Optimizer/dfa_pass.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ static void zend_ssa_remove_nops(zend_op_array *op_array, zend_ssa *ssa, zend_op
256256

257257
static bool safe_instanceof(const zend_class_entry *ce1, const zend_class_entry *ce2) {
258258
if (ce1 == ce2) {
259-
return 1;
259+
return true;
260260
}
261261
if (!(ce1->ce_flags & ZEND_ACC_LINKED)) {
262262
/* This case could be generalized, similarly to unlinked_instanceof */
263-
return 0;
263+
return false;
264264
}
265265
return instanceof_function(ce1, ce2);
266266
}
@@ -297,7 +297,7 @@ static inline bool can_elide_return_type_check(
297297
zend_ssa_var_info *use_info = &ssa->var_info[ssa_op->op1_use];
298298
uint32_t use_type = use_info->type & (MAY_BE_ANY|MAY_BE_UNDEF);
299299
if (use_type & MAY_BE_REF) {
300-
return 0;
300+
return false;
301301
}
302302

303303
if (use_type & MAY_BE_UNDEF) {
@@ -322,20 +322,20 @@ static bool opline_supports_assign_contraction(
322322
zend_op_array *op_array, zend_ssa *ssa, zend_op *opline, int src_var, uint32_t cv_var) {
323323
if (opline->opcode == ZEND_NEW) {
324324
/* see Zend/tests/generators/aborted_yield_during_new.phpt */
325-
return 0;
325+
return false;
326326
}
327327

328328
/* Frameless calls override the return value, but the return value may overlap with the arguments. */
329329
switch (opline->opcode) {
330330
case ZEND_FRAMELESS_ICALL_3:
331-
if ((opline + 1)->op1_type == IS_CV && (opline + 1)->op1.var == cv_var) return 0;
331+
if ((opline + 1)->op1_type == IS_CV && (opline + 1)->op1.var == cv_var) return false;
332332
ZEND_FALLTHROUGH;
333333
case ZEND_FRAMELESS_ICALL_2:
334-
if (opline->op2_type == IS_CV && opline->op2.var == cv_var) return 0;
334+
if (opline->op2_type == IS_CV && opline->op2.var == cv_var) return false;
335335
ZEND_FALLTHROUGH;
336336
case ZEND_FRAMELESS_ICALL_1:
337-
if (opline->op1_type == IS_CV && opline->op1.var == cv_var) return 0;
338-
return 1;
337+
if (opline->op1_type == IS_CV && opline->op1.var == cv_var) return false;
338+
return true;
339339
}
340340

341341
if (opline->opcode == ZEND_DO_ICALL || opline->opcode == ZEND_DO_UCALL
@@ -374,10 +374,10 @@ static bool opline_supports_assign_contraction(
374374
&& opline->op1_type == IS_CV
375375
&& opline->op1.var == cv_var
376376
&& zend_may_throw(opline, &ssa->ops[ssa->vars[src_var].definition], op_array, ssa)) {
377-
return 0;
377+
return false;
378378
}
379379

380-
return 1;
380+
return true;
381381
}
382382

383383
static bool variable_defined_or_used_in_range(zend_ssa *ssa, int var, int start, int end)
@@ -391,11 +391,11 @@ static bool variable_defined_or_used_in_range(zend_ssa *ssa, int var, int start,
391391
(ssa_op->op2_use >= 0 && ssa->vars[ssa_op->op2_use].var == var) ||
392392
(ssa_op->result_use >= 0 && ssa->vars[ssa_op->result_use].var == var)
393393
) {
394-
return 1;
394+
return true;
395395
}
396396
start++;
397397
}
398-
return 0;
398+
return false;
399399
}
400400

401401
int zend_dfa_optimize_calls(zend_op_array *op_array, zend_ssa *ssa)
@@ -989,7 +989,7 @@ static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ss
989989
if ((opline->op1_type == IS_CV && opline->op1.var == cv)
990990
|| (opline->op2_type == IS_CV && opline->op2.var == cv)
991991
|| (opline->result_type == IS_CV && opline->result.var == cv)) {
992-
return 0;
992+
return false;
993993
}
994994
opline--;
995995
i--;
@@ -1026,12 +1026,12 @@ static bool zend_dfa_try_to_replace_result(zend_op_array *op_array, zend_ssa *ss
10261026
op_array->opcodes[use].result.var = cv;
10271027
}
10281028

1029-
return 1;
1029+
return true;
10301030
}
10311031
}
10321032
}
10331033

1034-
return 0;
1034+
return false;
10351035
}
10361036

10371037
void zend_dfa_optimize_op_array(zend_op_array *op_array, zend_optimizer_ctx *ctx, zend_ssa *ssa, zend_call_info **call_map)

0 commit comments

Comments
 (0)