the current one. */
#define ECF_FORK_OR_EXEC 128
#define ECF_SIBCALL 256
+/* Nonzero if this is a call to "pure" function (like const function,
+ but may read memory. */
+#define ECF_PURE 512
static void emit_call_1 PARAMS ((rtx, tree, tree, HOST_WIDE_INT,
HOST_WIDE_INT, HOST_WIDE_INT, rtx,
if (! call_insn)
abort ();
+ /* Mark memory as used for "pure" function call. */
+ if (ecf_flags & ECF_PURE)
+ {
+ call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
+ gen_rtx_USE (VOIDmode,
+ gen_rtx_MEM (BLKmode,
+ gen_rtx_SCRATCH (VOIDmode))), call_fusage);
+ }
+
/* Put the register usage information on the CALL. If there is already
some usage information, put ours at the end. */
if (CALL_INSN_FUNCTION_USAGE (call_insn))
CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
/* If this is a const call, then set the insn's unchanging bit. */
- if (ecf_flags & ECF_CONST)
+ if (ecf_flags & (ECF_CONST | ECF_PURE))
CONST_CALL_P (call_insn) = 1;
/* If this call can't throw, attach a REG_EH_REGION reg note to that
if (rounded_stack_size != 0)
{
if (flag_defer_pop && inhibit_defer_pop == 0
- && !(ecf_flags & ECF_CONST))
+ && !(ecf_flags & (ECF_CONST | ECF_PURE)))
pending_stack_adjust += rounded_stack_size;
else
adjust_stack (rounded_stack_size_rtx);
if (DECL_P (exp) && DECL_IS_MALLOC (exp))
flags |= ECF_MALLOC;
+ /* The function exp may have the `pure' attribute. */
+ if (DECL_P (exp) && DECL_IS_PURE (exp))
+ flags |= ECF_PURE;
+
if (TREE_NOTHROW (exp))
flags |= ECF_NOTHROW;
}
MEM_SET_IN_STRUCT_P (copy, AGGREGATE_TYPE_P (type));
store_expr (args[i].tree_value, copy, 0);
- *ecf_flags &= ~ECF_CONST;
+ *ecf_flags &= ~(ECF_CONST | ECF_PURE);
args[i].tree_value = build1 (ADDR_EXPR,
build_pointer_type (type),
/* If this is an addressable type, we cannot pre-evaluate it. Thus,
we cannot consider this function call constant. */
if (TREE_ADDRESSABLE (type))
- *ecf_flags &= ~ECF_CONST;
+ *ecf_flags &= ~(ECF_CONST | ECF_PURE);
/* Compute the stack-size of this argument. */
if (args[i].reg == 0 || args[i].partial != 0
which have already been stored into the stack. */
for (i = 0; i < num_actuals; i++)
- if ((flags & ECF_CONST)
+ if ((flags & (ECF_CONST | ECF_PURE))
|| ((args_size->var != 0 || args_size->constant != 0)
&& calls_function (args[i].tree_value, 1))
|| (must_preallocate
if (aggregate_value_p (exp))
{
/* This call returns a big structure. */
- flags &= ~ECF_CONST;
+ flags &= ~(ECF_CONST | ECF_PURE);
#ifdef PCC_STATIC_STRUCT_RETURN
{
/* When calling a const function, we must pop the stack args right away,
so that the pop is deleted or moved with the call. */
- if (flags & ECF_CONST)
+ if (flags & (ECF_CONST | ECF_PURE))
NO_DEFER_POP;
/* Don't let pending stack adjusts add up to too much.
Also do not make a sibling call. */
- flags &= ~ECF_CONST;
+ flags &= ~(ECF_CONST | ECF_PURE);
must_preallocate = 1;
sibcall_failure = 1;
}
/* Now we are about to start emitting insns that can be deleted
if a libcall is deleted. */
- if (flags & (ECF_CONST | ECF_MALLOC))
+ if (flags & (ECF_CONST | ECF_PURE | ECF_MALLOC))
start_sequence ();
old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
{
/* When the stack adjustment is pending, we get better code
by combining the adjustments. */
- if (pending_stack_adjust && ! (flags & ECF_CONST)
+ if (pending_stack_adjust && ! (flags & (ECF_CONST | ECF_PURE))
&& ! inhibit_defer_pop)
{
int adjust;
Test valreg so we don't crash; may safely ignore `const'
if return type is void. Disable for PARALLEL return values, because
we have no way to move such values into a pseudo register. */
- if ((flags & ECF_CONST) && valreg != 0 && GET_CODE (valreg) != PARALLEL)
+ if ((flags & (ECF_CONST | ECF_PURE))
+ && valreg != 0 && GET_CODE (valreg) != PARALLEL)
{
rtx note = 0;
rtx temp = gen_reg_rtx (GET_MODE (valreg));
insns = get_insns ();
end_sequence ();
+ if (flags & ECF_PURE)
+ note = gen_rtx_EXPR_LIST (VOIDmode,
+ gen_rtx_USE (VOIDmode,
+ gen_rtx_MEM (BLKmode,
+ gen_rtx_SCRATCH (VOIDmode))), note);
+
emit_libcall_block (insns, temp, valreg, note);
valreg = temp;
}
- else if (flags & ECF_CONST)
+ else if (flags & (ECF_CONST | ECF_PURE))
{
/* Otherwise, just write out the sequence without a note. */
rtx insns = get_insns ();
The RETVAL parameter specifies whether return value needs to be saved, other
parameters are documented in the emit_library_call function bellow. */
static rtx
-emit_library_call_value_1 (retval, orgfun, value, no_queue, outmode, nargs, p)
+emit_library_call_value_1 (retval, orgfun, value, fn_type, outmode, nargs, p)
int retval;
rtx orgfun;
rtx value;
- int no_queue;
+ int fn_type;
enum machine_mode outmode;
int nargs;
va_list p;
#endif
#endif
- if (no_queue)
+ if (fn_type == 1)
flags |= ECF_CONST;
+ else if (fn_type == 2)
+ flags |= ECF_PURE;
fun = orgfun;
if (libfunc_nothrow (fun))
#endif
/* This call returns a big structure. */
- flags &= ~ECF_CONST;
+ flags &= ~(ECF_CONST | ECF_PURE);
}
/* ??? Unfinished: must pass the memory address as an argument. */
/* Now we are about to start emitting insns that can be deleted
if a libcall is deleted. */
- if (flags & ECF_CONST)
+ if (flags & (ECF_CONST | ECF_PURE))
start_sequence ();
push_temp_slots ();
Test valreg so we don't crash; may safely ignore `const'
if return type is void. Disable for PARALLEL return values, because
we have no way to move such values into a pseudo register. */
- if ((flags & ECF_CONST)
+ if ((flags & (ECF_CONST | ECF_PURE))
&& valreg != 0 && GET_CODE (valreg) != PARALLEL)
{
rtx note = 0;
insns = get_insns ();
end_sequence ();
+ if (flags & ECF_PURE)
+ note = gen_rtx_EXPR_LIST (VOIDmode,
+ gen_rtx_USE (VOIDmode,
+ gen_rtx_MEM (BLKmode,
+ gen_rtx_SCRATCH (VOIDmode))), note);
+
emit_libcall_block (insns, temp, valreg, note);
valreg = temp;
}
- else if (flags & ECF_CONST)
+ else if (flags & (ECF_CONST | ECF_PURE))
{
/* Otherwise, just write out the sequence without a note. */
rtx insns = get_insns ();
and machine_modes to convert them to.
The rtx values should have been passed through protect_from_queue already.
- NO_QUEUE will be true if and only if the library call is a `const' call
- which will be enclosed in REG_LIBCALL/REG_RETVAL notes; it is equivalent
- to the flag ECF_CONST in expand_call.
-
- NO_QUEUE must be true for const calls, because if it isn't, then
- any pending increment will be emitted between REG_LIBCALL/REG_RETVAL notes,
- and will be lost if the libcall sequence is optimized away.
-
- NO_QUEUE must be false for non-const calls, because if it isn't, the
- call insn will have its CONST_CALL_P bit set, and it will be incorrectly
- optimized. For instance, the instruction scheduler may incorrectly
- move memory references across the non-const call. */
+ FN_TYPE will is zero for `normal' calls, one for `const' calls, wich
+ which will be enclosed in REG_LIBCALL/REG_RETVAL notes and two for `pure'
+ calls, that are handled like `const' calls with extra
+ (use (memory (scratch)). */
void
-emit_library_call VPARAMS((rtx orgfun, int no_queue, enum machine_mode outmode,
+emit_library_call VPARAMS((rtx orgfun, int fn_type, enum machine_mode outmode,
int nargs, ...))
{
#ifndef ANSI_PROTOTYPES
rtx orgfun;
- int no_queue;
+ int fn_type;
enum machine_mode outmode;
int nargs;
#endif
#ifndef ANSI_PROTOTYPES
orgfun = va_arg (p, rtx);
- no_queue = va_arg (p, int);
+ fn_type = va_arg (p, int);
outmode = va_arg (p, enum machine_mode);
nargs = va_arg (p, int);
#endif
- emit_library_call_value_1 (0, orgfun, NULL_RTX, no_queue, outmode, nargs, p);
+ emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
va_end (p);
}
If VALUE is nonzero, VALUE is returned. */
rtx
-emit_library_call_value VPARAMS((rtx orgfun, rtx value, int no_queue,
+emit_library_call_value VPARAMS((rtx orgfun, rtx value, int fn_type,
enum machine_mode outmode, int nargs, ...))
{
#ifndef ANSI_PROTOTYPES
rtx orgfun;
rtx value;
- int no_queue;
+ int fn_type;
enum machine_mode outmode;
int nargs;
#endif
#ifndef ANSI_PROTOTYPES
orgfun = va_arg (p, rtx);
value = va_arg (p, rtx);
- no_queue = va_arg (p, int);
+ fn_type = va_arg (p, int);
outmode = va_arg (p, enum machine_mode);
nargs = va_arg (p, int);
#endif
- value = emit_library_call_value_1 (1, orgfun, value, no_queue, outmode, nargs, p);
+ value = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode, nargs, p);
va_end (p);
volatile voidfn fatal;
@end smallexample
-@cindex @code{const} function attribute
-@item const
-Many functions do not examine any values except their arguments, and
-have no effects except the return value. Such a function can be subject
+@cindex @code{pure} function attribute
+@item pure
+Many functions have no effects except the return value and their
+return value and depends only on the parameters and/or global variables.
+Such a function can be subject
to common subexpression elimination and loop optimization just as an
arithmetic operator would be. These functions should be declared
-with the attribute @code{const}. For example,
+with the attribute @code{pure}. For example,
@smallexample
-int square (int) __attribute__ ((const));
+int square (int) __attribute__ ((pure));
@end smallexample
@noindent
says that the hypothetical function @code{square} is safe to call
fewer times than the program says.
+Some of common examples of pure functions are @code{strlen} or @code{memcmp}.
+Interesting non-pure functions are functions with infinite loops or those
+depending on volatile memory or other system resource, that may change between
+two consetuctive calls (such as @code{feof} in multithreding environment).
+
+The attribute @code{pure} is not implemented in GNU C versions earlier
+than 2.96.
+@cindex @code{const} function attribute
+@item const
+Many functions do not examine any values except their arguments, and
+have no effects except the return value. Basically this is just slightly
+more strict class than the "pure" attribute above, since function is not
+alloved to read global memory.
+
+@cindex pointer arguments
+Note that a function that has pointer arguments and examines the data
+pointed to must @emph{not} be declared @code{const}. Likewise, a
+function that calls a non-@code{const} function usually must not be
+@code{const}. It does not make sense for a @code{const} function to
+return @code{void}.
+
The attribute @code{const} is not implemented in GNU C versions earlier
than 2.5. An alternative way to declare that a function has no side
effects, which works in the current version and in some older versions,
This approach does not work in GNU C++ from 2.6.0 on, since the language
specifies that the @samp{const} must be attached to the return value.
-@cindex pointer arguments
-Note that a function that has pointer arguments and examines the data
-pointed to must @emph{not} be declared @code{const}. Likewise, a
-function that calls a non-@code{const} function usually must not be
-@code{const}. It does not make sense for a @code{const} function to
-return @code{void}.
@item format (@var{archetype}, @var{string-index}, @var{first-to-check})
@cindex @code{format} function attribute