1 /*
2  * Copyright 2015, 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 #ifndef BCC_RS_UTILS_H
18 #define BCC_RS_UTILS_H
19 
20 #include "rsDefines.h"
21 
22 #include <llvm/IR/Type.h>
23 #include <llvm/IR/DerivedTypes.h>
24 
25 namespace {
26 
getUnsuffixedStructName(const llvm::StructType * T)27 static inline llvm::StringRef getUnsuffixedStructName(const llvm::StructType *T) {
28   // Get just the object type name with no suffix.
29   size_t LastDot = T->getName().rfind('.');
30   if (LastDot == strlen("struct")) {
31     // If we get back to just the "struct" part, we know that we had a
32     // raw typename (i.e. struct.rs_element with no ".[0-9]+" suffix on it.
33     // In that case, we will want to create our slice such that it contains
34     // the entire name.
35     LastDot = T->getName().size();
36   }
37   return T->getStructName().slice(0, LastDot);
38 }
39 
40 const char kAllocationTypeName[] = "struct.rs_allocation";
41 const char kElementTypeName[]    = "struct.rs_element";
42 const char kSamplerTypeName[]    = "struct.rs_sampler";
43 const char kScriptTypeName[]     = "struct.rs_script";
44 const char kTypeTypeName[]       = "struct.rs_type";
45 
46 // Returns the RsDataType for a given input LLVM type.
47 // This is only used to distinguish the associated RS object types (i.e.
48 // rs_allocation, rs_element, rs_sampler, rs_script, and rs_type).
49 // All other types are reported back as RS_TYPE_NONE, since no special
50 // handling would be necessary.
getRsDataTypeForType(const llvm::Type * T)51 static inline enum RsDataType getRsDataTypeForType(const llvm::Type *T) {
52   if (T->isStructTy()) {
53     const llvm::StringRef StructName = getUnsuffixedStructName(llvm::dyn_cast<const llvm::StructType>(T));
54     if (StructName.equals(kAllocationTypeName)) {
55       return RS_TYPE_ALLOCATION;
56     } else if (StructName.equals(kElementTypeName)) {
57       return RS_TYPE_ELEMENT;
58     } else if (StructName.equals(kSamplerTypeName)) {
59       return RS_TYPE_SAMPLER;
60     } else if (StructName.equals(kScriptTypeName)) {
61       return RS_TYPE_SCRIPT;
62     } else if (StructName.equals(kTypeTypeName)) {
63       return RS_TYPE_TYPE;
64     }
65   }
66   return RS_TYPE_NONE;
67 }
68 
69 // Returns true if the input type is one of our RenderScript object types
70 // (allocation, element, sampler, script, type) and false if it is not.
isRsObjectType(const llvm::Type * T)71 static inline bool isRsObjectType(const llvm::Type *T) {
72   return getRsDataTypeForType(T) != RS_TYPE_NONE;
73 }
74 
75 }  // end namespace
76 
77 #endif // BCC_RS_UTILS_H
78