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