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