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