blob: 94213f5b6b779924c8968f19fb9abc95610f9f0b [file] [log] [blame]
Tim Murrayb7bce742014-11-03 16:17:30 -08001/*
2 * Copyright 2014, 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 "bcc/Assert.h"
18#include "bcc/Renderscript/RSTransforms.h"
19
20#include <cstdlib>
21
22#include <llvm/IR/DataLayout.h>
23#include <llvm/IR/DerivedTypes.h>
24#include <llvm/IR/Function.h>
25#include <llvm/IR/Instructions.h>
26#include <llvm/IR/IRBuilder.h>
27#include <llvm/IR/MDBuilder.h>
28#include <llvm/IR/Module.h>
29#include <llvm/IR/Type.h>
30#include <llvm/Pass.h>
31#include <llvm/Support/raw_ostream.h>
32#include <llvm/Transforms/Utils/BasicBlockUtils.h>
33
34#include "bcc/Config/Config.h"
35#include "bcc/Support/Log.h"
36
37#include "bcinfo/MetadataExtractor.h"
38
39using namespace bcc;
40
41namespace {
42
43class RSInvokeHelperPass : public llvm::FunctionPass {
44private:
45 static char ID;
46
47 llvm::StructType* rsAllocationType;
48 llvm::StructType* rsElementType;
49 llvm::StructType* rsSamplerType;
50 llvm::StructType* rsScriptType;
51 llvm::StructType* rsTypeType;
52
53 llvm::Constant* rsAllocationSetObj;
54 llvm::Constant* rsElementSetObj;
55 llvm::Constant* rsSamplerSetObj;
56 llvm::Constant* rsScriptSetObj;
57 llvm::Constant* rsTypeSetObj;
58
59
60public:
61 RSInvokeHelperPass()
62 : FunctionPass(ID) {
63
64 }
65
Stephen Hinesc754d492015-01-08 16:00:50 -080066 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
67 // This pass does not use any other analysis passes, but it does
68 // modify the existing functions in the module (thus altering the CFG).
69 }
70
Tim Murrayb7bce742014-11-03 16:17:30 -080071 virtual bool doInitialization(llvm::Module &M) override {
72 llvm::FunctionType * SetObjType = nullptr;
73 llvm::SmallVector<llvm::Type*, 4> rsBaseObj;
74 rsBaseObj.append(4, llvm::Type::getInt64PtrTy(M.getContext()));
75
76 rsAllocationType = llvm::StructType::create(rsBaseObj, "struct.rs_allocation");
77 rsElementType = llvm::StructType::create(rsBaseObj, "struct.rs_element");
78 rsSamplerType = llvm::StructType::create(rsBaseObj, "struct.rs_sampler");
79 rsScriptType = llvm::StructType::create(rsBaseObj, "struct.rs_script");
80 rsTypeType = llvm::StructType::create(rsBaseObj, "struct.rs_type");
81
82 llvm::SmallVector<llvm::Value*, 1> SetObjParams;
83 llvm::SmallVector<llvm::Type*, 2> SetObjTypeParams;
84
85 // get rsSetObject(rs_allocation*, rs_allocation*)
86 // according to AArch64 calling convention, these are both pointers because of the size of the struct
87 SetObjTypeParams.push_back(rsAllocationType->getPointerTo());
88 SetObjTypeParams.push_back(rsAllocationType->getPointerTo());
89 SetObjType = llvm::FunctionType::get(llvm::Type::getVoidTy(M.getContext()), SetObjTypeParams, false);
90 rsAllocationSetObj = M.getOrInsertFunction("_Z11rsSetObjectP13rs_allocationS_", SetObjType);
91 SetObjTypeParams.clear();
92
93 SetObjTypeParams.push_back(rsElementType->getPointerTo());
94 SetObjTypeParams.push_back(rsElementType->getPointerTo());
95 SetObjType = llvm::FunctionType::get(llvm::Type::getVoidTy(M.getContext()), SetObjTypeParams, false);
96 rsElementSetObj = M.getOrInsertFunction("_Z11rsSetObjectP10rs_elementS_", SetObjType);
97 SetObjTypeParams.clear();
98
99 SetObjTypeParams.push_back(rsSamplerType->getPointerTo());
100 SetObjTypeParams.push_back(rsSamplerType->getPointerTo());
101 SetObjType = llvm::FunctionType::get(llvm::Type::getVoidTy(M.getContext()), SetObjTypeParams, false);
102 rsSamplerSetObj = M.getOrInsertFunction("_Z11rsSetObjectP10rs_samplerS_", SetObjType);
103 SetObjTypeParams.clear();
104
105 SetObjTypeParams.push_back(rsScriptType->getPointerTo());
106 SetObjTypeParams.push_back(rsScriptType->getPointerTo());
107 SetObjType = llvm::FunctionType::get(llvm::Type::getVoidTy(M.getContext()), SetObjTypeParams, false);
108 rsScriptSetObj = M.getOrInsertFunction("_Z11rsSetObjectP9rs_scriptS_", SetObjType);
109 SetObjTypeParams.clear();
110
111 SetObjTypeParams.push_back(rsTypeType->getPointerTo());
112 SetObjTypeParams.push_back(rsTypeType->getPointerTo());
113 SetObjType = llvm::FunctionType::get(llvm::Type::getVoidTy(M.getContext()), SetObjTypeParams, false);
114 rsTypeSetObj = M.getOrInsertFunction("_Z11rsSetObjectP7rs_typeS_", SetObjType);
115 SetObjTypeParams.clear();
116
117 return true;
118 }
119
120 bool insertSetObjectHelper(llvm::CallInst *Call, llvm::Value *V, llvm::StringRef StructName) {
121 llvm::Constant *SetObj = nullptr;
Tim Murray4100e8e2015-03-30 12:27:28 -0700122 llvm::StructType *RSStructType = nullptr;
Tim Murrayb7bce742014-11-03 16:17:30 -0800123 if (StructName.equals(rsAllocationType->getName())) {
124 SetObj = rsAllocationSetObj;
Tim Murray4100e8e2015-03-30 12:27:28 -0700125 RSStructType = rsAllocationType;
Tim Murrayb7bce742014-11-03 16:17:30 -0800126 } else if (StructName.equals(rsElementType->getName())) {
127 SetObj = rsElementSetObj;
Tim Murray4100e8e2015-03-30 12:27:28 -0700128 RSStructType = rsElementType;
Tim Murrayb7bce742014-11-03 16:17:30 -0800129 } else if (StructName.equals(rsSamplerType->getName())) {
130 SetObj = rsSamplerSetObj;
Tim Murray4100e8e2015-03-30 12:27:28 -0700131 RSStructType = rsSamplerType;
Tim Murrayb7bce742014-11-03 16:17:30 -0800132 } else if (StructName.equals(rsScriptType->getName())) {
133 SetObj = rsScriptSetObj;
Tim Murray4100e8e2015-03-30 12:27:28 -0700134 RSStructType = rsScriptType;
Tim Murrayb7bce742014-11-03 16:17:30 -0800135 } else if (StructName.equals(rsTypeType->getName())) {
136 SetObj = rsTypeSetObj;
Tim Murray4100e8e2015-03-30 12:27:28 -0700137 RSStructType = rsTypeType;
Tim Murrayb7bce742014-11-03 16:17:30 -0800138 } else {
139 return false; // this is for graphics types and matrices; do nothing
140 }
141
Tim Murray4100e8e2015-03-30 12:27:28 -0700142
143 llvm::CastInst* CastedValue = llvm::CastInst::CreatePointerCast(V, RSStructType->getPointerTo(), "", Call);
144
Tim Murrayb7bce742014-11-03 16:17:30 -0800145 llvm::SmallVector<llvm::Value*, 2> SetObjParams;
Tim Murray4100e8e2015-03-30 12:27:28 -0700146 SetObjParams.push_back(CastedValue);
147 SetObjParams.push_back(CastedValue);
Tim Murrayb7bce742014-11-03 16:17:30 -0800148
149 llvm::CallInst::Create(SetObj, SetObjParams, "", Call);
Tim Murray4100e8e2015-03-30 12:27:28 -0700150
151
Tim Murrayb7bce742014-11-03 16:17:30 -0800152 return true;
153 }
154
155
156 // this only modifies .helper functions that take certain RS base object types
157 virtual bool runOnFunction(llvm::Function &F) override {
158 if (!F.getName().startswith(".helper"))
159 return false;
160
161 bool changed = false;
162 const llvm::Function::ArgumentListType &argList(F.getArgumentList());
163 bool containsBaseObj = false;
164
165 // .helper methods should have one arg only, an anonymous struct
166 // that struct may contain BaseObjs
167 for (auto arg = argList.begin(); arg != argList.end(); arg++) {
168 llvm::Type *argType = arg->getType();
169 if (!argType->isPointerTy() || !argType->getPointerElementType()->isStructTy())
170 continue;
171
172 llvm::StructType *argStructType = llvm::dyn_cast<llvm::StructType>(argType->getPointerElementType());
173
174 for (unsigned int i = 0; i < argStructType->getNumElements(); i++) {
175 llvm::Type *currentType = argStructType->getElementType(i);
176 if (currentType->isStructTy() && currentType->getStructName().startswith("struct.rs_")) {
177 containsBaseObj = true;
178 }
179 }
180 break;
181 }
182
183
184 if (containsBaseObj) {
185 // modify the thing that should not be
186 auto &BBList(F.getBasicBlockList());
187 for (auto &BB : BBList) {
188 auto &InstList(BB.getInstList());
189 for (auto &Inst : InstList) {
190 // don't care about anything except call instructions that we didn't already add
191 if (llvm::CallInst *call = llvm::dyn_cast<llvm::CallInst>(&Inst)) {
192 for (unsigned int i = 0; i < call->getNumArgOperands(); i++) {
193 llvm::Value *V = call->getArgOperand(i);
194 if (V->getType()->isPointerTy() && V->getType()->getPointerElementType()->isStructTy() &&
195 V->getType()->getPointerElementType()->getStructName().startswith("struct.rs_")) {
196 // get just the object type name with no prefix or suffix
197 size_t LastDot = V->getType()->getPointerElementType()->getStructName().rfind('.');
198 llvm::StringRef StructName = V->getType()->getPointerElementType()->getStructName().slice(0, LastDot);
199
200 // generate the new call instruction and insert it
201 changed |= insertSetObjectHelper(call, V, StructName);
202 }
203 }
204 }
205 }
206 }
207 }
208
209 return changed;
210 }
211
Stephen Hines57936132014-11-25 17:54:59 -0800212 virtual const char *getPassName() const override {
Tim Murrayb7bce742014-11-03 16:17:30 -0800213 return ".helper method expansion for large RS objects";
214 }
215}; // end RSInvokeHelperPass class
216} // end anonymous namespace
217
218char RSInvokeHelperPass::ID = 0;
219
220namespace bcc {
221
222llvm::FunctionPass *
223createRSInvokeHelperPass(){
224 return new RSInvokeHelperPass();
225}
226
227}