From patchwork Wed Jun 10 15:24:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 482732 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 820F41401AD for ; Thu, 11 Jun 2015 01:12:24 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=qYWpImkB; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=TkYhb04HoUqPcGbrn4sAMmC7S9Ga7hBQ+7Mzx11Jl31yiS+PJeQrN YR2PF89T4Y5p+m1H/z+tniJRj6eAMbr/grZzNXy/5ONeOwtby/Z25s4VydsyFBAc xspdonNXT44LRXkgWDrVTPYLT3yaqX2lUGThBaQShPxFnV23z1Z3x4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=RH8puOZPR7WPLST0clTYL0YlXgY=; b=qYWpImkBtJ7J65/XR9Np 1P6gF+qJNCuajcwIlzTSy9HbYwwSqjjlwjRqrG81rK7QN7zwotO7bZLHh9xt1tKk b4zHV5dQ9mRe0OaepfQ9VIrRQG0QiDmSf7L53yB97W7+jn1B5U7mmfR4BBDsdFNz phYpzmcpgNVBHMqKipnWm/A= Received: (qmail 125362 invoked by alias); 10 Jun 2015 15:10:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 125288 invoked by uid 89); 10 Jun 2015 15:10:38 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.0 required=5.0 tests=AWL, BAYES_50, KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2 X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (208.118.235.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 10 Jun 2015 15:10:33 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z2he6-0000BS-JS for gcc-patches@gcc.gnu.org; Wed, 10 Jun 2015 11:10:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55471) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z2he6-0000Ay-Co for gcc-patches@gcc.gnu.org; Wed, 10 Jun 2015 11:10:26 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 12EF436B37B for ; Wed, 10 Jun 2015 15:10:26 +0000 (UTC) Received: from c64.redhat.com (vpn-230-17.phx2.redhat.com [10.3.230.17]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5AFAMLo007589; Wed, 10 Jun 2015 11:10:25 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 04/17] Add test-folding.c to gcc/unittests Date: Wed, 10 Jun 2015 11:24:45 -0400 Message-Id: <1433949898-22033-5-git-send-email-dmalcolm@redhat.com> In-Reply-To: <1433949898-22033-1-git-send-email-dmalcolm@redhat.com> References: <1433949898-22033-1-git-send-email-dmalcolm@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-IsSubscribed: yes gcc/unittests/ChangeLog: * test-folding.c: New file. --- gcc/unittests/test-folding.c | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 gcc/unittests/test-folding.c diff --git a/gcc/unittests/test-folding.c b/gcc/unittests/test-folding.c new file mode 100644 index 0000000..277acea --- /dev/null +++ b/gcc/unittests/test-folding.c @@ -0,0 +1,121 @@ +/* Unit tests for GCC's expression folding. + Copyright (C) 2015 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "opts.h" +#include "signop.h" +#include "hash-set.h" +#include "fixed-value.h" +#include "alias.h" +#include "flags.h" +#include "symtab.h" +#include "tree-core.h" +#include "stor-layout.h" +#include "tree.h" +#include "stringpool.h" +#include "stor-layout.h" +#include "rtl.h" +#include "predict.h" +#include "vec.h" +#include "hashtab.h" +#include "hash-set.h" +#include "machmode.h" +#include "hard-reg-set.h" +#include "input.h" +#include "function.h" +#include "dominance.h" +#include "cfg.h" +#include "cfganal.h" +#include "basic-block.h" +#include "tree-ssa-alias.h" +#include "internal-fn.h" +#include "gimple-fold.h" +#include "gimple-expr.h" +#include "toplev.h" +#include "print-tree.h" +#include "tree-iterator.h" +#include "gimplify.h" +#include "tree-cfg.h" +#include "fold-const.h" + +namespace { + +/* A test fixture for writing tests of folding trees. */ +class tree_folding_test : public ::testing::Test +{ + protected: + void + assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs, + tree constant) + { + EXPECT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs)); + } + + void + assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs, + tree wrapped_expr) + { + tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs); + EXPECT_NE (wrapped_expr, result); + EXPECT_EQ (NON_LVALUE_EXPR, TREE_CODE (result)); + EXPECT_EQ (wrapped_expr, TREE_OPERAND (result, 0)); + } +}; + +TEST_F (tree_folding_test, arithmetic_folding) +{ + tree type = integer_type_node; + tree x = create_tmp_var_raw (type, "x"); + tree zero = build_zero_cst (type); + tree one = build_int_cst (type, 1); + + /* Addition. */ + /* 1 <-- (0 + 1) */ + assert_binop_folds_to_const (zero, PLUS_EXPR, one, + one); + assert_binop_folds_to_const (one, PLUS_EXPR, zero, + one); + + /* (nonlvalue)x <-- (x + 0) */ + assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero, + x); + + /* Subtraction. */ + /* 0 <-- (x - x) */ + assert_binop_folds_to_const (x, MINUS_EXPR, x, + zero); + assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero, + x); + + /* Multiplication. */ + /* 0 <-- (x * 0) */ + assert_binop_folds_to_const (x, MULT_EXPR, zero, + zero); + + /* (nonlvalue)x <-- (x * 1) */ + assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one, + x); +} + +} // anon namespace