From acaeabca4571be4a52bb26348210b8cb244916ed Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 10 Jun 2015 06:06:31 -0400 Subject: [PATCH 06/16] Add test-folding.c to unittests gcc/testsuite/ChangeLog: * unittests/test-folding.c: New file. --- gcc/testsuite/unittests/test-folding.c | 120 +++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 gcc/testsuite/unittests/test-folding.c diff --git a/gcc/testsuite/unittests/test-folding.c b/gcc/testsuite/unittests/test-folding.c new file mode 100644 index 0000000..6623c55 --- /dev/null +++ b/gcc/testsuite/unittests/test-folding.c @@ -0,0 +1,120 @@ +/* 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 "config.h" +#include "gtest/gtest.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 -- 1.8.5.3