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