Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1 | //===- SROA.cpp - Scalar Replacement Of Aggregates ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This transformation implements the well known scalar replacement of |
| 11 | /// aggregates transformation. It tries to identify promotable elements of an |
| 12 | /// aggregate alloca, and promote them to registers. It will also try to |
| 13 | /// convert uses of an element (or set of elements) of an alloca into a vector |
| 14 | /// or bitfield-style integer scalar if appropriate. |
| 15 | /// |
| 16 | /// It works to do this with minimal slicing of the alloca so that regions |
| 17 | /// which are merely transferred in and out of external memory remain unchanged |
| 18 | /// and are not decomposed to scalar code. |
| 19 | /// |
| 20 | /// Because this also performs alloca promotion, it can be thought of as also |
| 21 | /// serving the purpose of SSA formation. The algorithm iterates on the |
| 22 | /// function until all opportunities for promotion have been realized. |
| 23 | /// |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | #define DEBUG_TYPE "sroa" |
| 27 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/ADT/SetVector.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/PtrUseVisitor.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 35 | #include "llvm/DIBuilder.h" |
| 36 | #include "llvm/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Constants.h" |
| 38 | #include "llvm/IR/DataLayout.h" |
| 39 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Function.h" |
| 42 | #include "llvm/IR/IRBuilder.h" |
| 43 | #include "llvm/IR/Instructions.h" |
| 44 | #include "llvm/IR/IntrinsicInst.h" |
| 45 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Operator.h" |
Chandler Carruth | dbd6958 | 2012-11-30 03:08:41 +0000 | [diff] [blame] | 47 | #include "llvm/InstVisitor.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 48 | #include "llvm/Pass.h" |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
| 52 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 54 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Utils/Local.h" |
| 56 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
| 57 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 58 | using namespace llvm; |
| 59 | |
| 60 | STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 61 | STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed"); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 62 | STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca"); |
| 63 | STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten"); |
| 64 | STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 65 | STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced"); |
| 66 | STATISTIC(NumPromoted, "Number of allocas promoted to SSA values"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 67 | STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 68 | STATISTIC(NumDeleted, "Number of instructions deleted"); |
| 69 | STATISTIC(NumVectorized, "Number of vectorized aggregates"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 71 | /// Hidden option to force the pass to not use DomTree and mem2reg, instead |
| 72 | /// forming SSA values through the SSAUpdater infrastructure. |
| 73 | static cl::opt<bool> |
| 74 | ForceSSAUpdater("force-ssa-updater", cl::init(false), cl::Hidden); |
| 75 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 76 | namespace { |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 77 | /// \brief A custom IRBuilder inserter which prefixes all names if they are |
| 78 | /// preserved. |
| 79 | template <bool preserveNames = true> |
| 80 | class IRBuilderPrefixedInserter : |
| 81 | public IRBuilderDefaultInserter<preserveNames> { |
| 82 | std::string Prefix; |
| 83 | |
| 84 | public: |
| 85 | void SetNamePrefix(const Twine &P) { Prefix = P.str(); } |
| 86 | |
| 87 | protected: |
| 88 | void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, |
| 89 | BasicBlock::iterator InsertPt) const { |
| 90 | IRBuilderDefaultInserter<preserveNames>::InsertHelper( |
| 91 | I, Name.isTriviallyEmpty() ? Name : Prefix + Name, BB, InsertPt); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | // Specialization for not preserving the name is trivial. |
| 96 | template <> |
| 97 | class IRBuilderPrefixedInserter<false> : |
| 98 | public IRBuilderDefaultInserter<false> { |
| 99 | public: |
| 100 | void SetNamePrefix(const Twine &P) {} |
| 101 | }; |
| 102 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 103 | /// \brief Provide a typedef for IRBuilder that drops names in release builds. |
| 104 | #ifndef NDEBUG |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 105 | typedef llvm::IRBuilder<true, ConstantFolder, |
| 106 | IRBuilderPrefixedInserter<true> > IRBuilderTy; |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 107 | #else |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 108 | typedef llvm::IRBuilder<false, ConstantFolder, |
| 109 | IRBuilderPrefixedInserter<false> > IRBuilderTy; |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 110 | #endif |
| 111 | } |
| 112 | |
| 113 | namespace { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 114 | /// \brief A used slice of an alloca. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 115 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 116 | /// This structure represents a slice of an alloca used by some instruction. It |
| 117 | /// stores both the begin and end offsets of this use, a pointer to the use |
| 118 | /// itself, and a flag indicating whether we can classify the use as splittable |
| 119 | /// or not when forming partitions of the alloca. |
| 120 | class Slice { |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 121 | /// \brief The beginning offset of the range. |
| 122 | uint64_t BeginOffset; |
| 123 | |
| 124 | /// \brief The ending offset, not included in the range. |
| 125 | uint64_t EndOffset; |
| 126 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 127 | /// \brief Storage for both the use of this slice and whether it can be |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 128 | /// split. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 129 | PointerIntPair<Use *, 1, bool> UseAndIsSplittable; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 130 | |
| 131 | public: |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 132 | Slice() : BeginOffset(), EndOffset() {} |
| 133 | Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 134 | : BeginOffset(BeginOffset), EndOffset(EndOffset), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 135 | UseAndIsSplittable(U, IsSplittable) {} |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 136 | |
| 137 | uint64_t beginOffset() const { return BeginOffset; } |
| 138 | uint64_t endOffset() const { return EndOffset; } |
| 139 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 140 | bool isSplittable() const { return UseAndIsSplittable.getInt(); } |
| 141 | void makeUnsplittable() { UseAndIsSplittable.setInt(false); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 142 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 143 | Use *getUse() const { return UseAndIsSplittable.getPointer(); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 144 | |
| 145 | bool isDead() const { return getUse() == 0; } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 146 | void kill() { UseAndIsSplittable.setPointer(0); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 147 | |
| 148 | /// \brief Support for ordering ranges. |
| 149 | /// |
| 150 | /// This provides an ordering over ranges such that start offsets are |
| 151 | /// always increasing, and within equal start offsets, the end offsets are |
| 152 | /// decreasing. Thus the spanning range comes first in a cluster with the |
| 153 | /// same start position. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 154 | bool operator<(const Slice &RHS) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 155 | if (beginOffset() < RHS.beginOffset()) return true; |
| 156 | if (beginOffset() > RHS.beginOffset()) return false; |
| 157 | if (isSplittable() != RHS.isSplittable()) return !isSplittable(); |
| 158 | if (endOffset() > RHS.endOffset()) return true; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | /// \brief Support comparison with a single offset to allow binary searches. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 163 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(const Slice &LHS, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 164 | uint64_t RHSOffset) { |
| 165 | return LHS.beginOffset() < RHSOffset; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 166 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 167 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(uint64_t LHSOffset, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 168 | const Slice &RHS) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 169 | return LHSOffset < RHS.beginOffset(); |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 170 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 171 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 172 | bool operator==(const Slice &RHS) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 173 | return isSplittable() == RHS.isSplittable() && |
| 174 | beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 175 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 176 | bool operator!=(const Slice &RHS) const { return !operator==(RHS); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 177 | }; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 178 | } // end anonymous namespace |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 179 | |
| 180 | namespace llvm { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 181 | template <typename T> struct isPodLike; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 182 | template <> struct isPodLike<Slice> { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 183 | static const bool value = true; |
| 184 | }; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | namespace { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 188 | /// \brief Representation of the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 189 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 190 | /// This class represents the slices of an alloca which are formed by its |
| 191 | /// various uses. If a pointer escapes, we can't fully build a representation |
| 192 | /// for the slices used and we reflect that in this structure. The uses are |
| 193 | /// stored, sorted by increasing beginning offset and with unsplittable slices |
| 194 | /// starting at a particular offset before splittable slices. |
| 195 | class AllocaSlices { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 196 | public: |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 197 | /// \brief Construct the slices of a particular alloca. |
| 198 | AllocaSlices(const DataLayout &DL, AllocaInst &AI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 199 | |
| 200 | /// \brief Test whether a pointer to the allocation escapes our analysis. |
| 201 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 202 | /// If this is true, the slices are never fully built and should be |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 203 | /// ignored. |
| 204 | bool isEscaped() const { return PointerEscapingInstr; } |
| 205 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 206 | /// \brief Support for iterating over the slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 207 | /// @{ |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 208 | typedef SmallVectorImpl<Slice>::iterator iterator; |
| 209 | iterator begin() { return Slices.begin(); } |
| 210 | iterator end() { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 211 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 212 | typedef SmallVectorImpl<Slice>::const_iterator const_iterator; |
| 213 | const_iterator begin() const { return Slices.begin(); } |
| 214 | const_iterator end() const { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 215 | /// @} |
| 216 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 217 | /// \brief Allow iterating the dead users for this alloca. |
| 218 | /// |
| 219 | /// These are instructions which will never actually use the alloca as they |
| 220 | /// are outside the allocated range. They are safe to replace with undef and |
| 221 | /// delete. |
| 222 | /// @{ |
| 223 | typedef SmallVectorImpl<Instruction *>::const_iterator dead_user_iterator; |
| 224 | dead_user_iterator dead_user_begin() const { return DeadUsers.begin(); } |
| 225 | dead_user_iterator dead_user_end() const { return DeadUsers.end(); } |
| 226 | /// @} |
| 227 | |
Chandler Carruth | 93a21e7 | 2012-09-14 10:18:49 +0000 | [diff] [blame] | 228 | /// \brief Allow iterating the dead expressions referring to this alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 229 | /// |
| 230 | /// These are operands which have cannot actually be used to refer to the |
| 231 | /// alloca as they are outside its range and the user doesn't correct for |
| 232 | /// that. These mostly consist of PHI node inputs and the like which we just |
| 233 | /// need to replace with undef. |
| 234 | /// @{ |
| 235 | typedef SmallVectorImpl<Use *>::const_iterator dead_op_iterator; |
| 236 | dead_op_iterator dead_op_begin() const { return DeadOperands.begin(); } |
| 237 | dead_op_iterator dead_op_end() const { return DeadOperands.end(); } |
| 238 | /// @} |
| 239 | |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 240 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 241 | void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 242 | void printSlice(raw_ostream &OS, const_iterator I, |
| 243 | StringRef Indent = " ") const; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 244 | void printUse(raw_ostream &OS, const_iterator I, |
| 245 | StringRef Indent = " ") const; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 246 | void print(raw_ostream &OS) const; |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 247 | void dump(const_iterator I) const; |
| 248 | void dump() const; |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 249 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 250 | |
| 251 | private: |
| 252 | template <typename DerivedT, typename RetT = void> class BuilderBase; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 253 | class SliceBuilder; |
| 254 | friend class AllocaSlices::SliceBuilder; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 255 | |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 256 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 257 | /// \brief Handle to alloca instruction to simplify method interfaces. |
| 258 | AllocaInst &AI; |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 259 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 260 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 261 | /// \brief The instruction responsible for this alloca not having a known set |
| 262 | /// of slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 263 | /// |
| 264 | /// When an instruction (potentially) escapes the pointer to the alloca, we |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 265 | /// store a pointer to that here and abort trying to form slices of the |
| 266 | /// alloca. This will be null if the alloca slices are analyzed successfully. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 267 | Instruction *PointerEscapingInstr; |
| 268 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 269 | /// \brief The slices of the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 270 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 271 | /// We store a vector of the slices formed by uses of the alloca here. This |
| 272 | /// vector is sorted by increasing begin offset, and then the unsplittable |
| 273 | /// slices before the splittable ones. See the Slice inner class for more |
| 274 | /// details. |
| 275 | SmallVector<Slice, 8> Slices; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 276 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 277 | /// \brief Instructions which will become dead if we rewrite the alloca. |
| 278 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 279 | /// Note that these are not separated by slice. This is because we expect an |
| 280 | /// alloca to be completely rewritten or not rewritten at all. If rewritten, |
| 281 | /// all these instructions can simply be removed and replaced with undef as |
| 282 | /// they come from outside of the allocated space. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 283 | SmallVector<Instruction *, 8> DeadUsers; |
| 284 | |
| 285 | /// \brief Operands which will become dead if we rewrite the alloca. |
| 286 | /// |
| 287 | /// These are operands that in their particular use can be replaced with |
| 288 | /// undef when we rewrite the alloca. These show up in out-of-bounds inputs |
| 289 | /// to PHI nodes and the like. They aren't entirely dead (there might be |
| 290 | /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we |
| 291 | /// want to swap this particular input for undef to simplify the use lists of |
| 292 | /// the alloca. |
| 293 | SmallVector<Use *, 8> DeadOperands; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 294 | }; |
| 295 | } |
| 296 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 297 | static Value *foldSelectInst(SelectInst &SI) { |
| 298 | // If the condition being selected on is a constant or the same value is |
| 299 | // being selected between, fold the select. Yes this does (rarely) happen |
| 300 | // early on. |
| 301 | if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition())) |
| 302 | return SI.getOperand(1+CI->isZero()); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 303 | if (SI.getOperand(1) == SI.getOperand(2)) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 304 | return SI.getOperand(1); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 305 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 308 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 309 | /// \brief Builder for the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 310 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 311 | /// This class builds a set of alloca slices by recursively visiting the uses |
| 312 | /// of an alloca and making a slice for each load and store at each offset. |
| 313 | class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> { |
| 314 | friend class PtrUseVisitor<SliceBuilder>; |
| 315 | friend class InstVisitor<SliceBuilder>; |
| 316 | typedef PtrUseVisitor<SliceBuilder> Base; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 317 | |
| 318 | const uint64_t AllocSize; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 319 | AllocaSlices &S; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 320 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 321 | SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 322 | SmallDenseMap<Instruction *, uint64_t> PHIOrSelectSizes; |
| 323 | |
| 324 | /// \brief Set to de-duplicate dead instructions found in the use walk. |
| 325 | SmallPtrSet<Instruction *, 4> VisitedDeadInsts; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 326 | |
| 327 | public: |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 328 | SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &S) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 329 | : PtrUseVisitor<SliceBuilder>(DL), |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 330 | AllocSize(DL.getTypeAllocSize(AI.getAllocatedType())), S(S) {} |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 331 | |
| 332 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 333 | void markAsDead(Instruction &I) { |
| 334 | if (VisitedDeadInsts.insert(&I)) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 335 | S.DeadUsers.push_back(&I); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 338 | void insertUse(Instruction &I, const APInt &Offset, uint64_t Size, |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 339 | bool IsSplittable = false) { |
Chandler Carruth | f02b8bf | 2012-12-03 10:59:55 +0000 | [diff] [blame] | 340 | // Completely skip uses which have a zero size or start either before or |
| 341 | // past the end of the allocation. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 342 | if (Size == 0 || Offset.isNegative() || Offset.uge(AllocSize)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 343 | DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @" << Offset |
Chandler Carruth | f02b8bf | 2012-12-03 10:59:55 +0000 | [diff] [blame] | 344 | << " which has zero size or starts outside of the " |
| 345 | << AllocSize << " byte alloca:\n" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 346 | << " alloca: " << S.AI << "\n" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 347 | << " use: " << I << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 348 | return markAsDead(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 351 | uint64_t BeginOffset = Offset.getZExtValue(); |
| 352 | uint64_t EndOffset = BeginOffset + Size; |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 353 | |
| 354 | // Clamp the end offset to the end of the allocation. Note that this is |
| 355 | // formulated to handle even the case where "BeginOffset + Size" overflows. |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 356 | // This may appear superficially to be something we could ignore entirely, |
| 357 | // but that is not so! There may be widened loads or PHI-node uses where |
| 358 | // some instructions are dead but not others. We can't completely ignore |
| 359 | // them, and so have to record at least the information here. |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 360 | assert(AllocSize >= BeginOffset); // Established above. |
| 361 | if (Size > AllocSize - BeginOffset) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 362 | DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @" << Offset |
| 363 | << " to remain within the " << AllocSize << " byte alloca:\n" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 364 | << " alloca: " << S.AI << "\n" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 365 | << " use: " << I << "\n"); |
| 366 | EndOffset = AllocSize; |
| 367 | } |
| 368 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 369 | S.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable)); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void visitBitCastInst(BitCastInst &BC) { |
| 373 | if (BC.use_empty()) |
| 374 | return markAsDead(BC); |
| 375 | |
| 376 | return Base::visitBitCastInst(BC); |
| 377 | } |
| 378 | |
| 379 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 380 | if (GEPI.use_empty()) |
| 381 | return markAsDead(GEPI); |
| 382 | |
| 383 | return Base::visitGetElementPtrInst(GEPI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 386 | void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset, |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 387 | uint64_t Size, bool IsVolatile) { |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 388 | // We allow splitting of loads and stores where the type is an integer type |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 389 | // and cover the entire alloca. This prevents us from splitting over |
| 390 | // eagerly. |
| 391 | // FIXME: In the great blue eventually, we should eagerly split all integer |
| 392 | // loads and stores, and then have a separate step that merges adjacent |
| 393 | // alloca partitions into a single partition suitable for integer widening. |
| 394 | // Or we should skip the merge step and rely on GVN and other passes to |
| 395 | // merge adjacent loads and stores that survive mem2reg. |
| 396 | bool IsSplittable = |
| 397 | Ty->isIntegerTy() && !IsVolatile && Offset == 0 && Size >= AllocSize; |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 398 | |
| 399 | insertUse(I, Offset, Size, IsSplittable); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 402 | void visitLoadInst(LoadInst &LI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 403 | assert((!LI.isSimple() || LI.getType()->isSingleValueType()) && |
| 404 | "All simple FCA loads should have been pre-split"); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 405 | |
| 406 | if (!IsOffsetKnown) |
| 407 | return PI.setAborted(&LI); |
| 408 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 409 | uint64_t Size = DL.getTypeStoreSize(LI.getType()); |
| 410 | return handleLoadOrStore(LI.getType(), LI, Offset, Size, LI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 413 | void visitStoreInst(StoreInst &SI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 414 | Value *ValOp = SI.getValueOperand(); |
| 415 | if (ValOp == *U) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 416 | return PI.setEscapedAndAborted(&SI); |
| 417 | if (!IsOffsetKnown) |
| 418 | return PI.setAborted(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 419 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 420 | uint64_t Size = DL.getTypeStoreSize(ValOp->getType()); |
| 421 | |
| 422 | // If this memory access can be shown to *statically* extend outside the |
| 423 | // bounds of of the allocation, it's behavior is undefined, so simply |
| 424 | // ignore it. Note that this is more strict than the generic clamping |
| 425 | // behavior of insertUse. We also try to handle cases which might run the |
| 426 | // risk of overflow. |
| 427 | // FIXME: We should instead consider the pointer to have escaped if this |
| 428 | // function is being instrumented for addressing bugs or race conditions. |
| 429 | if (Offset.isNegative() || Size > AllocSize || |
| 430 | Offset.ugt(AllocSize - Size)) { |
| 431 | DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @" << Offset |
| 432 | << " which extends past the end of the " << AllocSize |
| 433 | << " byte alloca:\n" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 434 | << " alloca: " << S.AI << "\n" |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 435 | << " use: " << SI << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 436 | return markAsDead(SI); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 439 | assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) && |
| 440 | "All simple FCA stores should have been pre-split"); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 441 | handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 445 | void visitMemSetInst(MemSetInst &II) { |
Chandler Carruth | b0de6dd | 2012-09-14 10:26:34 +0000 | [diff] [blame] | 446 | assert(II.getRawDest() == *U && "Pointer use is not the destination?"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 447 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 448 | if ((Length && Length->getValue() == 0) || |
| 449 | (IsOffsetKnown && !Offset.isNegative() && Offset.uge(AllocSize))) |
| 450 | // Zero-length mem transfer intrinsics can be ignored entirely. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 451 | return markAsDead(II); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 452 | |
| 453 | if (!IsOffsetKnown) |
| 454 | return PI.setAborted(&II); |
| 455 | |
| 456 | insertUse(II, Offset, |
| 457 | Length ? Length->getLimitedValue() |
| 458 | : AllocSize - Offset.getLimitedValue(), |
| 459 | (bool)Length); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 462 | void visitMemTransferInst(MemTransferInst &II) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 463 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 464 | if (Length && Length->getValue() == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 465 | // Zero-length mem transfer intrinsics can be ignored entirely. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 466 | return markAsDead(II); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 467 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 468 | // Because we can visit these intrinsics twice, also check to see if the |
| 469 | // first time marked this instruction as dead. If so, skip it. |
| 470 | if (VisitedDeadInsts.count(&II)) |
| 471 | return; |
| 472 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 473 | if (!IsOffsetKnown) |
| 474 | return PI.setAborted(&II); |
| 475 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 476 | // This side of the transfer is completely out-of-bounds, and so we can |
| 477 | // nuke the entire transfer. However, we also need to nuke the other side |
| 478 | // if already added to our partitions. |
| 479 | // FIXME: Yet another place we really should bypass this when |
| 480 | // instrumenting for ASan. |
| 481 | if (!Offset.isNegative() && Offset.uge(AllocSize)) { |
| 482 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI = MemTransferSliceMap.find(&II); |
| 483 | if (MTPI != MemTransferSliceMap.end()) |
| 484 | S.Slices[MTPI->second].kill(); |
| 485 | return markAsDead(II); |
| 486 | } |
| 487 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 488 | uint64_t RawOffset = Offset.getLimitedValue(); |
| 489 | uint64_t Size = Length ? Length->getLimitedValue() |
| 490 | : AllocSize - RawOffset; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 491 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 492 | // Check for the special case where the same exact value is used for both |
| 493 | // source and dest. |
| 494 | if (*U == II.getRawDest() && *U == II.getRawSource()) { |
| 495 | // For non-volatile transfers this is a no-op. |
| 496 | if (!II.isVolatile()) |
| 497 | return markAsDead(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 498 | |
Nick Lewycky | 6ab9d93 | 2013-07-22 23:38:27 +0000 | [diff] [blame] | 499 | return insertUse(II, Offset, Size, /*IsSplittable=*/false); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 500 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 501 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 502 | // If we have seen both source and destination for a mem transfer, then |
| 503 | // they both point to the same alloca. |
| 504 | bool Inserted; |
| 505 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI; |
| 506 | llvm::tie(MTPI, Inserted) = |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 507 | MemTransferSliceMap.insert(std::make_pair(&II, S.Slices.size())); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 508 | unsigned PrevIdx = MTPI->second; |
| 509 | if (!Inserted) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 510 | Slice &PrevP = S.Slices[PrevIdx]; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 511 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 512 | // Check if the begin offsets match and this is a non-volatile transfer. |
| 513 | // In that case, we can completely elide the transfer. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 514 | if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) { |
| 515 | PrevP.kill(); |
| 516 | return markAsDead(II); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Otherwise we have an offset transfer within the same alloca. We can't |
| 520 | // split those. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 521 | PrevP.makeUnsplittable(); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 524 | // Insert the use now that we've fixed up the splittable nature. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 525 | insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 526 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 527 | // Check that we ended up with a valid index in the map. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 528 | assert(S.Slices[PrevIdx].getUse()->getUser() == &II && |
| 529 | "Map index doesn't point back to a slice with this user."); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | // Disable SRoA for any intrinsics except for lifetime invariants. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 533 | // FIXME: What about debug intrinsics? This matches old behavior, but |
Chandler Carruth | 4b40e00 | 2012-09-14 10:26:36 +0000 | [diff] [blame] | 534 | // doesn't make sense. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 535 | void visitIntrinsicInst(IntrinsicInst &II) { |
| 536 | if (!IsOffsetKnown) |
| 537 | return PI.setAborted(&II); |
| 538 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 539 | if (II.getIntrinsicID() == Intrinsic::lifetime_start || |
| 540 | II.getIntrinsicID() == Intrinsic::lifetime_end) { |
| 541 | ConstantInt *Length = cast<ConstantInt>(II.getArgOperand(0)); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 542 | uint64_t Size = std::min(AllocSize - Offset.getLimitedValue(), |
| 543 | Length->getLimitedValue()); |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 544 | insertUse(II, Offset, Size, true); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 545 | return; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 548 | Base::visitIntrinsicInst(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) { |
| 552 | // We consider any PHI or select that results in a direct load or store of |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 553 | // the same offset to be a viable use for slicing purposes. These uses |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 554 | // are considered unsplittable and the size is the maximum loaded or stored |
| 555 | // size. |
| 556 | SmallPtrSet<Instruction *, 4> Visited; |
| 557 | SmallVector<std::pair<Instruction *, Instruction *>, 4> Uses; |
| 558 | Visited.insert(Root); |
| 559 | Uses.push_back(std::make_pair(cast<Instruction>(*U), Root)); |
Chandler Carruth | 8b907e8 | 2012-09-25 10:03:40 +0000 | [diff] [blame] | 560 | // If there are no loads or stores, the access is dead. We mark that as |
| 561 | // a size zero access. |
| 562 | Size = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 563 | do { |
| 564 | Instruction *I, *UsedI; |
| 565 | llvm::tie(UsedI, I) = Uses.pop_back_val(); |
| 566 | |
| 567 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 568 | Size = std::max(Size, DL.getTypeStoreSize(LI->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 569 | continue; |
| 570 | } |
| 571 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 572 | Value *Op = SI->getOperand(0); |
| 573 | if (Op == UsedI) |
| 574 | return SI; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 575 | Size = std::max(Size, DL.getTypeStoreSize(Op->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 576 | continue; |
| 577 | } |
| 578 | |
| 579 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 580 | if (!GEP->hasAllZeroIndices()) |
| 581 | return GEP; |
| 582 | } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) && |
| 583 | !isa<SelectInst>(I)) { |
| 584 | return I; |
| 585 | } |
| 586 | |
| 587 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE; |
| 588 | ++UI) |
| 589 | if (Visited.insert(cast<Instruction>(*UI))) |
| 590 | Uses.push_back(std::make_pair(I, cast<Instruction>(*UI))); |
| 591 | } while (!Uses.empty()); |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 596 | void visitPHINode(PHINode &PN) { |
| 597 | if (PN.use_empty()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 598 | return markAsDead(PN); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 599 | if (!IsOffsetKnown) |
| 600 | return PI.setAborted(&PN); |
| 601 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 602 | // See if we already have computed info on this node. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 603 | uint64_t &PHISize = PHIOrSelectSizes[&PN]; |
| 604 | if (!PHISize) { |
| 605 | // This is a new PHI node, check for an unsafe use of the PHI node. |
| 606 | if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&PN, PHISize)) |
| 607 | return PI.setAborted(UnsafeI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 610 | // For PHI and select operands outside the alloca, we can't nuke the entire |
| 611 | // phi or select -- the other side might still be relevant, so we special |
| 612 | // case them here and use a separate structure to track the operands |
| 613 | // themselves which should be replaced with undef. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 614 | // FIXME: This should instead be escaped in the event we're instrumenting |
| 615 | // for address sanitization. |
| 616 | if ((Offset.isNegative() && (-Offset).uge(PHISize)) || |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 617 | (!Offset.isNegative() && Offset.uge(AllocSize))) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 618 | S.DeadOperands.push_back(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 622 | insertUse(PN, Offset, PHISize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 623 | } |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 624 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 625 | void visitSelectInst(SelectInst &SI) { |
| 626 | if (SI.use_empty()) |
| 627 | return markAsDead(SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 628 | if (Value *Result = foldSelectInst(SI)) { |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 629 | if (Result == *U) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 630 | // If the result of the constant fold will be the pointer, recurse |
| 631 | // through the select as if we had RAUW'ed it. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 632 | enqueueUsers(SI); |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 633 | else |
Chandler Carruth | 225d4bd | 2012-09-21 23:36:40 +0000 | [diff] [blame] | 634 | // Otherwise the operand to the select is dead, and we can replace it |
| 635 | // with undef. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 636 | S.DeadOperands.push_back(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 637 | |
| 638 | return; |
| 639 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 640 | if (!IsOffsetKnown) |
| 641 | return PI.setAborted(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 642 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 643 | // See if we already have computed info on this node. |
| 644 | uint64_t &SelectSize = PHIOrSelectSizes[&SI]; |
| 645 | if (!SelectSize) { |
| 646 | // This is a new Select, check for an unsafe use of it. |
| 647 | if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&SI, SelectSize)) |
| 648 | return PI.setAborted(UnsafeI); |
| 649 | } |
| 650 | |
| 651 | // For PHI and select operands outside the alloca, we can't nuke the entire |
| 652 | // phi or select -- the other side might still be relevant, so we special |
| 653 | // case them here and use a separate structure to track the operands |
| 654 | // themselves which should be replaced with undef. |
| 655 | // FIXME: This should instead be escaped in the event we're instrumenting |
| 656 | // for address sanitization. |
| 657 | if ((Offset.isNegative() && Offset.uge(SelectSize)) || |
| 658 | (!Offset.isNegative() && Offset.uge(AllocSize))) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 659 | S.DeadOperands.push_back(U); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | insertUse(SI, Offset, SelectSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 666 | /// \brief Disable SROA entirely if there are unhandled users of the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 667 | void visitInstruction(Instruction &I) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 668 | PI.setAborted(&I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 669 | } |
| 670 | }; |
| 671 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 672 | AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI) |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 673 | : |
| 674 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 675 | AI(AI), |
| 676 | #endif |
| 677 | PointerEscapingInstr(0) { |
| 678 | SliceBuilder PB(DL, AI, *this); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 679 | SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 680 | if (PtrI.isEscaped() || PtrI.isAborted()) { |
| 681 | // FIXME: We should sink the escape vs. abort info into the caller nicely, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 682 | // possibly by just storing the PtrInfo in the AllocaSlices. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 683 | PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst() |
| 684 | : PtrI.getAbortingInst(); |
| 685 | assert(PointerEscapingInstr && "Did not track a bad instruction"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 686 | return; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 687 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 688 | |
Benjamin Kramer | 08e5070 | 2013-07-20 08:38:34 +0000 | [diff] [blame] | 689 | Slices.erase(std::remove_if(Slices.begin(), Slices.end(), |
| 690 | std::mem_fun_ref(&Slice::isDead)), |
| 691 | Slices.end()); |
| 692 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 693 | // Sort the uses. This arranges for the offsets to be in ascending order, |
| 694 | // and the sizes to be in descending order. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 695 | std::sort(Slices.begin(), Slices.end()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 698 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 699 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 700 | void AllocaSlices::print(raw_ostream &OS, const_iterator I, |
| 701 | StringRef Indent) const { |
| 702 | printSlice(OS, I, Indent); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 703 | printUse(OS, I, Indent); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 706 | void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I, |
| 707 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 708 | OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 709 | << " slice #" << (I - begin()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 710 | << (I->isSplittable() ? " (splittable)" : "") << "\n"; |
| 711 | } |
| 712 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 713 | void AllocaSlices::printUse(raw_ostream &OS, const_iterator I, |
| 714 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 715 | OS << Indent << " used by: " << *I->getUse()->getUser() << "\n"; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 718 | void AllocaSlices::print(raw_ostream &OS) const { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 719 | if (PointerEscapingInstr) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 720 | OS << "Can't analyze slices for alloca: " << AI << "\n" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 721 | << " A pointer to this alloca escaped by:\n" |
| 722 | << " " << *PointerEscapingInstr << "\n"; |
| 723 | return; |
| 724 | } |
| 725 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 726 | OS << "Slices of alloca: " << AI << "\n"; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 727 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 728 | print(OS, I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 731 | LLVM_DUMP_METHOD void AllocaSlices::dump(const_iterator I) const { |
| 732 | print(dbgs(), I); |
| 733 | } |
| 734 | LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 735 | |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 736 | #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 737 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 738 | namespace { |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 739 | /// \brief Implementation of LoadAndStorePromoter for promoting allocas. |
| 740 | /// |
| 741 | /// This subclass of LoadAndStorePromoter adds overrides to handle promoting |
| 742 | /// the loads and stores of an alloca instruction, as well as updating its |
| 743 | /// debug information. This is used when a domtree is unavailable and thus |
| 744 | /// mem2reg in its full form can't be used to handle promotion of allocas to |
| 745 | /// scalar values. |
| 746 | class AllocaPromoter : public LoadAndStorePromoter { |
| 747 | AllocaInst &AI; |
| 748 | DIBuilder &DIB; |
| 749 | |
| 750 | SmallVector<DbgDeclareInst *, 4> DDIs; |
| 751 | SmallVector<DbgValueInst *, 4> DVIs; |
| 752 | |
| 753 | public: |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 754 | AllocaPromoter(const SmallVectorImpl<Instruction *> &Insts, SSAUpdater &S, |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 755 | AllocaInst &AI, DIBuilder &DIB) |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 756 | : LoadAndStorePromoter(Insts, S), AI(AI), DIB(DIB) {} |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 757 | |
| 758 | void run(const SmallVectorImpl<Instruction*> &Insts) { |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 759 | // Retain the debug information attached to the alloca for use when |
| 760 | // rewriting loads and stores. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 761 | if (MDNode *DebugNode = MDNode::getIfExists(AI.getContext(), &AI)) { |
| 762 | for (Value::use_iterator UI = DebugNode->use_begin(), |
| 763 | UE = DebugNode->use_end(); |
| 764 | UI != UE; ++UI) |
| 765 | if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(*UI)) |
| 766 | DDIs.push_back(DDI); |
| 767 | else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(*UI)) |
| 768 | DVIs.push_back(DVI); |
| 769 | } |
| 770 | |
| 771 | LoadAndStorePromoter::run(Insts); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 772 | |
| 773 | // While we have the debug information, clear it off of the alloca. The |
| 774 | // caller takes care of deleting the alloca. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 775 | while (!DDIs.empty()) |
| 776 | DDIs.pop_back_val()->eraseFromParent(); |
| 777 | while (!DVIs.empty()) |
| 778 | DVIs.pop_back_val()->eraseFromParent(); |
| 779 | } |
| 780 | |
| 781 | virtual bool isInstInList(Instruction *I, |
| 782 | const SmallVectorImpl<Instruction*> &Insts) const { |
Chandler Carruth | c17283b | 2013-08-11 01:56:15 +0000 | [diff] [blame] | 783 | Value *Ptr; |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 784 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) |
Chandler Carruth | c17283b | 2013-08-11 01:56:15 +0000 | [diff] [blame] | 785 | Ptr = LI->getOperand(0); |
| 786 | else |
| 787 | Ptr = cast<StoreInst>(I)->getPointerOperand(); |
| 788 | |
| 789 | // Only used to detect cycles, which will be rare and quickly found as |
| 790 | // we're walking up a chain of defs rather than down through uses. |
| 791 | SmallPtrSet<Value *, 4> Visited; |
| 792 | |
| 793 | do { |
| 794 | if (Ptr == &AI) |
| 795 | return true; |
| 796 | |
| 797 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr)) |
| 798 | Ptr = BCI->getOperand(0); |
| 799 | else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr)) |
| 800 | Ptr = GEPI->getPointerOperand(); |
| 801 | else |
| 802 | return false; |
| 803 | |
| 804 | } while (Visited.insert(Ptr)); |
| 805 | |
| 806 | return false; |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | virtual void updateDebugInfo(Instruction *Inst) const { |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 810 | for (SmallVectorImpl<DbgDeclareInst *>::const_iterator I = DDIs.begin(), |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 811 | E = DDIs.end(); I != E; ++I) { |
| 812 | DbgDeclareInst *DDI = *I; |
| 813 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) |
| 814 | ConvertDebugDeclareToDebugValue(DDI, SI, DIB); |
| 815 | else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
| 816 | ConvertDebugDeclareToDebugValue(DDI, LI, DIB); |
| 817 | } |
Craig Topper | 31ee586 | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 818 | for (SmallVectorImpl<DbgValueInst *>::const_iterator I = DVIs.begin(), |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 819 | E = DVIs.end(); I != E; ++I) { |
| 820 | DbgValueInst *DVI = *I; |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 821 | Value *Arg = 0; |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 822 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 823 | // If an argument is zero extended then use argument directly. The ZExt |
| 824 | // may be zapped by an optimization pass in future. |
| 825 | if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0))) |
| 826 | Arg = dyn_cast<Argument>(ZExt->getOperand(0)); |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 827 | else if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0))) |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 828 | Arg = dyn_cast<Argument>(SExt->getOperand(0)); |
| 829 | if (!Arg) |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 830 | Arg = SI->getValueOperand(); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 831 | } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 832 | Arg = LI->getPointerOperand(); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 833 | } else { |
| 834 | continue; |
| 835 | } |
| 836 | Instruction *DbgVal = |
| 837 | DIB.insertDbgValueIntrinsic(Arg, 0, DIVariable(DVI->getVariable()), |
| 838 | Inst); |
| 839 | DbgVal->setDebugLoc(DVI->getDebugLoc()); |
| 840 | } |
| 841 | } |
| 842 | }; |
| 843 | } // end anon namespace |
| 844 | |
| 845 | |
| 846 | namespace { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 847 | /// \brief An optimization pass providing Scalar Replacement of Aggregates. |
| 848 | /// |
| 849 | /// This pass takes allocations which can be completely analyzed (that is, they |
| 850 | /// don't escape) and tries to turn them into scalar SSA values. There are |
| 851 | /// a few steps to this process. |
| 852 | /// |
| 853 | /// 1) It takes allocations of aggregates and analyzes the ways in which they |
| 854 | /// are used to try to split them into smaller allocations, ideally of |
| 855 | /// a single scalar data type. It will split up memcpy and memset accesses |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 856 | /// as necessary and try to isolate individual scalar accesses. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 857 | /// 2) It will transform accesses into forms which are suitable for SSA value |
| 858 | /// promotion. This can be replacing a memset with a scalar store of an |
| 859 | /// integer value, or it can involve speculating operations on a PHI or |
| 860 | /// select to be a PHI or select of the results. |
| 861 | /// 3) Finally, this will try to detect a pattern of accesses which map cleanly |
| 862 | /// onto insert and extract operations on a vector value, and convert them to |
| 863 | /// this form. By doing so, it will enable promotion of vector aggregates to |
| 864 | /// SSA vector values. |
| 865 | class SROA : public FunctionPass { |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 866 | const bool RequiresDomTree; |
| 867 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 868 | LLVMContext *C; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 869 | const DataLayout *DL; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 870 | DominatorTree *DT; |
| 871 | |
| 872 | /// \brief Worklist of alloca instructions to simplify. |
| 873 | /// |
| 874 | /// Each alloca in the function is added to this. Each new alloca formed gets |
| 875 | /// added to it as well to recursively simplify unless that alloca can be |
| 876 | /// directly promoted. Finally, each time we rewrite a use of an alloca other |
| 877 | /// the one being actively rewritten, we add it back onto the list if not |
| 878 | /// already present to ensure it is re-visited. |
| 879 | SetVector<AllocaInst *, SmallVector<AllocaInst *, 16> > Worklist; |
| 880 | |
| 881 | /// \brief A collection of instructions to delete. |
| 882 | /// We try to batch deletions to simplify code and make things a bit more |
| 883 | /// efficient. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 884 | SetVector<Instruction *, SmallVector<Instruction *, 8> > DeadInsts; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 885 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 886 | /// \brief Post-promotion worklist. |
| 887 | /// |
| 888 | /// Sometimes we discover an alloca which has a high probability of becoming |
| 889 | /// viable for SROA after a round of promotion takes place. In those cases, |
| 890 | /// the alloca is enqueued here for re-processing. |
| 891 | /// |
| 892 | /// Note that we have to be very careful to clear allocas out of this list in |
| 893 | /// the event they are deleted. |
| 894 | SetVector<AllocaInst *, SmallVector<AllocaInst *, 16> > PostPromotionWorklist; |
| 895 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 896 | /// \brief A collection of alloca instructions we can directly promote. |
| 897 | std::vector<AllocaInst *> PromotableAllocas; |
| 898 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 899 | /// \brief A worklist of PHIs to speculate prior to promoting allocas. |
| 900 | /// |
| 901 | /// All of these PHIs have been checked for the safety of speculation and by |
| 902 | /// being speculated will allow promoting allocas currently in the promotable |
| 903 | /// queue. |
| 904 | SetVector<PHINode *, SmallVector<PHINode *, 2> > SpeculatablePHIs; |
| 905 | |
| 906 | /// \brief A worklist of select instructions to speculate prior to promoting |
| 907 | /// allocas. |
| 908 | /// |
| 909 | /// All of these select instructions have been checked for the safety of |
| 910 | /// speculation and by being speculated will allow promoting allocas |
| 911 | /// currently in the promotable queue. |
| 912 | SetVector<SelectInst *, SmallVector<SelectInst *, 2> > SpeculatableSelects; |
| 913 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 914 | public: |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 915 | SROA(bool RequiresDomTree = true) |
| 916 | : FunctionPass(ID), RequiresDomTree(RequiresDomTree), |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 917 | C(0), DL(0), DT(0) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 918 | initializeSROAPass(*PassRegistry::getPassRegistry()); |
| 919 | } |
| 920 | bool runOnFunction(Function &F); |
| 921 | void getAnalysisUsage(AnalysisUsage &AU) const; |
| 922 | |
| 923 | const char *getPassName() const { return "SROA"; } |
| 924 | static char ID; |
| 925 | |
| 926 | private: |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 927 | friend class PHIOrSelectSpeculator; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 928 | friend class AllocaSliceRewriter; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 929 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 930 | bool rewritePartition(AllocaInst &AI, AllocaSlices &S, |
| 931 | AllocaSlices::iterator B, AllocaSlices::iterator E, |
| 932 | int64_t BeginOffset, int64_t EndOffset, |
| 933 | ArrayRef<AllocaSlices::iterator> SplitUses); |
| 934 | bool splitAlloca(AllocaInst &AI, AllocaSlices &S); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 935 | bool runOnAlloca(AllocaInst &AI); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 936 | void clobberUse(Use &U); |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 937 | void deleteDeadInstructions(SmallPtrSet<AllocaInst *, 4> &DeletedAllocas); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 938 | bool promoteAllocas(Function &F); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 939 | }; |
| 940 | } |
| 941 | |
| 942 | char SROA::ID = 0; |
| 943 | |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 944 | FunctionPass *llvm::createSROAPass(bool RequiresDomTree) { |
| 945 | return new SROA(RequiresDomTree); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | INITIALIZE_PASS_BEGIN(SROA, "sroa", "Scalar Replacement Of Aggregates", |
| 949 | false, false) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 950 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 951 | INITIALIZE_PASS_END(SROA, "sroa", "Scalar Replacement Of Aggregates", |
| 952 | false, false) |
| 953 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 954 | /// Walk the range of a partitioning looking for a common type to cover this |
| 955 | /// sequence of slices. |
| 956 | static Type *findCommonType(AllocaSlices::const_iterator B, |
| 957 | AllocaSlices::const_iterator E, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 958 | uint64_t EndOffset) { |
| 959 | Type *Ty = 0; |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 960 | bool IgnoreNonIntegralTypes = false; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 961 | for (AllocaSlices::const_iterator I = B; I != E; ++I) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 962 | Use *U = I->getUse(); |
| 963 | if (isa<IntrinsicInst>(*U->getUser())) |
| 964 | continue; |
| 965 | if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset) |
| 966 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 967 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 968 | Type *UserTy = 0; |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 969 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 970 | UserTy = LI->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 971 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 972 | UserTy = SI->getValueOperand()->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 973 | } else { |
| 974 | IgnoreNonIntegralTypes = true; // Give up on anything but an iN type. |
| 975 | continue; |
| 976 | } |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 977 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 978 | if (IntegerType *ITy = dyn_cast<IntegerType>(UserTy)) { |
| 979 | // If the type is larger than the partition, skip it. We only encounter |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 980 | // this for split integer operations where we want to use the type of the |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 981 | // entity causing the split. Also skip if the type is not a byte width |
| 982 | // multiple. |
| 983 | if (ITy->getBitWidth() % 8 != 0 || |
| 984 | ITy->getBitWidth() / 8 > (EndOffset - B->beginOffset())) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 985 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 986 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 987 | // If we have found an integer type use covering the alloca, use that |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 988 | // regardless of the other types, as integers are often used for |
| 989 | // a "bucket of bits" type. |
| 990 | // |
| 991 | // NB: This *must* be the only return from inside the loop so that the |
| 992 | // order of slices doesn't impact the computed type. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 993 | return ITy; |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 994 | } else if (IgnoreNonIntegralTypes) { |
| 995 | continue; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 996 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 997 | |
| 998 | if (Ty && Ty != UserTy) |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 999 | IgnoreNonIntegralTypes = true; // Give up on anything but an iN type. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1000 | |
| 1001 | Ty = UserTy; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1002 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1003 | return Ty; |
| 1004 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1005 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1006 | /// PHI instructions that use an alloca and are subsequently loaded can be |
| 1007 | /// rewritten to load both input pointers in the pred blocks and then PHI the |
| 1008 | /// results, allowing the load of the alloca to be promoted. |
| 1009 | /// From this: |
| 1010 | /// %P2 = phi [i32* %Alloca, i32* %Other] |
| 1011 | /// %V = load i32* %P2 |
| 1012 | /// to: |
| 1013 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1014 | /// ... |
| 1015 | /// %V2 = load i32* %Other |
| 1016 | /// ... |
| 1017 | /// %V = phi [i32 %V1, i32 %V2] |
| 1018 | /// |
| 1019 | /// We can do this to a select if its only uses are loads and if the operands |
| 1020 | /// to the select can be loaded unconditionally. |
| 1021 | /// |
| 1022 | /// FIXME: This should be hoisted into a generic utility, likely in |
| 1023 | /// Transforms/Util/Local.h |
| 1024 | static bool isSafePHIToSpeculate(PHINode &PN, |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1025 | const DataLayout *DL = 0) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1026 | // For now, we can only do this promotion if the load is in the same block |
| 1027 | // as the PHI, and if there are no stores between the phi and load. |
| 1028 | // TODO: Allow recursive phi users. |
| 1029 | // TODO: Allow stores. |
| 1030 | BasicBlock *BB = PN.getParent(); |
| 1031 | unsigned MaxAlign = 0; |
| 1032 | bool HaveLoad = false; |
| 1033 | for (Value::use_iterator UI = PN.use_begin(), UE = PN.use_end(); UI != UE; |
| 1034 | ++UI) { |
| 1035 | LoadInst *LI = dyn_cast<LoadInst>(*UI); |
| 1036 | if (LI == 0 || !LI->isSimple()) |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1037 | return false; |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1038 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1039 | // For now we only allow loads in the same block as the PHI. This is |
| 1040 | // a common case that happens when instcombine merges two loads through |
| 1041 | // a PHI. |
| 1042 | if (LI->getParent() != BB) |
| 1043 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1044 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1045 | // Ensure that there are no instructions between the PHI and the load that |
| 1046 | // could store. |
| 1047 | for (BasicBlock::iterator BBI = &PN; &*BBI != LI; ++BBI) |
| 1048 | if (BBI->mayWriteToMemory()) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1049 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1050 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1051 | MaxAlign = std::max(MaxAlign, LI->getAlignment()); |
| 1052 | HaveLoad = true; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1055 | if (!HaveLoad) |
| 1056 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1057 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1058 | // We can only transform this if it is safe to push the loads into the |
| 1059 | // predecessor blocks. The only thing to watch out for is that we can't put |
| 1060 | // a possibly trapping load in the predecessor if it is a critical edge. |
| 1061 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
| 1062 | TerminatorInst *TI = PN.getIncomingBlock(Idx)->getTerminator(); |
| 1063 | Value *InVal = PN.getIncomingValue(Idx); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1064 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1065 | // If the value is produced by the terminator of the predecessor (an |
| 1066 | // invoke) or it has side-effects, there is no valid place to put a load |
| 1067 | // in the predecessor. |
| 1068 | if (TI == InVal || TI->mayHaveSideEffects()) |
| 1069 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1070 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1071 | // If the predecessor has a single successor, then the edge isn't |
| 1072 | // critical. |
| 1073 | if (TI->getNumSuccessors() == 1) |
| 1074 | continue; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1075 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1076 | // If this pointer is always safe to load, or if we can prove that there |
| 1077 | // is already a load in the block, then we can move the load to the pred |
| 1078 | // block. |
| 1079 | if (InVal->isDereferenceablePointer() || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1080 | isSafeToLoadUnconditionally(InVal, TI, MaxAlign, DL)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1081 | continue; |
| 1082 | |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | static void speculatePHINodeLoads(PHINode &PN) { |
| 1090 | DEBUG(dbgs() << " original: " << PN << "\n"); |
| 1091 | |
| 1092 | Type *LoadTy = cast<PointerType>(PN.getType())->getElementType(); |
| 1093 | IRBuilderTy PHIBuilder(&PN); |
| 1094 | PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(), |
| 1095 | PN.getName() + ".sroa.speculated"); |
| 1096 | |
| 1097 | // Get the TBAA tag and alignment to use from one of the loads. It doesn't |
| 1098 | // matter which one we get and if any differ. |
| 1099 | LoadInst *SomeLoad = cast<LoadInst>(*PN.use_begin()); |
| 1100 | MDNode *TBAATag = SomeLoad->getMetadata(LLVMContext::MD_tbaa); |
| 1101 | unsigned Align = SomeLoad->getAlignment(); |
| 1102 | |
| 1103 | // Rewrite all loads of the PN to use the new PHI. |
| 1104 | while (!PN.use_empty()) { |
| 1105 | LoadInst *LI = cast<LoadInst>(*PN.use_begin()); |
| 1106 | LI->replaceAllUsesWith(NewPN); |
| 1107 | LI->eraseFromParent(); |
| 1108 | } |
| 1109 | |
| 1110 | // Inject loads into all of the pred blocks. |
| 1111 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
| 1112 | BasicBlock *Pred = PN.getIncomingBlock(Idx); |
| 1113 | TerminatorInst *TI = Pred->getTerminator(); |
| 1114 | Value *InVal = PN.getIncomingValue(Idx); |
| 1115 | IRBuilderTy PredBuilder(TI); |
| 1116 | |
| 1117 | LoadInst *Load = PredBuilder.CreateLoad( |
| 1118 | InVal, (PN.getName() + ".sroa.speculate.load." + Pred->getName())); |
| 1119 | ++NumLoadsSpeculated; |
| 1120 | Load->setAlignment(Align); |
| 1121 | if (TBAATag) |
| 1122 | Load->setMetadata(LLVMContext::MD_tbaa, TBAATag); |
| 1123 | NewPN->addIncoming(Load, Pred); |
| 1124 | } |
| 1125 | |
| 1126 | DEBUG(dbgs() << " speculated to: " << *NewPN << "\n"); |
| 1127 | PN.eraseFromParent(); |
| 1128 | } |
| 1129 | |
| 1130 | /// Select instructions that use an alloca and are subsequently loaded can be |
| 1131 | /// rewritten to load both input pointers and then select between the result, |
| 1132 | /// allowing the load of the alloca to be promoted. |
| 1133 | /// From this: |
| 1134 | /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other |
| 1135 | /// %V = load i32* %P2 |
| 1136 | /// to: |
| 1137 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1138 | /// %V2 = load i32* %Other |
| 1139 | /// %V = select i1 %cond, i32 %V1, i32 %V2 |
| 1140 | /// |
| 1141 | /// We can do this to a select if its only uses are loads and if the operand |
| 1142 | /// to the select can be loaded unconditionally. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1143 | static bool isSafeSelectToSpeculate(SelectInst &SI, const DataLayout *DL = 0) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1144 | Value *TValue = SI.getTrueValue(); |
| 1145 | Value *FValue = SI.getFalseValue(); |
| 1146 | bool TDerefable = TValue->isDereferenceablePointer(); |
| 1147 | bool FDerefable = FValue->isDereferenceablePointer(); |
| 1148 | |
| 1149 | for (Value::use_iterator UI = SI.use_begin(), UE = SI.use_end(); UI != UE; |
| 1150 | ++UI) { |
| 1151 | LoadInst *LI = dyn_cast<LoadInst>(*UI); |
| 1152 | if (LI == 0 || !LI->isSimple()) |
| 1153 | return false; |
| 1154 | |
| 1155 | // Both operands to the select need to be dereferencable, either |
| 1156 | // absolutely (e.g. allocas) or at this point because we can see other |
| 1157 | // accesses to it. |
| 1158 | if (!TDerefable && |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1159 | !isSafeToLoadUnconditionally(TValue, LI, LI->getAlignment(), DL)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1160 | return false; |
| 1161 | if (!FDerefable && |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1162 | !isSafeToLoadUnconditionally(FValue, LI, LI->getAlignment(), DL)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | } |
| 1165 | |
| 1166 | return true; |
| 1167 | } |
| 1168 | |
| 1169 | static void speculateSelectInstLoads(SelectInst &SI) { |
| 1170 | DEBUG(dbgs() << " original: " << SI << "\n"); |
| 1171 | |
| 1172 | IRBuilderTy IRB(&SI); |
| 1173 | Value *TV = SI.getTrueValue(); |
| 1174 | Value *FV = SI.getFalseValue(); |
| 1175 | // Replace the loads of the select with a select of two loads. |
| 1176 | while (!SI.use_empty()) { |
| 1177 | LoadInst *LI = cast<LoadInst>(*SI.use_begin()); |
| 1178 | assert(LI->isSimple() && "We only speculate simple loads"); |
| 1179 | |
| 1180 | IRB.SetInsertPoint(LI); |
| 1181 | LoadInst *TL = |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1182 | IRB.CreateLoad(TV, LI->getName() + ".sroa.speculate.load.true"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1183 | LoadInst *FL = |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1184 | IRB.CreateLoad(FV, LI->getName() + ".sroa.speculate.load.false"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1185 | NumLoadsSpeculated += 2; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1186 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1187 | // Transfer alignment and TBAA info if present. |
| 1188 | TL->setAlignment(LI->getAlignment()); |
| 1189 | FL->setAlignment(LI->getAlignment()); |
| 1190 | if (MDNode *Tag = LI->getMetadata(LLVMContext::MD_tbaa)) { |
| 1191 | TL->setMetadata(LLVMContext::MD_tbaa, Tag); |
| 1192 | FL->setMetadata(LLVMContext::MD_tbaa, Tag); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1193 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1194 | |
| 1195 | Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL, |
| 1196 | LI->getName() + ".sroa.speculated"); |
| 1197 | |
| 1198 | DEBUG(dbgs() << " speculated to: " << *V << "\n"); |
| 1199 | LI->replaceAllUsesWith(V); |
| 1200 | LI->eraseFromParent(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1201 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1202 | SI.eraseFromParent(); |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1205 | /// \brief Build a GEP out of a base pointer and indices. |
| 1206 | /// |
| 1207 | /// This will return the BasePtr if that is valid, or build a new GEP |
| 1208 | /// instruction using the IRBuilder if GEP-ing is needed. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1209 | static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1210 | SmallVectorImpl<Value *> &Indices) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1211 | if (Indices.empty()) |
| 1212 | return BasePtr; |
| 1213 | |
| 1214 | // A single zero index is a no-op, so check for this and avoid building a GEP |
| 1215 | // in that case. |
| 1216 | if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero()) |
| 1217 | return BasePtr; |
| 1218 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1219 | return IRB.CreateInBoundsGEP(BasePtr, Indices, "idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /// \brief Get a natural GEP off of the BasePtr walking through Ty toward |
| 1223 | /// TargetTy without changing the offset of the pointer. |
| 1224 | /// |
| 1225 | /// This routine assumes we've already established a properly offset GEP with |
| 1226 | /// Indices, and arrived at the Ty type. The goal is to continue to GEP with |
| 1227 | /// zero-indices down through type layers until we find one the same as |
| 1228 | /// TargetTy. If we can't find one with the same type, we at least try to use |
| 1229 | /// one with the same size. If none of that works, we just produce the GEP as |
| 1230 | /// indicated by Indices to have the correct offset. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1231 | static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1232 | Value *BasePtr, Type *Ty, Type *TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1233 | SmallVectorImpl<Value *> &Indices) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1234 | if (Ty == TargetTy) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1235 | return buildGEP(IRB, BasePtr, Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1236 | |
| 1237 | // See if we can descend into a struct and locate a field with the correct |
| 1238 | // type. |
| 1239 | unsigned NumLayers = 0; |
| 1240 | Type *ElementTy = Ty; |
| 1241 | do { |
| 1242 | if (ElementTy->isPointerTy()) |
| 1243 | break; |
| 1244 | if (SequentialType *SeqTy = dyn_cast<SequentialType>(ElementTy)) { |
| 1245 | ElementTy = SeqTy->getElementType(); |
Chandler Carruth | 40617f5 | 2012-10-17 07:22:16 +0000 | [diff] [blame] | 1246 | // Note that we use the default address space as this index is over an |
| 1247 | // array or a vector, not a pointer. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1248 | Indices.push_back(IRB.getInt(APInt(DL.getPointerSizeInBits(0), 0))); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1249 | } else if (StructType *STy = dyn_cast<StructType>(ElementTy)) { |
Chandler Carruth | 503eb2b | 2012-10-09 01:58:35 +0000 | [diff] [blame] | 1250 | if (STy->element_begin() == STy->element_end()) |
| 1251 | break; // Nothing left to descend into. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1252 | ElementTy = *STy->element_begin(); |
| 1253 | Indices.push_back(IRB.getInt32(0)); |
| 1254 | } else { |
| 1255 | break; |
| 1256 | } |
| 1257 | ++NumLayers; |
| 1258 | } while (ElementTy != TargetTy); |
| 1259 | if (ElementTy != TargetTy) |
| 1260 | Indices.erase(Indices.end() - NumLayers, Indices.end()); |
| 1261 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1262 | return buildGEP(IRB, BasePtr, Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /// \brief Recursively compute indices for a natural GEP. |
| 1266 | /// |
| 1267 | /// This is the recursive step for getNaturalGEPWithOffset that walks down the |
| 1268 | /// element types adding appropriate indices for the GEP. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1269 | static Value *getNaturalGEPRecursively(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1270 | Value *Ptr, Type *Ty, APInt &Offset, |
| 1271 | Type *TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1272 | SmallVectorImpl<Value *> &Indices) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1273 | if (Offset == 0) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1274 | return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1275 | |
| 1276 | // We can't recurse through pointer types. |
| 1277 | if (Ty->isPointerTy()) |
| 1278 | return 0; |
| 1279 | |
Chandler Carruth | dd3cea8 | 2012-09-14 10:30:40 +0000 | [diff] [blame] | 1280 | // We try to analyze GEPs over vectors here, but note that these GEPs are |
| 1281 | // extremely poorly defined currently. The long-term goal is to remove GEPing |
| 1282 | // over a vector from the IR completely. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1283 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1284 | unsigned ElementSizeInBits = DL.getTypeSizeInBits(VecTy->getScalarType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1285 | if (ElementSizeInBits % 8) |
Chandler Carruth | dd3cea8 | 2012-09-14 10:30:40 +0000 | [diff] [blame] | 1286 | return 0; // GEPs over non-multiple of 8 size vector elements are invalid. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1287 | APInt ElementSize(Offset.getBitWidth(), ElementSizeInBits / 8); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1288 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1289 | if (NumSkippedElements.ugt(VecTy->getNumElements())) |
| 1290 | return 0; |
| 1291 | Offset -= NumSkippedElements * ElementSize; |
| 1292 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1293 | return getNaturalGEPRecursively(IRB, DL, Ptr, VecTy->getElementType(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1294 | Offset, TargetTy, Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 1298 | Type *ElementTy = ArrTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1299 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1300 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1301 | if (NumSkippedElements.ugt(ArrTy->getNumElements())) |
| 1302 | return 0; |
| 1303 | |
| 1304 | Offset -= NumSkippedElements * ElementSize; |
| 1305 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1306 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1307 | Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | StructType *STy = dyn_cast<StructType>(Ty); |
| 1311 | if (!STy) |
| 1312 | return 0; |
| 1313 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1314 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1315 | uint64_t StructOffset = Offset.getZExtValue(); |
Chandler Carruth | cabd96c | 2012-09-14 10:30:42 +0000 | [diff] [blame] | 1316 | if (StructOffset >= SL->getSizeInBytes()) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1317 | return 0; |
| 1318 | unsigned Index = SL->getElementContainingOffset(StructOffset); |
| 1319 | Offset -= APInt(Offset.getBitWidth(), SL->getElementOffset(Index)); |
| 1320 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1321 | if (Offset.uge(DL.getTypeAllocSize(ElementTy))) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1322 | return 0; // The offset points into alignment padding. |
| 1323 | |
| 1324 | Indices.push_back(IRB.getInt32(Index)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1325 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1326 | Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | /// \brief Get a natural GEP from a base pointer to a particular offset and |
| 1330 | /// resulting in a particular type. |
| 1331 | /// |
| 1332 | /// The goal is to produce a "natural" looking GEP that works with the existing |
| 1333 | /// composite types to arrive at the appropriate offset and element type for |
| 1334 | /// a pointer. TargetTy is the element type the returned GEP should point-to if |
| 1335 | /// possible. We recurse by decreasing Offset, adding the appropriate index to |
| 1336 | /// Indices, and setting Ty to the result subtype. |
| 1337 | /// |
Chandler Carruth | 93a21e7 | 2012-09-14 10:18:49 +0000 | [diff] [blame] | 1338 | /// If no natural GEP can be constructed, this function returns null. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1339 | static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1340 | Value *Ptr, APInt Offset, Type *TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1341 | SmallVectorImpl<Value *> &Indices) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1342 | PointerType *Ty = cast<PointerType>(Ptr->getType()); |
| 1343 | |
| 1344 | // Don't consider any GEPs through an i8* as natural unless the TargetTy is |
| 1345 | // an i8. |
| 1346 | if (Ty == IRB.getInt8PtrTy() && TargetTy->isIntegerTy(8)) |
| 1347 | return 0; |
| 1348 | |
| 1349 | Type *ElementTy = Ty->getElementType(); |
Chandler Carruth | 3f882d4 | 2012-09-18 22:37:19 +0000 | [diff] [blame] | 1350 | if (!ElementTy->isSized()) |
| 1351 | return 0; // We can't GEP through an unsized element. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1352 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1353 | if (ElementSize == 0) |
| 1354 | return 0; // Zero-length arrays can't help us build a natural GEP. |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1355 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1356 | |
| 1357 | Offset -= NumSkippedElements * ElementSize; |
| 1358 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1359 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1360 | Indices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /// \brief Compute an adjusted pointer from Ptr by Offset bytes where the |
| 1364 | /// resulting pointer has PointerTy. |
| 1365 | /// |
| 1366 | /// This tries very hard to compute a "natural" GEP which arrives at the offset |
| 1367 | /// and produces the pointer type desired. Where it cannot, it will try to use |
| 1368 | /// the natural GEP to arrive at the offset and bitcast to the type. Where that |
| 1369 | /// fails, it will try to use an existing i8* and GEP to the byte offset and |
| 1370 | /// bitcast to the type. |
| 1371 | /// |
| 1372 | /// The strategy for finding the more natural GEPs is to peel off layers of the |
| 1373 | /// pointer, walking back through bit casts and GEPs, searching for a base |
| 1374 | /// pointer from which we can compute a natural GEP with the desired |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 1375 | /// properties. The algorithm tries to fold as many constant indices into |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1376 | /// a single GEP as possible, thus making each GEP more independent of the |
| 1377 | /// surrounding code. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1378 | static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1379 | Value *Ptr, APInt Offset, Type *PointerTy) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1380 | // Even though we don't look through PHI nodes, we could be called on an |
| 1381 | // instruction in an unreachable block, which may be on a cycle. |
| 1382 | SmallPtrSet<Value *, 4> Visited; |
| 1383 | Visited.insert(Ptr); |
| 1384 | SmallVector<Value *, 4> Indices; |
| 1385 | |
| 1386 | // We may end up computing an offset pointer that has the wrong type. If we |
| 1387 | // never are able to compute one directly that has the correct type, we'll |
| 1388 | // fall back to it, so keep it around here. |
| 1389 | Value *OffsetPtr = 0; |
| 1390 | |
| 1391 | // Remember any i8 pointer we come across to re-use if we need to do a raw |
| 1392 | // byte offset. |
| 1393 | Value *Int8Ptr = 0; |
| 1394 | APInt Int8PtrOffset(Offset.getBitWidth(), 0); |
| 1395 | |
| 1396 | Type *TargetTy = PointerTy->getPointerElementType(); |
| 1397 | |
| 1398 | do { |
| 1399 | // First fold any existing GEPs into the offset. |
| 1400 | while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
| 1401 | APInt GEPOffset(Offset.getBitWidth(), 0); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1402 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | Offset += GEPOffset; |
| 1405 | Ptr = GEP->getPointerOperand(); |
| 1406 | if (!Visited.insert(Ptr)) |
| 1407 | break; |
| 1408 | } |
| 1409 | |
| 1410 | // See if we can perform a natural GEP here. |
| 1411 | Indices.clear(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1412 | if (Value *P = getNaturalGEPWithOffset(IRB, DL, Ptr, Offset, TargetTy, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1413 | Indices)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1414 | if (P->getType() == PointerTy) { |
| 1415 | // Zap any offset pointer that we ended up computing in previous rounds. |
| 1416 | if (OffsetPtr && OffsetPtr->use_empty()) |
| 1417 | if (Instruction *I = dyn_cast<Instruction>(OffsetPtr)) |
| 1418 | I->eraseFromParent(); |
| 1419 | return P; |
| 1420 | } |
| 1421 | if (!OffsetPtr) { |
| 1422 | OffsetPtr = P; |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | // Stash this pointer if we've found an i8*. |
| 1427 | if (Ptr->getType()->isIntegerTy(8)) { |
| 1428 | Int8Ptr = Ptr; |
| 1429 | Int8PtrOffset = Offset; |
| 1430 | } |
| 1431 | |
| 1432 | // Peel off a layer of the pointer and update the offset appropriately. |
| 1433 | if (Operator::getOpcode(Ptr) == Instruction::BitCast) { |
| 1434 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 1435 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
| 1436 | if (GA->mayBeOverridden()) |
| 1437 | break; |
| 1438 | Ptr = GA->getAliasee(); |
| 1439 | } else { |
| 1440 | break; |
| 1441 | } |
| 1442 | assert(Ptr->getType()->isPointerTy() && "Unexpected operand type!"); |
| 1443 | } while (Visited.insert(Ptr)); |
| 1444 | |
| 1445 | if (!OffsetPtr) { |
| 1446 | if (!Int8Ptr) { |
| 1447 | Int8Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1448 | "raw_cast"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1449 | Int8PtrOffset = Offset; |
| 1450 | } |
| 1451 | |
| 1452 | OffsetPtr = Int8PtrOffset == 0 ? Int8Ptr : |
| 1453 | IRB.CreateInBoundsGEP(Int8Ptr, IRB.getInt(Int8PtrOffset), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1454 | "raw_idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1455 | } |
| 1456 | Ptr = OffsetPtr; |
| 1457 | |
| 1458 | // On the off chance we were targeting i8*, guard the bitcast here. |
| 1459 | if (Ptr->getType() != PointerTy) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1460 | Ptr = IRB.CreateBitCast(Ptr, PointerTy, "cast"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1461 | |
| 1462 | return Ptr; |
| 1463 | } |
| 1464 | |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1465 | /// \brief Test whether we can convert a value from the old to the new type. |
| 1466 | /// |
| 1467 | /// This predicate should be used to guard calls to convertValue in order to |
| 1468 | /// ensure that we only try to convert viable values. The strategy is that we |
| 1469 | /// will peel off single element struct and array wrappings to get to an |
| 1470 | /// underlying value, and convert that value. |
| 1471 | static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy) { |
| 1472 | if (OldTy == NewTy) |
| 1473 | return true; |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 1474 | if (IntegerType *OldITy = dyn_cast<IntegerType>(OldTy)) |
| 1475 | if (IntegerType *NewITy = dyn_cast<IntegerType>(NewTy)) |
| 1476 | if (NewITy->getBitWidth() >= OldITy->getBitWidth()) |
| 1477 | return true; |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1478 | if (DL.getTypeSizeInBits(NewTy) != DL.getTypeSizeInBits(OldTy)) |
| 1479 | return false; |
| 1480 | if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType()) |
| 1481 | return false; |
| 1482 | |
Benjamin Kramer | 5626259 | 2013-09-22 11:24:58 +0000 | [diff] [blame] | 1483 | // We can convert pointers to integers and vice-versa. Same for vectors |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1484 | // of pointers and integers. |
| 1485 | OldTy = OldTy->getScalarType(); |
| 1486 | NewTy = NewTy->getScalarType(); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1487 | if (NewTy->isPointerTy() || OldTy->isPointerTy()) { |
| 1488 | if (NewTy->isPointerTy() && OldTy->isPointerTy()) |
| 1489 | return true; |
| 1490 | if (NewTy->isIntegerTy() || OldTy->isIntegerTy()) |
| 1491 | return true; |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | return true; |
| 1496 | } |
| 1497 | |
| 1498 | /// \brief Generic routine to convert an SSA value to a value of a different |
| 1499 | /// type. |
| 1500 | /// |
| 1501 | /// This will try various different casting techniques, such as bitcasts, |
| 1502 | /// inttoptr, and ptrtoint casts. Use the \c canConvertValue predicate to test |
| 1503 | /// two types for viability with this routine. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1504 | static Value *convertValue(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1505 | Type *NewTy) { |
| 1506 | Type *OldTy = V->getType(); |
| 1507 | assert(canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type"); |
| 1508 | |
| 1509 | if (OldTy == NewTy) |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1510 | return V; |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (IntegerType *OldITy = dyn_cast<IntegerType>(OldTy)) |
| 1513 | if (IntegerType *NewITy = dyn_cast<IntegerType>(NewTy)) |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 1514 | if (NewITy->getBitWidth() > OldITy->getBitWidth()) |
| 1515 | return IRB.CreateZExt(V, NewITy); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1516 | |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1517 | // See if we need inttoptr for this type pair. A cast involving both scalars |
| 1518 | // and vectors requires and additional bitcast. |
| 1519 | if (OldTy->getScalarType()->isIntegerTy() && |
| 1520 | NewTy->getScalarType()->isPointerTy()) { |
| 1521 | // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8* |
| 1522 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1523 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1524 | NewTy); |
| 1525 | |
| 1526 | // Expand i128 to <2 x i8*> --> i128 to <2 x i64> to <2 x i8*> |
| 1527 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1528 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1529 | NewTy); |
| 1530 | |
| 1531 | return IRB.CreateIntToPtr(V, NewTy); |
| 1532 | } |
| 1533 | |
| 1534 | // See if we need ptrtoint for this type pair. A cast involving both scalars |
| 1535 | // and vectors requires and additional bitcast. |
| 1536 | if (OldTy->getScalarType()->isPointerTy() && |
| 1537 | NewTy->getScalarType()->isIntegerTy()) { |
| 1538 | // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128 |
| 1539 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1540 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1541 | NewTy); |
| 1542 | |
| 1543 | // Expand i8* to <2 x i32> --> i8* to i64 to <2 x i32> |
| 1544 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1545 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1546 | NewTy); |
| 1547 | |
| 1548 | return IRB.CreatePtrToInt(V, NewTy); |
| 1549 | } |
| 1550 | |
| 1551 | return IRB.CreateBitCast(V, NewTy); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1554 | /// \brief Test whether the given slice use can be promoted to a vector. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1555 | /// |
| 1556 | /// This function is called to test each entry in a partioning which is slated |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1557 | /// for a single slice. |
| 1558 | static bool isVectorPromotionViableForSlice( |
| 1559 | const DataLayout &DL, AllocaSlices &S, uint64_t SliceBeginOffset, |
| 1560 | uint64_t SliceEndOffset, VectorType *Ty, uint64_t ElementSize, |
| 1561 | AllocaSlices::const_iterator I) { |
| 1562 | // First validate the slice offsets. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1563 | uint64_t BeginOffset = |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1564 | std::max(I->beginOffset(), SliceBeginOffset) - SliceBeginOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1565 | uint64_t BeginIndex = BeginOffset / ElementSize; |
| 1566 | if (BeginIndex * ElementSize != BeginOffset || |
| 1567 | BeginIndex >= Ty->getNumElements()) |
| 1568 | return false; |
| 1569 | uint64_t EndOffset = |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1570 | std::min(I->endOffset(), SliceEndOffset) - SliceBeginOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1571 | uint64_t EndIndex = EndOffset / ElementSize; |
| 1572 | if (EndIndex * ElementSize != EndOffset || EndIndex > Ty->getNumElements()) |
| 1573 | return false; |
| 1574 | |
| 1575 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 1576 | uint64_t NumElements = EndIndex - BeginIndex; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1577 | Type *SliceTy = |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1578 | (NumElements == 1) ? Ty->getElementType() |
| 1579 | : VectorType::get(Ty->getElementType(), NumElements); |
| 1580 | |
| 1581 | Type *SplitIntTy = |
| 1582 | Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8); |
| 1583 | |
| 1584 | Use *U = I->getUse(); |
| 1585 | |
| 1586 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 1587 | if (MI->isVolatile()) |
| 1588 | return false; |
| 1589 | if (!I->isSplittable()) |
| 1590 | return false; // Skip any unsplittable intrinsics. |
| 1591 | } else if (U->get()->getType()->getPointerElementType()->isStructTy()) { |
| 1592 | // Disable vector promotion when there are loads or stores of an FCA. |
| 1593 | return false; |
| 1594 | } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 1595 | if (LI->isVolatile()) |
| 1596 | return false; |
| 1597 | Type *LTy = LI->getType(); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1598 | if (SliceBeginOffset > I->beginOffset() || |
| 1599 | SliceEndOffset < I->endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1600 | assert(LTy->isIntegerTy()); |
| 1601 | LTy = SplitIntTy; |
| 1602 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1603 | if (!canConvertValue(DL, SliceTy, LTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1604 | return false; |
| 1605 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 1606 | if (SI->isVolatile()) |
| 1607 | return false; |
| 1608 | Type *STy = SI->getValueOperand()->getType(); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1609 | if (SliceBeginOffset > I->beginOffset() || |
| 1610 | SliceEndOffset < I->endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1611 | assert(STy->isIntegerTy()); |
| 1612 | STy = SplitIntTy; |
| 1613 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1614 | if (!canConvertValue(DL, STy, SliceTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1615 | return false; |
Chandler Carruth | 1ed848d | 2013-07-19 10:57:32 +0000 | [diff] [blame] | 1616 | } else { |
| 1617 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | return true; |
| 1621 | } |
| 1622 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1623 | /// \brief Test whether the given alloca partitioning and range of slices can be |
| 1624 | /// promoted to a vector. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1625 | /// |
| 1626 | /// This is a quick test to check whether we can rewrite a particular alloca |
| 1627 | /// partition (and its newly formed alloca) into a vector alloca with only |
| 1628 | /// whole-vector loads and stores such that it could be promoted to a vector |
| 1629 | /// SSA value. We only can ensure this for a limited set of operations, and we |
| 1630 | /// don't want to do the rewrites unless we are confident that the result will |
| 1631 | /// be promotable, so we have an early test here. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1632 | static bool |
| 1633 | isVectorPromotionViable(const DataLayout &DL, Type *AllocaTy, AllocaSlices &S, |
| 1634 | uint64_t SliceBeginOffset, uint64_t SliceEndOffset, |
| 1635 | AllocaSlices::const_iterator I, |
| 1636 | AllocaSlices::const_iterator E, |
| 1637 | ArrayRef<AllocaSlices::iterator> SplitUses) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1638 | VectorType *Ty = dyn_cast<VectorType>(AllocaTy); |
| 1639 | if (!Ty) |
| 1640 | return false; |
| 1641 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1642 | uint64_t ElementSize = DL.getTypeSizeInBits(Ty->getScalarType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1643 | |
| 1644 | // While the definition of LLVM vectors is bitpacked, we don't support sizes |
| 1645 | // that aren't byte sized. |
| 1646 | if (ElementSize % 8) |
| 1647 | return false; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1648 | assert((DL.getTypeSizeInBits(Ty) % 8) == 0 && |
Benjamin Kramer | c003a45 | 2013-01-01 16:13:35 +0000 | [diff] [blame] | 1649 | "vector size not a multiple of element size?"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1650 | ElementSize /= 8; |
| 1651 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1652 | for (; I != E; ++I) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1653 | if (!isVectorPromotionViableForSlice(DL, S, SliceBeginOffset, |
| 1654 | SliceEndOffset, Ty, ElementSize, I)) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1655 | return false; |
| 1656 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1657 | for (ArrayRef<AllocaSlices::iterator>::const_iterator SUI = SplitUses.begin(), |
| 1658 | SUE = SplitUses.end(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1659 | SUI != SUE; ++SUI) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1660 | if (!isVectorPromotionViableForSlice(DL, S, SliceBeginOffset, |
| 1661 | SliceEndOffset, Ty, ElementSize, *SUI)) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1662 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1663 | |
| 1664 | return true; |
| 1665 | } |
| 1666 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1667 | /// \brief Test whether a slice of an alloca is valid for integer widening. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1668 | /// |
| 1669 | /// This implements the necessary checking for the \c isIntegerWideningViable |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1670 | /// test below on a single slice of the alloca. |
| 1671 | static bool isIntegerWideningViableForSlice(const DataLayout &DL, |
| 1672 | Type *AllocaTy, |
| 1673 | uint64_t AllocBeginOffset, |
| 1674 | uint64_t Size, AllocaSlices &S, |
| 1675 | AllocaSlices::const_iterator I, |
| 1676 | bool &WholeAllocaOp) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1677 | uint64_t RelBegin = I->beginOffset() - AllocBeginOffset; |
| 1678 | uint64_t RelEnd = I->endOffset() - AllocBeginOffset; |
| 1679 | |
| 1680 | // We can't reasonably handle cases where the load or store extends past |
| 1681 | // the end of the aloca's type and into its padding. |
| 1682 | if (RelEnd > Size) |
| 1683 | return false; |
| 1684 | |
| 1685 | Use *U = I->getUse(); |
| 1686 | |
| 1687 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 1688 | if (LI->isVolatile()) |
| 1689 | return false; |
| 1690 | if (RelBegin == 0 && RelEnd == Size) |
| 1691 | WholeAllocaOp = true; |
| 1692 | if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1693 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1694 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1695 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1696 | !canConvertValue(DL, AllocaTy, LI->getType())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1697 | // Non-integer loads need to be convertible from the alloca type so that |
| 1698 | // they are promotable. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1699 | return false; |
| 1700 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1701 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 1702 | Type *ValueTy = SI->getValueOperand()->getType(); |
| 1703 | if (SI->isVolatile()) |
| 1704 | return false; |
| 1705 | if (RelBegin == 0 && RelEnd == Size) |
| 1706 | WholeAllocaOp = true; |
| 1707 | if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1708 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1709 | return false; |
| 1710 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1711 | !canConvertValue(DL, ValueTy, AllocaTy)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1712 | // Non-integer stores need to be convertible to the alloca type so that |
| 1713 | // they are promotable. |
| 1714 | return false; |
| 1715 | } |
| 1716 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 1717 | if (MI->isVolatile() || !isa<Constant>(MI->getLength())) |
| 1718 | return false; |
| 1719 | if (!I->isSplittable()) |
| 1720 | return false; // Skip any unsplittable intrinsics. |
| 1721 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) { |
| 1722 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 1723 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 1724 | return false; |
| 1725 | } else { |
| 1726 | return false; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1727 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1728 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
| 1731 | |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1732 | /// \brief Test whether the given alloca partition's integer operations can be |
| 1733 | /// widened to promotable ones. |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1734 | /// |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1735 | /// This is a quick test to check whether we can rewrite the integer loads and |
| 1736 | /// stores to a particular alloca into wider loads and stores and be able to |
| 1737 | /// promote the resulting alloca. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1738 | static bool |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1739 | isIntegerWideningViable(const DataLayout &DL, Type *AllocaTy, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1740 | uint64_t AllocBeginOffset, AllocaSlices &S, |
| 1741 | AllocaSlices::const_iterator I, |
| 1742 | AllocaSlices::const_iterator E, |
| 1743 | ArrayRef<AllocaSlices::iterator> SplitUses) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1744 | uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy); |
Benjamin Kramer | 47534c7 | 2012-12-01 11:53:32 +0000 | [diff] [blame] | 1745 | // Don't create integer types larger than the maximum bitwidth. |
| 1746 | if (SizeInBits > IntegerType::MAX_INT_BITS) |
| 1747 | return false; |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Don't try to handle allocas with bit-padding. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1750 | if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1751 | return false; |
| 1752 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 1753 | // We need to ensure that an integer type with the appropriate bitwidth can |
| 1754 | // be converted to the alloca type, whatever that is. We don't want to force |
| 1755 | // the alloca itself to have an integer type if there is a more suitable one. |
| 1756 | Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1757 | if (!canConvertValue(DL, AllocaTy, IntTy) || |
| 1758 | !canConvertValue(DL, IntTy, AllocaTy)) |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 1759 | return false; |
| 1760 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1761 | uint64_t Size = DL.getTypeStoreSize(AllocaTy); |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1762 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1763 | // While examining uses, we ensure that the alloca has a covering load or |
| 1764 | // store. We don't want to widen the integer operations only to fail to |
| 1765 | // promote due to some other unsplittable entry (which we may make splittable |
Chandler Carruth | 5955c9e | 2013-07-19 07:12:23 +0000 | [diff] [blame] | 1766 | // later). However, if there are only splittable uses, go ahead and assume |
| 1767 | // that we cover the alloca. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1768 | bool WholeAllocaOp = (I != E) ? false : DL.isLegalInteger(SizeInBits); |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 1769 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1770 | for (; I != E; ++I) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1771 | if (!isIntegerWideningViableForSlice(DL, AllocaTy, AllocBeginOffset, Size, |
| 1772 | S, I, WholeAllocaOp)) |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 1773 | return false; |
| 1774 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1775 | for (ArrayRef<AllocaSlices::iterator>::const_iterator SUI = SplitUses.begin(), |
| 1776 | SUE = SplitUses.end(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1777 | SUI != SUE; ++SUI) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1778 | if (!isIntegerWideningViableForSlice(DL, AllocaTy, AllocBeginOffset, Size, |
| 1779 | S, *SUI, WholeAllocaOp)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1780 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1781 | |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1782 | return WholeAllocaOp; |
| 1783 | } |
| 1784 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1785 | static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1786 | IntegerType *Ty, uint64_t Offset, |
| 1787 | const Twine &Name) { |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1788 | DEBUG(dbgs() << " start: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1789 | IntegerType *IntTy = cast<IntegerType>(V->getType()); |
| 1790 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 1791 | "Element extends past full value"); |
| 1792 | uint64_t ShAmt = 8*Offset; |
| 1793 | if (DL.isBigEndian()) |
| 1794 | ShAmt = 8*(DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1795 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1796 | V = IRB.CreateLShr(V, ShAmt, Name + ".shift"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1797 | DEBUG(dbgs() << " shifted: " << *V << "\n"); |
| 1798 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1799 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 1800 | "Cannot extract to a larger integer!"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1801 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1802 | V = IRB.CreateTrunc(V, Ty, Name + ".trunc"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1803 | DEBUG(dbgs() << " trunced: " << *V << "\n"); |
| 1804 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1805 | return V; |
| 1806 | } |
| 1807 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1808 | static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1809 | Value *V, uint64_t Offset, const Twine &Name) { |
| 1810 | IntegerType *IntTy = cast<IntegerType>(Old->getType()); |
| 1811 | IntegerType *Ty = cast<IntegerType>(V->getType()); |
| 1812 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 1813 | "Cannot insert a larger integer!"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1814 | DEBUG(dbgs() << " start: " << *V << "\n"); |
| 1815 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1816 | V = IRB.CreateZExt(V, IntTy, Name + ".ext"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1817 | DEBUG(dbgs() << " extended: " << *V << "\n"); |
| 1818 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1819 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 1820 | "Element store outside of alloca store"); |
| 1821 | uint64_t ShAmt = 8*Offset; |
| 1822 | if (DL.isBigEndian()) |
| 1823 | ShAmt = 8*(DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1824 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1825 | V = IRB.CreateShl(V, ShAmt, Name + ".shift"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1826 | DEBUG(dbgs() << " shifted: " << *V << "\n"); |
| 1827 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1828 | |
| 1829 | if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) { |
| 1830 | APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt); |
| 1831 | Old = IRB.CreateAnd(Old, Mask, Name + ".mask"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1832 | DEBUG(dbgs() << " masked: " << *Old << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1833 | V = IRB.CreateOr(Old, V, Name + ".insert"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 1834 | DEBUG(dbgs() << " inserted: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 1835 | } |
| 1836 | return V; |
| 1837 | } |
| 1838 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1839 | static Value *extractVector(IRBuilderTy &IRB, Value *V, |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 1840 | unsigned BeginIndex, unsigned EndIndex, |
| 1841 | const Twine &Name) { |
| 1842 | VectorType *VecTy = cast<VectorType>(V->getType()); |
| 1843 | unsigned NumElements = EndIndex - BeginIndex; |
| 1844 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 1845 | |
| 1846 | if (NumElements == VecTy->getNumElements()) |
| 1847 | return V; |
| 1848 | |
| 1849 | if (NumElements == 1) { |
| 1850 | V = IRB.CreateExtractElement(V, IRB.getInt32(BeginIndex), |
| 1851 | Name + ".extract"); |
| 1852 | DEBUG(dbgs() << " extract: " << *V << "\n"); |
| 1853 | return V; |
| 1854 | } |
| 1855 | |
| 1856 | SmallVector<Constant*, 8> Mask; |
| 1857 | Mask.reserve(NumElements); |
| 1858 | for (unsigned i = BeginIndex; i != EndIndex; ++i) |
| 1859 | Mask.push_back(IRB.getInt32(i)); |
| 1860 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
| 1861 | ConstantVector::get(Mask), |
| 1862 | Name + ".extract"); |
| 1863 | DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
| 1864 | return V; |
| 1865 | } |
| 1866 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1867 | static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V, |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 1868 | unsigned BeginIndex, const Twine &Name) { |
| 1869 | VectorType *VecTy = cast<VectorType>(Old->getType()); |
| 1870 | assert(VecTy && "Can only insert a vector into a vector"); |
| 1871 | |
| 1872 | VectorType *Ty = dyn_cast<VectorType>(V->getType()); |
| 1873 | if (!Ty) { |
| 1874 | // Single element to insert. |
| 1875 | V = IRB.CreateInsertElement(Old, V, IRB.getInt32(BeginIndex), |
| 1876 | Name + ".insert"); |
| 1877 | DEBUG(dbgs() << " insert: " << *V << "\n"); |
| 1878 | return V; |
| 1879 | } |
| 1880 | |
| 1881 | assert(Ty->getNumElements() <= VecTy->getNumElements() && |
| 1882 | "Too many elements!"); |
| 1883 | if (Ty->getNumElements() == VecTy->getNumElements()) { |
| 1884 | assert(V->getType() == VecTy && "Vector type mismatch"); |
| 1885 | return V; |
| 1886 | } |
| 1887 | unsigned EndIndex = BeginIndex + Ty->getNumElements(); |
| 1888 | |
| 1889 | // When inserting a smaller vector into the larger to store, we first |
| 1890 | // use a shuffle vector to widen it with undef elements, and then |
| 1891 | // a second shuffle vector to select between the loaded vector and the |
| 1892 | // incoming vector. |
| 1893 | SmallVector<Constant*, 8> Mask; |
| 1894 | Mask.reserve(VecTy->getNumElements()); |
| 1895 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
| 1896 | if (i >= BeginIndex && i < EndIndex) |
| 1897 | Mask.push_back(IRB.getInt32(i - BeginIndex)); |
| 1898 | else |
| 1899 | Mask.push_back(UndefValue::get(IRB.getInt32Ty())); |
| 1900 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
| 1901 | ConstantVector::get(Mask), |
| 1902 | Name + ".expand"); |
Nadav Rotem | 1e21191 | 2013-05-01 19:53:30 +0000 | [diff] [blame] | 1903 | DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 1904 | |
| 1905 | Mask.clear(); |
| 1906 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
Nadav Rotem | 1e21191 | 2013-05-01 19:53:30 +0000 | [diff] [blame] | 1907 | Mask.push_back(IRB.getInt1(i >= BeginIndex && i < EndIndex)); |
| 1908 | |
| 1909 | V = IRB.CreateSelect(ConstantVector::get(Mask), V, Old, Name + "blend"); |
| 1910 | |
| 1911 | DEBUG(dbgs() << " blend: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 1912 | return V; |
| 1913 | } |
| 1914 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1915 | namespace { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1916 | /// \brief Visitor to rewrite instructions using p particular slice of an alloca |
| 1917 | /// to use a new alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1918 | /// |
| 1919 | /// Also implements the rewriting to vector-based accesses when the partition |
| 1920 | /// passes the isVectorPromotionViable predicate. Most of the rewriting logic |
| 1921 | /// lives here. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1922 | class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1923 | // Befriend the base class so it can delegate to private visit methods. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1924 | friend class llvm::InstVisitor<AllocaSliceRewriter, bool>; |
| 1925 | typedef llvm::InstVisitor<AllocaSliceRewriter, bool> Base; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1926 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1927 | const DataLayout &DL; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1928 | AllocaSlices &S; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1929 | SROA &Pass; |
| 1930 | AllocaInst &OldAI, &NewAI; |
| 1931 | const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset; |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 1932 | Type *NewAllocaTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1933 | |
| 1934 | // If we are rewriting an alloca partition which can be written as pure |
| 1935 | // vector operations, we stash extra information here. When VecTy is |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 1936 | // non-null, we have some strict guarantees about the rewritten alloca: |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1937 | // - The new alloca is exactly the size of the vector type here. |
| 1938 | // - The accesses all either map to the entire vector or to a single |
| 1939 | // element. |
| 1940 | // - The set of accessing instructions is only one of those handled above |
| 1941 | // in isVectorPromotionViable. Generally these are the same access kinds |
| 1942 | // which are promotable via mem2reg. |
| 1943 | VectorType *VecTy; |
| 1944 | Type *ElementTy; |
| 1945 | uint64_t ElementSize; |
| 1946 | |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1947 | // This is a convenience and flag variable that will be null unless the new |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1948 | // alloca's integer operations should be widened to this integer type due to |
| 1949 | // passing isIntegerWideningViable above. If it is non-null, the desired |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1950 | // integer type will be stored here for easy access during rewriting. |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 1951 | IntegerType *IntTy; |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 1952 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1953 | // The offset of the slice currently being rewritten. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1954 | uint64_t BeginOffset, EndOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1955 | bool IsSplittable; |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 1956 | bool IsSplit; |
Chandler Carruth | 54e8f0b | 2012-10-01 01:49:22 +0000 | [diff] [blame] | 1957 | Use *OldUse; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1958 | Instruction *OldPtr; |
| 1959 | |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 1960 | // Output members carrying state about the result of visiting and rewriting |
| 1961 | // the slice of the alloca. |
| 1962 | bool IsUsedByRewrittenSpeculatableInstructions; |
| 1963 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 1964 | // Utility IR builder, whose name prefix is setup for each visited use, and |
| 1965 | // the insertion point is set to point to the user. |
| 1966 | IRBuilderTy IRB; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1967 | |
| 1968 | public: |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1969 | AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &S, SROA &Pass, |
| 1970 | AllocaInst &OldAI, AllocaInst &NewAI, |
| 1971 | uint64_t NewBeginOffset, uint64_t NewEndOffset, |
| 1972 | bool IsVectorPromotable = false, |
| 1973 | bool IsIntegerPromotable = false) |
| 1974 | : DL(DL), S(S), Pass(Pass), OldAI(OldAI), NewAI(NewAI), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1975 | NewAllocaBeginOffset(NewBeginOffset), NewAllocaEndOffset(NewEndOffset), |
| 1976 | NewAllocaTy(NewAI.getAllocatedType()), |
| 1977 | VecTy(IsVectorPromotable ? cast<VectorType>(NewAllocaTy) : 0), |
| 1978 | ElementTy(VecTy ? VecTy->getElementType() : 0), |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1979 | ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy) / 8 : 0), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1980 | IntTy(IsIntegerPromotable |
| 1981 | ? Type::getIntNTy( |
| 1982 | NewAI.getContext(), |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1983 | DL.getTypeSizeInBits(NewAI.getAllocatedType())) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1984 | : 0), |
| 1985 | BeginOffset(), EndOffset(), IsSplittable(), IsSplit(), OldUse(), |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 1986 | OldPtr(), IsUsedByRewrittenSpeculatableInstructions(false), |
| 1987 | IRB(NewAI.getContext(), ConstantFolder()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1988 | if (VecTy) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1989 | assert((DL.getTypeSizeInBits(ElementTy) % 8) == 0 && |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1990 | "Only multiple-of-8 sized vector elements are viable"); |
| 1991 | ++NumVectorized; |
| 1992 | } |
| 1993 | assert((!IsVectorPromotable && !IsIntegerPromotable) || |
| 1994 | IsVectorPromotable != IsIntegerPromotable); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1997 | bool visit(AllocaSlices::const_iterator I) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1998 | bool CanSROA = true; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1999 | BeginOffset = I->beginOffset(); |
| 2000 | EndOffset = I->endOffset(); |
| 2001 | IsSplittable = I->isSplittable(); |
| 2002 | IsSplit = |
| 2003 | BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset; |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2004 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2005 | OldUse = I->getUse(); |
| 2006 | OldPtr = cast<Instruction>(OldUse->get()); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2007 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2008 | Instruction *OldUserI = cast<Instruction>(OldUse->getUser()); |
| 2009 | IRB.SetInsertPoint(OldUserI); |
| 2010 | IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc()); |
| 2011 | IRB.SetNamePrefix(Twine(NewAI.getName()) + "." + Twine(BeginOffset) + "."); |
| 2012 | |
| 2013 | CanSROA &= visit(cast<Instruction>(OldUse->getUser())); |
| 2014 | if (VecTy || IntTy) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2015 | assert(CanSROA); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2016 | return CanSROA; |
| 2017 | } |
| 2018 | |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2019 | /// \brief Query whether this slice is used by speculatable instructions after |
| 2020 | /// rewriting. |
| 2021 | /// |
| 2022 | /// These instructions (PHIs and Selects currently) require the alloca slice |
| 2023 | /// to run back through the rewriter. Thus, they are promotable, but not on |
| 2024 | /// this iteration. This is distinct from a slice which is unpromotable for |
| 2025 | /// some other reason, in which case we don't even want to perform the |
| 2026 | /// speculation. This can be querried at any time and reflects whether (at |
| 2027 | /// that point) a visit call has rewritten a speculatable instruction on the |
| 2028 | /// current slice. |
| 2029 | bool isUsedByRewrittenSpeculatableInstructions() const { |
| 2030 | return IsUsedByRewrittenSpeculatableInstructions; |
| 2031 | } |
| 2032 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2033 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2034 | // Make sure the other visit overloads are visible. |
| 2035 | using Base::visit; |
| 2036 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2037 | // Every instruction which can end up as a user must have a rewrite rule. |
| 2038 | bool visitInstruction(Instruction &I) { |
| 2039 | DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n"); |
| 2040 | llvm_unreachable("No rewrite rule for this instruction!"); |
| 2041 | } |
| 2042 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2043 | Value *getAdjustedAllocaPtr(IRBuilderTy &IRB, uint64_t Offset, |
| 2044 | Type *PointerTy) { |
| 2045 | assert(Offset >= NewAllocaBeginOffset); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2046 | return getAdjustedPtr(IRB, DL, &NewAI, APInt(DL.getPointerSizeInBits(), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2047 | Offset - NewAllocaBeginOffset), |
| 2048 | PointerTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2051 | /// \brief Compute suitable alignment to access an offset into the new alloca. |
| 2052 | unsigned getOffsetAlign(uint64_t Offset) { |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2053 | unsigned NewAIAlign = NewAI.getAlignment(); |
| 2054 | if (!NewAIAlign) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2055 | NewAIAlign = DL.getABITypeAlignment(NewAI.getAllocatedType()); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2056 | return MinAlign(NewAIAlign, Offset); |
| 2057 | } |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2058 | |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2059 | /// \brief Compute suitable alignment to access a type at an offset of the |
| 2060 | /// new alloca. |
| 2061 | /// |
| 2062 | /// \returns zero if the type's ABI alignment is a suitable alignment, |
| 2063 | /// otherwise returns the maximal suitable alignment. |
| 2064 | unsigned getOffsetTypeAlign(Type *Ty, uint64_t Offset) { |
| 2065 | unsigned Align = getOffsetAlign(Offset); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2066 | return Align == DL.getABITypeAlignment(Ty) ? 0 : Align; |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2069 | unsigned getIndex(uint64_t Offset) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2070 | assert(VecTy && "Can only call getIndex when rewriting a vector"); |
| 2071 | uint64_t RelOffset = Offset - NewAllocaBeginOffset; |
| 2072 | assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds"); |
| 2073 | uint32_t Index = RelOffset / ElementSize; |
| 2074 | assert(Index * ElementSize == RelOffset); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2075 | return Index; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | void deleteIfTriviallyDead(Value *V) { |
| 2079 | Instruction *I = cast<Instruction>(V); |
| 2080 | if (isInstructionTriviallyDead(I)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2081 | Pass.DeadInsts.insert(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2084 | Value *rewriteVectorizedLoadInst(uint64_t NewBeginOffset, |
| 2085 | uint64_t NewEndOffset) { |
| 2086 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2087 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2088 | assert(EndIndex > BeginIndex && "Empty vector!"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2089 | |
| 2090 | Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2091 | "load"); |
| 2092 | return extractVector(IRB, V, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2095 | Value *rewriteIntegerLoad(LoadInst &LI, uint64_t NewBeginOffset, |
| 2096 | uint64_t NewEndOffset) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2097 | assert(IntTy && "We cannot insert an integer to the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2098 | assert(!LI.isVolatile()); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2099 | Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2100 | "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2101 | V = convertValue(DL, IRB, V, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2102 | assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2103 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
| 2104 | if (Offset > 0 || NewEndOffset < NewAllocaEndOffset) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2105 | V = extractInteger(DL, IRB, V, cast<IntegerType>(LI.getType()), Offset, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2106 | "extract"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2107 | return V; |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2110 | bool visitLoadInst(LoadInst &LI) { |
| 2111 | DEBUG(dbgs() << " original: " << LI << "\n"); |
| 2112 | Value *OldOp = LI.getOperand(0); |
| 2113 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2114 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2115 | // Compute the intersecting offset range. |
| 2116 | assert(BeginOffset < NewAllocaEndOffset); |
| 2117 | assert(EndOffset > NewAllocaBeginOffset); |
| 2118 | uint64_t NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2119 | uint64_t NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2120 | |
| 2121 | uint64_t Size = NewEndOffset - NewBeginOffset; |
Chandler Carruth | 3e994a2 | 2012-11-20 10:02:19 +0000 | [diff] [blame] | 2122 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2123 | Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), Size * 8) |
| 2124 | : LI.getType(); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2125 | bool IsPtrAdjusted = false; |
| 2126 | Value *V; |
| 2127 | if (VecTy) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2128 | V = rewriteVectorizedLoadInst(NewBeginOffset, NewEndOffset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2129 | } else if (IntTy && LI.getType()->isIntegerTy()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2130 | V = rewriteIntegerLoad(LI, NewBeginOffset, NewEndOffset); |
| 2131 | } else if (NewBeginOffset == NewAllocaBeginOffset && |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2132 | canConvertValue(DL, NewAllocaTy, LI.getType())) { |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2133 | V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2134 | LI.isVolatile(), "load"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2135 | } else { |
| 2136 | Type *LTy = TargetTy->getPointerTo(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2137 | V = IRB.CreateAlignedLoad( |
| 2138 | getAdjustedAllocaPtr(IRB, NewBeginOffset, LTy), |
| 2139 | getOffsetTypeAlign(TargetTy, NewBeginOffset - NewAllocaBeginOffset), |
| 2140 | LI.isVolatile(), "load"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2141 | IsPtrAdjusted = true; |
| 2142 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2143 | V = convertValue(DL, IRB, V, TargetTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2144 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2145 | if (IsSplit) { |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2146 | assert(!LI.isVolatile()); |
| 2147 | assert(LI.getType()->isIntegerTy() && |
| 2148 | "Only integer type loads and stores are split"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2149 | assert(Size < DL.getTypeStoreSize(LI.getType()) && |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2150 | "Split load isn't smaller than original load"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2151 | assert(LI.getType()->getIntegerBitWidth() == |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2152 | DL.getTypeStoreSizeInBits(LI.getType()) && |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2153 | "Non-byte-multiple bit width"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2154 | // Move the insertion point just past the load so that we can refer to it. |
| 2155 | IRB.SetInsertPoint(llvm::next(BasicBlock::iterator(&LI))); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2156 | // Create a placeholder value with the same type as LI to use as the |
| 2157 | // basis for the new value. This allows us to replace the uses of LI with |
| 2158 | // the computed value, and then replace the placeholder with LI, leaving |
| 2159 | // LI only used for this computation. |
| 2160 | Value *Placeholder |
Jakub Staszak | 4e45abf | 2012-11-01 01:10:43 +0000 | [diff] [blame] | 2161 | = new LoadInst(UndefValue::get(LI.getType()->getPointerTo())); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2162 | V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2163 | "insert"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2164 | LI.replaceAllUsesWith(V); |
| 2165 | Placeholder->replaceAllUsesWith(&LI); |
Jakub Staszak | 4e45abf | 2012-11-01 01:10:43 +0000 | [diff] [blame] | 2166 | delete Placeholder; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2167 | } else { |
| 2168 | LI.replaceAllUsesWith(V); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2171 | Pass.DeadInsts.insert(&LI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2172 | deleteIfTriviallyDead(OldOp); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2173 | DEBUG(dbgs() << " to: " << *V << "\n"); |
| 2174 | return !LI.isVolatile() && !IsPtrAdjusted; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2177 | bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp, |
| 2178 | uint64_t NewBeginOffset, |
| 2179 | uint64_t NewEndOffset) { |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2180 | if (V->getType() != VecTy) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2181 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2182 | unsigned EndIndex = getIndex(NewEndOffset); |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2183 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2184 | unsigned NumElements = EndIndex - BeginIndex; |
| 2185 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2186 | Type *SliceTy = |
| 2187 | (NumElements == 1) ? ElementTy |
| 2188 | : VectorType::get(ElementTy, NumElements); |
| 2189 | if (V->getType() != SliceTy) |
| 2190 | V = convertValue(DL, IRB, V, SliceTy); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2191 | |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2192 | // Mix in the existing elements. |
| 2193 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
| 2194 | "load"); |
| 2195 | V = insertVector(IRB, Old, V, BeginIndex, "vec"); |
| 2196 | } |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2197 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2198 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2199 | |
| 2200 | (void)Store; |
| 2201 | DEBUG(dbgs() << " to: " << *Store << "\n"); |
| 2202 | return true; |
| 2203 | } |
| 2204 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2205 | bool rewriteIntegerStore(Value *V, StoreInst &SI, |
| 2206 | uint64_t NewBeginOffset, uint64_t NewEndOffset) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2207 | assert(IntTy && "We cannot extract an integer from the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2208 | assert(!SI.isVolatile()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2209 | if (DL.getTypeSizeInBits(V->getType()) != IntTy->getBitWidth()) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2210 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2211 | "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2212 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2213 | assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2214 | uint64_t Offset = BeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2215 | V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2216 | "insert"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2217 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2218 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2219 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2220 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2221 | (void)Store; |
| 2222 | DEBUG(dbgs() << " to: " << *Store << "\n"); |
| 2223 | return true; |
| 2224 | } |
| 2225 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2226 | bool visitStoreInst(StoreInst &SI) { |
| 2227 | DEBUG(dbgs() << " original: " << SI << "\n"); |
| 2228 | Value *OldOp = SI.getOperand(1); |
| 2229 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2230 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2231 | Value *V = SI.getValueOperand(); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2232 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2233 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2234 | // alloca that should be re-examined after promoting this alloca. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2235 | if (V->getType()->isPointerTy()) |
| 2236 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets())) |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2237 | Pass.PostPromotionWorklist.insert(AI); |
| 2238 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2239 | // Compute the intersecting offset range. |
| 2240 | assert(BeginOffset < NewAllocaEndOffset); |
| 2241 | assert(EndOffset > NewAllocaBeginOffset); |
| 2242 | uint64_t NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2243 | uint64_t NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2244 | |
| 2245 | uint64_t Size = NewEndOffset - NewBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2246 | if (Size < DL.getTypeStoreSize(V->getType())) { |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2247 | assert(!SI.isVolatile()); |
| 2248 | assert(V->getType()->isIntegerTy() && |
| 2249 | "Only integer type loads and stores are split"); |
| 2250 | assert(V->getType()->getIntegerBitWidth() == |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2251 | DL.getTypeStoreSizeInBits(V->getType()) && |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2252 | "Non-byte-multiple bit width"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2253 | IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), Size * 8); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2254 | V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset, |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2255 | "extract"); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2258 | if (VecTy) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2259 | return rewriteVectorizedStoreInst(V, SI, OldOp, NewBeginOffset, |
| 2260 | NewEndOffset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2261 | if (IntTy && V->getType()->isIntegerTy()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2262 | return rewriteIntegerStore(V, SI, NewBeginOffset, NewEndOffset); |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2263 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2264 | StoreInst *NewSI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2265 | if (NewBeginOffset == NewAllocaBeginOffset && |
| 2266 | NewEndOffset == NewAllocaEndOffset && |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2267 | canConvertValue(DL, V->getType(), NewAllocaTy)) { |
| 2268 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2269 | NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
| 2270 | SI.isVolatile()); |
| 2271 | } else { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2272 | Value *NewPtr = getAdjustedAllocaPtr(IRB, NewBeginOffset, |
| 2273 | V->getType()->getPointerTo()); |
| 2274 | NewSI = IRB.CreateAlignedStore( |
| 2275 | V, NewPtr, getOffsetTypeAlign( |
| 2276 | V->getType(), NewBeginOffset - NewAllocaBeginOffset), |
| 2277 | SI.isVolatile()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2278 | } |
| 2279 | (void)NewSI; |
| 2280 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2281 | deleteIfTriviallyDead(OldOp); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2282 | |
| 2283 | DEBUG(dbgs() << " to: " << *NewSI << "\n"); |
| 2284 | return NewSI->getPointerOperand() == &NewAI && !SI.isVolatile(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2287 | /// \brief Compute an integer value from splatting an i8 across the given |
| 2288 | /// number of bytes. |
| 2289 | /// |
| 2290 | /// Note that this routine assumes an i8 is a byte. If that isn't true, don't |
| 2291 | /// call this routine. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 2292 | /// FIXME: Heed the advice above. |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2293 | /// |
| 2294 | /// \param V The i8 value to splat. |
| 2295 | /// \param Size The number of bytes in the output (assuming i8 is one byte) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2296 | Value *getIntegerSplat(Value *V, unsigned Size) { |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2297 | assert(Size > 0 && "Expected a positive number of bytes."); |
| 2298 | IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 2299 | assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte"); |
| 2300 | if (Size == 1) |
| 2301 | return V; |
| 2302 | |
| 2303 | Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size*8); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2304 | V = IRB.CreateMul(IRB.CreateZExt(V, SplatIntTy, "zext"), |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2305 | ConstantExpr::getUDiv( |
| 2306 | Constant::getAllOnesValue(SplatIntTy), |
| 2307 | ConstantExpr::getZExt( |
| 2308 | Constant::getAllOnesValue(V->getType()), |
| 2309 | SplatIntTy)), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2310 | "isplat"); |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2311 | return V; |
| 2312 | } |
| 2313 | |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2314 | /// \brief Compute a vector splat for a given element value. |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2315 | Value *getVectorSplat(Value *V, unsigned NumElements) { |
| 2316 | V = IRB.CreateVectorSplat(NumElements, V, "vsplat"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2317 | DEBUG(dbgs() << " splat: " << *V << "\n"); |
| 2318 | return V; |
| 2319 | } |
| 2320 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2321 | bool visitMemSetInst(MemSetInst &II) { |
| 2322 | DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2323 | assert(II.getRawDest() == OldPtr); |
| 2324 | |
| 2325 | // If the memset has a variable size, it cannot be split, just adjust the |
| 2326 | // pointer to the new alloca. |
| 2327 | if (!isa<Constant>(II.getLength())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2328 | assert(!IsSplit); |
| 2329 | assert(BeginOffset >= NewAllocaBeginOffset); |
| 2330 | II.setDest( |
| 2331 | getAdjustedAllocaPtr(IRB, BeginOffset, II.getRawDest()->getType())); |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2332 | Type *CstTy = II.getAlignmentCst()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2333 | II.setAlignment(ConstantInt::get(CstTy, getOffsetAlign(BeginOffset))); |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2334 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2335 | deleteIfTriviallyDead(OldPtr); |
| 2336 | return false; |
| 2337 | } |
| 2338 | |
| 2339 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2340 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2341 | |
| 2342 | Type *AllocaTy = NewAI.getAllocatedType(); |
| 2343 | Type *ScalarTy = AllocaTy->getScalarType(); |
| 2344 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2345 | // Compute the intersecting offset range. |
| 2346 | assert(BeginOffset < NewAllocaEndOffset); |
| 2347 | assert(EndOffset > NewAllocaBeginOffset); |
| 2348 | uint64_t NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2349 | uint64_t NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2350 | uint64_t SliceOffset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2351 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2352 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2353 | // a single value type, just emit a memset. |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2354 | if (!VecTy && !IntTy && |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2355 | (BeginOffset > NewAllocaBeginOffset || |
| 2356 | EndOffset < NewAllocaEndOffset || |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2357 | !AllocaTy->isSingleValueType() || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2358 | !DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy)) || |
| 2359 | DL.getTypeSizeInBits(ScalarTy)%8 != 0)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2360 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2361 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
| 2362 | CallInst *New = IRB.CreateMemSet( |
| 2363 | getAdjustedAllocaPtr(IRB, NewBeginOffset, II.getRawDest()->getType()), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2364 | II.getValue(), Size, getOffsetAlign(SliceOffset), II.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2365 | (void)New; |
| 2366 | DEBUG(dbgs() << " to: " << *New << "\n"); |
| 2367 | return false; |
| 2368 | } |
| 2369 | |
| 2370 | // If we can represent this as a simple value, we have to build the actual |
| 2371 | // value to store, which requires expanding the byte present in memset to |
| 2372 | // a sensible representation for the alloca type. This is essentially |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2373 | // splatting the byte to a sufficiently wide integer, splatting it across |
| 2374 | // any desired vector width, and bitcasting to the final type. |
Benjamin Kramer | c003a45 | 2013-01-01 16:13:35 +0000 | [diff] [blame] | 2375 | Value *V; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2376 | |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2377 | if (VecTy) { |
| 2378 | // If this is a memset of a vectorized alloca, insert it. |
| 2379 | assert(ElementTy == ScalarTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2380 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2381 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2382 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2383 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2384 | unsigned NumElements = EndIndex - BeginIndex; |
| 2385 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 2386 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2387 | Value *Splat = |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2388 | getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ElementTy) / 8); |
| 2389 | Splat = convertValue(DL, IRB, Splat, ElementTy); |
Chandler Carruth | cacda25 | 2012-12-17 14:03:01 +0000 | [diff] [blame] | 2390 | if (NumElements > 1) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2391 | Splat = getVectorSplat(Splat, NumElements); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2392 | |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2393 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2394 | "oldload"); |
| 2395 | V = insertVector(IRB, Old, Splat, BeginIndex, "vec"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2396 | } else if (IntTy) { |
| 2397 | // If this is a memset on an alloca where we can widen stores, insert the |
| 2398 | // set integer. |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2399 | assert(!II.isVolatile()); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2400 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2401 | uint64_t Size = NewEndOffset - NewBeginOffset; |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2402 | V = getIntegerSplat(II.getValue(), Size); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2403 | |
| 2404 | if (IntTy && (BeginOffset != NewAllocaBeginOffset || |
| 2405 | EndOffset != NewAllocaBeginOffset)) { |
| 2406 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2407 | "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2408 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2409 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2410 | V = insertInteger(DL, IRB, Old, V, Offset, "insert"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2411 | } else { |
| 2412 | assert(V->getType() == IntTy && |
| 2413 | "Wrong type for an alloca wide integer!"); |
| 2414 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2415 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2416 | } else { |
| 2417 | // Established these invariants above. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2418 | assert(NewBeginOffset == NewAllocaBeginOffset); |
| 2419 | assert(NewEndOffset == NewAllocaEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2420 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2421 | V = getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ScalarTy) / 8); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2422 | if (VectorType *AllocaVecTy = dyn_cast<VectorType>(AllocaTy)) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2423 | V = getVectorSplat(V, AllocaVecTy->getNumElements()); |
Chandler Carruth | 95e1fb8 | 2012-12-17 13:51:03 +0000 | [diff] [blame] | 2424 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2425 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Chandler Carruth | 95e1fb8 | 2012-12-17 13:51:03 +0000 | [diff] [blame] | 2428 | Value *New = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2429 | II.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2430 | (void)New; |
| 2431 | DEBUG(dbgs() << " to: " << *New << "\n"); |
| 2432 | return !II.isVolatile(); |
| 2433 | } |
| 2434 | |
| 2435 | bool visitMemTransferInst(MemTransferInst &II) { |
| 2436 | // Rewriting of memory transfer instructions can be a bit tricky. We break |
| 2437 | // them into two categories: split intrinsics and unsplit intrinsics. |
| 2438 | |
| 2439 | DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2440 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2441 | // Compute the intersecting offset range. |
| 2442 | assert(BeginOffset < NewAllocaEndOffset); |
| 2443 | assert(EndOffset > NewAllocaBeginOffset); |
| 2444 | uint64_t NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2445 | uint64_t NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2446 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2447 | assert(II.getRawSource() == OldPtr || II.getRawDest() == OldPtr); |
| 2448 | bool IsDest = II.getRawDest() == OldPtr; |
| 2449 | |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2450 | // Compute the relative offset within the transfer. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2451 | unsigned IntPtrWidth = DL.getPointerSizeInBits(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2452 | APInt RelOffset(IntPtrWidth, NewBeginOffset - BeginOffset); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2453 | |
| 2454 | unsigned Align = II.getAlignment(); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2455 | uint64_t SliceOffset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2456 | if (Align > 1) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2457 | Align = |
| 2458 | MinAlign(RelOffset.zextOrTrunc(64).getZExtValue(), |
| 2459 | MinAlign(II.getAlignment(), getOffsetAlign(SliceOffset))); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2460 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2461 | // For unsplit intrinsics, we simply modify the source and destination |
| 2462 | // pointers in place. This isn't just an optimization, it is a matter of |
| 2463 | // correctness. With unsplit intrinsics we may be dealing with transfers |
| 2464 | // within a single alloca before SROA ran, or with transfers that have |
| 2465 | // a variable length. We may also be dealing with memmove instead of |
| 2466 | // memcpy, and so simply updating the pointers is the necessary for us to |
| 2467 | // update both source and dest of a single call. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2468 | if (!IsSplittable) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2469 | Value *OldOp = IsDest ? II.getRawDest() : II.getRawSource(); |
| 2470 | if (IsDest) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2471 | II.setDest( |
| 2472 | getAdjustedAllocaPtr(IRB, BeginOffset, II.getRawDest()->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2473 | else |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2474 | II.setSource(getAdjustedAllocaPtr(IRB, BeginOffset, |
| 2475 | II.getRawSource()->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2476 | |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2477 | Type *CstTy = II.getAlignmentCst()->getType(); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2478 | II.setAlignment(ConstantInt::get(CstTy, Align)); |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2479 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2480 | DEBUG(dbgs() << " to: " << II << "\n"); |
| 2481 | deleteIfTriviallyDead(OldOp); |
| 2482 | return false; |
| 2483 | } |
| 2484 | // For split transfer intrinsics we have an incredibly useful assurance: |
| 2485 | // the source and destination do not reside within the same alloca, and at |
| 2486 | // least one of them does not escape. This means that we can replace |
| 2487 | // memmove with memcpy, and we don't need to worry about all manner of |
| 2488 | // downsides to splitting and transforming the operations. |
| 2489 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2490 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2491 | // a single value type, just emit a memcpy. |
| 2492 | bool EmitMemCpy |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2493 | = !VecTy && !IntTy && (BeginOffset > NewAllocaBeginOffset || |
| 2494 | EndOffset < NewAllocaEndOffset || |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 2495 | !NewAI.getAllocatedType()->isSingleValueType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2496 | |
| 2497 | // If we're just going to emit a memcpy, the alloca hasn't changed, and the |
| 2498 | // size hasn't been shrunk based on analysis of the viable range, this is |
| 2499 | // a no-op. |
| 2500 | if (EmitMemCpy && &OldAI == &NewAI) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2501 | // Ensure the start lines up. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2502 | assert(NewBeginOffset == BeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2503 | |
| 2504 | // Rewrite the size as needed. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2505 | if (NewEndOffset != EndOffset) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2506 | II.setLength(ConstantInt::get(II.getLength()->getType(), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2507 | NewEndOffset - NewBeginOffset)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2508 | return false; |
| 2509 | } |
| 2510 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2511 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2512 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2513 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2514 | // alloca that should be re-examined after rewriting this instruction. |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2515 | Value *OtherPtr = IsDest ? II.getRawSource() : II.getRawDest(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2516 | if (AllocaInst *AI |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2517 | = dyn_cast<AllocaInst>(OtherPtr->stripInBoundsOffsets())) { |
| 2518 | assert(AI != &OldAI && AI != &NewAI && |
| 2519 | "Splittable transfers cannot reach the same alloca on both ends."); |
Chandler Carruth | 4bd8f66 | 2012-09-26 07:41:40 +0000 | [diff] [blame] | 2520 | Pass.Worklist.insert(AI); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2521 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2522 | |
| 2523 | if (EmitMemCpy) { |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2524 | Type *OtherPtrTy = IsDest ? II.getRawSource()->getType() |
| 2525 | : II.getRawDest()->getType(); |
| 2526 | |
| 2527 | // Compute the other pointer, folding as much as possible to produce |
| 2528 | // a single, simple GEP in most cases. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2529 | OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, RelOffset, OtherPtrTy); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2530 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2531 | Value *OurPtr = getAdjustedAllocaPtr( |
| 2532 | IRB, NewBeginOffset, |
| 2533 | IsDest ? II.getRawDest()->getType() : II.getRawSource()->getType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2534 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2535 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2536 | |
| 2537 | CallInst *New = IRB.CreateMemCpy(IsDest ? OurPtr : OtherPtr, |
| 2538 | IsDest ? OtherPtr : OurPtr, |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2539 | Size, Align, II.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2540 | (void)New; |
| 2541 | DEBUG(dbgs() << " to: " << *New << "\n"); |
| 2542 | return false; |
| 2543 | } |
| 2544 | |
Chandler Carruth | 08e5f49 | 2012-10-03 08:26:28 +0000 | [diff] [blame] | 2545 | // Note that we clamp the alignment to 1 here as a 0 alignment for a memcpy |
| 2546 | // is equivalent to 1, but that isn't true if we end up rewriting this as |
| 2547 | // a load or store. |
| 2548 | if (!Align) |
| 2549 | Align = 1; |
| 2550 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2551 | bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset && |
| 2552 | NewEndOffset == NewAllocaEndOffset; |
| 2553 | uint64_t Size = NewEndOffset - NewBeginOffset; |
| 2554 | unsigned BeginIndex = VecTy ? getIndex(NewBeginOffset) : 0; |
| 2555 | unsigned EndIndex = VecTy ? getIndex(NewEndOffset) : 0; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2556 | unsigned NumElements = EndIndex - BeginIndex; |
| 2557 | IntegerType *SubIntTy |
| 2558 | = IntTy ? Type::getIntNTy(IntTy->getContext(), Size*8) : 0; |
| 2559 | |
| 2560 | Type *OtherPtrTy = NewAI.getType(); |
| 2561 | if (VecTy && !IsWholeAlloca) { |
| 2562 | if (NumElements == 1) |
| 2563 | OtherPtrTy = VecTy->getElementType(); |
| 2564 | else |
| 2565 | OtherPtrTy = VectorType::get(VecTy->getElementType(), NumElements); |
| 2566 | |
| 2567 | OtherPtrTy = OtherPtrTy->getPointerTo(); |
| 2568 | } else if (IntTy && !IsWholeAlloca) { |
| 2569 | OtherPtrTy = SubIntTy->getPointerTo(); |
| 2570 | } |
| 2571 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2572 | Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, RelOffset, OtherPtrTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2573 | Value *DstPtr = &NewAI; |
| 2574 | if (!IsDest) |
| 2575 | std::swap(SrcPtr, DstPtr); |
| 2576 | |
| 2577 | Value *Src; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2578 | if (VecTy && !IsWholeAlloca && !IsDest) { |
| 2579 | Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2580 | "load"); |
| 2581 | Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 2582 | } else if (IntTy && !IsWholeAlloca && !IsDest) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2583 | Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2584 | "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2585 | Src = convertValue(DL, IRB, Src, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2586 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2587 | Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2588 | } else { |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2589 | Src = IRB.CreateAlignedLoad(SrcPtr, Align, II.isVolatile(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2590 | "copyload"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2593 | if (VecTy && !IsWholeAlloca && IsDest) { |
| 2594 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2595 | "oldload"); |
| 2596 | Src = insertVector(IRB, Old, Src, BeginIndex, "vec"); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2597 | } else if (IntTy && !IsWholeAlloca && IsDest) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2598 | Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2599 | "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2600 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2601 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2602 | Src = insertInteger(DL, IRB, Old, Src, Offset, "insert"); |
| 2603 | Src = convertValue(DL, IRB, Src, NewAllocaTy); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2606 | StoreInst *Store = cast<StoreInst>( |
| 2607 | IRB.CreateAlignedStore(Src, DstPtr, Align, II.isVolatile())); |
| 2608 | (void)Store; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2609 | DEBUG(dbgs() << " to: " << *Store << "\n"); |
| 2610 | return !II.isVolatile(); |
| 2611 | } |
| 2612 | |
| 2613 | bool visitIntrinsicInst(IntrinsicInst &II) { |
| 2614 | assert(II.getIntrinsicID() == Intrinsic::lifetime_start || |
| 2615 | II.getIntrinsicID() == Intrinsic::lifetime_end); |
| 2616 | DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2617 | assert(II.getArgOperand(1) == OldPtr); |
| 2618 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2619 | // Compute the intersecting offset range. |
| 2620 | assert(BeginOffset < NewAllocaEndOffset); |
| 2621 | assert(EndOffset > NewAllocaBeginOffset); |
| 2622 | uint64_t NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2623 | uint64_t NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2624 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2625 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2626 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2627 | |
| 2628 | ConstantInt *Size |
| 2629 | = ConstantInt::get(cast<IntegerType>(II.getArgOperand(0)->getType()), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2630 | NewEndOffset - NewBeginOffset); |
| 2631 | Value *Ptr = |
| 2632 | getAdjustedAllocaPtr(IRB, NewBeginOffset, II.getArgOperand(1)->getType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2633 | Value *New; |
| 2634 | if (II.getIntrinsicID() == Intrinsic::lifetime_start) |
| 2635 | New = IRB.CreateLifetimeStart(Ptr, Size); |
| 2636 | else |
| 2637 | New = IRB.CreateLifetimeEnd(Ptr, Size); |
| 2638 | |
Edwin Vane | 82f80d4 | 2013-01-29 17:42:24 +0000 | [diff] [blame] | 2639 | (void)New; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2640 | DEBUG(dbgs() << " to: " << *New << "\n"); |
| 2641 | return true; |
| 2642 | } |
| 2643 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2644 | bool visitPHINode(PHINode &PN) { |
| 2645 | DEBUG(dbgs() << " original: " << PN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2646 | assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable"); |
| 2647 | assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 2648 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2649 | // We would like to compute a new pointer in only one place, but have it be |
| 2650 | // as local as possible to the PHI. To do that, we re-use the location of |
| 2651 | // the old pointer, which necessarily must be in the right position to |
| 2652 | // dominate the PHI. |
Jakub Staszak | cb132fa | 2013-07-22 22:10:43 +0000 | [diff] [blame] | 2653 | IRBuilderTy PtrBuilder(OldPtr); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2654 | PtrBuilder.SetNamePrefix(Twine(NewAI.getName()) + "." + Twine(BeginOffset) + |
| 2655 | "."); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2656 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2657 | Value *NewPtr = |
| 2658 | getAdjustedAllocaPtr(PtrBuilder, BeginOffset, OldPtr->getType()); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 2659 | // Replace the operands which were using the old pointer. |
Benjamin Kramer | 7ddd705 | 2012-10-20 12:04:57 +0000 | [diff] [blame] | 2660 | std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2661 | |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 2662 | DEBUG(dbgs() << " to: " << PN << "\n"); |
| 2663 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2664 | |
| 2665 | // Check whether we can speculate this PHI node, and if so remember that |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2666 | // fact and queue it up for another iteration after the speculation |
| 2667 | // occurs. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2668 | if (isSafePHIToSpeculate(PN, &DL)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2669 | Pass.SpeculatablePHIs.insert(&PN); |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2670 | IsUsedByRewrittenSpeculatableInstructions = true; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2671 | return true; |
| 2672 | } |
| 2673 | |
| 2674 | return false; // PHIs can't be promoted on their own. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | bool visitSelectInst(SelectInst &SI) { |
| 2678 | DEBUG(dbgs() << " original: " << SI << "\n"); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 2679 | assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && |
| 2680 | "Pointer isn't an operand!"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2681 | assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable"); |
| 2682 | assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 2683 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2684 | Value *NewPtr = getAdjustedAllocaPtr(IRB, BeginOffset, OldPtr->getType()); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 2685 | // Replace the operands which were using the old pointer. |
| 2686 | if (SI.getOperand(1) == OldPtr) |
| 2687 | SI.setOperand(1, NewPtr); |
| 2688 | if (SI.getOperand(2) == OldPtr) |
| 2689 | SI.setOperand(2, NewPtr); |
| 2690 | |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 2691 | DEBUG(dbgs() << " to: " << SI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2692 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2693 | |
| 2694 | // Check whether we can speculate this select instruction, and if so |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2695 | // remember that fact and queue it up for another iteration after the |
| 2696 | // speculation occurs. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2697 | if (isSafeSelectToSpeculate(SI, &DL)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2698 | Pass.SpeculatableSelects.insert(&SI); |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2699 | IsUsedByRewrittenSpeculatableInstructions = true; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2700 | return true; |
| 2701 | } |
| 2702 | |
| 2703 | return false; // Selects can't be promoted on their own. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | }; |
| 2707 | } |
| 2708 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2709 | namespace { |
| 2710 | /// \brief Visitor to rewrite aggregate loads and stores as scalar. |
| 2711 | /// |
| 2712 | /// This pass aggressively rewrites all aggregate loads and stores on |
| 2713 | /// a particular pointer (or any pointer derived from it which we can identify) |
| 2714 | /// with scalar loads and stores. |
| 2715 | class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> { |
| 2716 | // Befriend the base class so it can delegate to private visit methods. |
| 2717 | friend class llvm::InstVisitor<AggLoadStoreRewriter, bool>; |
| 2718 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2719 | const DataLayout &DL; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2720 | |
| 2721 | /// Queue of pointer uses to analyze and potentially rewrite. |
| 2722 | SmallVector<Use *, 8> Queue; |
| 2723 | |
| 2724 | /// Set to prevent us from cycling with phi nodes and loops. |
| 2725 | SmallPtrSet<User *, 8> Visited; |
| 2726 | |
| 2727 | /// The current pointer use being rewritten. This is used to dig up the used |
| 2728 | /// value (as opposed to the user). |
| 2729 | Use *U; |
| 2730 | |
| 2731 | public: |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2732 | AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {} |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2733 | |
| 2734 | /// Rewrite loads and stores through a pointer and all pointers derived from |
| 2735 | /// it. |
| 2736 | bool rewrite(Instruction &I) { |
| 2737 | DEBUG(dbgs() << " Rewriting FCA loads and stores...\n"); |
| 2738 | enqueueUsers(I); |
| 2739 | bool Changed = false; |
| 2740 | while (!Queue.empty()) { |
| 2741 | U = Queue.pop_back_val(); |
| 2742 | Changed |= visit(cast<Instruction>(U->getUser())); |
| 2743 | } |
| 2744 | return Changed; |
| 2745 | } |
| 2746 | |
| 2747 | private: |
| 2748 | /// Enqueue all the users of the given instruction for further processing. |
| 2749 | /// This uses a set to de-duplicate users. |
| 2750 | void enqueueUsers(Instruction &I) { |
| 2751 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); UI != UE; |
| 2752 | ++UI) |
| 2753 | if (Visited.insert(*UI)) |
| 2754 | Queue.push_back(&UI.getUse()); |
| 2755 | } |
| 2756 | |
| 2757 | // Conservative default is to not rewrite anything. |
| 2758 | bool visitInstruction(Instruction &I) { return false; } |
| 2759 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2760 | /// \brief Generic recursive split emission class. |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2761 | template <typename Derived> |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2762 | class OpSplitter { |
| 2763 | protected: |
| 2764 | /// The builder used to form new instructions. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2765 | IRBuilderTy IRB; |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2766 | /// The indices which to be used with insert- or extractvalue to select the |
| 2767 | /// appropriate value within the aggregate. |
| 2768 | SmallVector<unsigned, 4> Indices; |
| 2769 | /// The indices to a GEP instruction which will move Ptr to the correct slot |
| 2770 | /// within the aggregate. |
| 2771 | SmallVector<Value *, 4> GEPIndices; |
| 2772 | /// The base pointer of the original op, used as a base for GEPing the |
| 2773 | /// split operations. |
| 2774 | Value *Ptr; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2775 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2776 | /// Initialize the splitter with an insertion point, Ptr and start with a |
| 2777 | /// single zero GEP index. |
| 2778 | OpSplitter(Instruction *InsertionPoint, Value *Ptr) |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2779 | : IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr) {} |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2780 | |
| 2781 | public: |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2782 | /// \brief Generic recursive split emission routine. |
| 2783 | /// |
| 2784 | /// This method recursively splits an aggregate op (load or store) into |
| 2785 | /// scalar or vector ops. It splits recursively until it hits a single value |
| 2786 | /// and emits that single value operation via the template argument. |
| 2787 | /// |
| 2788 | /// The logic of this routine relies on GEPs and insertvalue and |
| 2789 | /// extractvalue all operating with the same fundamental index list, merely |
| 2790 | /// formatted differently (GEPs need actual values). |
| 2791 | /// |
| 2792 | /// \param Ty The type being split recursively into smaller ops. |
| 2793 | /// \param Agg The aggregate value being built up or stored, depending on |
| 2794 | /// whether this is splitting a load or a store respectively. |
| 2795 | void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) { |
| 2796 | if (Ty->isSingleValueType()) |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2797 | return static_cast<Derived *>(this)->emitFunc(Ty, Agg, Name); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2798 | |
| 2799 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 2800 | unsigned OldSize = Indices.size(); |
| 2801 | (void)OldSize; |
| 2802 | for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size; |
| 2803 | ++Idx) { |
| 2804 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 2805 | Indices.push_back(Idx); |
| 2806 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 2807 | emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx)); |
| 2808 | GEPIndices.pop_back(); |
| 2809 | Indices.pop_back(); |
| 2810 | } |
| 2811 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2812 | } |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2813 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2814 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 2815 | unsigned OldSize = Indices.size(); |
| 2816 | (void)OldSize; |
| 2817 | for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size; |
| 2818 | ++Idx) { |
| 2819 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 2820 | Indices.push_back(Idx); |
| 2821 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 2822 | emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx)); |
| 2823 | GEPIndices.pop_back(); |
| 2824 | Indices.pop_back(); |
| 2825 | } |
| 2826 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2827 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2828 | |
| 2829 | llvm_unreachable("Only arrays and structs are aggregate loadable types"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2830 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2831 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2832 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2833 | struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2834 | LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr) |
Benjamin Kramer | a59ef57 | 2012-09-18 17:11:47 +0000 | [diff] [blame] | 2835 | : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr) {} |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2836 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2837 | /// Emit a leaf load of a single value. This is called at the leaves of the |
| 2838 | /// recursive emission to actually load values. |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2839 | void emitFunc(Type *Ty, Value *&Agg, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2840 | assert(Ty->isSingleValueType()); |
| 2841 | // Load the single value and insert it using the indices. |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 2842 | Value *GEP = IRB.CreateInBoundsGEP(Ptr, GEPIndices, Name + ".gep"); |
| 2843 | Value *Load = IRB.CreateLoad(GEP, Name + ".load"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2844 | Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert"); |
| 2845 | DEBUG(dbgs() << " to: " << *Load << "\n"); |
| 2846 | } |
| 2847 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2848 | |
| 2849 | bool visitLoadInst(LoadInst &LI) { |
| 2850 | assert(LI.getPointerOperand() == *U); |
| 2851 | if (!LI.isSimple() || LI.getType()->isSingleValueType()) |
| 2852 | return false; |
| 2853 | |
| 2854 | // We have an aggregate being loaded, split it apart. |
| 2855 | DEBUG(dbgs() << " original: " << LI << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2856 | LoadOpSplitter Splitter(&LI, *U); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2857 | Value *V = UndefValue::get(LI.getType()); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2858 | Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2859 | LI.replaceAllUsesWith(V); |
| 2860 | LI.eraseFromParent(); |
| 2861 | return true; |
| 2862 | } |
| 2863 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2864 | struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2865 | StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr) |
Benjamin Kramer | a59ef57 | 2012-09-18 17:11:47 +0000 | [diff] [blame] | 2866 | : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr) {} |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2867 | |
| 2868 | /// Emit a leaf store of a single value. This is called at the leaves of the |
| 2869 | /// recursive emission to actually produce stores. |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 2870 | void emitFunc(Type *Ty, Value *&Agg, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2871 | assert(Ty->isSingleValueType()); |
| 2872 | // Extract the single value and store it using the indices. |
| 2873 | Value *Store = IRB.CreateStore( |
| 2874 | IRB.CreateExtractValue(Agg, Indices, Name + ".extract"), |
| 2875 | IRB.CreateInBoundsGEP(Ptr, GEPIndices, Name + ".gep")); |
| 2876 | (void)Store; |
| 2877 | DEBUG(dbgs() << " to: " << *Store << "\n"); |
| 2878 | } |
| 2879 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2880 | |
| 2881 | bool visitStoreInst(StoreInst &SI) { |
| 2882 | if (!SI.isSimple() || SI.getPointerOperand() != *U) |
| 2883 | return false; |
| 2884 | Value *V = SI.getValueOperand(); |
| 2885 | if (V->getType()->isSingleValueType()) |
| 2886 | return false; |
| 2887 | |
| 2888 | // We have an aggregate being stored, split it apart. |
| 2889 | DEBUG(dbgs() << " original: " << SI << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 2890 | StoreOpSplitter Splitter(&SI, *U); |
| 2891 | Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 2892 | SI.eraseFromParent(); |
| 2893 | return true; |
| 2894 | } |
| 2895 | |
| 2896 | bool visitBitCastInst(BitCastInst &BC) { |
| 2897 | enqueueUsers(BC); |
| 2898 | return false; |
| 2899 | } |
| 2900 | |
| 2901 | bool visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 2902 | enqueueUsers(GEPI); |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | bool visitPHINode(PHINode &PN) { |
| 2907 | enqueueUsers(PN); |
| 2908 | return false; |
| 2909 | } |
| 2910 | |
| 2911 | bool visitSelectInst(SelectInst &SI) { |
| 2912 | enqueueUsers(SI); |
| 2913 | return false; |
| 2914 | } |
| 2915 | }; |
| 2916 | } |
| 2917 | |
Chandler Carruth | ba93199 | 2012-10-13 10:49:33 +0000 | [diff] [blame] | 2918 | /// \brief Strip aggregate type wrapping. |
| 2919 | /// |
| 2920 | /// This removes no-op aggregate types wrapping an underlying type. It will |
| 2921 | /// strip as many layers of types as it can without changing either the type |
| 2922 | /// size or the allocated size. |
| 2923 | static Type *stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty) { |
| 2924 | if (Ty->isSingleValueType()) |
| 2925 | return Ty; |
| 2926 | |
| 2927 | uint64_t AllocSize = DL.getTypeAllocSize(Ty); |
| 2928 | uint64_t TypeSize = DL.getTypeSizeInBits(Ty); |
| 2929 | |
| 2930 | Type *InnerTy; |
| 2931 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 2932 | InnerTy = ArrTy->getElementType(); |
| 2933 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 2934 | const StructLayout *SL = DL.getStructLayout(STy); |
| 2935 | unsigned Index = SL->getElementContainingOffset(0); |
| 2936 | InnerTy = STy->getElementType(Index); |
| 2937 | } else { |
| 2938 | return Ty; |
| 2939 | } |
| 2940 | |
| 2941 | if (AllocSize > DL.getTypeAllocSize(InnerTy) || |
| 2942 | TypeSize > DL.getTypeSizeInBits(InnerTy)) |
| 2943 | return Ty; |
| 2944 | |
| 2945 | return stripAggregateTypeWrapping(DL, InnerTy); |
| 2946 | } |
| 2947 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2948 | /// \brief Try to find a partition of the aggregate type passed in for a given |
| 2949 | /// offset and size. |
| 2950 | /// |
| 2951 | /// This recurses through the aggregate type and tries to compute a subtype |
| 2952 | /// based on the offset and size. When the offset and size span a sub-section |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 2953 | /// of an array, it will even compute a new array type for that sub-section, |
| 2954 | /// and the same for structs. |
| 2955 | /// |
| 2956 | /// Note that this routine is very strict and tries to find a partition of the |
| 2957 | /// type which produces the *exact* right offset and size. It is not forgiving |
| 2958 | /// when the size or offset cause either end of type-based partition to be off. |
| 2959 | /// Also, this is a best-effort routine. It is reasonable to give up and not |
| 2960 | /// return a type if necessary. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2961 | static Type *getTypePartition(const DataLayout &DL, Type *Ty, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2962 | uint64_t Offset, uint64_t Size) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2963 | if (Offset == 0 && DL.getTypeAllocSize(Ty) == Size) |
| 2964 | return stripAggregateTypeWrapping(DL, Ty); |
| 2965 | if (Offset > DL.getTypeAllocSize(Ty) || |
| 2966 | (DL.getTypeAllocSize(Ty) - Offset) < Size) |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2967 | return 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2968 | |
| 2969 | if (SequentialType *SeqTy = dyn_cast<SequentialType>(Ty)) { |
| 2970 | // We can't partition pointers... |
| 2971 | if (SeqTy->isPointerTy()) |
| 2972 | return 0; |
| 2973 | |
| 2974 | Type *ElementTy = SeqTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2975 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2976 | uint64_t NumSkippedElements = Offset / ElementSize; |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 2977 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(SeqTy)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2978 | if (NumSkippedElements >= ArrTy->getNumElements()) |
| 2979 | return 0; |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 2980 | } else if (VectorType *VecTy = dyn_cast<VectorType>(SeqTy)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2981 | if (NumSkippedElements >= VecTy->getNumElements()) |
| 2982 | return 0; |
Jakub Staszak | 4f9d1e8 | 2013-03-24 09:56:28 +0000 | [diff] [blame] | 2983 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2984 | Offset -= NumSkippedElements * ElementSize; |
| 2985 | |
| 2986 | // First check if we need to recurse. |
| 2987 | if (Offset > 0 || Size < ElementSize) { |
| 2988 | // Bail if the partition ends in a different array element. |
| 2989 | if ((Offset + Size) > ElementSize) |
| 2990 | return 0; |
| 2991 | // Recurse through the element type trying to peel off offset bytes. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2992 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2993 | } |
| 2994 | assert(Offset == 0); |
| 2995 | |
| 2996 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2997 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2998 | assert(Size > ElementSize); |
| 2999 | uint64_t NumElements = Size / ElementSize; |
| 3000 | if (NumElements * ElementSize != Size) |
| 3001 | return 0; |
| 3002 | return ArrayType::get(ElementTy, NumElements); |
| 3003 | } |
| 3004 | |
| 3005 | StructType *STy = dyn_cast<StructType>(Ty); |
| 3006 | if (!STy) |
| 3007 | return 0; |
| 3008 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3009 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3010 | if (Offset >= SL->getSizeInBytes()) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3011 | return 0; |
| 3012 | uint64_t EndOffset = Offset + Size; |
| 3013 | if (EndOffset > SL->getSizeInBytes()) |
| 3014 | return 0; |
| 3015 | |
| 3016 | unsigned Index = SL->getElementContainingOffset(Offset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3017 | Offset -= SL->getElementOffset(Index); |
| 3018 | |
| 3019 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3020 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3021 | if (Offset >= ElementSize) |
| 3022 | return 0; // The offset points into alignment padding. |
| 3023 | |
| 3024 | // See if any partition must be contained by the element. |
| 3025 | if (Offset > 0 || Size < ElementSize) { |
| 3026 | if ((Offset + Size) > ElementSize) |
| 3027 | return 0; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3028 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3029 | } |
| 3030 | assert(Offset == 0); |
| 3031 | |
| 3032 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3033 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3034 | |
| 3035 | StructType::element_iterator EI = STy->element_begin() + Index, |
| 3036 | EE = STy->element_end(); |
| 3037 | if (EndOffset < SL->getSizeInBytes()) { |
| 3038 | unsigned EndIndex = SL->getElementContainingOffset(EndOffset); |
| 3039 | if (Index == EndIndex) |
| 3040 | return 0; // Within a single element and its padding. |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3041 | |
| 3042 | // Don't try to form "natural" types if the elements don't line up with the |
| 3043 | // expected size. |
| 3044 | // FIXME: We could potentially recurse down through the last element in the |
| 3045 | // sub-struct to find a natural end point. |
| 3046 | if (SL->getElementOffset(EndIndex) != EndOffset) |
| 3047 | return 0; |
| 3048 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3049 | assert(Index < EndIndex); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3050 | EE = STy->element_begin() + EndIndex; |
| 3051 | } |
| 3052 | |
| 3053 | // Try to build up a sub-structure. |
Benjamin Kramer | 7ddd705 | 2012-10-20 12:04:57 +0000 | [diff] [blame] | 3054 | StructType *SubTy = StructType::get(STy->getContext(), makeArrayRef(EI, EE), |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3055 | STy->isPacked()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3056 | const StructLayout *SubSL = DL.getStructLayout(SubTy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3057 | if (Size != SubSL->getSizeInBytes()) |
| 3058 | return 0; // The sub-struct doesn't have quite the size needed. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3059 | |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3060 | return SubTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
| 3063 | /// \brief Rewrite an alloca partition's users. |
| 3064 | /// |
| 3065 | /// This routine drives both of the rewriting goals of the SROA pass. It tries |
| 3066 | /// to rewrite uses of an alloca partition to be conducive for SSA value |
| 3067 | /// promotion. If the partition needs a new, more refined alloca, this will |
| 3068 | /// build that new alloca, preserving as much type information as possible, and |
| 3069 | /// rewrite the uses of the old alloca to point at the new one and have the |
| 3070 | /// appropriate new offsets. It also evaluates how successful the rewrite was |
| 3071 | /// at enabling promotion and if it was successful queues the alloca to be |
| 3072 | /// promoted. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3073 | bool SROA::rewritePartition(AllocaInst &AI, AllocaSlices &S, |
| 3074 | AllocaSlices::iterator B, AllocaSlices::iterator E, |
| 3075 | int64_t BeginOffset, int64_t EndOffset, |
| 3076 | ArrayRef<AllocaSlices::iterator> SplitUses) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3077 | assert(BeginOffset < EndOffset); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3078 | uint64_t SliceSize = EndOffset - BeginOffset; |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3079 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3080 | // Try to compute a friendly type for this partition of the alloca. This |
| 3081 | // won't always succeed, in which case we fall back to a legal integer type |
| 3082 | // or an i8 array of an appropriate size. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3083 | Type *SliceTy = 0; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3084 | if (Type *CommonUseTy = findCommonType(B, E, EndOffset)) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3085 | if (DL->getTypeAllocSize(CommonUseTy) >= SliceSize) |
| 3086 | SliceTy = CommonUseTy; |
| 3087 | if (!SliceTy) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3088 | if (Type *TypePartitionTy = getTypePartition(*DL, AI.getAllocatedType(), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3089 | BeginOffset, SliceSize)) |
| 3090 | SliceTy = TypePartitionTy; |
| 3091 | if ((!SliceTy || (SliceTy->isArrayTy() && |
| 3092 | SliceTy->getArrayElementType()->isIntegerTy())) && |
| 3093 | DL->isLegalInteger(SliceSize * 8)) |
| 3094 | SliceTy = Type::getIntNTy(*C, SliceSize * 8); |
| 3095 | if (!SliceTy) |
| 3096 | SliceTy = ArrayType::get(Type::getInt8Ty(*C), SliceSize); |
| 3097 | assert(DL->getTypeAllocSize(SliceTy) >= SliceSize); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3098 | |
| 3099 | bool IsVectorPromotable = isVectorPromotionViable( |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3100 | *DL, SliceTy, S, BeginOffset, EndOffset, B, E, SplitUses); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3101 | |
| 3102 | bool IsIntegerPromotable = |
| 3103 | !IsVectorPromotable && |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3104 | isIntegerWideningViable(*DL, SliceTy, BeginOffset, S, B, E, SplitUses); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3105 | |
| 3106 | // Check for the case where we're going to rewrite to a new alloca of the |
| 3107 | // exact same type as the original, and with the same access offsets. In that |
| 3108 | // case, re-use the existing alloca, but still run through the rewriter to |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 3109 | // perform phi and select speculation. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3110 | AllocaInst *NewAI; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3111 | if (SliceTy == AI.getAllocatedType()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3112 | assert(BeginOffset == 0 && |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3113 | "Non-zero begin offset but same alloca type"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3114 | NewAI = &AI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3115 | // FIXME: We should be able to bail at this point with "nothing changed". |
| 3116 | // FIXME: We might want to defer PHI speculation until after here. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3117 | } else { |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 3118 | unsigned Alignment = AI.getAlignment(); |
| 3119 | if (!Alignment) { |
| 3120 | // The minimum alignment which users can rely on when the explicit |
| 3121 | // alignment is omitted or zero is that required by the ABI for this |
| 3122 | // type. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3123 | Alignment = DL->getABITypeAlignment(AI.getAllocatedType()); |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 3124 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3125 | Alignment = MinAlign(Alignment, BeginOffset); |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 3126 | // If we will get at least this much alignment from the type alone, leave |
| 3127 | // the alloca's alignment unconstrained. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3128 | if (Alignment <= DL->getABITypeAlignment(SliceTy)) |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 3129 | Alignment = 0; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3130 | NewAI = new AllocaInst(SliceTy, 0, Alignment, |
| 3131 | AI.getName() + ".sroa." + Twine(B - S.begin()), &AI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3132 | ++NumNewAllocas; |
| 3133 | } |
| 3134 | |
| 3135 | DEBUG(dbgs() << "Rewriting alloca partition " |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3136 | << "[" << BeginOffset << "," << EndOffset << ") to: " << *NewAI |
| 3137 | << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3138 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3139 | // Track the high watermark on several worklists that are only relevant for |
| 3140 | // promoted allocas. We will reset it to this point if the alloca is not in |
| 3141 | // fact scheduled for promotion. |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3142 | unsigned PPWOldSize = PostPromotionWorklist.size(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3143 | unsigned SPOldSize = SpeculatablePHIs.size(); |
| 3144 | unsigned SSOldSize = SpeculatableSelects.size(); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3145 | unsigned NumUses = 0; |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3146 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3147 | AllocaSliceRewriter Rewriter(*DL, S, *this, AI, *NewAI, BeginOffset, |
| 3148 | EndOffset, IsVectorPromotable, |
| 3149 | IsIntegerPromotable); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3150 | bool Promotable = true; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3151 | for (ArrayRef<AllocaSlices::iterator>::const_iterator SUI = SplitUses.begin(), |
| 3152 | SUE = SplitUses.end(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3153 | SUI != SUE; ++SUI) { |
| 3154 | DEBUG(dbgs() << " rewriting split "); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3155 | DEBUG(S.printSlice(dbgs(), *SUI, "")); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3156 | Promotable &= Rewriter.visit(*SUI); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3157 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3158 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3159 | for (AllocaSlices::iterator I = B; I != E; ++I) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3160 | DEBUG(dbgs() << " rewriting "); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3161 | DEBUG(S.printSlice(dbgs(), I, "")); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3162 | Promotable &= Rewriter.visit(I); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3163 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3166 | NumAllocaPartitionUses += NumUses; |
| 3167 | MaxUsesPerAllocaPartition = |
| 3168 | std::max<unsigned>(NumUses, MaxUsesPerAllocaPartition); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3169 | |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 3170 | if (Promotable && !Rewriter.isUsedByRewrittenSpeculatableInstructions()) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3171 | DEBUG(dbgs() << " and queuing for promotion\n"); |
| 3172 | PromotableAllocas.push_back(NewAI); |
Chandler Carruth | 58e25d3 | 2013-07-24 12:12:17 +0000 | [diff] [blame] | 3173 | } else if (NewAI != &AI || |
| 3174 | (Promotable && |
| 3175 | Rewriter.isUsedByRewrittenSpeculatableInstructions())) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3176 | // If we can't promote the alloca, iterate on it to check for new |
| 3177 | // refinements exposed by splitting the current alloca. Don't iterate on an |
| 3178 | // alloca which didn't actually change and didn't get promoted. |
Chandler Carruth | 58e25d3 | 2013-07-24 12:12:17 +0000 | [diff] [blame] | 3179 | // |
| 3180 | // Alternatively, if we could promote the alloca but have speculatable |
| 3181 | // instructions then we will speculate them after finishing our processing |
| 3182 | // of the original alloca. Mark the new one for re-visiting in the next |
| 3183 | // iteration so the speculated operations can be rewritten. |
| 3184 | // |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3185 | // FIXME: We should actually track whether the rewriter changed anything. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3186 | Worklist.insert(NewAI); |
| 3187 | } |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3188 | |
| 3189 | // Drop any post-promotion work items if promotion didn't happen. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3190 | if (!Promotable) { |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3191 | while (PostPromotionWorklist.size() > PPWOldSize) |
| 3192 | PostPromotionWorklist.pop_back(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3193 | while (SpeculatablePHIs.size() > SPOldSize) |
| 3194 | SpeculatablePHIs.pop_back(); |
| 3195 | while (SpeculatableSelects.size() > SSOldSize) |
| 3196 | SpeculatableSelects.pop_back(); |
| 3197 | } |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3198 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3199 | return true; |
| 3200 | } |
| 3201 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3202 | namespace { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3203 | struct IsSliceEndLessOrEqualTo { |
| 3204 | uint64_t UpperBound; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3205 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3206 | IsSliceEndLessOrEqualTo(uint64_t UpperBound) : UpperBound(UpperBound) {} |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3207 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3208 | bool operator()(const AllocaSlices::iterator &I) { |
| 3209 | return I->endOffset() <= UpperBound; |
| 3210 | } |
| 3211 | }; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3214 | static void |
| 3215 | removeFinishedSplitUses(SmallVectorImpl<AllocaSlices::iterator> &SplitUses, |
| 3216 | uint64_t &MaxSplitUseEndOffset, uint64_t Offset) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3217 | if (Offset >= MaxSplitUseEndOffset) { |
| 3218 | SplitUses.clear(); |
| 3219 | MaxSplitUseEndOffset = 0; |
| 3220 | return; |
| 3221 | } |
| 3222 | |
| 3223 | size_t SplitUsesOldSize = SplitUses.size(); |
| 3224 | SplitUses.erase(std::remove_if(SplitUses.begin(), SplitUses.end(), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3225 | IsSliceEndLessOrEqualTo(Offset)), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3226 | SplitUses.end()); |
| 3227 | if (SplitUsesOldSize == SplitUses.size()) |
| 3228 | return; |
| 3229 | |
| 3230 | // Recompute the max. While this is linear, so is remove_if. |
| 3231 | MaxSplitUseEndOffset = 0; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3232 | for (SmallVectorImpl<AllocaSlices::iterator>::iterator |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3233 | SUI = SplitUses.begin(), |
| 3234 | SUE = SplitUses.end(); |
| 3235 | SUI != SUE; ++SUI) |
| 3236 | MaxSplitUseEndOffset = std::max((*SUI)->endOffset(), MaxSplitUseEndOffset); |
| 3237 | } |
| 3238 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3239 | /// \brief Walks the slices of an alloca and form partitions based on them, |
| 3240 | /// rewriting each of their uses. |
| 3241 | bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &S) { |
| 3242 | if (S.begin() == S.end()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3243 | return false; |
| 3244 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3245 | unsigned NumPartitions = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3246 | bool Changed = false; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3247 | SmallVector<AllocaSlices::iterator, 4> SplitUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3248 | uint64_t MaxSplitUseEndOffset = 0; |
| 3249 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3250 | uint64_t BeginOffset = S.begin()->beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3251 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3252 | for (AllocaSlices::iterator SI = S.begin(), SJ = llvm::next(SI), SE = S.end(); |
| 3253 | SI != SE; SI = SJ) { |
| 3254 | uint64_t MaxEndOffset = SI->endOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3255 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3256 | if (!SI->isSplittable()) { |
| 3257 | // When we're forming an unsplittable region, it must always start at the |
| 3258 | // first slice and will extend through its end. |
| 3259 | assert(BeginOffset == SI->beginOffset()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3260 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3261 | // Form a partition including all of the overlapping slices with this |
| 3262 | // unsplittable slice. |
| 3263 | while (SJ != SE && SJ->beginOffset() < MaxEndOffset) { |
| 3264 | if (!SJ->isSplittable()) |
| 3265 | MaxEndOffset = std::max(MaxEndOffset, SJ->endOffset()); |
| 3266 | ++SJ; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3267 | } |
| 3268 | } else { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3269 | assert(SI->isSplittable()); // Established above. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3270 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3271 | // Collect all of the overlapping splittable slices. |
| 3272 | while (SJ != SE && SJ->beginOffset() < MaxEndOffset && |
| 3273 | SJ->isSplittable()) { |
| 3274 | MaxEndOffset = std::max(MaxEndOffset, SJ->endOffset()); |
| 3275 | ++SJ; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3278 | // Back up MaxEndOffset and SJ if we ended the span early when |
| 3279 | // encountering an unsplittable slice. |
| 3280 | if (SJ != SE && SJ->beginOffset() < MaxEndOffset) { |
| 3281 | assert(!SJ->isSplittable()); |
| 3282 | MaxEndOffset = SJ->beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | // Check if we have managed to move the end offset forward yet. If so, |
| 3287 | // we'll have to rewrite uses and erase old split uses. |
| 3288 | if (BeginOffset < MaxEndOffset) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3289 | // Rewrite a sequence of overlapping slices. |
| 3290 | Changed |= |
| 3291 | rewritePartition(AI, S, SI, SJ, BeginOffset, MaxEndOffset, SplitUses); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3292 | ++NumPartitions; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3293 | |
| 3294 | removeFinishedSplitUses(SplitUses, MaxSplitUseEndOffset, MaxEndOffset); |
| 3295 | } |
| 3296 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3297 | // Accumulate all the splittable slices from the [SI,SJ) region which |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3298 | // overlap going forward. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3299 | for (AllocaSlices::iterator SK = SI; SK != SJ; ++SK) |
| 3300 | if (SK->isSplittable() && SK->endOffset() > MaxEndOffset) { |
| 3301 | SplitUses.push_back(SK); |
| 3302 | MaxSplitUseEndOffset = std::max(SK->endOffset(), MaxSplitUseEndOffset); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | // If we're already at the end and we have no split uses, we're done. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3306 | if (SJ == SE && SplitUses.empty()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3307 | break; |
| 3308 | |
| 3309 | // If we have no split uses or no gap in offsets, we're ready to move to |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3310 | // the next slice. |
| 3311 | if (SplitUses.empty() || (SJ != SE && MaxEndOffset == SJ->beginOffset())) { |
| 3312 | BeginOffset = SJ->beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3313 | continue; |
| 3314 | } |
| 3315 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3316 | // Even if we have split slices, if the next slice is splittable and the |
| 3317 | // split slices reach it, we can simply set up the beginning offset of the |
| 3318 | // next iteration to bridge between them. |
| 3319 | if (SJ != SE && SJ->isSplittable() && |
| 3320 | MaxSplitUseEndOffset > SJ->beginOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3321 | BeginOffset = MaxEndOffset; |
| 3322 | continue; |
| 3323 | } |
| 3324 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3325 | // Otherwise, we have a tail of split slices. Rewrite them with an empty |
| 3326 | // range of slices. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3327 | uint64_t PostSplitEndOffset = |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3328 | SJ == SE ? MaxSplitUseEndOffset : SJ->beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3329 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3330 | Changed |= rewritePartition(AI, S, SJ, SJ, MaxEndOffset, PostSplitEndOffset, |
| 3331 | SplitUses); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3332 | ++NumPartitions; |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3333 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3334 | if (SJ == SE) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3335 | break; // Skip the rest, we don't need to do any cleanup. |
| 3336 | |
| 3337 | removeFinishedSplitUses(SplitUses, MaxSplitUseEndOffset, |
| 3338 | PostSplitEndOffset); |
| 3339 | |
| 3340 | // Now just reset the begin offset for the next iteration. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3341 | BeginOffset = SJ->beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3342 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3343 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3344 | NumAllocaPartitions += NumPartitions; |
| 3345 | MaxPartitionsPerAlloca = |
| 3346 | std::max<unsigned>(NumPartitions, MaxPartitionsPerAlloca); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 3347 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3348 | return Changed; |
| 3349 | } |
| 3350 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 3351 | /// \brief Clobber a use with undef, deleting the used value if it becomes dead. |
| 3352 | void SROA::clobberUse(Use &U) { |
| 3353 | Value *OldV = U; |
| 3354 | // Replace the use with an undef value. |
| 3355 | U = UndefValue::get(OldV->getType()); |
| 3356 | |
| 3357 | // Check for this making an instruction dead. We have to garbage collect |
| 3358 | // all the dead instructions to ensure the uses of any alloca end up being |
| 3359 | // minimal. |
| 3360 | if (Instruction *OldI = dyn_cast<Instruction>(OldV)) |
| 3361 | if (isInstructionTriviallyDead(OldI)) { |
| 3362 | DeadInsts.insert(OldI); |
| 3363 | } |
| 3364 | } |
| 3365 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3366 | /// \brief Analyze an alloca for SROA. |
| 3367 | /// |
| 3368 | /// This analyzes the alloca to ensure we can reason about it, builds |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3369 | /// the slices of the alloca, and then hands it off to be split and |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3370 | /// rewritten as needed. |
| 3371 | bool SROA::runOnAlloca(AllocaInst &AI) { |
| 3372 | DEBUG(dbgs() << "SROA alloca: " << AI << "\n"); |
| 3373 | ++NumAllocasAnalyzed; |
| 3374 | |
| 3375 | // Special case dead allocas, as they're trivial. |
| 3376 | if (AI.use_empty()) { |
| 3377 | AI.eraseFromParent(); |
| 3378 | return true; |
| 3379 | } |
| 3380 | |
| 3381 | // Skip alloca forms that this analysis can't handle. |
| 3382 | if (AI.isArrayAllocation() || !AI.getAllocatedType()->isSized() || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3383 | DL->getTypeAllocSize(AI.getAllocatedType()) == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3384 | return false; |
| 3385 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3386 | bool Changed = false; |
| 3387 | |
| 3388 | // First, split any FCA loads and stores touching this alloca to promote |
| 3389 | // better splitting and promotion opportunities. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3390 | AggLoadStoreRewriter AggRewriter(*DL); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3391 | Changed |= AggRewriter.rewrite(AI); |
| 3392 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3393 | // Build the slices using a recursive instruction-visiting builder. |
| 3394 | AllocaSlices S(*DL, AI); |
| 3395 | DEBUG(S.print(dbgs())); |
| 3396 | if (S.isEscaped()) |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3397 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3398 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3399 | // Delete all the dead users of this alloca before splitting and rewriting it. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3400 | for (AllocaSlices::dead_user_iterator DI = S.dead_user_begin(), |
| 3401 | DE = S.dead_user_end(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3402 | DI != DE; ++DI) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 3403 | // Free up everything used by this instruction. |
| 3404 | for (User::op_iterator DOI = (*DI)->op_begin(), DOE = (*DI)->op_end(); |
| 3405 | DOI != DOE; ++DOI) |
| 3406 | clobberUse(*DOI); |
| 3407 | |
| 3408 | // Now replace the uses of this instruction. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3409 | (*DI)->replaceAllUsesWith(UndefValue::get((*DI)->getType())); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 3410 | |
| 3411 | // And mark it for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 3412 | DeadInsts.insert(*DI); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 3413 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3414 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3415 | for (AllocaSlices::dead_op_iterator DO = S.dead_op_begin(), |
| 3416 | DE = S.dead_op_end(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3417 | DO != DE; ++DO) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 3418 | clobberUse(**DO); |
| 3419 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3422 | // No slices to split. Leave the dead alloca for a later pass to clean up. |
| 3423 | if (S.begin() == S.end()) |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 3424 | return Changed; |
| 3425 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 3426 | Changed |= splitAlloca(AI, S); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3427 | |
| 3428 | DEBUG(dbgs() << " Speculating PHIs\n"); |
| 3429 | while (!SpeculatablePHIs.empty()) |
| 3430 | speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val()); |
| 3431 | |
| 3432 | DEBUG(dbgs() << " Speculating Selects\n"); |
| 3433 | while (!SpeculatableSelects.empty()) |
| 3434 | speculateSelectInstLoads(*SpeculatableSelects.pop_back_val()); |
| 3435 | |
| 3436 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 3439 | /// \brief Delete the dead instructions accumulated in this run. |
| 3440 | /// |
| 3441 | /// Recursively deletes the dead instructions we've accumulated. This is done |
| 3442 | /// at the very end to maximize locality of the recursive delete and to |
| 3443 | /// minimize the problems of invalidated instruction pointers as such pointers |
| 3444 | /// are used heavily in the intermediate stages of the algorithm. |
| 3445 | /// |
| 3446 | /// We also record the alloca instructions deleted here so that they aren't |
| 3447 | /// subsequently handed to mem2reg to promote. |
| 3448 | void SROA::deleteDeadInstructions(SmallPtrSet<AllocaInst*, 4> &DeletedAllocas) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3449 | while (!DeadInsts.empty()) { |
| 3450 | Instruction *I = DeadInsts.pop_back_val(); |
| 3451 | DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n"); |
| 3452 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 3453 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 3454 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3455 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 3456 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 3457 | // Zero out the operand and see if it becomes trivially dead. |
| 3458 | *OI = 0; |
| 3459 | if (isInstructionTriviallyDead(U)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 3460 | DeadInsts.insert(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 3464 | DeletedAllocas.insert(AI); |
| 3465 | |
| 3466 | ++NumDeleted; |
| 3467 | I->eraseFromParent(); |
| 3468 | } |
| 3469 | } |
| 3470 | |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3471 | static void enqueueUsersInWorklist(Instruction &I, |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3472 | SmallVectorImpl<Instruction *> &Worklist, |
| 3473 | SmallPtrSet<Instruction *, 8> &Visited) { |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3474 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); UI != UE; |
| 3475 | ++UI) |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3476 | if (Visited.insert(cast<Instruction>(*UI))) |
| 3477 | Worklist.push_back(cast<Instruction>(*UI)); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3480 | /// \brief Promote the allocas, using the best available technique. |
| 3481 | /// |
| 3482 | /// This attempts to promote whatever allocas have been identified as viable in |
| 3483 | /// the PromotableAllocas list. If that list is empty, there is nothing to do. |
| 3484 | /// If there is a domtree available, we attempt to promote using the full power |
| 3485 | /// of mem2reg. Otherwise, we build and use the AllocaPromoter above which is |
| 3486 | /// based on the SSAUpdater utilities. This function returns whether any |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 3487 | /// promotion occurred. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3488 | bool SROA::promoteAllocas(Function &F) { |
| 3489 | if (PromotableAllocas.empty()) |
| 3490 | return false; |
| 3491 | |
| 3492 | NumPromoted += PromotableAllocas.size(); |
| 3493 | |
| 3494 | if (DT && !ForceSSAUpdater) { |
| 3495 | DEBUG(dbgs() << "Promoting allocas with mem2reg...\n"); |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 3496 | PromoteMemToReg(PromotableAllocas, *DT); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3497 | PromotableAllocas.clear(); |
| 3498 | return true; |
| 3499 | } |
| 3500 | |
| 3501 | DEBUG(dbgs() << "Promoting allocas with SSAUpdater...\n"); |
| 3502 | SSAUpdater SSA; |
| 3503 | DIBuilder DIB(*F.getParent()); |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3504 | SmallVector<Instruction *, 64> Insts; |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3505 | |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3506 | // We need a worklist to walk the uses of each alloca. |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3507 | SmallVector<Instruction *, 8> Worklist; |
| 3508 | SmallPtrSet<Instruction *, 8> Visited; |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3509 | SmallVector<Instruction *, 32> DeadInsts; |
| 3510 | |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3511 | for (unsigned Idx = 0, Size = PromotableAllocas.size(); Idx != Size; ++Idx) { |
| 3512 | AllocaInst *AI = PromotableAllocas[Idx]; |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3513 | Insts.clear(); |
| 3514 | Worklist.clear(); |
| 3515 | Visited.clear(); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3516 | |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3517 | enqueueUsersInWorklist(*AI, Worklist, Visited); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3518 | |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3519 | while (!Worklist.empty()) { |
| 3520 | Instruction *I = Worklist.pop_back_val(); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3521 | |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3522 | // FIXME: Currently the SSAUpdater infrastructure doesn't reason about |
| 3523 | // lifetime intrinsics and so we strip them (and the bitcasts+GEPs |
| 3524 | // leading to them) here. Eventually it should use them to optimize the |
| 3525 | // scalar values produced. |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3526 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3527 | assert(II->getIntrinsicID() == Intrinsic::lifetime_start || |
| 3528 | II->getIntrinsicID() == Intrinsic::lifetime_end); |
| 3529 | II->eraseFromParent(); |
| 3530 | continue; |
| 3531 | } |
| 3532 | |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3533 | // Push the loads and stores we find onto the list. SROA will already |
| 3534 | // have validated that all loads and stores are viable candidates for |
| 3535 | // promotion. |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3536 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3537 | assert(LI->getType() == AI->getAllocatedType()); |
| 3538 | Insts.push_back(LI); |
| 3539 | continue; |
| 3540 | } |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3541 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3542 | assert(SI->getValueOperand()->getType() == AI->getAllocatedType()); |
| 3543 | Insts.push_back(SI); |
| 3544 | continue; |
| 3545 | } |
| 3546 | |
| 3547 | // For everything else, we know that only no-op bitcasts and GEPs will |
| 3548 | // make it this far, just recurse through them and recall them for later |
| 3549 | // removal. |
Chandler Carruth | 45b136f | 2013-08-11 01:03:18 +0000 | [diff] [blame] | 3550 | DeadInsts.push_back(I); |
| 3551 | enqueueUsersInWorklist(*I, Worklist, Visited); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3552 | } |
| 3553 | AllocaPromoter(Insts, SSA, *AI, DIB).run(Insts); |
Chandler Carruth | cd7c8cd | 2013-07-29 09:06:53 +0000 | [diff] [blame] | 3554 | while (!DeadInsts.empty()) |
| 3555 | DeadInsts.pop_back_val()->eraseFromParent(); |
| 3556 | AI->eraseFromParent(); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | PromotableAllocas.clear(); |
| 3560 | return true; |
| 3561 | } |
| 3562 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3563 | namespace { |
| 3564 | /// \brief A predicate to test whether an alloca belongs to a set. |
| 3565 | class IsAllocaInSet { |
| 3566 | typedef SmallPtrSet<AllocaInst *, 4> SetType; |
| 3567 | const SetType &Set; |
| 3568 | |
| 3569 | public: |
Chandler Carruth | 3f57b82 | 2012-10-03 00:03:00 +0000 | [diff] [blame] | 3570 | typedef AllocaInst *argument_type; |
| 3571 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3572 | IsAllocaInSet(const SetType &Set) : Set(Set) {} |
Chandler Carruth | 3f57b82 | 2012-10-03 00:03:00 +0000 | [diff] [blame] | 3573 | bool operator()(AllocaInst *AI) const { return Set.count(AI); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3574 | }; |
| 3575 | } |
| 3576 | |
| 3577 | bool SROA::runOnFunction(Function &F) { |
| 3578 | DEBUG(dbgs() << "SROA function: " << F.getName() << "\n"); |
| 3579 | C = &F.getContext(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3580 | DL = getAnalysisIfAvailable<DataLayout>(); |
| 3581 | if (!DL) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3582 | DEBUG(dbgs() << " Skipping SROA -- no target data!\n"); |
| 3583 | return false; |
| 3584 | } |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 3585 | DominatorTreeWrapperPass *DTWP = |
| 3586 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 3587 | DT = DTWP ? &DTWP->getDomTree() : 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3588 | |
| 3589 | BasicBlock &EntryBB = F.getEntryBlock(); |
| 3590 | for (BasicBlock::iterator I = EntryBB.begin(), E = llvm::prior(EntryBB.end()); |
| 3591 | I != E; ++I) |
| 3592 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 3593 | Worklist.insert(AI); |
| 3594 | |
| 3595 | bool Changed = false; |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 3596 | // A set of deleted alloca instruction pointers which should be removed from |
| 3597 | // the list of promotable allocas. |
| 3598 | SmallPtrSet<AllocaInst *, 4> DeletedAllocas; |
| 3599 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3600 | do { |
| 3601 | while (!Worklist.empty()) { |
| 3602 | Changed |= runOnAlloca(*Worklist.pop_back_val()); |
| 3603 | deleteDeadInstructions(DeletedAllocas); |
Chandler Carruth | b09f0a3 | 2012-10-02 22:46:45 +0000 | [diff] [blame] | 3604 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3605 | // Remove the deleted allocas from various lists so that we don't try to |
| 3606 | // continue processing them. |
| 3607 | if (!DeletedAllocas.empty()) { |
| 3608 | Worklist.remove_if(IsAllocaInSet(DeletedAllocas)); |
| 3609 | PostPromotionWorklist.remove_if(IsAllocaInSet(DeletedAllocas)); |
| 3610 | PromotableAllocas.erase(std::remove_if(PromotableAllocas.begin(), |
| 3611 | PromotableAllocas.end(), |
| 3612 | IsAllocaInSet(DeletedAllocas)), |
| 3613 | PromotableAllocas.end()); |
| 3614 | DeletedAllocas.clear(); |
| 3615 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3616 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3617 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 3618 | Changed |= promoteAllocas(F); |
| 3619 | |
| 3620 | Worklist = PostPromotionWorklist; |
| 3621 | PostPromotionWorklist.clear(); |
| 3622 | } while (!Worklist.empty()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3623 | |
| 3624 | return Changed; |
| 3625 | } |
| 3626 | |
| 3627 | void SROA::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 3628 | if (RequiresDomTree) |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 3629 | AU.addRequired<DominatorTreeWrapperPass>(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3630 | AU.setPreservesCFG(); |
| 3631 | } |