11d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross/*
21d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Copyright 2015, The Android Open Source Project
31d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
41d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Licensed under the Apache License, Version 2.0 (the "License");
51d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * you may not use this file except in compliance with the License.
61d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * You may obtain a copy of the License at
71d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
81d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *     http://www.apache.org/licenses/LICENSE-2.0
91d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
101d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Unless required by applicable law or agreed to in writing, software
111d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * distributed under the License is distributed on an "AS IS" BASIS,
121d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * See the License for the specific language governing permissions and
141d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * limitations under the License.
151d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross */
161d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
171d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include "bcc/Renderscript/RSTransforms.h"
1823c2bfe96698091f2886d1959435d8949f76c46bDavid Gross#include "bcc/Renderscript/RSUtils.h"
191d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
201d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Function.h>
211d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Instructions.h>
221d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Metadata.h>
231d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Module.h>
241d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/IR/Type.h>
251d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross#include <llvm/Pass.h>
261d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
271d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossnamespace {
281d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
291d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross/*
301d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * RSInvariantPass - This pass looks for Loads that access
311d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * RsExpandKernelDriverInfo instances (which should never be written by
321d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * a script, only by the driver) and marks them "invariant.load".
331d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
341d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * There should be only two sources of Loads from such instances:
351d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - An instance can appear as an argument of type
361d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   "RsExpandKernelDriverInfoPfx*" passed to a .expand function by
371d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   the driver.
381d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - An instance can appear as an argument of type
391d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   "rs_kernel_context_t*" passed to an API query function by the
401d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   user.
411d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Only the compiler-generated .expand functions and the API query
421d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * functions can see the fields of RsExpandKernelDriverInfo --
431d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * rs_kernel_context_t is opaque to user code, so there cannot be any
441d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * Loads from it in user code.
451d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
461d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * This pass should be run
471d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - after foreachexp, so that it can see the Loads generated within
481d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   .expand functions
491d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - before inlining, so that it can recognize API query function
501d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   arguments.
511d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *
521d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * WARNINGS:
531d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If user code or APIs can modify RsExpandKernelDriverInfo
541d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   instances, this pass MAY ALLOW ILLEGAL OPTIMIZATION.
551d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If this pass runs at a different time, it may be ineffective
561d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   (fail to mark some or all eligible Loads, and thereby cost
571d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   performance).
581d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If the names of the data types change, this pass may be
591d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   ineffective.
601d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross * - If the patterns by which fields are loaded from
611d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   RsExpandKernelDriverInfo instances change, this pass may be
621d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross *   ineffective.
631d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross */
641d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossclass RSInvariantPass : public llvm::FunctionPass {
651d93a190e62ec1588b4724ca8759216b2d0b76d7David Grosspublic:
661d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  static char ID;
671d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
681d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  RSInvariantPass() : FunctionPass(ID), EmptyMDNode(nullptr) { }
691d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
701d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual bool doInitialization(llvm::Module &M) {
711d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    EmptyMDNode = llvm::MDNode::get(M.getContext(), llvm::None);
721d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return true;
731d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
741d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
751d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual bool runOnFunction(llvm::Function &F) {
761d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    bool Changed = false;
771d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
781d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    for (llvm::Value &Arg : F.args()) {
791d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      const llvm::Type *ArgType = Arg.getType();
801d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      if (ArgType->isPointerTy()) {
811d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        const llvm::Type *ArgPtrDomainType =  ArgType->getPointerElementType();
82e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar        if (auto ArgPtrDomainStructType = llvm::dyn_cast<llvm::StructType>(ArgPtrDomainType)) {
83150eef4097c996e14b50b7f58184265385c44713Pirama Arumuga Nainar          if (!ArgPtrDomainStructType->isLiteral()) {
8423c2bfe96698091f2886d1959435d8949f76c46bDavid Gross            const llvm::StringRef StructName = getUnsuffixedStructName(ArgPtrDomainStructType);
85e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar            if (StructName.equals("struct.rs_kernel_context_t") || StructName.equals("RsExpandKernelDriverInfoPfx")) {
86e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar              Changed |= markInvariantUserLoads(&Arg);
87e8b9f0607d59e991d984302d790b8b16e3623cc3Pirama Arumuga Nainar            }
881d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          }
891d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        }
901d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      }
911d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    }
921d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
931d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return Changed;
941d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
951d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
961d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  virtual const char *getPassName() const {
971d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return "Renderscript Invariant Load Annotation";
981d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
991d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1001d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossprivate:
1011d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1021d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  /*
1031d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * Follow def->use chains rooted at Value through calculations
1041d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * "based on" Value (see the "based on" definition at
1051d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * http://llvm.org/docs/LangRef.html#pointer-aliasing-rules).  If a
1061d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * chain reaches the pointer operand of a Load, mark that Load as
1071d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * "invariant.load" -- i.e., it accesses memory which does not
1081d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   * change.
1091d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross   */
1101d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  bool markInvariantUserLoads(llvm::Value *Value) {
1111d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    bool Changed = false;
1121d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    for (llvm::Use &Use : Value->uses()) {
1131d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      llvm::Instruction *Inst = llvm::cast<llvm::Instruction>(Use.getUser());
1141d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1151d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      /*
1161d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * We only examine a small set of opcodes here, because these
1171d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * are the opcodes that currently appear in the patterns of
1181d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * interest (foreachexp-generated code, and
1191d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * rsGet*(rs_kernel_context_t*) APIs).  Other opcodes could be
1201d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       * added if necessary.
1211d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross       */
1221d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      if (auto BitCast = llvm::dyn_cast<llvm::BitCastInst>(Inst)) {
1231d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        Changed |= markInvariantUserLoads(BitCast);
1241d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      } else if (auto GetElementPtr = llvm::dyn_cast<llvm::GetElementPtrInst>(Inst)) {
1251d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        if (Use.get() == GetElementPtr->getPointerOperand())
1261d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Changed |= markInvariantUserLoads(GetElementPtr);
1271d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      } else if (auto Load = llvm::dyn_cast<llvm::LoadInst>(Inst)) {
1281d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        if (Use.get() == Load->getPointerOperand()) {
1291d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Load->setMetadata("invariant.load", EmptyMDNode);
1301d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross          Changed = true;
1311d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross        }
1321d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross      }
1331d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    }
1341d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross    return Changed;
1351d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  }
1361d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1371d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  // Pointer to empty metadata node used for "invariant.load" marking.
1381d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  llvm::MDNode *EmptyMDNode;
1391d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross}; // end RSInvariantPass
1401d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1411d93a190e62ec1588b4724ca8759216b2d0b76d7David Grosschar RSInvariantPass::ID = 0;
1421d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossllvm::RegisterPass<RSInvariantPass> X("rsinvariant", "RS Invariant Load Pass");
1431d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1441d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross} // end anonymous namespace
1451d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1461d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossnamespace bcc {
1471d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1481d93a190e62ec1588b4724ca8759216b2d0b76d7David Grossllvm::FunctionPass *
1491d93a190e62ec1588b4724ca8759216b2d0b76d7David GrosscreateRSInvariantPass() {
1501d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross  return new RSInvariantPass();
1511d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross}
1521d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross
1531d93a190e62ec1588b4724ca8759216b2d0b76d7David Gross} // end namespace bcc
154