This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[tree-ssa] fold __builtin_expect
- From: Richard Henderson <rth at twiddle dot net>
- To: gcc-patches at gcc dot gnu dot org
- Date: Wed, 4 Feb 2004 22:11:39 -0800
- Subject: [tree-ssa] fold __builtin_expect
I noticed this while looking at some bits of kernel code. By folding
this away as soon as we know that the value is constant, we gain
opportunities to remove dead code.
Nothing tree-ssa specific, I guess, but it certainly helps most here.
r~
* builtins.c (fold_builtin_expect): New.
(fold_builtin_1): Call it.
Index: builtins.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/builtins.c,v
retrieving revision 1.152.2.48
diff -u -p -r1.152.2.48 builtins.c
--- builtins.c 4 Feb 2004 02:24:24 -0000 1.152.2.48
+++ builtins.c 5 Feb 2004 06:06:23 -0000
@@ -5650,6 +5650,44 @@ fold_builtin_constant_p (tree arglist)
return 0;
}
+/* Fold a call to __builtin_expect, if we expect that a comparison against
+ the argument will fold to a constant. In practice, this means a true
+ constant or the address of a non-weak symbol. ARGLIST is the argument
+ list of the call. */
+
+static tree
+fold_builtin_expect (tree arglist)
+{
+ tree arg, inner;
+
+ if (arglist == 0)
+ return 0;
+
+ arg = TREE_VALUE (arglist);
+
+ /* If the argument isn't invariant, then there's nothing we can do. */
+ if (!TREE_INVARIANT (arg))
+ return 0;
+
+ /* If we're looking at an address of a weak decl, then do not fold. */
+ inner = arg;
+ STRIP_NOPS (inner);
+ if (TREE_CODE (inner) == ADDR_EXPR)
+ {
+ do
+ {
+ inner = TREE_OPERAND (inner, 0);
+ }
+ while (TREE_CODE (inner) == COMPONENT_REF
+ || TREE_CODE (inner) == ARRAY_REF);
+ if (DECL_P (inner) && DECL_WEAK (inner))
+ return 0;
+ }
+
+ /* Otherwise, ARG already has the proper type for the return value. */
+ return arg;
+}
+
/* Fold a call to __builtin_classify_type. */
static tree
@@ -6622,6 +6660,9 @@ fold_builtin_1 (tree exp)
{
case BUILT_IN_CONSTANT_P:
return fold_builtin_constant_p (arglist);
+
+ case BUILT_IN_EXPECT:
+ return fold_builtin_expect (arglist);
case BUILT_IN_CLASSIFY_TYPE:
return fold_builtin_classify_type (arglist);