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