LLVM  9.0.0svn
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 "undef" 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 `undef` and fix all the uses of undef later.
82 // For instance, our algorithm first converts %y to
83 // %y' = phi float addrspace(3)* [ %input, undef ]
84 // Then, it converts %y2 to
85 // %y2' = getelementptr %y', 1
86 // Finally, it fixes the undef in %y' so that
87 // %y' = phi float addrspace(3)* [ %input, %y2' ]
88 //
89 //===----------------------------------------------------------------------===//
90 
91 #include "llvm/ADT/ArrayRef.h"
92 #include "llvm/ADT/DenseMap.h"
93 #include "llvm/ADT/DenseSet.h"
94 #include "llvm/ADT/None.h"
95 #include "llvm/ADT/Optional.h"
96 #include "llvm/ADT/SetVector.h"
97 #include "llvm/ADT/SmallVector.h"
100 #include "llvm/IR/BasicBlock.h"
101 #include "llvm/IR/Constant.h"
102 #include "llvm/IR/Constants.h"
103 #include "llvm/IR/Function.h"
104 #include "llvm/IR/IRBuilder.h"
105 #include "llvm/IR/InstIterator.h"
106 #include "llvm/IR/Instruction.h"
107 #include "llvm/IR/Instructions.h"
108 #include "llvm/IR/IntrinsicInst.h"
109 #include "llvm/IR/Intrinsics.h"
110 #include "llvm/IR/LLVMContext.h"
111 #include "llvm/IR/Operator.h"
112 #include "llvm/IR/Type.h"
113 #include "llvm/IR/Use.h"
114 #include "llvm/IR/User.h"
115 #include "llvm/IR/Value.h"
116 #include "llvm/IR/ValueHandle.h"
117 #include "llvm/Pass.h"
118 #include "llvm/Support/Casting.h"
119 #include "llvm/Support/Compiler.h"
120 #include "llvm/Support/Debug.h"
123 #include "llvm/Transforms/Scalar.h"
125 #include <cassert>
126 #include <iterator>
127 #include <limits>
128 #include <utility>
129 #include <vector>
130 
131 #define DEBUG_TYPE "infer-address-spaces"
132 
133 using namespace llvm;
134 
135 static const unsigned UninitializedAddressSpace =
137 
138 namespace {
139 
140 using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>;
141 
142 /// InferAddressSpaces
143 class InferAddressSpaces : public FunctionPass {
144  /// Target specific address space which uses of should be replaced if
145  /// possible.
146  unsigned FlatAddrSpace;
147 
148 public:
149  static char ID;
150 
151  InferAddressSpaces() : FunctionPass(ID) {}
152 
153  void getAnalysisUsage(AnalysisUsage &AU) const override {
154  AU.setPreservesCFG();
156  }
157 
158  bool runOnFunction(Function &F) override;
159 
160 private:
161  // Returns the new address space of V if updated; otherwise, returns None.
163  updateAddressSpace(const Value &V,
164  const ValueToAddrSpaceMapTy &InferredAddrSpace) const;
165 
166  // Tries to infer the specific address space of each address expression in
167  // Postorder.
168  void inferAddressSpaces(ArrayRef<WeakTrackingVH> Postorder,
169  ValueToAddrSpaceMapTy *InferredAddrSpace) const;
170 
171  bool isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const;
172 
173  // Changes the flat address expressions in function F to point to specific
174  // address spaces if InferredAddrSpace says so. Postorder is the postorder of
175  // all flat expressions in the use-def graph of function F.
176  bool rewriteWithNewAddressSpaces(
177  const TargetTransformInfo &TTI, ArrayRef<WeakTrackingVH> Postorder,
178  const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const;
179 
180  void appendsFlatAddressExpressionToPostorderStack(
181  Value *V, std::vector<std::pair<Value *, bool>> &PostorderStack,
182  DenseSet<Value *> &Visited) const;
183 
184  bool rewriteIntrinsicOperands(IntrinsicInst *II,
185  Value *OldV, Value *NewV) const;
186  void collectRewritableIntrinsicOperands(
187  IntrinsicInst *II,
188  std::vector<std::pair<Value *, bool>> &PostorderStack,
189  DenseSet<Value *> &Visited) const;
190 
191  std::vector<WeakTrackingVH> collectFlatAddressExpressions(Function &F) const;
192 
193  Value *cloneValueWithNewAddressSpace(
194  Value *V, unsigned NewAddrSpace,
195  const ValueToValueMapTy &ValueWithNewAddrSpace,
196  SmallVectorImpl<const Use *> *UndefUsesToFix) const;
197  unsigned joinAddressSpaces(unsigned AS1, unsigned AS2) const;
198 };
199 
200 } // end anonymous namespace
201 
202 char InferAddressSpaces::ID = 0;
203 
204 namespace llvm {
205 
207 
208 } // end namespace llvm
209 
210 INITIALIZE_PASS(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
211  false, false)
212 
213 // Returns true if V is an address expression.
214 // TODO: Currently, we consider only phi, bitcast, addrspacecast, and
215 // getelementptr operators.
216 static bool isAddressExpression(const Value &V) {
217  if (!isa<Operator>(V))
218  return false;
219 
220  const Operator &Op = cast<Operator>(V);
221  switch (Op.getOpcode()) {
222  case Instruction::PHI:
223  assert(Op.getType()->isPointerTy());
224  return true;
225  case Instruction::BitCast:
226  case Instruction::AddrSpaceCast:
227  case Instruction::GetElementPtr:
228  return true;
229  case Instruction::Select:
230  return Op.getType()->isPointerTy();
231  default:
232  return false;
233  }
234 }
235 
236 // Returns the pointer operands of V.
237 //
238 // Precondition: V is an address expression.
240  const Operator &Op = cast<Operator>(V);
241  switch (Op.getOpcode()) {
242  case Instruction::PHI: {
243  auto IncomingValues = cast<PHINode>(Op).incoming_values();
244  return SmallVector<Value *, 2>(IncomingValues.begin(),
245  IncomingValues.end());
246  }
247  case Instruction::BitCast:
248  case Instruction::AddrSpaceCast:
249  case Instruction::GetElementPtr:
250  return {Op.getOperand(0)};
251  case Instruction::Select:
252  return {Op.getOperand(1), Op.getOperand(2)};
253  default:
254  llvm_unreachable("Unexpected instruction type.");
255  }
256 }
257 
258 // TODO: Move logic to TTI?
259 bool InferAddressSpaces::rewriteIntrinsicOperands(IntrinsicInst *II,
260  Value *OldV,
261  Value *NewV) const {
262  Module *M = II->getParent()->getParent()->getParent();
263 
264  switch (II->getIntrinsicID()) {
265  case Intrinsic::amdgcn_atomic_inc:
266  case Intrinsic::amdgcn_atomic_dec:
267  case Intrinsic::amdgcn_ds_fadd:
268  case Intrinsic::amdgcn_ds_fmin:
269  case Intrinsic::amdgcn_ds_fmax: {
271  if (!IsVolatile || !IsVolatile->isZero())
272  return false;
273 
275  }
276  case Intrinsic::objectsize: {
277  Type *DestTy = II->getType();
278  Type *SrcTy = NewV->getType();
279  Function *NewDecl =
280  Intrinsic::getDeclaration(M, II->getIntrinsicID(), {DestTy, SrcTy});
281  II->setArgOperand(0, NewV);
282  II->setCalledFunction(NewDecl);
283  return true;
284  }
285  default:
286  return false;
287  }
288 }
289 
290 // TODO: Move logic to TTI?
291 void InferAddressSpaces::collectRewritableIntrinsicOperands(
292  IntrinsicInst *II, std::vector<std::pair<Value *, bool>> &PostorderStack,
293  DenseSet<Value *> &Visited) const {
294  switch (II->getIntrinsicID()) {
295  case Intrinsic::objectsize:
296  case Intrinsic::amdgcn_atomic_inc:
297  case Intrinsic::amdgcn_atomic_dec:
298  case Intrinsic::amdgcn_ds_fadd:
299  case Intrinsic::amdgcn_ds_fmin:
300  case Intrinsic::amdgcn_ds_fmax:
301  appendsFlatAddressExpressionToPostorderStack(II->getArgOperand(0),
302  PostorderStack, Visited);
303  break;
304  default:
305  break;
306  }
307 }
308 
309 // Returns all flat address expressions in function F. The elements are
310 // If V is an unvisited flat address expression, appends V to PostorderStack
311 // and marks it as visited.
312 void InferAddressSpaces::appendsFlatAddressExpressionToPostorderStack(
313  Value *V, std::vector<std::pair<Value *, bool>> &PostorderStack,
314  DenseSet<Value *> &Visited) const {
315  assert(V->getType()->isPointerTy());
316 
317  // Generic addressing expressions may be hidden in nested constant
318  // expressions.
319  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
320  // TODO: Look in non-address parts, like icmp operands.
321  if (isAddressExpression(*CE) && Visited.insert(CE).second)
322  PostorderStack.push_back(std::make_pair(CE, false));
323 
324  return;
325  }
326 
327  if (isAddressExpression(*V) &&
328  V->getType()->getPointerAddressSpace() == FlatAddrSpace) {
329  if (Visited.insert(V).second) {
330  PostorderStack.push_back(std::make_pair(V, false));
331 
332  Operator *Op = cast<Operator>(V);
333  for (unsigned I = 0, E = Op->getNumOperands(); I != E; ++I) {
334  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op->getOperand(I))) {
335  if (isAddressExpression(*CE) && Visited.insert(CE).second)
336  PostorderStack.emplace_back(CE, false);
337  }
338  }
339  }
340  }
341 }
342 
343 // Returns all flat address expressions in function F. The elements are ordered
344 // ordered in postorder.
345 std::vector<WeakTrackingVH>
346 InferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
347  // This function implements a non-recursive postorder traversal of a partial
348  // use-def graph of function F.
349  std::vector<std::pair<Value *, bool>> PostorderStack;
350  // The set of visited expressions.
351  DenseSet<Value *> Visited;
352 
353  auto PushPtrOperand = [&](Value *Ptr) {
354  appendsFlatAddressExpressionToPostorderStack(Ptr, PostorderStack,
355  Visited);
356  };
357 
358  // Look at operations that may be interesting accelerate by moving to a known
359  // address space. We aim at generating after loads and stores, but pure
360  // addressing calculations may also be faster.
361  for (Instruction &I : instructions(F)) {
362  if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
363  if (!GEP->getType()->isVectorTy())
364  PushPtrOperand(GEP->getPointerOperand());
365  } else if (auto *LI = dyn_cast<LoadInst>(&I))
366  PushPtrOperand(LI->getPointerOperand());
367  else if (auto *SI = dyn_cast<StoreInst>(&I))
368  PushPtrOperand(SI->getPointerOperand());
369  else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I))
370  PushPtrOperand(RMW->getPointerOperand());
371  else if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(&I))
372  PushPtrOperand(CmpX->getPointerOperand());
373  else if (auto *MI = dyn_cast<MemIntrinsic>(&I)) {
374  // For memset/memcpy/memmove, any pointer operand can be replaced.
375  PushPtrOperand(MI->getRawDest());
376 
377  // Handle 2nd operand for memcpy/memmove.
378  if (auto *MTI = dyn_cast<MemTransferInst>(MI))
379  PushPtrOperand(MTI->getRawSource());
380  } else if (auto *II = dyn_cast<IntrinsicInst>(&I))
381  collectRewritableIntrinsicOperands(II, PostorderStack, Visited);
382  else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(&I)) {
383  // FIXME: Handle vectors of pointers
384  if (Cmp->getOperand(0)->getType()->isPointerTy()) {
385  PushPtrOperand(Cmp->getOperand(0));
386  PushPtrOperand(Cmp->getOperand(1));
387  }
388  } else if (auto *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
389  if (!ASC->getType()->isVectorTy())
390  PushPtrOperand(ASC->getPointerOperand());
391  }
392  }
393 
394  std::vector<WeakTrackingVH> Postorder; // The resultant postorder.
395  while (!PostorderStack.empty()) {
396  Value *TopVal = PostorderStack.back().first;
397  // If the operands of the expression on the top are already explored,
398  // adds that expression to the resultant postorder.
399  if (PostorderStack.back().second) {
400  if (TopVal->getType()->getPointerAddressSpace() == FlatAddrSpace)
401  Postorder.push_back(TopVal);
402  PostorderStack.pop_back();
403  continue;
404  }
405  // Otherwise, adds its operands to the stack and explores them.
406  PostorderStack.back().second = true;
407  for (Value *PtrOperand : getPointerOperands(*TopVal)) {
408  appendsFlatAddressExpressionToPostorderStack(PtrOperand, PostorderStack,
409  Visited);
410  }
411  }
412  return Postorder;
413 }
414 
415 // A helper function for cloneInstructionWithNewAddressSpace. Returns the clone
416 // of OperandUse.get() in the new address space. If the clone is not ready yet,
417 // returns an undef in the new address space as a placeholder.
419  const Use &OperandUse, unsigned NewAddrSpace,
420  const ValueToValueMapTy &ValueWithNewAddrSpace,
421  SmallVectorImpl<const Use *> *UndefUsesToFix) {
422  Value *Operand = OperandUse.get();
423 
424  Type *NewPtrTy =
425  Operand->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
426 
427  if (Constant *C = dyn_cast<Constant>(Operand))
428  return ConstantExpr::getAddrSpaceCast(C, NewPtrTy);
429 
430  if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand))
431  return NewOperand;
432 
433  UndefUsesToFix->push_back(&OperandUse);
434  return UndefValue::get(NewPtrTy);
435 }
436 
437 // Returns a clone of `I` with its operands converted to those specified in
438 // ValueWithNewAddrSpace. Due to potential cycles in the data flow graph, an
439 // operand whose address space needs to be modified might not exist in
440 // ValueWithNewAddrSpace. In that case, uses undef as a placeholder operand and
441 // adds that operand use to UndefUsesToFix so that caller can fix them later.
442 //
443 // Note that we do not necessarily clone `I`, e.g., if it is an addrspacecast
444 // from a pointer whose type already matches. Therefore, this function returns a
445 // Value* instead of an Instruction*.
447  Instruction *I, unsigned NewAddrSpace,
448  const ValueToValueMapTy &ValueWithNewAddrSpace,
449  SmallVectorImpl<const Use *> *UndefUsesToFix) {
450  Type *NewPtrType =
451  I->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
452 
453  if (I->getOpcode() == Instruction::AddrSpaceCast) {
454  Value *Src = I->getOperand(0);
455  // Because `I` is flat, the source address space must be specific.
456  // Therefore, the inferred address space must be the source space, according
457  // to our algorithm.
458  assert(Src->getType()->getPointerAddressSpace() == NewAddrSpace);
459  if (Src->getType() != NewPtrType)
460  return new BitCastInst(Src, NewPtrType);
461  return Src;
462  }
463 
464  // Computes the converted pointer operands.
465  SmallVector<Value *, 4> NewPointerOperands;
466  for (const Use &OperandUse : I->operands()) {
467  if (!OperandUse.get()->getType()->isPointerTy())
468  NewPointerOperands.push_back(nullptr);
469  else
471  OperandUse, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix));
472  }
473 
474  switch (I->getOpcode()) {
475  case Instruction::BitCast:
476  return new BitCastInst(NewPointerOperands[0], NewPtrType);
477  case Instruction::PHI: {
478  assert(I->getType()->isPointerTy());
479  PHINode *PHI = cast<PHINode>(I);
480  PHINode *NewPHI = PHINode::Create(NewPtrType, PHI->getNumIncomingValues());
481  for (unsigned Index = 0; Index < PHI->getNumIncomingValues(); ++Index) {
482  unsigned OperandNo = PHINode::getOperandNumForIncomingValue(Index);
483  NewPHI->addIncoming(NewPointerOperands[OperandNo],
484  PHI->getIncomingBlock(Index));
485  }
486  return NewPHI;
487  }
488  case Instruction::GetElementPtr: {
489  GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
491  GEP->getSourceElementType(), NewPointerOperands[0],
492  SmallVector<Value *, 4>(GEP->idx_begin(), GEP->idx_end()));
493  NewGEP->setIsInBounds(GEP->isInBounds());
494  return NewGEP;
495  }
496  case Instruction::Select:
497  assert(I->getType()->isPointerTy());
498  return SelectInst::Create(I->getOperand(0), NewPointerOperands[1],
499  NewPointerOperands[2], "", nullptr, I);
500  default:
501  llvm_unreachable("Unexpected opcode");
502  }
503 }
504 
505 // Similar to cloneInstructionWithNewAddressSpace, returns a clone of the
506 // constant expression `CE` with its operands replaced as specified in
507 // ValueWithNewAddrSpace.
509  ConstantExpr *CE, unsigned NewAddrSpace,
510  const ValueToValueMapTy &ValueWithNewAddrSpace) {
511  Type *TargetType =
512  CE->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
513 
514  if (CE->getOpcode() == Instruction::AddrSpaceCast) {
515  // Because CE is flat, the source address space must be specific.
516  // Therefore, the inferred address space must be the source space according
517  // to our algorithm.
519  NewAddrSpace);
520  return ConstantExpr::getBitCast(CE->getOperand(0), TargetType);
521  }
522 
523  if (CE->getOpcode() == Instruction::BitCast) {
524  if (Value *NewOperand = ValueWithNewAddrSpace.lookup(CE->getOperand(0)))
525  return ConstantExpr::getBitCast(cast<Constant>(NewOperand), TargetType);
526  return ConstantExpr::getAddrSpaceCast(CE, TargetType);
527  }
528 
529  if (CE->getOpcode() == Instruction::Select) {
530  Constant *Src0 = CE->getOperand(1);
531  Constant *Src1 = CE->getOperand(2);
532  if (Src0->getType()->getPointerAddressSpace() ==
533  Src1->getType()->getPointerAddressSpace()) {
534 
536  CE->getOperand(0), ConstantExpr::getAddrSpaceCast(Src0, TargetType),
537  ConstantExpr::getAddrSpaceCast(Src1, TargetType));
538  }
539  }
540 
541  // Computes the operands of the new constant expression.
542  bool IsNew = false;
543  SmallVector<Constant *, 4> NewOperands;
544  for (unsigned Index = 0; Index < CE->getNumOperands(); ++Index) {
545  Constant *Operand = CE->getOperand(Index);
546  // If the address space of `Operand` needs to be modified, the new operand
547  // with the new address space should already be in ValueWithNewAddrSpace
548  // because (1) the constant expressions we consider (i.e. addrspacecast,
549  // bitcast, and getelementptr) do not incur cycles in the data flow graph
550  // and (2) this function is called on constant expressions in postorder.
551  if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) {
552  IsNew = true;
553  NewOperands.push_back(cast<Constant>(NewOperand));
554  } else {
555  // Otherwise, reuses the old operand.
556  NewOperands.push_back(Operand);
557  }
558  }
559 
560  // If !IsNew, we will replace the Value with itself. However, replaced values
561  // are assumed to wrapped in a addrspace cast later so drop it now.
562  if (!IsNew)
563  return nullptr;
564 
565  if (CE->getOpcode() == Instruction::GetElementPtr) {
566  // Needs to specify the source type while constructing a getelementptr
567  // constant expression.
568  return CE->getWithOperands(
569  NewOperands, TargetType, /*OnlyIfReduced=*/false,
570  NewOperands[0]->getType()->getPointerElementType());
571  }
572 
573  return CE->getWithOperands(NewOperands, TargetType);
574 }
575 
576 // Returns a clone of the value `V`, with its operands replaced as specified in
577 // ValueWithNewAddrSpace. This function is called on every flat address
578 // expression whose address space needs to be modified, in postorder.
579 //
580 // See cloneInstructionWithNewAddressSpace for the meaning of UndefUsesToFix.
581 Value *InferAddressSpaces::cloneValueWithNewAddressSpace(
582  Value *V, unsigned NewAddrSpace,
583  const ValueToValueMapTy &ValueWithNewAddrSpace,
584  SmallVectorImpl<const Use *> *UndefUsesToFix) const {
585  // All values in Postorder are flat address expressions.
586  assert(isAddressExpression(*V) &&
587  V->getType()->getPointerAddressSpace() == FlatAddrSpace);
588 
589  if (Instruction *I = dyn_cast<Instruction>(V)) {
591  I, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix);
592  if (Instruction *NewI = dyn_cast<Instruction>(NewV)) {
593  if (NewI->getParent() == nullptr) {
594  NewI->insertBefore(I);
595  NewI->takeName(I);
596  }
597  }
598  return NewV;
599  }
600 
602  cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace);
603 }
604 
605 // Defines the join operation on the address space lattice (see the file header
606 // comments).
607 unsigned InferAddressSpaces::joinAddressSpaces(unsigned AS1,
608  unsigned AS2) const {
609  if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace)
610  return FlatAddrSpace;
611 
612  if (AS1 == UninitializedAddressSpace)
613  return AS2;
614  if (AS2 == UninitializedAddressSpace)
615  return AS1;
616 
617  // The join of two different specific address spaces is flat.
618  return (AS1 == AS2) ? AS1 : FlatAddrSpace;
619 }
620 
622  if (skipFunction(F))
623  return false;
624 
625  const TargetTransformInfo &TTI =
626  getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
627  FlatAddrSpace = TTI.getFlatAddressSpace();
628  if (FlatAddrSpace == UninitializedAddressSpace)
629  return false;
630 
631  // Collects all flat address expressions in postorder.
632  std::vector<WeakTrackingVH> Postorder = collectFlatAddressExpressions(F);
633 
634  // Runs a data-flow analysis to refine the address spaces of every expression
635  // in Postorder.
636  ValueToAddrSpaceMapTy InferredAddrSpace;
637  inferAddressSpaces(Postorder, &InferredAddrSpace);
638 
639  // Changes the address spaces of the flat address expressions who are inferred
640  // to point to a specific address space.
641  return rewriteWithNewAddressSpaces(TTI, Postorder, InferredAddrSpace, &F);
642 }
643 
644 // Constants need to be tracked through RAUW to handle cases with nested
645 // constant expressions, so wrap values in WeakTrackingVH.
646 void InferAddressSpaces::inferAddressSpaces(
647  ArrayRef<WeakTrackingVH> Postorder,
648  ValueToAddrSpaceMapTy *InferredAddrSpace) const {
649  SetVector<Value *> Worklist(Postorder.begin(), Postorder.end());
650  // Initially, all expressions are in the uninitialized address space.
651  for (Value *V : Postorder)
652  (*InferredAddrSpace)[V] = UninitializedAddressSpace;
653 
654  while (!Worklist.empty()) {
655  Value *V = Worklist.pop_back_val();
656 
657  // Tries to update the address space of the stack top according to the
658  // address spaces of its operands.
659  LLVM_DEBUG(dbgs() << "Updating the address space of\n " << *V << '\n');
660  Optional<unsigned> NewAS = updateAddressSpace(*V, *InferredAddrSpace);
661  if (!NewAS.hasValue())
662  continue;
663  // If any updates are made, grabs its users to the worklist because
664  // their address spaces can also be possibly updated.
665  LLVM_DEBUG(dbgs() << " to " << NewAS.getValue() << '\n');
666  (*InferredAddrSpace)[V] = NewAS.getValue();
667 
668  for (Value *User : V->users()) {
669  // Skip if User is already in the worklist.
670  if (Worklist.count(User))
671  continue;
672 
673  auto Pos = InferredAddrSpace->find(User);
674  // Our algorithm only updates the address spaces of flat address
675  // expressions, which are those in InferredAddrSpace.
676  if (Pos == InferredAddrSpace->end())
677  continue;
678 
679  // Function updateAddressSpace moves the address space down a lattice
680  // path. Therefore, nothing to do if User is already inferred as flat (the
681  // bottom element in the lattice).
682  if (Pos->second == FlatAddrSpace)
683  continue;
684 
685  Worklist.insert(User);
686  }
687  }
688 }
689 
690 Optional<unsigned> InferAddressSpaces::updateAddressSpace(
691  const Value &V, const ValueToAddrSpaceMapTy &InferredAddrSpace) const {
692  assert(InferredAddrSpace.count(&V));
693 
694  // The new inferred address space equals the join of the address spaces
695  // of all its pointer operands.
696  unsigned NewAS = UninitializedAddressSpace;
697 
698  const Operator &Op = cast<Operator>(V);
699  if (Op.getOpcode() == Instruction::Select) {
700  Value *Src0 = Op.getOperand(1);
701  Value *Src1 = Op.getOperand(2);
702 
703  auto I = InferredAddrSpace.find(Src0);
704  unsigned Src0AS = (I != InferredAddrSpace.end()) ?
705  I->second : Src0->getType()->getPointerAddressSpace();
706 
707  auto J = InferredAddrSpace.find(Src1);
708  unsigned Src1AS = (J != InferredAddrSpace.end()) ?
709  J->second : Src1->getType()->getPointerAddressSpace();
710 
711  auto *C0 = dyn_cast<Constant>(Src0);
712  auto *C1 = dyn_cast<Constant>(Src1);
713 
714  // If one of the inputs is a constant, we may be able to do a constant
715  // addrspacecast of it. Defer inferring the address space until the input
716  // address space is known.
717  if ((C1 && Src0AS == UninitializedAddressSpace) ||
718  (C0 && Src1AS == UninitializedAddressSpace))
719  return None;
720 
721  if (C0 && isSafeToCastConstAddrSpace(C0, Src1AS))
722  NewAS = Src1AS;
723  else if (C1 && isSafeToCastConstAddrSpace(C1, Src0AS))
724  NewAS = Src0AS;
725  else
726  NewAS = joinAddressSpaces(Src0AS, Src1AS);
727  } else {
728  for (Value *PtrOperand : getPointerOperands(V)) {
729  auto I = InferredAddrSpace.find(PtrOperand);
730  unsigned OperandAS = I != InferredAddrSpace.end() ?
731  I->second : PtrOperand->getType()->getPointerAddressSpace();
732 
733  // join(flat, *) = flat. So we can break if NewAS is already flat.
734  NewAS = joinAddressSpaces(NewAS, OperandAS);
735  if (NewAS == FlatAddrSpace)
736  break;
737  }
738  }
739 
740  unsigned OldAS = InferredAddrSpace.lookup(&V);
741  assert(OldAS != FlatAddrSpace);
742  if (OldAS == NewAS)
743  return None;
744  return NewAS;
745 }
746 
747 /// \p returns true if \p U is the pointer operand of a memory instruction with
748 /// a single pointer operand that can have its address space changed by simply
749 /// mutating the use to a new value. If the memory instruction is volatile,
750 /// return true only if the target allows the memory instruction to be volatile
751 /// in the new address space.
753  Use &U, unsigned AddrSpace) {
754  User *Inst = U.getUser();
755  unsigned OpNo = U.getOperandNo();
756  bool VolatileIsAllowed = false;
757  if (auto *I = dyn_cast<Instruction>(Inst))
758  VolatileIsAllowed = TTI.hasVolatileVariant(I, AddrSpace);
759 
760  if (auto *LI = dyn_cast<LoadInst>(Inst))
761  return OpNo == LoadInst::getPointerOperandIndex() &&
762  (VolatileIsAllowed || !LI->isVolatile());
763 
764  if (auto *SI = dyn_cast<StoreInst>(Inst))
765  return OpNo == StoreInst::getPointerOperandIndex() &&
766  (VolatileIsAllowed || !SI->isVolatile());
767 
768  if (auto *RMW = dyn_cast<AtomicRMWInst>(Inst))
769  return OpNo == AtomicRMWInst::getPointerOperandIndex() &&
770  (VolatileIsAllowed || !RMW->isVolatile());
771 
772  if (auto *CmpX = dyn_cast<AtomicCmpXchgInst>(Inst))
774  (VolatileIsAllowed || !CmpX->isVolatile());
775 
776  return false;
777 }
778 
779 /// Update memory intrinsic uses that require more complex processing than
780 /// simple memory instructions. Thse require re-mangling and may have multiple
781 /// pointer operands.
783  Value *NewV) {
784  IRBuilder<> B(MI);
787  MDNode *NoAliasMD = MI->getMetadata(LLVMContext::MD_noalias);
788 
789  if (auto *MSI = dyn_cast<MemSetInst>(MI)) {
790  B.CreateMemSet(NewV, MSI->getValue(),
791  MSI->getLength(), MSI->getDestAlignment(),
792  false, // isVolatile
793  TBAA, ScopeMD, NoAliasMD);
794  } else if (auto *MTI = dyn_cast<MemTransferInst>(MI)) {
795  Value *Src = MTI->getRawSource();
796  Value *Dest = MTI->getRawDest();
797 
798  // Be careful in case this is a self-to-self copy.
799  if (Src == OldV)
800  Src = NewV;
801 
802  if (Dest == OldV)
803  Dest = NewV;
804 
805  if (isa<MemCpyInst>(MTI)) {
806  MDNode *TBAAStruct = MTI->getMetadata(LLVMContext::MD_tbaa_struct);
807  B.CreateMemCpy(Dest, MTI->getDestAlignment(),
808  Src, MTI->getSourceAlignment(),
809  MTI->getLength(),
810  false, // isVolatile
811  TBAA, TBAAStruct, ScopeMD, NoAliasMD);
812  } else {
813  assert(isa<MemMoveInst>(MTI));
814  B.CreateMemMove(Dest, MTI->getDestAlignment(),
815  Src, MTI->getSourceAlignment(),
816  MTI->getLength(),
817  false, // isVolatile
818  TBAA, ScopeMD, NoAliasMD);
819  }
820  } else
821  llvm_unreachable("unhandled MemIntrinsic");
822 
823  MI->eraseFromParent();
824  return true;
825 }
826 
827 // \p returns true if it is OK to change the address space of constant \p C with
828 // a ConstantExpr addrspacecast.
829 bool InferAddressSpaces::isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const {
830  assert(NewAS != UninitializedAddressSpace);
831 
832  unsigned SrcAS = C->getType()->getPointerAddressSpace();
833  if (SrcAS == NewAS || isa<UndefValue>(C))
834  return true;
835 
836  // Prevent illegal casts between different non-flat address spaces.
837  if (SrcAS != FlatAddrSpace && NewAS != FlatAddrSpace)
838  return false;
839 
840  if (isa<ConstantPointerNull>(C))
841  return true;
842 
843  if (auto *Op = dyn_cast<Operator>(C)) {
844  // If we already have a constant addrspacecast, it should be safe to cast it
845  // off.
846  if (Op->getOpcode() == Instruction::AddrSpaceCast)
847  return isSafeToCastConstAddrSpace(cast<Constant>(Op->getOperand(0)), NewAS);
848 
849  if (Op->getOpcode() == Instruction::IntToPtr &&
850  Op->getType()->getPointerAddressSpace() == FlatAddrSpace)
851  return true;
852  }
853 
854  return false;
855 }
856 
858  Value::use_iterator End) {
859  User *CurUser = I->getUser();
860  ++I;
861 
862  while (I != End && I->getUser() == CurUser)
863  ++I;
864 
865  return I;
866 }
867 
868 bool InferAddressSpaces::rewriteWithNewAddressSpaces(
869  const TargetTransformInfo &TTI, ArrayRef<WeakTrackingVH> Postorder,
870  const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
871  // For each address expression to be modified, creates a clone of it with its
872  // pointer operands converted to the new address space. Since the pointer
873  // operands are converted, the clone is naturally in the new address space by
874  // construction.
875  ValueToValueMapTy ValueWithNewAddrSpace;
876  SmallVector<const Use *, 32> UndefUsesToFix;
877  for (Value* V : Postorder) {
878  unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
879  if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
880  ValueWithNewAddrSpace[V] = cloneValueWithNewAddressSpace(
881  V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix);
882  }
883  }
884 
885  if (ValueWithNewAddrSpace.empty())
886  return false;
887 
888  // Fixes all the undef uses generated by cloneInstructionWithNewAddressSpace.
889  for (const Use *UndefUse : UndefUsesToFix) {
890  User *V = UndefUse->getUser();
891  User *NewV = cast<User>(ValueWithNewAddrSpace.lookup(V));
892  unsigned OperandNo = UndefUse->getOperandNo();
893  assert(isa<UndefValue>(NewV->getOperand(OperandNo)));
894  NewV->setOperand(OperandNo, ValueWithNewAddrSpace.lookup(UndefUse->get()));
895  }
896 
897  SmallVector<Instruction *, 16> DeadInstructions;
898 
899  // Replaces the uses of the old address expressions with the new ones.
900  for (const WeakTrackingVH &WVH : Postorder) {
901  assert(WVH && "value was unexpectedly deleted");
902  Value *V = WVH;
903  Value *NewV = ValueWithNewAddrSpace.lookup(V);
904  if (NewV == nullptr)
905  continue;
906 
907  LLVM_DEBUG(dbgs() << "Replacing the uses of " << *V << "\n with\n "
908  << *NewV << '\n');
909 
910  if (Constant *C = dyn_cast<Constant>(V)) {
911  Constant *Replace = ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV),
912  C->getType());
913  if (C != Replace) {
914  LLVM_DEBUG(dbgs() << "Inserting replacement const cast: " << Replace
915  << ": " << *Replace << '\n');
916  C->replaceAllUsesWith(Replace);
917  V = Replace;
918  }
919  }
920 
921  Value::use_iterator I, E, Next;
922  for (I = V->use_begin(), E = V->use_end(); I != E; ) {
923  Use &U = *I;
924 
925  // Some users may see the same pointer operand in multiple operands. Skip
926  // to the next instruction.
927  I = skipToNextUser(I, E);
928 
930  TTI, U, V->getType()->getPointerAddressSpace())) {
931  // If V is used as the pointer operand of a compatible memory operation,
932  // sets the pointer operand to NewV. This replacement does not change
933  // the element type, so the resultant load/store is still valid.
934  U.set(NewV);
935  continue;
936  }
937 
938  User *CurUser = U.getUser();
939  // Handle more complex cases like intrinsic that need to be remangled.
940  if (auto *MI = dyn_cast<MemIntrinsic>(CurUser)) {
941  if (!MI->isVolatile() && handleMemIntrinsicPtrUse(MI, V, NewV))
942  continue;
943  }
944 
945  if (auto *II = dyn_cast<IntrinsicInst>(CurUser)) {
946  if (rewriteIntrinsicOperands(II, V, NewV))
947  continue;
948  }
949 
950  if (isa<Instruction>(CurUser)) {
951  if (ICmpInst *Cmp = dyn_cast<ICmpInst>(CurUser)) {
952  // If we can infer that both pointers are in the same addrspace,
953  // transform e.g.
954  // %cmp = icmp eq float* %p, %q
955  // into
956  // %cmp = icmp eq float addrspace(3)* %new_p, %new_q
957 
958  unsigned NewAS = NewV->getType()->getPointerAddressSpace();
959  int SrcIdx = U.getOperandNo();
960  int OtherIdx = (SrcIdx == 0) ? 1 : 0;
961  Value *OtherSrc = Cmp->getOperand(OtherIdx);
962 
963  if (Value *OtherNewV = ValueWithNewAddrSpace.lookup(OtherSrc)) {
964  if (OtherNewV->getType()->getPointerAddressSpace() == NewAS) {
965  Cmp->setOperand(OtherIdx, OtherNewV);
966  Cmp->setOperand(SrcIdx, NewV);
967  continue;
968  }
969  }
970 
971  // Even if the type mismatches, we can cast the constant.
972  if (auto *KOtherSrc = dyn_cast<Constant>(OtherSrc)) {
973  if (isSafeToCastConstAddrSpace(KOtherSrc, NewAS)) {
974  Cmp->setOperand(SrcIdx, NewV);
975  Cmp->setOperand(OtherIdx,
976  ConstantExpr::getAddrSpaceCast(KOtherSrc, NewV->getType()));
977  continue;
978  }
979  }
980  }
981 
982  if (AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(CurUser)) {
983  unsigned NewAS = NewV->getType()->getPointerAddressSpace();
984  if (ASC->getDestAddressSpace() == NewAS) {
985  if (ASC->getType()->getPointerElementType() !=
986  NewV->getType()->getPointerElementType()) {
987  NewV = CastInst::Create(Instruction::BitCast, NewV,
988  ASC->getType(), "", ASC);
989  }
990  ASC->replaceAllUsesWith(NewV);
991  DeadInstructions.push_back(ASC);
992  continue;
993  }
994  }
995 
996  // Otherwise, replaces the use with flat(NewV).
997  if (Instruction *I = dyn_cast<Instruction>(V)) {
998  BasicBlock::iterator InsertPos = std::next(I->getIterator());
999  while (isa<PHINode>(InsertPos))
1000  ++InsertPos;
1001  U.set(new AddrSpaceCastInst(NewV, V->getType(), "", &*InsertPos));
1002  } else {
1003  U.set(ConstantExpr::getAddrSpaceCast(cast<Constant>(NewV),
1004  V->getType()));
1005  }
1006  }
1007  }
1008 
1009  if (V->use_empty()) {
1010  if (Instruction *I = dyn_cast<Instruction>(V))
1011  DeadInstructions.push_back(I);
1012  }
1013  }
1014 
1015  for (Instruction *I : DeadInstructions)
1017 
1018  return true;
1019 }
1020 
1022  return new InferAddressSpaces();
1023 }
uint64_t CallInst * C
static bool handleMemIntrinsicPtrUse(MemIntrinsic *MI, Value *OldV, Value *NewV)
Update memory intrinsic uses that require more complex processing than simple memory instructions...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
use_iterator use_end()
Definition: Value.h:346
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1209
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
iterator begin() const
Definition: ArrayRef.h:136
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
static Constant * getAddrSpaceCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1794
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:899
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1160
Metadata node.
Definition: Metadata.h:863
F(f)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:534
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:403
Hexagon Common GEP
This defines the Use class.
Value * get() const
Definition: Use.h:107
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1155
static unsigned getOperandNumForIncomingValue(unsigned i)
AnalysisUsage & addRequired()
This class represents a conversion between pointers from one address space to another.
Type * getPointerElementType() const
Definition: Type.h:375
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:651
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
CallInst * CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memmove between the specified pointers.
Definition: IRBuilder.h:493
Type * getSourceElementType() const
Definition: Instructions.h:970
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Definition: Use.cpp:40
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:181
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1237
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:1987
This class represents a no-op cast from one type to another.
op_iterator idx_begin()
Definition: Instructions.h:998
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:234
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:125
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:47
bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) const
Return true if the given instruction (assumed to be a memory access instruction) has a volatile varia...
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:170
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1018
Value * getOperand(unsigned i) const
Definition: User.h:169
use_iterator_impl< Use > use_iterator
Definition: Value.h:331
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1782
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:873
static bool runOnFunction(Function &F, bool PostInlining)
static unsigned getPointerOperandIndex()
Definition: Instructions.h:619
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Wrapper pass for TargetTransformInfo.
void set(Value *Val)
Definition: Value.h:670
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static unsigned getPointerOperandIndex()
Definition: Instructions.h:414
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:223
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
Represent the analysis usage information of a pass.
This instruction compares its operands according to the predicate given to the constructor.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
op_range operands()
Definition: User.h:237
void initializeInferAddressSpacesPass(PassRegistry &)
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:430
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1424
static wasm::ValType getType(const TargetRegisterClass *RC)
Constant * getWithOperands(ArrayRef< Constant *> Ops) const
This returns the current constant expression with the operands replaced with the specified values...
Definition: Constants.h:1229
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:50
static Value * cloneConstantExprWithNewAddressSpace(ConstantExpr *CE, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace)
This is the common base class for memset/memcpy/memmove.
#define DEBUG_TYPE
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
Definition: User.h:191
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:30
static Value * cloneInstructionWithNewAddressSpace(Instruction *I, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace, SmallVectorImpl< const Use *> *UndefUsesToFix)
iterator end() const
Definition: ArrayRef.h:137
bool empty() const
Definition: ValueMap.h:145
CallInst * CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memcpy between the specified pointers.
Definition: IRBuilder.h:445
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
static const unsigned UninitializedAddressSpace
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
iterator_range< user_iterator > users()
Definition: Value.h:399
INITIALIZE_PASS(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces", false, false) static bool isAddressExpression(const Value &V)
use_iterator use_begin()
Definition: Value.h:338
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass&#39;s ...
bool hasValue() const
Definition: Optional.h:259
FunctionPass * createInferAddressSpacesPass()
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:192
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
static unsigned getPointerOperandIndex()
Definition: Instructions.h:813
static unsigned getPointerOperandIndex()
Definition: Instructions.h:286
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:72
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:40
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
static SmallVector< Value *, 2 > getPointerOperands(const Value &V)
static Value::use_iterator skipToNextUser(Value::use_iterator I, Value::use_iterator End)
IRTranslator LLVM IR MI
inst_range instructions(Function *F)
Definition: InstIterator.h:133
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
unsigned getFlatAddressSpace() const
Returns the address space ID for a target&#39;s &#39;flat&#39; address space.
static bool isSimplePointerUseValidToReplace(const TargetTransformInfo &TTI, Use &U, unsigned AddrSpace)
returns true if U is the pointer operand of a memory instruction with a single pointer operand that c...
bool use_empty() const
Definition: Value.h:322
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:43
const BasicBlock * getParent() const
Definition: Instruction.h:66
static Value * operandWithNewAddressSpaceOrCreateUndef(const Use &OperandUse, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace, SmallVectorImpl< const Use *> *UndefUsesToFix)