1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "common_test.h"
18#include "sea_ir/types/type_inference_visitor.h"
19#include "sea_ir/ir/sea.h"
20
21namespace sea_ir {
22
23class TestInstructionNode:public InstructionNode {
24 public:
25  explicit TestInstructionNode(std::vector<InstructionNode*> prods): InstructionNode(NULL),
26      producers_(prods) { }
27  std::vector<InstructionNode*> GetSSAProducers() {
28    return producers_;
29  }
30 protected:
31  std::vector<InstructionNode*> producers_;
32};
33
34class TypeInferenceVisitorTest : public art::CommonTest {
35};
36
37TEST_F(TypeInferenceVisitorTest, MergeIntWithByte) {
38  TypeData td;
39  art::verifier::RegTypeCache type_cache(false);
40  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
41  const Type* int_type = &type_cache.Integer();
42  const Type* byte_type = &type_cache.Byte();
43  const Type* ib_type = tiv.MergeTypes(int_type, byte_type);
44  const Type* bi_type = tiv.MergeTypes(byte_type, int_type);
45  EXPECT_TRUE(ib_type == int_type);
46  EXPECT_TRUE(bi_type == int_type);
47}
48
49TEST_F(TypeInferenceVisitorTest, MergeIntWithShort) {
50  TypeData td;
51  art::verifier::RegTypeCache type_cache(false);
52  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
53  const Type* int_type = &type_cache.Integer();
54  const Type* short_type = &type_cache.Short();
55  const Type* is_type = tiv.MergeTypes(int_type, short_type);
56  const Type* si_type = tiv.MergeTypes(short_type, int_type);
57  EXPECT_TRUE(is_type == int_type);
58  EXPECT_TRUE(si_type == int_type);
59}
60
61TEST_F(TypeInferenceVisitorTest, MergeMultipleInts) {
62  int N = 10;  // Number of types to merge.
63  TypeData td;
64  art::verifier::RegTypeCache type_cache(false);
65  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
66  std::vector<const Type*> types;
67  for (int i = 0; i < N; i++) {
68    const Type* new_type = &type_cache.Integer();
69    types.push_back(new_type);
70  }
71  const Type* merged_type = tiv.MergeTypes(types);
72  EXPECT_TRUE(merged_type == &type_cache.Integer());
73}
74
75TEST_F(TypeInferenceVisitorTest, MergeMultipleShorts) {
76  int N = 10;  // Number of types to merge.
77  TypeData td;
78  art::verifier::RegTypeCache type_cache(false);
79  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
80  std::vector<const Type*> types;
81  for (int i = 0; i < N; i++) {
82    const Type* new_type = &type_cache.Short();
83    types.push_back(new_type);
84  }
85  const Type* merged_type = tiv.MergeTypes(types);
86  EXPECT_TRUE(merged_type == &type_cache.Short());
87}
88
89TEST_F(TypeInferenceVisitorTest, MergeMultipleIntsWithShorts) {
90  int N = 10;  // Number of types to merge.
91  TypeData td;
92  art::verifier::RegTypeCache type_cache(false);
93  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
94  std::vector<const Type*> types;
95  for (int i = 0; i < N; i++) {
96    const Type* short_type = &type_cache.Short();
97    const Type* int_type = &type_cache.Integer();
98    types.push_back(short_type);
99    types.push_back(int_type);
100  }
101  const Type* merged_type = tiv.MergeTypes(types);
102  EXPECT_TRUE(merged_type == &type_cache.Integer());
103}
104
105TEST_F(TypeInferenceVisitorTest, GetOperandTypes) {
106  int N = 10;  // Number of types to merge.
107  TypeData td;
108  art::verifier::RegTypeCache type_cache(false);
109  TypeInferenceVisitor tiv(NULL, &td, &type_cache);
110  std::vector<const Type*> types;
111  std::vector<InstructionNode*> preds;
112  for (int i = 0; i < N; i++) {
113    const Type* short_type = &type_cache.Short();
114    const Type* int_type = &type_cache.Integer();
115    TestInstructionNode* short_inst =
116        new TestInstructionNode(std::vector<InstructionNode*>());
117    TestInstructionNode* int_inst =
118        new TestInstructionNode(std::vector<InstructionNode*>());
119    preds.push_back(short_inst);
120    preds.push_back(int_inst);
121    td.SetTypeOf(short_inst->Id(), short_type);
122    td.SetTypeOf(int_inst->Id(), int_type);
123    types.push_back(short_type);
124    types.push_back(int_type);
125  }
126  TestInstructionNode* inst_to_test = new TestInstructionNode(preds);
127  std::vector<const Type*> result = tiv.GetOperandTypes(inst_to_test);
128  EXPECT_TRUE(result.size() == types.size());
129  EXPECT_TRUE(true == std::equal(types.begin(), types.begin() + 2, result.begin()));
130}
131
132
133}  // namespace sea_ir
134