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