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