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" |
Chris Lattner | 9fc5cdf | 2011-01-02 22:09:33 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/DominanceFrontier.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetData.h" |
| 36 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Devang Patel | 4afc90d | 2009-02-10 07:00:59 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | a9be1df | 2010-11-18 06:26:49 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 41 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 65a6502 | 2009-02-03 19:41:50 +0000 | [diff] [blame] | 42 | #include "llvm/Support/IRBuilder.h" |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 43 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 44 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 1ccd185 | 2007-02-12 22:56:41 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 47 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 49 | STATISTIC(NumReplaced, "Number of allocas broken up"); |
| 50 | STATISTIC(NumPromoted, "Number of allocas promoted"); |
| 51 | STATISTIC(NumConverted, "Number of aggregates converted to scalar"); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 52 | STATISTIC(NumGlobals, "Number of allocas copied from constant global"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 54 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 55 | struct SROA : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 56 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 57 | explicit SROA(signed T = -1) : FunctionPass(ID) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 58 | initializeSROAPass(*PassRegistry::getPassRegistry()); |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 59 | if (T == -1) |
Chris Lattner | b0e71ed | 2007-08-02 21:33:36 +0000 | [diff] [blame] | 60 | SRThreshold = 128; |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 61 | else |
| 62 | SRThreshold = T; |
| 63 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 64 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 65 | bool runOnFunction(Function &F); |
| 66 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 67 | bool performScalarRepl(Function &F); |
| 68 | bool performPromotion(Function &F); |
| 69 | |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 70 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 71 | // will not alter the CFG, so say so. |
| 72 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 73 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 74 | AU.addRequired<DominanceFrontier>(); |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 75 | AU.setPreservesCFG(); |
| 76 | } |
| 77 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 78 | private: |
Chris Lattner | 56c3852 | 2009-01-07 06:34:28 +0000 | [diff] [blame] | 79 | TargetData *TD; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 80 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 81 | /// DeadInsts - Keep track of instructions we have made dead, so that |
| 82 | /// we can remove them after we are done working. |
| 83 | SmallVector<Value*, 32> DeadInsts; |
| 84 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 85 | /// AllocaInfo - When analyzing uses of an alloca instruction, this captures |
| 86 | /// information about the uses. All these fields are initialized to false |
| 87 | /// and set to true when something is learned. |
| 88 | struct AllocaInfo { |
| 89 | /// isUnsafe - This is set to true if the alloca cannot be SROA'd. |
| 90 | bool isUnsafe : 1; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 91 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 92 | /// isMemCpySrc - This is true if this aggregate is memcpy'd from. |
| 93 | bool isMemCpySrc : 1; |
| 94 | |
Zhou Sheng | 33b0b8d | 2007-07-06 06:01:16 +0000 | [diff] [blame] | 95 | /// isMemCpyDst - This is true if this aggregate is memcpy'd into. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 96 | bool isMemCpyDst : 1; |
| 97 | |
| 98 | AllocaInfo() |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 99 | : isUnsafe(false), isMemCpySrc(false), isMemCpyDst(false) {} |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 100 | }; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 101 | |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 102 | unsigned SRThreshold; |
| 103 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 104 | void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; } |
| 105 | |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 106 | bool isSafeAllocaToScalarRepl(AllocaInst *AI); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 107 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 108 | void isSafeForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 109 | AllocaInfo &Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 110 | void isSafeGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t &Offset, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 111 | AllocaInfo &Info); |
| 112 | void isSafeMemAccess(AllocaInst *AI, uint64_t Offset, uint64_t MemSize, |
| 113 | const Type *MemOpType, bool isStore, AllocaInfo &Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 114 | bool TypeHasComponent(const Type *T, uint64_t Offset, uint64_t Size); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 115 | uint64_t FindElementAndOffset(const Type *&T, uint64_t &Offset, |
| 116 | const Type *&IdxTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 117 | |
| 118 | void DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 119 | std::vector<AllocaInst*> &WorkList); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 120 | void DeleteDeadInstructions(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 121 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 122 | void RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
| 123 | SmallVector<AllocaInst*, 32> &NewElts); |
| 124 | void RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
| 125 | SmallVector<AllocaInst*, 32> &NewElts); |
| 126 | void RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
| 127 | SmallVector<AllocaInst*, 32> &NewElts); |
| 128 | void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 129 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 130 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 131 | void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 132 | SmallVector<AllocaInst*, 32> &NewElts); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 133 | void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 134 | SmallVector<AllocaInst*, 32> &NewElts); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 135 | |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 136 | static MemTransferInst *isOnlyCopiedFromConstantGlobal(AllocaInst *AI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 137 | }; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 140 | char SROA::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 141 | INITIALIZE_PASS_BEGIN(SROA, "scalarrepl", |
| 142 | "Scalar Replacement of Aggregates", false, false) |
| 143 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 144 | INITIALIZE_PASS_DEPENDENCY(DominanceFrontier) |
| 145 | INITIALIZE_PASS_END(SROA, "scalarrepl", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 146 | "Scalar Replacement of Aggregates", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 147 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 148 | // Public interface to the ScalarReplAggregates pass |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 149 | FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) { |
Devang Patel | ff36685 | 2007-07-09 21:19:23 +0000 | [diff] [blame] | 150 | return new SROA(Threshold); |
| 151 | } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 152 | |
| 153 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 154 | //===----------------------------------------------------------------------===// |
| 155 | // Convert To Scalar Optimization. |
| 156 | //===----------------------------------------------------------------------===// |
| 157 | |
| 158 | namespace { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 159 | /// ConvertToScalarInfo - This class implements the "Convert To Scalar" |
| 160 | /// optimization, which scans the uses of an alloca and determines if it can |
| 161 | /// 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] | 162 | class ConvertToScalarInfo { |
| 163 | /// AllocaSize - The size of the alloca being considered. |
| 164 | unsigned AllocaSize; |
| 165 | const TargetData &TD; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 166 | |
Chris Lattner | a0bada7 | 2010-04-16 02:32:17 +0000 | [diff] [blame] | 167 | /// 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] | 168 | /// which means that mem2reg can't promote it. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 169 | bool IsNotTrivial; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 170 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 171 | /// VectorTy - This tracks the type that we should promote the vector to if |
| 172 | /// it is possible to turn it into a vector. This starts out null, and if it |
| 173 | /// 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] | 174 | const Type *VectorTy; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 175 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 176 | /// HadAVector - True if there is at least one vector access to the alloca. |
| 177 | /// We don't want to turn random arrays into vectors and use vector element |
| 178 | /// insert/extract, but if there are element accesses to something that is |
| 179 | /// 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] | 180 | bool HadAVector; |
| 181 | |
| 182 | public: |
| 183 | explicit ConvertToScalarInfo(unsigned Size, const TargetData &td) |
| 184 | : AllocaSize(Size), TD(td) { |
| 185 | IsNotTrivial = false; |
| 186 | VectorTy = 0; |
| 187 | HadAVector = false; |
| 188 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 189 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 190 | AllocaInst *TryConvert(AllocaInst *AI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 191 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 192 | private: |
| 193 | bool CanConvertToScalar(Value *V, uint64_t Offset); |
| 194 | void MergeInType(const Type *In, uint64_t Offset); |
| 195 | void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 196 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 197 | Value *ConvertScalar_ExtractValue(Value *NV, const Type *ToType, |
| 198 | uint64_t Offset, IRBuilder<> &Builder); |
| 199 | Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal, |
| 200 | uint64_t Offset, IRBuilder<> &Builder); |
| 201 | }; |
| 202 | } // end anonymous namespace. |
| 203 | |
Chris Lattner | 91abace | 2010-09-01 05:14:33 +0000 | [diff] [blame] | 204 | |
| 205 | /// IsVerbotenVectorType - Return true if this is a vector type ScalarRepl isn't |
| 206 | /// allowed to form. We do this to avoid MMX types, which is a complete hack, |
| 207 | /// but is required until the backend is fixed. |
Chris Lattner | 72eaa0e | 2010-09-01 23:09:27 +0000 | [diff] [blame] | 208 | static bool IsVerbotenVectorType(const VectorType *VTy, const Instruction *I) { |
| 209 | StringRef Triple(I->getParent()->getParent()->getParent()->getTargetTriple()); |
| 210 | if (!Triple.startswith("i386") && |
| 211 | !Triple.startswith("x86_64")) |
| 212 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 213 | |
Chris Lattner | 91abace | 2010-09-01 05:14:33 +0000 | [diff] [blame] | 214 | // Reject all the MMX vector types. |
| 215 | switch (VTy->getNumElements()) { |
| 216 | default: return false; |
| 217 | case 1: return VTy->getElementType()->isIntegerTy(64); |
| 218 | case 2: return VTy->getElementType()->isIntegerTy(32); |
| 219 | case 4: return VTy->getElementType()->isIntegerTy(16); |
| 220 | case 8: return VTy->getElementType()->isIntegerTy(8); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 225 | /// TryConvert - Analyze the specified alloca, and if it is safe to do so, |
| 226 | /// rewrite it to be a new alloca which is mem2reg'able. This returns the new |
| 227 | /// alloca if possible or null if not. |
| 228 | AllocaInst *ConvertToScalarInfo::TryConvert(AllocaInst *AI) { |
| 229 | // If we can't convert this scalar, or if mem2reg can trivially do it, bail |
| 230 | // out. |
| 231 | if (!CanConvertToScalar(AI, 0) || !IsNotTrivial) |
| 232 | return 0; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 233 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 234 | // If we were able to find a vector type that can handle this with |
| 235 | // insert/extract elements, and if there was at least one use that had |
| 236 | // a vector type, promote this to a vector. We don't want to promote |
| 237 | // random stuff that doesn't use vectors (e.g. <9 x double>) because then |
| 238 | // we just get a lot of insert/extracts. If at least one vector is |
| 239 | // involved, then we probably really do have a union of vector/array. |
| 240 | const Type *NewTy; |
Chris Lattner | 91abace | 2010-09-01 05:14:33 +0000 | [diff] [blame] | 241 | if (VectorTy && VectorTy->isVectorTy() && HadAVector && |
Chris Lattner | 72eaa0e | 2010-09-01 23:09:27 +0000 | [diff] [blame] | 242 | !IsVerbotenVectorType(cast<VectorType>(VectorTy), AI)) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 243 | DEBUG(dbgs() << "CONVERT TO VECTOR: " << *AI << "\n TYPE = " |
| 244 | << *VectorTy << '\n'); |
| 245 | NewTy = VectorTy; // Use the vector type. |
| 246 | } else { |
| 247 | DEBUG(dbgs() << "CONVERT TO SCALAR INTEGER: " << *AI << "\n"); |
| 248 | // Create and insert the integer alloca. |
| 249 | NewTy = IntegerType::get(AI->getContext(), AllocaSize*8); |
| 250 | } |
| 251 | AllocaInst *NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin()); |
| 252 | ConvertUsesToScalar(AI, NewAI, 0); |
| 253 | return NewAI; |
| 254 | } |
| 255 | |
| 256 | /// MergeInType - Add the 'In' type to the accumulated vector type (VectorTy) |
| 257 | /// 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] | 258 | /// |
| 259 | /// There are two cases we handle here: |
| 260 | /// 1) A union of vector types of the same size and potentially its elements. |
| 261 | /// Here we turn element accesses into insert/extract element operations. |
| 262 | /// This promotes a <4 x float> with a store of float to the third element |
| 263 | /// into a <4 x float> that uses insert element. |
| 264 | /// 2) A fully general blob of memory, which we turn into some (potentially |
| 265 | /// large) integer type with extract and insert operations where the loads |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 266 | /// and stores would mutate the memory. We mark this by setting VectorTy |
| 267 | /// to VoidTy. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 268 | void ConvertToScalarInfo::MergeInType(const Type *In, uint64_t Offset) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 269 | // If we already decided to turn this into a blob of integer memory, there is |
| 270 | // nothing to be done. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 271 | if (VectorTy && VectorTy->isVoidTy()) |
| 272 | return; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 273 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 274 | // If this could be contributing to a vector, analyze it. |
| 275 | |
| 276 | // If the In type is a vector that is the same size as the alloca, see if it |
| 277 | // matches the existing VecTy. |
| 278 | if (const VectorType *VInTy = dyn_cast<VectorType>(In)) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 279 | // Remember if we saw a vector type. |
| 280 | HadAVector = true; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 281 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 282 | if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) { |
| 283 | // If we're storing/loading a vector of the right size, allow it as a |
| 284 | // vector. If this the first vector we see, remember the type so that |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 285 | // we know the element size. If this is a subsequent access, ignore it |
| 286 | // even if it is a differing type but the same size. Worst case we can |
| 287 | // bitcast the resultant vectors. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 288 | if (VectorTy == 0) |
| 289 | VectorTy = VInTy; |
| 290 | return; |
| 291 | } |
| 292 | } else if (In->isFloatTy() || In->isDoubleTy() || |
| 293 | (In->isIntegerTy() && In->getPrimitiveSizeInBits() >= 8 && |
| 294 | isPowerOf2_32(In->getPrimitiveSizeInBits()))) { |
| 295 | // If we're accessing something that could be an element of a vector, see |
| 296 | // if the implied vector agrees with what we already have and if Offset is |
| 297 | // compatible with it. |
| 298 | unsigned EltSize = In->getPrimitiveSizeInBits()/8; |
| 299 | if (Offset % EltSize == 0 && AllocaSize % EltSize == 0 && |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 300 | (VectorTy == 0 || |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 301 | cast<VectorType>(VectorTy)->getElementType() |
| 302 | ->getPrimitiveSizeInBits()/8 == EltSize)) { |
| 303 | if (VectorTy == 0) |
| 304 | VectorTy = VectorType::get(In, AllocaSize/EltSize); |
| 305 | return; |
| 306 | } |
| 307 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 308 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 309 | // Otherwise, we have a case that we can't handle with an optimized vector |
| 310 | // form. We can still turn this into a large integer. |
| 311 | VectorTy = Type::getVoidTy(In->getContext()); |
| 312 | } |
| 313 | |
| 314 | /// CanConvertToScalar - V is a pointer. If we can convert the pointee and all |
| 315 | /// its accesses to a single vector type, return true and set VecTy to |
| 316 | /// the new type. If we could convert the alloca into a single promotable |
| 317 | /// integer, return true but set VecTy to VoidTy. Further, if the use is not a |
| 318 | /// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset |
| 319 | /// is the current offset from the base of the alloca being analyzed. |
| 320 | /// |
| 321 | /// If we see at least one access to the value that is as a vector type, set the |
| 322 | /// SawVec flag. |
| 323 | bool ConvertToScalarInfo::CanConvertToScalar(Value *V, uint64_t Offset) { |
| 324 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) { |
| 325 | Instruction *User = cast<Instruction>(*UI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 326 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 327 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 328 | // Don't break volatile loads. |
| 329 | if (LI->isVolatile()) |
| 330 | return false; |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 331 | // Don't touch MMX operations. |
| 332 | if (LI->getType()->isX86_MMXTy()) |
| 333 | return false; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 334 | MergeInType(LI->getType(), Offset); |
| 335 | continue; |
| 336 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 337 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 338 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 339 | // Storing the pointer, not into the value? |
| 340 | if (SI->getOperand(0) == V || SI->isVolatile()) return false; |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 341 | // Don't touch MMX operations. |
| 342 | if (SI->getOperand(0)->getType()->isX86_MMXTy()) |
| 343 | return false; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 344 | MergeInType(SI->getOperand(0)->getType(), Offset); |
| 345 | continue; |
| 346 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 347 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 348 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 349 | IsNotTrivial = true; // Can't be mem2reg'd. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 350 | if (!CanConvertToScalar(BCI, Offset)) |
| 351 | return false; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 352 | continue; |
| 353 | } |
| 354 | |
| 355 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 356 | // If this is a GEP with a variable indices, we can't handle it. |
| 357 | if (!GEP->hasAllConstantIndices()) |
| 358 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 359 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 360 | // Compute the offset that this GEP adds to the pointer. |
| 361 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
| 362 | uint64_t GEPOffset = TD.getIndexedOffset(GEP->getPointerOperandType(), |
| 363 | &Indices[0], Indices.size()); |
| 364 | // See if all uses can be converted. |
| 365 | if (!CanConvertToScalar(GEP, Offset+GEPOffset)) |
| 366 | return false; |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 367 | IsNotTrivial = true; // Can't be mem2reg'd. |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 368 | continue; |
| 369 | } |
| 370 | |
| 371 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 372 | // handle it. |
| 373 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 374 | // Store of constant value and constant size. |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 375 | if (!isa<ConstantInt>(MSI->getValue()) || |
| 376 | !isa<ConstantInt>(MSI->getLength())) |
| 377 | return false; |
| 378 | IsNotTrivial = true; // Can't be mem2reg'd. |
| 379 | continue; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 383 | // can handle it like a load or store of the scalar type. |
| 384 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 385 | ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength()); |
| 386 | if (Len == 0 || Len->getZExtValue() != AllocaSize || Offset != 0) |
| 387 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 388 | |
Chris Lattner | a001b66 | 2010-04-16 00:38:19 +0000 | [diff] [blame] | 389 | IsNotTrivial = true; // Can't be mem2reg'd. |
| 390 | continue; |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 391 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 392 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 393 | // Otherwise, we cannot handle this! |
| 394 | return false; |
| 395 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 396 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 397 | return true; |
| 398 | } |
| 399 | |
| 400 | /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca |
| 401 | /// directly. This happens when we are converting an "integer union" to a |
| 402 | /// single integer scalar, or when we are converting a "vector union" to a |
| 403 | /// vector with insert/extractelement instructions. |
| 404 | /// |
| 405 | /// Offset is an offset from the original alloca, in bits that need to be |
| 406 | /// shifted to the right. By the end of this, there should be no uses of Ptr. |
| 407 | void ConvertToScalarInfo::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, |
| 408 | uint64_t Offset) { |
| 409 | while (!Ptr->use_empty()) { |
| 410 | Instruction *User = cast<Instruction>(Ptr->use_back()); |
| 411 | |
| 412 | if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) { |
| 413 | ConvertUsesToScalar(CI, NewAI, Offset); |
| 414 | CI->eraseFromParent(); |
| 415 | continue; |
| 416 | } |
| 417 | |
| 418 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) { |
| 419 | // Compute the offset that this GEP adds to the pointer. |
| 420 | SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end()); |
| 421 | uint64_t GEPOffset = TD.getIndexedOffset(GEP->getPointerOperandType(), |
| 422 | &Indices[0], Indices.size()); |
| 423 | ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8); |
| 424 | GEP->eraseFromParent(); |
| 425 | continue; |
| 426 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 427 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 428 | IRBuilder<> Builder(User); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 429 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 430 | if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 431 | // The load is a bit extract from NewAI shifted right by Offset bits. |
| 432 | Value *LoadedVal = Builder.CreateLoad(NewAI, "tmp"); |
| 433 | Value *NewLoadVal |
| 434 | = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, Builder); |
| 435 | LI->replaceAllUsesWith(NewLoadVal); |
| 436 | LI->eraseFromParent(); |
| 437 | continue; |
| 438 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 439 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 440 | if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 441 | assert(SI->getOperand(0) != Ptr && "Consistency error!"); |
| 442 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 443 | Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset, |
| 444 | Builder); |
| 445 | Builder.CreateStore(New, NewAI); |
| 446 | SI->eraseFromParent(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 447 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 448 | // If the load we just inserted is now dead, then the inserted store |
| 449 | // overwrote the entire thing. |
| 450 | if (Old->use_empty()) |
| 451 | Old->eraseFromParent(); |
| 452 | continue; |
| 453 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 454 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 455 | // If this is a constant sized memset of a constant value (e.g. 0) we can |
| 456 | // transform it into a store of the expanded constant value. |
| 457 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) { |
| 458 | assert(MSI->getRawDest() == Ptr && "Consistency error!"); |
| 459 | unsigned NumBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue(); |
| 460 | if (NumBytes != 0) { |
| 461 | unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 462 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 463 | // Compute the value replicated the right number of times. |
| 464 | APInt APVal(NumBytes*8, Val); |
| 465 | |
| 466 | // Splat the value if non-zero. |
| 467 | if (Val) |
| 468 | for (unsigned i = 1; i != NumBytes; ++i) |
| 469 | APVal |= APVal << 8; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 470 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 471 | Instruction *Old = Builder.CreateLoad(NewAI, NewAI->getName()+".in"); |
| 472 | Value *New = ConvertScalar_InsertValue( |
| 473 | ConstantInt::get(User->getContext(), APVal), |
| 474 | Old, Offset, Builder); |
| 475 | Builder.CreateStore(New, NewAI); |
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 | // If the load we just inserted is now dead, then the memset overwrote |
| 478 | // the entire thing. |
| 479 | if (Old->use_empty()) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 480 | Old->eraseFromParent(); |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 481 | } |
| 482 | MSI->eraseFromParent(); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | // If this is a memcpy or memmove into or out of the whole allocation, we |
| 487 | // can handle it like a load or store of the scalar type. |
| 488 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) { |
| 489 | assert(Offset == 0 && "must be store to start of alloca"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 490 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 491 | // If the source and destination are both to the same alloca, then this is |
| 492 | // a noop copy-to-self, just delete it. Otherwise, emit a load and store |
| 493 | // as appropriate. |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 494 | AllocaInst *OrigAI = cast<AllocaInst>(GetUnderlyingObject(Ptr, 0)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 495 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 496 | if (GetUnderlyingObject(MTI->getSource(), 0) != OrigAI) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 497 | // Dest must be OrigAI, change this to be a load from the original |
| 498 | // pointer (bitcasted), then a store to our new alloca. |
| 499 | assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?"); |
| 500 | Value *SrcPtr = MTI->getSource(); |
Mon P Wang | e90a633 | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 501 | const PointerType* SPTy = cast<PointerType>(SrcPtr->getType()); |
| 502 | const PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
| 503 | if (SPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
| 504 | AIPTy = PointerType::get(AIPTy->getElementType(), |
| 505 | SPTy->getAddressSpace()); |
| 506 | } |
| 507 | SrcPtr = Builder.CreateBitCast(SrcPtr, AIPTy); |
| 508 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 509 | LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval"); |
| 510 | SrcVal->setAlignment(MTI->getAlignment()); |
| 511 | Builder.CreateStore(SrcVal, NewAI); |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 512 | } else if (GetUnderlyingObject(MTI->getDest(), 0) != OrigAI) { |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 513 | // Src must be OrigAI, change this to be a load from NewAI then a store |
| 514 | // through the original dest pointer (bitcasted). |
| 515 | assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?"); |
| 516 | LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval"); |
| 517 | |
Mon P Wang | e90a633 | 2010-12-23 01:41:32 +0000 | [diff] [blame] | 518 | const PointerType* DPTy = cast<PointerType>(MTI->getDest()->getType()); |
| 519 | const PointerType* AIPTy = cast<PointerType>(NewAI->getType()); |
| 520 | if (DPTy->getAddressSpace() != AIPTy->getAddressSpace()) { |
| 521 | AIPTy = PointerType::get(AIPTy->getElementType(), |
| 522 | DPTy->getAddressSpace()); |
| 523 | } |
| 524 | Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), AIPTy); |
| 525 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 526 | StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr); |
| 527 | NewStore->setAlignment(MTI->getAlignment()); |
| 528 | } else { |
| 529 | // Noop transfer. Src == Dst |
| 530 | } |
| 531 | |
| 532 | MTI->eraseFromParent(); |
| 533 | continue; |
| 534 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 535 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 536 | llvm_unreachable("Unsupported operation!"); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer |
| 541 | /// or vector value FromVal, extracting the bits from the offset specified by |
| 542 | /// Offset. This returns the value, which is of type ToType. |
| 543 | /// |
| 544 | /// This happens when we are converting an "integer union" to a single |
| 545 | /// integer scalar, or when we are converting a "vector union" to a vector with |
| 546 | /// insert/extractelement instructions. |
| 547 | /// |
| 548 | /// Offset is an offset from the original alloca, in bits that need to be |
| 549 | /// shifted to the right. |
| 550 | Value *ConvertToScalarInfo:: |
| 551 | ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType, |
| 552 | uint64_t Offset, IRBuilder<> &Builder) { |
| 553 | // If the load is of the whole new alloca, no conversion is needed. |
| 554 | if (FromVal->getType() == ToType && Offset == 0) |
| 555 | return FromVal; |
| 556 | |
| 557 | // If the result alloca is a vector type, this is either an element |
| 558 | // access or a bitcast to another vector type of the same size. |
| 559 | if (const VectorType *VTy = dyn_cast<VectorType>(FromVal->getType())) { |
| 560 | if (ToType->isVectorTy()) |
| 561 | return Builder.CreateBitCast(FromVal, ToType, "tmp"); |
| 562 | |
| 563 | // Otherwise it must be an element access. |
| 564 | unsigned Elt = 0; |
| 565 | if (Offset) { |
| 566 | unsigned EltSize = TD.getTypeAllocSizeInBits(VTy->getElementType()); |
| 567 | Elt = Offset/EltSize; |
| 568 | assert(EltSize*Elt == Offset && "Invalid modulus in validity checking"); |
| 569 | } |
| 570 | // Return the element extracted out of it. |
| 571 | Value *V = Builder.CreateExtractElement(FromVal, ConstantInt::get( |
| 572 | Type::getInt32Ty(FromVal->getContext()), Elt), "tmp"); |
| 573 | if (V->getType() != ToType) |
| 574 | V = Builder.CreateBitCast(V, ToType, "tmp"); |
| 575 | return V; |
| 576 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 577 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 578 | // If ToType is a first class aggregate, extract out each of the pieces and |
| 579 | // use insertvalue's to form the FCA. |
| 580 | if (const StructType *ST = dyn_cast<StructType>(ToType)) { |
| 581 | const StructLayout &Layout = *TD.getStructLayout(ST); |
| 582 | Value *Res = UndefValue::get(ST); |
| 583 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
| 584 | Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i), |
| 585 | Offset+Layout.getElementOffsetInBits(i), |
| 586 | Builder); |
| 587 | Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); |
| 588 | } |
| 589 | return Res; |
| 590 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 591 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 592 | if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) { |
| 593 | uint64_t EltSize = TD.getTypeAllocSizeInBits(AT->getElementType()); |
| 594 | Value *Res = UndefValue::get(AT); |
| 595 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 596 | Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(), |
| 597 | Offset+i*EltSize, Builder); |
| 598 | Res = Builder.CreateInsertValue(Res, Elt, i, "tmp"); |
| 599 | } |
| 600 | return Res; |
| 601 | } |
| 602 | |
| 603 | // Otherwise, this must be a union that was converted to an integer value. |
| 604 | const IntegerType *NTy = cast<IntegerType>(FromVal->getType()); |
| 605 | |
| 606 | // If this is a big-endian system and the load is narrower than the |
| 607 | // full alloca type, we need to do a shift to get the right bits. |
| 608 | int ShAmt = 0; |
| 609 | if (TD.isBigEndian()) { |
| 610 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 611 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 612 | // integers with a bitwidth that is not a multiple of 8. |
| 613 | ShAmt = TD.getTypeStoreSizeInBits(NTy) - |
| 614 | TD.getTypeStoreSizeInBits(ToType) - Offset; |
| 615 | } else { |
| 616 | ShAmt = Offset; |
| 617 | } |
| 618 | |
| 619 | // Note: we support negative bitwidths (with shl) which are not defined. |
| 620 | // We do this to support (f.e.) loads off the end of a structure where |
| 621 | // only some bits are used. |
| 622 | if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth()) |
| 623 | FromVal = Builder.CreateLShr(FromVal, |
| 624 | ConstantInt::get(FromVal->getType(), |
| 625 | ShAmt), "tmp"); |
| 626 | else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth()) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 627 | FromVal = Builder.CreateShl(FromVal, |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 628 | ConstantInt::get(FromVal->getType(), |
| 629 | -ShAmt), "tmp"); |
| 630 | |
| 631 | // Finally, unconditionally truncate the integer to the right width. |
| 632 | unsigned LIBitWidth = TD.getTypeSizeInBits(ToType); |
| 633 | if (LIBitWidth < NTy->getBitWidth()) |
| 634 | FromVal = |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 635 | Builder.CreateTrunc(FromVal, IntegerType::get(FromVal->getContext(), |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 636 | LIBitWidth), "tmp"); |
| 637 | else if (LIBitWidth > NTy->getBitWidth()) |
| 638 | FromVal = |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 639 | Builder.CreateZExt(FromVal, IntegerType::get(FromVal->getContext(), |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 640 | LIBitWidth), "tmp"); |
| 641 | |
| 642 | // If the result is an integer, this is a trunc or bitcast. |
| 643 | if (ToType->isIntegerTy()) { |
| 644 | // Should be done. |
| 645 | } else if (ToType->isFloatingPointTy() || ToType->isVectorTy()) { |
| 646 | // Just do a bitcast, we know the sizes match up. |
| 647 | FromVal = Builder.CreateBitCast(FromVal, ToType, "tmp"); |
| 648 | } else { |
| 649 | // Otherwise must be a pointer. |
| 650 | FromVal = Builder.CreateIntToPtr(FromVal, ToType, "tmp"); |
| 651 | } |
| 652 | assert(FromVal->getType() == ToType && "Didn't convert right?"); |
| 653 | return FromVal; |
| 654 | } |
| 655 | |
| 656 | /// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer |
| 657 | /// or vector value "Old" at the offset specified by Offset. |
| 658 | /// |
| 659 | /// This happens when we are converting an "integer union" to a |
| 660 | /// single integer scalar, or when we are converting a "vector union" to a |
| 661 | /// vector with insert/extractelement instructions. |
| 662 | /// |
| 663 | /// Offset is an offset from the original alloca, in bits that need to be |
| 664 | /// shifted to the right. |
| 665 | Value *ConvertToScalarInfo:: |
| 666 | ConvertScalar_InsertValue(Value *SV, Value *Old, |
| 667 | uint64_t Offset, IRBuilder<> &Builder) { |
| 668 | // Convert the stored type to the actual type, shift it left to insert |
| 669 | // then 'or' into place. |
| 670 | const Type *AllocaType = Old->getType(); |
| 671 | LLVMContext &Context = Old->getContext(); |
| 672 | |
| 673 | if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) { |
| 674 | uint64_t VecSize = TD.getTypeAllocSizeInBits(VTy); |
| 675 | uint64_t ValSize = TD.getTypeAllocSizeInBits(SV->getType()); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 676 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 677 | // Changing the whole vector with memset or with an access of a different |
| 678 | // vector type? |
| 679 | if (ValSize == VecSize) |
| 680 | return Builder.CreateBitCast(SV, AllocaType, "tmp"); |
| 681 | |
| 682 | uint64_t EltSize = TD.getTypeAllocSizeInBits(VTy->getElementType()); |
| 683 | |
| 684 | // Must be an element insertion. |
| 685 | unsigned Elt = Offset/EltSize; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 686 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 687 | if (SV->getType() != VTy->getElementType()) |
| 688 | SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 689 | |
| 690 | SV = Builder.CreateInsertElement(Old, SV, |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 691 | ConstantInt::get(Type::getInt32Ty(SV->getContext()), Elt), |
| 692 | "tmp"); |
| 693 | return SV; |
| 694 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 695 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 696 | // If SV is a first-class aggregate value, insert each value recursively. |
| 697 | if (const StructType *ST = dyn_cast<StructType>(SV->getType())) { |
| 698 | const StructLayout &Layout = *TD.getStructLayout(ST); |
| 699 | for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) { |
| 700 | Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 701 | Old = ConvertScalar_InsertValue(Elt, Old, |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 702 | Offset+Layout.getElementOffsetInBits(i), |
| 703 | Builder); |
| 704 | } |
| 705 | return Old; |
| 706 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 707 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 708 | if (const ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) { |
| 709 | uint64_t EltSize = TD.getTypeAllocSizeInBits(AT->getElementType()); |
| 710 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 711 | Value *Elt = Builder.CreateExtractValue(SV, i, "tmp"); |
| 712 | Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, Builder); |
| 713 | } |
| 714 | return Old; |
| 715 | } |
| 716 | |
| 717 | // If SV is a float, convert it to the appropriate integer type. |
| 718 | // If it is a pointer, do the same. |
| 719 | unsigned SrcWidth = TD.getTypeSizeInBits(SV->getType()); |
| 720 | unsigned DestWidth = TD.getTypeSizeInBits(AllocaType); |
| 721 | unsigned SrcStoreWidth = TD.getTypeStoreSizeInBits(SV->getType()); |
| 722 | unsigned DestStoreWidth = TD.getTypeStoreSizeInBits(AllocaType); |
| 723 | if (SV->getType()->isFloatingPointTy() || SV->getType()->isVectorTy()) |
| 724 | SV = Builder.CreateBitCast(SV, |
| 725 | IntegerType::get(SV->getContext(),SrcWidth), "tmp"); |
| 726 | else if (SV->getType()->isPointerTy()) |
| 727 | SV = Builder.CreatePtrToInt(SV, TD.getIntPtrType(SV->getContext()), "tmp"); |
| 728 | |
| 729 | // Zero extend or truncate the value if needed. |
| 730 | if (SV->getType() != AllocaType) { |
| 731 | if (SV->getType()->getPrimitiveSizeInBits() < |
| 732 | AllocaType->getPrimitiveSizeInBits()) |
| 733 | SV = Builder.CreateZExt(SV, AllocaType, "tmp"); |
| 734 | else { |
| 735 | // Truncation may be needed if storing more than the alloca can hold |
| 736 | // (undefined behavior). |
| 737 | SV = Builder.CreateTrunc(SV, AllocaType, "tmp"); |
| 738 | SrcWidth = DestWidth; |
| 739 | SrcStoreWidth = DestStoreWidth; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // If this is a big-endian system and the store is narrower than the |
| 744 | // full alloca type, we need to do a shift to get the right bits. |
| 745 | int ShAmt = 0; |
| 746 | if (TD.isBigEndian()) { |
| 747 | // On big-endian machines, the lowest bit is stored at the bit offset |
| 748 | // from the pointer given by getTypeStoreSizeInBits. This matters for |
| 749 | // integers with a bitwidth that is not a multiple of 8. |
| 750 | ShAmt = DestStoreWidth - SrcStoreWidth - Offset; |
| 751 | } else { |
| 752 | ShAmt = Offset; |
| 753 | } |
| 754 | |
| 755 | // Note: we support negative bitwidths (with shr) which are not defined. |
| 756 | // We do this to support (f.e.) stores off the end of a structure where |
| 757 | // only some bits in the structure are set. |
| 758 | APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth)); |
| 759 | if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) { |
| 760 | SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), |
| 761 | ShAmt), "tmp"); |
| 762 | Mask <<= ShAmt; |
| 763 | } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) { |
| 764 | SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), |
| 765 | -ShAmt), "tmp"); |
| 766 | Mask = Mask.lshr(-ShAmt); |
| 767 | } |
| 768 | |
| 769 | // Mask out the bits we are about to insert from the old value, and or |
| 770 | // in the new bits. |
| 771 | if (SrcWidth != DestWidth) { |
| 772 | assert(DestWidth > SrcWidth); |
| 773 | Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask"); |
| 774 | SV = Builder.CreateOr(Old, SV, "ins"); |
| 775 | } |
| 776 | return SV; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | //===----------------------------------------------------------------------===// |
| 781 | // SRoA Driver |
| 782 | //===----------------------------------------------------------------------===// |
| 783 | |
| 784 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 785 | bool SROA::runOnFunction(Function &F) { |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 786 | TD = getAnalysisIfAvailable<TargetData>(); |
| 787 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 788 | bool Changed = performPromotion(F); |
Dan Gohman | e4af1cf | 2009-08-19 18:22:18 +0000 | [diff] [blame] | 789 | |
| 790 | // FIXME: ScalarRepl currently depends on TargetData more than it |
| 791 | // theoretically needs to. It should be refactored in order to support |
| 792 | // target-independent IR. Until this is done, just skip the actual |
| 793 | // scalar-replacement portion of this pass. |
| 794 | if (!TD) return Changed; |
| 795 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 796 | while (1) { |
| 797 | bool LocalChange = performScalarRepl(F); |
| 798 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 799 | Changed = true; |
| 800 | LocalChange = performPromotion(F); |
| 801 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 802 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 803 | |
| 804 | return Changed; |
| 805 | } |
| 806 | |
| 807 | |
| 808 | bool SROA::performPromotion(Function &F) { |
| 809 | std::vector<AllocaInst*> Allocas; |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 810 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 811 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 813 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 814 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 815 | bool Changed = false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 817 | while (1) { |
| 818 | Allocas.clear(); |
| 819 | |
| 820 | // Find allocas that are safe to promote, by looking at all instructions in |
| 821 | // the entry node |
| 822 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 823 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
Devang Patel | 41968df | 2007-04-25 17:15:20 +0000 | [diff] [blame] | 824 | if (isAllocaPromotable(AI)) |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 825 | Allocas.push_back(AI); |
| 826 | |
| 827 | if (Allocas.empty()) break; |
| 828 | |
Nick Lewycky | ce2c51b | 2009-11-23 03:50:44 +0000 | [diff] [blame] | 829 | PromoteMemToReg(Allocas, DT, DF); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 830 | NumPromoted += Allocas.size(); |
| 831 | Changed = true; |
| 832 | } |
| 833 | |
| 834 | return Changed; |
| 835 | } |
| 836 | |
Chris Lattner | 4cc576b | 2010-04-16 00:24:57 +0000 | [diff] [blame] | 837 | |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 838 | /// ShouldAttemptScalarRepl - Decide if an alloca is a good candidate for |
| 839 | /// SROA. It must be a struct or array type with a small number of elements. |
| 840 | static bool ShouldAttemptScalarRepl(AllocaInst *AI) { |
| 841 | const Type *T = AI->getAllocatedType(); |
| 842 | // Do not promote any struct into more than 32 separate vars. |
Chris Lattner | 963a97f | 2008-06-22 17:46:21 +0000 | [diff] [blame] | 843 | if (const StructType *ST = dyn_cast<StructType>(T)) |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 844 | return ST->getNumElements() <= 32; |
| 845 | // Arrays are much less likely to be safe for SROA; only consider |
| 846 | // them if they are very small. |
| 847 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 848 | return AT->getNumElements() <= 8; |
| 849 | return false; |
Chris Lattner | 963a97f | 2008-06-22 17:46:21 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | c447207 | 2010-04-15 23:50:26 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 853 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
| 854 | // which runs on all of the malloc/alloca instructions in the function, removing |
| 855 | // them if they are only used by getelementptr instructions. |
| 856 | // |
| 857 | bool SROA::performScalarRepl(Function &F) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 858 | std::vector<AllocaInst*> WorkList; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 859 | |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 860 | // Scan the entry basic block, adding allocas to the worklist. |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 861 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 862 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 863 | if (AllocaInst *A = dyn_cast<AllocaInst>(I)) |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 864 | WorkList.push_back(A); |
| 865 | |
| 866 | // Process the worklist |
| 867 | bool Changed = false; |
| 868 | while (!WorkList.empty()) { |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 869 | AllocaInst *AI = WorkList.back(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 870 | WorkList.pop_back(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 871 | |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 872 | // Handle dead allocas trivially. These can be formed by SROA'ing arrays |
| 873 | // with unused elements. |
| 874 | if (AI->use_empty()) { |
| 875 | AI->eraseFromParent(); |
Chris Lattner | c447207 | 2010-04-15 23:50:26 +0000 | [diff] [blame] | 876 | Changed = true; |
Chris Lattner | add2bd7 | 2006-12-22 23:14:42 +0000 | [diff] [blame] | 877 | continue; |
| 878 | } |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 879 | |
| 880 | // If this alloca is impossible for us to promote, reject it early. |
| 881 | if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized()) |
| 882 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 883 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 884 | // Check to see if this allocation is only modified by a memcpy/memmove from |
| 885 | // a constant global. If this is the case, we can change all users to use |
| 886 | // the constant global instead. This is commonly produced by the CFE by |
| 887 | // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A' |
| 888 | // is only subsequently read. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 889 | if (MemTransferInst *TheCopy = isOnlyCopiedFromConstantGlobal(AI)) { |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 890 | DEBUG(dbgs() << "Found alloca equal to global: " << *AI << '\n'); |
| 891 | DEBUG(dbgs() << " memcpy = " << *TheCopy << '\n'); |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 892 | Constant *TheSrc = cast<Constant>(TheCopy->getSource()); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 893 | AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType())); |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 894 | TheCopy->eraseFromParent(); // Don't mutate the global. |
| 895 | AI->eraseFromParent(); |
| 896 | ++NumGlobals; |
| 897 | Changed = true; |
| 898 | continue; |
| 899 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 900 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 901 | // Check to see if we can perform the core SROA transformation. We cannot |
| 902 | // transform the allocation instruction if it is an array allocation |
| 903 | // (allocations OF arrays are ok though), and an allocation of a scalar |
| 904 | // value cannot be decomposed at all. |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 905 | uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); |
Bill Wendling | 5a377cb | 2009-03-03 12:12:58 +0000 | [diff] [blame] | 906 | |
Nick Lewycky | d3aa25e | 2009-08-17 05:37:31 +0000 | [diff] [blame] | 907 | // Do not promote [0 x %struct]. |
| 908 | if (AllocaSize == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 909 | |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 910 | // Do not promote any struct whose size is too big. |
| 911 | if (AllocaSize > SRThreshold) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 912 | |
Bob Wilson | 3992feb | 2010-02-03 17:23:56 +0000 | [diff] [blame] | 913 | // If the alloca looks like a good candidate for scalar replacement, and if |
| 914 | // all its users can be transformed, then split up the aggregate into its |
| 915 | // separate elements. |
| 916 | if (ShouldAttemptScalarRepl(AI) && isSafeAllocaToScalarRepl(AI)) { |
| 917 | DoScalarReplacement(AI, WorkList); |
| 918 | Changed = true; |
| 919 | continue; |
| 920 | } |
| 921 | |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 922 | // If we can turn this aggregate value (potentially with casts) into a |
| 923 | // simple scalar value that can be mem2reg'd into a register value. |
Chris Lattner | 2e0d5f8 | 2009-01-31 02:28:54 +0000 | [diff] [blame] | 924 | // IsNotTrivial tracks whether this is something that mem2reg could have |
| 925 | // promoted itself. If so, we don't want to transform it needlessly. Note |
| 926 | // that we can't just check based on the type: the alloca may be of an i32 |
| 927 | // 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] | 928 | if (AllocaInst *NewAI = |
| 929 | ConvertToScalarInfo((unsigned)AllocaSize, *TD).TryConvert(AI)) { |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 930 | NewAI->takeName(AI); |
| 931 | AI->eraseFromParent(); |
| 932 | ++NumConverted; |
| 933 | Changed = true; |
| 934 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 935 | } |
| 936 | |
Chris Lattner | 7809ecd | 2009-02-03 01:30:09 +0000 | [diff] [blame] | 937 | // Otherwise, couldn't process this alloca. |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return Changed; |
| 941 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 942 | |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 943 | /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl |
| 944 | /// predicate, do SROA now. |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 945 | void SROA::DoScalarReplacement(AllocaInst *AI, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 946 | std::vector<AllocaInst*> &WorkList) { |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 947 | DEBUG(dbgs() << "Found inst to SROA: " << *AI << '\n'); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 948 | SmallVector<AllocaInst*, 32> ElementAllocas; |
| 949 | if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
| 950 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 951 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 952 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 953 | AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 954 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 955 | ElementAllocas.push_back(NA); |
| 956 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 957 | } |
| 958 | } else { |
| 959 | const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
| 960 | ElementAllocas.reserve(AT->getNumElements()); |
| 961 | const Type *ElTy = AT->getElementType(); |
| 962 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 963 | AllocaInst *NA = new AllocaInst(ElTy, 0, AI->getAlignment(), |
Daniel Dunbar | fe09b20 | 2009-07-30 17:37:43 +0000 | [diff] [blame] | 964 | AI->getName() + "." + Twine(i), AI); |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 965 | ElementAllocas.push_back(NA); |
| 966 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 967 | } |
| 968 | } |
| 969 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 970 | // Now that we have created the new alloca instructions, rewrite all the |
| 971 | // uses of the old alloca. |
| 972 | RewriteForScalarRepl(AI, AI, 0, ElementAllocas); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 973 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 974 | // Now erase any instructions that were made dead while rewriting the alloca. |
| 975 | DeleteDeadInstructions(); |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 976 | AI->eraseFromParent(); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 977 | |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 978 | ++NumReplaced; |
Chris Lattner | a10b29b | 2007-04-25 05:02:56 +0000 | [diff] [blame] | 979 | } |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 980 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 981 | /// DeleteDeadInstructions - Erase instructions on the DeadInstrs list, |
| 982 | /// recursively including all their operands that become trivially dead. |
| 983 | void SROA::DeleteDeadInstructions() { |
| 984 | while (!DeadInsts.empty()) { |
| 985 | Instruction *I = cast<Instruction>(DeadInsts.pop_back_val()); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 986 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 987 | for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) |
| 988 | if (Instruction *U = dyn_cast<Instruction>(*OI)) { |
| 989 | // Zero out the operand and see if it becomes trivially dead. |
| 990 | // (But, don't add allocas to the dead instruction list -- they are |
| 991 | // already on the worklist and will be deleted separately.) |
| 992 | *OI = 0; |
| 993 | if (isInstructionTriviallyDead(U) && !isa<AllocaInst>(U)) |
| 994 | DeadInsts.push_back(U); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 995 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 996 | |
| 997 | I->eraseFromParent(); |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 998 | } |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 999 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1000 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1001 | /// isSafeForScalarRepl - Check if instruction I is a safe use with regard to |
| 1002 | /// performing scalar replacement of alloca AI. The results are flagged in |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1003 | /// the Info parameter. Offset indicates the position within AI that is |
| 1004 | /// referenced by this instruction. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1005 | void SROA::isSafeForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1006 | AllocaInfo &Info) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1007 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) { |
| 1008 | Instruction *User = cast<Instruction>(*UI); |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 1009 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1010 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1011 | isSafeForScalarRepl(BC, AI, Offset, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1012 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1013 | uint64_t GEPOffset = Offset; |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1014 | isSafeGEP(GEPI, AI, GEPOffset, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1015 | if (!Info.isUnsafe) |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1016 | isSafeForScalarRepl(GEPI, AI, GEPOffset, Info); |
Gabor Greif | 19101c7 | 2010-06-28 11:20:42 +0000 | [diff] [blame] | 1017 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1018 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
| 1019 | if (Length) |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1020 | isSafeMemAccess(AI, Offset, Length->getZExtValue(), 0, |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 1021 | UI.getOperandNo() == 0, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1022 | else |
| 1023 | MarkUnsafe(Info); |
| 1024 | } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 1025 | if (!LI->isVolatile()) { |
| 1026 | const Type *LIType = LI->getType(); |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1027 | isSafeMemAccess(AI, Offset, TD->getTypeAllocSize(LIType), |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1028 | LIType, false, Info); |
| 1029 | } else |
| 1030 | MarkUnsafe(Info); |
| 1031 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1032 | // Store is ok if storing INTO the pointer, not storing the pointer |
| 1033 | if (!SI->isVolatile() && SI->getOperand(0) != I) { |
| 1034 | const Type *SIType = SI->getOperand(0)->getType(); |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1035 | isSafeMemAccess(AI, Offset, TD->getTypeAllocSize(SIType), |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1036 | SIType, true, Info); |
| 1037 | } else |
| 1038 | MarkUnsafe(Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1039 | } else { |
| 1040 | DEBUG(errs() << " Transformation preventing inst: " << *User << '\n'); |
| 1041 | MarkUnsafe(Info); |
| 1042 | } |
| 1043 | if (Info.isUnsafe) return; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1044 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1045 | } |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1046 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1047 | /// isSafeGEP - Check if a GEP instruction can be handled for scalar |
| 1048 | /// replacement. It is safe when all the indices are constant, in-bounds |
| 1049 | /// references, and when the resulting offset corresponds to an element within |
| 1050 | /// the alloca type. The results are flagged in the Info parameter. Upon |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1051 | /// return, Offset is adjusted as specified by the GEP indices. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1052 | void SROA::isSafeGEP(GetElementPtrInst *GEPI, AllocaInst *AI, |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1053 | uint64_t &Offset, AllocaInfo &Info) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1054 | gep_type_iterator GEPIt = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 1055 | if (GEPIt == E) |
| 1056 | return; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1058 | // Walk through the GEP type indices, checking the types that this indexes |
| 1059 | // into. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1060 | for (; GEPIt != E; ++GEPIt) { |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1061 | // Ignore struct elements, no extra checking needed for these. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1062 | if ((*GEPIt)->isStructTy()) |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1063 | continue; |
Matthijs Kooijman | 5fac55f | 2008-10-06 16:23:31 +0000 | [diff] [blame] | 1064 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1065 | ConstantInt *IdxVal = dyn_cast<ConstantInt>(GEPIt.getOperand()); |
| 1066 | if (!IdxVal) |
| 1067 | return MarkUnsafe(Info); |
Chris Lattner | 88e6dc8 | 2008-08-23 05:21:06 +0000 | [diff] [blame] | 1068 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1069 | |
Bob Wilson | f27a4cd | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1070 | // Compute the offset due to this GEP and check if the alloca has a |
| 1071 | // component element at that offset. |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1072 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
| 1073 | Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(), |
| 1074 | &Indices[0], Indices.size()); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1075 | if (!TypeHasComponent(AI->getAllocatedType(), Offset, 0)) |
| 1076 | MarkUnsafe(Info); |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1079 | /// isHomogeneousAggregate - Check if type T is a struct or array containing |
| 1080 | /// elements of the same type (which is always true for arrays). If so, |
| 1081 | /// return true with NumElts and EltTy set to the number of elements and the |
| 1082 | /// element type, respectively. |
| 1083 | static bool isHomogeneousAggregate(const Type *T, unsigned &NumElts, |
| 1084 | const Type *&EltTy) { |
| 1085 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) { |
| 1086 | NumElts = AT->getNumElements(); |
Bob Wilson | f0908ae | 2011-01-13 18:26:59 +0000 | [diff] [blame] | 1087 | EltTy = (NumElts == 0 ? 0 : AT->getElementType()); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1088 | return true; |
| 1089 | } |
| 1090 | if (const StructType *ST = dyn_cast<StructType>(T)) { |
| 1091 | NumElts = ST->getNumContainedTypes(); |
Bob Wilson | f0908ae | 2011-01-13 18:26:59 +0000 | [diff] [blame] | 1092 | EltTy = (NumElts == 0 ? 0 : ST->getContainedType(0)); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1093 | for (unsigned n = 1; n < NumElts; ++n) { |
| 1094 | if (ST->getContainedType(n) != EltTy) |
| 1095 | return false; |
| 1096 | } |
| 1097 | return true; |
| 1098 | } |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | /// isCompatibleAggregate - Check if T1 and T2 are either the same type or are |
| 1103 | /// "homogeneous" aggregates with the same element type and number of elements. |
| 1104 | static bool isCompatibleAggregate(const Type *T1, const Type *T2) { |
| 1105 | if (T1 == T2) |
| 1106 | return true; |
| 1107 | |
| 1108 | unsigned NumElts1, NumElts2; |
| 1109 | const Type *EltTy1, *EltTy2; |
| 1110 | if (isHomogeneousAggregate(T1, NumElts1, EltTy1) && |
| 1111 | isHomogeneousAggregate(T2, NumElts2, EltTy2) && |
| 1112 | NumElts1 == NumElts2 && |
| 1113 | EltTy1 == EltTy2) |
| 1114 | return true; |
| 1115 | |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1119 | /// isSafeMemAccess - Check if a load/store/memcpy operates on the entire AI |
| 1120 | /// alloca or has an offset and size that corresponds to a component element |
| 1121 | /// within it. The offset checked here may have been formed from a GEP with a |
| 1122 | /// pointer bitcasted to a different type. |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1123 | void SROA::isSafeMemAccess(AllocaInst *AI, uint64_t Offset, uint64_t MemSize, |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1124 | const Type *MemOpType, bool isStore, |
| 1125 | AllocaInfo &Info) { |
| 1126 | // Check if this is a load/store of the entire alloca. |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1127 | if (Offset == 0 && MemSize == TD->getTypeAllocSize(AI->getAllocatedType())) { |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1128 | // This can be safe for MemIntrinsics (where MemOpType is 0) and integer |
| 1129 | // loads/stores (which are essentially the same as the MemIntrinsics with |
| 1130 | // regard to copying padding between elements). But, if an alloca is |
| 1131 | // flagged as both a source and destination of such operations, we'll need |
| 1132 | // to check later for padding between elements. |
| 1133 | if (!MemOpType || MemOpType->isIntegerTy()) { |
| 1134 | if (isStore) |
| 1135 | Info.isMemCpyDst = true; |
| 1136 | else |
| 1137 | Info.isMemCpySrc = true; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1138 | return; |
| 1139 | } |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1140 | // This is also safe for references using a type that is compatible with |
| 1141 | // the type of the alloca, so that loads/stores can be rewritten using |
| 1142 | // insertvalue/extractvalue. |
| 1143 | if (isCompatibleAggregate(MemOpType, AI->getAllocatedType())) |
| 1144 | return; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1145 | } |
| 1146 | // Check if the offset/size correspond to a component within the alloca type. |
| 1147 | const Type *T = AI->getAllocatedType(); |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1148 | if (TypeHasComponent(T, Offset, MemSize)) |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | |
| 1151 | return MarkUnsafe(Info); |
| 1152 | } |
| 1153 | |
| 1154 | /// TypeHasComponent - Return true if T has a component type with the |
| 1155 | /// specified offset and size. If Size is zero, do not check the size. |
| 1156 | bool SROA::TypeHasComponent(const Type *T, uint64_t Offset, uint64_t Size) { |
| 1157 | const Type *EltTy; |
| 1158 | uint64_t EltSize; |
| 1159 | if (const StructType *ST = dyn_cast<StructType>(T)) { |
| 1160 | const StructLayout *Layout = TD->getStructLayout(ST); |
| 1161 | unsigned EltIdx = Layout->getElementContainingOffset(Offset); |
| 1162 | EltTy = ST->getContainedType(EltIdx); |
| 1163 | EltSize = TD->getTypeAllocSize(EltTy); |
| 1164 | Offset -= Layout->getElementOffset(EltIdx); |
| 1165 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(T)) { |
| 1166 | EltTy = AT->getElementType(); |
| 1167 | EltSize = TD->getTypeAllocSize(EltTy); |
Bob Wilson | f27a4cd | 2009-12-22 06:57:14 +0000 | [diff] [blame] | 1168 | if (Offset >= AT->getNumElements() * EltSize) |
| 1169 | return false; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1170 | Offset %= EltSize; |
| 1171 | } else { |
| 1172 | return false; |
| 1173 | } |
| 1174 | if (Offset == 0 && (Size == 0 || EltSize == Size)) |
| 1175 | return true; |
| 1176 | // Check if the component spans multiple elements. |
| 1177 | if (Offset + Size > EltSize) |
| 1178 | return false; |
| 1179 | return TypeHasComponent(EltTy, Offset, Size); |
| 1180 | } |
| 1181 | |
| 1182 | /// RewriteForScalarRepl - Alloca AI is being split into NewElts, so rewrite |
| 1183 | /// the instruction I, which references it, to use the separate elements. |
| 1184 | /// Offset indicates the position within AI that is referenced by this |
| 1185 | /// instruction. |
| 1186 | void SROA::RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset, |
| 1187 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 1188 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) { |
| 1189 | Instruction *User = cast<Instruction>(*UI); |
| 1190 | |
| 1191 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
| 1192 | RewriteBitCast(BC, AI, Offset, NewElts); |
| 1193 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
| 1194 | RewriteGEP(GEPI, AI, Offset, NewElts); |
| 1195 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) { |
| 1196 | ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength()); |
| 1197 | uint64_t MemSize = Length->getZExtValue(); |
| 1198 | if (Offset == 0 && |
| 1199 | MemSize == TD->getTypeAllocSize(AI->getAllocatedType())) |
| 1200 | RewriteMemIntrinUserOfAlloca(MI, I, AI, NewElts); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1201 | // Otherwise the intrinsic can only touch a single element and the |
| 1202 | // address operand will be updated, so nothing else needs to be done. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1203 | } else if (LoadInst *LI = dyn_cast<LoadInst>(User)) { |
| 1204 | const Type *LIType = LI->getType(); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1205 | if (isCompatibleAggregate(LIType, AI->getAllocatedType())) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1206 | // Replace: |
| 1207 | // %res = load { i32, i32 }* %alloc |
| 1208 | // with: |
| 1209 | // %load.0 = load i32* %alloc.0 |
| 1210 | // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0 |
| 1211 | // %load.1 = load i32* %alloc.1 |
| 1212 | // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1 |
| 1213 | // (Also works for arrays instead of structs) |
| 1214 | Value *Insert = UndefValue::get(LIType); |
| 1215 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1216 | Value *Load = new LoadInst(NewElts[i], "load", LI); |
| 1217 | Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI); |
| 1218 | } |
| 1219 | LI->replaceAllUsesWith(Insert); |
| 1220 | DeadInsts.push_back(LI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1221 | } else if (LIType->isIntegerTy() && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1222 | TD->getTypeAllocSize(LIType) == |
| 1223 | TD->getTypeAllocSize(AI->getAllocatedType())) { |
| 1224 | // If this is a load of the entire alloca to an integer, rewrite it. |
| 1225 | RewriteLoadUserOfWholeAlloca(LI, AI, NewElts); |
| 1226 | } |
| 1227 | } else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
| 1228 | Value *Val = SI->getOperand(0); |
| 1229 | const Type *SIType = Val->getType(); |
Bob Wilson | 704d134 | 2011-01-13 17:45:11 +0000 | [diff] [blame] | 1230 | if (isCompatibleAggregate(SIType, AI->getAllocatedType())) { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1231 | // Replace: |
| 1232 | // store { i32, i32 } %val, { i32, i32 }* %alloc |
| 1233 | // with: |
| 1234 | // %val.0 = extractvalue { i32, i32 } %val, 0 |
| 1235 | // store i32 %val.0, i32* %alloc.0 |
| 1236 | // %val.1 = extractvalue { i32, i32 } %val, 1 |
| 1237 | // store i32 %val.1, i32* %alloc.1 |
| 1238 | // (Also works for arrays instead of structs) |
| 1239 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1240 | Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI); |
| 1241 | new StoreInst(Extract, NewElts[i], SI); |
| 1242 | } |
| 1243 | DeadInsts.push_back(SI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1244 | } else if (SIType->isIntegerTy() && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1245 | TD->getTypeAllocSize(SIType) == |
| 1246 | TD->getTypeAllocSize(AI->getAllocatedType())) { |
| 1247 | // If this is a store of the entire alloca from an integer, rewrite it. |
| 1248 | RewriteStoreUserOfWholeAlloca(SI, AI, NewElts); |
| 1249 | } |
| 1250 | } |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1254 | /// RewriteBitCast - Update a bitcast reference to the alloca being replaced |
| 1255 | /// and recursively continue updating all of its uses. |
| 1256 | void SROA::RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset, |
| 1257 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 1258 | RewriteForScalarRepl(BC, AI, Offset, NewElts); |
| 1259 | if (BC->getOperand(0) != AI) |
| 1260 | return; |
Bob Wilson | 39c88a6 | 2009-12-17 18:34:24 +0000 | [diff] [blame] | 1261 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1262 | // The bitcast references the original alloca. Replace its uses with |
| 1263 | // references to the first new element alloca. |
| 1264 | Instruction *Val = NewElts[0]; |
| 1265 | if (Val->getType() != BC->getDestTy()) { |
| 1266 | Val = new BitCastInst(Val, BC->getDestTy(), "", BC); |
| 1267 | Val->takeName(BC); |
Daniel Dunbar | fca55c8 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 1268 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1269 | BC->replaceAllUsesWith(Val); |
| 1270 | DeadInsts.push_back(BC); |
Daniel Dunbar | fca55c8 | 2009-12-16 10:56:17 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1273 | /// FindElementAndOffset - Return the index of the element containing Offset |
| 1274 | /// within the specified type, which must be either a struct or an array. |
| 1275 | /// 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] | 1276 | /// element. IdxTy is set to the type of the index result to be used in a |
| 1277 | /// GEP instruction. |
| 1278 | uint64_t SROA::FindElementAndOffset(const Type *&T, uint64_t &Offset, |
| 1279 | const Type *&IdxTy) { |
| 1280 | uint64_t Idx = 0; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1281 | if (const StructType *ST = dyn_cast<StructType>(T)) { |
| 1282 | const StructLayout *Layout = TD->getStructLayout(ST); |
| 1283 | Idx = Layout->getElementContainingOffset(Offset); |
| 1284 | T = ST->getContainedType(Idx); |
| 1285 | Offset -= Layout->getElementOffset(Idx); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1286 | IdxTy = Type::getInt32Ty(T->getContext()); |
| 1287 | return Idx; |
Chris Lattner | a59adc4 | 2009-12-14 05:11:02 +0000 | [diff] [blame] | 1288 | } |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1289 | const ArrayType *AT = cast<ArrayType>(T); |
| 1290 | T = AT->getElementType(); |
| 1291 | uint64_t EltSize = TD->getTypeAllocSize(T); |
| 1292 | Idx = Offset / EltSize; |
| 1293 | Offset -= Idx * EltSize; |
| 1294 | IdxTy = Type::getInt64Ty(T->getContext()); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1295 | return Idx; |
| 1296 | } |
| 1297 | |
| 1298 | /// RewriteGEP - Check if this GEP instruction moves the pointer across |
| 1299 | /// elements of the alloca that are being split apart, and if so, rewrite |
| 1300 | /// the GEP to be relative to the new element. |
| 1301 | void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset, |
| 1302 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 1303 | uint64_t OldOffset = Offset; |
| 1304 | SmallVector<Value*, 8> Indices(GEPI->op_begin() + 1, GEPI->op_end()); |
| 1305 | Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(), |
| 1306 | &Indices[0], Indices.size()); |
| 1307 | |
| 1308 | RewriteForScalarRepl(GEPI, AI, Offset, NewElts); |
| 1309 | |
| 1310 | const Type *T = AI->getAllocatedType(); |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1311 | const Type *IdxTy; |
| 1312 | uint64_t OldIdx = FindElementAndOffset(T, OldOffset, IdxTy); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1313 | if (GEPI->getOperand(0) == AI) |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1314 | OldIdx = ~0ULL; // Force the GEP to be rewritten. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1315 | |
| 1316 | T = AI->getAllocatedType(); |
| 1317 | uint64_t EltOffset = Offset; |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1318 | uint64_t Idx = FindElementAndOffset(T, EltOffset, IdxTy); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1319 | |
| 1320 | // If this GEP does not move the pointer across elements of the alloca |
| 1321 | // being split, then it does not needs to be rewritten. |
| 1322 | if (Idx == OldIdx) |
| 1323 | return; |
| 1324 | |
| 1325 | const Type *i32Ty = Type::getInt32Ty(AI->getContext()); |
| 1326 | SmallVector<Value*, 8> NewArgs; |
| 1327 | NewArgs.push_back(Constant::getNullValue(i32Ty)); |
| 1328 | while (EltOffset != 0) { |
Bob Wilson | e88728d | 2009-12-19 06:53:17 +0000 | [diff] [blame] | 1329 | uint64_t EltIdx = FindElementAndOffset(T, EltOffset, IdxTy); |
| 1330 | NewArgs.push_back(ConstantInt::get(IdxTy, EltIdx)); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1331 | } |
| 1332 | Instruction *Val = NewElts[Idx]; |
| 1333 | if (NewArgs.size() > 1) { |
| 1334 | Val = GetElementPtrInst::CreateInBounds(Val, NewArgs.begin(), |
| 1335 | NewArgs.end(), "", GEPI); |
| 1336 | Val->takeName(GEPI); |
| 1337 | } |
| 1338 | if (Val->getType() != GEPI->getType()) |
Benjamin Kramer | 2d64ca0 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 1339 | Val = new BitCastInst(Val, GEPI->getType(), Val->getName(), GEPI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1340 | GEPI->replaceAllUsesWith(Val); |
| 1341 | DeadInsts.push_back(GEPI); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI. |
| 1345 | /// Rewrite it to copy or set the elements of the scalarized memory. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1346 | void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1347 | AllocaInst *AI, |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1348 | SmallVector<AllocaInst*, 32> &NewElts) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1349 | // If this is a memcpy/memmove, construct the other pointer as the |
Chris Lattner | 88fe1ad | 2009-03-04 19:23:25 +0000 | [diff] [blame] | 1350 | // appropriate type. The "Other" pointer is the pointer that goes to memory |
| 1351 | // that doesn't have anything to do with the alloca that we are promoting. For |
| 1352 | // memset, this Value* stays null. |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1353 | Value *OtherPtr = 0; |
Chris Lattner | dfe964c | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 1354 | unsigned MemAlignment = MI->getAlignment(); |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1355 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1356 | if (Inst == MTI->getRawDest()) |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1357 | OtherPtr = MTI->getRawSource(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1358 | else { |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1359 | assert(Inst == MTI->getRawSource()); |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1360 | OtherPtr = MTI->getRawDest(); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1361 | } |
| 1362 | } |
Bob Wilson | 78c50b8 | 2009-12-08 18:22:03 +0000 | [diff] [blame] | 1363 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1364 | // If there is an other pointer, we want to convert it to the same pointer |
| 1365 | // type as AI has, so we can GEP through it safely. |
| 1366 | if (OtherPtr) { |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 1367 | unsigned AddrSpace = |
| 1368 | cast<PointerType>(OtherPtr->getType())->getAddressSpace(); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Remove bitcasts and all-zero GEPs from OtherPtr. This is an |
| 1371 | // optimization, but it's also required to detect the corner case where |
| 1372 | // both pointer operands are referencing the same memory, and where |
| 1373 | // OtherPtr may be a bitcast or GEP that currently being rewritten. (This |
| 1374 | // function is only called for mem intrinsics that access the whole |
| 1375 | // aggregate, so non-zero GEPs are not an issue here.) |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 1376 | OtherPtr = OtherPtr->stripPointerCasts(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1377 | |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 1378 | // Copying the alloca to itself is a no-op: just delete it. |
| 1379 | if (OtherPtr == AI || OtherPtr == NewElts[0]) { |
| 1380 | // This code will run twice for a no-op memcpy -- once for each operand. |
| 1381 | // Put only one reference to MI on the DeadInsts list. |
| 1382 | for (SmallVector<Value*, 32>::const_iterator I = DeadInsts.begin(), |
| 1383 | E = DeadInsts.end(); I != E; ++I) |
| 1384 | if (*I == MI) return; |
| 1385 | DeadInsts.push_back(MI); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1386 | return; |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 1387 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1388 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1389 | // If the pointer is not the right type, insert a bitcast to the right |
| 1390 | // type. |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 1391 | const Type *NewTy = |
| 1392 | PointerType::get(AI->getType()->getElementType(), AddrSpace); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1393 | |
Chris Lattner | 0238f8c | 2010-07-08 00:27:05 +0000 | [diff] [blame] | 1394 | if (OtherPtr->getType() != NewTy) |
| 1395 | OtherPtr = new BitCastInst(OtherPtr, NewTy, OtherPtr->getName(), MI); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1396 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1397 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1398 | // Process each element of the aggregate. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1399 | bool SROADest = MI->getRawDest() == Inst; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1400 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1401 | Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext())); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1402 | |
| 1403 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1404 | // If this is a memcpy/memmove, emit a GEP of the other element address. |
| 1405 | Value *OtherElt = 0; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1406 | unsigned OtherEltAlign = MemAlignment; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1407 | |
Bob Wilson | a756b1d | 2010-01-19 04:32:48 +0000 | [diff] [blame] | 1408 | if (OtherPtr) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1409 | Value *Idx[2] = { Zero, |
| 1410 | ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) }; |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1411 | OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx, Idx + 2, |
Benjamin Kramer | 2d64ca0 | 2010-01-27 19:46:52 +0000 | [diff] [blame] | 1412 | OtherPtr->getName()+"."+Twine(i), |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1413 | MI); |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1414 | uint64_t EltOffset; |
| 1415 | const PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType()); |
Chris Lattner | d55c1c1 | 2010-04-16 01:05:38 +0000 | [diff] [blame] | 1416 | const Type *OtherTy = OtherPtrTy->getElementType(); |
| 1417 | if (const StructType *ST = dyn_cast<StructType>(OtherTy)) { |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1418 | EltOffset = TD->getStructLayout(ST)->getElementOffset(i); |
| 1419 | } else { |
Chris Lattner | d55c1c1 | 2010-04-16 01:05:38 +0000 | [diff] [blame] | 1420 | const Type *EltTy = cast<SequentialType>(OtherTy)->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1421 | EltOffset = TD->getTypeAllocSize(EltTy)*i; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1422 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1423 | |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1424 | // The alignment of the other pointer is the guaranteed alignment of the |
| 1425 | // element, which is affected by both the known alignment of the whole |
| 1426 | // mem intrinsic and the alignment of the element. If the alignment of |
| 1427 | // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the |
| 1428 | // known alignment is just 4 bytes. |
| 1429 | OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset); |
Chris Lattner | c14d3ca | 2007-03-08 06:36:54 +0000 | [diff] [blame] | 1430 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1431 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1432 | Value *EltPtr = NewElts[i]; |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1433 | const Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1434 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1435 | // If we got down to a scalar, insert a load or store as appropriate. |
| 1436 | if (EltTy->isSingleValueType()) { |
Chris Lattner | 3ce5e88 | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 1437 | if (isa<MemTransferInst>(MI)) { |
Chris Lattner | 1541e0f | 2009-03-04 19:20:50 +0000 | [diff] [blame] | 1438 | if (SROADest) { |
| 1439 | // From Other to Alloca. |
| 1440 | Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI); |
| 1441 | new StoreInst(Elt, EltPtr, MI); |
| 1442 | } else { |
| 1443 | // From Alloca to Other. |
| 1444 | Value *Elt = new LoadInst(EltPtr, "tmp", MI); |
| 1445 | new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI); |
| 1446 | } |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1447 | continue; |
| 1448 | } |
| 1449 | assert(isa<MemSetInst>(MI)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1450 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1451 | // If the stored element is zero (common case), just store a null |
| 1452 | // constant. |
| 1453 | Constant *StoreVal; |
Gabor Greif | 6f14c8c | 2010-06-30 09:16:16 +0000 | [diff] [blame] | 1454 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getArgOperand(1))) { |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1455 | if (CI->isZero()) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1456 | StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0> |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1457 | } else { |
| 1458 | // If EltTy is a vector type, get the element type. |
Dan Gohman | 44118f0 | 2009-06-16 00:20:26 +0000 | [diff] [blame] | 1459 | const Type *ValTy = EltTy->getScalarType(); |
| 1460 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1461 | // Construct an integer with the right value. |
| 1462 | unsigned EltSize = TD->getTypeSizeInBits(ValTy); |
| 1463 | APInt OneVal(EltSize, CI->getZExtValue()); |
| 1464 | APInt TotalVal(OneVal); |
| 1465 | // Set each byte. |
| 1466 | for (unsigned i = 0; 8*i < EltSize; ++i) { |
| 1467 | TotalVal = TotalVal.shl(8); |
| 1468 | TotalVal |= OneVal; |
| 1469 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1470 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1471 | // Convert the integer value to the appropriate type. |
Chris Lattner | d55c1c1 | 2010-04-16 01:05:38 +0000 | [diff] [blame] | 1472 | StoreVal = ConstantInt::get(CI->getContext(), TotalVal); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1473 | if (ValTy->isPointerTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1474 | StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1475 | else if (ValTy->isFloatingPointTy()) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 1476 | StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1477 | assert(StoreVal->getType() == ValTy && "Type mismatch!"); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1478 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1479 | // If the requested value was a vector constant, create it. |
| 1480 | if (EltTy != ValTy) { |
| 1481 | unsigned NumElts = cast<VectorType>(ValTy)->getNumElements(); |
| 1482 | SmallVector<Constant*, 16> Elts(NumElts, StoreVal); |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1483 | StoreVal = ConstantVector::get(&Elts[0], NumElts); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | new StoreInst(StoreVal, EltPtr, MI); |
| 1487 | continue; |
| 1488 | } |
| 1489 | // Otherwise, if we're storing a byte variable, use a memset call for |
| 1490 | // this element. |
| 1491 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1492 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1493 | unsigned EltSize = TD->getTypeAllocSize(EltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1494 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1495 | IRBuilder<> Builder(MI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1496 | |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1497 | // Finally, insert the meminst for this element. |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1498 | if (isa<MemSetInst>(MI)) { |
| 1499 | Builder.CreateMemSet(EltPtr, MI->getArgOperand(1), EltSize, |
| 1500 | MI->isVolatile()); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1501 | } else { |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1502 | assert(isa<MemTransferInst>(MI)); |
| 1503 | Value *Dst = SROADest ? EltPtr : OtherElt; // Dest ptr |
| 1504 | Value *Src = SROADest ? OtherElt : EltPtr; // Src ptr |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1505 | |
Chris Lattner | 61db1f5 | 2010-12-26 22:57:41 +0000 | [diff] [blame] | 1506 | if (isa<MemCpyInst>(MI)) |
| 1507 | Builder.CreateMemCpy(Dst, Src, EltSize, OtherEltAlign,MI->isVolatile()); |
| 1508 | else |
| 1509 | Builder.CreateMemMove(Dst, Src, EltSize,OtherEltAlign,MI->isVolatile()); |
Chris Lattner | d93afec | 2009-01-07 07:18:45 +0000 | [diff] [blame] | 1510 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 1511 | } |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1512 | DeadInsts.push_back(MI); |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 1513 | } |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1514 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 1515 | /// RewriteStoreUserOfWholeAlloca - We found a store of an integer that |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1516 | /// overwrites the entire allocation. Extract out the pieces of the stored |
| 1517 | /// integer and store them individually. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1518 | void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI, |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1519 | SmallVector<AllocaInst*, 32> &NewElts){ |
| 1520 | // Extract each element out of the integer according to its structure offset |
| 1521 | // and store the element value to the individual alloca. |
| 1522 | Value *SrcVal = SI->getOperand(0); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1523 | const Type *AllocaEltTy = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1524 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1525 | |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 1526 | // Handle tail padding by extending the operand |
| 1527 | if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1528 | SrcVal = new ZExtInst(SrcVal, |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1529 | IntegerType::get(SI->getContext(), AllocaSizeBits), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1530 | "", SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1531 | |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 1532 | DEBUG(dbgs() << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << '\n' << *SI |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 1533 | << '\n'); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1534 | |
| 1535 | // There are two forms here: AI could be an array or struct. Both cases |
| 1536 | // have different ways to compute the element offset. |
| 1537 | if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
| 1538 | const StructLayout *Layout = TD->getStructLayout(EltSTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1539 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1540 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1541 | // Get the number of bits to shift SrcVal to get the value. |
| 1542 | const Type *FieldTy = EltSTy->getElementType(i); |
| 1543 | uint64_t Shift = Layout->getElementOffsetInBits(i); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1544 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1545 | if (TD->isBigEndian()) |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1546 | Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1547 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1548 | Value *EltVal = SrcVal; |
| 1549 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1550 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1551 | EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, |
| 1552 | "sroa.store.elt", SI); |
| 1553 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1554 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1555 | // Truncate down to an integer of the right size. |
| 1556 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1557 | |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1558 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 1559 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1560 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1561 | if (FieldSizeBits != AllocaSizeBits) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1562 | EltVal = new TruncInst(EltVal, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1563 | IntegerType::get(SI->getContext(), FieldSizeBits), |
| 1564 | "", SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1565 | Value *DestField = NewElts[i]; |
| 1566 | if (EltVal->getType() == FieldTy) { |
| 1567 | // Storing to an integer field of this size, just do it. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1568 | } else if (FieldTy->isFloatingPointTy() || FieldTy->isVectorTy()) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1569 | // Bitcast to the right element type (for fp/vector values). |
| 1570 | EltVal = new BitCastInst(EltVal, FieldTy, "", SI); |
| 1571 | } else { |
| 1572 | // Otherwise, bitcast the dest pointer (for aggregates). |
| 1573 | DestField = new BitCastInst(DestField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1574 | PointerType::getUnqual(EltVal->getType()), |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1575 | "", SI); |
| 1576 | } |
| 1577 | new StoreInst(EltVal, DestField, SI); |
| 1578 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1579 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1580 | } else { |
| 1581 | const ArrayType *ATy = cast<ArrayType>(AllocaEltTy); |
| 1582 | const Type *ArrayEltTy = ATy->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1583 | uint64_t ElementOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1584 | uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy); |
| 1585 | |
| 1586 | uint64_t Shift; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1587 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1588 | if (TD->isBigEndian()) |
| 1589 | Shift = AllocaSizeBits-ElementOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1590 | else |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1591 | Shift = 0; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1592 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1593 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1594 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 1595 | if (ElementSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1596 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1597 | Value *EltVal = SrcVal; |
| 1598 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1599 | Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1600 | EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal, |
| 1601 | "sroa.store.elt", SI); |
| 1602 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1603 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1604 | // Truncate down to an integer of the right size. |
| 1605 | if (ElementSizeBits != AllocaSizeBits) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1606 | EltVal = new TruncInst(EltVal, |
| 1607 | IntegerType::get(SI->getContext(), |
| 1608 | ElementSizeBits), "", SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1609 | Value *DestField = NewElts[i]; |
| 1610 | if (EltVal->getType() == ArrayEltTy) { |
| 1611 | // Storing to an integer field of this size, just do it. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1612 | } else if (ArrayEltTy->isFloatingPointTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1613 | ArrayEltTy->isVectorTy()) { |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1614 | // Bitcast to the right element type (for fp/vector values). |
| 1615 | EltVal = new BitCastInst(EltVal, ArrayEltTy, "", SI); |
| 1616 | } else { |
| 1617 | // Otherwise, bitcast the dest pointer (for aggregates). |
| 1618 | DestField = new BitCastInst(DestField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1619 | PointerType::getUnqual(EltVal->getType()), |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1620 | "", SI); |
| 1621 | } |
| 1622 | new StoreInst(EltVal, DestField, SI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1623 | |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1624 | if (TD->isBigEndian()) |
| 1625 | Shift -= ElementOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1626 | else |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1627 | Shift += ElementOffset; |
| 1628 | } |
| 1629 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1630 | |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1631 | DeadInsts.push_back(SI); |
Chris Lattner | d2fa781 | 2009-01-07 08:11:13 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Bob Wilson | 39fdd69 | 2009-12-04 21:57:37 +0000 | [diff] [blame] | 1634 | /// RewriteLoadUserOfWholeAlloca - We found a load of the entire allocation to |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1635 | /// an integer. Load the individual pieces to form the aggregate value. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1636 | void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocaInst *AI, |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1637 | SmallVector<AllocaInst*, 32> &NewElts) { |
| 1638 | // Extract each element out of the NewElts according to its structure offset |
| 1639 | // and form the result value. |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1640 | const Type *AllocaEltTy = AI->getAllocatedType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1641 | uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1642 | |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 1643 | DEBUG(dbgs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI |
Nick Lewycky | 5913625 | 2009-09-15 07:08:25 +0000 | [diff] [blame] | 1644 | << '\n'); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1645 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1646 | // There are two forms here: AI could be an array or struct. Both cases |
| 1647 | // have different ways to compute the element offset. |
| 1648 | const StructLayout *Layout = 0; |
| 1649 | uint64_t ArrayEltBitOffset = 0; |
| 1650 | if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) { |
| 1651 | Layout = TD->getStructLayout(EltSTy); |
| 1652 | } else { |
| 1653 | const Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1654 | ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1655 | } |
| 1656 | |
| 1657 | Value *ResultVal = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1658 | Constant::getNullValue(IntegerType::get(LI->getContext(), AllocaSizeBits)); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1659 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1660 | for (unsigned i = 0, e = NewElts.size(); i != e; ++i) { |
| 1661 | // Load the value from the alloca. If the NewElt is an aggregate, cast |
| 1662 | // the pointer to an integer of the same size before doing the load. |
| 1663 | Value *SrcField = NewElts[i]; |
| 1664 | const Type *FieldTy = |
| 1665 | cast<PointerType>(SrcField->getType())->getElementType(); |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1666 | uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1667 | |
Chris Lattner | 583dd60 | 2009-01-09 18:18:43 +0000 | [diff] [blame] | 1668 | // Ignore zero sized fields like {}, they obviously contain no data. |
| 1669 | if (FieldSizeBits == 0) continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1670 | |
| 1671 | const IntegerType *FieldIntTy = IntegerType::get(LI->getContext(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1672 | FieldSizeBits); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1673 | if (!FieldTy->isIntegerTy() && !FieldTy->isFloatingPointTy() && |
| 1674 | !FieldTy->isVectorTy()) |
Owen Anderson | fa5cbd6 | 2009-07-03 19:42:02 +0000 | [diff] [blame] | 1675 | SrcField = new BitCastInst(SrcField, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1676 | PointerType::getUnqual(FieldIntTy), |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1677 | "", LI); |
| 1678 | SrcField = new LoadInst(SrcField, "sroa.load.elt", LI); |
| 1679 | |
| 1680 | // If SrcField is a fp or vector of the right size but that isn't an |
| 1681 | // integer type, bitcast to an integer so we can shift it. |
| 1682 | if (SrcField->getType() != FieldIntTy) |
| 1683 | SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI); |
| 1684 | |
| 1685 | // Zero extend the field to be the same size as the final alloca so that |
| 1686 | // we can shift and insert it. |
| 1687 | if (SrcField->getType() != ResultVal->getType()) |
| 1688 | SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1689 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1690 | // Determine the number of bits to shift SrcField. |
| 1691 | uint64_t Shift; |
| 1692 | if (Layout) // Struct case. |
| 1693 | Shift = Layout->getElementOffsetInBits(i); |
| 1694 | else // Array case. |
| 1695 | Shift = i*ArrayEltBitOffset; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1696 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1697 | if (TD->isBigEndian()) |
| 1698 | Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth(); |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1699 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1700 | if (Shift) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1701 | Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1702 | SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI); |
| 1703 | } |
| 1704 | |
Chris Lattner | 1495247 | 2010-06-27 07:58:26 +0000 | [diff] [blame] | 1705 | // Don't create an 'or x, 0' on the first iteration. |
| 1706 | if (!isa<Constant>(ResultVal) || |
| 1707 | !cast<Constant>(ResultVal)->isNullValue()) |
| 1708 | ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI); |
| 1709 | else |
| 1710 | ResultVal = SrcField; |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1711 | } |
Eli Friedman | 41b33f4 | 2009-06-01 09:14:32 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Handle tail padding by truncating the result |
| 1714 | if (TD->getTypeSizeInBits(LI->getType()) != AllocaSizeBits) |
| 1715 | ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI); |
| 1716 | |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1717 | LI->replaceAllUsesWith(ResultVal); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1718 | DeadInsts.push_back(LI); |
Chris Lattner | 5ffe6ac | 2009-01-08 05:42:05 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1721 | /// HasPadding - Return true if the specified type has any structure or |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 1722 | /// alignment padding in between the elements that would be split apart |
| 1723 | /// by SROA; return false otherwise. |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1724 | static bool HasPadding(const Type *Ty, const TargetData &TD) { |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 1725 | if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 1726 | Ty = ATy->getElementType(); |
| 1727 | return TD.getTypeSizeInBits(Ty) != TD.getTypeAllocSizeInBits(Ty); |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1728 | } |
Bob Wilson | 694a10e | 2011-01-13 17:45:08 +0000 | [diff] [blame] | 1729 | |
| 1730 | // SROA currently handles only Arrays and Structs. |
| 1731 | const StructType *STy = cast<StructType>(Ty); |
| 1732 | const StructLayout *SL = TD.getStructLayout(STy); |
| 1733 | unsigned PrevFieldBitOffset = 0; |
| 1734 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1735 | unsigned FieldBitOffset = SL->getElementOffsetInBits(i); |
| 1736 | |
| 1737 | // Check to see if there is any padding between this element and the |
| 1738 | // previous one. |
| 1739 | if (i) { |
| 1740 | unsigned PrevFieldEnd = |
| 1741 | PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1)); |
| 1742 | if (PrevFieldEnd < FieldBitOffset) |
| 1743 | return true; |
| 1744 | } |
| 1745 | PrevFieldBitOffset = FieldBitOffset; |
| 1746 | } |
| 1747 | // Check for tail padding. |
| 1748 | if (unsigned EltCount = STy->getNumElements()) { |
| 1749 | unsigned PrevFieldEnd = PrevFieldBitOffset + |
| 1750 | TD.getTypeSizeInBits(STy->getElementType(EltCount-1)); |
| 1751 | if (PrevFieldEnd < SL->getSizeInBits()) |
| 1752 | return true; |
| 1753 | } |
| 1754 | return false; |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1755 | } |
Chris Lattner | 372dda8 | 2007-03-05 07:52:57 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1757 | /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of |
| 1758 | /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, |
| 1759 | /// or 1 if safe after canonicalization has been performed. |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 1760 | bool SROA::isSafeAllocaToScalarRepl(AllocaInst *AI) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1761 | // Loop over the use list of the alloca. We can only transform it if all of |
| 1762 | // the users are safe to transform. |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1763 | AllocaInfo Info; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1764 | |
Bob Wilson | 3c3af5d | 2009-12-21 18:39:47 +0000 | [diff] [blame] | 1765 | isSafeForScalarRepl(AI, AI, 0, Info); |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1766 | if (Info.isUnsafe) { |
David Greene | 504c7d8 | 2010-01-05 01:27:09 +0000 | [diff] [blame] | 1767 | DEBUG(dbgs() << "Cannot transform: " << *AI << '\n'); |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 1768 | return false; |
Chris Lattner | f5990ed | 2004-11-14 04:24:28 +0000 | [diff] [blame] | 1769 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1770 | |
Chris Lattner | 39a1c04 | 2007-05-30 06:11:23 +0000 | [diff] [blame] | 1771 | // Okay, we know all the users are promotable. If the aggregate is a memcpy |
| 1772 | // source and destination, we have to be careful. In particular, the memcpy |
| 1773 | // could be moving around elements that live in structure padding of the LLVM |
| 1774 | // types, but may actually be used. In these cases, we refuse to promote the |
| 1775 | // struct. |
| 1776 | if (Info.isMemCpySrc && Info.isMemCpyDst && |
Bob Wilson | b742def | 2009-12-18 20:14:40 +0000 | [diff] [blame] | 1777 | HasPadding(AI->getAllocatedType(), *TD)) |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 1778 | return false; |
Duncan Sands | 3cb3650 | 2007-11-04 14:43:57 +0000 | [diff] [blame] | 1779 | |
Victor Hernandez | 6c146ee | 2010-01-21 23:05:53 +0000 | [diff] [blame] | 1780 | return true; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 1781 | } |
Chris Lattner | a188894 | 2005-12-12 07:19:13 +0000 | [diff] [blame] | 1782 | |
Chris Lattner | 800de31 | 2008-02-29 07:03:13 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1784 | |
| 1785 | /// PointsToConstantGlobal - Return true if V (possibly indirectly) points to |
| 1786 | /// some part of a constant global variable. This intentionally only accepts |
| 1787 | /// constant expressions because we don't can't rewrite arbitrary instructions. |
| 1788 | static bool PointsToConstantGlobal(Value *V) { |
| 1789 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 1790 | return GV->isConstant(); |
| 1791 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1792 | if (CE->getOpcode() == Instruction::BitCast || |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1793 | CE->getOpcode() == Instruction::GetElementPtr) |
| 1794 | return PointsToConstantGlobal(CE->getOperand(0)); |
| 1795 | return false; |
| 1796 | } |
| 1797 | |
| 1798 | /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived) |
| 1799 | /// pointer to an alloca. Ignore any reads of the pointer, return false if we |
| 1800 | /// see any stores or other unknown uses. If we see pointer arithmetic, keep |
| 1801 | /// track of whether it moves the pointer (with isOffset) but otherwise traverse |
| 1802 | /// 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] | 1803 | /// 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] | 1804 | /// can optimize this. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1805 | static bool isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy, |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1806 | bool isOffset) { |
| 1807 | 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] | 1808 | User *U = cast<Instruction>(*UI); |
| 1809 | |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 1810 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
Chris Lattner | 6e733d3 | 2009-01-28 20:16:43 +0000 | [diff] [blame] | 1811 | // Ignore non-volatile loads, they are always ok. |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 1812 | if (LI->isVolatile()) return false; |
| 1813 | continue; |
| 1814 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1815 | |
Gabor Greif | 8a8a435 | 2010-04-06 19:32:30 +0000 | [diff] [blame] | 1816 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(U)) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1817 | // If uses of the bitcast are ok, we are ok. |
| 1818 | if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset)) |
| 1819 | return false; |
| 1820 | continue; |
| 1821 | } |
Gabor Greif | 8a8a435 | 2010-04-06 19:32:30 +0000 | [diff] [blame] | 1822 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) { |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1823 | // If the GEP has all zero indices, it doesn't offset the pointer. If it |
| 1824 | // doesn't, it does. |
| 1825 | if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy, |
| 1826 | isOffset || !GEP->hasAllZeroIndices())) |
| 1827 | return false; |
| 1828 | continue; |
| 1829 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1830 | |
Chris Lattner | 6248065 | 2010-11-18 06:41:51 +0000 | [diff] [blame] | 1831 | if (CallSite CS = U) { |
| 1832 | // If this is a readonly/readnone call site, then we know it is just a |
| 1833 | // load and we can ignore it. |
Chris Lattner | a9be1df | 2010-11-18 06:26:49 +0000 | [diff] [blame] | 1834 | if (CS.onlyReadsMemory()) |
| 1835 | continue; |
Nick Lewycky | 081f800 | 2010-11-24 22:04:20 +0000 | [diff] [blame] | 1836 | |
| 1837 | // If this is the function being called then we treat it like a load and |
| 1838 | // ignore it. |
| 1839 | if (CS.isCallee(UI)) |
| 1840 | continue; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1841 | |
Chris Lattner | 6248065 | 2010-11-18 06:41:51 +0000 | [diff] [blame] | 1842 | // If this is being passed as a byval argument, the caller is making a |
| 1843 | // copy, so it is only a read of the alloca. |
| 1844 | unsigned ArgNo = CS.getArgumentNo(UI); |
| 1845 | if (CS.paramHasAttr(ArgNo+1, Attribute::ByVal)) |
| 1846 | continue; |
| 1847 | } |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1848 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1849 | // If this is isn't our memcpy/memmove, reject it as something we can't |
| 1850 | // handle. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1851 | MemTransferInst *MI = dyn_cast<MemTransferInst>(U); |
| 1852 | if (MI == 0) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1853 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1854 | |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 1855 | // 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] | 1856 | // ignore it since it is a load (unless the transfer is volatile). |
Chris Lattner | 2e61849 | 2010-11-18 06:20:47 +0000 | [diff] [blame] | 1857 | if (UI.getOperandNo() == 1) { |
| 1858 | if (MI->isVolatile()) return false; |
| 1859 | continue; |
| 1860 | } |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1861 | |
| 1862 | // If we already have seen a copy, reject the second one. |
| 1863 | if (TheCopy) return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1864 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1865 | // If the pointer has been offset from the start of the alloca, we can't |
| 1866 | // safely handle this. |
| 1867 | if (isOffset) return false; |
| 1868 | |
| 1869 | // 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] | 1870 | if (UI.getOperandNo() != 0) return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1871 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1872 | // 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] | 1873 | if (!PointsToConstantGlobal(MI->getSource())) |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1874 | return false; |
Bob Wilson | 6974302 | 2011-01-13 20:59:44 +0000 | [diff] [blame^] | 1875 | |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1876 | // Otherwise, the transform is safe. Remember the copy instruction. |
| 1877 | TheCopy = MI; |
| 1878 | } |
| 1879 | return true; |
| 1880 | } |
| 1881 | |
| 1882 | /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only |
| 1883 | /// modified by a copy from a constant global. If we can prove this, we can |
| 1884 | /// replace any uses of the alloca with uses of the global directly. |
Chris Lattner | 31d8010 | 2010-04-15 21:59:20 +0000 | [diff] [blame] | 1885 | MemTransferInst *SROA::isOnlyCopiedFromConstantGlobal(AllocaInst *AI) { |
| 1886 | MemTransferInst *TheCopy = 0; |
Chris Lattner | 79b3bd3 | 2007-04-25 06:40:51 +0000 | [diff] [blame] | 1887 | if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false)) |
| 1888 | return TheCopy; |
| 1889 | return 0; |
| 1890 | } |