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.parser.OpParser;
 35 import java.util.HashMap;
 36 import java.util.List;
 37 import java.util.Map;
 38 import java.util.Set;
 39 import java.util.concurrent.atomic.AtomicInteger;
 40 import java.util.function.BiConsumer;
 41 import java.util.function.Function;
 42 
 43 public class TestUsesDependsOn {
 44 
 45     static final String OP = """
 46             func @"f" (%0 : int, %1 : int)int -> {
 47                 %2 : int = add %0 %1;
 48                 %3 : boolean = lt %0 %1;
 49                 %4 : void = cbranch %3 ^b1(%2, %2) ^b2(%0, %1);
 50 
 51               ^b1(%5 : int, %6 : int):
 52                 %7 : void = return %5;
 53 
 54               ^b2(%8 : int, %9 : int):
 55                 %10 : void = return %8;
 56             };
 57             """;
 58 
 59     @Test
 60     public void testDependsOn() {
 61         Op f = OpParser.fromStringOfFuncOp(OP);
 62 
 63         Map<String, List<String>> dependsUpon = computeValueMap(f, Value::dependsOn);
 64 
 65         var expected = Map.ofEntries(
 66                 Map.entry("0", List.of()),
 67                 Map.entry("1", List.of()),
 68                 Map.entry("2", List.of("0", "1")),
 69                 Map.entry("3", List.of("0", "1")),
 70                 Map.entry("4", List.of("3", "2", "0", "1")),
 71                 Map.entry("5", List.of()),
 72                 Map.entry("6", List.of()),
 73                 Map.entry("7", List.of("5")),
 74                 Map.entry("8", List.of()),
 75                 Map.entry("9", List.of()),
 76                 Map.entry("10", List.of("8"))
 77         );
 78 
 79         Assert.assertEquals(dependsUpon, expected);
 80     }
 81 
 82 
 83     @Test
 84     public void testUses() {
 85         Op f = OpParser.fromStringOfFuncOp(OP);
 86         f.writeTo(System.out);
 87 
 88         Map<String, List<String>> uses = computeValueMap(f, Value::uses);
 89 
 90         var expected = Map.ofEntries(
 91                 Map.entry("0", List.of("2", "3", "4")),
 92                 Map.entry("1", List.of("2", "3", "4")),
 93                 Map.entry("2", List.of("4")),
 94                 Map.entry("3", List.of("4")),
 95                 Map.entry("4", List.of()),
 96                 Map.entry("5", List.of("7")),
 97                 Map.entry("6", List.of()),
 98                 Map.entry("7", List.of()),
 99                 Map.entry("8", List.of("10")),
100                 Map.entry("9", List.of()),
101                 Map.entry("10", List.of())
102         );
103         System.out.println(uses.toString());
104         System.out.println(expected);
105 
106         Assert.assertEquals(uses, expected);
107     }
108 
109     static Map<String, List<String>> computeValueMap(Op op, Function<Value, Set<? extends Value>> f) {
110         AtomicInteger ai = new AtomicInteger();
111 
112         Map<Value, String> valueNameMap = computeValues(op, new HashMap<>(), (v, m) -> {
113             String name = Integer.toString(ai.getAndIncrement());
114             m.put(v, name);
115         });
116 
117         return computeValues(op, new HashMap<>(), (v, m) -> {
118             m.put(valueNameMap.get(v), f.apply(v).stream().map(valueNameMap::get).toList());
119         });
120     }
121 
122     static <T> T computeValues(Op op, T t, BiConsumer<Value, T> c) {
123         return op.traverse(t, (m, codeElement) -> {
124             return switch (codeElement) {
125                 case Block b -> {
126                     for (var a : b.parameters()) {
127                         c.accept(a, m);
128                     }
129 
130                     yield m;
131                 }
132                 case Op o -> {
133                     if (o.result() != null) {
134                         c.accept(o.result(), m);
135                     }
136 
137                     yield m;
138                 }
139                 default -> m;
140             };
141         });
142     }
143 }