Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1 | //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +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 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 9 | // |
| 10 | // This transformation implements the well known scalar replacement of |
| 11 | // aggregates transformation. This xform breaks up alloca instructions of |
| 12 | // aggregate type (structure or array) into individual alloca instructions for |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 13 | // each member (if possible). Then, if possible, it transforms the individual |
| 14 | // alloca instructions into nice clean scalar SSA form. |
| 15 | // |
Chad Rosier | cc899f3 | 2012-04-11 19:21:58 +0000 | [diff] [blame] | 16 | // This combines a simple SRoA algorithm with the Mem2Reg algorithm because they |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 17 | // often interact, especially for C++ programs. As such, iterating between |
| 18 | // SRoA, then Mem2Reg until we run out of things to promote works well. |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SetVector.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/Loads.h" |
| 28 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 29 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 33 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Function.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 37 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 38 | #include "llvm/IR/GlobalVariable.h" |
| 39 | #include "llvm/IR/IRBuilder.h" |
| 40 | #include "llvm/IR/Instructions.h" |
| 41 | #include "llvm/IR/IntrinsicInst.h" |
| 42 | #include "llvm/IR/LLVMContext.h" |
| 43 | #include "llvm/IR/Module.h" |
| 44 | #include "llvm/IR/Operator.h" |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 45 | #include "llvm/Pass.h" |
Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 47 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 3b0a62d | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Utils/Local.h" |
| 51 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 52 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Chris Lattner | 40d2aeb | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 53 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 54 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 55 | #define DEBUG_TYPE "scalarrepl" |
| 56 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 57 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 58 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 59 | STATISTIC(NumAdjusted, "Number of scalar allocas adjusted to allow promotion"); |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 60 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 62 | namespace { |
NAKAMURA Takumi | 26c3872 | 2015-10-24 06:42:42 +0000 | [diff] [blame] | 63 | #define SROA SROA_ |
Chris Lattner | 2dd09db | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 64 | struct SROA : public FunctionPass { |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 65 | SROA(int T, bool hasDT, char &ID, int ST, int AT, int SLT) |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 66 | : FunctionPass(ID), HasDomTree(hasDT) { |
Devang Patel | e8ec766 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 67 | if (T == -1) |
Chris Lattner | 1f70816 | 2007-08-02 21:33:36 +0000 | [diff] [blame] | 68 | SRThreshold = 128; |
Devang Patel | e8ec766 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 69 | else |
| 70 | SRThreshold = T; |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 71 | if (ST == -1) |
| 72 | StructMemberThreshold = 32; |
| 73 | else |
| 74 | StructMemberThreshold = ST; |
| 75 | if (AT == -1) |
| 76 | ArrayElementThreshold = 8; |
| 77 | else |
| 78 | ArrayElementThreshold = AT; |
| 79 | if (SLT == -1) |
| 80 | // Do not limit the scalar integer load size if no threshold is given. |
| 81 | ScalarLoadThreshold = -1; |
| 82 | else |
| 83 | ScalarLoadThreshold = SLT; |
Devang Patel | e8ec766 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 84 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 85 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 86 | bool runOnFunction(Function &F) override; |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 88 | bool performScalarRepl(Function &F); |
| 89 | bool performPromotion(Function &F); |
| 90 | |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 91 | private: |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 92 | bool HasDomTree; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 93 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 94 | /// DeadInsts - Keep track of instructions we have made dead, so that |
| 95 | /// we can remove them after we are done working. |
| 96 | SmallVector<Value*, 32> DeadInsts; |
| 97 | |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 98 | /// AllocaInfo - When analyzing uses of an alloca instruction, this captures |
| 99 | /// information about the uses. All these fields are initialized to false |
| 100 | /// and set to true when something is learned. |
| 101 | struct AllocaInfo { |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 102 | /// The alloca to promote. |
| 103 | AllocaInst *AI; |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 105 | /// CheckedPHIs - This is a set of verified PHI nodes, to prevent infinite |
| 106 | /// looping and avoid redundant work. |
| 107 | SmallPtrSet<PHINode*, 8> CheckedPHIs; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 109 | /// isUnsafe - This is set to true if the alloca cannot be SROA'd. |
| 110 | bool isUnsafe : 1; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 112 | /// isMemCpySrc - This is true if this aggregate is memcpy'd from. |
| 113 | bool isMemCpySrc : 1; |
| 114 | |
Zhou Sheng | 1ee941d | 2007-07-06 06:01:16 +0000 | [diff] [blame] | 115 | /// isMemCpyDst - This is true if this aggregate is memcpy'd into. |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 116 | bool isMemCpyDst : 1; |
| 117 | |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 118 | /// hasSubelementAccess - This is true if a subelement of the alloca is |
| 119 | /// ever accessed, or false if the alloca is only accessed with mem |
| 120 | /// intrinsics or load/store that only access the entire alloca at once. |
| 121 | bool hasSubelementAccess : 1; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 123 | /// hasALoadOrStore - This is true if there are any loads or stores to it. |
| 124 | /// The alloca may just be accessed with memcpy, for example, which would |
| 125 | /// not set this. |
| 126 | bool hasALoadOrStore : 1; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 128 | explicit AllocaInfo(AllocaInst *ai) |
| 129 | : AI(ai), isUnsafe(false), isMemCpySrc(false), isMemCpyDst(false), |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 130 | hasSubelementAccess(false), hasALoadOrStore(false) {} |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 131 | }; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 132 | |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 133 | /// SRThreshold - The maximum alloca size to considered for SROA. |
Devang Patel | e8ec766 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 134 | unsigned SRThreshold; |
| 135 | |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 136 | /// StructMemberThreshold - The maximum number of members a struct can |
| 137 | /// contain to be considered for SROA. |
| 138 | unsigned StructMemberThreshold; |
| 139 | |
| 140 | /// ArrayElementThreshold - The maximum number of elements an array can |
| 141 | /// have to be considered for SROA. |
| 142 | unsigned ArrayElementThreshold; |
| 143 | |
| 144 | /// ScalarLoadThreshold - The maximum size in bits of scalars to load when |
| 145 | /// converting to scalar |
| 146 | unsigned ScalarLoadThreshold; |
| 147 | |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 148 | void MarkUnsafe(AllocaInfo &I, Instruction *User) { |
| 149 | I.isUnsafe = true; |
| 150 | DEBUG(dbgs() << " Transformation preventing inst: " << *User << '\n'); |
| 151 | } |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 152 | |
Victor Hernandez | 1df6518 | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 153 | bool isSafeAllocaToScalarRepl(AllocaInst *AI); |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 155 | void isSafeForScalarRepl(Instruction *I, uint64_t Offset, AllocaInfo &Info); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 156 | void isSafePHISelectUseForScalarRepl(Instruction *User, uint64_t Offset, |
| 157 | AllocaInfo &Info); |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 158 | void isSafeGEP(GetElementPtrInst *GEPI, uint64_t &Offset, AllocaInfo &Info); |
| 159 | void isSafeMemAccess(uint64_t Offset, uint64_t MemSize, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 160 | Type *MemOpType, bool isStore, AllocaInfo &Info, |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 161 | Instruction *TheAccess, bool AllowWholeAccess); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 162 | bool TypeHasComponent(Type *T, uint64_t Offset, uint64_t Size, |
| 163 | const DataLayout &DL); |
| 164 | uint64_t FindElementAndOffset(Type *&T, uint64_t &Offset, Type *&IdxTy, |
| 165 | const DataLayout &DL); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 166 | |
| 167 | void DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 168 | std::vector<AllocaInst*> &WorkList); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 169 | void DeleteDeadInstructions(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 170 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 171 | void RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 172 | SmallVectorImpl<AllocaInst *> &NewElts); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 173 | void RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 174 | SmallVectorImpl<AllocaInst *> &NewElts); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 175 | void RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 176 | SmallVectorImpl<AllocaInst *> &NewElts); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 177 | void RewriteLifetimeIntrinsic(IntrinsicInst *II, AllocaInst *AI, |
| 178 | uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 179 | SmallVectorImpl<AllocaInst *> &NewElts); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 180 | void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 181 | AllocaInst *AI, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 182 | SmallVectorImpl<AllocaInst *> &NewElts); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 183 | void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 184 | SmallVectorImpl<AllocaInst *> &NewElts); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 185 | void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 186 | SmallVectorImpl<AllocaInst *> &NewElts); |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 187 | bool ShouldAttemptScalarRepl(AllocaInst *AI); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 188 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 189 | |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 190 | // SROA_DT - SROA that uses DominatorTree. |
| 191 | struct SROA_DT : public SROA { |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 192 | static char ID; |
| 193 | public: |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 194 | SROA_DT(int T = -1, int ST = -1, int AT = -1, int SLT = -1) : |
| 195 | SROA(T, true, ID, ST, AT, SLT) { |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 196 | initializeSROA_DTPass(*PassRegistry::getPassRegistry()); |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 197 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 199 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 200 | // will not alter the CFG, so say so. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 201 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 202 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 203 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 204 | AU.setPreservesCFG(); |
| 205 | } |
| 206 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 208 | // SROA_SSAUp - SROA that uses SSAUpdater. |
| 209 | struct SROA_SSAUp : public SROA { |
| 210 | static char ID; |
| 211 | public: |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 212 | SROA_SSAUp(int T = -1, int ST = -1, int AT = -1, int SLT = -1) : |
| 213 | SROA(T, false, ID, ST, AT, SLT) { |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 214 | initializeSROA_SSAUpPass(*PassRegistry::getPassRegistry()); |
| 215 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 217 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 218 | // will not alter the CFG, so say so. |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 219 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 220 | AU.addRequired<AssumptionCacheTracker>(); |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 221 | AU.setPreservesCFG(); |
| 222 | } |
| 223 | }; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 224 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 225 | } |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 226 | |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 227 | char SROA_DT::ID = 0; |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 228 | char SROA_SSAUp::ID = 0; |
| 229 | |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 230 | INITIALIZE_PASS_BEGIN(SROA_DT, "scalarrepl", |
| 231 | "Scalar Replacement of Aggregates (DT)", false, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 232 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 233 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 234 | INITIALIZE_PASS_END(SROA_DT, "scalarrepl", |
| 235 | "Scalar Replacement of Aggregates (DT)", false, false) |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 236 | |
| 237 | INITIALIZE_PASS_BEGIN(SROA_SSAUp, "scalarrepl-ssa", |
| 238 | "Scalar Replacement of Aggregates (SSAUp)", false, false) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 239 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 240 | INITIALIZE_PASS_END(SROA_SSAUp, "scalarrepl-ssa", |
| 241 | "Scalar Replacement of Aggregates (SSAUp)", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 242 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 243 | // Public interface to the ScalarReplAggregates pass |
Chris Lattner | 9987a6f | 2011-01-14 08:13:00 +0000 | [diff] [blame] | 244 | FunctionPass *llvm::createScalarReplAggregatesPass(int Threshold, |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 245 | bool UseDomTree, |
| 246 | int StructMemberThreshold, |
| 247 | int ArrayElementThreshold, |
| 248 | int ScalarLoadThreshold) { |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 249 | if (UseDomTree) |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 250 | return new SROA_DT(Threshold, StructMemberThreshold, ArrayElementThreshold, |
| 251 | ScalarLoadThreshold); |
| 252 | return new SROA_SSAUp(Threshold, StructMemberThreshold, |
| 253 | ArrayElementThreshold, ScalarLoadThreshold); |
Devang Patel | e8ec766 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 255 | |
| 256 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 257 | //===----------------------------------------------------------------------===// |
| 258 | // Convert To Scalar Optimization. |
| 259 | //===----------------------------------------------------------------------===// |
| 260 | |
| 261 | namespace { |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 262 | /// ConvertToScalarInfo - This class implements the "Convert To Scalar" |
| 263 | /// optimization, which scans the uses of an alloca and determines if it can |
| 264 | /// rewrite it in terms of a single new alloca that can be mem2reg'd. |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 265 | class ConvertToScalarInfo { |
Cameron Zwarich | 63062cc | 2011-03-16 00:13:35 +0000 | [diff] [blame] | 266 | /// AllocaSize - The size of the alloca being considered in bytes. |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 267 | unsigned AllocaSize; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 268 | const DataLayout &DL; |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 269 | unsigned ScalarLoadThreshold; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 270 | |
Chris Lattner | bd2d943 | 2010-04-16 02:32:17 +0000 | [diff] [blame] | 271 | /// IsNotTrivial - This is set to true if there is some access to the object |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 272 | /// which means that mem2reg can't promote it. |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 273 | bool IsNotTrivial; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 274 | |
Cameron Zwarich | 5e9a0be | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 275 | /// ScalarKind - Tracks the kind of alloca being considered for promotion, |
| 276 | /// computed based on the uses of the alloca rather than the LLVM type system. |
| 277 | enum { |
| 278 | Unknown, |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 279 | |
Cameron Zwarich | 922e494 | 2011-06-13 23:39:23 +0000 | [diff] [blame] | 280 | // Accesses via GEPs that are consistent with element access of a vector |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 281 | // type. This will not be converted into a vector unless there is a later |
| 282 | // access using an actual vector type. |
| 283 | ImplicitVector, |
| 284 | |
Cameron Zwarich | 922e494 | 2011-06-13 23:39:23 +0000 | [diff] [blame] | 285 | // Accesses via vector operations and GEPs that are consistent with the |
| 286 | // layout of a vector type. |
Cameron Zwarich | 5e9a0be | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 287 | Vector, |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 288 | |
| 289 | // An integer bag-of-bits with bitwise operations for insertion and |
| 290 | // extraction. Any combination of types can be converted into this kind |
| 291 | // of scalar. |
Cameron Zwarich | 5e9a0be | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 292 | Integer |
| 293 | } ScalarKind; |
| 294 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 295 | /// VectorTy - This tracks the type that we should promote the vector to if |
| 296 | /// it is possible to turn it into a vector. This starts out null, and if it |
| 297 | /// isn't possible to turn into a vector type, it gets set to VoidTy. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 298 | VectorType *VectorTy; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 299 | |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 300 | /// HadNonMemTransferAccess - True if there is at least one access to the |
Cameron Zwarich | 7599b10 | 2011-03-16 08:13:42 +0000 | [diff] [blame] | 301 | /// alloca that is not a MemTransferInst. We don't want to turn structs into |
| 302 | /// large integers unless there is some potential for optimization. |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 303 | bool HadNonMemTransferAccess; |
| 304 | |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 305 | /// HadDynamicAccess - True if some element of this alloca was dynamic. |
| 306 | /// We don't yet have support for turning a dynamic access into a large |
| 307 | /// integer. |
| 308 | bool HadDynamicAccess; |
| 309 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 310 | public: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 311 | explicit ConvertToScalarInfo(unsigned Size, const DataLayout &DL, |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 312 | unsigned SLT) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 313 | : AllocaSize(Size), DL(DL), ScalarLoadThreshold(SLT), IsNotTrivial(false), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 314 | ScalarKind(Unknown), VectorTy(nullptr), HadNonMemTransferAccess(false), |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 315 | HadDynamicAccess(false) { } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 316 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 317 | AllocaInst *TryConvert(AllocaInst *AI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 319 | private: |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 320 | bool CanConvertToScalar(Value *V, uint64_t Offset, Value* NonConstantIdx); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 321 | void MergeInTypeForLoadOrStore(Type *In, uint64_t Offset); |
| 322 | bool MergeInVectorType(VectorType *VInTy, uint64_t Offset); |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 323 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset, |
| 324 | Value *NonConstantIdx); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 326 | Value *ConvertScalar_ExtractValue(Value *NV, Type *ToType, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 327 | uint64_t Offset, Value* NonConstantIdx, |
| 328 | IRBuilder<> &Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 329 | Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 330 | uint64_t Offset, Value* NonConstantIdx, |
| 331 | IRBuilder<> &Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 332 | }; |
| 333 | } // end anonymous namespace. |
| 334 | |
Chris Lattner | 34e5361 | 2010-09-01 05:14:33 +0000 | [diff] [blame] | 335 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 336 | /// TryConvert - Analyze the specified alloca, and if it is safe to do so, |
| 337 | /// rewrite it to be a new alloca which is mem2reg'able. This returns the new |
| 338 | /// alloca if possible or null if not. |
| 339 | AllocaInst *ConvertToScalarInfo::TryConvert(AllocaInst *AI) { |
| 340 | // If we can't convert this scalar, or if mem2reg can trivially do it, bail |
| 341 | // out. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 342 | if (!CanConvertToScalar(AI, 0, nullptr) || !IsNotTrivial) |
| 343 | return nullptr; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 344 | |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 345 | // If an alloca has only memset / memcpy uses, it may still have an Unknown |
| 346 | // ScalarKind. Treat it as an Integer below. |
| 347 | if (ScalarKind == Unknown) |
| 348 | ScalarKind = Integer; |
| 349 | |
Cameron Zwarich | 9601ddb | 2011-06-18 06:17:51 +0000 | [diff] [blame] | 350 | if (ScalarKind == Vector && VectorTy->getBitWidth() != AllocaSize * 8) |
| 351 | ScalarKind = Integer; |
| 352 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 353 | // If we were able to find a vector type that can handle this with |
| 354 | // insert/extract elements, and if there was at least one use that had |
| 355 | // a vector type, promote this to a vector. We don't want to promote |
| 356 | // random stuff that doesn't use vectors (e.g. <9 x double>) because then |
| 357 | // we just get a lot of insert/extracts. If at least one vector is |
| 358 | // involved, then we probably really do have a union of vector/array. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 359 | Type *NewTy; |
Cameron Zwarich | b5f19d9 | 2011-06-14 06:33:51 +0000 | [diff] [blame] | 360 | if (ScalarKind == Vector) { |
| 361 | assert(VectorTy && "Missing type for vector scalar."); |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 362 | DEBUG(dbgs() << "CONVERT TO VECTOR: " << *AI << "\n TYPE = " |
| 363 | << *VectorTy << '\n'); |
| 364 | NewTy = VectorTy; // Use the vector type. |
| 365 | } else { |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 366 | unsigned BitWidth = AllocaSize * 8; |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 367 | |
| 368 | // Do not convert to scalar integer if the alloca size exceeds the |
| 369 | // scalar load threshold. |
| 370 | if (BitWidth > ScalarLoadThreshold) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 371 | return nullptr; |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 372 | |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 373 | if ((ScalarKind == ImplicitVector || ScalarKind == Integer) && |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 374 | !HadNonMemTransferAccess && !DL.fitsInLegalInteger(BitWidth)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 375 | return nullptr; |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 376 | // Dynamic accesses on integers aren't yet supported. They need us to shift |
| 377 | // by a dynamic amount which could be difficult to work out as we might not |
| 378 | // know whether to use a left or right shift. |
| 379 | if (ScalarKind == Integer && HadDynamicAccess) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 380 | return nullptr; |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 381 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 382 | DEBUG(dbgs() << "CONVERT TO SCALAR INTEGER: " << *AI << "\n"); |
| 383 | // Create and insert the integer alloca. |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 384 | NewTy = IntegerType::get(AI->getContext(), BitWidth); |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 385 | } |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 386 | AllocaInst *NewAI = |
| 387 | new AllocaInst(NewTy, nullptr, "", &AI->getParent()->front()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 388 | ConvertUsesToScalar(AI, NewAI, 0, nullptr); |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 389 | return NewAI; |
| 390 | } |
| 391 | |
Cameron Zwarich | 3ecbd59 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 392 | /// MergeInTypeForLoadOrStore - Add the 'In' type to the accumulated vector type |
| 393 | /// (VectorTy) so far at the offset specified by Offset (which is specified in |
| 394 | /// bytes). |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 395 | /// |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 396 | /// There are two cases we handle here: |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 397 | /// 1) A union of vector types of the same size and potentially its elements. |
| 398 | /// Here we turn element accesses into insert/extract element operations. |
| 399 | /// This promotes a <4 x float> with a store of float to the third element |
| 400 | /// into a <4 x float> that uses insert element. |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 401 | /// 2) A fully general blob of memory, which we turn into some (potentially |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 402 | /// large) integer type with extract and insert operations where the loads |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 403 | /// and stores would mutate the memory. We mark this by setting VectorTy |
| 404 | /// to VoidTy. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 405 | void ConvertToScalarInfo::MergeInTypeForLoadOrStore(Type *In, |
Cameron Zwarich | 3ecbd59 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 406 | uint64_t Offset) { |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 407 | // If we already decided to turn this into a blob of integer memory, there is |
| 408 | // nothing to be done. |
Cameron Zwarich | 5e9a0be | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 409 | if (ScalarKind == Integer) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 410 | return; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 412 | // If this could be contributing to a vector, analyze it. |
| 413 | |
| 414 | // If the In type is a vector that is the same size as the alloca, see if it |
| 415 | // matches the existing VecTy. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 416 | if (VectorType *VInTy = dyn_cast<VectorType>(In)) { |
Cameron Zwarich | 43a241f | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 417 | if (MergeInVectorType(VInTy, Offset)) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 418 | return; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 419 | } else if (In->isFloatTy() || In->isDoubleTy() || |
| 420 | (In->isIntegerTy() && In->getPrimitiveSizeInBits() >= 8 && |
| 421 | isPowerOf2_32(In->getPrimitiveSizeInBits()))) { |
Cameron Zwarich | ff811cc | 2011-03-29 05:19:52 +0000 | [diff] [blame] | 422 | // Full width accesses can be ignored, because they can always be turned |
| 423 | // into bitcasts. |
| 424 | unsigned EltSize = In->getPrimitiveSizeInBits()/8; |
Cameron Zwarich | 8deb615 | 2011-06-13 21:44:31 +0000 | [diff] [blame] | 425 | if (EltSize == AllocaSize) |
Cameron Zwarich | ff811cc | 2011-03-29 05:19:52 +0000 | [diff] [blame] | 426 | return; |
Cameron Zwarich | 4cd9a4a | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 428 | // If we're accessing something that could be an element of a vector, see |
| 429 | // if the implied vector agrees with what we already have and if Offset is |
| 430 | // compatible with it. |
Cameron Zwarich | 77a699a | 2011-06-09 01:45:33 +0000 | [diff] [blame] | 431 | if (Offset % EltSize == 0 && AllocaSize % EltSize == 0 && |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 432 | (!VectorTy || EltSize == VectorTy->getElementType() |
| 433 | ->getPrimitiveSizeInBits()/8)) { |
Cameron Zwarich | 4cd9a4a | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 434 | if (!VectorTy) { |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 435 | ScalarKind = ImplicitVector; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 436 | VectorTy = VectorType::get(In, AllocaSize/EltSize); |
Cameron Zwarich | 4cd9a4a | 2011-04-20 21:48:16 +0000 | [diff] [blame] | 437 | } |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 438 | return; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 442 | // Otherwise, we have a case that we can't handle with an optimized vector |
| 443 | // form. We can still turn this into a large integer. |
Cameron Zwarich | 5e9a0be | 2011-06-13 21:44:35 +0000 | [diff] [blame] | 444 | ScalarKind = Integer; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Cameron Zwarich | 3ecbd59 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 447 | /// MergeInVectorType - Handles the vector case of MergeInTypeForLoadOrStore, |
| 448 | /// returning true if the type was successfully merged and false otherwise. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 449 | bool ConvertToScalarInfo::MergeInVectorType(VectorType *VInTy, |
Cameron Zwarich | 43a241f | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 450 | uint64_t Offset) { |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 451 | if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) { |
| 452 | // If we're storing/loading a vector of the right size, allow it as a |
| 453 | // vector. If this the first vector we see, remember the type so that |
| 454 | // we know the element size. If this is a subsequent access, ignore it |
| 455 | // even if it is a differing type but the same size. Worst case we can |
| 456 | // bitcast the resultant vectors. |
| 457 | if (!VectorTy) |
| 458 | VectorTy = VInTy; |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 459 | ScalarKind = Vector; |
Cameron Zwarich | 3b649f4 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 460 | return true; |
Cameron Zwarich | 8cb90ac | 2011-06-13 21:44:40 +0000 | [diff] [blame] | 461 | } |
Cameron Zwarich | 3b649f4 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 462 | |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 463 | return false; |
Cameron Zwarich | 43a241f | 2011-03-09 05:43:01 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 466 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee and all |
| 467 | /// its accesses to a single vector type, return true and set VecTy to |
| 468 | /// the new type. If we could convert the alloca into a single promotable |
| 469 | /// integer, return true but set VecTy to VoidTy. Further, if the use is not a |
| 470 | /// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset |
| 471 | /// is the current offset from the base of the alloca being analyzed. |
| 472 | /// |
| 473 | /// If we see at least one access to the value that is as a vector type, set the |
| 474 | /// SawVec flag. |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 475 | bool ConvertToScalarInfo::CanConvertToScalar(Value *V, uint64_t Offset, |
| 476 | Value* NonConstantIdx) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 477 | for (User *U : V->users()) { |
| 478 | Instruction *UI = cast<Instruction>(U); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 479 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 480 | if (LoadInst *LI = dyn_cast<LoadInst>(UI)) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 481 | // Don't break volatile loads. |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 482 | if (!LI->isSimple()) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 483 | return false; |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 484 | // Don't touch MMX operations. |
| 485 | if (LI->getType()->isX86_MMXTy()) |
| 486 | return false; |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 487 | HadNonMemTransferAccess = true; |
Cameron Zwarich | 3ecbd59 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 488 | MergeInTypeForLoadOrStore(LI->getType(), Offset); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 489 | continue; |
| 490 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 491 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 492 | if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 493 | // Storing the pointer, not into the value? |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 494 | if (SI->getOperand(0) == V || !SI->isSimple()) return false; |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 495 | // Don't touch MMX operations. |
| 496 | if (SI->getOperand(0)->getType()->isX86_MMXTy()) |
| 497 | return false; |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 498 | HadNonMemTransferAccess = true; |
Cameron Zwarich | 3ecbd59 | 2011-06-13 21:44:43 +0000 | [diff] [blame] | 499 | MergeInTypeForLoadOrStore(SI->getOperand(0)->getType(), Offset); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 500 | continue; |
| 501 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 502 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 503 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(UI)) { |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 504 | if (!onlyUsedByLifetimeMarkers(BCI)) |
| 505 | IsNotTrivial = true; // Can't be mem2reg'd. |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 506 | if (!CanConvertToScalar(BCI, Offset, NonConstantIdx)) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 507 | return false; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 508 | continue; |
| 509 | } |
| 510 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 511 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UI)) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 512 | // If this is a GEP with a variable indices, we can't handle it. |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 513 | // Compute the offset that this GEP adds to the pointer. |
| 514 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 515 | Value *GEPNonConstantIdx = nullptr; |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 516 | if (!GEP->hasAllConstantIndices()) { |
Eduard Burtescu | 19eb031 | 2016-01-19 17:28:00 +0000 | [diff] [blame] | 517 | if (!isa<VectorType>(GEP->getSourceElementType())) |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 518 | return false; |
| 519 | if (NonConstantIdx) |
| 520 | return false; |
| 521 | GEPNonConstantIdx = Indices.pop_back_val(); |
| 522 | if (!GEPNonConstantIdx->getType()->isIntegerTy(32)) |
| 523 | return false; |
| 524 | HadDynamicAccess = true; |
| 525 | } else |
| 526 | GEPNonConstantIdx = NonConstantIdx; |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 527 | uint64_t GEPOffset = DL.getIndexedOffsetInType(GEP->getSourceElementType(), |
| 528 | Indices); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 529 | // See if all uses can be converted. |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 530 | if (!CanConvertToScalar(GEP, Offset+GEPOffset, GEPNonConstantIdx)) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 531 | return false; |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 532 | IsNotTrivial = true; // Can't be mem2reg'd. |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 533 | HadNonMemTransferAccess = true; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 534 | continue; |
| 535 | } |
| 536 | |
| 537 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 538 | // handle it. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 539 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(UI)) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 540 | // Store to dynamic index. |
| 541 | if (NonConstantIdx) |
| 542 | return false; |
Cameron Zwarich | 2a26100 | 2011-06-18 05:47:49 +0000 | [diff] [blame] | 543 | // Store of constant value. |
| 544 | if (!isa<ConstantInt>(MSI->getValue())) |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 545 | return false; |
Cameron Zwarich | 2a26100 | 2011-06-18 05:47:49 +0000 | [diff] [blame] | 546 | |
| 547 | // Store of constant size. |
| 548 | ConstantInt *Len = dyn_cast<ConstantInt>(MSI->getLength()); |
| 549 | if (!Len) |
| 550 | return false; |
| 551 | |
| 552 | // If the size differs from the alloca, we can only convert the alloca to |
| 553 | // an integer bag-of-bits. |
| 554 | // FIXME: This should handle all of the cases that are currently accepted |
| 555 | // as vector element insertions. |
| 556 | if (Len->getZExtValue() != AllocaSize || Offset != 0) |
| 557 | ScalarKind = Integer; |
| 558 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 559 | IsNotTrivial = true; // Can't be mem2reg'd. |
Cameron Zwarich | 0454253 | 2011-03-16 00:13:44 +0000 | [diff] [blame] | 560 | HadNonMemTransferAccess = true; |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 561 | continue; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 565 | // can handle it like a load or store of the scalar type. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 566 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(UI)) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 567 | // Store to dynamic index. |
| 568 | if (NonConstantIdx) |
| 569 | return false; |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 570 | ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 571 | if (!Len || Len->getZExtValue() != AllocaSize || Offset != 0) |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 572 | return false; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 573 | |
Chris Lattner | b735529 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 574 | IsNotTrivial = true; // Can't be mem2reg'd. |
| 575 | continue; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 576 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 577 | |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 578 | // If this is a lifetime intrinsic, we can handle it. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 579 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(UI)) { |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 580 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 581 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 582 | continue; |
| 583 | } |
| 584 | } |
| 585 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 586 | // Otherwise, we cannot handle this! |
| 587 | return false; |
| 588 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 590 | return true; |
| 591 | } |
| 592 | |
| 593 | /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca |
| 594 | /// directly. This happens when we are converting an "integer union" to a |
| 595 | /// single integer scalar, or when we are converting a "vector union" to a |
| 596 | /// vector with insert/extractelement instructions. |
| 597 | /// |
| 598 | /// Offset is an offset from the original alloca, in bits that need to be |
| 599 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
| 600 | void ConvertToScalarInfo::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 601 | uint64_t Offset, |
| 602 | Value* NonConstantIdx) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 603 | while (!Ptr->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 604 | Instruction *User = cast<Instruction>(Ptr->user_back()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 605 | |
| 606 | if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 607 | ConvertUsesToScalar(CI, NewAI, Offset, NonConstantIdx); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 608 | CI->eraseFromParent(); |
| 609 | continue; |
| 610 | } |
| 611 | |
| 612 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 613 | // Compute the offset that this GEP adds to the pointer. |
| 614 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 615 | Value* GEPNonConstantIdx = nullptr; |
Pete Cooper | 0deca6b | 2012-08-10 03:26:36 +0000 | [diff] [blame] | 616 | if (!GEP->hasAllConstantIndices()) { |
| 617 | assert(!NonConstantIdx && |
| 618 | "Dynamic GEP reading from dynamic GEP unsupported"); |
| 619 | GEPNonConstantIdx = Indices.pop_back_val(); |
| 620 | } else |
| 621 | GEPNonConstantIdx = NonConstantIdx; |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 622 | uint64_t GEPOffset = DL.getIndexedOffsetInType(GEP->getSourceElementType(), |
| 623 | Indices); |
Pete Cooper | 0deca6b | 2012-08-10 03:26:36 +0000 | [diff] [blame] | 624 | ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8, GEPNonConstantIdx); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 625 | GEP->eraseFromParent(); |
| 626 | continue; |
| 627 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 629 | IRBuilder<> Builder(User); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 631 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 632 | // The load is a bit extract from NewAI shifted right by Offset bits. |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 633 | Value *LoadedVal = Builder.CreateLoad(NewAI); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 634 | Value *NewLoadVal |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 635 | = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, |
| 636 | NonConstantIdx, Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 637 | LI->replaceAllUsesWith(NewLoadVal); |
| 638 | LI->eraseFromParent(); |
| 639 | continue; |
| 640 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 642 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 643 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
| 644 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 645 | Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 646 | NonConstantIdx, Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 647 | Builder.CreateStore(New, NewAI); |
| 648 | SI->eraseFromParent(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 650 | // If the load we just inserted is now dead, then the inserted store |
| 651 | // overwrote the entire thing. |
| 652 | if (Old->use_empty()) |
| 653 | Old->eraseFromParent(); |
| 654 | continue; |
| 655 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 657 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 658 | // transform it into a store of the expanded constant value. |
| 659 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 660 | assert(MSI->getRawDest() == Ptr && "Consistency error!"); |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 661 | assert(!NonConstantIdx && "Cannot replace dynamic memset with insert"); |
Duncan Sands | 8f897dc | 2012-03-23 08:29:04 +0000 | [diff] [blame] | 662 | int64_t SNumBytes = cast<ConstantInt>(MSI->getLength())->getSExtValue(); |
Chris Lattner | 7d7dba3 | 2012-03-22 03:46:58 +0000 | [diff] [blame] | 663 | if (SNumBytes > 0 && (SNumBytes >> 32) == 0) { |
Aaron Ballman | a733297 | 2012-03-15 00:05:31 +0000 | [diff] [blame] | 664 | unsigned NumBytes = static_cast<unsigned>(SNumBytes); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 665 | unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 667 | // Compute the value replicated the right number of times. |
| 668 | APInt APVal(NumBytes*8, Val); |
| 669 | |
| 670 | // Splat the value if non-zero. |
| 671 | if (Val) |
| 672 | for (unsigned i = 1; i != NumBytes; ++i) |
| 673 | APVal |= APVal << 8; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 675 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 676 | Value *New = ConvertScalar_InsertValue( |
| 677 | ConstantInt::get(User->getContext(), APVal), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 678 | Old, Offset, nullptr, Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 679 | Builder.CreateStore(New, NewAI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 680 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 681 | // If the load we just inserted is now dead, then the memset overwrote |
| 682 | // the entire thing. |
| 683 | if (Old->use_empty()) |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 684 | Old->eraseFromParent(); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 685 | } |
| 686 | MSI->eraseFromParent(); |
| 687 | continue; |
| 688 | } |
| 689 | |
| 690 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 691 | // can handle it like a load or store of the scalar type. |
| 692 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
| 693 | assert(Offset == 0 && "must be store to start of alloca"); |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 694 | assert(!NonConstantIdx && "Cannot replace dynamic transfer with insert"); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 696 | // If the source and destination are both to the same alloca, then this is |
| 697 | // a noop copy-to-self, just delete it. Otherwise, emit a load and store |
| 698 | // as appropriate. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 699 | AllocaInst *OrigAI = cast<AllocaInst>(GetUnderlyingObject(Ptr, DL, 0)); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 700 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 701 | if (GetUnderlyingObject(MTI->getSource(), DL, 0) != OrigAI) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 702 | // Dest must be OrigAI, change this to be a load from the original |
| 703 | // pointer (bitcasted), then a store to our new alloca. |
| 704 | assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?"); |
| 705 | Value *SrcPtr = MTI->getSource(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 706 | PointerType* SPTy = cast<PointerType>(SrcPtr->getType()); |
| 707 | PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
Mon P Wang | 18b762a | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 708 | if (SPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 709 | AIPTy = PointerType::get(NewAI->getAllocatedType(), |
Mon P Wang | 18b762a | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 710 | SPTy->getAddressSpace()); |
| 711 | } |
| 712 | SrcPtr = Builder.CreateBitCast(SrcPtr, AIPTy); |
| 713 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 714 | LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval"); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 715 | SrcVal->setAlignment(MTI->getAlignment()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 716 | Builder.CreateStore(SrcVal, NewAI); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 717 | } else if (GetUnderlyingObject(MTI->getDest(), DL, 0) != OrigAI) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 718 | // Src must be OrigAI, change this to be a load from NewAI then a store |
| 719 | // through the original dest pointer (bitcasted). |
| 720 | assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?"); |
| 721 | LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval"); |
| 722 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 723 | PointerType* DPTy = cast<PointerType>(MTI->getDest()->getType()); |
| 724 | PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
Mon P Wang | 18b762a | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 725 | if (DPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 726 | AIPTy = PointerType::get(NewAI->getAllocatedType(), |
Mon P Wang | 18b762a | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 727 | DPTy->getAddressSpace()); |
| 728 | } |
| 729 | Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), AIPTy); |
| 730 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 731 | StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 732 | NewStore->setAlignment(MTI->getAlignment()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 733 | } else { |
| 734 | // Noop transfer. Src == Dst |
| 735 | } |
| 736 | |
| 737 | MTI->eraseFromParent(); |
| 738 | continue; |
| 739 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 740 | |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 741 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 742 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 743 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 744 | // There's no need to preserve these, as the resulting alloca will be |
| 745 | // converted to a register anyways. |
| 746 | II->eraseFromParent(); |
| 747 | continue; |
| 748 | } |
| 749 | } |
| 750 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 751 | llvm_unreachable("Unsupported operation!"); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | /// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer |
| 756 | /// or vector value FromVal, extracting the bits from the offset specified by |
| 757 | /// Offset. This returns the value, which is of type ToType. |
| 758 | /// |
| 759 | /// This happens when we are converting an "integer union" to a single |
| 760 | /// integer scalar, or when we are converting a "vector union" to a vector with |
| 761 | /// insert/extractelement instructions. |
| 762 | /// |
| 763 | /// Offset is an offset from the original alloca, in bits that need to be |
| 764 | /// shifted to the right. |
| 765 | Value *ConvertToScalarInfo:: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 766 | ConvertScalar_ExtractValue(Value *FromVal, Type *ToType, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 767 | uint64_t Offset, Value* NonConstantIdx, |
| 768 | IRBuilder<> &Builder) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 769 | // If the load is of the whole new alloca, no conversion is needed. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 770 | Type *FromType = FromVal->getType(); |
Mon P Wang | 2e5528f | 2011-04-13 21:40:02 +0000 | [diff] [blame] | 771 | if (FromType == ToType && Offset == 0) |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 772 | return FromVal; |
| 773 | |
| 774 | // If the result alloca is a vector type, this is either an element |
| 775 | // access or a bitcast to another vector type of the same size. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 776 | if (VectorType *VTy = dyn_cast<VectorType>(FromType)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 777 | unsigned FromTypeSize = DL.getTypeAllocSize(FromType); |
| 778 | unsigned ToTypeSize = DL.getTypeAllocSize(ToType); |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 779 | if (FromTypeSize == ToTypeSize) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 780 | return Builder.CreateBitCast(FromVal, ToType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 781 | |
| 782 | // Otherwise it must be an element access. |
| 783 | unsigned Elt = 0; |
| 784 | if (Offset) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 785 | unsigned EltSize = DL.getTypeAllocSizeInBits(VTy->getElementType()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 786 | Elt = Offset/EltSize; |
| 787 | assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); |
| 788 | } |
| 789 | // Return the element extracted out of it. |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 790 | Value *Idx; |
| 791 | if (NonConstantIdx) { |
| 792 | if (Elt) |
| 793 | Idx = Builder.CreateAdd(NonConstantIdx, |
| 794 | Builder.getInt32(Elt), |
| 795 | "dyn.offset"); |
| 796 | else |
| 797 | Idx = NonConstantIdx; |
| 798 | } else |
| 799 | Idx = Builder.getInt32(Elt); |
| 800 | Value *V = Builder.CreateExtractElement(FromVal, Idx); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 801 | if (V->getType() != ToType) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 802 | V = Builder.CreateBitCast(V, ToType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 803 | return V; |
| 804 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 806 | // If ToType is a first class aggregate, extract out each of the pieces and |
| 807 | // use insertvalue's to form the FCA. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 808 | if (StructType *ST = dyn_cast<StructType>(ToType)) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 809 | assert(!NonConstantIdx && |
| 810 | "Dynamic indexing into struct types not supported"); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 811 | const StructLayout &Layout = *DL.getStructLayout(ST); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 812 | Value *Res = UndefValue::get(ST); |
| 813 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
| 814 | Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i), |
| 815 | Offset+Layout.getElementOffsetInBits(i), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 816 | nullptr, Builder); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 817 | Res = Builder.CreateInsertValue(Res, Elt, i); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 818 | } |
| 819 | return Res; |
| 820 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 821 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 822 | if (ArrayType *AT = dyn_cast<ArrayType>(ToType)) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 823 | assert(!NonConstantIdx && |
| 824 | "Dynamic indexing into array types not supported"); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 825 | uint64_t EltSize = DL.getTypeAllocSizeInBits(AT->getElementType()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 826 | Value *Res = UndefValue::get(AT); |
| 827 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 828 | Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 829 | Offset+i*EltSize, nullptr, |
| 830 | Builder); |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 831 | Res = Builder.CreateInsertValue(Res, Elt, i); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 832 | } |
| 833 | return Res; |
| 834 | } |
| 835 | |
| 836 | // Otherwise, this must be a union that was converted to an integer value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 837 | IntegerType *NTy = cast<IntegerType>(FromVal->getType()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 838 | |
| 839 | // If this is a big-endian system and the load is narrower than the |
| 840 | // full alloca type, we need to do a shift to get the right bits. |
| 841 | int ShAmt = 0; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 842 | if (DL.isBigEndian()) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 843 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 844 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 845 | // integers with a bitwidth that is not a multiple of 8. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 846 | ShAmt = DL.getTypeStoreSizeInBits(NTy) - |
| 847 | DL.getTypeStoreSizeInBits(ToType) - Offset; |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 848 | } else { |
| 849 | ShAmt = Offset; |
| 850 | } |
| 851 | |
| 852 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 853 | // We do this to support (f.e.) loads off the end of a structure where |
| 854 | // only some bits are used. |
| 855 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
| 856 | FromVal = Builder.CreateLShr(FromVal, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 857 | ConstantInt::get(FromVal->getType(), ShAmt)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 858 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 859 | FromVal = Builder.CreateShl(FromVal, |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 860 | ConstantInt::get(FromVal->getType(), -ShAmt)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 861 | |
| 862 | // Finally, unconditionally truncate the integer to the right width. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 863 | unsigned LIBitWidth = DL.getTypeSizeInBits(ToType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 864 | if (LIBitWidth < NTy->getBitWidth()) |
| 865 | FromVal = |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 866 | Builder.CreateTrunc(FromVal, IntegerType::get(FromVal->getContext(), |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 867 | LIBitWidth)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 868 | else if (LIBitWidth > NTy->getBitWidth()) |
| 869 | FromVal = |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 870 | Builder.CreateZExt(FromVal, IntegerType::get(FromVal->getContext(), |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 871 | LIBitWidth)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 872 | |
| 873 | // If the result is an integer, this is a trunc or bitcast. |
| 874 | if (ToType->isIntegerTy()) { |
| 875 | // Should be done. |
| 876 | } else if (ToType->isFloatingPointTy() || ToType->isVectorTy()) { |
| 877 | // Just do a bitcast, we know the sizes match up. |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 878 | FromVal = Builder.CreateBitCast(FromVal, ToType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 879 | } else { |
| 880 | // Otherwise must be a pointer. |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 881 | FromVal = Builder.CreateIntToPtr(FromVal, ToType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 882 | } |
| 883 | assert(FromVal->getType() == ToType && "Didn't convert right?"); |
| 884 | return FromVal; |
| 885 | } |
| 886 | |
| 887 | /// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer |
| 888 | /// or vector value "Old" at the offset specified by Offset. |
| 889 | /// |
| 890 | /// This happens when we are converting an "integer union" to a |
| 891 | /// single integer scalar, or when we are converting a "vector union" to a |
| 892 | /// vector with insert/extractelement instructions. |
| 893 | /// |
| 894 | /// Offset is an offset from the original alloca, in bits that need to be |
| 895 | /// shifted to the right. |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 896 | /// |
| 897 | /// NonConstantIdx is an index value if there was a GEP with a non-constant |
| 898 | /// index value. If this is 0 then all GEPs used to find this insert address |
| 899 | /// are constant. |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 900 | Value *ConvertToScalarInfo:: |
| 901 | ConvertScalar_InsertValue(Value *SV, Value *Old, |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 902 | uint64_t Offset, Value* NonConstantIdx, |
| 903 | IRBuilder<> &Builder) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 904 | // Convert the stored type to the actual type, shift it left to insert |
| 905 | // then 'or' into place. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 906 | Type *AllocaType = Old->getType(); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 907 | LLVMContext &Context = Old->getContext(); |
| 908 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 909 | if (VectorType *VTy = dyn_cast<VectorType>(AllocaType)) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 910 | uint64_t VecSize = DL.getTypeAllocSizeInBits(VTy); |
| 911 | uint64_t ValSize = DL.getTypeAllocSizeInBits(SV->getType()); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 912 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 913 | // Changing the whole vector with memset or with an access of a different |
| 914 | // vector type? |
Cameron Zwarich | d7515cc | 2011-10-11 06:10:30 +0000 | [diff] [blame] | 915 | if (ValSize == VecSize) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 916 | return Builder.CreateBitCast(SV, AllocaType); |
Cameron Zwarich | 3b649f4 | 2011-03-09 05:43:05 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 918 | // Must be an element insertion. |
Cameron Zwarich | 057fbb1 | 2011-10-23 07:02:10 +0000 | [diff] [blame] | 919 | Type *EltTy = VTy->getElementType(); |
| 920 | if (SV->getType() != EltTy) |
| 921 | SV = Builder.CreateBitCast(SV, EltTy); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 922 | uint64_t EltSize = DL.getTypeAllocSizeInBits(EltTy); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 923 | unsigned Elt = Offset/EltSize; |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 924 | Value *Idx; |
| 925 | if (NonConstantIdx) { |
| 926 | if (Elt) |
| 927 | Idx = Builder.CreateAdd(NonConstantIdx, |
| 928 | Builder.getInt32(Elt), |
| 929 | "dyn.offset"); |
| 930 | else |
| 931 | Idx = NonConstantIdx; |
| 932 | } else |
| 933 | Idx = Builder.getInt32(Elt); |
| 934 | return Builder.CreateInsertElement(Old, SV, Idx); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 935 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 937 | // If SV is a first-class aggregate value, insert each value recursively. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 938 | if (StructType *ST = dyn_cast<StructType>(SV->getType())) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 939 | assert(!NonConstantIdx && |
| 940 | "Dynamic indexing into struct types not supported"); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 941 | const StructLayout &Layout = *DL.getStructLayout(ST); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 942 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 943 | Value *Elt = Builder.CreateExtractValue(SV, i); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 944 | Old = ConvertScalar_InsertValue(Elt, Old, |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 945 | Offset+Layout.getElementOffsetInBits(i), |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 946 | nullptr, Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 947 | } |
| 948 | return Old; |
| 949 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 951 | if (ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) { |
Pete Cooper | 33ee6c9 | 2012-06-17 03:58:26 +0000 | [diff] [blame] | 952 | assert(!NonConstantIdx && |
| 953 | "Dynamic indexing into array types not supported"); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 954 | uint64_t EltSize = DL.getTypeAllocSizeInBits(AT->getElementType()); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 955 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 956 | Value *Elt = Builder.CreateExtractValue(SV, i); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 957 | Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, nullptr, |
| 958 | Builder); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 959 | } |
| 960 | return Old; |
| 961 | } |
| 962 | |
| 963 | // If SV is a float, convert it to the appropriate integer type. |
| 964 | // If it is a pointer, do the same. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 965 | unsigned SrcWidth = DL.getTypeSizeInBits(SV->getType()); |
| 966 | unsigned DestWidth = DL.getTypeSizeInBits(AllocaType); |
| 967 | unsigned SrcStoreWidth = DL.getTypeStoreSizeInBits(SV->getType()); |
| 968 | unsigned DestStoreWidth = DL.getTypeStoreSizeInBits(AllocaType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 969 | if (SV->getType()->isFloatingPointTy() || SV->getType()->isVectorTy()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 970 | SV = Builder.CreateBitCast(SV, IntegerType::get(SV->getContext(),SrcWidth)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 971 | else if (SV->getType()->isPointerTy()) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 972 | SV = Builder.CreatePtrToInt(SV, DL.getIntPtrType(SV->getType())); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 973 | |
| 974 | // Zero extend or truncate the value if needed. |
| 975 | if (SV->getType() != AllocaType) { |
| 976 | if (SV->getType()->getPrimitiveSizeInBits() < |
| 977 | AllocaType->getPrimitiveSizeInBits()) |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 978 | SV = Builder.CreateZExt(SV, AllocaType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 979 | else { |
| 980 | // Truncation may be needed if storing more than the alloca can hold |
| 981 | // (undefined behavior). |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 982 | SV = Builder.CreateTrunc(SV, AllocaType); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 983 | SrcWidth = DestWidth; |
| 984 | SrcStoreWidth = DestStoreWidth; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // If this is a big-endian system and the store is narrower than the |
| 989 | // full alloca type, we need to do a shift to get the right bits. |
| 990 | int ShAmt = 0; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 991 | if (DL.isBigEndian()) { |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 992 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 993 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 994 | // integers with a bitwidth that is not a multiple of 8. |
| 995 | ShAmt = DestStoreWidth - SrcStoreWidth - Offset; |
| 996 | } else { |
| 997 | ShAmt = Offset; |
| 998 | } |
| 999 | |
| 1000 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 1001 | // We do this to support (f.e.) stores off the end of a structure where |
| 1002 | // only some bits in the structure are set. |
| 1003 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 1004 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1005 | SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), ShAmt)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1006 | Mask <<= ShAmt; |
| 1007 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
Benjamin Kramer | 547b6c5 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1008 | SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), -ShAmt)); |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1009 | Mask = Mask.lshr(-ShAmt); |
| 1010 | } |
| 1011 | |
| 1012 | // Mask out the bits we are about to insert from the old value, and or |
| 1013 | // in the new bits. |
| 1014 | if (SrcWidth != DestWidth) { |
| 1015 | assert(DestWidth > SrcWidth); |
| 1016 | Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask"); |
| 1017 | SV = Builder.CreateOr(Old, SV, "ins"); |
| 1018 | } |
| 1019 | return SV; |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | //===----------------------------------------------------------------------===// |
| 1024 | // SRoA Driver |
| 1025 | //===----------------------------------------------------------------------===// |
| 1026 | |
| 1027 | |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1028 | bool SROA::runOnFunction(Function &F) { |
Paul Robinson | af4e64d | 2014-02-06 00:07:05 +0000 | [diff] [blame] | 1029 | if (skipOptnoneFunction(F)) |
| 1030 | return false; |
| 1031 | |
Chris Lattner | 9a95f2a | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1032 | bool Changed = performPromotion(F); |
Dan Gohman | 915302c | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | 9a95f2a | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1034 | while (1) { |
| 1035 | bool LocalChange = performScalarRepl(F); |
| 1036 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 1037 | Changed = true; |
| 1038 | LocalChange = performPromotion(F); |
| 1039 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 1040 | } |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1041 | |
| 1042 | return Changed; |
| 1043 | } |
| 1044 | |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1045 | namespace { |
| 1046 | class AllocaPromoter : public LoadAndStorePromoter { |
| 1047 | AllocaInst *AI; |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1048 | DIBuilder *DIB; |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1049 | SmallVector<DbgDeclareInst *, 4> DDIs; |
| 1050 | SmallVector<DbgValueInst *, 4> DVIs; |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1051 | public: |
Pete Cooper | 41e0ee3 | 2015-05-13 01:12:16 +0000 | [diff] [blame] | 1052 | AllocaPromoter(ArrayRef<Instruction*> Insts, SSAUpdater &S, |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1053 | DIBuilder *DB) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1054 | : LoadAndStorePromoter(Insts, S), AI(nullptr), DIB(DB) {} |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1056 | void run(AllocaInst *AI, const SmallVectorImpl<Instruction*> &Insts) { |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1057 | // Remember which alloca we're promoting (for isInstInList). |
| 1058 | this->AI = AI; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1059 | if (auto *L = LocalAsMetadata::getIfExists(AI)) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 1060 | if (auto *DINode = MetadataAsValue::getIfExists(AI->getContext(), L)) { |
| 1061 | for (User *U : DINode->users()) |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1062 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U)) |
| 1063 | DDIs.push_back(DDI); |
| 1064 | else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U)) |
| 1065 | DVIs.push_back(DVI); |
| 1066 | } |
Rafael Espindola | 2b14b80 | 2011-12-26 23:12:42 +0000 | [diff] [blame] | 1067 | } |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1069 | LoadAndStorePromoter::run(Insts); |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1070 | AI->eraseFromParent(); |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 1071 | for (SmallVectorImpl<DbgDeclareInst *>::iterator I = DDIs.begin(), |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1072 | E = DDIs.end(); I != E; ++I) { |
| 1073 | DbgDeclareInst *DDI = *I; |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1074 | DDI->eraseFromParent(); |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1075 | } |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 1076 | for (SmallVectorImpl<DbgValueInst *>::iterator I = DVIs.begin(), |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1077 | E = DVIs.end(); I != E; ++I) { |
| 1078 | DbgValueInst *DVI = *I; |
| 1079 | DVI->eraseFromParent(); |
| 1080 | } |
Chris Lattner | 543384e | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1081 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1082 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 1083 | bool isInstInList(Instruction *I, |
| 1084 | const SmallVectorImpl<Instruction*> &Insts) const override { |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1085 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 1086 | return LI->getOperand(0) == AI; |
| 1087 | return cast<StoreInst>(I)->getPointerOperand() == AI; |
Chris Lattner | 543384e | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1088 | } |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1089 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 1090 | void updateDebugInfo(Instruction *Inst) const override { |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 1091 | for (SmallVectorImpl<DbgDeclareInst *>::const_iterator I = DDIs.begin(), |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1092 | E = DDIs.end(); I != E; ++I) { |
| 1093 | DbgDeclareInst *DDI = *I; |
| 1094 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 1095 | ConvertDebugDeclareToDebugValue(DDI, SI, *DIB); |
| 1096 | else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 1097 | ConvertDebugDeclareToDebugValue(DDI, LI, *DIB); |
| 1098 | } |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 1099 | for (SmallVectorImpl<DbgValueInst *>::const_iterator I = DVIs.begin(), |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1100 | E = DVIs.end(); I != E; ++I) { |
| 1101 | DbgValueInst *DVI = *I; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1102 | Value *Arg = nullptr; |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1103 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1104 | // If an argument is zero extended then use argument directly. The ZExt |
| 1105 | // may be zapped by an optimization pass in future. |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1106 | if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) |
Benjamin Kramer | 077e552 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1107 | Arg = dyn_cast<Argument>(ZExt->getOperand(0)); |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1108 | if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) |
Benjamin Kramer | 077e552 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1109 | Arg = dyn_cast<Argument>(SExt->getOperand(0)); |
| 1110 | if (!Arg) |
| 1111 | Arg = SI->getOperand(0); |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1112 | } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Benjamin Kramer | 077e552 | 2012-02-23 17:42:19 +0000 | [diff] [blame] | 1113 | Arg = LI->getOperand(0); |
| 1114 | } else { |
| 1115 | continue; |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1116 | } |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 1117 | DIB->insertDbgValueIntrinsic(Arg, 0, DVI->getVariable(), |
| 1118 | DVI->getExpression(), DVI->getDebugLoc(), |
| 1119 | Inst); |
Devang Patel | c6ee918 | 2011-07-06 22:06:11 +0000 | [diff] [blame] | 1120 | } |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1121 | } |
Chris Lattner | b498f9a | 2011-01-14 19:50:47 +0000 | [diff] [blame] | 1122 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1123 | } // end anon namespace |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1125 | /// isSafeSelectToSpeculate - Select instructions that use an alloca and are |
| 1126 | /// subsequently loaded can be rewritten to load both input pointers and then |
| 1127 | /// select between the result, allowing the load of the alloca to be promoted. |
| 1128 | /// From this: |
| 1129 | /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other |
| 1130 | /// %V = load i32* %P2 |
| 1131 | /// to: |
| 1132 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1133 | /// %V2 = load i32* %Other |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1134 | /// %V = select i1 %cond, i32 %V1, i32 %V2 |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1135 | /// |
| 1136 | /// We can do this to a select if its only uses are loads and if the operand to |
| 1137 | /// the select can be loaded unconditionally. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1138 | static bool isSafeSelectToSpeculate(SelectInst *SI) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1139 | for (User *U : SI->users()) { |
| 1140 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1141 | if (!LI || !LI->isSimple()) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1143 | // Both operands to the select need to be dereferencable, either absolutely |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1144 | // (e.g. allocas) or at this point because we can see other accesses to it. |
Artur Pilipenko | f84dc06 | 2016-01-17 12:35:29 +0000 | [diff] [blame] | 1145 | if (!isSafeToLoadUnconditionally(SI->getTrueValue(), LI->getAlignment(), |
Artur Pilipenko | 6dd6969 | 2016-01-15 15:27:46 +0000 | [diff] [blame] | 1146 | LI)) |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1147 | return false; |
Artur Pilipenko | f84dc06 | 2016-01-17 12:35:29 +0000 | [diff] [blame] | 1148 | if (!isSafeToLoadUnconditionally(SI->getFalseValue(), LI->getAlignment(), |
Artur Pilipenko | 6dd6969 | 2016-01-15 15:27:46 +0000 | [diff] [blame] | 1149 | LI)) |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1150 | return false; |
| 1151 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1153 | return true; |
| 1154 | } |
| 1155 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1156 | /// isSafePHIToSpeculate - PHI instructions that use an alloca and are |
| 1157 | /// subsequently loaded can be rewritten to load both input pointers in the pred |
| 1158 | /// blocks and then PHI the results, allowing the load of the alloca to be |
| 1159 | /// promoted. |
| 1160 | /// From this: |
| 1161 | /// %P2 = phi [i32* %Alloca, i32* %Other] |
| 1162 | /// %V = load i32* %P2 |
| 1163 | /// to: |
| 1164 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1165 | /// ... |
| 1166 | /// %V2 = load i32* %Other |
| 1167 | /// ... |
| 1168 | /// %V = phi [i32 %V1, i32 %V2] |
| 1169 | /// |
| 1170 | /// We can do this to a select if its only uses are loads and if the operand to |
| 1171 | /// the select can be loaded unconditionally. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1172 | static bool isSafePHIToSpeculate(PHINode *PN) { |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1173 | // For now, we can only do this promotion if the load is in the same block as |
| 1174 | // the PHI, and if there are no stores between the phi and load. |
| 1175 | // TODO: Allow recursive phi users. |
| 1176 | // TODO: Allow stores. |
| 1177 | BasicBlock *BB = PN->getParent(); |
| 1178 | unsigned MaxAlign = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1179 | for (User *U : PN->users()) { |
| 1180 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1181 | if (!LI || !LI->isSimple()) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1183 | // For now we only allow loads in the same block as the PHI. This is a |
| 1184 | // common case that happens when instcombine merges two loads through a PHI. |
| 1185 | if (LI->getParent() != BB) return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1187 | // Ensure that there are no instructions between the PHI and the load that |
| 1188 | // could store. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1189 | for (BasicBlock::iterator BBI(PN); &*BBI != LI; ++BBI) |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1190 | if (BBI->mayWriteToMemory()) |
| 1191 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1192 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1193 | MaxAlign = std::max(MaxAlign, LI->getAlignment()); |
| 1194 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1196 | // Okay, we know that we have one or more loads in the same block as the PHI. |
| 1197 | // We can transform this if it is safe to push the loads into the predecessor |
| 1198 | // blocks. The only thing to watch out for is that we can't put a possibly |
| 1199 | // trapping load in the predecessor if it is a critical edge. |
| 1200 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1201 | BasicBlock *Pred = PN->getIncomingBlock(i); |
Eli Friedman | f9b785f | 2011-09-22 18:56:30 +0000 | [diff] [blame] | 1202 | Value *InVal = PN->getIncomingValue(i); |
| 1203 | |
| 1204 | // If the terminator of the predecessor has side-effects (an invoke), |
| 1205 | // there is no safe place to put a load in the predecessor. |
| 1206 | if (Pred->getTerminator()->mayHaveSideEffects()) |
| 1207 | return false; |
| 1208 | |
| 1209 | // If the value is produced by the terminator of the predecessor |
| 1210 | // (an invoke), there is no valid place to put a load in the predecessor. |
| 1211 | if (Pred->getTerminator() == InVal) |
| 1212 | return false; |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1213 | |
| 1214 | // If the predecessor has a single successor, then the edge isn't critical. |
| 1215 | if (Pred->getTerminator()->getNumSuccessors() == 1) |
| 1216 | continue; |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1217 | |
| 1218 | // If this pointer is always safe to load, or if we can prove that there is |
| 1219 | // already a load in the block, then we can move the load to the pred block. |
Artur Pilipenko | f84dc06 | 2016-01-17 12:35:29 +0000 | [diff] [blame] | 1220 | if (isSafeToLoadUnconditionally(InVal, MaxAlign, Pred->getTerminator())) |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1221 | continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1222 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1223 | return false; |
| 1224 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1229 | |
| 1230 | /// tryToMakeAllocaBePromotable - This returns true if the alloca only has |
| 1231 | /// direct (non-volatile) loads and stores to it. If the alloca is close but |
| 1232 | /// not quite there, this will transform the code to allow promotion. As such, |
| 1233 | /// it is a non-pure predicate. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1234 | static bool tryToMakeAllocaBePromotable(AllocaInst *AI, const DataLayout &DL) { |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1235 | SetVector<Instruction*, SmallVector<Instruction*, 4>, |
| 1236 | SmallPtrSet<Instruction*, 4> > InstsToRewrite; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1237 | for (User *U : AI->users()) { |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1238 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1239 | if (!LI->isSimple()) |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | continue; |
| 1242 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1243 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1244 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1245 | if (SI->getOperand(0) == AI || !SI->isSimple()) |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1246 | return false; // Don't allow a store OF the AI, only INTO the AI. |
| 1247 | continue; |
| 1248 | } |
| 1249 | |
| 1250 | if (SelectInst *SI = dyn_cast<SelectInst>(U)) { |
| 1251 | // If the condition being selected on is a constant, fold the select, yes |
| 1252 | // this does (rarely) happen early on. |
| 1253 | if (ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 1254 | Value *Result = SI->getOperand(1+CI->isZero()); |
| 1255 | SI->replaceAllUsesWith(Result); |
| 1256 | SI->eraseFromParent(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1257 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1258 | // This is very rare and we just scrambled the use list of AI, start |
| 1259 | // over completely. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1260 | return tryToMakeAllocaBePromotable(AI, DL); |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | // If it is safe to turn "load (select c, AI, ptr)" into a select of two |
| 1264 | // loads, then we can transform this by rewriting the select. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1265 | if (!isSafeSelectToSpeculate(SI)) |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1266 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1268 | InstsToRewrite.insert(SI); |
| 1269 | continue; |
| 1270 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1272 | if (PHINode *PN = dyn_cast<PHINode>(U)) { |
| 1273 | if (PN->use_empty()) { // Dead PHIs can be stripped. |
| 1274 | InstsToRewrite.insert(PN); |
| 1275 | continue; |
| 1276 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1278 | // If it is safe to turn "load (phi [AI, ptr, ...])" into a PHI of loads |
| 1279 | // in the pred blocks, then we can transform this by rewriting the PHI. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1280 | if (!isSafePHIToSpeculate(PN)) |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1281 | return false; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1283 | InstsToRewrite.insert(PN); |
| 1284 | continue; |
| 1285 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1286 | |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1287 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) { |
| 1288 | if (onlyUsedByLifetimeMarkers(BCI)) { |
| 1289 | InstsToRewrite.insert(BCI); |
| 1290 | continue; |
| 1291 | } |
| 1292 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1294 | return false; |
| 1295 | } |
| 1296 | |
| 1297 | // If there are no instructions to rewrite, then all uses are load/stores and |
| 1298 | // we're done! |
| 1299 | if (InstsToRewrite.empty()) |
| 1300 | return true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1301 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1302 | // If we have instructions that need to be rewritten for this to be promotable |
| 1303 | // take care of it now. |
| 1304 | for (unsigned i = 0, e = InstsToRewrite.size(); i != e; ++i) { |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1305 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(InstsToRewrite[i])) { |
| 1306 | // This could only be a bitcast used by nothing but lifetime intrinsics. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1307 | for (BitCastInst::user_iterator I = BCI->user_begin(), E = BCI->user_end(); |
| 1308 | I != E;) |
| 1309 | cast<Instruction>(*I++)->eraseFromParent(); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1310 | BCI->eraseFromParent(); |
| 1311 | continue; |
| 1312 | } |
| 1313 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1314 | if (SelectInst *SI = dyn_cast<SelectInst>(InstsToRewrite[i])) { |
| 1315 | // Selects in InstsToRewrite only have load uses. Rewrite each as two |
| 1316 | // loads with a new select. |
| 1317 | while (!SI->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1318 | LoadInst *LI = cast<LoadInst>(SI->user_back()); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1320 | IRBuilder<> Builder(LI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1321 | LoadInst *TrueLoad = |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1322 | Builder.CreateLoad(SI->getTrueValue(), LI->getName()+".t"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1323 | LoadInst *FalseLoad = |
Nick Lewycky | f64a397 | 2011-07-01 06:27:03 +0000 | [diff] [blame] | 1324 | Builder.CreateLoad(SI->getFalseValue(), LI->getName()+".f"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1325 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1326 | // Transfer alignment and AA info if present. |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1327 | TrueLoad->setAlignment(LI->getAlignment()); |
| 1328 | FalseLoad->setAlignment(LI->getAlignment()); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1329 | |
| 1330 | AAMDNodes Tags; |
| 1331 | LI->getAAMetadata(Tags); |
| 1332 | if (Tags) { |
| 1333 | TrueLoad->setAAMetadata(Tags); |
| 1334 | FalseLoad->setAAMetadata(Tags); |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1335 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1337 | Value *V = Builder.CreateSelect(SI->getCondition(), TrueLoad, FalseLoad); |
| 1338 | V->takeName(LI); |
| 1339 | LI->replaceAllUsesWith(V); |
| 1340 | LI->eraseFromParent(); |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1341 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1343 | // Now that all the loads are gone, the select is gone too. |
| 1344 | SI->eraseFromParent(); |
| 1345 | continue; |
| 1346 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1348 | // Otherwise, we have a PHI node which allows us to push the loads into the |
| 1349 | // predecessors. |
| 1350 | PHINode *PN = cast<PHINode>(InstsToRewrite[i]); |
| 1351 | if (PN->use_empty()) { |
| 1352 | PN->eraseFromParent(); |
| 1353 | continue; |
| 1354 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1355 | |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 1356 | Type *LoadTy = AI->getAllocatedType(); |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1357 | PHINode *NewPN = PHINode::Create(LoadTy, PN->getNumIncomingValues(), |
| 1358 | PN->getName()+".ld", PN); |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1359 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1360 | // Get the AA tags and alignment to use from one of the loads. It doesn't |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1361 | // matter which one we get and if any differ, it doesn't matter. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1362 | LoadInst *SomeLoad = cast<LoadInst>(PN->user_back()); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1363 | |
| 1364 | AAMDNodes AATags; |
| 1365 | SomeLoad->getAAMetadata(AATags); |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1366 | unsigned Align = SomeLoad->getAlignment(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1368 | // Rewrite all loads of the PN to use the new PHI. |
| 1369 | while (!PN->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1370 | LoadInst *LI = cast<LoadInst>(PN->user_back()); |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1371 | LI->replaceAllUsesWith(NewPN); |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1372 | LI->eraseFromParent(); |
| 1373 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1375 | // Inject loads into all of the pred blocks. Keep track of which blocks we |
| 1376 | // insert them into in case we have multiple edges from the same block. |
| 1377 | DenseMap<BasicBlock*, LoadInst*> InsertedLoads; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1379 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1380 | BasicBlock *Pred = PN->getIncomingBlock(i); |
| 1381 | LoadInst *&Load = InsertedLoads[Pred]; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1382 | if (!Load) { |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1383 | Load = new LoadInst(PN->getIncomingValue(i), |
| 1384 | PN->getName() + "." + Pred->getName(), |
| 1385 | Pred->getTerminator()); |
| 1386 | Load->setAlignment(Align); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1387 | if (AATags) Load->setAAMetadata(AATags); |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1388 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1389 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1390 | NewPN->addIncoming(Load, Pred); |
| 1391 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | d83e7b0 | 2011-01-24 01:07:11 +0000 | [diff] [blame] | 1393 | PN->eraseFromParent(); |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1394 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | a960725 | 2011-01-23 22:04:55 +0000 | [diff] [blame] | 1396 | ++NumAdjusted; |
| 1397 | return true; |
| 1398 | } |
| 1399 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1400 | bool SROA::performPromotion(Function &F) { |
| 1401 | std::vector<AllocaInst*> Allocas; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1402 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1403 | DominatorTree *DT = nullptr; |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 1404 | if (HasDomTree) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 1405 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1406 | AssumptionCache &AC = |
| 1407 | getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 1409 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1410 | DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false); |
Chris Lattner | 9a95f2a | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 1411 | bool Changed = false; |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1412 | SmallVector<Instruction*, 64> Insts; |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1413 | while (1) { |
| 1414 | Allocas.clear(); |
| 1415 | |
| 1416 | // Find allocas that are safe to promote, by looking at all instructions in |
| 1417 | // the entry node |
| 1418 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 1419 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1420 | if (tryToMakeAllocaBePromotable(AI, DL)) |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1421 | Allocas.push_back(AI); |
| 1422 | |
| 1423 | if (Allocas.empty()) break; |
| 1424 | |
Cameron Zwarich | 4694e69 | 2011-01-18 03:53:26 +0000 | [diff] [blame] | 1425 | if (HasDomTree) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1426 | PromoteMemToReg(Allocas, *DT, nullptr, &AC); |
Chris Lattner | 543384e | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1427 | else { |
| 1428 | SSAUpdater SSA; |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1429 | for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { |
| 1430 | AllocaInst *AI = Allocas[i]; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1432 | // Build list of instructions to promote. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1433 | for (User *U : AI->users()) |
| 1434 | Insts.push_back(cast<Instruction>(U)); |
Devang Patel | a3cbf52 | 2011-07-06 21:09:55 +0000 | [diff] [blame] | 1435 | AllocaPromoter(Insts, SSA, &DIB).run(AI, Insts); |
Chris Lattner | b68ec5c | 2011-01-15 00:12:35 +0000 | [diff] [blame] | 1436 | Insts.clear(); |
| 1437 | } |
Chris Lattner | 543384e | 2011-01-14 07:50:47 +0000 | [diff] [blame] | 1438 | } |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1439 | NumPromoted += Allocas.size(); |
| 1440 | Changed = true; |
| 1441 | } |
| 1442 | |
| 1443 | return Changed; |
| 1444 | } |
| 1445 | |
Chris Lattner | 78d7dbb | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 1446 | |
Bob Wilson | 04365c5 | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1447 | /// ShouldAttemptScalarRepl - Decide if an alloca is a good candidate for |
| 1448 | /// SROA. It must be a struct or array type with a small number of elements. |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1449 | bool SROA::ShouldAttemptScalarRepl(AllocaInst *AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1450 | Type *T = AI->getAllocatedType(); |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1451 | // Do not promote any struct that has too many members. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1452 | if (StructType *ST = dyn_cast<StructType>(T)) |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1453 | return ST->getNumElements() <= StructMemberThreshold; |
| 1454 | // Do not promote any array that has too many elements. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1455 | if (ArrayType *AT = dyn_cast<ArrayType>(T)) |
Nadav Rotem | 4e9012c | 2012-06-21 13:44:31 +0000 | [diff] [blame] | 1456 | return AT->getNumElements() <= ArrayElementThreshold; |
Bob Wilson | 04365c5 | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1457 | return false; |
Chris Lattner | 6ff8568 | 2008-06-22 17:46:21 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1460 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
Chris Lattner | 8cf0941 | 2013-04-18 17:42:14 +0000 | [diff] [blame] | 1461 | // which runs on all of the alloca instructions in the entry block, removing |
| 1462 | // them if they are only used by getelementptr instructions. |
Chris Lattner | 5d8a12e | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 1463 | // |
| 1464 | bool SROA::performScalarRepl(Function &F) { |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1465 | std::vector<AllocaInst*> WorkList; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1466 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1467 | |
Chris Lattner | 9c1172d | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1468 | // Scan the entry basic block, adding allocas to the worklist. |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 1469 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1470 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1471 | if (AllocaInst *A = dyn_cast<AllocaInst>(I)) |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1472 | WorkList.push_back(A); |
| 1473 | |
| 1474 | // Process the worklist |
| 1475 | bool Changed = false; |
| 1476 | while (!WorkList.empty()) { |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1477 | AllocaInst *AI = WorkList.back(); |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1478 | WorkList.pop_back(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | f171af9 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 1480 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 1481 | // with unused elements. |
| 1482 | if (AI->use_empty()) { |
| 1483 | AI->eraseFromParent(); |
Chris Lattner | 9ef4eae | 2010-04-15 23:50:26 +0000 | [diff] [blame] | 1484 | Changed = true; |
Chris Lattner | f171af9 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 1485 | continue; |
| 1486 | } |
Chris Lattner | 09b65ab | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1487 | |
| 1488 | // If this alloca is impossible for us to promote, reject it early. |
| 1489 | if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized()) |
| 1490 | continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 09b65ab | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1492 | // Check to see if we can perform the core SROA transformation. We cannot |
| 1493 | // transform the allocation instruction if it is an array allocation |
| 1494 | // (allocations OF arrays are ok though), and an allocation of a scalar |
| 1495 | // value cannot be decomposed at all. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1496 | uint64_t AllocaSize = DL.getTypeAllocSize(AI->getAllocatedType()); |
Bill Wendling | 3e44bf3 | 2009-03-03 12:12:58 +0000 | [diff] [blame] | 1497 | |
Nick Lewycky | aa46400 | 2009-08-17 05:37:31 +0000 | [diff] [blame] | 1498 | // Do not promote [0 x %struct]. |
| 1499 | if (AllocaSize == 0) continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | 9c1172d | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1501 | // Do not promote any struct whose size is too big. |
| 1502 | if (AllocaSize > SRThreshold) continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1503 | |
Bob Wilson | 04365c5 | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 1504 | // If the alloca looks like a good candidate for scalar replacement, and if |
| 1505 | // all its users can be transformed, then split up the aggregate into its |
| 1506 | // separate elements. |
| 1507 | if (ShouldAttemptScalarRepl(AI) && isSafeAllocaToScalarRepl(AI)) { |
| 1508 | DoScalarReplacement(AI, WorkList); |
| 1509 | Changed = true; |
| 1510 | continue; |
| 1511 | } |
| 1512 | |
Chris Lattner | df17987 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1513 | // If we can turn this aggregate value (potentially with casts) into a |
| 1514 | // simple scalar value that can be mem2reg'd into a register value. |
Chris Lattner | ec99c46 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 1515 | // IsNotTrivial tracks whether this is something that mem2reg could have |
| 1516 | // promoted itself. If so, we don't want to transform it needlessly. Note |
| 1517 | // that we can't just check based on the type: the alloca may be of an i32 |
| 1518 | // but that has pointer arithmetic to set byte 3 of it or something. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1519 | if (AllocaInst *NewAI = |
| 1520 | ConvertToScalarInfo((unsigned)AllocaSize, DL, ScalarLoadThreshold) |
| 1521 | .TryConvert(AI)) { |
Chris Lattner | 09b65ab | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1522 | NewAI->takeName(AI); |
| 1523 | AI->eraseFromParent(); |
| 1524 | ++NumConverted; |
| 1525 | Changed = true; |
| 1526 | continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Chris Lattner | 09b65ab | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 1529 | // Otherwise, couldn't process this alloca. |
Chris Lattner | fb41a50 | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | return Changed; |
| 1533 | } |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1535 | /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl |
| 1536 | /// predicate, do SROA now. |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1537 | void SROA::DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1538 | std::vector<AllocaInst*> &WorkList) { |
David Greene | 48c86be | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 1539 | DEBUG(dbgs() << "Found inst to SROA: " << *AI << '\n'); |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1540 | SmallVector<AllocaInst*, 32> ElementAllocas; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1541 | if (StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1542 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 1543 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1544 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), nullptr, |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1545 | AI->getAlignment(), |
Daniel Dunbar | 132f783 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1546 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1547 | ElementAllocas.push_back(NA); |
| 1548 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 1549 | } |
| 1550 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1551 | ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1552 | ElementAllocas.reserve(AT->getNumElements()); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1553 | Type *ElTy = AT->getElementType(); |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1554 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1555 | AllocaInst *NA = new AllocaInst(ElTy, nullptr, AI->getAlignment(), |
Daniel Dunbar | 132f783 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 1556 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1557 | ElementAllocas.push_back(NA); |
| 1558 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 1559 | } |
| 1560 | } |
| 1561 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1562 | // Now that we have created the new alloca instructions, rewrite all the |
| 1563 | // uses of the old alloca. |
| 1564 | RewriteForScalarRepl(AI, AI, 0, ElementAllocas); |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1565 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1566 | // Now erase any instructions that were made dead while rewriting the alloca. |
| 1567 | DeleteDeadInstructions(); |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1568 | AI->eraseFromParent(); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1569 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1570 | ++NumReplaced; |
Chris Lattner | 31e5add | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 1571 | } |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1572 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1573 | /// DeleteDeadInstructions - Erase instructions on the DeadInstrs list, |
| 1574 | /// recursively including all their operands that become trivially dead. |
| 1575 | void SROA::DeleteDeadInstructions() { |
| 1576 | while (!DeadInsts.empty()) { |
| 1577 | Instruction *I = cast<Instruction>(DeadInsts.pop_back_val()); |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1578 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1579 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 1580 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 1581 | // Zero out the operand and see if it becomes trivially dead. |
| 1582 | // (But, don't add allocas to the dead instruction list -- they are |
| 1583 | // already on the worklist and will be deleted separately.) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1584 | *OI = nullptr; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1585 | if (isInstructionTriviallyDead(U) && !isa<AllocaInst>(U)) |
| 1586 | DeadInsts.push_back(U); |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1587 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1588 | |
| 1589 | I->eraseFromParent(); |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1590 | } |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1591 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 1592 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1593 | /// isSafeForScalarRepl - Check if instruction I is a safe use with regard to |
| 1594 | /// performing scalar replacement of alloca AI. The results are flagged in |
Bob Wilson | 88a0598 | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1595 | /// the Info parameter. Offset indicates the position within AI that is |
| 1596 | /// referenced by this instruction. |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1597 | void SROA::isSafeForScalarRepl(Instruction *I, uint64_t Offset, |
Bob Wilson | 88a0598 | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1598 | AllocaInfo &Info) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1599 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1600 | for (Use &U : I->uses()) { |
| 1601 | Instruction *User = cast<Instruction>(U.getUser()); |
Chris Lattner | 5231070 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 1602 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1603 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1604 | isSafeForScalarRepl(BC, Offset, Info); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1605 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1606 | uint64_t GEPOffset = Offset; |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1607 | isSafeGEP(GEPI, GEPOffset, Info); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1608 | if (!Info.isUnsafe) |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1609 | isSafeForScalarRepl(GEPI, GEPOffset, Info); |
Gabor Greif | 4300fc7 | 2010-06-28 11:20:42 +0000 | [diff] [blame] | 1610 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1611 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1612 | if (!Length || Length->isNegative()) |
Aaron Ballman | a733297 | 2012-03-15 00:05:31 +0000 | [diff] [blame] | 1613 | return MarkUnsafe(Info, User); |
| 1614 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1615 | isSafeMemAccess(Offset, Length->getZExtValue(), nullptr, |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1616 | U.getOperandNo() == 0, Info, MI, |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1617 | true /*AllowWholeAccess*/); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1618 | } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1619 | if (!LI->isSimple()) |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1620 | return MarkUnsafe(Info, User); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1621 | Type *LIType = LI->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1622 | isSafeMemAccess(Offset, DL.getTypeAllocSize(LIType), LIType, false, Info, |
| 1623 | LI, true /*AllowWholeAccess*/); |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1624 | Info.hasALoadOrStore = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1625 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1626 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1627 | // Store is ok if storing INTO the pointer, not storing the pointer |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1628 | if (!SI->isSimple() || SI->getOperand(0) == I) |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1629 | return MarkUnsafe(Info, User); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1630 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1631 | Type *SIType = SI->getOperand(0)->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1632 | isSafeMemAccess(Offset, DL.getTypeAllocSize(SIType), SIType, true, Info, |
| 1633 | SI, true /*AllowWholeAccess*/); |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1634 | Info.hasALoadOrStore = true; |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1635 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 1636 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 1637 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 1638 | return MarkUnsafe(Info, User); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1639 | } else if (isa<PHINode>(User) || isa<SelectInst>(User)) { |
| 1640 | isSafePHISelectUseForScalarRepl(User, Offset, Info); |
| 1641 | } else { |
| 1642 | return MarkUnsafe(Info, User); |
| 1643 | } |
| 1644 | if (Info.isUnsafe) return; |
| 1645 | } |
| 1646 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1648 | |
| 1649 | /// isSafePHIUseForScalarRepl - If we see a PHI node or select using a pointer |
| 1650 | /// derived from the alloca, we can often still split the alloca into elements. |
| 1651 | /// This is useful if we have a large alloca where one element is phi'd |
| 1652 | /// together somewhere: we can SRoA and promote all the other elements even if |
| 1653 | /// we end up not being able to promote this one. |
| 1654 | /// |
| 1655 | /// All we require is that the uses of the PHI do not index into other parts of |
| 1656 | /// the alloca. The most important use case for this is single load and stores |
| 1657 | /// that are PHI'd together, which can happen due to code sinking. |
| 1658 | void SROA::isSafePHISelectUseForScalarRepl(Instruction *I, uint64_t Offset, |
| 1659 | AllocaInfo &Info) { |
| 1660 | // If we've already checked this PHI, don't do it again. |
| 1661 | if (PHINode *PN = dyn_cast<PHINode>(I)) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1662 | if (!Info.CheckedPHIs.insert(PN).second) |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1663 | return; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1664 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1665 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1666 | for (User *U : I->users()) { |
| 1667 | Instruction *UI = cast<Instruction>(U); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1668 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1669 | if (BitCastInst *BC = dyn_cast<BitCastInst>(UI)) { |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1670 | isSafePHISelectUseForScalarRepl(BC, Offset, Info); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1671 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(UI)) { |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1672 | // Only allow "bitcast" GEPs for simplicity. We could generalize this, |
| 1673 | // but would have to prove that we're staying inside of an element being |
| 1674 | // promoted. |
| 1675 | if (!GEPI->hasAllZeroIndices()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1676 | return MarkUnsafe(Info, UI); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1677 | isSafePHISelectUseForScalarRepl(GEPI, Offset, Info); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1678 | } else if (LoadInst *LI = dyn_cast<LoadInst>(UI)) { |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1679 | if (!LI->isSimple()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1680 | return MarkUnsafe(Info, UI); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1681 | Type *LIType = LI->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1682 | isSafeMemAccess(Offset, DL.getTypeAllocSize(LIType), LIType, false, Info, |
| 1683 | LI, false /*AllowWholeAccess*/); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1684 | Info.hasALoadOrStore = true; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1685 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1686 | } else if (StoreInst *SI = dyn_cast<StoreInst>(UI)) { |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1687 | // Store is ok if storing INTO the pointer, not storing the pointer |
Eli Friedman | 7c5dc12 | 2011-09-12 20:23:13 +0000 | [diff] [blame] | 1688 | if (!SI->isSimple() || SI->getOperand(0) == I) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1689 | return MarkUnsafe(Info, UI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1690 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1691 | Type *SIType = SI->getOperand(0)->getType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1692 | isSafeMemAccess(Offset, DL.getTypeAllocSize(SIType), SIType, true, Info, |
| 1693 | SI, false /*AllowWholeAccess*/); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1694 | Info.hasALoadOrStore = true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1695 | } else if (isa<PHINode>(UI) || isa<SelectInst>(UI)) { |
| 1696 | isSafePHISelectUseForScalarRepl(UI, Offset, Info); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1697 | } else { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1698 | return MarkUnsafe(Info, UI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1699 | } |
| 1700 | if (Info.isUnsafe) return; |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1701 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1702 | } |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1703 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1704 | /// isSafeGEP - Check if a GEP instruction can be handled for scalar |
| 1705 | /// replacement. It is safe when all the indices are constant, in-bounds |
| 1706 | /// references, and when the resulting offset corresponds to an element within |
| 1707 | /// the alloca type. The results are flagged in the Info parameter. Upon |
Bob Wilson | 88a0598 | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1708 | /// return, Offset is adjusted as specified by the GEP indices. |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1709 | void SROA::isSafeGEP(GetElementPtrInst *GEPI, |
Bob Wilson | 88a0598 | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1710 | uint64_t &Offset, AllocaInfo &Info) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1711 | gep_type_iterator GEPIt = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 1712 | if (GEPIt == E) |
| 1713 | return; |
Pete Cooper | e24d6a1 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1714 | bool NonConstant = false; |
| 1715 | unsigned NonConstantIdxSize = 0; |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 3f972c9 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1717 | // Walk through the GEP type indices, checking the types that this indexes |
| 1718 | // into. |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1719 | for (; GEPIt != E; ++GEPIt) { |
Chris Lattner | 3f972c9 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1720 | // Ignore struct elements, no extra checking needed for these. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1721 | if ((*GEPIt)->isStructTy()) |
Chris Lattner | 3f972c9 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1722 | continue; |
Matthijs Kooijman | cbe5e16 | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 1723 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1724 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPIt.getOperand()); |
Shuxin Yang | 95adf52 | 2013-04-05 21:07:08 +0000 | [diff] [blame] | 1725 | if (!IdxVal) |
| 1726 | return MarkUnsafe(Info, GEPI); |
Chris Lattner | 3f972c9 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1727 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1728 | |
Bob Wilson | 62a84ea | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1729 | // Compute the offset due to this GEP and check if the alloca has a |
| 1730 | // component element at that offset. |
Bob Wilson | 88a0598 | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1731 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 1732 | // If this GEP is non-constant then the last operand must have been a |
Pete Cooper | e24d6a1 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 1733 | // dynamic index into a vector. Pop this now as it has no impact on the |
| 1734 | // constant part of the offset. |
| 1735 | if (NonConstant) |
| 1736 | Indices.pop_back(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1737 | |
| 1738 | const DataLayout &DL = GEPI->getModule()->getDataLayout(); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 1739 | Offset += DL.getIndexedOffsetInType(GEPI->getSourceElementType(), Indices); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1740 | if (!TypeHasComponent(Info.AI->getAllocatedType(), Offset, NonConstantIdxSize, |
| 1741 | DL)) |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1742 | MarkUnsafe(Info, GEPI); |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1745 | /// isHomogeneousAggregate - Check if type T is a struct or array containing |
| 1746 | /// elements of the same type (which is always true for arrays). If so, |
| 1747 | /// return true with NumElts and EltTy set to the number of elements and the |
| 1748 | /// element type, respectively. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1749 | static bool isHomogeneousAggregate(Type *T, unsigned &NumElts, |
| 1750 | Type *&EltTy) { |
| 1751 | if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1752 | NumElts = AT->getNumElements(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1753 | EltTy = (NumElts == 0 ? nullptr : AT->getElementType()); |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1754 | return true; |
| 1755 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1756 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1757 | NumElts = ST->getNumContainedTypes(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1758 | EltTy = (NumElts == 0 ? nullptr : ST->getContainedType(0)); |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1759 | for (unsigned n = 1; n < NumElts; ++n) { |
| 1760 | if (ST->getContainedType(n) != EltTy) |
| 1761 | return false; |
| 1762 | } |
| 1763 | return true; |
| 1764 | } |
| 1765 | return false; |
| 1766 | } |
| 1767 | |
| 1768 | /// isCompatibleAggregate - Check if T1 and T2 are either the same type or are |
| 1769 | /// "homogeneous" aggregates with the same element type and number of elements. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1770 | static bool isCompatibleAggregate(Type *T1, Type *T2) { |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1771 | if (T1 == T2) |
| 1772 | return true; |
| 1773 | |
| 1774 | unsigned NumElts1, NumElts2; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1775 | Type *EltTy1, *EltTy2; |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1776 | if (isHomogeneousAggregate(T1, NumElts1, EltTy1) && |
| 1777 | isHomogeneousAggregate(T2, NumElts2, EltTy2) && |
| 1778 | NumElts1 == NumElts2 && |
| 1779 | EltTy1 == EltTy2) |
| 1780 | return true; |
| 1781 | |
| 1782 | return false; |
| 1783 | } |
| 1784 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1785 | /// isSafeMemAccess - Check if a load/store/memcpy operates on the entire AI |
| 1786 | /// alloca or has an offset and size that corresponds to a component element |
| 1787 | /// within it. The offset checked here may have been formed from a GEP with a |
| 1788 | /// pointer bitcasted to a different type. |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1789 | /// |
| 1790 | /// If AllowWholeAccess is true, then this allows uses of the entire alloca as a |
| 1791 | /// unit. If false, it only allows accesses known to be in a single element. |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1792 | void SROA::isSafeMemAccess(uint64_t Offset, uint64_t MemSize, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1793 | Type *MemOpType, bool isStore, |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1794 | AllocaInfo &Info, Instruction *TheAccess, |
| 1795 | bool AllowWholeAccess) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1796 | const DataLayout &DL = TheAccess->getModule()->getDataLayout(); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1797 | // Check if this is a load/store of the entire alloca. |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1798 | if (Offset == 0 && AllowWholeAccess && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1799 | MemSize == DL.getTypeAllocSize(Info.AI->getAllocatedType())) { |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1800 | // This can be safe for MemIntrinsics (where MemOpType is 0) and integer |
| 1801 | // loads/stores (which are essentially the same as the MemIntrinsics with |
| 1802 | // regard to copying padding between elements). But, if an alloca is |
| 1803 | // flagged as both a source and destination of such operations, we'll need |
| 1804 | // to check later for padding between elements. |
| 1805 | if (!MemOpType || MemOpType->isIntegerTy()) { |
| 1806 | if (isStore) |
| 1807 | Info.isMemCpyDst = true; |
| 1808 | else |
| 1809 | Info.isMemCpySrc = true; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1810 | return; |
| 1811 | } |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1812 | // This is also safe for references using a type that is compatible with |
| 1813 | // the type of the alloca, so that loads/stores can be rewritten using |
| 1814 | // insertvalue/extractvalue. |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 1815 | if (isCompatibleAggregate(MemOpType, Info.AI->getAllocatedType())) { |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1816 | Info.hasSubelementAccess = true; |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1817 | return; |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1818 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1819 | } |
| 1820 | // Check if the offset/size correspond to a component within the alloca type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1821 | Type *T = Info.AI->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1822 | if (TypeHasComponent(T, Offset, MemSize, DL)) { |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1823 | Info.hasSubelementAccess = true; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1824 | return; |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 1825 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | 3e56c29 | 2011-01-23 07:05:44 +0000 | [diff] [blame] | 1827 | return MarkUnsafe(Info, TheAccess); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | /// TypeHasComponent - Return true if T has a component type with the |
| 1831 | /// specified offset and size. If Size is zero, do not check the size. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1832 | bool SROA::TypeHasComponent(Type *T, uint64_t Offset, uint64_t Size, |
| 1833 | const DataLayout &DL) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1834 | Type *EltTy; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1835 | uint64_t EltSize; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1836 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1837 | const StructLayout *Layout = DL.getStructLayout(ST); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1838 | unsigned EltIdx = Layout->getElementContainingOffset(Offset); |
| 1839 | EltTy = ST->getContainedType(EltIdx); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1840 | EltSize = DL.getTypeAllocSize(EltTy); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1841 | Offset -= Layout->getElementOffset(EltIdx); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1842 | } else if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1843 | EltTy = AT->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1844 | EltSize = DL.getTypeAllocSize(EltTy); |
Bob Wilson | 62a84ea | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1845 | if (Offset >= AT->getNumElements() * EltSize) |
| 1846 | return false; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1847 | Offset %= EltSize; |
Pete Cooper | 1d1fa72 | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 1848 | } else if (VectorType *VT = dyn_cast<VectorType>(T)) { |
| 1849 | EltTy = VT->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1850 | EltSize = DL.getTypeAllocSize(EltTy); |
Pete Cooper | 1d1fa72 | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 1851 | if (Offset >= VT->getNumElements() * EltSize) |
| 1852 | return false; |
| 1853 | Offset %= EltSize; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1854 | } else { |
| 1855 | return false; |
| 1856 | } |
| 1857 | if (Offset == 0 && (Size == 0 || EltSize == Size)) |
| 1858 | return true; |
| 1859 | // Check if the component spans multiple elements. |
| 1860 | if (Offset + Size > EltSize) |
| 1861 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1862 | return TypeHasComponent(EltTy, Offset, Size, DL); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /// RewriteForScalarRepl - Alloca AI is being split into NewElts, so rewrite |
| 1866 | /// the instruction I, which references it, to use the separate elements. |
| 1867 | /// Offset indicates the position within AI that is referenced by this |
| 1868 | /// instruction. |
| 1869 | void SROA::RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1870 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1871 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1872 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E;) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1873 | Use &TheUse = *UI++; |
| 1874 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1875 | |
| 1876 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
| 1877 | RewriteBitCast(BC, AI, Offset, NewElts); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1878 | continue; |
| 1879 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1881 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1882 | RewriteGEP(GEPI, AI, Offset, NewElts); |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1883 | continue; |
| 1884 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1886 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1887 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
| 1888 | uint64_t MemSize = Length->getZExtValue(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1889 | if (Offset == 0 && MemSize == DL.getTypeAllocSize(AI->getAllocatedType())) |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1890 | RewriteMemIntrinUserOfAlloca(MI, I, AI, NewElts); |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1891 | // Otherwise the intrinsic can only touch a single element and the |
| 1892 | // address operand will be updated, so nothing else needs to be done. |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1893 | continue; |
| 1894 | } |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 1895 | |
| 1896 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(User)) { |
| 1897 | if (II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 1898 | II->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 1899 | RewriteLifetimeIntrinsic(II, AI, Offset, NewElts); |
| 1900 | } |
| 1901 | continue; |
| 1902 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1904 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1905 | Type *LIType = LI->getType(); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1906 | |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1907 | if (isCompatibleAggregate(LIType, AI->getAllocatedType())) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1908 | // Replace: |
| 1909 | // %res = load { i32, i32 }* %alloc |
| 1910 | // with: |
| 1911 | // %load.0 = load i32* %alloc.0 |
| 1912 | // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0 |
| 1913 | // %load.1 = load i32* %alloc.1 |
| 1914 | // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1 |
| 1915 | // (Also works for arrays instead of structs) |
| 1916 | Value *Insert = UndefValue::get(LIType); |
Devang Patel | 84bb33a | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1917 | IRBuilder<> Builder(LI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1918 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Devang Patel | 84bb33a | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1919 | Value *Load = Builder.CreateLoad(NewElts[i], "load"); |
| 1920 | Insert = Builder.CreateInsertValue(Insert, Load, i, "insert"); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1921 | } |
| 1922 | LI->replaceAllUsesWith(Insert); |
| 1923 | DeadInsts.push_back(LI); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1924 | } else if (LIType->isIntegerTy() && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1925 | DL.getTypeAllocSize(LIType) == |
| 1926 | DL.getTypeAllocSize(AI->getAllocatedType())) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1927 | // If this is a load of the entire alloca to an integer, rewrite it. |
| 1928 | RewriteLoadUserOfWholeAlloca(LI, AI, NewElts); |
| 1929 | } |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1930 | continue; |
| 1931 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1932 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1933 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1934 | Value *Val = SI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1935 | Type *SIType = Val->getType(); |
Bob Wilson | 08713d3 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1936 | if (isCompatibleAggregate(SIType, AI->getAllocatedType())) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1937 | // Replace: |
| 1938 | // store { i32, i32 } %val, { i32, i32 }* %alloc |
| 1939 | // with: |
| 1940 | // %val.0 = extractvalue { i32, i32 } %val, 0 |
| 1941 | // store i32 %val.0, i32* %alloc.0 |
| 1942 | // %val.1 = extractvalue { i32, i32 } %val, 1 |
| 1943 | // store i32 %val.1, i32* %alloc.1 |
| 1944 | // (Also works for arrays instead of structs) |
Devang Patel | 84bb33a | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1945 | IRBuilder<> Builder(SI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1946 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Devang Patel | 84bb33a | 2011-06-03 19:46:19 +0000 | [diff] [blame] | 1947 | Value *Extract = Builder.CreateExtractValue(Val, i, Val->getName()); |
| 1948 | Builder.CreateStore(Extract, NewElts[i]); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1949 | } |
| 1950 | DeadInsts.push_back(SI); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1951 | } else if (SIType->isIntegerTy() && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1952 | DL.getTypeAllocSize(SIType) == |
| 1953 | DL.getTypeAllocSize(AI->getAllocatedType())) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1954 | // If this is a store of the entire alloca from an integer, rewrite it. |
| 1955 | RewriteStoreUserOfWholeAlloca(SI, AI, NewElts); |
| 1956 | } |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1957 | continue; |
| 1958 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1960 | if (isa<SelectInst>(User) || isa<PHINode>(User)) { |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1961 | // If we have a PHI user of the alloca itself (as opposed to a GEP or |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1962 | // bitcast) we have to rewrite it. GEP and bitcast uses will be RAUW'd to |
| 1963 | // the new pointer. |
| 1964 | if (!isa<AllocaInst>(I)) continue; |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1966 | assert(Offset == 0 && NewElts[0] && |
| 1967 | "Direct alloca use should have a zero offset"); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1968 | |
Chris Lattner | 9491dee | 2011-01-23 08:27:54 +0000 | [diff] [blame] | 1969 | // If we have a use of the alloca, we know the derived uses will be |
| 1970 | // utilizing just the first element of the scalarized result. Insert a |
| 1971 | // bitcast of the first alloca before the user as required. |
| 1972 | AllocaInst *NewAI = NewElts[0]; |
| 1973 | BitCastInst *BCI = new BitCastInst(NewAI, AI->getType(), "", NewAI); |
| 1974 | NewAI->moveBefore(BCI); |
| 1975 | TheUse = BCI; |
| 1976 | continue; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1977 | } |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1981 | /// RewriteBitCast - Update a bitcast reference to the alloca being replaced |
| 1982 | /// and recursively continue updating all of its uses. |
| 1983 | void SROA::RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1984 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1985 | RewriteForScalarRepl(BC, AI, Offset, NewElts); |
| 1986 | if (BC->getOperand(0) != AI) |
| 1987 | return; |
Bob Wilson | f3927b7 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1988 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1989 | // The bitcast references the original alloca. Replace its uses with |
Eli Friedman | ecb4538 | 2011-11-12 02:07:50 +0000 | [diff] [blame] | 1990 | // references to the alloca containing offset zero (which is normally at |
| 1991 | // index zero, but might not be in cases involving structs with elements |
| 1992 | // of size zero). |
| 1993 | Type *T = AI->getAllocatedType(); |
| 1994 | uint64_t EltOffset = 0; |
| 1995 | Type *IdxTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1996 | uint64_t Idx = FindElementAndOffset(T, EltOffset, IdxTy, |
| 1997 | BC->getModule()->getDataLayout()); |
Eli Friedman | ecb4538 | 2011-11-12 02:07:50 +0000 | [diff] [blame] | 1998 | Instruction *Val = NewElts[Idx]; |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1999 | if (Val->getType() != BC->getDestTy()) { |
| 2000 | Val = new BitCastInst(Val, BC->getDestTy(), "", BC); |
| 2001 | Val->takeName(BC); |
Daniel Dunbar | 133efc3 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 2002 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2003 | BC->replaceAllUsesWith(Val); |
| 2004 | DeadInsts.push_back(BC); |
Daniel Dunbar | 133efc3 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2007 | /// FindElementAndOffset - Return the index of the element containing Offset |
| 2008 | /// within the specified type, which must be either a struct or an array. |
| 2009 | /// Sets T to the type of the element and Offset to the offset within that |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2010 | /// element. IdxTy is set to the type of the index result to be used in a |
| 2011 | /// GEP instruction. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2012 | uint64_t SROA::FindElementAndOffset(Type *&T, uint64_t &Offset, Type *&IdxTy, |
| 2013 | const DataLayout &DL) { |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2014 | uint64_t Idx = 0; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2015 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2016 | if (StructType *ST = dyn_cast<StructType>(T)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2017 | const StructLayout *Layout = DL.getStructLayout(ST); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2018 | Idx = Layout->getElementContainingOffset(Offset); |
| 2019 | T = ST->getContainedType(Idx); |
| 2020 | Offset -= Layout->getElementOffset(Idx); |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2021 | IdxTy = Type::getInt32Ty(T->getContext()); |
| 2022 | return Idx; |
Pete Cooper | 1d1fa72 | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 2023 | } else if (ArrayType *AT = dyn_cast<ArrayType>(T)) { |
| 2024 | T = AT->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2025 | uint64_t EltSize = DL.getTypeAllocSize(T); |
Pete Cooper | 1d1fa72 | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 2026 | Idx = Offset / EltSize; |
| 2027 | Offset -= Idx * EltSize; |
| 2028 | IdxTy = Type::getInt64Ty(T->getContext()); |
| 2029 | return Idx; |
Chris Lattner | aaa6ac1 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 2030 | } |
Pete Cooper | 1d1fa72 | 2012-06-14 23:53:53 +0000 | [diff] [blame] | 2031 | VectorType *VT = cast<VectorType>(T); |
| 2032 | T = VT->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2033 | uint64_t EltSize = DL.getTypeAllocSize(T); |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2034 | Idx = Offset / EltSize; |
| 2035 | Offset -= Idx * EltSize; |
| 2036 | IdxTy = Type::getInt64Ty(T->getContext()); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2037 | return Idx; |
| 2038 | } |
| 2039 | |
| 2040 | /// RewriteGEP - Check if this GEP instruction moves the pointer across |
| 2041 | /// elements of the alloca that are being split apart, and if so, rewrite |
| 2042 | /// the GEP to be relative to the new element. |
| 2043 | void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2044 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2045 | uint64_t OldOffset = Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2046 | const DataLayout &DL = GEPI->getModule()->getDataLayout(); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2047 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
Pete Cooper | e24d6a1 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 2048 | // If the GEP was dynamic then it must have been a dynamic vector lookup. |
| 2049 | // In this case, it must be the last GEP operand which is dynamic so keep that |
| 2050 | // aside until we've found the constant GEP offset then add it back in at the |
| 2051 | // end. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2052 | Value* NonConstantIdx = nullptr; |
Pete Cooper | e24d6a1 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 2053 | if (!GEPI->hasAllConstantIndices()) |
| 2054 | NonConstantIdx = Indices.pop_back_val(); |
Eduard Burtescu | 68e7f49 | 2016-01-22 03:08:27 +0000 | [diff] [blame] | 2055 | Offset += DL.getIndexedOffsetInType(GEPI->getSourceElementType(), Indices); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2056 | |
| 2057 | RewriteForScalarRepl(GEPI, AI, Offset, NewElts); |
| 2058 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2059 | Type *T = AI->getAllocatedType(); |
| 2060 | Type *IdxTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2061 | uint64_t OldIdx = FindElementAndOffset(T, OldOffset, IdxTy, DL); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2062 | if (GEPI->getOperand(0) == AI) |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2063 | OldIdx = ~0ULL; // Force the GEP to be rewritten. |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2064 | |
| 2065 | T = AI->getAllocatedType(); |
| 2066 | uint64_t EltOffset = Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2067 | uint64_t Idx = FindElementAndOffset(T, EltOffset, IdxTy, DL); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2068 | |
| 2069 | // If this GEP does not move the pointer across elements of the alloca |
| 2070 | // being split, then it does not needs to be rewritten. |
| 2071 | if (Idx == OldIdx) |
| 2072 | return; |
| 2073 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2074 | Type *i32Ty = Type::getInt32Ty(AI->getContext()); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2075 | SmallVector<Value*, 8> NewArgs; |
| 2076 | NewArgs.push_back(Constant::getNullValue(i32Ty)); |
| 2077 | while (EltOffset != 0) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2078 | uint64_t EltIdx = FindElementAndOffset(T, EltOffset, IdxTy, DL); |
Bob Wilson | c16811b | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 2079 | NewArgs.push_back(ConstantInt::get(IdxTy, EltIdx)); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2080 | } |
Pete Cooper | 818e9f4 | 2012-06-16 01:43:26 +0000 | [diff] [blame] | 2081 | if (NonConstantIdx) { |
| 2082 | Type* GepTy = T; |
| 2083 | // This GEP has a dynamic index. We need to add "i32 0" to index through |
| 2084 | // any structs or arrays in the original type until we get to the vector |
| 2085 | // to index. |
| 2086 | while (!isa<VectorType>(GepTy)) { |
| 2087 | NewArgs.push_back(Constant::getNullValue(i32Ty)); |
| 2088 | GepTy = cast<CompositeType>(GepTy)->getTypeAtIndex(0U); |
| 2089 | } |
Pete Cooper | e24d6a1 | 2012-06-15 18:07:29 +0000 | [diff] [blame] | 2090 | NewArgs.push_back(NonConstantIdx); |
Pete Cooper | 818e9f4 | 2012-06-16 01:43:26 +0000 | [diff] [blame] | 2091 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2092 | Instruction *Val = NewElts[Idx]; |
| 2093 | if (NewArgs.size() > 1) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2094 | Val = GetElementPtrInst::CreateInBounds(Val, NewArgs, "", GEPI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2095 | Val->takeName(GEPI); |
| 2096 | } |
| 2097 | if (Val->getType() != GEPI->getType()) |
Benjamin Kramer | 40582a8 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 2098 | Val = new BitCastInst(Val, GEPI->getType(), Val->getName(), GEPI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2099 | GEPI->replaceAllUsesWith(Val); |
| 2100 | DeadInsts.push_back(GEPI); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2103 | /// RewriteLifetimeIntrinsic - II is a lifetime.start/lifetime.end. Rewrite it |
| 2104 | /// to mark the lifetime of the scalarized memory. |
| 2105 | void SROA::RewriteLifetimeIntrinsic(IntrinsicInst *II, AllocaInst *AI, |
| 2106 | uint64_t Offset, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2107 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2108 | ConstantInt *OldSize = cast<ConstantInt>(II->getArgOperand(0)); |
| 2109 | // Put matching lifetime markers on everything from Offset up to |
| 2110 | // Offset+OldSize. |
| 2111 | Type *AIType = AI->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2112 | const DataLayout &DL = II->getModule()->getDataLayout(); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2113 | uint64_t NewOffset = Offset; |
| 2114 | Type *IdxTy; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2115 | uint64_t Idx = FindElementAndOffset(AIType, NewOffset, IdxTy, DL); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2116 | |
| 2117 | IRBuilder<> Builder(II); |
| 2118 | uint64_t Size = OldSize->getLimitedValue(); |
| 2119 | |
| 2120 | if (NewOffset) { |
| 2121 | // Splice the first element and index 'NewOffset' bytes in. SROA will |
| 2122 | // split the alloca again later. |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 2123 | unsigned AS = AI->getType()->getAddressSpace(); |
| 2124 | Value *V = Builder.CreateBitCast(NewElts[Idx], Builder.getInt8PtrTy(AS)); |
David Blaikie | 93c5444 | 2015-04-03 19:41:44 +0000 | [diff] [blame] | 2125 | V = Builder.CreateGEP(Builder.getInt8Ty(), V, Builder.getInt64(NewOffset)); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2126 | |
| 2127 | IdxTy = NewElts[Idx]->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2128 | uint64_t EltSize = DL.getTypeAllocSize(IdxTy) - NewOffset; |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2129 | if (EltSize > Size) { |
| 2130 | EltSize = Size; |
| 2131 | Size = 0; |
| 2132 | } else { |
| 2133 | Size -= EltSize; |
| 2134 | } |
| 2135 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 2136 | Builder.CreateLifetimeStart(V, Builder.getInt64(EltSize)); |
| 2137 | else |
| 2138 | Builder.CreateLifetimeEnd(V, Builder.getInt64(EltSize)); |
| 2139 | ++Idx; |
| 2140 | } |
| 2141 | |
| 2142 | for (; Idx != NewElts.size() && Size; ++Idx) { |
| 2143 | IdxTy = NewElts[Idx]->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2144 | uint64_t EltSize = DL.getTypeAllocSize(IdxTy); |
Nick Lewycky | 15e2d90 | 2011-07-25 23:14:22 +0000 | [diff] [blame] | 2145 | if (EltSize > Size) { |
| 2146 | EltSize = Size; |
| 2147 | Size = 0; |
| 2148 | } else { |
| 2149 | Size -= EltSize; |
| 2150 | } |
| 2151 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) |
| 2152 | Builder.CreateLifetimeStart(NewElts[Idx], |
| 2153 | Builder.getInt64(EltSize)); |
| 2154 | else |
| 2155 | Builder.CreateLifetimeEnd(NewElts[Idx], |
| 2156 | Builder.getInt64(EltSize)); |
| 2157 | } |
| 2158 | DeadInsts.push_back(II); |
| 2159 | } |
| 2160 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2161 | /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI. |
| 2162 | /// Rewrite it to copy or set the elements of the scalarized memory. |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2163 | void |
| 2164 | SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
| 2165 | AllocaInst *AI, |
| 2166 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2167 | // If this is a memcpy/memmove, construct the other pointer as the |
Chris Lattner | a41bb40 | 2009-03-04 19:23:25 +0000 | [diff] [blame] | 2168 | // appropriate type. The "Other" pointer is the pointer that goes to memory |
| 2169 | // that doesn't have anything to do with the alloca that we are promoting. For |
| 2170 | // memset, this Value* stays null. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2171 | Value *OtherPtr = nullptr; |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2172 | unsigned MemAlignment = MI->getAlignment(); |
Chris Lattner | 334268a | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2173 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2174 | if (Inst == MTI->getRawDest()) |
Chris Lattner | 334268a | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2175 | OtherPtr = MTI->getRawSource(); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2176 | else { |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2177 | assert(Inst == MTI->getRawSource()); |
Chris Lattner | 334268a | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2178 | OtherPtr = MTI->getRawDest(); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
Bob Wilson | 2029ea0 | 2009-12-08 18:22:03 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2182 | // If there is an other pointer, we want to convert it to the same pointer |
| 2183 | // type as AI has, so we can GEP through it safely. |
| 2184 | if (OtherPtr) { |
Chris Lattner | efa3c82 | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2185 | unsigned AddrSpace = |
| 2186 | cast<PointerType>(OtherPtr->getType())->getAddressSpace(); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2187 | |
| 2188 | // Remove bitcasts and all-zero GEPs from OtherPtr. This is an |
| 2189 | // optimization, but it's also required to detect the corner case where |
| 2190 | // both pointer operands are referencing the same memory, and where |
| 2191 | // OtherPtr may be a bitcast or GEP that currently being rewritten. (This |
| 2192 | // function is only called for mem intrinsics that access the whole |
| 2193 | // aggregate, so non-zero GEPs are not an issue here.) |
Chris Lattner | efa3c82 | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2194 | OtherPtr = OtherPtr->stripPointerCasts(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2195 | |
Bob Wilson | 58d59fe | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2196 | // Copying the alloca to itself is a no-op: just delete it. |
| 2197 | if (OtherPtr == AI || OtherPtr == NewElts[0]) { |
| 2198 | // This code will run twice for a no-op memcpy -- once for each operand. |
| 2199 | // Put only one reference to MI on the DeadInsts list. |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 2200 | for (SmallVectorImpl<Value *>::const_iterator I = DeadInsts.begin(), |
Bob Wilson | 58d59fe | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2201 | E = DeadInsts.end(); I != E; ++I) |
| 2202 | if (*I == MI) return; |
| 2203 | DeadInsts.push_back(MI); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2204 | return; |
Bob Wilson | 58d59fe | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2205 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2207 | // If the pointer is not the right type, insert a bitcast to the right |
| 2208 | // type. |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 2209 | Type *NewTy = PointerType::get(AI->getAllocatedType(), AddrSpace); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2210 | |
Chris Lattner | efa3c82 | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 2211 | if (OtherPtr->getType() != NewTy) |
| 2212 | OtherPtr = new BitCastInst(OtherPtr, NewTy, OtherPtr->getName(), MI); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2213 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2214 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2215 | // Process each element of the aggregate. |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2216 | bool SROADest = MI->getRawDest() == Inst; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2217 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2218 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext())); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2219 | const DataLayout &DL = MI->getModule()->getDataLayout(); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2220 | |
| 2221 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2222 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2223 | Value *OtherElt = nullptr; |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2224 | unsigned OtherEltAlign = MemAlignment; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2225 | |
Bob Wilson | 58d59fe | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 2226 | if (OtherPtr) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2227 | Value *Idx[2] = { Zero, |
| 2228 | ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 2229 | OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx, |
Benjamin Kramer | 40582a8 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 2230 | OtherPtr->getName()+"."+Twine(i), |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2231 | MI); |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2232 | uint64_t EltOffset; |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 2233 | Type *OtherTy = AI->getAllocatedType(); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2234 | if (StructType *ST = dyn_cast<StructType>(OtherTy)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2235 | EltOffset = DL.getStructLayout(ST)->getElementOffset(i); |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2236 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2237 | Type *EltTy = cast<SequentialType>(OtherTy)->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2238 | EltOffset = DL.getTypeAllocSize(EltTy) * i; |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2239 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2240 | |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2241 | // The alignment of the other pointer is the guaranteed alignment of the |
| 2242 | // element, which is affected by both the known alignment of the whole |
| 2243 | // mem intrinsic and the alignment of the element. If the alignment of |
| 2244 | // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the |
| 2245 | // known alignment is just 4 bytes. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2246 | OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset); |
Chris Lattner | 9f022d5 | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 2247 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2248 | |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 2249 | AllocaInst *EltPtr = NewElts[i]; |
| 2250 | Type *EltTy = EltPtr->getAllocatedType(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2251 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2252 | // If we got down to a scalar, insert a load or store as appropriate. |
| 2253 | if (EltTy->isSingleValueType()) { |
Chris Lattner | 334268a | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 2254 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2255 | if (SROADest) { |
| 2256 | // From Other to Alloca. |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2257 | Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI); |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2258 | new StoreInst(Elt, EltPtr, MI); |
| 2259 | } else { |
| 2260 | // From Alloca to Other. |
| 2261 | Value *Elt = new LoadInst(EltPtr, "tmp", MI); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2262 | new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI); |
Chris Lattner | 5c204c9 | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 2263 | } |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2264 | continue; |
| 2265 | } |
| 2266 | assert(isa<MemSetInst>(MI)); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2267 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2268 | // If the stored element is zero (common case), just store a null |
| 2269 | // constant. |
| 2270 | Constant *StoreVal; |
Gabor Greif | fe252e6 | 2010-06-30 09:16:16 +0000 | [diff] [blame] | 2271 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getArgOperand(1))) { |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2272 | if (CI->isZero()) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 2273 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2274 | } else { |
| 2275 | // If EltTy is a vector type, get the element type. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2276 | Type *ValTy = EltTy->getScalarType(); |
Dan Gohman | adfd42a | 2009-06-16 00:20:26 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2278 | // Construct an integer with the right value. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2279 | unsigned EltSize = DL.getTypeSizeInBits(ValTy); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2280 | APInt OneVal(EltSize, CI->getZExtValue()); |
| 2281 | APInt TotalVal(OneVal); |
| 2282 | // Set each byte. |
| 2283 | for (unsigned i = 0; 8*i < EltSize; ++i) { |
| 2284 | TotalVal = TotalVal.shl(8); |
| 2285 | TotalVal |= OneVal; |
| 2286 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2288 | // Convert the integer value to the appropriate type. |
Chris Lattner | 1146d32 | 2010-04-16 01:05:38 +0000 | [diff] [blame] | 2289 | StoreVal = ConstantInt::get(CI->getContext(), TotalVal); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2290 | if (ValTy->isPointerTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2291 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2292 | else if (ValTy->isFloatingPointTy()) |
Owen Anderson | 487375e | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 2293 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2294 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2295 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2296 | // If the requested value was a vector constant, create it. |
Cameron Zwarich | 1a761dc | 2011-10-11 21:26:40 +0000 | [diff] [blame] | 2297 | if (EltTy->isVectorTy()) { |
| 2298 | unsigned NumElts = cast<VectorType>(EltTy)->getNumElements(); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2299 | StoreVal = ConstantVector::getSplat(NumElts, StoreVal); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | new StoreInst(StoreVal, EltPtr, MI); |
| 2303 | continue; |
| 2304 | } |
| 2305 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 2306 | // this element. |
| 2307 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2308 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2309 | unsigned EltSize = DL.getTypeAllocSize(EltTy); |
Eli Friedman | ecb4538 | 2011-11-12 02:07:50 +0000 | [diff] [blame] | 2310 | if (!EltSize) |
| 2311 | continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2313 | IRBuilder<> Builder(MI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2315 | // Finally, insert the meminst for this element. |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2316 | if (isa<MemSetInst>(MI)) { |
| 2317 | Builder.CreateMemSet(EltPtr, MI->getArgOperand(1), EltSize, |
| 2318 | MI->isVolatile()); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2319 | } else { |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2320 | assert(isa<MemTransferInst>(MI)); |
| 2321 | Value *Dst = SROADest ? EltPtr : OtherElt; // Dest ptr |
| 2322 | Value *Src = SROADest ? OtherElt : EltPtr; // Src ptr |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2323 | |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2324 | if (isa<MemCpyInst>(MI)) |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2325 | Builder.CreateMemCpy(Dst, Src, EltSize, OtherEltAlign,MI->isVolatile()); |
Chris Lattner | 6cf8d6c | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 2326 | else |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2327 | Builder.CreateMemMove(Dst, Src, EltSize,OtherEltAlign,MI->isVolatile()); |
Chris Lattner | 9a2de65 | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 2328 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2329 | } |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2330 | DeadInsts.push_back(MI); |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2331 | } |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2332 | |
Bob Wilson | 050b812 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 2333 | /// RewriteStoreUserOfWholeAlloca - We found a store of an integer that |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2334 | /// overwrites the entire allocation. Extract out the pieces of the stored |
| 2335 | /// integer and store them individually. |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2336 | void |
| 2337 | SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
| 2338 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2339 | // Extract each element out of the integer according to its structure offset |
| 2340 | // and store the element value to the individual alloca. |
| 2341 | Value *SrcVal = SI->getOperand(0); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2342 | Type *AllocaEltTy = AI->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2343 | const DataLayout &DL = SI->getModule()->getDataLayout(); |
| 2344 | uint64_t AllocaSizeBits = DL.getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2345 | |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2346 | IRBuilder<> Builder(SI); |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2347 | |
Eli Friedman | ee94e3c | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 2348 | // Handle tail padding by extending the operand |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2349 | if (DL.getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits) |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2350 | SrcVal = Builder.CreateZExt(SrcVal, |
| 2351 | IntegerType::get(SI->getContext(), AllocaSizeBits)); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2352 | |
David Greene | 48c86be | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2353 | DEBUG(dbgs() << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << '\n' << *SI |
Nick Lewycky | 7465cd7 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 2354 | << '\n'); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2355 | |
| 2356 | // There are two forms here: AI could be an array or struct. Both cases |
| 2357 | // have different ways to compute the element offset. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2358 | if (StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2359 | const StructLayout *Layout = DL.getStructLayout(EltSTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2360 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2361 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2362 | // Get the number of bits to shift SrcVal to get the value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2363 | Type *FieldTy = EltSTy->getElementType(i); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2364 | uint64_t Shift = Layout->getElementOffsetInBits(i); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2365 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2366 | if (DL.isBigEndian()) |
| 2367 | Shift = AllocaSizeBits - Shift - DL.getTypeAllocSizeInBits(FieldTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2368 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2369 | Value *EltVal = SrcVal; |
| 2370 | if (Shift) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2371 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2372 | EltVal = Builder.CreateLShr(EltVal, ShiftVal, "sroa.store.elt"); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2373 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2375 | // Truncate down to an integer of the right size. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2376 | uint64_t FieldSizeBits = DL.getTypeSizeInBits(FieldTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | ae0e857 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2378 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2379 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2380 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2381 | if (FieldSizeBits != AllocaSizeBits) |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2382 | EltVal = Builder.CreateTrunc(EltVal, |
| 2383 | IntegerType::get(SI->getContext(), FieldSizeBits)); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2384 | Value *DestField = NewElts[i]; |
| 2385 | if (EltVal->getType() == FieldTy) { |
| 2386 | // Storing to an integer field of this size, just do it. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2387 | } else if (FieldTy->isFloatingPointTy() || FieldTy->isVectorTy()) { |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2388 | // Bitcast to the right element type (for fp/vector values). |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2389 | EltVal = Builder.CreateBitCast(EltVal, FieldTy); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | // Otherwise, bitcast the dest pointer (for aggregates). |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2392 | DestField = Builder.CreateBitCast(DestField, |
| 2393 | PointerType::getUnqual(EltVal->getType())); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2394 | } |
| 2395 | new StoreInst(EltVal, DestField, SI); |
| 2396 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2398 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2399 | ArrayType *ATy = cast<ArrayType>(AllocaEltTy); |
| 2400 | Type *ArrayEltTy = ATy->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2401 | uint64_t ElementOffset = DL.getTypeAllocSizeInBits(ArrayEltTy); |
| 2402 | uint64_t ElementSizeBits = DL.getTypeSizeInBits(ArrayEltTy); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2403 | |
| 2404 | uint64_t Shift; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2405 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2406 | if (DL.isBigEndian()) |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2407 | Shift = AllocaSizeBits-ElementOffset; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2408 | else |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2409 | Shift = 0; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2410 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2411 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Chris Lattner | ae0e857 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2412 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2413 | if (ElementSizeBits == 0) continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2415 | Value *EltVal = SrcVal; |
| 2416 | if (Shift) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2417 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2418 | EltVal = Builder.CreateLShr(EltVal, ShiftVal, "sroa.store.elt"); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2419 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2420 | |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2421 | // Truncate down to an integer of the right size. |
| 2422 | if (ElementSizeBits != AllocaSizeBits) |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2423 | EltVal = Builder.CreateTrunc(EltVal, |
| 2424 | IntegerType::get(SI->getContext(), |
| 2425 | ElementSizeBits)); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2426 | Value *DestField = NewElts[i]; |
| 2427 | if (EltVal->getType() == ArrayEltTy) { |
| 2428 | // Storing to an integer field of this size, just do it. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2429 | } else if (ArrayEltTy->isFloatingPointTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2430 | ArrayEltTy->isVectorTy()) { |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2431 | // Bitcast to the right element type (for fp/vector values). |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2432 | EltVal = Builder.CreateBitCast(EltVal, ArrayEltTy); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2433 | } else { |
| 2434 | // Otherwise, bitcast the dest pointer (for aggregates). |
Chris Lattner | 7cd8cf7 | 2011-01-16 05:58:24 +0000 | [diff] [blame] | 2435 | DestField = Builder.CreateBitCast(DestField, |
| 2436 | PointerType::getUnqual(EltVal->getType())); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2437 | } |
| 2438 | new StoreInst(EltVal, DestField, SI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2439 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2440 | if (DL.isBigEndian()) |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2441 | Shift -= ElementOffset; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2442 | else |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2443 | Shift += ElementOffset; |
| 2444 | } |
| 2445 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2446 | |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2447 | DeadInsts.push_back(SI); |
Chris Lattner | f2b8c82 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Bob Wilson | 050b812 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 2450 | /// RewriteLoadUserOfWholeAlloca - We found a load of the entire allocation to |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2451 | /// an integer. Load the individual pieces to form the aggregate value. |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2452 | void |
| 2453 | SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
| 2454 | SmallVectorImpl<AllocaInst *> &NewElts) { |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2455 | // Extract each element out of the NewElts according to its structure offset |
| 2456 | // and form the result value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2457 | Type *AllocaEltTy = AI->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2458 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
| 2459 | uint64_t AllocaSizeBits = DL.getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2460 | |
David Greene | 48c86be | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2461 | DEBUG(dbgs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI |
Nick Lewycky | 7465cd7 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 2462 | << '\n'); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2463 | |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2464 | // There are two forms here: AI could be an array or struct. Both cases |
| 2465 | // have different ways to compute the element offset. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2466 | const StructLayout *Layout = nullptr; |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2467 | uint64_t ArrayEltBitOffset = 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2468 | if (StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2469 | Layout = DL.getStructLayout(EltSTy); |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2470 | } else { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2471 | Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2472 | ArrayEltBitOffset = DL.getTypeAllocSizeInBits(ArrayEltTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | Value *ResultVal = |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2476 | Constant::getNullValue(IntegerType::get(LI->getContext(), AllocaSizeBits)); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2477 | |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2478 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 2479 | // Load the value from the alloca. If the NewElt is an aggregate, cast |
| 2480 | // the pointer to an integer of the same size before doing the load. |
| 2481 | Value *SrcField = NewElts[i]; |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 2482 | Type *FieldTy = NewElts[i]->getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2483 | uint64_t FieldSizeBits = DL.getTypeSizeInBits(FieldTy); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2484 | |
Chris Lattner | ae0e857 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 2485 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 2486 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2488 | IntegerType *FieldIntTy = IntegerType::get(LI->getContext(), |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2489 | FieldSizeBits); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2490 | if (!FieldTy->isIntegerTy() && !FieldTy->isFloatingPointTy() && |
| 2491 | !FieldTy->isVectorTy()) |
Owen Anderson | 340288c | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 2492 | SrcField = new BitCastInst(SrcField, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 2493 | PointerType::getUnqual(FieldIntTy), |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2494 | "", LI); |
| 2495 | SrcField = new LoadInst(SrcField, "sroa.load.elt", LI); |
| 2496 | |
| 2497 | // If SrcField is a fp or vector of the right size but that isn't an |
| 2498 | // integer type, bitcast to an integer so we can shift it. |
| 2499 | if (SrcField->getType() != FieldIntTy) |
| 2500 | SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI); |
| 2501 | |
| 2502 | // Zero extend the field to be the same size as the final alloca so that |
| 2503 | // we can shift and insert it. |
| 2504 | if (SrcField->getType() != ResultVal->getType()) |
| 2505 | SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2506 | |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2507 | // Determine the number of bits to shift SrcField. |
| 2508 | uint64_t Shift; |
| 2509 | if (Layout) // Struct case. |
| 2510 | Shift = Layout->getElementOffsetInBits(i); |
| 2511 | else // Array case. |
| 2512 | Shift = i*ArrayEltBitOffset; |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2513 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2514 | if (DL.isBigEndian()) |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2515 | Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth(); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2516 | |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2517 | if (Shift) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 2518 | Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift); |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2519 | SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI); |
| 2520 | } |
| 2521 | |
Chris Lattner | 25a843f | 2010-06-27 07:58:26 +0000 | [diff] [blame] | 2522 | // Don't create an 'or x, 0' on the first iteration. |
| 2523 | if (!isa<Constant>(ResultVal) || |
| 2524 | !cast<Constant>(ResultVal)->isNullValue()) |
| 2525 | ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI); |
| 2526 | else |
| 2527 | ResultVal = SrcField; |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2528 | } |
Eli Friedman | ee94e3c | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Handle tail padding by truncating the result |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2531 | if (DL.getTypeSizeInBits(LI->getType()) != AllocaSizeBits) |
Eli Friedman | ee94e3c | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 2532 | ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI); |
| 2533 | |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2534 | LI->replaceAllUsesWith(ResultVal); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2535 | DeadInsts.push_back(LI); |
Chris Lattner | c518dfd | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Duncan Sands | 399d979 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 2538 | /// HasPadding - Return true if the specified type has any structure or |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2539 | /// alignment padding in between the elements that would be split apart |
| 2540 | /// by SROA; return false otherwise. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2541 | static bool HasPadding(Type *Ty, const DataLayout &DL) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2542 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2543 | Ty = ATy->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2544 | return DL.getTypeSizeInBits(Ty) != DL.getTypeAllocSizeInBits(Ty); |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2545 | } |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2546 | |
| 2547 | // SROA currently handles only Arrays and Structs. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2548 | StructType *STy = cast<StructType>(Ty); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2549 | const StructLayout *SL = DL.getStructLayout(STy); |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2550 | unsigned PrevFieldBitOffset = 0; |
| 2551 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 2552 | unsigned FieldBitOffset = SL->getElementOffsetInBits(i); |
| 2553 | |
| 2554 | // Check to see if there is any padding between this element and the |
| 2555 | // previous one. |
| 2556 | if (i) { |
| 2557 | unsigned PrevFieldEnd = |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2558 | PrevFieldBitOffset+DL.getTypeSizeInBits(STy->getElementType(i-1)); |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2559 | if (PrevFieldEnd < FieldBitOffset) |
| 2560 | return true; |
| 2561 | } |
| 2562 | PrevFieldBitOffset = FieldBitOffset; |
| 2563 | } |
| 2564 | // Check for tail padding. |
| 2565 | if (unsigned EltCount = STy->getNumElements()) { |
| 2566 | unsigned PrevFieldEnd = PrevFieldBitOffset + |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 2567 | DL.getTypeSizeInBits(STy->getElementType(EltCount-1)); |
Bob Wilson | 12eec40 | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 2568 | if (PrevFieldEnd < SL->getSizeInBits()) |
| 2569 | return true; |
| 2570 | } |
| 2571 | return false; |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2572 | } |
Chris Lattner | 66e6a82 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 2573 | |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 2574 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 2575 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 2576 | /// or 1 if safe after canonicalization has been performed. |
Victor Hernandez | 1df6518 | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2577 | bool SROA::isSafeAllocaToScalarRepl(AllocaInst *AI) { |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 2578 | // Loop over the use list of the alloca. We can only transform it if all of |
| 2579 | // the users are safe to transform. |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 2580 | AllocaInfo Info(AI); |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | 8acbb79 | 2011-01-23 07:29:29 +0000 | [diff] [blame] | 2582 | isSafeForScalarRepl(AI, 0, Info); |
Bob Wilson | 532cd23 | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 2583 | if (Info.isUnsafe) { |
David Greene | 48c86be | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 2584 | DEBUG(dbgs() << "Cannot transform: " << *AI << '\n'); |
Victor Hernandez | 1df6518 | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2585 | return false; |
Chris Lattner | 8881912 | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 2586 | } |
Bob Wilson | 328e91b | 2011-01-13 20:59:44 +0000 | [diff] [blame] | 2587 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2588 | const DataLayout &DL = AI->getModule()->getDataLayout(); |
| 2589 | |
Chris Lattner | 8767920 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 2590 | // Okay, we know all the users are promotable. If the aggregate is a memcpy |
| 2591 | // source and destination, we have to be careful. In particular, the memcpy |
| 2592 | // could be moving around elements that live in structure padding of the LLVM |
| 2593 | // types, but may actually be used. In these cases, we refuse to promote the |
| 2594 | // struct. |
| 2595 | if (Info.isMemCpySrc && Info.isMemCpyDst && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2596 | HasPadding(AI->getAllocatedType(), DL)) |
Victor Hernandez | 1df6518 | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2597 | return false; |
Duncan Sands | 399d979 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | 7c9f4c9 | 2011-01-16 17:46:19 +0000 | [diff] [blame] | 2599 | // If the alloca never has an access to just *part* of it, but is accessed |
| 2600 | // via loads and stores, then we should use ConvertToScalarInfo to promote |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 2601 | // the alloca instead of promoting each piece at a time and inserting fission |
| 2602 | // and fusion code. |
| 2603 | if (!Info.hasSubelementAccess && Info.hasALoadOrStore) { |
| 2604 | // If the struct/array just has one element, use basic SRoA. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2605 | if (StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
Chris Lattner | 6fab2e9 | 2011-01-16 06:18:28 +0000 | [diff] [blame] | 2606 | if (ST->getNumElements() > 1) return false; |
| 2607 | } else { |
| 2608 | if (cast<ArrayType>(AI->getAllocatedType())->getNumElements() > 1) |
| 2609 | return false; |
| 2610 | } |
| 2611 | } |
Nadav Rotem | 465834c | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 2612 | |
Victor Hernandez | 1df6518 | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 2613 | return true; |
Chris Lattner | 6e5398d | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 2614 | } |