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