diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 86426e734ccf669126649c09fc91ac7e361b79b1..61ea5212119f5692da4eded339414e6951ee2f61 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2013-02-27  Andrey Belevantsev  <abel@ispras.ru>
+
+	PR middle-end/45472
+	
+	* sel-sched-ir.c (merge_expr): Also change vinsn of merged expr
+	when the may_trap_p bit of the exprs being merged differs.
+
+	Reorder tests for speculativeness in the logical and operator.
+
 2013-02-27  Jakub Jelinek  <jakub@redhat.com>
 
 	* incpath.c (add_standard_paths): Use reconcat instead of concat
diff --git a/gcc/sel-sched-ir.c b/gcc/sel-sched-ir.c
index 39dc52f66d91a65dc4896228b8228ee2fe5bd1cf..ae17351b5956b03e23aa2be384ca7e1af6a545cf 100644
--- a/gcc/sel-sched-ir.c
+++ b/gcc/sel-sched-ir.c
@@ -1866,8 +1866,12 @@ merge_expr (expr_t to, expr_t from, insn_t split_point)
   /* Make sure that speculative pattern is propagated into exprs that
      have non-speculative one.  This will provide us with consistent
      speculative bits and speculative patterns inside expr.  */
-  if (EXPR_SPEC_DONE_DS (to) == 0
-      && EXPR_SPEC_DONE_DS (from) != 0)
+  if ((EXPR_SPEC_DONE_DS (from) != 0
+       && EXPR_SPEC_DONE_DS (to) == 0)
+      /* Do likewise for volatile insns, so that we always retain
+	 the may_trap_p bit on the resulting expression.  */
+      || (VINSN_MAY_TRAP_P (EXPR_VINSN (from))
+	  && !VINSN_MAY_TRAP_P (EXPR_VINSN (to))))
     change_vinsn_in_expr (to, EXPR_VINSN (from));
 
   merge_expr_data (to, from, split_point);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 9f00b8708c29b17217e12d5638f1a56313870757..20e9673121fb75812141312b5d6a1f9b77cf1229 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2013-02-27  Andrey Belevantsev  <abel@ispras.ru>
+
+	PR middle-end/45472
+	* gcc.dg/pr45472.c: New test.
+
 2013-02-26  Marek Polacek  <polacek@redhat.com>
 
 	PR tree-optimization/56426
diff --git a/gcc/testsuite/gcc.dg/pr45472.c b/gcc/testsuite/gcc.dg/pr45472.c
new file mode 100644
index 0000000000000000000000000000000000000000..573e83d700c02ab2a346f88f94beab2b06d14100
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr45472.c
@@ -0,0 +1,21 @@
+/* { dg-do compile { target powerpc*-*-* ia64-*-* x86_64-*-* } } */
+/* { dg-options "-O -fschedule-insns2 -fselective-scheduling2" } */
+
+struct S
+{
+  volatile long vl;
+  int i;
+};
+struct S s1, s2;
+
+void
+foo (int j, int c)
+{
+  int i;
+  for (i = 0; i <= j; i++)
+    {
+      if (c)
+	s2.vl += s1.vl;
+      s1 = s2;
+    }
+}