GCC Wikia
Advertisement

このページを編集する際は,編集に関する方針に従ってください.[]

概要[]

引数[]

実装[]

2387 /* Return nonzero if two operands (typically of the same tree node)
2388    are necessarily equal.  If either argument has side-effects this
2389    function returns zero.  FLAGS modifies behavior as follows:
2390 
2391    If OEP_ONLY_CONST is set, only return nonzero for constants.
2392    This function tests whether the operands are indistinguishable;
2393    it does not test whether they are equal using C's == operation.
2394    The distinction is important for IEEE floating point, because
2395    (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2396    (2) two NaNs may be indistinguishable, but NaN!=NaN.
2397 
2398    If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2399    even though it may hold multiple values during a function.
2400    This is because a GCC tree node guarantees that nothing else is
2401    executed between the evaluation of its "operands" (which may often
2402    be evaluated in arbitrary order).  Hence if the operands themselves
2403    don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2404    same value in each operand/subexpression.  Hence leaving OEP_ONLY_CONST
2405    unset means assuming isochronic (or instantaneous) tree equivalence.
2406    Unless comparing arbitrary expression trees, such as from different
2407    statements, this flag can usually be left unset.
2408 
2409    If OEP_PURE_SAME is set, then pure functions with identical arguments
2410    are considered the same.  It is used when the caller has other ways
2411    to ensure that global memory is unchanged in between.  */
2412 
2413 int
2414 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2415 {

2416   /* If either is ERROR_MARK, they aren't equal.  */
2417   if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2418     return 0;
2419 

  • unsigned, signedが同じでなければ一致ではない

2420   /* If both types don't have the same signedness, then we can't consider
2421      them equal.  We must check this before the STRIP_NOPS calls
2422      because they may change the signedness of the arguments.  */
2423   if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2424     return 0;
2425 

  • NOPを省略

2426   STRIP_NOPS (arg0);
2427   STRIP_NOPS (arg1);
2428 

  • ここでコードが違かどうかと上でのチェック(NOPの省略による変化を考慮)

2429   if (TREE_CODE (arg0) != TREE_CODE (arg1)
2430       /* This is needed for conversions and for COMPONENT_REF.
2431          Might as well play it safe and always test this.  */
2432       || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2433       || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2434       || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2435     return 0;
2436 
2437   /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2438      We don't care about side effects in that case because the SAVE_EXPR
2439      takes care of that for us. In all other cases, two expressions are
2440      equal if they have no side effects.  If we have two identical
2441      expressions with side effects that should be treated the same due
2442      to the only side effects being identical SAVE_EXPR's, that will
2443      be detected in the recursive calls below.  */
2444   if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2445       && (TREE_CODE (arg0) == SAVE_EXPR
2446           || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2447     return 1;
2448 
2449   /* Next handle constant cases, those for which we can return 1 even
2450      if ONLY_CONST is set.  */
2451   if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2452     switch (TREE_CODE (arg0))
2453       {
2454       case INTEGER_CST:
2455         return (! TREE_CONSTANT_OVERFLOW (arg0)
2456                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2457                 && tree_int_cst_equal (arg0, arg1));
2458 
2459       case REAL_CST:
2460         return (! TREE_CONSTANT_OVERFLOW (arg0)
2461                 && ! TREE_CONSTANT_OVERFLOW (arg1)
2462                 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2463                                           TREE_REAL_CST (arg1)));
2464 
2465       case VECTOR_CST:
2466         {
2467           tree v1, v2;
2468 
2469           if (TREE_CONSTANT_OVERFLOW (arg0)
2470               || TREE_CONSTANT_OVERFLOW (arg1))
2471             return 0;
2472 
2473           v1 = TREE_VECTOR_CST_ELTS (arg0);
2474           v2 = TREE_VECTOR_CST_ELTS (arg1);
2475           while (v1 && v2)
2476             {
2477               if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2478                                     flags))
2479                 return 0;
2480               v1 = TREE_CHAIN (v1);
2481               v2 = TREE_CHAIN (v2);
2482             }
2483 
2484           return v1 == v2;
2485         }
2486 
2487       case COMPLEX_CST:
2488         return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2489                                  flags)
2490                 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2491                                     flags));
2492 
2493       case STRING_CST:
2494         return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2495                 && ! memcmp (TREE_STRING_POINTER (arg0),
2496                               TREE_STRING_POINTER (arg1),
2497                               TREE_STRING_LENGTH (arg0)));
2498 
2499       case ADDR_EXPR:
2500         return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2501                                 0);
2502       default:
2503         break;
2504       }
2505 
2506   if (flags & OEP_ONLY_CONST)
2507     return 0;
2508 


2509 /* Define macros to test an operand from arg0 and arg1 for equality and a
2510    variant that allows null and views null as being different from any
2511    non-null value.  In the latter case, if either is null, the both
2512    must be; otherwise, do the normal comparison.  */
2513 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N),     \
2514                                     TREE_OPERAND (arg1, N), flags)
2515 


2516 #define OP_SAME_WITH_NULL(N)                            \
2517   ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2518    ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2519 


