• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016, 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/Config.h"
18 #include "Assert.h"
19 #include "Log.h"
20 #include "RSTransforms.h"
21 
22 #include <cstdlib>
23 
24 #include <llvm/IR/Function.h>
25 #include <llvm/IR/Instructions.h>
26 #include <llvm/IR/Module.h>
27 #include <llvm/Pass.h>
28 #include <llvm/IR/GetElementPtrTypeIterator.h>
29 
30 namespace { // anonymous namespace
31 
32 /* This pass translates GEPs that index into structs or arrays of structs to
33  * GEPs with an int8* operand and a byte offset.  This translation is done to
34  * enforce on x86 the ARM alignment rule that 64-bit scalars be 8-byte aligned
35  * for structs with such scalars.
36  */
37 class RSX86TranslateGEPPass : public llvm::FunctionPass {
38 private:
39   static char ID;
40   llvm::LLVMContext *Context;
41   const llvm::DataLayout DL;
42 
43   // Walk a GEP instruction and return true if any type indexed is a struct.
GEPIndexesStructType(const llvm::GetElementPtrInst * GEP)44   bool GEPIndexesStructType(const llvm::GetElementPtrInst *GEP) {
45     for (llvm::gep_type_iterator GTI = gep_type_begin(GEP),
46                                  GTE = gep_type_end(GEP);
47          GTI != GTE; ++GTI) {
48       if (llvm::dyn_cast<llvm::StructType>(*GTI)) {
49         return true;
50       }
51     }
52     return false;
53   }
54 
55   // Helper method to add two llvm::Value parameters
incrementOffset(llvm::Value * accum,llvm::Value * incr,llvm::Instruction * InsertBefore)56   llvm::Value *incrementOffset(llvm::Value *accum, llvm::Value *incr,
57                                llvm::Instruction *InsertBefore) {
58     if (accum == nullptr)
59       return incr;
60     return llvm::BinaryOperator::CreateAdd(accum, incr, "", InsertBefore);
61   }
62 
63   // Compute the byte offset for a GEP from the GEP's base pointer operand.
64   // Based on visitGetElementPtrInst in llvm/lib/Transforms/Scalar/SROA.cpp.
65   // The difference is that this function handles non-constant array indices and
66   // constructs a sequence of instructions to calculate the offset.  These
67   // instructions might not be the most efficient way to calculate this offset,
68   // but we rely on subsequent optimizations to do necessary fold/combine.
computeGEPOffset(llvm::GetElementPtrInst * GEP)69   llvm::Value *computeGEPOffset(llvm::GetElementPtrInst *GEP) {
70     llvm::Value *Offset = nullptr;
71 
72     for (llvm::gep_type_iterator GTI = gep_type_begin(GEP),
73                                  GTE = gep_type_end(GEP);
74          GTI != GTE; ++GTI) {
75       if (llvm::StructType *STy = llvm::dyn_cast<llvm::StructType>(*GTI)) {
76         llvm::ConstantInt *OpC = llvm::dyn_cast<llvm::ConstantInt>(GTI.getOperand());
77         if (!OpC) {
78           ALOGE("Operand for struct type is not constant!");
79           bccAssert(false);
80           return nullptr;  // NOLINT, unreached
81         }
82 
83         // Offset = Offset + EltOffset for index into a struct
84         const llvm::StructLayout *SL = DL.getStructLayout(STy);
85         unsigned EltOffset = SL->getElementOffset(OpC->getZExtValue());
86         llvm::Value *Incr = llvm::ConstantInt::get(
87                                 llvm::Type::getInt32Ty(*Context), EltOffset);
88         Offset = incrementOffset(Offset, Incr, GEP);
89       } else {
90         // Offset = Offset + Index * EltSize for index into an array or a vector
91         llvm::Value *EltSize = llvm::ConstantInt::get(
92                                  llvm::Type::getInt32Ty(*Context),
93                                  DL.getTypeAllocSize(GTI.getIndexedType()));
94         llvm::Value *Incr = llvm::BinaryOperator::CreateMul(
95                                 GTI.getOperand() /* Index */,
96                                 EltSize, "", GEP);
97         Offset = incrementOffset(Offset, Incr, GEP);
98       }
99     }
100 
101     return Offset;
102   }
103 
translateGEP(llvm::GetElementPtrInst * GEP)104   void translateGEP(llvm::GetElementPtrInst *GEP) {
105     // cast GEP pointer operand to int8*
106     llvm::CastInst *Int8Ptr = llvm::CastInst::CreatePointerCast(
107                                   GEP->getPointerOperand(),
108                                   llvm::Type::getInt8PtrTy(*Context),
109                                   "to.int8ptr",
110                                   GEP);
111     llvm::Value *Indices[1] = {computeGEPOffset(GEP)};
112 
113     // index into the int8* based on the byte offset
114     llvm::GetElementPtrInst *Int8PtrGEP = llvm::GetElementPtrInst::Create(
115         llvm::Type::getInt8Ty(*Context), Int8Ptr, llvm::makeArrayRef(Indices),
116         "int8ptr.indexed", GEP);
117     Int8PtrGEP->setIsInBounds(GEP->isInBounds());
118 
119     // cast the indexed int8* back to the type of the original GEP
120     llvm::CastInst *OutCast = llvm::CastInst::CreatePointerCast(
121         Int8PtrGEP, GEP->getType(), "to.orig.geptype", GEP);
122 
123     GEP->replaceAllUsesWith(OutCast);
124   }
125 
126 public:
RSX86TranslateGEPPass()127   RSX86TranslateGEPPass()
128     : FunctionPass (ID), DL(X86_CUSTOM_DL_STRING) {
129   }
130 
getAnalysisUsage(llvm::AnalysisUsage & AU) const131   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
132     // This pass is run in isolation in a separate pass manager.  So setting
133     // AnalysisUsage is unnecessary.  Set just for completeness.
134     AU.setPreservesCFG();
135   }
136 
runOnFunction(llvm::Function & F)137   virtual bool runOnFunction(llvm::Function &F) override {
138     bool changed = false;
139     Context = &F.getParent()->getContext();
140 
141     // To avoid updating/deleting instructions while walking a BasicBlock's instructions,
142     // collect the GEPs that need to be translated and process them
143     // subsequently.
144     std::vector<llvm::GetElementPtrInst *> GEPsToHandle;
145 
146     for (auto &BB: F) {
147       for (auto &I: BB) {
148         if (auto *GEP = llvm::dyn_cast<llvm::GetElementPtrInst>(&I)) {
149           if (GEPIndexesStructType(GEP)) {
150             GEPsToHandle.push_back(GEP);
151           }
152         }
153       }
154     }
155 
156     for (auto *GEP: GEPsToHandle) {
157       // Translate GEPs and erase them
158       translateGEP(GEP);
159       changed = true;
160       GEP->eraseFromParent();
161     }
162 
163     return changed;
164   }
165 
getPassName() const166   virtual const char *getPassName() const override {
167     return "Translate GEPs on structs, intended for x86 target";
168   }
169 };
170 
171 }
172 
173 char RSX86TranslateGEPPass::ID = 0;
174 
175 namespace bcc {
176 
177 llvm::FunctionPass *
createRSX86TranslateGEPPass()178 createRSX86TranslateGEPPass() {
179   return new RSX86TranslateGEPPass();
180 }
181 
182 }
183