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