RSForEachExpand.cpp revision 18a38a3fc6fad8355891b771dd3c6537fa8699ec
1/*
2 * Copyright 2012, 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/DerivedTypes.h>
23#include <llvm/IR/Function.h>
24#include <llvm/IR/Instructions.h>
25#include <llvm/IR/IRBuilder.h>
26#include <llvm/IR/MDBuilder.h>
27#include <llvm/IR/Module.h>
28#include <llvm/Pass.h>
29#include <llvm/Support/raw_ostream.h>
30#include <llvm/IR/DataLayout.h>
31#include <llvm/IR/Function.h>
32#include <llvm/IR/Type.h>
33#include <llvm/Transforms/Utils/BasicBlockUtils.h>
34
35#include "bcc/Config/Config.h"
36#include "bcc/Renderscript/RSInfo.h"
37#include "bcc/Support/Log.h"
38
39using namespace bcc;
40
41namespace {
42
43/* RSForEachExpandPass - This pass operates on functions that are able to be
44 * called via rsForEach() or "foreach_<NAME>". We create an inner loop for the
45 * ForEach-able function to be invoked over the appropriate data cells of the
46 * input/output allocations (adjusting other relevant parameters as we go). We
47 * support doing this for any ForEach-able compute kernels. The new function
48 * name is the original function name followed by ".expand". Note that we
49 * still generate code for the original function.
50 */
51class RSForEachExpandPass : public llvm::ModulePass {
52private:
53  static char ID;
54
55  llvm::Module *M;
56  llvm::LLVMContext *C;
57
58  const RSInfo::ExportForeachFuncListTy &mFuncs;
59
60  // Turns on optimization of allocation stride values.
61  bool mEnableStepOpt;
62
63  uint32_t getRootSignature(llvm::Function *F) {
64    const llvm::NamedMDNode *ExportForEachMetadata =
65        M->getNamedMetadata("#rs_export_foreach");
66
67    if (!ExportForEachMetadata) {
68      llvm::SmallVector<llvm::Type*, 8> RootArgTys;
69      for (llvm::Function::arg_iterator B = F->arg_begin(),
70                                        E = F->arg_end();
71           B != E;
72           ++B) {
73        RootArgTys.push_back(B->getType());
74      }
75
76      // For pre-ICS bitcode, we may not have signature information. In that
77      // case, we use the size of the RootArgTys to select the number of
78      // arguments.
79      return (1 << RootArgTys.size()) - 1;
80    }
81
82    if (ExportForEachMetadata->getNumOperands() == 0) {
83      return 0;
84    }
85
86    bccAssert(ExportForEachMetadata->getNumOperands() > 0);
87
88    // We only handle the case for legacy root() functions here, so this is
89    // hard-coded to look at only the first such function.
90    llvm::MDNode *SigNode = ExportForEachMetadata->getOperand(0);
91    if (SigNode != NULL && SigNode->getNumOperands() == 1) {
92      llvm::Value *SigVal = SigNode->getOperand(0);
93      if (SigVal->getValueID() == llvm::Value::MDStringVal) {
94        llvm::StringRef SigString =
95            static_cast<llvm::MDString*>(SigVal)->getString();
96        uint32_t Signature = 0;
97        if (SigString.getAsInteger(10, Signature)) {
98          ALOGE("Non-integer signature value '%s'", SigString.str().c_str());
99          return 0;
100        }
101        return Signature;
102      }
103    }
104
105    return 0;
106  }
107
108  // Get the actual value we should use to step through an allocation.
109  //
110  // Normally the value we use to step through an allocation is given to us by
111  // the driver. However, for certain primitive data types, we can derive an
112  // integer constant for the step value. We use this integer constant whenever
113  // possible to allow further compiler optimizations to take place.
114  //
115  // DL - Target Data size/layout information.
116  // T - Type of allocation (should be a pointer).
117  // OrigStep - Original step increment (root.expand() input from driver).
118  llvm::Value *getStepValue(llvm::DataLayout *DL, llvm::Type *T,
119                            llvm::Value *OrigStep) {
120    bccAssert(DL);
121    bccAssert(T);
122    bccAssert(OrigStep);
123    llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(T);
124    llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
125    if (mEnableStepOpt && T != VoidPtrTy && PT) {
126      llvm::Type *ET = PT->getElementType();
127      uint64_t ETSize = DL->getTypeAllocSize(ET);
128      llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
129      return llvm::ConstantInt::get(Int32Ty, ETSize);
130    } else {
131      return OrigStep;
132    }
133  }
134
135  static bool hasIn(uint32_t Signature) {
136    return Signature & 0x01;
137  }
138
139  static bool hasOut(uint32_t Signature) {
140    return Signature & 0x02;
141  }
142
143  static bool hasUsrData(uint32_t Signature) {
144    return Signature & 0x04;
145  }
146
147  static bool hasX(uint32_t Signature) {
148    return Signature & 0x08;
149  }
150
151  static bool hasY(uint32_t Signature) {
152    return Signature & 0x10;
153  }
154
155  static bool isKernel(uint32_t Signature) {
156    return Signature & 0x20;
157  }
158
159  /// @brief Returns the type of the ForEach stub parameter structure.
160  ///
161  /// Renderscript uses a single structure in which all parameters are passed
162  /// to keep the signature of the expanded function independent of the
163  /// parameters passed to it.
164  llvm::Type *getForeachStubTy() {
165    llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
166    llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
167    llvm::Type *SizeTy = Int32Ty;
168    /* Defined in frameworks/base/libs/rs/rs_hal.h:
169     *
170     * struct RsForEachStubParamStruct {
171     *   const void *in;
172     *   void *out;
173     *   const void *usr;
174     *   size_t usr_len;
175     *   uint32_t x;
176     *   uint32_t y;
177     *   uint32_t z;
178     *   uint32_t lod;
179     *   enum RsAllocationCubemapFace face;
180     *   uint32_t ar[16];
181     * };
182     */
183    llvm::SmallVector<llvm::Type*, 9> StructTys;
184    StructTys.push_back(VoidPtrTy);  // const void *in
185    StructTys.push_back(VoidPtrTy);  // void *out
186    StructTys.push_back(VoidPtrTy);  // const void *usr
187    StructTys.push_back(SizeTy);     // size_t usr_len
188    StructTys.push_back(Int32Ty);    // uint32_t x
189    StructTys.push_back(Int32Ty);    // uint32_t y
190    StructTys.push_back(Int32Ty);    // uint32_t z
191    StructTys.push_back(Int32Ty);    // uint32_t lod
192    StructTys.push_back(Int32Ty);    // enum RsAllocationCubemapFace
193    StructTys.push_back(llvm::ArrayType::get(Int32Ty, 16));  // uint32_t ar[16]
194
195    return llvm::StructType::create(StructTys, "RsForEachStubParamStruct");
196  }
197
198  /// @brief Create skeleton of the expanded function.
199  ///
200  /// This creates a function with the following signature:
201  ///
202  ///   void (const RsForEachStubParamStruct *p, uint32_t x1, uint32_t x2,
203  ///         uint32_t instep, uint32_t outstep)
204  ///
205  llvm::Function *createEmptyExpandedFunction(llvm::StringRef OldName) {
206    llvm::Type *ForEachStubPtrTy = getForeachStubTy()->getPointerTo();
207    llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
208
209    llvm::SmallVector<llvm::Type*, 8> ParamTys;
210    ParamTys.push_back(ForEachStubPtrTy);  // const RsForEachStubParamStruct *p
211    ParamTys.push_back(Int32Ty);           // uint32_t x1
212    ParamTys.push_back(Int32Ty);           // uint32_t x2
213    ParamTys.push_back(Int32Ty);           // uint32_t instep
214    ParamTys.push_back(Int32Ty);           // uint32_t outstep
215
216    llvm::FunctionType *FT =
217        llvm::FunctionType::get(llvm::Type::getVoidTy(*C), ParamTys, false);
218    llvm::Function *F =
219        llvm::Function::Create(FT, llvm::GlobalValue::ExternalLinkage,
220                               OldName + ".expand", M);
221
222    llvm::Function::arg_iterator AI = F->arg_begin();
223
224    AI->setName("p");
225    AI++;
226    AI->setName("x1");
227    AI++;
228    AI->setName("x2");
229    AI++;
230    AI->setName("arg_instep");
231    AI++;
232    AI->setName("arg_outstep");
233    AI++;
234
235    assert(AI == F->arg_end());
236
237    llvm::BasicBlock *Begin = llvm::BasicBlock::Create(*C, "Begin", F);
238    llvm::IRBuilder<> Builder(Begin);
239    Builder.CreateRetVoid();
240
241    return F;
242  }
243
244  /// @brief Create an empty loop
245  ///
246  /// Create a loop of the form:
247  ///
248  /// for (i = LowerBound; i < UpperBound; i++)
249  ///   ;
250  ///
251  /// After the loop has been created, the builder is set such that
252  /// instructions can be added to the loop body.
253  ///
254  /// @param Builder The builder to use to build this loop. The current
255  ///                position of the builder is the position the loop
256  ///                will be inserted.
257  /// @param LowerBound The first value of the loop iterator
258  /// @param UpperBound The maximal value of the loop iterator
259  /// @param LoopIV A reference that will be set to the loop iterator.
260  /// @return The BasicBlock that will be executed after the loop.
261  llvm::BasicBlock *createLoop(llvm::IRBuilder<> &Builder,
262                               llvm::Value *LowerBound,
263                               llvm::Value *UpperBound,
264                               llvm::PHINode **LoopIV) {
265    assert(LowerBound->getType() == UpperBound->getType());
266
267    llvm::BasicBlock *CondBB, *AfterBB, *HeaderBB;
268    llvm::Value *Cond, *IVNext;
269    llvm::PHINode *IV;
270
271    CondBB = Builder.GetInsertBlock();
272    AfterBB = llvm::SplitBlock(CondBB, Builder.GetInsertPoint(), this);
273    HeaderBB = llvm::BasicBlock::Create(*C, "Loop", CondBB->getParent());
274
275    // if (LowerBound < Upperbound)
276    //   goto LoopHeader
277    // else
278    //   goto AfterBB
279    CondBB->getTerminator()->eraseFromParent();
280    Builder.SetInsertPoint(CondBB);
281    Cond = Builder.CreateICmpULT(LowerBound, UpperBound);
282    Builder.CreateCondBr(Cond, HeaderBB, AfterBB);
283
284    // iv = PHI [CondBB -> LowerBound], [LoopHeader -> NextIV ]
285    // iv.next = iv + 1
286    // if (iv.next < Upperbound)
287    //   goto LoopHeader
288    // else
289    //   goto AfterBB
290    Builder.SetInsertPoint(HeaderBB);
291    IV = Builder.CreatePHI(LowerBound->getType(), 2, "X");
292    IV->addIncoming(LowerBound, CondBB);
293    IVNext = Builder.CreateNUWAdd(IV, Builder.getInt32(1));
294    IV->addIncoming(IVNext, HeaderBB);
295    Cond = Builder.CreateICmpULT(IVNext, UpperBound);
296    Builder.CreateCondBr(Cond, HeaderBB, AfterBB);
297    AfterBB->setName("Exit");
298    Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
299    *LoopIV = IV;
300    return AfterBB;
301  }
302
303public:
304  RSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs,
305                      bool pEnableStepOpt)
306      : ModulePass(ID), M(NULL), C(NULL), mFuncs(pForeachFuncs),
307        mEnableStepOpt(pEnableStepOpt) {
308  }
309
310  /* Performs the actual optimization on a selected function. On success, the
311   * Module will contain a new function of the name "<NAME>.expand" that
312   * invokes <NAME>() in a loop with the appropriate parameters.
313   */
314  bool ExpandFunction(llvm::Function *F, uint32_t Signature) {
315    ALOGV("Expanding ForEach-able Function %s", F->getName().str().c_str());
316
317    if (!Signature) {
318      Signature = getRootSignature(F);
319      if (!Signature) {
320        // We couldn't determine how to expand this function based on its
321        // function signature.
322        return false;
323      }
324    }
325
326    llvm::DataLayout DL(M);
327
328    llvm::Function *ExpandedFunc = createEmptyExpandedFunction(F->getName());
329
330    // Create and name the actual arguments to this expanded function.
331    llvm::SmallVector<llvm::Argument*, 8> ArgVec;
332    for (llvm::Function::arg_iterator B = ExpandedFunc->arg_begin(),
333                                      E = ExpandedFunc->arg_end();
334         B != E;
335         ++B) {
336      ArgVec.push_back(B);
337    }
338
339    if (ArgVec.size() != 5) {
340      ALOGE("Incorrect number of arguments to function: %zu",
341            ArgVec.size());
342      return false;
343    }
344    llvm::Value *Arg_p = ArgVec[0];
345    llvm::Value *Arg_x1 = ArgVec[1];
346    llvm::Value *Arg_x2 = ArgVec[2];
347    llvm::Value *Arg_instep = ArgVec[3];
348    llvm::Value *Arg_outstep = ArgVec[4];
349
350    llvm::Value *InStep = NULL;
351    llvm::Value *OutStep = NULL;
352
353    // Construct the actual function body.
354    llvm::IRBuilder<> Builder(ExpandedFunc->getEntryBlock().begin());
355
356    // Collect and construct the arguments for the kernel().
357    // Note that we load any loop-invariant arguments before entering the Loop.
358    llvm::Function::arg_iterator Args = F->arg_begin();
359
360    llvm::Type *InTy = NULL;
361    llvm::Value *InBasePtr = NULL;
362    if (hasIn(Signature)) {
363      InTy = Args->getType();
364      InStep = getStepValue(&DL, InTy, Arg_instep);
365      InStep->setName("instep");
366      InBasePtr = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 0));
367      Args++;
368    }
369
370    llvm::Type *OutTy = NULL;
371    llvm::Value *OutBasePtr = NULL;
372    if (hasOut(Signature)) {
373      OutTy = Args->getType();
374      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
375      OutStep->setName("outstep");
376      OutBasePtr = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 1));
377      Args++;
378    }
379
380    llvm::Value *UsrData = NULL;
381    if (hasUsrData(Signature)) {
382      llvm::Type *UsrDataTy = Args->getType();
383      UsrData = Builder.CreatePointerCast(Builder.CreateLoad(
384          Builder.CreateStructGEP(Arg_p, 2)), UsrDataTy);
385      UsrData->setName("UsrData");
386      Args++;
387    }
388
389    if (hasX(Signature)) {
390      Args++;
391    }
392
393    llvm::Value *Y = NULL;
394    if (hasY(Signature)) {
395      Y = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 5), "Y");
396      Args++;
397    }
398
399    bccAssert(Args == F->arg_end());
400
401    llvm::PHINode *IV;
402    createLoop(Builder, Arg_x1, Arg_x2, &IV);
403
404    // Populate the actual call to kernel().
405    llvm::SmallVector<llvm::Value*, 8> RootArgs;
406
407    llvm::Value *InPtr = NULL;
408    llvm::Value *OutPtr = NULL;
409
410    // Calculate the current input and output pointers
411    //
412    // We always calculate the input/output pointers with a GEP operating on i8
413    // values and only cast at the very end to OutTy. This is because the step
414    // between two values is given in bytes.
415    //
416    // TODO: We could further optimize the output by using a GEP operation of
417    // type 'OutTy' in cases where the element type of the allocation allows.
418    if (OutBasePtr) {
419      llvm::Value *OutOffset = Builder.CreateSub(IV, Arg_x1);
420      OutOffset = Builder.CreateMul(OutOffset, OutStep);
421      OutPtr = Builder.CreateGEP(OutBasePtr, OutOffset);
422      OutPtr = Builder.CreatePointerCast(OutPtr, OutTy);
423    }
424    if (InBasePtr) {
425      llvm::Value *InOffset = Builder.CreateSub(IV, Arg_x1);
426      InOffset = Builder.CreateMul(InOffset, InStep);
427      InPtr = Builder.CreateGEP(InBasePtr, InOffset);
428      InPtr = Builder.CreatePointerCast(InPtr, InTy);
429    }
430
431    if (InPtr) {
432      RootArgs.push_back(InPtr);
433    }
434
435    if (OutPtr) {
436      RootArgs.push_back(OutPtr);
437    }
438
439    if (UsrData) {
440      RootArgs.push_back(UsrData);
441    }
442
443    llvm::Value *X = IV;
444    if (hasX(Signature)) {
445      RootArgs.push_back(X);
446    }
447
448    if (Y) {
449      RootArgs.push_back(Y);
450    }
451
452    Builder.CreateCall(F, RootArgs);
453
454    return true;
455  }
456
457  /* Expand a pass-by-value kernel.
458   */
459  bool ExpandKernel(llvm::Function *F, uint32_t Signature) {
460    bccAssert(isKernel(Signature));
461    ALOGV("Expanding kernel Function %s", F->getName().str().c_str());
462
463    // TODO: Refactor this to share functionality with ExpandFunction.
464    llvm::DataLayout DL(M);
465
466    llvm::Function *ExpandedFunc = createEmptyExpandedFunction(F->getName());
467
468    // Create and name the actual arguments to this expanded function.
469    llvm::SmallVector<llvm::Argument*, 8> ArgVec;
470    for (llvm::Function::arg_iterator B = ExpandedFunc->arg_begin(),
471                                      E = ExpandedFunc->arg_end();
472         B != E;
473         ++B) {
474      ArgVec.push_back(B);
475    }
476
477    if (ArgVec.size() != 5) {
478      ALOGE("Incorrect number of arguments to function: %zu",
479            ArgVec.size());
480      return false;
481    }
482    llvm::Value *Arg_p = ArgVec[0];
483    llvm::Value *Arg_x1 = ArgVec[1];
484    llvm::Value *Arg_x2 = ArgVec[2];
485    llvm::Value *Arg_instep = ArgVec[3];
486    llvm::Value *Arg_outstep = ArgVec[4];
487
488    llvm::Value *InStep = NULL;
489    llvm::Value *OutStep = NULL;
490
491    // Construct the actual function body.
492    llvm::IRBuilder<> Builder(ExpandedFunc->getEntryBlock().begin());
493
494    // Create TBAA meta-data.
495    llvm::MDNode *TBAARenderScript, *TBAAAllocation, *TBAAPointer;
496
497    llvm::MDBuilder MDHelper(*C);
498    TBAARenderScript = MDHelper.createTBAARoot("RenderScript TBAA");
499    TBAAAllocation = MDHelper.createTBAANode("allocation", TBAARenderScript);
500    TBAAPointer = MDHelper.createTBAANode("pointer", TBAARenderScript);
501
502    // Collect and construct the arguments for the kernel().
503    // Note that we load any loop-invariant arguments before entering the Loop.
504    llvm::Function::arg_iterator Args = F->arg_begin();
505
506    llvm::Type *OutTy = NULL;
507    bool PassOutByReference = false;
508    llvm::LoadInst *OutBasePtr = NULL;
509    if (hasOut(Signature)) {
510      llvm::Type *OutBaseTy = F->getReturnType();
511      if (OutBaseTy->isVoidTy()) {
512        PassOutByReference = true;
513        OutTy = Args->getType();
514        Args++;
515      } else {
516        OutTy = OutBaseTy->getPointerTo();
517        // We don't increment Args, since we are using the actual return type.
518      }
519      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
520      OutStep->setName("outstep");
521      OutBasePtr = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 1));
522      OutBasePtr->setMetadata("tbaa", TBAAPointer);
523    }
524
525    llvm::Type *InBaseTy = NULL;
526    llvm::Type *InTy = NULL;
527    llvm::LoadInst *InBasePtr = NULL;
528    if (hasIn(Signature)) {
529      InBaseTy = Args->getType();
530      InTy =InBaseTy->getPointerTo();
531      InStep = getStepValue(&DL, InTy, Arg_instep);
532      InStep->setName("instep");
533      InBasePtr = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 0));
534      InBasePtr->setMetadata("tbaa", TBAAPointer);
535      Args++;
536    }
537
538    // No usrData parameter on kernels.
539    bccAssert(!hasUsrData(Signature));
540
541    if (hasX(Signature)) {
542      Args++;
543    }
544
545    llvm::Value *Y = NULL;
546    if (hasY(Signature)) {
547      Y = Builder.CreateLoad(Builder.CreateStructGEP(Arg_p, 5), "Y");
548      Args++;
549    }
550
551    bccAssert(Args == F->arg_end());
552
553    llvm::PHINode *IV;
554    createLoop(Builder, Arg_x1, Arg_x2, &IV);
555
556    // Populate the actual call to kernel().
557    llvm::SmallVector<llvm::Value*, 8> RootArgs;
558
559    llvm::Value *InPtr = NULL;
560    llvm::Value *OutPtr = NULL;
561
562    // Calculate the current input and output pointers
563    //
564    // We always calculate the input/output pointers with a GEP operating on i8
565    // values and only cast at the very end to OutTy. This is because the step
566    // between two values is given in bytes.
567    //
568    // TODO: We could further optimize the output by using a GEP operation of
569    // type 'OutTy' in cases where the element type of the allocation allows.
570    if (OutBasePtr) {
571      llvm::Value *OutOffset = Builder.CreateSub(IV, Arg_x1);
572      OutOffset = Builder.CreateMul(OutOffset, OutStep);
573      OutPtr = Builder.CreateGEP(OutBasePtr, OutOffset);
574      OutPtr = Builder.CreatePointerCast(OutPtr, OutTy);
575    }
576    if (InBasePtr) {
577      llvm::Value *InOffset = Builder.CreateSub(IV, Arg_x1);
578      InOffset = Builder.CreateMul(InOffset, InStep);
579      InPtr = Builder.CreateGEP(InBasePtr, InOffset);
580      InPtr = Builder.CreatePointerCast(InPtr, InTy);
581    }
582
583    if (PassOutByReference) {
584      RootArgs.push_back(OutPtr);
585    }
586
587    if (InPtr) {
588      llvm::LoadInst *In = Builder.CreateLoad(InPtr, "In");
589      In->setMetadata("tbaa", TBAAAllocation);
590      RootArgs.push_back(In);
591    }
592
593    llvm::Value *X = IV;
594    if (hasX(Signature)) {
595      RootArgs.push_back(X);
596    }
597
598    if (Y) {
599      RootArgs.push_back(Y);
600    }
601
602    llvm::Value *RetVal = Builder.CreateCall(F, RootArgs);
603
604    if (OutPtr && !PassOutByReference) {
605      llvm::StoreInst *Store = Builder.CreateStore(RetVal, OutPtr);
606      Store->setMetadata("tbaa", TBAAAllocation);
607    }
608
609    return true;
610  }
611
612  /// @brief Checks if pointers to allocation internals are exposed
613  ///
614  /// This function verifies if through the parameters passed to the kernel
615  /// or through calls to the runtime library the script gains access to
616  /// pointers pointing to data within a RenderScript Allocation.
617  /// If we know we control all loads from and stores to data within
618  /// RenderScript allocations and if we know the run-time internal accesses
619  /// are all annotated with RenderScript TBAA metadata, only then we
620  /// can safely use TBAA to distinguish between generic and from-allocation
621  /// pointers.
622  bool allocPointersExposed(llvm::Module &M) {
623    // Old style kernel function can expose pointers to elements within
624    // allocations.
625    // TODO: Extend analysis to allow simple cases of old-style kernels.
626    for (RSInfo::ExportForeachFuncListTy::const_iterator
627             func_iter = mFuncs.begin(), func_end = mFuncs.end();
628         func_iter != func_end; func_iter++) {
629      const char *Name = func_iter->first;
630      uint32_t Signature = func_iter->second;
631      if (M.getFunction(Name) && !isKernel(Signature)) {
632        return true;
633      }
634    }
635
636    // Check for library functions that expose a pointer to an Allocation or
637    // that are not yet annotated with RenderScript-specific tbaa information.
638    static std::vector<std::string> Funcs;
639
640    // rsGetElementAt(...)
641    Funcs.push_back("_Z14rsGetElementAt13rs_allocationj");
642    Funcs.push_back("_Z14rsGetElementAt13rs_allocationjj");
643    Funcs.push_back("_Z14rsGetElementAt13rs_allocationjjj");
644    // rsSetElementAt()
645    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvj");
646    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvjj");
647    Funcs.push_back("_Z14rsSetElementAt13rs_allocationPvjjj");
648    // rsGetElementAtYuv_uchar_Y()
649    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_Y13rs_allocationjj");
650    // rsGetElementAtYuv_uchar_U()
651    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_U13rs_allocationjj");
652    // rsGetElementAtYuv_uchar_V()
653    Funcs.push_back("_Z25rsGetElementAtYuv_uchar_V13rs_allocationjj");
654
655    for (std::vector<std::string>::iterator FI = Funcs.begin(),
656                                            FE = Funcs.end();
657         FI != FE; ++FI) {
658      llvm::Function *F = M.getFunction(*FI);
659
660      if (!F) {
661        ALOGE("Missing run-time function '%s'", FI->c_str());
662        return true;
663      }
664
665      if (F->getNumUses() > 0) {
666        return true;
667      }
668    }
669
670    return false;
671  }
672
673  /// @brief Connect RenderScript TBAA metadata to C/C++ metadata
674  ///
675  /// The TBAA metadata used to annotate loads/stores from RenderScript
676  /// Allocations is generated in a separate TBAA tree with a "RenderScript TBAA"
677  /// root node. LLVM does assume may-alias for all nodes in unrelated alias
678  /// analysis trees. This function makes the RenderScript TBAA a subtree of the
679  /// normal C/C++ TBAA tree aside of normal C/C++ types. With the connected trees
680  /// every access to an Allocation is resolved to must-alias if compared to
681  /// a normal C/C++ access.
682  void connectRenderScriptTBAAMetadata(llvm::Module &M) {
683    llvm::MDBuilder MDHelper(*C);
684    llvm::MDNode *TBAARenderScript = MDHelper.createTBAARoot("RenderScript TBAA");
685
686    llvm::MDNode *TBAARoot = MDHelper.createTBAARoot("Simple C/C++ TBAA");
687    llvm::MDNode *TBAAMergedRS = MDHelper.createTBAANode("RenderScript", TBAARoot);
688
689    TBAARenderScript->replaceAllUsesWith(TBAAMergedRS);
690  }
691
692  virtual bool runOnModule(llvm::Module &M) {
693    bool Changed = false;
694    this->M = &M;
695    C = &M.getContext();
696
697    bool AllocsExposed = allocPointersExposed(M);
698
699    for (RSInfo::ExportForeachFuncListTy::const_iterator
700             func_iter = mFuncs.begin(), func_end = mFuncs.end();
701         func_iter != func_end; func_iter++) {
702      const char *name = func_iter->first;
703      uint32_t signature = func_iter->second;
704      llvm::Function *kernel = M.getFunction(name);
705      if (kernel) {
706        if (isKernel(signature)) {
707          Changed |= ExpandKernel(kernel, signature);
708          kernel->setLinkage(llvm::GlobalValue::InternalLinkage);
709        } else if (kernel->getReturnType()->isVoidTy()) {
710          Changed |= ExpandFunction(kernel, signature);
711          kernel->setLinkage(llvm::GlobalValue::InternalLinkage);
712        } else {
713          // There are some graphics root functions that are not
714          // expanded, but that will be called directly. For those
715          // functions, we can not set the linkage to internal.
716        }
717      }
718    }
719
720    if (!AllocsExposed) {
721      connectRenderScriptTBAAMetadata(M);
722    }
723
724    return Changed;
725  }
726
727  virtual const char *getPassName() const {
728    return "ForEach-able Function Expansion";
729  }
730
731}; // end RSForEachExpandPass
732
733} // end anonymous namespace
734
735char RSForEachExpandPass::ID = 0;
736
737namespace bcc {
738
739llvm::ModulePass *
740createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs,
741                          bool pEnableStepOpt){
742  return new RSForEachExpandPass(pForeachFuncs, pEnableStepOpt);
743}
744
745} // end namespace bcc
746