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