LLVM  16.0.0git
Value.cpp
Go to the documentation of this file.
1 //===-- Value.cpp - Implement the Value class -----------------------------===//
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 // This file implements the Value, ValueHandle, and User classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Value.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DerivedUser.h"
23 #include "llvm/IR/InstrTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/ValueHandle.h"
34 #include <algorithm>
35 
36 using namespace llvm;
37 
39  "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),
40  cl::desc("Deref attributes and metadata infer facts at definition only"));
41 
42 //===----------------------------------------------------------------------===//
43 // Value Class
44 //===----------------------------------------------------------------------===//
45 static inline Type *checkType(Type *Ty) {
46  assert(Ty && "Value defined with a null type: Error!");
47  assert(!isa<TypedPointerType>(Ty) &&
48  "Cannot have values with typed pointer types");
49  return Ty;
50 }
51 
52 Value::Value(Type *ty, unsigned scid)
53  : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
54  SubclassOptionalData(0), SubclassData(0), NumUserOperands(0),
55  IsUsedByMD(false), HasName(false), HasMetadata(false) {
56  static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
57  // FIXME: Why isn't this in the subclass gunk??
58  // Note, we cannot call isa<CallInst> before the CallInst has been
59  // constructed.
60  unsigned OpCode = 0;
61  if (SubclassID >= InstructionVal)
62  OpCode = SubclassID - InstructionVal;
63  if (OpCode == Instruction::Call || OpCode == Instruction::Invoke ||
64  OpCode == Instruction::CallBr)
65  assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
66  "invalid CallBase type!");
67  else if (SubclassID != BasicBlockVal &&
68  (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))
69  assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
70  "Cannot create non-first-class values except for constants!");
71  static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
72  "Value too big");
73 }
74 
76  // Notify all ValueHandles (if present) that this value is going away.
77  if (HasValueHandle)
79  if (isUsedByMetadata())
81 
82  // Remove associated metadata from context.
83  if (HasMetadata)
84  clearMetadata();
85 
86 #ifndef NDEBUG // Only in -g mode...
87  // Check to make sure that there are no uses of this value that are still
88  // around when the value is destroyed. If there are, then we have a dangling
89  // reference and something is wrong. This code is here to print out where
90  // the value is still being referenced.
91  //
92  // Note that use_empty() cannot be called here, as it eventually downcasts
93  // 'this' to GlobalValue (derived class of Value), but GlobalValue has already
94  // been destructed, so accessing it is UB.
95  //
96  if (!materialized_use_empty()) {
97  dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
98  for (auto *U : users())
99  dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
100  }
101 #endif
102  assert(materialized_use_empty() && "Uses remain when a value is destroyed!");
103 
104  // If this value is named, destroy the name. This should not be in a symtab
105  // at this point.
106  destroyValueName();
107 }
108 
110  switch (getValueID()) {
111 #define HANDLE_VALUE(Name) \
112  case Value::Name##Val: \
113  delete static_cast<Name *>(this); \
114  break;
115 #define HANDLE_MEMORY_VALUE(Name) \
116  case Value::Name##Val: \
117  static_cast<DerivedUser *>(this)->DeleteValue( \
118  static_cast<DerivedUser *>(this)); \
119  break;
120 #define HANDLE_CONSTANT(Name) \
121  case Value::Name##Val: \
122  llvm_unreachable("constants should be destroyed with destroyConstant"); \
123  break;
124 #define HANDLE_INSTRUCTION(Name) /* nothing */
125 #include "llvm/IR/Value.def"
126 
127 #define HANDLE_INST(N, OPC, CLASS) \
128  case Value::InstructionVal + Instruction::OPC: \
129  delete static_cast<CLASS *>(this); \
130  break;
131 #define HANDLE_USER_INST(N, OPC, CLASS)
132 #include "llvm/IR/Instruction.def"
133 
134  default:
135  llvm_unreachable("attempting to delete unknown value kind");
136  }
137 }
138 
139 void Value::destroyValueName() {
141  if (Name) {
143  Name->Destroy(Allocator);
144  }
145  setValueName(nullptr);
146 }
147 
148 bool Value::hasNUses(unsigned N) const {
149  return hasNItems(use_begin(), use_end(), N);
150 }
151 
152 bool Value::hasNUsesOrMore(unsigned N) const {
153  return hasNItemsOrMore(use_begin(), use_end(), N);
154 }
155 
156 bool Value::hasOneUser() const {
157  if (use_empty())
158  return false;
159  if (hasOneUse())
160  return true;
161  return std::equal(++user_begin(), user_end(), user_begin());
162 }
163 
164 static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
165 
167  Use *Result = nullptr;
168  for (Use &U : uses()) {
169  if (!U.getUser()->isDroppable()) {
170  if (Result)
171  return nullptr;
172  Result = &U;
173  }
174  }
175  return Result;
176 }
177 
179  User *Result = nullptr;
180  for (auto *U : users()) {
181  if (!U->isDroppable()) {
182  if (Result && Result != U)
183  return nullptr;
184  Result = U;
185  }
186  }
187  return Result;
188 }
189 
190 bool Value::hasNUndroppableUses(unsigned int N) const {
192 }
193 
194 bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {
196 }
197 
199  llvm::function_ref<bool(const Use *)> ShouldDrop) {
200  SmallVector<Use *, 8> ToBeEdited;
201  for (Use &U : uses())
202  if (U.getUser()->isDroppable() && ShouldDrop(&U))
203  ToBeEdited.push_back(&U);
204  for (Use *U : ToBeEdited)
205  dropDroppableUse(*U);
206 }
207 
209  assert(Usr.isDroppable() && "Expected a droppable user!");
210  for (Use &UsrOp : Usr.operands()) {
211  if (UsrOp.get() == this)
212  dropDroppableUse(UsrOp);
213  }
214 }
215 
217  U.removeFromList();
218  if (auto *Assume = dyn_cast<AssumeInst>(U.getUser())) {
219  unsigned OpNo = U.getOperandNo();
220  if (OpNo == 0)
221  U.set(ConstantInt::getTrue(Assume->getContext()));
222  else {
223  U.set(UndefValue::get(U.get()->getType()));
224  CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);
225  BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");
226  }
227  return;
228  }
229 
230  llvm_unreachable("unkown droppable use");
231 }
232 
234  // This can be computed either by scanning the instructions in BB, or by
235  // scanning the use list of this Value. Both lists can be very long, but
236  // usually one is quite short.
237  //
238  // Scan both lists simultaneously until one is exhausted. This limits the
239  // search to the shorter list.
240  BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
241  const_user_iterator UI = user_begin(), UE = user_end();
242  for (; BI != BE && UI != UE; ++BI, ++UI) {
243  // Scan basic block: Check if this Value is used by the instruction at BI.
244  if (is_contained(BI->operands(), this))
245  return true;
246  // Scan use list: Check if the use at UI is in BB.
247  const auto *User = dyn_cast<Instruction>(*UI);
248  if (User && User->getParent() == BB)
249  return true;
250  }
251  return false;
252 }
253 
254 unsigned Value::getNumUses() const {
255  return (unsigned)std::distance(use_begin(), use_end());
256 }
257 
258 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
259  ST = nullptr;
260  if (Instruction *I = dyn_cast<Instruction>(V)) {
261  if (BasicBlock *P = I->getParent())
262  if (Function *PP = P->getParent())
263  ST = PP->getValueSymbolTable();
264  } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
265  if (Function *P = BB->getParent())
266  ST = P->getValueSymbolTable();
267  } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
268  if (Module *P = GV->getParent())
269  ST = &P->getValueSymbolTable();
270  } else if (Argument *A = dyn_cast<Argument>(V)) {
271  if (Function *P = A->getParent())
272  ST = P->getValueSymbolTable();
273  } else {
274  assert(isa<Constant>(V) && "Unknown value type!");
275  return true; // no name is setable for this.
276  }
277  return false;
278 }
279 
281  if (!HasName) return nullptr;
282 
283  LLVMContext &Ctx = getContext();
284  auto I = Ctx.pImpl->ValueNames.find(this);
285  assert(I != Ctx.pImpl->ValueNames.end() &&
286  "No name entry found!");
287 
288  return I->second;
289 }
290 
292  LLVMContext &Ctx = getContext();
293 
294  assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
295  "HasName bit out of sync!");
296 
297  if (!VN) {
298  if (HasName)
299  Ctx.pImpl->ValueNames.erase(this);
300  HasName = false;
301  return;
302  }
303 
304  HasName = true;
305  Ctx.pImpl->ValueNames[this] = VN;
306 }
307 
309  // Make sure the empty string is still a C string. For historical reasons,
310  // some clients want to call .data() on the result and expect it to be null
311  // terminated.
312  if (!hasName())
313  return StringRef("", 0);
314  return getValueName()->getKey();
315 }
316 
317 void Value::setNameImpl(const Twine &NewName) {
318  // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
319  if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
320  return;
321 
322  // Fast path for common IRBuilder case of setName("") when there is no name.
323  if (NewName.isTriviallyEmpty() && !hasName())
324  return;
325 
326  SmallString<256> NameData;
327  StringRef NameRef = NewName.toStringRef(NameData);
328  assert(NameRef.find_first_of(0) == StringRef::npos &&
329  "Null bytes are not allowed in names");
330 
331  // Name isn't changing?
332  if (getName() == NameRef)
333  return;
334 
335  assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
336 
337  // Get the symbol table to update for this object.
339  if (getSymTab(this, ST))
340  return; // Cannot set a name on this value (e.g. constant).
341 
342  if (!ST) { // No symbol table to update? Just do the change.
343  if (NameRef.empty()) {
344  // Free the name for this value.
345  destroyValueName();
346  return;
347  }
348 
349  // NOTE: Could optimize for the case the name is shrinking to not deallocate
350  // then reallocated.
351  destroyValueName();
352 
353  // Create the new name.
356  getValueName()->setValue(this);
357  return;
358  }
359 
360  // NOTE: Could optimize for the case the name is shrinking to not deallocate
361  // then reallocated.
362  if (hasName()) {
363  // Remove old name.
364  ST->removeValueName(getValueName());
365  destroyValueName();
366 
367  if (NameRef.empty())
368  return;
369  }
370 
371  // Name is changing to something new.
372  setValueName(ST->createValueName(NameRef, this));
373 }
374 
375 void Value::setName(const Twine &NewName) {
376  setNameImpl(NewName);
377  if (Function *F = dyn_cast<Function>(this))
378  F->recalculateIntrinsicID();
379 }
380 
382  assert(V != this && "Illegal call to this->takeName(this)!");
383  ValueSymbolTable *ST = nullptr;
384  // If this value has a name, drop it.
385  if (hasName()) {
386  // Get the symtab this is in.
387  if (getSymTab(this, ST)) {
388  // We can't set a name on this value, but we need to clear V's name if
389  // it has one.
390  if (V->hasName()) V->setName("");
391  return; // Cannot set a name on this value (e.g. constant).
392  }
393 
394  // Remove old name.
395  if (ST)
396  ST->removeValueName(getValueName());
397  destroyValueName();
398  }
399 
400  // Now we know that this has no name.
401 
402  // If V has no name either, we're done.
403  if (!V->hasName()) return;
404 
405  // Get this's symtab if we didn't before.
406  if (!ST) {
407  if (getSymTab(this, ST)) {
408  // Clear V's name.
409  V->setName("");
410  return; // Cannot set a name on this value (e.g. constant).
411  }
412  }
413 
414  // Get V's ST, this should always succeed, because V has a name.
415  ValueSymbolTable *VST;
416  bool Failure = getSymTab(V, VST);
417  assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
418 
419  // If these values are both in the same symtab, we can do this very fast.
420  // This works even if both values have no symtab yet.
421  if (ST == VST) {
422  // Take the name!
424  V->setValueName(nullptr);
425  getValueName()->setValue(this);
426  return;
427  }
428 
429  // Otherwise, things are slightly more complex. Remove V's name from VST and
430  // then reinsert it into ST.
431 
432  if (VST)
433  VST->removeValueName(V->getValueName());
435  V->setValueName(nullptr);
436  getValueName()->setValue(this);
437 
438  if (ST)
439  ST->reinsertValue(this);
440 }
441 
442 #ifndef NDEBUG
443 std::string Value::getNameOrAsOperand() const {
444  if (!getName().empty())
445  return std::string(getName());
446 
447  std::string BBName;
448  raw_string_ostream OS(BBName);
449  printAsOperand(OS, false);
450  return OS.str();
451 }
452 #endif
453 
455 #ifndef NDEBUG
456  const GlobalValue *GV = dyn_cast<GlobalValue>(this);
457  if (!GV)
458  return;
459  const Module *M = GV->getParent();
460  if (!M)
461  return;
462  assert(M->isMaterialized());
463 #endif
464 }
465 
466 #ifndef NDEBUG
468  Constant *C) {
469  if (!Cache.insert(Expr).second)
470  return false;
471 
472  for (auto &O : Expr->operands()) {
473  if (O == C)
474  return true;
475  auto *CE = dyn_cast<ConstantExpr>(O);
476  if (!CE)
477  continue;
478  if (contains(Cache, CE, C))
479  return true;
480  }
481  return false;
482 }
483 
484 static bool contains(Value *Expr, Value *V) {
485  if (Expr == V)
486  return true;
487 
488  auto *C = dyn_cast<Constant>(V);
489  if (!C)
490  return false;
491 
492  auto *CE = dyn_cast<ConstantExpr>(Expr);
493  if (!CE)
494  return false;
495 
497  return contains(Cache, CE, C);
498 }
499 #endif // NDEBUG
500 
501 void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
502  assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
503  assert(!contains(New, this) &&
504  "this->replaceAllUsesWith(expr(this)) is NOT valid!");
505  assert(New->getType() == getType() &&
506  "replaceAllUses of value with new value of different type!");
507 
508  // Notify all ValueHandles (if present) that this value is going away.
509  if (HasValueHandle)
511  if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())
512  ValueAsMetadata::handleRAUW(this, New);
513 
514  while (!materialized_use_empty()) {
515  Use &U = *UseList;
516  // Must handle Constants specially, we cannot call replaceUsesOfWith on a
517  // constant because they are uniqued.
518  if (auto *C = dyn_cast<Constant>(U.getUser())) {
519  if (!isa<GlobalValue>(C)) {
520  C->handleOperandChange(this, New);
521  continue;
522  }
523  }
524 
525  U.set(New);
526  }
527 
528  if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
529  BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
530 }
531 
533  doRAUW(New, ReplaceMetadataUses::Yes);
534 }
535 
537  doRAUW(New, ReplaceMetadataUses::No);
538 }
539 
541  llvm::function_ref<bool(Use &U)> ShouldReplace) {
542  assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
543  assert(New->getType() == getType() &&
544  "replaceUses of value with new value of different type!");
545 
548 
549  for (Use &U : llvm::make_early_inc_range(uses())) {
550  if (!ShouldReplace(U))
551  continue;
552  // Must handle Constants specially, we cannot call replaceUsesOfWith on a
553  // constant because they are uniqued.
554  if (auto *C = dyn_cast<Constant>(U.getUser())) {
555  if (!isa<GlobalValue>(C)) {
556  if (Visited.insert(C).second)
557  Consts.push_back(TrackingVH<Constant>(C));
558  continue;
559  }
560  }
561  U.set(New);
562  }
563 
564  while (!Consts.empty()) {
565  // FIXME: handleOperandChange() updates all the uses in a given Constant,
566  // not just the one passed to ShouldReplace
567  Consts.pop_back_val()->handleOperandChange(this, New);
568  }
569 }
570 
571 /// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
572 /// with New.
575  findDbgUsers(DbgUsers, V);
576  for (auto *DVI : DbgUsers) {
577  if (DVI->getParent() != BB)
578  DVI->replaceVariableLocationOp(V, New);
579  }
580 }
581 
582 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
583 // This routine leaves uses within BB.
585  assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
586  assert(!contains(New, this) &&
587  "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
588  assert(New->getType() == getType() &&
589  "replaceUses of value with new value of different type!");
590  assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
591 
592  replaceDbgUsesOutsideBlock(this, New, BB);
593  replaceUsesWithIf(New, [BB](Use &U) {
594  auto *I = dyn_cast<Instruction>(U.getUser());
595  // Don't replace if it's an instruction in the BB basic block.
596  return !I || I->getParent() != BB;
597  });
598 }
599 
600 namespace {
601 // Various metrics for how much to strip off of pointers.
602 enum PointerStripKind {
603  PSK_ZeroIndices,
604  PSK_ZeroIndicesAndAliases,
605  PSK_ZeroIndicesSameRepresentation,
606  PSK_ForAliasAnalysis,
607  PSK_InBoundsConstantIndices,
608  PSK_InBounds
609 };
610 
611 template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}
612 
613 template <PointerStripKind StripKind>
614 static const Value *stripPointerCastsAndOffsets(
615  const Value *V,
616  function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {
617  if (!V->getType()->isPointerTy())
618  return V;
619 
620  // Even though we don't look through PHI nodes, we could be called on an
621  // instruction in an unreachable block, which may be on a cycle.
623 
624  Visited.insert(V);
625  do {
626  Func(V);
627  if (auto *GEP = dyn_cast<GEPOperator>(V)) {
628  switch (StripKind) {
629  case PSK_ZeroIndices:
630  case PSK_ZeroIndicesAndAliases:
631  case PSK_ZeroIndicesSameRepresentation:
632  case PSK_ForAliasAnalysis:
633  if (!GEP->hasAllZeroIndices())
634  return V;
635  break;
636  case PSK_InBoundsConstantIndices:
637  if (!GEP->hasAllConstantIndices())
638  return V;
639  [[fallthrough]];
640  case PSK_InBounds:
641  if (!GEP->isInBounds())
642  return V;
643  break;
644  }
645  V = GEP->getPointerOperand();
646  } else if (Operator::getOpcode(V) == Instruction::BitCast) {
647  V = cast<Operator>(V)->getOperand(0);
648  if (!V->getType()->isPointerTy())
649  return V;
650  } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
651  Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
652  // TODO: If we know an address space cast will not change the
653  // representation we could look through it here as well.
654  V = cast<Operator>(V)->getOperand(0);
655  } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
656  V = cast<GlobalAlias>(V)->getAliasee();
657  } else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&
658  cast<PHINode>(V)->getNumIncomingValues() == 1) {
659  V = cast<PHINode>(V)->getIncomingValue(0);
660  } else {
661  if (const auto *Call = dyn_cast<CallBase>(V)) {
662  if (const Value *RV = Call->getReturnedArgOperand()) {
663  V = RV;
664  continue;
665  }
666  // The result of launder.invariant.group must alias it's argument,
667  // but it can't be marked with returned attribute, that's why it needs
668  // special case.
669  if (StripKind == PSK_ForAliasAnalysis &&
670  (Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
671  Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {
672  V = Call->getArgOperand(0);
673  continue;
674  }
675  }
676  return V;
677  }
678  assert(V->getType()->isPointerTy() && "Unexpected operand type!");
679  } while (Visited.insert(V).second);
680 
681  return V;
682 }
683 } // end anonymous namespace
684 
686  return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
687 }
688 
690  return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
691 }
692 
694  return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);
695 }
696 
698  return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
699 }
700 
702  return stripPointerCastsAndOffsets<PSK_ForAliasAnalysis>(this);
703 }
704 
706  const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
707  bool AllowInvariantGroup,
708  function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
709  if (!getType()->isPtrOrPtrVectorTy())
710  return this;
711 
712  unsigned BitWidth = Offset.getBitWidth();
713  assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
714  "The offset bit width does not match the DL specification.");
715 
716  // Even though we don't look through PHI nodes, we could be called on an
717  // instruction in an unreachable block, which may be on a cycle.
719  Visited.insert(this);
720  const Value *V = this;
721  do {
722  if (auto *GEP = dyn_cast<GEPOperator>(V)) {
723  // If in-bounds was requested, we do not strip non-in-bounds GEPs.
724  if (!AllowNonInbounds && !GEP->isInBounds())
725  return V;
726 
727  // If one of the values we have visited is an addrspacecast, then
728  // the pointer type of this GEP may be different from the type
729  // of the Ptr parameter which was passed to this function. This
730  // means when we construct GEPOffset, we need to use the size
731  // of GEP's pointer type rather than the size of the original
732  // pointer type.
733  APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
734  if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
735  return V;
736 
737  // Stop traversal if the pointer offset wouldn't fit in the bit-width
738  // provided by the Offset argument. This can happen due to AddrSpaceCast
739  // stripping.
740  if (GEPOffset.getMinSignedBits() > BitWidth)
741  return V;
742 
743  // External Analysis can return a result higher/lower than the value
744  // represents. We need to detect overflow/underflow.
745  APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
746  if (!ExternalAnalysis) {
747  Offset += GEPOffsetST;
748  } else {
749  bool Overflow = false;
750  APInt OldOffset = Offset;
751  Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
752  if (Overflow) {
753  Offset = OldOffset;
754  return V;
755  }
756  }
757  V = GEP->getPointerOperand();
758  } else if (Operator::getOpcode(V) == Instruction::BitCast ||
759  Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
760  V = cast<Operator>(V)->getOperand(0);
761  } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
762  if (!GA->isInterposable())
763  V = GA->getAliasee();
764  } else if (const auto *Call = dyn_cast<CallBase>(V)) {
765  if (const Value *RV = Call->getReturnedArgOperand())
766  V = RV;
767  if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
768  V = Call->getArgOperand(0);
769  }
770  assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
771  } while (Visited.insert(V).second);
772 
773  return V;
774 }
775 
776 const Value *
777 Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {
778  return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func);
779 }
780 
781 bool Value::canBeFreed() const {
782  assert(getType()->isPointerTy());
783 
784  // Cases that can simply never be deallocated
785  // *) Constants aren't allocated per se, thus not deallocated either.
786  if (isa<Constant>(this))
787  return false;
788 
789  // Handle byval/byref/sret/inalloca/preallocated arguments. The storage
790  // lifetime is guaranteed to be longer than the callee's lifetime.
791  if (auto *A = dyn_cast<Argument>(this)) {
792  if (A->hasPointeeInMemoryValueAttr())
793  return false;
794  // A pointer to an object in a function which neither frees, nor can arrange
795  // for another thread to free on its behalf, can not be freed in the scope
796  // of the function. Note that this logic is restricted to memory
797  // allocations in existance before the call; a nofree function *is* allowed
798  // to free memory it allocated.
799  const Function *F = A->getParent();
800  if (F->doesNotFreeMemory() && F->hasNoSync())
801  return false;
802  }
803 
804  const Function *F = nullptr;
805  if (auto *I = dyn_cast<Instruction>(this))
806  F = I->getFunction();
807  if (auto *A = dyn_cast<Argument>(this))
808  F = A->getParent();
809 
810  if (!F)
811  return true;
812 
813  // With garbage collection, deallocation typically occurs solely at or after
814  // safepoints. If we're compiling for a collector which uses the
815  // gc.statepoint infrastructure, safepoints aren't explicitly present
816  // in the IR until after lowering from abstract to physical machine model.
817  // The collector could chose to mix explicit deallocation and gc'd objects
818  // which is why we need the explicit opt in on a per collector basis.
819  if (!F->hasGC())
820  return true;
821 
822  const auto &GCName = F->getGC();
823  if (GCName == "statepoint-example") {
824  auto *PT = cast<PointerType>(this->getType());
825  if (PT->getAddressSpace() != 1)
826  // For the sake of this example GC, we arbitrarily pick addrspace(1) as
827  // our GC managed heap. This must match the same check in
828  // RewriteStatepointsForGC (and probably needs better factored.)
829  return true;
830 
831  // It is cheaper to scan for a declaration than to scan for a use in this
832  // function. Note that gc.statepoint is a type overloaded function so the
833  // usual trick of requesting declaration of the intrinsic from the module
834  // doesn't work.
835  for (auto &Fn : *F->getParent())
836  if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)
837  return true;
838  return false;
839  }
840  return true;
841 }
842 
844  bool &CanBeNull,
845  bool &CanBeFreed) const {
846  assert(getType()->isPointerTy() && "must be pointer");
847 
848  uint64_t DerefBytes = 0;
849  CanBeNull = false;
850  CanBeFreed = UseDerefAtPointSemantics && canBeFreed();
851  if (const Argument *A = dyn_cast<Argument>(this)) {
852  DerefBytes = A->getDereferenceableBytes();
853  if (DerefBytes == 0) {
854  // Handle byval/byref/inalloca/preallocated arguments
855  if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {
856  if (ArgMemTy->isSized()) {
857  // FIXME: Why isn't this the type alloc size?
858  DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinSize();
859  }
860  }
861  }
862 
863  if (DerefBytes == 0) {
864  DerefBytes = A->getDereferenceableOrNullBytes();
865  CanBeNull = true;
866  }
867  } else if (const auto *Call = dyn_cast<CallBase>(this)) {
868  DerefBytes = Call->getRetDereferenceableBytes();
869  if (DerefBytes == 0) {
870  DerefBytes = Call->getRetDereferenceableOrNullBytes();
871  CanBeNull = true;
872  }
873  } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
874  if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
875  ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
876  DerefBytes = CI->getLimitedValue();
877  }
878  if (DerefBytes == 0) {
879  if (MDNode *MD =
880  LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
881  ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
882  DerefBytes = CI->getLimitedValue();
883  }
884  CanBeNull = true;
885  }
886  } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {
887  if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {
888  ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
889  DerefBytes = CI->getLimitedValue();
890  }
891  if (DerefBytes == 0) {
892  if (MDNode *MD =
893  IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
894  ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
895  DerefBytes = CI->getLimitedValue();
896  }
897  CanBeNull = true;
898  }
899  } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
900  if (!AI->isArrayAllocation()) {
901  DerefBytes =
902  DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinSize();
903  CanBeNull = false;
904  CanBeFreed = false;
905  }
906  } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
907  if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
908  // TODO: Don't outright reject hasExternalWeakLinkage but set the
909  // CanBeNull flag.
910  DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedSize();
911  CanBeNull = false;
912  CanBeFreed = false;
913  }
914  }
915  return DerefBytes;
916 }
917 
919  assert(getType()->isPointerTy() && "must be pointer");
920  if (auto *GO = dyn_cast<GlobalObject>(this)) {
921  if (isa<Function>(GO)) {
922  Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
923  switch (DL.getFunctionPtrAlignType()) {
925  return FunctionPtrAlign;
927  return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne());
928  }
929  llvm_unreachable("Unhandled FunctionPtrAlignType");
930  }
931  const MaybeAlign Alignment(GO->getAlign());
932  if (!Alignment) {
933  if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
934  Type *ObjectType = GVar->getValueType();
935  if (ObjectType->isSized()) {
936  // If the object is defined in the current Module, we'll be giving
937  // it the preferred alignment. Otherwise, we have to assume that it
938  // may only have the minimum ABI alignment.
939  if (GVar->isStrongDefinitionForLinker())
940  return DL.getPreferredAlign(GVar);
941  else
942  return DL.getABITypeAlign(ObjectType);
943  }
944  }
945  }
946  return Alignment.valueOrOne();
947  } else if (const Argument *A = dyn_cast<Argument>(this)) {
948  const MaybeAlign Alignment = A->getParamAlign();
949  if (!Alignment && A->hasStructRetAttr()) {
950  // An sret parameter has at least the ABI alignment of the return type.
951  Type *EltTy = A->getParamStructRetType();
952  if (EltTy->isSized())
953  return DL.getABITypeAlign(EltTy);
954  }
955  return Alignment.valueOrOne();
956  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
957  return AI->getAlign();
958  } else if (const auto *Call = dyn_cast<CallBase>(this)) {
959  MaybeAlign Alignment = Call->getRetAlign();
960  if (!Alignment && Call->getCalledFunction())
961  Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment();
962  return Alignment.valueOrOne();
963  } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
964  if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
965  ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
966  return Align(CI->getLimitedValue());
967  }
968  } else if (auto *CstPtr = dyn_cast<Constant>(this)) {
969  // Strip pointer casts to avoid creating unnecessary ptrtoint expression
970  // if the only "reduction" is combining a bitcast + ptrtoint.
971  CstPtr = CstPtr->stripPointerCasts();
972  if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
973  const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
974  /*OnlyIfReduced=*/true))) {
975  size_t TrailingZeros = CstInt->getValue().countTrailingZeros();
976  // While the actual alignment may be large, elsewhere we have
977  // an arbitrary upper alignmet limit, so let's clamp to it.
978  return Align(TrailingZeros < Value::MaxAlignmentExponent
979  ? uint64_t(1) << TrailingZeros
981  }
982  }
983  return Align(1);
984 }
985 
987  const BasicBlock *PredBB) const {
988  auto *PN = dyn_cast<PHINode>(this);
989  if (PN && PN->getParent() == CurBB)
990  return PN->getIncomingValueForBlock(PredBB);
991  return this;
992 }
993 
994 LLVMContext &Value::getContext() const { return VTy->getContext(); }
995 
997  if (!UseList || !UseList->Next)
998  // No need to reverse 0 or 1 uses.
999  return;
1000 
1001  Use *Head = UseList;
1002  Use *Current = UseList->Next;
1003  Head->Next = nullptr;
1004  while (Current) {
1005  Use *Next = Current->Next;
1006  Current->Next = Head;
1007  Head->Prev = &Current->Next;
1008  Head = Current;
1009  Current = Next;
1010  }
1011  UseList = Head;
1012  Head->Prev = &UseList;
1013 }
1014 
1015 bool Value::isSwiftError() const {
1016  auto *Arg = dyn_cast<Argument>(this);
1017  if (Arg)
1018  return Arg->hasSwiftErrorAttr();
1019  auto *Alloca = dyn_cast<AllocaInst>(this);
1020  if (!Alloca)
1021  return false;
1022  return Alloca->isSwiftError();
1023 }
1024 
1028  while (!WorkList.empty()) {
1029  const User *U = WorkList.pop_back_val();
1030  // If it is transitively used by a global value or a non-constant value,
1031  // it's obviously not only used by metadata.
1032  if (!isa<Constant>(U) || isa<GlobalValue>(U))
1033  return false;
1034  for (const User *UU : U->users())
1035  if (Visited.insert(UU).second)
1036  WorkList.push_back(UU);
1037  }
1038  return true;
1039 }
1040 
1041 //===----------------------------------------------------------------------===//
1042 // ValueHandleBase Class
1043 //===----------------------------------------------------------------------===//
1044 
1045 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
1046  assert(List && "Handle list is null?");
1047 
1048  // Splice ourselves into the list.
1049  Next = *List;
1050  *List = this;
1051  setPrevPtr(List);
1052  if (Next) {
1053  Next->setPrevPtr(&Next);
1054  assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
1055  }
1056 }
1057 
1058 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
1059  assert(List && "Must insert after existing node");
1060 
1061  Next = List->Next;
1062  setPrevPtr(&List->Next);
1063  List->Next = this;
1064  if (Next)
1065  Next->setPrevPtr(&Next);
1066 }
1067 
1068 void ValueHandleBase::AddToUseList() {
1069  assert(getValPtr() && "Null pointer doesn't have a use list!");
1070 
1071  LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1072 
1073  if (getValPtr()->HasValueHandle) {
1074  // If this value already has a ValueHandle, then it must be in the
1075  // ValueHandles map already.
1076  ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
1077  assert(Entry && "Value doesn't have any handles?");
1078  AddToExistingUseList(&Entry);
1079  return;
1080  }
1081 
1082  // Ok, it doesn't have any handles yet, so we must insert it into the
1083  // DenseMap. However, doing this insertion could cause the DenseMap to
1084  // reallocate itself, which would invalidate all of the PrevP pointers that
1085  // point into the old table. Handle this by checking for reallocation and
1086  // updating the stale pointers only if needed.
1088  const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
1089 
1090  ValueHandleBase *&Entry = Handles[getValPtr()];
1091  assert(!Entry && "Value really did already have handles?");
1092  AddToExistingUseList(&Entry);
1093  getValPtr()->HasValueHandle = true;
1094 
1095  // If reallocation didn't happen or if this was the first insertion, don't
1096  // walk the table.
1097  if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
1098  Handles.size() == 1) {
1099  return;
1100  }
1101 
1102  // Okay, reallocation did happen. Fix the Prev Pointers.
1104  E = Handles.end(); I != E; ++I) {
1105  assert(I->second && I->first == I->second->getValPtr() &&
1106  "List invariant broken!");
1107  I->second->setPrevPtr(&I->second);
1108  }
1109 }
1110 
1112  assert(getValPtr() && getValPtr()->HasValueHandle &&
1113  "Pointer doesn't have a use list!");
1114 
1115  // Unlink this from its use list.
1116  ValueHandleBase **PrevPtr = getPrevPtr();
1117  assert(*PrevPtr == this && "List invariant broken");
1118 
1119  *PrevPtr = Next;
1120  if (Next) {
1121  assert(Next->getPrevPtr() == &Next && "List invariant broken");
1122  Next->setPrevPtr(PrevPtr);
1123  return;
1124  }
1125 
1126  // If the Next pointer was null, then it is possible that this was the last
1127  // ValueHandle watching VP. If so, delete its entry from the ValueHandles
1128  // map.
1129  LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
1131  if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
1132  Handles.erase(getValPtr());
1133  getValPtr()->HasValueHandle = false;
1134  }
1135 }
1136 
1138  assert(V->HasValueHandle && "Should only be called if ValueHandles present");
1139 
1140  // Get the linked list base, which is guaranteed to exist since the
1141  // HasValueHandle flag is set.
1142  LLVMContextImpl *pImpl = V->getContext().pImpl;
1143  ValueHandleBase *Entry = pImpl->ValueHandles[V];
1144  assert(Entry && "Value bit set but no entries exist");
1145 
1146  // We use a local ValueHandleBase as an iterator so that ValueHandles can add
1147  // and remove themselves from the list without breaking our iteration. This
1148  // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
1149  // Note that we deliberately do not the support the case when dropping a value
1150  // handle results in a new value handle being permanently added to the list
1151  // (as might occur in theory for CallbackVH's): the new value handle will not
1152  // be processed and the checking code will mete out righteous punishment if
1153  // the handle is still present once we have finished processing all the other
1154  // value handles (it is fine to momentarily add then remove a value handle).
1155  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1156  Iterator.RemoveFromUseList();
1157  Iterator.AddToExistingUseListAfter(Entry);
1158  assert(Entry->Next == &Iterator && "Loop invariant broken.");
1159 
1160  switch (Entry->getKind()) {
1161  case Assert:
1162  break;
1163  case Weak:
1164  case WeakTracking:
1165  // WeakTracking and Weak just go to null, which unlinks them
1166  // from the list.
1167  Entry->operator=(nullptr);
1168  break;
1169  case Callback:
1170  // Forward to the subclass's implementation.
1171  static_cast<CallbackVH*>(Entry)->deleted();
1172  break;
1173  }
1174  }
1175 
1176  // All callbacks, weak references, and assertingVHs should be dropped by now.
1177  if (V->HasValueHandle) {
1178 #ifndef NDEBUG // Only in +Asserts mode...
1179  dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
1180  << "\n";
1181  if (pImpl->ValueHandles[V]->getKind() == Assert)
1182  llvm_unreachable("An asserting value handle still pointed to this"
1183  " value!");
1184 
1185 #endif
1186  llvm_unreachable("All references to V were not removed?");
1187  }
1188 }
1189 
1191  assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
1192  assert(Old != New && "Changing value into itself!");
1193  assert(Old->getType() == New->getType() &&
1194  "replaceAllUses of value with new value of different type!");
1195 
1196  // Get the linked list base, which is guaranteed to exist since the
1197  // HasValueHandle flag is set.
1198  LLVMContextImpl *pImpl = Old->getContext().pImpl;
1199  ValueHandleBase *Entry = pImpl->ValueHandles[Old];
1200 
1201  assert(Entry && "Value bit set but no entries exist");
1202 
1203  // We use a local ValueHandleBase as an iterator so that
1204  // ValueHandles can add and remove themselves from the list without
1205  // breaking our iteration. This is not really an AssertingVH; we
1206  // just have to give ValueHandleBase some kind.
1207  for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
1208  Iterator.RemoveFromUseList();
1209  Iterator.AddToExistingUseListAfter(Entry);
1210  assert(Entry->Next == &Iterator && "Loop invariant broken.");
1211 
1212  switch (Entry->getKind()) {
1213  case Assert:
1214  case Weak:
1215  // Asserting and Weak handles do not follow RAUW implicitly.
1216  break;
1217  case WeakTracking:
1218  // Weak goes to the new value, which will unlink it from Old's list.
1219  Entry->operator=(New);
1220  break;
1221  case Callback:
1222  // Forward to the subclass's implementation.
1223  static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
1224  break;
1225  }
1226  }
1227 
1228 #ifndef NDEBUG
1229  // If any new weak value handles were added while processing the
1230  // list, then complain about it now.
1231  if (Old->HasValueHandle)
1232  for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
1233  switch (Entry->getKind()) {
1234  case WeakTracking:
1235  dbgs() << "After RAUW from " << *Old->getType() << " %"
1236  << Old->getName() << " to " << *New->getType() << " %"
1237  << New->getName() << "\n";
1239  "A weak tracking value handle still pointed to the old value!\n");
1240  default:
1241  break;
1242  }
1243 #endif
1244 }
1245 
1246 // Pin the vtable to this file.
1247 void CallbackVH::anchor() {}
llvm::LLVMContext::pImpl
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:69
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:269
llvm::Value::const_user_iterator
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:391
llvm::Value::stripPointerCastsAndAliases
const Value * stripPointerCastsAndAliases() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
Definition: Value.cpp:689
llvm::StringMapEntry::Create
static StringMapEntry * Create(StringRef key, AllocatorTy &allocator, InitTy &&...initVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
Definition: StringMapEntry.h:125
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Value::isUsedInBasicBlock
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
Definition: Value.cpp:233
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::Value::dropDroppableUse
static void dropDroppableUse(Use &U)
Remove the droppable use U.
Definition: Value.cpp:216
llvm::Value::getPointerAlignment
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:918
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:101
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::Value::hasOneUse
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
llvm::Value::MaximumAlignment
static constexpr uint64_t MaximumAlignment
Definition: Value.h:793
llvm::User::operands
op_range operands()
Definition: User.h:242
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:223
llvm::hasNItems
bool hasNItems(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)>>::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has exactly N items.
Definition: STLExtras.h:2116
llvm::ValueHandleBase
This is the common base class of value handles.
Definition: ValueHandle.h:29
checkType
static Type * checkType(Type *Ty)
Definition: Value.cpp:45
llvm::Function
Definition: Function.h:60
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::DataLayout::FunctionPtrAlignType::Independent
@ Independent
The function pointer alignment is independent of the function alignment.
llvm::CallBase::BundleOpInfo::Tag
StringMapEntry< uint32_t > * Tag
The operand bundle tag, interned by LLVMContextImpl::getOrInsertBundleTag.
Definition: InstrTypes.h:2152
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:628
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:52
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
llvm::Value::hasName
bool hasName() const
Definition: Value.h:261
ErrorHandling.h
llvm::Use::get
Value * get() const
Definition: Use.h:66
llvm::findDbgUsers
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V)
Finds the debug info intrinsics describing a value.
Definition: DebugInfo.cpp:101
llvm::ValueHandleBase::Callback
@ Callback
Definition: ValueHandle.h:37
llvm::ValueAsMetadata::handleDeletion
static void handleDeletion(Value *V)
Definition: Metadata.cpp:417
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::DenseMapBase::erase
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
llvm::ConstantInt::getLimitedValue
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:244
llvm::DenseMapBase::begin
iterator begin()
Definition: DenseMap.h:75
llvm::StringRef::find_first_of
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:368
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Twine::isTriviallyEmpty
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition: Twine.h:417
DenseMap.h
Module.h
llvm::MaybeAlign::valueOrOne
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:142
llvm::Use::getOperandNo
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:31
llvm::ValueHandleBase::getValPtr
Value * getValPtr() const
Definition: ValueHandle.h:99
llvm::ValueHandleBase::WeakTracking
@ WeakTracking
Definition: ValueHandle.h:37
llvm::Value::hasOneUser
bool hasOneUser() const
Return true if there is exactly one user of this value.
Definition: Value.cpp:156
llvm::DenseMapBase::isPointerIntoBucketsArray
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
Definition: DenseMap.h:348
llvm::Value::stripAndAccumulateConstantOffsets
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::LLVMContextImpl::ValueNames
DenseMap< const Value *, ValueName * > ValueNames
Definition: LLVMContextImpl.h:1400
Operator.h
llvm::Value::user_begin
user_iterator user_begin()
Definition: Value.h:397
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
DerivedUser.h
llvm::Value::dropDroppableUses
void dropDroppableUses(llvm::function_ref< bool(const Use *)> ShouldDrop=[](const Use *) { return true;})
Remove every uses that can safely be removed.
Definition: Value.cpp:198
llvm::DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign
@ MultipleOfFunctionAlign
The function pointer alignment is a multiple of the function alignment.
contains
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition: Value.cpp:467
F
#define F(x, y, z)
Definition: MD5.cpp:55
UseDerefAtPointSemantics
static cl::opt< unsigned > UseDerefAtPointSemantics("use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false), cl::desc("Deref attributes and metadata infer facts at definition only"))
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::lltok::equal
@ equal
Definition: LLToken.h:25
llvm::Value::isSwiftError
bool isSwiftError() const
Return true if this value is a swifterror value.
Definition: Value.cpp:1015
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:187
CommandLine.h
llvm::Value::reverseUseList
void reverseUseList()
Reverse the use-list.
Definition: Value.cpp:996
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::Value::HasName
unsigned HasName
Definition: Value.h:118
Constants.h
llvm::LLVMContextImpl::ValueHandles
ValueHandlesTy ValueHandles
Definition: LLVMContextImpl.h:1494
SmallString.h
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::User
Definition: User.h:44
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::LLVMContextImpl
Definition: LLVMContextImpl.h:1355
llvm::Value::getSingleUndroppableUse
Use * getSingleUndroppableUse()
Return true if there is exactly one use of this value that cannot be dropped.
Definition: Value.cpp:166
InstrTypes.h
llvm::Value::assertModuleIsMaterializedImpl
void assertModuleIsMaterializedImpl() const
Definition: Value.cpp:454
llvm::ConstantExpr::getPtrToInt
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2174
IP
Definition: NVPTXLowerArgs.cpp:168
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition: Value.h:376
false
Definition: StackSlotColoring.cpp:141
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:532
llvm::Instruction
Definition: Instruction.h:42
LLVMContextImpl.h
llvm::Operator::getOpcode
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:42
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:375
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1713
llvm::Use::getUser
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
llvm::hasNItemsOrMore
bool hasNItemsOrMore(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;}, std::enable_if_t< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< std::remove_reference_t< decltype(Begin)>>::iterator_category >::value, void > *=nullptr)
Return true if the sequence [Begin, End) has N or more items.
Definition: STLExtras.h:2141
llvm::CallBase::BundleOpInfo
Used to keep track of an operand bundle.
Definition: InstrTypes.h:2149
llvm::DenseMapBase::getPointerIntoBucketsArray
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
Definition: DenseMap.h:355
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
llvm::Value::MaxAlignmentExponent
static constexpr unsigned MaxAlignmentExponent
The maximum alignment for instructions.
Definition: Value.h:792
llvm::Value::isTransitiveUsedByMetadataOnly
bool isTransitiveUsedByMetadataOnly() const
Definition: Value.cpp:1025
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:344
llvm::SmallString< 256 >
llvm::Value::dropDroppableUsesIn
void dropDroppableUsesIn(User &Usr)
Remove every use of this value in User that can safely be removed.
Definition: Value.cpp:208
llvm::ValueHandleBase::ValueIsRAUWd
static void ValueIsRAUWd(Value *Old, Value *New)
Definition: Value.cpp:1190
ValueSymbolTable.h
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::Value::user_end
user_iterator user_end()
Definition: Value.h:405
llvm::Value::stripPointerCastsSameRepresentation
const Value * stripPointerCastsSameRepresentation() const
Strip off pointer casts, all-zero GEPs and address space casts but ensures the representation of the ...
Definition: Value.cpp:693
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
llvm::Value::Value
Value(Type *Ty, unsigned scid)
Definition: Value.cpp:52
llvm::Value::hasNUndroppableUsesOrMore
bool hasNUndroppableUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:194
llvm::Use::set
void set(Value *Val)
Definition: Value.h:868
llvm::cl::opt
Definition: CommandLine.h:1400
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
llvm::ValueHandleBase::Assert
@ Assert
Definition: ValueHandle.h:37
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::Value::getNameOrAsOperand
std::string getNameOrAsOperand() const
Definition: Value.cpp:443
llvm::Value::getPointerDereferenceableBytes
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull, bool &CanBeFreed) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Definition: Value.cpp:843
uint64_t
llvm::ValueHandleBase::RemoveFromUseList
void RemoveFromUseList()
Remove this ValueHandle from its current use list.
Definition: Value.cpp:1111
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:650
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
llvm::Value::getUniqueUndroppableUser
User * getUniqueUndroppableUser()
Return true if there is exactly one unique user of this value that cannot be dropped (that user can h...
Definition: Value.cpp:178
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::Value::hasNUsesOrMore
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N uses or more.
Definition: Value.cpp:152
llvm::Value::materialized_use_empty
bool materialized_use_empty() const
Definition: Value.h:349
llvm::DenseMap< Value *, ValueHandleBase * >
DebugInfo.h
llvm::StringMapEntryStorage::setValue
void setValue(const ValueTy &V)
Definition: StringMapEntry.h:85
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:440
llvm::ValueHandleBase::Weak
@ Weak
Definition: ValueHandle.h:37
llvm::make_early_inc_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:596
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1673
llvm::Value::getValueName
ValueName * getValueName() const
Definition: Value.cpp:280
llvm::Value::replaceNonMetadataUsesWith
void replaceNonMetadataUsesWith(Value *V)
Change non-metadata uses of this to point to a new Value.
Definition: Value.cpp:536
llvm::TrackingVH
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:331
llvm::StringMapEntry::getKey
StringRef getKey() const
Definition: StringMapEntry.h:107
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Value::setValueName
void setValueName(ValueName *VN)
Definition: Value.cpp:291
llvm::Value::replaceUsesOutsideBlock
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
Definition: Value.cpp:584
llvm::Value::stripPointerCastsForAliasAnalysis
const Value * stripPointerCastsForAliasAnalysis() const
Strip off pointer casts, all-zero GEPs, single-argument phi nodes and invariant group info.
Definition: Value.cpp:701
llvm::Value::use_begin
use_iterator use_begin()
Definition: Value.h:360
llvm::Type::isVoidTy
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:139
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
llvm::Value::clearMetadata
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1383
llvm::User::isDroppable
bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition: User.cpp:115
llvm::Value::printAsOperand
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:4677
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::ValueHandleBase::ValueIsDeleted
static void ValueIsDeleted(Value *V)
Definition: Value.cpp:1137
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Value::getNumUses
unsigned getNumUses() const
This method computes the number of uses of this Value.
Definition: Value.cpp:254
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
llvm::ms_demangle::IntrinsicFunctionKind::New
@ New
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:994
llvm::Value::use_end
use_iterator use_end()
Definition: Value.h:368
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::Type::isPtrOrPtrVectorTy
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:229
llvm::ifs::IFSSymbolType::Func
@ Func
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
ValueHandle.h
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
llvm::Value::stripInBoundsOffsets
const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition: Value.cpp:777
llvm::Value::HasMetadata
unsigned HasMetadata
Definition: Value.h:119
Constant.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:972
llvm::empty
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:256
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:827
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
isUnDroppableUser
static bool isUnDroppableUser(const User *U)
Definition: Value.cpp:164
llvm::MallocAllocator
Definition: AllocatorBase.h:81
llvm::Value::hasNUndroppableUses
bool hasNUndroppableUses(unsigned N) const
Return true if there this value.
Definition: Value.cpp:190
llvm::Value::~Value
~Value()
Value's destructor should be virtual by design, but that would require that Value and all of its subc...
Definition: Value.cpp:75
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:84
replaceDbgUsesOutsideBlock
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB)
Replace llvm.dbg.
Definition: Value.cpp:573
llvm::APInt::sextOrTrunc
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1002
NodeList::Next
NodeList * Next
Definition: MicrosoftDemangle.cpp:39
llvm::Value::hasNUses
bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition: Value.cpp:148
llvm::BitWidth
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:147
llvm::DenseMapBase::size
unsigned size() const
Definition: DenseMap.h:99
llvm::CallbackVH
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:383
llvm::ValueAsMetadata::handleRAUW
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:436
llvm::Value::canBeFreed
bool canBeFreed() const
Return true if the memory object referred to by V can by freed in the scope for which the SSA value d...
Definition: Value.cpp:781
Instructions.h
llvm::Type::isStructTy
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:217
llvm::ValueSymbolTable
This class provides a symbol table of name/value pairs.
Definition: ValueSymbolTable.h:37
llvm::Value::isUsedByMetadata
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
Definition: Value.h:557
llvm::Value::stripInBoundsConstantOffsets
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
Definition: Value.cpp:697
llvm::Value::DoPHITranslation
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
Definition: Value.cpp:986
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:143
getSymTab
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
Definition: Value.cpp:258
List
const NodeList & List
Definition: RDFGraph.cpp:199
N
#define N
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition: SmallVector.h:659
llvm::Value::deleteValue
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:109
DerivedTypes.h
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
llvm::Twine::toStringRef
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:473
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::Value::replaceUsesWithIf
void replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition: Value.cpp:540
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:381
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:59
llvm::cl::desc
Definition: CommandLine.h:413
raw_ostream.h
Value.h
llvm::raw_string_ostream::str
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:646
TypedPointerType.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::BasicBlock::const_iterator
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:88
llvm::Type::isFirstClassType
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition: Type.h:248
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365