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