]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/d-gimplify.cc
1 /* D-specific tree lowering bits; see also gimple.c.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
20 #include "coretypes.h"
22 #include "dmd/globals.h"
25 #include "gimple-expr.h"
31 /* Return TRUE if an operand OP of a given TYPE being copied has no data.
32 The middle-end does a similar check with zero sized types. */
35 empty_modify_p (tree type
, tree op
)
37 tree_code code
= TREE_CODE (op
);
41 return empty_modify_p (type
, TREE_OPERAND (op
, 1));
44 /* Non-empty construcors are valid. */
45 if (CONSTRUCTOR_NELTS (op
) != 0 || TREE_CLOBBER_P (op
))
50 /* Leave nrvo alone because it isn't a copy. */
51 if (CALL_EXPR_RETURN_SLOT_OPT (op
))
56 /* If the operand doesn't have a simple form. */
57 if (!is_gimple_lvalue (op
) && !INDIRECT_REF_P (op
))
62 return empty_aggregate_p (type
);
65 /* Gimplify assignment from an INIT_EXPR or MODIFY_EXPR. */
67 static gimplify_status
68 d_gimplify_modify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
70 tree op0
= TREE_OPERAND (*expr_p
, 0);
71 tree op1
= TREE_OPERAND (*expr_p
, 1);
73 if (error_operand_p (op0
) || error_operand_p (op1
))
76 /* Remove any copies of empty aggregates. */
77 if (empty_modify_p (TREE_TYPE (op0
), op1
))
79 gimplify_expr (&TREE_OPERAND (*expr_p
, 0), pre_p
, post_p
,
80 is_gimple_lvalue
, fb_lvalue
);
82 if (TREE_SIDE_EFFECTS (op1
))
83 gimplify_and_add (op1
, pre_p
);
85 *expr_p
= TREE_OPERAND (*expr_p
, 0);
89 /* If the back end isn't clever enough to know that the lhs and rhs
90 types are the same, add an explicit conversion. */
91 if ((AGGREGATE_TYPE_P (TREE_TYPE (op0
)) || AGGREGATE_TYPE_P (TREE_TYPE (op1
)))
92 && !useless_type_conversion_p (TREE_TYPE (op1
), TREE_TYPE (op0
)))
94 TREE_OPERAND (*expr_p
, 1) = build1 (VIEW_CONVERT_EXPR
,
95 TREE_TYPE (op0
), op1
);
102 /* Gimplify an ADDR_EXPR node. */
104 static gimplify_status
105 d_gimplify_addr_expr (tree
*expr_p
)
107 tree op0
= TREE_OPERAND (*expr_p
, 0);
108 /* Constructors are not lvalues, so make them one. */
109 if (TREE_CODE (op0
) == CONSTRUCTOR
)
111 TREE_OPERAND (*expr_p
, 0) = force_target_expr (op0
);
118 /* Gimplify a CALL_EXPR node. */
120 static gimplify_status
121 d_gimplify_call_expr (tree
*expr_p
, gimple_seq
*pre_p
)
123 /* Strictly evaluate all arguments from left to right. */
124 int nargs
= call_expr_nargs (*expr_p
);
125 location_t loc
= EXPR_LOC_OR_LOC (*expr_p
, input_location
);
127 /* No need to enforce evaluation order if only one argument. */
131 /* Or if all arguments are already free of side-effects. */
132 bool has_side_effects
= false;
133 for (int i
= 0; i
< nargs
; i
++)
135 if (TREE_SIDE_EFFECTS (CALL_EXPR_ARG (*expr_p
, i
)))
137 has_side_effects
= true;
142 if (!has_side_effects
)
145 /* Leave the last argument for gimplify_call_expr. */
146 for (int i
= 0; i
< nargs
- 1; i
++)
148 tree new_arg
= CALL_EXPR_ARG (*expr_p
, i
);
150 /* If argument has a side-effect, gimplify_arg will handle it. */
151 if (gimplify_arg (&new_arg
, pre_p
, loc
) == GS_ERROR
)
154 /* Even if an argument itself doesn't have any side-effects, it
155 might be altered by another argument in the list. */
156 if (new_arg
== CALL_EXPR_ARG (*expr_p
, i
)
157 && !really_constant_p (new_arg
))
158 new_arg
= get_formal_tmp_var (new_arg
, pre_p
);
160 CALL_EXPR_ARG (*expr_p
, i
) = new_arg
;
166 /* Gimplify an UNSIGNED_RSHIFT_EXPR node. */
168 static gimplify_status
169 d_gimplify_unsigned_rshift_expr (tree
*expr_p
)
171 /* Convert op0 to an unsigned type. */
172 tree op0
= TREE_OPERAND (*expr_p
, 0);
173 tree op1
= TREE_OPERAND (*expr_p
, 1);
174 tree type
= d_unsigned_type (TREE_TYPE (op0
));
176 *expr_p
= convert (TREE_TYPE (*expr_p
),
177 build2 (RSHIFT_EXPR
, type
, convert (type
, op0
), op1
));
181 /* Implements the lang_hooks.gimplify_expr routine for language D.
182 Do gimplification of D specific expression trees in EXPR_P. */
185 d_gimplify_expr (tree
*expr_p
, gimple_seq
*pre_p
, gimple_seq
*post_p
)
187 switch (TREE_CODE (*expr_p
))
191 return d_gimplify_modify_expr (expr_p
, pre_p
, post_p
);
194 return d_gimplify_addr_expr (expr_p
);
197 return d_gimplify_call_expr (expr_p
, pre_p
);
199 case UNSIGNED_RSHIFT_EXPR
:
200 return d_gimplify_unsigned_rshift_expr (expr_p
);