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