LLVM  3.7.0
GlobalOpt.cpp
Go to the documentation of this file.
1 //===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass transforms simple global variables that never have their address
11 // taken. If obviously true, it marks read/write globals as constant, deletes
12 // variables only stored to, etc.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/IPO.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
26 #include "llvm/IR/CallSite.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/IR/Operator.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Support/Debug.h"
45 #include <algorithm>
46 #include <deque>
47 using namespace llvm;
48 
49 #define DEBUG_TYPE "globalopt"
50 
51 STATISTIC(NumMarked , "Number of globals marked constant");
52 STATISTIC(NumUnnamed , "Number of globals marked unnamed_addr");
53 STATISTIC(NumSRA , "Number of aggregate globals broken into scalars");
54 STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
55 STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
56 STATISTIC(NumDeleted , "Number of globals deleted");
57 STATISTIC(NumFnDeleted , "Number of functions deleted");
58 STATISTIC(NumGlobUses , "Number of global uses devirtualized");
59 STATISTIC(NumLocalized , "Number of globals localized");
60 STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
61 STATISTIC(NumFastCallFns , "Number of functions converted to fastcc");
62 STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
63 STATISTIC(NumNestRemoved , "Number of nest attributes removed");
64 STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
65 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
66 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
67 
68 namespace {
69  struct GlobalOpt : public ModulePass {
70  void getAnalysisUsage(AnalysisUsage &AU) const override {
72  }
73  static char ID; // Pass identification, replacement for typeid
74  GlobalOpt() : ModulePass(ID) {
76  }
77 
78  bool runOnModule(Module &M) override;
79 
80  private:
81  bool OptimizeFunctions(Module &M);
82  bool OptimizeGlobalVars(Module &M);
83  bool OptimizeGlobalAliases(Module &M);
84  bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
85  bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI,
86  const GlobalStatus &GS);
87  bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
88 
89  TargetLibraryInfo *TLI;
90  SmallSet<const Comdat *, 8> NotDiscardableComdats;
91  };
92 }
93 
94 char GlobalOpt::ID = 0;
95 INITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
96  "Global Variable Optimizer", false, false)
99  "Global Variable Optimizer", false, false)
100 
101 ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
102 
103 /// isLeakCheckerRoot - Is this global variable possibly used by a leak checker
104 /// as a root? If so, we might not really want to eliminate the stores to it.
106  // A global variable is a root if it is a pointer, or could plausibly contain
107  // a pointer. There are two challenges; one is that we could have a struct
108  // the has an inner member which is a pointer. We recurse through the type to
109  // detect these (up to a point). The other is that we may actually be a union
110  // of a pointer and another type, and so our LLVM type is an integer which
111  // gets converted into a pointer, or our type is an [i8 x #] with a pointer
112  // potentially contained here.
113 
114  if (GV->hasPrivateLinkage())
115  return false;
116 
118  Types.push_back(cast<PointerType>(GV->getType())->getElementType());
119 
120  unsigned Limit = 20;
121  do {
122  Type *Ty = Types.pop_back_val();
123  switch (Ty->getTypeID()) {
124  default: break;
125  case Type::PointerTyID: return true;
126  case Type::ArrayTyID:
127  case Type::VectorTyID: {
128  SequentialType *STy = cast<SequentialType>(Ty);
129  Types.push_back(STy->getElementType());
130  break;
131  }
132  case Type::StructTyID: {
133  StructType *STy = cast<StructType>(Ty);
134  if (STy->isOpaque()) return true;
136  E = STy->element_end(); I != E; ++I) {
137  Type *InnerTy = *I;
138  if (isa<PointerType>(InnerTy)) return true;
139  if (isa<CompositeType>(InnerTy))
140  Types.push_back(InnerTy);
141  }
142  break;
143  }
144  }
145  if (--Limit == 0) return true;
146  } while (!Types.empty());
147  return false;
148 }
149 
150 /// Given a value that is stored to a global but never read, determine whether
151 /// it's safe to remove the store and the chain of computation that feeds the
152 /// store.
153 static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) {
154  do {
155  if (isa<Constant>(V))
156  return true;
157  if (!V->hasOneUse())
158  return false;
159  if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
160  isa<GlobalValue>(V))
161  return false;
162  if (isAllocationFn(V, TLI))
163  return true;
164 
165  Instruction *I = cast<Instruction>(V);
166  if (I->mayHaveSideEffects())
167  return false;
168  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
169  if (!GEP->hasAllConstantIndices())
170  return false;
171  } else if (I->getNumOperands() != 1) {
172  return false;
173  }
174 
175  V = I->getOperand(0);
176  } while (1);
177 }
178 
179 /// CleanupPointerRootUsers - This GV is a pointer root. Loop over all users
180 /// of the global and clean up any that obviously don't assign the global a
181 /// value that isn't dynamically allocated.
182 ///
184  const TargetLibraryInfo *TLI) {
185  // A brief explanation of leak checkers. The goal is to find bugs where
186  // pointers are forgotten, causing an accumulating growth in memory
187  // usage over time. The common strategy for leak checkers is to whitelist the
188  // memory pointed to by globals at exit. This is popular because it also
189  // solves another problem where the main thread of a C++ program may shut down
190  // before other threads that are still expecting to use those globals. To
191  // handle that case, we expect the program may create a singleton and never
192  // destroy it.
193 
194  bool Changed = false;
195 
196  // If Dead[n].first is the only use of a malloc result, we can delete its
197  // chain of computation and the store to the global in Dead[n].second.
199 
200  // Constants can't be pointers to dynamically allocated memory.
201  for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end();
202  UI != E;) {
203  User *U = *UI++;
204  if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
205  Value *V = SI->getValueOperand();
206  if (isa<Constant>(V)) {
207  Changed = true;
208  SI->eraseFromParent();
209  } else if (Instruction *I = dyn_cast<Instruction>(V)) {
210  if (I->hasOneUse())
211  Dead.push_back(std::make_pair(I, SI));
212  }
213  } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
214  if (isa<Constant>(MSI->getValue())) {
215  Changed = true;
216  MSI->eraseFromParent();
217  } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
218  if (I->hasOneUse())
219  Dead.push_back(std::make_pair(I, MSI));
220  }
221  } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
222  GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
223  if (MemSrc && MemSrc->isConstant()) {
224  Changed = true;
225  MTI->eraseFromParent();
226  } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
227  if (I->hasOneUse())
228  Dead.push_back(std::make_pair(I, MTI));
229  }
230  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
231  if (CE->use_empty()) {
232  CE->destroyConstant();
233  Changed = true;
234  }
235  } else if (Constant *C = dyn_cast<Constant>(U)) {
236  if (isSafeToDestroyConstant(C)) {
237  C->destroyConstant();
238  // This could have invalidated UI, start over from scratch.
239  Dead.clear();
240  CleanupPointerRootUsers(GV, TLI);
241  return true;
242  }
243  }
244  }
245 
246  for (int i = 0, e = Dead.size(); i != e; ++i) {
247  if (IsSafeComputationToRemove(Dead[i].first, TLI)) {
248  Dead[i].second->eraseFromParent();
249  Instruction *I = Dead[i].first;
250  do {
251  if (isAllocationFn(I, TLI))
252  break;
254  if (!J)
255  break;
256  I->eraseFromParent();
257  I = J;
258  } while (1);
259  I->eraseFromParent();
260  }
261  }
262 
263  return Changed;
264 }
265 
266 /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all
267 /// users of the global, cleaning up the obvious ones. This is largely just a
268 /// quick scan over the use list to clean up the easy and obvious cruft. This
269 /// returns true if it made a change.
271  const DataLayout &DL,
272  TargetLibraryInfo *TLI) {
273  bool Changed = false;
274  // Note that we need to use a weak value handle for the worklist items. When
275  // we delete a constant array, we may also be holding pointer to one of its
276  // elements (or an element of one of its elements if we're dealing with an
277  // array of arrays) in the worklist.
278  SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
279  while (!WorkList.empty()) {
280  Value *UV = WorkList.pop_back_val();
281  if (!UV)
282  continue;
283 
284  User *U = cast<User>(UV);
285 
286  if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
287  if (Init) {
288  // Replace the load with the initializer.
289  LI->replaceAllUsesWith(Init);
290  LI->eraseFromParent();
291  Changed = true;
292  }
293  } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
294  // Store must be unreachable or storing Init into the global.
295  SI->eraseFromParent();
296  Changed = true;
297  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
298  if (CE->getOpcode() == Instruction::GetElementPtr) {
299  Constant *SubInit = nullptr;
300  if (Init)
301  SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
302  Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, TLI);
303  } else if ((CE->getOpcode() == Instruction::BitCast &&
304  CE->getType()->isPointerTy()) ||
305  CE->getOpcode() == Instruction::AddrSpaceCast) {
306  // Pointer cast, delete any stores and memsets to the global.
307  Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, TLI);
308  }
309 
310  if (CE->use_empty()) {
311  CE->destroyConstant();
312  Changed = true;
313  }
314  } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
315  // Do not transform "gepinst (gep constexpr (GV))" here, because forming
316  // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
317  // and will invalidate our notion of what Init is.
318  Constant *SubInit = nullptr;
319  if (!isa<ConstantExpr>(GEP->getOperand(0))) {
320  ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(
321  ConstantFoldInstruction(GEP, DL, TLI));
322  if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
323  SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
324 
325  // If the initializer is an all-null value and we have an inbounds GEP,
326  // we already know what the result of any load from that GEP is.
327  // TODO: Handle splats.
328  if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
329  SubInit = Constant::getNullValue(GEP->getType()->getElementType());
330  }
331  Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, TLI);
332 
333  if (GEP->use_empty()) {
334  GEP->eraseFromParent();
335  Changed = true;
336  }
337  } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
338  if (MI->getRawDest() == V) {
339  MI->eraseFromParent();
340  Changed = true;
341  }
342 
343  } else if (Constant *C = dyn_cast<Constant>(U)) {
344  // If we have a chain of dead constantexprs or other things dangling from
345  // us, and if they are all dead, nuke them without remorse.
346  if (isSafeToDestroyConstant(C)) {
347  C->destroyConstant();
348  CleanupConstantGlobalUsers(V, Init, DL, TLI);
349  return true;
350  }
351  }
352  }
353  return Changed;
354 }
355 
356 /// isSafeSROAElementUse - Return true if the specified instruction is a safe
357 /// user of a derived expression from a global that we want to SROA.
358 static bool isSafeSROAElementUse(Value *V) {
359  // We might have a dead and dangling constant hanging off of here.
360  if (Constant *C = dyn_cast<Constant>(V))
361  return isSafeToDestroyConstant(C);
362 
364  if (!I) return false;
365 
366  // Loads are ok.
367  if (isa<LoadInst>(I)) return true;
368 
369  // Stores *to* the pointer are ok.
370  if (StoreInst *SI = dyn_cast<StoreInst>(I))
371  return SI->getOperand(0) != V;
372 
373  // Otherwise, it must be a GEP.
375  if (!GEPI) return false;
376 
377  if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
378  !cast<Constant>(GEPI->getOperand(1))->isNullValue())
379  return false;
380 
381  for (User *U : GEPI->users())
382  if (!isSafeSROAElementUse(U))
383  return false;
384  return true;
385 }
386 
387 
388 /// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
389 /// Look at it and its uses and decide whether it is safe to SROA this global.
390 ///
392  // The user of the global must be a GEP Inst or a ConstantExpr GEP.
393  if (!isa<GetElementPtrInst>(U) &&
394  (!isa<ConstantExpr>(U) ||
395  cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
396  return false;
397 
398  // Check to see if this ConstantExpr GEP is SRA'able. In particular, we
399  // don't like < 3 operand CE's, and we don't like non-constant integer
400  // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some
401  // value of C.
402  if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
403  !cast<Constant>(U->getOperand(1))->isNullValue() ||
404  !isa<ConstantInt>(U->getOperand(2)))
405  return false;
406 
408  ++GEPI; // Skip over the pointer index.
409 
410  // If this is a use of an array allocation, do a bit more checking for sanity.
411  if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
412  uint64_t NumElements = AT->getNumElements();
413  ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
414 
415  // Check to make sure that index falls within the array. If not,
416  // something funny is going on, so we won't do the optimization.
417  //
418  if (Idx->getZExtValue() >= NumElements)
419  return false;
420 
421  // We cannot scalar repl this level of the array unless any array
422  // sub-indices are in-range constants. In particular, consider:
423  // A[0][i]. We cannot know that the user isn't doing invalid things like
424  // allowing i to index an out-of-range subscript that accesses A[1].
425  //
426  // Scalar replacing *just* the outer index of the array is probably not
427  // going to be a win anyway, so just give up.
428  for (++GEPI; // Skip array index.
429  GEPI != E;
430  ++GEPI) {
431  uint64_t NumElements;
432  if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI))
433  NumElements = SubArrayTy->getNumElements();
434  else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI))
435  NumElements = SubVectorTy->getNumElements();
436  else {
437  assert((*GEPI)->isStructTy() &&
438  "Indexed GEP type is not array, vector, or struct!");
439  continue;
440  }
441 
442  ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
443  if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
444  return false;
445  }
446  }
447 
448  for (User *UU : U->users())
449  if (!isSafeSROAElementUse(UU))
450  return false;
451 
452  return true;
453 }
454 
455 /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
456 /// is safe for us to perform this transformation.
457 ///
459  for (User *U : GV->users())
460  if (!IsUserOfGlobalSafeForSRA(U, GV))
461  return false;
462 
463  return true;
464 }
465 
466 
467 /// SRAGlobal - Perform scalar replacement of aggregates on the specified global
468 /// variable. This opens the door for other optimizations by exposing the
469 /// behavior of the program in a more fine-grained way. We have determined that
470 /// this transformation is safe already. We return the first global variable we
471 /// insert so that the caller can reprocess it.
473  // Make sure this global only has simple uses that we can SRA.
474  if (!GlobalUsersSafeToSRA(GV))
475  return nullptr;
476 
477  assert(GV->hasLocalLinkage() && !GV->isConstant());
478  Constant *Init = GV->getInitializer();
479  Type *Ty = Init->getType();
480 
481  std::vector<GlobalVariable*> NewGlobals;
482  Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
483 
484  // Get the alignment of the global, either explicit or target-specific.
485  unsigned StartAlignment = GV->getAlignment();
486  if (StartAlignment == 0)
487  StartAlignment = DL.getABITypeAlignment(GV->getType());
488 
489  if (StructType *STy = dyn_cast<StructType>(Ty)) {
490  NewGlobals.reserve(STy->getNumElements());
491  const StructLayout &Layout = *DL.getStructLayout(STy);
492  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
493  Constant *In = Init->getAggregateElement(i);
494  assert(In && "Couldn't get element of initializer?");
495  GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
497  In, GV->getName()+"."+Twine(i),
498  GV->getThreadLocalMode(),
499  GV->getType()->getAddressSpace());
500  Globals.insert(GV, NGV);
501  NewGlobals.push_back(NGV);
502 
503  // Calculate the known alignment of the field. If the original aggregate
504  // had 256 byte alignment for example, something might depend on that:
505  // propagate info to each field.
506  uint64_t FieldOffset = Layout.getElementOffset(i);
507  unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
508  if (NewAlign > DL.getABITypeAlignment(STy->getElementType(i)))
509  NGV->setAlignment(NewAlign);
510  }
511  } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
512  unsigned NumElements = 0;
513  if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
514  NumElements = ATy->getNumElements();
515  else
516  NumElements = cast<VectorType>(STy)->getNumElements();
517 
518  if (NumElements > 16 && GV->hasNUsesOrMore(16))
519  return nullptr; // It's not worth it.
520  NewGlobals.reserve(NumElements);
521 
522  uint64_t EltSize = DL.getTypeAllocSize(STy->getElementType());
523  unsigned EltAlign = DL.getABITypeAlignment(STy->getElementType());
524  for (unsigned i = 0, e = NumElements; i != e; ++i) {
525  Constant *In = Init->getAggregateElement(i);
526  assert(In && "Couldn't get element of initializer?");
527 
528  GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
530  In, GV->getName()+"."+Twine(i),
531  GV->getThreadLocalMode(),
532  GV->getType()->getAddressSpace());
533  Globals.insert(GV, NGV);
534  NewGlobals.push_back(NGV);
535 
536  // Calculate the known alignment of the field. If the original aggregate
537  // had 256 byte alignment for example, something might depend on that:
538  // propagate info to each field.
539  unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
540  if (NewAlign > EltAlign)
541  NGV->setAlignment(NewAlign);
542  }
543  }
544 
545  if (NewGlobals.empty())
546  return nullptr;
547 
548  DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV);
549 
551 
552  // Loop over all of the uses of the global, replacing the constantexpr geps,
553  // with smaller constantexpr geps or direct references.
554  while (!GV->use_empty()) {
555  User *GEP = GV->user_back();
556  assert(((isa<ConstantExpr>(GEP) &&
557  cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
558  isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
559 
560  // Ignore the 1th operand, which has to be zero or else the program is quite
561  // broken (undefined). Get the 2nd operand, which is the structure or array
562  // index.
563  unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
564  if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
565 
566  Value *NewPtr = NewGlobals[Val];
567  Type *NewTy = NewGlobals[Val]->getValueType();
568 
569  // Form a shorter GEP if needed.
570  if (GEP->getNumOperands() > 3) {
571  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
573  Idxs.push_back(NullInt);
574  for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
575  Idxs.push_back(CE->getOperand(i));
576  NewPtr =
577  ConstantExpr::getGetElementPtr(NewTy, cast<Constant>(NewPtr), Idxs);
578  } else {
579  GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
581  Idxs.push_back(NullInt);
582  for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
583  Idxs.push_back(GEPI->getOperand(i));
584  NewPtr = GetElementPtrInst::Create(
585  NewTy, NewPtr, Idxs, GEPI->getName() + "." + Twine(Val), GEPI);
586  }
587  }
588  GEP->replaceAllUsesWith(NewPtr);
589 
590  if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
591  GEPI->eraseFromParent();
592  else
593  cast<ConstantExpr>(GEP)->destroyConstant();
594  }
595 
596  // Delete the old global, now that it is dead.
597  Globals.erase(GV);
598  ++NumSRA;
599 
600  // Loop over the new globals array deleting any globals that are obviously
601  // dead. This can arise due to scalarization of a structure or an array that
602  // has elements that are dead.
603  unsigned FirstGlobal = 0;
604  for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
605  if (NewGlobals[i]->use_empty()) {
606  Globals.erase(NewGlobals[i]);
607  if (FirstGlobal == i) ++FirstGlobal;
608  }
609 
610  return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : nullptr;
611 }
612 
613 /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
614 /// value will trap if the value is dynamically null. PHIs keeps track of any
615 /// phi nodes we've seen to avoid reprocessing them.
616 static bool AllUsesOfValueWillTrapIfNull(const Value *V,
618  for (const User *U : V->users())
619  if (isa<LoadInst>(U)) {
620  // Will trap.
621  } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
622  if (SI->getOperand(0) == V) {
623  //cerr << "NONTRAPPING USE: " << *U;
624  return false; // Storing the value.
625  }
626  } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
627  if (CI->getCalledValue() != V) {
628  //cerr << "NONTRAPPING USE: " << *U;
629  return false; // Not calling the ptr
630  }
631  } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
632  if (II->getCalledValue() != V) {
633  //cerr << "NONTRAPPING USE: " << *U;
634  return false; // Not calling the ptr
635  }
636  } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
637  if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
638  } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
639  if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
640  } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
641  // If we've already seen this phi node, ignore it, it has already been
642  // checked.
643  if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
644  return false;
645  } else if (isa<ICmpInst>(U) &&
646  isa<ConstantPointerNull>(U->getOperand(1))) {
647  // Ignore icmp X, null
648  } else {
649  //cerr << "NONTRAPPING USE: " << *U;
650  return false;
651  }
652 
653  return true;
654 }
655 
656 /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
657 /// from GV will trap if the loaded value is null. Note that this also permits
658 /// comparisons of the loaded value against null, as a special case.
660  for (const User *U : GV->users())
661  if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
663  if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
664  return false;
665  } else if (isa<StoreInst>(U)) {
666  // Ignore stores to the global.
667  } else {
668  // We don't know or understand this user, bail out.
669  //cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
670  return false;
671  }
672  return true;
673 }
674 
676  bool Changed = false;
677  for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
678  Instruction *I = cast<Instruction>(*UI++);
679  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
680  LI->setOperand(0, NewV);
681  Changed = true;
682  } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
683  if (SI->getOperand(1) == V) {
684  SI->setOperand(1, NewV);
685  Changed = true;
686  }
687  } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
688  CallSite CS(I);
689  if (CS.getCalledValue() == V) {
690  // Calling through the pointer! Turn into a direct call, but be careful
691  // that the pointer is not also being passed as an argument.
692  CS.setCalledFunction(NewV);
693  Changed = true;
694  bool PassedAsArg = false;
695  for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
696  if (CS.getArgument(i) == V) {
697  PassedAsArg = true;
698  CS.setArgument(i, NewV);
699  }
700 
701  if (PassedAsArg) {
702  // Being passed as an argument also. Be careful to not invalidate UI!
703  UI = V->user_begin();
704  }
705  }
706  } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
707  Changed |= OptimizeAwayTrappingUsesOfValue(CI,
708  ConstantExpr::getCast(CI->getOpcode(),
709  NewV, CI->getType()));
710  if (CI->use_empty()) {
711  Changed = true;
712  CI->eraseFromParent();
713  }
714  } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
715  // Should handle GEP here.
717  Idxs.reserve(GEPI->getNumOperands()-1);
718  for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
719  i != e; ++i)
720  if (Constant *C = dyn_cast<Constant>(*i))
721  Idxs.push_back(C);
722  else
723  break;
724  if (Idxs.size() == GEPI->getNumOperands()-1)
726  GEPI, ConstantExpr::getGetElementPtr(nullptr, NewV, Idxs));
727  if (GEPI->use_empty()) {
728  Changed = true;
729  GEPI->eraseFromParent();
730  }
731  }
732  }
733 
734  return Changed;
735 }
736 
737 
738 /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
739 /// value stored into it. If there are uses of the loaded value that would trap
740 /// if the loaded value is dynamically null, then we know that they cannot be
741 /// reachable with a null optimize away the load.
743  const DataLayout &DL,
744  TargetLibraryInfo *TLI) {
745  bool Changed = false;
746 
747  // Keep track of whether we are able to remove all the uses of the global
748  // other than the store that defines it.
749  bool AllNonStoreUsesGone = true;
750 
751  // Replace all uses of loads with uses of uses of the stored value.
752  for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){
753  User *GlobalUser = *GUI++;
754  if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
755  Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
756  // If we were able to delete all uses of the loads
757  if (LI->use_empty()) {
758  LI->eraseFromParent();
759  Changed = true;
760  } else {
761  AllNonStoreUsesGone = false;
762  }
763  } else if (isa<StoreInst>(GlobalUser)) {
764  // Ignore the store that stores "LV" to the global.
765  assert(GlobalUser->getOperand(1) == GV &&
766  "Must be storing *to* the global");
767  } else {
768  AllNonStoreUsesGone = false;
769 
770  // If we get here we could have other crazy uses that are transitively
771  // loaded.
772  assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
773  isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
774  isa<BitCastInst>(GlobalUser) ||
775  isa<GetElementPtrInst>(GlobalUser)) &&
776  "Only expect load and stores!");
777  }
778  }
779 
780  if (Changed) {
781  DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
782  ++NumGlobUses;
783  }
784 
785  // If we nuked all of the loads, then none of the stores are needed either,
786  // nor is the global.
787  if (AllNonStoreUsesGone) {
788  if (isLeakCheckerRoot(GV)) {
789  Changed |= CleanupPointerRootUsers(GV, TLI);
790  } else {
791  Changed = true;
792  CleanupConstantGlobalUsers(GV, nullptr, DL, TLI);
793  }
794  if (GV->use_empty()) {
795  DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n");
796  Changed = true;
797  GV->eraseFromParent();
798  ++NumDeleted;
799  }
800  }
801  return Changed;
802 }
803 
804 /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
805 /// instructions that are foldable.
806 static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
807  TargetLibraryInfo *TLI) {
808  for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
809  if (Instruction *I = dyn_cast<Instruction>(*UI++))
810  if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
811  I->replaceAllUsesWith(NewC);
812 
813  // Advance UI to the next non-I use to avoid invalidating it!
814  // Instructions could multiply use V.
815  while (UI != E && *UI == I)
816  ++UI;
817  I->eraseFromParent();
818  }
819 }
820 
821 /// OptimizeGlobalAddressOfMalloc - This function takes the specified global
822 /// variable, and transforms the program as if it always contained the result of
823 /// the specified malloc. Because it is always the result of the specified
824 /// malloc, there is no reason to actually DO the malloc. Instead, turn the
825 /// malloc into a global, and any loads of GV as uses of the new global.
826 static GlobalVariable *
828  ConstantInt *NElements, const DataLayout &DL,
829  TargetLibraryInfo *TLI) {
830  DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n');
831 
832  Type *GlobalType;
833  if (NElements->getZExtValue() == 1)
834  GlobalType = AllocTy;
835  else
836  // If we have an array allocation, the global variable is of an array.
837  GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
838 
839  // Create the new global variable. The contents of the malloc'd memory is
840  // undefined, so initialize with an undef value.
841  GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
842  GlobalType, false,
844  UndefValue::get(GlobalType),
845  GV->getName()+".body",
846  GV,
847  GV->getThreadLocalMode());
848 
849  // If there are bitcast users of the malloc (which is typical, usually we have
850  // a malloc + bitcast) then replace them with uses of the new global. Update
851  // other users to use the global as well.
852  BitCastInst *TheBC = nullptr;
853  while (!CI->use_empty()) {
854  Instruction *User = cast<Instruction>(CI->user_back());
855  if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
856  if (BCI->getType() == NewGV->getType()) {
857  BCI->replaceAllUsesWith(NewGV);
858  BCI->eraseFromParent();
859  } else {
860  BCI->setOperand(0, NewGV);
861  }
862  } else {
863  if (!TheBC)
864  TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
865  User->replaceUsesOfWith(CI, TheBC);
866  }
867  }
868 
869  Constant *RepValue = NewGV;
870  if (NewGV->getType() != GV->getType()->getElementType())
871  RepValue = ConstantExpr::getBitCast(RepValue,
872  GV->getType()->getElementType());
873 
874  // If there is a comparison against null, we will insert a global bool to
875  // keep track of whether the global was initialized yet or not.
876  GlobalVariable *InitBool =
877  new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
878  GlobalValue::InternalLinkage,
880  GV->getName()+".init", GV->getThreadLocalMode());
881  bool InitBoolUsed = false;
882 
883  // Loop over all uses of GV, processing them in turn.
884  while (!GV->use_empty()) {
885  if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
886  // The global is initialized when the store to it occurs.
887  new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
888  SI->getOrdering(), SI->getSynchScope(), SI);
889  SI->eraseFromParent();
890  continue;
891  }
892 
893  LoadInst *LI = cast<LoadInst>(GV->user_back());
894  while (!LI->use_empty()) {
895  Use &LoadUse = *LI->use_begin();
896  ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
897  if (!ICI) {
898  LoadUse = RepValue;
899  continue;
900  }
901 
902  // Replace the cmp X, 0 with a use of the bool value.
903  // Sink the load to where the compare was, if atomic rules allow us to.
904  Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
905  LI->getOrdering(), LI->getSynchScope(),
906  LI->isUnordered() ? (Instruction*)ICI : LI);
907  InitBoolUsed = true;
908  switch (ICI->getPredicate()) {
909  default: llvm_unreachable("Unknown ICmp Predicate!");
910  case ICmpInst::ICMP_ULT:
911  case ICmpInst::ICMP_SLT: // X < null -> always false
912  LV = ConstantInt::getFalse(GV->getContext());
913  break;
914  case ICmpInst::ICMP_ULE:
915  case ICmpInst::ICMP_SLE:
916  case ICmpInst::ICMP_EQ:
917  LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
918  break;
919  case ICmpInst::ICMP_NE:
920  case ICmpInst::ICMP_UGE:
921  case ICmpInst::ICMP_SGE:
922  case ICmpInst::ICMP_UGT:
923  case ICmpInst::ICMP_SGT:
924  break; // no change.
925  }
926  ICI->replaceAllUsesWith(LV);
927  ICI->eraseFromParent();
928  }
929  LI->eraseFromParent();
930  }
931 
932  // If the initialization boolean was used, insert it, otherwise delete it.
933  if (!InitBoolUsed) {
934  while (!InitBool->use_empty()) // Delete initializations
935  cast<StoreInst>(InitBool->user_back())->eraseFromParent();
936  delete InitBool;
937  } else
938  GV->getParent()->getGlobalList().insert(GV, InitBool);
939 
940  // Now the GV is dead, nuke it and the malloc..
941  GV->eraseFromParent();
942  CI->eraseFromParent();
943 
944  // To further other optimizations, loop over all users of NewGV and try to
945  // constant prop them. This will promote GEP instructions with constant
946  // indices into GEP constant-exprs, which will allow global-opt to hack on it.
947  ConstantPropUsersOf(NewGV, DL, TLI);
948  if (RepValue != NewGV)
949  ConstantPropUsersOf(RepValue, DL, TLI);
950 
951  return NewGV;
952 }
953 
954 /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
955 /// to make sure that there are no complex uses of V. We permit simple things
956 /// like dereferencing the pointer, but not storing through the address, unless
957 /// it is to the specified global.
959  const GlobalVariable *GV,
961  for (const User *U : V->users()) {
962  const Instruction *Inst = cast<Instruction>(U);
963 
964  if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
965  continue; // Fine, ignore.
966  }
967 
968  if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
969  if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
970  return false; // Storing the pointer itself... bad.
971  continue; // Otherwise, storing through it, or storing into GV... fine.
972  }
973 
974  // Must index into the array and into the struct.
975  if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
976  if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
977  return false;
978  continue;
979  }
980 
981  if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
982  // PHIs are ok if all uses are ok. Don't infinitely recurse through PHI
983  // cycles.
984  if (PHIs.insert(PN).second)
986  return false;
987  continue;
988  }
989 
990  if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
991  if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
992  return false;
993  continue;
994  }
995 
996  return false;
997  }
998  return true;
999 }
1000 
1001 /// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV
1002 /// somewhere. Transform all uses of the allocation into loads from the
1003 /// global and uses of the resultant pointer. Further, delete the store into
1004 /// GV. This assumes that these value pass the
1005 /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
1007  GlobalVariable *GV) {
1008  while (!Alloc->use_empty()) {
1009  Instruction *U = cast<Instruction>(*Alloc->user_begin());
1010  Instruction *InsertPt = U;
1011  if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1012  // If this is the store of the allocation into the global, remove it.
1013  if (SI->getOperand(1) == GV) {
1014  SI->eraseFromParent();
1015  continue;
1016  }
1017  } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
1018  // Insert the load in the corresponding predecessor, not right before the
1019  // PHI.
1020  InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator();
1021  } else if (isa<BitCastInst>(U)) {
1022  // Must be bitcast between the malloc and store to initialize the global.
1024  U->eraseFromParent();
1025  continue;
1026  } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
1027  // If this is a "GEP bitcast" and the user is a store to the global, then
1028  // just process it as a bitcast.
1029  if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
1030  if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back()))
1031  if (SI->getOperand(1) == GV) {
1032  // Must be bitcast GEP between the malloc and store to initialize
1033  // the global.
1035  GEPI->eraseFromParent();
1036  continue;
1037  }
1038  }
1039 
1040  // Insert a load from the global, and use it instead of the malloc.
1041  Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
1042  U->replaceUsesOfWith(Alloc, NL);
1043  }
1044 }
1045 
1046 /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi
1047 /// of a load) are simple enough to perform heap SRA on. This permits GEP's
1048 /// that index through the array and struct field, icmps of null, and PHIs.
1050  SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs,
1051  SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) {
1052  // We permit two users of the load: setcc comparing against the null
1053  // pointer, and a getelementptr of a specific form.
1054  for (const User *U : V->users()) {
1055  const Instruction *UI = cast<Instruction>(U);
1056 
1057  // Comparison against null is ok.
1058  if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) {
1059  if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
1060  return false;
1061  continue;
1062  }
1063 
1064  // getelementptr is also ok, but only a simple form.
1065  if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) {
1066  // Must index into the array and into the struct.
1067  if (GEPI->getNumOperands() < 3)
1068  return false;
1069 
1070  // Otherwise the GEP is ok.
1071  continue;
1072  }
1073 
1074  if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
1075  if (!LoadUsingPHIsPerLoad.insert(PN).second)
1076  // This means some phi nodes are dependent on each other.
1077  // Avoid infinite looping!
1078  return false;
1079  if (!LoadUsingPHIs.insert(PN).second)
1080  // If we have already analyzed this PHI, then it is safe.
1081  continue;
1082 
1083  // Make sure all uses of the PHI are simple enough to transform.
1085  LoadUsingPHIs, LoadUsingPHIsPerLoad))
1086  return false;
1087 
1088  continue;
1089  }
1090 
1091  // Otherwise we don't know what this is, not ok.
1092  return false;
1093  }
1094 
1095  return true;
1096 }
1097 
1098 
1099 /// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from
1100 /// GV are simple enough to perform HeapSRA, return true.
1102  Instruction *StoredVal) {
1103  SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
1104  SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
1105  for (const User *U : GV->users())
1106  if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
1107  if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
1108  LoadUsingPHIsPerLoad))
1109  return false;
1110  LoadUsingPHIsPerLoad.clear();
1111  }
1112 
1113  // If we reach here, we know that all uses of the loads and transitive uses
1114  // (through PHI nodes) are simple enough to transform. However, we don't know
1115  // that all inputs the to the PHI nodes are in the same equivalence sets.
1116  // Check to verify that all operands of the PHIs are either PHIS that can be
1117  // transformed, loads from GV, or MI itself.
1118  for (const PHINode *PN : LoadUsingPHIs) {
1119  for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
1120  Value *InVal = PN->getIncomingValue(op);
1121 
1122  // PHI of the stored value itself is ok.
1123  if (InVal == StoredVal) continue;
1124 
1125  if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
1126  // One of the PHIs in our set is (optimistically) ok.
1127  if (LoadUsingPHIs.count(InPN))
1128  continue;
1129  return false;
1130  }
1131 
1132  // Load from GV is ok.
1133  if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
1134  if (LI->getOperand(0) == GV)
1135  continue;
1136 
1137  // UNDEF? NULL?
1138 
1139  // Anything else is rejected.
1140  return false;
1141  }
1142  }
1143 
1144  return true;
1145 }
1146 
1147 static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
1148  DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1149  std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1150  std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
1151 
1152  if (FieldNo >= FieldVals.size())
1153  FieldVals.resize(FieldNo+1);
1154 
1155  // If we already have this value, just reuse the previously scalarized
1156  // version.
1157  if (Value *FieldVal = FieldVals[FieldNo])
1158  return FieldVal;
1159 
1160  // Depending on what instruction this is, we have several cases.
1161  Value *Result;
1162  if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
1163  // This is a scalarized version of the load from the global. Just create
1164  // a new Load of the scalarized global.
1165  Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
1166  InsertedScalarizedValues,
1167  PHIsToRewrite),
1168  LI->getName()+".f"+Twine(FieldNo), LI);
1169  } else {
1170  PHINode *PN = cast<PHINode>(V);
1171  // PN's type is pointer to struct. Make a new PHI of pointer to struct
1172  // field.
1173 
1174  PointerType *PTy = cast<PointerType>(PN->getType());
1175  StructType *ST = cast<StructType>(PTy->getElementType());
1176 
1177  unsigned AS = PTy->getAddressSpace();
1178  PHINode *NewPN =
1179  PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS),
1180  PN->getNumIncomingValues(),
1181  PN->getName()+".f"+Twine(FieldNo), PN);
1182  Result = NewPN;
1183  PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
1184  }
1185 
1186  return FieldVals[FieldNo] = Result;
1187 }
1188 
1189 /// RewriteHeapSROALoadUser - Given a load instruction and a value derived from
1190 /// the load, rewrite the derived value to use the HeapSRoA'd load.
1191 static void RewriteHeapSROALoadUser(Instruction *LoadUser,
1192  DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1193  std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1194  // If this is a comparison against null, handle it.
1195  if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
1196  assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
1197  // If we have a setcc of the loaded pointer, we can use a setcc of any
1198  // field.
1199  Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
1200  InsertedScalarizedValues, PHIsToRewrite);
1201 
1202  Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
1204  SCI->getName());
1205  SCI->replaceAllUsesWith(New);
1206  SCI->eraseFromParent();
1207  return;
1208  }
1209 
1210  // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
1211  if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
1212  assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
1213  && "Unexpected GEPI!");
1214 
1215  // Load the pointer for this field.
1216  unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
1217  Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
1218  InsertedScalarizedValues, PHIsToRewrite);
1219 
1220  // Create the new GEP idx vector.
1221  SmallVector<Value*, 8> GEPIdx;
1222  GEPIdx.push_back(GEPI->getOperand(1));
1223  GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
1224 
1225  Value *NGEPI = GetElementPtrInst::Create(GEPI->getResultElementType(), NewPtr, GEPIdx,
1226  GEPI->getName(), GEPI);
1227  GEPI->replaceAllUsesWith(NGEPI);
1228  GEPI->eraseFromParent();
1229  return;
1230  }
1231 
1232  // Recursively transform the users of PHI nodes. This will lazily create the
1233  // PHIs that are needed for individual elements. Keep track of what PHIs we
1234  // see in InsertedScalarizedValues so that we don't get infinite loops (very
1235  // antisocial). If the PHI is already in InsertedScalarizedValues, it has
1236  // already been seen first by another load, so its uses have already been
1237  // processed.
1238  PHINode *PN = cast<PHINode>(LoadUser);
1239  if (!InsertedScalarizedValues.insert(std::make_pair(PN,
1240  std::vector<Value*>())).second)
1241  return;
1242 
1243  // If this is the first time we've seen this PHI, recursively process all
1244  // users.
1245  for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
1246  Instruction *User = cast<Instruction>(*UI++);
1247  RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1248  }
1249 }
1250 
1251 /// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr
1252 /// is a value loaded from the global. Eliminate all uses of Ptr, making them
1253 /// use FieldGlobals instead. All uses of loaded values satisfy
1254 /// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
1256  DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1257  std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1258  for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) {
1259  Instruction *User = cast<Instruction>(*UI++);
1260  RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1261  }
1262 
1263  if (Load->use_empty()) {
1264  Load->eraseFromParent();
1265  InsertedScalarizedValues.erase(Load);
1266  }
1267 }
1268 
1269 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
1270 /// it up into multiple allocations of arrays of the fields.
1272  Value *NElems, const DataLayout &DL,
1273  const TargetLibraryInfo *TLI) {
1274  DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n');
1275  Type *MAT = getMallocAllocatedType(CI, TLI);
1276  StructType *STy = cast<StructType>(MAT);
1277 
1278  // There is guaranteed to be at least one use of the malloc (storing
1279  // it into GV). If there are other uses, change them to be uses of
1280  // the global to simplify later code. This also deletes the store
1281  // into GV.
1283 
1284  // Okay, at this point, there are no users of the malloc. Insert N
1285  // new mallocs at the same place as CI, and N globals.
1286  std::vector<Value*> FieldGlobals;
1287  std::vector<Value*> FieldMallocs;
1288 
1289  unsigned AS = GV->getType()->getPointerAddressSpace();
1290  for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
1291  Type *FieldTy = STy->getElementType(FieldNo);
1292  PointerType *PFieldTy = PointerType::get(FieldTy, AS);
1293 
1294  GlobalVariable *NGV =
1295  new GlobalVariable(*GV->getParent(),
1296  PFieldTy, false, GlobalValue::InternalLinkage,
1297  Constant::getNullValue(PFieldTy),
1298  GV->getName() + ".f" + Twine(FieldNo), GV,
1299  GV->getThreadLocalMode());
1300  FieldGlobals.push_back(NGV);
1301 
1302  unsigned TypeSize = DL.getTypeAllocSize(FieldTy);
1303  if (StructType *ST = dyn_cast<StructType>(FieldTy))
1304  TypeSize = DL.getStructLayout(ST)->getSizeInBytes();
1305  Type *IntPtrTy = DL.getIntPtrType(CI->getType());
1306  Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1307  ConstantInt::get(IntPtrTy, TypeSize),
1308  NElems, nullptr,
1309  CI->getName() + ".f" + Twine(FieldNo));
1310  FieldMallocs.push_back(NMI);
1311  new StoreInst(NMI, NGV, CI);
1312  }
1313 
1314  // The tricky aspect of this transformation is handling the case when malloc
1315  // fails. In the original code, malloc failing would set the result pointer
1316  // of malloc to null. In this case, some mallocs could succeed and others
1317  // could fail. As such, we emit code that looks like this:
1318  // F0 = malloc(field0)
1319  // F1 = malloc(field1)
1320  // F2 = malloc(field2)
1321  // if (F0 == 0 || F1 == 0 || F2 == 0) {
1322  // if (F0) { free(F0); F0 = 0; }
1323  // if (F1) { free(F1); F1 = 0; }
1324  // if (F2) { free(F2); F2 = 0; }
1325  // }
1326  // The malloc can also fail if its argument is too large.
1327  Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
1328  Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
1329  ConstantZero, "isneg");
1330  for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1331  Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1332  Constant::getNullValue(FieldMallocs[i]->getType()),
1333  "isnull");
1334  RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
1335  }
1336 
1337  // Split the basic block at the old malloc.
1338  BasicBlock *OrigBB = CI->getParent();
1339  BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
1340 
1341  // Create the block to check the first condition. Put all these blocks at the
1342  // end of the function as they are unlikely to be executed.
1343  BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
1344  "malloc_ret_null",
1345  OrigBB->getParent());
1346 
1347  // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
1348  // branch on RunningOr.
1349  OrigBB->getTerminator()->eraseFromParent();
1350  BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
1351 
1352  // Within the NullPtrBlock, we need to emit a comparison and branch for each
1353  // pointer, because some may be null while others are not.
1354  for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1355  Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
1356  Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1357  Constant::getNullValue(GVVal->getType()));
1358  BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
1359  OrigBB->getParent());
1360  BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
1361  OrigBB->getParent());
1362  Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
1363  Cmp, NullPtrBlock);
1364 
1365  // Fill in FreeBlock.
1366  CallInst::CreateFree(GVVal, BI);
1367  new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
1368  FreeBlock);
1369  BranchInst::Create(NextBlock, FreeBlock);
1370 
1371  NullPtrBlock = NextBlock;
1372  }
1373 
1374  BranchInst::Create(ContBB, NullPtrBlock);
1375 
1376  // CI is no longer needed, remove it.
1377  CI->eraseFromParent();
1378 
1379  /// InsertedScalarizedLoads - As we process loads, if we can't immediately
1380  /// update all uses of the load, keep track of what scalarized loads are
1381  /// inserted for a given load.
1382  DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
1383  InsertedScalarizedValues[GV] = FieldGlobals;
1384 
1385  std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
1386 
1387  // Okay, the malloc site is completely handled. All of the uses of GV are now
1388  // loads, and all uses of those loads are simple. Rewrite them to use loads
1389  // of the per-field globals instead.
1390  for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) {
1391  Instruction *User = cast<Instruction>(*UI++);
1392 
1393  if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1394  RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
1395  continue;
1396  }
1397 
1398  // Must be a store of null.
1399  StoreInst *SI = cast<StoreInst>(User);
1400  assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
1401  "Unexpected heap-sra user!");
1402 
1403  // Insert a store of null into each global.
1404  for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1405  PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType());
1407  new StoreInst(Null, FieldGlobals[i], SI);
1408  }
1409  // Erase the original store.
1410  SI->eraseFromParent();
1411  }
1412 
1413  // While we have PHIs that are interesting to rewrite, do it.
1414  while (!PHIsToRewrite.empty()) {
1415  PHINode *PN = PHIsToRewrite.back().first;
1416  unsigned FieldNo = PHIsToRewrite.back().second;
1417  PHIsToRewrite.pop_back();
1418  PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
1419  assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
1420 
1421  // Add all the incoming values. This can materialize more phis.
1422  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1423  Value *InVal = PN->getIncomingValue(i);
1424  InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
1425  PHIsToRewrite);
1426  FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
1427  }
1428  }
1429 
1430  // Drop all inter-phi links and any loads that made it this far.
1431  for (DenseMap<Value*, std::vector<Value*> >::iterator
1432  I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1433  I != E; ++I) {
1434  if (PHINode *PN = dyn_cast<PHINode>(I->first))
1435  PN->dropAllReferences();
1436  else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1437  LI->dropAllReferences();
1438  }
1439 
1440  // Delete all the phis and loads now that inter-references are dead.
1441  for (DenseMap<Value*, std::vector<Value*> >::iterator
1442  I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1443  I != E; ++I) {
1444  if (PHINode *PN = dyn_cast<PHINode>(I->first))
1445  PN->eraseFromParent();
1446  else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1447  LI->eraseFromParent();
1448  }
1449 
1450  // The old global is now dead, remove it.
1451  GV->eraseFromParent();
1452 
1453  ++NumHeapSRA;
1454  return cast<GlobalVariable>(FieldGlobals[0]);
1455 }
1456 
1457 /// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a
1458 /// pointer global variable with a single value stored it that is a malloc or
1459 /// cast of malloc.
1461  Type *AllocTy,
1462  AtomicOrdering Ordering,
1464  const DataLayout &DL,
1465  TargetLibraryInfo *TLI) {
1466  // If this is a malloc of an abstract type, don't touch it.
1467  if (!AllocTy->isSized())
1468  return false;
1469 
1470  // We can't optimize this global unless all uses of it are *known* to be
1471  // of the malloc value, not of the null initializer value (consider a use
1472  // that compares the global's value against zero to see if the malloc has
1473  // been reached). To do this, we check to see if all uses of the global
1474  // would trap if the global were null: this proves that they must all
1475  // happen after the malloc.
1477  return false;
1478 
1479  // We can't optimize this if the malloc itself is used in a complex way,
1480  // for example, being stored into multiple globals. This allows the
1481  // malloc to be stored into the specified global, loaded icmp'd, and
1482  // GEP'd. These are all things we could transform to using the global
1483  // for.
1485  if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
1486  return false;
1487 
1488  // If we have a global that is only initialized with a fixed size malloc,
1489  // transform the program to use global memory instead of malloc'd memory.
1490  // This eliminates dynamic allocation, avoids an indirection accessing the
1491  // data, and exposes the resultant global to further GlobalOpt.
1492  // We cannot optimize the malloc if we cannot determine malloc array size.
1493  Value *NElems = getMallocArraySize(CI, DL, TLI, true);
1494  if (!NElems)
1495  return false;
1496 
1497  if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
1498  // Restrict this transformation to only working on small allocations
1499  // (2048 bytes currently), as we don't want to introduce a 16M global or
1500  // something.
1501  if (NElements->getZExtValue() * DL.getTypeAllocSize(AllocTy) < 2048) {
1502  GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI);
1503  return true;
1504  }
1505 
1506  // If the allocation is an array of structures, consider transforming this
1507  // into multiple malloc'd arrays, one for each field. This is basically
1508  // SRoA for malloc'd memory.
1509 
1510  if (Ordering != NotAtomic)
1511  return false;
1512 
1513  // If this is an allocation of a fixed size array of structs, analyze as a
1514  // variable size array. malloc [100 x struct],1 -> malloc struct, 100
1515  if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
1516  if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
1517  AllocTy = AT->getElementType();
1518 
1519  StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
1520  if (!AllocSTy)
1521  return false;
1522 
1523  // This the structure has an unreasonable number of fields, leave it
1524  // alone.
1525  if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1527 
1528  // If this is a fixed size array, transform the Malloc to be an alloc of
1529  // structs. malloc [100 x struct],1 -> malloc struct, 100
1530  if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
1531  Type *IntPtrTy = DL.getIntPtrType(CI->getType());
1532  unsigned TypeSize = DL.getStructLayout(AllocSTy)->getSizeInBytes();
1533  Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1534  Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1535  Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
1536  AllocSize, NumElements,
1537  nullptr, CI->getName());
1538  Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1539  CI->replaceAllUsesWith(Cast);
1540  CI->eraseFromParent();
1541  if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
1542  CI = cast<CallInst>(BCI->getOperand(0));
1543  else
1544  CI = cast<CallInst>(Malloc);
1545  }
1546 
1547  GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true),
1548  DL, TLI);
1549  return true;
1550  }
1551 
1552  return false;
1553 }
1554 
1555 // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
1556 // that only one value (besides its initializer) is ever stored to the global.
1557 static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
1558  AtomicOrdering Ordering,
1560  const DataLayout &DL,
1561  TargetLibraryInfo *TLI) {
1562  // Ignore no-op GEPs and bitcasts.
1563  StoredOnceVal = StoredOnceVal->stripPointerCasts();
1564 
1565  // If we are dealing with a pointer global that is initialized to null and
1566  // only has one (non-null) value stored into it, then we can optimize any
1567  // users of the loaded value (often calls and loads) that would trap if the
1568  // value was null.
1569  if (GV->getInitializer()->getType()->isPointerTy() &&
1570  GV->getInitializer()->isNullValue()) {
1571  if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1572  if (GV->getInitializer()->getType() != SOVC->getType())
1573  SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
1574 
1575  // Optimize away any trapping uses of the loaded value.
1576  if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, TLI))
1577  return true;
1578  } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
1579  Type *MallocType = getMallocAllocatedType(CI, TLI);
1580  if (MallocType &&
1581  TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI,
1582  DL, TLI))
1583  return true;
1584  }
1585  }
1586 
1587  return false;
1588 }
1589 
1590 /// TryToShrinkGlobalToBoolean - At this point, we have learned that the only
1591 /// two values ever stored into GV are its initializer and OtherVal. See if we
1592 /// can shrink the global into a boolean and select between the two values
1593 /// whenever it is used. This exposes the values to other scalar optimizations.
1595  Type *GVElType = GV->getType()->getElementType();
1596 
1597  // If GVElType is already i1, it is already shrunk. If the type of the GV is
1598  // an FP value, pointer or vector, don't do this optimization because a select
1599  // between them is very expensive and unlikely to lead to later
1600  // simplification. In these cases, we typically end up with "cond ? v1 : v2"
1601  // where v1 and v2 both require constant pool loads, a big loss.
1602  if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1603  GVElType->isFloatingPointTy() ||
1604  GVElType->isPointerTy() || GVElType->isVectorTy())
1605  return false;
1606 
1607  // Walk the use list of the global seeing if all the uses are load or store.
1608  // If there is anything else, bail out.
1609  for (User *U : GV->users())
1610  if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1611  return false;
1612 
1613  DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV);
1614 
1615  // Create the new global, initializing it to false.
1617  false,
1620  GV->getName()+".b",
1621  GV->getThreadLocalMode(),
1622  GV->getType()->getAddressSpace());
1623  GV->getParent()->getGlobalList().insert(GV, NewGV);
1624 
1625  Constant *InitVal = GV->getInitializer();
1626  assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1627  "No reason to shrink to bool!");
1628 
1629  // If initialized to zero and storing one into the global, we can use a cast
1630  // instead of a select to synthesize the desired value.
1631  bool IsOneZero = false;
1632  if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
1633  IsOneZero = InitVal->isNullValue() && CI->isOne();
1634 
1635  while (!GV->use_empty()) {
1636  Instruction *UI = cast<Instruction>(GV->user_back());
1637  if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1638  // Change the store into a boolean store.
1639  bool StoringOther = SI->getOperand(0) == OtherVal;
1640  // Only do this if we weren't storing a loaded value.
1641  Value *StoreVal;
1642  if (StoringOther || SI->getOperand(0) == InitVal) {
1643  StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1644  StoringOther);
1645  } else {
1646  // Otherwise, we are storing a previously loaded copy. To do this,
1647  // change the copy from copying the original value to just copying the
1648  // bool.
1649  Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
1650 
1651  // If we've already replaced the input, StoredVal will be a cast or
1652  // select instruction. If not, it will be a load of the original
1653  // global.
1654  if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
1655  assert(LI->getOperand(0) == GV && "Not a copy!");
1656  // Insert a new load, to preserve the saved value.
1657  StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1658  LI->getOrdering(), LI->getSynchScope(), LI);
1659  } else {
1660  assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
1661  "This is not a form that we understand!");
1662  StoreVal = StoredVal->getOperand(0);
1663  assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
1664  }
1665  }
1666  new StoreInst(StoreVal, NewGV, false, 0,
1667  SI->getOrdering(), SI->getSynchScope(), SI);
1668  } else {
1669  // Change the load into a load of bool then a select.
1670  LoadInst *LI = cast<LoadInst>(UI);
1671  LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1672  LI->getOrdering(), LI->getSynchScope(), LI);
1673  Value *NSI;
1674  if (IsOneZero)
1675  NSI = new ZExtInst(NLI, LI->getType(), "", LI);
1676  else
1677  NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
1678  NSI->takeName(LI);
1679  LI->replaceAllUsesWith(NSI);
1680  }
1681  UI->eraseFromParent();
1682  }
1683 
1684  // Retain the name of the old global variable. People who are debugging their
1685  // programs may expect these variables to be named the same.
1686  NewGV->takeName(GV);
1687  GV->eraseFromParent();
1688  return true;
1689 }
1690 
1691 
1692 /// ProcessGlobal - Analyze the specified global variable and optimize it if
1693 /// possible. If we make a change, return true.
1694 bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
1695  Module::global_iterator &GVI) {
1696  // Do more involved optimizations if the global is internal.
1698 
1699  if (GV->use_empty()) {
1700  DEBUG(dbgs() << "GLOBAL DEAD: " << *GV);
1701  GV->eraseFromParent();
1702  ++NumDeleted;
1703  return true;
1704  }
1705 
1706  if (!GV->hasLocalLinkage())
1707  return false;
1708 
1709  GlobalStatus GS;
1710 
1711  if (GlobalStatus::analyzeGlobal(GV, GS))
1712  return false;
1713 
1714  if (!GS.IsCompared && !GV->hasUnnamedAddr()) {
1715  GV->setUnnamedAddr(true);
1716  NumUnnamed++;
1717  }
1718 
1719  if (GV->isConstant() || !GV->hasInitializer())
1720  return false;
1721 
1722  return ProcessInternalGlobal(GV, GVI, GS);
1723 }
1724 
1725 /// ProcessInternalGlobal - Analyze the specified global variable and optimize
1726 /// it if possible. If we make a change, return true.
1727 bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
1729  const GlobalStatus &GS) {
1730  auto &DL = GV->getParent()->getDataLayout();
1731  // If this is a first class global and has only one accessing function
1732  // and this function is main (which we know is not recursive), we replace
1733  // the global with a local alloca in this function.
1734  //
1735  // NOTE: It doesn't make sense to promote non-single-value types since we
1736  // are just replacing static memory to stack memory.
1737  //
1738  // If the global is in different address space, don't bring it to stack.
1742  GS.AccessingFunction->getName() == "main" &&
1744  GV->getType()->getAddressSpace() == 0) {
1745  DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV);
1746  Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
1747  ->getEntryBlock().begin());
1748  Type *ElemTy = GV->getType()->getElementType();
1749  // FIXME: Pass Global's alignment when globals have alignment
1750  AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
1751  GV->getName(), &FirstI);
1752  if (!isa<UndefValue>(GV->getInitializer()))
1753  new StoreInst(GV->getInitializer(), Alloca, &FirstI);
1754 
1755  GV->replaceAllUsesWith(Alloca);
1756  GV->eraseFromParent();
1757  ++NumLocalized;
1758  return true;
1759  }
1760 
1761  // If the global is never loaded (but may be stored to), it is dead.
1762  // Delete it now.
1763  if (!GS.IsLoaded) {
1764  DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV);
1765 
1766  bool Changed;
1767  if (isLeakCheckerRoot(GV)) {
1768  // Delete any constant stores to the global.
1769  Changed = CleanupPointerRootUsers(GV, TLI);
1770  } else {
1771  // Delete any stores we can find to the global. We may not be able to
1772  // make it completely dead though.
1773  Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1774  }
1775 
1776  // If the global is dead now, delete it.
1777  if (GV->use_empty()) {
1778  GV->eraseFromParent();
1779  ++NumDeleted;
1780  Changed = true;
1781  }
1782  return Changed;
1783 
1784  } else if (GS.StoredType <= GlobalStatus::InitializerStored) {
1785  DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1786  GV->setConstant(true);
1787 
1788  // Clean up any obviously simplifiable users now.
1789  CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1790 
1791  // If the global is dead now, just nuke it.
1792  if (GV->use_empty()) {
1793  DEBUG(dbgs() << " *** Marking constant allowed us to simplify "
1794  << "all users and delete global!\n");
1795  GV->eraseFromParent();
1796  ++NumDeleted;
1797  }
1798 
1799  ++NumMarked;
1800  return true;
1801  } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
1802  const DataLayout &DL = GV->getParent()->getDataLayout();
1803  if (GlobalVariable *FirstNewGV = SRAGlobal(GV, DL)) {
1804  GVI = FirstNewGV; // Don't skip the newly produced globals!
1805  return true;
1806  }
1807  } else if (GS.StoredType == GlobalStatus::StoredOnce) {
1808  // If the initial value for the global was an undef value, and if only
1809  // one other value was stored into it, we can just change the
1810  // initializer to be the stored value, then delete all stores to the
1811  // global. This allows us to mark it constant.
1812  if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
1813  if (isa<UndefValue>(GV->getInitializer())) {
1814  // Change the initial value here.
1815  GV->setInitializer(SOVConstant);
1816 
1817  // Clean up any obviously simplifiable users now.
1818  CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1819 
1820  if (GV->use_empty()) {
1821  DEBUG(dbgs() << " *** Substituting initializer allowed us to "
1822  << "simplify all users and delete global!\n");
1823  GV->eraseFromParent();
1824  ++NumDeleted;
1825  } else {
1826  GVI = GV;
1827  }
1828  ++NumSubstitute;
1829  return true;
1830  }
1831 
1832  // Try to optimize globals based on the knowledge that only one value
1833  // (besides its initializer) is ever stored to the global.
1835  DL, TLI))
1836  return true;
1837 
1838  // Otherwise, if the global was not a boolean, we can shrink it to be a
1839  // boolean.
1840  if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) {
1841  if (GS.Ordering == NotAtomic) {
1842  if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1843  ++NumShrunkToBool;
1844  return true;
1845  }
1846  }
1847  }
1848  }
1849 
1850  return false;
1851 }
1852 
1853 /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
1854 /// function, changing them to FastCC.
1856  for (User *U : F->users()) {
1857  if (isa<BlockAddress>(U))
1858  continue;
1859  CallSite CS(cast<Instruction>(U));
1861  }
1862 }
1863 
1865  for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
1866  unsigned Index = Attrs.getSlotIndex(i);
1867  if (!Attrs.getSlotAttributes(i).hasAttribute(Index, Attribute::Nest))
1868  continue;
1869 
1870  // There can be only one.
1871  return Attrs.removeAttribute(C, Index, Attribute::Nest);
1872  }
1873 
1874  return Attrs;
1875 }
1876 
1879  for (User *U : F->users()) {
1880  if (isa<BlockAddress>(U))
1881  continue;
1882  CallSite CS(cast<Instruction>(U));
1884  }
1885 }
1886 
1887 /// Return true if this is a calling convention that we'd like to change. The
1888 /// idea here is that we don't want to mess with the convention if the user
1889 /// explicitly requested something with performance implications like coldcc,
1890 /// GHC, or anyregcc.
1892  CallingConv::ID CC = F->getCallingConv();
1893  // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
1894  return CC == CallingConv::C || CC == CallingConv::X86_ThisCall;
1895 }
1896 
1897 bool GlobalOpt::OptimizeFunctions(Module &M) {
1898  bool Changed = false;
1899  // Optimize functions.
1900  for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
1901  Function *F = FI++;
1902  // Functions without names cannot be referenced outside this module.
1903  if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
1905 
1906  const Comdat *C = F->getComdat();
1907  bool inComdat = C && NotDiscardableComdats.count(C);
1909  if ((!inComdat || F->hasLocalLinkage()) && F->isDefTriviallyDead()) {
1910  F->eraseFromParent();
1911  Changed = true;
1912  ++NumFnDeleted;
1913  } else if (F->hasLocalLinkage()) {
1914  if (isProfitableToMakeFastCC(F) && !F->isVarArg() &&
1915  !F->hasAddressTaken()) {
1916  // If this function has a calling convention worth changing, is not a
1917  // varargs function, and is only called directly, promote it to use the
1918  // Fast calling convention.
1921  ++NumFastCallFns;
1922  Changed = true;
1923  }
1924 
1926  !F->hasAddressTaken()) {
1927  // The function is not used by a trampoline intrinsic, so it is safe
1928  // to remove the 'nest' attribute.
1930  ++NumNestRemoved;
1931  Changed = true;
1932  }
1933  }
1934  }
1935  return Changed;
1936 }
1937 
1938 bool GlobalOpt::OptimizeGlobalVars(Module &M) {
1939  bool Changed = false;
1940 
1941  for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1942  GVI != E; ) {
1943  GlobalVariable *GV = GVI++;
1944  // Global variables without names cannot be referenced outside this module.
1945  if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage())
1947  // Simplify the initializer.
1948  if (GV->hasInitializer())
1949  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
1950  auto &DL = M.getDataLayout();
1951  Constant *New = ConstantFoldConstantExpression(CE, DL, TLI);
1952  if (New && New != CE)
1953  GV->setInitializer(New);
1954  }
1955 
1956  if (GV->isDiscardableIfUnused()) {
1957  if (const Comdat *C = GV->getComdat())
1958  if (NotDiscardableComdats.count(C) && !GV->hasLocalLinkage())
1959  continue;
1960  Changed |= ProcessGlobal(GV, GVI);
1961  }
1962  }
1963  return Changed;
1964 }
1965 
1966 static inline bool
1968  SmallPtrSetImpl<Constant *> &SimpleConstants,
1969  const DataLayout &DL);
1970 
1971 /// isSimpleEnoughValueToCommit - Return true if the specified constant can be
1972 /// handled by the code generator. We don't want to generate something like:
1973 /// void *X = &X/42;
1974 /// because the code generator doesn't have a relocation that can handle that.
1975 ///
1976 /// This function should be called if C was not found (but just got inserted)
1977 /// in SimpleConstants to avoid having to rescan the same constants all the
1978 /// time.
1979 static bool
1981  SmallPtrSetImpl<Constant *> &SimpleConstants,
1982  const DataLayout &DL) {
1983  // Simple global addresses are supported, do not allow dllimport or
1984  // thread-local globals.
1985  if (auto *GV = dyn_cast<GlobalValue>(C))
1986  return !GV->hasDLLImportStorageClass() && !GV->isThreadLocal();
1987 
1988  // Simple integer, undef, constant aggregate zero, etc are all supported.
1989  if (C->getNumOperands() == 0 || isa<BlockAddress>(C))
1990  return true;
1991 
1992  // Aggregate values are safe if all their elements are.
1993  if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1994  isa<ConstantVector>(C)) {
1995  for (Value *Op : C->operands())
1996  if (!isSimpleEnoughValueToCommit(cast<Constant>(Op), SimpleConstants, DL))
1997  return false;
1998  return true;
1999  }
2000 
2001  // We don't know exactly what relocations are allowed in constant expressions,
2002  // so we allow &global+constantoffset, which is safe and uniformly supported
2003  // across targets.
2004  ConstantExpr *CE = cast<ConstantExpr>(C);
2005  switch (CE->getOpcode()) {
2006  case Instruction::BitCast:
2007  // Bitcast is fine if the casted value is fine.
2008  return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2009 
2010  case Instruction::IntToPtr:
2011  case Instruction::PtrToInt:
2012  // int <=> ptr is fine if the int type is the same size as the
2013  // pointer type.
2014  if (DL.getTypeSizeInBits(CE->getType()) !=
2015  DL.getTypeSizeInBits(CE->getOperand(0)->getType()))
2016  return false;
2017  return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2018 
2019  // GEP is fine if it is simple + constant offset.
2020  case Instruction::GetElementPtr:
2021  for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
2022  if (!isa<ConstantInt>(CE->getOperand(i)))
2023  return false;
2024  return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2025 
2026  case Instruction::Add:
2027  // We allow simple+cst.
2028  if (!isa<ConstantInt>(CE->getOperand(1)))
2029  return false;
2030  return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, DL);
2031  }
2032  return false;
2033 }
2034 
2035 static inline bool
2037  SmallPtrSetImpl<Constant *> &SimpleConstants,
2038  const DataLayout &DL) {
2039  // If we already checked this constant, we win.
2040  if (!SimpleConstants.insert(C).second)
2041  return true;
2042  // Check the constant.
2043  return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, DL);
2044 }
2045 
2046 
2047 /// isSimpleEnoughPointerToCommit - Return true if this constant is simple
2048 /// enough for us to understand. In particular, if it is a cast to anything
2049 /// other than from one pointer type to another pointer type, we punt.
2050 /// We basically just support direct accesses to globals and GEP's of
2051 /// globals. This should be kept up to date with CommitValueTo.
2053  // Conservatively, avoid aggregate types. This is because we don't
2054  // want to worry about them partially overlapping other stores.
2055  if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
2056  return false;
2057 
2058  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
2059  // Do not allow weak/*_odr/linkonce linkage or external globals.
2060  return GV->hasUniqueInitializer();
2061 
2062  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2063  // Handle a constantexpr gep.
2064  if (CE->getOpcode() == Instruction::GetElementPtr &&
2065  isa<GlobalVariable>(CE->getOperand(0)) &&
2066  cast<GEPOperator>(CE)->isInBounds()) {
2067  GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2068  // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
2069  // external globals.
2070  if (!GV->hasUniqueInitializer())
2071  return false;
2072 
2073  // The first index must be zero.
2074  ConstantInt *CI = dyn_cast<ConstantInt>(*std::next(CE->op_begin()));
2075  if (!CI || !CI->isZero()) return false;
2076 
2077  // The remaining indices must be compile-time known integers within the
2078  // notional bounds of the corresponding static array types.
2079  if (!CE->isGEPWithNoNotionalOverIndexing())
2080  return false;
2081 
2083 
2084  // A constantexpr bitcast from a pointer to another pointer is a no-op,
2085  // and we know how to evaluate it by moving the bitcast from the pointer
2086  // operand to the value operand.
2087  } else if (CE->getOpcode() == Instruction::BitCast &&
2088  isa<GlobalVariable>(CE->getOperand(0))) {
2089  // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
2090  // external globals.
2091  return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
2092  }
2093  }
2094 
2095  return false;
2096 }
2097 
2098 /// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global
2099 /// initializer. This returns 'Init' modified to reflect 'Val' stored into it.
2100 /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
2102  ConstantExpr *Addr, unsigned OpNo) {
2103  // Base case of the recursion.
2104  if (OpNo == Addr->getNumOperands()) {
2105  assert(Val->getType() == Init->getType() && "Type mismatch!");
2106  return Val;
2107  }
2108 
2110  if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
2111  // Break up the constant into its elements.
2112  for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2113  Elts.push_back(Init->getAggregateElement(i));
2114 
2115  // Replace the element that we are supposed to.
2116  ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
2117  unsigned Idx = CU->getZExtValue();
2118  assert(Idx < STy->getNumElements() && "Struct index out of range!");
2119  Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
2120 
2121  // Return the modified struct.
2122  return ConstantStruct::get(STy, Elts);
2123  }
2124 
2125  ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
2126  SequentialType *InitTy = cast<SequentialType>(Init->getType());
2127 
2128  uint64_t NumElts;
2129  if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy))
2130  NumElts = ATy->getNumElements();
2131  else
2132  NumElts = InitTy->getVectorNumElements();
2133 
2134  // Break up the array into elements.
2135  for (uint64_t i = 0, e = NumElts; i != e; ++i)
2136  Elts.push_back(Init->getAggregateElement(i));
2137 
2138  assert(CI->getZExtValue() < NumElts);
2139  Elts[CI->getZExtValue()] =
2140  EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
2141 
2142  if (Init->getType()->isArrayTy())
2143  return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
2144  return ConstantVector::get(Elts);
2145 }
2146 
2147 /// CommitValueTo - We have decided that Addr (which satisfies the predicate
2148 /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen.
2149 static void CommitValueTo(Constant *Val, Constant *Addr) {
2150  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
2151  assert(GV->hasInitializer());
2152  GV->setInitializer(Val);
2153  return;
2154  }
2155 
2156  ConstantExpr *CE = cast<ConstantExpr>(Addr);
2157  GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2158  GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
2159 }
2160 
2161 namespace {
2162 
2163 /// Evaluator - This class evaluates LLVM IR, producing the Constant
2164 /// representing each SSA instruction. Changes to global variables are stored
2165 /// in a mapping that can be iterated over after the evaluation is complete.
2166 /// Once an evaluation call fails, the evaluation object should not be reused.
2167 class Evaluator {
2168 public:
2169  Evaluator(const DataLayout &DL, const TargetLibraryInfo *TLI)
2170  : DL(DL), TLI(TLI) {
2171  ValueStack.emplace_back();
2172  }
2173 
2174  ~Evaluator() {
2175  for (auto &Tmp : AllocaTmps)
2176  // If there are still users of the alloca, the program is doing something
2177  // silly, e.g. storing the address of the alloca somewhere and using it
2178  // later. Since this is undefined, we'll just make it be null.
2179  if (!Tmp->use_empty())
2180  Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
2181  }
2182 
2183  /// EvaluateFunction - Evaluate a call to function F, returning true if
2184  /// successful, false if we can't evaluate it. ActualArgs contains the formal
2185  /// arguments for the function.
2186  bool EvaluateFunction(Function *F, Constant *&RetVal,
2187  const SmallVectorImpl<Constant*> &ActualArgs);
2188 
2189  /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
2190  /// successful, false if we can't evaluate it. NewBB returns the next BB that
2191  /// control flows into, or null upon return.
2192  bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
2193 
2194  Constant *getVal(Value *V) {
2195  if (Constant *CV = dyn_cast<Constant>(V)) return CV;
2196  Constant *R = ValueStack.back().lookup(V);
2197  assert(R && "Reference to an uncomputed value!");
2198  return R;
2199  }
2200 
2201  void setVal(Value *V, Constant *C) {
2202  ValueStack.back()[V] = C;
2203  }
2204 
2205  const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
2206  return MutatedMemory;
2207  }
2208 
2209  const SmallPtrSetImpl<GlobalVariable*> &getInvariants() const {
2210  return Invariants;
2211  }
2212 
2213 private:
2214  Constant *ComputeLoadResult(Constant *P);
2215 
2216  /// ValueStack - As we compute SSA register values, we store their contents
2217  /// here. The back of the deque contains the current function and the stack
2218  /// contains the values in the calling frames.
2219  std::deque<DenseMap<Value*, Constant*>> ValueStack;
2220 
2221  /// CallStack - This is used to detect recursion. In pathological situations
2222  /// we could hit exponential behavior, but at least there is nothing
2223  /// unbounded.
2224  SmallVector<Function*, 4> CallStack;
2225 
2226  /// MutatedMemory - For each store we execute, we update this map. Loads
2227  /// check this to get the most up-to-date value. If evaluation is successful,
2228  /// this state is committed to the process.
2229  DenseMap<Constant*, Constant*> MutatedMemory;
2230 
2231  /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
2232  /// to represent its body. This vector is needed so we can delete the
2233  /// temporary globals when we are done.
2235 
2236  /// Invariants - These global variables have been marked invariant by the
2237  /// static constructor.
2239 
2240  /// SimpleConstants - These are constants we have checked and know to be
2241  /// simple enough to live in a static initializer of a global.
2242  SmallPtrSet<Constant*, 8> SimpleConstants;
2243 
2244  const DataLayout &DL;
2245  const TargetLibraryInfo *TLI;
2246 };
2247 
2248 } // anonymous namespace
2249 
2250 /// ComputeLoadResult - Return the value that would be computed by a load from
2251 /// P after the stores reflected by 'memory' have been performed. If we can't
2252 /// decide, return null.
2253 Constant *Evaluator::ComputeLoadResult(Constant *P) {
2254  // If this memory location has been recently stored, use the stored value: it
2255  // is the most up-to-date.
2256  DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
2257  if (I != MutatedMemory.end()) return I->second;
2258 
2259  // Access it.
2260  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
2261  if (GV->hasDefinitiveInitializer())
2262  return GV->getInitializer();
2263  return nullptr;
2264  }
2265 
2266  // Handle a constantexpr getelementptr.
2267  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
2268  if (CE->getOpcode() == Instruction::GetElementPtr &&
2269  isa<GlobalVariable>(CE->getOperand(0))) {
2270  GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2271  if (GV->hasDefinitiveInitializer())
2273  }
2274 
2275  return nullptr; // don't know how to evaluate.
2276 }
2277 
2278 /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
2279 /// successful, false if we can't evaluate it. NewBB returns the next BB that
2280 /// control flows into, or null upon return.
2281 bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
2282  BasicBlock *&NextBB) {
2283  // This is the main evaluation loop.
2284  while (1) {
2285  Constant *InstResult = nullptr;
2286 
2287  DEBUG(dbgs() << "Evaluating Instruction: " << *CurInst << "\n");
2288 
2289  if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
2290  if (!SI->isSimple()) {
2291  DEBUG(dbgs() << "Store is not simple! Can not evaluate.\n");
2292  return false; // no volatile/atomic accesses.
2293  }
2294  Constant *Ptr = getVal(SI->getOperand(1));
2295  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
2296  DEBUG(dbgs() << "Folding constant ptr expression: " << *Ptr);
2297  Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2298  DEBUG(dbgs() << "; To: " << *Ptr << "\n");
2299  }
2300  if (!isSimpleEnoughPointerToCommit(Ptr)) {
2301  // If this is too complex for us to commit, reject it.
2302  DEBUG(dbgs() << "Pointer is too complex for us to evaluate store.");
2303  return false;
2304  }
2305 
2306  Constant *Val = getVal(SI->getOperand(0));
2307 
2308  // If this might be too difficult for the backend to handle (e.g. the addr
2309  // of one global variable divided by another) then we can't commit it.
2310  if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, DL)) {
2311  DEBUG(dbgs() << "Store value is too complex to evaluate store. " << *Val
2312  << "\n");
2313  return false;
2314  }
2315 
2316  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
2317  if (CE->getOpcode() == Instruction::BitCast) {
2318  DEBUG(dbgs() << "Attempting to resolve bitcast on constant ptr.\n");
2319  // If we're evaluating a store through a bitcast, then we need
2320  // to pull the bitcast off the pointer type and push it onto the
2321  // stored value.
2322  Ptr = CE->getOperand(0);
2323 
2324  Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
2325 
2326  // In order to push the bitcast onto the stored value, a bitcast
2327  // from NewTy to Val's type must be legal. If it's not, we can try
2328  // introspecting NewTy to find a legal conversion.
2329  while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) {
2330  // If NewTy is a struct, we can convert the pointer to the struct
2331  // into a pointer to its first member.
2332  // FIXME: This could be extended to support arrays as well.
2333  if (StructType *STy = dyn_cast<StructType>(NewTy)) {
2334  NewTy = STy->getTypeAtIndex(0U);
2335 
2336  IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
2337  Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
2338  Constant * const IdxList[] = {IdxZero, IdxZero};
2339 
2340  Ptr = ConstantExpr::getGetElementPtr(nullptr, Ptr, IdxList);
2341  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2342  Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2343 
2344  // If we can't improve the situation by introspecting NewTy,
2345  // we have to give up.
2346  } else {
2347  DEBUG(dbgs() << "Failed to bitcast constant ptr, can not "
2348  "evaluate.\n");
2349  return false;
2350  }
2351  }
2352 
2353  // If we found compatible types, go ahead and push the bitcast
2354  // onto the stored value.
2355  Val = ConstantExpr::getBitCast(Val, NewTy);
2356 
2357  DEBUG(dbgs() << "Evaluated bitcast: " << *Val << "\n");
2358  }
2359  }
2360 
2361  MutatedMemory[Ptr] = Val;
2362  } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
2363  InstResult = ConstantExpr::get(BO->getOpcode(),
2364  getVal(BO->getOperand(0)),
2365  getVal(BO->getOperand(1)));
2366  DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: " << *InstResult
2367  << "\n");
2368  } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
2369  InstResult = ConstantExpr::getCompare(CI->getPredicate(),
2370  getVal(CI->getOperand(0)),
2371  getVal(CI->getOperand(1)));
2372  DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult
2373  << "\n");
2374  } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
2375  InstResult = ConstantExpr::getCast(CI->getOpcode(),
2376  getVal(CI->getOperand(0)),
2377  CI->getType());
2378  DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult
2379  << "\n");
2380  } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
2381  InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
2382  getVal(SI->getOperand(1)),
2383  getVal(SI->getOperand(2)));
2384  DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult
2385  << "\n");
2386  } else if (auto *EVI = dyn_cast<ExtractValueInst>(CurInst)) {
2387  InstResult = ConstantExpr::getExtractValue(
2388  getVal(EVI->getAggregateOperand()), EVI->getIndices());
2389  DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " << *InstResult
2390  << "\n");
2391  } else if (auto *IVI = dyn_cast<InsertValueInst>(CurInst)) {
2392  InstResult = ConstantExpr::getInsertValue(
2393  getVal(IVI->getAggregateOperand()),
2394  getVal(IVI->getInsertedValueOperand()), IVI->getIndices());
2395  DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " << *InstResult
2396  << "\n");
2397  } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
2398  Constant *P = getVal(GEP->getOperand(0));
2400  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
2401  i != e; ++i)
2402  GEPOps.push_back(getVal(*i));
2403  InstResult =
2404  ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps,
2405  cast<GEPOperator>(GEP)->isInBounds());
2406  DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult
2407  << "\n");
2408  } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
2409 
2410  if (!LI->isSimple()) {
2411  DEBUG(dbgs() << "Found a Load! Not a simple load, can not evaluate.\n");
2412  return false; // no volatile/atomic accesses.
2413  }
2414 
2415  Constant *Ptr = getVal(LI->getOperand(0));
2416  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
2417  Ptr = ConstantFoldConstantExpression(CE, DL, TLI);
2418  DEBUG(dbgs() << "Found a constant pointer expression, constant "
2419  "folding: " << *Ptr << "\n");
2420  }
2421  InstResult = ComputeLoadResult(Ptr);
2422  if (!InstResult) {
2423  DEBUG(dbgs() << "Failed to compute load result. Can not evaluate load."
2424  "\n");
2425  return false; // Could not evaluate load.
2426  }
2427 
2428  DEBUG(dbgs() << "Evaluated load: " << *InstResult << "\n");
2429  } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
2430  if (AI->isArrayAllocation()) {
2431  DEBUG(dbgs() << "Found an array alloca. Can not evaluate.\n");
2432  return false; // Cannot handle array allocs.
2433  }
2434  Type *Ty = AI->getType()->getElementType();
2435  AllocaTmps.push_back(
2436  make_unique<GlobalVariable>(Ty, false, GlobalValue::InternalLinkage,
2437  UndefValue::get(Ty), AI->getName()));
2438  InstResult = AllocaTmps.back().get();
2439  DEBUG(dbgs() << "Found an alloca. Result: " << *InstResult << "\n");
2440  } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
2441  CallSite CS(CurInst);
2442 
2443  // Debug info can safely be ignored here.
2444  if (isa<DbgInfoIntrinsic>(CS.getInstruction())) {
2445  DEBUG(dbgs() << "Ignoring debug info.\n");
2446  ++CurInst;
2447  continue;
2448  }
2449 
2450  // Cannot handle inline asm.
2451  if (isa<InlineAsm>(CS.getCalledValue())) {
2452  DEBUG(dbgs() << "Found inline asm, can not evaluate.\n");
2453  return false;
2454  }
2455 
2456  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
2457  if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
2458  if (MSI->isVolatile()) {
2459  DEBUG(dbgs() << "Can not optimize a volatile memset " <<
2460  "intrinsic.\n");
2461  return false;
2462  }
2463  Constant *Ptr = getVal(MSI->getDest());
2464  Constant *Val = getVal(MSI->getValue());
2465  Constant *DestVal = ComputeLoadResult(getVal(Ptr));
2466  if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
2467  // This memset is a no-op.
2468  DEBUG(dbgs() << "Ignoring no-op memset.\n");
2469  ++CurInst;
2470  continue;
2471  }
2472  }
2473 
2474  if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
2475  II->getIntrinsicID() == Intrinsic::lifetime_end) {
2476  DEBUG(dbgs() << "Ignoring lifetime intrinsic.\n");
2477  ++CurInst;
2478  continue;
2479  }
2480 
2481  if (II->getIntrinsicID() == Intrinsic::invariant_start) {
2482  // We don't insert an entry into Values, as it doesn't have a
2483  // meaningful return value.
2484  if (!II->use_empty()) {
2485  DEBUG(dbgs() << "Found unused invariant_start. Can't evaluate.\n");
2486  return false;
2487  }
2488  ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
2489  Value *PtrArg = getVal(II->getArgOperand(1));
2490  Value *Ptr = PtrArg->stripPointerCasts();
2491  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
2492  Type *ElemTy = cast<PointerType>(GV->getType())->getElementType();
2493  if (!Size->isAllOnesValue() &&
2494  Size->getValue().getLimitedValue() >=
2495  DL.getTypeStoreSize(ElemTy)) {
2496  Invariants.insert(GV);
2497  DEBUG(dbgs() << "Found a global var that is an invariant: " << *GV
2498  << "\n");
2499  } else {
2500  DEBUG(dbgs() << "Found a global var, but can not treat it as an "
2501  "invariant.\n");
2502  }
2503  }
2504  // Continue even if we do nothing.
2505  ++CurInst;
2506  continue;
2507  }
2508 
2509  DEBUG(dbgs() << "Unknown intrinsic. Can not evaluate.\n");
2510  return false;
2511  }
2512 
2513  // Resolve function pointers.
2514  Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
2515  if (!Callee || Callee->mayBeOverridden()) {
2516  DEBUG(dbgs() << "Can not resolve function pointer.\n");
2517  return false; // Cannot resolve.
2518  }
2519 
2520  SmallVector<Constant*, 8> Formals;
2521  for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
2522  Formals.push_back(getVal(*i));
2523 
2524  if (Callee->isDeclaration()) {
2525  // If this is a function we can constant fold, do it.
2526  if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
2527  InstResult = C;
2528  DEBUG(dbgs() << "Constant folded function call. Result: " <<
2529  *InstResult << "\n");
2530  } else {
2531  DEBUG(dbgs() << "Can not constant fold function call.\n");
2532  return false;
2533  }
2534  } else {
2535  if (Callee->getFunctionType()->isVarArg()) {
2536  DEBUG(dbgs() << "Can not constant fold vararg function call.\n");
2537  return false;
2538  }
2539 
2540  Constant *RetVal = nullptr;
2541  // Execute the call, if successful, use the return value.
2542  ValueStack.emplace_back();
2543  if (!EvaluateFunction(Callee, RetVal, Formals)) {
2544  DEBUG(dbgs() << "Failed to evaluate function.\n");
2545  return false;
2546  }
2547  ValueStack.pop_back();
2548  InstResult = RetVal;
2549 
2550  if (InstResult) {
2551  DEBUG(dbgs() << "Successfully evaluated function. Result: " <<
2552  InstResult << "\n\n");
2553  } else {
2554  DEBUG(dbgs() << "Successfully evaluated function. Result: 0\n\n");
2555  }
2556  }
2557  } else if (isa<TerminatorInst>(CurInst)) {
2558  DEBUG(dbgs() << "Found a terminator instruction.\n");
2559 
2560  if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
2561  if (BI->isUnconditional()) {
2562  NextBB = BI->getSuccessor(0);
2563  } else {
2564  ConstantInt *Cond =
2565  dyn_cast<ConstantInt>(getVal(BI->getCondition()));
2566  if (!Cond) return false; // Cannot determine.
2567 
2568  NextBB = BI->getSuccessor(!Cond->getZExtValue());
2569  }
2570  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
2571  ConstantInt *Val =
2572  dyn_cast<ConstantInt>(getVal(SI->getCondition()));
2573  if (!Val) return false; // Cannot determine.
2574  NextBB = SI->findCaseValue(Val).getCaseSuccessor();
2575  } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
2576  Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
2577  if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
2578  NextBB = BA->getBasicBlock();
2579  else
2580  return false; // Cannot determine.
2581  } else if (isa<ReturnInst>(CurInst)) {
2582  NextBB = nullptr;
2583  } else {
2584  // invoke, unwind, resume, unreachable.
2585  DEBUG(dbgs() << "Can not handle terminator.");
2586  return false; // Cannot handle this terminator.
2587  }
2588 
2589  // We succeeded at evaluating this block!
2590  DEBUG(dbgs() << "Successfully evaluated block.\n");
2591  return true;
2592  } else {
2593  // Did not know how to evaluate this!
2594  DEBUG(dbgs() << "Failed to evaluate block due to unhandled instruction."
2595  "\n");
2596  return false;
2597  }
2598 
2599  if (!CurInst->use_empty()) {
2600  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
2601  InstResult = ConstantFoldConstantExpression(CE, DL, TLI);
2602 
2603  setVal(CurInst, InstResult);
2604  }
2605 
2606  // If we just processed an invoke, we finished evaluating the block.
2607  if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
2608  NextBB = II->getNormalDest();
2609  DEBUG(dbgs() << "Found an invoke instruction. Finished Block.\n\n");
2610  return true;
2611  }
2612 
2613  // Advance program counter.
2614  ++CurInst;
2615  }
2616 }
2617 
2618 /// EvaluateFunction - Evaluate a call to function F, returning true if
2619 /// successful, false if we can't evaluate it. ActualArgs contains the formal
2620 /// arguments for the function.
2621 bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
2622  const SmallVectorImpl<Constant*> &ActualArgs) {
2623  // Check to see if this function is already executing (recursion). If so,
2624  // bail out. TODO: we might want to accept limited recursion.
2625  if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
2626  return false;
2627 
2628  CallStack.push_back(F);
2629 
2630  // Initialize arguments to the incoming values specified.
2631  unsigned ArgNo = 0;
2632  for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
2633  ++AI, ++ArgNo)
2634  setVal(AI, ActualArgs[ArgNo]);
2635 
2636  // ExecutedBlocks - We only handle non-looping, non-recursive code. As such,
2637  // we can only evaluate any one basic block at most once. This set keeps
2638  // track of what we have executed so we can detect recursive cases etc.
2639  SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
2640 
2641  // CurBB - The current basic block we're evaluating.
2642  BasicBlock *CurBB = F->begin();
2643 
2644  BasicBlock::iterator CurInst = CurBB->begin();
2645 
2646  while (1) {
2647  BasicBlock *NextBB = nullptr; // Initialized to avoid compiler warnings.
2648  DEBUG(dbgs() << "Trying to evaluate BB: " << *CurBB << "\n");
2649 
2650  if (!EvaluateBlock(CurInst, NextBB))
2651  return false;
2652 
2653  if (!NextBB) {
2654  // Successfully running until there's no next block means that we found
2655  // the return. Fill it the return value and pop the call stack.
2656  ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
2657  if (RI->getNumOperands())
2658  RetVal = getVal(RI->getOperand(0));
2659  CallStack.pop_back();
2660  return true;
2661  }
2662 
2663  // Okay, we succeeded in evaluating this control flow. See if we have
2664  // executed the new block before. If so, we have a looping function,
2665  // which we cannot evaluate in reasonable time.
2666  if (!ExecutedBlocks.insert(NextBB).second)
2667  return false; // looped!
2668 
2669  // Okay, we have never been in this block before. Check to see if there
2670  // are any PHI nodes. If so, evaluate them with information about where
2671  // we came from.
2672  PHINode *PN = nullptr;
2673  for (CurInst = NextBB->begin();
2674  (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
2675  setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
2676 
2677  // Advance to the next block.
2678  CurBB = NextBB;
2679  }
2680 }
2681 
2682 /// EvaluateStaticConstructor - Evaluate static constructors in the function, if
2683 /// we can. Return true if we can, false otherwise.
2685  const TargetLibraryInfo *TLI) {
2686  // Call the function.
2687  Evaluator Eval(DL, TLI);
2688  Constant *RetValDummy;
2689  bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2691 
2692  if (EvalSuccess) {
2693  ++NumCtorsEvaluated;
2694 
2695  // We succeeded at evaluation: commit the result.
2696  DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2697  << F->getName() << "' to " << Eval.getMutatedMemory().size()
2698  << " stores.\n");
2700  Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
2701  I != E; ++I)
2702  CommitValueTo(I->second, I->first);
2703  for (GlobalVariable *GV : Eval.getInvariants())
2704  GV->setConstant(true);
2705  }
2706 
2707  return EvalSuccess;
2708 }
2709 
2710 static int compareNames(Constant *const *A, Constant *const *B) {
2711  return (*A)->getName().compare((*B)->getName());
2712 }
2713 
2716  if (Init.empty()) {
2717  V.eraseFromParent();
2718  return;
2719  }
2720 
2721  // Type of pointer to the array of pointers.
2722  PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
2723 
2725  for (GlobalValue *GV : Init) {
2726  Constant *Cast
2728  UsedArray.push_back(Cast);
2729  }
2730  // Sort to get deterministic order.
2731  array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
2732  ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
2733 
2734  Module *M = V.getParent();
2735  V.removeFromParent();
2736  GlobalVariable *NV =
2738  llvm::ConstantArray::get(ATy, UsedArray), "");
2739  NV->takeName(&V);
2740  NV->setSection("llvm.metadata");
2741  delete &V;
2742 }
2743 
2744 namespace {
2745 /// \brief An easy to access representation of llvm.used and llvm.compiler.used.
2746 class LLVMUsed {
2748  SmallPtrSet<GlobalValue *, 8> CompilerUsed;
2749  GlobalVariable *UsedV;
2750  GlobalVariable *CompilerUsedV;
2751 
2752 public:
2753  LLVMUsed(Module &M) {
2754  UsedV = collectUsedGlobalVariables(M, Used, false);
2755  CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
2756  }
2757  typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator;
2758  typedef iterator_range<iterator> used_iterator_range;
2759  iterator usedBegin() { return Used.begin(); }
2760  iterator usedEnd() { return Used.end(); }
2761  used_iterator_range used() {
2762  return used_iterator_range(usedBegin(), usedEnd());
2763  }
2764  iterator compilerUsedBegin() { return CompilerUsed.begin(); }
2765  iterator compilerUsedEnd() { return CompilerUsed.end(); }
2766  used_iterator_range compilerUsed() {
2767  return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
2768  }
2769  bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2770  bool compilerUsedCount(GlobalValue *GV) const {
2771  return CompilerUsed.count(GV);
2772  }
2773  bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2774  bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
2775  bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
2776  bool compilerUsedInsert(GlobalValue *GV) {
2777  return CompilerUsed.insert(GV).second;
2778  }
2779 
2780  void syncVariablesAndSets() {
2781  if (UsedV)
2782  setUsedInitializer(*UsedV, Used);
2783  if (CompilerUsedV)
2784  setUsedInitializer(*CompilerUsedV, CompilerUsed);
2785  }
2786 };
2787 }
2788 
2789 static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
2790  if (GA.use_empty()) // No use at all.
2791  return false;
2792 
2793  assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
2794  "We should have removed the duplicated "
2795  "element from llvm.compiler.used");
2796  if (!GA.hasOneUse())
2797  // Strictly more than one use. So at least one is not in llvm.used and
2798  // llvm.compiler.used.
2799  return true;
2800 
2801  // Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
2802  return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
2803 }
2804 
2806  const LLVMUsed &U) {
2807  unsigned N = 2;
2808  assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
2809  "We should have removed the duplicated "
2810  "element from llvm.compiler.used");
2811  if (U.usedCount(&V) || U.compilerUsedCount(&V))
2812  ++N;
2813  return V.hasNUsesOrMore(N);
2814 }
2815 
2816 static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
2817  if (!GA.hasLocalLinkage())
2818  return true;
2819 
2820  return U.usedCount(&GA) || U.compilerUsedCount(&GA);
2821 }
2822 
2823 static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
2824  bool &RenameTarget) {
2825  RenameTarget = false;
2826  bool Ret = false;
2827  if (hasUseOtherThanLLVMUsed(GA, U))
2828  Ret = true;
2829 
2830  // If the alias is externally visible, we may still be able to simplify it.
2831  if (!mayHaveOtherReferences(GA, U))
2832  return Ret;
2833 
2834  // If the aliasee has internal linkage, give it the name and linkage
2835  // of the alias, and delete the alias. This turns:
2836  // define internal ... @f(...)
2837  // @a = alias ... @f
2838  // into:
2839  // define ... @a(...)
2840  Constant *Aliasee = GA.getAliasee();
2841  GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2842  if (!Target->hasLocalLinkage())
2843  return Ret;
2844 
2845  // Do not perform the transform if multiple aliases potentially target the
2846  // aliasee. This check also ensures that it is safe to replace the section
2847  // and other attributes of the aliasee with those of the alias.
2848  if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
2849  return Ret;
2850 
2851  RenameTarget = true;
2852  return true;
2853 }
2854 
2855 bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
2856  bool Changed = false;
2857  LLVMUsed Used(M);
2858 
2859  for (GlobalValue *GV : Used.used())
2860  Used.compilerUsedErase(GV);
2861 
2862  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
2863  I != E;) {
2864  Module::alias_iterator J = I++;
2865  // Aliases without names cannot be referenced outside this module.
2866  if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage())
2867  J->setLinkage(GlobalValue::InternalLinkage);
2868  // If the aliasee may change at link time, nothing can be done - bail out.
2869  if (J->mayBeOverridden())
2870  continue;
2871 
2872  Constant *Aliasee = J->getAliasee();
2874  // We can't trivially replace the alias with the aliasee if the aliasee is
2875  // non-trivial in some way.
2876  // TODO: Try to handle non-zero GEPs of local aliasees.
2877  if (!Target)
2878  continue;
2879  Target->removeDeadConstantUsers();
2880 
2881  // Make all users of the alias use the aliasee instead.
2882  bool RenameTarget;
2883  if (!hasUsesToReplace(*J, Used, RenameTarget))
2884  continue;
2885 
2886  J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
2887  ++NumAliasesResolved;
2888  Changed = true;
2889 
2890  if (RenameTarget) {
2891  // Give the aliasee the name, linkage and other attributes of the alias.
2892  Target->takeName(J);
2893  Target->setLinkage(J->getLinkage());
2894  Target->setVisibility(J->getVisibility());
2895  Target->setDLLStorageClass(J->getDLLStorageClass());
2896 
2897  if (Used.usedErase(J))
2898  Used.usedInsert(Target);
2899 
2900  if (Used.compilerUsedErase(J))
2901  Used.compilerUsedInsert(Target);
2902  } else if (mayHaveOtherReferences(*J, Used))
2903  continue;
2904 
2905  // Delete the alias.
2906  M.getAliasList().erase(J);
2907  ++NumAliasesRemoved;
2908  Changed = true;
2909  }
2910 
2911  Used.syncVariablesAndSets();
2912 
2913  return Changed;
2914 }
2915 
2917  if (!TLI->has(LibFunc::cxa_atexit))
2918  return nullptr;
2919 
2920  Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
2921 
2922  if (!Fn)
2923  return nullptr;
2924 
2925  FunctionType *FTy = Fn->getFunctionType();
2926 
2927  // Checking that the function has the right return type, the right number of
2928  // parameters and that they all have pointer types should be enough.
2929  if (!FTy->getReturnType()->isIntegerTy() ||
2930  FTy->getNumParams() != 3 ||
2931  !FTy->getParamType(0)->isPointerTy() ||
2932  !FTy->getParamType(1)->isPointerTy() ||
2933  !FTy->getParamType(2)->isPointerTy())
2934  return nullptr;
2935 
2936  return Fn;
2937 }
2938 
2939 /// cxxDtorIsEmpty - Returns whether the given function is an empty C++
2940 /// destructor and can therefore be eliminated.
2941 /// Note that we assume that other optimization passes have already simplified
2942 /// the code so we only look for a function with a single basic block, where
2943 /// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
2944 /// other side-effect free instructions.
2945 static bool cxxDtorIsEmpty(const Function &Fn,
2946  SmallPtrSet<const Function *, 8> &CalledFunctions) {
2947  // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
2948  // nounwind, but that doesn't seem worth doing.
2949  if (Fn.isDeclaration())
2950  return false;
2951 
2952  if (++Fn.begin() != Fn.end())
2953  return false;
2954 
2955  const BasicBlock &EntryBlock = Fn.getEntryBlock();
2956  for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
2957  I != E; ++I) {
2958  if (const CallInst *CI = dyn_cast<CallInst>(I)) {
2959  // Ignore debug intrinsics.
2960  if (isa<DbgInfoIntrinsic>(CI))
2961  continue;
2962 
2963  const Function *CalledFn = CI->getCalledFunction();
2964 
2965  if (!CalledFn)
2966  return false;
2967 
2968  SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
2969 
2970  // Don't treat recursive functions as empty.
2971  if (!NewCalledFunctions.insert(CalledFn).second)
2972  return false;
2973 
2974  if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
2975  return false;
2976  } else if (isa<ReturnInst>(*I))
2977  return true; // We're done.
2978  else if (I->mayHaveSideEffects())
2979  return false; // Destructor with side effects, bail.
2980  }
2981 
2982  return false;
2983 }
2984 
2985 bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
2986  /// Itanium C++ ABI p3.3.5:
2987  ///
2988  /// After constructing a global (or local static) object, that will require
2989  /// destruction on exit, a termination function is registered as follows:
2990  ///
2991  /// extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
2992  ///
2993  /// This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
2994  /// call f(p) when DSO d is unloaded, before all such termination calls
2995  /// registered before this one. It returns zero if registration is
2996  /// successful, nonzero on failure.
2997 
2998  // This pass will look for calls to __cxa_atexit where the function is trivial
2999  // and remove them.
3000  bool Changed = false;
3001 
3002  for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
3003  I != E;) {
3004  // We're only interested in calls. Theoretically, we could handle invoke
3005  // instructions as well, but neither llvm-gcc nor clang generate invokes
3006  // to __cxa_atexit.
3007  CallInst *CI = dyn_cast<CallInst>(*I++);
3008  if (!CI)
3009  continue;
3010 
3011  Function *DtorFn =
3013  if (!DtorFn)
3014  continue;
3015 
3016  SmallPtrSet<const Function *, 8> CalledFunctions;
3017  if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
3018  continue;
3019 
3020  // Just remove the call.
3022  CI->eraseFromParent();
3023 
3024  ++NumCXXDtorsRemoved;
3025 
3026  Changed |= true;
3027  }
3028 
3029  return Changed;
3030 }
3031 
3032 bool GlobalOpt::runOnModule(Module &M) {
3033  bool Changed = false;
3034 
3035  auto &DL = M.getDataLayout();
3036  TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
3037 
3038  bool LocalChange = true;
3039  while (LocalChange) {
3040  LocalChange = false;
3041 
3042  NotDiscardableComdats.clear();
3043  for (const GlobalVariable &GV : M.globals())
3044  if (const Comdat *C = GV.getComdat())
3045  if (!GV.isDiscardableIfUnused() || !GV.use_empty())
3046  NotDiscardableComdats.insert(C);
3047  for (Function &F : M)
3048  if (const Comdat *C = F.getComdat())
3049  if (!F.isDefTriviallyDead())
3050  NotDiscardableComdats.insert(C);
3051  for (GlobalAlias &GA : M.aliases())
3052  if (const Comdat *C = GA.getComdat())
3053  if (!GA.isDiscardableIfUnused() || !GA.use_empty())
3054  NotDiscardableComdats.insert(C);
3055 
3056  // Delete functions that are trivially dead, ccc -> fastcc
3057  LocalChange |= OptimizeFunctions(M);
3058 
3059  // Optimize global_ctors list.
3060  LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
3061  return EvaluateStaticConstructor(F, DL, TLI);
3062  });
3063 
3064  // Optimize non-address-taken globals.
3065  LocalChange |= OptimizeGlobalVars(M);
3066 
3067  // Resolve aliases, when possible.
3068  LocalChange |= OptimizeGlobalAliases(M);
3069 
3070  // Try to remove trivial global destructors if they are not removed
3071  // already.
3072  Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
3073  if (CXAAtExitFn)
3074  LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
3075 
3076  Changed |= LocalChange;
3077  }
3078 
3079  // TODO: Move all global ctors functions to the end of the module for code
3080  // layout.
3081 
3082  return Changed;
3083 }
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:145
bool isDefTriviallyDead() const
isDefTriviallyDead - Return true if it is trivially safe to remove this function definition from the ...
Definition: Function.cpp:900
ReturnInst - Return a value (possibly void), from a function.
static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL, const TargetLibraryInfo *TLI)
EvaluateStaticConstructor - Evaluate static constructors in the function, if we can.
Definition: GlobalOpt.cpp:2684
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
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...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:537
static bool CleanupConstantGlobalUsers(Value *V, Constant *Init, const DataLayout &DL, TargetLibraryInfo *TLI)
CleanupConstantGlobalUsers - We just marked GV constant.
Definition: GlobalOpt.cpp:270
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:679
bool IsLoaded
True if the global is ever loaded.
Definition: GlobalStatus.h:34
static bool isLeakCheckerRoot(GlobalVariable *GV)
isLeakCheckerRoot - Is this global variable possibly used by a leak checker as a root? If so, we might not really want to eliminate the stores to it.
Definition: GlobalOpt.cpp:105
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:46
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Value * StoredOnceValue
If only one value (besides the initializer constant) is ever stored to this global, keep track of what value it is.
Definition: GlobalStatus.h:58
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
const Function * AccessingFunction
These start out null/false.
Definition: GlobalStatus.h:63
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallPtrSet.h:78
static Constant * getPointerBitCastOrAddrSpaceCast(Constant *C, Type *Ty)
Create a BitCast or AddrSpaceCast for a pointer type depending on the address space.
Definition: Constants.cpp:1663
static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", Instruction *InsertBefore=nullptr)
bool hasName() const
Definition: Value.h:228
STATISTIC(NumFunctions,"Total number of functions")
static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI)
Given a value that is stored to a global but never read, determine whether it's safe to remove the st...
Definition: GlobalOpt.cpp:153
bool isOpaque() const
isOpaque - Return true if this is a type with an identity that has no body specified yet...
Definition: DerivedTypes.h:250
Constant * ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE)
ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a getelementptr constantexpr, return the constant value being addressed by the constant expression, or null if something is funny and we can't decide.
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:119
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
Definition: DerivedTypes.h:136
iterator end()
Definition: Function.h:459
INITIALIZE_PASS_BEGIN(GlobalOpt,"globalopt","Global Variable Optimizer", false, false) INITIALIZE_PASS_END(GlobalOpt
static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc, GlobalVariable *GV)
ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV somewhere.
Definition: GlobalOpt.cpp:1006
This class represents zero extension of integer types.
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
Definition: Module.h:512
unsigned getNumOperands() const
Definition: User.h:138
Nested function static chain.
Definition: Attributes.h:82
static bool isSafeSROAElementUse(Value *V)
isSafeSROAElementUse - Return true if the specified instruction is a safe user of a derived expressio...
Definition: GlobalOpt.cpp:358
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
PointerType::get - This constructs a pointer to an object of the specified type in a numbered address...
Definition: Type.cpp:738
gep_type_iterator gep_type_end(const User *GEP)
unsigned less or equal
Definition: InstrTypes.h:723
unsigned less than
Definition: InstrTypes.h:722
bool mayHaveSideEffects() const
mayHaveSideEffects - Return true if the instruction may have side effects.
Definition: Instruction.h:387
static Constant * EvaluateStoreInto(Constant *Init, Constant *Val, ConstantExpr *Addr, unsigned OpNo)
EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global initializer.
Definition: GlobalOpt.cpp:2101
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1092
static GlobalVariable * OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, CallInst *CI, Type *AllocTy, ConstantInt *NElements, const DataLayout &DL, TargetLibraryInfo *TLI)
OptimizeGlobalAddressOfMalloc - This function takes the specified global variable, and transforms the program as if it always contained the result of the specified malloc.
Definition: GlobalOpt.cpp:827
bool HasMultipleAccessingFunctions
Definition: GlobalStatus.h:64
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
Definition: APInt.h:404
MemSetInst - This class wraps the llvm.memset intrinsic.
void setAttributes(const AttributeSet &PAL)
Definition: CallSite.h:232
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
arg_iterator arg_end()
Definition: Function.h:480
12: Structures
Definition: Type.h:71
F(f)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:472
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
FunctionType * getType(LLVMContext &Context, ID id, ArrayRef< Type * > Tys=None)
Return the function type for an intrinsic.
Definition: Function.cpp:822
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Definition: Constants.cpp:1990
Hexagon Common GEP
static Instruction * CreateFree(Value *Source, Instruction *InsertBefore)
CreateFree - Generate the IR for a call to the builtin free function.
static GlobalVariable * PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI, Value *NElems, const DataLayout &DL, const TargetLibraryInfo *TLI)
PerformHeapAllocSRoA - CI is an allocation of an array of structures.
Definition: GlobalOpt.cpp:1271
#define op(i)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: Type.cpp:216
14: Pointers
Definition: Type.h:73
void reserve(size_type N)
Definition: SmallVector.h:401
void setAlignment(unsigned Align)
Definition: Globals.cpp:77
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasAttribute(unsigned Index, Attribute::AttrKind Kind) const
Return true if the attribute exists at the given index.
Definition: Attributes.cpp:956
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstantExpression - Attempt to fold the constant expression using the specified DataLayo...
const AliasListType & getAliasList() const
Get the Module's list of aliases (constant).
Definition: Module.h:526
const Constant * getAliasee() const
Definition: GlobalAlias.h:81
unsigned getOpcode() const
getOpcode - Return the opcode at the root of this constant expression
Definition: Constants.h:1144
static bool AllUsesOfValueWillTrapIfNull(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs)
AllUsesOfValueWillTrapIfNull - Return true if all users of the specified value will trap if the value...
Definition: GlobalOpt.cpp:616
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:172
This global is stored to, but the only thing stored is the constant it was initialized with...
Definition: GlobalStatus.h:43
static int compareNames(Constant *const *A, Constant *const *B)
Definition: GlobalOpt.cpp:2710
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:178
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
bool isSingleValueType() const
isSingleValueType - Return true if the type is a valid type for a register in codegen.
Definition: Type.h:250
element_iterator element_end() const
Definition: DerivedTypes.h:280
BlockAddress - The address of a basic block.
Definition: Constants.h:802
AnalysisUsage & addRequired()
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:475
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U, bool &RenameTarget)
Definition: GlobalOpt.cpp:2823
AttributeSet removeAttribute(LLVMContext &C, unsigned Index, Attribute::AttrKind Attr) const
Remove the specified attribute at the specified index from this attribute list.
Definition: Attributes.cpp:822
SelectInst - This class represents the LLVM 'select' instruction.
static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV)
AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads from GV will trap if the lo...
Definition: GlobalOpt.cpp:659
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Type::subtype_iterator element_iterator
Definition: DerivedTypes.h:278
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:551
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:106
bool isUnordered() const
Definition: Instructions.h:280
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:198
bool has(LibFunc::Func F) const
Tests whether a library function is available.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
Definition: SmallVector.h:406
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
void initializeGlobalOptPass(PassRegistry &)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
bool canLosslesslyBitCastTo(Type *Ty) const
canLosslesslyBitCastTo - Return true if this type could be converted with a lossless BitCast to type ...
Definition: Type.cpp:65
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1057
static bool isSimpleEnoughValueToCommitHelper(Constant *C, SmallPtrSetImpl< Constant * > &SimpleConstants, const DataLayout &DL)
isSimpleEnoughValueToCommit - Return true if the specified constant can be handled by the code genera...
Definition: GlobalOpt.cpp:1980
Global Variable false
Definition: GlobalOpt.cpp:98
StoredType
Keep track of what stores to the global look like.
Definition: GlobalStatus.h:37
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Definition: Type.h:268
Windows NT (Windows on ARM)
element_iterator element_begin() const
Definition: DerivedTypes.h:279
bool HasNonInstructionUser
Set to true if this global has a user that is not an instruction (e.g.
Definition: GlobalStatus.h:68
bool hasUniqueInitializer() const
hasUniqueInitializer - Whether the global variable has an initializer, and any changes made to the in...
static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2816
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Globals.cpp:194
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:173
static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal)
TryToShrinkGlobalToBoolean - At this point, we have learned that the only two values ever stored into...
Definition: GlobalOpt.cpp:1594
static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2789
bool hasPrivateLinkage() const
Definition: GlobalValue.h:279
SynchronizationScope getSynchScope() const
Definition: Instructions.h:261
AtomicOrdering
Definition: Instructions.h:38
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:117
global_iterator global_begin()
Definition: Module.h:552
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
Definition: Constants.cpp:1868
user_iterator_impl< User > user_iterator
Definition: Value.h:292
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
ValTy * getCalledValue() const
getCalledValue - Return the pointer to function that is being called.
Definition: CallSite.h:91
static void ChangeCalleesToFastCall(Function *F)
ChangeCalleesToFastCall - Walk all of the direct calls of the specified function, changing them to Fa...
Definition: GlobalOpt.cpp:1855
static Function * FindCXAAtExit(Module &M, TargetLibraryInfo *TLI)
Definition: GlobalOpt.cpp:2916
static void RewriteHeapSROALoadUser(Instruction *LoadUser, DenseMap< Value *, std::vector< Value * > > &InsertedScalarizedValues, std::vector< std::pair< PHINode *, unsigned > > &PHIsToRewrite)
RewriteHeapSROALoadUser - Given a load instruction and a value derived from the load, rewrite the derived value to use the HeapSRoA'd load.
Definition: GlobalOpt.cpp:1191
unsigned getAlignment() const
Definition: GlobalObject.h:46
ArrayType - Class to represent array types.
Definition: DerivedTypes.h:336
static Constant * getSelect(Constant *C, Constant *V1, Constant *V2, Type *OnlyIfReducedTy=nullptr)
Select constant expr.
Definition: Constants.cpp:2012
This class represents a no-op cast from one type to another.
TypeID getTypeID() const
getTypeID - Return the type id for the type.
Definition: Type.h:134
bool isFloatingPointTy() const
isFloatingPointTy - Return true if this is one of the six floating point types
Definition: Type.h:159
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
void setCallingConv(CallingConv::ID CC)
Definition: CallSite.h:215
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
bool isArrayTy() const
isArrayTy - True if this is an instance of ArrayType.
Definition: Type.h:213
static bool isSimpleEnoughValueToCommit(Constant *C, SmallPtrSetImpl< Constant * > &SimpleConstants, const DataLayout &DL)
Definition: GlobalOpt.cpp:2036
aa Exhaustive Alias Analysis Precision Evaluator
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:256
iterator begin()
Definition: Function.h:457
Type * getElementType() const
Definition: DerivedTypes.h:323
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
This global is stored to, but only its initializer and one other value is ever stored to it...
Definition: GlobalStatus.h:49
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:188
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:491
Value * getMallocArraySize(CallInst *CI, const DataLayout &DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
getMallocArraySize - Returns the array size of a malloc call.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1835
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
static Constant * getInsertValue(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2191
#define P(N)
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:175
unsigned getNumSlots() const
Return the number of slots used in this attribute list.
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:131
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:287
As we analyze each global, keep track of some information about it.
Definition: GlobalStatus.h:28
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
Constant * stripPointerCasts()
Definition: Constant.h:170
alias_iterator alias_end()
Definition: Module.h:593
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
globalopt
Definition: GlobalOpt.cpp:98
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static bool mayBeOverridden(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time...
Definition: GlobalValue.h:245
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
BranchInst - Conditional or Unconditional Branch instruction.
AttributeSet getSlotAttributes(unsigned Slot) const
Return the attributes at the given slot.
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Definition: Type.h:226
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:291
const CallInst * extractMallocCall(const Value *I, const TargetLibraryInfo *TLI)
extractMallocCall - Returns the corresponding CallInst if the instruction is a malloc call...
const Comdat * getComdat() const
Definition: GlobalObject.h:61
This is an important base class in LLVM.
Definition: Constant.h:41
static Value * GetHeapSROAValue(Value *V, unsigned FieldNo, DenseMap< Value *, std::vector< Value * > > &InsertedScalarizedValues, std::vector< std::pair< PHINode *, unsigned > > &PHIsToRewrite)
Definition: GlobalOpt.cpp:1147
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:32
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IndirectBrInst - Indirect Branch Instruction.
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:873
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:264
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
static bool isSimpleEnoughPointerToCommit(Constant *C)
isSimpleEnoughPointerToCommit - Return true if this constant is simple enough for us to understand...
Definition: GlobalOpt.cpp:2052
static void RemoveNestAttribute(Function *F)
Definition: GlobalOpt.cpp:1877
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, CallInst *CI, Type *AllocTy, AtomicOrdering Ordering, Module::global_iterator &GVI, const DataLayout &DL, TargetLibraryInfo *TLI)
TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a pointer global variable wi...
Definition: GlobalOpt.cpp:1460
This instruction compares its operands according to the predicate given to the constructor.
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
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:238
User * getUser() const
Returns the User that contains this Use.
Definition: Use.cpp:41
static bool cxxDtorIsEmpty(const Function &Fn, SmallPtrSet< const Function *, 8 > &CalledFunctions)
cxxDtorIsEmpty - Returns whether the given function is an empty C++ destructor and can therefore be e...
Definition: GlobalOpt.cpp:2945
Value * getOperand(unsigned i) const
Definition: User.h:118
op_range operands()
Definition: User.h:191
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
arg_iterator arg_begin()
Definition: Function.h:472
Class to represent integer types.
Definition: DerivedTypes.h:37
ModulePass * createGlobalOptimizerPass()
createGlobalOptimizerPass - This function returns a new pass that optimizes non-address taken interna...
Definition: GlobalOpt.cpp:101
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:760
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1008
static bool analyzeGlobal(const Value *V, GlobalStatus &GS)
Look at all uses of the global and fill in the GlobalStatus structure.
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Definition: Constants.cpp:250
static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV)
IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
Definition: GlobalOpt.cpp:391
void setConstant(bool Val)
static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V, const LLVMUsed &U)
Definition: GlobalOpt.cpp:2805
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:152
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:283
static void ConstantPropUsersOf(Value *V, const DataLayout &DL, TargetLibraryInfo *TLI)
ConstantPropUsersOf - Walk the use list of V, constant folding all of the instructions that are folda...
Definition: GlobalOpt.cpp:806
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:854
iterator erase(iterator where)
Definition: ilist.h:465
signed greater than
Definition: InstrTypes.h:724
global_iterator global_end()
Definition: Module.h:554
13: Arrays
Definition: Type.h:72
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:694
SequentialType - This is the superclass of the array, pointer and vector type classes.
Definition: DerivedTypes.h:310
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:674
bool hasExternalLinkage() const
Definition: GlobalValue.h:260
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:167
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:304
MemIntrinsic - This is the common base class for memset/memcpy/memmove.
Global Variable Optimizer
Definition: GlobalOpt.cpp:98
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
Comdat * getComdat()
Definition: Globals.cpp:116
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
static void CommitValueTo(Constant *Val, Constant *Addr)
CommitValueTo - We have decided that Addr (which satisfies the predicate isSimpleEnoughPointerToCommi...
Definition: GlobalOpt.cpp:2149
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
15: SIMD 'packed' format, or other vector type
Definition: Type.h:74
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
unsigned getVectorNumElements() const
Definition: Type.cpp:212
iterator end()
Definition: BasicBlock.h:233
static bool isProfitableToMakeFastCC(Function *F)
Return true if this is a calling convention that we'd like to change.
Definition: GlobalOpt.cpp:1891
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Instruction * user_back()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
Definition: Instruction.h:69
Provides information about what library functions are available for the current target.
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:131
signed less than
Definition: InstrTypes.h:726
uint64_t getSizeInBytes() const
Definition: DataLayout.h:481
alias_iterator alias_begin()
Definition: Module.h:591
unsigned arg_size() const
Definition: CallSite.h:162
IteratorT begin() const
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:458
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:582
static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs)
Definition: GlobalOpt.cpp:1864
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
AtomicOrdering getOrdering() const
Returns the ordering effect of this fence.
Definition: Instructions.h:250
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:161
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, AtomicOrdering Ordering, Module::global_iterator &GVI, const DataLayout &DL, TargetLibraryInfo *TLI)
Definition: GlobalOpt.cpp:1557
bool isNullValue() const
isNullValue - Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:75
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:530
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:284
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
bool isAllOnesValue() const
isAllOnesValue - Return true if this is the value that would be returned by getAllOnesValue.
Definition: Constants.cpp:88
AttributeSet getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:181
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
signed less or equal
Definition: InstrTypes.h:727
A range adaptor for a pair of iterators.
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
Target - Wrapper for Target specific information.
bool hasInitializer() const
Definitions have initializers, declarations don't.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
Type * getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI)
getMallocAllocatedType - Returns the Type allocated by malloc call.
iterator_range< user_iterator > users()
Definition: Value.h:300
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:1591
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
void eraseFromParent() override
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
Definition: Function.cpp:241
static Instruction * CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, Type *AllocTy, Value *AllocSize, Value *ArraySize=nullptr, Function *MallocF=nullptr, const Twine &Name="")
CreateMalloc - Generate the IR for a call to malloc:
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:160
use_iterator use_begin()
Definition: Value.h:279
uint64_t MinAlign(uint64_t A, uint64_t B)
MinAlign - A and B are either alignments or offsets.
Definition: MathExtras.h:552
static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV, Instruction *StoredVal)
AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from GV are simple enough to ...
Definition: GlobalOpt.cpp:1101
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
static bool GlobalUsersSafeToSRA(GlobalValue *GV)
GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it is safe for us to perform...
Definition: GlobalOpt.cpp:458
bool hasAddressTaken(const User **=nullptr) const
hasAddressTaken - returns true if there are any uses of this function other than direct calls or invo...
Definition: Function.cpp:886
iterator end()
Definition: Module.h:571
static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V, const GlobalVariable *GV, SmallPtrSetImpl< const PHINode * > &PHIs)
ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking to make sure that there a...
Definition: GlobalOpt.cpp:958
StringRef getName(LibFunc::Func F) const
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:239
bool IsCompared
True if the global's address is used in a comparison.
Definition: GlobalStatus.h:30
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
bool hasAttrSomewhere(Attribute::AttrKind Attr) const
Return true if the specified attribute is set for at least one parameter or for the return value...
Definition: Attributes.cpp:973
unsigned getSlotIndex(unsigned Slot) const
Return the index for the given slot.
void setCalledFunction(Value *V)
setCalledFunction - Set the callee to the specified value.
Definition: CallSite.h:105
AtomicOrdering Ordering
Set to the strongest atomic ordering requirement.
Definition: GlobalStatus.h:71
unsigned greater or equal
Definition: InstrTypes.h:721
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
FunctionType * getFunctionType() const
Definition: Function.cpp:227
Fast - This calling convention attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:42
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:311
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
iterator begin()
Definition: Module.h:569
GraphT::NodeType * Eval(DominatorTreeBase< typename GraphT::NodeType > &DT, typename GraphT::NodeType *VIn, unsigned LastLinked)
static GlobalVariable * SRAGlobal(GlobalVariable *GV, const DataLayout &DL)
SRAGlobal - Perform scalar replacement of aggregates on the specified global variable.
Definition: GlobalOpt.cpp:472
static ArrayType * get(Type *ElementType, uint64_t NumElements)
ArrayType::get - This static method is the primary way to construct an ArrayType. ...
Definition: Type.cpp:686
X86_ThisCall - Similar to X86_StdCall.
Definition: CallingConv.h:104
const AttributeSet & getAttributes() const
getAttributes/setAttributes - get or set the parameter attributes of the call.
Definition: CallSite.h:229
Rename collisions when linking (static functions).
Definition: GlobalValue.h:47
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:348
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
Definition: DataLayout.h:371
void setAttributes(AttributeSet attrs)
Set the attribute list for this Function.
Definition: Function.h:184
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
Definition: Value.cpp:104
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
bool isVarArg() const
Definition: DerivedTypes.h:120
static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV, const DataLayout &DL, TargetLibraryInfo *TLI)
OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null value stored into it...
Definition: GlobalOpt.cpp:742
SwitchInst - Multiway switch.
bool use_empty() const
Definition: Value.h:275
static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, DenseMap< Value *, std::vector< Value * > > &InsertedScalarizedValues, std::vector< std::pair< PHINode *, unsigned > > &PHIsToRewrite)
RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global.
Definition: GlobalOpt.cpp:1255
void setArgument(unsigned ArgNo, Value *newVal)
Definition: CallSite.h:124
Type * getReturnType() const
Definition: DerivedTypes.h:121
user_iterator user_begin()
Definition: Value.h:294
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V, SmallPtrSetImpl< const PHINode * > &LoadUsingPHIs, SmallPtrSetImpl< const PHINode * > &LoadUsingPHIsPerLoad)
LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi of a load) are simple en...
Definition: GlobalOpt.cpp:1049
void removeDeadConstantUsers() const
removeDeadConstantUsers - If there are any dead constant users dangling off of this constant...
Definition: Constants.cpp:487
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
bool hasUnnamedAddr() const
Definition: GlobalValue.h:130
static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV)
Definition: GlobalOpt.cpp:675
GlobalVariable * collectUsedGlobalVariables(Module &M, SmallPtrSetImpl< GlobalValue * > &Set, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Definition: ModuleUtils.cpp:82
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:507
InvokeInst - Invoke instruction.
#define DEBUG(X)
Definition: Debug.h:92
iterator_range< global_iterator > globals()
Definition: Module.h:558
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static Constant * getExtractValue(Constant *Agg, ArrayRef< unsigned > Idxs, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2215
unsigned greater than
Definition: InstrTypes.h:720
static bool CleanupPointerRootUsers(GlobalVariable *GV, const TargetLibraryInfo *TLI)
CleanupPointerRootUsers - This GV is a pointer root.
Definition: GlobalOpt.cpp:183
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 ret...
Definition: CtorUtils.cpp:120
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:290
static void setUsedInitializer(GlobalVariable &V, const SmallPtrSet< GlobalValue *, 8 > &Init)
Definition: GlobalOpt.cpp:2714
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.cpp:229
void setSection(StringRef S)
Definition: Globals.cpp:126
const BasicBlock * getParent() const
Definition: Instruction.h:72
void removeFromParent() override
removeFromParent - This method unlinks 'this' from the containing module, but does not delete it...
Definition: Globals.cpp:190
signed greater or equal
Definition: InstrTypes.h:725
User * user_back()
Definition: Value.h:298
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37
Constant * ConstantFoldCall(Function *F, ArrayRef< Constant * > Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76
gep_type_iterator gep_type_begin(const User *GEP)
user_iterator user_end()
Definition: Value.h:296