1 //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
20 //===----------------------------------------------------------------------===//
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/LLVMContext.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Analysis/Dominators.h"
33 #include "llvm/Target/TargetData.h"
34 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
35 #include "llvm/Transforms/Utils/Local.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/GetElementPtrTypeIterator.h"
39 #include "llvm/Support/IRBuilder.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/ADT/SmallVector.h"
43 #include "llvm/ADT/Statistic.h"
44 #include "llvm/ADT/StringExtras.h"
47 STATISTIC(NumReplaced, "Number of allocas broken up");
48 STATISTIC(NumPromoted, "Number of allocas promoted");
49 STATISTIC(NumConverted, "Number of aggregates converted to scalar");
50 STATISTIC(NumGlobals, "Number of allocas copied from constant global");
53 struct VISIBILITY_HIDDEN SROA : public FunctionPass {
54 static char ID; // Pass identification, replacement for typeid
55 explicit SROA(signed T = -1) : FunctionPass(&ID) {
62 bool runOnFunction(Function &F);
64 bool performScalarRepl(Function &F);
65 bool performPromotion(Function &F);
67 // getAnalysisUsage - This pass does not require any passes, but we know it
68 // will not alter the CFG, so say so.
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.addRequired<DominatorTree>();
71 AU.addRequired<DominanceFrontier>();
72 AU.addRequired<TargetData>();
79 /// AllocaInfo - When analyzing uses of an alloca instruction, this captures
80 /// information about the uses. All these fields are initialized to false
81 /// and set to true when something is learned.
83 /// isUnsafe - This is set to true if the alloca cannot be SROA'd.
86 /// needsCleanup - This is set to true if there is some use of the alloca
87 /// that requires cleanup.
88 bool needsCleanup : 1;
90 /// isMemCpySrc - This is true if this aggregate is memcpy'd from.
93 /// isMemCpyDst - This is true if this aggregate is memcpy'd into.
97 : isUnsafe(false), needsCleanup(false),
98 isMemCpySrc(false), isMemCpyDst(false) {}
101 unsigned SRThreshold;
103 void MarkUnsafe(AllocaInfo &I) { I.isUnsafe = true; }
105 int isSafeAllocaToScalarRepl(AllocationInst *AI);
107 void isSafeUseOfAllocation(Instruction *User, AllocationInst *AI,
109 void isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI,
111 void isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI,
112 unsigned OpNo, AllocaInfo &Info);
113 void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocationInst *AI,
116 void DoScalarReplacement(AllocationInst *AI,
117 std::vector<AllocationInst*> &WorkList);
118 void CleanupGEP(GetElementPtrInst *GEP);
119 void CleanupAllocaUsers(AllocationInst *AI);
120 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
122 void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI,
123 SmallVector<AllocaInst*, 32> &NewElts);
125 void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
127 SmallVector<AllocaInst*, 32> &NewElts);
128 void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocationInst *AI,
129 SmallVector<AllocaInst*, 32> &NewElts);
130 void RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
131 SmallVector<AllocaInst*, 32> &NewElts);
133 bool CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
134 bool &SawVec, uint64_t Offset, unsigned AllocaSize);
135 void ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset);
136 Value *ConvertScalar_ExtractValue(Value *NV, const Type *ToType,
137 uint64_t Offset, IRBuilder<> &Builder);
138 Value *ConvertScalar_InsertValue(Value *StoredVal, Value *ExistingVal,
139 uint64_t Offset, IRBuilder<> &Builder);
140 static Instruction *isOnlyCopiedFromConstantGlobal(AllocationInst *AI);
145 static RegisterPass<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
147 // Public interface to the ScalarReplAggregates pass
148 FunctionPass *llvm::createScalarReplAggregatesPass(signed int Threshold) {
149 return new SROA(Threshold);
153 bool SROA::runOnFunction(Function &F) {
154 TD = &getAnalysis<TargetData>();
156 bool Changed = performPromotion(F);
158 bool LocalChange = performScalarRepl(F);
159 if (!LocalChange) break; // No need to repromote if no scalarrepl
161 LocalChange = performPromotion(F);
162 if (!LocalChange) break; // No need to re-scalarrepl if no promotion
169 bool SROA::performPromotion(Function &F) {
170 std::vector<AllocaInst*> Allocas;
171 DominatorTree &DT = getAnalysis<DominatorTree>();
172 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
174 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
176 bool Changed = false;
181 // Find allocas that are safe to promote, by looking at all instructions in
183 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
184 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
185 if (isAllocaPromotable(AI))
186 Allocas.push_back(AI);
188 if (Allocas.empty()) break;
190 PromoteMemToReg(Allocas, DT, DF, Context);
191 NumPromoted += Allocas.size();
198 /// getNumSAElements - Return the number of elements in the specific struct or
200 static uint64_t getNumSAElements(const Type *T) {
201 if (const StructType *ST = dyn_cast<StructType>(T))
202 return ST->getNumElements();
203 return cast<ArrayType>(T)->getNumElements();
206 // performScalarRepl - This algorithm is a simple worklist driven algorithm,
207 // which runs on all of the malloc/alloca instructions in the function, removing
208 // them if they are only used by getelementptr instructions.
210 bool SROA::performScalarRepl(Function &F) {
211 std::vector<AllocationInst*> WorkList;
213 // Scan the entry basic block, adding any alloca's and mallocs to the worklist
214 BasicBlock &BB = F.getEntryBlock();
215 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
216 if (AllocationInst *A = dyn_cast<AllocationInst>(I))
217 WorkList.push_back(A);
219 // Process the worklist
220 bool Changed = false;
221 while (!WorkList.empty()) {
222 AllocationInst *AI = WorkList.back();
225 // Handle dead allocas trivially. These can be formed by SROA'ing arrays
226 // with unused elements.
227 if (AI->use_empty()) {
228 AI->eraseFromParent();
232 // If this alloca is impossible for us to promote, reject it early.
233 if (AI->isArrayAllocation() || !AI->getAllocatedType()->isSized())
236 // Check to see if this allocation is only modified by a memcpy/memmove from
237 // a constant global. If this is the case, we can change all users to use
238 // the constant global instead. This is commonly produced by the CFE by
239 // constructs like "void foo() { int A[] = {1,2,3,4,5,6,7,8,9...}; }" if 'A'
240 // is only subsequently read.
241 if (Instruction *TheCopy = isOnlyCopiedFromConstantGlobal(AI)) {
242 DOUT << "Found alloca equal to global: " << *AI;
243 DOUT << " memcpy = " << *TheCopy;
244 Constant *TheSrc = cast<Constant>(TheCopy->getOperand(2));
245 AI->replaceAllUsesWith(
246 Context->getConstantExprBitCast(TheSrc, AI->getType()));
247 TheCopy->eraseFromParent(); // Don't mutate the global.
248 AI->eraseFromParent();
254 // Check to see if we can perform the core SROA transformation. We cannot
255 // transform the allocation instruction if it is an array allocation
256 // (allocations OF arrays are ok though), and an allocation of a scalar
257 // value cannot be decomposed at all.
258 uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType());
260 // Do not promote any struct whose size is too big.
261 if (AllocaSize > SRThreshold) continue;
263 if ((isa<StructType>(AI->getAllocatedType()) ||
264 isa<ArrayType>(AI->getAllocatedType())) &&
265 // Do not promote any struct into more than "32" separate vars.
266 getNumSAElements(AI->getAllocatedType()) <= SRThreshold/4) {
267 // Check that all of the users of the allocation are capable of being
269 switch (isSafeAllocaToScalarRepl(AI)) {
270 default: llvm_unreachable("Unexpected value!");
271 case 0: // Not safe to scalar replace.
273 case 1: // Safe, but requires cleanup/canonicalizations first
274 CleanupAllocaUsers(AI);
276 case 3: // Safe to scalar replace.
277 DoScalarReplacement(AI, WorkList);
283 // If we can turn this aggregate value (potentially with casts) into a
284 // simple scalar value that can be mem2reg'd into a register value.
285 // IsNotTrivial tracks whether this is something that mem2reg could have
286 // promoted itself. If so, we don't want to transform it needlessly. Note
287 // that we can't just check based on the type: the alloca may be of an i32
288 // but that has pointer arithmetic to set byte 3 of it or something.
289 bool IsNotTrivial = false;
290 const Type *VectorTy = 0;
291 bool HadAVector = false;
292 if (CanConvertToScalar(AI, IsNotTrivial, VectorTy, HadAVector,
293 0, unsigned(AllocaSize)) && IsNotTrivial) {
295 // If we were able to find a vector type that can handle this with
296 // insert/extract elements, and if there was at least one use that had
297 // a vector type, promote this to a vector. We don't want to promote
298 // random stuff that doesn't use vectors (e.g. <9 x double>) because then
299 // we just get a lot of insert/extracts. If at least one vector is
300 // involved, then we probably really do have a union of vector/array.
301 if (VectorTy && isa<VectorType>(VectorTy) && HadAVector) {
302 DOUT << "CONVERT TO VECTOR: " << *AI << " TYPE = " << *VectorTy <<"\n";
304 // Create and insert the vector alloca.
305 NewAI = new AllocaInst(*Context, VectorTy, 0, "",
306 AI->getParent()->begin());
307 ConvertUsesToScalar(AI, NewAI, 0);
309 DOUT << "CONVERT TO SCALAR INTEGER: " << *AI << "\n";
311 // Create and insert the integer alloca.
312 const Type *NewTy = Context->getIntegerType(AllocaSize*8);
313 NewAI = new AllocaInst(*Context, NewTy, 0, "",
314 AI->getParent()->begin());
315 ConvertUsesToScalar(AI, NewAI, 0);
318 AI->eraseFromParent();
324 // Otherwise, couldn't process this alloca.
330 /// DoScalarReplacement - This alloca satisfied the isSafeAllocaToScalarRepl
331 /// predicate, do SROA now.
332 void SROA::DoScalarReplacement(AllocationInst *AI,
333 std::vector<AllocationInst*> &WorkList) {
334 DOUT << "Found inst to SROA: " << *AI;
335 SmallVector<AllocaInst*, 32> ElementAllocas;
336 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
337 ElementAllocas.reserve(ST->getNumContainedTypes());
338 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
339 AllocaInst *NA = new AllocaInst(*Context,
340 ST->getContainedType(i), 0,
342 AI->getName() + "." + utostr(i), AI);
343 ElementAllocas.push_back(NA);
344 WorkList.push_back(NA); // Add to worklist for recursive processing
347 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
348 ElementAllocas.reserve(AT->getNumElements());
349 const Type *ElTy = AT->getElementType();
350 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
351 AllocaInst *NA = new AllocaInst(*Context, ElTy, 0, AI->getAlignment(),
352 AI->getName() + "." + utostr(i), AI);
353 ElementAllocas.push_back(NA);
354 WorkList.push_back(NA); // Add to worklist for recursive processing
358 // Now that we have created the alloca instructions that we want to use,
359 // expand the getelementptr instructions to use them.
361 while (!AI->use_empty()) {
362 Instruction *User = cast<Instruction>(AI->use_back());
363 if (BitCastInst *BCInst = dyn_cast<BitCastInst>(User)) {
364 RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas);
365 BCInst->eraseFromParent();
370 // %res = load { i32, i32 }* %alloc
372 // %load.0 = load i32* %alloc.0
373 // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0
374 // %load.1 = load i32* %alloc.1
375 // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
376 // (Also works for arrays instead of structs)
377 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
378 Value *Insert = Context->getUndef(LI->getType());
379 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) {
380 Value *Load = new LoadInst(ElementAllocas[i], "load", LI);
381 Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI);
383 LI->replaceAllUsesWith(Insert);
384 LI->eraseFromParent();
389 // store { i32, i32 } %val, { i32, i32 }* %alloc
391 // %val.0 = extractvalue { i32, i32 } %val, 0
392 // store i32 %val.0, i32* %alloc.0
393 // %val.1 = extractvalue { i32, i32 } %val, 1
394 // store i32 %val.1, i32* %alloc.1
395 // (Also works for arrays instead of structs)
396 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
397 Value *Val = SI->getOperand(0);
398 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) {
399 Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI);
400 new StoreInst(Extract, ElementAllocas[i], SI);
402 SI->eraseFromParent();
406 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User);
407 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
409 (unsigned)cast<ConstantInt>(GEPI->getOperand(2))->getZExtValue();
411 assert(Idx < ElementAllocas.size() && "Index out of range?");
412 AllocaInst *AllocaToUse = ElementAllocas[Idx];
415 if (GEPI->getNumOperands() == 3) {
416 // Do not insert a new getelementptr instruction with zero indices, only
417 // to have it optimized out later.
418 RepValue = AllocaToUse;
420 // We are indexing deeply into the structure, so we still need a
421 // getelement ptr instruction to finish the indexing. This may be
422 // expanded itself once the worklist is rerun.
424 SmallVector<Value*, 8> NewArgs;
425 NewArgs.push_back(Context->getNullValue(Type::Int32Ty));
426 NewArgs.append(GEPI->op_begin()+3, GEPI->op_end());
427 RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(),
428 NewArgs.end(), "", GEPI);
429 RepValue->takeName(GEPI);
432 // If this GEP is to the start of the aggregate, check for memcpys.
433 if (Idx == 0 && GEPI->hasAllZeroIndices())
434 RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas);
436 // Move all of the users over to the new GEP.
437 GEPI->replaceAllUsesWith(RepValue);
438 // Delete the old GEP
439 GEPI->eraseFromParent();
442 // Finally, delete the Alloca instruction
443 AI->eraseFromParent();
448 /// isSafeElementUse - Check to see if this use is an allowed use for a
449 /// getelementptr instruction of an array aggregate allocation. isFirstElt
450 /// indicates whether Ptr is known to the start of the aggregate.
452 void SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocationInst *AI,
454 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
456 Instruction *User = cast<Instruction>(*I);
457 switch (User->getOpcode()) {
458 case Instruction::Load: break;
459 case Instruction::Store:
460 // Store is ok if storing INTO the pointer, not storing the pointer
461 if (User->getOperand(0) == Ptr) return MarkUnsafe(Info);
463 case Instruction::GetElementPtr: {
464 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
465 bool AreAllZeroIndices = isFirstElt;
466 if (GEP->getNumOperands() > 1) {
467 if (!isa<ConstantInt>(GEP->getOperand(1)) ||
468 !cast<ConstantInt>(GEP->getOperand(1))->isZero())
469 // Using pointer arithmetic to navigate the array.
470 return MarkUnsafe(Info);
472 if (AreAllZeroIndices)
473 AreAllZeroIndices = GEP->hasAllZeroIndices();
475 isSafeElementUse(GEP, AreAllZeroIndices, AI, Info);
476 if (Info.isUnsafe) return;
479 case Instruction::BitCast:
481 isSafeUseOfBitCastedAllocation(cast<BitCastInst>(User), AI, Info);
482 if (Info.isUnsafe) return;
485 DOUT << " Transformation preventing inst: " << *User;
486 return MarkUnsafe(Info);
487 case Instruction::Call:
488 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) {
490 isSafeMemIntrinsicOnAllocation(MI, AI, I.getOperandNo(), Info);
491 if (Info.isUnsafe) return;
495 DOUT << " Transformation preventing inst: " << *User;
496 return MarkUnsafe(Info);
498 DOUT << " Transformation preventing inst: " << *User;
499 return MarkUnsafe(Info);
502 return; // All users look ok :)
505 /// AllUsersAreLoads - Return true if all users of this value are loads.
506 static bool AllUsersAreLoads(Value *Ptr) {
507 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
509 if (cast<Instruction>(*I)->getOpcode() != Instruction::Load)
514 /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
515 /// aggregate allocation.
517 void SROA::isSafeUseOfAllocation(Instruction *User, AllocationInst *AI,
519 if (BitCastInst *C = dyn_cast<BitCastInst>(User))
520 return isSafeUseOfBitCastedAllocation(C, AI, Info);
522 if (LoadInst *LI = dyn_cast<LoadInst>(User))
523 if (!LI->isVolatile())
524 return;// Loads (returning a first class aggregrate) are always rewritable
526 if (StoreInst *SI = dyn_cast<StoreInst>(User))
527 if (!SI->isVolatile() && SI->getOperand(0) != AI)
528 return;// Store is ok if storing INTO the pointer, not storing the pointer
530 GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User);
532 return MarkUnsafe(Info);
534 gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI);
536 // The GEP is not safe to transform if not of the form "GEP <ptr>, 0, <cst>".
538 I.getOperand() != Context->getNullValue(I.getOperand()->getType())) {
539 return MarkUnsafe(Info);
543 if (I == E) return MarkUnsafe(Info); // ran out of GEP indices??
545 bool IsAllZeroIndices = true;
547 // If the first index is a non-constant index into an array, see if we can
548 // handle it as a special case.
549 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
550 if (!isa<ConstantInt>(I.getOperand())) {
551 IsAllZeroIndices = 0;
552 uint64_t NumElements = AT->getNumElements();
554 // If this is an array index and the index is not constant, we cannot
555 // promote... that is unless the array has exactly one or two elements in
556 // it, in which case we CAN promote it, but we have to canonicalize this
557 // out if this is the only problem.
558 if ((NumElements == 1 || NumElements == 2) &&
559 AllUsersAreLoads(GEPI)) {
560 Info.needsCleanup = true;
561 return; // Canonicalization required!
563 return MarkUnsafe(Info);
567 // Walk through the GEP type indices, checking the types that this indexes
569 for (; I != E; ++I) {
570 // Ignore struct elements, no extra checking needed for these.
571 if (isa<StructType>(*I))
574 ConstantInt *IdxVal = dyn_cast<ConstantInt>(I.getOperand());
575 if (!IdxVal) return MarkUnsafe(Info);
577 // Are all indices still zero?
578 IsAllZeroIndices &= IdxVal->isZero();
580 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
581 // This GEP indexes an array. Verify that this is an in-range constant
582 // integer. Specifically, consider A[0][i]. We cannot know that the user
583 // isn't doing invalid things like allowing i to index an out-of-range
584 // subscript that accesses A[1]. Because of this, we have to reject SROA
585 // of any accesses into structs where any of the components are variables.
586 if (IdxVal->getZExtValue() >= AT->getNumElements())
587 return MarkUnsafe(Info);
588 } else if (const VectorType *VT = dyn_cast<VectorType>(*I)) {
589 if (IdxVal->getZExtValue() >= VT->getNumElements())
590 return MarkUnsafe(Info);
594 // If there are any non-simple uses of this getelementptr, make sure to reject
596 return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info);
599 /// isSafeMemIntrinsicOnAllocation - Return true if the specified memory
600 /// intrinsic can be promoted by SROA. At this point, we know that the operand
601 /// of the memintrinsic is a pointer to the beginning of the allocation.
602 void SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocationInst *AI,
603 unsigned OpNo, AllocaInfo &Info) {
604 // If not constant length, give up.
605 ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
606 if (!Length) return MarkUnsafe(Info);
608 // If not the whole aggregate, give up.
609 if (Length->getZExtValue() !=
610 TD->getTypeAllocSize(AI->getType()->getElementType()))
611 return MarkUnsafe(Info);
613 // We only know about memcpy/memset/memmove.
614 if (!isa<MemIntrinsic>(MI))
615 return MarkUnsafe(Info);
617 // Otherwise, we can transform it. Determine whether this is a memcpy/set
618 // into or out of the aggregate.
620 Info.isMemCpyDst = true;
623 Info.isMemCpySrc = true;
627 /// isSafeUseOfBitCastedAllocation - Return true if all users of this bitcast
629 void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocationInst *AI,
631 for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end();
633 if (BitCastInst *BCU = dyn_cast<BitCastInst>(UI)) {
634 isSafeUseOfBitCastedAllocation(BCU, AI, Info);
635 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(UI)) {
636 isSafeMemIntrinsicOnAllocation(MI, AI, UI.getOperandNo(), Info);
637 } else if (StoreInst *SI = dyn_cast<StoreInst>(UI)) {
638 if (SI->isVolatile())
639 return MarkUnsafe(Info);
641 // If storing the entire alloca in one chunk through a bitcasted pointer
642 // to integer, we can transform it. This happens (for example) when you
643 // cast a {i32,i32}* to i64* and store through it. This is similar to the
644 // memcpy case and occurs in various "byval" cases and emulated memcpys.
645 if (isa<IntegerType>(SI->getOperand(0)->getType()) &&
646 TD->getTypeAllocSize(SI->getOperand(0)->getType()) ==
647 TD->getTypeAllocSize(AI->getType()->getElementType())) {
648 Info.isMemCpyDst = true;
651 return MarkUnsafe(Info);
652 } else if (LoadInst *LI = dyn_cast<LoadInst>(UI)) {
653 if (LI->isVolatile())
654 return MarkUnsafe(Info);
656 // If loading the entire alloca in one chunk through a bitcasted pointer
657 // to integer, we can transform it. This happens (for example) when you
658 // cast a {i32,i32}* to i64* and load through it. This is similar to the
659 // memcpy case and occurs in various "byval" cases and emulated memcpys.
660 if (isa<IntegerType>(LI->getType()) &&
661 TD->getTypeAllocSize(LI->getType()) ==
662 TD->getTypeAllocSize(AI->getType()->getElementType())) {
663 Info.isMemCpySrc = true;
666 return MarkUnsafe(Info);
667 } else if (isa<DbgInfoIntrinsic>(UI)) {
668 // If one user is DbgInfoIntrinsic then check if all users are
669 // DbgInfoIntrinsics.
670 if (OnlyUsedByDbgInfoIntrinsics(BC)) {
671 Info.needsCleanup = true;
678 return MarkUnsafe(Info);
680 if (Info.isUnsafe) return;
684 /// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes
685 /// to its first element. Transform users of the cast to use the new values
687 void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocationInst *AI,
688 SmallVector<AllocaInst*, 32> &NewElts) {
689 Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end();
691 Instruction *User = cast<Instruction>(*UI++);
692 if (BitCastInst *BCU = dyn_cast<BitCastInst>(User)) {
693 RewriteBitCastUserOfAlloca(BCU, AI, NewElts);
694 if (BCU->use_empty()) BCU->eraseFromParent();
698 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(User)) {
699 // This must be memcpy/memmove/memset of the entire aggregate.
700 // Split into one per element.
701 RewriteMemIntrinUserOfAlloca(MI, BCInst, AI, NewElts);
705 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
706 // If this is a store of the entire alloca from an integer, rewrite it.
707 RewriteStoreUserOfWholeAlloca(SI, AI, NewElts);
711 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
712 // If this is a load of the entire alloca to an integer, rewrite it.
713 RewriteLoadUserOfWholeAlloca(LI, AI, NewElts);
717 // Otherwise it must be some other user of a gep of the first pointer. Just
718 // leave these alone.
723 /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI.
724 /// Rewrite it to copy or set the elements of the scalarized memory.
725 void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
727 SmallVector<AllocaInst*, 32> &NewElts) {
729 // If this is a memcpy/memmove, construct the other pointer as the
730 // appropriate type. The "Other" pointer is the pointer that goes to memory
731 // that doesn't have anything to do with the alloca that we are promoting. For
732 // memset, this Value* stays null.
734 unsigned MemAlignment = MI->getAlignment();
735 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { // memmove/memcopy
736 if (BCInst == MTI->getRawDest())
737 OtherPtr = MTI->getRawSource();
739 assert(BCInst == MTI->getRawSource());
740 OtherPtr = MTI->getRawDest();
744 // If there is an other pointer, we want to convert it to the same pointer
745 // type as AI has, so we can GEP through it safely.
747 // It is likely that OtherPtr is a bitcast, if so, remove it.
748 if (BitCastInst *BC = dyn_cast<BitCastInst>(OtherPtr))
749 OtherPtr = BC->getOperand(0);
750 // All zero GEPs are effectively bitcasts.
751 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(OtherPtr))
752 if (GEP->hasAllZeroIndices())
753 OtherPtr = GEP->getOperand(0);
755 if (ConstantExpr *BCE = dyn_cast<ConstantExpr>(OtherPtr))
756 if (BCE->getOpcode() == Instruction::BitCast)
757 OtherPtr = BCE->getOperand(0);
759 // If the pointer is not the right type, insert a bitcast to the right
761 if (OtherPtr->getType() != AI->getType())
762 OtherPtr = new BitCastInst(OtherPtr, AI->getType(), OtherPtr->getName(),
766 // Process each element of the aggregate.
767 Value *TheFn = MI->getOperand(0);
768 const Type *BytePtrTy = MI->getRawDest()->getType();
769 bool SROADest = MI->getRawDest() == BCInst;
771 Constant *Zero = Context->getNullValue(Type::Int32Ty);
773 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
774 // If this is a memcpy/memmove, emit a GEP of the other element address.
776 unsigned OtherEltAlign = MemAlignment;
779 Value *Idx[2] = { Zero, Context->getConstantInt(Type::Int32Ty, i) };
780 OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2,
781 OtherPtr->getNameStr()+"."+utostr(i),
784 const PointerType *OtherPtrTy = cast<PointerType>(OtherPtr->getType());
785 if (const StructType *ST =
786 dyn_cast<StructType>(OtherPtrTy->getElementType())) {
787 EltOffset = TD->getStructLayout(ST)->getElementOffset(i);
790 cast<SequentialType>(OtherPtr->getType())->getElementType();
791 EltOffset = TD->getTypeAllocSize(EltTy)*i;
794 // The alignment of the other pointer is the guaranteed alignment of the
795 // element, which is affected by both the known alignment of the whole
796 // mem intrinsic and the alignment of the element. If the alignment of
797 // the memcpy (f.e.) is 32 but the element is at a 4-byte offset, then the
798 // known alignment is just 4 bytes.
799 OtherEltAlign = (unsigned)MinAlign(OtherEltAlign, EltOffset);
802 Value *EltPtr = NewElts[i];
803 const Type *EltTy = cast<PointerType>(EltPtr->getType())->getElementType();
805 // If we got down to a scalar, insert a load or store as appropriate.
806 if (EltTy->isSingleValueType()) {
807 if (isa<MemTransferInst>(MI)) {
809 // From Other to Alloca.
810 Value *Elt = new LoadInst(OtherElt, "tmp", false, OtherEltAlign, MI);
811 new StoreInst(Elt, EltPtr, MI);
813 // From Alloca to Other.
814 Value *Elt = new LoadInst(EltPtr, "tmp", MI);
815 new StoreInst(Elt, OtherElt, false, OtherEltAlign, MI);
819 assert(isa<MemSetInst>(MI));
821 // If the stored element is zero (common case), just store a null
824 if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) {
826 StoreVal = Context->getNullValue(EltTy); // 0.0, null, 0, <0,0>
828 // If EltTy is a vector type, get the element type.
829 const Type *ValTy = EltTy->getScalarType();
831 // Construct an integer with the right value.
832 unsigned EltSize = TD->getTypeSizeInBits(ValTy);
833 APInt OneVal(EltSize, CI->getZExtValue());
834 APInt TotalVal(OneVal);
836 for (unsigned i = 0; 8*i < EltSize; ++i) {
837 TotalVal = TotalVal.shl(8);
841 // Convert the integer value to the appropriate type.
842 StoreVal = Context->getConstantInt(TotalVal);
843 if (isa<PointerType>(ValTy))
844 StoreVal = Context->getConstantExprIntToPtr(StoreVal, ValTy);
845 else if (ValTy->isFloatingPoint())
846 StoreVal = Context->getConstantExprBitCast(StoreVal, ValTy);
847 assert(StoreVal->getType() == ValTy && "Type mismatch!");
849 // If the requested value was a vector constant, create it.
850 if (EltTy != ValTy) {
851 unsigned NumElts = cast<VectorType>(ValTy)->getNumElements();
852 SmallVector<Constant*, 16> Elts(NumElts, StoreVal);
853 StoreVal = Context->getConstantVector(&Elts[0], NumElts);
856 new StoreInst(StoreVal, EltPtr, MI);
859 // Otherwise, if we're storing a byte variable, use a memset call for
863 // Cast the element pointer to BytePtrTy.
864 if (EltPtr->getType() != BytePtrTy)
865 EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getNameStr(), MI);
867 // Cast the other pointer (if we have one) to BytePtrTy.
868 if (OtherElt && OtherElt->getType() != BytePtrTy)
869 OtherElt = new BitCastInst(OtherElt, BytePtrTy,OtherElt->getNameStr(),
872 unsigned EltSize = TD->getTypeAllocSize(EltTy);
874 // Finally, insert the meminst for this element.
875 if (isa<MemTransferInst>(MI)) {
877 SROADest ? EltPtr : OtherElt, // Dest ptr
878 SROADest ? OtherElt : EltPtr, // Src ptr
879 Context->getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
880 Context->getConstantInt(Type::Int32Ty, OtherEltAlign) // Align
882 CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
884 assert(isa<MemSetInst>(MI));
886 EltPtr, MI->getOperand(2), // Dest, Value,
887 Context->getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
890 CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
893 MI->eraseFromParent();
896 /// RewriteStoreUserOfWholeAlloca - We found an store of an integer that
897 /// overwrites the entire allocation. Extract out the pieces of the stored
898 /// integer and store them individually.
899 void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
901 SmallVector<AllocaInst*, 32> &NewElts){
902 // Extract each element out of the integer according to its structure offset
903 // and store the element value to the individual alloca.
904 Value *SrcVal = SI->getOperand(0);
905 const Type *AllocaEltTy = AI->getType()->getElementType();
906 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
908 // If this isn't a store of an integer to the whole alloca, it may be a store
909 // to the first element. Just ignore the store in this case and normal SROA
911 if (!isa<IntegerType>(SrcVal->getType()) ||
912 TD->getTypeAllocSizeInBits(SrcVal->getType()) != AllocaSizeBits)
914 // Handle tail padding by extending the operand
915 if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits)
916 SrcVal = new ZExtInst(SrcVal,
917 Context->getIntegerType(AllocaSizeBits), "", SI);
919 DOUT << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << *SI;
921 // There are two forms here: AI could be an array or struct. Both cases
922 // have different ways to compute the element offset.
923 if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) {
924 const StructLayout *Layout = TD->getStructLayout(EltSTy);
926 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
927 // Get the number of bits to shift SrcVal to get the value.
928 const Type *FieldTy = EltSTy->getElementType(i);
929 uint64_t Shift = Layout->getElementOffsetInBits(i);
931 if (TD->isBigEndian())
932 Shift = AllocaSizeBits-Shift-TD->getTypeAllocSizeInBits(FieldTy);
934 Value *EltVal = SrcVal;
936 Value *ShiftVal = Context->getConstantInt(EltVal->getType(), Shift);
937 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
938 "sroa.store.elt", SI);
941 // Truncate down to an integer of the right size.
942 uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy);
944 // Ignore zero sized fields like {}, they obviously contain no data.
945 if (FieldSizeBits == 0) continue;
947 if (FieldSizeBits != AllocaSizeBits)
948 EltVal = new TruncInst(EltVal,
949 Context->getIntegerType(FieldSizeBits), "", SI);
950 Value *DestField = NewElts[i];
951 if (EltVal->getType() == FieldTy) {
952 // Storing to an integer field of this size, just do it.
953 } else if (FieldTy->isFloatingPoint() || isa<VectorType>(FieldTy)) {
954 // Bitcast to the right element type (for fp/vector values).
955 EltVal = new BitCastInst(EltVal, FieldTy, "", SI);
957 // Otherwise, bitcast the dest pointer (for aggregates).
958 DestField = new BitCastInst(DestField,
959 Context->getPointerTypeUnqual(EltVal->getType()),
962 new StoreInst(EltVal, DestField, SI);
966 const ArrayType *ATy = cast<ArrayType>(AllocaEltTy);
967 const Type *ArrayEltTy = ATy->getElementType();
968 uint64_t ElementOffset = TD->getTypeAllocSizeInBits(ArrayEltTy);
969 uint64_t ElementSizeBits = TD->getTypeSizeInBits(ArrayEltTy);
973 if (TD->isBigEndian())
974 Shift = AllocaSizeBits-ElementOffset;
978 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
979 // Ignore zero sized fields like {}, they obviously contain no data.
980 if (ElementSizeBits == 0) continue;
982 Value *EltVal = SrcVal;
984 Value *ShiftVal = Context->getConstantInt(EltVal->getType(), Shift);
985 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
986 "sroa.store.elt", SI);
989 // Truncate down to an integer of the right size.
990 if (ElementSizeBits != AllocaSizeBits)
991 EltVal = new TruncInst(EltVal,
992 Context->getIntegerType(ElementSizeBits),"",SI);
993 Value *DestField = NewElts[i];
994 if (EltVal->getType() == ArrayEltTy) {
995 // Storing to an integer field of this size, just do it.
996 } else if (ArrayEltTy->isFloatingPoint() || isa<VectorType>(ArrayEltTy)) {
997 // Bitcast to the right element type (for fp/vector values).
998 EltVal = new BitCastInst(EltVal, ArrayEltTy, "", SI);
1000 // Otherwise, bitcast the dest pointer (for aggregates).
1001 DestField = new BitCastInst(DestField,
1002 Context->getPointerTypeUnqual(EltVal->getType()),
1005 new StoreInst(EltVal, DestField, SI);
1007 if (TD->isBigEndian())
1008 Shift -= ElementOffset;
1010 Shift += ElementOffset;
1014 SI->eraseFromParent();
1017 /// RewriteLoadUserOfWholeAlloca - We found an load of the entire allocation to
1018 /// an integer. Load the individual pieces to form the aggregate value.
1019 void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
1020 SmallVector<AllocaInst*, 32> &NewElts) {
1021 // Extract each element out of the NewElts according to its structure offset
1022 // and form the result value.
1023 const Type *AllocaEltTy = AI->getType()->getElementType();
1024 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
1026 // If this isn't a load of the whole alloca to an integer, it may be a load
1027 // of the first element. Just ignore the load in this case and normal SROA
1029 if (!isa<IntegerType>(LI->getType()) ||
1030 TD->getTypeAllocSizeInBits(LI->getType()) != AllocaSizeBits)
1033 DOUT << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << *LI;
1035 // There are two forms here: AI could be an array or struct. Both cases
1036 // have different ways to compute the element offset.
1037 const StructLayout *Layout = 0;
1038 uint64_t ArrayEltBitOffset = 0;
1039 if (const StructType *EltSTy = dyn_cast<StructType>(AllocaEltTy)) {
1040 Layout = TD->getStructLayout(EltSTy);
1042 const Type *ArrayEltTy = cast<ArrayType>(AllocaEltTy)->getElementType();
1043 ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy);
1047 Context->getNullValue(Context->getIntegerType(AllocaSizeBits));
1049 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
1050 // Load the value from the alloca. If the NewElt is an aggregate, cast
1051 // the pointer to an integer of the same size before doing the load.
1052 Value *SrcField = NewElts[i];
1053 const Type *FieldTy =
1054 cast<PointerType>(SrcField->getType())->getElementType();
1055 uint64_t FieldSizeBits = TD->getTypeSizeInBits(FieldTy);
1057 // Ignore zero sized fields like {}, they obviously contain no data.
1058 if (FieldSizeBits == 0) continue;
1060 const IntegerType *FieldIntTy = Context->getIntegerType(FieldSizeBits);
1061 if (!isa<IntegerType>(FieldTy) && !FieldTy->isFloatingPoint() &&
1062 !isa<VectorType>(FieldTy))
1063 SrcField = new BitCastInst(SrcField,
1064 Context->getPointerTypeUnqual(FieldIntTy),
1066 SrcField = new LoadInst(SrcField, "sroa.load.elt", LI);
1068 // If SrcField is a fp or vector of the right size but that isn't an
1069 // integer type, bitcast to an integer so we can shift it.
1070 if (SrcField->getType() != FieldIntTy)
1071 SrcField = new BitCastInst(SrcField, FieldIntTy, "", LI);
1073 // Zero extend the field to be the same size as the final alloca so that
1074 // we can shift and insert it.
1075 if (SrcField->getType() != ResultVal->getType())
1076 SrcField = new ZExtInst(SrcField, ResultVal->getType(), "", LI);
1078 // Determine the number of bits to shift SrcField.
1080 if (Layout) // Struct case.
1081 Shift = Layout->getElementOffsetInBits(i);
1083 Shift = i*ArrayEltBitOffset;
1085 if (TD->isBigEndian())
1086 Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth();
1089 Value *ShiftVal = Context->getConstantInt(SrcField->getType(), Shift);
1090 SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI);
1093 ResultVal = BinaryOperator::CreateOr(SrcField, ResultVal, "", LI);
1096 // Handle tail padding by truncating the result
1097 if (TD->getTypeSizeInBits(LI->getType()) != AllocaSizeBits)
1098 ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI);
1100 LI->replaceAllUsesWith(ResultVal);
1101 LI->eraseFromParent();
1105 /// HasPadding - Return true if the specified type has any structure or
1106 /// alignment padding, false otherwise.
1107 static bool HasPadding(const Type *Ty, const TargetData &TD) {
1108 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1109 const StructLayout *SL = TD.getStructLayout(STy);
1110 unsigned PrevFieldBitOffset = 0;
1111 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1112 unsigned FieldBitOffset = SL->getElementOffsetInBits(i);
1114 // Padding in sub-elements?
1115 if (HasPadding(STy->getElementType(i), TD))
1118 // Check to see if there is any padding between this element and the
1121 unsigned PrevFieldEnd =
1122 PrevFieldBitOffset+TD.getTypeSizeInBits(STy->getElementType(i-1));
1123 if (PrevFieldEnd < FieldBitOffset)
1127 PrevFieldBitOffset = FieldBitOffset;
1130 // Check for tail padding.
1131 if (unsigned EltCount = STy->getNumElements()) {
1132 unsigned PrevFieldEnd = PrevFieldBitOffset +
1133 TD.getTypeSizeInBits(STy->getElementType(EltCount-1));
1134 if (PrevFieldEnd < SL->getSizeInBits())
1138 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1139 return HasPadding(ATy->getElementType(), TD);
1140 } else if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1141 return HasPadding(VTy->getElementType(), TD);
1143 return TD.getTypeSizeInBits(Ty) != TD.getTypeAllocSizeInBits(Ty);
1146 /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of
1147 /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe,
1148 /// or 1 if safe after canonicalization has been performed.
1150 int SROA::isSafeAllocaToScalarRepl(AllocationInst *AI) {
1151 // Loop over the use list of the alloca. We can only transform it if all of
1152 // the users are safe to transform.
1155 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
1157 isSafeUseOfAllocation(cast<Instruction>(*I), AI, Info);
1158 if (Info.isUnsafe) {
1159 DOUT << "Cannot transform: " << *AI << " due to user: " << **I;
1164 // Okay, we know all the users are promotable. If the aggregate is a memcpy
1165 // source and destination, we have to be careful. In particular, the memcpy
1166 // could be moving around elements that live in structure padding of the LLVM
1167 // types, but may actually be used. In these cases, we refuse to promote the
1169 if (Info.isMemCpySrc && Info.isMemCpyDst &&
1170 HasPadding(AI->getType()->getElementType(), *TD))
1173 // If we require cleanup, return 1, otherwise return 3.
1174 return Info.needsCleanup ? 1 : 3;
1177 /// CleanupGEP - GEP is used by an Alloca, which can be prompted after the GEP
1178 /// is canonicalized here.
1179 void SROA::CleanupGEP(GetElementPtrInst *GEPI) {
1180 gep_type_iterator I = gep_type_begin(GEPI);
1183 const ArrayType *AT = dyn_cast<ArrayType>(*I);
1187 uint64_t NumElements = AT->getNumElements();
1189 if (isa<ConstantInt>(I.getOperand()))
1192 if (NumElements == 1) {
1193 GEPI->setOperand(2, Context->getNullValue(Type::Int32Ty));
1197 assert(NumElements == 2 && "Unhandled case!");
1198 // All users of the GEP must be loads. At each use of the GEP, insert
1199 // two loads of the appropriate indexed GEP and select between them.
1200 Value *IsOne = new ICmpInst(GEPI, ICmpInst::ICMP_NE, I.getOperand(),
1201 Context->getNullValue(I.getOperand()->getType()),
1203 // Insert the new GEP instructions, which are properly indexed.
1204 SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end());
1205 Indices[1] = Context->getNullValue(Type::Int32Ty);
1206 Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
1209 GEPI->getName()+".0", GEPI);
1210 Indices[1] = Context->getConstantInt(Type::Int32Ty, 1);
1211 Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
1214 GEPI->getName()+".1", GEPI);
1215 // Replace all loads of the variable index GEP with loads from both
1216 // indexes and a select.
1217 while (!GEPI->use_empty()) {
1218 LoadInst *LI = cast<LoadInst>(GEPI->use_back());
1219 Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI);
1220 Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI);
1221 Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI);
1222 LI->replaceAllUsesWith(R);
1223 LI->eraseFromParent();
1225 GEPI->eraseFromParent();
1229 /// CleanupAllocaUsers - If SROA reported that it can promote the specified
1230 /// allocation, but only if cleaned up, perform the cleanups required.
1231 void SROA::CleanupAllocaUsers(AllocationInst *AI) {
1232 // At this point, we know that the end result will be SROA'd and promoted, so
1233 // we can insert ugly code if required so long as sroa+mem2reg will clean it
1235 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
1238 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
1241 Instruction *I = cast<Instruction>(U);
1242 SmallVector<DbgInfoIntrinsic *, 2> DbgInUses;
1243 if (!isa<StoreInst>(I) && OnlyUsedByDbgInfoIntrinsics(I, &DbgInUses)) {
1244 // Safe to remove debug info uses.
1245 while (!DbgInUses.empty()) {
1246 DbgInfoIntrinsic *DI = DbgInUses.back(); DbgInUses.pop_back();
1247 DI->eraseFromParent();
1249 I->eraseFromParent();
1255 /// MergeInType - Add the 'In' type to the accumulated type (Accum) so far at
1256 /// the offset specified by Offset (which is specified in bytes).
1258 /// There are two cases we handle here:
1259 /// 1) A union of vector types of the same size and potentially its elements.
1260 /// Here we turn element accesses into insert/extract element operations.
1261 /// This promotes a <4 x float> with a store of float to the third element
1262 /// into a <4 x float> that uses insert element.
1263 /// 2) A fully general blob of memory, which we turn into some (potentially
1264 /// large) integer type with extract and insert operations where the loads
1265 /// and stores would mutate the memory.
1266 static void MergeInType(const Type *In, uint64_t Offset, const Type *&VecTy,
1267 unsigned AllocaSize, const TargetData &TD,
1268 LLVMContext *Context) {
1269 // If this could be contributing to a vector, analyze it.
1270 if (VecTy != Type::VoidTy) { // either null or a vector type.
1272 // If the In type is a vector that is the same size as the alloca, see if it
1273 // matches the existing VecTy.
1274 if (const VectorType *VInTy = dyn_cast<VectorType>(In)) {
1275 if (VInTy->getBitWidth()/8 == AllocaSize && Offset == 0) {
1276 // If we're storing/loading a vector of the right size, allow it as a
1277 // vector. If this the first vector we see, remember the type so that
1278 // we know the element size.
1283 } else if (In == Type::FloatTy || In == Type::DoubleTy ||
1284 (isa<IntegerType>(In) && In->getPrimitiveSizeInBits() >= 8 &&
1285 isPowerOf2_32(In->getPrimitiveSizeInBits()))) {
1286 // If we're accessing something that could be an element of a vector, see
1287 // if the implied vector agrees with what we already have and if Offset is
1288 // compatible with it.
1289 unsigned EltSize = In->getPrimitiveSizeInBits()/8;
1290 if (Offset % EltSize == 0 &&
1291 AllocaSize % EltSize == 0 &&
1293 cast<VectorType>(VecTy)->getElementType()
1294 ->getPrimitiveSizeInBits()/8 == EltSize)) {
1296 VecTy = Context->getVectorType(In, AllocaSize/EltSize);
1302 // Otherwise, we have a case that we can't handle with an optimized vector
1303 // form. We can still turn this into a large integer.
1304 VecTy = Type::VoidTy;
1307 /// CanConvertToScalar - V is a pointer. If we can convert the pointee and all
1308 /// its accesses to use a to single vector type, return true, and set VecTy to
1309 /// the new type. If we could convert the alloca into a single promotable
1310 /// integer, return true but set VecTy to VoidTy. Further, if the use is not a
1311 /// completely trivial use that mem2reg could promote, set IsNotTrivial. Offset
1312 /// is the current offset from the base of the alloca being analyzed.
1314 /// If we see at least one access to the value that is as a vector type, set the
1317 bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
1318 bool &SawVec, uint64_t Offset,
1319 unsigned AllocaSize) {
1320 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) {
1321 Instruction *User = cast<Instruction>(*UI);
1323 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1324 // Don't break volatile loads.
1325 if (LI->isVolatile())
1327 MergeInType(LI->getType(), Offset, VecTy, AllocaSize, *TD, Context);
1328 SawVec |= isa<VectorType>(LI->getType());
1332 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
1333 // Storing the pointer, not into the value?
1334 if (SI->getOperand(0) == V || SI->isVolatile()) return 0;
1335 MergeInType(SI->getOperand(0)->getType(), Offset,
1336 VecTy, AllocaSize, *TD, Context);
1337 SawVec |= isa<VectorType>(SI->getOperand(0)->getType());
1341 if (BitCastInst *BCI = dyn_cast<BitCastInst>(User)) {
1342 if (!CanConvertToScalar(BCI, IsNotTrivial, VecTy, SawVec, Offset,
1345 IsNotTrivial = true;
1349 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) {
1350 // If this is a GEP with a variable indices, we can't handle it.
1351 if (!GEP->hasAllConstantIndices())
1354 // Compute the offset that this GEP adds to the pointer.
1355 SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end());
1356 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(),
1357 &Indices[0], Indices.size());
1358 // See if all uses can be converted.
1359 if (!CanConvertToScalar(GEP, IsNotTrivial, VecTy, SawVec,Offset+GEPOffset,
1362 IsNotTrivial = true;
1366 // If this is a constant sized memset of a constant value (e.g. 0) we can
1368 if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) {
1369 // Store of constant value and constant size.
1370 if (isa<ConstantInt>(MSI->getValue()) &&
1371 isa<ConstantInt>(MSI->getLength())) {
1372 IsNotTrivial = true;
1377 // If this is a memcpy or memmove into or out of the whole allocation, we
1378 // can handle it like a load or store of the scalar type.
1379 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) {
1380 if (ConstantInt *Len = dyn_cast<ConstantInt>(MTI->getLength()))
1381 if (Len->getZExtValue() == AllocaSize && Offset == 0) {
1382 IsNotTrivial = true;
1387 // Ignore dbg intrinsic.
1388 if (isa<DbgInfoIntrinsic>(User))
1391 // Otherwise, we cannot handle this!
1399 /// ConvertUsesToScalar - Convert all of the users of Ptr to use the new alloca
1400 /// directly. This happens when we are converting an "integer union" to a
1401 /// single integer scalar, or when we are converting a "vector union" to a
1402 /// vector with insert/extractelement instructions.
1404 /// Offset is an offset from the original alloca, in bits that need to be
1405 /// shifted to the right. By the end of this, there should be no uses of Ptr.
1406 void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) {
1407 while (!Ptr->use_empty()) {
1408 Instruction *User = cast<Instruction>(Ptr->use_back());
1410 if (BitCastInst *CI = dyn_cast<BitCastInst>(User)) {
1411 ConvertUsesToScalar(CI, NewAI, Offset);
1412 CI->eraseFromParent();
1416 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(User)) {
1417 // Compute the offset that this GEP adds to the pointer.
1418 SmallVector<Value*, 8> Indices(GEP->op_begin()+1, GEP->op_end());
1419 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(),
1420 &Indices[0], Indices.size());
1421 ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8);
1422 GEP->eraseFromParent();
1426 IRBuilder<> Builder(User->getParent(), User);
1428 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
1429 // The load is a bit extract from NewAI shifted right by Offset bits.
1430 Value *LoadedVal = Builder.CreateLoad(NewAI, "tmp");
1432 = ConvertScalar_ExtractValue(LoadedVal, LI->getType(), Offset, Builder);
1433 LI->replaceAllUsesWith(NewLoadVal);
1434 LI->eraseFromParent();
1438 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
1439 assert(SI->getOperand(0) != Ptr && "Consistency error!");
1440 Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str());
1441 Value *New = ConvertScalar_InsertValue(SI->getOperand(0), Old, Offset,
1443 Builder.CreateStore(New, NewAI);
1444 SI->eraseFromParent();
1448 // If this is a constant sized memset of a constant value (e.g. 0) we can
1449 // transform it into a store of the expanded constant value.
1450 if (MemSetInst *MSI = dyn_cast<MemSetInst>(User)) {
1451 assert(MSI->getRawDest() == Ptr && "Consistency error!");
1452 unsigned NumBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
1453 if (NumBytes != 0) {
1454 unsigned Val = cast<ConstantInt>(MSI->getValue())->getZExtValue();
1456 // Compute the value replicated the right number of times.
1457 APInt APVal(NumBytes*8, Val);
1459 // Splat the value if non-zero.
1461 for (unsigned i = 1; i != NumBytes; ++i)
1462 APVal |= APVal << 8;
1464 Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str());
1465 Value *New = ConvertScalar_InsertValue(Context->getConstantInt(APVal),
1466 Old, Offset, Builder);
1467 Builder.CreateStore(New, NewAI);
1469 MSI->eraseFromParent();
1473 // If this is a memcpy or memmove into or out of the whole allocation, we
1474 // can handle it like a load or store of the scalar type.
1475 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(User)) {
1476 assert(Offset == 0 && "must be store to start of alloca");
1478 // If the source and destination are both to the same alloca, then this is
1479 // a noop copy-to-self, just delete it. Otherwise, emit a load and store
1481 AllocaInst *OrigAI = cast<AllocaInst>(Ptr->getUnderlyingObject());
1483 if (MTI->getSource()->getUnderlyingObject() != OrigAI) {
1484 // Dest must be OrigAI, change this to be a load from the original
1485 // pointer (bitcasted), then a store to our new alloca.
1486 assert(MTI->getRawDest() == Ptr && "Neither use is of pointer?");
1487 Value *SrcPtr = MTI->getSource();
1488 SrcPtr = Builder.CreateBitCast(SrcPtr, NewAI->getType());
1490 LoadInst *SrcVal = Builder.CreateLoad(SrcPtr, "srcval");
1491 SrcVal->setAlignment(MTI->getAlignment());
1492 Builder.CreateStore(SrcVal, NewAI);
1493 } else if (MTI->getDest()->getUnderlyingObject() != OrigAI) {
1494 // Src must be OrigAI, change this to be a load from NewAI then a store
1495 // through the original dest pointer (bitcasted).
1496 assert(MTI->getRawSource() == Ptr && "Neither use is of pointer?");
1497 LoadInst *SrcVal = Builder.CreateLoad(NewAI, "srcval");
1499 Value *DstPtr = Builder.CreateBitCast(MTI->getDest(), NewAI->getType());
1500 StoreInst *NewStore = Builder.CreateStore(SrcVal, DstPtr);
1501 NewStore->setAlignment(MTI->getAlignment());
1503 // Noop transfer. Src == Dst
1507 MTI->eraseFromParent();
1511 // If user is a dbg info intrinsic then it is safe to remove it.
1512 if (isa<DbgInfoIntrinsic>(User)) {
1513 User->eraseFromParent();
1517 llvm_unreachable("Unsupported operation!");
1521 /// ConvertScalar_ExtractValue - Extract a value of type ToType from an integer
1522 /// or vector value FromVal, extracting the bits from the offset specified by
1523 /// Offset. This returns the value, which is of type ToType.
1525 /// This happens when we are converting an "integer union" to a single
1526 /// integer scalar, or when we are converting a "vector union" to a vector with
1527 /// insert/extractelement instructions.
1529 /// Offset is an offset from the original alloca, in bits that need to be
1530 /// shifted to the right.
1531 Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType,
1532 uint64_t Offset, IRBuilder<> &Builder) {
1533 // If the load is of the whole new alloca, no conversion is needed.
1534 if (FromVal->getType() == ToType && Offset == 0)
1537 // If the result alloca is a vector type, this is either an element
1538 // access or a bitcast to another vector type of the same size.
1539 if (const VectorType *VTy = dyn_cast<VectorType>(FromVal->getType())) {
1540 if (isa<VectorType>(ToType))
1541 return Builder.CreateBitCast(FromVal, ToType, "tmp");
1543 // Otherwise it must be an element access.
1546 unsigned EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType());
1547 Elt = Offset/EltSize;
1548 assert(EltSize*Elt == Offset && "Invalid modulus in validity checking");
1550 // Return the element extracted out of it.
1551 Value *V = Builder.CreateExtractElement(FromVal,
1552 Context->getConstantInt(Type::Int32Ty,Elt),
1554 if (V->getType() != ToType)
1555 V = Builder.CreateBitCast(V, ToType, "tmp");
1559 // If ToType is a first class aggregate, extract out each of the pieces and
1560 // use insertvalue's to form the FCA.
1561 if (const StructType *ST = dyn_cast<StructType>(ToType)) {
1562 const StructLayout &Layout = *TD->getStructLayout(ST);
1563 Value *Res = Context->getUndef(ST);
1564 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
1565 Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i),
1566 Offset+Layout.getElementOffsetInBits(i),
1568 Res = Builder.CreateInsertValue(Res, Elt, i, "tmp");
1573 if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) {
1574 uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType());
1575 Value *Res = Context->getUndef(AT);
1576 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
1577 Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(),
1578 Offset+i*EltSize, Builder);
1579 Res = Builder.CreateInsertValue(Res, Elt, i, "tmp");
1584 // Otherwise, this must be a union that was converted to an integer value.
1585 const IntegerType *NTy = cast<IntegerType>(FromVal->getType());
1587 // If this is a big-endian system and the load is narrower than the
1588 // full alloca type, we need to do a shift to get the right bits.
1590 if (TD->isBigEndian()) {
1591 // On big-endian machines, the lowest bit is stored at the bit offset
1592 // from the pointer given by getTypeStoreSizeInBits. This matters for
1593 // integers with a bitwidth that is not a multiple of 8.
1594 ShAmt = TD->getTypeStoreSizeInBits(NTy) -
1595 TD->getTypeStoreSizeInBits(ToType) - Offset;
1600 // Note: we support negative bitwidths (with shl) which are not defined.
1601 // We do this to support (f.e.) loads off the end of a structure where
1602 // only some bits are used.
1603 if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth())
1604 FromVal = Builder.CreateLShr(FromVal,
1605 Context->getConstantInt(FromVal->getType(),
1607 else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth())
1608 FromVal = Builder.CreateShl(FromVal,
1609 Context->getConstantInt(FromVal->getType(),
1612 // Finally, unconditionally truncate the integer to the right width.
1613 unsigned LIBitWidth = TD->getTypeSizeInBits(ToType);
1614 if (LIBitWidth < NTy->getBitWidth())
1616 Builder.CreateTrunc(FromVal, Context->getIntegerType(LIBitWidth), "tmp");
1617 else if (LIBitWidth > NTy->getBitWidth())
1619 Builder.CreateZExt(FromVal, Context->getIntegerType(LIBitWidth), "tmp");
1621 // If the result is an integer, this is a trunc or bitcast.
1622 if (isa<IntegerType>(ToType)) {
1624 } else if (ToType->isFloatingPoint() || isa<VectorType>(ToType)) {
1625 // Just do a bitcast, we know the sizes match up.
1626 FromVal = Builder.CreateBitCast(FromVal, ToType, "tmp");
1628 // Otherwise must be a pointer.
1629 FromVal = Builder.CreateIntToPtr(FromVal, ToType, "tmp");
1631 assert(FromVal->getType() == ToType && "Didn't convert right?");
1636 /// ConvertScalar_InsertValue - Insert the value "SV" into the existing integer
1637 /// or vector value "Old" at the offset specified by Offset.
1639 /// This happens when we are converting an "integer union" to a
1640 /// single integer scalar, or when we are converting a "vector union" to a
1641 /// vector with insert/extractelement instructions.
1643 /// Offset is an offset from the original alloca, in bits that need to be
1644 /// shifted to the right.
1645 Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
1646 uint64_t Offset, IRBuilder<> &Builder) {
1648 // Convert the stored type to the actual type, shift it left to insert
1649 // then 'or' into place.
1650 const Type *AllocaType = Old->getType();
1652 if (const VectorType *VTy = dyn_cast<VectorType>(AllocaType)) {
1653 uint64_t VecSize = TD->getTypeAllocSizeInBits(VTy);
1654 uint64_t ValSize = TD->getTypeAllocSizeInBits(SV->getType());
1656 // Changing the whole vector with memset or with an access of a different
1658 if (ValSize == VecSize)
1659 return Builder.CreateBitCast(SV, AllocaType, "tmp");
1661 uint64_t EltSize = TD->getTypeAllocSizeInBits(VTy->getElementType());
1663 // Must be an element insertion.
1664 unsigned Elt = Offset/EltSize;
1666 if (SV->getType() != VTy->getElementType())
1667 SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
1669 SV = Builder.CreateInsertElement(Old, SV,
1670 Context->getConstantInt(Type::Int32Ty, Elt),
1675 // If SV is a first-class aggregate value, insert each value recursively.
1676 if (const StructType *ST = dyn_cast<StructType>(SV->getType())) {
1677 const StructLayout &Layout = *TD->getStructLayout(ST);
1678 for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
1679 Value *Elt = Builder.CreateExtractValue(SV, i, "tmp");
1680 Old = ConvertScalar_InsertValue(Elt, Old,
1681 Offset+Layout.getElementOffsetInBits(i),
1687 if (const ArrayType *AT = dyn_cast<ArrayType>(SV->getType())) {
1688 uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType());
1689 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
1690 Value *Elt = Builder.CreateExtractValue(SV, i, "tmp");
1691 Old = ConvertScalar_InsertValue(Elt, Old, Offset+i*EltSize, Builder);
1696 // If SV is a float, convert it to the appropriate integer type.
1697 // If it is a pointer, do the same.
1698 unsigned SrcWidth = TD->getTypeSizeInBits(SV->getType());
1699 unsigned DestWidth = TD->getTypeSizeInBits(AllocaType);
1700 unsigned SrcStoreWidth = TD->getTypeStoreSizeInBits(SV->getType());
1701 unsigned DestStoreWidth = TD->getTypeStoreSizeInBits(AllocaType);
1702 if (SV->getType()->isFloatingPoint() || isa<VectorType>(SV->getType()))
1703 SV = Builder.CreateBitCast(SV, Context->getIntegerType(SrcWidth), "tmp");
1704 else if (isa<PointerType>(SV->getType()))
1705 SV = Builder.CreatePtrToInt(SV, TD->getIntPtrType(), "tmp");
1707 // Zero extend or truncate the value if needed.
1708 if (SV->getType() != AllocaType) {
1709 if (SV->getType()->getPrimitiveSizeInBits() <
1710 AllocaType->getPrimitiveSizeInBits())
1711 SV = Builder.CreateZExt(SV, AllocaType, "tmp");
1713 // Truncation may be needed if storing more than the alloca can hold
1714 // (undefined behavior).
1715 SV = Builder.CreateTrunc(SV, AllocaType, "tmp");
1716 SrcWidth = DestWidth;
1717 SrcStoreWidth = DestStoreWidth;
1721 // If this is a big-endian system and the store is narrower than the
1722 // full alloca type, we need to do a shift to get the right bits.
1724 if (TD->isBigEndian()) {
1725 // On big-endian machines, the lowest bit is stored at the bit offset
1726 // from the pointer given by getTypeStoreSizeInBits. This matters for
1727 // integers with a bitwidth that is not a multiple of 8.
1728 ShAmt = DestStoreWidth - SrcStoreWidth - Offset;
1733 // Note: we support negative bitwidths (with shr) which are not defined.
1734 // We do this to support (f.e.) stores off the end of a structure where
1735 // only some bits in the structure are set.
1736 APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth));
1737 if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) {
1738 SV = Builder.CreateShl(SV, Context->getConstantInt(SV->getType(),
1741 } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) {
1742 SV = Builder.CreateLShr(SV, Context->getConstantInt(SV->getType(),
1744 Mask = Mask.lshr(-ShAmt);
1747 // Mask out the bits we are about to insert from the old value, and or
1749 if (SrcWidth != DestWidth) {
1750 assert(DestWidth > SrcWidth);
1751 Old = Builder.CreateAnd(Old, Context->getConstantInt(~Mask), "mask");
1752 SV = Builder.CreateOr(Old, SV, "ins");
1759 /// PointsToConstantGlobal - Return true if V (possibly indirectly) points to
1760 /// some part of a constant global variable. This intentionally only accepts
1761 /// constant expressions because we don't can't rewrite arbitrary instructions.
1762 static bool PointsToConstantGlobal(Value *V) {
1763 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
1764 return GV->isConstant();
1765 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
1766 if (CE->getOpcode() == Instruction::BitCast ||
1767 CE->getOpcode() == Instruction::GetElementPtr)
1768 return PointsToConstantGlobal(CE->getOperand(0));
1772 /// isOnlyCopiedFromConstantGlobal - Recursively walk the uses of a (derived)
1773 /// pointer to an alloca. Ignore any reads of the pointer, return false if we
1774 /// see any stores or other unknown uses. If we see pointer arithmetic, keep
1775 /// track of whether it moves the pointer (with isOffset) but otherwise traverse
1776 /// the uses. If we see a memcpy/memmove that targets an unoffseted pointer to
1777 /// the alloca, and if the source pointer is a pointer to a constant global, we
1778 /// can optimize this.
1779 static bool isOnlyCopiedFromConstantGlobal(Value *V, Instruction *&TheCopy,
1781 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI!=E; ++UI) {
1782 if (LoadInst *LI = dyn_cast<LoadInst>(*UI))
1783 // Ignore non-volatile loads, they are always ok.
1784 if (!LI->isVolatile())
1787 if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) {
1788 // If uses of the bitcast are ok, we are ok.
1789 if (!isOnlyCopiedFromConstantGlobal(BCI, TheCopy, isOffset))
1793 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
1794 // If the GEP has all zero indices, it doesn't offset the pointer. If it
1795 // doesn't, it does.
1796 if (!isOnlyCopiedFromConstantGlobal(GEP, TheCopy,
1797 isOffset || !GEP->hasAllZeroIndices()))
1802 // If this is isn't our memcpy/memmove, reject it as something we can't
1804 if (!isa<MemTransferInst>(*UI))
1807 // If we already have seen a copy, reject the second one.
1808 if (TheCopy) return false;
1810 // If the pointer has been offset from the start of the alloca, we can't
1811 // safely handle this.
1812 if (isOffset) return false;
1814 // If the memintrinsic isn't using the alloca as the dest, reject it.
1815 if (UI.getOperandNo() != 1) return false;
1817 MemIntrinsic *MI = cast<MemIntrinsic>(*UI);
1819 // If the source of the memcpy/move is not a constant global, reject it.
1820 if (!PointsToConstantGlobal(MI->getOperand(2)))
1823 // Otherwise, the transform is safe. Remember the copy instruction.
1829 /// isOnlyCopiedFromConstantGlobal - Return true if the specified alloca is only
1830 /// modified by a copy from a constant global. If we can prove this, we can
1831 /// replace any uses of the alloca with uses of the global directly.
1832 Instruction *SROA::isOnlyCopiedFromConstantGlobal(AllocationInst *AI) {
1833 Instruction *TheCopy = 0;
1834 if (::isOnlyCopiedFromConstantGlobal(AI, TheCopy, false))