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