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/GlobalOpt.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"
23 #include "llvm/Analysis/ConstantFolding.h"
24 #include "llvm/Analysis/MemoryBuiltins.h"
25 #include "llvm/Analysis/TargetLibraryInfo.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"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/GetElementPtrTypeIterator.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/IR/ValueHandle.h"
38 #include "llvm/Pass.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/ErrorHandling.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Transforms/IPO.h"
44 #include "llvm/Transforms/Utils/CtorUtils.h"
45 #include "llvm/Transforms/Utils/Evaluator.h"
46 #include "llvm/Transforms/Utils/GlobalStatus.h"
47 #include "llvm/Transforms/Utils/Local.h"
48 #include <algorithm>
49 using namespace llvm;
50 
51 #define DEBUG_TYPE "globalopt"
52 
53 STATISTIC(NumMarked    , "Number of globals marked constant");
54 STATISTIC(NumUnnamed   , "Number of globals marked unnamed_addr");
55 STATISTIC(NumSRA       , "Number of aggregate globals broken into scalars");
56 STATISTIC(NumHeapSRA   , "Number of heap objects SRA'd");
57 STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
58 STATISTIC(NumDeleted   , "Number of globals deleted");
59 STATISTIC(NumGlobUses  , "Number of global uses devirtualized");
60 STATISTIC(NumLocalized , "Number of globals localized");
61 STATISTIC(NumShrunkToBool  , "Number of global vars shrunk to booleans");
62 STATISTIC(NumFastCallFns   , "Number of functions converted to fastcc");
63 STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
64 STATISTIC(NumNestRemoved   , "Number of nest attributes removed");
65 STATISTIC(NumAliasesResolved, "Number of global aliases resolved");
66 STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
67 STATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
68 
69 /// Is this global variable possibly used by a leak checker as a root?  If so,
70 /// we might not really want to eliminate the stores to it.
71 static bool isLeakCheckerRoot(GlobalVariable *GV) {
72   // A global variable is a root if it is a pointer, or could plausibly contain
73   // a pointer.  There are two challenges; one is that we could have a struct
74   // the has an inner member which is a pointer.  We recurse through the type to
75   // detect these (up to a point).  The other is that we may actually be a union
76   // of a pointer and another type, and so our LLVM type is an integer which
77   // gets converted into a pointer, or our type is an [i8 x #] with a pointer
78   // potentially contained here.
79 
80   if (GV->hasPrivateLinkage())
81     return false;
82 
83   SmallVector<Type *, 4> Types;
84   Types.push_back(GV->getValueType());
85 
86   unsigned Limit = 20;
87   do {
88     Type *Ty = Types.pop_back_val();
89     switch (Ty->getTypeID()) {
90       default: break;
91       case Type::PointerTyID: return true;
92       case Type::ArrayTyID:
93       case Type::VectorTyID: {
94         SequentialType *STy = cast<SequentialType>(Ty);
95         Types.push_back(STy->getElementType());
96         break;
97       }
98       case Type::StructTyID: {
99         StructType *STy = cast<StructType>(Ty);
100         if (STy->isOpaque()) return true;
101         for (StructType::element_iterator I = STy->element_begin(),
102                  E = STy->element_end(); I != E; ++I) {
103           Type *InnerTy = *I;
104           if (isa<PointerType>(InnerTy)) return true;
105           if (isa<CompositeType>(InnerTy))
106             Types.push_back(InnerTy);
107         }
108         break;
109       }
110     }
111     if (--Limit == 0) return true;
112   } while (!Types.empty());
113   return false;
114 }
115 
116 /// Given a value that is stored to a global but never read, determine whether
117 /// it's safe to remove the store and the chain of computation that feeds the
118 /// store.
119 static bool IsSafeComputationToRemove(Value *V, const TargetLibraryInfo *TLI) {
120   do {
121     if (isa<Constant>(V))
122       return true;
123     if (!V->hasOneUse())
124       return false;
125     if (isa<LoadInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V) ||
126         isa<GlobalValue>(V))
127       return false;
128     if (isAllocationFn(V, TLI))
129       return true;
130 
131     Instruction *I = cast<Instruction>(V);
132     if (I->mayHaveSideEffects())
133       return false;
134     if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
135       if (!GEP->hasAllConstantIndices())
136         return false;
137     } else if (I->getNumOperands() != 1) {
138       return false;
139     }
140 
141     V = I->getOperand(0);
142   } while (1);
143 }
144 
145 /// This GV is a pointer root.  Loop over all users of the global and clean up
146 /// any that obviously don't assign the global a value that isn't dynamically
147 /// allocated.
148 static bool CleanupPointerRootUsers(GlobalVariable *GV,
149                                     const TargetLibraryInfo *TLI) {
150   // A brief explanation of leak checkers.  The goal is to find bugs where
151   // pointers are forgotten, causing an accumulating growth in memory
152   // usage over time.  The common strategy for leak checkers is to whitelist the
153   // memory pointed to by globals at exit.  This is popular because it also
154   // solves another problem where the main thread of a C++ program may shut down
155   // before other threads that are still expecting to use those globals.  To
156   // handle that case, we expect the program may create a singleton and never
157   // destroy it.
158 
159   bool Changed = false;
160 
161   // If Dead[n].first is the only use of a malloc result, we can delete its
162   // chain of computation and the store to the global in Dead[n].second.
163   SmallVector<std::pair<Instruction *, Instruction *>, 32> Dead;
164 
165   // Constants can't be pointers to dynamically allocated memory.
166   for (Value::user_iterator UI = GV->user_begin(), E = GV->user_end();
167        UI != E;) {
168     User *U = *UI++;
169     if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
170       Value *V = SI->getValueOperand();
171       if (isa<Constant>(V)) {
172         Changed = true;
173         SI->eraseFromParent();
174       } else if (Instruction *I = dyn_cast<Instruction>(V)) {
175         if (I->hasOneUse())
176           Dead.push_back(std::make_pair(I, SI));
177       }
178     } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(U)) {
179       if (isa<Constant>(MSI->getValue())) {
180         Changed = true;
181         MSI->eraseFromParent();
182       } else if (Instruction *I = dyn_cast<Instruction>(MSI->getValue())) {
183         if (I->hasOneUse())
184           Dead.push_back(std::make_pair(I, MSI));
185       }
186     } else if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(U)) {
187       GlobalVariable *MemSrc = dyn_cast<GlobalVariable>(MTI->getSource());
188       if (MemSrc && MemSrc->isConstant()) {
189         Changed = true;
190         MTI->eraseFromParent();
191       } else if (Instruction *I = dyn_cast<Instruction>(MemSrc)) {
192         if (I->hasOneUse())
193           Dead.push_back(std::make_pair(I, MTI));
194       }
195     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
196       if (CE->use_empty()) {
197         CE->destroyConstant();
198         Changed = true;
199       }
200     } else if (Constant *C = dyn_cast<Constant>(U)) {
201       if (isSafeToDestroyConstant(C)) {
202         C->destroyConstant();
203         // This could have invalidated UI, start over from scratch.
204         Dead.clear();
205         CleanupPointerRootUsers(GV, TLI);
206         return true;
207       }
208     }
209   }
210 
211   for (int i = 0, e = Dead.size(); i != e; ++i) {
212     if (IsSafeComputationToRemove(Dead[i].first, TLI)) {
213       Dead[i].second->eraseFromParent();
214       Instruction *I = Dead[i].first;
215       do {
216         if (isAllocationFn(I, TLI))
217           break;
218         Instruction *J = dyn_cast<Instruction>(I->getOperand(0));
219         if (!J)
220           break;
221         I->eraseFromParent();
222         I = J;
223       } while (1);
224       I->eraseFromParent();
225     }
226   }
227 
228   return Changed;
229 }
230 
231 /// We just marked GV constant.  Loop over all users of the global, cleaning up
232 /// the obvious ones.  This is largely just a quick scan over the use list to
233 /// clean up the easy and obvious cruft.  This returns true if it made a change.
234 static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
235                                        const DataLayout &DL,
236                                        TargetLibraryInfo *TLI) {
237   bool Changed = false;
238   // Note that we need to use a weak value handle for the worklist items. When
239   // we delete a constant array, we may also be holding pointer to one of its
240   // elements (or an element of one of its elements if we're dealing with an
241   // array of arrays) in the worklist.
242   SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
243   while (!WorkList.empty()) {
244     Value *UV = WorkList.pop_back_val();
245     if (!UV)
246       continue;
247 
248     User *U = cast<User>(UV);
249 
250     if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
251       if (Init) {
252         // Replace the load with the initializer.
253         LI->replaceAllUsesWith(Init);
254         LI->eraseFromParent();
255         Changed = true;
256       }
257     } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
258       // Store must be unreachable or storing Init into the global.
259       SI->eraseFromParent();
260       Changed = true;
261     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
262       if (CE->getOpcode() == Instruction::GetElementPtr) {
263         Constant *SubInit = nullptr;
264         if (Init)
265           SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
266         Changed |= CleanupConstantGlobalUsers(CE, SubInit, DL, TLI);
267       } else if ((CE->getOpcode() == Instruction::BitCast &&
268                   CE->getType()->isPointerTy()) ||
269                  CE->getOpcode() == Instruction::AddrSpaceCast) {
270         // Pointer cast, delete any stores and memsets to the global.
271         Changed |= CleanupConstantGlobalUsers(CE, nullptr, DL, TLI);
272       }
273 
274       if (CE->use_empty()) {
275         CE->destroyConstant();
276         Changed = true;
277       }
278     } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
279       // Do not transform "gepinst (gep constexpr (GV))" here, because forming
280       // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
281       // and will invalidate our notion of what Init is.
282       Constant *SubInit = nullptr;
283       if (!isa<ConstantExpr>(GEP->getOperand(0))) {
284         ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(
285             ConstantFoldInstruction(GEP, DL, TLI));
286         if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
287           SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
288 
289         // If the initializer is an all-null value and we have an inbounds GEP,
290         // we already know what the result of any load from that GEP is.
291         // TODO: Handle splats.
292         if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
293           SubInit = Constant::getNullValue(GEP->getResultElementType());
294       }
295       Changed |= CleanupConstantGlobalUsers(GEP, SubInit, DL, TLI);
296 
297       if (GEP->use_empty()) {
298         GEP->eraseFromParent();
299         Changed = true;
300       }
301     } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
302       if (MI->getRawDest() == V) {
303         MI->eraseFromParent();
304         Changed = true;
305       }
306 
307     } else if (Constant *C = dyn_cast<Constant>(U)) {
308       // If we have a chain of dead constantexprs or other things dangling from
309       // us, and if they are all dead, nuke them without remorse.
310       if (isSafeToDestroyConstant(C)) {
311         C->destroyConstant();
312         CleanupConstantGlobalUsers(V, Init, DL, TLI);
313         return true;
314       }
315     }
316   }
317   return Changed;
318 }
319 
320 /// Return true if the specified instruction is a safe user of a derived
321 /// expression from a global that we want to SROA.
322 static bool isSafeSROAElementUse(Value *V) {
323   // We might have a dead and dangling constant hanging off of here.
324   if (Constant *C = dyn_cast<Constant>(V))
325     return isSafeToDestroyConstant(C);
326 
327   Instruction *I = dyn_cast<Instruction>(V);
328   if (!I) return false;
329 
330   // Loads are ok.
331   if (isa<LoadInst>(I)) return true;
332 
333   // Stores *to* the pointer are ok.
334   if (StoreInst *SI = dyn_cast<StoreInst>(I))
335     return SI->getOperand(0) != V;
336 
337   // Otherwise, it must be a GEP.
338   GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
339   if (!GEPI) return false;
340 
341   if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
342       !cast<Constant>(GEPI->getOperand(1))->isNullValue())
343     return false;
344 
345   for (User *U : GEPI->users())
346     if (!isSafeSROAElementUse(U))
347       return false;
348   return true;
349 }
350 
351 
352 /// U is a direct user of the specified global value.  Look at it and its uses
353 /// and decide whether it is safe to SROA this global.
354 static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
355   // The user of the global must be a GEP Inst or a ConstantExpr GEP.
356   if (!isa<GetElementPtrInst>(U) &&
357       (!isa<ConstantExpr>(U) ||
358        cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
359     return false;
360 
361   // Check to see if this ConstantExpr GEP is SRA'able.  In particular, we
362   // don't like < 3 operand CE's, and we don't like non-constant integer
363   // indices.  This enforces that all uses are 'gep GV, 0, C, ...' for some
364   // value of C.
365   if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
366       !cast<Constant>(U->getOperand(1))->isNullValue() ||
367       !isa<ConstantInt>(U->getOperand(2)))
368     return false;
369 
370   gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
371   ++GEPI;  // Skip over the pointer index.
372 
373   // If this is a use of an array allocation, do a bit more checking for sanity.
374   if (GEPI.isSequential()) {
375     ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
376 
377     // Check to make sure that index falls within the array.  If not,
378     // something funny is going on, so we won't do the optimization.
379     //
380     if (GEPI.isBoundedSequential() &&
381         Idx->getZExtValue() >= GEPI.getSequentialNumElements())
382       return false;
383 
384     // We cannot scalar repl this level of the array unless any array
385     // sub-indices are in-range constants.  In particular, consider:
386     // A[0][i].  We cannot know that the user isn't doing invalid things like
387     // allowing i to index an out-of-range subscript that accesses A[1].
388     //
389     // Scalar replacing *just* the outer index of the array is probably not
390     // going to be a win anyway, so just give up.
391     for (++GEPI; // Skip array index.
392          GEPI != E;
393          ++GEPI) {
394       if (GEPI.isStruct())
395         continue;
396 
397       ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
398       if (!IdxVal ||
399           (GEPI.isBoundedSequential() &&
400            IdxVal->getZExtValue() >= GEPI.getSequentialNumElements()))
401         return false;
402     }
403   }
404 
405   for (User *UU : U->users())
406     if (!isSafeSROAElementUse(UU))
407       return false;
408 
409   return true;
410 }
411 
412 /// Look at all uses of the global and decide whether it is safe for us to
413 /// perform this transformation.
414 static bool GlobalUsersSafeToSRA(GlobalValue *GV) {
415   for (User *U : GV->users())
416     if (!IsUserOfGlobalSafeForSRA(U, GV))
417       return false;
418 
419   return true;
420 }
421 
422 
423 /// Perform scalar replacement of aggregates on the specified global variable.
424 /// This opens the door for other optimizations by exposing the behavior of the
425 /// program in a more fine-grained way.  We have determined that this
426 /// transformation is safe already.  We return the first global variable we
427 /// insert so that the caller can reprocess it.
428 static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
429   // Make sure this global only has simple uses that we can SRA.
430   if (!GlobalUsersSafeToSRA(GV))
431     return nullptr;
432 
433   assert(GV->hasLocalLinkage());
434   Constant *Init = GV->getInitializer();
435   Type *Ty = Init->getType();
436 
437   std::vector<GlobalVariable*> NewGlobals;
438   Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
439 
440   // Get the alignment of the global, either explicit or target-specific.
441   unsigned StartAlignment = GV->getAlignment();
442   if (StartAlignment == 0)
443     StartAlignment = DL.getABITypeAlignment(GV->getType());
444 
445   if (StructType *STy = dyn_cast<StructType>(Ty)) {
446     NewGlobals.reserve(STy->getNumElements());
447     const StructLayout &Layout = *DL.getStructLayout(STy);
448     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
449       Constant *In = Init->getAggregateElement(i);
450       assert(In && "Couldn't get element of initializer?");
451       GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
452                                                GlobalVariable::InternalLinkage,
453                                                In, GV->getName()+"."+Twine(i),
454                                                GV->getThreadLocalMode(),
455                                               GV->getType()->getAddressSpace());
456       NGV->setExternallyInitialized(GV->isExternallyInitialized());
457       NGV->copyAttributesFrom(GV);
458       Globals.push_back(NGV);
459       NewGlobals.push_back(NGV);
460 
461       // Calculate the known alignment of the field.  If the original aggregate
462       // had 256 byte alignment for example, something might depend on that:
463       // propagate info to each field.
464       uint64_t FieldOffset = Layout.getElementOffset(i);
465       unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
466       if (NewAlign > DL.getABITypeAlignment(STy->getElementType(i)))
467         NGV->setAlignment(NewAlign);
468     }
469   } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
470     unsigned NumElements = STy->getNumElements();
471     if (NumElements > 16 && GV->hasNUsesOrMore(16))
472       return nullptr; // It's not worth it.
473     NewGlobals.reserve(NumElements);
474 
475     uint64_t EltSize = DL.getTypeAllocSize(STy->getElementType());
476     unsigned EltAlign = DL.getABITypeAlignment(STy->getElementType());
477     for (unsigned i = 0, e = NumElements; i != e; ++i) {
478       Constant *In = Init->getAggregateElement(i);
479       assert(In && "Couldn't get element of initializer?");
480 
481       GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
482                                                GlobalVariable::InternalLinkage,
483                                                In, GV->getName()+"."+Twine(i),
484                                                GV->getThreadLocalMode(),
485                                               GV->getType()->getAddressSpace());
486       NGV->setExternallyInitialized(GV->isExternallyInitialized());
487       NGV->copyAttributesFrom(GV);
488       Globals.push_back(NGV);
489       NewGlobals.push_back(NGV);
490 
491       // Calculate the known alignment of the field.  If the original aggregate
492       // had 256 byte alignment for example, something might depend on that:
493       // propagate info to each field.
494       unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
495       if (NewAlign > EltAlign)
496         NGV->setAlignment(NewAlign);
497     }
498   }
499 
500   if (NewGlobals.empty())
501     return nullptr;
502 
503   DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV << "\n");
504 
505   Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
506 
507   // Loop over all of the uses of the global, replacing the constantexpr geps,
508   // with smaller constantexpr geps or direct references.
509   while (!GV->use_empty()) {
510     User *GEP = GV->user_back();
511     assert(((isa<ConstantExpr>(GEP) &&
512              cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
513             isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
514 
515     // Ignore the 1th operand, which has to be zero or else the program is quite
516     // broken (undefined).  Get the 2nd operand, which is the structure or array
517     // index.
518     unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
519     if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
520 
521     Value *NewPtr = NewGlobals[Val];
522     Type *NewTy = NewGlobals[Val]->getValueType();
523 
524     // Form a shorter GEP if needed.
525     if (GEP->getNumOperands() > 3) {
526       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
527         SmallVector<Constant*, 8> Idxs;
528         Idxs.push_back(NullInt);
529         for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
530           Idxs.push_back(CE->getOperand(i));
531         NewPtr =
532             ConstantExpr::getGetElementPtr(NewTy, cast<Constant>(NewPtr), Idxs);
533       } else {
534         GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
535         SmallVector<Value*, 8> Idxs;
536         Idxs.push_back(NullInt);
537         for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
538           Idxs.push_back(GEPI->getOperand(i));
539         NewPtr = GetElementPtrInst::Create(
540             NewTy, NewPtr, Idxs, GEPI->getName() + "." + Twine(Val), GEPI);
541       }
542     }
543     GEP->replaceAllUsesWith(NewPtr);
544 
545     if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
546       GEPI->eraseFromParent();
547     else
548       cast<ConstantExpr>(GEP)->destroyConstant();
549   }
550 
551   // Delete the old global, now that it is dead.
552   Globals.erase(GV);
553   ++NumSRA;
554 
555   // Loop over the new globals array deleting any globals that are obviously
556   // dead.  This can arise due to scalarization of a structure or an array that
557   // has elements that are dead.
558   unsigned FirstGlobal = 0;
559   for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
560     if (NewGlobals[i]->use_empty()) {
561       Globals.erase(NewGlobals[i]);
562       if (FirstGlobal == i) ++FirstGlobal;
563     }
564 
565   return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : nullptr;
566 }
567 
568 /// Return true if all users of the specified value will trap if the value is
569 /// dynamically null.  PHIs keeps track of any phi nodes we've seen to avoid
570 /// reprocessing them.
571 static bool AllUsesOfValueWillTrapIfNull(const Value *V,
572                                         SmallPtrSetImpl<const PHINode*> &PHIs) {
573   for (const User *U : V->users())
574     if (isa<LoadInst>(U)) {
575       // Will trap.
576     } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
577       if (SI->getOperand(0) == V) {
578         //cerr << "NONTRAPPING USE: " << *U;
579         return false;  // Storing the value.
580       }
581     } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
582       if (CI->getCalledValue() != V) {
583         //cerr << "NONTRAPPING USE: " << *U;
584         return false;  // Not calling the ptr
585       }
586     } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
587       if (II->getCalledValue() != V) {
588         //cerr << "NONTRAPPING USE: " << *U;
589         return false;  // Not calling the ptr
590       }
591     } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
592       if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
593     } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
594       if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
595     } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
596       // If we've already seen this phi node, ignore it, it has already been
597       // checked.
598       if (PHIs.insert(PN).second && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
599         return false;
600     } else if (isa<ICmpInst>(U) &&
601                isa<ConstantPointerNull>(U->getOperand(1))) {
602       // Ignore icmp X, null
603     } else {
604       //cerr << "NONTRAPPING USE: " << *U;
605       return false;
606     }
607 
608   return true;
609 }
610 
611 /// Return true if all uses of any loads from GV will trap if the loaded value
612 /// is null.  Note that this also permits comparisons of the loaded value
613 /// against null, as a special case.
614 static bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
615   for (const User *U : GV->users())
616     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
617       SmallPtrSet<const PHINode*, 8> PHIs;
618       if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
619         return false;
620     } else if (isa<StoreInst>(U)) {
621       // Ignore stores to the global.
622     } else {
623       // We don't know or understand this user, bail out.
624       //cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
625       return false;
626     }
627   return true;
628 }
629 
630 static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
631   bool Changed = false;
632   for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
633     Instruction *I = cast<Instruction>(*UI++);
634     if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
635       LI->setOperand(0, NewV);
636       Changed = true;
637     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
638       if (SI->getOperand(1) == V) {
639         SI->setOperand(1, NewV);
640         Changed = true;
641       }
642     } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
643       CallSite CS(I);
644       if (CS.getCalledValue() == V) {
645         // Calling through the pointer!  Turn into a direct call, but be careful
646         // that the pointer is not also being passed as an argument.
647         CS.setCalledFunction(NewV);
648         Changed = true;
649         bool PassedAsArg = false;
650         for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
651           if (CS.getArgument(i) == V) {
652             PassedAsArg = true;
653             CS.setArgument(i, NewV);
654           }
655 
656         if (PassedAsArg) {
657           // Being passed as an argument also.  Be careful to not invalidate UI!
658           UI = V->user_begin();
659         }
660       }
661     } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
662       Changed |= OptimizeAwayTrappingUsesOfValue(CI,
663                                 ConstantExpr::getCast(CI->getOpcode(),
664                                                       NewV, CI->getType()));
665       if (CI->use_empty()) {
666         Changed = true;
667         CI->eraseFromParent();
668       }
669     } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
670       // Should handle GEP here.
671       SmallVector<Constant*, 8> Idxs;
672       Idxs.reserve(GEPI->getNumOperands()-1);
673       for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
674            i != e; ++i)
675         if (Constant *C = dyn_cast<Constant>(*i))
676           Idxs.push_back(C);
677         else
678           break;
679       if (Idxs.size() == GEPI->getNumOperands()-1)
680         Changed |= OptimizeAwayTrappingUsesOfValue(
681             GEPI, ConstantExpr::getGetElementPtr(nullptr, NewV, Idxs));
682       if (GEPI->use_empty()) {
683         Changed = true;
684         GEPI->eraseFromParent();
685       }
686     }
687   }
688 
689   return Changed;
690 }
691 
692 
693 /// The specified global has only one non-null value stored into it.  If there
694 /// are uses of the loaded value that would trap if the loaded value is
695 /// dynamically null, then we know that they cannot be reachable with a null
696 /// optimize away the load.
697 static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
698                                             const DataLayout &DL,
699                                             TargetLibraryInfo *TLI) {
700   bool Changed = false;
701 
702   // Keep track of whether we are able to remove all the uses of the global
703   // other than the store that defines it.
704   bool AllNonStoreUsesGone = true;
705 
706   // Replace all uses of loads with uses of uses of the stored value.
707   for (Value::user_iterator GUI = GV->user_begin(), E = GV->user_end(); GUI != E;){
708     User *GlobalUser = *GUI++;
709     if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
710       Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
711       // If we were able to delete all uses of the loads
712       if (LI->use_empty()) {
713         LI->eraseFromParent();
714         Changed = true;
715       } else {
716         AllNonStoreUsesGone = false;
717       }
718     } else if (isa<StoreInst>(GlobalUser)) {
719       // Ignore the store that stores "LV" to the global.
720       assert(GlobalUser->getOperand(1) == GV &&
721              "Must be storing *to* the global");
722     } else {
723       AllNonStoreUsesGone = false;
724 
725       // If we get here we could have other crazy uses that are transitively
726       // loaded.
727       assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
728               isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser) ||
729               isa<BitCastInst>(GlobalUser) ||
730               isa<GetElementPtrInst>(GlobalUser)) &&
731              "Only expect load and stores!");
732     }
733   }
734 
735   if (Changed) {
736     DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV << "\n");
737     ++NumGlobUses;
738   }
739 
740   // If we nuked all of the loads, then none of the stores are needed either,
741   // nor is the global.
742   if (AllNonStoreUsesGone) {
743     if (isLeakCheckerRoot(GV)) {
744       Changed |= CleanupPointerRootUsers(GV, TLI);
745     } else {
746       Changed = true;
747       CleanupConstantGlobalUsers(GV, nullptr, DL, TLI);
748     }
749     if (GV->use_empty()) {
750       DEBUG(dbgs() << "  *** GLOBAL NOW DEAD!\n");
751       Changed = true;
752       GV->eraseFromParent();
753       ++NumDeleted;
754     }
755   }
756   return Changed;
757 }
758 
759 /// Walk the use list of V, constant folding all of the instructions that are
760 /// foldable.
761 static void ConstantPropUsersOf(Value *V, const DataLayout &DL,
762                                 TargetLibraryInfo *TLI) {
763   for (Value::user_iterator UI = V->user_begin(), E = V->user_end(); UI != E; )
764     if (Instruction *I = dyn_cast<Instruction>(*UI++))
765       if (Constant *NewC = ConstantFoldInstruction(I, DL, TLI)) {
766         I->replaceAllUsesWith(NewC);
767 
768         // Advance UI to the next non-I use to avoid invalidating it!
769         // Instructions could multiply use V.
770         while (UI != E && *UI == I)
771           ++UI;
772         if (isInstructionTriviallyDead(I, TLI))
773           I->eraseFromParent();
774       }
775 }
776 
777 /// This function takes the specified global variable, and transforms the
778 /// program as if it always contained the result of the specified malloc.
779 /// Because it is always the result of the specified malloc, there is no reason
780 /// to actually DO the malloc.  Instead, turn the malloc into a global, and any
781 /// loads of GV as uses of the new global.
782 static GlobalVariable *
783 OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, CallInst *CI, Type *AllocTy,
784                               ConstantInt *NElements, const DataLayout &DL,
785                               TargetLibraryInfo *TLI) {
786   DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << "  CALL = " << *CI << '\n');
787 
788   Type *GlobalType;
789   if (NElements->getZExtValue() == 1)
790     GlobalType = AllocTy;
791   else
792     // If we have an array allocation, the global variable is of an array.
793     GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
794 
795   // Create the new global variable.  The contents of the malloc'd memory is
796   // undefined, so initialize with an undef value.
797   GlobalVariable *NewGV = new GlobalVariable(
798       *GV->getParent(), GlobalType, false, GlobalValue::InternalLinkage,
799       UndefValue::get(GlobalType), GV->getName() + ".body", nullptr,
800       GV->getThreadLocalMode());
801 
802   // If there are bitcast users of the malloc (which is typical, usually we have
803   // a malloc + bitcast) then replace them with uses of the new global.  Update
804   // other users to use the global as well.
805   BitCastInst *TheBC = nullptr;
806   while (!CI->use_empty()) {
807     Instruction *User = cast<Instruction>(CI->user_back());
808     if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
809       if (BCI->getType() == NewGV->getType()) {
810         BCI->replaceAllUsesWith(NewGV);
811         BCI->eraseFromParent();
812       } else {
813         BCI->setOperand(0, NewGV);
814       }
815     } else {
816       if (!TheBC)
817         TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
818       User->replaceUsesOfWith(CI, TheBC);
819     }
820   }
821 
822   Constant *RepValue = NewGV;
823   if (NewGV->getType() != GV->getValueType())
824     RepValue = ConstantExpr::getBitCast(RepValue, GV->getValueType());
825 
826   // If there is a comparison against null, we will insert a global bool to
827   // keep track of whether the global was initialized yet or not.
828   GlobalVariable *InitBool =
829     new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
830                        GlobalValue::InternalLinkage,
831                        ConstantInt::getFalse(GV->getContext()),
832                        GV->getName()+".init", GV->getThreadLocalMode());
833   bool InitBoolUsed = false;
834 
835   // Loop over all uses of GV, processing them in turn.
836   while (!GV->use_empty()) {
837     if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
838       // The global is initialized when the store to it occurs.
839       new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
840                     SI->getOrdering(), SI->getSynchScope(), SI);
841       SI->eraseFromParent();
842       continue;
843     }
844 
845     LoadInst *LI = cast<LoadInst>(GV->user_back());
846     while (!LI->use_empty()) {
847       Use &LoadUse = *LI->use_begin();
848       ICmpInst *ICI = dyn_cast<ICmpInst>(LoadUse.getUser());
849       if (!ICI) {
850         LoadUse = RepValue;
851         continue;
852       }
853 
854       // Replace the cmp X, 0 with a use of the bool value.
855       // Sink the load to where the compare was, if atomic rules allow us to.
856       Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
857                                LI->getOrdering(), LI->getSynchScope(),
858                                LI->isUnordered() ? (Instruction*)ICI : LI);
859       InitBoolUsed = true;
860       switch (ICI->getPredicate()) {
861       default: llvm_unreachable("Unknown ICmp Predicate!");
862       case ICmpInst::ICMP_ULT:
863       case ICmpInst::ICMP_SLT:   // X < null -> always false
864         LV = ConstantInt::getFalse(GV->getContext());
865         break;
866       case ICmpInst::ICMP_ULE:
867       case ICmpInst::ICMP_SLE:
868       case ICmpInst::ICMP_EQ:
869         LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
870         break;
871       case ICmpInst::ICMP_NE:
872       case ICmpInst::ICMP_UGE:
873       case ICmpInst::ICMP_SGE:
874       case ICmpInst::ICMP_UGT:
875       case ICmpInst::ICMP_SGT:
876         break;  // no change.
877       }
878       ICI->replaceAllUsesWith(LV);
879       ICI->eraseFromParent();
880     }
881     LI->eraseFromParent();
882   }
883 
884   // If the initialization boolean was used, insert it, otherwise delete it.
885   if (!InitBoolUsed) {
886     while (!InitBool->use_empty())  // Delete initializations
887       cast<StoreInst>(InitBool->user_back())->eraseFromParent();
888     delete InitBool;
889   } else
890     GV->getParent()->getGlobalList().insert(GV->getIterator(), InitBool);
891 
892   // Now the GV is dead, nuke it and the malloc..
893   GV->eraseFromParent();
894   CI->eraseFromParent();
895 
896   // To further other optimizations, loop over all users of NewGV and try to
897   // constant prop them.  This will promote GEP instructions with constant
898   // indices into GEP constant-exprs, which will allow global-opt to hack on it.
899   ConstantPropUsersOf(NewGV, DL, TLI);
900   if (RepValue != NewGV)
901     ConstantPropUsersOf(RepValue, DL, TLI);
902 
903   return NewGV;
904 }
905 
906 /// Scan the use-list of V checking to make sure that there are no complex uses
907 /// of V.  We permit simple things like dereferencing the pointer, but not
908 /// storing through the address, unless it is to the specified global.
909 static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
910                                                       const GlobalVariable *GV,
911                                         SmallPtrSetImpl<const PHINode*> &PHIs) {
912   for (const User *U : V->users()) {
913     const Instruction *Inst = cast<Instruction>(U);
914 
915     if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
916       continue; // Fine, ignore.
917     }
918 
919     if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
920       if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
921         return false;  // Storing the pointer itself... bad.
922       continue; // Otherwise, storing through it, or storing into GV... fine.
923     }
924 
925     // Must index into the array and into the struct.
926     if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
927       if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
928         return false;
929       continue;
930     }
931 
932     if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
933       // PHIs are ok if all uses are ok.  Don't infinitely recurse through PHI
934       // cycles.
935       if (PHIs.insert(PN).second)
936         if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs))
937           return false;
938       continue;
939     }
940 
941     if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
942       if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
943         return false;
944       continue;
945     }
946 
947     return false;
948   }
949   return true;
950 }
951 
952 /// The Alloc pointer is stored into GV somewhere.  Transform all uses of the
953 /// allocation into loads from the global and uses of the resultant pointer.
954 /// Further, delete the store into GV.  This assumes that these value pass the
955 /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
956 static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
957                                           GlobalVariable *GV) {
958   while (!Alloc->use_empty()) {
959     Instruction *U = cast<Instruction>(*Alloc->user_begin());
960     Instruction *InsertPt = U;
961     if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
962       // If this is the store of the allocation into the global, remove it.
963       if (SI->getOperand(1) == GV) {
964         SI->eraseFromParent();
965         continue;
966       }
967     } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
968       // Insert the load in the corresponding predecessor, not right before the
969       // PHI.
970       InsertPt = PN->getIncomingBlock(*Alloc->use_begin())->getTerminator();
971     } else if (isa<BitCastInst>(U)) {
972       // Must be bitcast between the malloc and store to initialize the global.
973       ReplaceUsesOfMallocWithGlobal(U, GV);
974       U->eraseFromParent();
975       continue;
976     } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
977       // If this is a "GEP bitcast" and the user is a store to the global, then
978       // just process it as a bitcast.
979       if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
980         if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->user_back()))
981           if (SI->getOperand(1) == GV) {
982             // Must be bitcast GEP between the malloc and store to initialize
983             // the global.
984             ReplaceUsesOfMallocWithGlobal(GEPI, GV);
985             GEPI->eraseFromParent();
986             continue;
987           }
988     }
989 
990     // Insert a load from the global, and use it instead of the malloc.
991     Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
992     U->replaceUsesOfWith(Alloc, NL);
993   }
994 }
995 
996 /// Verify that all uses of V (a load, or a phi of a load) are simple enough to
997 /// perform heap SRA on.  This permits GEP's that index through the array and
998 /// struct field, icmps of null, and PHIs.
999 static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
1000                         SmallPtrSetImpl<const PHINode*> &LoadUsingPHIs,
1001                         SmallPtrSetImpl<const PHINode*> &LoadUsingPHIsPerLoad) {
1002   // We permit two users of the load: setcc comparing against the null
1003   // pointer, and a getelementptr of a specific form.
1004   for (const User *U : V->users()) {
1005     const Instruction *UI = cast<Instruction>(U);
1006 
1007     // Comparison against null is ok.
1008     if (const ICmpInst *ICI = dyn_cast<ICmpInst>(UI)) {
1009       if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
1010         return false;
1011       continue;
1012     }
1013 
1014     // getelementptr is also ok, but only a simple form.
1015     if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) {
1016       // Must index into the array and into the struct.
1017       if (GEPI->getNumOperands() < 3)
1018         return false;
1019 
1020       // Otherwise the GEP is ok.
1021       continue;
1022     }
1023 
1024     if (const PHINode *PN = dyn_cast<PHINode>(UI)) {
1025       if (!LoadUsingPHIsPerLoad.insert(PN).second)
1026         // This means some phi nodes are dependent on each other.
1027         // Avoid infinite looping!
1028         return false;
1029       if (!LoadUsingPHIs.insert(PN).second)
1030         // If we have already analyzed this PHI, then it is safe.
1031         continue;
1032 
1033       // Make sure all uses of the PHI are simple enough to transform.
1034       if (!LoadUsesSimpleEnoughForHeapSRA(PN,
1035                                           LoadUsingPHIs, LoadUsingPHIsPerLoad))
1036         return false;
1037 
1038       continue;
1039     }
1040 
1041     // Otherwise we don't know what this is, not ok.
1042     return false;
1043   }
1044 
1045   return true;
1046 }
1047 
1048 
1049 /// If all users of values loaded from GV are simple enough to perform HeapSRA,
1050 /// return true.
1051 static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
1052                                                     Instruction *StoredVal) {
1053   SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
1054   SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
1055   for (const User *U : GV->users())
1056     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
1057       if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
1058                                           LoadUsingPHIsPerLoad))
1059         return false;
1060       LoadUsingPHIsPerLoad.clear();
1061     }
1062 
1063   // If we reach here, we know that all uses of the loads and transitive uses
1064   // (through PHI nodes) are simple enough to transform.  However, we don't know
1065   // that all inputs the to the PHI nodes are in the same equivalence sets.
1066   // Check to verify that all operands of the PHIs are either PHIS that can be
1067   // transformed, loads from GV, or MI itself.
1068   for (const PHINode *PN : LoadUsingPHIs) {
1069     for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
1070       Value *InVal = PN->getIncomingValue(op);
1071 
1072       // PHI of the stored value itself is ok.
1073       if (InVal == StoredVal) continue;
1074 
1075       if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
1076         // One of the PHIs in our set is (optimistically) ok.
1077         if (LoadUsingPHIs.count(InPN))
1078           continue;
1079         return false;
1080       }
1081 
1082       // Load from GV is ok.
1083       if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
1084         if (LI->getOperand(0) == GV)
1085           continue;
1086 
1087       // UNDEF? NULL?
1088 
1089       // Anything else is rejected.
1090       return false;
1091     }
1092   }
1093 
1094   return true;
1095 }
1096 
1097 static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
1098                DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1099                    std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1100   std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
1101 
1102   if (FieldNo >= FieldVals.size())
1103     FieldVals.resize(FieldNo+1);
1104 
1105   // If we already have this value, just reuse the previously scalarized
1106   // version.
1107   if (Value *FieldVal = FieldVals[FieldNo])
1108     return FieldVal;
1109 
1110   // Depending on what instruction this is, we have several cases.
1111   Value *Result;
1112   if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
1113     // This is a scalarized version of the load from the global.  Just create
1114     // a new Load of the scalarized global.
1115     Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
1116                                            InsertedScalarizedValues,
1117                                            PHIsToRewrite),
1118                           LI->getName()+".f"+Twine(FieldNo), LI);
1119   } else {
1120     PHINode *PN = cast<PHINode>(V);
1121     // PN's type is pointer to struct.  Make a new PHI of pointer to struct
1122     // field.
1123 
1124     PointerType *PTy = cast<PointerType>(PN->getType());
1125     StructType *ST = cast<StructType>(PTy->getElementType());
1126 
1127     unsigned AS = PTy->getAddressSpace();
1128     PHINode *NewPN =
1129       PHINode::Create(PointerType::get(ST->getElementType(FieldNo), AS),
1130                      PN->getNumIncomingValues(),
1131                      PN->getName()+".f"+Twine(FieldNo), PN);
1132     Result = NewPN;
1133     PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
1134   }
1135 
1136   return FieldVals[FieldNo] = Result;
1137 }
1138 
1139 /// Given a load instruction and a value derived from the load, rewrite the
1140 /// derived value to use the HeapSRoA'd load.
1141 static void RewriteHeapSROALoadUser(Instruction *LoadUser,
1142              DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1143                    std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1144   // If this is a comparison against null, handle it.
1145   if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
1146     assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
1147     // If we have a setcc of the loaded pointer, we can use a setcc of any
1148     // field.
1149     Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
1150                                    InsertedScalarizedValues, PHIsToRewrite);
1151 
1152     Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
1153                               Constant::getNullValue(NPtr->getType()),
1154                               SCI->getName());
1155     SCI->replaceAllUsesWith(New);
1156     SCI->eraseFromParent();
1157     return;
1158   }
1159 
1160   // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
1161   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
1162     assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
1163            && "Unexpected GEPI!");
1164 
1165     // Load the pointer for this field.
1166     unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
1167     Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
1168                                      InsertedScalarizedValues, PHIsToRewrite);
1169 
1170     // Create the new GEP idx vector.
1171     SmallVector<Value*, 8> GEPIdx;
1172     GEPIdx.push_back(GEPI->getOperand(1));
1173     GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
1174 
1175     Value *NGEPI = GetElementPtrInst::Create(GEPI->getResultElementType(), NewPtr, GEPIdx,
1176                                              GEPI->getName(), GEPI);
1177     GEPI->replaceAllUsesWith(NGEPI);
1178     GEPI->eraseFromParent();
1179     return;
1180   }
1181 
1182   // Recursively transform the users of PHI nodes.  This will lazily create the
1183   // PHIs that are needed for individual elements.  Keep track of what PHIs we
1184   // see in InsertedScalarizedValues so that we don't get infinite loops (very
1185   // antisocial).  If the PHI is already in InsertedScalarizedValues, it has
1186   // already been seen first by another load, so its uses have already been
1187   // processed.
1188   PHINode *PN = cast<PHINode>(LoadUser);
1189   if (!InsertedScalarizedValues.insert(std::make_pair(PN,
1190                                               std::vector<Value*>())).second)
1191     return;
1192 
1193   // If this is the first time we've seen this PHI, recursively process all
1194   // users.
1195   for (auto UI = PN->user_begin(), E = PN->user_end(); UI != E;) {
1196     Instruction *User = cast<Instruction>(*UI++);
1197     RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1198   }
1199 }
1200 
1201 /// We are performing Heap SRoA on a global.  Ptr is a value loaded from the
1202 /// global.  Eliminate all uses of Ptr, making them use FieldGlobals instead.
1203 /// All uses of loaded values satisfy AllGlobalLoadUsesSimpleEnoughForHeapSRA.
1204 static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
1205                DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1206                    std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1207   for (auto UI = Load->user_begin(), E = Load->user_end(); UI != E;) {
1208     Instruction *User = cast<Instruction>(*UI++);
1209     RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1210   }
1211 
1212   if (Load->use_empty()) {
1213     Load->eraseFromParent();
1214     InsertedScalarizedValues.erase(Load);
1215   }
1216 }
1217 
1218 /// CI is an allocation of an array of structures.  Break it up into multiple
1219 /// allocations of arrays of the fields.
1220 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
1221                                             Value *NElems, const DataLayout &DL,
1222                                             const TargetLibraryInfo *TLI) {
1223   DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << "  MALLOC = " << *CI << '\n');
1224   Type *MAT = getMallocAllocatedType(CI, TLI);
1225   StructType *STy = cast<StructType>(MAT);
1226 
1227   // There is guaranteed to be at least one use of the malloc (storing
1228   // it into GV).  If there are other uses, change them to be uses of
1229   // the global to simplify later code.  This also deletes the store
1230   // into GV.
1231   ReplaceUsesOfMallocWithGlobal(CI, GV);
1232 
1233   // Okay, at this point, there are no users of the malloc.  Insert N
1234   // new mallocs at the same place as CI, and N globals.
1235   std::vector<Value*> FieldGlobals;
1236   std::vector<Value*> FieldMallocs;
1237 
1238   SmallVector<OperandBundleDef, 1> OpBundles;
1239   CI->getOperandBundlesAsDefs(OpBundles);
1240 
1241   unsigned AS = GV->getType()->getPointerAddressSpace();
1242   for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
1243     Type *FieldTy = STy->getElementType(FieldNo);
1244     PointerType *PFieldTy = PointerType::get(FieldTy, AS);
1245 
1246     GlobalVariable *NGV = new GlobalVariable(
1247         *GV->getParent(), PFieldTy, false, GlobalValue::InternalLinkage,
1248         Constant::getNullValue(PFieldTy), GV->getName() + ".f" + Twine(FieldNo),
1249         nullptr, GV->getThreadLocalMode());
1250     NGV->copyAttributesFrom(GV);
1251     FieldGlobals.push_back(NGV);
1252 
1253     unsigned TypeSize = DL.getTypeAllocSize(FieldTy);
1254     if (StructType *ST = dyn_cast<StructType>(FieldTy))
1255       TypeSize = DL.getStructLayout(ST)->getSizeInBytes();
1256     Type *IntPtrTy = DL.getIntPtrType(CI->getType());
1257     Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1258                                         ConstantInt::get(IntPtrTy, TypeSize),
1259                                         NElems, OpBundles, nullptr,
1260                                         CI->getName() + ".f" + Twine(FieldNo));
1261     FieldMallocs.push_back(NMI);
1262     new StoreInst(NMI, NGV, CI);
1263   }
1264 
1265   // The tricky aspect of this transformation is handling the case when malloc
1266   // fails.  In the original code, malloc failing would set the result pointer
1267   // of malloc to null.  In this case, some mallocs could succeed and others
1268   // could fail.  As such, we emit code that looks like this:
1269   //    F0 = malloc(field0)
1270   //    F1 = malloc(field1)
1271   //    F2 = malloc(field2)
1272   //    if (F0 == 0 || F1 == 0 || F2 == 0) {
1273   //      if (F0) { free(F0); F0 = 0; }
1274   //      if (F1) { free(F1); F1 = 0; }
1275   //      if (F2) { free(F2); F2 = 0; }
1276   //    }
1277   // The malloc can also fail if its argument is too large.
1278   Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
1279   Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
1280                                   ConstantZero, "isneg");
1281   for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1282     Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1283                              Constant::getNullValue(FieldMallocs[i]->getType()),
1284                                "isnull");
1285     RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
1286   }
1287 
1288   // Split the basic block at the old malloc.
1289   BasicBlock *OrigBB = CI->getParent();
1290   BasicBlock *ContBB =
1291       OrigBB->splitBasicBlock(CI->getIterator(), "malloc_cont");
1292 
1293   // Create the block to check the first condition.  Put all these blocks at the
1294   // end of the function as they are unlikely to be executed.
1295   BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
1296                                                 "malloc_ret_null",
1297                                                 OrigBB->getParent());
1298 
1299   // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
1300   // branch on RunningOr.
1301   OrigBB->getTerminator()->eraseFromParent();
1302   BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
1303 
1304   // Within the NullPtrBlock, we need to emit a comparison and branch for each
1305   // pointer, because some may be null while others are not.
1306   for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1307     Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
1308     Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1309                               Constant::getNullValue(GVVal->getType()));
1310     BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
1311                                                OrigBB->getParent());
1312     BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
1313                                                OrigBB->getParent());
1314     Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
1315                                          Cmp, NullPtrBlock);
1316 
1317     // Fill in FreeBlock.
1318     CallInst::CreateFree(GVVal, OpBundles, BI);
1319     new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
1320                   FreeBlock);
1321     BranchInst::Create(NextBlock, FreeBlock);
1322 
1323     NullPtrBlock = NextBlock;
1324   }
1325 
1326   BranchInst::Create(ContBB, NullPtrBlock);
1327 
1328   // CI is no longer needed, remove it.
1329   CI->eraseFromParent();
1330 
1331   /// As we process loads, if we can't immediately update all uses of the load,
1332   /// keep track of what scalarized loads are inserted for a given load.
1333   DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
1334   InsertedScalarizedValues[GV] = FieldGlobals;
1335 
1336   std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
1337 
1338   // Okay, the malloc site is completely handled.  All of the uses of GV are now
1339   // loads, and all uses of those loads are simple.  Rewrite them to use loads
1340   // of the per-field globals instead.
1341   for (auto UI = GV->user_begin(), E = GV->user_end(); UI != E;) {
1342     Instruction *User = cast<Instruction>(*UI++);
1343 
1344     if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1345       RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
1346       continue;
1347     }
1348 
1349     // Must be a store of null.
1350     StoreInst *SI = cast<StoreInst>(User);
1351     assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
1352            "Unexpected heap-sra user!");
1353 
1354     // Insert a store of null into each global.
1355     for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1356       Type *ValTy = cast<GlobalValue>(FieldGlobals[i])->getValueType();
1357       Constant *Null = Constant::getNullValue(ValTy);
1358       new StoreInst(Null, FieldGlobals[i], SI);
1359     }
1360     // Erase the original store.
1361     SI->eraseFromParent();
1362   }
1363 
1364   // While we have PHIs that are interesting to rewrite, do it.
1365   while (!PHIsToRewrite.empty()) {
1366     PHINode *PN = PHIsToRewrite.back().first;
1367     unsigned FieldNo = PHIsToRewrite.back().second;
1368     PHIsToRewrite.pop_back();
1369     PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
1370     assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
1371 
1372     // Add all the incoming values.  This can materialize more phis.
1373     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1374       Value *InVal = PN->getIncomingValue(i);
1375       InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
1376                                PHIsToRewrite);
1377       FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
1378     }
1379   }
1380 
1381   // Drop all inter-phi links and any loads that made it this far.
1382   for (DenseMap<Value*, std::vector<Value*> >::iterator
1383        I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1384        I != E; ++I) {
1385     if (PHINode *PN = dyn_cast<PHINode>(I->first))
1386       PN->dropAllReferences();
1387     else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1388       LI->dropAllReferences();
1389   }
1390 
1391   // Delete all the phis and loads now that inter-references are dead.
1392   for (DenseMap<Value*, std::vector<Value*> >::iterator
1393        I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1394        I != E; ++I) {
1395     if (PHINode *PN = dyn_cast<PHINode>(I->first))
1396       PN->eraseFromParent();
1397     else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1398       LI->eraseFromParent();
1399   }
1400 
1401   // The old global is now dead, remove it.
1402   GV->eraseFromParent();
1403 
1404   ++NumHeapSRA;
1405   return cast<GlobalVariable>(FieldGlobals[0]);
1406 }
1407 
1408 /// This function is called when we see a pointer global variable with a single
1409 /// value stored it that is a malloc or cast of malloc.
1410 static bool tryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, CallInst *CI,
1411                                                Type *AllocTy,
1412                                                AtomicOrdering Ordering,
1413                                                const DataLayout &DL,
1414                                                TargetLibraryInfo *TLI) {
1415   // If this is a malloc of an abstract type, don't touch it.
1416   if (!AllocTy->isSized())
1417     return false;
1418 
1419   // We can't optimize this global unless all uses of it are *known* to be
1420   // of the malloc value, not of the null initializer value (consider a use
1421   // that compares the global's value against zero to see if the malloc has
1422   // been reached).  To do this, we check to see if all uses of the global
1423   // would trap if the global were null: this proves that they must all
1424   // happen after the malloc.
1425   if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
1426     return false;
1427 
1428   // We can't optimize this if the malloc itself is used in a complex way,
1429   // for example, being stored into multiple globals.  This allows the
1430   // malloc to be stored into the specified global, loaded icmp'd, and
1431   // GEP'd.  These are all things we could transform to using the global
1432   // for.
1433   SmallPtrSet<const PHINode*, 8> PHIs;
1434   if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
1435     return false;
1436 
1437   // If we have a global that is only initialized with a fixed size malloc,
1438   // transform the program to use global memory instead of malloc'd memory.
1439   // This eliminates dynamic allocation, avoids an indirection accessing the
1440   // data, and exposes the resultant global to further GlobalOpt.
1441   // We cannot optimize the malloc if we cannot determine malloc array size.
1442   Value *NElems = getMallocArraySize(CI, DL, TLI, true);
1443   if (!NElems)
1444     return false;
1445 
1446   if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
1447     // Restrict this transformation to only working on small allocations
1448     // (2048 bytes currently), as we don't want to introduce a 16M global or
1449     // something.
1450     if (NElements->getZExtValue() * DL.getTypeAllocSize(AllocTy) < 2048) {
1451       OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, DL, TLI);
1452       return true;
1453     }
1454 
1455   // If the allocation is an array of structures, consider transforming this
1456   // into multiple malloc'd arrays, one for each field.  This is basically
1457   // SRoA for malloc'd memory.
1458 
1459   if (Ordering != AtomicOrdering::NotAtomic)
1460     return false;
1461 
1462   // If this is an allocation of a fixed size array of structs, analyze as a
1463   // variable size array.  malloc [100 x struct],1 -> malloc struct, 100
1464   if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
1465     if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
1466       AllocTy = AT->getElementType();
1467 
1468   StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
1469   if (!AllocSTy)
1470     return false;
1471 
1472   // This the structure has an unreasonable number of fields, leave it
1473   // alone.
1474   if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1475       AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
1476 
1477     // If this is a fixed size array, transform the Malloc to be an alloc of
1478     // structs.  malloc [100 x struct],1 -> malloc struct, 100
1479     if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI, TLI))) {
1480       Type *IntPtrTy = DL.getIntPtrType(CI->getType());
1481       unsigned TypeSize = DL.getStructLayout(AllocSTy)->getSizeInBytes();
1482       Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1483       Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1484       SmallVector<OperandBundleDef, 1> OpBundles;
1485       CI->getOperandBundlesAsDefs(OpBundles);
1486       Instruction *Malloc =
1487           CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy, AllocSize, NumElements,
1488                                  OpBundles, nullptr, CI->getName());
1489       Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1490       CI->replaceAllUsesWith(Cast);
1491       CI->eraseFromParent();
1492       if (BitCastInst *BCI = dyn_cast<BitCastInst>(Malloc))
1493         CI = cast<CallInst>(BCI->getOperand(0));
1494       else
1495         CI = cast<CallInst>(Malloc);
1496     }
1497 
1498     PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, DL, TLI, true), DL,
1499                          TLI);
1500     return true;
1501   }
1502 
1503   return false;
1504 }
1505 
1506 // Try to optimize globals based on the knowledge that only one value (besides
1507 // its initializer) is ever stored to the global.
1508 static bool optimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
1509                                      AtomicOrdering Ordering,
1510                                      const DataLayout &DL,
1511                                      TargetLibraryInfo *TLI) {
1512   // Ignore no-op GEPs and bitcasts.
1513   StoredOnceVal = StoredOnceVal->stripPointerCasts();
1514 
1515   // If we are dealing with a pointer global that is initialized to null and
1516   // only has one (non-null) value stored into it, then we can optimize any
1517   // users of the loaded value (often calls and loads) that would trap if the
1518   // value was null.
1519   if (GV->getInitializer()->getType()->isPointerTy() &&
1520       GV->getInitializer()->isNullValue()) {
1521     if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1522       if (GV->getInitializer()->getType() != SOVC->getType())
1523         SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
1524 
1525       // Optimize away any trapping uses of the loaded value.
1526       if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, DL, TLI))
1527         return true;
1528     } else if (CallInst *CI = extractMallocCall(StoredOnceVal, TLI)) {
1529       Type *MallocType = getMallocAllocatedType(CI, TLI);
1530       if (MallocType && tryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType,
1531                                                            Ordering, DL, TLI))
1532         return true;
1533     }
1534   }
1535 
1536   return false;
1537 }
1538 
1539 /// At this point, we have learned that the only two values ever stored into GV
1540 /// are its initializer and OtherVal.  See if we can shrink the global into a
1541 /// boolean and select between the two values whenever it is used.  This exposes
1542 /// the values to other scalar optimizations.
1543 static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
1544   Type *GVElType = GV->getValueType();
1545 
1546   // If GVElType is already i1, it is already shrunk.  If the type of the GV is
1547   // an FP value, pointer or vector, don't do this optimization because a select
1548   // between them is very expensive and unlikely to lead to later
1549   // simplification.  In these cases, we typically end up with "cond ? v1 : v2"
1550   // where v1 and v2 both require constant pool loads, a big loss.
1551   if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1552       GVElType->isFloatingPointTy() ||
1553       GVElType->isPointerTy() || GVElType->isVectorTy())
1554     return false;
1555 
1556   // Walk the use list of the global seeing if all the uses are load or store.
1557   // If there is anything else, bail out.
1558   for (User *U : GV->users())
1559     if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1560       return false;
1561 
1562   DEBUG(dbgs() << "   *** SHRINKING TO BOOL: " << *GV << "\n");
1563 
1564   // Create the new global, initializing it to false.
1565   GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
1566                                              false,
1567                                              GlobalValue::InternalLinkage,
1568                                         ConstantInt::getFalse(GV->getContext()),
1569                                              GV->getName()+".b",
1570                                              GV->getThreadLocalMode(),
1571                                              GV->getType()->getAddressSpace());
1572   NewGV->copyAttributesFrom(GV);
1573   GV->getParent()->getGlobalList().insert(GV->getIterator(), NewGV);
1574 
1575   Constant *InitVal = GV->getInitializer();
1576   assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1577          "No reason to shrink to bool!");
1578 
1579   // If initialized to zero and storing one into the global, we can use a cast
1580   // instead of a select to synthesize the desired value.
1581   bool IsOneZero = false;
1582   if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
1583     IsOneZero = InitVal->isNullValue() && CI->isOne();
1584 
1585   while (!GV->use_empty()) {
1586     Instruction *UI = cast<Instruction>(GV->user_back());
1587     if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1588       // Change the store into a boolean store.
1589       bool StoringOther = SI->getOperand(0) == OtherVal;
1590       // Only do this if we weren't storing a loaded value.
1591       Value *StoreVal;
1592       if (StoringOther || SI->getOperand(0) == InitVal) {
1593         StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1594                                     StoringOther);
1595       } else {
1596         // Otherwise, we are storing a previously loaded copy.  To do this,
1597         // change the copy from copying the original value to just copying the
1598         // bool.
1599         Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
1600 
1601         // If we've already replaced the input, StoredVal will be a cast or
1602         // select instruction.  If not, it will be a load of the original
1603         // global.
1604         if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
1605           assert(LI->getOperand(0) == GV && "Not a copy!");
1606           // Insert a new load, to preserve the saved value.
1607           StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1608                                   LI->getOrdering(), LI->getSynchScope(), LI);
1609         } else {
1610           assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
1611                  "This is not a form that we understand!");
1612           StoreVal = StoredVal->getOperand(0);
1613           assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
1614         }
1615       }
1616       new StoreInst(StoreVal, NewGV, false, 0,
1617                     SI->getOrdering(), SI->getSynchScope(), SI);
1618     } else {
1619       // Change the load into a load of bool then a select.
1620       LoadInst *LI = cast<LoadInst>(UI);
1621       LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1622                                    LI->getOrdering(), LI->getSynchScope(), LI);
1623       Value *NSI;
1624       if (IsOneZero)
1625         NSI = new ZExtInst(NLI, LI->getType(), "", LI);
1626       else
1627         NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
1628       NSI->takeName(LI);
1629       LI->replaceAllUsesWith(NSI);
1630     }
1631     UI->eraseFromParent();
1632   }
1633 
1634   // Retain the name of the old global variable. People who are debugging their
1635   // programs may expect these variables to be named the same.
1636   NewGV->takeName(GV);
1637   GV->eraseFromParent();
1638   return true;
1639 }
1640 
1641 static bool deleteIfDead(GlobalValue &GV,
1642                          SmallSet<const Comdat *, 8> &NotDiscardableComdats) {
1643   GV.removeDeadConstantUsers();
1644 
1645   if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())
1646     return false;
1647 
1648   if (const Comdat *C = GV.getComdat())
1649     if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
1650       return false;
1651 
1652   bool Dead;
1653   if (auto *F = dyn_cast<Function>(&GV))
1654     Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead();
1655   else
1656     Dead = GV.use_empty();
1657   if (!Dead)
1658     return false;
1659 
1660   DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
1661   GV.eraseFromParent();
1662   ++NumDeleted;
1663   return true;
1664 }
1665 
1666 static bool isPointerValueDeadOnEntryToFunction(
1667     const Function *F, GlobalValue *GV,
1668     function_ref<DominatorTree &(Function &)> LookupDomTree) {
1669   // Find all uses of GV. We expect them all to be in F, and if we can't
1670   // identify any of the uses we bail out.
1671   //
1672   // On each of these uses, identify if the memory that GV points to is
1673   // used/required/live at the start of the function. If it is not, for example
1674   // if the first thing the function does is store to the GV, the GV can
1675   // possibly be demoted.
1676   //
1677   // We don't do an exhaustive search for memory operations - simply look
1678   // through bitcasts as they're quite common and benign.
1679   const DataLayout &DL = GV->getParent()->getDataLayout();
1680   SmallVector<LoadInst *, 4> Loads;
1681   SmallVector<StoreInst *, 4> Stores;
1682   for (auto *U : GV->users()) {
1683     if (Operator::getOpcode(U) == Instruction::BitCast) {
1684       for (auto *UU : U->users()) {
1685         if (auto *LI = dyn_cast<LoadInst>(UU))
1686           Loads.push_back(LI);
1687         else if (auto *SI = dyn_cast<StoreInst>(UU))
1688           Stores.push_back(SI);
1689         else
1690           return false;
1691       }
1692       continue;
1693     }
1694 
1695     Instruction *I = dyn_cast<Instruction>(U);
1696     if (!I)
1697       return false;
1698     assert(I->getParent()->getParent() == F);
1699 
1700     if (auto *LI = dyn_cast<LoadInst>(I))
1701       Loads.push_back(LI);
1702     else if (auto *SI = dyn_cast<StoreInst>(I))
1703       Stores.push_back(SI);
1704     else
1705       return false;
1706   }
1707 
1708   // We have identified all uses of GV into loads and stores. Now check if all
1709   // of them are known not to depend on the value of the global at the function
1710   // entry point. We do this by ensuring that every load is dominated by at
1711   // least one store.
1712   auto &DT = LookupDomTree(*const_cast<Function *>(F));
1713 
1714   // The below check is quadratic. Check we're not going to do too many tests.
1715   // FIXME: Even though this will always have worst-case quadratic time, we
1716   // could put effort into minimizing the average time by putting stores that
1717   // have been shown to dominate at least one load at the beginning of the
1718   // Stores array, making subsequent dominance checks more likely to succeed
1719   // early.
1720   //
1721   // The threshold here is fairly large because global->local demotion is a
1722   // very powerful optimization should it fire.
1723   const unsigned Threshold = 100;
1724   if (Loads.size() * Stores.size() > Threshold)
1725     return false;
1726 
1727   for (auto *L : Loads) {
1728     auto *LTy = L->getType();
1729     if (none_of(Stores, [&](const StoreInst *S) {
1730           auto *STy = S->getValueOperand()->getType();
1731           // The load is only dominated by the store if DomTree says so
1732           // and the number of bits loaded in L is less than or equal to
1733           // the number of bits stored in S.
1734           return DT.dominates(S, L) &&
1735                  DL.getTypeStoreSize(LTy) <= DL.getTypeStoreSize(STy);
1736         }))
1737       return false;
1738   }
1739   // All loads have known dependences inside F, so the global can be localized.
1740   return true;
1741 }
1742 
1743 /// C may have non-instruction users. Can all of those users be turned into
1744 /// instructions?
1745 static bool allNonInstructionUsersCanBeMadeInstructions(Constant *C) {
1746   // We don't do this exhaustively. The most common pattern that we really need
1747   // to care about is a constant GEP or constant bitcast - so just looking
1748   // through one single ConstantExpr.
1749   //
1750   // The set of constants that this function returns true for must be able to be
1751   // handled by makeAllConstantUsesInstructions.
1752   for (auto *U : C->users()) {
1753     if (isa<Instruction>(U))
1754       continue;
1755     if (!isa<ConstantExpr>(U))
1756       // Non instruction, non-constantexpr user; cannot convert this.
1757       return false;
1758     for (auto *UU : U->users())
1759       if (!isa<Instruction>(UU))
1760         // A constantexpr used by another constant. We don't try and recurse any
1761         // further but just bail out at this point.
1762         return false;
1763   }
1764 
1765   return true;
1766 }
1767 
1768 /// C may have non-instruction users, and
1769 /// allNonInstructionUsersCanBeMadeInstructions has returned true. Convert the
1770 /// non-instruction users to instructions.
1771 static void makeAllConstantUsesInstructions(Constant *C) {
1772   SmallVector<ConstantExpr*,4> Users;
1773   for (auto *U : C->users()) {
1774     if (isa<ConstantExpr>(U))
1775       Users.push_back(cast<ConstantExpr>(U));
1776     else
1777       // We should never get here; allNonInstructionUsersCanBeMadeInstructions
1778       // should not have returned true for C.
1779       assert(
1780           isa<Instruction>(U) &&
1781           "Can't transform non-constantexpr non-instruction to instruction!");
1782   }
1783 
1784   SmallVector<Value*,4> UUsers;
1785   for (auto *U : Users) {
1786     UUsers.clear();
1787     for (auto *UU : U->users())
1788       UUsers.push_back(UU);
1789     for (auto *UU : UUsers) {
1790       Instruction *UI = cast<Instruction>(UU);
1791       Instruction *NewU = U->getAsInstruction();
1792       NewU->insertBefore(UI);
1793       UI->replaceUsesOfWith(U, NewU);
1794     }
1795     U->dropAllReferences();
1796   }
1797 }
1798 
1799 /// Analyze the specified global variable and optimize
1800 /// it if possible.  If we make a change, return true.
1801 static bool processInternalGlobal(
1802     GlobalVariable *GV, const GlobalStatus &GS, TargetLibraryInfo *TLI,
1803     function_ref<DominatorTree &(Function &)> LookupDomTree) {
1804   auto &DL = GV->getParent()->getDataLayout();
1805   // If this is a first class global and has only one accessing function and
1806   // this function is non-recursive, we replace the global with a local alloca
1807   // in this function.
1808   //
1809   // NOTE: It doesn't make sense to promote non-single-value types since we
1810   // are just replacing static memory to stack memory.
1811   //
1812   // If the global is in different address space, don't bring it to stack.
1813   if (!GS.HasMultipleAccessingFunctions &&
1814       GS.AccessingFunction &&
1815       GV->getValueType()->isSingleValueType() &&
1816       GV->getType()->getAddressSpace() == 0 &&
1817       !GV->isExternallyInitialized() &&
1818       allNonInstructionUsersCanBeMadeInstructions(GV) &&
1819       GS.AccessingFunction->doesNotRecurse() &&
1820       isPointerValueDeadOnEntryToFunction(GS.AccessingFunction, GV,
1821                                           LookupDomTree)) {
1822     DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV << "\n");
1823     Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
1824                                                    ->getEntryBlock().begin());
1825     Type *ElemTy = GV->getValueType();
1826     // FIXME: Pass Global's alignment when globals have alignment
1827     AllocaInst *Alloca = new AllocaInst(ElemTy, nullptr,
1828                                         GV->getName(), &FirstI);
1829     if (!isa<UndefValue>(GV->getInitializer()))
1830       new StoreInst(GV->getInitializer(), Alloca, &FirstI);
1831 
1832     makeAllConstantUsesInstructions(GV);
1833 
1834     GV->replaceAllUsesWith(Alloca);
1835     GV->eraseFromParent();
1836     ++NumLocalized;
1837     return true;
1838   }
1839 
1840   // If the global is never loaded (but may be stored to), it is dead.
1841   // Delete it now.
1842   if (!GS.IsLoaded) {
1843     DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV << "\n");
1844 
1845     bool Changed;
1846     if (isLeakCheckerRoot(GV)) {
1847       // Delete any constant stores to the global.
1848       Changed = CleanupPointerRootUsers(GV, TLI);
1849     } else {
1850       // Delete any stores we can find to the global.  We may not be able to
1851       // make it completely dead though.
1852       Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1853     }
1854 
1855     // If the global is dead now, delete it.
1856     if (GV->use_empty()) {
1857       GV->eraseFromParent();
1858       ++NumDeleted;
1859       Changed = true;
1860     }
1861     return Changed;
1862 
1863   }
1864   if (GS.StoredType <= GlobalStatus::InitializerStored) {
1865     DEBUG(dbgs() << "MARKING CONSTANT: " << *GV << "\n");
1866     GV->setConstant(true);
1867 
1868     // Clean up any obviously simplifiable users now.
1869     CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1870 
1871     // If the global is dead now, just nuke it.
1872     if (GV->use_empty()) {
1873       DEBUG(dbgs() << "   *** Marking constant allowed us to simplify "
1874             << "all users and delete global!\n");
1875       GV->eraseFromParent();
1876       ++NumDeleted;
1877       return true;
1878     }
1879 
1880     // Fall through to the next check; see if we can optimize further.
1881     ++NumMarked;
1882   }
1883   if (!GV->getInitializer()->getType()->isSingleValueType()) {
1884     const DataLayout &DL = GV->getParent()->getDataLayout();
1885     if (SRAGlobal(GV, DL))
1886       return true;
1887   }
1888   if (GS.StoredType == GlobalStatus::StoredOnce && GS.StoredOnceValue) {
1889     // If the initial value for the global was an undef value, and if only
1890     // one other value was stored into it, we can just change the
1891     // initializer to be the stored value, then delete all stores to the
1892     // global.  This allows us to mark it constant.
1893     if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
1894       if (isa<UndefValue>(GV->getInitializer())) {
1895         // Change the initial value here.
1896         GV->setInitializer(SOVConstant);
1897 
1898         // Clean up any obviously simplifiable users now.
1899         CleanupConstantGlobalUsers(GV, GV->getInitializer(), DL, TLI);
1900 
1901         if (GV->use_empty()) {
1902           DEBUG(dbgs() << "   *** Substituting initializer allowed us to "
1903                        << "simplify all users and delete global!\n");
1904           GV->eraseFromParent();
1905           ++NumDeleted;
1906         }
1907         ++NumSubstitute;
1908         return true;
1909       }
1910 
1911     // Try to optimize globals based on the knowledge that only one value
1912     // (besides its initializer) is ever stored to the global.
1913     if (optimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, DL, TLI))
1914       return true;
1915 
1916     // Otherwise, if the global was not a boolean, we can shrink it to be a
1917     // boolean.
1918     if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) {
1919       if (GS.Ordering == AtomicOrdering::NotAtomic) {
1920         if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1921           ++NumShrunkToBool;
1922           return true;
1923         }
1924       }
1925     }
1926   }
1927 
1928   return false;
1929 }
1930 
1931 /// Analyze the specified global variable and optimize it if possible.  If we
1932 /// make a change, return true.
1933 static bool
1934 processGlobal(GlobalValue &GV, TargetLibraryInfo *TLI,
1935               function_ref<DominatorTree &(Function &)> LookupDomTree) {
1936   if (GV.getName().startswith("llvm."))
1937     return false;
1938 
1939   GlobalStatus GS;
1940 
1941   if (GlobalStatus::analyzeGlobal(&GV, GS))
1942     return false;
1943 
1944   bool Changed = false;
1945   if (!GS.IsCompared && !GV.hasGlobalUnnamedAddr()) {
1946     auto NewUnnamedAddr = GV.hasLocalLinkage() ? GlobalValue::UnnamedAddr::Global
1947                                                : GlobalValue::UnnamedAddr::Local;
1948     if (NewUnnamedAddr != GV.getUnnamedAddr()) {
1949       GV.setUnnamedAddr(NewUnnamedAddr);
1950       NumUnnamed++;
1951       Changed = true;
1952     }
1953   }
1954 
1955   // Do more involved optimizations if the global is internal.
1956   if (!GV.hasLocalLinkage())
1957     return Changed;
1958 
1959   auto *GVar = dyn_cast<GlobalVariable>(&GV);
1960   if (!GVar)
1961     return Changed;
1962 
1963   if (GVar->isConstant() || !GVar->hasInitializer())
1964     return Changed;
1965 
1966   return processInternalGlobal(GVar, GS, TLI, LookupDomTree) || Changed;
1967 }
1968 
1969 /// Walk all of the direct calls of the specified function, changing them to
1970 /// FastCC.
1971 static void ChangeCalleesToFastCall(Function *F) {
1972   for (User *U : F->users()) {
1973     if (isa<BlockAddress>(U))
1974       continue;
1975     CallSite CS(cast<Instruction>(U));
1976     CS.setCallingConv(CallingConv::Fast);
1977   }
1978 }
1979 
1980 static AttributeSet StripNest(LLVMContext &C, const AttributeSet &Attrs) {
1981   for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
1982     unsigned Index = Attrs.getSlotIndex(i);
1983     if (!Attrs.getSlotAttributes(i).hasAttribute(Index, Attribute::Nest))
1984       continue;
1985 
1986     // There can be only one.
1987     return Attrs.removeAttribute(C, Index, Attribute::Nest);
1988   }
1989 
1990   return Attrs;
1991 }
1992 
1993 static void RemoveNestAttribute(Function *F) {
1994   F->setAttributes(StripNest(F->getContext(), F->getAttributes()));
1995   for (User *U : F->users()) {
1996     if (isa<BlockAddress>(U))
1997       continue;
1998     CallSite CS(cast<Instruction>(U));
1999     CS.setAttributes(StripNest(F->getContext(), CS.getAttributes()));
2000   }
2001 }
2002 
2003 /// Return true if this is a calling convention that we'd like to change.  The
2004 /// idea here is that we don't want to mess with the convention if the user
2005 /// explicitly requested something with performance implications like coldcc,
2006 /// GHC, or anyregcc.
2007 static bool isProfitableToMakeFastCC(Function *F) {
2008   CallingConv::ID CC = F->getCallingConv();
2009   // FIXME: Is it worth transforming x86_stdcallcc and x86_fastcallcc?
2010   return CC == CallingConv::C || CC == CallingConv::X86_ThisCall;
2011 }
2012 
2013 static bool
2014 OptimizeFunctions(Module &M, TargetLibraryInfo *TLI,
2015                   function_ref<DominatorTree &(Function &)> LookupDomTree,
2016                   SmallSet<const Comdat *, 8> &NotDiscardableComdats) {
2017   bool Changed = false;
2018   // Optimize functions.
2019   for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
2020     Function *F = &*FI++;
2021     // Functions without names cannot be referenced outside this module.
2022     if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
2023       F->setLinkage(GlobalValue::InternalLinkage);
2024 
2025     if (deleteIfDead(*F, NotDiscardableComdats)) {
2026       Changed = true;
2027       continue;
2028     }
2029 
2030     Changed |= processGlobal(*F, TLI, LookupDomTree);
2031 
2032     if (!F->hasLocalLinkage())
2033       continue;
2034     if (isProfitableToMakeFastCC(F) && !F->isVarArg() &&
2035         !F->hasAddressTaken()) {
2036       // If this function has a calling convention worth changing, is not a
2037       // varargs function, and is only called directly, promote it to use the
2038       // Fast calling convention.
2039       F->setCallingConv(CallingConv::Fast);
2040       ChangeCalleesToFastCall(F);
2041       ++NumFastCallFns;
2042       Changed = true;
2043     }
2044 
2045     if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) &&
2046         !F->hasAddressTaken()) {
2047       // The function is not used by a trampoline intrinsic, so it is safe
2048       // to remove the 'nest' attribute.
2049       RemoveNestAttribute(F);
2050       ++NumNestRemoved;
2051       Changed = true;
2052     }
2053   }
2054   return Changed;
2055 }
2056 
2057 static bool
2058 OptimizeGlobalVars(Module &M, TargetLibraryInfo *TLI,
2059                    function_ref<DominatorTree &(Function &)> LookupDomTree,
2060                    SmallSet<const Comdat *, 8> &NotDiscardableComdats) {
2061   bool Changed = false;
2062 
2063   for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
2064        GVI != E; ) {
2065     GlobalVariable *GV = &*GVI++;
2066     // Global variables without names cannot be referenced outside this module.
2067     if (!GV->hasName() && !GV->isDeclaration() && !GV->hasLocalLinkage())
2068       GV->setLinkage(GlobalValue::InternalLinkage);
2069     // Simplify the initializer.
2070     if (GV->hasInitializer())
2071       if (auto *C = dyn_cast<Constant>(GV->getInitializer())) {
2072         auto &DL = M.getDataLayout();
2073         Constant *New = ConstantFoldConstant(C, DL, TLI);
2074         if (New && New != C)
2075           GV->setInitializer(New);
2076       }
2077 
2078     if (deleteIfDead(*GV, NotDiscardableComdats)) {
2079       Changed = true;
2080       continue;
2081     }
2082 
2083     Changed |= processGlobal(*GV, TLI, LookupDomTree);
2084   }
2085   return Changed;
2086 }
2087 
2088 /// Evaluate a piece of a constantexpr store into a global initializer.  This
2089 /// returns 'Init' modified to reflect 'Val' stored into it.  At this point, the
2090 /// GEP operands of Addr [0, OpNo) have been stepped into.
2091 static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
2092                                    ConstantExpr *Addr, unsigned OpNo) {
2093   // Base case of the recursion.
2094   if (OpNo == Addr->getNumOperands()) {
2095     assert(Val->getType() == Init->getType() && "Type mismatch!");
2096     return Val;
2097   }
2098 
2099   SmallVector<Constant*, 32> Elts;
2100   if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
2101     // Break up the constant into its elements.
2102     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2103       Elts.push_back(Init->getAggregateElement(i));
2104 
2105     // Replace the element that we are supposed to.
2106     ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
2107     unsigned Idx = CU->getZExtValue();
2108     assert(Idx < STy->getNumElements() && "Struct index out of range!");
2109     Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
2110 
2111     // Return the modified struct.
2112     return ConstantStruct::get(STy, Elts);
2113   }
2114 
2115   ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
2116   SequentialType *InitTy = cast<SequentialType>(Init->getType());
2117   uint64_t NumElts = InitTy->getNumElements();
2118 
2119   // Break up the array into elements.
2120   for (uint64_t i = 0, e = NumElts; i != e; ++i)
2121     Elts.push_back(Init->getAggregateElement(i));
2122 
2123   assert(CI->getZExtValue() < NumElts);
2124   Elts[CI->getZExtValue()] =
2125     EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
2126 
2127   if (Init->getType()->isArrayTy())
2128     return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
2129   return ConstantVector::get(Elts);
2130 }
2131 
2132 /// We have decided that Addr (which satisfies the predicate
2133 /// isSimpleEnoughPointerToCommit) should get Val as its value.  Make it happen.
2134 static void CommitValueTo(Constant *Val, Constant *Addr) {
2135   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
2136     assert(GV->hasInitializer());
2137     GV->setInitializer(Val);
2138     return;
2139   }
2140 
2141   ConstantExpr *CE = cast<ConstantExpr>(Addr);
2142   GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2143   GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
2144 }
2145 
2146 /// Evaluate static constructors in the function, if we can.  Return true if we
2147 /// can, false otherwise.
2148 static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,
2149                                       TargetLibraryInfo *TLI) {
2150   // Call the function.
2151   Evaluator Eval(DL, TLI);
2152   Constant *RetValDummy;
2153   bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2154                                            SmallVector<Constant*, 0>());
2155 
2156   if (EvalSuccess) {
2157     ++NumCtorsEvaluated;
2158 
2159     // We succeeded at evaluation: commit the result.
2160     DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2161           << F->getName() << "' to " << Eval.getMutatedMemory().size()
2162           << " stores.\n");
2163     for (const auto &I : Eval.getMutatedMemory())
2164       CommitValueTo(I.second, I.first);
2165     for (GlobalVariable *GV : Eval.getInvariants())
2166       GV->setConstant(true);
2167   }
2168 
2169   return EvalSuccess;
2170 }
2171 
2172 static int compareNames(Constant *const *A, Constant *const *B) {
2173   Value *AStripped = (*A)->stripPointerCastsNoFollowAliases();
2174   Value *BStripped = (*B)->stripPointerCastsNoFollowAliases();
2175   return AStripped->getName().compare(BStripped->getName());
2176 }
2177 
2178 static void setUsedInitializer(GlobalVariable &V,
2179                                const SmallPtrSet<GlobalValue *, 8> &Init) {
2180   if (Init.empty()) {
2181     V.eraseFromParent();
2182     return;
2183   }
2184 
2185   // Type of pointer to the array of pointers.
2186   PointerType *Int8PtrTy = Type::getInt8PtrTy(V.getContext(), 0);
2187 
2188   SmallVector<llvm::Constant *, 8> UsedArray;
2189   for (GlobalValue *GV : Init) {
2190     Constant *Cast
2191       = ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, Int8PtrTy);
2192     UsedArray.push_back(Cast);
2193   }
2194   // Sort to get deterministic order.
2195   array_pod_sort(UsedArray.begin(), UsedArray.end(), compareNames);
2196   ArrayType *ATy = ArrayType::get(Int8PtrTy, UsedArray.size());
2197 
2198   Module *M = V.getParent();
2199   V.removeFromParent();
2200   GlobalVariable *NV =
2201       new GlobalVariable(*M, ATy, false, llvm::GlobalValue::AppendingLinkage,
2202                          llvm::ConstantArray::get(ATy, UsedArray), "");
2203   NV->takeName(&V);
2204   NV->setSection("llvm.metadata");
2205   delete &V;
2206 }
2207 
2208 namespace {
2209 /// An easy to access representation of llvm.used and llvm.compiler.used.
2210 class LLVMUsed {
2211   SmallPtrSet<GlobalValue *, 8> Used;
2212   SmallPtrSet<GlobalValue *, 8> CompilerUsed;
2213   GlobalVariable *UsedV;
2214   GlobalVariable *CompilerUsedV;
2215 
2216 public:
2217   LLVMUsed(Module &M) {
2218     UsedV = collectUsedGlobalVariables(M, Used, false);
2219     CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
2220   }
2221   typedef SmallPtrSet<GlobalValue *, 8>::iterator iterator;
2222   typedef iterator_range<iterator> used_iterator_range;
2223   iterator usedBegin() { return Used.begin(); }
2224   iterator usedEnd() { return Used.end(); }
2225   used_iterator_range used() {
2226     return used_iterator_range(usedBegin(), usedEnd());
2227   }
2228   iterator compilerUsedBegin() { return CompilerUsed.begin(); }
2229   iterator compilerUsedEnd() { return CompilerUsed.end(); }
2230   used_iterator_range compilerUsed() {
2231     return used_iterator_range(compilerUsedBegin(), compilerUsedEnd());
2232   }
2233   bool usedCount(GlobalValue *GV) const { return Used.count(GV); }
2234   bool compilerUsedCount(GlobalValue *GV) const {
2235     return CompilerUsed.count(GV);
2236   }
2237   bool usedErase(GlobalValue *GV) { return Used.erase(GV); }
2238   bool compilerUsedErase(GlobalValue *GV) { return CompilerUsed.erase(GV); }
2239   bool usedInsert(GlobalValue *GV) { return Used.insert(GV).second; }
2240   bool compilerUsedInsert(GlobalValue *GV) {
2241     return CompilerUsed.insert(GV).second;
2242   }
2243 
2244   void syncVariablesAndSets() {
2245     if (UsedV)
2246       setUsedInitializer(*UsedV, Used);
2247     if (CompilerUsedV)
2248       setUsedInitializer(*CompilerUsedV, CompilerUsed);
2249   }
2250 };
2251 }
2252 
2253 static bool hasUseOtherThanLLVMUsed(GlobalAlias &GA, const LLVMUsed &U) {
2254   if (GA.use_empty()) // No use at all.
2255     return false;
2256 
2257   assert((!U.usedCount(&GA) || !U.compilerUsedCount(&GA)) &&
2258          "We should have removed the duplicated "
2259          "element from llvm.compiler.used");
2260   if (!GA.hasOneUse())
2261     // Strictly more than one use. So at least one is not in llvm.used and
2262     // llvm.compiler.used.
2263     return true;
2264 
2265   // Exactly one use. Check if it is in llvm.used or llvm.compiler.used.
2266   return !U.usedCount(&GA) && !U.compilerUsedCount(&GA);
2267 }
2268 
2269 static bool hasMoreThanOneUseOtherThanLLVMUsed(GlobalValue &V,
2270                                                const LLVMUsed &U) {
2271   unsigned N = 2;
2272   assert((!U.usedCount(&V) || !U.compilerUsedCount(&V)) &&
2273          "We should have removed the duplicated "
2274          "element from llvm.compiler.used");
2275   if (U.usedCount(&V) || U.compilerUsedCount(&V))
2276     ++N;
2277   return V.hasNUsesOrMore(N);
2278 }
2279 
2280 static bool mayHaveOtherReferences(GlobalAlias &GA, const LLVMUsed &U) {
2281   if (!GA.hasLocalLinkage())
2282     return true;
2283 
2284   return U.usedCount(&GA) || U.compilerUsedCount(&GA);
2285 }
2286 
2287 static bool hasUsesToReplace(GlobalAlias &GA, const LLVMUsed &U,
2288                              bool &RenameTarget) {
2289   RenameTarget = false;
2290   bool Ret = false;
2291   if (hasUseOtherThanLLVMUsed(GA, U))
2292     Ret = true;
2293 
2294   // If the alias is externally visible, we may still be able to simplify it.
2295   if (!mayHaveOtherReferences(GA, U))
2296     return Ret;
2297 
2298   // If the aliasee has internal linkage, give it the name and linkage
2299   // of the alias, and delete the alias.  This turns:
2300   //   define internal ... @f(...)
2301   //   @a = alias ... @f
2302   // into:
2303   //   define ... @a(...)
2304   Constant *Aliasee = GA.getAliasee();
2305   GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2306   if (!Target->hasLocalLinkage())
2307     return Ret;
2308 
2309   // Do not perform the transform if multiple aliases potentially target the
2310   // aliasee. This check also ensures that it is safe to replace the section
2311   // and other attributes of the aliasee with those of the alias.
2312   if (hasMoreThanOneUseOtherThanLLVMUsed(*Target, U))
2313     return Ret;
2314 
2315   RenameTarget = true;
2316   return true;
2317 }
2318 
2319 static bool
2320 OptimizeGlobalAliases(Module &M,
2321                       SmallSet<const Comdat *, 8> &NotDiscardableComdats) {
2322   bool Changed = false;
2323   LLVMUsed Used(M);
2324 
2325   for (GlobalValue *GV : Used.used())
2326     Used.compilerUsedErase(GV);
2327 
2328   for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
2329        I != E;) {
2330     GlobalAlias *J = &*I++;
2331 
2332     // Aliases without names cannot be referenced outside this module.
2333     if (!J->hasName() && !J->isDeclaration() && !J->hasLocalLinkage())
2334       J->setLinkage(GlobalValue::InternalLinkage);
2335 
2336     if (deleteIfDead(*J, NotDiscardableComdats)) {
2337       Changed = true;
2338       continue;
2339     }
2340 
2341     // If the aliasee may change at link time, nothing can be done - bail out.
2342     if (J->isInterposable())
2343       continue;
2344 
2345     Constant *Aliasee = J->getAliasee();
2346     GlobalValue *Target = dyn_cast<GlobalValue>(Aliasee->stripPointerCasts());
2347     // We can't trivially replace the alias with the aliasee if the aliasee is
2348     // non-trivial in some way.
2349     // TODO: Try to handle non-zero GEPs of local aliasees.
2350     if (!Target)
2351       continue;
2352     Target->removeDeadConstantUsers();
2353 
2354     // Make all users of the alias use the aliasee instead.
2355     bool RenameTarget;
2356     if (!hasUsesToReplace(*J, Used, RenameTarget))
2357       continue;
2358 
2359     J->replaceAllUsesWith(ConstantExpr::getBitCast(Aliasee, J->getType()));
2360     ++NumAliasesResolved;
2361     Changed = true;
2362 
2363     if (RenameTarget) {
2364       // Give the aliasee the name, linkage and other attributes of the alias.
2365       Target->takeName(&*J);
2366       Target->setLinkage(J->getLinkage());
2367       Target->setVisibility(J->getVisibility());
2368       Target->setDLLStorageClass(J->getDLLStorageClass());
2369 
2370       if (Used.usedErase(&*J))
2371         Used.usedInsert(Target);
2372 
2373       if (Used.compilerUsedErase(&*J))
2374         Used.compilerUsedInsert(Target);
2375     } else if (mayHaveOtherReferences(*J, Used))
2376       continue;
2377 
2378     // Delete the alias.
2379     M.getAliasList().erase(J);
2380     ++NumAliasesRemoved;
2381     Changed = true;
2382   }
2383 
2384   Used.syncVariablesAndSets();
2385 
2386   return Changed;
2387 }
2388 
2389 static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
2390   LibFunc::Func F = LibFunc::cxa_atexit;
2391   if (!TLI->has(F))
2392     return nullptr;
2393 
2394   Function *Fn = M.getFunction(TLI->getName(F));
2395   if (!Fn)
2396     return nullptr;
2397 
2398   // Make sure that the function has the correct prototype.
2399   if (!TLI->getLibFunc(*Fn, F) || F != LibFunc::cxa_atexit)
2400     return nullptr;
2401 
2402   return Fn;
2403 }
2404 
2405 /// Returns whether the given function is an empty C++ destructor and can
2406 /// therefore be eliminated.
2407 /// Note that we assume that other optimization passes have already simplified
2408 /// the code so we only look for a function with a single basic block, where
2409 /// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
2410 /// other side-effect free instructions.
2411 static bool cxxDtorIsEmpty(const Function &Fn,
2412                            SmallPtrSet<const Function *, 8> &CalledFunctions) {
2413   // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
2414   // nounwind, but that doesn't seem worth doing.
2415   if (Fn.isDeclaration())
2416     return false;
2417 
2418   if (++Fn.begin() != Fn.end())
2419     return false;
2420 
2421   const BasicBlock &EntryBlock = Fn.getEntryBlock();
2422   for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
2423        I != E; ++I) {
2424     if (const CallInst *CI = dyn_cast<CallInst>(I)) {
2425       // Ignore debug intrinsics.
2426       if (isa<DbgInfoIntrinsic>(CI))
2427         continue;
2428 
2429       const Function *CalledFn = CI->getCalledFunction();
2430 
2431       if (!CalledFn)
2432         return false;
2433 
2434       SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
2435 
2436       // Don't treat recursive functions as empty.
2437       if (!NewCalledFunctions.insert(CalledFn).second)
2438         return false;
2439 
2440       if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
2441         return false;
2442     } else if (isa<ReturnInst>(*I))
2443       return true; // We're done.
2444     else if (I->mayHaveSideEffects())
2445       return false; // Destructor with side effects, bail.
2446   }
2447 
2448   return false;
2449 }
2450 
2451 static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
2452   /// Itanium C++ ABI p3.3.5:
2453   ///
2454   ///   After constructing a global (or local static) object, that will require
2455   ///   destruction on exit, a termination function is registered as follows:
2456   ///
2457   ///   extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
2458   ///
2459   ///   This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
2460   ///   call f(p) when DSO d is unloaded, before all such termination calls
2461   ///   registered before this one. It returns zero if registration is
2462   ///   successful, nonzero on failure.
2463 
2464   // This pass will look for calls to __cxa_atexit where the function is trivial
2465   // and remove them.
2466   bool Changed = false;
2467 
2468   for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
2469        I != E;) {
2470     // We're only interested in calls. Theoretically, we could handle invoke
2471     // instructions as well, but neither llvm-gcc nor clang generate invokes
2472     // to __cxa_atexit.
2473     CallInst *CI = dyn_cast<CallInst>(*I++);
2474     if (!CI)
2475       continue;
2476 
2477     Function *DtorFn =
2478       dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
2479     if (!DtorFn)
2480       continue;
2481 
2482     SmallPtrSet<const Function *, 8> CalledFunctions;
2483     if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
2484       continue;
2485 
2486     // Just remove the call.
2487     CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
2488     CI->eraseFromParent();
2489 
2490     ++NumCXXDtorsRemoved;
2491 
2492     Changed |= true;
2493   }
2494 
2495   return Changed;
2496 }
2497 
2498 static bool optimizeGlobalsInModule(
2499     Module &M, const DataLayout &DL, TargetLibraryInfo *TLI,
2500     function_ref<DominatorTree &(Function &)> LookupDomTree) {
2501   SmallSet<const Comdat *, 8> NotDiscardableComdats;
2502   bool Changed = false;
2503   bool LocalChange = true;
2504   while (LocalChange) {
2505     LocalChange = false;
2506 
2507     NotDiscardableComdats.clear();
2508     for (const GlobalVariable &GV : M.globals())
2509       if (const Comdat *C = GV.getComdat())
2510         if (!GV.isDiscardableIfUnused() || !GV.use_empty())
2511           NotDiscardableComdats.insert(C);
2512     for (Function &F : M)
2513       if (const Comdat *C = F.getComdat())
2514         if (!F.isDefTriviallyDead())
2515           NotDiscardableComdats.insert(C);
2516     for (GlobalAlias &GA : M.aliases())
2517       if (const Comdat *C = GA.getComdat())
2518         if (!GA.isDiscardableIfUnused() || !GA.use_empty())
2519           NotDiscardableComdats.insert(C);
2520 
2521     // Delete functions that are trivially dead, ccc -> fastcc
2522     LocalChange |=
2523         OptimizeFunctions(M, TLI, LookupDomTree, NotDiscardableComdats);
2524 
2525     // Optimize global_ctors list.
2526     LocalChange |= optimizeGlobalCtorsList(M, [&](Function *F) {
2527       return EvaluateStaticConstructor(F, DL, TLI);
2528     });
2529 
2530     // Optimize non-address-taken globals.
2531     LocalChange |= OptimizeGlobalVars(M, TLI, LookupDomTree,
2532                                       NotDiscardableComdats);
2533 
2534     // Resolve aliases, when possible.
2535     LocalChange |= OptimizeGlobalAliases(M, NotDiscardableComdats);
2536 
2537     // Try to remove trivial global destructors if they are not removed
2538     // already.
2539     Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
2540     if (CXAAtExitFn)
2541       LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
2542 
2543     Changed |= LocalChange;
2544   }
2545 
2546   // TODO: Move all global ctors functions to the end of the module for code
2547   // layout.
2548 
2549   return Changed;
2550 }
2551 
2552 PreservedAnalyses GlobalOptPass::run(Module &M, ModuleAnalysisManager &AM) {
2553     auto &DL = M.getDataLayout();
2554     auto &TLI = AM.getResult<TargetLibraryAnalysis>(M);
2555     auto &FAM =
2556         AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
2557     auto LookupDomTree = [&FAM](Function &F) -> DominatorTree &{
2558       return FAM.getResult<DominatorTreeAnalysis>(F);
2559     };
2560     if (!optimizeGlobalsInModule(M, DL, &TLI, LookupDomTree))
2561       return PreservedAnalyses::all();
2562     return PreservedAnalyses::none();
2563 }
2564 
2565 namespace {
2566 struct GlobalOptLegacyPass : public ModulePass {
2567   static char ID; // Pass identification, replacement for typeid
2568   GlobalOptLegacyPass() : ModulePass(ID) {
2569     initializeGlobalOptLegacyPassPass(*PassRegistry::getPassRegistry());
2570   }
2571 
2572   bool runOnModule(Module &M) override {
2573     if (skipModule(M))
2574       return false;
2575 
2576     auto &DL = M.getDataLayout();
2577     auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
2578     auto LookupDomTree = [this](Function &F) -> DominatorTree & {
2579       return this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
2580     };
2581     return optimizeGlobalsInModule(M, DL, TLI, LookupDomTree);
2582   }
2583 
2584   void getAnalysisUsage(AnalysisUsage &AU) const override {
2585     AU.addRequired<TargetLibraryInfoWrapperPass>();
2586     AU.addRequired<DominatorTreeWrapperPass>();
2587   }
2588 };
2589 }
2590 
2591 char GlobalOptLegacyPass::ID = 0;
2592 INITIALIZE_PASS_BEGIN(GlobalOptLegacyPass, "globalopt",
2593                       "Global Variable Optimizer", false, false)
2594 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
2595 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2596 INITIALIZE_PASS_END(GlobalOptLegacyPass, "globalopt",
2597                     "Global Variable Optimizer", false, false)
2598 
2599 ModulePass *llvm::createGlobalOptimizerPass() {
2600   return new GlobalOptLegacyPass();
2601 }
2602