blob: fbbc09eb487f0008aaee3ed656bc1f33387b3879 [file] [log] [blame]
Eugene Zelenko57bd5a02017-10-27 01:09:08 +00001//===- InferAddressSpace.cpp - --------------------------------------------===//
Jingyue Wu13755602016-03-20 20:59:20 +00002//
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 Wu13755602016-03-20 20:59:20 +000090//===----------------------------------------------------------------------===//
91
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000092#include "llvm/ADT/ArrayRef.h"
93#include "llvm/ADT/DenseMap.h"
Jingyue Wu13755602016-03-20 20:59:20 +000094#include "llvm/ADT/DenseSet.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000095#include "llvm/ADT/None.h"
Jingyue Wu13755602016-03-20 20:59:20 +000096#include "llvm/ADT/Optional.h"
97#include "llvm/ADT/SetVector.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000098#include "llvm/ADT/SmallVector.h"
Matt Arsenault42b64782017-01-30 23:02:12 +000099#include "llvm/Analysis/TargetTransformInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +0000100#include "llvm/Transforms/Utils/Local.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000101#include "llvm/IR/BasicBlock.h"
102#include "llvm/IR/Constant.h"
103#include "llvm/IR/Constants.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000104#include "llvm/IR/Function.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +0000105#include "llvm/IR/IRBuilder.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000106#include "llvm/IR/InstIterator.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000107#include "llvm/IR/Instruction.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000108#include "llvm/IR/Instructions.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +0000109#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000110#include "llvm/IR/Intrinsics.h"
111#include "llvm/IR/LLVMContext.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000112#include "llvm/IR/Operator.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000113#include "llvm/IR/Type.h"
114#include "llvm/IR/Use.h"
115#include "llvm/IR/User.h"
116#include "llvm/IR/Value.h"
117#include "llvm/IR/ValueHandle.h"
118#include "llvm/Pass.h"
119#include "llvm/Support/Casting.h"
120#include "llvm/Support/Compiler.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000121#include "llvm/Support/Debug.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000122#include "llvm/Support/ErrorHandling.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000123#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +0000124#include "llvm/Transforms/Scalar.h"
Jingyue Wu13755602016-03-20 20:59:20 +0000125#include "llvm/Transforms/Utils/ValueMapper.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000126#include <cassert>
127#include <iterator>
128#include <limits>
129#include <utility>
130#include <vector>
Jingyue Wu13755602016-03-20 20:59:20 +0000131
Matt Arsenault850657a2017-01-31 01:10:58 +0000132#define DEBUG_TYPE "infer-address-spaces"
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000133
Jingyue Wu13755602016-03-20 20:59:20 +0000134using namespace llvm;
135
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000136static const unsigned UninitializedAddressSpace =
137 std::numeric_limits<unsigned>::max();
138
Jingyue Wu13755602016-03-20 20:59:20 +0000139namespace {
Jingyue Wu13755602016-03-20 20:59:20 +0000140
141using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>;
142
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000143/// InferAddressSpaces
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000144class InferAddressSpaces : public FunctionPass {
Matt Arsenault42b64782017-01-30 23:02:12 +0000145 /// Target specific address space which uses of should be replaced if
146 /// possible.
147 unsigned FlatAddrSpace;
148
Jingyue Wu13755602016-03-20 20:59:20 +0000149public:
150 static char ID;
151
Matt Arsenault850657a2017-01-31 01:10:58 +0000152 InferAddressSpaces() : FunctionPass(ID) {}
Jingyue Wu13755602016-03-20 20:59:20 +0000153
Matt Arsenault32b96002017-01-27 17:30:39 +0000154 void getAnalysisUsage(AnalysisUsage &AU) const override {
155 AU.setPreservesCFG();
Matt Arsenault42b64782017-01-30 23:02:12 +0000156 AU.addRequired<TargetTransformInfoWrapperPass>();
Matt Arsenault32b96002017-01-27 17:30:39 +0000157 }
158
Jingyue Wu13755602016-03-20 20:59:20 +0000159 bool runOnFunction(Function &F) override;
160
161private:
162 // Returns the new address space of V if updated; otherwise, returns None.
163 Optional<unsigned>
164 updateAddressSpace(const Value &V,
Matt Arsenault42b64782017-01-30 23:02:12 +0000165 const ValueToAddrSpaceMapTy &InferredAddrSpace) const;
Jingyue Wu13755602016-03-20 20:59:20 +0000166
167 // Tries to infer the specific address space of each address expression in
168 // Postorder.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000169 void inferAddressSpaces(ArrayRef<WeakTrackingVH> Postorder,
Matt Arsenault42b64782017-01-30 23:02:12 +0000170 ValueToAddrSpaceMapTy *InferredAddrSpace) const;
Jingyue Wu13755602016-03-20 20:59:20 +0000171
Matt Arsenault72f259b2017-01-31 02:17:32 +0000172 bool isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const;
173
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000174 // Changes the flat address expressions in function F to point to specific
Jingyue Wu13755602016-03-20 20:59:20 +0000175 // address spaces if InferredAddrSpace says so. Postorder is the postorder of
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000176 // all flat expressions in the use-def graph of function F.
Artem Belevichcb8f6322017-10-24 20:31:44 +0000177 bool rewriteWithNewAddressSpaces(
178 const TargetTransformInfo &TTI, ArrayRef<WeakTrackingVH> Postorder,
179 const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const;
Matt Arsenault42b64782017-01-30 23:02:12 +0000180
181 void appendsFlatAddressExpressionToPostorderStack(
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000182 Value *V, std::vector<std::pair<Value *, bool>> &PostorderStack,
183 DenseSet<Value *> &Visited) const;
Matt Arsenault42b64782017-01-30 23:02:12 +0000184
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000185 bool rewriteIntrinsicOperands(IntrinsicInst *II,
186 Value *OldV, Value *NewV) const;
187 void collectRewritableIntrinsicOperands(
188 IntrinsicInst *II,
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000189 std::vector<std::pair<Value *, bool>> &PostorderStack,
190 DenseSet<Value *> &Visited) const;
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000191
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000192 std::vector<WeakTrackingVH> collectFlatAddressExpressions(Function &F) const;
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000193
Matt Arsenault42b64782017-01-30 23:02:12 +0000194 Value *cloneValueWithNewAddressSpace(
195 Value *V, unsigned NewAddrSpace,
196 const ValueToValueMapTy &ValueWithNewAddrSpace,
197 SmallVectorImpl<const Use *> *UndefUsesToFix) const;
198 unsigned joinAddressSpaces(unsigned AS1, unsigned AS2) const;
Jingyue Wu13755602016-03-20 20:59:20 +0000199};
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000200
Jingyue Wu13755602016-03-20 20:59:20 +0000201} // end anonymous namespace
202
Matt Arsenault850657a2017-01-31 01:10:58 +0000203char InferAddressSpaces::ID = 0;
Jingyue Wu13755602016-03-20 20:59:20 +0000204
205namespace llvm {
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000206
Matt Arsenault850657a2017-01-31 01:10:58 +0000207void initializeInferAddressSpacesPass(PassRegistry &);
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000208
209} // end namespace llvm
Matt Arsenault850657a2017-01-31 01:10:58 +0000210
211INITIALIZE_PASS(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
Jingyue Wu13755602016-03-20 20:59:20 +0000212 false, false)
213
214// Returns true if V is an address expression.
215// TODO: Currently, we consider only phi, bitcast, addrspacecast, and
216// getelementptr operators.
217static bool isAddressExpression(const Value &V) {
218 if (!isa<Operator>(V))
219 return false;
220
221 switch (cast<Operator>(V).getOpcode()) {
222 case Instruction::PHI:
223 case Instruction::BitCast:
224 case Instruction::AddrSpaceCast:
225 case Instruction::GetElementPtr:
Matt Arsenaultbdd59e62017-02-01 00:08:53 +0000226 case Instruction::Select:
Jingyue Wu13755602016-03-20 20:59:20 +0000227 return true;
228 default:
229 return false;
230 }
231}
232
233// Returns the pointer operands of V.
234//
235// Precondition: V is an address expression.
236static SmallVector<Value *, 2> getPointerOperands(const Value &V) {
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000237 const Operator &Op = cast<Operator>(V);
Jingyue Wu13755602016-03-20 20:59:20 +0000238 switch (Op.getOpcode()) {
239 case Instruction::PHI: {
240 auto IncomingValues = cast<PHINode>(Op).incoming_values();
241 return SmallVector<Value *, 2>(IncomingValues.begin(),
242 IncomingValues.end());
243 }
244 case Instruction::BitCast:
245 case Instruction::AddrSpaceCast:
246 case Instruction::GetElementPtr:
247 return {Op.getOperand(0)};
Matt Arsenaultbdd59e62017-02-01 00:08:53 +0000248 case Instruction::Select:
249 return {Op.getOperand(1), Op.getOperand(2)};
Jingyue Wu13755602016-03-20 20:59:20 +0000250 default:
251 llvm_unreachable("Unexpected instruction type.");
252 }
253}
254
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000255// TODO: Move logic to TTI?
256bool InferAddressSpaces::rewriteIntrinsicOperands(IntrinsicInst *II,
257 Value *OldV,
258 Value *NewV) const {
259 Module *M = II->getParent()->getParent()->getParent();
260
261 switch (II->getIntrinsicID()) {
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000262 case Intrinsic::amdgcn_atomic_inc:
Daniil Fukalovd5fca552018-01-17 14:05:05 +0000263 case Intrinsic::amdgcn_atomic_dec:
Daniil Fukalov6e1dc682018-01-26 11:09:38 +0000264 case Intrinsic::amdgcn_ds_fadd:
265 case Intrinsic::amdgcn_ds_fmin:
266 case Intrinsic::amdgcn_ds_fmax: {
Matt Arsenault79f837c2017-03-30 22:21:40 +0000267 const ConstantInt *IsVolatile = dyn_cast<ConstantInt>(II->getArgOperand(4));
Craig Topper79ab6432017-07-06 18:39:47 +0000268 if (!IsVolatile || !IsVolatile->isZero())
Matt Arsenault79f837c2017-03-30 22:21:40 +0000269 return false;
270
271 LLVM_FALLTHROUGH;
272 }
273 case Intrinsic::objectsize: {
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000274 Type *DestTy = II->getType();
275 Type *SrcTy = NewV->getType();
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000276 Function *NewDecl =
277 Intrinsic::getDeclaration(M, II->getIntrinsicID(), {DestTy, SrcTy});
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000278 II->setArgOperand(0, NewV);
279 II->setCalledFunction(NewDecl);
280 return true;
281 }
282 default:
283 return false;
284 }
285}
286
287// TODO: Move logic to TTI?
288void InferAddressSpaces::collectRewritableIntrinsicOperands(
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000289 IntrinsicInst *II, std::vector<std::pair<Value *, bool>> &PostorderStack,
290 DenseSet<Value *> &Visited) const {
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000291 switch (II->getIntrinsicID()) {
292 case Intrinsic::objectsize:
293 case Intrinsic::amdgcn_atomic_inc:
294 case Intrinsic::amdgcn_atomic_dec:
Daniil Fukalov6e1dc682018-01-26 11:09:38 +0000295 case Intrinsic::amdgcn_ds_fadd:
296 case Intrinsic::amdgcn_ds_fmin:
297 case Intrinsic::amdgcn_ds_fmax:
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000298 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
299 PostorderStack, Visited);
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000300 break;
301 default:
302 break;
303 }
304}
305
306// Returns all flat address expressions in function F. The elements are
Matt Arsenault42b64782017-01-30 23:02:12 +0000307// If V is an unvisited flat address expression, appends V to PostorderStack
Jingyue Wu13755602016-03-20 20:59:20 +0000308// and marks it as visited.
Matt Arsenault850657a2017-01-31 01:10:58 +0000309void InferAddressSpaces::appendsFlatAddressExpressionToPostorderStack(
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000310 Value *V, std::vector<std::pair<Value *, bool>> &PostorderStack,
311 DenseSet<Value *> &Visited) const {
Jingyue Wu13755602016-03-20 20:59:20 +0000312 assert(V->getType()->isPointerTy());
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000313
314 // Generic addressing expressions may be hidden in nested constant
315 // expressions.
316 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
317 // TODO: Look in non-address parts, like icmp operands.
318 if (isAddressExpression(*CE) && Visited.insert(CE).second)
319 PostorderStack.push_back(std::make_pair(CE, false));
320
321 return;
322 }
323
Jingyue Wu13755602016-03-20 20:59:20 +0000324 if (isAddressExpression(*V) &&
Matt Arsenault42b64782017-01-30 23:02:12 +0000325 V->getType()->getPointerAddressSpace() == FlatAddrSpace) {
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000326 if (Visited.insert(V).second) {
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000327 PostorderStack.push_back(std::make_pair(V, false));
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000328
329 Operator *Op = cast<Operator>(V);
330 for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I) {
331 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op->getOperand(I))) {
332 if (isAddressExpression(*CE) && Visited.insert(CE).second)
333 PostorderStack.emplace_back(CE, false);
334 }
335 }
336 }
Jingyue Wu13755602016-03-20 20:59:20 +0000337 }
338}
339
Matt Arsenault42b64782017-01-30 23:02:12 +0000340// Returns all flat address expressions in function F. The elements are ordered
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000341// ordered in postorder.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000342std::vector<WeakTrackingVH>
Matt Arsenault850657a2017-01-31 01:10:58 +0000343InferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
Jingyue Wu13755602016-03-20 20:59:20 +0000344 // This function implements a non-recursive postorder traversal of a partial
345 // use-def graph of function F.
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000346 std::vector<std::pair<Value *, bool>> PostorderStack;
Jingyue Wu13755602016-03-20 20:59:20 +0000347 // The set of visited expressions.
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000348 DenseSet<Value *> Visited;
Matt Arsenault6c907a92017-01-31 01:40:38 +0000349
350 auto PushPtrOperand = [&](Value *Ptr) {
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000351 appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
352 Visited);
Matt Arsenault6c907a92017-01-31 01:40:38 +0000353 };
354
Matt Arsenaultc07bda72017-04-21 21:35:04 +0000355 // Look at operations that may be interesting accelerate by moving to a known
356 // address space. We aim at generating after loads and stores, but pure
357 // addressing calculations may also be faster.
Jingyue Wu13755602016-03-20 20:59:20 +0000358 for (Instruction &I : instructions(F)) {
Matt Arsenaultc07bda72017-04-21 21:35:04 +0000359 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
360 if (!GEP->getType()->isVectorTy())
361 PushPtrOperand(GEP->getPointerOperand());
362 } else if (auto *LI = dyn_cast<LoadInst>(&I))
Matt Arsenault6c907a92017-01-31 01:40:38 +0000363 PushPtrOperand(LI->getPointerOperand());
364 else if (auto *SI = dyn_cast<StoreInst>(&I))
365 PushPtrOperand(SI->getPointerOperand());
366 else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I))
367 PushPtrOperand(RMW->getPointerOperand());
368 else if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(&I))
369 PushPtrOperand(CmpX->getPointerOperand());
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000370 else if (auto *MI = dyn_cast<MemIntrinsic>(&I)) {
371 // For memset/memcpy/memmove, any pointer operand can be replaced.
372 PushPtrOperand(MI->getRawDest());
Matt Arsenault6c907a92017-01-31 01:40:38 +0000373
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000374 // Handle 2nd operand for memcpy/memmove.
375 if (auto *MTI = dyn_cast<MemTransferInst>(MI))
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000376 PushPtrOperand(MTI->getRawSource());
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000377 } else if (auto *II = dyn_cast<IntrinsicInst>(&I))
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000378 collectRewritableIntrinsicOperands(II, PostorderStack, Visited);
Matt Arsenault72f259b2017-01-31 02:17:32 +0000379 else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(&I)) {
380 // FIXME: Handle vectors of pointers
381 if (Cmp->getOperand(0)->getType()->isPointerTy()) {
382 PushPtrOperand(Cmp->getOperand(0));
383 PushPtrOperand(Cmp->getOperand(1));
384 }
Matt Arsenaulta1e73402017-04-28 22:18:08 +0000385 } else if (auto *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
386 if (!ASC->getType()->isVectorTy())
387 PushPtrOperand(ASC->getPointerOperand());
Matt Arsenault72f259b2017-01-31 02:17:32 +0000388 }
Jingyue Wu13755602016-03-20 20:59:20 +0000389 }
390
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000391 std::vector<WeakTrackingVH> Postorder; // The resultant postorder.
Jingyue Wu13755602016-03-20 20:59:20 +0000392 while (!PostorderStack.empty()) {
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000393 Value *TopVal = PostorderStack.back().first;
Jingyue Wu13755602016-03-20 20:59:20 +0000394 // If the operands of the expression on the top are already explored,
395 // adds that expression to the resultant postorder.
396 if (PostorderStack.back().second) {
Yaxun Liub909f112017-07-07 02:40:13 +0000397 if (TopVal->getType()->getPointerAddressSpace() == FlatAddrSpace)
398 Postorder.push_back(TopVal);
Jingyue Wu13755602016-03-20 20:59:20 +0000399 PostorderStack.pop_back();
400 continue;
401 }
402 // Otherwise, adds its operands to the stack and explores them.
403 PostorderStack.back().second = true;
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000404 for (Value *PtrOperand : getPointerOperands(*TopVal)) {
Matt Arsenault6d7f01e2017-04-24 23:42:41 +0000405 appendsFlatAddressExpressionToPostorderStack(PtrOperand, PostorderStack,
406 Visited);
Jingyue Wu13755602016-03-20 20:59:20 +0000407 }
408 }
409 return Postorder;
410}
411
412// A helper function for cloneInstructionWithNewAddressSpace. Returns the clone
413// of OperandUse.get() in the new address space. If the clone is not ready yet,
414// returns an undef in the new address space as a placeholder.
415static Value *operandWithNewAddressSpaceOrCreateUndef(
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000416 const Use &OperandUse, unsigned NewAddrSpace,
417 const ValueToValueMapTy &ValueWithNewAddrSpace,
418 SmallVectorImpl<const Use *> *UndefUsesToFix) {
Jingyue Wu13755602016-03-20 20:59:20 +0000419 Value *Operand = OperandUse.get();
Matt Arsenault30083602017-02-02 03:37:22 +0000420
421 Type *NewPtrTy =
422 Operand->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
423
424 if (Constant *C = dyn_cast<Constant>(Operand))
425 return ConstantExpr::getAddrSpaceCast(C, NewPtrTy);
426
Jingyue Wu13755602016-03-20 20:59:20 +0000427 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand))
428 return NewOperand;
429
430 UndefUsesToFix->push_back(&OperandUse);
Matt Arsenault30083602017-02-02 03:37:22 +0000431 return UndefValue::get(NewPtrTy);
Jingyue Wu13755602016-03-20 20:59:20 +0000432}
433
434// Returns a clone of `I` with its operands converted to those specified in
435// ValueWithNewAddrSpace. Due to potential cycles in the data flow graph, an
436// operand whose address space needs to be modified might not exist in
437// ValueWithNewAddrSpace. In that case, uses undef as a placeholder operand and
438// adds that operand use to UndefUsesToFix so that caller can fix them later.
439//
440// Note that we do not necessarily clone `I`, e.g., if it is an addrspacecast
441// from a pointer whose type already matches. Therefore, this function returns a
442// Value* instead of an Instruction*.
443static Value *cloneInstructionWithNewAddressSpace(
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000444 Instruction *I, unsigned NewAddrSpace,
445 const ValueToValueMapTy &ValueWithNewAddrSpace,
446 SmallVectorImpl<const Use *> *UndefUsesToFix) {
Jingyue Wu13755602016-03-20 20:59:20 +0000447 Type *NewPtrType =
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000448 I->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
Jingyue Wu13755602016-03-20 20:59:20 +0000449
450 if (I->getOpcode() == Instruction::AddrSpaceCast) {
451 Value *Src = I->getOperand(0);
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000452 // Because `I` is flat, the source address space must be specific.
Jingyue Wu13755602016-03-20 20:59:20 +0000453 // Therefore, the inferred address space must be the source space, according
454 // to our algorithm.
455 assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
456 if (Src->getType() != NewPtrType)
457 return new BitCastInst(Src, NewPtrType);
458 return Src;
459 }
460
461 // Computes the converted pointer operands.
462 SmallVector<Value *, 4> NewPointerOperands;
463 for (const Use &OperandUse : I->operands()) {
464 if (!OperandUse.get()->getType()->isPointerTy())
465 NewPointerOperands.push_back(nullptr);
466 else
467 NewPointerOperands.push_back(operandWithNewAddressSpaceOrCreateUndef(
Matt Arsenault850657a2017-01-31 01:10:58 +0000468 OperandUse, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix));
Jingyue Wu13755602016-03-20 20:59:20 +0000469 }
470
471 switch (I->getOpcode()) {
472 case Instruction::BitCast:
473 return new BitCastInst(NewPointerOperands[0], NewPtrType);
474 case Instruction::PHI: {
475 assert(I->getType()->isPointerTy());
476 PHINode *PHI = cast<PHINode>(I);
477 PHINode *NewPHI = PHINode::Create(NewPtrType, PHI->getNumIncomingValues());
478 for (unsigned Index = 0; Index < PHI->getNumIncomingValues(); ++Index) {
479 unsigned OperandNo = PHINode::getOperandNumForIncomingValue(Index);
480 NewPHI->addIncoming(NewPointerOperands[OperandNo],
481 PHI->getIncomingBlock(Index));
482 }
483 return NewPHI;
484 }
485 case Instruction::GetElementPtr: {
486 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
487 GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000488 GEP->getSourceElementType(), NewPointerOperands[0],
489 SmallVector<Value *, 4>(GEP->idx_begin(), GEP->idx_end()));
Jingyue Wu13755602016-03-20 20:59:20 +0000490 NewGEP->setIsInBounds(GEP->isInBounds());
491 return NewGEP;
492 }
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000493 case Instruction::Select:
Matt Arsenaultbdd59e62017-02-01 00:08:53 +0000494 assert(I->getType()->isPointerTy());
495 return SelectInst::Create(I->getOperand(0), NewPointerOperands[1],
496 NewPointerOperands[2], "", nullptr, I);
Jingyue Wu13755602016-03-20 20:59:20 +0000497 default:
498 llvm_unreachable("Unexpected opcode");
499 }
500}
501
502// Similar to cloneInstructionWithNewAddressSpace, returns a clone of the
503// constant expression `CE` with its operands replaced as specified in
504// ValueWithNewAddrSpace.
505static Value *cloneConstantExprWithNewAddressSpace(
Matt Arsenault850657a2017-01-31 01:10:58 +0000506 ConstantExpr *CE, unsigned NewAddrSpace,
507 const ValueToValueMapTy &ValueWithNewAddrSpace) {
Jingyue Wu13755602016-03-20 20:59:20 +0000508 Type *TargetType =
Matt Arsenault850657a2017-01-31 01:10:58 +0000509 CE->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
Jingyue Wu13755602016-03-20 20:59:20 +0000510
511 if (CE->getOpcode() == Instruction::AddrSpaceCast) {
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000512 // Because CE is flat, the source address space must be specific.
Jingyue Wu13755602016-03-20 20:59:20 +0000513 // Therefore, the inferred address space must be the source space according
514 // to our algorithm.
515 assert(CE->getOperand(0)->getType()->getPointerAddressSpace() ==
516 NewAddrSpace);
517 return ConstantExpr::getBitCast(CE->getOperand(0), TargetType);
518 }
519
Matt Arsenaultc18b6772017-02-17 00:32:19 +0000520 if (CE->getOpcode() == Instruction::BitCast) {
521 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(CE->getOperand(0)))
522 return ConstantExpr::getBitCast(cast<Constant>(NewOperand), TargetType);
523 return ConstantExpr::getAddrSpaceCast(CE, TargetType);
524 }
525
Matt Arsenault30083602017-02-02 03:37:22 +0000526 if (CE->getOpcode() == Instruction::Select) {
527 Constant *Src0 = CE->getOperand(1);
528 Constant *Src1 = CE->getOperand(2);
529 if (Src0->getType()->getPointerAddressSpace() ==
530 Src1->getType()->getPointerAddressSpace()) {
531
532 return ConstantExpr::getSelect(
533 CE->getOperand(0), ConstantExpr::getAddrSpaceCast(Src0, TargetType),
534 ConstantExpr::getAddrSpaceCast(Src1, TargetType));
535 }
536 }
537
Jingyue Wu13755602016-03-20 20:59:20 +0000538 // Computes the operands of the new constant expression.
Nirav Dave62fb8492017-06-08 13:20:55 +0000539 bool IsNew = false;
Jingyue Wu13755602016-03-20 20:59:20 +0000540 SmallVector<Constant *, 4> NewOperands;
541 for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) {
542 Constant *Operand = CE->getOperand(Index);
543 // If the address space of `Operand` needs to be modified, the new operand
544 // with the new address space should already be in ValueWithNewAddrSpace
545 // because (1) the constant expressions we consider (i.e. addrspacecast,
546 // bitcast, and getelementptr) do not incur cycles in the data flow graph
547 // and (2) this function is called on constant expressions in postorder.
548 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) {
Nirav Dave62fb8492017-06-08 13:20:55 +0000549 IsNew = true;
Jingyue Wu13755602016-03-20 20:59:20 +0000550 NewOperands.push_back(cast<Constant>(NewOperand));
551 } else {
552 // Otherwise, reuses the old operand.
553 NewOperands.push_back(Operand);
554 }
555 }
556
Nirav Dave62fb8492017-06-08 13:20:55 +0000557 // If !IsNew, we will replace the Value with itself. However, replaced values
558 // are assumed to wrapped in a addrspace cast later so drop it now.
559 if (!IsNew)
560 return nullptr;
561
Jingyue Wu13755602016-03-20 20:59:20 +0000562 if (CE->getOpcode() == Instruction::GetElementPtr) {
563 // Needs to specify the source type while constructing a getelementptr
564 // constant expression.
565 return CE->getWithOperands(
Matt Arsenault850657a2017-01-31 01:10:58 +0000566 NewOperands, TargetType, /*OnlyIfReduced=*/false,
567 NewOperands[0]->getType()->getPointerElementType());
Jingyue Wu13755602016-03-20 20:59:20 +0000568 }
569
570 return CE->getWithOperands(NewOperands, TargetType);
571}
572
573// Returns a clone of the value `V`, with its operands replaced as specified in
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000574// ValueWithNewAddrSpace. This function is called on every flat address
Jingyue Wu13755602016-03-20 20:59:20 +0000575// expression whose address space needs to be modified, in postorder.
576//
577// See cloneInstructionWithNewAddressSpace for the meaning of UndefUsesToFix.
Matt Arsenault850657a2017-01-31 01:10:58 +0000578Value *InferAddressSpaces::cloneValueWithNewAddressSpace(
Matt Arsenault42b64782017-01-30 23:02:12 +0000579 Value *V, unsigned NewAddrSpace,
580 const ValueToValueMapTy &ValueWithNewAddrSpace,
581 SmallVectorImpl<const Use *> *UndefUsesToFix) const {
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000582 // All values in Postorder are flat address expressions.
Jingyue Wu13755602016-03-20 20:59:20 +0000583 assert(isAddressExpression(*V) &&
Matt Arsenault42b64782017-01-30 23:02:12 +0000584 V->getType()->getPointerAddressSpace() == FlatAddrSpace);
Jingyue Wu13755602016-03-20 20:59:20 +0000585
586 if (Instruction *I = dyn_cast<Instruction>(V)) {
587 Value *NewV = cloneInstructionWithNewAddressSpace(
Matt Arsenault850657a2017-01-31 01:10:58 +0000588 I, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix);
Jingyue Wu13755602016-03-20 20:59:20 +0000589 if (Instruction *NewI = dyn_cast<Instruction>(NewV)) {
590 if (NewI->getParent() == nullptr) {
591 NewI->insertBefore(I);
592 NewI->takeName(I);
593 }
594 }
595 return NewV;
596 }
597
598 return cloneConstantExprWithNewAddressSpace(
Matt Arsenault850657a2017-01-31 01:10:58 +0000599 cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace);
Jingyue Wu13755602016-03-20 20:59:20 +0000600}
601
602// Defines the join operation on the address space lattice (see the file header
603// comments).
Matt Arsenault850657a2017-01-31 01:10:58 +0000604unsigned InferAddressSpaces::joinAddressSpaces(unsigned AS1,
605 unsigned AS2) const {
Matt Arsenault42b64782017-01-30 23:02:12 +0000606 if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace)
607 return FlatAddrSpace;
Jingyue Wu13755602016-03-20 20:59:20 +0000608
Matt Arsenault973c4ae2017-01-31 02:17:41 +0000609 if (AS1 == UninitializedAddressSpace)
Jingyue Wu13755602016-03-20 20:59:20 +0000610 return AS2;
Matt Arsenault973c4ae2017-01-31 02:17:41 +0000611 if (AS2 == UninitializedAddressSpace)
Jingyue Wu13755602016-03-20 20:59:20 +0000612 return AS1;
613
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000614 // The join of two different specific address spaces is flat.
Matt Arsenault42b64782017-01-30 23:02:12 +0000615 return (AS1 == AS2) ? AS1 : FlatAddrSpace;
Jingyue Wu13755602016-03-20 20:59:20 +0000616}
617
Matt Arsenault850657a2017-01-31 01:10:58 +0000618bool InferAddressSpaces::runOnFunction(Function &F) {
Andrew Kaylor87b10dd2016-04-26 23:44:31 +0000619 if (skipFunction(F))
620 return false;
621
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000622 const TargetTransformInfo &TTI =
623 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Matt Arsenault42b64782017-01-30 23:02:12 +0000624 FlatAddrSpace = TTI.getFlatAddressSpace();
Matt Arsenault973c4ae2017-01-31 02:17:41 +0000625 if (FlatAddrSpace == UninitializedAddressSpace)
Matt Arsenault42b64782017-01-30 23:02:12 +0000626 return false;
627
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000628 // Collects all flat address expressions in postorder.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000629 std::vector<WeakTrackingVH> Postorder = collectFlatAddressExpressions(F);
Jingyue Wu13755602016-03-20 20:59:20 +0000630
631 // Runs a data-flow analysis to refine the address spaces of every expression
632 // in Postorder.
633 ValueToAddrSpaceMapTy InferredAddrSpace;
634 inferAddressSpaces(Postorder, &InferredAddrSpace);
635
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000636 // Changes the address spaces of the flat address expressions who are inferred
637 // to point to a specific address space.
Artem Belevichcb8f6322017-10-24 20:31:44 +0000638 return rewriteWithNewAddressSpaces(TTI, Postorder, InferredAddrSpace, &F);
Jingyue Wu13755602016-03-20 20:59:20 +0000639}
640
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000641// Constants need to be tracked through RAUW to handle cases with nested
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000642// constant expressions, so wrap values in WeakTrackingVH.
Matt Arsenault850657a2017-01-31 01:10:58 +0000643void InferAddressSpaces::inferAddressSpaces(
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000644 ArrayRef<WeakTrackingVH> Postorder,
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000645 ValueToAddrSpaceMapTy *InferredAddrSpace) const {
Jingyue Wu13755602016-03-20 20:59:20 +0000646 SetVector<Value *> Worklist(Postorder.begin(), Postorder.end());
647 // Initially, all expressions are in the uninitialized address space.
648 for (Value *V : Postorder)
Matt Arsenault973c4ae2017-01-31 02:17:41 +0000649 (*InferredAddrSpace)[V] = UninitializedAddressSpace;
Jingyue Wu13755602016-03-20 20:59:20 +0000650
651 while (!Worklist.empty()) {
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000652 Value *V = Worklist.pop_back_val();
Jingyue Wu13755602016-03-20 20:59:20 +0000653
654 // Tries to update the address space of the stack top according to the
655 // address spaces of its operands.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000656 LLVM_DEBUG(dbgs() << "Updating the address space of\n " << *V << '\n');
Jingyue Wu13755602016-03-20 20:59:20 +0000657 Optional<unsigned> NewAS = updateAddressSpace(*V, *InferredAddrSpace);
658 if (!NewAS.hasValue())
659 continue;
660 // If any updates are made, grabs its users to the worklist because
661 // their address spaces can also be possibly updated.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000662 LLVM_DEBUG(dbgs() << " to " << NewAS.getValue() << '\n');
Jingyue Wu13755602016-03-20 20:59:20 +0000663 (*InferredAddrSpace)[V] = NewAS.getValue();
664
665 for (Value *User : V->users()) {
666 // Skip if User is already in the worklist.
667 if (Worklist.count(User))
668 continue;
669
670 auto Pos = InferredAddrSpace->find(User);
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000671 // Our algorithm only updates the address spaces of flat address
Jingyue Wu13755602016-03-20 20:59:20 +0000672 // expressions, which are those in InferredAddrSpace.
673 if (Pos == InferredAddrSpace->end())
674 continue;
675
676 // Function updateAddressSpace moves the address space down a lattice
Matt Arsenault850657a2017-01-31 01:10:58 +0000677 // path. Therefore, nothing to do if User is already inferred as flat (the
678 // bottom element in the lattice).
Matt Arsenault42b64782017-01-30 23:02:12 +0000679 if (Pos->second == FlatAddrSpace)
Jingyue Wu13755602016-03-20 20:59:20 +0000680 continue;
681
682 Worklist.insert(User);
683 }
684 }
685}
686
Matt Arsenault850657a2017-01-31 01:10:58 +0000687Optional<unsigned> InferAddressSpaces::updateAddressSpace(
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000688 const Value &V, const ValueToAddrSpaceMapTy &InferredAddrSpace) const {
Jingyue Wu13755602016-03-20 20:59:20 +0000689 assert(InferredAddrSpace.count(&V));
690
691 // The new inferred address space equals the join of the address spaces
692 // of all its pointer operands.
Matt Arsenault973c4ae2017-01-31 02:17:41 +0000693 unsigned NewAS = UninitializedAddressSpace;
Matt Arsenault850657a2017-01-31 01:10:58 +0000694
Matt Arsenault30083602017-02-02 03:37:22 +0000695 const Operator &Op = cast<Operator>(V);
696 if (Op.getOpcode() == Instruction::Select) {
697 Value *Src0 = Op.getOperand(1);
698 Value *Src1 = Op.getOperand(2);
699
700 auto I = InferredAddrSpace.find(Src0);
701 unsigned Src0AS = (I != InferredAddrSpace.end()) ?
702 I->second : Src0->getType()->getPointerAddressSpace();
703
704 auto J = InferredAddrSpace.find(Src1);
705 unsigned Src1AS = (J != InferredAddrSpace.end()) ?
706 J->second : Src1->getType()->getPointerAddressSpace();
707
708 auto *C0 = dyn_cast<Constant>(Src0);
709 auto *C1 = dyn_cast<Constant>(Src1);
710
711 // If one of the inputs is a constant, we may be able to do a constant
712 // addrspacecast of it. Defer inferring the address space until the input
713 // address space is known.
714 if ((C1 && Src0AS == UninitializedAddressSpace) ||
715 (C0 && Src1AS == UninitializedAddressSpace))
716 return None;
717
718 if (C0 && isSafeToCastConstAddrSpace(C0, Src1AS))
719 NewAS = Src1AS;
720 else if (C1 && isSafeToCastConstAddrSpace(C1, Src0AS))
721 NewAS = Src0AS;
722 else
723 NewAS = joinAddressSpaces(Src0AS, Src1AS);
724 } else {
725 for (Value *PtrOperand : getPointerOperands(V)) {
726 auto I = InferredAddrSpace.find(PtrOperand);
727 unsigned OperandAS = I != InferredAddrSpace.end() ?
728 I->second : PtrOperand->getType()->getPointerAddressSpace();
729
730 // join(flat, *) = flat. So we can break if NewAS is already flat.
731 NewAS = joinAddressSpaces(NewAS, OperandAS);
732 if (NewAS == FlatAddrSpace)
733 break;
734 }
Jingyue Wu13755602016-03-20 20:59:20 +0000735 }
736
737 unsigned OldAS = InferredAddrSpace.lookup(&V);
Matt Arsenault42b64782017-01-30 23:02:12 +0000738 assert(OldAS != FlatAddrSpace);
Jingyue Wu13755602016-03-20 20:59:20 +0000739 if (OldAS == NewAS)
740 return None;
741 return NewAS;
742}
743
Matt Arsenault6c907a92017-01-31 01:40:38 +0000744/// \p returns true if \p U is the pointer operand of a memory instruction with
745/// a single pointer operand that can have its address space changed by simply
Artem Belevichcb8f6322017-10-24 20:31:44 +0000746/// mutating the use to a new value. If the memory instruction is volatile,
747/// return true only if the target allows the memory instruction to be volatile
748/// in the new address space.
749static bool isSimplePointerUseValidToReplace(const TargetTransformInfo &TTI,
750 Use &U, unsigned AddrSpace) {
Matt Arsenault6c907a92017-01-31 01:40:38 +0000751 User *Inst = U.getUser();
752 unsigned OpNo = U.getOperandNo();
Artem Belevichcb8f6322017-10-24 20:31:44 +0000753 bool VolatileIsAllowed = false;
754 if (auto *I = dyn_cast<Instruction>(Inst))
755 VolatileIsAllowed = TTI.hasVolatileVariant(I, AddrSpace);
Matt Arsenault6c907a92017-01-31 01:40:38 +0000756
757 if (auto *LI = dyn_cast<LoadInst>(Inst))
Artem Belevichcb8f6322017-10-24 20:31:44 +0000758 return OpNo == LoadInst::getPointerOperandIndex() &&
759 (VolatileIsAllowed || !LI->isVolatile());
Matt Arsenault6c907a92017-01-31 01:40:38 +0000760
761 if (auto *SI = dyn_cast<StoreInst>(Inst))
Artem Belevichcb8f6322017-10-24 20:31:44 +0000762 return OpNo == StoreInst::getPointerOperandIndex() &&
763 (VolatileIsAllowed || !SI->isVolatile());
Matt Arsenault6c907a92017-01-31 01:40:38 +0000764
765 if (auto *RMW = dyn_cast<AtomicRMWInst>(Inst))
Artem Belevichcb8f6322017-10-24 20:31:44 +0000766 return OpNo == AtomicRMWInst::getPointerOperandIndex() &&
767 (VolatileIsAllowed || !RMW->isVolatile());
Matt Arsenault6c907a92017-01-31 01:40:38 +0000768
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000769 if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst))
Matt Arsenault6c907a92017-01-31 01:40:38 +0000770 return OpNo == AtomicCmpXchgInst::getPointerOperandIndex() &&
Artem Belevichcb8f6322017-10-24 20:31:44 +0000771 (VolatileIsAllowed || !CmpX->isVolatile());
Matt Arsenault6c907a92017-01-31 01:40:38 +0000772
773 return false;
774}
775
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000776/// Update memory intrinsic uses that require more complex processing than
777/// simple memory instructions. Thse require re-mangling and may have multiple
778/// pointer operands.
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000779static bool handleMemIntrinsicPtrUse(MemIntrinsic *MI, Value *OldV,
780 Value *NewV) {
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000781 IRBuilder<> B(MI);
782 MDNode *TBAA = MI->getMetadata(LLVMContext::MD_tbaa);
783 MDNode *ScopeMD = MI->getMetadata(LLVMContext::MD_alias_scope);
784 MDNode *NoAliasMD = MI->getMetadata(LLVMContext::MD_noalias);
785
786 if (auto *MSI = dyn_cast<MemSetInst>(MI)) {
787 B.CreateMemSet(NewV, MSI->getValue(),
Daniel Neilson5fdf08f2018-02-06 20:33:36 +0000788 MSI->getLength(), MSI->getDestAlignment(),
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000789 false, // isVolatile
790 TBAA, ScopeMD, NoAliasMD);
791 } else if (auto *MTI = dyn_cast<MemTransferInst>(MI)) {
792 Value *Src = MTI->getRawSource();
793 Value *Dest = MTI->getRawDest();
794
795 // Be careful in case this is a self-to-self copy.
796 if (Src == OldV)
797 Src = NewV;
798
799 if (Dest == OldV)
800 Dest = NewV;
801
802 if (isa<MemCpyInst>(MTI)) {
803 MDNode *TBAAStruct = MTI->getMetadata(LLVMContext::MD_tbaa_struct);
Daniel Neilson5fdf08f2018-02-06 20:33:36 +0000804 B.CreateMemCpy(Dest, MTI->getDestAlignment(),
805 Src, MTI->getSourceAlignment(),
806 MTI->getLength(),
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000807 false, // isVolatile
808 TBAA, TBAAStruct, ScopeMD, NoAliasMD);
809 } else {
810 assert(isa<MemMoveInst>(MTI));
Daniel Neilson5fdf08f2018-02-06 20:33:36 +0000811 B.CreateMemMove(Dest, MTI->getDestAlignment(),
812 Src, MTI->getSourceAlignment(),
813 MTI->getLength(),
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000814 false, // isVolatile
815 TBAA, ScopeMD, NoAliasMD);
816 }
817 } else
818 llvm_unreachable("unhandled MemIntrinsic");
819
820 MI->eraseFromParent();
821 return true;
822}
823
Matt Arsenault72f259b2017-01-31 02:17:32 +0000824// \p returns true if it is OK to change the address space of constant \p C with
825// a ConstantExpr addrspacecast.
826bool InferAddressSpaces::isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const {
Matt Arsenault30083602017-02-02 03:37:22 +0000827 assert(NewAS != UninitializedAddressSpace);
828
Matt Arsenault2a46d812017-01-31 23:48:40 +0000829 unsigned SrcAS = C->getType()->getPointerAddressSpace();
830 if (SrcAS == NewAS || isa<UndefValue>(C))
Matt Arsenault72f259b2017-01-31 02:17:32 +0000831 return true;
832
Matt Arsenault2a46d812017-01-31 23:48:40 +0000833 // Prevent illegal casts between different non-flat address spaces.
834 if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace)
835 return false;
836
837 if (isa<ConstantPointerNull>(C))
Matt Arsenault72f259b2017-01-31 02:17:32 +0000838 return true;
839
840 if (auto *Op = dyn_cast<Operator>(C)) {
841 // If we already have a constant addrspacecast, it should be safe to cast it
842 // off.
843 if (Op->getOpcode() == Instruction::AddrSpaceCast)
844 return isSafeToCastConstAddrSpace(cast<Constant>(Op->getOperand(0)), NewAS);
845
846 if (Op->getOpcode() == Instruction::IntToPtr &&
847 Op->getType()->getPointerAddressSpace() == FlatAddrSpace)
848 return true;
849 }
850
851 return false;
852}
853
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000854static Value::use_iterator skipToNextUser(Value::use_iterator I,
855 Value::use_iterator End) {
856 User *CurUser = I->getUser();
857 ++I;
858
859 while (I != End && I->getUser() == CurUser)
860 ++I;
861
862 return I;
863}
864
Matt Arsenault850657a2017-01-31 01:10:58 +0000865bool InferAddressSpaces::rewriteWithNewAddressSpaces(
Artem Belevichcb8f6322017-10-24 20:31:44 +0000866 const TargetTransformInfo &TTI, ArrayRef<WeakTrackingVH> Postorder,
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000867 const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
Jingyue Wu13755602016-03-20 20:59:20 +0000868 // For each address expression to be modified, creates a clone of it with its
869 // pointer operands converted to the new address space. Since the pointer
870 // operands are converted, the clone is naturally in the new address space by
871 // construction.
872 ValueToValueMapTy ValueWithNewAddrSpace;
873 SmallVector<const Use *, 32> UndefUsesToFix;
874 for (Value* V : Postorder) {
875 unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
876 if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
877 ValueWithNewAddrSpace[V] = cloneValueWithNewAddressSpace(
Matt Arsenault850657a2017-01-31 01:10:58 +0000878 V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix);
Jingyue Wu13755602016-03-20 20:59:20 +0000879 }
880 }
881
882 if (ValueWithNewAddrSpace.empty())
883 return false;
884
885 // Fixes all the undef uses generated by cloneInstructionWithNewAddressSpace.
Matt Arsenaultdb6e9e82017-02-02 00:28:25 +0000886 for (const Use *UndefUse : UndefUsesToFix) {
Jingyue Wu13755602016-03-20 20:59:20 +0000887 User *V = UndefUse->getUser();
888 User *NewV = cast<User>(ValueWithNewAddrSpace.lookup(V));
889 unsigned OperandNo = UndefUse->getOperandNo();
890 assert(isa<UndefValue>(NewV->getOperand(OperandNo)));
891 NewV->setOperand(OperandNo, ValueWithNewAddrSpace.lookup(UndefUse->get()));
892 }
893
Matt Arsenaultc20ccd22017-04-28 22:18:19 +0000894 SmallVector<Instruction *, 16> DeadInstructions;
895
Jingyue Wu13755602016-03-20 20:59:20 +0000896 // Replaces the uses of the old address expressions with the new ones.
Sanjoy Dase6bca0e2017-05-01 17:07:49 +0000897 for (const WeakTrackingVH &WVH : Postorder) {
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000898 assert(WVH && "value was unexpectedly deleted");
899 Value *V = WVH;
Jingyue Wu13755602016-03-20 20:59:20 +0000900 Value *NewV = ValueWithNewAddrSpace.lookup(V);
901 if (NewV == nullptr)
902 continue;
903
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000904 LLVM_DEBUG(dbgs() << "Replacing the uses of " << *V << "\n with\n "
905 << *NewV << '\n');
Matt Arsenault9f432ec2017-01-30 23:27:11 +0000906
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000907 if (Constant *C = dyn_cast<Constant>(V)) {
908 Constant *Replace = ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV),
909 C->getType());
910 if (C != Replace) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000911 LLVM_DEBUG(dbgs() << "Inserting replacement const cast: " << Replace
912 << ": " << *Replace << '\n');
Matt Arsenaulte0f9e982017-04-28 22:52:41 +0000913 C->replaceAllUsesWith(Replace);
914 V = Replace;
915 }
916 }
917
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000918 Value::use_iterator I, E, Next;
919 for (I = V->use_begin(), E = V->use_end(); I != E; ) {
920 Use &U = *I;
921
922 // Some users may see the same pointer operand in multiple operands. Skip
923 // to the next instruction.
924 I = skipToNextUser(I, E);
925
Artem Belevichcb8f6322017-10-24 20:31:44 +0000926 if (isSimplePointerUseValidToReplace(
927 TTI, U, V->getType()->getPointerAddressSpace())) {
Matt Arsenault6c907a92017-01-31 01:40:38 +0000928 // If V is used as the pointer operand of a compatible memory operation,
929 // sets the pointer operand to NewV. This replacement does not change
930 // the element type, so the resultant load/store is still valid.
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000931 U.set(NewV);
932 continue;
933 }
934
935 User *CurUser = U.getUser();
936 // Handle more complex cases like intrinsic that need to be remangled.
937 if (auto *MI = dyn_cast<MemIntrinsic>(CurUser)) {
938 if (!MI->isVolatile() && handleMemIntrinsicPtrUse(MI, V, NewV))
939 continue;
940 }
941
942 if (auto *II = dyn_cast<IntrinsicInst>(CurUser)) {
943 if (rewriteIntrinsicOperands(II, V, NewV))
944 continue;
945 }
946
947 if (isa<Instruction>(CurUser)) {
Matt Arsenault72f259b2017-01-31 02:17:32 +0000948 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) {
949 // If we can infer that both pointers are in the same addrspace,
950 // transform e.g.
951 // %cmp = icmp eq float* %p, %q
952 // into
953 // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
954
955 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
956 int SrcIdx = U.getOperandNo();
957 int OtherIdx = (SrcIdx == 0) ? 1 : 0;
958 Value *OtherSrc = Cmp->getOperand(OtherIdx);
959
960 if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) {
961 if (OtherNewV->getType()->getPointerAddressSpace() == NewAS) {
962 Cmp->setOperand(OtherIdx, OtherNewV);
963 Cmp->setOperand(SrcIdx, NewV);
964 continue;
965 }
966 }
967
968 // Even if the type mismatches, we can cast the constant.
969 if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
970 if (isSafeToCastConstAddrSpace(KOtherSrc, NewAS)) {
971 Cmp->setOperand(SrcIdx, NewV);
972 Cmp->setOperand(OtherIdx,
973 ConstantExpr::getAddrSpaceCast(KOtherSrc, NewV->getType()));
974 continue;
975 }
976 }
977 }
978
Matt Arsenaulta1e73402017-04-28 22:18:08 +0000979 if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUser)) {
980 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
981 if (ASC->getDestAddressSpace() == NewAS) {
Yaxun Liud23f23d2017-10-30 21:19:41 +0000982 if (ASC->getType()->getPointerElementType() !=
983 NewV->getType()->getPointerElementType()) {
984 NewV = CastInst::Create(Instruction::BitCast, NewV,
985 ASC->getType(), "", ASC);
986 }
Matt Arsenaulta1e73402017-04-28 22:18:08 +0000987 ASC->replaceAllUsesWith(NewV);
Matt Arsenaultc20ccd22017-04-28 22:18:19 +0000988 DeadInstructions.push_back(ASC);
Matt Arsenaulta1e73402017-04-28 22:18:08 +0000989 continue;
990 }
991 }
992
Matt Arsenault850657a2017-01-31 01:10:58 +0000993 // Otherwise, replaces the use with flat(NewV).
Jingyue Wu13755602016-03-20 20:59:20 +0000994 if (Instruction *I = dyn_cast<Instruction>(V)) {
995 BasicBlock::iterator InsertPos = std::next(I->getIterator());
996 while (isa<PHINode>(InsertPos))
997 ++InsertPos;
Matt Arsenault6d5a8d42017-01-31 01:56:57 +0000998 U.set(new AddrSpaceCastInst(NewV, V->getType(), "", &*InsertPos));
Jingyue Wu13755602016-03-20 20:59:20 +0000999 } else {
Matt Arsenault6d5a8d42017-01-31 01:56:57 +00001000 U.set(ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV),
1001 V->getType()));
Jingyue Wu13755602016-03-20 20:59:20 +00001002 }
1003 }
1004 }
Matt Arsenault6d5a8d42017-01-31 01:56:57 +00001005
Matt Arsenaultc20ccd22017-04-28 22:18:19 +00001006 if (V->use_empty()) {
1007 if (Instruction *I = dyn_cast<Instruction>(V))
1008 DeadInstructions.push_back(I);
1009 }
Jingyue Wu13755602016-03-20 20:59:20 +00001010 }
1011
Matt Arsenaultc20ccd22017-04-28 22:18:19 +00001012 for (Instruction *I : DeadInstructions)
1013 RecursivelyDeleteTriviallyDeadInstructions(I);
1014
Jingyue Wu13755602016-03-20 20:59:20 +00001015 return true;
1016}
1017
Matt Arsenault850657a2017-01-31 01:10:58 +00001018FunctionPass *llvm::createInferAddressSpacesPass() {
1019 return new InferAddressSpaces();
Jingyue Wu13755602016-03-20 20:59:20 +00001020}