Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 1 | //===-- NVPTXInferAddressSpace.cpp - ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // CUDA C/C++ includes memory space designation as variable type qualifers (such |
| 11 | // as __global__ and __shared__). Knowing the space of a memory access allows |
| 12 | // CUDA compilers to emit faster PTX loads and stores. For example, a load from |
| 13 | // shared memory can be translated to `ld.shared` which is roughly 10% faster |
| 14 | // than a generic `ld` on an NVIDIA Tesla K40c. |
| 15 | // |
| 16 | // Unfortunately, type qualifiers only apply to variable declarations, so CUDA |
| 17 | // compilers must infer the memory space of an address expression from |
| 18 | // type-qualified variables. |
| 19 | // |
| 20 | // LLVM IR uses non-zero (so-called) specific address spaces to represent memory |
| 21 | // spaces (e.g. addrspace(3) means shared memory). The Clang frontend |
| 22 | // places only type-qualified variables in specific address spaces, and then |
| 23 | // conservatively `addrspacecast`s each type-qualified variable to addrspace(0) |
| 24 | // (so-called the generic address space) for other instructions to use. |
| 25 | // |
| 26 | // For example, the Clang translates the following CUDA code |
| 27 | // __shared__ float a[10]; |
| 28 | // float v = a[i]; |
| 29 | // to |
| 30 | // %0 = addrspacecast [10 x float] addrspace(3)* @a to [10 x float]* |
| 31 | // %1 = gep [10 x float], [10 x float]* %0, i64 0, i64 %i |
| 32 | // %v = load float, float* %1 ; emits ld.f32 |
| 33 | // @a is in addrspace(3) since it's type-qualified, but its use from %1 is |
| 34 | // redirected to %0 (the generic version of @a). |
| 35 | // |
| 36 | // The optimization implemented in this file propagates specific address spaces |
| 37 | // from type-qualified variable declarations to its users. For example, it |
| 38 | // optimizes the above IR to |
| 39 | // %1 = gep [10 x float] addrspace(3)* @a, i64 0, i64 %i |
| 40 | // %v = load float addrspace(3)* %1 ; emits ld.shared.f32 |
| 41 | // propagating the addrspace(3) from @a to %1. As the result, the NVPTX |
| 42 | // codegen is able to emit ld.shared.f32 for %v. |
| 43 | // |
| 44 | // Address space inference works in two steps. First, it uses a data-flow |
| 45 | // analysis to infer as many generic pointers as possible to point to only one |
| 46 | // specific address space. In the above example, it can prove that %1 only |
| 47 | // points to addrspace(3). This algorithm was published in |
| 48 | // CUDA: Compiling and optimizing for a GPU platform |
| 49 | // Chakrabarti, Grover, Aarts, Kong, Kudlur, Lin, Marathe, Murphy, Wang |
| 50 | // ICCS 2012 |
| 51 | // |
| 52 | // Then, address space inference replaces all refinable generic pointers with |
| 53 | // equivalent specific pointers. |
| 54 | // |
| 55 | // The major challenge of implementing this optimization is handling PHINodes, |
| 56 | // which may create loops in the data flow graph. This brings two complications. |
| 57 | // |
| 58 | // First, the data flow analysis in Step 1 needs to be circular. For example, |
| 59 | // %generic.input = addrspacecast float addrspace(3)* %input to float* |
| 60 | // loop: |
| 61 | // %y = phi [ %generic.input, %y2 ] |
| 62 | // %y2 = getelementptr %y, 1 |
| 63 | // %v = load %y2 |
| 64 | // br ..., label %loop, ... |
| 65 | // proving %y specific requires proving both %generic.input and %y2 specific, |
| 66 | // but proving %y2 specific circles back to %y. To address this complication, |
| 67 | // the data flow analysis operates on a lattice: |
| 68 | // uninitialized > specific address spaces > generic. |
| 69 | // All address expressions (our implementation only considers phi, bitcast, |
| 70 | // addrspacecast, and getelementptr) start with the uninitialized address space. |
| 71 | // The monotone transfer function moves the address space of a pointer down a |
| 72 | // lattice path from uninitialized to specific and then to generic. A join |
| 73 | // operation of two different specific address spaces pushes the expression down |
| 74 | // to the generic address space. The analysis completes once it reaches a fixed |
| 75 | // point. |
| 76 | // |
| 77 | // Second, IR rewriting in Step 2 also needs to be circular. For example, |
| 78 | // converting %y to addrspace(3) requires the compiler to know the converted |
| 79 | // %y2, but converting %y2 needs the converted %y. To address this complication, |
| 80 | // we break these cycles using "undef" placeholders. When converting an |
| 81 | // instruction `I` to a new address space, if its operand `Op` is not converted |
| 82 | // yet, we let `I` temporarily use `undef` and fix all the uses of undef later. |
| 83 | // For instance, our algorithm first converts %y to |
| 84 | // %y' = phi float addrspace(3)* [ %input, undef ] |
| 85 | // Then, it converts %y2 to |
| 86 | // %y2' = getelementptr %y', 1 |
| 87 | // Finally, it fixes the undef in %y' so that |
| 88 | // %y' = phi float addrspace(3)* [ %input, %y2' ] |
| 89 | // |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 90 | //===----------------------------------------------------------------------===// |
| 91 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 92 | #include "llvm/Transforms/Scalar.h" |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 93 | #include "llvm/ADT/DenseSet.h" |
| 94 | #include "llvm/ADT/Optional.h" |
| 95 | #include "llvm/ADT/SetVector.h" |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 96 | #include "llvm/Analysis/TargetTransformInfo.h" |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 97 | #include "llvm/IR/Function.h" |
| 98 | #include "llvm/IR/InstIterator.h" |
| 99 | #include "llvm/IR/Instructions.h" |
| 100 | #include "llvm/IR/Operator.h" |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 101 | #include "llvm/Support/Debug.h" |
| 102 | #include "llvm/Support/raw_ostream.h" |
| 103 | #include "llvm/Transforms/Utils/Local.h" |
| 104 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 105 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 106 | #define DEBUG_TYPE "infer-address-spaces" |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 107 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 108 | using namespace llvm; |
| 109 | |
| 110 | namespace { |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 111 | static const unsigned UninitializedAddressSpace = ~0u; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 112 | |
| 113 | using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>; |
| 114 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 115 | /// \brief InferAddressSpaces |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 116 | class InferAddressSpaces : public FunctionPass { |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 117 | /// Target specific address space which uses of should be replaced if |
| 118 | /// possible. |
| 119 | unsigned FlatAddrSpace; |
| 120 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 121 | public: |
| 122 | static char ID; |
| 123 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 124 | InferAddressSpaces() : FunctionPass(ID) {} |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 125 | |
Matt Arsenault | 32b9600 | 2017-01-27 17:30:39 +0000 | [diff] [blame] | 126 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 127 | AU.setPreservesCFG(); |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 128 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Matt Arsenault | 32b9600 | 2017-01-27 17:30:39 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 131 | bool runOnFunction(Function &F) override; |
| 132 | |
| 133 | private: |
| 134 | // Returns the new address space of V if updated; otherwise, returns None. |
| 135 | Optional<unsigned> |
| 136 | updateAddressSpace(const Value &V, |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 137 | const ValueToAddrSpaceMapTy &InferredAddrSpace) const; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 138 | |
| 139 | // Tries to infer the specific address space of each address expression in |
| 140 | // Postorder. |
| 141 | void inferAddressSpaces(const std::vector<Value *> &Postorder, |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 142 | ValueToAddrSpaceMapTy *InferredAddrSpace) const; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 143 | |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 144 | bool isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const; |
| 145 | |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 146 | // Changes the flat address expressions in function F to point to specific |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 147 | // address spaces if InferredAddrSpace says so. Postorder is the postorder of |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 148 | // all flat expressions in the use-def graph of function F. |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 149 | bool |
| 150 | rewriteWithNewAddressSpaces(const std::vector<Value *> &Postorder, |
| 151 | const ValueToAddrSpaceMapTy &InferredAddrSpace, |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 152 | Function *F) const; |
| 153 | |
| 154 | void appendsFlatAddressExpressionToPostorderStack( |
| 155 | Value *V, std::vector<std::pair<Value *, bool>> *PostorderStack, |
| 156 | DenseSet<Value *> *Visited) const; |
| 157 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 158 | bool rewriteIntrinsicOperands(IntrinsicInst *II, |
| 159 | Value *OldV, Value *NewV) const; |
| 160 | void collectRewritableIntrinsicOperands( |
| 161 | IntrinsicInst *II, |
| 162 | std::vector<std::pair<Value *, bool>> *PostorderStack, |
| 163 | DenseSet<Value *> *Visited) const; |
| 164 | |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 165 | std::vector<Value *> collectFlatAddressExpressions(Function &F) const; |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 166 | |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 167 | Value *cloneValueWithNewAddressSpace( |
| 168 | Value *V, unsigned NewAddrSpace, |
| 169 | const ValueToValueMapTy &ValueWithNewAddrSpace, |
| 170 | SmallVectorImpl<const Use *> *UndefUsesToFix) const; |
| 171 | unsigned joinAddressSpaces(unsigned AS1, unsigned AS2) const; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 172 | }; |
| 173 | } // end anonymous namespace |
| 174 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 175 | char InferAddressSpaces::ID = 0; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 176 | |
| 177 | namespace llvm { |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 178 | void initializeInferAddressSpacesPass(PassRegistry &); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 179 | } |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 180 | |
| 181 | INITIALIZE_PASS(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces", |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 182 | false, false) |
| 183 | |
| 184 | // Returns true if V is an address expression. |
| 185 | // TODO: Currently, we consider only phi, bitcast, addrspacecast, and |
| 186 | // getelementptr operators. |
| 187 | static bool isAddressExpression(const Value &V) { |
| 188 | if (!isa<Operator>(V)) |
| 189 | return false; |
| 190 | |
| 191 | switch (cast<Operator>(V).getOpcode()) { |
| 192 | case Instruction::PHI: |
| 193 | case Instruction::BitCast: |
| 194 | case Instruction::AddrSpaceCast: |
| 195 | case Instruction::GetElementPtr: |
Matt Arsenault | bdd59e6 | 2017-02-01 00:08:53 +0000 | [diff] [blame] | 196 | case Instruction::Select: |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 197 | return true; |
| 198 | default: |
| 199 | return false; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Returns the pointer operands of V. |
| 204 | // |
| 205 | // Precondition: V is an address expression. |
| 206 | static SmallVector<Value *, 2> getPointerOperands(const Value &V) { |
| 207 | assert(isAddressExpression(V)); |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 208 | const Operator &Op = cast<Operator>(V); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 209 | switch (Op.getOpcode()) { |
| 210 | case Instruction::PHI: { |
| 211 | auto IncomingValues = cast<PHINode>(Op).incoming_values(); |
| 212 | return SmallVector<Value *, 2>(IncomingValues.begin(), |
| 213 | IncomingValues.end()); |
| 214 | } |
| 215 | case Instruction::BitCast: |
| 216 | case Instruction::AddrSpaceCast: |
| 217 | case Instruction::GetElementPtr: |
| 218 | return {Op.getOperand(0)}; |
Matt Arsenault | bdd59e6 | 2017-02-01 00:08:53 +0000 | [diff] [blame] | 219 | case Instruction::Select: |
| 220 | return {Op.getOperand(1), Op.getOperand(2)}; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 221 | default: |
| 222 | llvm_unreachable("Unexpected instruction type."); |
| 223 | } |
| 224 | } |
| 225 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 226 | // TODO: Move logic to TTI? |
| 227 | bool InferAddressSpaces::rewriteIntrinsicOperands(IntrinsicInst *II, |
| 228 | Value *OldV, |
| 229 | Value *NewV) const { |
| 230 | Module *M = II->getParent()->getParent()->getParent(); |
| 231 | |
| 232 | switch (II->getIntrinsicID()) { |
| 233 | case Intrinsic::objectsize: |
| 234 | case Intrinsic::amdgcn_atomic_inc: |
| 235 | case Intrinsic::amdgcn_atomic_dec: { |
| 236 | Type *DestTy = II->getType(); |
| 237 | Type *SrcTy = NewV->getType(); |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 238 | Function *NewDecl = |
| 239 | Intrinsic::getDeclaration(M, II->getIntrinsicID(), {DestTy, SrcTy}); |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 240 | II->setArgOperand(0, NewV); |
| 241 | II->setCalledFunction(NewDecl); |
| 242 | return true; |
| 243 | } |
| 244 | default: |
| 245 | return false; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // TODO: Move logic to TTI? |
| 250 | void InferAddressSpaces::collectRewritableIntrinsicOperands( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 251 | IntrinsicInst *II, std::vector<std::pair<Value *, bool>> *PostorderStack, |
| 252 | DenseSet<Value *> *Visited) const { |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 253 | switch (II->getIntrinsicID()) { |
| 254 | case Intrinsic::objectsize: |
| 255 | case Intrinsic::amdgcn_atomic_inc: |
| 256 | case Intrinsic::amdgcn_atomic_dec: |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 257 | appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0), |
| 258 | PostorderStack, Visited); |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 259 | break; |
| 260 | default: |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Returns all flat address expressions in function F. The elements are |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 266 | // If V is an unvisited flat address expression, appends V to PostorderStack |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 267 | // and marks it as visited. |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 268 | void InferAddressSpaces::appendsFlatAddressExpressionToPostorderStack( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 269 | Value *V, std::vector<std::pair<Value *, bool>> *PostorderStack, |
| 270 | DenseSet<Value *> *Visited) const { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 271 | assert(V->getType()->isPointerTy()); |
| 272 | if (isAddressExpression(*V) && |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 273 | V->getType()->getPointerAddressSpace() == FlatAddrSpace) { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 274 | if (Visited->insert(V).second) |
| 275 | PostorderStack->push_back(std::make_pair(V, false)); |
| 276 | } |
| 277 | } |
| 278 | |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 279 | // Returns all flat address expressions in function F. The elements are ordered |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 280 | // ordered in postorder. |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 281 | std::vector<Value *> |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 282 | InferAddressSpaces::collectFlatAddressExpressions(Function &F) const { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 283 | // This function implements a non-recursive postorder traversal of a partial |
| 284 | // use-def graph of function F. |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 285 | std::vector<std::pair<Value *, bool>> PostorderStack; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 286 | // The set of visited expressions. |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 287 | DenseSet<Value *> Visited; |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 288 | |
| 289 | auto PushPtrOperand = [&](Value *Ptr) { |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 290 | appendsFlatAddressExpressionToPostorderStack(Ptr, &PostorderStack, |
| 291 | &Visited); |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 294 | // We only explore address expressions that are reachable from loads and |
| 295 | // stores for now because we aim at generating faster loads and stores. |
| 296 | for (Instruction &I : instructions(F)) { |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 297 | if (auto *LI = dyn_cast<LoadInst>(&I)) |
| 298 | PushPtrOperand(LI->getPointerOperand()); |
| 299 | else if (auto *SI = dyn_cast<StoreInst>(&I)) |
| 300 | PushPtrOperand(SI->getPointerOperand()); |
| 301 | else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I)) |
| 302 | PushPtrOperand(RMW->getPointerOperand()); |
| 303 | else if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(&I)) |
| 304 | PushPtrOperand(CmpX->getPointerOperand()); |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 305 | else if (auto *MI = dyn_cast<MemIntrinsic>(&I)) { |
| 306 | // For memset/memcpy/memmove, any pointer operand can be replaced. |
| 307 | PushPtrOperand(MI->getRawDest()); |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 308 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 309 | // Handle 2nd operand for memcpy/memmove. |
| 310 | if (auto *MTI = dyn_cast<MemTransferInst>(MI)) |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 311 | PushPtrOperand(MTI->getRawSource()); |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 312 | } else if (auto *II = dyn_cast<IntrinsicInst>(&I)) |
| 313 | collectRewritableIntrinsicOperands(II, &PostorderStack, &Visited); |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 314 | else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(&I)) { |
| 315 | // FIXME: Handle vectors of pointers |
| 316 | if (Cmp->getOperand(0)->getType()->isPointerTy()) { |
| 317 | PushPtrOperand(Cmp->getOperand(0)); |
| 318 | PushPtrOperand(Cmp->getOperand(1)); |
| 319 | } |
| 320 | } |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | std::vector<Value *> Postorder; // The resultant postorder. |
| 324 | while (!PostorderStack.empty()) { |
| 325 | // If the operands of the expression on the top are already explored, |
| 326 | // adds that expression to the resultant postorder. |
| 327 | if (PostorderStack.back().second) { |
| 328 | Postorder.push_back(PostorderStack.back().first); |
| 329 | PostorderStack.pop_back(); |
| 330 | continue; |
| 331 | } |
| 332 | // Otherwise, adds its operands to the stack and explores them. |
| 333 | PostorderStack.back().second = true; |
| 334 | for (Value *PtrOperand : getPointerOperands(*PostorderStack.back().first)) { |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 335 | appendsFlatAddressExpressionToPostorderStack(PtrOperand, &PostorderStack, |
| 336 | &Visited); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | return Postorder; |
| 340 | } |
| 341 | |
| 342 | // A helper function for cloneInstructionWithNewAddressSpace. Returns the clone |
| 343 | // of OperandUse.get() in the new address space. If the clone is not ready yet, |
| 344 | // returns an undef in the new address space as a placeholder. |
| 345 | static Value *operandWithNewAddressSpaceOrCreateUndef( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 346 | const Use &OperandUse, unsigned NewAddrSpace, |
| 347 | const ValueToValueMapTy &ValueWithNewAddrSpace, |
| 348 | SmallVectorImpl<const Use *> *UndefUsesToFix) { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 349 | Value *Operand = OperandUse.get(); |
Matt Arsenault | 3008360 | 2017-02-02 03:37:22 +0000 | [diff] [blame] | 350 | |
| 351 | Type *NewPtrTy = |
| 352 | Operand->getType()->getPointerElementType()->getPointerTo(NewAddrSpace); |
| 353 | |
| 354 | if (Constant *C = dyn_cast<Constant>(Operand)) |
| 355 | return ConstantExpr::getAddrSpaceCast(C, NewPtrTy); |
| 356 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 357 | if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) |
| 358 | return NewOperand; |
| 359 | |
| 360 | UndefUsesToFix->push_back(&OperandUse); |
Matt Arsenault | 3008360 | 2017-02-02 03:37:22 +0000 | [diff] [blame] | 361 | return UndefValue::get(NewPtrTy); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Returns a clone of `I` with its operands converted to those specified in |
| 365 | // ValueWithNewAddrSpace. Due to potential cycles in the data flow graph, an |
| 366 | // operand whose address space needs to be modified might not exist in |
| 367 | // ValueWithNewAddrSpace. In that case, uses undef as a placeholder operand and |
| 368 | // adds that operand use to UndefUsesToFix so that caller can fix them later. |
| 369 | // |
| 370 | // Note that we do not necessarily clone `I`, e.g., if it is an addrspacecast |
| 371 | // from a pointer whose type already matches. Therefore, this function returns a |
| 372 | // Value* instead of an Instruction*. |
| 373 | static Value *cloneInstructionWithNewAddressSpace( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 374 | Instruction *I, unsigned NewAddrSpace, |
| 375 | const ValueToValueMapTy &ValueWithNewAddrSpace, |
| 376 | SmallVectorImpl<const Use *> *UndefUsesToFix) { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 377 | Type *NewPtrType = |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 378 | I->getType()->getPointerElementType()->getPointerTo(NewAddrSpace); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 379 | |
| 380 | if (I->getOpcode() == Instruction::AddrSpaceCast) { |
| 381 | Value *Src = I->getOperand(0); |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 382 | // Because `I` is flat, the source address space must be specific. |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 383 | // Therefore, the inferred address space must be the source space, according |
| 384 | // to our algorithm. |
| 385 | assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace); |
| 386 | if (Src->getType() != NewPtrType) |
| 387 | return new BitCastInst(Src, NewPtrType); |
| 388 | return Src; |
| 389 | } |
| 390 | |
| 391 | // Computes the converted pointer operands. |
| 392 | SmallVector<Value *, 4> NewPointerOperands; |
| 393 | for (const Use &OperandUse : I->operands()) { |
| 394 | if (!OperandUse.get()->getType()->isPointerTy()) |
| 395 | NewPointerOperands.push_back(nullptr); |
| 396 | else |
| 397 | NewPointerOperands.push_back(operandWithNewAddressSpaceOrCreateUndef( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 398 | OperandUse, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix)); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | switch (I->getOpcode()) { |
| 402 | case Instruction::BitCast: |
| 403 | return new BitCastInst(NewPointerOperands[0], NewPtrType); |
| 404 | case Instruction::PHI: { |
| 405 | assert(I->getType()->isPointerTy()); |
| 406 | PHINode *PHI = cast<PHINode>(I); |
| 407 | PHINode *NewPHI = PHINode::Create(NewPtrType, PHI->getNumIncomingValues()); |
| 408 | for (unsigned Index = 0; Index < PHI->getNumIncomingValues(); ++Index) { |
| 409 | unsigned OperandNo = PHINode::getOperandNumForIncomingValue(Index); |
| 410 | NewPHI->addIncoming(NewPointerOperands[OperandNo], |
| 411 | PHI->getIncomingBlock(Index)); |
| 412 | } |
| 413 | return NewPHI; |
| 414 | } |
| 415 | case Instruction::GetElementPtr: { |
| 416 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); |
| 417 | GetElementPtrInst *NewGEP = GetElementPtrInst::Create( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 418 | GEP->getSourceElementType(), NewPointerOperands[0], |
| 419 | SmallVector<Value *, 4>(GEP->idx_begin(), GEP->idx_end())); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 420 | NewGEP->setIsInBounds(GEP->isInBounds()); |
| 421 | return NewGEP; |
| 422 | } |
Matt Arsenault | bdd59e6 | 2017-02-01 00:08:53 +0000 | [diff] [blame] | 423 | case Instruction::Select: { |
| 424 | assert(I->getType()->isPointerTy()); |
| 425 | return SelectInst::Create(I->getOperand(0), NewPointerOperands[1], |
| 426 | NewPointerOperands[2], "", nullptr, I); |
| 427 | } |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 428 | default: |
| 429 | llvm_unreachable("Unexpected opcode"); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // Similar to cloneInstructionWithNewAddressSpace, returns a clone of the |
| 434 | // constant expression `CE` with its operands replaced as specified in |
| 435 | // ValueWithNewAddrSpace. |
| 436 | static Value *cloneConstantExprWithNewAddressSpace( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 437 | ConstantExpr *CE, unsigned NewAddrSpace, |
| 438 | const ValueToValueMapTy &ValueWithNewAddrSpace) { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 439 | Type *TargetType = |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 440 | CE->getType()->getPointerElementType()->getPointerTo(NewAddrSpace); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 441 | |
| 442 | if (CE->getOpcode() == Instruction::AddrSpaceCast) { |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 443 | // Because CE is flat, the source address space must be specific. |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 444 | // Therefore, the inferred address space must be the source space according |
| 445 | // to our algorithm. |
| 446 | assert(CE->getOperand(0)->getType()->getPointerAddressSpace() == |
| 447 | NewAddrSpace); |
| 448 | return ConstantExpr::getBitCast(CE->getOperand(0), TargetType); |
| 449 | } |
| 450 | |
Matt Arsenault | c18b677 | 2017-02-17 00:32:19 +0000 | [diff] [blame] | 451 | if (CE->getOpcode() == Instruction::BitCast) { |
| 452 | if (Value *NewOperand = ValueWithNewAddrSpace.lookup(CE->getOperand(0))) |
| 453 | return ConstantExpr::getBitCast(cast<Constant>(NewOperand), TargetType); |
| 454 | return ConstantExpr::getAddrSpaceCast(CE, TargetType); |
| 455 | } |
| 456 | |
Matt Arsenault | 3008360 | 2017-02-02 03:37:22 +0000 | [diff] [blame] | 457 | if (CE->getOpcode() == Instruction::Select) { |
| 458 | Constant *Src0 = CE->getOperand(1); |
| 459 | Constant *Src1 = CE->getOperand(2); |
| 460 | if (Src0->getType()->getPointerAddressSpace() == |
| 461 | Src1->getType()->getPointerAddressSpace()) { |
| 462 | |
| 463 | return ConstantExpr::getSelect( |
| 464 | CE->getOperand(0), ConstantExpr::getAddrSpaceCast(Src0, TargetType), |
| 465 | ConstantExpr::getAddrSpaceCast(Src1, TargetType)); |
| 466 | } |
| 467 | } |
| 468 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 469 | // Computes the operands of the new constant expression. |
| 470 | SmallVector<Constant *, 4> NewOperands; |
| 471 | for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) { |
| 472 | Constant *Operand = CE->getOperand(Index); |
| 473 | // If the address space of `Operand` needs to be modified, the new operand |
| 474 | // with the new address space should already be in ValueWithNewAddrSpace |
| 475 | // because (1) the constant expressions we consider (i.e. addrspacecast, |
| 476 | // bitcast, and getelementptr) do not incur cycles in the data flow graph |
| 477 | // and (2) this function is called on constant expressions in postorder. |
| 478 | if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) { |
| 479 | NewOperands.push_back(cast<Constant>(NewOperand)); |
| 480 | } else { |
| 481 | // Otherwise, reuses the old operand. |
| 482 | NewOperands.push_back(Operand); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 487 | // Needs to specify the source type while constructing a getelementptr |
| 488 | // constant expression. |
| 489 | return CE->getWithOperands( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 490 | NewOperands, TargetType, /*OnlyIfReduced=*/false, |
| 491 | NewOperands[0]->getType()->getPointerElementType()); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return CE->getWithOperands(NewOperands, TargetType); |
| 495 | } |
| 496 | |
| 497 | // Returns a clone of the value `V`, with its operands replaced as specified in |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 498 | // ValueWithNewAddrSpace. This function is called on every flat address |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 499 | // expression whose address space needs to be modified, in postorder. |
| 500 | // |
| 501 | // See cloneInstructionWithNewAddressSpace for the meaning of UndefUsesToFix. |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 502 | Value *InferAddressSpaces::cloneValueWithNewAddressSpace( |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 503 | Value *V, unsigned NewAddrSpace, |
| 504 | const ValueToValueMapTy &ValueWithNewAddrSpace, |
| 505 | SmallVectorImpl<const Use *> *UndefUsesToFix) const { |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 506 | // All values in Postorder are flat address expressions. |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 507 | assert(isAddressExpression(*V) && |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 508 | V->getType()->getPointerAddressSpace() == FlatAddrSpace); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 509 | |
| 510 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 511 | Value *NewV = cloneInstructionWithNewAddressSpace( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 512 | I, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 513 | if (Instruction *NewI = dyn_cast<Instruction>(NewV)) { |
| 514 | if (NewI->getParent() == nullptr) { |
| 515 | NewI->insertBefore(I); |
| 516 | NewI->takeName(I); |
| 517 | } |
| 518 | } |
| 519 | return NewV; |
| 520 | } |
| 521 | |
| 522 | return cloneConstantExprWithNewAddressSpace( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 523 | cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Defines the join operation on the address space lattice (see the file header |
| 527 | // comments). |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 528 | unsigned InferAddressSpaces::joinAddressSpaces(unsigned AS1, |
| 529 | unsigned AS2) const { |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 530 | if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace) |
| 531 | return FlatAddrSpace; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 532 | |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 533 | if (AS1 == UninitializedAddressSpace) |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 534 | return AS2; |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 535 | if (AS2 == UninitializedAddressSpace) |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 536 | return AS1; |
| 537 | |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 538 | // The join of two different specific address spaces is flat. |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 539 | return (AS1 == AS2) ? AS1 : FlatAddrSpace; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 542 | bool InferAddressSpaces::runOnFunction(Function &F) { |
Andrew Kaylor | 87b10dd | 2016-04-26 23:44:31 +0000 | [diff] [blame] | 543 | if (skipFunction(F)) |
| 544 | return false; |
| 545 | |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 546 | const TargetTransformInfo &TTI = |
| 547 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 548 | FlatAddrSpace = TTI.getFlatAddressSpace(); |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 549 | if (FlatAddrSpace == UninitializedAddressSpace) |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 550 | return false; |
| 551 | |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 552 | // Collects all flat address expressions in postorder. |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 553 | std::vector<Value *> Postorder = collectFlatAddressExpressions(F); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 554 | |
| 555 | // Runs a data-flow analysis to refine the address spaces of every expression |
| 556 | // in Postorder. |
| 557 | ValueToAddrSpaceMapTy InferredAddrSpace; |
| 558 | inferAddressSpaces(Postorder, &InferredAddrSpace); |
| 559 | |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 560 | // Changes the address spaces of the flat address expressions who are inferred |
| 561 | // to point to a specific address space. |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 562 | return rewriteWithNewAddressSpaces(Postorder, InferredAddrSpace, &F); |
| 563 | } |
| 564 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 565 | void InferAddressSpaces::inferAddressSpaces( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 566 | const std::vector<Value *> &Postorder, |
| 567 | ValueToAddrSpaceMapTy *InferredAddrSpace) const { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 568 | SetVector<Value *> Worklist(Postorder.begin(), Postorder.end()); |
| 569 | // Initially, all expressions are in the uninitialized address space. |
| 570 | for (Value *V : Postorder) |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 571 | (*InferredAddrSpace)[V] = UninitializedAddressSpace; |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 572 | |
| 573 | while (!Worklist.empty()) { |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 574 | Value *V = Worklist.pop_back_val(); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 575 | |
| 576 | // Tries to update the address space of the stack top according to the |
| 577 | // address spaces of its operands. |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 578 | DEBUG(dbgs() << "Updating the address space of\n " << *V << '\n'); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 579 | Optional<unsigned> NewAS = updateAddressSpace(*V, *InferredAddrSpace); |
| 580 | if (!NewAS.hasValue()) |
| 581 | continue; |
| 582 | // If any updates are made, grabs its users to the worklist because |
| 583 | // their address spaces can also be possibly updated. |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 584 | DEBUG(dbgs() << " to " << NewAS.getValue() << '\n'); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 585 | (*InferredAddrSpace)[V] = NewAS.getValue(); |
| 586 | |
| 587 | for (Value *User : V->users()) { |
| 588 | // Skip if User is already in the worklist. |
| 589 | if (Worklist.count(User)) |
| 590 | continue; |
| 591 | |
| 592 | auto Pos = InferredAddrSpace->find(User); |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 593 | // Our algorithm only updates the address spaces of flat address |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 594 | // expressions, which are those in InferredAddrSpace. |
| 595 | if (Pos == InferredAddrSpace->end()) |
| 596 | continue; |
| 597 | |
| 598 | // Function updateAddressSpace moves the address space down a lattice |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 599 | // path. Therefore, nothing to do if User is already inferred as flat (the |
| 600 | // bottom element in the lattice). |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 601 | if (Pos->second == FlatAddrSpace) |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 602 | continue; |
| 603 | |
| 604 | Worklist.insert(User); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 609 | Optional<unsigned> InferAddressSpaces::updateAddressSpace( |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 610 | const Value &V, const ValueToAddrSpaceMapTy &InferredAddrSpace) const { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 611 | assert(InferredAddrSpace.count(&V)); |
| 612 | |
| 613 | // The new inferred address space equals the join of the address spaces |
| 614 | // of all its pointer operands. |
Matt Arsenault | 973c4ae | 2017-01-31 02:17:41 +0000 | [diff] [blame] | 615 | unsigned NewAS = UninitializedAddressSpace; |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 616 | |
Matt Arsenault | 3008360 | 2017-02-02 03:37:22 +0000 | [diff] [blame] | 617 | const Operator &Op = cast<Operator>(V); |
| 618 | if (Op.getOpcode() == Instruction::Select) { |
| 619 | Value *Src0 = Op.getOperand(1); |
| 620 | Value *Src1 = Op.getOperand(2); |
| 621 | |
| 622 | auto I = InferredAddrSpace.find(Src0); |
| 623 | unsigned Src0AS = (I != InferredAddrSpace.end()) ? |
| 624 | I->second : Src0->getType()->getPointerAddressSpace(); |
| 625 | |
| 626 | auto J = InferredAddrSpace.find(Src1); |
| 627 | unsigned Src1AS = (J != InferredAddrSpace.end()) ? |
| 628 | J->second : Src1->getType()->getPointerAddressSpace(); |
| 629 | |
| 630 | auto *C0 = dyn_cast<Constant>(Src0); |
| 631 | auto *C1 = dyn_cast<Constant>(Src1); |
| 632 | |
| 633 | // If one of the inputs is a constant, we may be able to do a constant |
| 634 | // addrspacecast of it. Defer inferring the address space until the input |
| 635 | // address space is known. |
| 636 | if ((C1 && Src0AS == UninitializedAddressSpace) || |
| 637 | (C0 && Src1AS == UninitializedAddressSpace)) |
| 638 | return None; |
| 639 | |
| 640 | if (C0 && isSafeToCastConstAddrSpace(C0, Src1AS)) |
| 641 | NewAS = Src1AS; |
| 642 | else if (C1 && isSafeToCastConstAddrSpace(C1, Src0AS)) |
| 643 | NewAS = Src0AS; |
| 644 | else |
| 645 | NewAS = joinAddressSpaces(Src0AS, Src1AS); |
| 646 | } else { |
| 647 | for (Value *PtrOperand : getPointerOperands(V)) { |
| 648 | auto I = InferredAddrSpace.find(PtrOperand); |
| 649 | unsigned OperandAS = I != InferredAddrSpace.end() ? |
| 650 | I->second : PtrOperand->getType()->getPointerAddressSpace(); |
| 651 | |
| 652 | // join(flat, *) = flat. So we can break if NewAS is already flat. |
| 653 | NewAS = joinAddressSpaces(NewAS, OperandAS); |
| 654 | if (NewAS == FlatAddrSpace) |
| 655 | break; |
| 656 | } |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | unsigned OldAS = InferredAddrSpace.lookup(&V); |
Matt Arsenault | 42b6478 | 2017-01-30 23:02:12 +0000 | [diff] [blame] | 660 | assert(OldAS != FlatAddrSpace); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 661 | if (OldAS == NewAS) |
| 662 | return None; |
| 663 | return NewAS; |
| 664 | } |
| 665 | |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 666 | /// \p returns true if \p U is the pointer operand of a memory instruction with |
| 667 | /// a single pointer operand that can have its address space changed by simply |
| 668 | /// mutating the use to a new value. |
| 669 | static bool isSimplePointerUseValidToReplace(Use &U) { |
| 670 | User *Inst = U.getUser(); |
| 671 | unsigned OpNo = U.getOperandNo(); |
| 672 | |
| 673 | if (auto *LI = dyn_cast<LoadInst>(Inst)) |
| 674 | return OpNo == LoadInst::getPointerOperandIndex() && !LI->isVolatile(); |
| 675 | |
| 676 | if (auto *SI = dyn_cast<StoreInst>(Inst)) |
| 677 | return OpNo == StoreInst::getPointerOperandIndex() && !SI->isVolatile(); |
| 678 | |
| 679 | if (auto *RMW = dyn_cast<AtomicRMWInst>(Inst)) |
| 680 | return OpNo == AtomicRMWInst::getPointerOperandIndex() && !RMW->isVolatile(); |
| 681 | |
| 682 | if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst)) { |
| 683 | return OpNo == AtomicCmpXchgInst::getPointerOperandIndex() && |
| 684 | !CmpX->isVolatile(); |
| 685 | } |
| 686 | |
| 687 | return false; |
| 688 | } |
| 689 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 690 | /// Update memory intrinsic uses that require more complex processing than |
| 691 | /// simple memory instructions. Thse require re-mangling and may have multiple |
| 692 | /// pointer operands. |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 693 | static bool handleMemIntrinsicPtrUse(MemIntrinsic *MI, Value *OldV, |
| 694 | Value *NewV) { |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 695 | IRBuilder<> B(MI); |
| 696 | MDNode *TBAA = MI->getMetadata(LLVMContext::MD_tbaa); |
| 697 | MDNode *ScopeMD = MI->getMetadata(LLVMContext::MD_alias_scope); |
| 698 | MDNode *NoAliasMD = MI->getMetadata(LLVMContext::MD_noalias); |
| 699 | |
| 700 | if (auto *MSI = dyn_cast<MemSetInst>(MI)) { |
| 701 | B.CreateMemSet(NewV, MSI->getValue(), |
| 702 | MSI->getLength(), MSI->getAlignment(), |
| 703 | false, // isVolatile |
| 704 | TBAA, ScopeMD, NoAliasMD); |
| 705 | } else if (auto *MTI = dyn_cast<MemTransferInst>(MI)) { |
| 706 | Value *Src = MTI->getRawSource(); |
| 707 | Value *Dest = MTI->getRawDest(); |
| 708 | |
| 709 | // Be careful in case this is a self-to-self copy. |
| 710 | if (Src == OldV) |
| 711 | Src = NewV; |
| 712 | |
| 713 | if (Dest == OldV) |
| 714 | Dest = NewV; |
| 715 | |
| 716 | if (isa<MemCpyInst>(MTI)) { |
| 717 | MDNode *TBAAStruct = MTI->getMetadata(LLVMContext::MD_tbaa_struct); |
| 718 | B.CreateMemCpy(Dest, Src, MTI->getLength(), |
| 719 | MTI->getAlignment(), |
| 720 | false, // isVolatile |
| 721 | TBAA, TBAAStruct, ScopeMD, NoAliasMD); |
| 722 | } else { |
| 723 | assert(isa<MemMoveInst>(MTI)); |
| 724 | B.CreateMemMove(Dest, Src, MTI->getLength(), |
| 725 | MTI->getAlignment(), |
| 726 | false, // isVolatile |
| 727 | TBAA, ScopeMD, NoAliasMD); |
| 728 | } |
| 729 | } else |
| 730 | llvm_unreachable("unhandled MemIntrinsic"); |
| 731 | |
| 732 | MI->eraseFromParent(); |
| 733 | return true; |
| 734 | } |
| 735 | |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 736 | // \p returns true if it is OK to change the address space of constant \p C with |
| 737 | // a ConstantExpr addrspacecast. |
| 738 | bool InferAddressSpaces::isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const { |
Matt Arsenault | 3008360 | 2017-02-02 03:37:22 +0000 | [diff] [blame] | 739 | assert(NewAS != UninitializedAddressSpace); |
| 740 | |
Matt Arsenault | 2a46d81 | 2017-01-31 23:48:40 +0000 | [diff] [blame] | 741 | unsigned SrcAS = C->getType()->getPointerAddressSpace(); |
| 742 | if (SrcAS == NewAS || isa<UndefValue>(C)) |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 743 | return true; |
| 744 | |
Matt Arsenault | 2a46d81 | 2017-01-31 23:48:40 +0000 | [diff] [blame] | 745 | // Prevent illegal casts between different non-flat address spaces. |
| 746 | if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace) |
| 747 | return false; |
| 748 | |
| 749 | if (isa<ConstantPointerNull>(C)) |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 750 | return true; |
| 751 | |
| 752 | if (auto *Op = dyn_cast<Operator>(C)) { |
| 753 | // If we already have a constant addrspacecast, it should be safe to cast it |
| 754 | // off. |
| 755 | if (Op->getOpcode() == Instruction::AddrSpaceCast) |
| 756 | return isSafeToCastConstAddrSpace(cast<Constant>(Op->getOperand(0)), NewAS); |
| 757 | |
| 758 | if (Op->getOpcode() == Instruction::IntToPtr && |
| 759 | Op->getType()->getPointerAddressSpace() == FlatAddrSpace) |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | return false; |
| 764 | } |
| 765 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 766 | static Value::use_iterator skipToNextUser(Value::use_iterator I, |
| 767 | Value::use_iterator End) { |
| 768 | User *CurUser = I->getUser(); |
| 769 | ++I; |
| 770 | |
| 771 | while (I != End && I->getUser() == CurUser) |
| 772 | ++I; |
| 773 | |
| 774 | return I; |
| 775 | } |
| 776 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 777 | bool InferAddressSpaces::rewriteWithNewAddressSpaces( |
| 778 | const std::vector<Value *> &Postorder, |
| 779 | const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 780 | // For each address expression to be modified, creates a clone of it with its |
| 781 | // pointer operands converted to the new address space. Since the pointer |
| 782 | // operands are converted, the clone is naturally in the new address space by |
| 783 | // construction. |
| 784 | ValueToValueMapTy ValueWithNewAddrSpace; |
| 785 | SmallVector<const Use *, 32> UndefUsesToFix; |
| 786 | for (Value* V : Postorder) { |
| 787 | unsigned NewAddrSpace = InferredAddrSpace.lookup(V); |
| 788 | if (V->getType()->getPointerAddressSpace() != NewAddrSpace) { |
| 789 | ValueWithNewAddrSpace[V] = cloneValueWithNewAddressSpace( |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 790 | V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | if (ValueWithNewAddrSpace.empty()) |
| 795 | return false; |
| 796 | |
| 797 | // Fixes all the undef uses generated by cloneInstructionWithNewAddressSpace. |
Matt Arsenault | db6e9e8 | 2017-02-02 00:28:25 +0000 | [diff] [blame] | 798 | for (const Use *UndefUse : UndefUsesToFix) { |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 799 | User *V = UndefUse->getUser(); |
| 800 | User *NewV = cast<User>(ValueWithNewAddrSpace.lookup(V)); |
| 801 | unsigned OperandNo = UndefUse->getOperandNo(); |
| 802 | assert(isa<UndefValue>(NewV->getOperand(OperandNo))); |
| 803 | NewV->setOperand(OperandNo, ValueWithNewAddrSpace.lookup(UndefUse->get())); |
| 804 | } |
| 805 | |
| 806 | // Replaces the uses of the old address expressions with the new ones. |
| 807 | for (Value *V : Postorder) { |
| 808 | Value *NewV = ValueWithNewAddrSpace.lookup(V); |
| 809 | if (NewV == nullptr) |
| 810 | continue; |
| 811 | |
Matt Arsenault | 9f432ec | 2017-01-30 23:27:11 +0000 | [diff] [blame] | 812 | DEBUG(dbgs() << "Replacing the uses of " << *V |
| 813 | << "\n with\n " << *NewV << '\n'); |
| 814 | |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 815 | Value::use_iterator I, E, Next; |
| 816 | for (I = V->use_begin(), E = V->use_end(); I != E; ) { |
| 817 | Use &U = *I; |
| 818 | |
| 819 | // Some users may see the same pointer operand in multiple operands. Skip |
| 820 | // to the next instruction. |
| 821 | I = skipToNextUser(I, E); |
| 822 | |
| 823 | if (isSimplePointerUseValidToReplace(U)) { |
Matt Arsenault | 6c907a9 | 2017-01-31 01:40:38 +0000 | [diff] [blame] | 824 | // If V is used as the pointer operand of a compatible memory operation, |
| 825 | // sets the pointer operand to NewV. This replacement does not change |
| 826 | // the element type, so the resultant load/store is still valid. |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 827 | U.set(NewV); |
| 828 | continue; |
| 829 | } |
| 830 | |
| 831 | User *CurUser = U.getUser(); |
| 832 | // Handle more complex cases like intrinsic that need to be remangled. |
| 833 | if (auto *MI = dyn_cast<MemIntrinsic>(CurUser)) { |
| 834 | if (!MI->isVolatile() && handleMemIntrinsicPtrUse(MI, V, NewV)) |
| 835 | continue; |
| 836 | } |
| 837 | |
| 838 | if (auto *II = dyn_cast<IntrinsicInst>(CurUser)) { |
| 839 | if (rewriteIntrinsicOperands(II, V, NewV)) |
| 840 | continue; |
| 841 | } |
| 842 | |
| 843 | if (isa<Instruction>(CurUser)) { |
Matt Arsenault | 72f259b | 2017-01-31 02:17:32 +0000 | [diff] [blame] | 844 | if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) { |
| 845 | // If we can infer that both pointers are in the same addrspace, |
| 846 | // transform e.g. |
| 847 | // %cmp = icmp eq float* %p, %q |
| 848 | // into |
| 849 | // %cmp = icmp eq float addrspace(3)* %new_p, %new_q |
| 850 | |
| 851 | unsigned NewAS = NewV->getType()->getPointerAddressSpace(); |
| 852 | int SrcIdx = U.getOperandNo(); |
| 853 | int OtherIdx = (SrcIdx == 0) ? 1 : 0; |
| 854 | Value *OtherSrc = Cmp->getOperand(OtherIdx); |
| 855 | |
| 856 | if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) { |
| 857 | if (OtherNewV->getType()->getPointerAddressSpace() == NewAS) { |
| 858 | Cmp->setOperand(OtherIdx, OtherNewV); |
| 859 | Cmp->setOperand(SrcIdx, NewV); |
| 860 | continue; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // Even if the type mismatches, we can cast the constant. |
| 865 | if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) { |
| 866 | if (isSafeToCastConstAddrSpace(KOtherSrc, NewAS)) { |
| 867 | Cmp->setOperand(SrcIdx, NewV); |
| 868 | Cmp->setOperand(OtherIdx, |
| 869 | ConstantExpr::getAddrSpaceCast(KOtherSrc, NewV->getType())); |
| 870 | continue; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 875 | // Otherwise, replaces the use with flat(NewV). |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 876 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 877 | BasicBlock::iterator InsertPos = std::next(I->getIterator()); |
| 878 | while (isa<PHINode>(InsertPos)) |
| 879 | ++InsertPos; |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 880 | U.set(new AddrSpaceCastInst(NewV, V->getType(), "", &*InsertPos)); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 881 | } else { |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 882 | U.set(ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV), |
| 883 | V->getType())); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
Matt Arsenault | 6d5a8d4 | 2017-01-31 01:56:57 +0000 | [diff] [blame] | 887 | |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 888 | if (V->use_empty()) |
| 889 | RecursivelyDeleteTriviallyDeadInstructions(V); |
| 890 | } |
| 891 | |
| 892 | return true; |
| 893 | } |
| 894 | |
Matt Arsenault | 850657a | 2017-01-31 01:10:58 +0000 | [diff] [blame] | 895 | FunctionPass *llvm::createInferAddressSpacesPass() { |
| 896 | return new InferAddressSpaces(); |
Jingyue Wu | 1375560 | 2016-03-20 20:59:20 +0000 | [diff] [blame] | 897 | } |