/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.compiler.test;

import org.junit.*;

import com.oracle.graal.api.code.*;
import com.oracle.graal.api.meta.*;
import com.oracle.graal.debug.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.calc.*;
import com.oracle.graal.nodes.extended.*;
import com.oracle.graal.nodes.spi.Lowerable.LoweringType;
import com.oracle.graal.nodes.type.*;
import com.oracle.graal.phases.common.*;
import com.oracle.graal.phases.tiers.*;

public class PushNodesThroughPiTest extends GraalCompilerTest {

    public static class A {

        public long x = 20;
    }

    public static class B extends A {

        public long y = 10;
    }

    public static long test1Snippet(A a) {
        B b = (B) a;
        long ret = b.x; // this can be moved before the checkcast
        ret += b.y;
        // the null-check should be canonicalized with the null-check of the checkcast
        ret += b != null ? 100 : 200;
        return ret;
    }

    @Test
    public void test1() {
        final String snippet = "test1Snippet";
        Debug.scope("PushThroughPi", new DebugDumpScope(snippet), new Runnable() {

            public void run() {
                StructuredGraph graph = compileTestSnippet(snippet);

                int counter = 0;
                for (ReadNode rn : graph.getNodes().filter(ReadNode.class)) {
                    if (rn.location() instanceof ConstantLocationNode && rn.object().stamp() instanceof ObjectStamp) {
                        long disp = ((ConstantLocationNode) rn.location()).getDisplacement();
                        ResolvedJavaType receiverType = ObjectStamp.typeOrNull(rn.object());
                        ResolvedJavaField field = receiverType.findInstanceFieldWithOffset(disp);

                        if (field != null) {
                            if (field.getName().equals("x")) {
                                Assert.assertTrue(rn.object() instanceof LocalNode);
                            } else {
                                Assert.assertTrue(rn.object().toString(), rn.object() instanceof PiNode);
                            }
                            counter++;
                        }
                    }
                }
                Assert.assertEquals(2, counter);

                Assert.assertTrue(graph.getNodes().filter(IsNullNode.class).count() == 1);
            }
        });
    }

    private StructuredGraph compileTestSnippet(final String snippet) {
        StructuredGraph graph = parse(snippet);
        PhaseContext context = new PhaseContext(runtime(), new Assumptions(false), replacements);
        CanonicalizerPhase canonicalizer = new CanonicalizerPhase(true);
        new LoweringPhase(LoweringType.BEFORE_GUARDS).apply(graph, context);
        canonicalizer.apply(graph, context);
        new PushThroughPiPhase().apply(graph);
        canonicalizer.apply(graph, context);

        return graph;
    }
}

@@ -1697,32 +1697,105 @@
  y = 10;%0A    %7D%0A%0A
+    public static class C extends B %7B%0A%0A        public long z = 5;%0A    %7D%0A%0A
     public stati
@@ -1833,16 +1833,16 @@
     
-B b
+C c
  = (
-B
+C
 ) a;
@@ -1861,17 +1861,17 @@
 g ret = 
-b
+c
 .x; // t
@@ -1885,11 +1885,12 @@
  be 
-mov
+push
 ed b
@@ -1928,12 +1928,78 @@
  += 
-b
+c
 .y;
+ // not allowed to push%0A        ret += c.z; // not allowed to push
 %0A   
@@ -2097,17 +2097,17 @@
  ret += 
-b
+c
  != null
@@ -2420,41 +2420,8 @@
 );%0A%0A
-                int counter = 0;%0A
     
@@ -2919,28 +2919,31 @@
             
-if (
+assert 
 field != nul
@@ -2943,24 +2943,97 @@
  != null
-) %7B%0A    
+ : %22Node %22 + rn + %22 tries to access a field which doesn't exists for this type%22;%0A
         
@@ -3103,36 +3103,32 @@
                 
-    
 Assert.assertTru
@@ -3160,28 +3160,24 @@
 LocalNode);%0A
-    
             
@@ -3217,36 +3217,32 @@
                 
-    
 Assert.assertTru
@@ -3327,167 +3327,49 @@
     
-    %7D%0A                            counter++;%0A                        %7D%0A                    %7D%0A                %7D%0A                Assert.assertEquals(2, counter);
+%7D%0A                    %7D%0A                %7D
 %0A%0A  

extend test for ReadNodes