Home | History | Annotate | Download | only in types
      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_compiler_test.h"
     18 #include "sea_ir/types/type_inference_visitor.h"
     19 #include "sea_ir/ir/sea.h"
     20 
     21 namespace sea_ir {
     22 
     23 class 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 
     34 class TypeInferenceVisitorTest : public art::CommonCompilerTest {};
     35 
     36 TEST_F(TypeInferenceVisitorTest, MergeIntWithByte) {
     37   TypeData td;
     38   art::verifier::RegTypeCache type_cache(false);
     39   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
     40   const Type* int_type = &type_cache.Integer();
     41   const Type* byte_type = &type_cache.Byte();
     42   const Type* ib_type = tiv.MergeTypes(int_type, byte_type);
     43   const Type* bi_type = tiv.MergeTypes(byte_type, int_type);
     44   EXPECT_TRUE(ib_type == int_type);
     45   EXPECT_TRUE(bi_type == int_type);
     46 }
     47 
     48 TEST_F(TypeInferenceVisitorTest, MergeIntWithShort) {
     49   TypeData td;
     50   art::verifier::RegTypeCache type_cache(false);
     51   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
     52   const Type* int_type = &type_cache.Integer();
     53   const Type* short_type = &type_cache.Short();
     54   const Type* is_type = tiv.MergeTypes(int_type, short_type);
     55   const Type* si_type = tiv.MergeTypes(short_type, int_type);
     56   EXPECT_TRUE(is_type == int_type);
     57   EXPECT_TRUE(si_type == int_type);
     58 }
     59 
     60 TEST_F(TypeInferenceVisitorTest, MergeMultipleInts) {
     61   int N = 10;  // Number of types to merge.
     62   TypeData td;
     63   art::verifier::RegTypeCache type_cache(false);
     64   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
     65   std::vector<const Type*> types;
     66   for (int i = 0; i < N; i++) {
     67     const Type* new_type = &type_cache.Integer();
     68     types.push_back(new_type);
     69   }
     70   const Type* merged_type = tiv.MergeTypes(types);
     71   EXPECT_TRUE(merged_type == &type_cache.Integer());
     72 }
     73 
     74 TEST_F(TypeInferenceVisitorTest, MergeMultipleShorts) {
     75   int N = 10;  // Number of types to merge.
     76   TypeData td;
     77   art::verifier::RegTypeCache type_cache(false);
     78   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
     79   std::vector<const Type*> types;
     80   for (int i = 0; i < N; i++) {
     81     const Type* new_type = &type_cache.Short();
     82     types.push_back(new_type);
     83   }
     84   const Type* merged_type = tiv.MergeTypes(types);
     85   EXPECT_TRUE(merged_type == &type_cache.Short());
     86 }
     87 
     88 TEST_F(TypeInferenceVisitorTest, MergeMultipleIntsWithShorts) {
     89   int N = 10;  // Number of types to merge.
     90   TypeData td;
     91   art::verifier::RegTypeCache type_cache(false);
     92   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
     93   std::vector<const Type*> types;
     94   for (int i = 0; i < N; i++) {
     95     const Type* short_type = &type_cache.Short();
     96     const Type* int_type = &type_cache.Integer();
     97     types.push_back(short_type);
     98     types.push_back(int_type);
     99   }
    100   const Type* merged_type = tiv.MergeTypes(types);
    101   EXPECT_TRUE(merged_type == &type_cache.Integer());
    102 }
    103 
    104 TEST_F(TypeInferenceVisitorTest, GetOperandTypes) {
    105   int N = 10;  // Number of types to merge.
    106   TypeData td;
    107   art::verifier::RegTypeCache type_cache(false);
    108   TypeInferenceVisitor tiv(NULL, &td, &type_cache);
    109   std::vector<const Type*> types;
    110   std::vector<InstructionNode*> preds;
    111   for (int i = 0; i < N; i++) {
    112     const Type* short_type = &type_cache.Short();
    113     const Type* int_type = &type_cache.Integer();
    114     TestInstructionNode* short_inst =
    115         new TestInstructionNode(std::vector<InstructionNode*>());
    116     TestInstructionNode* int_inst =
    117         new TestInstructionNode(std::vector<InstructionNode*>());
    118     preds.push_back(short_inst);
    119     preds.push_back(int_inst);
    120     td.SetTypeOf(short_inst->Id(), short_type);
    121     td.SetTypeOf(int_inst->Id(), int_type);
    122     types.push_back(short_type);
    123     types.push_back(int_type);
    124   }
    125   TestInstructionNode* inst_to_test = new TestInstructionNode(preds);
    126   std::vector<const Type*> result = tiv.GetOperandTypes(inst_to_test);
    127   EXPECT_TRUE(result.size() == types.size());
    128   EXPECT_TRUE(true == std::equal(types.begin(), types.begin() + 2, result.begin()));
    129 }
    130 
    131 
    132 }  // namespace sea_ir
    133