LLVM 20.0.0git
InferAddressSpaces.cpp
Go to the documentation of this file.
1//===- InferAddressSpace.cpp - --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// CUDA C/C++ includes memory space designation as variable type qualifers (such
10// as __global__ and __shared__). Knowing the space of a memory access allows
11// CUDA compilers to emit faster PTX loads and stores. For example, a load from
12// shared memory can be translated to `ld.shared` which is roughly 10% faster
13// than a generic `ld` on an NVIDIA Tesla K40c.
14//
15// Unfortunately, type qualifiers only apply to variable declarations, so CUDA
16// compilers must infer the memory space of an address expression from
17// type-qualified variables.
18//
19// LLVM IR uses non-zero (so-called) specific address spaces to represent memory
20// spaces (e.g. addrspace(3) means shared memory). The Clang frontend
21// places only type-qualified variables in specific address spaces, and then
22// conservatively `addrspacecast`s each type-qualified variable to addrspace(0)
23// (so-called the generic address space) for other instructions to use.
24//
25// For example, the Clang translates the following CUDA code
26// __shared__ float a[10];
27// float v = a[i];
28// to
29// %0 = addrspacecast [10 x float] addrspace(3)* @a to [10 x float]*
30// %1 = gep [10 x float], [10 x float]* %0, i64 0, i64 %i
31// %v = load float, float* %1 ; emits ld.f32
32// @a is in addrspace(3) since it's type-qualified, but its use from %1 is
33// redirected to %0 (the generic version of @a).
34//
35// The optimization implemented in this file propagates specific address spaces
36// from type-qualified variable declarations to its users. For example, it
37// optimizes the above IR to
38// %1 = gep [10 x float] addrspace(3)* @a, i64 0, i64 %i
39// %v = load float addrspace(3)* %1 ; emits ld.shared.f32
40// propagating the addrspace(3) from @a to %1. As the result, the NVPTX
41// codegen is able to emit ld.shared.f32 for %v.
42//
43// Address space inference works in two steps. First, it uses a data-flow
44// analysis to infer as many generic pointers as possible to point to only one
45// specific address space. In the above example, it can prove that %1 only
46// points to addrspace(3). This algorithm was published in
47// CUDA: Compiling and optimizing for a GPU platform
48// Chakrabarti, Grover, Aarts, Kong, Kudlur, Lin, Marathe, Murphy, Wang
49// ICCS 2012
50//
51// Then, address space inference replaces all refinable generic pointers with
52// equivalent specific pointers.
53//
54// The major challenge of implementing this optimization is handling PHINodes,
55// which may create loops in the data flow graph. This brings two complications.
56//
57// First, the data flow analysis in Step 1 needs to be circular. For example,
58// %generic.input = addrspacecast float addrspace(3)* %input to float*
59// loop:
60// %y = phi [ %generic.input, %y2 ]
61// %y2 = getelementptr %y, 1
62// %v = load %y2
63// br ..., label %loop, ...
64// proving %y specific requires proving both %generic.input and %y2 specific,
65// but proving %y2 specific circles back to %y. To address this complication,
66// the data flow analysis operates on a lattice:
67// uninitialized > specific address spaces > generic.
68// All address expressions (our implementation only considers phi, bitcast,
69// addrspacecast, and getelementptr) start with the uninitialized address space.
70// The monotone transfer function moves the address space of a pointer down a
71// lattice path from uninitialized to specific and then to generic. A join
72// operation of two different specific address spaces pushes the expression down
73// to the generic address space. The analysis completes once it reaches a fixed
74// point.
75//
76// Second, IR rewriting in Step 2 also needs to be circular. For example,
77// converting %y to addrspace(3) requires the compiler to know the converted
78// %y2, but converting %y2 needs the converted %y. To address this complication,
79// we break these cycles using "poison" placeholders. When converting an
80// instruction `I` to a new address space, if its operand `Op` is not converted
81// yet, we let `I` temporarily use `poison` and fix all the uses later.
82// For instance, our algorithm first converts %y to
83// %y' = phi float addrspace(3)* [ %input, poison ]
84// Then, it converts %y2 to
85// %y2' = getelementptr %y', 1
86// Finally, it fixes the poison in %y' so that
87// %y' = phi float addrspace(3)* [ %input, %y2' ]
88//
89//===----------------------------------------------------------------------===//
90
92#include "llvm/ADT/ArrayRef.h"
93#include "llvm/ADT/DenseMap.h"
94#include "llvm/ADT/DenseSet.h"
95#include "llvm/ADT/SetVector.h"
100#include "llvm/IR/BasicBlock.h"
101#include "llvm/IR/Constant.h"
102#include "llvm/IR/Constants.h"
103#include "llvm/IR/Dominators.h"
104#include "llvm/IR/Function.h"
105#include "llvm/IR/IRBuilder.h"
106#include "llvm/IR/InstIterator.h"
107#include "llvm/IR/Instruction.h"
108#include "llvm/IR/Instructions.h"
110#include "llvm/IR/Intrinsics.h"
111#include "llvm/IR/LLVMContext.h"
112#include "llvm/IR/Operator.h"
113#include "llvm/IR/PassManager.h"
114#include "llvm/IR/Type.h"
115#include "llvm/IR/Use.h"
116#include "llvm/IR/User.h"
117#include "llvm/IR/Value.h"
118#include "llvm/IR/ValueHandle.h"
120#include "llvm/Pass.h"
121#include "llvm/Support/Casting.h"
124#include "llvm/Support/Debug.h"
130#include <cassert>
131#include <iterator>
132#include <limits>
133#include <utility>
134#include <vector>
135
136#define DEBUG_TYPE "infer-address-spaces"
137
138using namespace llvm;
139
141 "assume-default-is-flat-addrspace", cl::init(false), cl::ReallyHidden,
142 cl::desc("The default address space is assumed as the flat address space. "
143 "This is mainly for test purpose."));
144
145static const unsigned UninitializedAddressSpace =
146 std::numeric_limits<unsigned>::max();
147
148namespace {
149
150using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>;
151// Different from ValueToAddrSpaceMapTy, where a new addrspace is inferred on
152// the *def* of a value, PredicatedAddrSpaceMapTy is map where a new
153// addrspace is inferred on the *use* of a pointer. This map is introduced to
154// infer addrspace from the addrspace predicate assumption built from assume
155// intrinsic. In that scenario, only specific uses (under valid assumption
156// context) could be inferred with a new addrspace.
157using PredicatedAddrSpaceMapTy =
159using PostorderStackTy = llvm::SmallVector<PointerIntPair<Value *, 1, bool>, 4>;
160
161class InferAddressSpaces : public FunctionPass {
162 unsigned FlatAddrSpace = 0;
163
164public:
165 static char ID;
166
167 InferAddressSpaces()
168 : FunctionPass(ID), FlatAddrSpace(UninitializedAddressSpace) {
170 }
171 InferAddressSpaces(unsigned AS) : FunctionPass(ID), FlatAddrSpace(AS) {
173 }
174
175 void getAnalysisUsage(AnalysisUsage &AU) const override {
176 AU.setPreservesCFG();
180 }
181
182 bool runOnFunction(Function &F) override;
183};
184
185class InferAddressSpacesImpl {
186 AssumptionCache &AC;
187 const DominatorTree *DT = nullptr;
188 const TargetTransformInfo *TTI = nullptr;
189 const DataLayout *DL = nullptr;
190
191 /// Target specific address space which uses of should be replaced if
192 /// possible.
193 unsigned FlatAddrSpace = 0;
194
195 // Try to update the address space of V. If V is updated, returns true and
196 // false otherwise.
197 bool updateAddressSpace(const Value &V,
198 ValueToAddrSpaceMapTy &InferredAddrSpace,
199 PredicatedAddrSpaceMapTy &PredicatedAS) const;
200
201 // Tries to infer the specific address space of each address expression in
202 // Postorder.
203 void inferAddressSpaces(ArrayRef<WeakTrackingVH> Postorder,
204 ValueToAddrSpaceMapTy &InferredAddrSpace,
205 PredicatedAddrSpaceMapTy &PredicatedAS) const;
206
207 bool isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const;
208
209 Value *cloneInstructionWithNewAddressSpace(
210 Instruction *I, unsigned NewAddrSpace,
211 const ValueToValueMapTy &ValueWithNewAddrSpace,
212 const PredicatedAddrSpaceMapTy &PredicatedAS,
213 SmallVectorImpl<const Use *> *PoisonUsesToFix) const;
214
215 // Changes the flat address expressions in function F to point to specific
216 // address spaces if InferredAddrSpace says so. Postorder is the postorder of
217 // all flat expressions in the use-def graph of function F.
218 bool
219 rewriteWithNewAddressSpaces(ArrayRef<WeakTrackingVH> Postorder,
220 const ValueToAddrSpaceMapTy &InferredAddrSpace,
221 const PredicatedAddrSpaceMapTy &PredicatedAS,
222 Function *F) const;
223
224 void appendsFlatAddressExpressionToPostorderStack(
225 Value *V, PostorderStackTy &PostorderStack,
226 DenseSet<Value *> &Visited) const;
227
228 bool rewriteIntrinsicOperands(IntrinsicInst *II, Value *OldV,
229 Value *NewV) const;
230 void collectRewritableIntrinsicOperands(IntrinsicInst *II,
231 PostorderStackTy &PostorderStack,
232 DenseSet<Value *> &Visited) const;
233
234 std::vector<WeakTrackingVH> collectFlatAddressExpressions(Function &F) const;
235
236 Value *cloneValueWithNewAddressSpace(
237 Value *V, unsigned NewAddrSpace,
238 const ValueToValueMapTy &ValueWithNewAddrSpace,
239 const PredicatedAddrSpaceMapTy &PredicatedAS,
240 SmallVectorImpl<const Use *> *PoisonUsesToFix) const;
241 unsigned joinAddressSpaces(unsigned AS1, unsigned AS2) const;
242
243 unsigned getPredicatedAddrSpace(const Value &V, Value *Opnd) const;
244
245public:
246 InferAddressSpacesImpl(AssumptionCache &AC, const DominatorTree *DT,
247 const TargetTransformInfo *TTI, unsigned FlatAddrSpace)
248 : AC(AC), DT(DT), TTI(TTI), FlatAddrSpace(FlatAddrSpace) {}
249 bool run(Function &F);
250};
251
252} // end anonymous namespace
253
254char InferAddressSpaces::ID = 0;
255
256INITIALIZE_PASS_BEGIN(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
257 false, false)
260INITIALIZE_PASS_END(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
262
263static Type *getPtrOrVecOfPtrsWithNewAS(Type *Ty, unsigned NewAddrSpace) {
264 assert(Ty->isPtrOrPtrVectorTy());
265 PointerType *NPT = PointerType::get(Ty->getContext(), NewAddrSpace);
266 return Ty->getWithNewType(NPT);
267}
268
269// Check whether that's no-op pointer bicast using a pair of
270// `ptrtoint`/`inttoptr` due to the missing no-op pointer bitcast over
271// different address spaces.
272static bool isNoopPtrIntCastPair(const Operator *I2P, const DataLayout &DL,
273 const TargetTransformInfo *TTI) {
274 assert(I2P->getOpcode() == Instruction::IntToPtr);
275 auto *P2I = dyn_cast<Operator>(I2P->getOperand(0));
276 if (!P2I || P2I->getOpcode() != Instruction::PtrToInt)
277 return false;
278 // Check it's really safe to treat that pair of `ptrtoint`/`inttoptr` as a
279 // no-op cast. Besides checking both of them are no-op casts, as the
280 // reinterpreted pointer may be used in other pointer arithmetic, we also
281 // need to double-check that through the target-specific hook. That ensures
282 // the underlying target also agrees that's a no-op address space cast and
283 // pointer bits are preserved.
284 // The current IR spec doesn't have clear rules on address space casts,
285 // especially a clear definition for pointer bits in non-default address
286 // spaces. It would be undefined if that pointer is dereferenced after an
287 // invalid reinterpret cast. Also, due to the unclearness for the meaning of
288 // bits in non-default address spaces in the current spec, the pointer
289 // arithmetic may also be undefined after invalid pointer reinterpret cast.
290 // However, as we confirm through the target hooks that it's a no-op
291 // addrspacecast, it doesn't matter since the bits should be the same.
292 unsigned P2IOp0AS = P2I->getOperand(0)->getType()->getPointerAddressSpace();
293 unsigned I2PAS = I2P->getType()->getPointerAddressSpace();
295 I2P->getOperand(0)->getType(), I2P->getType(),
296 DL) &&
298 P2I->getOperand(0)->getType(), P2I->getType(),
299 DL) &&
300 (P2IOp0AS == I2PAS || TTI->isNoopAddrSpaceCast(P2IOp0AS, I2PAS));
301}
302
303// Returns true if V is an address expression.
304// TODO: Currently, we consider only phi, bitcast, addrspacecast, and
305// getelementptr operators.
306static bool isAddressExpression(const Value &V, const DataLayout &DL,
307 const TargetTransformInfo *TTI) {
308 const Operator *Op = dyn_cast<Operator>(&V);
309 if (!Op)
310 return false;
311
312 switch (Op->getOpcode()) {
313 case Instruction::PHI:
314 assert(Op->getType()->isPtrOrPtrVectorTy());
315 return true;
316 case Instruction::BitCast:
317 case Instruction::AddrSpaceCast:
318 case Instruction::GetElementPtr:
319 return true;
320 case Instruction::Select:
321 return Op->getType()->isPtrOrPtrVectorTy();
322 case Instruction::Call: {
323 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(&V);
324 return II && II->getIntrinsicID() == Intrinsic::ptrmask;
325 }
326 case Instruction::IntToPtr:
327 return isNoopPtrIntCastPair(Op, DL, TTI);
328 default:
329 // That value is an address expression if it has an assumed address space.
331 }
332}
333
334// Returns the pointer operands of V.
335//
336// Precondition: V is an address expression.
339 const TargetTransformInfo *TTI) {
340 const Operator &Op = cast<Operator>(V);
341 switch (Op.getOpcode()) {
342 case Instruction::PHI: {
343 auto IncomingValues = cast<PHINode>(Op).incoming_values();
344 return {IncomingValues.begin(), IncomingValues.end()};
345 }
346 case Instruction::BitCast:
347 case Instruction::AddrSpaceCast:
348 case Instruction::GetElementPtr:
349 return {Op.getOperand(0)};
350 case Instruction::Select:
351 return {Op.getOperand(1), Op.getOperand(2)};
352 case Instruction::Call: {
353 const IntrinsicInst &II = cast<IntrinsicInst>(Op);
354 assert(II.getIntrinsicID() == Intrinsic::ptrmask &&
355 "unexpected intrinsic call");
356 return {II.getArgOperand(0)};
357 }
358 case Instruction::IntToPtr: {
360 auto *P2I = cast<Operator>(Op.getOperand(0));
361 return {P2I->getOperand(0)};
362 }
363 default:
364 llvm_unreachable("Unexpected instruction type.");
365 }
366}
367
368bool InferAddressSpacesImpl::rewriteIntrinsicOperands(IntrinsicInst *II,
369 Value *OldV,
370 Value *NewV) const {
371 Module *M = II->getParent()->getParent()->getParent();
372 Intrinsic::ID IID = II->getIntrinsicID();
373 switch (IID) {
374 case Intrinsic::objectsize:
375 case Intrinsic::masked_load: {
376 Type *DestTy = II->getType();
377 Type *SrcTy = NewV->getType();
378 Function *NewDecl = Intrinsic::getDeclaration(M, IID, {DestTy, SrcTy});
379 II->setArgOperand(0, NewV);
380 II->setCalledFunction(NewDecl);
381 return true;
382 }
383 case Intrinsic::ptrmask:
384 // This is handled as an address expression, not as a use memory operation.
385 return false;
386 case Intrinsic::masked_gather: {
387 Type *RetTy = II->getType();
388 Type *NewPtrTy = NewV->getType();
389 Function *NewDecl = Intrinsic::getDeclaration(M, IID, {RetTy, NewPtrTy});
390 II->setArgOperand(0, NewV);
391 II->setCalledFunction(NewDecl);
392 return true;
393 }
394 case Intrinsic::masked_store:
395 case Intrinsic::masked_scatter: {
396 Type *ValueTy = II->getOperand(0)->getType();
397 Type *NewPtrTy = NewV->getType();
398 Function *NewDecl =
399 Intrinsic::getDeclaration(M, II->getIntrinsicID(), {ValueTy, NewPtrTy});
400 II->setArgOperand(1, NewV);
401 II->setCalledFunction(NewDecl);
402 return true;
403 }
404 case Intrinsic::prefetch:
405 case Intrinsic::is_constant: {
406 Function *NewDecl =
407 Intrinsic::getDeclaration(M, II->getIntrinsicID(), {NewV->getType()});
408 II->setArgOperand(0, NewV);
409 II->setCalledFunction(NewDecl);
410 return true;
411 }
412 default: {
413 Value *Rewrite = TTI->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
414 if (!Rewrite)
415 return false;
416 if (Rewrite != II)
417 II->replaceAllUsesWith(Rewrite);
418 return true;
419 }
420 }
421}
422
423void InferAddressSpacesImpl::collectRewritableIntrinsicOperands(
424 IntrinsicInst *II, PostorderStackTy &PostorderStack,
425 DenseSet<Value *> &Visited) const {
426 auto IID = II->getIntrinsicID();
427 switch (IID) {
428 case Intrinsic::ptrmask:
429 case Intrinsic::objectsize:
430 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
431 PostorderStack, Visited);
432 break;
433 case Intrinsic::is_constant: {
434 Value *Ptr = II->getArgOperand(0);
435 if (Ptr->getType()->isPtrOrPtrVectorTy()) {
436 appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
437 Visited);
438 }
439
440 break;
441 }
442 case Intrinsic::masked_load:
443 case Intrinsic::masked_gather:
444 case Intrinsic::prefetch:
445 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
446 PostorderStack, Visited);
447 break;
448 case Intrinsic::masked_store:
449 case Intrinsic::masked_scatter:
450 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(1),
451 PostorderStack, Visited);
452 break;
453 default:
454 SmallVector<int, 2> OpIndexes;
455 if (TTI->collectFlatAddressOperands(OpIndexes, IID)) {
456 for (int Idx : OpIndexes) {
457 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(Idx),
458 PostorderStack, Visited);
459 }
460 }
461 break;
462 }
463}
464
465// Returns all flat address expressions in function F. The elements are
466// If V is an unvisited flat address expression, appends V to PostorderStack
467// and marks it as visited.
468void InferAddressSpacesImpl::appendsFlatAddressExpressionToPostorderStack(
469 Value *V, PostorderStackTy &PostorderStack,
470 DenseSet<Value *> &Visited) const {
471 assert(V->getType()->isPtrOrPtrVectorTy());
472
473 // Generic addressing expressions may be hidden in nested constant
474 // expressions.
475 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
476 // TODO: Look in non-address parts, like icmp operands.
477 if (isAddressExpression(*CE, *DL, TTI) && Visited.insert(CE).second)
478 PostorderStack.emplace_back(CE, false);
479
480 return;
481 }
482
483 if (V->getType()->getPointerAddressSpace() == FlatAddrSpace &&
484 isAddressExpression(*V, *DL, TTI)) {
485 if (Visited.insert(V).second) {
486 PostorderStack.emplace_back(V, false);
487
488 Operator *Op = cast<Operator>(V);
489 for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I) {
490 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op->getOperand(I))) {
491 if (isAddressExpression(*CE, *DL, TTI) && Visited.insert(CE).second)
492 PostorderStack.emplace_back(CE, false);
493 }
494 }
495 }
496 }
497}
498
499// Returns all flat address expressions in function F. The elements are ordered
500// in postorder.
501std::vector<WeakTrackingVH>
502InferAddressSpacesImpl::collectFlatAddressExpressions(Function &F) const {
503 // This function implements a non-recursive postorder traversal of a partial
504 // use-def graph of function F.
505 PostorderStackTy PostorderStack;
506 // The set of visited expressions.
507 DenseSet<Value *> Visited;
508
509 auto PushPtrOperand = [&](Value *Ptr) {
510 appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack, Visited);
511 };
512
513 // Look at operations that may be interesting accelerate by moving to a known
514 // address space. We aim at generating after loads and stores, but pure
515 // addressing calculations may also be faster.
516 for (Instruction &I : instructions(F)) {
517 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
518 PushPtrOperand(GEP->getPointerOperand());
519 } else if (auto *LI = dyn_cast<LoadInst>(&I))
520 PushPtrOperand(LI->getPointerOperand());
521 else if (auto *SI = dyn_cast<StoreInst>(&I))
522 PushPtrOperand(SI->getPointerOperand());
523 else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I))
524 PushPtrOperand(RMW->getPointerOperand());
525 else if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(&I))
526 PushPtrOperand(CmpX->getPointerOperand());
527 else if (auto *MI = dyn_cast<MemIntrinsic>(&I)) {
528 // For memset/memcpy/memmove, any pointer operand can be replaced.
529 PushPtrOperand(MI->getRawDest());
530
531 // Handle 2nd operand for memcpy/memmove.
532 if (auto *MTI = dyn_cast<MemTransferInst>(MI))
533 PushPtrOperand(MTI->getRawSource());
534 } else if (auto *II = dyn_cast<IntrinsicInst>(&I))
535 collectRewritableIntrinsicOperands(II, PostorderStack, Visited);
536 else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(&I)) {
537 if (Cmp->getOperand(0)->getType()->isPtrOrPtrVectorTy()) {
538 PushPtrOperand(Cmp->getOperand(0));
539 PushPtrOperand(Cmp->getOperand(1));
540 }
541 } else if (auto *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
542 PushPtrOperand(ASC->getPointerOperand());
543 } else if (auto *I2P = dyn_cast<IntToPtrInst>(&I)) {
544 if (isNoopPtrIntCastPair(cast<Operator>(I2P), *DL, TTI))
545 PushPtrOperand(cast<Operator>(I2P->getOperand(0))->getOperand(0));
546 } else if (auto *RI = dyn_cast<ReturnInst>(&I)) {
547 if (auto *RV = RI->getReturnValue();
548 RV && RV->getType()->isPtrOrPtrVectorTy())
549 PushPtrOperand(RV);
550 }
551 }
552
553 std::vector<WeakTrackingVH> Postorder; // The resultant postorder.
554 while (!PostorderStack.empty()) {
555 Value *TopVal = PostorderStack.back().getPointer();
556 // If the operands of the expression on the top are already explored,
557 // adds that expression to the resultant postorder.
558 if (PostorderStack.back().getInt()) {
559 if (TopVal->getType()->getPointerAddressSpace() == FlatAddrSpace)
560 Postorder.push_back(TopVal);
561 PostorderStack.pop_back();
562 continue;
563 }
564 // Otherwise, adds its operands to the stack and explores them.
565 PostorderStack.back().setInt(true);
566 // Skip values with an assumed address space.
568 for (Value *PtrOperand : getPointerOperands(*TopVal, *DL, TTI)) {
569 appendsFlatAddressExpressionToPostorderStack(PtrOperand, PostorderStack,
570 Visited);
571 }
572 }
573 }
574 return Postorder;
575}
576
577// A helper function for cloneInstructionWithNewAddressSpace. Returns the clone
578// of OperandUse.get() in the new address space. If the clone is not ready yet,
579// returns poison in the new address space as a placeholder.
581 const Use &OperandUse, unsigned NewAddrSpace,
582 const ValueToValueMapTy &ValueWithNewAddrSpace,
583 const PredicatedAddrSpaceMapTy &PredicatedAS,
584 SmallVectorImpl<const Use *> *PoisonUsesToFix) {
585 Value *Operand = OperandUse.get();
586
587 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(Operand->getType(), NewAddrSpace);
588
589 if (Constant *C = dyn_cast<Constant>(Operand))
590 return ConstantExpr::getAddrSpaceCast(C, NewPtrTy);
591
592 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand))
593 return NewOperand;
594
595 Instruction *Inst = cast<Instruction>(OperandUse.getUser());
596 auto I = PredicatedAS.find(std::make_pair(Inst, Operand));
597 if (I != PredicatedAS.end()) {
598 // Insert an addrspacecast on that operand before the user.
599 unsigned NewAS = I->second;
600 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(Operand->getType(), NewAS);
601 auto *NewI = new AddrSpaceCastInst(Operand, NewPtrTy);
602 NewI->insertBefore(Inst);
603 NewI->setDebugLoc(Inst->getDebugLoc());
604 return NewI;
605 }
606
607 PoisonUsesToFix->push_back(&OperandUse);
608 return PoisonValue::get(NewPtrTy);
609}
610
611// Returns a clone of `I` with its operands converted to those specified in
612// ValueWithNewAddrSpace. Due to potential cycles in the data flow graph, an
613// operand whose address space needs to be modified might not exist in
614// ValueWithNewAddrSpace. In that case, uses poison as a placeholder operand and
615// adds that operand use to PoisonUsesToFix so that caller can fix them later.
616//
617// Note that we do not necessarily clone `I`, e.g., if it is an addrspacecast
618// from a pointer whose type already matches. Therefore, this function returns a
619// Value* instead of an Instruction*.
620//
621// This may also return nullptr in the case the instruction could not be
622// rewritten.
623Value *InferAddressSpacesImpl::cloneInstructionWithNewAddressSpace(
624 Instruction *I, unsigned NewAddrSpace,
625 const ValueToValueMapTy &ValueWithNewAddrSpace,
626 const PredicatedAddrSpaceMapTy &PredicatedAS,
627 SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
628 Type *NewPtrType = getPtrOrVecOfPtrsWithNewAS(I->getType(), NewAddrSpace);
629
630 if (I->getOpcode() == Instruction::AddrSpaceCast) {
631 Value *Src = I->getOperand(0);
632 // Because `I` is flat, the source address space must be specific.
633 // Therefore, the inferred address space must be the source space, according
634 // to our algorithm.
635 assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
636 if (Src->getType() != NewPtrType)
637 return new BitCastInst(Src, NewPtrType);
638 return Src;
639 }
640
641 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
642 // Technically the intrinsic ID is a pointer typed argument, so specially
643 // handle calls early.
644 assert(II->getIntrinsicID() == Intrinsic::ptrmask);
646 II->getArgOperandUse(0), NewAddrSpace, ValueWithNewAddrSpace,
647 PredicatedAS, PoisonUsesToFix);
648 Value *Rewrite =
649 TTI->rewriteIntrinsicWithAddressSpace(II, II->getArgOperand(0), NewPtr);
650 if (Rewrite) {
651 assert(Rewrite != II && "cannot modify this pointer operation in place");
652 return Rewrite;
653 }
654
655 return nullptr;
656 }
657
658 unsigned AS = TTI->getAssumedAddrSpace(I);
659 if (AS != UninitializedAddressSpace) {
660 // For the assumed address space, insert an `addrspacecast` to make that
661 // explicit.
662 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(I->getType(), AS);
663 auto *NewI = new AddrSpaceCastInst(I, NewPtrTy);
664 NewI->insertAfter(I);
665 NewI->setDebugLoc(I->getDebugLoc());
666 return NewI;
667 }
668
669 // Computes the converted pointer operands.
670 SmallVector<Value *, 4> NewPointerOperands;
671 for (const Use &OperandUse : I->operands()) {
672 if (!OperandUse.get()->getType()->isPtrOrPtrVectorTy())
673 NewPointerOperands.push_back(nullptr);
674 else
676 OperandUse, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS,
677 PoisonUsesToFix));
678 }
679
680 switch (I->getOpcode()) {
681 case Instruction::BitCast:
682 return new BitCastInst(NewPointerOperands[0], NewPtrType);
683 case Instruction::PHI: {
684 assert(I->getType()->isPtrOrPtrVectorTy());
685 PHINode *PHI = cast<PHINode>(I);
686 PHINode *NewPHI = PHINode::Create(NewPtrType, PHI->getNumIncomingValues());
687 for (unsigned Index = 0; Index < PHI->getNumIncomingValues(); ++Index) {
688 unsigned OperandNo = PHINode::getOperandNumForIncomingValue(Index);
689 NewPHI->addIncoming(NewPointerOperands[OperandNo],
690 PHI->getIncomingBlock(Index));
691 }
692 return NewPHI;
693 }
694 case Instruction::GetElementPtr: {
695 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
697 GEP->getSourceElementType(), NewPointerOperands[0],
698 SmallVector<Value *, 4>(GEP->indices()));
699 NewGEP->setIsInBounds(GEP->isInBounds());
700 return NewGEP;
701 }
702 case Instruction::Select:
703 assert(I->getType()->isPtrOrPtrVectorTy());
704 return SelectInst::Create(I->getOperand(0), NewPointerOperands[1],
705 NewPointerOperands[2], "", nullptr, I);
706 case Instruction::IntToPtr: {
707 assert(isNoopPtrIntCastPair(cast<Operator>(I), *DL, TTI));
708 Value *Src = cast<Operator>(I->getOperand(0))->getOperand(0);
709 if (Src->getType() == NewPtrType)
710 return Src;
711
712 // If we had a no-op inttoptr/ptrtoint pair, we may still have inferred a
713 // source address space from a generic pointer source need to insert a cast
714 // back.
715 return CastInst::CreatePointerBitCastOrAddrSpaceCast(Src, NewPtrType);
716 }
717 default:
718 llvm_unreachable("Unexpected opcode");
719 }
720}
721
722// Similar to cloneInstructionWithNewAddressSpace, returns a clone of the
723// constant expression `CE` with its operands replaced as specified in
724// ValueWithNewAddrSpace.
726 ConstantExpr *CE, unsigned NewAddrSpace,
727 const ValueToValueMapTy &ValueWithNewAddrSpace, const DataLayout *DL,
728 const TargetTransformInfo *TTI) {
729 Type *TargetType =
730 CE->getType()->isPtrOrPtrVectorTy()
731 ? getPtrOrVecOfPtrsWithNewAS(CE->getType(), NewAddrSpace)
732 : CE->getType();
733
734 if (CE->getOpcode() == Instruction::AddrSpaceCast) {
735 // Because CE is flat, the source address space must be specific.
736 // Therefore, the inferred address space must be the source space according
737 // to our algorithm.
738 assert(CE->getOperand(0)->getType()->getPointerAddressSpace() ==
739 NewAddrSpace);
740 return ConstantExpr::getBitCast(CE->getOperand(0), TargetType);
741 }
742
743 if (CE->getOpcode() == Instruction::BitCast) {
744 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(CE->getOperand(0)))
745 return ConstantExpr::getBitCast(cast<Constant>(NewOperand), TargetType);
746 return ConstantExpr::getAddrSpaceCast(CE, TargetType);
747 }
748
749 if (CE->getOpcode() == Instruction::IntToPtr) {
750 assert(isNoopPtrIntCastPair(cast<Operator>(CE), *DL, TTI));
751 Constant *Src = cast<ConstantExpr>(CE->getOperand(0))->getOperand(0);
752 assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
753 return ConstantExpr::getBitCast(Src, TargetType);
754 }
755
756 // Computes the operands of the new constant expression.
757 bool IsNew = false;
758 SmallVector<Constant *, 4> NewOperands;
759 for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) {
760 Constant *Operand = CE->getOperand(Index);
761 // If the address space of `Operand` needs to be modified, the new operand
762 // with the new address space should already be in ValueWithNewAddrSpace
763 // because (1) the constant expressions we consider (i.e. addrspacecast,
764 // bitcast, and getelementptr) do not incur cycles in the data flow graph
765 // and (2) this function is called on constant expressions in postorder.
766 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) {
767 IsNew = true;
768 NewOperands.push_back(cast<Constant>(NewOperand));
769 continue;
770 }
771 if (auto *CExpr = dyn_cast<ConstantExpr>(Operand))
773 CExpr, NewAddrSpace, ValueWithNewAddrSpace, DL, TTI)) {
774 IsNew = true;
775 NewOperands.push_back(cast<Constant>(NewOperand));
776 continue;
777 }
778 // Otherwise, reuses the old operand.
779 NewOperands.push_back(Operand);
780 }
781
782 // If !IsNew, we will replace the Value with itself. However, replaced values
783 // are assumed to wrapped in an addrspacecast cast later so drop it now.
784 if (!IsNew)
785 return nullptr;
786
787 if (CE->getOpcode() == Instruction::GetElementPtr) {
788 // Needs to specify the source type while constructing a getelementptr
789 // constant expression.
790 return CE->getWithOperands(NewOperands, TargetType, /*OnlyIfReduced=*/false,
791 cast<GEPOperator>(CE)->getSourceElementType());
792 }
793
794 return CE->getWithOperands(NewOperands, TargetType);
795}
796
797// Returns a clone of the value `V`, with its operands replaced as specified in
798// ValueWithNewAddrSpace. This function is called on every flat address
799// expression whose address space needs to be modified, in postorder.
800//
801// See cloneInstructionWithNewAddressSpace for the meaning of PoisonUsesToFix.
802Value *InferAddressSpacesImpl::cloneValueWithNewAddressSpace(
803 Value *V, unsigned NewAddrSpace,
804 const ValueToValueMapTy &ValueWithNewAddrSpace,
805 const PredicatedAddrSpaceMapTy &PredicatedAS,
806 SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
807 // All values in Postorder are flat address expressions.
808 assert(V->getType()->getPointerAddressSpace() == FlatAddrSpace &&
809 isAddressExpression(*V, *DL, TTI));
810
811 if (Instruction *I = dyn_cast<Instruction>(V)) {
812 Value *NewV = cloneInstructionWithNewAddressSpace(
813 I, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS, PoisonUsesToFix);
814 if (Instruction *NewI = dyn_cast_or_null<Instruction>(NewV)) {
815 if (NewI->getParent() == nullptr) {
816 NewI->insertBefore(I);
817 NewI->takeName(I);
818 NewI->setDebugLoc(I->getDebugLoc());
819 }
820 }
821 return NewV;
822 }
823
825 cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace, DL, TTI);
826}
827
828// Defines the join operation on the address space lattice (see the file header
829// comments).
830unsigned InferAddressSpacesImpl::joinAddressSpaces(unsigned AS1,
831 unsigned AS2) const {
832 if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace)
833 return FlatAddrSpace;
834
835 if (AS1 == UninitializedAddressSpace)
836 return AS2;
837 if (AS2 == UninitializedAddressSpace)
838 return AS1;
839
840 // The join of two different specific address spaces is flat.
841 return (AS1 == AS2) ? AS1 : FlatAddrSpace;
842}
843
844bool InferAddressSpacesImpl::run(Function &F) {
845 DL = &F.getDataLayout();
846
848 FlatAddrSpace = 0;
849
850 if (FlatAddrSpace == UninitializedAddressSpace) {
851 FlatAddrSpace = TTI->getFlatAddressSpace();
852 if (FlatAddrSpace == UninitializedAddressSpace)
853 return false;
854 }
855
856 // Collects all flat address expressions in postorder.
857 std::vector<WeakTrackingVH> Postorder = collectFlatAddressExpressions(F);
858
859 // Runs a data-flow analysis to refine the address spaces of every expression
860 // in Postorder.
861 ValueToAddrSpaceMapTy InferredAddrSpace;
862 PredicatedAddrSpaceMapTy PredicatedAS;
863 inferAddressSpaces(Postorder, InferredAddrSpace, PredicatedAS);
864
865 // Changes the address spaces of the flat address expressions who are inferred
866 // to point to a specific address space.
867 return rewriteWithNewAddressSpaces(Postorder, InferredAddrSpace, PredicatedAS,
868 &F);
869}
870
871// Constants need to be tracked through RAUW to handle cases with nested
872// constant expressions, so wrap values in WeakTrackingVH.
873void InferAddressSpacesImpl::inferAddressSpaces(
874 ArrayRef<WeakTrackingVH> Postorder,
875 ValueToAddrSpaceMapTy &InferredAddrSpace,
876 PredicatedAddrSpaceMapTy &PredicatedAS) const {
877 SetVector<Value *> Worklist(Postorder.begin(), Postorder.end());
878 // Initially, all expressions are in the uninitialized address space.
879 for (Value *V : Postorder)
880 InferredAddrSpace[V] = UninitializedAddressSpace;
881
882 while (!Worklist.empty()) {
883 Value *V = Worklist.pop_back_val();
884
885 // Try to update the address space of the stack top according to the
886 // address spaces of its operands.
887 if (!updateAddressSpace(*V, InferredAddrSpace, PredicatedAS))
888 continue;
889
890 for (Value *User : V->users()) {
891 // Skip if User is already in the worklist.
892 if (Worklist.count(User))
893 continue;
894
895 auto Pos = InferredAddrSpace.find(User);
896 // Our algorithm only updates the address spaces of flat address
897 // expressions, which are those in InferredAddrSpace.
898 if (Pos == InferredAddrSpace.end())
899 continue;
900
901 // Function updateAddressSpace moves the address space down a lattice
902 // path. Therefore, nothing to do if User is already inferred as flat (the
903 // bottom element in the lattice).
904 if (Pos->second == FlatAddrSpace)
905 continue;
906
907 Worklist.insert(User);
908 }
909 }
910}
911
912unsigned InferAddressSpacesImpl::getPredicatedAddrSpace(const Value &V,
913 Value *Opnd) const {
914 const Instruction *I = dyn_cast<Instruction>(&V);
915 if (!I)
917
918 Opnd = Opnd->stripInBoundsOffsets();
919 for (auto &AssumeVH : AC.assumptionsFor(Opnd)) {
920 if (!AssumeVH)
921 continue;
922 CallInst *CI = cast<CallInst>(AssumeVH);
923 if (!isValidAssumeForContext(CI, I, DT))
924 continue;
925
926 const Value *Ptr;
927 unsigned AS;
928 std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(CI->getArgOperand(0));
929 if (Ptr)
930 return AS;
931 }
932
934}
935
936bool InferAddressSpacesImpl::updateAddressSpace(
937 const Value &V, ValueToAddrSpaceMapTy &InferredAddrSpace,
938 PredicatedAddrSpaceMapTy &PredicatedAS) const {
939 assert(InferredAddrSpace.count(&V));
940
941 LLVM_DEBUG(dbgs() << "Updating the address space of\n " << V << '\n');
942
943 // The new inferred address space equals the join of the address spaces
944 // of all its pointer operands.
945 unsigned NewAS = UninitializedAddressSpace;
946
947 const Operator &Op = cast<Operator>(V);
948 if (Op.getOpcode() == Instruction::Select) {
949 Value *Src0 = Op.getOperand(1);
950 Value *Src1 = Op.getOperand(2);
951
952 auto I = InferredAddrSpace.find(Src0);
953 unsigned Src0AS = (I != InferredAddrSpace.end())
954 ? I->second
955 : Src0->getType()->getPointerAddressSpace();
956
957 auto J = InferredAddrSpace.find(Src1);
958 unsigned Src1AS = (J != InferredAddrSpace.end())
959 ? J->second
960 : Src1->getType()->getPointerAddressSpace();
961
962 auto *C0 = dyn_cast<Constant>(Src0);
963 auto *C1 = dyn_cast<Constant>(Src1);
964
965 // If one of the inputs is a constant, we may be able to do a constant
966 // addrspacecast of it. Defer inferring the address space until the input
967 // address space is known.
968 if ((C1 && Src0AS == UninitializedAddressSpace) ||
969 (C0 && Src1AS == UninitializedAddressSpace))
970 return false;
971
972 if (C0 && isSafeToCastConstAddrSpace(C0, Src1AS))
973 NewAS = Src1AS;
974 else if (C1 && isSafeToCastConstAddrSpace(C1, Src0AS))
975 NewAS = Src0AS;
976 else
977 NewAS = joinAddressSpaces(Src0AS, Src1AS);
978 } else {
979 unsigned AS = TTI->getAssumedAddrSpace(&V);
980 if (AS != UninitializedAddressSpace) {
981 // Use the assumed address space directly.
982 NewAS = AS;
983 } else {
984 // Otherwise, infer the address space from its pointer operands.
985 for (Value *PtrOperand : getPointerOperands(V, *DL, TTI)) {
986 auto I = InferredAddrSpace.find(PtrOperand);
987 unsigned OperandAS;
988 if (I == InferredAddrSpace.end()) {
989 OperandAS = PtrOperand->getType()->getPointerAddressSpace();
990 if (OperandAS == FlatAddrSpace) {
991 // Check AC for assumption dominating V.
992 unsigned AS = getPredicatedAddrSpace(V, PtrOperand);
993 if (AS != UninitializedAddressSpace) {
995 << " deduce operand AS from the predicate addrspace "
996 << AS << '\n');
997 OperandAS = AS;
998 // Record this use with the predicated AS.
999 PredicatedAS[std::make_pair(&V, PtrOperand)] = OperandAS;
1000 }
1001 }
1002 } else
1003 OperandAS = I->second;
1004
1005 // join(flat, *) = flat. So we can break if NewAS is already flat.
1006 NewAS = joinAddressSpaces(NewAS, OperandAS);
1007 if (NewAS == FlatAddrSpace)
1008 break;
1009 }
1010 }
1011 }
1012
1013 unsigned OldAS = InferredAddrSpace.lookup(&V);
1014 assert(OldAS != FlatAddrSpace);
1015 if (OldAS == NewAS)
1016 return false;
1017
1018 // If any updates are made, grabs its users to the worklist because
1019 // their address spaces can also be possibly updated.
1020 LLVM_DEBUG(dbgs() << " to " << NewAS << '\n');
1021 InferredAddrSpace[&V] = NewAS;
1022 return true;
1023}
1024
1025/// Replace operand \p OpIdx in \p Inst, if the value is the same as \p OldVal
1026/// with \p NewVal.
1027static bool replaceOperandIfSame(Instruction *Inst, unsigned OpIdx,
1028 Value *OldVal, Value *NewVal) {
1029 Use &U = Inst->getOperandUse(OpIdx);
1030 if (U.get() == OldVal) {
1031 U.set(NewVal);
1032 return true;
1033 }
1034
1035 return false;
1036}
1037
1038template <typename InstrType>
1040 InstrType *MemInstr, unsigned AddrSpace,
1041 Value *OldV, Value *NewV) {
1042 if (!MemInstr->isVolatile() || TTI.hasVolatileVariant(MemInstr, AddrSpace)) {
1043 return replaceOperandIfSame(MemInstr, InstrType::getPointerOperandIndex(),
1044 OldV, NewV);
1045 }
1046
1047 return false;
1048}
1049
1050/// If \p OldV is used as the pointer operand of a compatible memory operation
1051/// \p Inst, replaces the pointer operand with NewV.
1052///
1053/// This covers memory instructions with a single pointer operand that can have
1054/// its address space changed by simply mutating the use to a new value.
1055///
1056/// \p returns true the user replacement was made.
1058 User *Inst, unsigned AddrSpace,
1059 Value *OldV, Value *NewV) {
1060 if (auto *LI = dyn_cast<LoadInst>(Inst))
1061 return replaceSimplePointerUse(TTI, LI, AddrSpace, OldV, NewV);
1062
1063 if (auto *SI = dyn_cast<StoreInst>(Inst))
1064 return replaceSimplePointerUse(TTI, SI, AddrSpace, OldV, NewV);
1065
1066 if (auto *RMW = dyn_cast<AtomicRMWInst>(Inst))
1067 return replaceSimplePointerUse(TTI, RMW, AddrSpace, OldV, NewV);
1068
1069 if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst))
1070 return replaceSimplePointerUse(TTI, CmpX, AddrSpace, OldV, NewV);
1071
1072 return false;
1073}
1074
1075/// Update memory intrinsic uses that require more complex processing than
1076/// simple memory instructions. These require re-mangling and may have multiple
1077/// pointer operands.
1079 Value *NewV) {
1080 IRBuilder<> B(MI);
1081 MDNode *TBAA = MI->getMetadata(LLVMContext::MD_tbaa);
1082 MDNode *ScopeMD = MI->getMetadata(LLVMContext::MD_alias_scope);
1083 MDNode *NoAliasMD = MI->getMetadata(LLVMContext::MD_noalias);
1084
1085 if (auto *MSI = dyn_cast<MemSetInst>(MI)) {
1086 B.CreateMemSet(NewV, MSI->getValue(), MSI->getLength(), MSI->getDestAlign(),
1087 false, // isVolatile
1088 TBAA, ScopeMD, NoAliasMD);
1089 } else if (auto *MTI = dyn_cast<MemTransferInst>(MI)) {
1090 Value *Src = MTI->getRawSource();
1091 Value *Dest = MTI->getRawDest();
1092
1093 // Be careful in case this is a self-to-self copy.
1094 if (Src == OldV)
1095 Src = NewV;
1096
1097 if (Dest == OldV)
1098 Dest = NewV;
1099
1100 if (isa<MemCpyInlineInst>(MTI)) {
1101 MDNode *TBAAStruct = MTI->getMetadata(LLVMContext::MD_tbaa_struct);
1102 B.CreateMemCpyInline(Dest, MTI->getDestAlign(), Src,
1103 MTI->getSourceAlign(), MTI->getLength(),
1104 false, // isVolatile
1105 TBAA, TBAAStruct, ScopeMD, NoAliasMD);
1106 } else if (isa<MemCpyInst>(MTI)) {
1107 MDNode *TBAAStruct = MTI->getMetadata(LLVMContext::MD_tbaa_struct);
1108 B.CreateMemCpy(Dest, MTI->getDestAlign(), Src, MTI->getSourceAlign(),
1109 MTI->getLength(),
1110 false, // isVolatile
1111 TBAA, TBAAStruct, ScopeMD, NoAliasMD);
1112 } else {
1113 assert(isa<MemMoveInst>(MTI));
1114 B.CreateMemMove(Dest, MTI->getDestAlign(), Src, MTI->getSourceAlign(),
1115 MTI->getLength(),
1116 false, // isVolatile
1117 TBAA, ScopeMD, NoAliasMD);
1118 }
1119 } else
1120 llvm_unreachable("unhandled MemIntrinsic");
1121
1122 MI->eraseFromParent();
1123 return true;
1124}
1125
1126// \p returns true if it is OK to change the address space of constant \p C with
1127// a ConstantExpr addrspacecast.
1128bool InferAddressSpacesImpl::isSafeToCastConstAddrSpace(Constant *C,
1129 unsigned NewAS) const {
1131
1132 unsigned SrcAS = C->getType()->getPointerAddressSpace();
1133 if (SrcAS == NewAS || isa<UndefValue>(C))
1134 return true;
1135
1136 // Prevent illegal casts between different non-flat address spaces.
1137 if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace)
1138 return false;
1139
1140 if (isa<ConstantPointerNull>(C))
1141 return true;
1142
1143 if (auto *Op = dyn_cast<Operator>(C)) {
1144 // If we already have a constant addrspacecast, it should be safe to cast it
1145 // off.
1146 if (Op->getOpcode() == Instruction::AddrSpaceCast)
1147 return isSafeToCastConstAddrSpace(cast<Constant>(Op->getOperand(0)),
1148 NewAS);
1149
1150 if (Op->getOpcode() == Instruction::IntToPtr &&
1151 Op->getType()->getPointerAddressSpace() == FlatAddrSpace)
1152 return true;
1153 }
1154
1155 return false;
1156}
1157
1160 User *CurUser = I->getUser();
1161 ++I;
1162
1163 while (I != End && I->getUser() == CurUser)
1164 ++I;
1165
1166 return I;
1167}
1168
1169bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces(
1170 ArrayRef<WeakTrackingVH> Postorder,
1171 const ValueToAddrSpaceMapTy &InferredAddrSpace,
1172 const PredicatedAddrSpaceMapTy &PredicatedAS, Function *F) const {
1173 // For each address expression to be modified, creates a clone of it with its
1174 // pointer operands converted to the new address space. Since the pointer
1175 // operands are converted, the clone is naturally in the new address space by
1176 // construction.
1177 ValueToValueMapTy ValueWithNewAddrSpace;
1178 SmallVector<const Use *, 32> PoisonUsesToFix;
1179 for (Value *V : Postorder) {
1180 unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
1181
1182 // In some degenerate cases (e.g. invalid IR in unreachable code), we may
1183 // not even infer the value to have its original address space.
1184 if (NewAddrSpace == UninitializedAddressSpace)
1185 continue;
1186
1187 if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
1188 Value *New =
1189 cloneValueWithNewAddressSpace(V, NewAddrSpace, ValueWithNewAddrSpace,
1190 PredicatedAS, &PoisonUsesToFix);
1191 if (New)
1192 ValueWithNewAddrSpace[V] = New;
1193 }
1194 }
1195
1196 if (ValueWithNewAddrSpace.empty())
1197 return false;
1198
1199 // Fixes all the poison uses generated by cloneInstructionWithNewAddressSpace.
1200 for (const Use *PoisonUse : PoisonUsesToFix) {
1201 User *V = PoisonUse->getUser();
1202 User *NewV = cast_or_null<User>(ValueWithNewAddrSpace.lookup(V));
1203 if (!NewV)
1204 continue;
1205
1206 unsigned OperandNo = PoisonUse->getOperandNo();
1207 assert(isa<PoisonValue>(NewV->getOperand(OperandNo)));
1208 NewV->setOperand(OperandNo, ValueWithNewAddrSpace.lookup(PoisonUse->get()));
1209 }
1210
1211 SmallVector<Instruction *, 16> DeadInstructions;
1212 ValueToValueMapTy VMap;
1214
1215 // Replaces the uses of the old address expressions with the new ones.
1216 for (const WeakTrackingVH &WVH : Postorder) {
1217 assert(WVH && "value was unexpectedly deleted");
1218 Value *V = WVH;
1219 Value *NewV = ValueWithNewAddrSpace.lookup(V);
1220 if (NewV == nullptr)
1221 continue;
1222
1223 LLVM_DEBUG(dbgs() << "Replacing the uses of " << *V << "\n with\n "
1224 << *NewV << '\n');
1225
1226 if (Constant *C = dyn_cast<Constant>(V)) {
1227 Constant *Replace =
1228 ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV), C->getType());
1229 if (C != Replace) {
1230 LLVM_DEBUG(dbgs() << "Inserting replacement const cast: " << Replace
1231 << ": " << *Replace << '\n');
1232 SmallVector<User *, 16> WorkList;
1233 for (User *U : make_early_inc_range(C->users())) {
1234 if (auto *I = dyn_cast<Instruction>(U)) {
1235 if (I->getFunction() == F)
1236 I->replaceUsesOfWith(C, Replace);
1237 } else {
1238 WorkList.append(U->user_begin(), U->user_end());
1239 }
1240 }
1241 if (!WorkList.empty()) {
1242 VMap[C] = Replace;
1243 DenseSet<User *> Visited{WorkList.begin(), WorkList.end()};
1244 while (!WorkList.empty()) {
1245 User *U = WorkList.pop_back_val();
1246 if (auto *I = dyn_cast<Instruction>(U)) {
1247 if (I->getFunction() == F)
1248 VMapper.remapInstruction(*I);
1249 continue;
1250 }
1251 for (User *U2 : U->users())
1252 if (Visited.insert(U2).second)
1253 WorkList.push_back(U2);
1254 }
1255 }
1256 V = Replace;
1257 }
1258 }
1259
1260 Value::use_iterator I, E, Next;
1261 for (I = V->use_begin(), E = V->use_end(); I != E;) {
1262 Use &U = *I;
1263 User *CurUser = U.getUser();
1264
1265 // Some users may see the same pointer operand in multiple operands. Skip
1266 // to the next instruction.
1267 I = skipToNextUser(I, E);
1268
1269 unsigned AddrSpace = V->getType()->getPointerAddressSpace();
1270 if (replaceIfSimplePointerUse(*TTI, CurUser, AddrSpace, V, NewV))
1271 continue;
1272
1273 // Skip if the current user is the new value itself.
1274 if (CurUser == NewV)
1275 continue;
1276
1277 if (auto *CurUserI = dyn_cast<Instruction>(CurUser);
1278 CurUserI && CurUserI->getFunction() != F)
1279 continue;
1280
1281 // Handle more complex cases like intrinsic that need to be remangled.
1282 if (auto *MI = dyn_cast<MemIntrinsic>(CurUser)) {
1283 if (!MI->isVolatile() && handleMemIntrinsicPtrUse(MI, V, NewV))
1284 continue;
1285 }
1286
1287 if (auto *II = dyn_cast<IntrinsicInst>(CurUser)) {
1288 if (rewriteIntrinsicOperands(II, V, NewV))
1289 continue;
1290 }
1291
1292 if (isa<Instruction>(CurUser)) {
1293 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) {
1294 // If we can infer that both pointers are in the same addrspace,
1295 // transform e.g.
1296 // %cmp = icmp eq float* %p, %q
1297 // into
1298 // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
1299
1300 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1301 int SrcIdx = U.getOperandNo();
1302 int OtherIdx = (SrcIdx == 0) ? 1 : 0;
1303 Value *OtherSrc = Cmp->getOperand(OtherIdx);
1304
1305 if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) {
1306 if (OtherNewV->getType()->getPointerAddressSpace() == NewAS) {
1307 Cmp->setOperand(OtherIdx, OtherNewV);
1308 Cmp->setOperand(SrcIdx, NewV);
1309 continue;
1310 }
1311 }
1312
1313 // Even if the type mismatches, we can cast the constant.
1314 if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
1315 if (isSafeToCastConstAddrSpace(KOtherSrc, NewAS)) {
1316 Cmp->setOperand(SrcIdx, NewV);
1317 Cmp->setOperand(OtherIdx, ConstantExpr::getAddrSpaceCast(
1318 KOtherSrc, NewV->getType()));
1319 continue;
1320 }
1321 }
1322 }
1323
1324 if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUser)) {
1325 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1326 if (ASC->getDestAddressSpace() == NewAS) {
1327 ASC->replaceAllUsesWith(NewV);
1328 DeadInstructions.push_back(ASC);
1329 continue;
1330 }
1331 }
1332
1333 // Otherwise, replaces the use with flat(NewV).
1334 if (Instruction *VInst = dyn_cast<Instruction>(V)) {
1335 // Don't create a copy of the original addrspacecast.
1336 if (U == V && isa<AddrSpaceCastInst>(V))
1337 continue;
1338
1339 // Insert the addrspacecast after NewV.
1340 BasicBlock::iterator InsertPos;
1341 if (Instruction *NewVInst = dyn_cast<Instruction>(NewV))
1342 InsertPos = std::next(NewVInst->getIterator());
1343 else
1344 InsertPos = std::next(VInst->getIterator());
1345
1346 while (isa<PHINode>(InsertPos))
1347 ++InsertPos;
1348 // This instruction may contain multiple uses of V, update them all.
1349 CurUser->replaceUsesOfWith(
1350 V, new AddrSpaceCastInst(NewV, V->getType(), "", InsertPos));
1351 } else {
1352 CurUser->replaceUsesOfWith(
1353 V, ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV),
1354 V->getType()));
1355 }
1356 }
1357 }
1358
1359 if (V->use_empty()) {
1360 if (Instruction *I = dyn_cast<Instruction>(V))
1361 DeadInstructions.push_back(I);
1362 }
1363 }
1364
1365 for (Instruction *I : DeadInstructions)
1367
1368 return true;
1369}
1370
1371bool InferAddressSpaces::runOnFunction(Function &F) {
1372 if (skipFunction(F))
1373 return false;
1374
1375 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1376 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
1377 return InferAddressSpacesImpl(
1378 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), DT,
1379 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F),
1380 FlatAddrSpace)
1381 .run(F);
1382}
1383
1385 return new InferAddressSpaces(AddressSpace);
1386}
1387
1389 : FlatAddrSpace(UninitializedAddressSpace) {}
1391 : FlatAddrSpace(AddressSpace) {}
1392
1395 bool Changed =
1396 InferAddressSpacesImpl(AM.getResult<AssumptionAnalysis>(F),
1398 &AM.getResult<TargetIRAnalysis>(F), FlatAddrSpace)
1399 .run(F);
1400 if (Changed) {
1404 return PA;
1405 }
1406 return PreservedAnalyses::all();
1407}
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
bool End
Definition: ELF_riscv.cpp:480
Hexagon Common GEP
IRTranslator LLVM IR MI
This defines the Use class.
static bool replaceIfSimplePointerUse(const TargetTransformInfo &TTI, User *Inst, unsigned AddrSpace, Value *OldV, Value *NewV)
If OldV is used as the pointer operand of a compatible memory operation Inst, replaces the pointer op...
static bool replaceOperandIfSame(Instruction *Inst, unsigned OpIdx, Value *OldVal, Value *NewVal)
Replace operand OpIdx in Inst, if the value is the same as OldVal with NewVal.
static cl::opt< bool > AssumeDefaultIsFlatAddressSpace("assume-default-is-flat-addrspace", cl::init(false), cl::ReallyHidden, cl::desc("The default address space is assumed as the flat address space. " "This is mainly for test purpose."))
static bool isNoopPtrIntCastPair(const Operator *I2P, const DataLayout &DL, const TargetTransformInfo *TTI)
static bool isAddressExpression(const Value &V, const DataLayout &DL, const TargetTransformInfo *TTI)
static bool handleMemIntrinsicPtrUse(MemIntrinsic *MI, Value *OldV, Value *NewV)
Update memory intrinsic uses that require more complex processing than simple memory instructions.
Infer address spaces
static SmallVector< Value *, 2 > getPointerOperands(const Value &V, const DataLayout &DL, const TargetTransformInfo *TTI)
static Value * operandWithNewAddressSpaceOrCreatePoison(const Use &OperandUse, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace, const PredicatedAddrSpaceMapTy &PredicatedAS, SmallVectorImpl< const Use * > *PoisonUsesToFix)
static Value * cloneConstantExprWithNewAddressSpace(ConstantExpr *CE, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace, const DataLayout *DL, const TargetTransformInfo *TTI)
static Value::use_iterator skipToNextUser(Value::use_iterator I, Value::use_iterator End)
Infer address static false Type * getPtrOrVecOfPtrsWithNewAS(Type *Ty, unsigned NewAddrSpace)
static bool replaceSimplePointerUse(const TargetTransformInfo &TTI, InstrType *MemInstr, unsigned AddrSpace, Value *OldV, Value *NewV)
#define DEBUG_TYPE
static const unsigned UninitializedAddressSpace
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This pass exposes codegen information to IR-level passes.
This class represents a conversion between pointers from one address space to another.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:424
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:154
iterator begin() const
Definition: ArrayRef.h:153
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
This class represents a no-op cast from one type to another.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
This class represents a function call, abstracting a target machine's calling convention.
static CastInst * CreatePointerBitCastOrAddrSpaceCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast or an AddrSpaceCast cast instruction.
static bool isNoopCast(Instruction::CastOps Opcode, Type *SrcTy, Type *DstTy, const DataLayout &DL)
A no-op cast is one that can be effected without changing any bits.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:1097
static Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2307
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2295
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:109
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:915
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition: Instructions.h:938
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2674
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:466
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Metadata node.
Definition: Metadata.h:1069
This is the common base class for memset/memcpy/memmove.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:42
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static unsigned getOperandNumForIncomingValue(unsigned i)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1852
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool empty() const
Definition: SmallVector.h:95
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:587
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:697
void push_back(const T &Elt)
Definition: SmallVector.h:427
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
unsigned getAssumedAddrSpace(const Value *V) const
bool isNoopAddrSpaceCast(unsigned FromAS, unsigned ToAS) const
std::pair< const Value *, unsigned > getPredicatedAddrSpace(const Value *V) const
bool collectFlatAddressOperands(SmallVectorImpl< int > &OpIndexes, Intrinsic::ID IID) const
Return any intrinsic address operand indexes which may be rewritten if they use a flat address space ...
Value * rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, Value *OldV, Value *NewV) const
Rewrite intrinsic call II such that OldV will be replaced with NewV, which has a different address sp...
unsigned getFlatAddressSpace() const
Returns the address space ID for a target's 'flat' address space.
bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) const
Return true if the given instruction (assumed to be a memory access instruction) has a volatile varia...
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
Value * get() const
Definition: Use.h:66
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:21
const Use & getOperandUse(unsigned i) const
Definition: User.h:182
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: ValueMap.h:164
bool empty() const
Definition: ValueMap.h:139
Context for (re-)mapping values (and metadata).
Definition: ValueMapper.h:149
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition: Value.cpp:786
use_iterator_impl< Use > use_iterator
Definition: Value.h:353
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:204
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
InstrType
This represents what is and is not supported when finding similarity in Instructions.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1539
@ ReallyHidden
Definition: CommandLine.h:138
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:540
AddressSpace
Definition: NVPTXBaseInfo.h:21
void initializeInferAddressSpacesPass(PassRegistry &)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:94
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:76
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createInferAddressSpacesPass(unsigned AddressSpace=~0u)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)