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