GlobalOpt.cpp revision 234353
1251881Speter//===- GlobalOpt.cpp - Optimize Global Variables --------------------------===//
2251881Speter//
3251881Speter//                     The LLVM Compiler Infrastructure
4251881Speter//
5251881Speter// This file is distributed under the University of Illinois Open Source
6251881Speter// License. See LICENSE.TXT for details.
7251881Speter//
8251881Speter//===----------------------------------------------------------------------===//
9251881Speter//
10251881Speter// This pass transforms simple global variables that never have their address
11251881Speter// taken.  If obviously true, it marks read/write globals as constant, deletes
12251881Speter// variables only stored to, etc.
13251881Speter//
14251881Speter//===----------------------------------------------------------------------===//
15251881Speter
16251881Speter#define DEBUG_TYPE "globalopt"
17251881Speter#include "llvm/Transforms/IPO.h"
18251881Speter#include "llvm/CallingConv.h"
19251881Speter#include "llvm/Constants.h"
20251881Speter#include "llvm/DerivedTypes.h"
21251881Speter#include "llvm/Instructions.h"
22251881Speter#include "llvm/IntrinsicInst.h"
23251881Speter#include "llvm/Module.h"
24251881Speter#include "llvm/Operator.h"
25251881Speter#include "llvm/Pass.h"
26251881Speter#include "llvm/Analysis/ConstantFolding.h"
27251881Speter#include "llvm/Analysis/MemoryBuiltins.h"
28251881Speter#include "llvm/Target/TargetData.h"
29251881Speter#include "llvm/Target/TargetLibraryInfo.h"
30251881Speter#include "llvm/Support/CallSite.h"
31251881Speter#include "llvm/Support/Debug.h"
32251881Speter#include "llvm/Support/ErrorHandling.h"
33251881Speter#include "llvm/Support/GetElementPtrTypeIterator.h"
34251881Speter#include "llvm/Support/MathExtras.h"
35251881Speter#include "llvm/Support/raw_ostream.h"
36251881Speter#include "llvm/ADT/DenseMap.h"
37251881Speter#include "llvm/ADT/SmallPtrSet.h"
38251881Speter#include "llvm/ADT/SmallVector.h"
39251881Speter#include "llvm/ADT/Statistic.h"
40251881Speter#include "llvm/ADT/STLExtras.h"
41251881Speter#include <algorithm>
42251881Speterusing namespace llvm;
43251881Speter
44251881SpeterSTATISTIC(NumMarked    , "Number of globals marked constant");
45251881SpeterSTATISTIC(NumUnnamed   , "Number of globals marked unnamed_addr");
46251881SpeterSTATISTIC(NumSRA       , "Number of aggregate globals broken into scalars");
47251881SpeterSTATISTIC(NumHeapSRA   , "Number of heap objects SRA'd");
48251881SpeterSTATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
49251881SpeterSTATISTIC(NumDeleted   , "Number of globals deleted");
50251881SpeterSTATISTIC(NumFnDeleted , "Number of functions deleted");
51251881SpeterSTATISTIC(NumGlobUses  , "Number of global uses devirtualized");
52251881SpeterSTATISTIC(NumLocalized , "Number of globals localized");
53251881SpeterSTATISTIC(NumShrunkToBool  , "Number of global vars shrunk to booleans");
54251881SpeterSTATISTIC(NumFastCallFns   , "Number of functions converted to fastcc");
55251881SpeterSTATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated");
56251881SpeterSTATISTIC(NumNestRemoved   , "Number of nest attributes removed");
57251881SpeterSTATISTIC(NumAliasesResolved, "Number of global aliases resolved");
58251881SpeterSTATISTIC(NumAliasesRemoved, "Number of global aliases eliminated");
59251881SpeterSTATISTIC(NumCXXDtorsRemoved, "Number of global C++ destructors removed");
60251881Speter
61251881Speternamespace {
62251881Speter  struct GlobalStatus;
63251881Speter  struct GlobalOpt : public ModulePass {
64251881Speter    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65251881Speter      AU.addRequired<TargetLibraryInfo>();
66251881Speter    }
67251881Speter    static char ID; // Pass identification, replacement for typeid
68251881Speter    GlobalOpt() : ModulePass(ID) {
69251881Speter      initializeGlobalOptPass(*PassRegistry::getPassRegistry());
70251881Speter    }
71251881Speter
72251881Speter    bool runOnModule(Module &M);
73251881Speter
74251881Speter  private:
75251881Speter    GlobalVariable *FindGlobalCtors(Module &M);
76251881Speter    bool OptimizeFunctions(Module &M);
77251881Speter    bool OptimizeGlobalVars(Module &M);
78251881Speter    bool OptimizeGlobalAliases(Module &M);
79251881Speter    bool OptimizeGlobalCtorsList(GlobalVariable *&GCL);
80251881Speter    bool ProcessGlobal(GlobalVariable *GV,Module::global_iterator &GVI);
81251881Speter    bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI,
82251881Speter                               const SmallPtrSet<const PHINode*, 16> &PHIUsers,
83251881Speter                               const GlobalStatus &GS);
84251881Speter    bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
85251881Speter
86251881Speter    TargetData *TD;
87251881Speter    TargetLibraryInfo *TLI;
88251881Speter  };
89251881Speter}
90251881Speter
91251881Speterchar GlobalOpt::ID = 0;
92251881SpeterINITIALIZE_PASS_BEGIN(GlobalOpt, "globalopt",
93251881Speter                "Global Variable Optimizer", false, false)
94251881SpeterINITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
95251881SpeterINITIALIZE_PASS_END(GlobalOpt, "globalopt",
96251881Speter                "Global Variable Optimizer", false, false)
97251881Speter
98251881SpeterModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); }
99251881Speter
100251881Speternamespace {
101251881Speter
102251881Speter/// GlobalStatus - As we analyze each global, keep track of some information
103251881Speter/// about it.  If we find out that the address of the global is taken, none of
104251881Speter/// this info will be accurate.
105251881Speterstruct GlobalStatus {
106251881Speter  /// isCompared - True if the global's address is used in a comparison.
107251881Speter  bool isCompared;
108251881Speter
109251881Speter  /// isLoaded - True if the global is ever loaded.  If the global isn't ever
110251881Speter  /// loaded it can be deleted.
111251881Speter  bool isLoaded;
112251881Speter
113251881Speter  /// StoredType - Keep track of what stores to the global look like.
114251881Speter  ///
115251881Speter  enum StoredType {
116251881Speter    /// NotStored - There is no store to this global.  It can thus be marked
117251881Speter    /// constant.
118251881Speter    NotStored,
119251881Speter
120251881Speter    /// isInitializerStored - This global is stored to, but the only thing
121251881Speter    /// stored is the constant it was initialized with.  This is only tracked
122251881Speter    /// for scalar globals.
123251881Speter    isInitializerStored,
124251881Speter
125251881Speter    /// isStoredOnce - This global is stored to, but only its initializer and
126251881Speter    /// one other value is ever stored to it.  If this global isStoredOnce, we
127251881Speter    /// track the value stored to it in StoredOnceValue below.  This is only
128251881Speter    /// tracked for scalar globals.
129251881Speter    isStoredOnce,
130251881Speter
131251881Speter    /// isStored - This global is stored to by multiple values or something else
132251881Speter    /// that we cannot track.
133251881Speter    isStored
134251881Speter  } StoredType;
135251881Speter
136251881Speter  /// StoredOnceValue - If only one value (besides the initializer constant) is
137251881Speter  /// ever stored to this global, keep track of what value it is.
138251881Speter  Value *StoredOnceValue;
139251881Speter
140251881Speter  /// AccessingFunction/HasMultipleAccessingFunctions - These start out
141251881Speter  /// null/false.  When the first accessing function is noticed, it is recorded.
142251881Speter  /// When a second different accessing function is noticed,
143251881Speter  /// HasMultipleAccessingFunctions is set to true.
144251881Speter  const Function *AccessingFunction;
145251881Speter  bool HasMultipleAccessingFunctions;
146251881Speter
147251881Speter  /// HasNonInstructionUser - Set to true if this global has a user that is not
148251881Speter  /// an instruction (e.g. a constant expr or GV initializer).
149251881Speter  bool HasNonInstructionUser;
150251881Speter
151251881Speter  /// HasPHIUser - Set to true if this global has a user that is a PHI node.
152251881Speter  bool HasPHIUser;
153251881Speter
154251881Speter  /// AtomicOrdering - Set to the strongest atomic ordering requirement.
155251881Speter  AtomicOrdering Ordering;
156251881Speter
157251881Speter  GlobalStatus() : isCompared(false), isLoaded(false), StoredType(NotStored),
158251881Speter                   StoredOnceValue(0), AccessingFunction(0),
159251881Speter                   HasMultipleAccessingFunctions(false),
160251881Speter                   HasNonInstructionUser(false), HasPHIUser(false),
161251881Speter                   Ordering(NotAtomic) {}
162251881Speter};
163251881Speter
164251881Speter}
165251881Speter
166251881Speter/// StrongerOrdering - Return the stronger of the two ordering. If the two
167251881Speter/// orderings are acquire and release, then return AcquireRelease.
168251881Speter///
169251881Speterstatic AtomicOrdering StrongerOrdering(AtomicOrdering X, AtomicOrdering Y) {
170251881Speter  if (X == Acquire && Y == Release) return AcquireRelease;
171251881Speter  if (Y == Acquire && X == Release) return AcquireRelease;
172251881Speter  return (AtomicOrdering)std::max(X, Y);
173251881Speter}
174251881Speter
175251881Speter/// SafeToDestroyConstant - It is safe to destroy a constant iff it is only used
176251881Speter/// by constants itself.  Note that constants cannot be cyclic, so this test is
177251881Speter/// pretty easy to implement recursively.
178251881Speter///
179251881Speterstatic bool SafeToDestroyConstant(const Constant *C) {
180251881Speter  if (isa<GlobalValue>(C)) return false;
181251881Speter
182251881Speter  for (Value::const_use_iterator UI = C->use_begin(), E = C->use_end(); UI != E;
183251881Speter       ++UI)
184251881Speter    if (const Constant *CU = dyn_cast<Constant>(*UI)) {
185251881Speter      if (!SafeToDestroyConstant(CU)) return false;
186251881Speter    } else
187251881Speter      return false;
188251881Speter  return true;
189251881Speter}
190251881Speter
191251881Speter
192251881Speter/// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus
193251881Speter/// structure.  If the global has its address taken, return true to indicate we
194251881Speter/// can't do anything with it.
195251881Speter///
196251881Speterstatic bool AnalyzeGlobal(const Value *V, GlobalStatus &GS,
197251881Speter                          SmallPtrSet<const PHINode*, 16> &PHIUsers) {
198251881Speter  for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
199251881Speter       ++UI) {
200251881Speter    const User *U = *UI;
201251881Speter    if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
202251881Speter      GS.HasNonInstructionUser = true;
203251881Speter
204251881Speter      // If the result of the constantexpr isn't pointer type, then we won't
205251881Speter      // know to expect it in various places.  Just reject early.
206251881Speter      if (!isa<PointerType>(CE->getType())) return true;
207251881Speter
208251881Speter      if (AnalyzeGlobal(CE, GS, PHIUsers)) return true;
209251881Speter    } else if (const Instruction *I = dyn_cast<Instruction>(U)) {
210251881Speter      if (!GS.HasMultipleAccessingFunctions) {
211251881Speter        const Function *F = I->getParent()->getParent();
212251881Speter        if (GS.AccessingFunction == 0)
213251881Speter          GS.AccessingFunction = F;
214251881Speter        else if (GS.AccessingFunction != F)
215251881Speter          GS.HasMultipleAccessingFunctions = true;
216251881Speter      }
217251881Speter      if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
218251881Speter        GS.isLoaded = true;
219251881Speter        // Don't hack on volatile loads.
220251881Speter        if (LI->isVolatile()) return true;
221251881Speter        GS.Ordering = StrongerOrdering(GS.Ordering, LI->getOrdering());
222251881Speter      } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) {
223251881Speter        // Don't allow a store OF the address, only stores TO the address.
224251881Speter        if (SI->getOperand(0) == V) return true;
225251881Speter
226251881Speter        // Don't hack on volatile stores.
227251881Speter        if (SI->isVolatile()) return true;
228251881Speter        GS.Ordering = StrongerOrdering(GS.Ordering, SI->getOrdering());
229251881Speter
230251881Speter        // If this is a direct store to the global (i.e., the global is a scalar
231251881Speter        // value, not an aggregate), keep more specific information about
232251881Speter        // stores.
233251881Speter        if (GS.StoredType != GlobalStatus::isStored) {
234251881Speter          if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(
235251881Speter                                                           SI->getOperand(1))) {
236251881Speter            Value *StoredVal = SI->getOperand(0);
237251881Speter            if (StoredVal == GV->getInitializer()) {
238251881Speter              if (GS.StoredType < GlobalStatus::isInitializerStored)
239251881Speter                GS.StoredType = GlobalStatus::isInitializerStored;
240251881Speter            } else if (isa<LoadInst>(StoredVal) &&
241251881Speter                       cast<LoadInst>(StoredVal)->getOperand(0) == GV) {
242251881Speter              if (GS.StoredType < GlobalStatus::isInitializerStored)
243251881Speter                GS.StoredType = GlobalStatus::isInitializerStored;
244251881Speter            } else if (GS.StoredType < GlobalStatus::isStoredOnce) {
245251881Speter              GS.StoredType = GlobalStatus::isStoredOnce;
246251881Speter              GS.StoredOnceValue = StoredVal;
247251881Speter            } else if (GS.StoredType == GlobalStatus::isStoredOnce &&
248251881Speter                       GS.StoredOnceValue == StoredVal) {
249251881Speter              // noop.
250251881Speter            } else {
251251881Speter              GS.StoredType = GlobalStatus::isStored;
252251881Speter            }
253251881Speter          } else {
254251881Speter            GS.StoredType = GlobalStatus::isStored;
255251881Speter          }
256251881Speter        }
257251881Speter      } else if (isa<GetElementPtrInst>(I)) {
258251881Speter        if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
259251881Speter      } else if (isa<SelectInst>(I)) {
260251881Speter        if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
261251881Speter      } else if (const PHINode *PN = dyn_cast<PHINode>(I)) {
262251881Speter        // PHI nodes we can check just like select or GEP instructions, but we
263251881Speter        // have to be careful about infinite recursion.
264251881Speter        if (PHIUsers.insert(PN))  // Not already visited.
265251881Speter          if (AnalyzeGlobal(I, GS, PHIUsers)) return true;
266251881Speter        GS.HasPHIUser = true;
267251881Speter      } else if (isa<CmpInst>(I)) {
268251881Speter        GS.isCompared = true;
269251881Speter      } else if (const MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
270251881Speter        if (MTI->isVolatile()) return true;
271251881Speter        if (MTI->getArgOperand(0) == V)
272251881Speter          GS.StoredType = GlobalStatus::isStored;
273251881Speter        if (MTI->getArgOperand(1) == V)
274251881Speter          GS.isLoaded = true;
275251881Speter      } else if (const MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
276251881Speter        assert(MSI->getArgOperand(0) == V && "Memset only takes one pointer!");
277251881Speter        if (MSI->isVolatile()) return true;
278251881Speter        GS.StoredType = GlobalStatus::isStored;
279251881Speter      } else {
280251881Speter        return true;  // Any other non-load instruction might take address!
281251881Speter      }
282251881Speter    } else if (const Constant *C = dyn_cast<Constant>(U)) {
283251881Speter      GS.HasNonInstructionUser = true;
284251881Speter      // We might have a dead and dangling constant hanging off of here.
285251881Speter      if (!SafeToDestroyConstant(C))
286251881Speter        return true;
287251881Speter    } else {
288251881Speter      GS.HasNonInstructionUser = true;
289251881Speter      // Otherwise must be some other user.
290251881Speter      return true;
291251881Speter    }
292251881Speter  }
293251881Speter
294251881Speter  return false;
295251881Speter}
296251881Speter
297251881Speter/// CleanupConstantGlobalUsers - We just marked GV constant.  Loop over all
298251881Speter/// users of the global, cleaning up the obvious ones.  This is largely just a
299251881Speter/// quick scan over the use list to clean up the easy and obvious cruft.  This
300251881Speter/// returns true if it made a change.
301251881Speterstatic bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
302251881Speter                                       TargetData *TD, TargetLibraryInfo *TLI) {
303251881Speter  bool Changed = false;
304251881Speter  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) {
305251881Speter    User *U = *UI++;
306251881Speter
307251881Speter    if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
308251881Speter      if (Init) {
309251881Speter        // Replace the load with the initializer.
310251881Speter        LI->replaceAllUsesWith(Init);
311251881Speter        LI->eraseFromParent();
312251881Speter        Changed = true;
313251881Speter      }
314251881Speter    } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
315251881Speter      // Store must be unreachable or storing Init into the global.
316251881Speter      SI->eraseFromParent();
317251881Speter      Changed = true;
318251881Speter    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) {
319251881Speter      if (CE->getOpcode() == Instruction::GetElementPtr) {
320251881Speter        Constant *SubInit = 0;
321251881Speter        if (Init)
322251881Speter          SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
323251881Speter        Changed |= CleanupConstantGlobalUsers(CE, SubInit, TD, TLI);
324251881Speter      } else if (CE->getOpcode() == Instruction::BitCast &&
325251881Speter                 CE->getType()->isPointerTy()) {
326251881Speter        // Pointer cast, delete any stores and memsets to the global.
327251881Speter        Changed |= CleanupConstantGlobalUsers(CE, 0, TD, TLI);
328251881Speter      }
329251881Speter
330251881Speter      if (CE->use_empty()) {
331251881Speter        CE->destroyConstant();
332251881Speter        Changed = true;
333251881Speter      }
334251881Speter    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
335251881Speter      // Do not transform "gepinst (gep constexpr (GV))" here, because forming
336251881Speter      // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold
337251881Speter      // and will invalidate our notion of what Init is.
338251881Speter      Constant *SubInit = 0;
339251881Speter      if (!isa<ConstantExpr>(GEP->getOperand(0))) {
340251881Speter        ConstantExpr *CE =
341251881Speter          dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP, TD, TLI));
342251881Speter        if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr)
343251881Speter          SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE);
344251881Speter
345251881Speter        // If the initializer is an all-null value and we have an inbounds GEP,
346251881Speter        // we already know what the result of any load from that GEP is.
347251881Speter        // TODO: Handle splats.
348251881Speter        if (Init && isa<ConstantAggregateZero>(Init) && GEP->isInBounds())
349251881Speter          SubInit = Constant::getNullValue(GEP->getType()->getElementType());
350251881Speter      }
351251881Speter      Changed |= CleanupConstantGlobalUsers(GEP, SubInit, TD, TLI);
352251881Speter
353251881Speter      if (GEP->use_empty()) {
354251881Speter        GEP->eraseFromParent();
355251881Speter        Changed = true;
356251881Speter      }
357251881Speter    } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv
358251881Speter      if (MI->getRawDest() == V) {
359251881Speter        MI->eraseFromParent();
360251881Speter        Changed = true;
361251881Speter      }
362251881Speter
363251881Speter    } else if (Constant *C = dyn_cast<Constant>(U)) {
364251881Speter      // If we have a chain of dead constantexprs or other things dangling from
365251881Speter      // us, and if they are all dead, nuke them without remorse.
366251881Speter      if (SafeToDestroyConstant(C)) {
367251881Speter        C->destroyConstant();
368251881Speter        // This could have invalidated UI, start over from scratch.
369251881Speter        CleanupConstantGlobalUsers(V, Init, TD, TLI);
370251881Speter        return true;
371251881Speter      }
372251881Speter    }
373251881Speter  }
374251881Speter  return Changed;
375251881Speter}
376251881Speter
377251881Speter/// isSafeSROAElementUse - Return true if the specified instruction is a safe
378251881Speter/// user of a derived expression from a global that we want to SROA.
379251881Speterstatic bool isSafeSROAElementUse(Value *V) {
380251881Speter  // We might have a dead and dangling constant hanging off of here.
381251881Speter  if (Constant *C = dyn_cast<Constant>(V))
382251881Speter    return SafeToDestroyConstant(C);
383251881Speter
384251881Speter  Instruction *I = dyn_cast<Instruction>(V);
385251881Speter  if (!I) return false;
386251881Speter
387251881Speter  // Loads are ok.
388251881Speter  if (isa<LoadInst>(I)) return true;
389251881Speter
390251881Speter  // Stores *to* the pointer are ok.
391251881Speter  if (StoreInst *SI = dyn_cast<StoreInst>(I))
392251881Speter    return SI->getOperand(0) != V;
393251881Speter
394251881Speter  // Otherwise, it must be a GEP.
395251881Speter  GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I);
396251881Speter  if (GEPI == 0) return false;
397251881Speter
398251881Speter  if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) ||
399251881Speter      !cast<Constant>(GEPI->getOperand(1))->isNullValue())
400251881Speter    return false;
401251881Speter
402251881Speter  for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end();
403251881Speter       I != E; ++I)
404251881Speter    if (!isSafeSROAElementUse(*I))
405251881Speter      return false;
406251881Speter  return true;
407251881Speter}
408251881Speter
409251881Speter
410251881Speter/// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value.
411251881Speter/// Look at it and its uses and decide whether it is safe to SROA this global.
412251881Speter///
413251881Speterstatic bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) {
414251881Speter  // The user of the global must be a GEP Inst or a ConstantExpr GEP.
415251881Speter  if (!isa<GetElementPtrInst>(U) &&
416251881Speter      (!isa<ConstantExpr>(U) ||
417251881Speter       cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr))
418251881Speter    return false;
419251881Speter
420251881Speter  // Check to see if this ConstantExpr GEP is SRA'able.  In particular, we
421251881Speter  // don't like < 3 operand CE's, and we don't like non-constant integer
422251881Speter  // indices.  This enforces that all uses are 'gep GV, 0, C, ...' for some
423251881Speter  // value of C.
424251881Speter  if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) ||
425251881Speter      !cast<Constant>(U->getOperand(1))->isNullValue() ||
426251881Speter      !isa<ConstantInt>(U->getOperand(2)))
427251881Speter    return false;
428251881Speter
429251881Speter  gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U);
430251881Speter  ++GEPI;  // Skip over the pointer index.
431251881Speter
432251881Speter  // If this is a use of an array allocation, do a bit more checking for sanity.
433251881Speter  if (ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) {
434251881Speter    uint64_t NumElements = AT->getNumElements();
435251881Speter    ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2));
436251881Speter
437251881Speter    // Check to make sure that index falls within the array.  If not,
438251881Speter    // something funny is going on, so we won't do the optimization.
439251881Speter    //
440251881Speter    if (Idx->getZExtValue() >= NumElements)
441251881Speter      return false;
442251881Speter
443251881Speter    // We cannot scalar repl this level of the array unless any array
444251881Speter    // sub-indices are in-range constants.  In particular, consider:
445251881Speter    // A[0][i].  We cannot know that the user isn't doing invalid things like
446251881Speter    // allowing i to index an out-of-range subscript that accesses A[1].
447251881Speter    //
448251881Speter    // Scalar replacing *just* the outer index of the array is probably not
449251881Speter    // going to be a win anyway, so just give up.
450251881Speter    for (++GEPI; // Skip array index.
451251881Speter         GEPI != E;
452251881Speter         ++GEPI) {
453251881Speter      uint64_t NumElements;
454251881Speter      if (ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI))
455251881Speter        NumElements = SubArrayTy->getNumElements();
456251881Speter      else if (VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI))
457251881Speter        NumElements = SubVectorTy->getNumElements();
458251881Speter      else {
459251881Speter        assert((*GEPI)->isStructTy() &&
460251881Speter               "Indexed GEP type is not array, vector, or struct!");
461251881Speter        continue;
462251881Speter      }
463251881Speter
464251881Speter      ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand());
465251881Speter      if (!IdxVal || IdxVal->getZExtValue() >= NumElements)
466251881Speter        return false;
467251881Speter    }
468251881Speter  }
469251881Speter
470251881Speter  for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I)
471251881Speter    if (!isSafeSROAElementUse(*I))
472251881Speter      return false;
473251881Speter  return true;
474251881Speter}
475251881Speter
476251881Speter/// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it
477251881Speter/// is safe for us to perform this transformation.
478251881Speter///
479251881Speterstatic bool GlobalUsersSafeToSRA(GlobalValue *GV) {
480251881Speter  for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
481251881Speter       UI != E; ++UI) {
482251881Speter    if (!IsUserOfGlobalSafeForSRA(*UI, GV))
483251881Speter      return false;
484251881Speter  }
485251881Speter  return true;
486251881Speter}
487251881Speter
488251881Speter
489251881Speter/// SRAGlobal - Perform scalar replacement of aggregates on the specified global
490251881Speter/// variable.  This opens the door for other optimizations by exposing the
491251881Speter/// behavior of the program in a more fine-grained way.  We have determined that
492251881Speter/// this transformation is safe already.  We return the first global variable we
493251881Speter/// insert so that the caller can reprocess it.
494251881Speterstatic GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) {
495251881Speter  // Make sure this global only has simple uses that we can SRA.
496251881Speter  if (!GlobalUsersSafeToSRA(GV))
497251881Speter    return 0;
498251881Speter
499251881Speter  assert(GV->hasLocalLinkage() && !GV->isConstant());
500251881Speter  Constant *Init = GV->getInitializer();
501251881Speter  Type *Ty = Init->getType();
502251881Speter
503251881Speter  std::vector<GlobalVariable*> NewGlobals;
504251881Speter  Module::GlobalListType &Globals = GV->getParent()->getGlobalList();
505251881Speter
506251881Speter  // Get the alignment of the global, either explicit or target-specific.
507251881Speter  unsigned StartAlignment = GV->getAlignment();
508251881Speter  if (StartAlignment == 0)
509251881Speter    StartAlignment = TD.getABITypeAlignment(GV->getType());
510251881Speter
511251881Speter  if (StructType *STy = dyn_cast<StructType>(Ty)) {
512251881Speter    NewGlobals.reserve(STy->getNumElements());
513251881Speter    const StructLayout &Layout = *TD.getStructLayout(STy);
514251881Speter    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
515251881Speter      Constant *In = Init->getAggregateElement(i);
516251881Speter      assert(In && "Couldn't get element of initializer?");
517251881Speter      GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
518251881Speter                                               GlobalVariable::InternalLinkage,
519251881Speter                                               In, GV->getName()+"."+Twine(i),
520251881Speter                                               GV->isThreadLocal(),
521251881Speter                                              GV->getType()->getAddressSpace());
522251881Speter      Globals.insert(GV, NGV);
523251881Speter      NewGlobals.push_back(NGV);
524251881Speter
525251881Speter      // Calculate the known alignment of the field.  If the original aggregate
526251881Speter      // had 256 byte alignment for example, something might depend on that:
527251881Speter      // propagate info to each field.
528251881Speter      uint64_t FieldOffset = Layout.getElementOffset(i);
529251881Speter      unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset);
530251881Speter      if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i)))
531251881Speter        NGV->setAlignment(NewAlign);
532251881Speter    }
533251881Speter  } else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
534251881Speter    unsigned NumElements = 0;
535251881Speter    if (ArrayType *ATy = dyn_cast<ArrayType>(STy))
536251881Speter      NumElements = ATy->getNumElements();
537251881Speter    else
538251881Speter      NumElements = cast<VectorType>(STy)->getNumElements();
539251881Speter
540251881Speter    if (NumElements > 16 && GV->hasNUsesOrMore(16))
541251881Speter      return 0; // It's not worth it.
542251881Speter    NewGlobals.reserve(NumElements);
543251881Speter
544251881Speter    uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType());
545251881Speter    unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
546251881Speter    for (unsigned i = 0, e = NumElements; i != e; ++i) {
547251881Speter      Constant *In = Init->getAggregateElement(i);
548251881Speter      assert(In && "Couldn't get element of initializer?");
549251881Speter
550251881Speter      GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
551251881Speter                                               GlobalVariable::InternalLinkage,
552251881Speter                                               In, GV->getName()+"."+Twine(i),
553251881Speter                                               GV->isThreadLocal(),
554251881Speter                                              GV->getType()->getAddressSpace());
555251881Speter      Globals.insert(GV, NGV);
556251881Speter      NewGlobals.push_back(NGV);
557251881Speter
558251881Speter      // Calculate the known alignment of the field.  If the original aggregate
559251881Speter      // had 256 byte alignment for example, something might depend on that:
560251881Speter      // propagate info to each field.
561251881Speter      unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i);
562251881Speter      if (NewAlign > EltAlign)
563251881Speter        NGV->setAlignment(NewAlign);
564251881Speter    }
565251881Speter  }
566251881Speter
567251881Speter  if (NewGlobals.empty())
568251881Speter    return 0;
569251881Speter
570251881Speter  DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV);
571251881Speter
572251881Speter  Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext()));
573251881Speter
574251881Speter  // Loop over all of the uses of the global, replacing the constantexpr geps,
575251881Speter  // with smaller constantexpr geps or direct references.
576251881Speter  while (!GV->use_empty()) {
577251881Speter    User *GEP = GV->use_back();
578251881Speter    assert(((isa<ConstantExpr>(GEP) &&
579251881Speter             cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)||
580251881Speter            isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!");
581251881Speter
582251881Speter    // Ignore the 1th operand, which has to be zero or else the program is quite
583251881Speter    // broken (undefined).  Get the 2nd operand, which is the structure or array
584251881Speter    // index.
585251881Speter    unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue();
586251881Speter    if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access.
587251881Speter
588251881Speter    Value *NewPtr = NewGlobals[Val];
589251881Speter
590251881Speter    // Form a shorter GEP if needed.
591251881Speter    if (GEP->getNumOperands() > 3) {
592251881Speter      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) {
593251881Speter        SmallVector<Constant*, 8> Idxs;
594251881Speter        Idxs.push_back(NullInt);
595251881Speter        for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i)
596251881Speter          Idxs.push_back(CE->getOperand(i));
597251881Speter        NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), Idxs);
598251881Speter      } else {
599251881Speter        GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP);
600251881Speter        SmallVector<Value*, 8> Idxs;
601251881Speter        Idxs.push_back(NullInt);
602251881Speter        for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i)
603251881Speter          Idxs.push_back(GEPI->getOperand(i));
604251881Speter        NewPtr = GetElementPtrInst::Create(NewPtr, Idxs,
605251881Speter                                           GEPI->getName()+"."+Twine(Val),GEPI);
606251881Speter      }
607251881Speter    }
608251881Speter    GEP->replaceAllUsesWith(NewPtr);
609251881Speter
610251881Speter    if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP))
611251881Speter      GEPI->eraseFromParent();
612251881Speter    else
613251881Speter      cast<ConstantExpr>(GEP)->destroyConstant();
614251881Speter  }
615251881Speter
616251881Speter  // Delete the old global, now that it is dead.
617251881Speter  Globals.erase(GV);
618251881Speter  ++NumSRA;
619251881Speter
620251881Speter  // Loop over the new globals array deleting any globals that are obviously
621251881Speter  // dead.  This can arise due to scalarization of a structure or an array that
622251881Speter  // has elements that are dead.
623251881Speter  unsigned FirstGlobal = 0;
624251881Speter  for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i)
625251881Speter    if (NewGlobals[i]->use_empty()) {
626251881Speter      Globals.erase(NewGlobals[i]);
627251881Speter      if (FirstGlobal == i) ++FirstGlobal;
628251881Speter    }
629251881Speter
630251881Speter  return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0;
631251881Speter}
632251881Speter
633251881Speter/// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified
634251881Speter/// value will trap if the value is dynamically null.  PHIs keeps track of any
635251881Speter/// phi nodes we've seen to avoid reprocessing them.
636251881Speterstatic bool AllUsesOfValueWillTrapIfNull(const Value *V,
637251881Speter                                         SmallPtrSet<const PHINode*, 8> &PHIs) {
638251881Speter  for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
639251881Speter       ++UI) {
640251881Speter    const User *U = *UI;
641251881Speter
642251881Speter    if (isa<LoadInst>(U)) {
643251881Speter      // Will trap.
644251881Speter    } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
645251881Speter      if (SI->getOperand(0) == V) {
646251881Speter        //cerr << "NONTRAPPING USE: " << *U;
647251881Speter        return false;  // Storing the value.
648251881Speter      }
649251881Speter    } else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
650251881Speter      if (CI->getCalledValue() != V) {
651251881Speter        //cerr << "NONTRAPPING USE: " << *U;
652251881Speter        return false;  // Not calling the ptr
653251881Speter      }
654251881Speter    } else if (const InvokeInst *II = dyn_cast<InvokeInst>(U)) {
655251881Speter      if (II->getCalledValue() != V) {
656251881Speter        //cerr << "NONTRAPPING USE: " << *U;
657251881Speter        return false;  // Not calling the ptr
658251881Speter      }
659251881Speter    } else if (const BitCastInst *CI = dyn_cast<BitCastInst>(U)) {
660251881Speter      if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false;
661251881Speter    } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
662251881Speter      if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false;
663251881Speter    } else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
664251881Speter      // If we've already seen this phi node, ignore it, it has already been
665251881Speter      // checked.
666251881Speter      if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs))
667251881Speter        return false;
668251881Speter    } else if (isa<ICmpInst>(U) &&
669251881Speter               isa<ConstantPointerNull>(UI->getOperand(1))) {
670251881Speter      // Ignore icmp X, null
671251881Speter    } else {
672251881Speter      //cerr << "NONTRAPPING USE: " << *U;
673251881Speter      return false;
674251881Speter    }
675251881Speter  }
676251881Speter  return true;
677251881Speter}
678251881Speter
679251881Speter/// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads
680251881Speter/// from GV will trap if the loaded value is null.  Note that this also permits
681251881Speter/// comparisons of the loaded value against null, as a special case.
682251881Speterstatic bool AllUsesOfLoadedValueWillTrapIfNull(const GlobalVariable *GV) {
683251881Speter  for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
684251881Speter       UI != E; ++UI) {
685251881Speter    const User *U = *UI;
686251881Speter
687251881Speter    if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
688251881Speter      SmallPtrSet<const PHINode*, 8> PHIs;
689251881Speter      if (!AllUsesOfValueWillTrapIfNull(LI, PHIs))
690251881Speter        return false;
691251881Speter    } else if (isa<StoreInst>(U)) {
692251881Speter      // Ignore stores to the global.
693251881Speter    } else {
694251881Speter      // We don't know or understand this user, bail out.
695251881Speter      //cerr << "UNKNOWN USER OF GLOBAL!: " << *U;
696251881Speter      return false;
697251881Speter    }
698251881Speter  }
699251881Speter  return true;
700251881Speter}
701251881Speter
702251881Speterstatic bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) {
703251881Speter  bool Changed = false;
704251881Speter  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) {
705251881Speter    Instruction *I = cast<Instruction>(*UI++);
706251881Speter    if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
707251881Speter      LI->setOperand(0, NewV);
708251881Speter      Changed = true;
709251881Speter    } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
710251881Speter      if (SI->getOperand(1) == V) {
711251881Speter        SI->setOperand(1, NewV);
712251881Speter        Changed = true;
713251881Speter      }
714251881Speter    } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
715251881Speter      CallSite CS(I);
716251881Speter      if (CS.getCalledValue() == V) {
717251881Speter        // Calling through the pointer!  Turn into a direct call, but be careful
718251881Speter        // that the pointer is not also being passed as an argument.
719251881Speter        CS.setCalledFunction(NewV);
720251881Speter        Changed = true;
721251881Speter        bool PassedAsArg = false;
722251881Speter        for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
723251881Speter          if (CS.getArgument(i) == V) {
724251881Speter            PassedAsArg = true;
725251881Speter            CS.setArgument(i, NewV);
726251881Speter          }
727251881Speter
728251881Speter        if (PassedAsArg) {
729251881Speter          // Being passed as an argument also.  Be careful to not invalidate UI!
730251881Speter          UI = V->use_begin();
731251881Speter        }
732251881Speter      }
733251881Speter    } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
734251881Speter      Changed |= OptimizeAwayTrappingUsesOfValue(CI,
735251881Speter                                ConstantExpr::getCast(CI->getOpcode(),
736251881Speter                                                      NewV, CI->getType()));
737251881Speter      if (CI->use_empty()) {
738251881Speter        Changed = true;
739251881Speter        CI->eraseFromParent();
740251881Speter      }
741251881Speter    } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
742251881Speter      // Should handle GEP here.
743251881Speter      SmallVector<Constant*, 8> Idxs;
744251881Speter      Idxs.reserve(GEPI->getNumOperands()-1);
745251881Speter      for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end();
746251881Speter           i != e; ++i)
747251881Speter        if (Constant *C = dyn_cast<Constant>(*i))
748251881Speter          Idxs.push_back(C);
749251881Speter        else
750251881Speter          break;
751251881Speter      if (Idxs.size() == GEPI->getNumOperands()-1)
752251881Speter        Changed |= OptimizeAwayTrappingUsesOfValue(GEPI,
753251881Speter                          ConstantExpr::getGetElementPtr(NewV, Idxs));
754251881Speter      if (GEPI->use_empty()) {
755251881Speter        Changed = true;
756251881Speter        GEPI->eraseFromParent();
757251881Speter      }
758251881Speter    }
759251881Speter  }
760251881Speter
761251881Speter  return Changed;
762251881Speter}
763251881Speter
764251881Speter
765251881Speter/// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null
766251881Speter/// value stored into it.  If there are uses of the loaded value that would trap
767251881Speter/// if the loaded value is dynamically null, then we know that they cannot be
768251881Speter/// reachable with a null optimize away the load.
769251881Speterstatic bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV,
770251881Speter                                            TargetData *TD,
771251881Speter                                            TargetLibraryInfo *TLI) {
772251881Speter  bool Changed = false;
773251881Speter
774251881Speter  // Keep track of whether we are able to remove all the uses of the global
775251881Speter  // other than the store that defines it.
776251881Speter  bool AllNonStoreUsesGone = true;
777251881Speter
778251881Speter  // Replace all uses of loads with uses of uses of the stored value.
779251881Speter  for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){
780251881Speter    User *GlobalUser = *GUI++;
781251881Speter    if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) {
782251881Speter      Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV);
783251881Speter      // If we were able to delete all uses of the loads
784251881Speter      if (LI->use_empty()) {
785251881Speter        LI->eraseFromParent();
786251881Speter        Changed = true;
787251881Speter      } else {
788251881Speter        AllNonStoreUsesGone = false;
789251881Speter      }
790251881Speter    } else if (isa<StoreInst>(GlobalUser)) {
791251881Speter      // Ignore the store that stores "LV" to the global.
792251881Speter      assert(GlobalUser->getOperand(1) == GV &&
793251881Speter             "Must be storing *to* the global");
794251881Speter    } else {
795251881Speter      AllNonStoreUsesGone = false;
796251881Speter
797251881Speter      // If we get here we could have other crazy uses that are transitively
798251881Speter      // loaded.
799251881Speter      assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) ||
800251881Speter              isa<ConstantExpr>(GlobalUser) || isa<CmpInst>(GlobalUser)) &&
801251881Speter             "Only expect load and stores!");
802251881Speter    }
803251881Speter  }
804251881Speter
805251881Speter  if (Changed) {
806251881Speter    DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV);
807251881Speter    ++NumGlobUses;
808251881Speter  }
809251881Speter
810251881Speter  // If we nuked all of the loads, then none of the stores are needed either,
811251881Speter  // nor is the global.
812251881Speter  if (AllNonStoreUsesGone) {
813251881Speter    DEBUG(dbgs() << "  *** GLOBAL NOW DEAD!\n");
814251881Speter    CleanupConstantGlobalUsers(GV, 0, TD, TLI);
815251881Speter    if (GV->use_empty()) {
816251881Speter      GV->eraseFromParent();
817251881Speter      ++NumDeleted;
818251881Speter    }
819251881Speter    Changed = true;
820251881Speter  }
821251881Speter  return Changed;
822251881Speter}
823251881Speter
824251881Speter/// ConstantPropUsersOf - Walk the use list of V, constant folding all of the
825251881Speter/// instructions that are foldable.
826251881Speterstatic void ConstantPropUsersOf(Value *V,
827251881Speter                                TargetData *TD, TargetLibraryInfo *TLI) {
828251881Speter  for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; )
829251881Speter    if (Instruction *I = dyn_cast<Instruction>(*UI++))
830251881Speter      if (Constant *NewC = ConstantFoldInstruction(I, TD, TLI)) {
831251881Speter        I->replaceAllUsesWith(NewC);
832251881Speter
833251881Speter        // Advance UI to the next non-I use to avoid invalidating it!
834251881Speter        // Instructions could multiply use V.
835251881Speter        while (UI != E && *UI == I)
836251881Speter          ++UI;
837251881Speter        I->eraseFromParent();
838251881Speter      }
839251881Speter}
840251881Speter
841251881Speter/// OptimizeGlobalAddressOfMalloc - This function takes the specified global
842251881Speter/// variable, and transforms the program as if it always contained the result of
843251881Speter/// the specified malloc.  Because it is always the result of the specified
844251881Speter/// malloc, there is no reason to actually DO the malloc.  Instead, turn the
845251881Speter/// malloc into a global, and any loads of GV as uses of the new global.
846251881Speterstatic GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
847251881Speter                                                     CallInst *CI,
848251881Speter                                                     Type *AllocTy,
849251881Speter                                                     ConstantInt *NElements,
850251881Speter                                                     TargetData *TD,
851251881Speter                                                     TargetLibraryInfo *TLI) {
852251881Speter  DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << "  CALL = " << *CI << '\n');
853251881Speter
854251881Speter  Type *GlobalType;
855251881Speter  if (NElements->getZExtValue() == 1)
856251881Speter    GlobalType = AllocTy;
857251881Speter  else
858251881Speter    // If we have an array allocation, the global variable is of an array.
859251881Speter    GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue());
860251881Speter
861251881Speter  // Create the new global variable.  The contents of the malloc'd memory is
862251881Speter  // undefined, so initialize with an undef value.
863251881Speter  GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
864251881Speter                                             GlobalType, false,
865251881Speter                                             GlobalValue::InternalLinkage,
866251881Speter                                             UndefValue::get(GlobalType),
867251881Speter                                             GV->getName()+".body",
868251881Speter                                             GV,
869251881Speter                                             GV->isThreadLocal());
870251881Speter
871251881Speter  // If there are bitcast users of the malloc (which is typical, usually we have
872251881Speter  // a malloc + bitcast) then replace them with uses of the new global.  Update
873251881Speter  // other users to use the global as well.
874251881Speter  BitCastInst *TheBC = 0;
875251881Speter  while (!CI->use_empty()) {
876251881Speter    Instruction *User = cast<Instruction>(CI->use_back());
877251881Speter    if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
878251881Speter      if (BCI->getType() == NewGV->getType()) {
879251881Speter        BCI->replaceAllUsesWith(NewGV);
880251881Speter        BCI->eraseFromParent();
881251881Speter      } else {
882251881Speter        BCI->setOperand(0, NewGV);
883251881Speter      }
884251881Speter    } else {
885251881Speter      if (TheBC == 0)
886251881Speter        TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI);
887      User->replaceUsesOfWith(CI, TheBC);
888    }
889  }
890
891  Constant *RepValue = NewGV;
892  if (NewGV->getType() != GV->getType()->getElementType())
893    RepValue = ConstantExpr::getBitCast(RepValue,
894                                        GV->getType()->getElementType());
895
896  // If there is a comparison against null, we will insert a global bool to
897  // keep track of whether the global was initialized yet or not.
898  GlobalVariable *InitBool =
899    new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
900                       GlobalValue::InternalLinkage,
901                       ConstantInt::getFalse(GV->getContext()),
902                       GV->getName()+".init", GV->isThreadLocal());
903  bool InitBoolUsed = false;
904
905  // Loop over all uses of GV, processing them in turn.
906  while (!GV->use_empty()) {
907    if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) {
908      // The global is initialized when the store to it occurs.
909      new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, false, 0,
910                    SI->getOrdering(), SI->getSynchScope(), SI);
911      SI->eraseFromParent();
912      continue;
913    }
914
915    LoadInst *LI = cast<LoadInst>(GV->use_back());
916    while (!LI->use_empty()) {
917      Use &LoadUse = LI->use_begin().getUse();
918      if (!isa<ICmpInst>(LoadUse.getUser())) {
919        LoadUse = RepValue;
920        continue;
921      }
922
923      ICmpInst *ICI = cast<ICmpInst>(LoadUse.getUser());
924      // Replace the cmp X, 0 with a use of the bool value.
925      // Sink the load to where the compare was, if atomic rules allow us to.
926      Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", false, 0,
927                               LI->getOrdering(), LI->getSynchScope(),
928                               LI->isUnordered() ? (Instruction*)ICI : LI);
929      InitBoolUsed = true;
930      switch (ICI->getPredicate()) {
931      default: llvm_unreachable("Unknown ICmp Predicate!");
932      case ICmpInst::ICMP_ULT:
933      case ICmpInst::ICMP_SLT:   // X < null -> always false
934        LV = ConstantInt::getFalse(GV->getContext());
935        break;
936      case ICmpInst::ICMP_ULE:
937      case ICmpInst::ICMP_SLE:
938      case ICmpInst::ICMP_EQ:
939        LV = BinaryOperator::CreateNot(LV, "notinit", ICI);
940        break;
941      case ICmpInst::ICMP_NE:
942      case ICmpInst::ICMP_UGE:
943      case ICmpInst::ICMP_SGE:
944      case ICmpInst::ICMP_UGT:
945      case ICmpInst::ICMP_SGT:
946        break;  // no change.
947      }
948      ICI->replaceAllUsesWith(LV);
949      ICI->eraseFromParent();
950    }
951    LI->eraseFromParent();
952  }
953
954  // If the initialization boolean was used, insert it, otherwise delete it.
955  if (!InitBoolUsed) {
956    while (!InitBool->use_empty())  // Delete initializations
957      cast<StoreInst>(InitBool->use_back())->eraseFromParent();
958    delete InitBool;
959  } else
960    GV->getParent()->getGlobalList().insert(GV, InitBool);
961
962  // Now the GV is dead, nuke it and the malloc..
963  GV->eraseFromParent();
964  CI->eraseFromParent();
965
966  // To further other optimizations, loop over all users of NewGV and try to
967  // constant prop them.  This will promote GEP instructions with constant
968  // indices into GEP constant-exprs, which will allow global-opt to hack on it.
969  ConstantPropUsersOf(NewGV, TD, TLI);
970  if (RepValue != NewGV)
971    ConstantPropUsersOf(RepValue, TD, TLI);
972
973  return NewGV;
974}
975
976/// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking
977/// to make sure that there are no complex uses of V.  We permit simple things
978/// like dereferencing the pointer, but not storing through the address, unless
979/// it is to the specified global.
980static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(const Instruction *V,
981                                                      const GlobalVariable *GV,
982                                         SmallPtrSet<const PHINode*, 8> &PHIs) {
983  for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
984       UI != E; ++UI) {
985    const Instruction *Inst = cast<Instruction>(*UI);
986
987    if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) {
988      continue; // Fine, ignore.
989    }
990
991    if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
992      if (SI->getOperand(0) == V && SI->getOperand(1) != GV)
993        return false;  // Storing the pointer itself... bad.
994      continue; // Otherwise, storing through it, or storing into GV... fine.
995    }
996
997    // Must index into the array and into the struct.
998    if (isa<GetElementPtrInst>(Inst) && Inst->getNumOperands() >= 3) {
999      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs))
1000        return false;
1001      continue;
1002    }
1003
1004    if (const PHINode *PN = dyn_cast<PHINode>(Inst)) {
1005      // PHIs are ok if all uses are ok.  Don't infinitely recurse through PHI
1006      // cycles.
1007      if (PHIs.insert(PN))
1008        if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs))
1009          return false;
1010      continue;
1011    }
1012
1013    if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) {
1014      if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs))
1015        return false;
1016      continue;
1017    }
1018
1019    return false;
1020  }
1021  return true;
1022}
1023
1024/// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV
1025/// somewhere.  Transform all uses of the allocation into loads from the
1026/// global and uses of the resultant pointer.  Further, delete the store into
1027/// GV.  This assumes that these value pass the
1028/// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate.
1029static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc,
1030                                          GlobalVariable *GV) {
1031  while (!Alloc->use_empty()) {
1032    Instruction *U = cast<Instruction>(*Alloc->use_begin());
1033    Instruction *InsertPt = U;
1034    if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
1035      // If this is the store of the allocation into the global, remove it.
1036      if (SI->getOperand(1) == GV) {
1037        SI->eraseFromParent();
1038        continue;
1039      }
1040    } else if (PHINode *PN = dyn_cast<PHINode>(U)) {
1041      // Insert the load in the corresponding predecessor, not right before the
1042      // PHI.
1043      InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator();
1044    } else if (isa<BitCastInst>(U)) {
1045      // Must be bitcast between the malloc and store to initialize the global.
1046      ReplaceUsesOfMallocWithGlobal(U, GV);
1047      U->eraseFromParent();
1048      continue;
1049    } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) {
1050      // If this is a "GEP bitcast" and the user is a store to the global, then
1051      // just process it as a bitcast.
1052      if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse())
1053        if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->use_back()))
1054          if (SI->getOperand(1) == GV) {
1055            // Must be bitcast GEP between the malloc and store to initialize
1056            // the global.
1057            ReplaceUsesOfMallocWithGlobal(GEPI, GV);
1058            GEPI->eraseFromParent();
1059            continue;
1060          }
1061    }
1062
1063    // Insert a load from the global, and use it instead of the malloc.
1064    Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt);
1065    U->replaceUsesOfWith(Alloc, NL);
1066  }
1067}
1068
1069/// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi
1070/// of a load) are simple enough to perform heap SRA on.  This permits GEP's
1071/// that index through the array and struct field, icmps of null, and PHIs.
1072static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V,
1073                        SmallPtrSet<const PHINode*, 32> &LoadUsingPHIs,
1074                        SmallPtrSet<const PHINode*, 32> &LoadUsingPHIsPerLoad) {
1075  // We permit two users of the load: setcc comparing against the null
1076  // pointer, and a getelementptr of a specific form.
1077  for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;
1078       ++UI) {
1079    const Instruction *User = cast<Instruction>(*UI);
1080
1081    // Comparison against null is ok.
1082    if (const ICmpInst *ICI = dyn_cast<ICmpInst>(User)) {
1083      if (!isa<ConstantPointerNull>(ICI->getOperand(1)))
1084        return false;
1085      continue;
1086    }
1087
1088    // getelementptr is also ok, but only a simple form.
1089    if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
1090      // Must index into the array and into the struct.
1091      if (GEPI->getNumOperands() < 3)
1092        return false;
1093
1094      // Otherwise the GEP is ok.
1095      continue;
1096    }
1097
1098    if (const PHINode *PN = dyn_cast<PHINode>(User)) {
1099      if (!LoadUsingPHIsPerLoad.insert(PN))
1100        // This means some phi nodes are dependent on each other.
1101        // Avoid infinite looping!
1102        return false;
1103      if (!LoadUsingPHIs.insert(PN))
1104        // If we have already analyzed this PHI, then it is safe.
1105        continue;
1106
1107      // Make sure all uses of the PHI are simple enough to transform.
1108      if (!LoadUsesSimpleEnoughForHeapSRA(PN,
1109                                          LoadUsingPHIs, LoadUsingPHIsPerLoad))
1110        return false;
1111
1112      continue;
1113    }
1114
1115    // Otherwise we don't know what this is, not ok.
1116    return false;
1117  }
1118
1119  return true;
1120}
1121
1122
1123/// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from
1124/// GV are simple enough to perform HeapSRA, return true.
1125static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV,
1126                                                    Instruction *StoredVal) {
1127  SmallPtrSet<const PHINode*, 32> LoadUsingPHIs;
1128  SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad;
1129  for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end();
1130       UI != E; ++UI)
1131    if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1132      if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs,
1133                                          LoadUsingPHIsPerLoad))
1134        return false;
1135      LoadUsingPHIsPerLoad.clear();
1136    }
1137
1138  // If we reach here, we know that all uses of the loads and transitive uses
1139  // (through PHI nodes) are simple enough to transform.  However, we don't know
1140  // that all inputs the to the PHI nodes are in the same equivalence sets.
1141  // Check to verify that all operands of the PHIs are either PHIS that can be
1142  // transformed, loads from GV, or MI itself.
1143  for (SmallPtrSet<const PHINode*, 32>::const_iterator I = LoadUsingPHIs.begin()
1144       , E = LoadUsingPHIs.end(); I != E; ++I) {
1145    const PHINode *PN = *I;
1146    for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) {
1147      Value *InVal = PN->getIncomingValue(op);
1148
1149      // PHI of the stored value itself is ok.
1150      if (InVal == StoredVal) continue;
1151
1152      if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) {
1153        // One of the PHIs in our set is (optimistically) ok.
1154        if (LoadUsingPHIs.count(InPN))
1155          continue;
1156        return false;
1157      }
1158
1159      // Load from GV is ok.
1160      if (const LoadInst *LI = dyn_cast<LoadInst>(InVal))
1161        if (LI->getOperand(0) == GV)
1162          continue;
1163
1164      // UNDEF? NULL?
1165
1166      // Anything else is rejected.
1167      return false;
1168    }
1169  }
1170
1171  return true;
1172}
1173
1174static Value *GetHeapSROAValue(Value *V, unsigned FieldNo,
1175               DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1176                   std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1177  std::vector<Value*> &FieldVals = InsertedScalarizedValues[V];
1178
1179  if (FieldNo >= FieldVals.size())
1180    FieldVals.resize(FieldNo+1);
1181
1182  // If we already have this value, just reuse the previously scalarized
1183  // version.
1184  if (Value *FieldVal = FieldVals[FieldNo])
1185    return FieldVal;
1186
1187  // Depending on what instruction this is, we have several cases.
1188  Value *Result;
1189  if (LoadInst *LI = dyn_cast<LoadInst>(V)) {
1190    // This is a scalarized version of the load from the global.  Just create
1191    // a new Load of the scalarized global.
1192    Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo,
1193                                           InsertedScalarizedValues,
1194                                           PHIsToRewrite),
1195                          LI->getName()+".f"+Twine(FieldNo), LI);
1196  } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1197    // PN's type is pointer to struct.  Make a new PHI of pointer to struct
1198    // field.
1199    StructType *ST =
1200      cast<StructType>(cast<PointerType>(PN->getType())->getElementType());
1201
1202    PHINode *NewPN =
1203     PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)),
1204                     PN->getNumIncomingValues(),
1205                     PN->getName()+".f"+Twine(FieldNo), PN);
1206    Result = NewPN;
1207    PHIsToRewrite.push_back(std::make_pair(PN, FieldNo));
1208  } else {
1209    llvm_unreachable("Unknown usable value");
1210  }
1211
1212  return FieldVals[FieldNo] = Result;
1213}
1214
1215/// RewriteHeapSROALoadUser - Given a load instruction and a value derived from
1216/// the load, rewrite the derived value to use the HeapSRoA'd load.
1217static void RewriteHeapSROALoadUser(Instruction *LoadUser,
1218             DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1219                   std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1220  // If this is a comparison against null, handle it.
1221  if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) {
1222    assert(isa<ConstantPointerNull>(SCI->getOperand(1)));
1223    // If we have a setcc of the loaded pointer, we can use a setcc of any
1224    // field.
1225    Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0,
1226                                   InsertedScalarizedValues, PHIsToRewrite);
1227
1228    Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr,
1229                              Constant::getNullValue(NPtr->getType()),
1230                              SCI->getName());
1231    SCI->replaceAllUsesWith(New);
1232    SCI->eraseFromParent();
1233    return;
1234  }
1235
1236  // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...'
1237  if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) {
1238    assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2))
1239           && "Unexpected GEPI!");
1240
1241    // Load the pointer for this field.
1242    unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
1243    Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo,
1244                                     InsertedScalarizedValues, PHIsToRewrite);
1245
1246    // Create the new GEP idx vector.
1247    SmallVector<Value*, 8> GEPIdx;
1248    GEPIdx.push_back(GEPI->getOperand(1));
1249    GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end());
1250
1251    Value *NGEPI = GetElementPtrInst::Create(NewPtr, GEPIdx,
1252                                             GEPI->getName(), GEPI);
1253    GEPI->replaceAllUsesWith(NGEPI);
1254    GEPI->eraseFromParent();
1255    return;
1256  }
1257
1258  // Recursively transform the users of PHI nodes.  This will lazily create the
1259  // PHIs that are needed for individual elements.  Keep track of what PHIs we
1260  // see in InsertedScalarizedValues so that we don't get infinite loops (very
1261  // antisocial).  If the PHI is already in InsertedScalarizedValues, it has
1262  // already been seen first by another load, so its uses have already been
1263  // processed.
1264  PHINode *PN = cast<PHINode>(LoadUser);
1265  if (!InsertedScalarizedValues.insert(std::make_pair(PN,
1266                                              std::vector<Value*>())).second)
1267    return;
1268
1269  // If this is the first time we've seen this PHI, recursively process all
1270  // users.
1271  for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) {
1272    Instruction *User = cast<Instruction>(*UI++);
1273    RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1274  }
1275}
1276
1277/// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global.  Ptr
1278/// is a value loaded from the global.  Eliminate all uses of Ptr, making them
1279/// use FieldGlobals instead.  All uses of loaded values satisfy
1280/// AllGlobalLoadUsesSimpleEnoughForHeapSRA.
1281static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load,
1282               DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues,
1283                   std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) {
1284  for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end();
1285       UI != E; ) {
1286    Instruction *User = cast<Instruction>(*UI++);
1287    RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite);
1288  }
1289
1290  if (Load->use_empty()) {
1291    Load->eraseFromParent();
1292    InsertedScalarizedValues.erase(Load);
1293  }
1294}
1295
1296/// PerformHeapAllocSRoA - CI is an allocation of an array of structures.  Break
1297/// it up into multiple allocations of arrays of the fields.
1298static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI,
1299                                            Value *NElems, TargetData *TD) {
1300  DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << "  MALLOC = " << *CI << '\n');
1301  Type *MAT = getMallocAllocatedType(CI);
1302  StructType *STy = cast<StructType>(MAT);
1303
1304  // There is guaranteed to be at least one use of the malloc (storing
1305  // it into GV).  If there are other uses, change them to be uses of
1306  // the global to simplify later code.  This also deletes the store
1307  // into GV.
1308  ReplaceUsesOfMallocWithGlobal(CI, GV);
1309
1310  // Okay, at this point, there are no users of the malloc.  Insert N
1311  // new mallocs at the same place as CI, and N globals.
1312  std::vector<Value*> FieldGlobals;
1313  std::vector<Value*> FieldMallocs;
1314
1315  for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
1316    Type *FieldTy = STy->getElementType(FieldNo);
1317    PointerType *PFieldTy = PointerType::getUnqual(FieldTy);
1318
1319    GlobalVariable *NGV =
1320      new GlobalVariable(*GV->getParent(),
1321                         PFieldTy, false, GlobalValue::InternalLinkage,
1322                         Constant::getNullValue(PFieldTy),
1323                         GV->getName() + ".f" + Twine(FieldNo), GV,
1324                         GV->isThreadLocal());
1325    FieldGlobals.push_back(NGV);
1326
1327    unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
1328    if (StructType *ST = dyn_cast<StructType>(FieldTy))
1329      TypeSize = TD->getStructLayout(ST)->getSizeInBytes();
1330    Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
1331    Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy,
1332                                        ConstantInt::get(IntPtrTy, TypeSize),
1333                                        NElems, 0,
1334                                        CI->getName() + ".f" + Twine(FieldNo));
1335    FieldMallocs.push_back(NMI);
1336    new StoreInst(NMI, NGV, CI);
1337  }
1338
1339  // The tricky aspect of this transformation is handling the case when malloc
1340  // fails.  In the original code, malloc failing would set the result pointer
1341  // of malloc to null.  In this case, some mallocs could succeed and others
1342  // could fail.  As such, we emit code that looks like this:
1343  //    F0 = malloc(field0)
1344  //    F1 = malloc(field1)
1345  //    F2 = malloc(field2)
1346  //    if (F0 == 0 || F1 == 0 || F2 == 0) {
1347  //      if (F0) { free(F0); F0 = 0; }
1348  //      if (F1) { free(F1); F1 = 0; }
1349  //      if (F2) { free(F2); F2 = 0; }
1350  //    }
1351  // The malloc can also fail if its argument is too large.
1352  Constant *ConstantZero = ConstantInt::get(CI->getArgOperand(0)->getType(), 0);
1353  Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getArgOperand(0),
1354                                  ConstantZero, "isneg");
1355  for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1356    Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1357                             Constant::getNullValue(FieldMallocs[i]->getType()),
1358                               "isnull");
1359    RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI);
1360  }
1361
1362  // Split the basic block at the old malloc.
1363  BasicBlock *OrigBB = CI->getParent();
1364  BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont");
1365
1366  // Create the block to check the first condition.  Put all these blocks at the
1367  // end of the function as they are unlikely to be executed.
1368  BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(),
1369                                                "malloc_ret_null",
1370                                                OrigBB->getParent());
1371
1372  // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
1373  // branch on RunningOr.
1374  OrigBB->getTerminator()->eraseFromParent();
1375  BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
1376
1377  // Within the NullPtrBlock, we need to emit a comparison and branch for each
1378  // pointer, because some may be null while others are not.
1379  for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1380    Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
1381    Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1382                              Constant::getNullValue(GVVal->getType()));
1383    BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it",
1384                                               OrigBB->getParent());
1385    BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next",
1386                                               OrigBB->getParent());
1387    Instruction *BI = BranchInst::Create(FreeBlock, NextBlock,
1388                                         Cmp, NullPtrBlock);
1389
1390    // Fill in FreeBlock.
1391    CallInst::CreateFree(GVVal, BI);
1392    new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
1393                  FreeBlock);
1394    BranchInst::Create(NextBlock, FreeBlock);
1395
1396    NullPtrBlock = NextBlock;
1397  }
1398
1399  BranchInst::Create(ContBB, NullPtrBlock);
1400
1401  // CI is no longer needed, remove it.
1402  CI->eraseFromParent();
1403
1404  /// InsertedScalarizedLoads - As we process loads, if we can't immediately
1405  /// update all uses of the load, keep track of what scalarized loads are
1406  /// inserted for a given load.
1407  DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues;
1408  InsertedScalarizedValues[GV] = FieldGlobals;
1409
1410  std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite;
1411
1412  // Okay, the malloc site is completely handled.  All of the uses of GV are now
1413  // loads, and all uses of those loads are simple.  Rewrite them to use loads
1414  // of the per-field globals instead.
1415  for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) {
1416    Instruction *User = cast<Instruction>(*UI++);
1417
1418    if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1419      RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite);
1420      continue;
1421    }
1422
1423    // Must be a store of null.
1424    StoreInst *SI = cast<StoreInst>(User);
1425    assert(isa<ConstantPointerNull>(SI->getOperand(0)) &&
1426           "Unexpected heap-sra user!");
1427
1428    // Insert a store of null into each global.
1429    for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1430      PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType());
1431      Constant *Null = Constant::getNullValue(PT->getElementType());
1432      new StoreInst(Null, FieldGlobals[i], SI);
1433    }
1434    // Erase the original store.
1435    SI->eraseFromParent();
1436  }
1437
1438  // While we have PHIs that are interesting to rewrite, do it.
1439  while (!PHIsToRewrite.empty()) {
1440    PHINode *PN = PHIsToRewrite.back().first;
1441    unsigned FieldNo = PHIsToRewrite.back().second;
1442    PHIsToRewrite.pop_back();
1443    PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]);
1444    assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
1445
1446    // Add all the incoming values.  This can materialize more phis.
1447    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1448      Value *InVal = PN->getIncomingValue(i);
1449      InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
1450                               PHIsToRewrite);
1451      FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
1452    }
1453  }
1454
1455  // Drop all inter-phi links and any loads that made it this far.
1456  for (DenseMap<Value*, std::vector<Value*> >::iterator
1457       I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1458       I != E; ++I) {
1459    if (PHINode *PN = dyn_cast<PHINode>(I->first))
1460      PN->dropAllReferences();
1461    else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1462      LI->dropAllReferences();
1463  }
1464
1465  // Delete all the phis and loads now that inter-references are dead.
1466  for (DenseMap<Value*, std::vector<Value*> >::iterator
1467       I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1468       I != E; ++I) {
1469    if (PHINode *PN = dyn_cast<PHINode>(I->first))
1470      PN->eraseFromParent();
1471    else if (LoadInst *LI = dyn_cast<LoadInst>(I->first))
1472      LI->eraseFromParent();
1473  }
1474
1475  // The old global is now dead, remove it.
1476  GV->eraseFromParent();
1477
1478  ++NumHeapSRA;
1479  return cast<GlobalVariable>(FieldGlobals[0]);
1480}
1481
1482/// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a
1483/// pointer global variable with a single value stored it that is a malloc or
1484/// cast of malloc.
1485static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
1486                                               CallInst *CI,
1487                                               Type *AllocTy,
1488                                               AtomicOrdering Ordering,
1489                                               Module::global_iterator &GVI,
1490                                               TargetData *TD,
1491                                               TargetLibraryInfo *TLI) {
1492  if (!TD)
1493    return false;
1494
1495  // If this is a malloc of an abstract type, don't touch it.
1496  if (!AllocTy->isSized())
1497    return false;
1498
1499  // We can't optimize this global unless all uses of it are *known* to be
1500  // of the malloc value, not of the null initializer value (consider a use
1501  // that compares the global's value against zero to see if the malloc has
1502  // been reached).  To do this, we check to see if all uses of the global
1503  // would trap if the global were null: this proves that they must all
1504  // happen after the malloc.
1505  if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
1506    return false;
1507
1508  // We can't optimize this if the malloc itself is used in a complex way,
1509  // for example, being stored into multiple globals.  This allows the
1510  // malloc to be stored into the specified global, loaded icmp'd, and
1511  // GEP'd.  These are all things we could transform to using the global
1512  // for.
1513  SmallPtrSet<const PHINode*, 8> PHIs;
1514  if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs))
1515    return false;
1516
1517  // If we have a global that is only initialized with a fixed size malloc,
1518  // transform the program to use global memory instead of malloc'd memory.
1519  // This eliminates dynamic allocation, avoids an indirection accessing the
1520  // data, and exposes the resultant global to further GlobalOpt.
1521  // We cannot optimize the malloc if we cannot determine malloc array size.
1522  Value *NElems = getMallocArraySize(CI, TD, true);
1523  if (!NElems)
1524    return false;
1525
1526  if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
1527    // Restrict this transformation to only working on small allocations
1528    // (2048 bytes currently), as we don't want to introduce a 16M global or
1529    // something.
1530    if (NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) {
1531      GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, TD, TLI);
1532      return true;
1533    }
1534
1535  // If the allocation is an array of structures, consider transforming this
1536  // into multiple malloc'd arrays, one for each field.  This is basically
1537  // SRoA for malloc'd memory.
1538
1539  if (Ordering != NotAtomic)
1540    return false;
1541
1542  // If this is an allocation of a fixed size array of structs, analyze as a
1543  // variable size array.  malloc [100 x struct],1 -> malloc struct, 100
1544  if (NElems == ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
1545    if (ArrayType *AT = dyn_cast<ArrayType>(AllocTy))
1546      AllocTy = AT->getElementType();
1547
1548  StructType *AllocSTy = dyn_cast<StructType>(AllocTy);
1549  if (!AllocSTy)
1550    return false;
1551
1552  // This the structure has an unreasonable number of fields, leave it
1553  // alone.
1554  if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1555      AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) {
1556
1557    // If this is a fixed size array, transform the Malloc to be an alloc of
1558    // structs.  malloc [100 x struct],1 -> malloc struct, 100
1559    if (ArrayType *AT = dyn_cast<ArrayType>(getMallocAllocatedType(CI))) {
1560      Type *IntPtrTy = TD->getIntPtrType(CI->getContext());
1561      unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes();
1562      Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize);
1563      Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements());
1564      Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy,
1565                                                   AllocSize, NumElements,
1566                                                   0, CI->getName());
1567      Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI);
1568      CI->replaceAllUsesWith(Cast);
1569      CI->eraseFromParent();
1570      CI = dyn_cast<BitCastInst>(Malloc) ?
1571        extractMallocCallFromBitCast(Malloc) : cast<CallInst>(Malloc);
1572    }
1573
1574    GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD, true), TD);
1575    return true;
1576  }
1577
1578  return false;
1579}
1580
1581// OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge
1582// that only one value (besides its initializer) is ever stored to the global.
1583static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal,
1584                                     AtomicOrdering Ordering,
1585                                     Module::global_iterator &GVI,
1586                                     TargetData *TD, TargetLibraryInfo *TLI) {
1587  // Ignore no-op GEPs and bitcasts.
1588  StoredOnceVal = StoredOnceVal->stripPointerCasts();
1589
1590  // If we are dealing with a pointer global that is initialized to null and
1591  // only has one (non-null) value stored into it, then we can optimize any
1592  // users of the loaded value (often calls and loads) that would trap if the
1593  // value was null.
1594  if (GV->getInitializer()->getType()->isPointerTy() &&
1595      GV->getInitializer()->isNullValue()) {
1596    if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) {
1597      if (GV->getInitializer()->getType() != SOVC->getType())
1598        SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
1599
1600      // Optimize away any trapping uses of the loaded value.
1601      if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, TD, TLI))
1602        return true;
1603    } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) {
1604      Type *MallocType = getMallocAllocatedType(CI);
1605      if (MallocType &&
1606          TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, Ordering, GVI,
1607                                             TD, TLI))
1608        return true;
1609    }
1610  }
1611
1612  return false;
1613}
1614
1615/// TryToShrinkGlobalToBoolean - At this point, we have learned that the only
1616/// two values ever stored into GV are its initializer and OtherVal.  See if we
1617/// can shrink the global into a boolean and select between the two values
1618/// whenever it is used.  This exposes the values to other scalar optimizations.
1619static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
1620  Type *GVElType = GV->getType()->getElementType();
1621
1622  // If GVElType is already i1, it is already shrunk.  If the type of the GV is
1623  // an FP value, pointer or vector, don't do this optimization because a select
1624  // between them is very expensive and unlikely to lead to later
1625  // simplification.  In these cases, we typically end up with "cond ? v1 : v2"
1626  // where v1 and v2 both require constant pool loads, a big loss.
1627  if (GVElType == Type::getInt1Ty(GV->getContext()) ||
1628      GVElType->isFloatingPointTy() ||
1629      GVElType->isPointerTy() || GVElType->isVectorTy())
1630    return false;
1631
1632  // Walk the use list of the global seeing if all the uses are load or store.
1633  // If there is anything else, bail out.
1634  for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I){
1635    User *U = *I;
1636    if (!isa<LoadInst>(U) && !isa<StoreInst>(U))
1637      return false;
1638  }
1639
1640  DEBUG(dbgs() << "   *** SHRINKING TO BOOL: " << *GV);
1641
1642  // Create the new global, initializing it to false.
1643  GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()),
1644                                             false,
1645                                             GlobalValue::InternalLinkage,
1646                                        ConstantInt::getFalse(GV->getContext()),
1647                                             GV->getName()+".b",
1648                                             GV->isThreadLocal());
1649  GV->getParent()->getGlobalList().insert(GV, NewGV);
1650
1651  Constant *InitVal = GV->getInitializer();
1652  assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) &&
1653         "No reason to shrink to bool!");
1654
1655  // If initialized to zero and storing one into the global, we can use a cast
1656  // instead of a select to synthesize the desired value.
1657  bool IsOneZero = false;
1658  if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal))
1659    IsOneZero = InitVal->isNullValue() && CI->isOne();
1660
1661  while (!GV->use_empty()) {
1662    Instruction *UI = cast<Instruction>(GV->use_back());
1663    if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
1664      // Change the store into a boolean store.
1665      bool StoringOther = SI->getOperand(0) == OtherVal;
1666      // Only do this if we weren't storing a loaded value.
1667      Value *StoreVal;
1668      if (StoringOther || SI->getOperand(0) == InitVal)
1669        StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()),
1670                                    StoringOther);
1671      else {
1672        // Otherwise, we are storing a previously loaded copy.  To do this,
1673        // change the copy from copying the original value to just copying the
1674        // bool.
1675        Instruction *StoredVal = cast<Instruction>(SI->getOperand(0));
1676
1677        // If we've already replaced the input, StoredVal will be a cast or
1678        // select instruction.  If not, it will be a load of the original
1679        // global.
1680        if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
1681          assert(LI->getOperand(0) == GV && "Not a copy!");
1682          // Insert a new load, to preserve the saved value.
1683          StoreVal = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1684                                  LI->getOrdering(), LI->getSynchScope(), LI);
1685        } else {
1686          assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) &&
1687                 "This is not a form that we understand!");
1688          StoreVal = StoredVal->getOperand(0);
1689          assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!");
1690        }
1691      }
1692      new StoreInst(StoreVal, NewGV, false, 0,
1693                    SI->getOrdering(), SI->getSynchScope(), SI);
1694    } else {
1695      // Change the load into a load of bool then a select.
1696      LoadInst *LI = cast<LoadInst>(UI);
1697      LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", false, 0,
1698                                   LI->getOrdering(), LI->getSynchScope(), LI);
1699      Value *NSI;
1700      if (IsOneZero)
1701        NSI = new ZExtInst(NLI, LI->getType(), "", LI);
1702      else
1703        NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI);
1704      NSI->takeName(LI);
1705      LI->replaceAllUsesWith(NSI);
1706    }
1707    UI->eraseFromParent();
1708  }
1709
1710  GV->eraseFromParent();
1711  return true;
1712}
1713
1714
1715/// ProcessGlobal - Analyze the specified global variable and optimize it if
1716/// possible.  If we make a change, return true.
1717bool GlobalOpt::ProcessGlobal(GlobalVariable *GV,
1718                              Module::global_iterator &GVI) {
1719  if (!GV->hasLocalLinkage())
1720    return false;
1721
1722  // Do more involved optimizations if the global is internal.
1723  GV->removeDeadConstantUsers();
1724
1725  if (GV->use_empty()) {
1726    DEBUG(dbgs() << "GLOBAL DEAD: " << *GV);
1727    GV->eraseFromParent();
1728    ++NumDeleted;
1729    return true;
1730  }
1731
1732  SmallPtrSet<const PHINode*, 16> PHIUsers;
1733  GlobalStatus GS;
1734
1735  if (AnalyzeGlobal(GV, GS, PHIUsers))
1736    return false;
1737
1738  if (!GS.isCompared && !GV->hasUnnamedAddr()) {
1739    GV->setUnnamedAddr(true);
1740    NumUnnamed++;
1741  }
1742
1743  if (GV->isConstant() || !GV->hasInitializer())
1744    return false;
1745
1746  return ProcessInternalGlobal(GV, GVI, PHIUsers, GS);
1747}
1748
1749/// ProcessInternalGlobal - Analyze the specified global variable and optimize
1750/// it if possible.  If we make a change, return true.
1751bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV,
1752                                      Module::global_iterator &GVI,
1753                                const SmallPtrSet<const PHINode*, 16> &PHIUsers,
1754                                      const GlobalStatus &GS) {
1755  // If this is a first class global and has only one accessing function
1756  // and this function is main (which we know is not recursive we can make
1757  // this global a local variable) we replace the global with a local alloca
1758  // in this function.
1759  //
1760  // NOTE: It doesn't make sense to promote non single-value types since we
1761  // are just replacing static memory to stack memory.
1762  //
1763  // If the global is in different address space, don't bring it to stack.
1764  if (!GS.HasMultipleAccessingFunctions &&
1765      GS.AccessingFunction && !GS.HasNonInstructionUser &&
1766      GV->getType()->getElementType()->isSingleValueType() &&
1767      GS.AccessingFunction->getName() == "main" &&
1768      GS.AccessingFunction->hasExternalLinkage() &&
1769      GV->getType()->getAddressSpace() == 0) {
1770    DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV);
1771    Instruction &FirstI = const_cast<Instruction&>(*GS.AccessingFunction
1772                                                   ->getEntryBlock().begin());
1773    Type *ElemTy = GV->getType()->getElementType();
1774    // FIXME: Pass Global's alignment when globals have alignment
1775    AllocaInst *Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), &FirstI);
1776    if (!isa<UndefValue>(GV->getInitializer()))
1777      new StoreInst(GV->getInitializer(), Alloca, &FirstI);
1778
1779    GV->replaceAllUsesWith(Alloca);
1780    GV->eraseFromParent();
1781    ++NumLocalized;
1782    return true;
1783  }
1784
1785  // If the global is never loaded (but may be stored to), it is dead.
1786  // Delete it now.
1787  if (!GS.isLoaded) {
1788    DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV);
1789
1790    // Delete any stores we can find to the global.  We may not be able to
1791    // make it completely dead though.
1792    bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer(),
1793                                              TD, TLI);
1794
1795    // If the global is dead now, delete it.
1796    if (GV->use_empty()) {
1797      GV->eraseFromParent();
1798      ++NumDeleted;
1799      Changed = true;
1800    }
1801    return Changed;
1802
1803  } else if (GS.StoredType <= GlobalStatus::isInitializerStored) {
1804    DEBUG(dbgs() << "MARKING CONSTANT: " << *GV);
1805    GV->setConstant(true);
1806
1807    // Clean up any obviously simplifiable users now.
1808    CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI);
1809
1810    // If the global is dead now, just nuke it.
1811    if (GV->use_empty()) {
1812      DEBUG(dbgs() << "   *** Marking constant allowed us to simplify "
1813            << "all users and delete global!\n");
1814      GV->eraseFromParent();
1815      ++NumDeleted;
1816    }
1817
1818    ++NumMarked;
1819    return true;
1820  } else if (!GV->getInitializer()->getType()->isSingleValueType()) {
1821    if (TargetData *TD = getAnalysisIfAvailable<TargetData>())
1822      if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) {
1823        GVI = FirstNewGV;  // Don't skip the newly produced globals!
1824        return true;
1825      }
1826  } else if (GS.StoredType == GlobalStatus::isStoredOnce) {
1827    // If the initial value for the global was an undef value, and if only
1828    // one other value was stored into it, we can just change the
1829    // initializer to be the stored value, then delete all stores to the
1830    // global.  This allows us to mark it constant.
1831    if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
1832      if (isa<UndefValue>(GV->getInitializer())) {
1833        // Change the initial value here.
1834        GV->setInitializer(SOVConstant);
1835
1836        // Clean up any obviously simplifiable users now.
1837        CleanupConstantGlobalUsers(GV, GV->getInitializer(), TD, TLI);
1838
1839        if (GV->use_empty()) {
1840          DEBUG(dbgs() << "   *** Substituting initializer allowed us to "
1841                << "simplify all users and delete global!\n");
1842          GV->eraseFromParent();
1843          ++NumDeleted;
1844        } else {
1845          GVI = GV;
1846        }
1847        ++NumSubstitute;
1848        return true;
1849      }
1850
1851    // Try to optimize globals based on the knowledge that only one value
1852    // (besides its initializer) is ever stored to the global.
1853    if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GS.Ordering, GVI,
1854                                 TD, TLI))
1855      return true;
1856
1857    // Otherwise, if the global was not a boolean, we can shrink it to be a
1858    // boolean.
1859    if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue))
1860      if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) {
1861        ++NumShrunkToBool;
1862        return true;
1863      }
1864  }
1865
1866  return false;
1867}
1868
1869/// ChangeCalleesToFastCall - Walk all of the direct calls of the specified
1870/// function, changing them to FastCC.
1871static void ChangeCalleesToFastCall(Function *F) {
1872  for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
1873    CallSite User(cast<Instruction>(*UI));
1874    User.setCallingConv(CallingConv::Fast);
1875  }
1876}
1877
1878static AttrListPtr StripNest(const AttrListPtr &Attrs) {
1879  for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
1880    if ((Attrs.getSlot(i).Attrs & Attribute::Nest) == 0)
1881      continue;
1882
1883    // There can be only one.
1884    return Attrs.removeAttr(Attrs.getSlot(i).Index, Attribute::Nest);
1885  }
1886
1887  return Attrs;
1888}
1889
1890static void RemoveNestAttribute(Function *F) {
1891  F->setAttributes(StripNest(F->getAttributes()));
1892  for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){
1893    CallSite User(cast<Instruction>(*UI));
1894    User.setAttributes(StripNest(User.getAttributes()));
1895  }
1896}
1897
1898bool GlobalOpt::OptimizeFunctions(Module &M) {
1899  bool Changed = false;
1900  // Optimize functions.
1901  for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) {
1902    Function *F = FI++;
1903    // Functions without names cannot be referenced outside this module.
1904    if (!F->hasName() && !F->isDeclaration())
1905      F->setLinkage(GlobalValue::InternalLinkage);
1906    F->removeDeadConstantUsers();
1907    if (F->isDefTriviallyDead()) {
1908      F->eraseFromParent();
1909      Changed = true;
1910      ++NumFnDeleted;
1911    } else if (F->hasLocalLinkage()) {
1912      if (F->getCallingConv() == CallingConv::C && !F->isVarArg() &&
1913          !F->hasAddressTaken()) {
1914        // If this function has C calling conventions, is not a varargs
1915        // function, and is only called directly, promote it to use the Fast
1916        // calling convention.
1917        F->setCallingConv(CallingConv::Fast);
1918        ChangeCalleesToFastCall(F);
1919        ++NumFastCallFns;
1920        Changed = true;
1921      }
1922
1923      if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) &&
1924          !F->hasAddressTaken()) {
1925        // The function is not used by a trampoline intrinsic, so it is safe
1926        // to remove the 'nest' attribute.
1927        RemoveNestAttribute(F);
1928        ++NumNestRemoved;
1929        Changed = true;
1930      }
1931    }
1932  }
1933  return Changed;
1934}
1935
1936bool GlobalOpt::OptimizeGlobalVars(Module &M) {
1937  bool Changed = false;
1938  for (Module::global_iterator GVI = M.global_begin(), E = M.global_end();
1939       GVI != E; ) {
1940    GlobalVariable *GV = GVI++;
1941    // Global variables without names cannot be referenced outside this module.
1942    if (!GV->hasName() && !GV->isDeclaration())
1943      GV->setLinkage(GlobalValue::InternalLinkage);
1944    // Simplify the initializer.
1945    if (GV->hasInitializer())
1946      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) {
1947        Constant *New = ConstantFoldConstantExpression(CE, TD, TLI);
1948        if (New && New != CE)
1949          GV->setInitializer(New);
1950      }
1951
1952    Changed |= ProcessGlobal(GV, GVI);
1953  }
1954  return Changed;
1955}
1956
1957/// FindGlobalCtors - Find the llvm.global_ctors list, verifying that all
1958/// initializers have an init priority of 65535.
1959GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) {
1960  GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1961  if (GV == 0) return 0;
1962
1963  // Verify that the initializer is simple enough for us to handle. We are
1964  // only allowed to optimize the initializer if it is unique.
1965  if (!GV->hasUniqueInitializer()) return 0;
1966
1967  if (isa<ConstantAggregateZero>(GV->getInitializer()))
1968    return GV;
1969  ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1970
1971  for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
1972    if (isa<ConstantAggregateZero>(*i))
1973      continue;
1974    ConstantStruct *CS = cast<ConstantStruct>(*i);
1975    if (isa<ConstantPointerNull>(CS->getOperand(1)))
1976      continue;
1977
1978    // Must have a function or null ptr.
1979    if (!isa<Function>(CS->getOperand(1)))
1980      return 0;
1981
1982    // Init priority must be standard.
1983    ConstantInt *CI = cast<ConstantInt>(CS->getOperand(0));
1984    if (CI->getZExtValue() != 65535)
1985      return 0;
1986  }
1987
1988  return GV;
1989}
1990
1991/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand,
1992/// return a list of the functions and null terminator as a vector.
1993static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) {
1994  if (GV->getInitializer()->isNullValue())
1995    return std::vector<Function*>();
1996  ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1997  std::vector<Function*> Result;
1998  Result.reserve(CA->getNumOperands());
1999  for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) {
2000    ConstantStruct *CS = cast<ConstantStruct>(*i);
2001    Result.push_back(dyn_cast<Function>(CS->getOperand(1)));
2002  }
2003  return Result;
2004}
2005
2006/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the
2007/// specified array, returning the new global to use.
2008static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
2009                                          const std::vector<Function*> &Ctors) {
2010  // If we made a change, reassemble the initializer list.
2011  Constant *CSVals[2];
2012  CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), 65535);
2013  CSVals[1] = 0;
2014
2015  StructType *StructTy =
2016    cast <StructType>(
2017    cast<ArrayType>(GCL->getType()->getElementType())->getElementType());
2018
2019  // Create the new init list.
2020  std::vector<Constant*> CAList;
2021  for (unsigned i = 0, e = Ctors.size(); i != e; ++i) {
2022    if (Ctors[i]) {
2023      CSVals[1] = Ctors[i];
2024    } else {
2025      Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()),
2026                                          false);
2027      PointerType *PFTy = PointerType::getUnqual(FTy);
2028      CSVals[1] = Constant::getNullValue(PFTy);
2029      CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()),
2030                                   0x7fffffff);
2031    }
2032    CAList.push_back(ConstantStruct::get(StructTy, CSVals));
2033  }
2034
2035  // Create the array initializer.
2036  Constant *CA = ConstantArray::get(ArrayType::get(StructTy,
2037                                                   CAList.size()), CAList);
2038
2039  // If we didn't change the number of elements, don't create a new GV.
2040  if (CA->getType() == GCL->getInitializer()->getType()) {
2041    GCL->setInitializer(CA);
2042    return GCL;
2043  }
2044
2045  // Create the new global and insert it next to the existing list.
2046  GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
2047                                           GCL->getLinkage(), CA, "",
2048                                           GCL->isThreadLocal());
2049  GCL->getParent()->getGlobalList().insert(GCL, NGV);
2050  NGV->takeName(GCL);
2051
2052  // Nuke the old list, replacing any uses with the new one.
2053  if (!GCL->use_empty()) {
2054    Constant *V = NGV;
2055    if (V->getType() != GCL->getType())
2056      V = ConstantExpr::getBitCast(V, GCL->getType());
2057    GCL->replaceAllUsesWith(V);
2058  }
2059  GCL->eraseFromParent();
2060
2061  if (Ctors.size())
2062    return NGV;
2063  else
2064    return 0;
2065}
2066
2067
2068static inline bool
2069isSimpleEnoughValueToCommit(Constant *C,
2070                            SmallPtrSet<Constant*, 8> &SimpleConstants,
2071                            const TargetData *TD);
2072
2073
2074/// isSimpleEnoughValueToCommit - Return true if the specified constant can be
2075/// handled by the code generator.  We don't want to generate something like:
2076///   void *X = &X/42;
2077/// because the code generator doesn't have a relocation that can handle that.
2078///
2079/// This function should be called if C was not found (but just got inserted)
2080/// in SimpleConstants to avoid having to rescan the same constants all the
2081/// time.
2082static bool isSimpleEnoughValueToCommitHelper(Constant *C,
2083                                   SmallPtrSet<Constant*, 8> &SimpleConstants,
2084                                   const TargetData *TD) {
2085  // Simple integer, undef, constant aggregate zero, global addresses, etc are
2086  // all supported.
2087  if (C->getNumOperands() == 0 || isa<BlockAddress>(C) ||
2088      isa<GlobalValue>(C))
2089    return true;
2090
2091  // Aggregate values are safe if all their elements are.
2092  if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
2093      isa<ConstantVector>(C)) {
2094    for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
2095      Constant *Op = cast<Constant>(C->getOperand(i));
2096      if (!isSimpleEnoughValueToCommit(Op, SimpleConstants, TD))
2097        return false;
2098    }
2099    return true;
2100  }
2101
2102  // We don't know exactly what relocations are allowed in constant expressions,
2103  // so we allow &global+constantoffset, which is safe and uniformly supported
2104  // across targets.
2105  ConstantExpr *CE = cast<ConstantExpr>(C);
2106  switch (CE->getOpcode()) {
2107  case Instruction::BitCast:
2108    // Bitcast is fine if the casted value is fine.
2109    return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
2110
2111  case Instruction::IntToPtr:
2112  case Instruction::PtrToInt:
2113    // int <=> ptr is fine if the int type is the same size as the
2114    // pointer type.
2115    if (!TD || TD->getTypeSizeInBits(CE->getType()) !=
2116               TD->getTypeSizeInBits(CE->getOperand(0)->getType()))
2117      return false;
2118    return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
2119
2120  // GEP is fine if it is simple + constant offset.
2121  case Instruction::GetElementPtr:
2122    for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
2123      if (!isa<ConstantInt>(CE->getOperand(i)))
2124        return false;
2125    return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
2126
2127  case Instruction::Add:
2128    // We allow simple+cst.
2129    if (!isa<ConstantInt>(CE->getOperand(1)))
2130      return false;
2131    return isSimpleEnoughValueToCommit(CE->getOperand(0), SimpleConstants, TD);
2132  }
2133  return false;
2134}
2135
2136static inline bool
2137isSimpleEnoughValueToCommit(Constant *C,
2138                            SmallPtrSet<Constant*, 8> &SimpleConstants,
2139                            const TargetData *TD) {
2140  // If we already checked this constant, we win.
2141  if (!SimpleConstants.insert(C)) return true;
2142  // Check the constant.
2143  return isSimpleEnoughValueToCommitHelper(C, SimpleConstants, TD);
2144}
2145
2146
2147/// isSimpleEnoughPointerToCommit - Return true if this constant is simple
2148/// enough for us to understand.  In particular, if it is a cast to anything
2149/// other than from one pointer type to another pointer type, we punt.
2150/// We basically just support direct accesses to globals and GEP's of
2151/// globals.  This should be kept up to date with CommitValueTo.
2152static bool isSimpleEnoughPointerToCommit(Constant *C) {
2153  // Conservatively, avoid aggregate types. This is because we don't
2154  // want to worry about them partially overlapping other stores.
2155  if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType())
2156    return false;
2157
2158  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
2159    // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
2160    // external globals.
2161    return GV->hasUniqueInitializer();
2162
2163  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2164    // Handle a constantexpr gep.
2165    if (CE->getOpcode() == Instruction::GetElementPtr &&
2166        isa<GlobalVariable>(CE->getOperand(0)) &&
2167        cast<GEPOperator>(CE)->isInBounds()) {
2168      GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2169      // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
2170      // external globals.
2171      if (!GV->hasUniqueInitializer())
2172        return false;
2173
2174      // The first index must be zero.
2175      ConstantInt *CI = dyn_cast<ConstantInt>(*llvm::next(CE->op_begin()));
2176      if (!CI || !CI->isZero()) return false;
2177
2178      // The remaining indices must be compile-time known integers within the
2179      // notional bounds of the corresponding static array types.
2180      if (!CE->isGEPWithNoNotionalOverIndexing())
2181        return false;
2182
2183      return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
2184
2185    // A constantexpr bitcast from a pointer to another pointer is a no-op,
2186    // and we know how to evaluate it by moving the bitcast from the pointer
2187    // operand to the value operand.
2188    } else if (CE->getOpcode() == Instruction::BitCast &&
2189               isa<GlobalVariable>(CE->getOperand(0))) {
2190      // Do not allow weak/*_odr/linkonce/dllimport/dllexport linkage or
2191      // external globals.
2192      return cast<GlobalVariable>(CE->getOperand(0))->hasUniqueInitializer();
2193    }
2194  }
2195
2196  return false;
2197}
2198
2199/// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global
2200/// initializer.  This returns 'Init' modified to reflect 'Val' stored into it.
2201/// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.
2202static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
2203                                   ConstantExpr *Addr, unsigned OpNo) {
2204  // Base case of the recursion.
2205  if (OpNo == Addr->getNumOperands()) {
2206    assert(Val->getType() == Init->getType() && "Type mismatch!");
2207    return Val;
2208  }
2209
2210  SmallVector<Constant*, 32> Elts;
2211  if (StructType *STy = dyn_cast<StructType>(Init->getType())) {
2212    // Break up the constant into its elements.
2213    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
2214      Elts.push_back(Init->getAggregateElement(i));
2215
2216    // Replace the element that we are supposed to.
2217    ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));
2218    unsigned Idx = CU->getZExtValue();
2219    assert(Idx < STy->getNumElements() && "Struct index out of range!");
2220    Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);
2221
2222    // Return the modified struct.
2223    return ConstantStruct::get(STy, Elts);
2224  }
2225
2226  ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
2227  SequentialType *InitTy = cast<SequentialType>(Init->getType());
2228
2229  uint64_t NumElts;
2230  if (ArrayType *ATy = dyn_cast<ArrayType>(InitTy))
2231    NumElts = ATy->getNumElements();
2232  else
2233    NumElts = InitTy->getVectorNumElements();
2234
2235  // Break up the array into elements.
2236  for (uint64_t i = 0, e = NumElts; i != e; ++i)
2237    Elts.push_back(Init->getAggregateElement(i));
2238
2239  assert(CI->getZExtValue() < NumElts);
2240  Elts[CI->getZExtValue()] =
2241    EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);
2242
2243  if (Init->getType()->isArrayTy())
2244    return ConstantArray::get(cast<ArrayType>(InitTy), Elts);
2245  return ConstantVector::get(Elts);
2246}
2247
2248/// CommitValueTo - We have decided that Addr (which satisfies the predicate
2249/// isSimpleEnoughPointerToCommit) should get Val as its value.  Make it happen.
2250static void CommitValueTo(Constant *Val, Constant *Addr) {
2251  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
2252    assert(GV->hasInitializer());
2253    GV->setInitializer(Val);
2254    return;
2255  }
2256
2257  ConstantExpr *CE = cast<ConstantExpr>(Addr);
2258  GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2259  GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));
2260}
2261
2262namespace {
2263
2264/// Evaluator - This class evaluates LLVM IR, producing the Constant
2265/// representing each SSA instruction.  Changes to global variables are stored
2266/// in a mapping that can be iterated over after the evaluation is complete.
2267/// Once an evaluation call fails, the evaluation object should not be reused.
2268class Evaluator {
2269public:
2270  Evaluator(const TargetData *TD, const TargetLibraryInfo *TLI)
2271    : TD(TD), TLI(TLI) {
2272    ValueStack.push_back(new DenseMap<Value*, Constant*>);
2273  }
2274
2275  ~Evaluator() {
2276    DeleteContainerPointers(ValueStack);
2277    while (!AllocaTmps.empty()) {
2278      GlobalVariable *Tmp = AllocaTmps.back();
2279      AllocaTmps.pop_back();
2280
2281      // If there are still users of the alloca, the program is doing something
2282      // silly, e.g. storing the address of the alloca somewhere and using it
2283      // later.  Since this is undefined, we'll just make it be null.
2284      if (!Tmp->use_empty())
2285        Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType()));
2286      delete Tmp;
2287    }
2288  }
2289
2290  /// EvaluateFunction - Evaluate a call to function F, returning true if
2291  /// successful, false if we can't evaluate it.  ActualArgs contains the formal
2292  /// arguments for the function.
2293  bool EvaluateFunction(Function *F, Constant *&RetVal,
2294                        const SmallVectorImpl<Constant*> &ActualArgs);
2295
2296  /// EvaluateBlock - Evaluate all instructions in block BB, returning true if
2297  /// successful, false if we can't evaluate it.  NewBB returns the next BB that
2298  /// control flows into, or null upon return.
2299  bool EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB);
2300
2301  Constant *getVal(Value *V) {
2302    if (Constant *CV = dyn_cast<Constant>(V)) return CV;
2303    Constant *R = ValueStack.back()->lookup(V);
2304    assert(R && "Reference to an uncomputed value!");
2305    return R;
2306  }
2307
2308  void setVal(Value *V, Constant *C) {
2309    ValueStack.back()->operator[](V) = C;
2310  }
2311
2312  const DenseMap<Constant*, Constant*> &getMutatedMemory() const {
2313    return MutatedMemory;
2314  }
2315
2316  const SmallPtrSet<GlobalVariable*, 8> &getInvariants() const {
2317    return Invariants;
2318  }
2319
2320private:
2321  Constant *ComputeLoadResult(Constant *P);
2322
2323  /// ValueStack - As we compute SSA register values, we store their contents
2324  /// here. The back of the vector contains the current function and the stack
2325  /// contains the values in the calling frames.
2326  SmallVector<DenseMap<Value*, Constant*>*, 4> ValueStack;
2327
2328  /// CallStack - This is used to detect recursion.  In pathological situations
2329  /// we could hit exponential behavior, but at least there is nothing
2330  /// unbounded.
2331  SmallVector<Function*, 4> CallStack;
2332
2333  /// MutatedMemory - For each store we execute, we update this map.  Loads
2334  /// check this to get the most up-to-date value.  If evaluation is successful,
2335  /// this state is committed to the process.
2336  DenseMap<Constant*, Constant*> MutatedMemory;
2337
2338  /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable
2339  /// to represent its body.  This vector is needed so we can delete the
2340  /// temporary globals when we are done.
2341  SmallVector<GlobalVariable*, 32> AllocaTmps;
2342
2343  /// Invariants - These global variables have been marked invariant by the
2344  /// static constructor.
2345  SmallPtrSet<GlobalVariable*, 8> Invariants;
2346
2347  /// SimpleConstants - These are constants we have checked and know to be
2348  /// simple enough to live in a static initializer of a global.
2349  SmallPtrSet<Constant*, 8> SimpleConstants;
2350
2351  const TargetData *TD;
2352  const TargetLibraryInfo *TLI;
2353};
2354
2355}  // anonymous namespace
2356
2357/// ComputeLoadResult - Return the value that would be computed by a load from
2358/// P after the stores reflected by 'memory' have been performed.  If we can't
2359/// decide, return null.
2360Constant *Evaluator::ComputeLoadResult(Constant *P) {
2361  // If this memory location has been recently stored, use the stored value: it
2362  // is the most up-to-date.
2363  DenseMap<Constant*, Constant*>::const_iterator I = MutatedMemory.find(P);
2364  if (I != MutatedMemory.end()) return I->second;
2365
2366  // Access it.
2367  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) {
2368    if (GV->hasDefinitiveInitializer())
2369      return GV->getInitializer();
2370    return 0;
2371  }
2372
2373  // Handle a constantexpr getelementptr.
2374  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P))
2375    if (CE->getOpcode() == Instruction::GetElementPtr &&
2376        isa<GlobalVariable>(CE->getOperand(0))) {
2377      GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));
2378      if (GV->hasDefinitiveInitializer())
2379        return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE);
2380    }
2381
2382  return 0;  // don't know how to evaluate.
2383}
2384
2385/// EvaluateBlock - Evaluate all instructions in block BB, returning true if
2386/// successful, false if we can't evaluate it.  NewBB returns the next BB that
2387/// control flows into, or null upon return.
2388bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst,
2389                              BasicBlock *&NextBB) {
2390  // This is the main evaluation loop.
2391  while (1) {
2392    Constant *InstResult = 0;
2393
2394    if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) {
2395      if (!SI->isSimple()) return false;  // no volatile/atomic accesses.
2396      Constant *Ptr = getVal(SI->getOperand(1));
2397      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2398        Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
2399      if (!isSimpleEnoughPointerToCommit(Ptr))
2400        // If this is too complex for us to commit, reject it.
2401        return false;
2402
2403      Constant *Val = getVal(SI->getOperand(0));
2404
2405      // If this might be too difficult for the backend to handle (e.g. the addr
2406      // of one global variable divided by another) then we can't commit it.
2407      if (!isSimpleEnoughValueToCommit(Val, SimpleConstants, TD))
2408        return false;
2409
2410      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2411        if (CE->getOpcode() == Instruction::BitCast) {
2412          // If we're evaluating a store through a bitcast, then we need
2413          // to pull the bitcast off the pointer type and push it onto the
2414          // stored value.
2415          Ptr = CE->getOperand(0);
2416
2417          Type *NewTy = cast<PointerType>(Ptr->getType())->getElementType();
2418
2419          // In order to push the bitcast onto the stored value, a bitcast
2420          // from NewTy to Val's type must be legal.  If it's not, we can try
2421          // introspecting NewTy to find a legal conversion.
2422          while (!Val->getType()->canLosslesslyBitCastTo(NewTy)) {
2423            // If NewTy is a struct, we can convert the pointer to the struct
2424            // into a pointer to its first member.
2425            // FIXME: This could be extended to support arrays as well.
2426            if (StructType *STy = dyn_cast<StructType>(NewTy)) {
2427              NewTy = STy->getTypeAtIndex(0U);
2428
2429              IntegerType *IdxTy = IntegerType::get(NewTy->getContext(), 32);
2430              Constant *IdxZero = ConstantInt::get(IdxTy, 0, false);
2431              Constant * const IdxList[] = {IdxZero, IdxZero};
2432
2433              Ptr = ConstantExpr::getGetElementPtr(Ptr, IdxList);
2434              if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2435                Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
2436
2437            // If we can't improve the situation by introspecting NewTy,
2438            // we have to give up.
2439            } else {
2440              return false;
2441            }
2442          }
2443
2444          // If we found compatible types, go ahead and push the bitcast
2445          // onto the stored value.
2446          Val = ConstantExpr::getBitCast(Val, NewTy);
2447        }
2448
2449      MutatedMemory[Ptr] = Val;
2450    } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) {
2451      InstResult = ConstantExpr::get(BO->getOpcode(),
2452                                     getVal(BO->getOperand(0)),
2453                                     getVal(BO->getOperand(1)));
2454    } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) {
2455      InstResult = ConstantExpr::getCompare(CI->getPredicate(),
2456                                            getVal(CI->getOperand(0)),
2457                                            getVal(CI->getOperand(1)));
2458    } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) {
2459      InstResult = ConstantExpr::getCast(CI->getOpcode(),
2460                                         getVal(CI->getOperand(0)),
2461                                         CI->getType());
2462    } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) {
2463      InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)),
2464                                           getVal(SI->getOperand(1)),
2465                                           getVal(SI->getOperand(2)));
2466    } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) {
2467      Constant *P = getVal(GEP->getOperand(0));
2468      SmallVector<Constant*, 8> GEPOps;
2469      for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
2470           i != e; ++i)
2471        GEPOps.push_back(getVal(*i));
2472      InstResult =
2473        ConstantExpr::getGetElementPtr(P, GEPOps,
2474                                       cast<GEPOperator>(GEP)->isInBounds());
2475    } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) {
2476      if (!LI->isSimple()) return false;  // no volatile/atomic accesses.
2477      Constant *Ptr = getVal(LI->getOperand(0));
2478      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
2479        Ptr = ConstantFoldConstantExpression(CE, TD, TLI);
2480      InstResult = ComputeLoadResult(Ptr);
2481      if (InstResult == 0) return false; // Could not evaluate load.
2482    } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) {
2483      if (AI->isArrayAllocation()) return false;  // Cannot handle array allocs.
2484      Type *Ty = AI->getType()->getElementType();
2485      AllocaTmps.push_back(new GlobalVariable(Ty, false,
2486                                              GlobalValue::InternalLinkage,
2487                                              UndefValue::get(Ty),
2488                                              AI->getName()));
2489      InstResult = AllocaTmps.back();
2490    } else if (isa<CallInst>(CurInst) || isa<InvokeInst>(CurInst)) {
2491      CallSite CS(CurInst);
2492
2493      // Debug info can safely be ignored here.
2494      if (isa<DbgInfoIntrinsic>(CS.getInstruction())) {
2495        ++CurInst;
2496        continue;
2497      }
2498
2499      // Cannot handle inline asm.
2500      if (isa<InlineAsm>(CS.getCalledValue())) return false;
2501
2502      if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
2503        if (MemSetInst *MSI = dyn_cast<MemSetInst>(II)) {
2504          if (MSI->isVolatile()) return false;
2505          Constant *Ptr = getVal(MSI->getDest());
2506          Constant *Val = getVal(MSI->getValue());
2507          Constant *DestVal = ComputeLoadResult(getVal(Ptr));
2508          if (Val->isNullValue() && DestVal && DestVal->isNullValue()) {
2509            // This memset is a no-op.
2510            ++CurInst;
2511            continue;
2512          }
2513        }
2514
2515        if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
2516            II->getIntrinsicID() == Intrinsic::lifetime_end) {
2517          ++CurInst;
2518          continue;
2519        }
2520
2521        if (II->getIntrinsicID() == Intrinsic::invariant_start) {
2522          // We don't insert an entry into Values, as it doesn't have a
2523          // meaningful return value.
2524          if (!II->use_empty())
2525            return false;
2526          ConstantInt *Size = cast<ConstantInt>(II->getArgOperand(0));
2527          Value *PtrArg = getVal(II->getArgOperand(1));
2528          Value *Ptr = PtrArg->stripPointerCasts();
2529          if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
2530            Type *ElemTy = cast<PointerType>(GV->getType())->getElementType();
2531            if (!Size->isAllOnesValue() &&
2532                Size->getValue().getLimitedValue() >=
2533                TD->getTypeStoreSize(ElemTy))
2534              Invariants.insert(GV);
2535          }
2536          // Continue even if we do nothing.
2537          ++CurInst;
2538          continue;
2539        }
2540        return false;
2541      }
2542
2543      // Resolve function pointers.
2544      Function *Callee = dyn_cast<Function>(getVal(CS.getCalledValue()));
2545      if (!Callee || Callee->mayBeOverridden())
2546        return false;  // Cannot resolve.
2547
2548      SmallVector<Constant*, 8> Formals;
2549      for (User::op_iterator i = CS.arg_begin(), e = CS.arg_end(); i != e; ++i)
2550        Formals.push_back(getVal(*i));
2551
2552      if (Callee->isDeclaration()) {
2553        // If this is a function we can constant fold, do it.
2554        if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
2555          InstResult = C;
2556        } else {
2557          return false;
2558        }
2559      } else {
2560        if (Callee->getFunctionType()->isVarArg())
2561          return false;
2562
2563        Constant *RetVal;
2564        // Execute the call, if successful, use the return value.
2565        ValueStack.push_back(new DenseMap<Value*, Constant*>);
2566        if (!EvaluateFunction(Callee, RetVal, Formals))
2567          return false;
2568        delete ValueStack.pop_back_val();
2569        InstResult = RetVal;
2570      }
2571    } else if (isa<TerminatorInst>(CurInst)) {
2572      if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) {
2573        if (BI->isUnconditional()) {
2574          NextBB = BI->getSuccessor(0);
2575        } else {
2576          ConstantInt *Cond =
2577            dyn_cast<ConstantInt>(getVal(BI->getCondition()));
2578          if (!Cond) return false;  // Cannot determine.
2579
2580          NextBB = BI->getSuccessor(!Cond->getZExtValue());
2581        }
2582      } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) {
2583        ConstantInt *Val =
2584          dyn_cast<ConstantInt>(getVal(SI->getCondition()));
2585        if (!Val) return false;  // Cannot determine.
2586        NextBB = SI->findCaseValue(Val).getCaseSuccessor();
2587      } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) {
2588        Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
2589        if (BlockAddress *BA = dyn_cast<BlockAddress>(Val))
2590          NextBB = BA->getBasicBlock();
2591        else
2592          return false;  // Cannot determine.
2593      } else if (isa<ReturnInst>(CurInst)) {
2594        NextBB = 0;
2595      } else {
2596        // invoke, unwind, resume, unreachable.
2597        return false;  // Cannot handle this terminator.
2598      }
2599
2600      // We succeeded at evaluating this block!
2601      return true;
2602    } else {
2603      // Did not know how to evaluate this!
2604      return false;
2605    }
2606
2607    if (!CurInst->use_empty()) {
2608      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InstResult))
2609        InstResult = ConstantFoldConstantExpression(CE, TD, TLI);
2610
2611      setVal(CurInst, InstResult);
2612    }
2613
2614    // If we just processed an invoke, we finished evaluating the block.
2615    if (InvokeInst *II = dyn_cast<InvokeInst>(CurInst)) {
2616      NextBB = II->getNormalDest();
2617      return true;
2618    }
2619
2620    // Advance program counter.
2621    ++CurInst;
2622  }
2623}
2624
2625/// EvaluateFunction - Evaluate a call to function F, returning true if
2626/// successful, false if we can't evaluate it.  ActualArgs contains the formal
2627/// arguments for the function.
2628bool Evaluator::EvaluateFunction(Function *F, Constant *&RetVal,
2629                                 const SmallVectorImpl<Constant*> &ActualArgs) {
2630  // Check to see if this function is already executing (recursion).  If so,
2631  // bail out.  TODO: we might want to accept limited recursion.
2632  if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end())
2633    return false;
2634
2635  CallStack.push_back(F);
2636
2637  // Initialize arguments to the incoming values specified.
2638  unsigned ArgNo = 0;
2639  for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E;
2640       ++AI, ++ArgNo)
2641    setVal(AI, ActualArgs[ArgNo]);
2642
2643  // ExecutedBlocks - We only handle non-looping, non-recursive code.  As such,
2644  // we can only evaluate any one basic block at most once.  This set keeps
2645  // track of what we have executed so we can detect recursive cases etc.
2646  SmallPtrSet<BasicBlock*, 32> ExecutedBlocks;
2647
2648  // CurBB - The current basic block we're evaluating.
2649  BasicBlock *CurBB = F->begin();
2650
2651  BasicBlock::iterator CurInst = CurBB->begin();
2652
2653  while (1) {
2654    BasicBlock *NextBB = 0; // Initialized to avoid compiler warnings.
2655    if (!EvaluateBlock(CurInst, NextBB))
2656      return false;
2657
2658    if (NextBB == 0) {
2659      // Successfully running until there's no next block means that we found
2660      // the return.  Fill it the return value and pop the call stack.
2661      ReturnInst *RI = cast<ReturnInst>(CurBB->getTerminator());
2662      if (RI->getNumOperands())
2663        RetVal = getVal(RI->getOperand(0));
2664      CallStack.pop_back();
2665      return true;
2666    }
2667
2668    // Okay, we succeeded in evaluating this control flow.  See if we have
2669    // executed the new block before.  If so, we have a looping function,
2670    // which we cannot evaluate in reasonable time.
2671    if (!ExecutedBlocks.insert(NextBB))
2672      return false;  // looped!
2673
2674    // Okay, we have never been in this block before.  Check to see if there
2675    // are any PHI nodes.  If so, evaluate them with information about where
2676    // we came from.
2677    PHINode *PN = 0;
2678    for (CurInst = NextBB->begin();
2679         (PN = dyn_cast<PHINode>(CurInst)); ++CurInst)
2680      setVal(PN, getVal(PN->getIncomingValueForBlock(CurBB)));
2681
2682    // Advance to the next block.
2683    CurBB = NextBB;
2684  }
2685}
2686
2687/// EvaluateStaticConstructor - Evaluate static constructors in the function, if
2688/// we can.  Return true if we can, false otherwise.
2689static bool EvaluateStaticConstructor(Function *F, const TargetData *TD,
2690                                      const TargetLibraryInfo *TLI) {
2691  // Call the function.
2692  Evaluator Eval(TD, TLI);
2693  Constant *RetValDummy;
2694  bool EvalSuccess = Eval.EvaluateFunction(F, RetValDummy,
2695                                           SmallVector<Constant*, 0>());
2696
2697  if (EvalSuccess) {
2698    // We succeeded at evaluation: commit the result.
2699    DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '"
2700          << F->getName() << "' to " << Eval.getMutatedMemory().size()
2701          << " stores.\n");
2702    for (DenseMap<Constant*, Constant*>::const_iterator I =
2703           Eval.getMutatedMemory().begin(), E = Eval.getMutatedMemory().end();
2704	 I != E; ++I)
2705      CommitValueTo(I->second, I->first);
2706    for (SmallPtrSet<GlobalVariable*, 8>::const_iterator I =
2707           Eval.getInvariants().begin(), E = Eval.getInvariants().end();
2708         I != E; ++I)
2709      (*I)->setConstant(true);
2710  }
2711
2712  return EvalSuccess;
2713}
2714
2715/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible.
2716/// Return true if anything changed.
2717bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) {
2718  std::vector<Function*> Ctors = ParseGlobalCtors(GCL);
2719  bool MadeChange = false;
2720  if (Ctors.empty()) return false;
2721
2722  // Loop over global ctors, optimizing them when we can.
2723  for (unsigned i = 0; i != Ctors.size(); ++i) {
2724    Function *F = Ctors[i];
2725    // Found a null terminator in the middle of the list, prune off the rest of
2726    // the list.
2727    if (F == 0) {
2728      if (i != Ctors.size()-1) {
2729        Ctors.resize(i+1);
2730        MadeChange = true;
2731      }
2732      break;
2733    }
2734
2735    // We cannot simplify external ctor functions.
2736    if (F->empty()) continue;
2737
2738    // If we can evaluate the ctor at compile time, do.
2739    if (EvaluateStaticConstructor(F, TD, TLI)) {
2740      Ctors.erase(Ctors.begin()+i);
2741      MadeChange = true;
2742      --i;
2743      ++NumCtorsEvaluated;
2744      continue;
2745    }
2746  }
2747
2748  if (!MadeChange) return false;
2749
2750  GCL = InstallGlobalCtors(GCL, Ctors);
2751  return true;
2752}
2753
2754bool GlobalOpt::OptimizeGlobalAliases(Module &M) {
2755  bool Changed = false;
2756
2757  for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
2758       I != E;) {
2759    Module::alias_iterator J = I++;
2760    // Aliases without names cannot be referenced outside this module.
2761    if (!J->hasName() && !J->isDeclaration())
2762      J->setLinkage(GlobalValue::InternalLinkage);
2763    // If the aliasee may change at link time, nothing can be done - bail out.
2764    if (J->mayBeOverridden())
2765      continue;
2766
2767    Constant *Aliasee = J->getAliasee();
2768    GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts());
2769    Target->removeDeadConstantUsers();
2770    bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse();
2771
2772    // Make all users of the alias use the aliasee instead.
2773    if (!J->use_empty()) {
2774      J->replaceAllUsesWith(Aliasee);
2775      ++NumAliasesResolved;
2776      Changed = true;
2777    }
2778
2779    // If the alias is externally visible, we may still be able to simplify it.
2780    if (!J->hasLocalLinkage()) {
2781      // If the aliasee has internal linkage, give it the name and linkage
2782      // of the alias, and delete the alias.  This turns:
2783      //   define internal ... @f(...)
2784      //   @a = alias ... @f
2785      // into:
2786      //   define ... @a(...)
2787      if (!Target->hasLocalLinkage())
2788        continue;
2789
2790      // Do not perform the transform if multiple aliases potentially target the
2791      // aliasee. This check also ensures that it is safe to replace the section
2792      // and other attributes of the aliasee with those of the alias.
2793      if (!hasOneUse)
2794        continue;
2795
2796      // Give the aliasee the name, linkage and other attributes of the alias.
2797      Target->takeName(J);
2798      Target->setLinkage(J->getLinkage());
2799      Target->GlobalValue::copyAttributesFrom(J);
2800    }
2801
2802    // Delete the alias.
2803    M.getAliasList().erase(J);
2804    ++NumAliasesRemoved;
2805    Changed = true;
2806  }
2807
2808  return Changed;
2809}
2810
2811static Function *FindCXAAtExit(Module &M, TargetLibraryInfo *TLI) {
2812  if (!TLI->has(LibFunc::cxa_atexit))
2813    return 0;
2814
2815  Function *Fn = M.getFunction(TLI->getName(LibFunc::cxa_atexit));
2816
2817  if (!Fn)
2818    return 0;
2819
2820  FunctionType *FTy = Fn->getFunctionType();
2821
2822  // Checking that the function has the right return type, the right number of
2823  // parameters and that they all have pointer types should be enough.
2824  if (!FTy->getReturnType()->isIntegerTy() ||
2825      FTy->getNumParams() != 3 ||
2826      !FTy->getParamType(0)->isPointerTy() ||
2827      !FTy->getParamType(1)->isPointerTy() ||
2828      !FTy->getParamType(2)->isPointerTy())
2829    return 0;
2830
2831  return Fn;
2832}
2833
2834/// cxxDtorIsEmpty - Returns whether the given function is an empty C++
2835/// destructor and can therefore be eliminated.
2836/// Note that we assume that other optimization passes have already simplified
2837/// the code so we only look for a function with a single basic block, where
2838/// the only allowed instructions are 'ret', 'call' to an empty C++ dtor and
2839/// other side-effect free instructions.
2840static bool cxxDtorIsEmpty(const Function &Fn,
2841                           SmallPtrSet<const Function *, 8> &CalledFunctions) {
2842  // FIXME: We could eliminate C++ destructors if they're readonly/readnone and
2843  // nounwind, but that doesn't seem worth doing.
2844  if (Fn.isDeclaration())
2845    return false;
2846
2847  if (++Fn.begin() != Fn.end())
2848    return false;
2849
2850  const BasicBlock &EntryBlock = Fn.getEntryBlock();
2851  for (BasicBlock::const_iterator I = EntryBlock.begin(), E = EntryBlock.end();
2852       I != E; ++I) {
2853    if (const CallInst *CI = dyn_cast<CallInst>(I)) {
2854      // Ignore debug intrinsics.
2855      if (isa<DbgInfoIntrinsic>(CI))
2856        continue;
2857
2858      const Function *CalledFn = CI->getCalledFunction();
2859
2860      if (!CalledFn)
2861        return false;
2862
2863      SmallPtrSet<const Function *, 8> NewCalledFunctions(CalledFunctions);
2864
2865      // Don't treat recursive functions as empty.
2866      if (!NewCalledFunctions.insert(CalledFn))
2867        return false;
2868
2869      if (!cxxDtorIsEmpty(*CalledFn, NewCalledFunctions))
2870        return false;
2871    } else if (isa<ReturnInst>(*I))
2872      return true; // We're done.
2873    else if (I->mayHaveSideEffects())
2874      return false; // Destructor with side effects, bail.
2875  }
2876
2877  return false;
2878}
2879
2880bool GlobalOpt::OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
2881  /// Itanium C++ ABI p3.3.5:
2882  ///
2883  ///   After constructing a global (or local static) object, that will require
2884  ///   destruction on exit, a termination function is registered as follows:
2885  ///
2886  ///   extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d );
2887  ///
2888  ///   This registration, e.g. __cxa_atexit(f,p,d), is intended to cause the
2889  ///   call f(p) when DSO d is unloaded, before all such termination calls
2890  ///   registered before this one. It returns zero if registration is
2891  ///   successful, nonzero on failure.
2892
2893  // This pass will look for calls to __cxa_atexit where the function is trivial
2894  // and remove them.
2895  bool Changed = false;
2896
2897  for (Function::use_iterator I = CXAAtExitFn->use_begin(),
2898       E = CXAAtExitFn->use_end(); I != E;) {
2899    // We're only interested in calls. Theoretically, we could handle invoke
2900    // instructions as well, but neither llvm-gcc nor clang generate invokes
2901    // to __cxa_atexit.
2902    CallInst *CI = dyn_cast<CallInst>(*I++);
2903    if (!CI)
2904      continue;
2905
2906    Function *DtorFn =
2907      dyn_cast<Function>(CI->getArgOperand(0)->stripPointerCasts());
2908    if (!DtorFn)
2909      continue;
2910
2911    SmallPtrSet<const Function *, 8> CalledFunctions;
2912    if (!cxxDtorIsEmpty(*DtorFn, CalledFunctions))
2913      continue;
2914
2915    // Just remove the call.
2916    CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
2917    CI->eraseFromParent();
2918
2919    ++NumCXXDtorsRemoved;
2920
2921    Changed |= true;
2922  }
2923
2924  return Changed;
2925}
2926
2927bool GlobalOpt::runOnModule(Module &M) {
2928  bool Changed = false;
2929
2930  TD = getAnalysisIfAvailable<TargetData>();
2931  TLI = &getAnalysis<TargetLibraryInfo>();
2932
2933  // Try to find the llvm.globalctors list.
2934  GlobalVariable *GlobalCtors = FindGlobalCtors(M);
2935
2936  Function *CXAAtExitFn = FindCXAAtExit(M, TLI);
2937
2938  bool LocalChange = true;
2939  while (LocalChange) {
2940    LocalChange = false;
2941
2942    // Delete functions that are trivially dead, ccc -> fastcc
2943    LocalChange |= OptimizeFunctions(M);
2944
2945    // Optimize global_ctors list.
2946    if (GlobalCtors)
2947      LocalChange |= OptimizeGlobalCtorsList(GlobalCtors);
2948
2949    // Optimize non-address-taken globals.
2950    LocalChange |= OptimizeGlobalVars(M);
2951
2952    // Resolve aliases, when possible.
2953    LocalChange |= OptimizeGlobalAliases(M);
2954
2955    // Try to remove trivial global destructors.
2956    if (CXAAtExitFn)
2957      LocalChange |= OptimizeEmptyGlobalCXXDtors(CXAAtExitFn);
2958
2959    Changed |= LocalChange;
2960  }
2961
2962  // TODO: Move all global ctors functions to the end of the module for code
2963  // layout.
2964
2965  return Changed;
2966}
2967