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