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