LLVM  15.0.0git
ArgumentPromotion.cpp
Go to the documentation of this file.
1 //===- ArgumentPromotion.cpp - Promote by-reference arguments -------------===//
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 pass promotes "by reference" arguments to be "by value" arguments. In
10 // practice, this means looking for internal functions that have pointer
11 // arguments. If it can prove, through the use of alias analysis, that an
12 // argument is *only* loaded, then it can pass the value into the function
13 // instead of the address of the value. This can cause recursive simplification
14 // of code and lead to the elimination of allocas (especially in C++ template
15 // code like the STL).
16 //
17 // This pass also handles aggregate arguments that are passed into a function,
18 // scalarizing them if the elements of the aggregate are only loaded. Note that
19 // by default it refuses to scalarize aggregates which would require passing in
20 // more than three operands to the function, because passing thousands of
21 // operands for a large array or structure is unprofitable! This limit can be
22 // configured or disabled, however.
23 //
24 // Note that this transformation could also be done for arguments that are only
25 // stored to (returning the value instead), but does not currently. This case
26 // would be best handled when and if LLVM begins supporting multiple return
27 // values from functions.
28 //
29 //===----------------------------------------------------------------------===//
30 
32 
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/ScopeExit.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/Statistic.h"
39 #include "llvm/ADT/Twine.h"
43 #include "llvm/Analysis/Loads.h"
47 #include "llvm/IR/Argument.h"
48 #include "llvm/IR/Attributes.h"
49 #include "llvm/IR/BasicBlock.h"
50 #include "llvm/IR/CFG.h"
51 #include "llvm/IR/Constants.h"
52 #include "llvm/IR/DataLayout.h"
53 #include "llvm/IR/DerivedTypes.h"
54 #include "llvm/IR/Dominators.h"
55 #include "llvm/IR/Function.h"
56 #include "llvm/IR/IRBuilder.h"
57 #include "llvm/IR/InstrTypes.h"
58 #include "llvm/IR/Instruction.h"
59 #include "llvm/IR/Instructions.h"
60 #include "llvm/IR/Metadata.h"
61 #include "llvm/IR/NoFolder.h"
62 #include "llvm/IR/PassManager.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/Use.h"
65 #include "llvm/IR/User.h"
66 #include "llvm/IR/Value.h"
67 #include "llvm/Support/Casting.h"
68 #include "llvm/Support/Debug.h"
71 #include <algorithm>
72 #include <cassert>
73 #include <cstdint>
74 #include <utility>
75 #include <vector>
76 
77 using namespace llvm;
78 
79 #define DEBUG_TYPE "argpromotion"
80 
81 STATISTIC(NumArgumentsPromoted, "Number of pointer arguments promoted");
82 STATISTIC(NumArgumentsDead, "Number of dead pointer args eliminated");
83 
84 namespace {
85 
86 struct ArgPart {
87  Type *Ty;
88  Align Alignment;
89  /// A representative guaranteed-executed load or store instruction for use by
90  /// metadata transfer.
91  Instruction *MustExecInstr;
92 };
93 
94 using OffsetAndArgPart = std::pair<int64_t, ArgPart>;
95 
96 } // end anonymous namespace
97 
99  Value *Ptr, Type *ResElemTy, int64_t Offset) {
100  // For non-opaque pointers, try to create a "nice" GEP if possible, otherwise
101  // fall back to an i8 GEP to a specific offset.
102  unsigned AddrSpace = Ptr->getType()->getPointerAddressSpace();
103  APInt OrigOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
104  if (!Ptr->getType()->isOpaquePointerTy()) {
105  Type *OrigElemTy = Ptr->getType()->getNonOpaquePointerElementType();
106  if (OrigOffset == 0 && OrigElemTy == ResElemTy)
107  return Ptr;
108 
109  if (OrigElemTy->isSized()) {
110  APInt TmpOffset = OrigOffset;
111  Type *TmpTy = OrigElemTy;
112  SmallVector<APInt> IntIndices =
113  DL.getGEPIndicesForOffset(TmpTy, TmpOffset);
114  if (TmpOffset == 0) {
115  // Try to add trailing zero indices to reach the right type.
116  while (TmpTy != ResElemTy) {
117  Type *NextTy = GetElementPtrInst::getTypeAtIndex(TmpTy, (uint64_t)0);
118  if (!NextTy)
119  break;
120 
121  IntIndices.push_back(APInt::getZero(
122  isa<StructType>(TmpTy) ? 32 : OrigOffset.getBitWidth()));
123  TmpTy = NextTy;
124  }
125 
126  SmallVector<Value *> Indices;
127  for (const APInt &Index : IntIndices)
128  Indices.push_back(IRB.getInt(Index));
129 
130  if (OrigOffset != 0 || TmpTy == ResElemTy) {
131  Ptr = IRB.CreateGEP(OrigElemTy, Ptr, Indices);
132  return IRB.CreateBitCast(Ptr, ResElemTy->getPointerTo(AddrSpace));
133  }
134  }
135  }
136  }
137 
138  if (OrigOffset != 0) {
139  Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy(AddrSpace));
140  Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(OrigOffset));
141  }
142  return IRB.CreateBitCast(Ptr, ResElemTy->getPointerTo(AddrSpace));
143 }
144 
145 /// DoPromotion - This method actually performs the promotion of the specified
146 /// arguments, and returns the new function. At this point, we know that it's
147 /// safe to do so.
148 static Function *
151  &ArgsToPromote) {
152  // Start by computing a new prototype for the function, which is the same as
153  // the old function, but has modified arguments.
154  FunctionType *FTy = F->getFunctionType();
155  std::vector<Type *> Params;
156 
157  // Attribute - Keep track of the parameter attributes for the arguments
158  // that we are *not* promoting. For the ones that we do promote, the parameter
159  // attributes are lost
160  SmallVector<AttributeSet, 8> ArgAttrVec;
161  AttributeList PAL = F->getAttributes();
162 
163  // First, determine the new argument list
164  unsigned ArgNo = 0;
165  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
166  ++I, ++ArgNo) {
167  if (!ArgsToPromote.count(&*I)) {
168  // Unchanged argument
169  Params.push_back(I->getType());
170  ArgAttrVec.push_back(PAL.getParamAttrs(ArgNo));
171  } else if (I->use_empty()) {
172  // Dead argument (which are always marked as promotable)
173  ++NumArgumentsDead;
174  } else {
175  const auto &ArgParts = ArgsToPromote.find(&*I)->second;
176  for (const auto &Pair : ArgParts) {
177  Params.push_back(Pair.second.Ty);
178  ArgAttrVec.push_back(AttributeSet());
179  }
180  ++NumArgumentsPromoted;
181  }
182  }
183 
184  Type *RetTy = FTy->getReturnType();
185 
186  // Construct the new function type using the new arguments.
187  FunctionType *NFTy = FunctionType::get(RetTy, Params, FTy->isVarArg());
188 
189  // Create the new function body and insert it into the module.
190  Function *NF = Function::Create(NFTy, F->getLinkage(), F->getAddressSpace(),
191  F->getName());
192  NF->copyAttributesFrom(F);
193  NF->copyMetadata(F, 0);
194 
195  // The new function will have the !dbg metadata copied from the original
196  // function. The original function may not be deleted, and dbg metadata need
197  // to be unique, so we need to drop it.
198  F->setSubprogram(nullptr);
199 
200  LLVM_DEBUG(dbgs() << "ARG PROMOTION: Promoting to:" << *NF << "\n"
201  << "From: " << *F);
202 
203  uint64_t LargestVectorWidth = 0;
204  for (auto *I : Params)
205  if (auto *VT = dyn_cast<llvm::VectorType>(I))
206  LargestVectorWidth = std::max(
207  LargestVectorWidth, VT->getPrimitiveSizeInBits().getKnownMinSize());
208 
209  // Recompute the parameter attributes list based on the new arguments for
210  // the function.
211  NF->setAttributes(AttributeList::get(F->getContext(), PAL.getFnAttrs(),
212  PAL.getRetAttrs(), ArgAttrVec));
213  AttributeFuncs::updateMinLegalVectorWidthAttr(*NF, LargestVectorWidth);
214  ArgAttrVec.clear();
215 
216  F->getParent()->getFunctionList().insert(F->getIterator(), NF);
217  NF->takeName(F);
218 
219  // Loop over all the callers of the function, transforming the call sites to
220  // pass in the loaded pointers.
222  const DataLayout &DL = F->getParent()->getDataLayout();
223  while (!F->use_empty()) {
224  CallBase &CB = cast<CallBase>(*F->user_back());
225  assert(CB.getCalledFunction() == F);
226  const AttributeList &CallPAL = CB.getAttributes();
227  IRBuilder<NoFolder> IRB(&CB);
228 
229  // Loop over the operands, inserting GEP and loads in the caller as
230  // appropriate.
231  auto *AI = CB.arg_begin();
232  ArgNo = 0;
233  for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
234  ++I, ++AI, ++ArgNo) {
235  if (!ArgsToPromote.count(&*I)) {
236  Args.push_back(*AI); // Unmodified argument
237  ArgAttrVec.push_back(CallPAL.getParamAttrs(ArgNo));
238  } else if (!I->use_empty()) {
239  Value *V = *AI;
240  const auto &ArgParts = ArgsToPromote.find(&*I)->second;
241  for (const auto &Pair : ArgParts) {
242  LoadInst *LI = IRB.CreateAlignedLoad(
243  Pair.second.Ty,
244  createByteGEP(IRB, DL, V, Pair.second.Ty, Pair.first),
245  Pair.second.Alignment, V->getName() + ".val");
246  if (Pair.second.MustExecInstr) {
247  LI->setAAMetadata(Pair.second.MustExecInstr->getAAMetadata());
248  LI->copyMetadata(*Pair.second.MustExecInstr,
249  {LLVMContext::MD_range, LLVMContext::MD_nonnull,
250  LLVMContext::MD_dereferenceable,
251  LLVMContext::MD_dereferenceable_or_null,
252  LLVMContext::MD_align, LLVMContext::MD_noundef});
253  }
254  Args.push_back(LI);
255  ArgAttrVec.push_back(AttributeSet());
256  }
257  }
258  }
259 
260  // Push any varargs arguments on the list.
261  for (; AI != CB.arg_end(); ++AI, ++ArgNo) {
262  Args.push_back(*AI);
263  ArgAttrVec.push_back(CallPAL.getParamAttrs(ArgNo));
264  }
265 
267  CB.getOperandBundlesAsDefs(OpBundles);
268 
269  CallBase *NewCS = nullptr;
270  if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {
271  NewCS = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
272  Args, OpBundles, "", &CB);
273  } else {
274  auto *NewCall = CallInst::Create(NF, Args, OpBundles, "", &CB);
275  NewCall->setTailCallKind(cast<CallInst>(&CB)->getTailCallKind());
276  NewCS = NewCall;
277  }
278  NewCS->setCallingConv(CB.getCallingConv());
279  NewCS->setAttributes(AttributeList::get(F->getContext(),
280  CallPAL.getFnAttrs(),
281  CallPAL.getRetAttrs(), ArgAttrVec));
282  NewCS->copyMetadata(CB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
283  Args.clear();
284  ArgAttrVec.clear();
285 
287  LargestVectorWidth);
288 
289  if (!CB.use_empty()) {
290  CB.replaceAllUsesWith(NewCS);
291  NewCS->takeName(&CB);
292  }
293 
294  // Finally, remove the old call from the program, reducing the use-count of
295  // F.
296  CB.eraseFromParent();
297  }
298 
299  // Since we have now created the new function, splice the body of the old
300  // function right into the new function, leaving the old rotting hulk of the
301  // function empty.
302  NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
303 
304  // We will collect all the new created allocas to promote them into registers
305  // after the following loop
307 
308  // Loop over the argument list, transferring uses of the old arguments over to
309  // the new arguments, also transferring over the names as well.
311  for (Argument &Arg : F->args()) {
312  if (!ArgsToPromote.count(&Arg)) {
313  // If this is an unmodified argument, move the name and users over to the
314  // new version.
315  Arg.replaceAllUsesWith(&*I2);
316  I2->takeName(&Arg);
317  ++I2;
318  continue;
319  }
320 
321  // There potentially are metadata uses for things like llvm.dbg.value.
322  // Replace them with undef, after handling the other regular uses.
323  auto RauwUndefMetadata = make_scope_exit(
324  [&]() { Arg.replaceAllUsesWith(UndefValue::get(Arg.getType())); });
325 
326  if (Arg.use_empty())
327  continue;
328 
329  // Otherwise, if we promoted this argument, we have to create an alloca in
330  // the callee for every promotable part and store each of the new incoming
331  // arguments into the corresponding alloca, what lets the old code (the
332  // store instructions if they are allowed especially) a chance to work as
333  // before.
334  assert(Arg.getType()->isPointerTy() &&
335  "Only arguments with a pointer type are promotable");
336 
337  IRBuilder<NoFolder> IRB(&NF->begin()->front());
338 
339  // Add only the promoted elements, so parts from ArgsToPromote
341  for (const auto &Pair : ArgsToPromote.find(&Arg)->second) {
342  int64_t Offset = Pair.first;
343  const ArgPart &Part = Pair.second;
344 
345  Argument *NewArg = I2++;
346  NewArg->setName(Arg.getName() + "." + Twine(Offset) + ".val");
347 
348  AllocaInst *NewAlloca = IRB.CreateAlloca(
349  Part.Ty, nullptr, Arg.getName() + "." + Twine(Offset) + ".allc");
350  NewAlloca->setAlignment(Pair.second.Alignment);
351  IRB.CreateAlignedStore(NewArg, NewAlloca, Pair.second.Alignment);
352 
353  // Collect the alloca to retarget the users to
354  OffsetToAlloca.insert({Offset, NewAlloca});
355  }
356 
357  auto GetAlloca = [&](Value *Ptr) {
358  APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
359  Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
360  /* AllowNonInbounds */ true);
361  assert(Ptr == &Arg && "Not constant offset from arg?");
362  return OffsetToAlloca.lookup(Offset.getSExtValue());
363  };
364 
365  // Cleanup the code from the dead instructions: GEPs and BitCasts in between
366  // the original argument and its users: loads and stores. Retarget every
367  // user to the new created alloca.
368  SmallVector<Value *, 16> Worklist;
370  append_range(Worklist, Arg.users());
371  while (!Worklist.empty()) {
372  Value *V = Worklist.pop_back_val();
373  if (isa<BitCastInst>(V) || isa<GetElementPtrInst>(V)) {
374  DeadInsts.push_back(cast<Instruction>(V));
375  append_range(Worklist, V->users());
376  continue;
377  }
378 
379  if (auto *LI = dyn_cast<LoadInst>(V)) {
380  Value *Ptr = LI->getPointerOperand();
381  LI->setOperand(LoadInst::getPointerOperandIndex(), GetAlloca(Ptr));
382  continue;
383  }
384 
385  if (auto *SI = dyn_cast<StoreInst>(V)) {
386  assert(!SI->isVolatile() && "Volatile operations can't be promoted.");
387  Value *Ptr = SI->getPointerOperand();
388  SI->setOperand(StoreInst::getPointerOperandIndex(), GetAlloca(Ptr));
389  continue;
390  }
391 
392  llvm_unreachable("Unexpected user");
393  }
394 
395  for (Instruction *I : DeadInsts) {
396  I->replaceAllUsesWith(PoisonValue::get(I->getType()));
397  I->eraseFromParent();
398  }
399 
400  // Collect the allocas for promotion
401  for (const auto &Pair : OffsetToAlloca) {
402  assert(isAllocaPromotable(Pair.second) &&
403  "By design, only promotable allocas should be produced.");
404  Allocas.push_back(Pair.second);
405  }
406  }
407 
408  LLVM_DEBUG(dbgs() << "ARG PROMOTION: " << Allocas.size()
409  << " alloca(s) are promotable by Mem2Reg\n");
410 
411  if (!Allocas.empty()) {
412  // And we are able to call the `promoteMemoryToRegister()` function.
413  // Our earlier checks have ensured that PromoteMemToReg() will
414  // succeed.
415  auto &DT = FAM.getResult<DominatorTreeAnalysis>(*NF);
416  auto &AC = FAM.getResult<AssumptionAnalysis>(*NF);
417  PromoteMemToReg(Allocas, DT, &AC);
418  }
419 
420  return NF;
421 }
422 
423 /// Return true if we can prove that all callees pass in a valid pointer for the
424 /// specified function argument.
426  Align NeededAlign,
427  uint64_t NeededDerefBytes) {
428  Function *Callee = Arg->getParent();
429  const DataLayout &DL = Callee->getParent()->getDataLayout();
430  APInt Bytes(64, NeededDerefBytes);
431 
432  // Check if the argument itself is marked dereferenceable and aligned.
433  if (isDereferenceableAndAlignedPointer(Arg, NeededAlign, Bytes, DL))
434  return true;
435 
436  // Look at all call sites of the function. At this point we know we only have
437  // direct callees.
438  return all_of(Callee->users(), [&](User *U) {
439  CallBase &CB = cast<CallBase>(*U);
440  return isDereferenceableAndAlignedPointer(CB.getArgOperand(Arg->getArgNo()),
441  NeededAlign, Bytes, DL);
442  });
443 }
444 
445 /// Determine that this argument is safe to promote, and find the argument
446 /// parts it can be promoted into.
447 static bool findArgParts(Argument *Arg, const DataLayout &DL, AAResults &AAR,
448  unsigned MaxElements, bool IsRecursive,
449  SmallVectorImpl<OffsetAndArgPart> &ArgPartsVec) {
450  // Quick exit for unused arguments
451  if (Arg->use_empty())
452  return true;
453 
454  // We can only promote this argument if all the uses are loads at known
455  // offsets.
456  //
457  // Promoting the argument causes it to be loaded in the caller
458  // unconditionally. This is only safe if we can prove that either the load
459  // would have happened in the callee anyway (ie, there is a load in the entry
460  // block) or the pointer passed in at every call site is guaranteed to be
461  // valid.
462  // In the former case, invalid loads can happen, but would have happened
463  // anyway, in the latter case, invalid loads won't happen. This prevents us
464  // from introducing an invalid load that wouldn't have happened in the
465  // original code.
466 
468  Align NeededAlign(1);
469  uint64_t NeededDerefBytes = 0;
470 
471  // And if this is a byval argument we also allow to have store instructions.
472  // Only handle in such way arguments with specified alignment;
473  // if it's unspecified, the actual alignment of the argument is
474  // target-specific.
475  bool AreStoresAllowed = Arg->getParamByValType() && Arg->getParamAlign();
476 
477  // An end user of a pointer argument is a load or store instruction.
478  // Returns None if this load or store is not based on the argument. Return
479  // true if we can promote the instruction, false otherwise.
480  auto HandleEndUser = [&](auto *I, Type *Ty,
481  bool GuaranteedToExecute) -> Optional<bool> {
482  // Don't promote volatile or atomic instructions.
483  if (!I->isSimple())
484  return false;
485 
486  Value *Ptr = I->getPointerOperand();
487  APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
488  Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
489  /* AllowNonInbounds */ true);
490  if (Ptr != Arg)
491  return None;
492 
493  if (Offset.getSignificantBits() >= 64)
494  return false;
495 
496  TypeSize Size = DL.getTypeStoreSize(Ty);
497  // Don't try to promote scalable types.
498  if (Size.isScalable())
499  return false;
500 
501  // If this is a recursive function and one of the types is a pointer,
502  // then promoting it might lead to recursive promotion.
503  if (IsRecursive && Ty->isPointerTy())
504  return false;
505 
506  int64_t Off = Offset.getSExtValue();
507  auto Pair = ArgParts.try_emplace(
508  Off, ArgPart{Ty, I->getAlign(), GuaranteedToExecute ? I : nullptr});
509  ArgPart &Part = Pair.first->second;
510  bool OffsetNotSeenBefore = Pair.second;
511 
512  // We limit promotion to only promoting up to a fixed number of elements of
513  // the aggregate.
514  if (MaxElements > 0 && ArgParts.size() > MaxElements) {
515  LLVM_DEBUG(dbgs() << "ArgPromotion of " << *Arg << " failed: "
516  << "more than " << MaxElements << " parts\n");
517  return false;
518  }
519 
520  // For now, we only support loading/storing one specific type at a given
521  // offset.
522  if (Part.Ty != Ty) {
523  LLVM_DEBUG(dbgs() << "ArgPromotion of " << *Arg << " failed: "
524  << "accessed as both " << *Part.Ty << " and " << *Ty
525  << " at offset " << Off << "\n");
526  return false;
527  }
528 
529  // If this instruction is not guaranteed to execute, and we haven't seen a
530  // load or store at this offset before (or it had lower alignment), then we
531  // need to remember that requirement.
532  // Note that skipping instructions of previously seen offsets is only
533  // correct because we only allow a single type for a given offset, which
534  // also means that the number of accessed bytes will be the same.
535  if (!GuaranteedToExecute &&
536  (OffsetNotSeenBefore || Part.Alignment < I->getAlign())) {
537  // We won't be able to prove dereferenceability for negative offsets.
538  if (Off < 0)
539  return false;
540 
541  // If the offset is not aligned, an aligned base pointer won't help.
542  if (!isAligned(I->getAlign(), Off))
543  return false;
544 
545  NeededDerefBytes = std::max(NeededDerefBytes, Off + Size.getFixedValue());
546  NeededAlign = std::max(NeededAlign, I->getAlign());
547  }
548 
549  Part.Alignment = std::max(Part.Alignment, I->getAlign());
550  return true;
551  };
552 
553  // Look for loads and stores that are guaranteed to execute on entry.
554  for (Instruction &I : Arg->getParent()->getEntryBlock()) {
555  Optional<bool> Res{};
556  if (LoadInst *LI = dyn_cast<LoadInst>(&I))
557  Res = HandleEndUser(LI, LI->getType(), /* GuaranteedToExecute */ true);
558  else if (StoreInst *SI = dyn_cast<StoreInst>(&I))
559  Res = HandleEndUser(SI, SI->getValueOperand()->getType(),
560  /* GuaranteedToExecute */ true);
561  if (Res && !*Res)
562  return false;
563 
565  break;
566  }
567 
568  // Now look at all loads of the argument. Remember the load instructions
569  // for the aliasing check below.
573  auto AppendUses = [&](const Value *V) {
574  for (const Use &U : V->uses())
575  if (Visited.insert(&U).second)
576  Worklist.push_back(&U);
577  };
578  AppendUses(Arg);
579  while (!Worklist.empty()) {
580  const Use *U = Worklist.pop_back_val();
581  Value *V = U->getUser();
582  if (isa<BitCastInst>(V)) {
583  AppendUses(V);
584  continue;
585  }
586 
587  if (auto *GEP = dyn_cast<GetElementPtrInst>(V)) {
588  if (!GEP->hasAllConstantIndices())
589  return false;
590  AppendUses(V);
591  continue;
592  }
593 
594  if (auto *LI = dyn_cast<LoadInst>(V)) {
595  if (!*HandleEndUser(LI, LI->getType(), /* GuaranteedToExecute */ false))
596  return false;
597  Loads.push_back(LI);
598  continue;
599  }
600 
601  // Stores are allowed for byval arguments
602  auto *SI = dyn_cast<StoreInst>(V);
603  if (AreStoresAllowed && SI &&
604  U->getOperandNo() == StoreInst::getPointerOperandIndex()) {
605  if (!*HandleEndUser(SI, SI->getValueOperand()->getType(),
606  /* GuaranteedToExecute */ false))
607  return false;
608  continue;
609  // Only stores TO the argument is allowed, all the other stores are
610  // unknown users
611  }
612 
613  // Unknown user.
614  LLVM_DEBUG(dbgs() << "ArgPromotion of " << *Arg << " failed: "
615  << "unknown user " << *V << "\n");
616  return false;
617  }
618 
619  if (NeededDerefBytes || NeededAlign > 1) {
620  // Try to prove a required deref / aligned requirement.
621  if (!allCallersPassValidPointerForArgument(Arg, NeededAlign,
622  NeededDerefBytes)) {
623  LLVM_DEBUG(dbgs() << "ArgPromotion of " << *Arg << " failed: "
624  << "not dereferenceable or aligned\n");
625  return false;
626  }
627  }
628 
629  if (ArgParts.empty())
630  return true; // No users, this is a dead argument.
631 
632  // Sort parts by offset.
633  append_range(ArgPartsVec, ArgParts);
634  sort(ArgPartsVec,
635  [](const auto &A, const auto &B) { return A.first < B.first; });
636 
637  // Make sure the parts are non-overlapping.
638  int64_t Offset = ArgPartsVec[0].first;
639  for (const auto &Pair : ArgPartsVec) {
640  if (Pair.first < Offset)
641  return false; // Overlap with previous part.
642 
643  Offset = Pair.first + DL.getTypeStoreSize(Pair.second.Ty);
644  }
645 
646  // If store instructions are allowed, the path from the entry of the function
647  // to each load may be not free of instructions that potentially invalidate
648  // the load, and this is an admissible situation.
649  if (AreStoresAllowed)
650  return true;
651 
652  // Okay, now we know that the argument is only used by load instructions, and
653  // it is safe to unconditionally perform all of them. Use alias analysis to
654  // check to see if the pointer is guaranteed to not be modified from entry of
655  // the function to each of the load instructions.
656 
657  // Because there could be several/many load instructions, remember which
658  // blocks we know to be transparent to the load.
660 
661  for (LoadInst *Load : Loads) {
662  // Check to see if the load is invalidated from the start of the block to
663  // the load itself.
664  BasicBlock *BB = Load->getParent();
665 
667  if (AAR.canInstructionRangeModRef(BB->front(), *Load, Loc, ModRefInfo::Mod))
668  return false; // Pointer is invalidated!
669 
670  // Now check every path from the entry block to the load for transparency.
671  // To do this, we perform a depth first search on the inverse CFG from the
672  // loading block.
673  for (BasicBlock *P : predecessors(BB)) {
674  for (BasicBlock *TranspBB : inverse_depth_first_ext(P, TranspBlocks))
675  if (AAR.canBasicBlockModify(*TranspBB, Loc))
676  return false;
677  }
678  }
679 
680  // If the path from the entry of the function to each load is free of
681  // instructions that potentially invalidate the load, we can make the
682  // transformation!
683  return true;
684 }
685 
686 /// Check if callers and callee agree on how promoted arguments would be
687 /// passed.
689  const TargetTransformInfo &TTI) {
690  return all_of(F.uses(), [&](const Use &U) {
691  CallBase *CB = dyn_cast<CallBase>(U.getUser());
692  if (!CB)
693  return false;
694 
695  const Function *Caller = CB->getCaller();
696  const Function *Callee = CB->getCalledFunction();
697  return TTI.areTypesABICompatible(Caller, Callee, Types);
698  });
699 }
700 
701 /// PromoteArguments - This method checks the specified function to see if there
702 /// are any promotable arguments and if it is safe to promote the function (for
703 /// example, all callers are direct). If safe to promote some arguments, it
704 /// calls the DoPromotion method.
706  unsigned MaxElements, bool IsRecursive) {
707  // Don't perform argument promotion for naked functions; otherwise we can end
708  // up removing parameters that are seemingly 'not used' as they are referred
709  // to in the assembly.
710  if (F->hasFnAttribute(Attribute::Naked))
711  return nullptr;
712 
713  // Make sure that it is local to this module.
714  if (!F->hasLocalLinkage())
715  return nullptr;
716 
717  // Don't promote arguments for variadic functions. Adding, removing, or
718  // changing non-pack parameters can change the classification of pack
719  // parameters. Frontends encode that classification at the call site in the
720  // IR, while in the callee the classification is determined dynamically based
721  // on the number of registers consumed so far.
722  if (F->isVarArg())
723  return nullptr;
724 
725  // Don't transform functions that receive inallocas, as the transformation may
726  // not be safe depending on calling convention.
727  if (F->getAttributes().hasAttrSomewhere(Attribute::InAlloca))
728  return nullptr;
729 
730  // First check: see if there are any pointer arguments! If not, quick exit.
731  SmallVector<Argument *, 16> PointerArgs;
732  for (Argument &I : F->args())
733  if (I.getType()->isPointerTy())
734  PointerArgs.push_back(&I);
735  if (PointerArgs.empty())
736  return nullptr;
737 
738  // Second check: make sure that all callers are direct callers. We can't
739  // transform functions that have indirect callers. Also see if the function
740  // is self-recursive.
741  for (Use &U : F->uses()) {
742  CallBase *CB = dyn_cast<CallBase>(U.getUser());
743  // Must be a direct call.
744  if (CB == nullptr || !CB->isCallee(&U) ||
745  CB->getFunctionType() != F->getFunctionType())
746  return nullptr;
747 
748  // Can't change signature of musttail callee
749  if (CB->isMustTailCall())
750  return nullptr;
751 
752  if (CB->getFunction() == F)
753  IsRecursive = true;
754  }
755 
756  // Can't change signature of musttail caller
757  // FIXME: Support promoting whole chain of musttail functions
758  for (BasicBlock &BB : *F)
759  if (BB.getTerminatingMustTailCall())
760  return nullptr;
761 
762  const DataLayout &DL = F->getParent()->getDataLayout();
763  auto &AAR = FAM.getResult<AAManager>(*F);
764  const auto &TTI = FAM.getResult<TargetIRAnalysis>(*F);
765 
766  // Check to see which arguments are promotable. If an argument is promotable,
767  // add it to ArgsToPromote.
769  for (Argument *PtrArg : PointerArgs) {
770  // Replace sret attribute with noalias. This reduces register pressure by
771  // avoiding a register copy.
772  if (PtrArg->hasStructRetAttr()) {
773  unsigned ArgNo = PtrArg->getArgNo();
774  F->removeParamAttr(ArgNo, Attribute::StructRet);
775  F->addParamAttr(ArgNo, Attribute::NoAlias);
776  for (Use &U : F->uses()) {
777  CallBase &CB = cast<CallBase>(*U.getUser());
778  CB.removeParamAttr(ArgNo, Attribute::StructRet);
779  CB.addParamAttr(ArgNo, Attribute::NoAlias);
780  }
781  }
782 
783  // If we can promote the pointer to its value.
785 
786  if (findArgParts(PtrArg, DL, AAR, MaxElements, IsRecursive, ArgParts)) {
788  for (const auto &Pair : ArgParts)
789  Types.push_back(Pair.second.Ty);
790 
791  if (areTypesABICompatible(Types, *F, TTI)) {
792  ArgsToPromote.insert({PtrArg, std::move(ArgParts)});
793  }
794  }
795  }
796 
797  // No promotable pointer arguments.
798  if (ArgsToPromote.empty())
799  return nullptr;
800 
801  return doPromotion(F, FAM, ArgsToPromote);
802 }
803 
806  LazyCallGraph &CG,
807  CGSCCUpdateResult &UR) {
808  bool Changed = false, LocalChange;
809 
810  // Iterate until we stop promoting from this SCC.
811  do {
812  LocalChange = false;
813 
815  AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
816 
817  bool IsRecursive = C.size() > 1;
818  for (LazyCallGraph::Node &N : C) {
819  Function &OldF = N.getFunction();
820  Function *NewF = promoteArguments(&OldF, FAM, MaxElements, IsRecursive);
821  if (!NewF)
822  continue;
823  LocalChange = true;
824 
825  // Directly substitute the functions in the call graph. Note that this
826  // requires the old function to be completely dead and completely
827  // replaced by the new function. It does no call graph updates, it merely
828  // swaps out the particular function mapped to a particular node in the
829  // graph.
830  C.getOuterRefSCC().replaceNodeFunction(N, *NewF);
831  FAM.clear(OldF, OldF.getName());
832  OldF.eraseFromParent();
833 
834  PreservedAnalyses FuncPA;
835  FuncPA.preserveSet<CFGAnalyses>();
836  for (auto *U : NewF->users()) {
837  auto *UserF = cast<CallBase>(U)->getFunction();
838  FAM.invalidate(*UserF, FuncPA);
839  }
840  }
841 
842  Changed |= LocalChange;
843  } while (LocalChange);
844 
845  if (!Changed)
846  return PreservedAnalyses::all();
847 
849  // We've cleared out analyses for deleted functions.
851  // We've manually invalidated analyses for functions we've modified.
853  return PA;
854 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
AssumptionCache.h
llvm::AAManager
A manager for alias analyses.
Definition: AliasAnalysis.h:1303
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:264
llvm::TargetIRAnalysis
Analysis pass providing the TargetTransformInfo.
Definition: TargetTransformInfo.h:2485
llvm::isAligned
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition: Alignment.h:146
llvm::MemoryLocation::get
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Definition: MemoryLocation.cpp:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::CallBase::getOperandBundlesAsDefs
void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const
Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.
Definition: Instructions.cpp:396
llvm::InvokeInst::Create
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, Instruction *InsertBefore=nullptr)
Definition: Instructions.h:3817
NoFolder.h
llvm::IRBuilderBase::CreateAlignedStore
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition: IRBuilder.h:1685
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
Metadata.h
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:780
llvm::Function::getBasicBlockList
const BasicBlockListType & getBasicBlockList() const
Get the underlying elements of the Function...
Definition: Function.h:703
Loads.h
llvm::Function
Definition: Function.h:60
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:199
llvm::AnalysisManager::invalidate
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
Definition: PassManagerImpl.h:89
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::Function::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Function.cpp:365
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
Statistic.h
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:168
llvm::Type::getPointerAddressSpace
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:729
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2485
llvm::AllAnalysesOn
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: PassManager.h:90
llvm::SmallDenseMap
Definition: DenseMap.h:882
llvm::FunctionType::get
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:361
ValueTracking.h
llvm::AttributeList::get
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute >> Attrs)
Create an AttributeList with the specified parameters in it.
Definition: Attributes.cpp:1050
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1411
promoteArguments
static Function * promoteArguments(Function *F, FunctionAnalysisManager &FAM, unsigned MaxElements, bool IsRecursive)
PromoteArguments - This method checks the specified function to see if there are any promotable argum...
Definition: ArgumentPromotion.cpp:705
llvm::AttributeList
Definition: Attributes.h:425
llvm::CallBase::getAttributes
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1474
llvm::LoadInst::getPointerOperandIndex
static unsigned getPointerOperandIndex()
Definition: Instructions.h:262
llvm::CallBase::getFunctionType
FunctionType * getFunctionType() const
Definition: InstrTypes.h:1254
llvm::CallBase::isCallee
bool isCallee(Value::const_user_iterator UI) const
Determine whether the passed iterator points to the callee operand's Use.
Definition: InstrTypes.h:1407
llvm::AttributeList::getFnAttrs
AttributeSet getFnAttrs() const
The function attributes are returned.
Definition: Attributes.cpp:1388
llvm::CallBase::isMustTailCall
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Definition: Instructions.cpp:298
llvm::Optional< bool >
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::PromoteMemToReg
void PromoteMemToReg(ArrayRef< AllocaInst * > Allocas, DominatorTree &DT, AssumptionCache *AC=nullptr)
Promote the specified list of alloca instructions into scalar registers, inserting PHI nodes as appro...
Definition: PromoteMemoryToRegister.cpp:1013
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::Instruction::copyMetadata
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Definition: Instruction.cpp:841
STLExtras.h
llvm::CallBase::arg_begin
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1316
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:654
llvm::IRBuilderBase::CreateAlignedLoad
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1666
llvm::CallBase::addParamAttr
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
Definition: InstrTypes.h:1526
BasicAliasAnalysis.h
llvm::Type::getNonOpaquePointerElementType
Type * getNonOpaquePointerElementType() const
Only use this method in code that is not reachable with opaque pointers, or part of deprecated method...
Definition: Type.h:382
llvm::IRBuilderBase::CreateAlloca
AllocaInst * CreateAlloca(Type *Ty, unsigned AddrSpace, Value *ArraySize=nullptr, const Twine &Name="")
Definition: IRBuilder.h:1632
Use.h
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:177
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
DepthFirstIterator.h
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::ArgumentPromotionPass::run
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
Definition: ArgumentPromotion.cpp:804
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
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:186
Instruction.h
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::CallBase::removeParamAttr
void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Removes the attribute from the given argument.
Definition: InstrTypes.h:1568
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1617
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:419
Constants.h
llvm::AAResults
Definition: AliasAnalysis.h:511
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::IRBuilderBase::CreateGEP
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", bool IsInBounds=false)
Definition: IRBuilder.h:1725
llvm::User
Definition: User.h:44
ArgumentPromotion.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
doPromotion
static Function * doPromotion(Function *F, FunctionAnalysisManager &FAM, const DenseMap< Argument *, SmallVector< OffsetAndArgPart, 4 >> &ArgsToPromote)
DoPromotion - This method actually performs the promotion of the specified arguments,...
Definition: ArgumentPromotion.cpp:149
Twine.h
InstrTypes.h
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1396
llvm::CallBase::setAttributes
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1478
llvm::AnalysisManager::clear
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
Definition: PassManagerImpl.h:36
llvm::CallInst::Create
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:1514
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition: Value.h:376
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::make_scope_exit
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
llvm::Instruction
Definition: Instruction.h:42
llvm::IRBuilderBase::getInt8Ty
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:491
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::predecessors
auto predecessors(MachineBasicBlock *BB)
Definition: MachineSSAContext.h:30
llvm::Function::copyAttributesFrom
void copyAttributesFrom(const Function *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a Function) from the ...
Definition: Function.cpp:712
llvm::Value::setName
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:372
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1710
SmallPtrSet.h
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::CallBase::getCallingConv
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1455
llvm::None
const NoneType None
Definition: None.h:24
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:344
Type.h
llvm::CallBase::getCaller
Function * getCaller()
Helper to get the caller (the parent function).
Definition: Instructions.cpp:282
CFG.h
llvm::InvokeInst
Invoke instruction.
Definition: Instructions.h:3773
BasicBlock.h
llvm::IRBuilderBase::CreateBitCast
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1952
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:297
llvm::AttributeList::getRetAttrs
AttributeSet getRetAttrs() const
The attributes for the ret value are returned.
Definition: Attributes.cpp:1384
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:77
uint64_t
llvm::AssumptionAnalysis
A function analysis which provides an AssumptionCache.
Definition: AssumptionCache.h:173
llvm::PreservedAnalyses::preserve
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:173
llvm::GlobalObject::copyMetadata
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1564
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
PromoteMemToReg.h
MemoryLocation.h
llvm::DenseMap
Definition: DenseMap.h:716
llvm::AAResults::canInstructionRangeModRef
bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2, const MemoryLocation &Loc, const ModRefInfo Mode)
Check if it is possible for the execution of the specified instructions to mod(according to the mode)...
Definition: AliasAnalysis.cpp:789
I
#define I(x, y, z)
Definition: MD5.cpp:58
createByteGEP
static Value * createByteGEP(IRBuilderBase &IRB, const DataLayout &DL, Value *Ptr, Type *ResElemTy, int64_t Offset)
Definition: ArgumentPromotion.cpp:98
llvm::IRBuilderBase::getInt8PtrTy
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:549
llvm::IRBuilderBase::getInt
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:477
llvm::Function::Create
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:137
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI
StandardInstrumentations SI(Debug, VerifyEach)
areTypesABICompatible
static bool areTypesABICompatible(ArrayRef< Type * > Types, const Function &F, const TargetTransformInfo &TTI)
Check if callers and callee agree on how promoted arguments would be passed.
Definition: ArgumentPromotion.cpp:688
llvm::CallBase::arg_end
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1322
llvm::LazyCallGraph::Node
A node in the call graph.
Definition: LazyCallGraph.h:315
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::df_iterator_default_set
Definition: DepthFirstIterator.h:70
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
DataLayout.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition: PassManager.h:113
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:69
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:529
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1823
llvm::ModRefInfo::Mod
@ Mod
The access may modify the value stored in memory.
llvm::IRBuilderBase
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:93
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
findArgParts
static bool findArgParts(Argument *Arg, const DataLayout &DL, AAResults &AAR, unsigned MaxElements, bool IsRecursive, SmallVectorImpl< OffsetAndArgPart > &ArgPartsVec)
Determine that this argument is safe to promote, and find the argument parts it can be promoted into.
Definition: ArgumentPromotion.cpp:447
llvm::AttributeFuncs::updateMinLegalVectorWidthAttr
void updateMinLegalVectorWidthAttr(Function &Fn, uint64_t Width)
Update min-legal-vector-width if it is in Attribute and less than Width.
Definition: Attributes.cpp:2068
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:305
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:173
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:209
llvm::Function::begin
iterator begin()
Definition: Function.h:726
Argument.h
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::empty
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:98
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:186
Attributes.h
llvm::CGSCCUpdateResult
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
Definition: CGSCCPassManager.h:232
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::isGuaranteedToTransferExecutionToSuccessor
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
Definition: ValueTracking.cpp:5420
llvm::TypeSize
Definition: TypeSize.h:435
Casting.h
llvm::Instruction::setAAMetadata
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1477
llvm::GetElementPtrInst::getTypeAtIndex
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
Definition: Instructions.cpp:1754
Function.h
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::size
unsigned size() const
Definition: DenseMap.h:101
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1562
PassManager.h
llvm::Type::getPointerTo
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
Definition: Type.cpp:774
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:591
llvm::Function::arg_begin
arg_iterator arg_begin()
Definition: Function.h:741
llvm::AllocaInst::setAlignment
void setAlignment(Align Align)
Definition: Instructions.h:125
CallGraph.h
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:267
llvm::Type::isOpaquePointerTy
bool isOpaquePointerTy() const
True if this is an instance of an opaque PointerType.
Definition: Type.cpp:61
Instructions.h
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:188
llvm::AttributeSet
Definition: Attributes.h:290
SmallVector.h
User.h
Dominators.h
N
#define N
llvm::DenseMapBase< SmallDenseMap< KeyT, ValueT, 4, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:224
TargetTransformInfo.h
llvm::AAResults::canBasicBlockModify
bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc)
Check if it is possible for execution of the specified basic block to modify the location Loc.
Definition: AliasAnalysis.cpp:779
ScopeExit.h
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
DerivedTypes.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
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::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:378
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:58
llvm::inverse_depth_first_ext
iterator_range< idf_ext_iterator< T, SetTy > > inverse_depth_first_ext(const T &G, SetTy &S)
Definition: DepthFirstIterator.h:303
llvm::LazyCallGraph
A lazily constructed view of the call graph of a module.
Definition: LazyCallGraph.h:110
raw_ostream.h
llvm::StoreInst::getPointerOperandIndex
static unsigned getPointerOperandIndex()
Definition: Instructions.h:391
llvm::AttributeList::getParamAttrs
AttributeSet getParamAttrs(unsigned ArgNo) const
The attributes for the argument or parameter at the given index are returned.
Definition: Attributes.cpp:1380
llvm::FunctionAnalysisManagerCGSCCProxy
A proxy from a FunctionAnalysisManager to an SCC.
Definition: CGSCCPassManager.h:392
allCallersPassValidPointerForArgument
static bool allCallersPassValidPointerForArgument(Argument *Arg, Align NeededAlign, uint64_t NeededDerefBytes)
Return true if we can prove that all callees pass in a valid pointer for the specified function argum...
Definition: ArgumentPromotion.cpp:425
Value.h
llvm::Function::setAttributes
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:317
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:124
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::isAllocaPromotable
bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
Definition: PromoteMemoryToRegister.cpp:63
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:210
llvm::CallBase::setCallingConv
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1459
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
llvm::isDereferenceableAndAlignedPointer
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:205
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
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1729