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