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#include "RSTransforms.h"
18#include "RSUtils.h"
19
20#include <llvm/IR/Function.h>
21#include <llvm/IR/Instructions.h>
22#include <llvm/IR/Metadata.h>
23#include <llvm/IR/Module.h>
24#include <llvm/IR/Type.h>
25#include <llvm/Pass.h>
26
27namespace {
28
29/*
30 * RSInvariantPass - This pass looks for Loads that access
31 * RsExpandKernelDriverInfo instances (which should never be written by
32 * a script, only by the driver) and marks them "invariant.load".
33 *
34 * There should be only two sources of Loads from such instances:
35 * - An instance can appear as an argument of type
36 *   "RsExpandKernelDriverInfoPfx*" passed to a .expand function by
37 *   the driver.
38 * - An instance can appear as an argument of type
39 *   "rs_kernel_context_t*" passed to an API query function by the
40 *   user.
41 * Only the compiler-generated .expand functions and the API query
42 * functions can see the fields of RsExpandKernelDriverInfo --
43 * rs_kernel_context_t is opaque to user code, so there cannot be any
44 * Loads from it in user code.
45 *
46 * This pass should be run
47 * - after foreachexp, so that it can see the Loads generated within
48 *   .expand functions
49 * - before inlining, so that it can recognize API query function
50 *   arguments.
51 *
52 * WARNINGS:
53 * - If user code or APIs can modify RsExpandKernelDriverInfo
54 *   instances, this pass MAY ALLOW ILLEGAL OPTIMIZATION.
55 * - If this pass runs at a different time, it may be ineffective
56 *   (fail to mark some or all eligible Loads, and thereby cost
57 *   performance).
58 * - If the names of the data types change, this pass may be
59 *   ineffective.
60 * - If the patterns by which fields are loaded from
61 *   RsExpandKernelDriverInfo instances change, this pass may be
62 *   ineffective.
63 */
64class RSInvariantPass : public llvm::FunctionPass {
65public:
66  static char ID;
67
68  RSInvariantPass() : FunctionPass(ID), EmptyMDNode(nullptr) { }
69
70  virtual bool doInitialization(llvm::Module &M) {
71    EmptyMDNode = llvm::MDNode::get(M.getContext(), llvm::None);
72    return true;
73  }
74
75  virtual bool runOnFunction(llvm::Function &F) {
76    bool Changed = false;
77
78    for (llvm::Value &Arg : F.args()) {
79      const llvm::Type *ArgType = Arg.getType();
80      if (ArgType->isPointerTy()) {
81        const llvm::Type *ArgPtrDomainType =  ArgType->getPointerElementType();
82        if (auto ArgPtrDomainStructType = llvm::dyn_cast<llvm::StructType>(ArgPtrDomainType)) {
83          if (!ArgPtrDomainStructType->isLiteral()) {
84            const llvm::StringRef StructName = getUnsuffixedStructName(ArgPtrDomainStructType);
85            if (StructName.equals("struct.rs_kernel_context_t") || StructName.equals("RsExpandKernelDriverInfoPfx")) {
86              Changed |= markInvariantUserLoads(&Arg);
87            }
88          }
89        }
90      }
91    }
92
93    return Changed;
94  }
95
96  virtual const char *getPassName() const {
97    return "Renderscript Invariant Load Annotation";
98  }
99
100private:
101
102  /*
103   * Follow def->use chains rooted at Value through calculations
104   * "based on" Value (see the "based on" definition at
105   * http://llvm.org/docs/LangRef.html#pointer-aliasing-rules).  If a
106   * chain reaches the pointer operand of a Load, mark that Load as
107   * "invariant.load" -- i.e., it accesses memory which does not
108   * change.
109   */
110  bool markInvariantUserLoads(llvm::Value *Value) {
111    bool Changed = false;
112    for (llvm::Use &Use : Value->uses()) {
113      llvm::Instruction *Inst = llvm::cast<llvm::Instruction>(Use.getUser());
114
115      /*
116       * We only examine a small set of opcodes here, because these
117       * are the opcodes that currently appear in the patterns of
118       * interest (foreachexp-generated code, and
119       * rsGet*(rs_kernel_context_t*) APIs).  Other opcodes could be
120       * added if necessary.
121       */
122      if (auto BitCast = llvm::dyn_cast<llvm::BitCastInst>(Inst)) {
123        Changed |= markInvariantUserLoads(BitCast);
124      } else if (auto GetElementPtr = llvm::dyn_cast<llvm::GetElementPtrInst>(Inst)) {
125        if (Use.get() == GetElementPtr->getPointerOperand())
126          Changed |= markInvariantUserLoads(GetElementPtr);
127      } else if (auto Load = llvm::dyn_cast<llvm::LoadInst>(Inst)) {
128        if (Use.get() == Load->getPointerOperand()) {
129          Load->setMetadata("invariant.load", EmptyMDNode);
130          Changed = true;
131        }
132      }
133    }
134    return Changed;
135  }
136
137  // Pointer to empty metadata node used for "invariant.load" marking.
138  llvm::MDNode *EmptyMDNode;
139}; // end RSInvariantPass
140
141char RSInvariantPass::ID = 0;
142llvm::RegisterPass<RSInvariantPass> X("rsinvariant", "RS Invariant Load Pass");
143
144} // end anonymous namespace
145
146namespace bcc {
147
148llvm::FunctionPass *
149createRSInvariantPass() {
150  return new RSInvariantPass();
151}
152
153} // end namespace bcc
154