2520   switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2521     {
2522     case tcc_unary:
2523       /* Two conversions are equal only if signedness and modes match.  */
2524       switch (TREE_CODE (arg0))
2525         {
2526         case NOP_EXPR:
2527         case CONVERT_EXPR:
2528         case FIX_CEIL_EXPR:
2529         case FIX_TRUNC_EXPR:
2530         case FIX_FLOOR_EXPR:
2531         case FIX_ROUND_EXPR:
2532           if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2533               != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2534             return 0;
2535           break;
2536         default:
2537           break;
2538         }
2539 
2540       return OP_SAME (0);
2541 
2542 
2543     case tcc_comparison:
2544     case tcc_binary:
2545       if (OP_SAME (0) && OP_SAME (1))
2546         return 1;
2547 
2548       /* For commutative ops, allow the other order.  */
2549       return (commutative_tree_code (TREE_CODE (arg0))
2550               && operand_equal_p (TREE_OPERAND (arg0, 0),
2551                                   TREE_OPERAND (arg1, 1), flags)
2552               && operand_equal_p (TREE_OPERAND (arg0, 1),
2553                                   TREE_OPERAND (arg1, 0), flags));
2554 
2555     case tcc_reference:
2556       /* If either of the pointer (or reference) expressions we are
2557          dereferencing contain a side effect, these cannot be equal.  */
2558       if (TREE_SIDE_EFFECTS (arg0)
2559           || TREE_SIDE_EFFECTS (arg1))
2560         return 0;
2561 
2562       switch (TREE_CODE (arg0))
2563         {
2564         case INDIRECT_REF:
2565         case ALIGN_INDIRECT_REF:
2566         case MISALIGNED_INDIRECT_REF:
2567         case REALPART_EXPR:
2568         case IMAGPART_EXPR:
2569           return OP_SAME (0);
2570 
2571         case ARRAY_REF:
2572         case ARRAY_RANGE_REF:
2573           /* Operands 2 and 3 may be null.  */
2574           return (OP_SAME (0)
2575                   && OP_SAME (1)
2576                   && OP_SAME_WITH_NULL (2)
2577                   && OP_SAME_WITH_NULL (3));
2578 
2579         case COMPONENT_REF:
2580           /* Handle operand 2 the same as for ARRAY_REF.  Operand 0
2581              may be NULL when we're called to compare MEM_EXPRs.  */
2582           return OP_SAME_WITH_NULL (0)
2583                  && OP_SAME (1)
2584                  && OP_SAME_WITH_NULL (2);
2585 
2586         case BIT_FIELD_REF:
2587           return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2588 
2589         default:
2590           return 0;
2591         }
2592 
2593     case tcc_expression:
2594       switch (TREE_CODE (arg0))
2595         {
2596         case ADDR_EXPR:
2597         case TRUTH_NOT_EXPR:
2598           return OP_SAME (0);
2599 
2600         case TRUTH_ANDIF_EXPR:
2601         case TRUTH_ORIF_EXPR:
2602           return OP_SAME (0) && OP_SAME (1);
2603 
2604         case TRUTH_AND_EXPR:
2605         case TRUTH_OR_EXPR:
2606         case TRUTH_XOR_EXPR:
2607           if (OP_SAME (0) && OP_SAME (1))
2608             return 1;
2609 
2610           /* Otherwise take into account this is a commutative operation.  */
2611           return (operand_equal_p (TREE_OPERAND (arg0, 0),
2612                                    TREE_OPERAND (arg1, 1), flags)
2613                   && operand_equal_p (TREE_OPERAND (arg0, 1),
2614                                       TREE_OPERAND (arg1, 0), flags));
2615 
2616         case CALL_EXPR:
2617           /* If the CALL_EXPRs call different functions, then they
2618              clearly can not be equal.  */
2619           if (!OP_SAME (0))
2620             return 0;
2621 
2622           {
2623             unsigned int cef = call_expr_flags (arg0);
2624             if (flags & OEP_PURE_SAME)
2625               cef &= ECF_CONST | ECF_PURE;
2626             else
2627               cef &= ECF_CONST;
2628             if (!cef)
2629               return 0;
2630           }
2631 
2632           /* Now see if all the arguments are the same.  operand_equal_p
2633              does not handle TREE_LIST, so we walk the operands here
2634              feeding them to operand_equal_p.  */
2635           arg0 = TREE_OPERAND (arg0, 1);
2636           arg1 = TREE_OPERAND (arg1, 1);
2637           while (arg0 && arg1)
2638             {
2639               if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2640                                      flags))
2641                 return 0;
2642 
2643               arg0 = TREE_CHAIN (arg0);
2644               arg1 = TREE_CHAIN (arg1);
2645             }
2646 
2647           /* If we get here and both argument lists are exhausted
2648              then the CALL_EXPRs are equal.  */
2649           return ! (arg0 || arg1);
2650 
2651         default:
2652           return 0;
2653         }
2654 
2655     case tcc_declaration:
2656       /* Consider __builtin_sqrt equal to sqrt.  */
2657       return (TREE_CODE (arg0) == FUNCTION_DECL
2658               && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2659               && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2660               && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2661 
2662     default:
2663       return 0;
2664     }
2665 
2666 #undef OP_SAME
2667 #undef OP_SAME_WITH_NULL
2668 }



リンク元

Advertisement