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