LLVM  14.0.0git
GlobalOpt.cpp
Go to the documentation of this file.
1 //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
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 transforms simple global variables that never have their address
10 // taken. If obviously true, it marks read/write globals as constant, deletes
11 // variables only stored to, etc.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/Twine.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/BasicBlock.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Constant.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/DerivedTypes.h"
38 #include "llvm/IR/Dominators.h"
39 #include "llvm/IR/Function.h"
41 #include "llvm/IR/GlobalAlias.h"
42 #include "llvm/IR/GlobalValue.h"
43 #include "llvm/IR/GlobalVariable.h"
44 #include "llvm/IR/IRBuilder.h"
45 #include "llvm/IR/InstrTypes.h"
46 #include "llvm/IR/Instruction.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/IntrinsicInst.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/IR/Operator.h"
51 #include "llvm/IR/Type.h"
52 #include "llvm/IR/Use.h"
53 #include "llvm/IR/User.h"
54 #include "llvm/IR/Value.h"
55 #include "llvm/IR/ValueHandle.h"
56 #include "llvm/InitializePasses.h"
57 #include "llvm/Pass.h"
59 #include "llvm/Support/Casting.h"
61 #include "llvm/Support/Debug.h"
65 #include "llvm/Transforms/IPO.h"
70 #include <cassert>
71 #include <cstdint>
72 #include <utility>
73 #include <vector>
74 
75 using namespace llvm;
76 
77 #define DEBUG_TYPE "globalopt"
78 
79 STATISTIC(NumMarked , "Number of globals marked constant");
80 STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
81 STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
82 STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
83 STATISTIC(NumDeleted , "Number of globals deleted");
84 STATISTIC(NumGlobUses , "Number of global uses devirtualized");
85 STATISTIC(NumLocalized , "Number of globals localized");
86 STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
87 STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
88 STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
89 STATISTIC(NumNestRemoved , "Number of nest attributes removed");
90 STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
91 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
92 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
93 STATISTIC(NumInternalFunc, "Number of internal functions");
94 STATISTIC(NumColdCC, "Number of functions marked coldcc");
95 
96 static cl::opt<bool>
97  EnableColdCCStressTest("enable-coldcc-stress-test",
98  cl::desc("Enable stress test of coldcc by adding "
99  "calling conv to all internal functions."),
100  cl::init(false), cl::Hidden);
101 
103  "coldcc-rel-freq", cl::Hidden, cl::init(2), cl::ZeroOrMore,
104  cl::desc(
105  "Maximum block frequency, expressed as a percentage of caller's "
106  "entry frequency, for a call site to be considered cold for enabling"
107  "coldcc"));
108 
109 /// Is this global variable possibly used by a leak checker as a root? If so,
110 /// we might not really want to eliminate the stores to it.
112  // A global variable is a root if it is a pointer, or could plausibly contain
113  // a pointer. There are two challenges; one is that we could have a struct
114  // the has an inner member which is a pointer. We recurse through the type to
115  // detect these (up to a point). The other is that we may actually be a union
116  // of a pointer and another type, and so our LLVM type is an integer which
117  // gets converted into a pointer, or our type is an [i8 x #] with a pointer
118  // potentially contained here.
119 
120  if (GV->hasPrivateLinkage())
121  return false;
122 
124  Types.push_back(GV->getValueType());
125 
126  unsigned Limit = 20;
127  do {
128  Type *Ty = Types.pop_back_val();
129  switch (Ty->getTypeID()) {
130  default: break;
131  case Type::PointerTyID:
132  return true;
135  if (cast<VectorType>(Ty)->getElementType()->isPointerTy())
136  return true;
137  break;
138  case Type::ArrayTyID:
139  Types.push_back(cast<ArrayType>(Ty)->getElementType());
140  break;
141  case Type::StructTyID: {
142  StructType *STy = cast<StructType>(Ty);
143  if (STy->isOpaque()) return true;
145  E = STy->element_end(); I != E; ++I) {
146  Type *InnerTy = *I;
147  if (isa<PointerType>(InnerTy)) return true;
148  if (isa<StructType>(InnerTy) || isa<ArrayType>(InnerTy) ||
149  isa<VectorType>(InnerTy))
150  Types.push_back(InnerTy);
151  }
152  break;
153  }
154  }
155  if (--Limit == 0) return true;
156  } while (!Types.empty());
157  return false;
158 }
159 
160 /// Given a value that is stored to a global but never read, determine whether
161 /// it's safe to remove the store and the chain of computation that feeds the
162 /// store.
164  Value *V, function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
165  do {
166  if (isa<Constant>(V))
167  return true;
168  if (!V->hasOneUse())
169  return false;
170  if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
171  isa<GlobalValue>(V))
172  return false;
173  if (isAllocationFn(V, GetTLI))
174  return true;
175 
176  Instruction *I = cast<Instruction>(V);
177  if (I->mayHaveSideEffects())
178  return false;
179  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
180  if (!GEP->hasAllConstantIndices())
181  return false;
182  } else if (I->getNumOperands() != 1) {
183  return false;
184  }
185 
186  V = I->getOperand(0);
187  } while (true);
188 }
189 
190 /// This GV is a pointer root. Loop over all users of the global and clean up
191 /// any that obviously don't assign the global a value that isn't dynamically
192 /// allocated.
193 static bool
195  function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
196  // A brief explanation of leak checkers. The goal is to find bugs where
197  // pointers are forgotten, causing an accumulating growth in memory
198  // usage over time. The common strategy for leak checkers is to explicitly
199  // allow the memory pointed to by globals at exit. This is popular because it
200  // also solves another problem where the main thread of a C++ program may shut
201  // down before other threads that are still expecting to use those globals. To
202  // handle that case, we expect the program may create a singleton and never
203  // destroy it.
204 
205  bool Changed = false;
206 
207  // If Dead[n].first is the only use of a malloc result, we can delete its
208  // chain of computation and the store to the global in Dead[n].second.
210 
211  // Constants can't be pointers to dynamically allocated memory.
212  for (User *U : llvm::make_early_inc_range(GV->users())) {
213  if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
214  Value *V = SI->getValueOperand();
215  if (isa<Constant>(V)) {
216  Changed = true;
217  SI->eraseFromParent();
218  } else if (Instruction *I = dyn_cast<Instruction>(V)) {
219  if (I->hasOneUse())
220  Dead.push_back(std::make_pair(I, SI));
221  }
222  } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
223  if (isa<Constant>(MSI->getValue())) {
224  Changed = true;
225  MSI->eraseFromParent();
226  } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
227  if (I->hasOneUse())
228  Dead.push_back(std::make_pair(I, MSI));
229  }
230  } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
231  GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
232  if (MemSrc && MemSrc->isConstant()) {
233  Changed = true;
234  MTI->eraseFromParent();
235  } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
236  if (I->hasOneUse())
237  Dead.push_back(std::make_pair(I, MTI));
238  }
239  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
240  if (CE->use_empty()) {
241  CE->destroyConstant();
242  Changed = true;
243  }
244  } else if (Constant *C = dyn_cast<Constant>(U)) {
245  if (isSafeToDestroyConstant(C)) {
246  C->destroyConstant();
247  // This could have invalidated UI, start over from scratch.
248  Dead.clear();
249  CleanupPointerRootUsers(GV, GetTLI);
250  return true;
251  }
252  }
253  }
254 
255  for (int i = 0, e = Dead.size(); i != e; ++i) {
256  if (IsSafeComputationToRemove(Dead[i].first, GetTLI)) {
257  Dead[i].second->eraseFromParent();
258  Instruction *I = Dead[i].first;
259  do {
260  if (isAllocationFn(I, GetTLI))
261  break;
262  Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
263  if (!J)
264  break;
265  I->eraseFromParent();
266  I = J;
267  } while (true);
268  I->eraseFromParent();
269  Changed = true;
270  }
271  }
272 
273  return Changed;
274 }
275 
276 /// We just marked GV constant. Loop over all users of the global, cleaning up
277 /// the obvious ones. This is largely just a quick scan over the use list to
278 /// clean up the easy and obvious cruft. This returns true if it made a change.
280  const DataLayout &DL) {
281  Constant *Init = GV->getInitializer();
282  SmallVector<User *, 8> WorkList(GV->users());
283  SmallPtrSet<User *, 8> Visited;
284  bool Changed = false;
285 
286  SmallVector<WeakTrackingVH> MaybeDeadInsts;
287  auto EraseFromParent = [&](Instruction *I) {
288  for (Value *Op : I->operands())
289  if (auto *OpI = dyn_cast<Instruction>(Op))
290  MaybeDeadInsts.push_back(OpI);
291  I->eraseFromParent();
292  Changed = true;
293  };
294  while (!WorkList.empty()) {
295  User *U = WorkList.pop_back_val();
296  if (!Visited.insert(U).second)
297  continue;
298 
299  if (auto *BO = dyn_cast<BitCastOperator>(U))
300  append_range(WorkList, BO->users());
301  if (auto *ASC = dyn_cast<AddrSpaceCastOperator>(U))
302  append_range(WorkList, ASC->users());
303  else if (auto *GEP = dyn_cast<GEPOperator>(U))
304  append_range(WorkList, GEP->users());
305  else if (auto *LI = dyn_cast<LoadInst>(U)) {
306  // A load from a uniform value is always the same, regardless of any
307  // applied offset.
308  Type *Ty = LI->getType();
310  LI->replaceAllUsesWith(Res);
311  EraseFromParent(LI);
312  continue;
313  }
314 
315  Value *PtrOp = LI->getPointerOperand();
316  APInt Offset(DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
317  PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
318  DL, Offset, /* AllowNonInbounds */ true);
319  if (PtrOp == GV) {
320  if (auto *Value = ConstantFoldLoadFromConst(Init, Ty, Offset, DL)) {
322  EraseFromParent(LI);
323  }
324  }
325  } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
326  // Store must be unreachable or storing Init into the global.
327  EraseFromParent(SI);
328  } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
329  if (getUnderlyingObject(MI->getRawDest()) == GV)
330  EraseFromParent(MI);
331  }
332  }
333 
334  Changed |=
337  return Changed;
338 }
339 
340 /// Look at all uses of the global and determine which (offset, type) pairs it
341 /// can be split into.
343  const DataLayout &DL) {
344  SmallVector<Use *, 16> Worklist;
345  SmallPtrSet<Use *, 16> Visited;
346  auto AppendUses = [&](Value *V) {
347  for (Use &U : V->uses())
348  if (Visited.insert(&U).second)
349  Worklist.push_back(&U);
350  };
351  AppendUses(GV);
352  while (!Worklist.empty()) {
353  Use *U = Worklist.pop_back_val();
354  User *V = U->getUser();
355  if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V)) {
356  AppendUses(V);
357  continue;
358  }
359 
360  if (auto *GEP = dyn_cast<GEPOperator>(V)) {
361  if (!GEP->hasAllConstantIndices())
362  return false;
363  AppendUses(V);
364  continue;
365  }
366 
367  if (Value *Ptr = getLoadStorePointerOperand(V)) {
368  // This is storing the global address into somewhere, not storing into
369  // the global.
370  if (isa<StoreInst>(V) && U->getOperandNo() == 0)
371  return false;
372 
373  APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
374  Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
375  /* AllowNonInbounds */ true);
376  if (Ptr != GV || Offset.getActiveBits() >= 64)
377  return false;
378 
379  // TODO: We currently require that all accesses at a given offset must
380  // use the same type. This could be relaxed.
381  Type *Ty = getLoadStoreType(V);
382  auto It = Types.try_emplace(Offset.getZExtValue(), Ty).first;
383  if (Ty != It->second)
384  return false;
385  continue;
386  }
387 
388  // Ignore dead constant users.
389  if (auto *C = dyn_cast<Constant>(V)) {
391  return false;
392  continue;
393  }
394 
395  // Unknown user.
396  return false;
397  }
398 
399  return true;
400 }
401 
402 /// Copy over the debug info for a variable to its SRA replacements.
404  uint64_t FragmentOffsetInBits,
405  uint64_t FragmentSizeInBits,
406  uint64_t VarSize) {
408  GV->getDebugInfo(GVs);
409  for (auto *GVE : GVs) {
410  DIVariable *Var = GVE->getVariable();
411  DIExpression *Expr = GVE->getExpression();
412  // If the FragmentSize is smaller than the variable,
413  // emit a fragment expression.
414  if (FragmentSizeInBits < VarSize) {
416  Expr, FragmentOffsetInBits, FragmentSizeInBits))
417  Expr = *E;
418  else
419  return;
420  }
421  auto *NGVE = DIGlobalVariableExpression::get(GVE->getContext(), Var, Expr);
422  NGV->addDebugInfo(NGVE);
423  }
424 }
425 
426 /// Perform scalar replacement of aggregates on the specified global variable.
427 /// This opens the door for other optimizations by exposing the behavior of the
428 /// program in a more fine-grained way. We have determined that this
429 /// transformation is safe already. We return the first global variable we
430 /// insert so that the caller can reprocess it.
432  assert(GV->hasLocalLinkage());
433 
434  // Collect types to split into.
436  if (!collectSRATypes(Types, GV, DL) || Types.empty())
437  return nullptr;
438 
439  // Make sure we don't SRA back to the same type.
440  if (Types.size() == 1 && Types.begin()->second == GV->getValueType())
441  return nullptr;
442 
443  // Don't perform SRA if we would have to split into many globals.
444  if (Types.size() > 16)
445  return nullptr;
446 
447  // Sort by offset.
449  append_range(TypesVector, Types);
450  sort(TypesVector,
451  [](const auto &A, const auto &B) { return A.first < B.first; });
452 
453  // Check that the types are non-overlapping.
454  uint64_t Offset = 0;
455  for (const auto &Pair : TypesVector) {
456  // Overlaps with previous type.
457  if (Pair.first < Offset)
458  return nullptr;
459 
460  Offset = Pair.first + DL.getTypeAllocSize(Pair.second);
461  }
462 
463  // Some accesses go beyond the end of the global, don't bother.
464  if (Offset > DL.getTypeAllocSize(GV->getValueType()))
465  return nullptr;
466 
467  // Collect initializers for new globals.
468  Constant *OrigInit = GV->getInitializer();
469  DenseMap<uint64_t, Constant *> Initializers;
470  for (const auto &Pair : Types) {
471  Constant *NewInit = ConstantFoldLoadFromConst(OrigInit, Pair.second,
472  APInt(64, Pair.first), DL);
473  if (!NewInit) {
474  LLVM_DEBUG(dbgs() << "Global SRA: Failed to evaluate initializer of "
475  << *GV << " with type " << *Pair.second << " at offset "
476  << Pair.first << "\n");
477  return nullptr;
478  }
479  Initializers.insert({Pair.first, NewInit});
480  }
481 
482  LLVM_DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");
483 
484  // Get the alignment of the global, either explicit or target-specific.
485  Align StartAlignment =
486  DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
487  uint64_t VarSize = DL.getTypeSizeInBits(GV->getValueType());
488 
489  // Create replacement globals.
491  unsigned NameSuffix = 0;
492  for (auto &Pair : TypesVector) {
493  uint64_t Offset = Pair.first;
494  Type *Ty = Pair.second;
495  GlobalVariable *NGV = new GlobalVariable(
496  *GV->getParent(), Ty, false, GlobalVariable::InternalLinkage,
497  Initializers[Offset], GV->getName() + "." + Twine(NameSuffix++), GV,
498  GV->getThreadLocalMode(), GV->getAddressSpace());
499  NGV->copyAttributesFrom(GV);
500  NewGlobals.insert({Offset, NGV});
501 
502  // Calculate the known alignment of the field. If the original aggregate
503  // had 256 byte alignment for example, something might depend on that:
504  // propagate info to each field.
505  Align NewAlign = commonAlignment(StartAlignment, Offset);
506  if (NewAlign > DL.getABITypeAlign(Ty))
507  NGV->setAlignment(NewAlign);
508 
509  // Copy over the debug info for the variable.
510  transferSRADebugInfo(GV, NGV, Offset * 8, DL.getTypeAllocSizeInBits(Ty),
511  VarSize);
512  }
513 
514  // Replace uses of the original global with uses of the new global.
515  SmallVector<Value *, 16> Worklist;
516  SmallPtrSet<Value *, 16> Visited;
518  auto AppendUsers = [&](Value *V) {
519  for (User *U : V->users())
520  if (Visited.insert(U).second)
521  Worklist.push_back(U);
522  };
523  AppendUsers(GV);
524  while (!Worklist.empty()) {
525  Value *V = Worklist.pop_back_val();
526  if (isa<BitCastOperator>(V) || isa<AddrSpaceCastOperator>(V) ||
527  isa<GEPOperator>(V)) {
528  AppendUsers(V);
529  if (isa<Instruction>(V))
530  DeadInsts.push_back(V);
531  continue;
532  }
533 
534  if (Value *Ptr = getLoadStorePointerOperand(V)) {
535  APInt Offset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
536  Ptr = Ptr->stripAndAccumulateConstantOffsets(DL, Offset,
537  /* AllowNonInbounds */ true);
538  assert(Ptr == GV && "Load/store must be from/to global");
539  GlobalVariable *NGV = NewGlobals[Offset.getZExtValue()];
540  assert(NGV && "Must have replacement global for this offset");
541 
542  // Update the pointer operand and recalculate alignment.
543  Align PrefAlign = DL.getPrefTypeAlign(getLoadStoreType(V));
544  Align NewAlign =
545  getOrEnforceKnownAlignment(NGV, PrefAlign, DL, cast<Instruction>(V));
546 
547  if (auto *LI = dyn_cast<LoadInst>(V)) {
548  LI->setOperand(0, NGV);
549  LI->setAlignment(NewAlign);
550  } else {
551  auto *SI = cast<StoreInst>(V);
552  SI->setOperand(1, NGV);
553  SI->setAlignment(NewAlign);
554  }
555  continue;
556  }
557 
558  assert(isa<Constant>(V) && isSafeToDestroyConstant(cast<Constant>(V)) &&
559  "Other users can only be dead constants");
560  }
561 
562  // Delete old instructions and global.
565  GV->eraseFromParent();
566  ++NumSRA;
567 
568  assert(NewGlobals.size() > 0);
569  return NewGlobals.begin()->second;
570 }
571 
572 /// Return true if all users of the specified value will trap if the value is
573 /// dynamically null. PHIs keeps track of any phi nodes we've seen to avoid
574 /// reprocessing them.
575 static bool AllUsesOfValueWillTrapIfNull(const Value *V,
577  for (const User *U : V->users()) {
578  if (const Instruction *I = dyn_cast<Instruction>(U)) {
579  // If null pointer is considered valid, then all uses are non-trapping.
580  // Non address-space 0 globals have already been pruned by the caller.
581  if (NullPointerIsDefined(I->getFunction()))
582  return false;
583  }
584  if (isa<LoadInst>(U)) {
585  // Will trap.
586  } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
587  if (SI->getOperand(0) == V) {
588  //cerr << "NONTRAPPING USE: " << *U;
589  return false; // Storing the value.
590  }
591  } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
592  if (CI->getCalledOperand() != V) {
593  //cerr << "NONTRAPPING USE: " << *U;
594  return false; // Not calling the ptr
595  }
596  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
597  if (II->getCalledOperand() != V) {
598  //cerr << "NONTRAPPING USE: " << *U;
599  return false; // Not calling the ptr
600  }
601  } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
602  if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
603  } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
604  if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
605  } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
606  // If we've already seen this phi node, ignore it, it has already been
607  // checked.
608  if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
609  return false;
610  } else if (isa<ICmpInst>(U) &&
611  !ICmpInst::isSigned(cast<ICmpInst>(U)->getPredicate()) &&
612  isa<LoadInst>(U->getOperand(0)) &&
613  isa<ConstantPointerNull>(U->getOperand(1))) {
614  assert(isa<GlobalValue>(cast<LoadInst>(U->getOperand(0))
615  ->getPointerOperand()
616  ->stripPointerCasts()) &&
617  "Should be GlobalVariable");
618  // This and only this kind of non-signed ICmpInst is to be replaced with
619  // the comparing of the value of the created global init bool later in
620  // optimizeGlobalAddressOfAllocation for the global variable.
621  } else {
622  //cerr << "NONTRAPPING USE: " << *U;
623  return false;
624  }
625  }
626  return true;
627 }
628 
629 /// Return true if all uses of any loads from GV will trap if the loaded value
630 /// is null. Note that this also permits comparisons of the loaded value
631 /// against null, as a special case.
634  Worklist.push_back(GV);
635  while (!Worklist.empty()) {
636  const Value *P = Worklist.pop_back_val();
637  for (auto *U : P->users()) {
638  if (auto *LI = dyn_cast<LoadInst>(U)) {
640  if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
641  return false;
642  } else if (auto *SI = dyn_cast<StoreInst>(U)) {
643  // Ignore stores to the global.
644  if (SI->getPointerOperand() != P)
645  return false;
646  } else if (auto *CE = dyn_cast<ConstantExpr>(U)) {
647  if (CE->stripPointerCasts() != GV)
648  return false;
649  // Check further the ConstantExpr.
650  Worklist.push_back(CE);
651  } else {
652  // We don't know or understand this user, bail out.
653  return false;
654  }
655  }
656  }
657 
658  return true;
659 }
660 
661 /// Get all the loads/store uses for global variable \p GV.
664  SmallVector<Value *, 4> Worklist;
665  Worklist.push_back(GV);
666  while (!Worklist.empty()) {
667  auto *P = Worklist.pop_back_val();
668  for (auto *U : P->users()) {
669  if (auto *CE = dyn_cast<ConstantExpr>(U)) {
670  Worklist.push_back(CE);
671  continue;
672  }
673 
674  assert((isa<LoadInst>(U) || isa<StoreInst>(U)) &&
675  "Expect only load or store instructions");
676  Uses.push_back(U);
677  }
678  }
679 }
680 
682  bool Changed = false;
683  for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
684  Instruction *I = cast<Instruction>(*UI++);
685  // Uses are non-trapping if null pointer is considered valid.
686  // Non address-space 0 globals are already pruned by the caller.
687  if (NullPointerIsDefined(I->getFunction()))
688  return false;
689  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
690  LI->setOperand(0, NewV);
691  Changed = true;
692  } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
693  if (SI->getOperand(1) == V) {
694  SI->setOperand(1, NewV);
695  Changed = true;
696  }
697  } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
698  CallBase *CB = cast<CallBase>(I);
699  if (CB->getCalledOperand() == V) {
700  // Calling through the pointer! Turn into a direct call, but be careful
701  // that the pointer is not also being passed as an argument.
702  CB->setCalledOperand(NewV);
703  Changed = true;
704  bool PassedAsArg = false;
705  for (unsigned i = 0, e = CB->arg_size(); i != e; ++i)
706  if (CB->getArgOperand(i) == V) {
707  PassedAsArg = true;
708  CB->setArgOperand(i, NewV);
709  }
710 
711  if (PassedAsArg) {
712  // Being passed as an argument also. Be careful to not invalidate UI!
713  UI = V->user_begin();
714  }
715  }
716  } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
717  Changed |= OptimizeAwayTrappingUsesOfValue(CI,
718  ConstantExpr::getCast(CI->getOpcode(),
719  NewV, CI->getType()));
720  if (CI->use_empty()) {
721  Changed = true;
722  CI->eraseFromParent();
723  }
724  } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
725  // Should handle GEP here.
727  Idxs.reserve(GEPI->getNumOperands()-1);
728  for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
729  i != e; ++i)
730  if (Constant *C = dyn_cast<Constant>(*i))
731  Idxs.push_back(C);
732  else
733  break;
734  if (Idxs.size() == GEPI->getNumOperands()-1)
736  GEPI, ConstantExpr::getGetElementPtr(GEPI->getSourceElementType(),
737  NewV, Idxs));
738  if (GEPI->use_empty()) {
739  Changed = true;
740  GEPI->eraseFromParent();
741  }
742  }
743  }
744 
745  return Changed;
746 }
747 
748 /// The specified global has only one non-null value stored into it. If there
749 /// are uses of the loaded value that would trap if the loaded value is
750 /// dynamically null, then we know that they cannot be reachable with a null
751 /// optimize away the load.
753  GlobalVariable *GV, Constant *LV, const DataLayout &DL,
754  function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
755  bool Changed = false;
756 
757  // Keep track of whether we are able to remove all the uses of the global
758  // other than the store that defines it.
759  bool AllNonStoreUsesGone = true;
760 
761  // Replace all uses of loads with uses of uses of the stored value.
762  for (User *GlobalUser : llvm::make_early_inc_range(GV->users())) {
763  if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
764  Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
765  // If we were able to delete all uses of the loads
766  if (LI->use_empty()) {
767  LI->eraseFromParent();
768  Changed = true;
769  } else {
770  AllNonStoreUsesGone = false;
771  }
772  } else if (isa<StoreInst>(GlobalUser)) {
773  // Ignore the store that stores "LV" to the global.
774  assert(GlobalUser->getOperand(1) == GV &&
775  "Must be storing *to* the global");
776  } else {
777  AllNonStoreUsesGone = false;
778 
779  // If we get here we could have other crazy uses that are transitively
780  // loaded.
781  assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
782  isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
783  isa<BitCastInst>(GlobalUser) ||
784  isa<GetElementPtrInst>(GlobalUser)) &&
785  "Only expect load and stores!");
786  }
787  }
788 
789  if (Changed) {
790  LLVM_DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV
791  << "\n");
792  ++NumGlobUses;
793  }
794 
795  // If we nuked all of the loads, then none of the stores are needed either,
796  // nor is the global.
797  if (AllNonStoreUsesGone) {
798  if (isLeakCheckerRoot(GV)) {
799  Changed |= CleanupPointerRootUsers(GV, GetTLI);
800  } else {
801  Changed = true;
803  }
804  if (GV->use_empty()) {
805  LLVM_DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
806  Changed = true;
807  GV->eraseFromParent();
808  ++NumDeleted;
809  }
810  }
811  return Changed;
812 }
813 
814 /// Walk the use list of V, constant folding all of the instructions that are
815 /// foldable.
816 static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
817  TargetLibraryInfo *TLI) {
818  for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
819  if (Instruction *I = dyn_cast<Instruction>(*UI++))
820  if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
821  I->replaceAllUsesWith(NewC);
822 
823  // Advance UI to the next non-I use to avoid invalidating it!
824  // Instructions could multiply use V.
825  while (UI != E && *UI == I)
826  ++UI;
827  if (isInstructionTriviallyDead(I, TLI))
828  I->eraseFromParent();
829  }
830 }
831 
832 /// This function takes the specified global variable, and transforms the
833 /// program as if it always contained the result of the specified malloc.
834 /// Because it is always the result of the specified malloc, there is no reason
835 /// to actually DO the malloc. Instead, turn the malloc into a global, and any
836 /// loads of GV as uses of the new global.
837 static GlobalVariable *
839  uint64_t AllocSize, Constant *InitVal,
840  const DataLayout &DL,
841  TargetLibraryInfo *TLI) {
842  LLVM_DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI
843  << '\n');
844 
845  // Create global of type [AllocSize x i8].
846  Type *GlobalType = ArrayType::get(Type::getInt8Ty(GV->getContext()),
847  AllocSize);
848 
849  // Create the new global variable. The contents of the allocated memory is
850  // undefined initially, so initialize with an undef value.
851  GlobalVariable *NewGV = new GlobalVariable(
852  *GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,
853  UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
854  GV->getThreadLocalMode());
855 
856  // Initialize the global at the point of the original call. Note that this
857  // is a different point from the initialization referred to below for the
858  // nullability handling. Sublety: We have not proven the original global was
859  // only initialized once. As such, we can not fold this into the initializer
860  // of the new global as may need to re-init the storage multiple times.
861  if (!isa<UndefValue>(InitVal)) {
863  // TODO: Use alignment above if align!=1
864  Builder.CreateMemSet(NewGV, InitVal, AllocSize, None);
865  }
866 
867  // Update users of the allocation to use the new global instead.
868  BitCastInst *TheBC = nullptr;
869  while (!CI->use_empty()) {
870  Instruction *User = cast<Instruction>(CI->user_back());
871  if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
872  if (BCI->getType() == NewGV->getType()) {
873  BCI->replaceAllUsesWith(NewGV);
874  BCI->eraseFromParent();
875  } else {
876  BCI->setOperand(0, NewGV);
877  }
878  } else {
879  if (!TheBC)
880  TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
881  User->replaceUsesOfWith(CI, TheBC);
882  }
883  }
884 
885  SmallPtrSet<Constant *, 1> RepValues;
886  RepValues.insert(NewGV);
887 
888  // If there is a comparison against null, we will insert a global bool to
889  // keep track of whether the global was initialized yet or not.
890  GlobalVariable *InitBool =
891  new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
894  GV->getName()+".init", GV->getThreadLocalMode());
895  bool InitBoolUsed = false;
896 
897  // Loop over all instruction uses of GV, processing them in turn.
899  allUsesOfLoadAndStores(GV, Guses);
900  for (auto *U : Guses) {
901  if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
902  // The global is initialized when the store to it occurs. If the stored
903  // value is null value, the global bool is set to false, otherwise true.
905  GV->getContext(),
906  !isa<ConstantPointerNull>(SI->getValueOperand())),
907  InitBool, false, Align(1), SI->getOrdering(),
908  SI->getSyncScopeID(), SI);
909  SI->eraseFromParent();
910  continue;
911  }
912 
913  LoadInst *LI = cast<LoadInst>(U);
914  while (!LI->use_empty()) {
915  Use &LoadUse = *LI->use_begin();
916  ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
917  if (!ICI) {
918  auto *CE = ConstantExpr::getBitCast(NewGV, LI->getType());
919  RepValues.insert(CE);
920  LoadUse.set(CE);
921  continue;
922  }
923 
924  // Replace the cmp X, 0 with a use of the bool value.
925  Value *LV = new LoadInst(InitBool->getValueType(), InitBool,
926  InitBool->getName() + ".val", false, Align(1),
927  LI->getOrdering(), LI->getSyncScopeID(), LI);
928  InitBoolUsed = true;
929  switch (ICI->getPredicate()) {
930  default: llvm_unreachable("Unknown ICmp Predicate!");
931  case ICmpInst::ICMP_ULT: // X < null -> always false
932  LV = ConstantInt::getFalse(GV->getContext());
933  break;
934  case ICmpInst::ICMP_UGE: // X >= null -> always true
935  LV = ConstantInt::getTrue(GV->getContext());
936  break;
937  case ICmpInst::ICMP_ULE:
938  case ICmpInst::ICMP_EQ:
939  LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
940  break;
941  case ICmpInst::ICMP_NE:
942  case ICmpInst::ICMP_UGT:
943  break; // no change.
944  }
945  ICI->replaceAllUsesWith(LV);
946  ICI->eraseFromParent();
947  }
948  LI->eraseFromParent();
949  }
950 
951  // If the initialization boolean was used, insert it, otherwise delete it.
952  if (!InitBoolUsed) {
953  while (!InitBool->use_empty()) // Delete initializations
954  cast<StoreInst>(InitBool->user_back())->eraseFromParent();
955  delete InitBool;
956  } else
957  GV->getParent()->getGlobalList().insert(GV->getIterator(), InitBool);
958 
959  // Now the GV is dead, nuke it and the allocation..
960  GV->eraseFromParent();
961  CI->eraseFromParent();
962 
963  // To further other optimizations, loop over all users of NewGV and try to
964  // constant prop them. This will promote GEP instructions with constant
965  // indices into GEP constant-exprs, which will allow global-opt to hack on it.
966  for (auto *CE : RepValues)
967  ConstantPropUsersOf(CE, DL, TLI);
968 
969  return NewGV;
970 }
971 
972 /// Scan the use-list of GV checking to make sure that there are no complex uses
973 /// of GV. We permit simple things like dereferencing the pointer, but not
974 /// storing through the address, unless it is to the specified global.
975 static bool
977  const GlobalVariable *GV) {
980  Worklist.push_back(CI);
981 
982  while (!Worklist.empty()) {
983  const Value *V = Worklist.pop_back_val();
984  if (!Visited.insert(V).second)
985  continue;
986 
987  for (const Use &VUse : V->uses()) {
988  const User *U = VUse.getUser();
989  if (isa<LoadInst>(U) || isa<CmpInst>(U))
990  continue; // Fine, ignore.
991 
992  if (auto *SI = dyn_cast<StoreInst>(U)) {
993  if (SI->getValueOperand() == V &&
994  SI->getPointerOperand()->stripPointerCasts() != GV)
995  return false; // Storing the pointer not into GV... bad.
996  continue; // Otherwise, storing through it, or storing into GV... fine.
997  }
998 
999  if (auto *BCI = dyn_cast<BitCastInst>(U)) {
1000  Worklist.push_back(BCI);
1001  continue;
1002  }
1003 
1004  if (auto *GEPI = dyn_cast<GetElementPtrInst>(U)) {
1005  Worklist.push_back(GEPI);
1006  continue;
1007  }
1008 
1009  return false;
1010  }
1011  }
1012 
1013  return true;
1014 }
1015 
1016 /// If we have a global that is only initialized with a fixed size allocation
1017 /// try to transform the program to use global memory instead of heap
1018 /// allocated memory. This eliminates dynamic allocation, avoids an indirection
1019 /// accessing the data, and exposes the resultant global to further GlobalOpt.
1021  CallInst *CI,
1022  AtomicOrdering Ordering,
1023  const DataLayout &DL,
1024  TargetLibraryInfo *TLI) {
1025  if (!isAllocRemovable(CI, TLI))
1026  // Must be able to remove the call when we get done..
1027  return false;
1028 
1029  Type *Int8Ty = Type::getInt8Ty(CI->getFunction()->getContext());
1030  Constant *InitVal = getInitialValueOfAllocation(CI, TLI, Int8Ty);
1031  if (!InitVal)
1032  // Must be able to emit a memset for initialization
1033  return false;
1034 
1035  uint64_t AllocSize;
1036  if (!getObjectSize(CI, AllocSize, DL, TLI, ObjectSizeOpts()))
1037  return false;
1038 
1039  // Restrict this transformation to only working on small allocations
1040  // (2048 bytes currently), as we don't want to introduce a 16M global or
1041  // something.
1042  if (AllocSize >= 2048)
1043  return false;
1044 
1045  // We can't optimize this global unless all uses of it are *known* to be
1046  // of the malloc value, not of the null initializer value (consider a use
1047  // that compares the global's value against zero to see if the malloc has
1048  // been reached). To do this, we check to see if all uses of the global
1049  // would trap if the global were null: this proves that they must all
1050  // happen after the malloc.
1052  return false;
1053 
1054  // We can't optimize this if the malloc itself is used in a complex way,
1055  // for example, being stored into multiple globals. This allows the
1056  // malloc to be stored into the specified global, loaded, gep, icmp'd.
1057  // These are all things we could transform to using the global for.
1059  return false;
1060 
1061  OptimizeGlobalAddressOfAllocation(GV, CI, AllocSize, InitVal, DL, TLI);
1062  return true;
1063 }
1064 
1065 // Try to optimize globals based on the knowledge that only one value (besides
1066 // its initializer) is ever stored to the global.
1067 static bool
1069  AtomicOrdering Ordering, const DataLayout &DL,
1070  function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
1071  // Ignore no-op GEPs and bitcasts.
1072  StoredOnceVal = StoredOnceVal->stripPointerCasts();
1073 
1074  // If we are dealing with a pointer global that is initialized to null and
1075  // only has one (non-null) value stored into it, then we can optimize any
1076  // users of the loaded value (often calls and loads) that would trap if the
1077  // value was null.
1078  if (GV->getInitializer()->getType()->isPointerTy() &&
1079  GV->getInitializer()->isNullValue() &&
1080  StoredOnceVal->getType()->isPointerTy() &&
1082  nullptr /* F */,
1084  if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1085  if (GV->getInitializer()->getType() != SOVC->getType())
1086  SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
1087 
1088  // Optimize away any trapping uses of the loaded value.
1089  if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, GetTLI))
1090  return true;
1091  } else if (isAllocationFn(StoredOnceVal, GetTLI)) {
1092  if (auto *CI = dyn_cast<CallInst>(StoredOnceVal)) {
1093  auto *TLI = &GetTLI(*CI->getFunction());
1094  if (tryToOptimizeStoreOfAllocationToGlobal(GV, CI, Ordering, DL, TLI))
1095  return true;
1096  }
1097  }
1098  }
1099 
1100  return false;
1101 }
1102 
1103 /// At this point, we have learned that the only two values ever stored into GV
1104 /// are its initializer and OtherVal. See if we can shrink the global into a
1105 /// boolean and select between the two values whenever it is used. This exposes
1106 /// the values to other scalar optimizations.
1108  Type *GVElType = GV->getValueType();
1109 
1110  // If GVElType is already i1, it is already shrunk. If the type of the GV is
1111  // an FP value, pointer or vector, don't do this optimization because a select
1112  // between them is very expensive and unlikely to lead to later
1113  // simplification. In these cases, we typically end up with "cond ? v1 : v2"
1114  // where v1 and v2 both require constant pool loads, a big loss.
1115  if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1116  GVElType->isFloatingPointTy() ||
1117  GVElType->isPointerTy() || GVElType->isVectorTy())
1118  return false;
1119 
1120  // Walk the use list of the global seeing if all the uses are load or store.
1121  // If there is anything else, bail out.
1122  for (User *U : GV->users()) {
1123  if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1124  return false;
1125  if (getLoadStoreType(U) != GVElType)
1126  return false;
1127  }
1128 
1129  LLVM_DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV << "\n");
1130 
1131  // Create the new global, initializing it to false.
1133  false,
1136  GV->getName()+".b",
1137  GV->getThreadLocalMode(),
1138  GV->getType()->getAddressSpace());
1139  NewGV->copyAttributesFrom(GV);
1140  GV->getParent()->getGlobalList().insert(GV->getIterator(), NewGV);
1141 
1142  Constant *InitVal = GV->getInitializer();
1143  assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1144  "No reason to shrink to bool!");
1145 
1147  GV->getDebugInfo(GVs);
1148 
1149  // If initialized to zero and storing one into the global, we can use a cast
1150  // instead of a select to synthesize the desired value.
1151  bool IsOneZero = false;
1152  bool EmitOneOrZero = true;
1153  auto *CI = dyn_cast<ConstantInt>(OtherVal);
1154  if (CI && CI->getValue().getActiveBits() <= 64) {
1155  IsOneZero = InitVal->isNullValue() && CI->isOne();
1156 
1157  auto *CIInit = dyn_cast<ConstantInt>(GV->getInitializer());
1158  if (CIInit && CIInit->getValue().getActiveBits() <= 64) {
1159  uint64_t ValInit = CIInit->getZExtValue();
1160  uint64_t ValOther = CI->getZExtValue();
1161  uint64_t ValMinus = ValOther - ValInit;
1162 
1163  for(auto *GVe : GVs){
1164  DIGlobalVariable *DGV = GVe->getVariable();
1165  DIExpression *E = GVe->getExpression();
1166  const DataLayout &DL = GV->getParent()->getDataLayout();
1167  unsigned SizeInOctets =
1168  DL.getTypeAllocSizeInBits(NewGV->getValueType()) / 8;
1169 
1170  // It is expected that the address of global optimized variable is on
1171  // top of the stack. After optimization, value of that variable will
1172  // be ether 0 for initial value or 1 for other value. The following
1173  // expression should return constant integer value depending on the
1174  // value at global object address:
1175  // val * (ValOther - ValInit) + ValInit:
1176  // DW_OP_deref DW_OP_constu <ValMinus>
1177  // DW_OP_mul DW_OP_constu <ValInit> DW_OP_plus DW_OP_stack_value
1179  dwarf::DW_OP_deref_size, SizeInOctets,
1180  dwarf::DW_OP_constu, ValMinus,
1181  dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit,
1182  dwarf::DW_OP_plus};
1183  bool WithStackValue = true;
1184  E = DIExpression::prependOpcodes(E, Ops, WithStackValue);
1187  NewGV->addDebugInfo(DGVE);
1188  }
1189  EmitOneOrZero = false;
1190  }
1191  }
1192 
1193  if (EmitOneOrZero) {
1194  // FIXME: This will only emit address for debugger on which will
1195  // be written only 0 or 1.
1196  for(auto *GV : GVs)
1197  NewGV->addDebugInfo(GV);
1198  }
1199 
1200  while (!GV->use_empty()) {
1201  Instruction *UI = cast<Instruction>(GV->user_back());
1202  if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1203  // Change the store into a boolean store.
1204  bool StoringOther = SI->getOperand(0) == OtherVal;
1205  // Only do this if we weren't storing a loaded value.
1206  Value *StoreVal;
1207  if (StoringOther || SI->getOperand(0) == InitVal) {
1208  StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1209  StoringOther);
1210  } else {
1211  // Otherwise, we are storing a previously loaded copy. To do this,
1212  // change the copy from copying the original value to just copying the
1213  // bool.
1214  Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
1215 
1216  // If we've already replaced the input, StoredVal will be a cast or
1217  // select instruction. If not, it will be a load of the original
1218  // global.
1219  if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
1220  assert(LI->getOperand(0) == GV && "Not a copy!");
1221  // Insert a new load, to preserve the saved value.
1222  StoreVal = new LoadInst(NewGV->getValueType(), NewGV,
1223  LI->getName() + ".b", false, Align(1),
1224  LI->getOrdering(), LI->getSyncScopeID(), LI);
1225  } else {
1226  assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
1227  "This is not a form that we understand!");
1228  StoreVal = StoredVal->getOperand(0);
1229  assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
1230  }
1231  }
1232  StoreInst *NSI =
1233  new StoreInst(StoreVal, NewGV, false, Align(1), SI->getOrdering(),
1234  SI->getSyncScopeID(), SI);
1235  NSI->setDebugLoc(SI->getDebugLoc());
1236  } else {
1237  // Change the load into a load of bool then a select.
1238  LoadInst *LI = cast<LoadInst>(UI);
1239  LoadInst *NLI = new LoadInst(NewGV->getValueType(), NewGV,
1240  LI->getName() + ".b", false, Align(1),
1241  LI->getOrdering(), LI->getSyncScopeID(), LI);
1242  Instruction *NSI;
1243  if (IsOneZero)
1244  NSI = new ZExtInst(NLI, LI->getType(), "", LI);
1245  else
1246  NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
1247  NSI->takeName(LI);
1248  // Since LI is split into two instructions, NLI and NSI both inherit the
1249  // same DebugLoc
1250  NLI->setDebugLoc(LI->getDebugLoc());
1251  NSI->setDebugLoc(LI->getDebugLoc());
1252  LI->replaceAllUsesWith(NSI);
1253  }
1254  UI->eraseFromParent();
1255  }
1256 
1257  // Retain the name of the old global variable. People who are debugging their
1258  // programs may expect these variables to be named the same.
1259  NewGV->takeName(GV);
1260  GV->eraseFromParent();
1261  return true;
1262 }
1263 
1264 static bool deleteIfDead(
1265  GlobalValue &GV, SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
1267 
1268  if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())
1269  return false;
1270 
1271  if (const Comdat *C = GV.getComdat())
1272  if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
1273  return false;
1274 
1275  bool Dead;
1276  if (auto *F = dyn_cast<Function>(&GV))
1277  Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead();
1278  else
1279  Dead = GV.use_empty();
1280  if (!Dead)
1281  return false;
1282 
1283  LLVM_DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
1284  GV.eraseFromParent();
1285  ++NumDeleted;
1286  return true;
1287 }
1288 
1290  const Function *F, GlobalValue *GV,
1291  function_ref<DominatorTree &(Function &)> LookupDomTree) {
1292  // Find all uses of GV. We expect them all to be in F, and if we can't
1293  // identify any of the uses we bail out.
1294  //
1295  // On each of these uses, identify if the memory that GV points to is
1296  // used/required/live at the start of the function. If it is not, for example
1297  // if the first thing the function does is store to the GV, the GV can
1298  // possibly be demoted.
1299  //
1300  // We don't do an exhaustive search for memory operations - simply look
1301  // through bitcasts as they're quite common and benign.
1302  const DataLayout &DL = GV->getParent()->getDataLayout();
1305  for (auto *U : GV->users()) {
1306  if (Operator::getOpcode(U) == Instruction::BitCast) {
1307  for (auto *UU : U->users()) {
1308  if (auto *LI = dyn_cast<LoadInst>(UU))
1309  Loads.push_back(LI);
1310  else if (auto *SI = dyn_cast<StoreInst>(UU))
1311  Stores.push_back(SI);
1312  else
1313  return false;
1314  }
1315  continue;
1316  }
1317 
1318  Instruction *I = dyn_cast<Instruction>(U);
1319  if (!I)
1320  return false;
1321  assert(I->getParent()->getParent() == F);
1322 
1323  if (auto *LI = dyn_cast<LoadInst>(I))
1324  Loads.push_back(LI);
1325  else if (auto *SI = dyn_cast<StoreInst>(I))
1326  Stores.push_back(SI);
1327  else
1328  return false;
1329  }
1330 
1331  // We have identified all uses of GV into loads and stores. Now check if all
1332  // of them are known not to depend on the value of the global at the function
1333  // entry point. We do this by ensuring that every load is dominated by at
1334  // least one store.
1335  auto &DT = LookupDomTree(*const_cast<Function *>(F));
1336 
1337  // The below check is quadratic. Check we're not going to do too many tests.
1338  // FIXME: Even though this will always have worst-case quadratic time, we
1339  // could put effort into minimizing the average time by putting stores that
1340  // have been shown to dominate at least one load at the beginning of the
1341  // Stores array, making subsequent dominance checks more likely to succeed
1342  // early.
1343  //
1344  // The threshold here is fairly large because global->local demotion is a
1345  // very powerful optimization should it fire.
1346  const unsigned Threshold = 100;
1347  if (Loads.size() * Stores.size() > Threshold)
1348  return false;
1349 
1350  for (auto *L : Loads) {
1351  auto *LTy = L->getType();
1352  if (none_of(Stores, [&](const StoreInst *S) {
1353  auto *STy = S->getValueOperand()->getType();
1354  // The load is only dominated by the store if DomTree says so
1355  // and the number of bits loaded in L is less than or equal to
1356  // the number of bits stored in S.
1357  return DT.dominates(S, L) &&
1358  DL.getTypeStoreSize(LTy).getFixedSize() <=
1359  DL.getTypeStoreSize(STy).getFixedSize();
1360  }))
1361  return false;
1362  }
1363  // All loads have known dependences inside F, so the global can be localized.
1364  return true;
1365 }
1366 
1367 /// C may have non-instruction users. Can all of those users be turned into
1368 /// instructions?
1370  // We don't do this exhaustively. The most common pattern that we really need
1371  // to care about is a constant GEP or constant bitcast - so just looking
1372  // through one single ConstantExpr.
1373  //
1374  // The set of constants that this function returns true for must be able to be
1375  // handled by makeAllConstantUsesInstructions.
1376  for (auto *U : C->users()) {
1377  if (isa<Instruction>(U))
1378  continue;
1379  if (!isa<ConstantExpr>(U))
1380  // Non instruction, non-constantexpr user; cannot convert this.
1381  return false;
1382  for (auto *UU : U->users())
1383  if (!isa<Instruction>(UU))
1384  // A constantexpr used by another constant. We don't try and recurse any
1385  // further but just bail out at this point.
1386  return false;
1387  }
1388 
1389  return true;
1390 }
1391 
1392 /// C may have non-instruction users, and
1393 /// allNonInstructionUsersCanBeMadeInstructions has returned true. Convert the
1394 /// non-instruction users to instructions.
1397  for (auto *U : C->users()) {
1398  if (isa<ConstantExpr>(U))
1399  Users.push_back(cast<ConstantExpr>(U));
1400  else
1401  // We should never get here; allNonInstructionUsersCanBeMadeInstructions
1402  // should not have returned true for C.
1403  assert(
1404  isa<Instruction>(U) &&
1405  "Can't transform non-constantexpr non-instruction to instruction!");
1406  }
1407 
1408  SmallVector<Value*,4> UUsers;
1409  for (auto *U : Users) {
1410  UUsers.clear();
1411  append_range(UUsers, U->users());
1412  for (auto *UU : UUsers) {
1413  Instruction *UI = cast<Instruction>(UU);
1414  Instruction *NewU = U->getAsInstruction(UI);
1415  UI->replaceUsesOfWith(U, NewU);
1416  }
1417  // We've replaced all the uses, so destroy the constant. (destroyConstant
1418  // will update value handles and metadata.)
1419  U->destroyConstant();
1420  }
1421 }
1422 
1423 /// Analyze the specified global variable and optimize
1424 /// it if possible. If we make a change, return true.
1425 static bool
1429  function_ref<DominatorTree &(Function &)> LookupDomTree) {
1430  auto &DL = GV->getParent()->getDataLayout();
1431  // If this is a first class global and has only one accessing function and
1432  // this function is non-recursive, we replace the global with a local alloca
1433  // in this function.
1434  //
1435  // NOTE: It doesn't make sense to promote non-single-value types since we
1436  // are just replacing static memory to stack memory.
1437  //
1438  // If the global is in different address space, don't bring it to stack.
1439  if (!GS.HasMultipleAccessingFunctions &&
1440  GS.AccessingFunction &&
1441  GV->getValueType()->isSingleValueType() &&
1442  GV->getType()->getAddressSpace() == 0 &&
1443  !GV->isExternallyInitialized() &&
1445  GS.AccessingFunction->doesNotRecurse() &&
1446  isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,
1447  LookupDomTree)) {
1448  const DataLayout &DL = GV->getParent()->getDataLayout();
1449 
1450  LLVM_DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
1451  Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
1452  ->getEntryBlock().begin());
1453  Type *ElemTy = GV->getValueType();
1454  // FIXME: Pass Global's alignment when globals have alignment
1455  AllocaInst *Alloca = new AllocaInst(ElemTy, DL.getAllocaAddrSpace(), nullptr,
1456  GV->getName(), &FirstI);
1457  if (!isa<UndefValue>(GV->getInitializer()))
1458  new StoreInst(GV->getInitializer(), Alloca, &FirstI);
1459 
1461 
1462  GV->replaceAllUsesWith(Alloca);
1463  GV->eraseFromParent();
1464  ++NumLocalized;
1465  return true;
1466  }
1467 
1468  bool Changed = false;
1469 
1470  // If the global is never loaded (but may be stored to), it is dead.
1471  // Delete it now.
1472  if (!GS.IsLoaded) {
1473  LLVM_DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");
1474 
1475  if (isLeakCheckerRoot(GV)) {
1476  // Delete any constant stores to the global.
1477  Changed = CleanupPointerRootUsers(GV, GetTLI);
1478  } else {
1479  // Delete any stores we can find to the global. We may not be able to
1480  // make it completely dead though.
1481  Changed = CleanupConstantGlobalUsers(GV, DL);
1482  }
1483 
1484  // If the global is dead now, delete it.
1485  if (GV->use_empty()) {
1486  GV->eraseFromParent();
1487  ++NumDeleted;
1488  Changed = true;
1489  }
1490  return Changed;
1491 
1492  }
1493  if (GS.StoredType <= GlobalStatus::InitializerStored) {
1494  LLVM_DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1495 
1496  // Don't actually mark a global constant if it's atomic because atomic loads
1497  // are implemented by a trivial cmpxchg in some edge-cases and that usually
1498  // requires write access to the variable even if it's not actually changed.
1499  if (GS.Ordering == AtomicOrdering::NotAtomic) {
1500  assert(!GV->isConstant() && "Expected a non-constant global");
1501  GV->setConstant(true);
1502  Changed = true;
1503  }
1504 
1505  // Clean up any obviously simplifiable users now.
1506  Changed |= CleanupConstantGlobalUsers(GV, DL);
1507 
1508  // If the global is dead now, just nuke it.
1509  if (GV->use_empty()) {
1510  LLVM_DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
1511  << "all users and delete global!\n");
1512  GV->eraseFromParent();
1513  ++NumDeleted;
1514  return true;
1515  }
1516 
1517  // Fall through to the next check; see if we can optimize further.
1518  ++NumMarked;
1519  }
1520  if (!GV->getInitializer()->getType()->isSingleValueType()) {
1521  const DataLayout &DL = GV->getParent()->getDataLayout();
1522  if (SRAGlobal(GV, DL))
1523  return true;
1524  }
1525  Value *StoredOnceValue = GS.getStoredOnceValue();
1526  if (GS.StoredType == GlobalStatus::StoredOnce && StoredOnceValue) {
1527  // Avoid speculating constant expressions that might trap (div/rem).
1528  auto *SOVConstant = dyn_cast<Constant>(StoredOnceValue);
1529  if (SOVConstant && SOVConstant->canTrap())
1530  return Changed;
1531 
1532  Function &StoreFn =
1533  const_cast<Function &>(*GS.StoredOnceStore->getFunction());
1534  bool CanHaveNonUndefGlobalInitializer =
1535  GetTTI(StoreFn).canHaveNonUndefGlobalInitializerInAddressSpace(
1536  GV->getType()->getAddressSpace());
1537  // If the initial value for the global was an undef value, and if only
1538  // one other value was stored into it, we can just change the
1539  // initializer to be the stored value, then delete all stores to the
1540  // global. This allows us to mark it constant.
1541  // This is restricted to address spaces that allow globals to have
1542  // initializers. NVPTX, for example, does not support initializers for
1543  // shared memory (AS 3).
1544  if (SOVConstant && isa<UndefValue>(GV->getInitializer()) &&
1545  DL.getTypeAllocSize(SOVConstant->getType()) ==
1546  DL.getTypeAllocSize(GV->getValueType()) &&
1547  CanHaveNonUndefGlobalInitializer) {
1548  if (SOVConstant->getType() == GV->getValueType()) {
1549  // Change the initializer in place.
1550  GV->setInitializer(SOVConstant);
1551  } else {
1552  // Create a new global with adjusted type.
1553  auto *NGV = new GlobalVariable(
1554  *GV->getParent(), SOVConstant->getType(), GV->isConstant(),
1555  GV->getLinkage(), SOVConstant, "", GV, GV->getThreadLocalMode(),
1556  GV->getAddressSpace());
1557  NGV->takeName(GV);
1558  NGV->copyAttributesFrom(GV);
1560  GV->eraseFromParent();
1561  GV = NGV;
1562  }
1563 
1564  // Clean up any obviously simplifiable users now.
1566 
1567  if (GV->use_empty()) {
1568  LLVM_DEBUG(dbgs() << " *** Substituting initializer allowed us to "
1569  << "simplify all users and delete global!\n");
1570  GV->eraseFromParent();
1571  ++NumDeleted;
1572  }
1573  ++NumSubstitute;
1574  return true;
1575  }
1576 
1577  // Try to optimize globals based on the knowledge that only one value
1578  // (besides its initializer) is ever stored to the global.
1579  if (optimizeOnceStoredGlobal(GV, StoredOnceValue, GS.Ordering, DL, GetTLI))
1580  return true;
1581 
1582  // Otherwise, if the global was not a boolean, we can shrink it to be a
1583  // boolean. Skip this optimization for AS that doesn't allow an initializer.
1584  if (SOVConstant && GS.Ordering == AtomicOrdering::NotAtomic &&
1585  (!isa<UndefValue>(GV->getInitializer()) ||
1586  CanHaveNonUndefGlobalInitializer)) {
1587  if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1588  ++NumShrunkToBool;
1589  return true;
1590  }
1591  }
1592  }
1593 
1594  return Changed;
1595 }
1596 
1597 /// Analyze the specified global variable and optimize it if possible. If we
1598 /// make a change, return true.
1599 static bool
1603  function_ref<DominatorTree &(Function &)> LookupDomTree) {
1604  if (GV.getName().startswith("llvm."))
1605  return false;
1606 
1607  GlobalStatus GS;
1608 
1609  if (GlobalStatus::analyzeGlobal(&GV, GS))
1610  return false;
1611 
1612  bool Changed = false;
1613  if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) {
1614  auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global
1616  if (NewUnnamedAddr != GV.getUnnamedAddr()) {
1617  GV.setUnnamedAddr(NewUnnamedAddr);
1618  NumUnnamed++;
1619  Changed = true;
1620  }
1621  }
1622 
1623  // Do more involved optimizations if the global is internal.
1624  if (!GV.hasLocalLinkage())
1625  return Changed;
1626 
1627  auto *GVar = dyn_cast<GlobalVariable>(&GV);
1628  if (!GVar)
1629  return Changed;
1630 
1631  if (GVar->isConstant() || !GVar->hasInitializer())
1632  return Changed;
1633 
1634  return processInternalGlobal(GVar, GS, GetTTI, GetTLI, LookupDomTree) ||
1635  Changed;
1636 }
1637 
1638 /// Walk all of the direct calls of the specified function, changing them to
1639 /// FastCC.
1641  for (User *U : F->users()) {
1642  if (isa<BlockAddress>(U))
1643  continue;
1644  cast<CallBase>(U)->setCallingConv(CallingConv::Fast);
1645  }
1646 }
1647 
1649  Attribute::AttrKind A) {
1650  unsigned AttrIndex;
1651  if (Attrs.hasAttrSomewhere(A, &AttrIndex))
1652  return Attrs.removeAttributeAtIndex(C, AttrIndex, A);
1653  return Attrs;
1654 }
1655 
1657  F->setAttributes(StripAttr(F->getContext(), F->getAttributes(), A));
1658  for (User *U : F->users()) {
1659  if (isa<BlockAddress>(U))
1660  continue;
1661  CallBase *CB = cast<CallBase>(U);
1662  CB->setAttributes(StripAttr(F->getContext(), CB->getAttributes(), A));
1663  }
1664 }
1665 
1666 /// Return true if this is a calling convention that we'd like to change. The
1667 /// idea here is that we don't want to mess with the convention if the user
1668 /// explicitly requested something with performance implications like coldcc,
1669 /// GHC, or anyregcc.
1670 static bool hasChangeableCC(Function *F) {
1671  CallingConv::ID CC = F->getCallingConv();
1672 
1673  // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
1674  if (CC != CallingConv::C && CC != CallingConv::X86_ThisCall)
1675  return false;
1676 
1677  // FIXME: Change CC for the whole chain of musttail calls when possible.
1678  //
1679  // Can't change CC of the function that either has musttail calls, or is a
1680  // musttail callee itself
1681  for (User *U : F->users()) {
1682  if (isa<BlockAddress>(U))
1683  continue;
1684  CallInst* CI = dyn_cast<CallInst>(U);
1685  if (!CI)
1686  continue;
1687 
1688  if (CI->isMustTailCall())
1689  return false;
1690  }
1691 
1692  for (BasicBlock &BB : *F)
1693  if (BB.getTerminatingMustTailCall())
1694  return false;
1695 
1696  return true;
1697 }
1698 
1699 /// Return true if the block containing the call site has a BlockFrequency of
1700 /// less than ColdCCRelFreq% of the entry block.
1701 static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI) {
1702  const BranchProbability ColdProb(ColdCCRelFreq, 100);
1703  auto *CallSiteBB = CB.getParent();
1704  auto CallSiteFreq = CallerBFI.getBlockFreq(CallSiteBB);
1705  auto CallerEntryFreq =
1706  CallerBFI.getBlockFreq(&(CB.getCaller()->getEntryBlock()));
1707  return CallSiteFreq < CallerEntryFreq * ColdProb;
1708 }
1709 
1710 // This function checks if the input function F is cold at all call sites. It
1711 // also looks each call site's containing function, returning false if the
1712 // caller function contains other non cold calls. The input vector AllCallsCold
1713 // contains a list of functions that only have call sites in cold blocks.
1714 static bool
1717  const std::vector<Function *> &AllCallsCold) {
1718 
1719  if (F.user_empty())
1720  return false;
1721 
1722  for (User *U : F.users()) {
1723  if (isa<BlockAddress>(U))
1724  continue;
1725 
1726  CallBase &CB = cast<CallBase>(*U);
1727  Function *CallerFunc = CB.getParent()->getParent();
1728  BlockFrequencyInfo &CallerBFI = GetBFI(*CallerFunc);
1729  if (!isColdCallSite(CB, CallerBFI))
1730  return false;
1731  if (!llvm::is_contained(AllCallsCold, CallerFunc))
1732  return false;
1733  }
1734  return true;
1735 }
1736 
1738  for (User *U : F->users()) {
1739  if (isa<BlockAddress>(U))
1740  continue;
1741  cast<CallBase>(U)->setCallingConv(CallingConv::Cold);
1742  }
1743 }
1744 
1745 // This function iterates over all the call instructions in the input Function
1746 // and checks that all call sites are in cold blocks and are allowed to use the
1747 // coldcc calling convention.
1748 static bool
1750  function_ref<BlockFrequencyInfo &(Function &)> GetBFI) {
1751  for (BasicBlock &BB : F) {
1752  for (Instruction &I : BB) {
1753  if (CallInst *CI = dyn_cast<CallInst>(&I)) {
1754  // Skip over isline asm instructions since they aren't function calls.
1755  if (CI->isInlineAsm())
1756  continue;
1757  Function *CalledFn = CI->getCalledFunction();
1758  if (!CalledFn)
1759  return false;
1760  if (!CalledFn->hasLocalLinkage())
1761  return false;
1762  // Skip over instrinsics since they won't remain as function calls.
1763  if (CalledFn->getIntrinsicID() != Intrinsic::not_intrinsic)
1764  continue;
1765  // Check if it's valid to use coldcc calling convention.
1766  if (!hasChangeableCC(CalledFn) || CalledFn->isVarArg() ||
1767  CalledFn->hasAddressTaken())
1768  return false;
1769  BlockFrequencyInfo &CallerBFI = GetBFI(F);
1770  if (!isColdCallSite(*CI, CallerBFI))
1771  return false;
1772  }
1773  }
1774  }
1775  return true;
1776 }
1777 
1779  for (User *U : F->users()) {
1780  CallBase *CB = dyn_cast<CallBase>(U);
1781  if (!CB) {
1782  assert(isa<BlockAddress>(U) &&
1783  "Expected either CallBase or BlockAddress");
1784  continue;
1785  }
1786  if (CB->isMustTailCall())
1787  return true;
1788  }
1789  return false;
1790 }
1791 
1792 static bool hasInvokeCallers(Function *F) {
1793  for (User *U : F->users())
1794  if (isa<InvokeInst>(U))
1795  return true;
1796  return false;
1797 }
1798 
1800  RemoveAttribute(F, Attribute::Preallocated);
1801 
1802  auto *M = F->getParent();
1803 
1804  IRBuilder<> Builder(M->getContext());
1805 
1806  // Cannot modify users() while iterating over it, so make a copy.
1807  SmallVector<User *, 4> PreallocatedCalls(F->users());
1808  for (User *U : PreallocatedCalls) {
1809  CallBase *CB = dyn_cast<CallBase>(U);
1810  if (!CB)
1811  continue;
1812 
1813  assert(
1814  !CB->isMustTailCall() &&
1815  "Shouldn't call RemotePreallocated() on a musttail preallocated call");
1816  // Create copy of call without "preallocated" operand bundle.
1818  CB->getOperandBundlesAsDefs(OpBundles);
1819  CallBase *PreallocatedSetup = nullptr;
1820  for (auto *It = OpBundles.begin(); It != OpBundles.end(); ++It) {
1821  if (It->getTag() == "preallocated") {
1822  PreallocatedSetup = cast<CallBase>(*It->input_begin());
1823  OpBundles.erase(It);
1824  break;
1825  }
1826  }
1827  assert(PreallocatedSetup && "Did not find preallocated bundle");
1828  uint64_t ArgCount =
1829  cast<ConstantInt>(PreallocatedSetup->getArgOperand(0))->getZExtValue();
1830 
1831  assert((isa<CallInst>(CB) || isa<InvokeInst>(CB)) &&
1832  "Unknown indirect call type");
1833  CallBase *NewCB = CallBase::Create(CB, OpBundles, CB);
1834  CB->replaceAllUsesWith(NewCB);
1835  NewCB->takeName(CB);
1836  CB->eraseFromParent();
1837 
1838  Builder.SetInsertPoint(PreallocatedSetup);
1839  auto *StackSave =
1840  Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stacksave));
1841 
1842  Builder.SetInsertPoint(NewCB->getNextNonDebugInstruction());
1843  Builder.CreateCall(Intrinsic::getDeclaration(M, Intrinsic::stackrestore),
1844  StackSave);
1845 
1846  // Replace @llvm.call.preallocated.arg() with alloca.
1847  // Cannot modify users() while iterating over it, so make a copy.
1848  // @llvm.call.preallocated.arg() can be called with the same index multiple
1849  // times. So for each @llvm.call.preallocated.arg(), we see if we have
1850  // already created a Value* for the index, and if not, create an alloca and
1851  // bitcast right after the @llvm.call.preallocated.setup() so that it
1852  // dominates all uses.
1853  SmallVector<Value *, 2> ArgAllocas(ArgCount);
1854  SmallVector<User *, 2> PreallocatedArgs(PreallocatedSetup->users());
1855  for (auto *User : PreallocatedArgs) {
1856  auto *UseCall = cast<CallBase>(User);
1857  assert(UseCall->getCalledFunction()->getIntrinsicID() ==
1858  Intrinsic::call_preallocated_arg &&
1859  "preallocated token use was not a llvm.call.preallocated.arg");
1860  uint64_t AllocArgIndex =
1861  cast<ConstantInt>(UseCall->getArgOperand(1))->getZExtValue();
1862  Value *AllocaReplacement = ArgAllocas[AllocArgIndex];
1863  if (!AllocaReplacement) {
1864  auto AddressSpace = UseCall->getType()->getPointerAddressSpace();
1865  auto *ArgType =
1866  UseCall->getFnAttr(Attribute::Preallocated).getValueAsType();
1867  auto *InsertBefore = PreallocatedSetup->getNextNonDebugInstruction();
1868  Builder.SetInsertPoint(InsertBefore);
1869  auto *Alloca =
1870  Builder.CreateAlloca(ArgType, AddressSpace, nullptr, "paarg");
1871  auto *BitCast = Builder.CreateBitCast(
1872  Alloca, Type::getInt8PtrTy(M->getContext()), UseCall->getName());
1873  ArgAllocas[AllocArgIndex] = BitCast;
1874  AllocaReplacement = BitCast;
1875  }
1876 
1877  UseCall->replaceAllUsesWith(AllocaReplacement);
1878  UseCall->eraseFromParent();
1879  }
1880  // Remove @llvm.call.preallocated.setup().
1881  cast<Instruction>(PreallocatedSetup)->eraseFromParent();
1882  }
1883 }
1884 
1885 static bool
1890  function_ref<DominatorTree &(Function &)> LookupDomTree,
1891  SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
1892 
1893  bool Changed = false;
1894 
1895  std::vector<Function *> AllCallsCold;
1897  if (hasOnlyColdCalls(F, GetBFI))
1898  AllCallsCold.push_back(&F);
1899 
1900  // Optimize functions.
1902  // Don't perform global opt pass on naked functions; we don't want fast
1903  // calling conventions for naked functions.
1904  if (F.hasFnAttribute(Attribute::Naked))
1905  continue;
1906 
1907  // Functions without names cannot be referenced outside this module.
1908  if (!F.hasName() && !F.isDeclaration() && !F.hasLocalLinkage())
1909  F.setLinkage(GlobalValue::InternalLinkage);
1910 
1911  if (deleteIfDead(F, NotDiscardableComdats)) {
1912  Changed = true;
1913  continue;
1914  }
1915 
1916  // LLVM's definition of dominance allows instructions that are cyclic
1917  // in unreachable blocks, e.g.:
1918  // %pat = select i1 %condition, @global, i16* %pat
1919  // because any instruction dominates an instruction in a block that's
1920  // not reachable from entry.
1921  // So, remove unreachable blocks from the function, because a) there's
1922  // no point in analyzing them and b) GlobalOpt should otherwise grow
1923  // some more complicated logic to break these cycles.
1924  // Removing unreachable blocks might invalidate the dominator so we
1925  // recalculate it.
1926  if (!F.isDeclaration()) {
1927  if (removeUnreachableBlocks(F)) {
1928  auto &DT = LookupDomTree(F);
1929  DT.recalculate(F);
1930  Changed = true;
1931  }
1932  }
1933 
1934  Changed |= processGlobal(F, GetTTI, GetTLI, LookupDomTree);
1935 
1936  if (!F.hasLocalLinkage())
1937  continue;
1938 
1939  // If we have an inalloca parameter that we can safely remove the
1940  // inalloca attribute from, do so. This unlocks optimizations that
1941  // wouldn't be safe in the presence of inalloca.
1942  // FIXME: We should also hoist alloca affected by this to the entry
1943  // block if possible.
1944  if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) &&
1945  !F.hasAddressTaken() && !hasMustTailCallers(&F)) {
1946  RemoveAttribute(&F, Attribute::InAlloca);
1947  Changed = true;
1948  }
1949 
1950  // FIXME: handle invokes
1951  // FIXME: handle musttail
1952  if (F.getAttributes().hasAttrSomewhere(Attribute::Preallocated)) {
1953  if (!F.hasAddressTaken() && !hasMustTailCallers(&F) &&
1954  !hasInvokeCallers(&F)) {
1956  Changed = true;
1957  }
1958  continue;
1959  }
1960 
1961  if (hasChangeableCC(&F) && !F.isVarArg() && !F.hasAddressTaken()) {
1962  NumInternalFunc++;
1963  TargetTransformInfo &TTI = GetTTI(F);
1964  // Change the calling convention to coldcc if either stress testing is
1965  // enabled or the target would like to use coldcc on functions which are
1966  // cold at all call sites and the callers contain no other non coldcc
1967  // calls.
1968  if (EnableColdCCStressTest ||
1970  isValidCandidateForColdCC(F, GetBFI, AllCallsCold))) {
1971  F.setCallingConv(CallingConv::Cold);
1973  Changed = true;
1974  NumColdCC++;
1975  }
1976  }
1977 
1978  if (hasChangeableCC(&F) && !F.isVarArg() && !F.hasAddressTaken()) {
1979  // If this function has a calling convention worth changing, is not a
1980  // varargs function, and is only called directly, promote it to use the
1981  // Fast calling convention.
1982  F.setCallingConv(CallingConv::Fast);
1984  ++NumFastCallFns;
1985  Changed = true;
1986  }
1987 
1988  if (F.getAttributes().hasAttrSomewhere(Attribute::Nest) &&
1989  !F.hasAddressTaken()) {
1990  // The function is not used by a trampoline intrinsic, so it is safe
1991  // to remove the 'nest' attribute.
1992  RemoveAttribute(&F, Attribute::Nest);
1993  ++NumNestRemoved;
1994  Changed = true;
1995  }
1996  }
1997  return Changed;
1998 }
1999 
2000 static bool
2004  function_ref<DominatorTree &(Function &)> LookupDomTree,
2005  SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
2006  bool Changed = false;
2007 
2008  for (GlobalVariable &GV : llvm::make_early_inc_range(M.globals())) {
2009  // Global variables without names cannot be referenced outside this module.
2010  if (!GV.hasName() && !GV.isDeclaration() && !GV.hasLocalLinkage())
2012  // Simplify the initializer.
2013  if (GV.hasInitializer())
2014  if (auto *C = dyn_cast<Constant>(GV.getInitializer())) {
2015  auto &DL = M.getDataLayout();
2016  // TLI is not used in the case of a Constant, so use default nullptr
2017  // for that optional parameter, since we don't have a Function to
2018  // provide GetTLI anyway.
2019  Constant *New = ConstantFoldConstant(C, DL, /*TLI*/ nullptr);
2020  if (New != C)
2021  GV.setInitializer(New);
2022  }
2023 
2024  if (deleteIfDead(GV, NotDiscardableComdats)) {
2025  Changed = true;
2026  continue;
2027  }
2028 
2029  Changed |= processGlobal(GV, GetTTI, GetTLI, LookupDomTree);
2030  }
2031  return Changed;
2032 }
2033 
2034 /// Evaluate static constructors in the function, if we can. Return true if we
2035 /// can, false otherwise.
2037  TargetLibraryInfo *TLI) {
2038  // Call the function.
2039  Evaluator Eval(DL, TLI);
2040  Constant *RetValDummy;
2041  bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2043 
2044  if (EvalSuccess) {
2045  ++NumCtorsEvaluated;
2046 
2047  // We succeeded at evaluation: commit the result.
2048  auto NewInitializers = Eval.getMutatedInitializers();
2049  LLVM_DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2050  << F->getName() << "' to " << NewInitializers.size()
2051  << " stores.\n");
2052  for (const auto &Pair : NewInitializers)
2053  Pair.first->setInitializer(Pair.second);
2054  for (GlobalVariable *GV : Eval.getInvariants())
2055  GV->setConstant(true);
2056  }
2057 
2058  return EvalSuccess;
2059 }
2060 
2061 static int compareNames(Constant *const *A, Constant *const *B) {
2062  Value *AStripped = (*A)->stripPointerCasts();
2063  Value *BStripped = (*B)->stripPointerCasts();
2064  return AStripped->getName().compare(BStripped->getName());
2065 }
2066 
2069  if (Init.empty()) {
2070  V.eraseFromParent();
2071  return;
2072  }
2073 
2074  // Type of pointer to the array of pointers.
2075  PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
2076 
2077  SmallVector<Constant *, 8> UsedArray;
2078  for (GlobalValue *GV : Init) {
2079  Constant *Cast
2081  UsedArray.push_back(Cast);
2082  }
2083  // Sort to get deterministic order.
2084  array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
2085  ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
2086 
2087  Module *M = V.getParent();
2088  V.removeFromParent();
2089  GlobalVariable *NV =
2091  ConstantArray::get(ATy, UsedArray), "");
2092  NV->takeName(&V);
2093  NV->setSection("llvm.metadata");
2094  delete &V;
2095 }
2096 
2097 namespace {
2098 
2099 /// An easy to access representation of llvm.used and llvm.compiler.used.
2100 class LLVMUsed {
2102  SmallPtrSet<GlobalValue *, 4> CompilerUsed;
2103  GlobalVariable *UsedV;
2104  GlobalVariable *CompilerUsedV;
2105 
2106 public:
2107  LLVMUsed(Module &M) {
2109  UsedV = collectUsedGlobalVariables(M, Vec, false);
2110  Used = {Vec.begin(), Vec.end()};
2111  Vec.clear();
2112  CompilerUsedV = collectUsedGlobalVariables(M, Vec, true);
2113  CompilerUsed = {Vec.begin(), Vec.end()};
2114  }
2115 
2116  using iterator = SmallPtrSet<GlobalValue *, 4>::iterator;
2117  using used_iterator_range = iterator_range<iterator>;
2118 
2119  iterator usedBegin() { return Used.begin(); }
2120  iterator usedEnd() { return Used.end(); }
2121 
2122  used_iterator_range used() {
2123  return used_iterator_range(usedBegin(), usedEnd());
2124  }
2125 
2126  iterator compilerUsedBegin() { return CompilerUsed.begin(); }
2127  iterator compilerUsedEnd() { return CompilerUsed.end(); }
2128 
2129  used_iterator_range compilerUsed() {
2130  return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
2131  }
2132 
2133  bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2134 
2135  bool compilerUsedCount(GlobalValue *GV) const {
2136  return CompilerUsed.count(GV);
2137  }
2138 
2139  bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2140  bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
2141  bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
2142 
2143  bool compilerUsedInsert(GlobalValue *GV) {
2144  return CompilerUsed.insert(GV).second;
2145  }
2146 
2147  void syncVariablesAndSets() {
2148  if (UsedV)
2149  setUsedInitializer(*UsedV, Used);
2150  if (CompilerUsedV)
2151  setUsedInitializer(*CompilerUsedV, CompilerUsed);
2152  }
2153 };
2154 
2155 } // end anonymous namespace
2156 
2157 static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
2158  if (GA.use_empty()) // No use at all.
2159  return false;
2160 
2161  assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
2162  "We should have removed the duplicated "
2163  "element from llvm.compiler.used");
2164  if (!GA.hasOneUse())
2165  // Strictly more than one use. So at least one is not in llvm.used and
2166  // llvm.compiler.used.
2167  return true;
2168 
2169  // Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
2170  return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
2171 }
2172 
2174  const LLVMUsed &U) {
2175  unsigned N = 2;
2176  assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
2177  "We should have removed the duplicated "
2178  "element from llvm.compiler.used");
2179  if (U.usedCount(&V) || U.compilerUsedCount(&V))
2180  ++N;
2181  return V.hasNUsesOrMore(N);
2182 }
2183 
2184 static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
2185  if (!GA.hasLocalLinkage())
2186  return true;
2187 
2188  return U.usedCount(&GA) || U.compilerUsedCount(&GA);
2189 }
2190 
2191 static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
2192  bool &RenameTarget) {
2193  RenameTarget = false;
2194  bool Ret = false;
2195  if (hasUseOtherThanLLVMUsed(GA, U))
2196  Ret = true;
2197 
2198  // If the alias is externally visible, we may still be able to simplify it.
2199  if (!mayHaveOtherReferences(GA, U))
2200  return Ret;
2201 
2202  // If the aliasee has internal linkage, give it the name and linkage
2203  // of the alias, and delete the alias. This turns:
2204  // define internal ... @f(...)
2205  // @a = alias ... @f
2206  // into:
2207  // define ... @a(...)
2208  Constant *Aliasee = GA.getAliasee();
2209  GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2210  if (!Target->hasLocalLinkage())
2211  return Ret;
2212 
2213  // Do not perform the transform if multiple aliases potentially target the
2214  // aliasee. This check also ensures that it is safe to replace the section
2215  // and other attributes of the aliasee with those of the alias.
2217  return Ret;
2218 
2219  RenameTarget = true;
2220  return true;
2221 }
2222 
2223 static bool
2225  SmallPtrSetImpl<const Comdat *> &NotDiscardableComdats) {
2226  bool Changed = false;
2227  LLVMUsed Used(M);
2228 
2229  for (GlobalValue *GV : Used.used())
2230  Used.compilerUsedErase(GV);
2231 
2232  for (GlobalAlias &J : llvm::make_early_inc_range(M.aliases())) {
2233  // Aliases without names cannot be referenced outside this module.
2234  if (!J.hasName() && !J.isDeclaration() && !J.hasLocalLinkage())
2235  J.setLinkage(GlobalValue::InternalLinkage);
2236 
2237  if (deleteIfDead(J, NotDiscardableComdats)) {
2238  Changed = true;
2239  continue;
2240  }
2241 
2242  // If the alias can change at link time, nothing can be done - bail out.
2243  if (J.isInterposable())
2244  continue;
2245 
2246  Constant *Aliasee = J.getAliasee();
2247  GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
2248  // We can't trivially replace the alias with the aliasee if the aliasee is
2249  // non-trivial in some way. We also can't replace the alias with the aliasee
2250  // if the aliasee is interposable because aliases point to the local
2251  // definition.
2252  // TODO: Try to handle non-zero GEPs of local aliasees.
2253  if (!Target || Target->isInterposable())
2254  continue;
2255  Target->removeDeadConstantUsers();
2256 
2257  // Make all users of the alias use the aliasee instead.
2258  bool RenameTarget;
2259  if (!hasUsesToReplace(J, Used, RenameTarget))
2260  continue;
2261 
2262  J.replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J.getType()));
2263  ++NumAliasesResolved;
2264  Changed = true;
2265 
2266  if (RenameTarget) {
2267  // Give the aliasee the name, linkage and other attributes of the alias.
2268  Target->takeName(&J);
2269  Target->setLinkage(J.getLinkage());
2270  Target->setDSOLocal(J.isDSOLocal());
2271  Target->setVisibility(J.getVisibility());
2272  Target->setDLLStorageClass(J.getDLLStorageClass());
2273 
2274  if (Used.usedErase(&J))
2275  Used.usedInsert(Target);
2276 
2277  if (Used.compilerUsedErase(&J))
2278  Used.compilerUsedInsert(Target);
2279  } else if (mayHaveOtherReferences(J, Used))
2280  continue;
2281 
2282  // Delete the alias.
2283  M.getAliasList().erase(&J);
2284  ++NumAliasesRemoved;
2285  Changed = true;
2286  }
2287 
2288  Used.syncVariablesAndSets();
2289 
2290  return Changed;
2291 }
2292 
2293 static Function *
2295  // Hack to get a default TLI before we have actual Function.
2296  auto FuncIter = M.begin();
2297  if (FuncIter == M.end())
2298  return nullptr;
2299  auto *TLI = &GetTLI(*FuncIter);
2300 
2301  LibFunc F = LibFunc_cxa_atexit;
2302  if (!TLI->has(F))
2303  return nullptr;
2304 
2305  Function *Fn = M.getFunction(TLI->getName(F));
2306  if (!Fn)
2307  return nullptr;
2308 
2309  // Now get the actual TLI for Fn.
2310  TLI = &GetTLI(*Fn);
2311 
2312  // Make sure that the function has the correct prototype.
2313  if (!TLI->getLibFunc(*Fn, F) || F != LibFunc_cxa_atexit)
2314  return nullptr;
2315 
2316  return Fn;
2317 }
2318 
2319 /// Returns whether the given function is an empty C++ destructor and can
2320 /// therefore be eliminated.
2321 /// Note that we assume that other optimization passes have already simplified
2322 /// the code so we simply check for 'ret'.
2323 static bool cxxDtorIsEmpty(const Function &Fn) {
2324  // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
2325  // nounwind, but that doesn't seem worth doing.
2326  if (Fn.isDeclaration())
2327  return false;
2328 
2329  for (auto &I : Fn.getEntryBlock()) {
2330  if (I.isDebugOrPseudoInst())
2331  continue;
2332  if (isa<ReturnInst>(I))
2333  return true;
2334  break;
2335  }
2336  return false;
2337 }
2338 
2339 static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
2340  /// Itanium C++ ABI p3.3.5:
2341  ///
2342  /// After constructing a global (or local static) object, that will require
2343  /// destruction on exit, a termination function is registered as follows:
2344  ///
2345  /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
2346  ///
2347  /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
2348  /// call f(p) when DSO d is unloaded, before all such termination calls
2349  /// registered before this one. It returns zero if registration is
2350  /// successful, nonzero on failure.
2351 
2352  // This pass will look for calls to __cxa_atexit where the function is trivial
2353  // and remove them.
2354  bool Changed = false;
2355 
2356  for (User *U : llvm::make_early_inc_range(CXAAtExitFn->users())) {
2357  // We're only interested in calls. Theoretically, we could handle invoke
2358  // instructions as well, but neither llvm-gcc nor clang generate invokes
2359  // to __cxa_atexit.
2360  CallInst *CI = dyn_cast<CallInst>(U);
2361  if (!CI)
2362  continue;
2363 
2364  Function *DtorFn =
2365  dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
2366  if (!DtorFn || !cxxDtorIsEmpty(*DtorFn))
2367  continue;
2368 
2369  // Just remove the call.
2371  CI->eraseFromParent();
2372 
2373  ++NumCXXDtorsRemoved;
2374 
2375  Changed |= true;
2376  }
2377 
2378  return Changed;
2379 }
2380 
2382  Module &M, const DataLayout &DL,
2386  function_ref<DominatorTree &(Function &)> LookupDomTree) {
2387  SmallPtrSet<const Comdat *, 8> NotDiscardableComdats;
2388  bool Changed = false;
2389  bool LocalChange = true;
2390  while (LocalChange) {
2391  LocalChange = false;
2392 
2393  NotDiscardableComdats.clear();
2394  for (const GlobalVariable &GV : M.globals())
2395  if (const Comdat *C = GV.getComdat())
2396  if (!GV.isDiscardableIfUnused() || !GV.use_empty())
2397  NotDiscardableComdats.insert(C);
2398  for (Function &F : M)
2399  if (const Comdat *C = F.getComdat())
2400  if (!F.isDefTriviallyDead())
2401  NotDiscardableComdats.insert(C);
2402  for (GlobalAlias &GA : M.aliases())
2403  if (const Comdat *C = GA.getComdat())
2404  if (!GA.isDiscardableIfUnused() || !GA.use_empty())
2405  NotDiscardableComdats.insert(C);
2406 
2407  // Delete functions that are trivially dead, ccc -> fastcc
2408  LocalChange |= OptimizeFunctions(M, GetTLI, GetTTI, GetBFI, LookupDomTree,
2409  NotDiscardableComdats);
2410 
2411  // Optimize global_ctors list.
2412  LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
2413  return EvaluateStaticConstructor(F, DL, &GetTLI(*F));
2414  });
2415 
2416  // Optimize non-address-taken globals.
2417  LocalChange |= OptimizeGlobalVars(M, GetTTI, GetTLI, LookupDomTree,
2418  NotDiscardableComdats);
2419 
2420  // Resolve aliases, when possible.
2421  LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats);
2422 
2423  // Try to remove trivial global destructors if they are not removed
2424  // already.
2425  Function *CXAAtExitFn = FindCXAAtExit(M, GetTLI);
2426  if (CXAAtExitFn)
2427  LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
2428 
2429  Changed |= LocalChange;
2430  }
2431 
2432  // TODO: Move all global ctors functions to the end of the module for code
2433  // layout.
2434 
2435  return Changed;
2436 }
2437 
2439  auto &DL = M.getDataLayout();
2440  auto &FAM =
2442  auto LookupDomTree = [&FAM](Function &F) -> DominatorTree &{
2444  };
2445  auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
2447  };
2448  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
2449  return FAM.getResult<TargetIRAnalysis>(F);
2450  };
2451 
2452  auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
2454  };
2455 
2456  if (!optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI, LookupDomTree))
2457  return PreservedAnalyses::all();
2458  return PreservedAnalyses::none();
2459 }
2460 
2461 namespace {
2462 
2463 struct GlobalOptLegacyPass : public ModulePass {
2464  static char ID; // Pass identification, replacement for typeid
2465 
2466  GlobalOptLegacyPass() : ModulePass(ID) {
2468  }
2469 
2470  bool runOnModule(Module &M) override {
2471  if (skipModule(M))
2472  return false;
2473 
2474  auto &DL = M.getDataLayout();
2475  auto LookupDomTree = [this](Function &F) -> DominatorTree & {
2476  return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
2477  };
2478  auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
2479  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
2480  };
2481  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
2482  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
2483  };
2484 
2485  auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
2486  return this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
2487  };
2488 
2489  return optimizeGlobalsInModule(M, DL, GetTLI, GetTTI, GetBFI,
2490  LookupDomTree);
2491  }
2492 
2493  void getAnalysisUsage(AnalysisUsage &AU) const override {
2498  }
2499 };
2500 
2501 } // end anonymous namespace
2502 
2503 char GlobalOptLegacyPass::ID = 0;
2504 
2505 INITIALIZE_PASS_BEGIN(GlobalOptLegacyPass, "globalopt",
2506  "Global Variable Optimizer", false, false)
2511 INITIALIZE_PASS_END(GlobalOptLegacyPass, "globalopt",
2512  "Global Variable Optimizer", false, false)
2513 
2515  return new GlobalOptLegacyPass();
2516 }
allNonInstructionUsersCanBeMadeInstructions
static bool allNonInstructionUsersCanBeMadeInstructions(Constant *C)
C may have non-instruction users.
Definition: GlobalOpt.cpp:1369
SRAGlobal
static GlobalVariable * SRAGlobal(GlobalVariable *GV, const DataLayout &DL)
Perform scalar replacement of aggregates on the specified global variable.
Definition: GlobalOpt.cpp:431
i
i
Definition: README.txt:29
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::Type::ArrayTyID
@ ArrayTyID
Arrays.
Definition: Type.h:75
llvm::array_pod_sort
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1548
llvm::RecursivelyDeleteTriviallyDeadInstructions
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:523
llvm::GlobalVariable::eraseFromParent
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:430
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:285
Attrs
Function Attrs
Definition: README_ALTIVEC.txt:215
llvm::TargetIRAnalysis
Analysis pass providing the TargetTransformInfo.
Definition: TargetTransformInfo.h:2418
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition: SmallVector.h:714
processGlobal
static bool processGlobal(GlobalValue &GV, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
Analyze the specified global variable and optimize it if possible.
Definition: GlobalOpt.cpp:1600
llvm::Evaluator::EvaluateFunction
bool EvaluateFunction(Function *F, Constant *&RetVal, const SmallVectorImpl< Constant * > &ActualArgs)
Evaluate a call to function F, returning true if successful, false if we can't evaluate it.
Definition: Evaluator.cpp:643
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:375
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:238
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::DIExpression::createFragmentExpression
static Optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Definition: DebugInfoMetadata.cpp:1457
llvm::CmpInst::ICMP_EQ
@ ICMP_EQ
equal
Definition: InstrTypes.h:742
llvm::none_of
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1663
FindCXAAtExit
static Function * FindCXAAtExit(Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
Definition: GlobalOpt.cpp:2294
llvm::SmallPtrSetImpl::erase
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:378
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::X86AS::GS
@ GS
Definition: X86.h:187
llvm::Value::hasOneUse
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
llvm::Intrinsic::getDeclaration
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1399
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:293
llvm::GlobalVariable::getDebugInfo
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1558
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
isColdCallSite
static bool isColdCallSite(CallBase &CB, BlockFrequencyInfo &CallerBFI)
Return true if the block containing the call site has a BlockFrequency of less than ColdCCRelFreq% of...
Definition: GlobalOpt.cpp:1701
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:107
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:217
llvm::RegState::Dead
@ Dead
Unused definition.
Definition: MachineInstrBuilder.h:50
llvm::GlobalVariable::setConstant
void setConstant(bool Val)
Definition: GlobalVariable.h:153
AtomicOrdering.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:783
DebugInfoMetadata.h
llvm::GlobalVariable::copyAttributesFrom
void copyAttributesFrom(const GlobalVariable *Src)
copyAttributesFrom - copy all additional attributes (those not needed to create a GlobalVariable) fro...
Definition: Globals.cpp:457
llvm::Instruction::getNextNonDebugInstruction
const Instruction * getNextNonDebugInstruction(bool SkipPseudoOp=false) const
Return a pointer to the next non-debug instruction in the same basic block as 'this',...
Definition: Instruction.cpp:738
llvm::optimizeGlobalCtorsList
bool optimizeGlobalCtorsList(Module &M, function_ref< bool(Function *)> ShouldRemove)
Call "ShouldRemove" for every entry in M's global_ctor list and remove the entries for which it retur...
Definition: CtorUtils.cpp:116
llvm::GlobalValue::getLinkage
LinkageTypes getLinkage() const
Definition: GlobalValue.h:467
llvm::MemTransferInst
This class wraps the llvm.memcpy/memmove intrinsics.
Definition: IntrinsicInst.h:969
llvm::CallingConv::X86_ThisCall
@ X86_ThisCall
X86_ThisCall - Similar to X86_StdCall.
Definition: CallingConv.h:126
llvm::Function
Definition: Function.h:62
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
Pass.h
llvm::BinaryOperator::CreateNot
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
Definition: Instructions.cpp:2806
llvm::BlockFrequencyInfoWrapperPass
Legacy analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:138
llvm::StructType::element_iterator
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:315
llvm::BitCastInst
This class represents a no-op cast from one type to another.
Definition: Instructions.h:5218
GetElementPtrTypeIterator.h
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::ilist_node_with_parent::getNextNode
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition: ilist_node.h:288
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1177
Statistic.h
llvm::GlobalObject::getAlign
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
llvm::GlobalVariable::isExternallyInitialized
bool isExternallyInitialized() const
Definition: GlobalVariable.h:155
llvm::Value::hasName
bool hasName() const
Definition: Value.h:261
OptimizeAwayTrappingUsesOfLoads
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
The specified global has only one non-null value stored into it.
Definition: GlobalOpt.cpp:752
ErrorHandling.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:734
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition: Function.h:707
llvm::IRBuilder<>
hasChangeableCC
static bool hasChangeableCC(Function *F)
Return true if this is a calling convention that we'd like to change.
Definition: GlobalOpt.cpp:1670
llvm::GlobalVariable
Definition: GlobalVariable.h:39
llvm::PointerType::getAddressSpace
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:687
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition: InstrTypes.h:743
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2282
llvm::GlobalAlias
Definition: GlobalAlias.h:28
ValueTracking.h
Local.h
llvm::getInitialValueOfAllocation
Constant * getInitialValueOfAllocation(const CallBase *Alloc, const TargetLibraryInfo *TLI, Type *Ty)
If this allocation function initializes memory to a fixed value, return said value in the requested t...
Definition: MemoryBuiltins.cpp:387
llvm::Type::getTypeID
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:135
changeCallSitesToColdCC
static void changeCallSitesToColdCC(Function *F)
Definition: GlobalOpt.cpp:1737
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::getLoadStoreType
Type * getLoadStoreType(Value *I)
A helper function that returns the type of a load or store instruction.
Definition: Instructions.h:5364
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::begin
iterator begin()
Definition: DenseMap.h:74
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::TargetTransformInfo::useColdCCForColdCall
bool useColdCCForColdCall(Function &F) const
Return true if the input function which is cold at all call sites, should use coldcc calling conventi...
Definition: TargetTransformInfo.cpp:490
DenseMap.h
llvm::removeUnreachableBlocks
bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Remove all blocks that can not be reached from the function's entry.
Definition: Local.cpp:2466
Module.h
MemoryBuiltins.h
llvm::AttributeList
Definition: Attributes.h:404
llvm::getOrEnforceKnownAlignment
Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to ensure that the alignment of V is at least PrefAlign bytes.
Definition: Local.cpp:1362
llvm::CallBase::getAttributes
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1473
llvm::GlobalValue::UnnamedAddr::Global
@ Global
llvm::CallBase::isMustTailCall
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Definition: Instructions.cpp:298
llvm::MemIntrinsic
This is the common base class for memset/memcpy/memmove.
Definition: IntrinsicInst.h:926
AllUsesOfValueWillTrapIfNull
static bool AllUsesOfValueWillTrapIfNull(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs)
Return true if all users of the specified value will trap if the value is dynamically null.
Definition: GlobalOpt.cpp:575
llvm::GlobalStatus::analyzeGlobal
static bool analyzeGlobal(const Value *V, GlobalStatus &GS)
Look at all uses of the global and fill in the GlobalStatus structure.
Definition: GlobalStatus.cpp:187
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.
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
llvm::ore::NV
DiagnosticInfoOptimizationBase::Argument NV
Definition: OptimizationRemarkEmitter.h:136
Operator.h
llvm::Value::user_begin
user_iterator user_begin()
Definition: Value.h:397
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:894
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
llvm::Intrinsic::not_intrinsic
@ not_intrinsic
Definition: Intrinsics.h:45
STLExtras.h
OptimizeGlobalAliases
static bool OptimizeGlobalAliases(Module &M, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:2224
llvm::GlobalValue::setUnnamedAddr
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:212
CtorUtils.h
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:644
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2596
TryToShrinkGlobalToBoolean
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal)
At this point, we have learned that the only two values ever stored into GV are its initializer and O...
Definition: GlobalOpt.cpp:1107
llvm::ArrayType
Class to represent array types.
Definition: DerivedTypes.h:357
llvm::Type::isFloatingPointTy
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
ChangeCalleesToFastCall
static void ChangeCalleesToFastCall(Function *F)
Walk all of the direct calls of the specified function, changing them to FastCC.
Definition: GlobalOpt.cpp:1640
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
Definition: Type.cpp:239
ConstantFolding.h
Use.h
llvm::Module::getGlobalList
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
Definition: Module.h:545
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(GlobalOptLegacyPass, "globalopt", "Global Variable Optimizer", false, false) INITIALIZE_PASS_END(GlobalOptLegacyPass
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1233
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::collectUsedGlobalVariables
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: Module.cpp:782
llvm::initializeGlobalOptLegacyPassPass
void initializeGlobalOptLegacyPassPass(PassRegistry &)
Uses
SmallPtrSet< MachineInstr *, 2 > Uses
Definition: ARMLowOverheadLoops.cpp:583
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::isAllocationFn
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
Definition: MemoryBuiltins.cpp:229
Instruction.h
CommandLine.h
llvm::GlobalValue::UnnamedAddr::Local
@ Local
llvm::Intrinsic::getType
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
Definition: Function.cpp:1355
llvm::Type::isSingleValueType
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
Definition: Type.h:248
llvm::GlobalStatus
As we analyze each global, keep track of some information about it.
Definition: GlobalStatus.h:30
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::SelectInst::Create
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
Definition: Instructions.h:1772
GlobalValue.h
ConstantPropUsersOf
static void ConstantPropUsersOf(Value *V, const DataLayout &DL, TargetLibraryInfo *TLI)
Walk the use list of V, constant folding all of the instructions that are foldable.
Definition: GlobalOpt.cpp:816
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::GlobalValue::isDeclaration
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:243
Constants.h
llvm::CallingConv::Fast
@ Fast
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:42
llvm::Constant::isNullValue
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:74
EnableColdCCStressTest
static cl::opt< bool > EnableColdCCStressTest("enable-coldcc-stress-test", cl::desc("Enable stress test of coldcc by adding " "calling conv to all internal functions."), cl::init(false), cl::Hidden)
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
CleanupConstantGlobalUsers
static bool CleanupConstantGlobalUsers(GlobalVariable *GV, const DataLayout &DL)
We just marked GV constant.
Definition: GlobalOpt.cpp:279
llvm::User
Definition: User.h:44
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::CmpInst::ICMP_ULE
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:747
valueIsOnlyUsedLocallyOrStoredToOneGlobal
static bool valueIsOnlyUsedLocallyOrStoredToOneGlobal(const CallInst *CI, const GlobalVariable *GV)
Scan the use-list of GV checking to make sure that there are no complex uses of GV.
Definition: GlobalOpt.cpp:976
llvm::GlobalValue::getThreadLocalMode
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:252
Twine.h
InstrTypes.h
llvm::CallBase::setAttributes
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1477
deleteIfDead
static bool deleteIfDead(GlobalValue &GV, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:1264
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::GlobalValue::hasPrivateLinkage
bool hasPrivateLinkage() const
Definition: GlobalValue.h:450
Optimizer
Global Variable Optimizer
Definition: GlobalOpt.cpp:2512
TargetLibraryInfo.h
llvm::Type::isVectorTy
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:226
llvm::Value::uses
iterator_range< use_iterator > uses()
Definition: Value.h:376
false
Definition: StackSlotColoring.cpp:142
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Instruction
Definition: Instruction.h:45
llvm::Operator::getOpcode
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:41
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:287
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1804
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:932
llvm::Use::getUser
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:73
llvm::Evaluator::getMutatedInitializers
DenseMap< GlobalVariable *, Constant * > getMutatedInitializers() const
Definition: Evaluator.h:103
llvm::getUnderlyingObject
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
Definition: ValueTracking.cpp:4280
llvm::createGlobalOptimizerPass
ModulePass * createGlobalOptimizerPass()
createGlobalOptimizerPass - This function returns a new pass that optimizes non-address taken interna...
Definition: GlobalOpt.cpp:2514
SmallPtrSet.h
llvm::Type::PointerTyID
@ PointerTyID
Pointers.
Definition: Type.h:73
llvm::GlobalValue::hasGlobalUnnamedAddr
bool hasGlobalUnnamedAddr() const
Definition: GlobalValue.h:196
llvm::Evaluator::getInvariants
const SmallPtrSetImpl< GlobalVariable * > & getInvariants() const
Definition: Evaluator.h:110
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::GlobalValue::InternalLinkage
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::Comdat
Definition: Comdat.h:33
llvm::AddressSpace
AddressSpace
Definition: NVPTXBaseInfo.h:21
llvm::BlockFrequencyAnalysis
Analysis pass which computes BlockFrequencyInfo.
Definition: BlockFrequencyInfo.h:112
llvm::getObjectSize
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
Definition: MemoryBuiltins.cpp:484
llvm::isSafeToDestroyConstant
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
Definition: GlobalStatus.cpp:44
llvm::None
const NoneType None
Definition: None.h:23
llvm::LoadInst::getSyncScopeID
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
Definition: Instructions.h:247
llvm::Value::use_empty
bool use_empty() const
Definition: Value.h:344
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::CallBase::getCaller
Function * getCaller()
Helper to get the caller (the parent function).
Definition: Instructions.cpp:282
llvm::MemSetInst
This class wraps the llvm.memset intrinsic.
Definition: IntrinsicInst.h:957
Evaluator.h
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
collectSRATypes
static bool collectSRATypes(DenseMap< uint64_t, Type * > &Types, GlobalValue *GV, const DataLayout &DL)
Look at all uses of the global and determine which (offset, type) pairs it can be split into.
Definition: GlobalOpt.cpp:342
llvm::InvokeInst
Invoke instruction.
Definition: Instructions.h:3769
llvm::cl::ZeroOrMore
@ ZeroOrMore
Definition: CommandLine.h:120
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:223
llvm::Value::user_end
user_iterator user_end()
Definition: Value.h:405
llvm::Value::user_back
User * user_back()
Definition: Value.h:407
llvm::GlobalVariable::addDebugInfo
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1554
llvm::AtomicOrdering
AtomicOrdering
Atomic ordering for LLVM's memory model.
Definition: AtomicOrdering.h:56
llvm::Use::set
void set(Value *Val)
Definition: Value.h:868
BasicBlock.h
llvm::cl::opt< bool >
isLeakCheckerRoot
static bool isLeakCheckerRoot(GlobalVariable *GV)
Is this global variable possibly used by a leak checker as a root? If so, we might not really want to...
Definition: GlobalOpt.cpp:111
mayHaveOtherReferences
static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2184
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:309
llvm::GlobalValue
Definition: GlobalValue.h:44
hasUseOtherThanLLVMUsed
static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2157
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition: GlobalVariable.h:135
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
RemoveAttribute
static void RemoveAttribute(Function *F, Attribute::AttrKind A)
Definition: GlobalOpt.cpp:1656
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:78
optimizeGlobalsInModule
static bool optimizeGlobalsInModule(Module &M, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree)
Definition: GlobalOpt.cpp:2381
isPointerValueDeadOnEntryToFunction
static bool isPointerValueDeadOnEntryToFunction(const Function *F, GlobalValue *GV, function_ref< DominatorTree &(Function &)> LookupDomTree)
Definition: GlobalOpt.cpp:1289
llvm::ICmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition: Instructions.h:1190
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:465
uint64_t
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2474
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:578
llvm::SmallPtrSetImpl::end
iterator end() const
Definition: SmallPtrSet.h:407
llvm::Instruction::user_back
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:91
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
IPO.h
llvm::StructType::isOpaque
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:281
GlobalStatus.h
llvm::DIGlobalVariable
Global variables.
Definition: DebugInfoMetadata.h:2957
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:153
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::DenseMap
Definition: DenseMap.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::ConstantFoldLoadFromUniformValue
Constant * ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty)
If C is a uniform value where all bits are the same (either all zero, all ones, all undef or all pois...
Definition: ConstantFolding.cpp:733
llvm::Evaluator
This class evaluates LLVM IR, producing the Constant representing each SSA instruction.
Definition: Evaluator.h:38
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
llvm::Attribute::AttrKind
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:72
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
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:642
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:402
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:632
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:1714
llvm::GlobalValue::setLinkage
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:460
llvm::Instruction::setDebugLoc
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:367
llvm::GlobalValue::isDiscardableIfUnused
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Whether the definition of this global may be discarded if it is not used in its compilation unit.
Definition: GlobalValue.h:372
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI
StandardInstrumentations SI(Debug, VerifyEach)
llvm::User::replaceUsesOfWith
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:21
llvm::ConstantExpr::getCast
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:2037
llvm::GlobalValue::hasLocalLinkage
bool hasLocalLinkage() const
Definition: GlobalValue.h:451
llvm::CmpInst::ICMP_UGE
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:745
iterator_range.h
llvm::Value::use_begin
use_iterator use_begin()
Definition: Value.h:360
hasUsesToReplace
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U, bool &RenameTarget)
Definition: GlobalOpt.cpp:2191
llvm::ArrayType::get
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:640
StripAttr
static AttributeList StripAttr(LLVMContext &C, AttributeList Attrs, Attribute::AttrKind A)
Definition: GlobalOpt.cpp:1648
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
OptimizeAwayTrappingUsesOfValue
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV)
Definition: GlobalOpt.cpp:681
llvm::CallBase::Create
static CallBase * Create(CallBase *CB, ArrayRef< OperandBundleDef > Bundles, Instruction *InsertPt=nullptr)
Create a clone of CB with a different set of operand bundles and insert it before InsertPt.
Definition: Instructions.cpp:255
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:650
llvm::ZExtInst
This class represents zero extension of integer types.
Definition: Instructions.h:4812
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::GlobalValue::AppendingLinkage
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
OptimizeEmptyGlobalCXXDtors
static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn)
Definition: GlobalOpt.cpp:2339
llvm::Function::getIntrinsicID
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:207
llvm::ConstantFoldLoadFromConst
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Definition: ConstantFolding.cpp:671
globalopt
globalopt
Definition: GlobalOpt.cpp:2511
llvm::SmallPtrSetImplBase::clear
void clear()
Definition: SmallPtrSet.h:94
llvm::isInstructionTriviallyDead
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:398
llvm::Constant::removeDeadConstantUsers
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
Definition: Constants.cpp:755
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:213
processInternalGlobal
static bool processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree)
Analyze the specified global variable and optimize it if possible.
Definition: GlobalOpt.cpp:1426
Dwarf.h
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:746
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
CleanupPointerRootUsers
static bool CleanupPointerRootUsers(GlobalVariable *GV, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
This GV is a pointer root.
Definition: GlobalOpt.cpp:194
llvm::StructType::element_begin
element_iterator element_begin() const
Definition: DerivedTypes.h:317
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
OptimizeGlobalAddressOfAllocation
static GlobalVariable * OptimizeGlobalAddressOfAllocation(GlobalVariable *GV, CallInst *CI, uint64_t AllocSize, Constant *InitVal, const DataLayout &DL, TargetLibraryInfo *TLI)
This function takes the specified global variable, and transforms the program as if it always contain...
Definition: GlobalOpt.cpp:838
llvm::BranchProbability
Definition: BranchProbability.h:30
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append a range to a container.
Definition: STLExtras.h:1846
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:991
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition: ilist_node.h:81
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
hasInvokeCallers
static bool hasInvokeCallers(Function *F)
Definition: GlobalOpt.cpp:1792
hasMustTailCallers
static bool hasMustTailCallers(Function *F)
Definition: GlobalOpt.cpp:1778
llvm::ObjectSizeOpts
Various options to control the behavior of getObjectSize.
Definition: MemoryBuiltins.h:131
llvm::CastInst
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:431
llvm::CallInst::isMustTailCall
bool isMustTailCall() const
Definition: Instructions.h:1677
BlockFrequencyInfo.h
isValidCandidateForColdCC
static bool isValidCandidateForColdCC(Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, const std::vector< Function * > &AllCallsCold)
Definition: GlobalOpt.cpp:1715
llvm::DIVariable
Base class for variables.
Definition: DebugInfoMetadata.h:2528
llvm::LoadInst::getOrdering
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Definition: Instructions.h:237
ColdCCRelFreq
static cl::opt< int > ColdCCRelFreq("coldcc-rel-freq", cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::desc("Maximum block frequency, expressed as a percentage of caller's " "entry frequency, for a call site to be considered cold for enabling" "coldcc"))
llvm::GlobalStatus::InitializerStored
@ InitializerStored
This global is stored to, but the only thing stored is the constant it was initialized with.
Definition: GlobalStatus.h:45
llvm::Function::hasAddressTaken
bool hasAddressTaken(const User **=nullptr, bool IgnoreCallbackUses=false, bool IgnoreAssumeLikeCalls=true, bool IngoreLLVMUsed=false, bool IgnoreARCAttachedCall=false) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition: Function.cpp:1759
llvm::GlobalAlias::getAliasee
const Constant * getAliasee() const
Definition: GlobalAlias.h:84
llvm::CallingConv::C
@ C
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:180
ValueHandle.h
llvm::DenseMapBase< DenseMap< KeyT, ValueT, 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:207
llvm::CallBase::setArgOperand
void setArgOperand(unsigned i, Value *v)
Definition: InstrTypes.h:1348
llvm::Constant::stripPointerCasts
const Constant * stripPointerCasts() const
Definition: Constant.h:213
llvm::Init
Definition: Record.h:274
llvm::GlobalValue::eraseFromParent
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:86
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:887
llvm::GlobalValue::getUnnamedAddr
UnnamedAddr getUnnamedAddr() const
Definition: GlobalValue.h:209
llvm::DenseMapBase< DenseMap< KeyT, ValueT, 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:97
CallingConv.h
Attributes.h
cxxDtorIsEmpty
static bool cxxDtorIsEmpty(const Function &Fn)
Returns whether the given function is an empty C++ destructor and can therefore be eliminated.
Definition: GlobalOpt.cpp:2323
optimizeOnceStoredGlobal
static bool optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, AtomicOrdering Ordering, const DataLayout &DL, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
Definition: GlobalOpt.cpp:1068
OptimizeGlobalVars
static bool OptimizeGlobalVars(Module &M, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:2001
Constant.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::commonAlignment
Align commonAlignment(Align A, Align B)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:211
llvm::DIExpression::prependOpcodes
static DIExpression * prependOpcodes(const DIExpression *Expr, SmallVectorImpl< uint64_t > &Ops, bool StackValue=false, bool EntryValue=false)
Prepend DIExpr with the given opcodes and optionally turn it into a stack value.
Definition: DebugInfoMetadata.cpp:1367
hasMoreThanOneUseOtherThanLLVMUsed
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2173
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:971
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:880
llvm::PPC::getPredicate
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:87
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:348
llvm::Function::removeAttributeAtIndex
void removeAttributeAtIndex(unsigned i, Attribute::AttrKind Kind)
removes the attribute from the list of attributes.
Definition: Function.cpp:576
llvm::Value::user_iterator
user_iterator_impl< User > user_iterator
Definition: Value.h:390
compareNames
static int compareNames(Constant *const *A, Constant *const *B)
Definition: GlobalOpt.cpp:2061
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:325
llvm::CallingConv::Cold
@ Cold
Definition: CallingConv.h:48
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
llvm::ConstantInt::getBool
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:894
llvm::Type::FixedVectorTyID
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:76
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1341
GlobalVariable.h
EvaluateStaticConstructor
static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL, TargetLibraryInfo *TLI)
Evaluate static constructors in the function, if we can.
Definition: GlobalOpt.cpp:2036
llvm::GlobalValue::getComdat
const Comdat * getComdat() const
Definition: Globals.cpp:179
allUsesOfLoadAndStores
static void allUsesOfLoadAndStores(GlobalVariable *GV, SmallVector< Value *, 4 > &Uses)
Get all the loads/store uses for global variable GV.
Definition: GlobalOpt.cpp:662
Casting.h
Function.h
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::size
unsigned size() const
Definition: DenseMap.h:100
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1590
llvm::CmpInst::isSigned
bool isSigned() const
Definition: InstrTypes.h:949
llvm::StructType::element_end
element_iterator element_end() const
Definition: DerivedTypes.h:318
OptimizeFunctions
static bool OptimizeFunctions(Module &M, function_ref< TargetLibraryInfo &(Function &)> GetTLI, function_ref< TargetTransformInfo &(Function &)> GetTTI, function_ref< BlockFrequencyInfo &(Function &)> GetBFI, function_ref< DominatorTree &(Function &)> LookupDomTree, SmallPtrSetImpl< const Comdat * > &NotDiscardableComdats)
Definition: GlobalOpt.cpp:1886
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:221
llvm::ConstantArray::get
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1295
llvm::ConstantExpr::getGetElementPtr
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1245
llvm::SmallVectorImpl::clear
void clear()
Definition: SmallVector.h:581
makeAllConstantUsesInstructions
static void makeAllConstantUsesInstructions(Constant *C)
C may have non-instruction users, and allNonInstructionUsersCanBeMadeInstructions has returned true.
Definition: GlobalOpt.cpp:1395
llvm::CallBase::getCalledOperand
Value * getCalledOperand() const
Definition: InstrTypes.h:1391
llvm::BlockFrequencyInfo::getBlockFreq
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Definition: BlockFrequencyInfo.cpp:204
llvm::DIGlobalVariableExpression
A pair of DIGlobalVariable and DIExpression.
Definition: DebugInfoMetadata.h:3408
GlobalAlias.h
llvm::GlobalOptPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: GlobalOpt.cpp:2438
llvm::CallBase::setCalledOperand
void setCalledOperand(Value *V)
Definition: InstrTypes.h:1431
transferSRADebugInfo
static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV, uint64_t FragmentOffsetInBits, uint64_t FragmentSizeInBits, uint64_t VarSize)
Copy over the debug info for a variable to its SRA replacements.
Definition: GlobalOpt.cpp:403
llvm::GlobalValue::getAddressSpace
unsigned getAddressSpace() const
Definition: Globals.cpp:119
llvm::GlobalStatus::StoredOnce
@ StoredOnce
This global is stored to, but only its initializer and one other value is ever stored to it.
Definition: GlobalStatus.h:51
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:252
llvm::Function::isVarArg
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.h:190
llvm::Type::StructTyID
@ StructTyID
Structures.
Definition: Type.h:74
allUsesOfLoadedValueWillTrapIfNull
static bool allUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV)
Return true if all uses of any loads from GV will trap if the loaded value is null.
Definition: GlobalOpt.cpp:632
Instructions.h
GlobalOpt.h
llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:2109
tryToOptimizeStoreOfAllocationToGlobal
static bool tryToOptimizeStoreOfAllocationToGlobal(GlobalVariable *GV, CallInst *CI, AtomicOrdering Ordering, const DataLayout &DL, TargetLibraryInfo *TLI)
If we have a global that is only initialized with a fixed size allocation try to transform the progra...
Definition: GlobalOpt.cpp:1020
SmallVector.h
llvm::Instruction::getDebugLoc
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:370
User.h
llvm::GlobalVariable::isConstant
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Definition: GlobalVariable.h:152
used
This might compile to this xmm1 xorps xmm0 movss xmm0 ret Now consider if the code caused xmm1 to get spilled This might produce this xmm1 movaps xmm0 movaps xmm1 movss xmm0 ret since the reload is only used by these we could fold it into the producing something like xmm1 movaps xmm0 ret saving two instructions The basic idea is that a reload from a spill if only one byte chunk is used
Definition: README-SSE.txt:270
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:744
Dominators.h
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1343
N
#define N
IsSafeComputationToRemove
static bool IsSafeComputationToRemove(Value *V, function_ref< TargetLibraryInfo &(Function &)> GetTLI)
Given a value that is stored to a global but never read, determine whether it's safe to remove the st...
Definition: GlobalOpt.cpp:163
llvm::DenseMapBase< DenseMap< KeyT, ValueT, 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:222
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:94
llvm::CmpInst::getPredicate
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:811
Users
iv Induction Variable Users
Definition: IVUsers.cpp:52
TargetTransformInfo.h
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::PHINode
Definition: Instructions.h:2657
Threshold
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
llvm::ConstantFoldConstant
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Definition: ConstantFolding.cpp:1179
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1176
llvm::Type::ScalableVectorTyID
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:77
llvm::GlobalValue::getType
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:271
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:401
DerivedTypes.h
llvm::SmallPtrSetImpl< const PHINode * >
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::getLoadStorePointerOperand
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
Definition: Instructions.h:5319
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:940
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1478
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:172
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::Value::takeName
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:382
llvm::isAllocRemovable
bool isAllocRemovable(const CallBase *V, const TargetLibraryInfo *TLI)
Return false if the allocation can have side effects on the program state we are required to preserve...
Definition: MemoryBuiltins.cpp:285
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:62
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::cl::desc
Definition: CommandLine.h:412
llvm::GlobalObject::setAlignment
void setAlignment(MaybeAlign Align)
Definition: Globals.cpp:124
llvm::ConstantFoldInstruction
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Definition: ConstantFolding.cpp:1115
raw_ostream.h
llvm::GlobalVariable::removeFromParent
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it.
Definition: Globals.cpp:426
llvm::NullPointerIsDefined
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1985
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:634
Value.h
InitializePasses.h
hasOnlyColdCalls
static bool hasOnlyColdCalls(Function &F, function_ref< BlockFrequencyInfo &(Function &)> GetBFI)
Definition: GlobalOpt.cpp:1749
llvm::RecursivelyDeleteTriviallyDeadInstructionsPermissive
bool RecursivelyDeleteTriviallyDeadInstructionsPermissive(SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow instructions that are not...
Definition: Local.cpp:538
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::StringRef::compare
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:200
Debug.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:440
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::AtomicOrdering::NotAtomic
@ NotAtomic
setUsedInitializer
static void setUsedInitializer(GlobalVariable &V, const SmallPtrSetImpl< GlobalValue * > &Init)
Definition: GlobalOpt.cpp:2067
llvm::GlobalVariable::setInitializer
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:434
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
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:364
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
RemovePreallocated
static void RemovePreallocated(Function *F)
Definition: GlobalOpt.cpp:1799