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