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" |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 18 | #include "llvm/CallingConv.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7d90a27 | 2004-02-27 18:09:25 +0000 | [diff] [blame] | 21 | #include "llvm/Instructions.h" |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 22 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/ConstantFolding.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 79066fa | 2007-01-30 23:46:24 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 31 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 39 | #include <algorithm> |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 42 | STATISTIC(NumMarked , "Number of globals marked constant"); |
| 43 | STATISTIC(NumSRA , "Number of aggregate globals broken into scalars"); |
| 44 | STATISTIC(NumHeapSRA , "Number of heap objects SRA'd"); |
| 45 | STATISTIC(NumSubstitute,"Number of globals with initializers stored into them"); |
| 46 | STATISTIC(NumDeleted , "Number of globals deleted"); |
| 47 | STATISTIC(NumFnDeleted , "Number of functions deleted"); |
| 48 | STATISTIC(NumGlobUses , "Number of global uses devirtualized"); |
| 49 | STATISTIC(NumLocalized , "Number of globals localized"); |
| 50 | STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans"); |
| 51 | STATISTIC(NumFastCallFns , "Number of functions converted to fastcc"); |
| 52 | STATISTIC(NumCtorsEvaluated, "Number of static ctors evaluated"); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 53 | STATISTIC(NumNestRemoved , "Number of nest attributes removed"); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 54 | STATISTIC(NumAliasesResolved, "Number of global aliases resolved"); |
| 55 | STATISTIC(NumAliasesRemoved, "Number of global aliases eliminated"); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 57 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 58 | struct GlobalOpt : public ModulePass { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 59 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 60 | } |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 61 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 62 | GlobalOpt() : ModulePass(&ID) {} |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 63 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 64 | bool runOnModule(Module &M); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 65 | |
| 66 | private: |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 67 | GlobalVariable *FindGlobalCtors(Module &M); |
| 68 | bool OptimizeFunctions(Module &M); |
| 69 | bool OptimizeGlobalVars(Module &M); |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 70 | bool OptimizeGlobalAliases(Module &M); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 71 | bool OptimizeGlobalCtorsList(GlobalVariable *&GCL); |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 72 | bool ProcessInternalGlobal(GlobalVariable *GV,Module::global_iterator &GVI); |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 73 | }; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 76 | char GlobalOpt::ID = 0; |
| 77 | static RegisterPass<GlobalOpt> X("globalopt", "Global Variable Optimizer"); |
| 78 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 79 | ModulePass *llvm::createGlobalOptimizerPass() { return new GlobalOpt(); } |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 80 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 81 | namespace { |
| 82 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 83 | /// GlobalStatus - As we analyze each global, keep track of some information |
| 84 | /// 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] | 85 | /// this info will be accurate. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 86 | struct GlobalStatus { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 87 | /// isLoaded - True if the global is ever loaded. If the global isn't ever |
| 88 | /// loaded it can be deleted. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 89 | bool isLoaded; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 90 | |
| 91 | /// StoredType - Keep track of what stores to the global look like. |
| 92 | /// |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 93 | enum StoredType { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 94 | /// NotStored - There is no store to this global. It can thus be marked |
| 95 | /// constant. |
| 96 | NotStored, |
| 97 | |
| 98 | /// isInitializerStored - This global is stored to, but the only thing |
| 99 | /// stored is the constant it was initialized with. This is only tracked |
| 100 | /// for scalar globals. |
| 101 | isInitializerStored, |
| 102 | |
| 103 | /// isStoredOnce - This global is stored to, but only its initializer and |
| 104 | /// one other value is ever stored to it. If this global isStoredOnce, we |
| 105 | /// track the value stored to it in StoredOnceValue below. This is only |
| 106 | /// tracked for scalar globals. |
| 107 | isStoredOnce, |
| 108 | |
| 109 | /// isStored - This global is stored to by multiple values or something else |
| 110 | /// that we cannot track. |
| 111 | isStored |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 112 | } StoredType; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 113 | |
| 114 | /// StoredOnceValue - If only one value (besides the initializer constant) is |
| 115 | /// ever stored to this global, keep track of what value it is. |
| 116 | Value *StoredOnceValue; |
| 117 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 118 | /// AccessingFunction/HasMultipleAccessingFunctions - These start out |
| 119 | /// null/false. When the first accessing function is noticed, it is recorded. |
| 120 | /// When a second different accessing function is noticed, |
| 121 | /// HasMultipleAccessingFunctions is set to true. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 122 | const Function *AccessingFunction; |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 123 | bool HasMultipleAccessingFunctions; |
| 124 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 125 | /// HasNonInstructionUser - Set to true if this global has a user that is not |
| 126 | /// an instruction (e.g. a constant expr or GV initializer). |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 127 | bool HasNonInstructionUser; |
| 128 | |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 129 | /// HasPHIUser - Set to true if this global has a user that is a PHI node. |
| 130 | bool HasPHIUser; |
| 131 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 132 | GlobalStatus() : isLoaded(false), StoredType(NotStored), StoredOnceValue(0), |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 133 | AccessingFunction(0), HasMultipleAccessingFunctions(false), |
Chris Lattner | 6a93fc0 | 2008-01-14 01:32:52 +0000 | [diff] [blame] | 134 | HasNonInstructionUser(false), HasPHIUser(false) {} |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 135 | }; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 136 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 138 | |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 139 | // SafeToDestroyConstant - It is safe to destroy a constant iff it is only used |
| 140 | // by constants itself. Note that constants cannot be cyclic, so this test is |
| 141 | // pretty easy to implement recursively. |
| 142 | // |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 143 | static bool SafeToDestroyConstant(const Constant *C) { |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 144 | if (isa<GlobalValue>(C)) return false; |
| 145 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 146 | for (Value::const_use_iterator UI = C->use_begin(), E = C->use_end(); UI != E; ++UI) |
| 147 | if (const Constant *CU = dyn_cast<Constant>(*UI)) { |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 148 | if (!SafeToDestroyConstant(CU)) return false; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 149 | } else |
| 150 | return false; |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 155 | /// AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus |
| 156 | /// structure. If the global has its address taken, return true to indicate we |
| 157 | /// can't do anything with it. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 158 | /// |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 159 | static bool AnalyzeGlobal(const Value *V, GlobalStatus &GS, |
| 160 | SmallPtrSet<const PHINode*, 16> &PHIUsers) { |
| 161 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 162 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(*UI)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 163 | GS.HasNonInstructionUser = true; |
| 164 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 165 | if (AnalyzeGlobal(CE, GS, PHIUsers)) return true; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 166 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 167 | } else if (const Instruction *I = dyn_cast<Instruction>(*UI)) { |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 168 | if (!GS.HasMultipleAccessingFunctions) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 169 | const Function *F = I->getParent()->getParent(); |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 170 | if (GS.AccessingFunction == 0) |
| 171 | GS.AccessingFunction = F; |
| 172 | else if (GS.AccessingFunction != F) |
| 173 | GS.HasMultipleAccessingFunctions = true; |
| 174 | } |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 175 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 176 | GS.isLoaded = true; |
Chris Lattner | c69d3c9 | 2008-01-29 19:01:37 +0000 | [diff] [blame] | 177 | if (LI->isVolatile()) return true; // Don't hack on volatile loads. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 178 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | 3602549 | 2004-10-07 06:01:25 +0000 | [diff] [blame] | 179 | // Don't allow a store OF the address, only stores TO the address. |
| 180 | if (SI->getOperand(0) == V) return true; |
| 181 | |
Chris Lattner | c69d3c9 | 2008-01-29 19:01:37 +0000 | [diff] [blame] | 182 | if (SI->isVolatile()) return true; // Don't hack on volatile stores. |
| 183 | |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 184 | // If this is a direct store to the global (i.e., the global is a scalar |
| 185 | // value, not an aggregate), keep more specific information about |
| 186 | // stores. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 187 | if (GS.StoredType != GlobalStatus::isStored) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 188 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(SI->getOperand(1))){ |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 189 | Value *StoredVal = SI->getOperand(0); |
| 190 | if (StoredVal == GV->getInitializer()) { |
| 191 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 192 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 193 | } else if (isa<LoadInst>(StoredVal) && |
| 194 | cast<LoadInst>(StoredVal)->getOperand(0) == GV) { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 195 | if (GS.StoredType < GlobalStatus::isInitializerStored) |
| 196 | GS.StoredType = GlobalStatus::isInitializerStored; |
| 197 | } else if (GS.StoredType < GlobalStatus::isStoredOnce) { |
| 198 | GS.StoredType = GlobalStatus::isStoredOnce; |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 199 | GS.StoredOnceValue = StoredVal; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 200 | } else if (GS.StoredType == GlobalStatus::isStoredOnce && |
Chris Lattner | bd38edf | 2004-11-14 20:50:30 +0000 | [diff] [blame] | 201 | GS.StoredOnceValue == StoredVal) { |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 202 | // noop. |
| 203 | } else { |
| 204 | GS.StoredType = GlobalStatus::isStored; |
| 205 | } |
| 206 | } else { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 207 | GS.StoredType = GlobalStatus::isStored; |
Chris Lattner | cf4d2a5 | 2004-10-07 21:30:30 +0000 | [diff] [blame] | 208 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 210 | } else if (isa<GetElementPtrInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 211 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 212 | } else if (isa<SelectInst>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 213 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 214 | } else if (const PHINode *PN = dyn_cast<PHINode>(I)) { |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 215 | // PHI nodes we can check just like select or GEP instructions, but we |
| 216 | // have to be careful about infinite recursion. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 217 | if (PHIUsers.insert(PN)) // Not already visited. |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 218 | if (AnalyzeGlobal(I, GS, PHIUsers)) return true; |
Chris Lattner | 25de4e5 | 2006-11-01 18:03:33 +0000 | [diff] [blame] | 219 | GS.HasPHIUser = true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 220 | } else if (isa<CmpInst>(I)) { |
Chris Lattner | 8e10844 | 2009-03-08 03:37:35 +0000 | [diff] [blame] | 221 | } else if (isa<MemTransferInst>(I)) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 222 | if (I->getOperand(1) == V) |
| 223 | GS.StoredType = GlobalStatus::isStored; |
| 224 | if (I->getOperand(2) == V) |
| 225 | GS.isLoaded = true; |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 226 | } else if (isa<MemSetInst>(I)) { |
| 227 | assert(I->getOperand(1) == V && "Memset only takes one pointer!"); |
| 228 | GS.StoredType = GlobalStatus::isStored; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 229 | } else { |
| 230 | return true; // Any other non-load instruction might take address! |
Chris Lattner | 9ce3000 | 2004-07-20 03:58:07 +0000 | [diff] [blame] | 231 | } |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 232 | } else if (const Constant *C = dyn_cast<Constant>(*UI)) { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 233 | GS.HasNonInstructionUser = true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 234 | // We might have a dead and dangling constant hanging off of here. |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 235 | if (!SafeToDestroyConstant(C)) |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 236 | return true; |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 237 | } else { |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 238 | GS.HasNonInstructionUser = true; |
| 239 | // Otherwise must be some other user. |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 246 | static Constant *getAggregateConstantElement(Constant *Agg, Constant *Idx) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 247 | ConstantInt *CI = dyn_cast<ConstantInt>(Idx); |
| 248 | if (!CI) return 0; |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 249 | unsigned IdxV = CI->getZExtValue(); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 251 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg)) { |
| 252 | if (IdxV < CS->getNumOperands()) return CS->getOperand(IdxV); |
| 253 | } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg)) { |
| 254 | if (IdxV < CA->getNumOperands()) return CA->getOperand(IdxV); |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 255 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Agg)) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 256 | if (IdxV < CP->getNumOperands()) return CP->getOperand(IdxV); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 257 | } else if (isa<ConstantAggregateZero>(Agg)) { |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 258 | if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { |
| 259 | if (IdxV < STy->getNumElements()) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 260 | return Constant::getNullValue(STy->getElementType(IdxV)); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 261 | } else if (const SequentialType *STy = |
| 262 | dyn_cast<SequentialType>(Agg->getType())) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 263 | return Constant::getNullValue(STy->getElementType()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 265 | } else if (isa<UndefValue>(Agg)) { |
| 266 | if (const StructType *STy = dyn_cast<StructType>(Agg->getType())) { |
| 267 | if (IdxV < STy->getNumElements()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 268 | return UndefValue::get(STy->getElementType(IdxV)); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 269 | } else if (const SequentialType *STy = |
| 270 | dyn_cast<SequentialType>(Agg->getType())) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 271 | return UndefValue::get(STy->getElementType()); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 273 | } |
| 274 | return 0; |
| 275 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 277 | |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 278 | /// CleanupConstantGlobalUsers - We just marked GV constant. Loop over all |
| 279 | /// 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] | 280 | /// quick scan over the use list to clean up the easy and obvious cruft. This |
| 281 | /// returns true if it made a change. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 282 | static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) { |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 283 | bool Changed = false; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 284 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;) { |
| 285 | User *U = *UI++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 287 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 288 | if (Init) { |
| 289 | // Replace the load with the initializer. |
| 290 | LI->replaceAllUsesWith(Init); |
| 291 | LI->eraseFromParent(); |
| 292 | Changed = true; |
| 293 | } |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 294 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 295 | // Store must be unreachable or storing Init into the global. |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 296 | SI->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 297 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 298 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { |
| 299 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
Chris Lattner | aae4a1c | 2005-09-26 07:34:35 +0000 | [diff] [blame] | 300 | Constant *SubInit = 0; |
| 301 | if (Init) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 302 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 303 | Changed |= CleanupConstantGlobalUsers(CE, SubInit); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 304 | } else if (CE->getOpcode() == Instruction::BitCast && |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 305 | CE->getType()->isPointerTy()) { |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 306 | // Pointer cast, delete any stores and memsets to the global. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 307 | Changed |= CleanupConstantGlobalUsers(CE, 0); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (CE->use_empty()) { |
| 311 | CE->destroyConstant(); |
| 312 | Changed = true; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 313 | } |
| 314 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 315 | // Do not transform "gepinst (gep constexpr (GV))" here, because forming |
| 316 | // "gepconstexpr (gep constexpr (GV))" will cause the two gep's to fold |
| 317 | // and will invalidate our notion of what Init is. |
Chris Lattner | 1945024 | 2007-11-13 21:46:23 +0000 | [diff] [blame] | 318 | Constant *SubInit = 0; |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 319 | if (!isa<ConstantExpr>(GEP->getOperand(0))) { |
| 320 | ConstantExpr *CE = |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 321 | dyn_cast_or_null<ConstantExpr>(ConstantFoldInstruction(GEP)); |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 322 | if (Init && CE && CE->getOpcode() == Instruction::GetElementPtr) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 323 | SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); |
Chris Lattner | 7b52fe7 | 2007-11-09 17:33:02 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 325 | Changed |= CleanupConstantGlobalUsers(GEP, SubInit); |
Chris Lattner | c4d81b0 | 2004-10-10 16:47:33 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 327 | if (GEP->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 328 | GEP->eraseFromParent(); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 329 | Changed = true; |
| 330 | } |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 331 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U)) { // memset/cpy/mv |
| 332 | if (MI->getRawDest() == V) { |
| 333 | MI->eraseFromParent(); |
| 334 | Changed = true; |
| 335 | } |
| 336 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 337 | } else if (Constant *C = dyn_cast<Constant>(U)) { |
| 338 | // If we have a chain of dead constantexprs or other things dangling from |
| 339 | // us, and if they are all dead, nuke them without remorse. |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 340 | if (SafeToDestroyConstant(C)) { |
Devang Patel | 743cdf8 | 2009-03-06 01:37:41 +0000 | [diff] [blame] | 341 | C->destroyConstant(); |
Chris Lattner | 35c81b0 | 2005-02-27 18:58:52 +0000 | [diff] [blame] | 342 | // This could have invalidated UI, start over from scratch. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 343 | CleanupConstantGlobalUsers(V, Init); |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 344 | return true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
Chris Lattner | 031955d | 2004-10-10 16:43:46 +0000 | [diff] [blame] | 348 | return Changed; |
Chris Lattner | e47ba74 | 2004-10-06 20:57:02 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 351 | /// isSafeSROAElementUse - Return true if the specified instruction is a safe |
| 352 | /// user of a derived expression from a global that we want to SROA. |
| 353 | static bool isSafeSROAElementUse(Value *V) { |
| 354 | // We might have a dead and dangling constant hanging off of here. |
| 355 | if (Constant *C = dyn_cast<Constant>(V)) |
Jay Foad | e3acf15 | 2009-06-09 21:37:11 +0000 | [diff] [blame] | 356 | return SafeToDestroyConstant(C); |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 358 | Instruction *I = dyn_cast<Instruction>(V); |
| 359 | if (!I) return false; |
| 360 | |
| 361 | // Loads are ok. |
| 362 | if (isa<LoadInst>(I)) return true; |
| 363 | |
| 364 | // Stores *to* the pointer are ok. |
| 365 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 366 | return SI->getOperand(0) != V; |
| 367 | |
| 368 | // Otherwise, it must be a GEP. |
| 369 | GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I); |
| 370 | if (GEPI == 0) return false; |
| 371 | |
| 372 | if (GEPI->getNumOperands() < 3 || !isa<Constant>(GEPI->getOperand(1)) || |
| 373 | !cast<Constant>(GEPI->getOperand(1))->isNullValue()) |
| 374 | return false; |
| 375 | |
| 376 | for (Value::use_iterator I = GEPI->use_begin(), E = GEPI->use_end(); |
| 377 | I != E; ++I) |
| 378 | if (!isSafeSROAElementUse(*I)) |
| 379 | return false; |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 383 | |
| 384 | /// IsUserOfGlobalSafeForSRA - U is a direct user of the specified global value. |
| 385 | /// Look at it and its uses and decide whether it is safe to SROA this global. |
| 386 | /// |
| 387 | static bool IsUserOfGlobalSafeForSRA(User *U, GlobalValue *GV) { |
| 388 | // The user of the global must be a GEP Inst or a ConstantExpr GEP. |
| 389 | if (!isa<GetElementPtrInst>(U) && |
| 390 | (!isa<ConstantExpr>(U) || |
| 391 | cast<ConstantExpr>(U)->getOpcode() != Instruction::GetElementPtr)) |
| 392 | return false; |
| 393 | |
| 394 | // Check to see if this ConstantExpr GEP is SRA'able. In particular, we |
| 395 | // don't like < 3 operand CE's, and we don't like non-constant integer |
| 396 | // indices. This enforces that all uses are 'gep GV, 0, C, ...' for some |
| 397 | // value of C. |
| 398 | if (U->getNumOperands() < 3 || !isa<Constant>(U->getOperand(1)) || |
| 399 | !cast<Constant>(U->getOperand(1))->isNullValue() || |
| 400 | !isa<ConstantInt>(U->getOperand(2))) |
| 401 | return false; |
| 402 | |
| 403 | gep_type_iterator GEPI = gep_type_begin(U), E = gep_type_end(U); |
| 404 | ++GEPI; // Skip over the pointer index. |
| 405 | |
| 406 | // If this is a use of an array allocation, do a bit more checking for sanity. |
| 407 | if (const ArrayType *AT = dyn_cast<ArrayType>(*GEPI)) { |
| 408 | uint64_t NumElements = AT->getNumElements(); |
| 409 | ConstantInt *Idx = cast<ConstantInt>(U->getOperand(2)); |
| 410 | |
| 411 | // Check to make sure that index falls within the array. If not, |
| 412 | // something funny is going on, so we won't do the optimization. |
| 413 | // |
| 414 | if (Idx->getZExtValue() >= NumElements) |
| 415 | return false; |
| 416 | |
| 417 | // We cannot scalar repl this level of the array unless any array |
| 418 | // sub-indices are in-range constants. In particular, consider: |
| 419 | // A[0][i]. We cannot know that the user isn't doing invalid things like |
| 420 | // allowing i to index an out-of-range subscript that accesses A[1]. |
| 421 | // |
| 422 | // Scalar replacing *just* the outer index of the array is probably not |
| 423 | // going to be a win anyway, so just give up. |
| 424 | for (++GEPI; // Skip array index. |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 425 | GEPI != E; |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 426 | ++GEPI) { |
| 427 | uint64_t NumElements; |
| 428 | if (const ArrayType *SubArrayTy = dyn_cast<ArrayType>(*GEPI)) |
| 429 | NumElements = SubArrayTy->getNumElements(); |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 430 | else if (const VectorType *SubVectorTy = dyn_cast<VectorType>(*GEPI)) |
| 431 | NumElements = SubVectorTy->getNumElements(); |
| 432 | else { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 433 | assert((*GEPI)->isStructTy() && |
Dan Gohman | 6874a2a | 2009-08-18 14:58:19 +0000 | [diff] [blame] | 434 | "Indexed GEP type is not array, vector, or struct!"); |
| 435 | continue; |
| 436 | } |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 437 | |
| 438 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPI.getOperand()); |
| 439 | if (!IdxVal || IdxVal->getZExtValue() >= NumElements) |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | for (Value::use_iterator I = U->use_begin(), E = U->use_end(); I != E; ++I) |
| 445 | if (!isSafeSROAElementUse(*I)) |
| 446 | return false; |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | /// GlobalUsersSafeToSRA - Look at all uses of the global and decide whether it |
| 451 | /// is safe for us to perform this transformation. |
| 452 | /// |
| 453 | static bool GlobalUsersSafeToSRA(GlobalValue *GV) { |
| 454 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); |
| 455 | UI != E; ++UI) { |
| 456 | if (!IsUserOfGlobalSafeForSRA(*UI, GV)) |
| 457 | return false; |
| 458 | } |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 463 | /// SRAGlobal - Perform scalar replacement of aggregates on the specified global |
| 464 | /// variable. This opens the door for other optimizations by exposing the |
| 465 | /// behavior of the program in a more fine-grained way. We have determined that |
| 466 | /// this transformation is safe already. We return the first global variable we |
| 467 | /// insert so that the caller can reprocess it. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 468 | static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD) { |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 469 | // Make sure this global only has simple uses that we can SRA. |
Chris Lattner | 941db49 | 2008-01-14 02:09:12 +0000 | [diff] [blame] | 470 | if (!GlobalUsersSafeToSRA(GV)) |
Chris Lattner | 727c210 | 2008-01-14 01:31:05 +0000 | [diff] [blame] | 471 | return 0; |
| 472 | |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 473 | assert(GV->hasLocalLinkage() && !GV->isConstant()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 474 | Constant *Init = GV->getInitializer(); |
| 475 | const Type *Ty = Init->getType(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 477 | std::vector<GlobalVariable*> NewGlobals; |
| 478 | Module::GlobalListType &Globals = GV->getParent()->getGlobalList(); |
| 479 | |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 480 | // Get the alignment of the global, either explicit or target-specific. |
| 481 | unsigned StartAlignment = GV->getAlignment(); |
| 482 | if (StartAlignment == 0) |
| 483 | StartAlignment = TD.getABITypeAlignment(GV->getType()); |
| 484 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 485 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 486 | NewGlobals.reserve(STy->getNumElements()); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 487 | const StructLayout &Layout = *TD.getStructLayout(STy); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 488 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 489 | Constant *In = getAggregateConstantElement(Init, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 490 | ConstantInt::get(Type::getInt32Ty(STy->getContext()), i)); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 491 | assert(In && "Couldn't get element of initializer?"); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 492 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 493 | GlobalVariable::InternalLinkage, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 494 | In, GV->getName()+"."+Twine(i), |
Matthijs Kooijman | bc1f989 | 2008-07-17 11:59:53 +0000 | [diff] [blame] | 495 | GV->isThreadLocal(), |
Owen Anderson | 3d29df3 | 2009-07-08 01:26:06 +0000 | [diff] [blame] | 496 | GV->getType()->getAddressSpace()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 497 | Globals.insert(GV, NGV); |
| 498 | NewGlobals.push_back(NGV); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 499 | |
| 500 | // Calculate the known alignment of the field. If the original aggregate |
| 501 | // had 256 byte alignment for example, something might depend on that: |
| 502 | // propagate info to each field. |
| 503 | uint64_t FieldOffset = Layout.getElementOffset(i); |
| 504 | unsigned NewAlign = (unsigned)MinAlign(StartAlignment, FieldOffset); |
| 505 | if (NewAlign > TD.getABITypeAlignment(STy->getElementType(i))) |
| 506 | NGV->setAlignment(NewAlign); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 507 | } |
| 508 | } else if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) { |
| 509 | unsigned NumElements = 0; |
| 510 | if (const ArrayType *ATy = dyn_cast<ArrayType>(STy)) |
| 511 | NumElements = ATy->getNumElements(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 512 | else |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 513 | NumElements = cast<VectorType>(STy)->getNumElements(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 1f21ef1 | 2005-02-23 16:53:04 +0000 | [diff] [blame] | 515 | if (NumElements > 16 && GV->hasNUsesOrMore(16)) |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 516 | return 0; // It's not worth it. |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 517 | NewGlobals.reserve(NumElements); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 518 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 519 | uint64_t EltSize = TD.getTypeAllocSize(STy->getElementType()); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 520 | unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 521 | for (unsigned i = 0, e = NumElements; i != e; ++i) { |
| 522 | Constant *In = getAggregateConstantElement(Init, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 523 | ConstantInt::get(Type::getInt32Ty(Init->getContext()), i)); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 524 | assert(In && "Couldn't get element of initializer?"); |
| 525 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 526 | GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 527 | GlobalVariable::InternalLinkage, |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 528 | In, GV->getName()+"."+Twine(i), |
Matthijs Kooijman | bc1f989 | 2008-07-17 11:59:53 +0000 | [diff] [blame] | 529 | GV->isThreadLocal(), |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 530 | GV->getType()->getAddressSpace()); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 531 | Globals.insert(GV, NGV); |
| 532 | NewGlobals.push_back(NGV); |
Chris Lattner | 998182b | 2008-04-26 07:40:11 +0000 | [diff] [blame] | 533 | |
| 534 | // Calculate the known alignment of the field. If the original aggregate |
| 535 | // had 256 byte alignment for example, something might depend on that: |
| 536 | // propagate info to each field. |
| 537 | unsigned NewAlign = (unsigned)MinAlign(StartAlignment, EltSize*i); |
| 538 | if (NewAlign > EltAlign) |
| 539 | NGV->setAlignment(NewAlign); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | if (NewGlobals.empty()) |
| 544 | return 0; |
Chris Lattner | c6a669b | 2010-02-26 23:35:25 +0000 | [diff] [blame] | 545 | |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 546 | DEBUG(dbgs() << "PERFORMING GLOBAL SRA ON: " << *GV); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 548 | Constant *NullInt =Constant::getNullValue(Type::getInt32Ty(GV->getContext())); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 549 | |
| 550 | // Loop over all of the uses of the global, replacing the constantexpr geps, |
| 551 | // with smaller constantexpr geps or direct references. |
| 552 | while (!GV->use_empty()) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 553 | User *GEP = GV->use_back(); |
| 554 | assert(((isa<ConstantExpr>(GEP) && |
| 555 | cast<ConstantExpr>(GEP)->getOpcode()==Instruction::GetElementPtr)|| |
| 556 | isa<GetElementPtrInst>(GEP)) && "NonGEP CE's are not SRAable!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 558 | // Ignore the 1th operand, which has to be zero or else the program is quite |
| 559 | // broken (undefined). Get the 2nd operand, which is the structure or array |
| 560 | // index. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 561 | unsigned Val = cast<ConstantInt>(GEP->getOperand(2))->getZExtValue(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 562 | if (Val >= NewGlobals.size()) Val = 0; // Out of bound array access. |
| 563 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 564 | Value *NewPtr = NewGlobals[Val]; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 565 | |
| 566 | // Form a shorter GEP if needed. |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 567 | if (GEP->getNumOperands() > 3) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 568 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP)) { |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 569 | SmallVector<Constant*, 8> Idxs; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 570 | Idxs.push_back(NullInt); |
| 571 | for (unsigned i = 3, e = CE->getNumOperands(); i != e; ++i) |
| 572 | Idxs.push_back(CE->getOperand(i)); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 573 | NewPtr = ConstantExpr::getGetElementPtr(cast<Constant>(NewPtr), |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 574 | &Idxs[0], Idxs.size()); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 575 | } else { |
| 576 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(GEP); |
Chris Lattner | 699d144 | 2007-01-31 19:59:55 +0000 | [diff] [blame] | 577 | SmallVector<Value*, 8> Idxs; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 578 | Idxs.push_back(NullInt); |
| 579 | for (unsigned i = 3, e = GEPI->getNumOperands(); i != e; ++i) |
| 580 | Idxs.push_back(GEPI->getOperand(i)); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 581 | NewPtr = GetElementPtrInst::Create(NewPtr, Idxs.begin(), Idxs.end(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 582 | GEPI->getName()+"."+Twine(Val),GEPI); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 583 | } |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 584 | } |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 585 | GEP->replaceAllUsesWith(NewPtr); |
| 586 | |
| 587 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(GEP)) |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 588 | GEPI->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 589 | else |
| 590 | cast<ConstantExpr>(GEP)->destroyConstant(); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Chris Lattner | e40e2d1 | 2004-10-08 20:25:55 +0000 | [diff] [blame] | 593 | // Delete the old global, now that it is dead. |
| 594 | Globals.erase(GV); |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 595 | ++NumSRA; |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 596 | |
| 597 | // Loop over the new globals array deleting any globals that are obviously |
| 598 | // dead. This can arise due to scalarization of a structure or an array that |
| 599 | // has elements that are dead. |
| 600 | unsigned FirstGlobal = 0; |
| 601 | for (unsigned i = 0, e = NewGlobals.size(); i != e; ++i) |
| 602 | if (NewGlobals[i]->use_empty()) { |
| 603 | Globals.erase(NewGlobals[i]); |
| 604 | if (FirstGlobal == i) ++FirstGlobal; |
| 605 | } |
| 606 | |
| 607 | return FirstGlobal != NewGlobals.size() ? NewGlobals[FirstGlobal] : 0; |
Chris Lattner | 670c889 | 2004-10-08 17:32:09 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 610 | /// AllUsesOfValueWillTrapIfNull - Return true if all users of the specified |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 611 | /// value will trap if the value is dynamically null. PHIs keeps track of any |
| 612 | /// phi nodes we've seen to avoid reprocessing them. |
| 613 | static bool AllUsesOfValueWillTrapIfNull(Value *V, |
| 614 | SmallPtrSet<PHINode*, 8> &PHIs) { |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 615 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) |
| 616 | if (isa<LoadInst>(*UI)) { |
| 617 | // Will trap. |
| 618 | } else if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 619 | if (SI->getOperand(0) == V) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 620 | //cerr << "NONTRAPPING USE: " << **UI; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 621 | return false; // Storing the value. |
| 622 | } |
| 623 | } else if (CallInst *CI = dyn_cast<CallInst>(*UI)) { |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 624 | if (CI->getCalledValue() != V) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 625 | //cerr << "NONTRAPPING USE: " << **UI; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 626 | return false; // Not calling the ptr |
| 627 | } |
| 628 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) { |
Gabor Greif | 654c06f | 2010-03-20 21:00:25 +0000 | [diff] [blame] | 629 | if (II->getCalledValue() != V) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 630 | //cerr << "NONTRAPPING USE: " << **UI; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 631 | return false; // Not calling the ptr |
| 632 | } |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 633 | } else if (BitCastInst *CI = dyn_cast<BitCastInst>(*UI)) { |
| 634 | if (!AllUsesOfValueWillTrapIfNull(CI, PHIs)) return false; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 635 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI)) { |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 636 | if (!AllUsesOfValueWillTrapIfNull(GEPI, PHIs)) return false; |
| 637 | } else if (PHINode *PN = dyn_cast<PHINode>(*UI)) { |
| 638 | // If we've already seen this phi node, ignore it, it has already been |
| 639 | // checked. |
Jakob Stoklund Olesen | b489d0f | 2010-01-29 23:54:14 +0000 | [diff] [blame] | 640 | if (PHIs.insert(PN) && !AllUsesOfValueWillTrapIfNull(PN, PHIs)) |
| 641 | return false; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 642 | } else if (isa<ICmpInst>(*UI) && |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 643 | isa<ConstantPointerNull>(UI->getOperand(1))) { |
Nick Lewycky | e7ee59b | 2010-02-25 06:39:10 +0000 | [diff] [blame] | 644 | // Ignore icmp X, null |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 645 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 646 | //cerr << "NONTRAPPING USE: " << **UI; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 647 | return false; |
| 648 | } |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | /// AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads |
Chris Lattner | e9ece2a | 2004-10-22 06:43:28 +0000 | [diff] [blame] | 653 | /// from GV will trap if the loaded value is null. Note that this also permits |
| 654 | /// comparisons of the loaded value against null, as a special case. |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 655 | static bool AllUsesOfLoadedValueWillTrapIfNull(GlobalVariable *GV) { |
| 656 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI!=E; ++UI) |
| 657 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
Chris Lattner | 8168618 | 2007-09-13 16:30:19 +0000 | [diff] [blame] | 658 | SmallPtrSet<PHINode*, 8> PHIs; |
| 659 | if (!AllUsesOfValueWillTrapIfNull(LI, PHIs)) |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 660 | return false; |
| 661 | } else if (isa<StoreInst>(*UI)) { |
| 662 | // Ignore stores to the global. |
| 663 | } else { |
| 664 | // We don't know or understand this user, bail out. |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 665 | //cerr << "UNKNOWN USER OF GLOBAL!: " << **UI; |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | |
| 669 | return true; |
| 670 | } |
| 671 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 672 | static bool OptimizeAwayTrappingUsesOfValue(Value *V, Constant *NewV) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 673 | bool Changed = false; |
| 674 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) { |
| 675 | Instruction *I = cast<Instruction>(*UI++); |
| 676 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 677 | LI->setOperand(0, NewV); |
| 678 | Changed = true; |
| 679 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 680 | if (SI->getOperand(1) == V) { |
| 681 | SI->setOperand(1, NewV); |
| 682 | Changed = true; |
| 683 | } |
| 684 | } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 685 | if (I->getOperand(0) == V) { |
| 686 | // Calling through the pointer! Turn into a direct call, but be careful |
| 687 | // that the pointer is not also being passed as an argument. |
| 688 | I->setOperand(0, NewV); |
| 689 | Changed = true; |
| 690 | bool PassedAsArg = false; |
| 691 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i) |
| 692 | if (I->getOperand(i) == V) { |
| 693 | PassedAsArg = true; |
| 694 | I->setOperand(i, NewV); |
| 695 | } |
| 696 | |
| 697 | if (PassedAsArg) { |
| 698 | // Being passed as an argument also. Be careful to not invalidate UI! |
| 699 | UI = V->use_begin(); |
| 700 | } |
| 701 | } |
| 702 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 703 | Changed |= OptimizeAwayTrappingUsesOfValue(CI, |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 704 | ConstantExpr::getCast(CI->getOpcode(), |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 705 | NewV, CI->getType())); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 706 | if (CI->use_empty()) { |
| 707 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 708 | CI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 709 | } |
| 710 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
| 711 | // Should handle GEP here. |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 712 | SmallVector<Constant*, 8> Idxs; |
| 713 | Idxs.reserve(GEPI->getNumOperands()-1); |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 714 | for (User::op_iterator i = GEPI->op_begin() + 1, e = GEPI->op_end(); |
| 715 | i != e; ++i) |
| 716 | if (Constant *C = dyn_cast<Constant>(*i)) |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 717 | Idxs.push_back(C); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 718 | else |
| 719 | break; |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 720 | if (Idxs.size() == GEPI->getNumOperands()-1) |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 721 | Changed |= OptimizeAwayTrappingUsesOfValue(GEPI, |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 722 | ConstantExpr::getGetElementPtr(NewV, &Idxs[0], |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 723 | Idxs.size())); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 724 | if (GEPI->use_empty()) { |
| 725 | Changed = true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 726 | GEPI->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | return Changed; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | /// OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null |
| 736 | /// value stored into it. If there are uses of the loaded value that would trap |
| 737 | /// if the loaded value is dynamically null, then we know that they cannot be |
| 738 | /// reachable with a null optimize away the load. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 739 | static bool OptimizeAwayTrappingUsesOfLoads(GlobalVariable *GV, Constant *LV) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 740 | bool Changed = false; |
| 741 | |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 742 | // Keep track of whether we are able to remove all the uses of the global |
| 743 | // other than the store that defines it. |
| 744 | bool AllNonStoreUsesGone = true; |
| 745 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 746 | // 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] | 747 | for (Value::use_iterator GUI = GV->use_begin(), E = GV->use_end(); GUI != E;){ |
| 748 | User *GlobalUser = *GUI++; |
| 749 | if (LoadInst *LI = dyn_cast<LoadInst>(GlobalUser)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 750 | Changed |= OptimizeAwayTrappingUsesOfValue(LI, LV); |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 751 | // If we were able to delete all uses of the loads |
| 752 | if (LI->use_empty()) { |
| 753 | LI->eraseFromParent(); |
| 754 | Changed = true; |
| 755 | } else { |
| 756 | AllNonStoreUsesGone = false; |
| 757 | } |
| 758 | } else if (isa<StoreInst>(GlobalUser)) { |
| 759 | // Ignore the store that stores "LV" to the global. |
| 760 | assert(GlobalUser->getOperand(1) == GV && |
| 761 | "Must be storing *to* the global"); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 762 | } else { |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 763 | AllNonStoreUsesGone = false; |
| 764 | |
| 765 | // If we get here we could have other crazy uses that are transitively |
| 766 | // loaded. |
| 767 | assert((isa<PHINode>(GlobalUser) || isa<SelectInst>(GlobalUser) || |
| 768 | isa<ConstantExpr>(GlobalUser)) && "Only expect load and stores!"); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 769 | } |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 770 | } |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 771 | |
| 772 | if (Changed) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 773 | DEBUG(dbgs() << "OPTIMIZED LOADS FROM STORED ONCE POINTER: " << *GV); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 774 | ++NumGlobUses; |
| 775 | } |
| 776 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 777 | // If we nuked all of the loads, then none of the stores are needed either, |
| 778 | // nor is the global. |
Chris Lattner | 92c6bd2 | 2009-01-14 00:12:58 +0000 | [diff] [blame] | 779 | if (AllNonStoreUsesGone) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 780 | DEBUG(dbgs() << " *** GLOBAL NOW DEAD!\n"); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 781 | CleanupConstantGlobalUsers(GV, 0); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 782 | if (GV->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 783 | GV->eraseFromParent(); |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 784 | ++NumDeleted; |
| 785 | } |
| 786 | Changed = true; |
| 787 | } |
| 788 | return Changed; |
| 789 | } |
| 790 | |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 791 | /// ConstantPropUsersOf - Walk the use list of V, constant folding all of the |
| 792 | /// instructions that are foldable. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 793 | static void ConstantPropUsersOf(Value *V) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 794 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ) |
| 795 | if (Instruction *I = dyn_cast<Instruction>(*UI++)) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 796 | if (Constant *NewC = ConstantFoldInstruction(I)) { |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 797 | I->replaceAllUsesWith(NewC); |
| 798 | |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 799 | // Advance UI to the next non-I use to avoid invalidating it! |
| 800 | // Instructions could multiply use V. |
| 801 | while (UI != E && *UI == I) |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 802 | ++UI; |
Chris Lattner | d514d82 | 2005-02-01 01:23:31 +0000 | [diff] [blame] | 803 | I->eraseFromParent(); |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | /// OptimizeGlobalAddressOfMalloc - This function takes the specified global |
| 808 | /// variable, and transforms the program as if it always contained the result of |
| 809 | /// the specified malloc. Because it is always the result of the specified |
| 810 | /// 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] | 811 | /// 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] | 812 | static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 813 | CallInst *CI, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 814 | const Type *AllocTy, |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 815 | ConstantInt *NElements, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 816 | TargetData* TD) { |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 817 | DEBUG(errs() << "PROMOTING GLOBAL: " << *GV << " CALL = " << *CI << '\n'); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 818 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 819 | const Type *GlobalType; |
| 820 | if (NElements->getZExtValue() == 1) |
| 821 | GlobalType = AllocTy; |
| 822 | else |
| 823 | // If we have an array allocation, the global variable is of an array. |
| 824 | GlobalType = ArrayType::get(AllocTy, NElements->getZExtValue()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 825 | |
| 826 | // Create the new global variable. The contents of the malloc'd memory is |
| 827 | // undefined, so initialize with an undef value. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 828 | GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(), |
Chris Lattner | e9fd444 | 2010-02-26 23:42:13 +0000 | [diff] [blame] | 829 | GlobalType, false, |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 830 | GlobalValue::InternalLinkage, |
Chris Lattner | e9fd444 | 2010-02-26 23:42:13 +0000 | [diff] [blame] | 831 | UndefValue::get(GlobalType), |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 832 | GV->getName()+".body", |
| 833 | GV, |
| 834 | GV->isThreadLocal()); |
| 835 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 836 | // If there are bitcast users of the malloc (which is typical, usually we have |
| 837 | // a malloc + bitcast) then replace them with uses of the new global. Update |
| 838 | // other users to use the global as well. |
| 839 | BitCastInst *TheBC = 0; |
| 840 | while (!CI->use_empty()) { |
| 841 | Instruction *User = cast<Instruction>(CI->use_back()); |
| 842 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { |
| 843 | if (BCI->getType() == NewGV->getType()) { |
| 844 | BCI->replaceAllUsesWith(NewGV); |
| 845 | BCI->eraseFromParent(); |
| 846 | } else { |
| 847 | BCI->setOperand(0, NewGV); |
| 848 | } |
| 849 | } else { |
| 850 | if (TheBC == 0) |
| 851 | TheBC = new BitCastInst(NewGV, CI->getType(), "newgv", CI); |
| 852 | User->replaceUsesOfWith(CI, TheBC); |
| 853 | } |
| 854 | } |
| 855 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 856 | Constant *RepValue = NewGV; |
| 857 | if (NewGV->getType() != GV->getType()->getElementType()) |
| 858 | RepValue = ConstantExpr::getBitCast(RepValue, |
| 859 | GV->getType()->getElementType()); |
| 860 | |
| 861 | // If there is a comparison against null, we will insert a global bool to |
| 862 | // keep track of whether the global was initialized yet or not. |
| 863 | GlobalVariable *InitBool = |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 864 | new GlobalVariable(Type::getInt1Ty(GV->getContext()), false, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 865 | GlobalValue::InternalLinkage, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 866 | ConstantInt::getFalse(GV->getContext()), |
| 867 | GV->getName()+".init", GV->isThreadLocal()); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 868 | bool InitBoolUsed = false; |
| 869 | |
| 870 | // Loop over all uses of GV, processing them in turn. |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 871 | while (!GV->use_empty()) { |
| 872 | if (StoreInst *SI = dyn_cast<StoreInst>(GV->use_back())) { |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 873 | // The global is initialized when the store to it occurs. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 874 | new StoreInst(ConstantInt::getTrue(GV->getContext()), InitBool, SI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 875 | SI->eraseFromParent(); |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 876 | continue; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 877 | } |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 878 | |
| 879 | LoadInst *LI = cast<LoadInst>(GV->use_back()); |
| 880 | while (!LI->use_empty()) { |
| 881 | Use &LoadUse = LI->use_begin().getUse(); |
| 882 | if (!isa<ICmpInst>(LoadUse.getUser())) { |
| 883 | LoadUse = RepValue; |
| 884 | continue; |
| 885 | } |
| 886 | |
| 887 | ICmpInst *ICI = cast<ICmpInst>(LoadUse.getUser()); |
| 888 | // Replace the cmp X, 0 with a use of the bool value. |
| 889 | Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", ICI); |
| 890 | InitBoolUsed = true; |
| 891 | switch (ICI->getPredicate()) { |
| 892 | default: llvm_unreachable("Unknown ICmp Predicate!"); |
| 893 | case ICmpInst::ICMP_ULT: |
| 894 | case ICmpInst::ICMP_SLT: // X < null -> always false |
| 895 | LV = ConstantInt::getFalse(GV->getContext()); |
| 896 | break; |
| 897 | case ICmpInst::ICMP_ULE: |
| 898 | case ICmpInst::ICMP_SLE: |
| 899 | case ICmpInst::ICMP_EQ: |
| 900 | LV = BinaryOperator::CreateNot(LV, "notinit", ICI); |
| 901 | break; |
| 902 | case ICmpInst::ICMP_NE: |
| 903 | case ICmpInst::ICMP_UGE: |
| 904 | case ICmpInst::ICMP_SGE: |
| 905 | case ICmpInst::ICMP_UGT: |
| 906 | case ICmpInst::ICMP_SGT: |
| 907 | break; // no change. |
| 908 | } |
| 909 | ICI->replaceAllUsesWith(LV); |
| 910 | ICI->eraseFromParent(); |
| 911 | } |
| 912 | LI->eraseFromParent(); |
| 913 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 914 | |
| 915 | // If the initialization boolean was used, insert it, otherwise delete it. |
| 916 | if (!InitBoolUsed) { |
| 917 | while (!InitBool->use_empty()) // Delete initializations |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 918 | cast<StoreInst>(InitBool->use_back())->eraseFromParent(); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 919 | delete InitBool; |
| 920 | } else |
| 921 | GV->getParent()->getGlobalList().insert(GV, InitBool); |
| 922 | |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 923 | // Now the GV is dead, nuke it and the malloc.. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 924 | GV->eraseFromParent(); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 925 | CI->eraseFromParent(); |
| 926 | |
| 927 | // To further other optimizations, loop over all users of NewGV and try to |
| 928 | // constant prop them. This will promote GEP instructions with constant |
| 929 | // indices into GEP constant-exprs, which will allow global-opt to hack on it. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 930 | ConstantPropUsersOf(NewGV); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 931 | if (RepValue != NewGV) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 932 | ConstantPropUsersOf(RepValue); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 933 | |
| 934 | return NewGV; |
| 935 | } |
| 936 | |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 937 | /// ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking |
| 938 | /// to make sure that there are no complex uses of V. We permit simple things |
| 939 | /// like dereferencing the pointer, but not storing through the address, unless |
| 940 | /// it is to the specified global. |
| 941 | static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Instruction *V, |
Chris Lattner | c451f9c | 2007-09-13 16:37:20 +0000 | [diff] [blame] | 942 | GlobalVariable *GV, |
| 943 | SmallPtrSet<PHINode*, 8> &PHIs) { |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 944 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
Jay Foad | 0906b1b | 2009-06-06 17:49:35 +0000 | [diff] [blame] | 945 | Instruction *Inst = cast<Instruction>(*UI); |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 946 | |
| 947 | if (isa<LoadInst>(Inst) || isa<CmpInst>(Inst)) { |
| 948 | continue; // Fine, ignore. |
| 949 | } |
| 950 | |
| 951 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 952 | if (SI->getOperand(0) == V && SI->getOperand(1) != GV) |
| 953 | return false; // Storing the pointer itself... bad. |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 954 | continue; // Otherwise, storing through it, or storing into GV... fine. |
| 955 | } |
| 956 | |
| 957 | if (isa<GetElementPtrInst>(Inst)) { |
| 958 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(Inst, GV, PHIs)) |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 959 | return false; |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 960 | continue; |
| 961 | } |
| 962 | |
| 963 | if (PHINode *PN = dyn_cast<PHINode>(Inst)) { |
Chris Lattner | c451f9c | 2007-09-13 16:37:20 +0000 | [diff] [blame] | 964 | // PHIs are ok if all uses are ok. Don't infinitely recurse through PHI |
| 965 | // cycles. |
| 966 | if (PHIs.insert(PN)) |
Chris Lattner | 5e6e494 | 2007-09-14 03:41:21 +0000 | [diff] [blame] | 967 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(PN, GV, PHIs)) |
| 968 | return false; |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 969 | continue; |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 970 | } |
Chris Lattner | 49b6d4a | 2008-12-15 21:08:54 +0000 | [diff] [blame] | 971 | |
| 972 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Inst)) { |
| 973 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(BCI, GV, PHIs)) |
| 974 | return false; |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | return false; |
| 979 | } |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 980 | return true; |
Chris Lattner | fa07e4f | 2004-12-02 07:11:07 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 983 | /// ReplaceUsesOfMallocWithGlobal - The Alloc pointer is stored into GV |
| 984 | /// somewhere. Transform all uses of the allocation into loads from the |
| 985 | /// global and uses of the resultant pointer. Further, delete the store into |
| 986 | /// GV. This assumes that these value pass the |
| 987 | /// 'ValueIsOnlyUsedLocallyOrStoredToOneGlobal' predicate. |
| 988 | static void ReplaceUsesOfMallocWithGlobal(Instruction *Alloc, |
| 989 | GlobalVariable *GV) { |
| 990 | while (!Alloc->use_empty()) { |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 991 | Instruction *U = cast<Instruction>(*Alloc->use_begin()); |
| 992 | Instruction *InsertPt = U; |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 993 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 994 | // If this is the store of the allocation into the global, remove it. |
| 995 | if (SI->getOperand(1) == GV) { |
| 996 | SI->eraseFromParent(); |
| 997 | continue; |
| 998 | } |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 999 | } else if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 1000 | // Insert the load in the corresponding predecessor, not right before the |
| 1001 | // PHI. |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 1002 | InsertPt = PN->getIncomingBlock(Alloc->use_begin())->getTerminator(); |
Chris Lattner | 101f44e | 2008-12-15 21:44:34 +0000 | [diff] [blame] | 1003 | } else if (isa<BitCastInst>(U)) { |
| 1004 | // Must be bitcast between the malloc and store to initialize the global. |
| 1005 | ReplaceUsesOfMallocWithGlobal(U, GV); |
| 1006 | U->eraseFromParent(); |
| 1007 | continue; |
| 1008 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U)) { |
| 1009 | // If this is a "GEP bitcast" and the user is a store to the global, then |
| 1010 | // just process it as a bitcast. |
| 1011 | if (GEPI->hasAllZeroIndices() && GEPI->hasOneUse()) |
| 1012 | if (StoreInst *SI = dyn_cast<StoreInst>(GEPI->use_back())) |
| 1013 | if (SI->getOperand(1) == GV) { |
| 1014 | // Must be bitcast GEP between the malloc and store to initialize |
| 1015 | // the global. |
| 1016 | ReplaceUsesOfMallocWithGlobal(GEPI, GV); |
| 1017 | GEPI->eraseFromParent(); |
| 1018 | continue; |
| 1019 | } |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1020 | } |
Chris Lattner | 101f44e | 2008-12-15 21:44:34 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1022 | // 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] | 1023 | Value *NL = new LoadInst(GV, GV->getName()+".val", InsertPt); |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1024 | U->replaceUsesOfWith(Alloc, NL); |
| 1025 | } |
| 1026 | } |
| 1027 | |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1028 | /// LoadUsesSimpleEnoughForHeapSRA - Verify that all uses of V (a load, or a phi |
| 1029 | /// of a load) are simple enough to perform heap SRA on. This permits GEP's |
| 1030 | /// 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] | 1031 | static bool LoadUsesSimpleEnoughForHeapSRA(const Value *V, |
| 1032 | SmallPtrSet<const PHINode*, 32> &LoadUsingPHIs, |
| 1033 | SmallPtrSet<const PHINode*, 32> &LoadUsingPHIsPerLoad) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1034 | // We permit two users of the load: setcc comparing against the null |
| 1035 | // pointer, and a getelementptr of a specific form. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1036 | for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
| 1037 | const Instruction *User = cast<Instruction>(*UI); |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1038 | |
| 1039 | // Comparison against null is ok. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1040 | if (const ICmpInst *ICI = dyn_cast<ICmpInst>(User)) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1041 | if (!isa<ConstantPointerNull>(ICI->getOperand(1))) |
| 1042 | return false; |
| 1043 | continue; |
| 1044 | } |
| 1045 | |
| 1046 | // getelementptr is also ok, but only a simple form. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1047 | if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1048 | // Must index into the array and into the struct. |
| 1049 | if (GEPI->getNumOperands() < 3) |
| 1050 | return false; |
| 1051 | |
| 1052 | // Otherwise the GEP is ok. |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1056 | if (const PHINode *PN = dyn_cast<PHINode>(User)) { |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1057 | if (!LoadUsingPHIsPerLoad.insert(PN)) |
| 1058 | // This means some phi nodes are dependent on each other. |
| 1059 | // Avoid infinite looping! |
| 1060 | return false; |
| 1061 | if (!LoadUsingPHIs.insert(PN)) |
| 1062 | // If we have already analyzed this PHI, then it is safe. |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1063 | continue; |
| 1064 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1065 | // Make sure all uses of the PHI are simple enough to transform. |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1066 | if (!LoadUsesSimpleEnoughForHeapSRA(PN, |
| 1067 | LoadUsingPHIs, LoadUsingPHIsPerLoad)) |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1068 | return false; |
| 1069 | |
| 1070 | continue; |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1071 | } |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1072 | |
| 1073 | // Otherwise we don't know what this is, not ok. |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | /// AllGlobalLoadUsesSimpleEnoughForHeapSRA - If all users of values loaded from |
| 1082 | /// GV are simple enough to perform HeapSRA, return true. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1083 | static bool AllGlobalLoadUsesSimpleEnoughForHeapSRA(const GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1084 | Instruction *StoredVal) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1085 | SmallPtrSet<const PHINode*, 32> LoadUsingPHIs; |
| 1086 | SmallPtrSet<const PHINode*, 32> LoadUsingPHIsPerLoad; |
| 1087 | for (Value::const_use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E; |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1088 | ++UI) |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1089 | if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1090 | if (!LoadUsesSimpleEnoughForHeapSRA(LI, LoadUsingPHIs, |
| 1091 | LoadUsingPHIsPerLoad)) |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1092 | return false; |
Evan Cheng | 5d16396 | 2009-06-02 00:56:07 +0000 | [diff] [blame] | 1093 | LoadUsingPHIsPerLoad.clear(); |
| 1094 | } |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1095 | |
| 1096 | // If we reach here, we know that all uses of the loads and transitive uses |
| 1097 | // (through PHI nodes) are simple enough to transform. However, we don't know |
| 1098 | // that all inputs the to the PHI nodes are in the same equivalence sets. |
| 1099 | // Check to verify that all operands of the PHIs are either PHIS that can be |
| 1100 | // transformed, loads from GV, or MI itself. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1101 | for (SmallPtrSet<const PHINode*, 32>::const_iterator I = LoadUsingPHIs.begin(), |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1102 | E = LoadUsingPHIs.end(); I != E; ++I) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1103 | const PHINode *PN = *I; |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1104 | for (unsigned op = 0, e = PN->getNumIncomingValues(); op != e; ++op) { |
| 1105 | Value *InVal = PN->getIncomingValue(op); |
| 1106 | |
| 1107 | // PHI of the stored value itself is ok. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1108 | if (InVal == StoredVal) continue; |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1109 | |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1110 | if (const PHINode *InPN = dyn_cast<PHINode>(InVal)) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1111 | // One of the PHIs in our set is (optimistically) ok. |
| 1112 | if (LoadUsingPHIs.count(InPN)) |
| 1113 | continue; |
| 1114 | return false; |
| 1115 | } |
| 1116 | |
| 1117 | // Load from GV is ok. |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1118 | if (const LoadInst *LI = dyn_cast<LoadInst>(InVal)) |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1119 | if (LI->getOperand(0) == GV) |
| 1120 | continue; |
| 1121 | |
| 1122 | // UNDEF? NULL? |
| 1123 | |
| 1124 | // Anything else is rejected. |
| 1125 | return false; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1129 | return true; |
| 1130 | } |
| 1131 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1132 | static Value *GetHeapSROAValue(Value *V, unsigned FieldNo, |
| 1133 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1134 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1135 | std::vector<Value*> &FieldVals = InsertedScalarizedValues[V]; |
| 1136 | |
| 1137 | if (FieldNo >= FieldVals.size()) |
| 1138 | FieldVals.resize(FieldNo+1); |
| 1139 | |
| 1140 | // If we already have this value, just reuse the previously scalarized |
| 1141 | // version. |
| 1142 | if (Value *FieldVal = FieldVals[FieldNo]) |
| 1143 | return FieldVal; |
| 1144 | |
| 1145 | // Depending on what instruction this is, we have several cases. |
| 1146 | Value *Result; |
| 1147 | if (LoadInst *LI = dyn_cast<LoadInst>(V)) { |
| 1148 | // This is a scalarized version of the load from the global. Just create |
| 1149 | // a new Load of the scalarized global. |
| 1150 | Result = new LoadInst(GetHeapSROAValue(LI->getOperand(0), FieldNo, |
| 1151 | InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1152 | PHIsToRewrite), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1153 | LI->getName()+".f"+Twine(FieldNo), LI); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1154 | } else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 1155 | // PN's type is pointer to struct. Make a new PHI of pointer to struct |
| 1156 | // field. |
| 1157 | const StructType *ST = |
| 1158 | cast<StructType>(cast<PointerType>(PN->getType())->getElementType()); |
| 1159 | |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 1160 | Result = |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1161 | PHINode::Create(PointerType::getUnqual(ST->getElementType(FieldNo)), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1162 | PN->getName()+".f"+Twine(FieldNo), PN); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1163 | PHIsToRewrite.push_back(std::make_pair(PN, FieldNo)); |
| 1164 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1165 | llvm_unreachable("Unknown usable value"); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1166 | Result = 0; |
| 1167 | } |
| 1168 | |
| 1169 | return FieldVals[FieldNo] = Result; |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1172 | /// RewriteHeapSROALoadUser - Given a load instruction and a value derived from |
| 1173 | /// the load, rewrite the derived value to use the HeapSRoA'd load. |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1174 | static void RewriteHeapSROALoadUser(Instruction *LoadUser, |
| 1175 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1176 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1177 | // If this is a comparison against null, handle it. |
| 1178 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(LoadUser)) { |
| 1179 | assert(isa<ConstantPointerNull>(SCI->getOperand(1))); |
| 1180 | // If we have a setcc of the loaded pointer, we can use a setcc of any |
| 1181 | // field. |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1182 | Value *NPtr = GetHeapSROAValue(SCI->getOperand(0), 0, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1183 | InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1184 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1185 | Value *New = new ICmpInst(SCI, SCI->getPredicate(), NPtr, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1186 | Constant::getNullValue(NPtr->getType()), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1187 | SCI->getName()); |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1188 | SCI->replaceAllUsesWith(New); |
| 1189 | SCI->eraseFromParent(); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1193 | // Handle 'getelementptr Ptr, Idx, i32 FieldNo ...' |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1194 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(LoadUser)) { |
| 1195 | assert(GEPI->getNumOperands() >= 3 && isa<ConstantInt>(GEPI->getOperand(2)) |
| 1196 | && "Unexpected GEPI!"); |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1198 | // Load the pointer for this field. |
| 1199 | unsigned FieldNo = cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue(); |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1200 | Value *NewPtr = GetHeapSROAValue(GEPI->getOperand(0), FieldNo, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1201 | InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Create the new GEP idx vector. |
| 1204 | SmallVector<Value*, 8> GEPIdx; |
| 1205 | GEPIdx.push_back(GEPI->getOperand(1)); |
| 1206 | GEPIdx.append(GEPI->op_begin()+3, GEPI->op_end()); |
| 1207 | |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1208 | Value *NGEPI = GetElementPtrInst::Create(NewPtr, |
| 1209 | GEPIdx.begin(), GEPIdx.end(), |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1210 | GEPI->getName(), GEPI); |
Chris Lattner | a637a8b | 2007-09-13 18:00:31 +0000 | [diff] [blame] | 1211 | GEPI->replaceAllUsesWith(NGEPI); |
| 1212 | GEPI->eraseFromParent(); |
| 1213 | return; |
| 1214 | } |
Chris Lattner | 309f20f | 2007-09-13 21:31:36 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1216 | // Recursively transform the users of PHI nodes. This will lazily create the |
| 1217 | // PHIs that are needed for individual elements. Keep track of what PHIs we |
| 1218 | // see in InsertedScalarizedValues so that we don't get infinite loops (very |
| 1219 | // antisocial). If the PHI is already in InsertedScalarizedValues, it has |
| 1220 | // already been seen first by another load, so its uses have already been |
| 1221 | // processed. |
| 1222 | PHINode *PN = cast<PHINode>(LoadUser); |
| 1223 | bool Inserted; |
| 1224 | DenseMap<Value*, std::vector<Value*> >::iterator InsertPos; |
| 1225 | tie(InsertPos, Inserted) = |
| 1226 | InsertedScalarizedValues.insert(std::make_pair(PN, std::vector<Value*>())); |
| 1227 | if (!Inserted) return; |
Chris Lattner | 309f20f | 2007-09-13 21:31:36 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1229 | // If this is the first time we've seen this PHI, recursively process all |
| 1230 | // users. |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1231 | for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; ) { |
| 1232 | Instruction *User = cast<Instruction>(*UI++); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1233 | RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1234 | } |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1237 | /// RewriteUsesOfLoadForHeapSRoA - We are performing Heap SRoA on a global. Ptr |
| 1238 | /// is a value loaded from the global. Eliminate all uses of Ptr, making them |
| 1239 | /// use FieldGlobals instead. All uses of loaded values satisfy |
Chris Lattner | 85d3d4f | 2008-12-16 21:24:51 +0000 | [diff] [blame] | 1240 | /// AllGlobalLoadUsesSimpleEnoughForHeapSRA. |
Chris Lattner | 330245e | 2007-09-13 17:29:05 +0000 | [diff] [blame] | 1241 | static void RewriteUsesOfLoadForHeapSRoA(LoadInst *Load, |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1242 | DenseMap<Value*, std::vector<Value*> > &InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1243 | std::vector<std::pair<PHINode*, unsigned> > &PHIsToRewrite) { |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1244 | for (Value::use_iterator UI = Load->use_begin(), E = Load->use_end(); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1245 | UI != E; ) { |
| 1246 | Instruction *User = cast<Instruction>(*UI++); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1247 | RewriteHeapSROALoadUser(User, InsertedScalarizedValues, PHIsToRewrite); |
Chris Lattner | f49a28c | 2008-12-17 05:42:08 +0000 | [diff] [blame] | 1248 | } |
Chris Lattner | bce4afe | 2008-12-17 05:28:49 +0000 | [diff] [blame] | 1249 | |
| 1250 | if (Load->use_empty()) { |
| 1251 | Load->eraseFromParent(); |
| 1252 | InsertedScalarizedValues.erase(Load); |
| 1253 | } |
Chris Lattner | 8639503 | 2006-09-30 23:32:09 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1256 | /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break |
| 1257 | /// it up into multiple allocations of arrays of the fields. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1258 | static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, CallInst *CI, |
| 1259 | Value* NElems, TargetData *TD) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1260 | DEBUG(dbgs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *CI << '\n'); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1261 | const Type* MAT = getMallocAllocatedType(CI); |
| 1262 | const StructType *STy = cast<StructType>(MAT); |
| 1263 | |
| 1264 | // There is guaranteed to be at least one use of the malloc (storing |
| 1265 | // it into GV). If there are other uses, change them to be uses of |
| 1266 | // the global to simplify later code. This also deletes the store |
| 1267 | // into GV. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1268 | ReplaceUsesOfMallocWithGlobal(CI, GV); |
| 1269 | |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1270 | // Okay, at this point, there are no users of the malloc. Insert N |
| 1271 | // new mallocs at the same place as CI, and N globals. |
| 1272 | std::vector<Value*> FieldGlobals; |
| 1273 | std::vector<Value*> FieldMallocs; |
| 1274 | |
| 1275 | for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){ |
| 1276 | const Type *FieldTy = STy->getElementType(FieldNo); |
| 1277 | const PointerType *PFieldTy = PointerType::getUnqual(FieldTy); |
| 1278 | |
| 1279 | GlobalVariable *NGV = |
| 1280 | new GlobalVariable(*GV->getParent(), |
| 1281 | PFieldTy, false, GlobalValue::InternalLinkage, |
| 1282 | Constant::getNullValue(PFieldTy), |
| 1283 | GV->getName() + ".f" + Twine(FieldNo), GV, |
| 1284 | GV->isThreadLocal()); |
| 1285 | FieldGlobals.push_back(NGV); |
| 1286 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1287 | unsigned TypeSize = TD->getTypeAllocSize(FieldTy); |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1288 | if (const StructType *ST = dyn_cast<StructType>(FieldTy)) |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1289 | TypeSize = TD->getStructLayout(ST)->getSizeInBytes(); |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1290 | const Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1291 | Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, |
| 1292 | ConstantInt::get(IntPtrTy, TypeSize), |
| 1293 | NElems, |
| 1294 | CI->getName() + ".f" + Twine(FieldNo)); |
Chris Lattner | 3f5e0b8 | 2010-02-26 18:23:13 +0000 | [diff] [blame] | 1295 | FieldMallocs.push_back(NMI); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1296 | new StoreInst(NMI, NGV, CI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | // The tricky aspect of this transformation is handling the case when malloc |
| 1300 | // fails. In the original code, malloc failing would set the result pointer |
| 1301 | // of malloc to null. In this case, some mallocs could succeed and others |
| 1302 | // could fail. As such, we emit code that looks like this: |
| 1303 | // F0 = malloc(field0) |
| 1304 | // F1 = malloc(field1) |
| 1305 | // F2 = malloc(field2) |
| 1306 | // if (F0 == 0 || F1 == 0 || F2 == 0) { |
| 1307 | // if (F0) { free(F0); F0 = 0; } |
| 1308 | // if (F1) { free(F1); F1 = 0; } |
| 1309 | // if (F2) { free(F2); F2 = 0; } |
| 1310 | // } |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1311 | // The malloc can also fail if its argument is too large. |
| 1312 | Constant *ConstantZero = ConstantInt::get(CI->getOperand(1)->getType(), 0); |
| 1313 | Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getOperand(1), |
| 1314 | ConstantZero, "isneg"); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1315 | for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) { |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1316 | Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i], |
| 1317 | Constant::getNullValue(FieldMallocs[i]->getType()), |
| 1318 | "isnull"); |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1319 | RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", CI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // Split the basic block at the old malloc. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1323 | BasicBlock *OrigBB = CI->getParent(); |
| 1324 | BasicBlock *ContBB = OrigBB->splitBasicBlock(CI, "malloc_cont"); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Create the block to check the first condition. Put all these blocks at the |
| 1327 | // end of the function as they are unlikely to be executed. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1328 | BasicBlock *NullPtrBlock = BasicBlock::Create(OrigBB->getContext(), |
| 1329 | "malloc_ret_null", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1330 | OrigBB->getParent()); |
| 1331 | |
| 1332 | // Remove the uncond branch from OrigBB to ContBB, turning it into a cond |
| 1333 | // branch on RunningOr. |
| 1334 | OrigBB->getTerminator()->eraseFromParent(); |
| 1335 | BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB); |
| 1336 | |
| 1337 | // Within the NullPtrBlock, we need to emit a comparison and branch for each |
| 1338 | // pointer, because some may be null while others are not. |
| 1339 | for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { |
| 1340 | Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock); |
| 1341 | Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal, |
| 1342 | Constant::getNullValue(GVVal->getType()), |
| 1343 | "tmp"); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1344 | BasicBlock *FreeBlock = BasicBlock::Create(Cmp->getContext(), "free_it", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1345 | OrigBB->getParent()); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1346 | BasicBlock *NextBlock = BasicBlock::Create(Cmp->getContext(), "next", |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1347 | OrigBB->getParent()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1348 | Instruction *BI = BranchInst::Create(FreeBlock, NextBlock, |
| 1349 | Cmp, NullPtrBlock); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Fill in FreeBlock. |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 1352 | CallInst::CreateFree(GVVal, BI); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1353 | new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i], |
| 1354 | FreeBlock); |
| 1355 | BranchInst::Create(NextBlock, FreeBlock); |
| 1356 | |
| 1357 | NullPtrBlock = NextBlock; |
| 1358 | } |
| 1359 | |
| 1360 | BranchInst::Create(ContBB, NullPtrBlock); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1361 | |
| 1362 | // CI is no longer needed, remove it. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1363 | CI->eraseFromParent(); |
| 1364 | |
| 1365 | /// InsertedScalarizedLoads - As we process loads, if we can't immediately |
| 1366 | /// update all uses of the load, keep track of what scalarized loads are |
| 1367 | /// inserted for a given load. |
| 1368 | DenseMap<Value*, std::vector<Value*> > InsertedScalarizedValues; |
| 1369 | InsertedScalarizedValues[GV] = FieldGlobals; |
| 1370 | |
| 1371 | std::vector<std::pair<PHINode*, unsigned> > PHIsToRewrite; |
| 1372 | |
| 1373 | // Okay, the malloc site is completely handled. All of the uses of GV are now |
| 1374 | // loads, and all uses of those loads are simple. Rewrite them to use loads |
| 1375 | // of the per-field globals instead. |
| 1376 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) { |
| 1377 | Instruction *User = cast<Instruction>(*UI++); |
| 1378 | |
| 1379 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1380 | RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1381 | continue; |
| 1382 | } |
| 1383 | |
| 1384 | // Must be a store of null. |
| 1385 | StoreInst *SI = cast<StoreInst>(User); |
| 1386 | assert(isa<ConstantPointerNull>(SI->getOperand(0)) && |
| 1387 | "Unexpected heap-sra user!"); |
| 1388 | |
| 1389 | // Insert a store of null into each global. |
| 1390 | for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) { |
| 1391 | const PointerType *PT = cast<PointerType>(FieldGlobals[i]->getType()); |
| 1392 | Constant *Null = Constant::getNullValue(PT->getElementType()); |
| 1393 | new StoreInst(Null, FieldGlobals[i], SI); |
| 1394 | } |
| 1395 | // Erase the original store. |
| 1396 | SI->eraseFromParent(); |
| 1397 | } |
| 1398 | |
| 1399 | // While we have PHIs that are interesting to rewrite, do it. |
| 1400 | while (!PHIsToRewrite.empty()) { |
| 1401 | PHINode *PN = PHIsToRewrite.back().first; |
| 1402 | unsigned FieldNo = PHIsToRewrite.back().second; |
| 1403 | PHIsToRewrite.pop_back(); |
| 1404 | PHINode *FieldPN = cast<PHINode>(InsertedScalarizedValues[PN][FieldNo]); |
| 1405 | assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi"); |
| 1406 | |
| 1407 | // Add all the incoming values. This can materialize more phis. |
| 1408 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1409 | Value *InVal = PN->getIncomingValue(i); |
| 1410 | InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1411 | PHIsToRewrite); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1412 | FieldPN->addIncoming(InVal, PN->getIncomingBlock(i)); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | // Drop all inter-phi links and any loads that made it this far. |
| 1417 | for (DenseMap<Value*, std::vector<Value*> >::iterator |
| 1418 | I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); |
| 1419 | I != E; ++I) { |
| 1420 | if (PHINode *PN = dyn_cast<PHINode>(I->first)) |
| 1421 | PN->dropAllReferences(); |
| 1422 | else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) |
| 1423 | LI->dropAllReferences(); |
| 1424 | } |
| 1425 | |
| 1426 | // Delete all the phis and loads now that inter-references are dead. |
| 1427 | for (DenseMap<Value*, std::vector<Value*> >::iterator |
| 1428 | I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end(); |
| 1429 | I != E; ++I) { |
| 1430 | if (PHINode *PN = dyn_cast<PHINode>(I->first)) |
| 1431 | PN->eraseFromParent(); |
| 1432 | else if (LoadInst *LI = dyn_cast<LoadInst>(I->first)) |
| 1433 | LI->eraseFromParent(); |
| 1434 | } |
| 1435 | |
| 1436 | // The old global is now dead, remove it. |
| 1437 | GV->eraseFromParent(); |
| 1438 | |
| 1439 | ++NumHeapSRA; |
| 1440 | return cast<GlobalVariable>(FieldGlobals[0]); |
| 1441 | } |
| 1442 | |
Chris Lattner | e61d0a6 | 2008-12-15 21:02:25 +0000 | [diff] [blame] | 1443 | /// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a |
| 1444 | /// pointer global variable with a single value stored it that is a malloc or |
| 1445 | /// cast of malloc. |
| 1446 | static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1447 | CallInst *CI, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1448 | const Type *AllocTy, |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1449 | Module::global_iterator &GVI, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1450 | TargetData *TD) { |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1451 | // If this is a malloc of an abstract type, don't touch it. |
| 1452 | if (!AllocTy->isSized()) |
| 1453 | return false; |
| 1454 | |
| 1455 | // We can't optimize this global unless all uses of it are *known* to be |
| 1456 | // of the malloc value, not of the null initializer value (consider a use |
| 1457 | // that compares the global's value against zero to see if the malloc has |
| 1458 | // been reached). To do this, we check to see if all uses of the global |
| 1459 | // would trap if the global were null: this proves that they must all |
| 1460 | // happen after the malloc. |
| 1461 | if (!AllUsesOfLoadedValueWillTrapIfNull(GV)) |
| 1462 | return false; |
| 1463 | |
| 1464 | // We can't optimize this if the malloc itself is used in a complex way, |
| 1465 | // for example, being stored into multiple globals. This allows the |
| 1466 | // malloc to be stored into the specified global, loaded setcc'd, and |
| 1467 | // GEP'd. These are all things we could transform to using the global |
| 1468 | // for. |
| 1469 | { |
| 1470 | SmallPtrSet<PHINode*, 8> PHIs; |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1471 | if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(CI, GV, PHIs)) |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
| 1474 | |
| 1475 | // If we have a global that is only initialized with a fixed size malloc, |
| 1476 | // transform the program to use global memory instead of malloc'd memory. |
| 1477 | // This eliminates dynamic allocation, avoids an indirection accessing the |
| 1478 | // data, and exposes the resultant global to further GlobalOpt. |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1479 | // We cannot optimize the malloc if we cannot determine malloc array size. |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1480 | if (Value *NElems = getMallocArraySize(CI, TD, true)) { |
Victor Hernandez | 2491ce0 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 1481 | if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) |
| 1482 | // Restrict this transformation to only working on small allocations |
| 1483 | // (2048 bytes currently), as we don't want to introduce a 16M global or |
| 1484 | // something. |
| 1485 | if (TD && |
| 1486 | NElements->getZExtValue() * TD->getTypeAllocSize(AllocTy) < 2048) { |
Chris Lattner | a687465 | 2010-02-25 22:33:52 +0000 | [diff] [blame] | 1487 | GVI = OptimizeGlobalAddressOfMalloc(GV, CI, AllocTy, NElements, TD); |
Victor Hernandez | 2491ce0 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 1488 | return true; |
| 1489 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1490 | |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1491 | // If the allocation is an array of structures, consider transforming this |
| 1492 | // into multiple malloc'd arrays, one for each field. This is basically |
| 1493 | // SRoA for malloc'd memory. |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1494 | |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1495 | // If this is an allocation of a fixed size array of structs, analyze as a |
| 1496 | // variable size array. malloc [100 x struct],1 -> malloc struct, 100 |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 1497 | if (NElems == ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1498 | if (const ArrayType *AT = dyn_cast<ArrayType>(AllocTy)) |
| 1499 | AllocTy = AT->getElementType(); |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1500 | |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1501 | if (const StructType *AllocSTy = dyn_cast<StructType>(AllocTy)) { |
| 1502 | // This the structure has an unreasonable number of fields, leave it |
| 1503 | // alone. |
| 1504 | if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 && |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1505 | AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, CI)) { |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1506 | |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1507 | // If this is a fixed size array, transform the Malloc to be an alloc of |
| 1508 | // structs. malloc [100 x struct],1 -> malloc struct, 100 |
| 1509 | if (const ArrayType *AT = |
| 1510 | dyn_cast<ArrayType>(getMallocAllocatedType(CI))) { |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1511 | const Type *IntPtrTy = TD->getIntPtrType(CI->getContext()); |
| 1512 | unsigned TypeSize = TD->getStructLayout(AllocSTy)->getSizeInBytes(); |
| 1513 | Value *AllocSize = ConstantInt::get(IntPtrTy, TypeSize); |
| 1514 | Value *NumElements = ConstantInt::get(IntPtrTy, AT->getNumElements()); |
| 1515 | Instruction *Malloc = CallInst::CreateMalloc(CI, IntPtrTy, AllocSTy, |
| 1516 | AllocSize, NumElements, |
| 1517 | CI->getName()); |
| 1518 | Instruction *Cast = new BitCastInst(Malloc, CI->getType(), "tmp", CI); |
| 1519 | CI->replaceAllUsesWith(Cast); |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1520 | CI->eraseFromParent(); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1521 | CI = dyn_cast<BitCastInst>(Malloc) ? |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1522 | extractMallocCallFromBitCast(Malloc) : cast<CallInst>(Malloc); |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1523 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1524 | |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 1525 | GVI = PerformHeapAllocSRoA(GV, CI, getMallocArraySize(CI, TD, true),TD); |
Victor Hernandez | 8db42d2 | 2009-10-16 23:12:25 +0000 | [diff] [blame] | 1526 | return true; |
| 1527 | } |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | return false; |
| 1532 | } |
| 1533 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1534 | // OptimizeOnceStoredGlobal - Try to optimize globals based on the knowledge |
| 1535 | // 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] | 1536 | static bool OptimizeOnceStoredGlobal(GlobalVariable *GV, Value *StoredOnceVal, |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 1537 | Module::global_iterator &GVI, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1538 | TargetData *TD) { |
Chris Lattner | 344b41c | 2008-12-15 21:20:32 +0000 | [diff] [blame] | 1539 | // Ignore no-op GEPs and bitcasts. |
| 1540 | StoredOnceVal = StoredOnceVal->stripPointerCasts(); |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1541 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1542 | // If we are dealing with a pointer global that is initialized to null and |
| 1543 | // only has one (non-null) value stored into it, then we can optimize any |
| 1544 | // users of the loaded value (often calls and loads) that would trap if the |
| 1545 | // value was null. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1546 | if (GV->getInitializer()->getType()->isPointerTy() && |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1547 | GV->getInitializer()->isNullValue()) { |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1548 | if (Constant *SOVC = dyn_cast<Constant>(StoredOnceVal)) { |
| 1549 | if (GV->getInitializer()->getType() != SOVC->getType()) |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 1550 | SOVC = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1551 | ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1553 | // Optimize away any trapping uses of the loaded value. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1554 | if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC)) |
Chris Lattner | 8be8012 | 2004-10-10 17:07:12 +0000 | [diff] [blame] | 1555 | return true; |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1556 | } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) { |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 1557 | const Type* MallocType = getMallocAllocatedType(CI); |
| 1558 | if (MallocType && TryToOptimizeStoreOfMallocToGlobal(GV, CI, MallocType, |
| 1559 | GVI, TD)) |
| 1560 | return true; |
Chris Lattner | 708148e | 2004-10-10 23:14:11 +0000 | [diff] [blame] | 1561 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1562 | } |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1564 | return false; |
| 1565 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1567 | /// TryToShrinkGlobalToBoolean - At this point, we have learned that the only |
| 1568 | /// two values ever stored into GV are its initializer and OtherVal. See if we |
| 1569 | /// can shrink the global into a boolean and select between the two values |
| 1570 | /// whenever it is used. This exposes the values to other scalar optimizations. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1571 | static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1572 | const Type *GVElType = GV->getType()->getElementType(); |
| 1573 | |
| 1574 | // 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] | 1575 | // an FP value, pointer or vector, don't do this optimization because a select |
| 1576 | // between them is very expensive and unlikely to lead to later |
| 1577 | // simplification. In these cases, we typically end up with "cond ? v1 : v2" |
| 1578 | // where v1 and v2 both require constant pool loads, a big loss. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1579 | if (GVElType == Type::getInt1Ty(GV->getContext()) || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1580 | GVElType->isFloatingPointTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1581 | GVElType->isPointerTy() || GVElType->isVectorTy()) |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1582 | return false; |
| 1583 | |
| 1584 | // Walk the use list of the global seeing if all the uses are load or store. |
| 1585 | // If there is anything else, bail out. |
| 1586 | for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); I != E; ++I) |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1587 | if (!isa<LoadInst>(I) && !isa<StoreInst>(I)) |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1588 | return false; |
| 1589 | |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1590 | DEBUG(dbgs() << " *** SHRINKING TO BOOL: " << *GV); |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1591 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1592 | // Create the new global, initializing it to false. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1593 | GlobalVariable *NewGV = new GlobalVariable(Type::getInt1Ty(GV->getContext()), |
| 1594 | false, |
| 1595 | GlobalValue::InternalLinkage, |
| 1596 | ConstantInt::getFalse(GV->getContext()), |
Nick Lewycky | 0e670df | 2009-05-03 03:49:08 +0000 | [diff] [blame] | 1597 | GV->getName()+".b", |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 1598 | GV->isThreadLocal()); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1599 | GV->getParent()->getGlobalList().insert(GV, NewGV); |
| 1600 | |
| 1601 | Constant *InitVal = GV->getInitializer(); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1602 | assert(InitVal->getType() != Type::getInt1Ty(GV->getContext()) && |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1603 | "No reason to shrink to bool!"); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1604 | |
| 1605 | // If initialized to zero and storing one into the global, we can use a cast |
| 1606 | // instead of a select to synthesize the desired value. |
| 1607 | bool IsOneZero = false; |
| 1608 | if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 1609 | IsOneZero = InitVal->isNullValue() && CI->isOne(); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1610 | |
| 1611 | while (!GV->use_empty()) { |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1612 | Instruction *UI = cast<Instruction>(GV->use_back()); |
| 1613 | if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1614 | // Change the store into a boolean store. |
| 1615 | bool StoringOther = SI->getOperand(0) == OtherVal; |
| 1616 | // Only do this if we weren't storing a loaded value. |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1617 | Value *StoreVal; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1618 | if (StoringOther || SI->getOperand(0) == InitVal) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1619 | StoreVal = ConstantInt::get(Type::getInt1Ty(GV->getContext()), |
| 1620 | StoringOther); |
Chris Lattner | 38c2556 | 2004-12-12 19:34:41 +0000 | [diff] [blame] | 1621 | else { |
| 1622 | // Otherwise, we are storing a previously loaded copy. To do this, |
| 1623 | // change the copy from copying the original value to just copying the |
| 1624 | // bool. |
| 1625 | Instruction *StoredVal = cast<Instruction>(SI->getOperand(0)); |
| 1626 | |
| 1627 | // If we're already replaced the input, StoredVal will be a cast or |
| 1628 | // select instruction. If not, it will be a load of the original |
| 1629 | // global. |
| 1630 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 1631 | assert(LI->getOperand(0) == GV && "Not a copy!"); |
| 1632 | // Insert a new load, to preserve the saved value. |
| 1633 | StoreVal = new LoadInst(NewGV, LI->getName()+".b", LI); |
| 1634 | } else { |
| 1635 | assert((isa<CastInst>(StoredVal) || isa<SelectInst>(StoredVal)) && |
| 1636 | "This is not a form that we understand!"); |
| 1637 | StoreVal = StoredVal->getOperand(0); |
| 1638 | assert(isa<LoadInst>(StoreVal) && "Not a load of NewGV!"); |
| 1639 | } |
| 1640 | } |
| 1641 | new StoreInst(StoreVal, NewGV, SI); |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1642 | } else { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1643 | // Change the load into a load of bool then a select. |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1644 | LoadInst *LI = cast<LoadInst>(UI); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 1645 | LoadInst *NLI = new LoadInst(NewGV, LI->getName()+".b", LI); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1646 | Value *NSI; |
| 1647 | if (IsOneZero) |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 1648 | NSI = new ZExtInst(NLI, LI->getType(), "", LI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1649 | else |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1650 | NSI = SelectInst::Create(NLI, OtherVal, InitVal, "", LI); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 1651 | NSI->takeName(LI); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1652 | LI->replaceAllUsesWith(NSI); |
Devang Patel | 771281f | 2009-03-06 01:39:36 +0000 | [diff] [blame] | 1653 | } |
| 1654 | UI->eraseFromParent(); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | GV->eraseFromParent(); |
Chris Lattner | 58e44f4 | 2008-01-14 01:17:44 +0000 | [diff] [blame] | 1658 | return true; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1662 | /// ProcessInternalGlobal - Analyze the specified global variable and optimize |
| 1663 | /// it if possible. If we make a change, return true. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1664 | bool GlobalOpt::ProcessInternalGlobal(GlobalVariable *GV, |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1665 | Module::global_iterator &GVI) { |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1666 | SmallPtrSet<const PHINode*, 16> PHIUsers; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1667 | GlobalStatus GS; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1668 | GV->removeDeadConstantUsers(); |
| 1669 | |
| 1670 | if (GV->use_empty()) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1671 | DEBUG(dbgs() << "GLOBAL DEAD: " << *GV); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1672 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1673 | ++NumDeleted; |
| 1674 | return true; |
| 1675 | } |
| 1676 | |
| 1677 | if (!AnalyzeGlobal(GV, GS, PHIUsers)) { |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1678 | #if 0 |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1679 | DEBUG(dbgs() << "Global: " << *GV); |
| 1680 | DEBUG(dbgs() << " isLoaded = " << GS.isLoaded << "\n"); |
| 1681 | DEBUG(dbgs() << " StoredType = "); |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1682 | switch (GS.StoredType) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1683 | case GlobalStatus::NotStored: DEBUG(dbgs() << "NEVER STORED\n"); break; |
| 1684 | case GlobalStatus::isInitializerStored: DEBUG(dbgs() << "INIT STORED\n"); |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1685 | break; |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1686 | case GlobalStatus::isStoredOnce: DEBUG(dbgs() << "STORED ONCE\n"); break; |
| 1687 | case GlobalStatus::isStored: DEBUG(dbgs() << "stored\n"); break; |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1688 | } |
| 1689 | if (GS.StoredType == GlobalStatus::isStoredOnce && GS.StoredOnceValue) |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1690 | DEBUG(dbgs() << " StoredOnceValue = " << *GS.StoredOnceValue << "\n"); |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1691 | if (GS.AccessingFunction && !GS.HasMultipleAccessingFunctions) |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1692 | DEBUG(dbgs() << " AccessingFunction = " << GS.AccessingFunction->getName() |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1693 | << "\n"); |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1694 | DEBUG(dbgs() << " HasMultipleAccessingFunctions = " |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1695 | << GS.HasMultipleAccessingFunctions << "\n"); |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1696 | DEBUG(dbgs() << " HasNonInstructionUser = " |
Victor Hernandez | 631f3c0 | 2009-11-07 00:41:19 +0000 | [diff] [blame] | 1697 | << GS.HasNonInstructionUser<<"\n"); |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1698 | DEBUG(dbgs() << "\n"); |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1699 | #endif |
| 1700 | |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1701 | // If this is a first class global and has only one accessing function |
| 1702 | // and this function is main (which we know is not recursive we can make |
| 1703 | // this global a local variable) we replace the global with a local alloca |
| 1704 | // in this function. |
| 1705 | // |
Dan Gohman | 399101a | 2008-05-23 00:17:26 +0000 | [diff] [blame] | 1706 | // NOTE: It doesn't make sense to promote non single-value types since we |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1707 | // are just replacing static memory to stack memory. |
Sanjiv Gupta | 059aa8c | 2009-06-17 06:47:15 +0000 | [diff] [blame] | 1708 | // |
| 1709 | // If the global is in different address space, don't bring it to stack. |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1710 | if (!GS.HasMultipleAccessingFunctions && |
Chris Lattner | 553ca52 | 2005-06-15 21:11:48 +0000 | [diff] [blame] | 1711 | GS.AccessingFunction && !GS.HasNonInstructionUser && |
Dan Gohman | 399101a | 2008-05-23 00:17:26 +0000 | [diff] [blame] | 1712 | GV->getType()->getElementType()->isSingleValueType() && |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1713 | GS.AccessingFunction->getName() == "main" && |
Sanjiv Gupta | 059aa8c | 2009-06-17 06:47:15 +0000 | [diff] [blame] | 1714 | GS.AccessingFunction->hasExternalLinkage() && |
| 1715 | GV->getType()->getAddressSpace() == 0) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1716 | DEBUG(dbgs() << "LOCALIZING GLOBAL: " << *GV); |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1717 | Instruction& FirstI = const_cast<Instruction&>(*GS.AccessingFunction |
| 1718 | ->getEntryBlock().begin()); |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1719 | const Type* ElemTy = GV->getType()->getElementType(); |
Nate Begeman | 14b0529 | 2005-11-05 09:21:28 +0000 | [diff] [blame] | 1720 | // FIXME: Pass Global's alignment when globals have alignment |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1721 | AllocaInst* Alloca = new AllocaInst(ElemTy, NULL, GV->getName(), &FirstI); |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1722 | if (!isa<UndefValue>(GV->getInitializer())) |
Gabor Greif | c8b82cc | 2010-04-01 08:21:08 +0000 | [diff] [blame] | 1723 | new StoreInst(GV->getInitializer(), Alloca, &FirstI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1724 | |
Alkis Evlogimenos | f64ea9d | 2005-02-10 18:36:30 +0000 | [diff] [blame] | 1725 | GV->replaceAllUsesWith(Alloca); |
| 1726 | GV->eraseFromParent(); |
| 1727 | ++NumLocalized; |
| 1728 | return true; |
| 1729 | } |
Chris Lattner | cff1673 | 2006-09-30 19:40:30 +0000 | [diff] [blame] | 1730 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1731 | // If the global is never loaded (but may be stored to), it is dead. |
| 1732 | // Delete it now. |
| 1733 | if (!GS.isLoaded) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1734 | DEBUG(dbgs() << "GLOBAL NEVER LOADED: " << *GV); |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1736 | // Delete any stores we can find to the global. We may not be able to |
| 1737 | // make it completely dead though. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1738 | bool Changed = CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1740 | // If the global is dead now, delete it. |
| 1741 | if (GV->use_empty()) { |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1742 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1743 | ++NumDeleted; |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 1744 | Changed = true; |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1745 | } |
Chris Lattner | 930f475 | 2004-10-09 03:32:52 +0000 | [diff] [blame] | 1746 | return Changed; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1747 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1748 | } else if (GS.StoredType <= GlobalStatus::isInitializerStored) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1749 | DEBUG(dbgs() << "MARKING CONSTANT: " << *GV); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1750 | GV->setConstant(true); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1752 | // Clean up any obviously simplifiable users now. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1753 | CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1754 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1755 | // If the global is dead now, just nuke it. |
| 1756 | if (GV->use_empty()) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1757 | DEBUG(dbgs() << " *** Marking constant allowed us to simplify " |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1758 | << "all users and delete global!\n"); |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1759 | GV->eraseFromParent(); |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1760 | ++NumDeleted; |
| 1761 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1762 | |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1763 | ++NumMarked; |
| 1764 | return true; |
Dan Gohman | 399101a | 2008-05-23 00:17:26 +0000 | [diff] [blame] | 1765 | } else if (!GV->getInitializer()->getType()->isSingleValueType()) { |
Dan Gohman | 7eb28f3 | 2009-08-14 00:11:03 +0000 | [diff] [blame] | 1766 | if (TargetData *TD = getAnalysisIfAvailable<TargetData>()) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1767 | if (GlobalVariable *FirstNewGV = SRAGlobal(GV, *TD)) { |
Dan Gohman | 7eb28f3 | 2009-08-14 00:11:03 +0000 | [diff] [blame] | 1768 | GVI = FirstNewGV; // Don't skip the newly produced globals! |
| 1769 | return true; |
| 1770 | } |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1771 | } else if (GS.StoredType == GlobalStatus::isStoredOnce) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1772 | // If the initial value for the global was an undef value, and if only |
| 1773 | // one other value was stored into it, we can just change the |
Duncan Sands | b502440 | 2009-01-13 13:48:44 +0000 | [diff] [blame] | 1774 | // initializer to be the stored value, then delete all stores to the |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1775 | // global. This allows us to mark it constant. |
| 1776 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
| 1777 | if (isa<UndefValue>(GV->getInitializer())) { |
| 1778 | // Change the initial value here. |
| 1779 | GV->setInitializer(SOVConstant); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1780 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1781 | // Clean up any obviously simplifiable users now. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1782 | CleanupConstantGlobalUsers(GV, GV->getInitializer()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1784 | if (GV->use_empty()) { |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 1785 | DEBUG(dbgs() << " *** Substituting initializer allowed us to " |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 1786 | << "simplify all users and delete global!\n"); |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1787 | GV->eraseFromParent(); |
| 1788 | ++NumDeleted; |
| 1789 | } else { |
| 1790 | GVI = GV; |
| 1791 | } |
| 1792 | ++NumSubstitute; |
| 1793 | return true; |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1794 | } |
Chris Lattner | 7a7ed02 | 2004-10-16 18:09:00 +0000 | [diff] [blame] | 1795 | |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1796 | // Try to optimize globals based on the knowledge that only one value |
| 1797 | // (besides its initializer) is ever stored to the global. |
Chris Lattner | 30ba569 | 2004-10-11 05:54:41 +0000 | [diff] [blame] | 1798 | if (OptimizeOnceStoredGlobal(GV, GS.StoredOnceValue, GVI, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1799 | getAnalysisIfAvailable<TargetData>())) |
Chris Lattner | 9b34a61 | 2004-10-09 21:48:45 +0000 | [diff] [blame] | 1800 | return true; |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1801 | |
| 1802 | // Otherwise, if the global was not a boolean, we can shrink it to be a |
| 1803 | // boolean. |
| 1804 | if (Constant *SOVConstant = dyn_cast<Constant>(GS.StoredOnceValue)) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1805 | if (TryToShrinkGlobalToBoolean(GV, SOVConstant)) { |
Chris Lattner | 96a86b2 | 2004-12-12 05:53:50 +0000 | [diff] [blame] | 1806 | ++NumShrunkToBool; |
| 1807 | return true; |
| 1808 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 1814 | /// ChangeCalleesToFastCall - Walk all of the direct calls of the specified |
| 1815 | /// function, changing them to FastCC. |
| 1816 | static void ChangeCalleesToFastCall(Function *F) { |
| 1817 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 1818 | CallSite User(cast<Instruction>(*UI)); |
| 1819 | User.setCallingConv(CallingConv::Fast); |
Chris Lattner | fb217ad | 2005-05-08 22:18:06 +0000 | [diff] [blame] | 1820 | } |
| 1821 | } |
Chris Lattner | a4be1dc | 2004-10-08 20:59:28 +0000 | [diff] [blame] | 1822 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1823 | static AttrListPtr StripNest(const AttrListPtr &Attrs) { |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1824 | for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1825 | if ((Attrs.getSlot(i).Attrs & Attribute::Nest) == 0) |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 1826 | continue; |
| 1827 | |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 1828 | // There can be only one. |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1829 | return Attrs.removeAttr(Attrs.getSlot(i).Index, Attribute::Nest); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | return Attrs; |
| 1833 | } |
| 1834 | |
| 1835 | static void RemoveNestAttribute(Function *F) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1836 | F->setAttributes(StripNest(F->getAttributes())); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1837 | for (Value::use_iterator UI = F->use_begin(), E = F->use_end(); UI != E;++UI){ |
Duncan Sands | 548448a | 2008-02-18 17:32:13 +0000 | [diff] [blame] | 1838 | CallSite User(cast<Instruction>(*UI)); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1839 | User.setAttributes(StripNest(User.getAttributes())); |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1840 | } |
| 1841 | } |
| 1842 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1843 | bool GlobalOpt::OptimizeFunctions(Module &M) { |
| 1844 | bool Changed = false; |
| 1845 | // Optimize functions. |
| 1846 | for (Module::iterator FI = M.begin(), E = M.end(); FI != E; ) { |
| 1847 | Function *F = FI++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 1848 | // Functions without names cannot be referenced outside this module. |
| 1849 | if (!F->hasName() && !F->isDeclaration()) |
| 1850 | F->setLinkage(GlobalValue::InternalLinkage); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1851 | F->removeDeadConstantUsers(); |
Chris Lattner | ec4c7b9 | 2009-11-01 19:03:42 +0000 | [diff] [blame] | 1852 | if (F->use_empty() && (F->hasLocalLinkage() || F->hasLinkOnceLinkage())) { |
| 1853 | F->eraseFromParent(); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1854 | Changed = true; |
| 1855 | ++NumFnDeleted; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1856 | } else if (F->hasLocalLinkage()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1857 | if (F->getCallingConv() == CallingConv::C && !F->isVarArg() && |
Jay Foad | 757068f | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 1858 | !F->hasAddressTaken()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1859 | // If this function has C calling conventions, is not a varargs |
| 1860 | // function, and is only called directly, promote it to use the Fast |
| 1861 | // calling convention. |
| 1862 | F->setCallingConv(CallingConv::Fast); |
| 1863 | ChangeCalleesToFastCall(F); |
| 1864 | ++NumFastCallFns; |
| 1865 | Changed = true; |
| 1866 | } |
| 1867 | |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1868 | if (F->getAttributes().hasAttrSomewhere(Attribute::Nest) && |
Jay Foad | 757068f | 2009-06-10 08:41:11 +0000 | [diff] [blame] | 1869 | !F->hasAddressTaken()) { |
Duncan Sands | 3d5378f | 2008-02-16 20:56:04 +0000 | [diff] [blame] | 1870 | // The function is not used by a trampoline intrinsic, so it is safe |
| 1871 | // to remove the 'nest' attribute. |
| 1872 | RemoveNestAttribute(F); |
| 1873 | ++NumNestRemoved; |
| 1874 | Changed = true; |
| 1875 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | return Changed; |
| 1879 | } |
| 1880 | |
| 1881 | bool GlobalOpt::OptimizeGlobalVars(Module &M) { |
| 1882 | bool Changed = false; |
| 1883 | for (Module::global_iterator GVI = M.global_begin(), E = M.global_end(); |
| 1884 | GVI != E; ) { |
| 1885 | GlobalVariable *GV = GVI++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 1886 | // Global variables without names cannot be referenced outside this module. |
| 1887 | if (!GV->hasName() && !GV->isDeclaration()) |
| 1888 | GV->setLinkage(GlobalValue::InternalLinkage); |
Dan Gohman | 01b97dd | 2009-11-23 16:22:21 +0000 | [diff] [blame] | 1889 | // Simplify the initializer. |
| 1890 | if (GV->hasInitializer()) |
| 1891 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GV->getInitializer())) { |
Dan Gohman | f860db2 | 2010-04-02 03:04:37 +0000 | [diff] [blame] | 1892 | TargetData *TD = getAnalysisIfAvailable<TargetData>(); |
Dan Gohman | 01b97dd | 2009-11-23 16:22:21 +0000 | [diff] [blame] | 1893 | Constant *New = ConstantFoldConstantExpression(CE, TD); |
| 1894 | if (New && New != CE) |
| 1895 | GV->setInitializer(New); |
| 1896 | } |
| 1897 | // Do more involved optimizations if the global is internal. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1898 | if (!GV->isConstant() && GV->hasLocalLinkage() && |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1899 | GV->hasInitializer()) |
| 1900 | Changed |= ProcessInternalGlobal(GV, GVI); |
| 1901 | } |
| 1902 | return Changed; |
| 1903 | } |
| 1904 | |
| 1905 | /// FindGlobalCtors - Find the llvm.globalctors list, verifying that all |
| 1906 | /// initializers have an init priority of 65535. |
| 1907 | GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) { |
Alkis Evlogimenos | e9c6d36 | 2005-10-25 11:18:06 +0000 | [diff] [blame] | 1908 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 1909 | I != E; ++I) |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1910 | if (I->getName() == "llvm.global_ctors") { |
| 1911 | // Found it, verify it's an array of { int, void()* }. |
| 1912 | const ArrayType *ATy =dyn_cast<ArrayType>(I->getType()->getElementType()); |
| 1913 | if (!ATy) return 0; |
| 1914 | const StructType *STy = dyn_cast<StructType>(ATy->getElementType()); |
| 1915 | if (!STy || STy->getNumElements() != 2 || |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1916 | !STy->getElementType(0)->isIntegerTy(32)) return 0; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1917 | const PointerType *PFTy = dyn_cast<PointerType>(STy->getElementType(1)); |
| 1918 | if (!PFTy) return 0; |
| 1919 | const FunctionType *FTy = dyn_cast<FunctionType>(PFTy->getElementType()); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1920 | if (!FTy || !FTy->getReturnType()->isVoidTy() || |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1921 | FTy->isVarArg() || FTy->getNumParams() != 0) |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1922 | return 0; |
| 1923 | |
| 1924 | // Verify that the initializer is simple enough for us to handle. |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 1925 | if (!I->hasDefinitiveInitializer()) return 0; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1926 | ConstantArray *CA = dyn_cast<ConstantArray>(I->getInitializer()); |
| 1927 | if (!CA) return 0; |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 1928 | for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) |
| 1929 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(*i)) { |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 1930 | if (isa<ConstantPointerNull>(CS->getOperand(1))) |
| 1931 | continue; |
| 1932 | |
| 1933 | // Must have a function or null ptr. |
| 1934 | if (!isa<Function>(CS->getOperand(1))) |
| 1935 | return 0; |
| 1936 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1937 | // Init priority must be standard. |
| 1938 | ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1939 | if (!CI || CI->getZExtValue() != 65535) |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1940 | return 0; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1941 | } else { |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | return I; |
| 1946 | } |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1950 | /// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand, |
| 1951 | /// return a list of the functions and null terminator as a vector. |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1952 | static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { |
| 1953 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
| 1954 | std::vector<Function*> Result; |
| 1955 | Result.reserve(CA->getNumOperands()); |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 1956 | for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) { |
| 1957 | ConstantStruct *CS = cast<ConstantStruct>(*i); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 1958 | Result.push_back(dyn_cast<Function>(CS->getOperand(1))); |
| 1959 | } |
| 1960 | return Result; |
| 1961 | } |
| 1962 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1963 | /// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the |
| 1964 | /// specified array, returning the new global to use. |
| 1965 | static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1966 | const std::vector<Function*> &Ctors) { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1967 | // If we made a change, reassemble the initializer list. |
| 1968 | std::vector<Constant*> CSVals; |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1969 | CSVals.push_back(ConstantInt::get(Type::getInt32Ty(GCL->getContext()),65535)); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1970 | CSVals.push_back(0); |
| 1971 | |
| 1972 | // Create the new init list. |
| 1973 | std::vector<Constant*> CAList; |
| 1974 | for (unsigned i = 0, e = Ctors.size(); i != e; ++i) { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1975 | if (Ctors[i]) { |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1976 | CSVals[1] = Ctors[i]; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 1977 | } else { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1978 | const Type *FTy = FunctionType::get(Type::getVoidTy(GCL->getContext()), |
| 1979 | false); |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1980 | const PointerType *PFTy = PointerType::getUnqual(FTy); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1981 | CSVals[1] = Constant::getNullValue(PFTy); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1982 | CSVals[0] = ConstantInt::get(Type::getInt32Ty(GCL->getContext()), |
| 1983 | 2147483647); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1984 | } |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 1985 | CAList.push_back(ConstantStruct::get(GCL->getContext(), CSVals, false)); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | // Create the array initializer. |
| 1989 | const Type *StructTy = |
Nick Lewycky | c332fba | 2009-09-19 20:30:26 +0000 | [diff] [blame] | 1990 | cast<ArrayType>(GCL->getType()->getElementType())->getElementType(); |
Owen Anderson | 1fd7096 | 2009-07-28 18:32:17 +0000 | [diff] [blame] | 1991 | Constant *CA = ConstantArray::get(ArrayType::get(StructTy, |
Nick Lewycky | c332fba | 2009-09-19 20:30:26 +0000 | [diff] [blame] | 1992 | CAList.size()), CAList); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 1993 | |
| 1994 | // If we didn't change the number of elements, don't create a new GV. |
| 1995 | if (CA->getType() == GCL->getInitializer()->getType()) { |
| 1996 | GCL->setInitializer(CA); |
| 1997 | return GCL; |
| 1998 | } |
| 1999 | |
| 2000 | // Create the new global and insert it next to the existing list. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2001 | GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2002 | GCL->getLinkage(), CA, "", |
Lauro Ramos Venancio | c763552 | 2007-04-12 18:32:50 +0000 | [diff] [blame] | 2003 | GCL->isThreadLocal()); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2004 | GCL->getParent()->getGlobalList().insert(GCL, NGV); |
Chris Lattner | 046800a | 2007-02-11 01:08:35 +0000 | [diff] [blame] | 2005 | NGV->takeName(GCL); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2006 | |
| 2007 | // Nuke the old list, replacing any uses with the new one. |
| 2008 | if (!GCL->use_empty()) { |
| 2009 | Constant *V = NGV; |
| 2010 | if (V->getType() != GCL->getType()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2011 | V = ConstantExpr::getBitCast(V, GCL->getType()); |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2012 | GCL->replaceAllUsesWith(V); |
| 2013 | } |
| 2014 | GCL->eraseFromParent(); |
| 2015 | |
| 2016 | if (Ctors.size()) |
| 2017 | return NGV; |
| 2018 | else |
| 2019 | return 0; |
| 2020 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2021 | |
| 2022 | |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2023 | static Constant *getVal(DenseMap<Value*, Constant*> &ComputedValues, |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2024 | Value *V) { |
| 2025 | if (Constant *CV = dyn_cast<Constant>(V)) return CV; |
| 2026 | Constant *R = ComputedValues[V]; |
| 2027 | assert(R && "Reference to an uncomputed value!"); |
| 2028 | return R; |
| 2029 | } |
| 2030 | |
| 2031 | /// isSimpleEnoughPointerToCommit - Return true if this constant is simple |
| 2032 | /// enough for us to understand. In particular, if it is a cast of something, |
| 2033 | /// we punt. We basically just support direct accesses to globals and GEP's of |
| 2034 | /// globals. This should be kept up to date with CommitValueTo. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2035 | static bool isSimpleEnoughPointerToCommit(Constant *C) { |
Dan Gohman | ce5de5b | 2009-09-07 22:42:05 +0000 | [diff] [blame] | 2036 | // Conservatively, avoid aggregate types. This is because we don't |
| 2037 | // want to worry about them partially overlapping other stores. |
| 2038 | if (!cast<PointerType>(C->getType())->getElementType()->isSingleValueType()) |
| 2039 | return false; |
| 2040 | |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2041 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
| 2042 | // Do not allow weak/linkonce/dllimport/dllexport linkage or |
| 2043 | // external globals. |
| 2044 | return GV->hasDefinitiveInitializer(); |
| 2045 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2046 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 2047 | // Handle a constantexpr gep. |
| 2048 | if (CE->getOpcode() == Instruction::GetElementPtr && |
Dan Gohman | c62482d | 2009-09-07 22:40:13 +0000 | [diff] [blame] | 2049 | isa<GlobalVariable>(CE->getOperand(0)) && |
| 2050 | cast<GEPOperator>(CE)->isInBounds()) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2051 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Dan Gohman | fd54a89 | 2009-09-07 22:31:26 +0000 | [diff] [blame] | 2052 | // Do not allow weak/linkonce/dllimport/dllexport linkage or |
| 2053 | // external globals. |
| 2054 | if (!GV->hasDefinitiveInitializer()) |
| 2055 | return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2056 | |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2057 | // The first index must be zero. |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 2058 | ConstantInt *CI = dyn_cast<ConstantInt>(*next(CE->op_begin())); |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2059 | if (!CI || !CI->isZero()) return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2060 | |
| 2061 | // The remaining indices must be compile-time known integers within the |
Dan Gohman | e6992f7 | 2009-09-10 23:37:55 +0000 | [diff] [blame] | 2062 | // notional bounds of the corresponding static array types. |
| 2063 | if (!CE->isGEPWithNoNotionalOverIndexing()) |
| 2064 | return false; |
Dan Gohman | 80bdc96 | 2009-09-07 22:44:55 +0000 | [diff] [blame] | 2065 | |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 2066 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2067 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2068 | return false; |
| 2069 | } |
| 2070 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2071 | /// EvaluateStoreInto - Evaluate a piece of a constantexpr store into a global |
| 2072 | /// initializer. This returns 'Init' modified to reflect 'Val' stored into it. |
| 2073 | /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into. |
| 2074 | static Constant *EvaluateStoreInto(Constant *Init, Constant *Val, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2075 | ConstantExpr *Addr, unsigned OpNo) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2076 | // Base case of the recursion. |
| 2077 | if (OpNo == Addr->getNumOperands()) { |
| 2078 | assert(Val->getType() == Init->getType() && "Type mismatch!"); |
| 2079 | return Val; |
| 2080 | } |
| 2081 | |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2082 | std::vector<Constant*> Elts; |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2083 | if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2084 | |
| 2085 | // Break up the constant into its elements. |
| 2086 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2087 | for (User::op_iterator i = CS->op_begin(), e = CS->op_end(); i != e; ++i) |
| 2088 | Elts.push_back(cast<Constant>(*i)); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2089 | } else if (isa<ConstantAggregateZero>(Init)) { |
| 2090 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2091 | Elts.push_back(Constant::getNullValue(STy->getElementType(i))); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2092 | } else if (isa<UndefValue>(Init)) { |
| 2093 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2094 | Elts.push_back(UndefValue::get(STy->getElementType(i))); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2095 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2096 | llvm_unreachable("This code is out of sync with " |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2097 | " ConstantFoldLoadThroughGEPConstantExpr"); |
| 2098 | } |
| 2099 | |
| 2100 | // Replace the element that we are supposed to. |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2101 | ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo)); |
| 2102 | unsigned Idx = CU->getZExtValue(); |
| 2103 | assert(Idx < STy->getNumElements() && "Struct index out of range!"); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2104 | Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2105 | |
| 2106 | // Return the modified struct. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2107 | return ConstantStruct::get(Init->getContext(), &Elts[0], Elts.size(), |
| 2108 | STy->isPacked()); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2109 | } else { |
| 2110 | ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo)); |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2111 | const SequentialType *InitTy = cast<SequentialType>(Init->getType()); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2113 | uint64_t NumElts; |
| 2114 | if (const ArrayType *ATy = dyn_cast<ArrayType>(InitTy)) |
| 2115 | NumElts = ATy->getNumElements(); |
| 2116 | else |
| 2117 | NumElts = cast<VectorType>(InitTy)->getNumElements(); |
| 2118 | |
| 2119 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2120 | // Break up the array into elements. |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2121 | if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2122 | for (User::op_iterator i = CA->op_begin(), e = CA->op_end(); i != e; ++i) |
| 2123 | Elts.push_back(cast<Constant>(*i)); |
Chris Lattner | 4039bd0 | 2010-01-07 01:20:20 +0000 | [diff] [blame] | 2124 | } else if (ConstantVector *CV = dyn_cast<ConstantVector>(Init)) { |
| 2125 | for (User::op_iterator i = CV->op_begin(), e = CV->op_end(); i != e; ++i) |
| 2126 | Elts.push_back(cast<Constant>(*i)); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2127 | } else if (isa<ConstantAggregateZero>(Init)) { |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2128 | Elts.assign(NumElts, Constant::getNullValue(InitTy->getElementType())); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2129 | } else { |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2130 | assert(isa<UndefValue>(Init) && "This code is out of sync with " |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2131 | " ConstantFoldLoadThroughGEPConstantExpr"); |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2132 | Elts.assign(NumElts, UndefValue::get(InitTy->getElementType())); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2135 | assert(CI->getZExtValue() < NumElts); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 2136 | Elts[CI->getZExtValue()] = |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2137 | EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1); |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2138 | |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2139 | if (Init->getType()->isArrayTy()) |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2140 | return ConstantArray::get(cast<ArrayType>(InitTy), Elts); |
| 2141 | else |
| 2142 | return ConstantVector::get(&Elts[0], Elts.size()); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2146 | /// CommitValueTo - We have decided that Addr (which satisfies the predicate |
| 2147 | /// isSimpleEnoughPointerToCommit) should get Val as its value. Make it happen. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2148 | static void CommitValueTo(Constant *Val, Constant *Addr) { |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2149 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) { |
| 2150 | assert(GV->hasInitializer()); |
| 2151 | GV->setInitializer(Val); |
| 2152 | return; |
| 2153 | } |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2154 | |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2155 | ConstantExpr *CE = cast<ConstantExpr>(Addr); |
| 2156 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Chris Lattner | a0e9a24 | 2010-01-07 01:16:21 +0000 | [diff] [blame] | 2157 | GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2)); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Chris Lattner | 562a055 | 2005-09-26 05:16:34 +0000 | [diff] [blame] | 2160 | /// ComputeLoadResult - Return the value that would be computed by a load from |
| 2161 | /// P after the stores reflected by 'memory' have been performed. If we can't |
| 2162 | /// decide, return null. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2163 | static Constant *ComputeLoadResult(Constant *P, |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2164 | const DenseMap<Constant*, Constant*> &Memory) { |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2165 | // If this memory location has been recently stored, use the stored value: it |
| 2166 | // is the most up-to-date. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2167 | DenseMap<Constant*, Constant*>::const_iterator I = Memory.find(P); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2168 | if (I != Memory.end()) return I->second; |
| 2169 | |
| 2170 | // Access it. |
| 2171 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P)) { |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2172 | if (GV->hasDefinitiveInitializer()) |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2173 | return GV->getInitializer(); |
| 2174 | return 0; |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2175 | } |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2176 | |
| 2177 | // Handle a constantexpr getelementptr. |
| 2178 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(P)) |
| 2179 | if (CE->getOpcode() == Instruction::GetElementPtr && |
| 2180 | isa<GlobalVariable>(CE->getOperand(0))) { |
| 2181 | GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0)); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 2182 | if (GV->hasDefinitiveInitializer()) |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 2183 | return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE); |
Chris Lattner | 798b4d5 | 2005-09-26 06:52:44 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | return 0; // don't know how to evaluate. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2189 | /// EvaluateFunction - Evaluate a call to function F, returning true if |
| 2190 | /// successful, false if we can't evaluate it. ActualArgs contains the formal |
| 2191 | /// arguments for the function. |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2192 | static bool EvaluateFunction(Function *F, Constant *&RetVal, |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2193 | const SmallVectorImpl<Constant*> &ActualArgs, |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2194 | std::vector<Function*> &CallStack, |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2195 | DenseMap<Constant*, Constant*> &MutatedMemory, |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2196 | std::vector<GlobalVariable*> &AllocaTmps) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2197 | // Check to see if this function is already executing (recursion). If so, |
| 2198 | // bail out. TODO: we might want to accept limited recursion. |
| 2199 | if (std::find(CallStack.begin(), CallStack.end(), F) != CallStack.end()) |
| 2200 | return false; |
| 2201 | |
| 2202 | CallStack.push_back(F); |
| 2203 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2204 | /// Values - As we compute SSA register values, we store their contents here. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2205 | DenseMap<Value*, Constant*> Values; |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Initialize arguments to the incoming values specified. |
| 2208 | unsigned ArgNo = 0; |
| 2209 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); AI != E; |
| 2210 | ++AI, ++ArgNo) |
| 2211 | Values[AI] = ActualArgs[ArgNo]; |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2213 | /// ExecutedBlocks - We only handle non-looping, non-recursive code. As such, |
| 2214 | /// we can only evaluate any one basic block at most once. This set keeps |
| 2215 | /// track of what we have executed so we can detect recursive cases etc. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2216 | SmallPtrSet<BasicBlock*, 32> ExecutedBlocks; |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2217 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2218 | // CurInst - The current instruction we're evaluating. |
| 2219 | BasicBlock::iterator CurInst = F->begin()->begin(); |
| 2220 | |
| 2221 | // This is the main evaluation loop. |
| 2222 | while (1) { |
| 2223 | Constant *InstResult = 0; |
| 2224 | |
| 2225 | if (StoreInst *SI = dyn_cast<StoreInst>(CurInst)) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2226 | if (SI->isVolatile()) return false; // no volatile accesses. |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2227 | Constant *Ptr = getVal(Values, SI->getOperand(1)); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2228 | if (!isSimpleEnoughPointerToCommit(Ptr)) |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2229 | // If this is too complex for us to commit, reject it. |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2230 | return false; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2231 | Constant *Val = getVal(Values, SI->getOperand(0)); |
| 2232 | MutatedMemory[Ptr] = Val; |
| 2233 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2234 | InstResult = ConstantExpr::get(BO->getOpcode(), |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2235 | getVal(Values, BO->getOperand(0)), |
| 2236 | getVal(Values, BO->getOperand(1))); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2237 | } else if (CmpInst *CI = dyn_cast<CmpInst>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2238 | InstResult = ConstantExpr::getCompare(CI->getPredicate(), |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2239 | getVal(Values, CI->getOperand(0)), |
| 2240 | getVal(Values, CI->getOperand(1))); |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2241 | } else if (CastInst *CI = dyn_cast<CastInst>(CurInst)) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2242 | InstResult = ConstantExpr::getCast(CI->getOpcode(), |
Chris Lattner | 9a989f0 | 2006-11-30 17:26:08 +0000 | [diff] [blame] | 2243 | getVal(Values, CI->getOperand(0)), |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2244 | CI->getType()); |
| 2245 | } else if (SelectInst *SI = dyn_cast<SelectInst>(CurInst)) { |
Owen Anderson | 14ce9ef | 2009-07-06 01:34:54 +0000 | [diff] [blame] | 2246 | InstResult = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2247 | ConstantExpr::getSelect(getVal(Values, SI->getOperand(0)), |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2248 | getVal(Values, SI->getOperand(1)), |
| 2249 | getVal(Values, SI->getOperand(2))); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2250 | } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurInst)) { |
| 2251 | Constant *P = getVal(Values, GEP->getOperand(0)); |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 2252 | SmallVector<Constant*, 8> GEPOps; |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2253 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); |
| 2254 | i != e; ++i) |
| 2255 | GEPOps.push_back(getVal(Values, *i)); |
Dan Gohman | 4a7e6b7 | 2009-09-07 22:34:43 +0000 | [diff] [blame] | 2256 | InstResult = cast<GEPOperator>(GEP)->isInBounds() ? |
| 2257 | ConstantExpr::getInBoundsGetElementPtr(P, &GEPOps[0], GEPOps.size()) : |
| 2258 | ConstantExpr::getGetElementPtr(P, &GEPOps[0], GEPOps.size()); |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2259 | } else if (LoadInst *LI = dyn_cast<LoadInst>(CurInst)) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2260 | if (LI->isVolatile()) return false; // no volatile accesses. |
Chris Lattner | 04de1cf | 2005-09-26 05:15:37 +0000 | [diff] [blame] | 2261 | InstResult = ComputeLoadResult(getVal(Values, LI->getOperand(0)), |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2262 | MutatedMemory); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2263 | if (InstResult == 0) return false; // Could not evaluate load. |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2264 | } else if (AllocaInst *AI = dyn_cast<AllocaInst>(CurInst)) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2265 | if (AI->isArrayAllocation()) return false; // Cannot handle array allocs. |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2266 | const Type *Ty = AI->getType()->getElementType(); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2267 | AllocaTmps.push_back(new GlobalVariable(Ty, false, |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2268 | GlobalValue::InternalLinkage, |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2269 | UndefValue::get(Ty), |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2270 | AI->getName())); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2271 | InstResult = AllocaTmps.back(); |
| 2272 | } else if (CallInst *CI = dyn_cast<CallInst>(CurInst)) { |
Devang Patel | 412a446 | 2009-03-09 23:04:12 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Debug info can safely be ignored here. |
| 2275 | if (isa<DbgInfoIntrinsic>(CI)) { |
| 2276 | ++CurInst; |
| 2277 | continue; |
| 2278 | } |
| 2279 | |
Chris Lattner | 7cd580f | 2006-07-07 21:37:01 +0000 | [diff] [blame] | 2280 | // Cannot handle inline asm. |
| 2281 | if (isa<InlineAsm>(CI->getOperand(0))) return false; |
| 2282 | |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2283 | // Resolve function pointers. |
| 2284 | Function *Callee = dyn_cast<Function>(getVal(Values, CI->getOperand(0))); |
| 2285 | if (!Callee) return false; // Cannot resolve. |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2286 | |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2287 | SmallVector<Constant*, 8> Formals; |
Gabor Greif | 5e46321 | 2008-05-29 01:59:18 +0000 | [diff] [blame] | 2288 | for (User::op_iterator i = CI->op_begin() + 1, e = CI->op_end(); |
| 2289 | i != e; ++i) |
| 2290 | Formals.push_back(getVal(Values, *i)); |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2291 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 2292 | if (Callee->isDeclaration()) { |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2293 | // If this is a function we can constant fold, do it. |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2294 | if (Constant *C = ConstantFoldCall(Callee, Formals.data(), |
Chris Lattner | 6c1f565 | 2007-01-30 23:14:52 +0000 | [diff] [blame] | 2295 | Formals.size())) { |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2296 | InstResult = C; |
| 2297 | } else { |
| 2298 | return false; |
| 2299 | } |
| 2300 | } else { |
| 2301 | if (Callee->getFunctionType()->isVarArg()) |
| 2302 | return false; |
| 2303 | |
| 2304 | Constant *RetVal; |
Chris Lattner | a9ec8ab | 2005-09-27 05:02:43 +0000 | [diff] [blame] | 2305 | // Execute the call, if successful, use the return value. |
| 2306 | if (!EvaluateFunction(Callee, RetVal, Formals, CallStack, |
| 2307 | MutatedMemory, AllocaTmps)) |
| 2308 | return false; |
| 2309 | InstResult = RetVal; |
| 2310 | } |
Reid Spencer | 3ed469c | 2006-11-02 20:25:50 +0000 | [diff] [blame] | 2311 | } else if (isa<TerminatorInst>(CurInst)) { |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2312 | BasicBlock *NewBB = 0; |
| 2313 | if (BranchInst *BI = dyn_cast<BranchInst>(CurInst)) { |
| 2314 | if (BI->isUnconditional()) { |
| 2315 | NewBB = BI->getSuccessor(0); |
| 2316 | } else { |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2317 | ConstantInt *Cond = |
| 2318 | dyn_cast<ConstantInt>(getVal(Values, BI->getCondition())); |
Chris Lattner | 97d1fad | 2007-01-12 18:30:11 +0000 | [diff] [blame] | 2319 | if (!Cond) return false; // Cannot determine. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 2320 | |
Reid Spencer | 579dca1 | 2007-01-12 04:24:46 +0000 | [diff] [blame] | 2321 | NewBB = BI->getSuccessor(!Cond->getZExtValue()); |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2322 | } |
| 2323 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(CurInst)) { |
| 2324 | ConstantInt *Val = |
| 2325 | dyn_cast<ConstantInt>(getVal(Values, SI->getCondition())); |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2326 | if (!Val) return false; // Cannot determine. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2327 | NewBB = SI->getSuccessor(SI->findCaseValue(Val)); |
Chris Lattner | b3d5a65 | 2009-10-29 05:51:50 +0000 | [diff] [blame] | 2328 | } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(CurInst)) { |
| 2329 | Value *Val = getVal(Values, IBI->getAddress())->stripPointerCasts(); |
| 2330 | if (BlockAddress *BA = dyn_cast<BlockAddress>(Val)) |
| 2331 | NewBB = BA->getBasicBlock(); |
Chris Lattner | cdfc940 | 2009-11-01 01:27:45 +0000 | [diff] [blame] | 2332 | else |
| 2333 | return false; // Cannot determine. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2334 | } else if (ReturnInst *RI = dyn_cast<ReturnInst>(CurInst)) { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2335 | if (RI->getNumOperands()) |
| 2336 | RetVal = getVal(Values, RI->getOperand(0)); |
| 2337 | |
| 2338 | CallStack.pop_back(); // return from fn. |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2339 | return true; // We succeeded at evaluating this ctor! |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2340 | } else { |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2341 | // invoke, unwind, unreachable. |
| 2342 | return false; // Cannot handle this terminator. |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | // Okay, we succeeded in evaluating this control flow. See if we have |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2346 | // executed the new block before. If so, we have a looping function, |
| 2347 | // which we cannot evaluate in reasonable time. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2348 | if (!ExecutedBlocks.insert(NewBB)) |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2349 | return false; // looped! |
Chris Lattner | cdf98be | 2005-09-26 04:57:38 +0000 | [diff] [blame] | 2350 | |
| 2351 | // Okay, we have never been in this block before. Check to see if there |
| 2352 | // are any PHI nodes. If so, evaluate them with information about where |
| 2353 | // we came from. |
| 2354 | BasicBlock *OldBB = CurInst->getParent(); |
| 2355 | CurInst = NewBB->begin(); |
| 2356 | PHINode *PN; |
| 2357 | for (; (PN = dyn_cast<PHINode>(CurInst)); ++CurInst) |
| 2358 | Values[PN] = getVal(Values, PN->getIncomingValueForBlock(OldBB)); |
| 2359 | |
| 2360 | // Do NOT increment CurInst. We know that the terminator had no value. |
| 2361 | continue; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2362 | } else { |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2363 | // Did not know how to evaluate this! |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2364 | return false; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | if (!CurInst->use_empty()) |
| 2368 | Values[CurInst] = InstResult; |
| 2369 | |
| 2370 | // Advance program counter. |
| 2371 | ++CurInst; |
| 2372 | } |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | /// EvaluateStaticConstructor - Evaluate static constructors in the function, if |
| 2376 | /// we can. Return true if we can, false otherwise. |
| 2377 | static bool EvaluateStaticConstructor(Function *F) { |
| 2378 | /// MutatedMemory - For each store we execute, we update this map. Loads |
| 2379 | /// check this to get the most up-to-date value. If evaluation is successful, |
| 2380 | /// this state is committed to the process. |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2381 | DenseMap<Constant*, Constant*> MutatedMemory; |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2382 | |
| 2383 | /// AllocaTmps - To 'execute' an alloca, we create a temporary global variable |
| 2384 | /// to represent its body. This vector is needed so we can delete the |
| 2385 | /// temporary globals when we are done. |
| 2386 | std::vector<GlobalVariable*> AllocaTmps; |
| 2387 | |
| 2388 | /// CallStack - This is used to detect recursion. In pathological situations |
| 2389 | /// we could hit exponential behavior, but at least there is nothing |
| 2390 | /// unbounded. |
| 2391 | std::vector<Function*> CallStack; |
| 2392 | |
| 2393 | // Call the function. |
Chris Lattner | cd27142 | 2005-09-27 04:45:34 +0000 | [diff] [blame] | 2394 | Constant *RetValDummy; |
Duncan Sands | fa6a1cf | 2009-08-17 14:33:27 +0000 | [diff] [blame] | 2395 | bool EvalSuccess = EvaluateFunction(F, RetValDummy, |
| 2396 | SmallVector<Constant*, 0>(), CallStack, |
| 2397 | MutatedMemory, AllocaTmps); |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2398 | if (EvalSuccess) { |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2399 | // We succeeded at evaluation: commit the result. |
David Greene | 3215b0e | 2010-01-05 01:28:05 +0000 | [diff] [blame] | 2400 | DEBUG(dbgs() << "FULLY EVALUATED GLOBAL CTOR FUNCTION '" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 2401 | << F->getName() << "' to " << MutatedMemory.size() |
| 2402 | << " stores.\n"); |
Chris Lattner | 5a6bb6a | 2008-12-16 07:34:30 +0000 | [diff] [blame] | 2403 | for (DenseMap<Constant*, Constant*>::iterator I = MutatedMemory.begin(), |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2404 | E = MutatedMemory.end(); I != E; ++I) |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2405 | CommitValueTo(I->second, I->first); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2406 | } |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2407 | |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2408 | // At this point, we are done interpreting. If we created any 'alloca' |
| 2409 | // temporaries, release them now. |
| 2410 | while (!AllocaTmps.empty()) { |
| 2411 | GlobalVariable *Tmp = AllocaTmps.back(); |
| 2412 | AllocaTmps.pop_back(); |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2414 | // If there are still users of the alloca, the program is doing something |
| 2415 | // silly, e.g. storing the address of the alloca somewhere and using it |
| 2416 | // later. Since this is undefined, we'll just make it be null. |
| 2417 | if (!Tmp->use_empty()) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2418 | Tmp->replaceAllUsesWith(Constant::getNullValue(Tmp->getType())); |
Chris Lattner | a22fdb0 | 2005-09-26 17:07:09 +0000 | [diff] [blame] | 2419 | delete Tmp; |
| 2420 | } |
Chris Lattner | aae4a1c | 2005-09-26 07:34:35 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2422 | return EvalSuccess; |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Chris Lattner | db973e6 | 2005-09-26 02:31:18 +0000 | [diff] [blame] | 2425 | |
Chris Lattner | 8a7cc6e | 2005-09-27 04:27:01 +0000 | [diff] [blame] | 2426 | |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2427 | /// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. |
| 2428 | /// Return true if anything changed. |
| 2429 | bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { |
| 2430 | std::vector<Function*> Ctors = ParseGlobalCtors(GCL); |
| 2431 | bool MadeChange = false; |
| 2432 | if (Ctors.empty()) return false; |
| 2433 | |
| 2434 | // Loop over global ctors, optimizing them when we can. |
| 2435 | for (unsigned i = 0; i != Ctors.size(); ++i) { |
| 2436 | Function *F = Ctors[i]; |
| 2437 | // Found a null terminator in the middle of the list, prune off the rest of |
| 2438 | // the list. |
Chris Lattner | 7d8e58f | 2005-09-26 02:19:27 +0000 | [diff] [blame] | 2439 | if (F == 0) { |
| 2440 | if (i != Ctors.size()-1) { |
| 2441 | Ctors.resize(i+1); |
| 2442 | MadeChange = true; |
| 2443 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2444 | break; |
| 2445 | } |
| 2446 | |
Chris Lattner | 79c1101 | 2005-09-26 04:44:35 +0000 | [diff] [blame] | 2447 | // We cannot simplify external ctor functions. |
| 2448 | if (F->empty()) continue; |
| 2449 | |
| 2450 | // If we can evaluate the ctor at compile time, do. |
| 2451 | if (EvaluateStaticConstructor(F)) { |
| 2452 | Ctors.erase(Ctors.begin()+i); |
| 2453 | MadeChange = true; |
| 2454 | --i; |
| 2455 | ++NumCtorsEvaluated; |
| 2456 | continue; |
| 2457 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | if (!MadeChange) return false; |
| 2461 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 2462 | GCL = InstallGlobalCtors(GCL, Ctors); |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2463 | return true; |
| 2464 | } |
| 2465 | |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2466 | bool GlobalOpt::OptimizeGlobalAliases(Module &M) { |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2467 | bool Changed = false; |
| 2468 | |
Duncan Sands | 177d84e | 2009-01-07 20:01:06 +0000 | [diff] [blame] | 2469 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2470 | I != E;) { |
| 2471 | Module::alias_iterator J = I++; |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2472 | // Aliases without names cannot be referenced outside this module. |
| 2473 | if (!J->hasName() && !J->isDeclaration()) |
| 2474 | J->setLinkage(GlobalValue::InternalLinkage); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2475 | // If the aliasee may change at link time, nothing can be done - bail out. |
| 2476 | if (J->mayBeOverridden()) |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2477 | continue; |
| 2478 | |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2479 | Constant *Aliasee = J->getAliasee(); |
| 2480 | GlobalValue *Target = cast<GlobalValue>(Aliasee->stripPointerCasts()); |
Duncan Sands | 95c5d0f | 2009-02-18 17:55:38 +0000 | [diff] [blame] | 2481 | Target->removeDeadConstantUsers(); |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2482 | bool hasOneUse = Target->hasOneUse() && Aliasee->hasOneUse(); |
| 2483 | |
| 2484 | // Make all users of the alias use the aliasee instead. |
| 2485 | if (!J->use_empty()) { |
| 2486 | J->replaceAllUsesWith(Aliasee); |
| 2487 | ++NumAliasesResolved; |
| 2488 | Changed = true; |
| 2489 | } |
| 2490 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2491 | // If the alias is externally visible, we may still be able to simplify it. |
| 2492 | if (!J->hasLocalLinkage()) { |
| 2493 | // If the aliasee has internal linkage, give it the name and linkage |
| 2494 | // of the alias, and delete the alias. This turns: |
| 2495 | // define internal ... @f(...) |
| 2496 | // @a = alias ... @f |
| 2497 | // into: |
| 2498 | // define ... @a(...) |
| 2499 | if (!Target->hasLocalLinkage()) |
| 2500 | continue; |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2501 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2502 | // Do not perform the transform if multiple aliases potentially target the |
| 2503 | // aliasee. This check also ensures that it is safe to replace the section |
| 2504 | // and other attributes of the aliasee with those of the alias. |
| 2505 | if (!hasOneUse) |
| 2506 | continue; |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2507 | |
Duncan Sands | 7a154cf | 2009-12-08 10:10:20 +0000 | [diff] [blame] | 2508 | // Give the aliasee the name, linkage and other attributes of the alias. |
| 2509 | Target->takeName(J); |
| 2510 | Target->setLinkage(J->getLinkage()); |
| 2511 | Target->GlobalValue::copyAttributesFrom(J); |
| 2512 | } |
Duncan Sands | 4782b30 | 2009-02-15 09:56:08 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Delete the alias. |
| 2515 | M.getAliasList().erase(J); |
| 2516 | ++NumAliasesRemoved; |
| 2517 | Changed = true; |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | return Changed; |
| 2521 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 2523 | bool GlobalOpt::runOnModule(Module &M) { |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 2524 | bool Changed = false; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2525 | |
| 2526 | // Try to find the llvm.globalctors list. |
| 2527 | GlobalVariable *GlobalCtors = FindGlobalCtors(M); |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 2528 | |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 2529 | bool LocalChange = true; |
| 2530 | while (LocalChange) { |
| 2531 | LocalChange = false; |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Delete functions that are trivially dead, ccc -> fastcc |
| 2534 | LocalChange |= OptimizeFunctions(M); |
| 2535 | |
| 2536 | // Optimize global_ctors list. |
| 2537 | if (GlobalCtors) |
| 2538 | LocalChange |= OptimizeGlobalCtorsList(GlobalCtors); |
| 2539 | |
| 2540 | // Optimize non-address-taken globals. |
| 2541 | LocalChange |= OptimizeGlobalVars(M); |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2542 | |
| 2543 | // Resolve aliases, when possible. |
Duncan Sands | fc5940d | 2009-03-06 10:21:56 +0000 | [diff] [blame] | 2544 | LocalChange |= OptimizeGlobalAliases(M); |
Anton Korobeynikov | e4c6b61 | 2008-09-09 19:04:59 +0000 | [diff] [blame] | 2545 | Changed |= LocalChange; |
Chris Lattner | 7a90b68 | 2004-10-07 04:16:33 +0000 | [diff] [blame] | 2546 | } |
Chris Lattner | b1ab458 | 2005-09-26 01:43:45 +0000 | [diff] [blame] | 2547 | |
| 2548 | // TODO: Move all global ctors functions to the end of the module for code |
| 2549 | // layout. |
| 2550 | |
Chris Lattner | 079236d | 2004-02-25 21:34:36 +0000 | [diff] [blame] | 2551 | return Changed; |
| 2552 | } |