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