1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @modules jdk.incubator.code
 27  * @run testng TestUsesDependsOn
 28  */
 29 
 30 import org.testng.Assert;
 31 import org.testng.annotations.Test;
 32 
 33 import jdk.incubator.code.*;
 34 import jdk.incubator.code.extern.OpParser;
 35 
 36 import java.util.HashMap;
 37 import java.util.List;
 38 import java.util.Map;
 39 import java.util.Set;
 40 import java.util.concurrent.atomic.AtomicInteger;
 41 import java.util.function.BiConsumer;
 42 import java.util.function.Function;
 43 
 44 public class TestUsesDependsOn {
 45 
 46     static final String OP = """
 47             func @"f" (%0 : java.type:"int", %1 : java.type:"int")java.type:"int" -> {
 48                 %2 : java.type:"int" = add %0 %1;
 49                 %3 : java.type:"boolean" = lt %0 %1;
 50                 %4 : java.type:"void" = cbranch %3 ^b1(%2, %2) ^b2(%0, %1);
 51 
 52               ^b1(%5 : java.type:"int", %6 : java.type:"int"):
 53                 %7 : java.type:"void" = return %5;
 54 
 55               ^b2(%8 : java.type:"int", %9 : java.type:"int"):
 56                 %10 : java.type:"void" = return %8;
 57             };
 58             """;
 59 
 60     @Test
 61     public void testDependsOn() {
 62         Op f = OpParser.fromStringOfJavaCodeModel(OP);
 63 
 64         Map<String, List<String>> dependsUpon = computeValueMap(f, Value::dependsOn);
 65 
 66         var expected = Map.ofEntries(
 67                 Map.entry("0", List.of()),
 68                 Map.entry("1", List.of()),
 69                 Map.entry("2", List.of("0", "1")),
 70                 Map.entry("3", List.of("0", "1")),
 71                 Map.entry("4", List.of("3", "2", "0", "1")),
 72                 Map.entry("5", List.of()),
 73                 Map.entry("6", List.of()),
 74                 Map.entry("7", List.of("5")),
 75                 Map.entry("8", List.of()),
 76                 Map.entry("9", List.of()),
 77                 Map.entry("10", List.of("8"))
 78         );
 79 
 80         Assert.assertEquals(dependsUpon, expected);
 81     }
 82 
 83 
 84     @Test
 85     public void testUses() {
 86         Op f = OpParser.fromStringOfJavaCodeModel(OP);
 87         System.out.println(f.toText());
 88 
 89         Map<String, List<String>> uses = computeValueMap(f, Value::uses);
 90 
 91         var expected = Map.ofEntries(
 92                 Map.entry("0", List.of("2", "3", "4")),
 93                 Map.entry("1", List.of("2", "3", "4")),
 94                 Map.entry("2", List.of("4")),
 95                 Map.entry("3", List.of("4")),
 96                 Map.entry("4", List.of()),
 97                 Map.entry("5", List.of("7")),
 98                 Map.entry("6", List.of()),
 99                 Map.entry("7", List.of()),
100                 Map.entry("8", List.of("10")),
101                 Map.entry("9", List.of()),
102                 Map.entry("10", List.of())
103         );
104         System.out.println(uses.toString());
105         System.out.println(expected);
106 
107         Assert.assertEquals(uses, expected);
108     }
109 
110     static Map<String, List<String>> computeValueMap(Op op, Function<Value, Set<? extends Value>> f) {
111         AtomicInteger ai = new AtomicInteger();
112 
113         Map<Value, String> valueNameMap = computeValues(op, new HashMap<>(), (v, m) -> {
114             String name = Integer.toString(ai.getAndIncrement());
115             m.put(v, name);
116         });
117 
118         return computeValues(op, new HashMap<>(), (v, m) -> {
119             m.put(valueNameMap.get(v), f.apply(v).stream().map(valueNameMap::get).toList());
120         });
121     }
122 
123     static <T> T computeValues(Op op, T t, BiConsumer<Value, T> c) {
124         return op.traverse(t, (m, codeElement) -> {
125             return switch (codeElement) {
126                 case Block b -> {
127                     for (var a : b.parameters()) {
128                         c.accept(a, m);
129                     }
130 
131                     yield m;
132                 }
133                 case Op o -> {
134                     if (o.result() != null) {
135                         c.accept(o.result(), m);
136                     }
137 
138                     yield m;
139                 }
140                 default -> m;
141             };
142         });
143     }
144 }