blob: 1119a7af4e086f8e04f3df027a794e44f602f23f [file] [log] [blame]
David Gross1d93a192015-03-25 14:59:27 -07001/*
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 (ArgPtrDomainType->isStructTy()) {
82 const llvm::StringRef StructName = ArgPtrDomainType->getStructName();
83 if (StructName.equals("struct.rs_kernel_context_t") || StructName.equals("RsExpandKernelDriverInfoPfx")) {
84 Changed |= markInvariantUserLoads(&Arg);
85 }
86 }
87 }
88 }
89
90 return Changed;
91 }
92
93 virtual const char *getPassName() const {
94 return "Renderscript Invariant Load Annotation";
95 }
96
97private:
98
99 /*
100 * Follow def->use chains rooted at Value through calculations
101 * "based on" Value (see the "based on" definition at
102 * http://llvm.org/docs/LangRef.html#pointer-aliasing-rules). If a
103 * chain reaches the pointer operand of a Load, mark that Load as
104 * "invariant.load" -- i.e., it accesses memory which does not
105 * change.
106 */
107 bool markInvariantUserLoads(llvm::Value *Value) {
108 bool Changed = false;
109 for (llvm::Use &Use : Value->uses()) {
110 llvm::Instruction *Inst = llvm::cast<llvm::Instruction>(Use.getUser());
111
112 /*
113 * We only examine a small set of opcodes here, because these
114 * are the opcodes that currently appear in the patterns of
115 * interest (foreachexp-generated code, and
116 * rsGet*(rs_kernel_context_t*) APIs). Other opcodes could be
117 * added if necessary.
118 */
119 if (auto BitCast = llvm::dyn_cast<llvm::BitCastInst>(Inst)) {
120 Changed |= markInvariantUserLoads(BitCast);
121 } else if (auto GetElementPtr = llvm::dyn_cast<llvm::GetElementPtrInst>(Inst)) {
122 if (Use.get() == GetElementPtr->getPointerOperand())
123 Changed |= markInvariantUserLoads(GetElementPtr);
124 } else if (auto Load = llvm::dyn_cast<llvm::LoadInst>(Inst)) {
125 if (Use.get() == Load->getPointerOperand()) {
126 Load->setMetadata("invariant.load", EmptyMDNode);
127 Changed = true;
128 }
129 }
130 }
131 return Changed;
132 }
133
134 // Pointer to empty metadata node used for "invariant.load" marking.
135 llvm::MDNode *EmptyMDNode;
136}; // end RSInvariantPass
137
138char RSInvariantPass::ID = 0;
139llvm::RegisterPass<RSInvariantPass> X("rsinvariant", "RS Invariant Load Pass");
140
141} // end anonymous namespace
142
143namespace bcc {
144
145llvm::FunctionPass *
146createRSInvariantPass() {
147 return new RSInvariantPass();
148}
149
150} // end namespace bcc