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