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