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