LLVM 23.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"
123#include "llvm/Support/Debug.h"
129#include <cassert>
130#include <iterator>
131#include <limits>
132#include <utility>
133#include <vector>
134
135#define DEBUG_TYPE "infer-address-spaces"
136
137using namespace llvm;
138
140 "assume-default-is-flat-addrspace", cl::init(false), cl::ReallyHidden,
141 cl::desc("The default address space is assumed as the flat address space. "
142 "This is mainly for test purpose."));
143
144static const unsigned UninitializedAddressSpace =
145 std::numeric_limits<unsigned>::max();
146
147namespace {
148
149using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>;
150// Different from ValueToAddrSpaceMapTy, where a new addrspace is inferred on
151// the *def* of a value, PredicatedAddrSpaceMapTy is map where a new
152// addrspace is inferred on the *use* of a pointer. This map is introduced to
153// infer addrspace from the addrspace predicate assumption built from assume
154// intrinsic. In that scenario, only specific uses (under valid assumption
155// context) could be inferred with a new addrspace.
156using PredicatedAddrSpaceMapTy =
158using PostorderStackTy = llvm::SmallVector<PointerIntPair<Value *, 1, bool>, 4>;
159
160class InferAddressSpaces : public FunctionPass {
161 unsigned FlatAddrSpace = 0;
162
163public:
164 static char ID;
165
166 InferAddressSpaces()
167 : FunctionPass(ID), FlatAddrSpace(UninitializedAddressSpace) {
169 }
170 InferAddressSpaces(unsigned AS) : FunctionPass(ID), FlatAddrSpace(AS) {
172 }
173
174 void getAnalysisUsage(AnalysisUsage &AU) const override {
175 AU.setPreservesCFG();
176 AU.addPreserved<DominatorTreeWrapperPass>();
177 AU.addRequired<AssumptionCacheTracker>();
178 AU.addRequired<TargetTransformInfoWrapperPass>();
179 }
180
181 bool runOnFunction(Function &F) override;
182};
183
184class InferAddressSpacesImpl {
185 AssumptionCache &AC;
186 Function *F = nullptr;
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 *clonePtrMaskWithNewAddressSpace(
210 IntrinsicInst *I, unsigned NewAddrSpace,
211 const ValueToValueMapTy &ValueWithNewAddrSpace,
212 const PredicatedAddrSpaceMapTy &PredicatedAS,
213 SmallVectorImpl<const Use *> *PoisonUsesToFix) const;
214
215 Value *cloneInstructionWithNewAddressSpace(
216 Instruction *I, unsigned NewAddrSpace,
217 const ValueToValueMapTy &ValueWithNewAddrSpace,
218 const PredicatedAddrSpaceMapTy &PredicatedAS,
219 SmallVectorImpl<const Use *> *PoisonUsesToFix) const;
220
221 void performPointerReplacement(
222 Value *V, Value *NewV, Use &U, ValueToValueMapTy &ValueWithNewAddrSpace,
223 SmallVectorImpl<Instruction *> &DeadInstructions) const;
224
225 // Changes the flat address expressions in function F to point to specific
226 // address spaces if InferredAddrSpace says so. Postorder is the postorder of
227 // all flat expressions in the use-def graph of function F.
228 bool rewriteWithNewAddressSpaces(
229 ArrayRef<WeakTrackingVH> Postorder,
230 const ValueToAddrSpaceMapTy &InferredAddrSpace,
231 const PredicatedAddrSpaceMapTy &PredicatedAS) const;
232
233 void appendsFlatAddressExpressionToPostorderStack(
234 Value *V, PostorderStackTy &PostorderStack,
235 DenseSet<Value *> &Visited) const;
236
237 bool rewriteIntrinsicOperands(IntrinsicInst *II, Value *OldV,
238 Value *NewV) const;
239 void collectRewritableIntrinsicOperands(IntrinsicInst *II,
240 PostorderStackTy &PostorderStack,
241 DenseSet<Value *> &Visited) const;
242
243 std::vector<WeakTrackingVH> collectFlatAddressExpressions(Function &F) const;
244
245 Value *cloneValueWithNewAddressSpace(
246 Value *V, unsigned NewAddrSpace,
247 const ValueToValueMapTy &ValueWithNewAddrSpace,
248 const PredicatedAddrSpaceMapTy &PredicatedAS,
249 SmallVectorImpl<const Use *> *PoisonUsesToFix) const;
250 unsigned joinAddressSpaces(unsigned AS1, unsigned AS2) const;
251
252 unsigned getPredicatedAddrSpace(const Value &PtrV,
253 const Value *UserCtx) const;
254
255public:
256 InferAddressSpacesImpl(AssumptionCache &AC, const DominatorTree *DT,
257 const TargetTransformInfo *TTI, unsigned FlatAddrSpace)
258 : AC(AC), DT(DT), TTI(TTI), FlatAddrSpace(FlatAddrSpace) {}
259 bool run(Function &F);
260};
261
262} // end anonymous namespace
263
264char InferAddressSpaces::ID = 0;
265
266INITIALIZE_PASS_BEGIN(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
267 false, false)
270INITIALIZE_PASS_END(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
272
273static Type *getPtrOrVecOfPtrsWithNewAS(Type *Ty, unsigned NewAddrSpace) {
274 assert(Ty->isPtrOrPtrVectorTy());
275 PointerType *NPT = PointerType::get(Ty->getContext(), NewAddrSpace);
276 return Ty->getWithNewType(NPT);
277}
278
279// Check whether that's no-op pointer bicast using a pair of
280// `ptrtoint`/`inttoptr` due to the missing no-op pointer bitcast over
281// different address spaces.
282static bool isNoopPtrIntCastPair(const Operator *I2P, const DataLayout &DL,
283 const TargetTransformInfo *TTI) {
284 assert(I2P->getOpcode() == Instruction::IntToPtr);
285 auto *P2I = dyn_cast<Operator>(I2P->getOperand(0));
286 if (!P2I || P2I->getOpcode() != Instruction::PtrToInt)
287 return false;
288 // Check it's really safe to treat that pair of `ptrtoint`/`inttoptr` as a
289 // no-op cast. Besides checking both of them are no-op casts, as the
290 // reinterpreted pointer may be used in other pointer arithmetic, we also
291 // need to double-check that through the target-specific hook. That ensures
292 // the underlying target also agrees that's a no-op address space cast and
293 // pointer bits are preserved.
294 // The current IR spec doesn't have clear rules on address space casts,
295 // especially a clear definition for pointer bits in non-default address
296 // spaces. It would be undefined if that pointer is dereferenced after an
297 // invalid reinterpret cast. Also, due to the unclearness for the meaning of
298 // bits in non-default address spaces in the current spec, the pointer
299 // arithmetic may also be undefined after invalid pointer reinterpret cast.
300 // However, as we confirm through the target hooks that it's a no-op
301 // addrspacecast, it doesn't matter since the bits should be the same.
302 unsigned P2IOp0AS = P2I->getOperand(0)->getType()->getPointerAddressSpace();
303 unsigned I2PAS = I2P->getType()->getPointerAddressSpace();
305 I2P->getOperand(0)->getType(), I2P->getType(),
306 DL) &&
308 P2I->getOperand(0)->getType(), P2I->getType(),
309 DL) &&
310 (P2IOp0AS == I2PAS || TTI->isNoopAddrSpaceCast(P2IOp0AS, I2PAS));
311}
312
313// Returns true if V is an address expression.
314// TODO: Currently, we only consider:
315// - arguments
316// - phi, bitcast, addrspacecast, and getelementptr operators
317static bool isAddressExpression(const Value &V, const DataLayout &DL,
318 const TargetTransformInfo *TTI) {
319
320 if (const Argument *Arg = dyn_cast<Argument>(&V))
321 return Arg->getType()->isPointerTy() &&
322 TTI->getAssumedAddrSpace(&V) != UninitializedAddressSpace;
323
324 const Operator *Op = dyn_cast<Operator>(&V);
325 if (!Op)
326 return false;
327
328 switch (Op->getOpcode()) {
329 case Instruction::PHI:
330 assert(Op->getType()->isPtrOrPtrVectorTy());
331 return true;
332 case Instruction::BitCast:
333 case Instruction::AddrSpaceCast:
334 case Instruction::GetElementPtr:
335 return true;
336 case Instruction::Select:
337 return Op->getType()->isPtrOrPtrVectorTy();
338 case Instruction::Call: {
340 return II && II->getIntrinsicID() == Intrinsic::ptrmask;
341 }
342 case Instruction::IntToPtr:
343 return isNoopPtrIntCastPair(Op, DL, TTI);
344 default:
345 // That value is an address expression if it has an assumed address space.
346 return TTI->getAssumedAddrSpace(&V) != UninitializedAddressSpace;
347 }
348}
349
350// Returns the pointer operands of V.
351//
352// Precondition: V is an address expression.
355 const TargetTransformInfo *TTI) {
356 if (isa<Argument>(&V))
357 return {};
358
359 const Operator &Op = cast<Operator>(V);
360 switch (Op.getOpcode()) {
361 case Instruction::PHI: {
362 auto IncomingValues = cast<PHINode>(Op).incoming_values();
363 return {IncomingValues.begin(), IncomingValues.end()};
364 }
365 case Instruction::BitCast:
366 case Instruction::AddrSpaceCast:
367 case Instruction::GetElementPtr:
368 return {Op.getOperand(0)};
369 case Instruction::Select:
370 return {Op.getOperand(1), Op.getOperand(2)};
371 case Instruction::Call: {
373 assert(II.getIntrinsicID() == Intrinsic::ptrmask &&
374 "unexpected intrinsic call");
375 return {II.getArgOperand(0)};
376 }
377 case Instruction::IntToPtr: {
379 auto *P2I = cast<Operator>(Op.getOperand(0));
380 return {P2I->getOperand(0)};
381 }
382 default:
383 llvm_unreachable("Unexpected instruction type.");
384 }
385}
386
387bool InferAddressSpacesImpl::rewriteIntrinsicOperands(IntrinsicInst *II,
388 Value *OldV,
389 Value *NewV) const {
390 Module *M = II->getParent()->getParent()->getParent();
391 Intrinsic::ID IID = II->getIntrinsicID();
392 switch (IID) {
393 case Intrinsic::objectsize:
394 case Intrinsic::masked_load: {
395 Type *DestTy = II->getType();
396 Type *SrcTy = NewV->getType();
397 Function *NewDecl =
398 Intrinsic::getOrInsertDeclaration(M, IID, {DestTy, SrcTy});
399 II->setArgOperand(0, NewV);
400 II->setCalledFunction(NewDecl);
401 return true;
402 }
403 case Intrinsic::ptrmask:
404 // This is handled as an address expression, not as a use memory operation.
405 return false;
406 case Intrinsic::masked_gather: {
407 Type *RetTy = II->getType();
408 Type *NewPtrTy = NewV->getType();
409 Function *NewDecl =
410 Intrinsic::getOrInsertDeclaration(M, IID, {RetTy, NewPtrTy});
411 II->setArgOperand(0, NewV);
412 II->setCalledFunction(NewDecl);
413 return true;
414 }
415 case Intrinsic::masked_store:
416 case Intrinsic::masked_scatter: {
417 Type *ValueTy = II->getOperand(0)->getType();
418 Type *NewPtrTy = NewV->getType();
420 M, II->getIntrinsicID(), {ValueTy, NewPtrTy});
421 II->setArgOperand(1, NewV);
422 II->setCalledFunction(NewDecl);
423 return true;
424 }
425 case Intrinsic::prefetch:
426 case Intrinsic::is_constant: {
428 M, II->getIntrinsicID(), {NewV->getType()});
429 II->setArgOperand(0, NewV);
430 II->setCalledFunction(NewDecl);
431 return true;
432 }
433 case Intrinsic::fake_use: {
434 II->replaceUsesOfWith(OldV, NewV);
435 return true;
436 }
437 case Intrinsic::lifetime_start:
438 case Intrinsic::lifetime_end: {
439 // Always force lifetime markers to work directly on the alloca.
440 NewV = NewV->stripPointerCasts();
442 M, II->getIntrinsicID(), {NewV->getType()});
443 II->setArgOperand(0, NewV);
444 II->setCalledFunction(NewDecl);
445 return true;
446 }
447 default: {
448 Value *Rewrite = TTI->rewriteIntrinsicWithAddressSpace(II, OldV, NewV);
449 if (!Rewrite)
450 return false;
451 if (Rewrite != II)
452 II->replaceAllUsesWith(Rewrite);
453 return true;
454 }
455 }
456}
457
458void InferAddressSpacesImpl::collectRewritableIntrinsicOperands(
459 IntrinsicInst *II, PostorderStackTy &PostorderStack,
460 DenseSet<Value *> &Visited) const {
461 auto IID = II->getIntrinsicID();
462 switch (IID) {
463 case Intrinsic::ptrmask:
464 case Intrinsic::objectsize:
465 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
466 PostorderStack, Visited);
467 break;
468 case Intrinsic::is_constant: {
469 Value *Ptr = II->getArgOperand(0);
470 if (Ptr->getType()->isPtrOrPtrVectorTy()) {
471 appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
472 Visited);
473 }
474
475 break;
476 }
477 case Intrinsic::masked_load:
478 case Intrinsic::masked_gather:
479 case Intrinsic::prefetch:
480 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
481 PostorderStack, Visited);
482 break;
483 case Intrinsic::masked_store:
484 case Intrinsic::masked_scatter:
485 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(1),
486 PostorderStack, Visited);
487 break;
488 case Intrinsic::fake_use: {
489 for (Value *Op : II->operands()) {
490 if (Op->getType()->isPtrOrPtrVectorTy()) {
491 appendsFlatAddressExpressionToPostorderStack(Op, PostorderStack,
492 Visited);
493 }
494 }
495
496 break;
497 }
498 case Intrinsic::lifetime_start:
499 case Intrinsic::lifetime_end: {
500 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
501 PostorderStack, Visited);
502 break;
503 }
504 default:
505 SmallVector<int, 2> OpIndexes;
506 if (TTI->collectFlatAddressOperands(OpIndexes, IID)) {
507 for (int Idx : OpIndexes) {
508 appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(Idx),
509 PostorderStack, Visited);
510 }
511 }
512 break;
513 }
514}
515
516// Returns all flat address expressions in function F. The elements are
517// If V is an unvisited flat address expression, appends V to PostorderStack
518// and marks it as visited.
519void InferAddressSpacesImpl::appendsFlatAddressExpressionToPostorderStack(
520 Value *V, PostorderStackTy &PostorderStack,
521 DenseSet<Value *> &Visited) const {
522 assert(V->getType()->isPtrOrPtrVectorTy());
523
524 // Generic addressing expressions may be hidden in nested constant
525 // expressions.
526 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
527 // TODO: Look in non-address parts, like icmp operands.
528 if (isAddressExpression(*CE, *DL, TTI) && Visited.insert(CE).second)
529 PostorderStack.emplace_back(CE, false);
530
531 return;
532 }
533
534 if (V->getType()->getPointerAddressSpace() == FlatAddrSpace &&
535 isAddressExpression(*V, *DL, TTI)) {
536 if (Visited.insert(V).second) {
537 PostorderStack.emplace_back(V, false);
538
539 if (auto *Op = dyn_cast<Operator>(V))
540 for (auto &O : Op->operands())
541 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(O))
542 if (isAddressExpression(*CE, *DL, TTI) && Visited.insert(CE).second)
543 PostorderStack.emplace_back(CE, false);
544 }
545 }
546}
547
548// Returns all flat address expressions in function F. The elements are ordered
549// in postorder.
550std::vector<WeakTrackingVH>
551InferAddressSpacesImpl::collectFlatAddressExpressions(Function &F) const {
552 // This function implements a non-recursive postorder traversal of a partial
553 // use-def graph of function F.
554 PostorderStackTy PostorderStack;
555 // The set of visited expressions.
556 DenseSet<Value *> Visited;
557
558 auto PushPtrOperand = [&](Value *Ptr) {
559 appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack, Visited);
560 };
561
562 // Look at operations that may be interesting accelerate by moving to a known
563 // address space. We aim at generating after loads and stores, but pure
564 // addressing calculations may also be faster.
565 for (Instruction &I : instructions(F)) {
566 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
567 PushPtrOperand(GEP->getPointerOperand());
568 } else if (auto *LI = dyn_cast<LoadInst>(&I))
569 PushPtrOperand(LI->getPointerOperand());
570 else if (auto *SI = dyn_cast<StoreInst>(&I))
571 PushPtrOperand(SI->getPointerOperand());
572 else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I))
573 PushPtrOperand(RMW->getPointerOperand());
574 else if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(&I))
575 PushPtrOperand(CmpX->getPointerOperand());
576 else if (auto *MI = dyn_cast<MemIntrinsic>(&I)) {
577 // For memset/memcpy/memmove, any pointer operand can be replaced.
578 PushPtrOperand(MI->getRawDest());
579
580 // Handle 2nd operand for memcpy/memmove.
581 if (auto *MTI = dyn_cast<MemTransferInst>(MI))
582 PushPtrOperand(MTI->getRawSource());
583 } else if (auto *II = dyn_cast<IntrinsicInst>(&I))
584 collectRewritableIntrinsicOperands(II, PostorderStack, Visited);
585 else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(&I)) {
586 if (Cmp->getOperand(0)->getType()->isPtrOrPtrVectorTy()) {
587 PushPtrOperand(Cmp->getOperand(0));
588 PushPtrOperand(Cmp->getOperand(1));
589 }
590 } else if (auto *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
591 PushPtrOperand(ASC->getPointerOperand());
592 } else if (auto *I2P = dyn_cast<IntToPtrInst>(&I)) {
594 PushPtrOperand(cast<Operator>(I2P->getOperand(0))->getOperand(0));
595 } else if (auto *RI = dyn_cast<ReturnInst>(&I)) {
596 if (auto *RV = RI->getReturnValue();
597 RV && RV->getType()->isPtrOrPtrVectorTy())
598 PushPtrOperand(RV);
599 }
600 }
601
602 std::vector<WeakTrackingVH> Postorder; // The resultant postorder.
603 while (!PostorderStack.empty()) {
604 Value *TopVal = PostorderStack.back().getPointer();
605 // If the operands of the expression on the top are already explored,
606 // adds that expression to the resultant postorder.
607 if (PostorderStack.back().getInt()) {
608 if (TopVal->getType()->getPointerAddressSpace() == FlatAddrSpace)
609 Postorder.push_back(TopVal);
610 PostorderStack.pop_back();
611 continue;
612 }
613 // Otherwise, adds its operands to the stack and explores them.
614 PostorderStack.back().setInt(true);
615 // Skip values with an assumed address space.
617 for (Value *PtrOperand : getPointerOperands(*TopVal, *DL, TTI)) {
618 appendsFlatAddressExpressionToPostorderStack(PtrOperand, PostorderStack,
619 Visited);
620 }
621 }
622 }
623 return Postorder;
624}
625
626// Inserts an addrspacecast for a phi node operand, handling the proper
627// insertion position based on the operand type.
629 Value *Operand) {
630 auto InsertBefore = [NewI](auto It) {
631 NewI->insertBefore(It);
632 NewI->setDebugLoc(It->getDebugLoc());
633 return NewI;
634 };
635
636 if (auto *Arg = dyn_cast<Argument>(Operand)) {
637 // For arguments, insert the cast at the beginning of entry block.
638 // Consider inserting at the dominating block for better placement.
639 Function *F = Arg->getParent();
640 auto InsertI = F->getEntryBlock().getFirstNonPHIIt();
641 return InsertBefore(InsertI);
642 }
643
644 // No check for Constant here, as constants are already handled.
645 assert(isa<Instruction>(Operand));
646
647 Instruction *OpInst = cast<Instruction>(Operand);
648 if (LLVM_UNLIKELY(OpInst->getOpcode() == Instruction::PHI)) {
649 // If the operand is defined by another PHI node, insert after the first
650 // non-PHI instruction at the corresponding basic block.
651 auto InsertI = OpInst->getParent()->getFirstNonPHIIt();
652 return InsertBefore(InsertI);
653 }
654
655 // Otherwise, insert immediately after the operand definition.
656 NewI->insertAfter(OpInst->getIterator());
657 NewI->setDebugLoc(OpInst->getDebugLoc());
658 return NewI;
659}
660
661// A helper function for cloneInstructionWithNewAddressSpace. Returns the clone
662// of OperandUse.get() in the new address space. If the clone is not ready yet,
663// returns poison in the new address space as a placeholder.
665 const Use &OperandUse, unsigned NewAddrSpace,
666 const ValueToValueMapTy &ValueWithNewAddrSpace,
667 const PredicatedAddrSpaceMapTy &PredicatedAS,
668 SmallVectorImpl<const Use *> *PoisonUsesToFix) {
669 Value *Operand = OperandUse.get();
670
671 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(Operand->getType(), NewAddrSpace);
672
673 if (Constant *C = dyn_cast<Constant>(Operand))
674 return ConstantExpr::getAddrSpaceCast(C, NewPtrTy);
675
676 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand))
677 return NewOperand;
678
679 Instruction *Inst = cast<Instruction>(OperandUse.getUser());
680 auto I = PredicatedAS.find(std::make_pair(Inst, Operand));
681 if (I != PredicatedAS.end()) {
682 // Insert an addrspacecast on that operand before the user.
683 unsigned NewAS = I->second;
684 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(Operand->getType(), NewAS);
685 auto *NewI = new AddrSpaceCastInst(Operand, NewPtrTy);
686
687 if (LLVM_UNLIKELY(Inst->getOpcode() == Instruction::PHI))
688 return phiNodeOperandWithNewAddressSpace(NewI, Operand);
689
690 NewI->insertBefore(Inst->getIterator());
691 NewI->setDebugLoc(Inst->getDebugLoc());
692 return NewI;
693 }
694
695 PoisonUsesToFix->push_back(&OperandUse);
696 return PoisonValue::get(NewPtrTy);
697}
698
699// A helper function for cloneInstructionWithNewAddressSpace. Handles the
700// conversion of a ptrmask intrinsic instruction.
701Value *InferAddressSpacesImpl::clonePtrMaskWithNewAddressSpace(
702 IntrinsicInst *I, unsigned NewAddrSpace,
703 const ValueToValueMapTy &ValueWithNewAddrSpace,
704 const PredicatedAddrSpaceMapTy &PredicatedAS,
705 SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
706 const Use &PtrOpUse = I->getArgOperandUse(0);
707 unsigned OldAddrSpace = PtrOpUse->getType()->getPointerAddressSpace();
708 Value *MaskOp = I->getArgOperand(1);
709 Type *MaskTy = MaskOp->getType();
710
711 KnownBits OldPtrBits{DL->getPointerSizeInBits(OldAddrSpace)};
712 KnownBits NewPtrBits{DL->getPointerSizeInBits(NewAddrSpace)};
713 if (!TTI->isNoopAddrSpaceCast(OldAddrSpace, NewAddrSpace)) {
714 std::tie(OldPtrBits, NewPtrBits) =
715 TTI->computeKnownBitsAddrSpaceCast(NewAddrSpace, *PtrOpUse.get());
716 }
717
718 // If the pointers in both addrspaces have a bitwise representation and if the
719 // representation of the new pointer is smaller (fewer bits) than the old one,
720 // check if the mask is applicable to the ptr in the new addrspace. Any
721 // masking only clearing the low bits will also apply in the new addrspace
722 // Note: checking if the mask clears high bits is not sufficient as those
723 // might have already been 0 in the old ptr.
724 if (OldPtrBits.getBitWidth() > NewPtrBits.getBitWidth()) {
725 KnownBits MaskBits =
726 computeKnownBits(MaskOp, *DL, /*AssumptionCache=*/nullptr, I);
727 // Set all unknown bits of the old ptr to 1, so that we are conservative in
728 // checking which bits are cleared by the mask.
729 OldPtrBits.One |= ~OldPtrBits.Zero;
730 // Check which bits are cleared by the mask in the old ptr.
731 KnownBits ClearedBits = KnownBits::sub(OldPtrBits, OldPtrBits & MaskBits);
732
733 // If the mask isn't applicable to the new ptr, leave the ptrmask as-is and
734 // insert an addrspacecast after it.
735 if (ClearedBits.countMaxActiveBits() > NewPtrBits.countMaxActiveBits()) {
736 std::optional<BasicBlock::iterator> InsertPoint =
737 I->getInsertionPointAfterDef();
738 assert(InsertPoint && "insertion after ptrmask should be possible");
739 Type *NewPtrType = getPtrOrVecOfPtrsWithNewAS(I->getType(), NewAddrSpace);
740 Instruction *AddrSpaceCast =
741 new AddrSpaceCastInst(I, NewPtrType, "", *InsertPoint);
742 AddrSpaceCast->setDebugLoc(I->getDebugLoc());
743 return AddrSpaceCast;
744 }
745 }
746
747 IRBuilder<> B(I);
748 if (NewPtrBits.getBitWidth() < MaskTy->getScalarSizeInBits()) {
749 MaskTy = MaskTy->getWithNewBitWidth(NewPtrBits.getBitWidth());
750 MaskOp = B.CreateTrunc(MaskOp, MaskTy);
751 }
753 PtrOpUse, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS,
754 PoisonUsesToFix);
755 return B.CreateIntrinsic(Intrinsic::ptrmask, {NewPtr->getType(), MaskTy},
756 {NewPtr, MaskOp});
757}
758
759// Returns a clone of `I` with its operands converted to those specified in
760// ValueWithNewAddrSpace. Due to potential cycles in the data flow graph, an
761// operand whose address space needs to be modified might not exist in
762// ValueWithNewAddrSpace. In that case, uses poison as a placeholder operand and
763// adds that operand use to PoisonUsesToFix so that caller can fix them later.
764//
765// Note that we do not necessarily clone `I`, e.g., if it is an addrspacecast
766// from a pointer whose type already matches. Therefore, this function returns a
767// Value* instead of an Instruction*.
768Value *InferAddressSpacesImpl::cloneInstructionWithNewAddressSpace(
769 Instruction *I, unsigned NewAddrSpace,
770 const ValueToValueMapTy &ValueWithNewAddrSpace,
771 const PredicatedAddrSpaceMapTy &PredicatedAS,
772 SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
773 Type *NewPtrType = getPtrOrVecOfPtrsWithNewAS(I->getType(), NewAddrSpace);
774
775 if (I->getOpcode() == Instruction::AddrSpaceCast) {
776 Value *Src = I->getOperand(0);
777 // Because `I` is flat, the source address space must be specific.
778 // Therefore, the inferred address space must be the source space, according
779 // to our algorithm.
780 assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
781 return Src;
782 }
783
784 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
785 // Technically the intrinsic ID is a pointer typed argument, so specially
786 // handle calls early.
787 assert(II->getIntrinsicID() == Intrinsic::ptrmask);
788 return clonePtrMaskWithNewAddressSpace(
789 II, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS, PoisonUsesToFix);
790 }
791
792 unsigned AS = TTI->getAssumedAddrSpace(I);
793 if (AS != UninitializedAddressSpace) {
794 // For the assumed address space, insert an `addrspacecast` to make that
795 // explicit.
796 Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(I->getType(), AS);
797 auto *NewI = new AddrSpaceCastInst(I, NewPtrTy);
798 NewI->insertAfter(I->getIterator());
799 NewI->setDebugLoc(I->getDebugLoc());
800 return NewI;
801 }
802
803 // Computes the converted pointer operands.
804 SmallVector<Value *, 4> NewPointerOperands;
805 for (const Use &OperandUse : I->operands()) {
806 if (!OperandUse.get()->getType()->isPtrOrPtrVectorTy())
807 NewPointerOperands.push_back(nullptr);
808 else
810 OperandUse, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS,
811 PoisonUsesToFix));
812 }
813
814 switch (I->getOpcode()) {
815 case Instruction::BitCast:
816 return new BitCastInst(NewPointerOperands[0], NewPtrType);
817 case Instruction::PHI: {
818 assert(I->getType()->isPtrOrPtrVectorTy());
819 PHINode *PHI = cast<PHINode>(I);
820 PHINode *NewPHI = PHINode::Create(NewPtrType, PHI->getNumIncomingValues());
821 for (unsigned Index = 0; Index < PHI->getNumIncomingValues(); ++Index) {
822 unsigned OperandNo = PHINode::getOperandNumForIncomingValue(Index);
823 NewPHI->addIncoming(NewPointerOperands[OperandNo],
824 PHI->getIncomingBlock(Index));
825 }
826 return NewPHI;
827 }
828 case Instruction::GetElementPtr: {
829 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
830 GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
831 GEP->getSourceElementType(), NewPointerOperands[0],
832 SmallVector<Value *, 4>(GEP->indices()));
833 NewGEP->setIsInBounds(GEP->isInBounds());
834 return NewGEP;
835 }
836 case Instruction::Select:
837 assert(I->getType()->isPtrOrPtrVectorTy());
838 return SelectInst::Create(I->getOperand(0), NewPointerOperands[1],
839 NewPointerOperands[2], "", nullptr, I);
840 case Instruction::IntToPtr: {
842 Value *Src = cast<Operator>(I->getOperand(0))->getOperand(0);
843 if (Src->getType() == NewPtrType)
844 return Src;
845
846 // If we had a no-op inttoptr/ptrtoint pair, we may still have inferred a
847 // source address space from a generic pointer source need to insert a cast
848 // back.
849 return new AddrSpaceCastInst(Src, NewPtrType);
850 }
851 default:
852 llvm_unreachable("Unexpected opcode");
853 }
854}
855
856// Similar to cloneInstructionWithNewAddressSpace, returns a clone of the
857// constant expression `CE` with its operands replaced as specified in
858// ValueWithNewAddrSpace.
860 ConstantExpr *CE, unsigned NewAddrSpace,
861 const ValueToValueMapTy &ValueWithNewAddrSpace, const DataLayout *DL,
862 const TargetTransformInfo *TTI) {
863 Type *TargetType =
864 CE->getType()->isPtrOrPtrVectorTy()
865 ? getPtrOrVecOfPtrsWithNewAS(CE->getType(), NewAddrSpace)
866 : CE->getType();
867
868 if (CE->getOpcode() == Instruction::AddrSpaceCast) {
869 // Because CE is flat, the source address space must be specific.
870 // Therefore, the inferred address space must be the source space according
871 // to our algorithm.
872 assert(CE->getOperand(0)->getType()->getPointerAddressSpace() ==
873 NewAddrSpace);
874 return CE->getOperand(0);
875 }
876
877 if (CE->getOpcode() == Instruction::BitCast) {
878 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(CE->getOperand(0)))
879 return ConstantExpr::getBitCast(cast<Constant>(NewOperand), TargetType);
880 return ConstantExpr::getAddrSpaceCast(CE, TargetType);
881 }
882
883 if (CE->getOpcode() == Instruction::IntToPtr) {
885 Constant *Src = cast<ConstantExpr>(CE->getOperand(0))->getOperand(0);
886 assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
887 return Src;
888 }
889
890 // Computes the operands of the new constant expression.
891 bool IsNew = false;
892 SmallVector<Constant *, 4> NewOperands;
893 for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) {
894 Constant *Operand = CE->getOperand(Index);
895 // If the address space of `Operand` needs to be modified, the new operand
896 // with the new address space should already be in ValueWithNewAddrSpace
897 // because (1) the constant expressions we consider (i.e. addrspacecast,
898 // bitcast, and getelementptr) do not incur cycles in the data flow graph
899 // and (2) this function is called on constant expressions in postorder.
900 if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) {
901 IsNew = true;
902 NewOperands.push_back(cast<Constant>(NewOperand));
903 continue;
904 }
905 if (auto *CExpr = dyn_cast<ConstantExpr>(Operand))
907 CExpr, NewAddrSpace, ValueWithNewAddrSpace, DL, TTI)) {
908 IsNew = true;
909 NewOperands.push_back(cast<Constant>(NewOperand));
910 continue;
911 }
912 // Otherwise, reuses the old operand.
913 NewOperands.push_back(Operand);
914 }
915
916 // If !IsNew, we will replace the Value with itself. However, replaced values
917 // are assumed to wrapped in an addrspacecast cast later so drop it now.
918 if (!IsNew)
919 return nullptr;
920
921 if (CE->getOpcode() == Instruction::GetElementPtr) {
922 // Needs to specify the source type while constructing a getelementptr
923 // constant expression.
924 return CE->getWithOperands(NewOperands, TargetType, /*OnlyIfReduced=*/false,
925 cast<GEPOperator>(CE)->getSourceElementType());
926 }
927
928 return CE->getWithOperands(NewOperands, TargetType);
929}
930
931// Returns a clone of the value `V`, with its operands replaced as specified in
932// ValueWithNewAddrSpace. This function is called on every flat address
933// expression whose address space needs to be modified, in postorder.
934//
935// See cloneInstructionWithNewAddressSpace for the meaning of PoisonUsesToFix.
936Value *InferAddressSpacesImpl::cloneValueWithNewAddressSpace(
937 Value *V, unsigned NewAddrSpace,
938 const ValueToValueMapTy &ValueWithNewAddrSpace,
939 const PredicatedAddrSpaceMapTy &PredicatedAS,
940 SmallVectorImpl<const Use *> *PoisonUsesToFix) const {
941 // All values in Postorder are flat address expressions.
942 assert(V->getType()->getPointerAddressSpace() == FlatAddrSpace &&
943 isAddressExpression(*V, *DL, TTI));
944
945 if (auto *Arg = dyn_cast<Argument>(V)) {
946 // Arguments are address space casted in the function body, as we do not
947 // want to change the function signature.
948 Function *F = Arg->getParent();
949 BasicBlock::iterator Insert = F->getEntryBlock().getFirstNonPHIIt();
950
951 Type *NewPtrTy = PointerType::get(Arg->getContext(), NewAddrSpace);
952 auto *NewI = new AddrSpaceCastInst(Arg, NewPtrTy);
953 NewI->insertBefore(Insert);
954 return NewI;
955 }
956
957 if (Instruction *I = dyn_cast<Instruction>(V)) {
958 Value *NewV = cloneInstructionWithNewAddressSpace(
959 I, NewAddrSpace, ValueWithNewAddrSpace, PredicatedAS, PoisonUsesToFix);
960 if (Instruction *NewI = dyn_cast_or_null<Instruction>(NewV)) {
961 if (NewI->getParent() == nullptr) {
962 NewI->insertBefore(I->getIterator());
963 NewI->takeName(I);
964 NewI->setDebugLoc(I->getDebugLoc());
965 }
966 }
967 return NewV;
968 }
969
971 cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace, DL, TTI);
972}
973
974// Defines the join operation on the address space lattice (see the file header
975// comments).
976unsigned InferAddressSpacesImpl::joinAddressSpaces(unsigned AS1,
977 unsigned AS2) const {
978 if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace)
979 return FlatAddrSpace;
980
981 if (AS1 == UninitializedAddressSpace)
982 return AS2;
983 if (AS2 == UninitializedAddressSpace)
984 return AS1;
985
986 // The join of two different specific address spaces is flat.
987 return (AS1 == AS2) ? AS1 : FlatAddrSpace;
988}
989
990bool InferAddressSpacesImpl::run(Function &CurFn) {
991 F = &CurFn;
992 DL = &F->getDataLayout();
993
995 FlatAddrSpace = 0;
996
997 if (FlatAddrSpace == UninitializedAddressSpace) {
998 FlatAddrSpace = TTI->getFlatAddressSpace();
999 if (FlatAddrSpace == UninitializedAddressSpace)
1000 return false;
1001 }
1002
1003 // Collects all flat address expressions in postorder.
1004 std::vector<WeakTrackingVH> Postorder = collectFlatAddressExpressions(*F);
1005
1006 // Runs a data-flow analysis to refine the address spaces of every expression
1007 // in Postorder.
1008 ValueToAddrSpaceMapTy InferredAddrSpace;
1009 PredicatedAddrSpaceMapTy PredicatedAS;
1010 inferAddressSpaces(Postorder, InferredAddrSpace, PredicatedAS);
1011
1012 // Changes the address spaces of the flat address expressions who are inferred
1013 // to point to a specific address space.
1014 return rewriteWithNewAddressSpaces(Postorder, InferredAddrSpace,
1015 PredicatedAS);
1016}
1017
1018// Constants need to be tracked through RAUW to handle cases with nested
1019// constant expressions, so wrap values in WeakTrackingVH.
1020void InferAddressSpacesImpl::inferAddressSpaces(
1021 ArrayRef<WeakTrackingVH> Postorder,
1022 ValueToAddrSpaceMapTy &InferredAddrSpace,
1023 PredicatedAddrSpaceMapTy &PredicatedAS) const {
1024 SetVector<Value *> Worklist(llvm::from_range, Postorder);
1025 // Initially, all expressions are in the uninitialized address space.
1026 for (Value *V : Postorder)
1027 InferredAddrSpace[V] = UninitializedAddressSpace;
1028
1029 while (!Worklist.empty()) {
1030 Value *V = Worklist.pop_back_val();
1031
1032 // Try to update the address space of the stack top according to the
1033 // address spaces of its operands.
1034 if (!updateAddressSpace(*V, InferredAddrSpace, PredicatedAS))
1035 continue;
1036
1037 for (Value *User : V->users()) {
1038 // Skip if User is already in the worklist.
1039 if (Worklist.count(User))
1040 continue;
1041
1042 auto Pos = InferredAddrSpace.find(User);
1043 // Our algorithm only updates the address spaces of flat address
1044 // expressions, which are those in InferredAddrSpace.
1045 if (Pos == InferredAddrSpace.end())
1046 continue;
1047
1048 // Function updateAddressSpace moves the address space down a lattice
1049 // path. Therefore, nothing to do if User is already inferred as flat (the
1050 // bottom element in the lattice).
1051 if (Pos->second == FlatAddrSpace)
1052 continue;
1053
1054 Worklist.insert(User);
1055 }
1056 }
1057}
1058
1059unsigned
1060InferAddressSpacesImpl::getPredicatedAddrSpace(const Value &Ptr,
1061 const Value *UserCtx) const {
1062 const Instruction *UserCtxI = dyn_cast<Instruction>(UserCtx);
1063 if (!UserCtxI)
1065
1066 const Value *StrippedPtr = Ptr.stripInBoundsOffsets();
1067 for (auto &AssumeVH : AC.assumptionsFor(StrippedPtr)) {
1068 if (!AssumeVH)
1069 continue;
1070 CallInst *CI = cast<CallInst>(AssumeVH);
1071 if (!isValidAssumeForContext(CI, UserCtxI, DT))
1072 continue;
1073
1074 const Value *Ptr;
1075 unsigned AS;
1076 std::tie(Ptr, AS) = TTI->getPredicatedAddrSpace(CI->getArgOperand(0));
1077 if (Ptr)
1078 return AS;
1079 }
1080
1082}
1083
1084bool InferAddressSpacesImpl::updateAddressSpace(
1085 const Value &V, ValueToAddrSpaceMapTy &InferredAddrSpace,
1086 PredicatedAddrSpaceMapTy &PredicatedAS) const {
1087 assert(InferredAddrSpace.count(&V));
1088
1089 LLVM_DEBUG(dbgs() << "Updating the address space of\n " << V << '\n');
1090
1091 // The new inferred address space equals the join of the address spaces
1092 // of all its pointer operands.
1093 unsigned NewAS = UninitializedAddressSpace;
1094
1095 // isAddressExpression should guarantee that V is an operator or an argument.
1097
1098 unsigned AS = TTI->getAssumedAddrSpace(&V);
1099 if (AS != UninitializedAddressSpace) {
1100 // Use the assumed address space directly.
1101 NewAS = AS;
1102 } else {
1103 // Otherwise, infer the address space from its pointer operands.
1104 SmallVector<Constant *, 2> ConstantPtrOps;
1105 for (Value *PtrOperand : getPointerOperands(V, *DL, TTI)) {
1106 auto I = InferredAddrSpace.find(PtrOperand);
1107 unsigned OperandAS;
1108 if (I == InferredAddrSpace.end()) {
1109 OperandAS = PtrOperand->getType()->getPointerAddressSpace();
1110 if (auto *C = dyn_cast<Constant>(PtrOperand);
1111 C && OperandAS == FlatAddrSpace) {
1112 // Defer joining the address space of constant pointer operands.
1113 ConstantPtrOps.push_back(C);
1114 continue;
1115 }
1116 if (OperandAS == FlatAddrSpace) {
1117 // Check AC for assumption dominating V.
1118 unsigned AS = getPredicatedAddrSpace(*PtrOperand, &V);
1119 if (AS != UninitializedAddressSpace) {
1121 << " deduce operand AS from the predicate addrspace "
1122 << AS << '\n');
1123 OperandAS = AS;
1124 // Record this use with the predicated AS.
1125 PredicatedAS[std::make_pair(&V, PtrOperand)] = OperandAS;
1126 }
1127 }
1128 } else
1129 OperandAS = I->second;
1130
1131 // join(flat, *) = flat. So we can break if NewAS is already flat.
1132 NewAS = joinAddressSpaces(NewAS, OperandAS);
1133 if (NewAS == FlatAddrSpace)
1134 break;
1135 }
1136 if (NewAS != FlatAddrSpace && NewAS != UninitializedAddressSpace) {
1137 if (any_of(ConstantPtrOps, [=](Constant *C) {
1138 return !isSafeToCastConstAddrSpace(C, NewAS);
1139 }))
1140 NewAS = FlatAddrSpace;
1141 }
1142 }
1143
1144 unsigned OldAS = InferredAddrSpace.lookup(&V);
1145 assert(OldAS != FlatAddrSpace);
1146 if (OldAS == NewAS)
1147 return false;
1148
1149 // If any updates are made, grabs its users to the worklist because
1150 // their address spaces can also be possibly updated.
1151 LLVM_DEBUG(dbgs() << " to " << NewAS << '\n');
1152 InferredAddrSpace[&V] = NewAS;
1153 return true;
1154}
1155
1156/// Replace operand \p OpIdx in \p Inst, if the value is the same as \p OldVal
1157/// with \p NewVal.
1158static bool replaceOperandIfSame(Instruction *Inst, unsigned OpIdx,
1159 Value *OldVal, Value *NewVal) {
1160 Use &U = Inst->getOperandUse(OpIdx);
1161 if (U.get() == OldVal) {
1162 U.set(NewVal);
1163 return true;
1164 }
1165
1166 return false;
1167}
1168
1169template <typename InstrType>
1171 InstrType *MemInstr, unsigned AddrSpace,
1172 Value *OldV, Value *NewV) {
1173 if (!MemInstr->isVolatile() || TTI.hasVolatileVariant(MemInstr, AddrSpace)) {
1174 return replaceOperandIfSame(MemInstr, InstrType::getPointerOperandIndex(),
1175 OldV, NewV);
1176 }
1177
1178 return false;
1179}
1180
1181/// If \p OldV is used as the pointer operand of a compatible memory operation
1182/// \p Inst, replaces the pointer operand with NewV.
1183///
1184/// This covers memory instructions with a single pointer operand that can have
1185/// its address space changed by simply mutating the use to a new value.
1186///
1187/// \p returns true the user replacement was made.
1189 User *Inst, unsigned AddrSpace,
1190 Value *OldV, Value *NewV) {
1191 if (auto *LI = dyn_cast<LoadInst>(Inst))
1192 return replaceSimplePointerUse(TTI, LI, AddrSpace, OldV, NewV);
1193
1194 if (auto *SI = dyn_cast<StoreInst>(Inst))
1195 return replaceSimplePointerUse(TTI, SI, AddrSpace, OldV, NewV);
1196
1197 if (auto *RMW = dyn_cast<AtomicRMWInst>(Inst))
1198 return replaceSimplePointerUse(TTI, RMW, AddrSpace, OldV, NewV);
1199
1200 if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst))
1201 return replaceSimplePointerUse(TTI, CmpX, AddrSpace, OldV, NewV);
1202
1203 return false;
1204}
1205
1206/// Update memory intrinsic uses that require more complex processing than
1207/// simple memory instructions. These require re-mangling and may have multiple
1208/// pointer operands.
1210 Value *NewV) {
1211 IRBuilder<> B(MI);
1212 if (auto *MSI = dyn_cast<MemSetInst>(MI)) {
1213 B.CreateMemSet(NewV, MSI->getValue(), MSI->getLength(), MSI->getDestAlign(),
1214 false, // isVolatile
1215 MI->getAAMetadata());
1216 } else if (auto *MTI = dyn_cast<MemTransferInst>(MI)) {
1217 Value *Src = MTI->getRawSource();
1218 Value *Dest = MTI->getRawDest();
1219
1220 // Be careful in case this is a self-to-self copy.
1221 if (Src == OldV)
1222 Src = NewV;
1223
1224 if (Dest == OldV)
1225 Dest = NewV;
1226
1227 if (auto *MCI = dyn_cast<MemCpyInst>(MTI)) {
1228 if (MCI->isForceInlined())
1229 B.CreateMemCpyInline(Dest, MTI->getDestAlign(), Src,
1230 MTI->getSourceAlign(), MTI->getLength(),
1231 false, // isVolatile
1232 MI->getAAMetadata());
1233 else
1234 B.CreateMemCpy(Dest, MTI->getDestAlign(), Src, MTI->getSourceAlign(),
1235 MTI->getLength(),
1236 false, // isVolatile
1237 MI->getAAMetadata());
1238 } else {
1240 B.CreateMemMove(Dest, MTI->getDestAlign(), Src, MTI->getSourceAlign(),
1241 MTI->getLength(),
1242 false, // isVolatile
1243 MI->getAAMetadata());
1244 }
1245 } else
1246 llvm_unreachable("unhandled MemIntrinsic");
1247
1248 MI->eraseFromParent();
1249 return true;
1250}
1251
1252// \p returns true if it is OK to change the address space of constant \p C with
1253// a ConstantExpr addrspacecast.
1254bool InferAddressSpacesImpl::isSafeToCastConstAddrSpace(Constant *C,
1255 unsigned NewAS) const {
1257
1258 unsigned SrcAS = C->getType()->getPointerAddressSpace();
1259 if (SrcAS == NewAS || isa<UndefValue>(C))
1260 return true;
1261
1262 // Prevent illegal casts between different non-flat address spaces.
1263 if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace)
1264 return false;
1265
1267 return true;
1268
1269 if (auto *Op = dyn_cast<Operator>(C)) {
1270 // If we already have a constant addrspacecast, it should be safe to cast it
1271 // off.
1272 if (Op->getOpcode() == Instruction::AddrSpaceCast)
1273 return isSafeToCastConstAddrSpace(cast<Constant>(Op->getOperand(0)),
1274 NewAS);
1275
1276 if (Op->getOpcode() == Instruction::IntToPtr &&
1277 Op->getType()->getPointerAddressSpace() == FlatAddrSpace)
1278 return true;
1279 }
1280
1281 return false;
1282}
1283
1285 Value::use_iterator End) {
1286 User *CurUser = I->getUser();
1287 ++I;
1288
1289 while (I != End && I->getUser() == CurUser)
1290 ++I;
1291
1292 return I;
1293}
1294
1295void InferAddressSpacesImpl::performPointerReplacement(
1296 Value *V, Value *NewV, Use &U, ValueToValueMapTy &ValueWithNewAddrSpace,
1297 SmallVectorImpl<Instruction *> &DeadInstructions) const {
1298
1299 User *CurUser = U.getUser();
1300
1301 unsigned AddrSpace = V->getType()->getPointerAddressSpace();
1302 if (replaceIfSimplePointerUse(*TTI, CurUser, AddrSpace, V, NewV))
1303 return;
1304
1305 // Skip if the current user is the new value itself.
1306 if (CurUser == NewV)
1307 return;
1308
1309 auto *CurUserI = dyn_cast<Instruction>(CurUser);
1310 if (!CurUserI || CurUserI->getFunction() != F)
1311 return;
1312
1313 // Handle more complex cases like intrinsic that need to be remangled.
1314 if (auto *MI = dyn_cast<MemIntrinsic>(CurUser)) {
1315 if (!MI->isVolatile() && handleMemIntrinsicPtrUse(MI, V, NewV))
1316 return;
1317 }
1318
1319 if (auto *II = dyn_cast<IntrinsicInst>(CurUser)) {
1320 if (rewriteIntrinsicOperands(II, V, NewV))
1321 return;
1322 }
1323
1324 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUserI)) {
1325 // If we can infer that both pointers are in the same addrspace,
1326 // transform e.g.
1327 // %cmp = icmp eq float* %p, %q
1328 // into
1329 // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
1330
1331 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1332 int SrcIdx = U.getOperandNo();
1333 int OtherIdx = (SrcIdx == 0) ? 1 : 0;
1334 Value *OtherSrc = Cmp->getOperand(OtherIdx);
1335
1336 if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) {
1337 if (OtherNewV->getType()->getPointerAddressSpace() == NewAS) {
1338 Cmp->setOperand(OtherIdx, OtherNewV);
1339 Cmp->setOperand(SrcIdx, NewV);
1340 return;
1341 }
1342 }
1343
1344 // Even if the type mismatches, we can cast the constant.
1345 if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
1346 if (isSafeToCastConstAddrSpace(KOtherSrc, NewAS)) {
1347 Cmp->setOperand(SrcIdx, NewV);
1348 Cmp->setOperand(OtherIdx, ConstantExpr::getAddrSpaceCast(
1349 KOtherSrc, NewV->getType()));
1350 return;
1351 }
1352 }
1353 }
1354
1355 if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUserI)) {
1356 unsigned NewAS = NewV->getType()->getPointerAddressSpace();
1357 if (ASC->getDestAddressSpace() == NewAS) {
1358 ASC->replaceAllUsesWith(NewV);
1359 DeadInstructions.push_back(ASC);
1360 return;
1361 }
1362 }
1363
1364 // Otherwise, replaces the use with flat(NewV).
1365 if (isa<Instruction>(V) || isa<Instruction>(NewV)) {
1366 // Don't create a copy of the original addrspacecast.
1367 if (U == V && isa<AddrSpaceCastInst>(V))
1368 return;
1369
1370 // Insert the addrspacecast after NewV.
1371 BasicBlock::iterator InsertPos;
1372 if (Instruction *NewVInst = dyn_cast<Instruction>(NewV))
1373 InsertPos = std::next(NewVInst->getIterator());
1374 else
1375 InsertPos = std::next(cast<Instruction>(V)->getIterator());
1376
1377 while (isa<PHINode>(InsertPos))
1378 ++InsertPos;
1379 // This instruction may contain multiple uses of V, update them all.
1380 CurUser->replaceUsesOfWith(
1381 V, new AddrSpaceCastInst(NewV, V->getType(), "", InsertPos));
1382 } else {
1383 CurUserI->replaceUsesOfWith(
1384 V, ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV), V->getType()));
1385 }
1386}
1387
1388bool InferAddressSpacesImpl::rewriteWithNewAddressSpaces(
1389 ArrayRef<WeakTrackingVH> Postorder,
1390 const ValueToAddrSpaceMapTy &InferredAddrSpace,
1391 const PredicatedAddrSpaceMapTy &PredicatedAS) const {
1392 // For each address expression to be modified, creates a clone of it with its
1393 // pointer operands converted to the new address space. Since the pointer
1394 // operands are converted, the clone is naturally in the new address space by
1395 // construction.
1396 ValueToValueMapTy ValueWithNewAddrSpace;
1397 SmallVector<const Use *, 32> PoisonUsesToFix;
1398 for (Value *V : Postorder) {
1399 unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
1400
1401 // In some degenerate cases (e.g. invalid IR in unreachable code), we may
1402 // not even infer the value to have its original address space.
1403 if (NewAddrSpace == UninitializedAddressSpace)
1404 continue;
1405
1406 if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
1407 Value *New =
1408 cloneValueWithNewAddressSpace(V, NewAddrSpace, ValueWithNewAddrSpace,
1409 PredicatedAS, &PoisonUsesToFix);
1410 if (New)
1411 ValueWithNewAddrSpace[V] = New;
1412 }
1413 }
1414
1415 if (ValueWithNewAddrSpace.empty())
1416 return false;
1417
1418 // Fixes all the poison uses generated by cloneInstructionWithNewAddressSpace.
1419 for (const Use *PoisonUse : PoisonUsesToFix) {
1420 User *V = PoisonUse->getUser();
1421 User *NewV = cast_or_null<User>(ValueWithNewAddrSpace.lookup(V));
1422 if (!NewV)
1423 continue;
1424
1425 unsigned OperandNo = PoisonUse->getOperandNo();
1426 assert(isa<PoisonValue>(NewV->getOperand(OperandNo)));
1427 WeakTrackingVH NewOp = ValueWithNewAddrSpace.lookup(PoisonUse->get());
1428 assert(NewOp &&
1429 "poison replacements in ValueWithNewAddrSpace shouldn't be null");
1430 NewV->setOperand(OperandNo, NewOp);
1431 }
1432
1433 SmallVector<Instruction *, 16> DeadInstructions;
1434 ValueToValueMapTy VMap;
1435 ValueMapper VMapper(VMap, RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1436
1437 // Replaces the uses of the old address expressions with the new ones.
1438 for (const WeakTrackingVH &WVH : Postorder) {
1439 assert(WVH && "value was unexpectedly deleted");
1440 Value *V = WVH;
1441 Value *NewV = ValueWithNewAddrSpace.lookup(V);
1442 if (NewV == nullptr)
1443 continue;
1444
1445 LLVM_DEBUG(dbgs() << "Replacing the uses of " << *V << "\n with\n "
1446 << *NewV << '\n');
1447
1448 if (Constant *C = dyn_cast<Constant>(V)) {
1449 Constant *Replace =
1451 if (C != Replace) {
1452 LLVM_DEBUG(dbgs() << "Inserting replacement const cast: " << Replace
1453 << ": " << *Replace << '\n');
1454 SmallVector<User *, 16> WorkList;
1455 for (User *U : make_early_inc_range(C->users())) {
1456 if (auto *I = dyn_cast<Instruction>(U)) {
1457 if (I->getFunction() == F)
1458 I->replaceUsesOfWith(C, Replace);
1459 } else {
1460 WorkList.append(U->user_begin(), U->user_end());
1461 }
1462 }
1463 if (!WorkList.empty()) {
1464 VMap[C] = Replace;
1465 DenseSet<User *> Visited{WorkList.begin(), WorkList.end()};
1466 while (!WorkList.empty()) {
1467 User *U = WorkList.pop_back_val();
1468 if (auto *I = dyn_cast<Instruction>(U)) {
1469 if (I->getFunction() == F)
1470 VMapper.remapInstruction(*I);
1471 continue;
1472 }
1473 for (User *U2 : U->users())
1474 if (Visited.insert(U2).second)
1475 WorkList.push_back(U2);
1476 }
1477 }
1478 V = Replace;
1479 }
1480 }
1481
1482 Value::use_iterator I, E, Next;
1483 for (I = V->use_begin(), E = V->use_end(); I != E;) {
1484 Use &U = *I;
1485
1486 // Some users may see the same pointer operand in multiple operands. Skip
1487 // to the next instruction.
1488 I = skipToNextUser(I, E);
1489
1490 performPointerReplacement(V, NewV, U, ValueWithNewAddrSpace,
1491 DeadInstructions);
1492 }
1493
1494 if (V->use_empty()) {
1495 if (Instruction *I = dyn_cast<Instruction>(V))
1496 DeadInstructions.push_back(I);
1497 }
1498 }
1499
1500 for (Instruction *I : DeadInstructions)
1502
1503 return true;
1504}
1505
1506bool InferAddressSpaces::runOnFunction(Function &F) {
1507 if (skipFunction(F))
1508 return false;
1509
1510 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
1511 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
1512 return InferAddressSpacesImpl(
1513 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), DT,
1514 &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F),
1515 FlatAddrSpace)
1516 .run(F);
1517}
1518
1520 return new InferAddressSpaces(AddressSpace);
1521}
1522
1527
1530 bool Changed =
1531 InferAddressSpacesImpl(AM.getResult<AssumptionAnalysis>(F),
1533 &AM.getResult<TargetIRAnalysis>(F), FlatAddrSpace)
1534 .run(F);
1535 if (Changed) {
1539 return PA;
1540 }
1541 return PreservedAnalyses::all();
1542}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
Hexagon Common GEP
IRTranslator LLVM IR MI
This header defines various interfaces for pass management in LLVM.
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 Value * phiNodeOperandWithNewAddressSpace(AddrSpaceCastInst *NewI, Value *Operand)
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.
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)
static const unsigned UninitializedAddressSpace
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This pass exposes codegen information to IR-level passes.
This class represents a conversion between pointers from one address space to another.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Value * getArgOperand(unsigned i) const
static LLVM_ABI 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:1130
static LLVM_ABI Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2794
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
A wrapper class for inspecting calls to intrinsic functions.
This is the common base class for memset/memcpy/memmove.
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
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 LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI unsigned getAssumedAddrSpace(const Value *V) const
LLVM_ABI std::pair< KnownBits, KnownBits > computeKnownBitsAddrSpaceCast(unsigned ToAS, const Value &PtrOp) const
LLVM_ABI bool isNoopAddrSpaceCast(unsigned FromAS, unsigned ToAS) const
LLVM_ABI std::pair< const Value *, unsigned > getPredicatedAddrSpace(const Value *V) const
LLVM_ABI 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 ...
LLVM_ABI 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...
LLVM_ABI unsigned getFlatAddressSpace() const
Returns the address space ID for a target's 'flat' address space.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI Type * getWithNewBitWidth(unsigned NewBitWidth) const
Given an integer or vector type, change the lane bitwidth to NewBitwidth, whilst keeping the old numb...
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * get() const
Definition Use.h:55
const Use & getOperandUse(unsigned i) const
Definition User.h:246
void setOperand(unsigned i, Value *Val)
Definition User.h:238
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Value * getOperand(unsigned i) const
Definition User.h:233
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:167
bool empty() const
Definition ValueMap.h:143
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition Value.cpp:819
use_iterator_impl< Use > use_iterator
Definition Value.h:353
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
#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.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
@ User
could "use" a pointer
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI 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,...
LLVM_ABI 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:533
LLVM_ABI void initializeInferAddressSpacesPass(PassRegistry &)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr from_range_t from_range
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:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition ValueMapper.h:98
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition ValueMapper.h:80
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI FunctionPass * createInferAddressSpacesPass(unsigned AddressSpace=~0u)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:299
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:356