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