RSInvokeHelperPass.cpp revision c754d49ee856be620e041348a9f2b3d5610a5a26
1/*
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
66  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
71  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;
122    if (StructName.equals(rsAllocationType->getName())) {
123      SetObj = rsAllocationSetObj;
124    } else if (StructName.equals(rsElementType->getName())) {
125      SetObj = rsElementSetObj;
126    } else if (StructName.equals(rsSamplerType->getName())) {
127      SetObj = rsSamplerSetObj;
128    } else if (StructName.equals(rsScriptType->getName())) {
129      SetObj = rsScriptSetObj;
130    } else if (StructName.equals(rsTypeType->getName())) {
131      SetObj = rsTypeSetObj;
132    } else {
133      return false; // this is for graphics types and matrices; do nothing
134    }
135
136    llvm::SmallVector<llvm::Value*, 2> SetObjParams;
137    SetObjParams.push_back(V);
138    SetObjParams.push_back(V);
139
140    llvm::CallInst::Create(SetObj, SetObjParams, "", Call);
141    return true;
142  }
143
144
145  // this only modifies .helper functions that take certain RS base object types
146  virtual bool runOnFunction(llvm::Function &F) override {
147    if (!F.getName().startswith(".helper"))
148      return false;
149
150    bool changed = false;
151    const llvm::Function::ArgumentListType &argList(F.getArgumentList());
152    bool containsBaseObj = false;
153
154    // .helper methods should have one arg only, an anonymous struct
155    // that struct may contain BaseObjs
156    for (auto arg = argList.begin(); arg != argList.end(); arg++) {
157      llvm::Type *argType = arg->getType();
158      if (!argType->isPointerTy() || !argType->getPointerElementType()->isStructTy())
159        continue;
160
161      llvm::StructType *argStructType = llvm::dyn_cast<llvm::StructType>(argType->getPointerElementType());
162
163      for (unsigned int i = 0; i < argStructType->getNumElements(); i++) {
164        llvm::Type *currentType = argStructType->getElementType(i);
165        if (currentType->isStructTy() && currentType->getStructName().startswith("struct.rs_")) {
166          containsBaseObj = true;
167        }
168      }
169      break;
170    }
171
172
173    if (containsBaseObj) {
174      // modify the thing that should not be
175      auto &BBList(F.getBasicBlockList());
176      for (auto &BB : BBList) {
177        auto &InstList(BB.getInstList());
178        for (auto &Inst : InstList) {
179          // don't care about anything except call instructions that we didn't already add
180          if (llvm::CallInst *call = llvm::dyn_cast<llvm::CallInst>(&Inst)) {
181            for (unsigned int i = 0; i < call->getNumArgOperands(); i++) {
182              llvm::Value *V = call->getArgOperand(i);
183              if (V->getType()->isPointerTy() && V->getType()->getPointerElementType()->isStructTy() &&
184                  V->getType()->getPointerElementType()->getStructName().startswith("struct.rs_")) {
185                // get just the object type name with no prefix or suffix
186                size_t LastDot = V->getType()->getPointerElementType()->getStructName().rfind('.');
187                llvm::StringRef StructName = V->getType()->getPointerElementType()->getStructName().slice(0, LastDot);
188
189                // generate the new call instruction and insert it
190                changed |= insertSetObjectHelper(call, V, StructName);
191              }
192            }
193          }
194        }
195      }
196    }
197
198    return changed;
199  }
200
201  virtual const char *getPassName() const override {
202    return ".helper method expansion for large RS objects";
203  }
204}; // end RSInvokeHelperPass class
205} // end anonymous namespace
206
207char RSInvokeHelperPass::ID = 0;
208
209namespace bcc {
210
211llvm::FunctionPass *
212createRSInvokeHelperPass(){
213  return new RSInvokeHelperPass();
214}
215
216}
217