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