new file mode 100644
@@ -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
+<http://www.gnu.org/licenses/>. */
+
+#include <gtest/gtest.h>
+
+#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