llvm.org GIT mirror llvm / 970eaf2
Reapply 91184 with fixes and an addition to the testcase to cover the problem found last time. Instead of trying to modify the IR while iterating over it, I've change it to keep a list of WeakVH references to dead instructions, and then delete those instructions later. I also added some special case code to detect and handle the situation when both operands of a memcpy intrinsic are referencing the same alloca. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91459 91177308-0d34-0410-b5e6-96231b3b80d8 Bob Wilson 9 years ago
2 changed file(s) with 545 addition(s) and 436 deletion(s). Raw diff Collapse all Expand all
7373 private:
7474 TargetData *TD;
7575
76 /// DeadInsts - Keep track of instructions we have made dead, so that
77 /// we can remove them after we are done working.
78 SmallVector DeadInsts;
79
7680 /// AllocaInfo - When analyzing uses of an alloca instruction, this captures
7781 /// information about the uses. All these fields are initialized to false
7882 /// and set to true when something is learned.
101105
102106 int isSafeAllocaToScalarRepl(AllocaInst *AI);
103107
104 void isSafeUseOfAllocation(Instruction *User, AllocaInst *AI,
105 AllocaInfo &Info);
106 void isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI,
107 AllocaInfo &Info);
108 void isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI,
109 unsigned OpNo, AllocaInfo &Info);
110 void isSafeUseOfBitCastedAllocation(BitCastInst *User, AllocaInst *AI,
111 AllocaInfo &Info);
108 void isSafeForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset,
109 uint64_t ArrayOffset, AllocaInfo &Info);
110 void isSafeGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t &Offset,
111 uint64_t &ArrayOffset, AllocaInfo &Info);
112 void isSafeMemAccess(AllocaInst *AI, uint64_t Offset, uint64_t ArrayOffset,
113 uint64_t MemSize, const Type *MemOpType, bool isStore,
114 AllocaInfo &Info);
115 bool TypeHasComponent(const Type *T, uint64_t Offset, uint64_t Size);
116 unsigned FindElementAndOffset(const Type *&T, uint64_t &Offset);
112117
113118 void DoScalarReplacement(AllocaInst *AI,
114119 std::vector &WorkList);
120 void DeleteDeadInstructions();
115121 void CleanupGEP(GetElementPtrInst *GEP);
116 void CleanupAllocaUsers(AllocaInst *AI);
122 void CleanupAllocaUsers(Value *V);
117123 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocaInst *Base);
118124
119 void RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI,
120 SmallVector &NewElts);
121
122 void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
125 void RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset,
126 SmallVector &NewElts);
127 void RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset,
128 SmallVector &NewElts);
129 void RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset,
130 SmallVector &NewElts);
131 void RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst,
123132 AllocaInst *AI,
124133 SmallVector &NewElts);
125134 void RewriteStoreUserOfWholeAlloca(StoreInst *SI, AllocaInst *AI,
359368 }
360369 }
361370
362 // Now that we have created the alloca instructions that we want to use,
363 // expand the getelementptr instructions to use them.
364 while (!AI->use_empty()) {
365 Instruction *User = cast(AI->use_back());
366 if (BitCastInst *BCInst = dyn_cast(User)) {
367 RewriteBitCastUserOfAlloca(BCInst, AI, ElementAllocas);
368 BCInst->eraseFromParent();
371 // Now that we have created the new alloca instructions, rewrite all the
372 // uses of the old alloca.
373 DeadInsts.push_back(AI);
374 RewriteForScalarRepl(AI, AI, 0, ElementAllocas);
375
376 // Now erase any instructions that were made dead while rewriting the alloca.
377 DeleteDeadInstructions();
378
379 NumReplaced++;
380 }
381
382 /// DeleteDeadInstructions - Erase instructions on the DeadInstrs list,
383 /// recursively including all their operands that become trivially dead.
384 void SROA::DeleteDeadInstructions() {
385 while (!DeadInsts.empty()) {
386 Instruction *I = dyn_cast_or_null(DeadInsts.pop_back_val());
387 if (I == 0)
369388 continue;
370 }
371
372 // Replace:
373 // %res = load { i32, i32 }* %alloc
374 // with:
375 // %load.0 = load i32* %alloc.0
376 // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0
377 // %load.1 = load i32* %alloc.1
378 // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
379 // (Also works for arrays instead of structs)
380 if (LoadInst *LI = dyn_cast(User)) {
381 Value *Insert = UndefValue::get(LI->getType());
382 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) {
383 Value *Load = new LoadInst(ElementAllocas[i], "load", LI);
384 Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI);
385 }
386 LI->replaceAllUsesWith(Insert);
387 LI->eraseFromParent();
388 continue;
389 }
390
391 // Replace:
392 // store { i32, i32 } %val, { i32, i32 }* %alloc
393 // with:
394 // %val.0 = extractvalue { i32, i32 } %val, 0
395 // store i32 %val.0, i32* %alloc.0
396 // %val.1 = extractvalue { i32, i32 } %val, 1
397 // store i32 %val.1, i32* %alloc.1
398 // (Also works for arrays instead of structs)
399 if (StoreInst *SI = dyn_cast(User)) {
400 Value *Val = SI->getOperand(0);
401 for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) {
402 Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI);
403 new StoreInst(Extract, ElementAllocas[i], SI);
404 }
405 SI->eraseFromParent();
406 continue;
407 }
408
409 GetElementPtrInst *GEPI = cast(User);
410 // We now know that the GEP is of the form: GEP , 0,
411 unsigned Idx =
412 (unsigned)cast(GEPI->getOperand(2))->getZExtValue();
413
414 assert(Idx < ElementAllocas.size() && "Index out of range?");
415 AllocaInst *AllocaToUse = ElementAllocas[Idx];
416
417 Value *RepValue;
418 if (GEPI->getNumOperands() == 3) {
419 // Do not insert a new getelementptr instruction with zero indices, only
420 // to have it optimized out later.
421 RepValue = AllocaToUse;
422 } else {
423 // We are indexing deeply into the structure, so we still need a
424 // getelement ptr instruction to finish the indexing. This may be
425 // expanded itself once the worklist is rerun.
426 //
427 SmallVector NewArgs;
428 NewArgs.push_back(Constant::getNullValue(
429 Type::getInt32Ty(AI->getContext())));
430 NewArgs.append(GEPI->op_begin()+3, GEPI->op_end());
431 RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(),
432 NewArgs.end(), "", GEPI);
433 RepValue->takeName(GEPI);
434 }
435
436 // If this GEP is to the start of the aggregate, check for memcpys.
437 if (Idx == 0 && GEPI->hasAllZeroIndices())
438 RewriteBitCastUserOfAlloca(GEPI, AI, ElementAllocas);
439
440 // Move all of the users over to the new GEP.
441 GEPI->replaceAllUsesWith(RepValue);
442 // Delete the old GEP
443 GEPI->eraseFromParent();
444 }
445
446 // Finally, delete the Alloca instruction
447 AI->eraseFromParent();
448 NumReplaced++;
449 }
450
451 /// isSafeElementUse - Check to see if this use is an allowed use for a
452 /// getelementptr instruction of an array aggregate allocation. isFirstElt
453 /// indicates whether Ptr is known to the start of the aggregate.
454 void SROA::isSafeElementUse(Value *Ptr, bool isFirstElt, AllocaInst *AI,
455 AllocaInfo &Info) {
456 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
457 I != E; ++I) {
458 Instruction *User = cast(*I);
459 switch (User->getOpcode()) {
460 case Instruction::Load: break;
461 case Instruction::Store:
462 // Store is ok if storing INTO the pointer, not storing the pointer
463 if (User->getOperand(0) == Ptr) return MarkUnsafe(Info);
464 break;
465 case Instruction::GetElementPtr: {
466 GetElementPtrInst *GEP = cast(User);
467 bool AreAllZeroIndices = isFirstElt;
468 if (GEP->getNumOperands() > 1 &&
469 (!isa(GEP->getOperand(1)) ||
470 !cast(GEP->getOperand(1))->isZero()))
471 // Using pointer arithmetic to navigate the array.
472 return MarkUnsafe(Info);
473
474 // Verify that any array subscripts are in range.
475 for (gep_type_iterator GEPIt = gep_type_begin(GEP),
476 E = gep_type_end(GEP); GEPIt != E; ++GEPIt) {
477 // Ignore struct elements, no extra checking needed for these.
478 if (isa(*GEPIt))
479 continue;
480
481 // This GEP indexes an array. Verify that this is an in-range
482 // constant integer. Specifically, consider A[0][i]. We cannot know that
483 // the user isn't doing invalid things like allowing i to index an
484 // out-of-range subscript that accesses A[1]. Because of this, we have
485 // to reject SROA of any accesses into structs where any of the
486 // components are variables.
487 ConstantInt *IdxVal = dyn_cast(GEPIt.getOperand());
488 if (!IdxVal) return MarkUnsafe(Info);
489
490 // Are all indices still zero?
491 AreAllZeroIndices &= IdxVal->isZero();
492
493 if (const ArrayType *AT = dyn_cast(*GEPIt)) {
494 if (IdxVal->getZExtValue() >= AT->getNumElements())
495 return MarkUnsafe(Info);
496 } else if (const VectorType *VT = dyn_cast(*GEPIt)) {
497 if (IdxVal->getZExtValue() >= VT->getNumElements())
498 return MarkUnsafe(Info);
499 }
500 }
501
502 isSafeElementUse(GEP, AreAllZeroIndices, AI, Info);
503 if (Info.isUnsafe) return;
504 break;
505 }
506 case Instruction::BitCast:
507 if (isFirstElt) {
508 isSafeUseOfBitCastedAllocation(cast(User), AI, Info);
509 if (Info.isUnsafe) return;
510 break;
511 }
512 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n');
513 return MarkUnsafe(Info);
514 case Instruction::Call:
515 if (MemIntrinsic *MI = dyn_cast(User)) {
516 if (isFirstElt) {
517 isSafeMemIntrinsicOnAllocation(MI, AI, I.getOperandNo(), Info);
518 if (Info.isUnsafe) return;
519 break;
520 }
521 }
522 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n');
523 return MarkUnsafe(Info);
524 default:
525 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n');
526 return MarkUnsafe(Info);
527 }
528 }
529 return; // All users look ok :)
530 }
531
389
390 for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
391 if (Instruction *U = dyn_cast(*OI)) {
392 // Zero out the operand and see if it becomes trivially dead.
393 *OI = 0;
394 if (isInstructionTriviallyDead(U))
395 DeadInsts.push_back(U);
396 }
397
398 I->eraseFromParent();
399 }
400 }
401
532402 /// AllUsersAreLoads - Return true if all users of this value are loads.
533403 static bool AllUsersAreLoads(Value *Ptr) {
534404 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
538408 return true;
539409 }
540410
541 /// isSafeUseOfAllocation - Check if this user is an allowed use for an
542 /// aggregate allocation.
543 void SROA::isSafeUseOfAllocation(Instruction *User, AllocaInst *AI,
544 AllocaInfo &Info) {
545 if (BitCastInst *C = dyn_cast(User))
546 return isSafeUseOfBitCastedAllocation(C, AI, Info);
547
548 if (LoadInst *LI = dyn_cast(User))
549 if (!LI->isVolatile())
550 return;// Loads (returning a first class aggregrate) are always rewritable
551
552 if (StoreInst *SI = dyn_cast(User))
553 if (!SI->isVolatile() && SI->getOperand(0) != AI)
554 return;// Store is ok if storing INTO the pointer, not storing the pointer
555
556 GetElementPtrInst *GEPI = dyn_cast(User);
557 if (GEPI == 0)
411 /// isSafeForScalarRepl - Check if instruction I is a safe use with regard to
412 /// performing scalar replacement of alloca AI. The results are flagged in
413 /// the Info parameter. Offset and ArrayOffset indicate the position within
414 /// AI that is referenced by this instruction.
415 void SROA::isSafeForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset,
416 uint64_t ArrayOffset, AllocaInfo &Info) {
417 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) {
418 Instruction *User = cast(*UI);
419
420 if (BitCastInst *BC = dyn_cast(User)) {
421 isSafeForScalarRepl(BC, AI, Offset, ArrayOffset, Info);
422 } else if (GetElementPtrInst *GEPI = dyn_cast(User)) {
423 uint64_t GEPArrayOffset = ArrayOffset;
424 uint64_t GEPOffset = Offset;
425 isSafeGEP(GEPI, AI, GEPOffset, GEPArrayOffset, Info);
426 if (!Info.isUnsafe)
427 isSafeForScalarRepl(GEPI, AI, GEPOffset, GEPArrayOffset, Info);
428 } else if (MemIntrinsic *MI = dyn_cast(UI)) {
429 ConstantInt *Length = dyn_cast(MI->getLength());
430 if (Length)
431 isSafeMemAccess(AI, Offset, ArrayOffset, Length->getZExtValue(), 0,
432 UI.getOperandNo() == 1, Info);
433 else
434 MarkUnsafe(Info);
435 } else if (LoadInst *LI = dyn_cast(User)) {
436 if (!LI->isVolatile()) {
437 const Type *LIType = LI->getType();
438 isSafeMemAccess(AI, Offset, ArrayOffset, TD->getTypeAllocSize(LIType),
439 LIType, false, Info);
440 } else
441 MarkUnsafe(Info);
442 } else if (StoreInst *SI = dyn_cast(User)) {
443 // Store is ok if storing INTO the pointer, not storing the pointer
444 if (!SI->isVolatile() && SI->getOperand(0) != I) {
445 const Type *SIType = SI->getOperand(0)->getType();
446 isSafeMemAccess(AI, Offset, ArrayOffset, TD->getTypeAllocSize(SIType),
447 SIType, true, Info);
448 } else
449 MarkUnsafe(Info);
450 } else if (isa(UI)) {
451 // If one user is DbgInfoIntrinsic then check if all users are
452 // DbgInfoIntrinsics.
453 if (OnlyUsedByDbgInfoIntrinsics(I)) {
454 Info.needsCleanup = true;
455 return;
456 }
457 MarkUnsafe(Info);
458 } else {
459 DEBUG(errs() << " Transformation preventing inst: " << *User << '\n');
460 MarkUnsafe(Info);
461 }
462 if (Info.isUnsafe) return;
463 }
464 }
465
466 /// isSafeGEP - Check if a GEP instruction can be handled for scalar
467 /// replacement. It is safe when all the indices are constant, in-bounds
468 /// references, and when the resulting offset corresponds to an element within
469 /// the alloca type. The results are flagged in the Info parameter. Upon
470 /// return, Offset is adjusted as specified by the GEP indices. For the
471 /// special case of a variable index to a 2-element array, ArrayOffset is set
472 /// to the array element size.
473 void SROA::isSafeGEP(GetElementPtrInst *GEPI, AllocaInst *AI,
474 uint64_t &Offset, uint64_t &ArrayOffset,
475 AllocaInfo &Info) {
476 gep_type_iterator GEPIt = gep_type_begin(GEPI), E = gep_type_end(GEPI);
477 if (GEPIt == E)
478 return;
479
480 // The first GEP index must be zero.
481 if (!isa(GEPIt.getOperand()) ||
482 !cast(GEPIt.getOperand())->isZero())
558483 return MarkUnsafe(Info);
559
560 gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI);
561
562 // The GEP is not safe to transform if not of the form "GEP , 0, ".
563 if (I == E ||
564 I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) {
565 return MarkUnsafe(Info);
566 }
567
568 ++I;
569 if (I == E) return MarkUnsafe(Info); // ran out of GEP indices??
570
571 bool IsAllZeroIndices = true;
572
484 if (++GEPIt == E)
485 return;
486
573487 // If the first index is a non-constant index into an array, see if we can
574488 // handle it as a special case.
575 if (const ArrayType *AT = dyn_cast(*I)) {
576 if (!isa(I.getOperand())) {
577 IsAllZeroIndices = 0;
578 uint64_t NumElements = AT->getNumElements();
579
580 // If this is an array index and the index is not constant, we cannot
581 // promote... that is unless the array has exactly one or two elements in
582 // it, in which case we CAN promote it, but we have to canonicalize this
583 // out if this is the only problem.
584 if ((NumElements == 1 || NumElements == 2) &&
585 AllUsersAreLoads(GEPI)) {
489 const Type *ArrayEltTy = 0;
490 if (ArrayOffset == 0 && Offset == 0) {
491 if (const ArrayType *AT = dyn_cast(*GEPIt)) {
492 if (!isa(GEPIt.getOperand())) {
493 uint64_t NumElements = AT->getNumElements();
494
495 // If this is an array index and the index is not constant, we cannot
496 // promote... that is unless the array has exactly one or two elements
497 // in it, in which case we CAN promote it, but we have to canonicalize
498 // this out if this is the only problem.
499 if ((NumElements != 1 && NumElements != 2) || !AllUsersAreLoads(GEPI))
500 return MarkUnsafe(Info);
586501 Info.needsCleanup = true;
587 return; // Canonicalization required!
588 }
589 return MarkUnsafe(Info);
590 }
591 }
592
502 ArrayOffset = TD->getTypeAllocSizeInBits(AT->getElementType());
503 ArrayEltTy = AT->getElementType();
504 ++GEPIt;
505 }
506 }
507 }
508
593509 // Walk through the GEP type indices, checking the types that this indexes
594510 // into.
595 for (; I != E; ++I) {
511 for (; GEPIt != E; ++GEPIt) {
596512 // Ignore struct elements, no extra checking needed for these.
597 if (isa(*I))
513 if (isa(*GEPIt))
598514 continue;
599
600 ConstantInt *IdxVal = dyn_cast(I.getOperand());
601 if (!IdxVal) return MarkUnsafe(Info);
602
603 // Are all indices still zero?
604 IsAllZeroIndices &= IdxVal->isZero();
605
606 if (const ArrayType *AT = dyn_cast(*I)) {
515
516 ConstantInt *IdxVal = dyn_cast(GEPIt.getOperand());
517 if (!IdxVal)
518 return MarkUnsafe(Info);
519
520 if (const ArrayType *AT = dyn_cast(*GEPIt)) {
607521 // This GEP indexes an array. Verify that this is an in-range constant
608522 // integer. Specifically, consider A[0][i]. We cannot know that the user
609523 // isn't doing invalid things like allowing i to index an out-of-range
611525 // of any accesses into structs where any of the components are variables.
612526 if (IdxVal->getZExtValue() >= AT->getNumElements())
613527 return MarkUnsafe(Info);
614 } else if (const VectorType *VT = dyn_cast(*I)) {
528 } else {
529 const VectorType *VT = dyn_cast(*GEPIt);
530 assert(VT && "unexpected type in GEP type iterator");
615531 if (IdxVal->getZExtValue() >= VT->getNumElements())
616532 return MarkUnsafe(Info);
617533 }
618534 }
619
620 // If there are any non-simple uses of this getelementptr, make sure to reject
621 // them.
622 return isSafeElementUse(GEPI, IsAllZeroIndices, AI, Info);
623 }
624
625 /// isSafeMemIntrinsicOnAllocation - Check if the specified memory
626 /// intrinsic can be promoted by SROA. At this point, we know that the operand
627 /// of the memintrinsic is a pointer to the beginning of the allocation.
628 void SROA::isSafeMemIntrinsicOnAllocation(MemIntrinsic *MI, AllocaInst *AI,
629 unsigned OpNo, AllocaInfo &Info) {
630 // If not constant length, give up.
631 ConstantInt *Length = dyn_cast(MI->getLength());
632 if (!Length) return MarkUnsafe(Info);
633
634 // If not the whole aggregate, give up.
635 if (Length->getZExtValue() !=
636 TD->getTypeAllocSize(AI->getType()->getElementType()))
637 return MarkUnsafe(Info);
638
639 // We only know about memcpy/memset/memmove.
640 if (!isa(MI))
641 return MarkUnsafe(Info);
642
643 // Otherwise, we can transform it. Determine whether this is a memcpy/set
644 // into or out of the aggregate.
645 if (OpNo == 1)
646 Info.isMemCpyDst = true;
647 else {
648 assert(OpNo == 2);
649 Info.isMemCpySrc = true;
650 }
651 }
652
653 /// isSafeUseOfBitCastedAllocation - Check if all users of this bitcast
654 /// from an alloca are safe for SROA of that alloca.
655 void SROA::isSafeUseOfBitCastedAllocation(BitCastInst *BC, AllocaInst *AI,
656 AllocaInfo &Info) {
657 for (Value::use_iterator UI = BC->use_begin(), E = BC->use_end();
658 UI != E; ++UI) {
659 if (BitCastInst *BCU = dyn_cast(UI)) {
660 isSafeUseOfBitCastedAllocation(BCU, AI, Info);
661 } else if (MemIntrinsic *MI = dyn_cast(UI)) {
662 isSafeMemIntrinsicOnAllocation(MI, AI, UI.getOperandNo(), Info);
663 } else if (StoreInst *SI = dyn_cast(UI)) {
664 if (SI->isVolatile())
665 return MarkUnsafe(Info);
666
667 // If storing the entire alloca in one chunk through a bitcasted pointer
668 // to integer, we can transform it. This happens (for example) when you
669 // cast a {i32,i32}* to i64* and store through it. This is similar to the
670 // memcpy case and occurs in various "byval" cases and emulated memcpys.
671 if (isa(SI->getOperand(0)->getType()) &&
672 TD->getTypeAllocSize(SI->getOperand(0)->getType()) ==
673 TD->getTypeAllocSize(AI->getType()->getElementType())) {
674 Info.isMemCpyDst = true;
675 continue;
676 }
677 return MarkUnsafe(Info);
678 } else if (LoadInst *LI = dyn_cast(UI)) {
679 if (LI->isVolatile())
680 return MarkUnsafe(Info);
681
682 // If loading the entire alloca in one chunk through a bitcasted pointer
683 // to integer, we can transform it. This happens (for example) when you
684 // cast a {i32,i32}* to i64* and load through it. This is similar to the
685 // memcpy case and occurs in various "byval" cases and emulated memcpys.
686 if (isa(LI->getType()) &&
687 TD->getTypeAllocSize(LI->getType()) ==
688 TD->getTypeAllocSize(AI->getType()->getElementType())) {
689 Info.isMemCpySrc = true;
690 continue;
691 }
692 return MarkUnsafe(Info);
693 } else if (isa(UI)) {
694 // If one user is DbgInfoIntrinsic then check if all users are
695 // DbgInfoIntrinsics.
696 if (OnlyUsedByDbgInfoIntrinsics(BC)) {
697 Info.needsCleanup = true;
698 return;
699 }
700 else
701 MarkUnsafe(Info);
702 }
703 else {
704 return MarkUnsafe(Info);
705 }
706 if (Info.isUnsafe) return;
707 }
708 }
709
710 /// RewriteBitCastUserOfAlloca - BCInst (transitively) bitcasts AI, or indexes
711 /// to its first element. Transform users of the cast to use the new values
712 /// instead.
713 void SROA::RewriteBitCastUserOfAlloca(Instruction *BCInst, AllocaInst *AI,
714 SmallVector &NewElts) {
715 Value::use_iterator UI = BCInst->use_begin(), UE = BCInst->use_end();
716 while (UI != UE) {
717 Instruction *User = cast(*UI++);
718 if (BitCastInst *BCU = dyn_cast(User)) {
719 RewriteBitCastUserOfAlloca(BCU, AI, NewElts);
720 if (BCU->use_empty()) BCU->eraseFromParent();
721 continue;
722 }
723
724 if (MemIntrinsic *MI = dyn_cast(User)) {
725 // This must be memcpy/memmove/memset of the entire aggregate.
726 // Split into one per element.
727 RewriteMemIntrinUserOfAlloca(MI, BCInst, AI, NewElts);
728 continue;
729 }
730
731 if (StoreInst *SI = dyn_cast(User)) {
732 // If this is a store of the entire alloca from an integer, rewrite it.
733 RewriteStoreUserOfWholeAlloca(SI, AI, NewElts);
734 continue;
735 }
736
737 if (LoadInst *LI = dyn_cast(User)) {
738 // If this is a load of the entire alloca to an integer, rewrite it.
739 RewriteLoadUserOfWholeAlloca(LI, AI, NewElts);
740 continue;
741 }
742
743 // Otherwise it must be some other user of a gep of the first pointer. Just
744 // leave these alone.
745 continue;
746 }
535
536 // All the indices are safe. Now compute the offset due to this GEP and
537 // check if the alloca has a component element at that offset.
538 if (ArrayOffset == 0) {
539 SmallVector Indices(GEPI->op_begin() + 1, GEPI->op_end());
540 Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(),
541 &Indices[0], Indices.size());
542 } else {
543 // Both array elements have the same type, so it suffices to check one of
544 // them. Copy the GEP indices starting from the array index, but replace
545 // that variable index with a constant zero.
546 SmallVector Indices(GEPI->op_begin() + 2, GEPI->op_end());
547 Indices[0] = Constant::getNullValue(Type::getInt32Ty(GEPI->getContext()));
548 const Type *ArrayEltPtr = PointerType::getUnqual(ArrayEltTy);
549 Offset += TD->getIndexedOffset(ArrayEltPtr, &Indices[0], Indices.size());
550 }
551 if (!TypeHasComponent(AI->getAllocatedType(), Offset, 0))
552 MarkUnsafe(Info);
553 }
554
555 /// isSafeMemAccess - Check if a load/store/memcpy operates on the entire AI
556 /// alloca or has an offset and size that corresponds to a component element
557 /// within it. The offset checked here may have been formed from a GEP with a
558 /// pointer bitcasted to a different type.
559 void SROA::isSafeMemAccess(AllocaInst *AI, uint64_t Offset,
560 uint64_t ArrayOffset, uint64_t MemSize,
561 const Type *MemOpType, bool isStore,
562 AllocaInfo &Info) {
563 // Check if this is a load/store of the entire alloca.
564 if (Offset == 0 && ArrayOffset == 0 &&
565 MemSize == TD->getTypeAllocSize(AI->getAllocatedType())) {
566 bool UsesAggregateType = (MemOpType == AI->getAllocatedType());
567 // This is safe for MemIntrinsics (where MemOpType is 0), integer types
568 // (which are essentially the same as the MemIntrinsics, especially with
569 // regard to copying padding between elements), or references using the
570 // aggregate type of the alloca.
571 if (!MemOpType || isa(MemOpType) || UsesAggregateType) {
572 if (!UsesAggregateType) {
573 if (isStore)
574 Info.isMemCpyDst = true;
575 else
576 Info.isMemCpySrc = true;
577 }
578 return;
579 }
580 }
581 // Check if the offset/size correspond to a component within the alloca type.
582 const Type *T = AI->getAllocatedType();
583 if (TypeHasComponent(T, Offset, MemSize) &&
584 (ArrayOffset == 0 || TypeHasComponent(T, Offset + ArrayOffset, MemSize)))
585 return;
586
587 return MarkUnsafe(Info);
588 }
589
590 /// TypeHasComponent - Return true if T has a component type with the
591 /// specified offset and size. If Size is zero, do not check the size.
592 bool SROA::TypeHasComponent(const Type *T, uint64_t Offset, uint64_t Size) {
593 const Type *EltTy;
594 uint64_t EltSize;
595 if (const StructType *ST = dyn_cast(T)) {
596 const StructLayout *Layout = TD->getStructLayout(ST);
597 unsigned EltIdx = Layout->getElementContainingOffset(Offset);
598 EltTy = ST->getContainedType(EltIdx);
599 EltSize = TD->getTypeAllocSize(EltTy);
600 Offset -= Layout->getElementOffset(EltIdx);
601 } else if (const ArrayType *AT = dyn_cast(T)) {
602 EltTy = AT->getElementType();
603 EltSize = TD->getTypeAllocSize(EltTy);
604 Offset %= EltSize;
605 } else {
606 return false;
607 }
608 if (Offset == 0 && (Size == 0 || EltSize == Size))
609 return true;
610 // Check if the component spans multiple elements.
611 if (Offset + Size > EltSize)
612 return false;
613 return TypeHasComponent(EltTy, Offset, Size);
614 }
615
616 /// RewriteForScalarRepl - Alloca AI is being split into NewElts, so rewrite
617 /// the instruction I, which references it, to use the separate elements.
618 /// Offset indicates the position within AI that is referenced by this
619 /// instruction.
620 void SROA::RewriteForScalarRepl(Instruction *I, AllocaInst *AI, uint64_t Offset,
621 SmallVector &NewElts) {
622 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI!=E; ++UI) {
623 Instruction *User = cast(*UI);
624
625 if (BitCastInst *BC = dyn_cast(User)) {
626 RewriteBitCast(BC, AI, Offset, NewElts);
627 } else if (GetElementPtrInst *GEPI = dyn_cast(User)) {
628 RewriteGEP(GEPI, AI, Offset, NewElts);
629 } else if (MemIntrinsic *MI = dyn_cast(User)) {
630 ConstantInt *Length = dyn_cast(MI->getLength());
631 uint64_t MemSize = Length->getZExtValue();
632 if (Offset == 0 &&
633 MemSize == TD->getTypeAllocSize(AI->getAllocatedType()))
634 RewriteMemIntrinUserOfAlloca(MI, I, AI, NewElts);
635 } else if (LoadInst *LI = dyn_cast(User)) {
636 const Type *LIType = LI->getType();
637 if (LIType == AI->getAllocatedType()) {
638 // Replace:
639 // %res = load { i32, i32 }* %alloc
640 // with:
641 // %load.0 = load i32* %alloc.0
642 // %insert.0 insertvalue { i32, i32 } zeroinitializer, i32 %load.0, 0
643 // %load.1 = load i32* %alloc.1
644 // %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
645 // (Also works for arrays instead of structs)
646 Value *Insert = UndefValue::get(LIType);
647 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
648 Value *Load = new LoadInst(NewElts[i], "load", LI);
649 Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI);
650 }
651 LI->replaceAllUsesWith(Insert);
652 DeadInsts.push_back(LI);
653 } else if (isa(LIType) &&
654 TD->getTypeAllocSize(LIType) ==
655 TD->getTypeAllocSize(AI->getAllocatedType())) {
656 // If this is a load of the entire alloca to an integer, rewrite it.
657 RewriteLoadUserOfWholeAlloca(LI, AI, NewElts);
658 }
659 } else if (StoreInst *SI = dyn_cast(User)) {
660 Value *Val = SI->getOperand(0);
661 const Type *SIType = Val->getType();
662 if (SIType == AI->getAllocatedType()) {
663 // Replace:
664 // store { i32, i32 } %val, { i32, i32 }* %alloc
665 // with:
666 // %val.0 = extractvalue { i32, i32 } %val, 0
667 // store i32 %val.0, i32* %alloc.0
668 // %val.1 = extractvalue { i32, i32 } %val, 1
669 // store i32 %val.1, i32* %alloc.1
670 // (Also works for arrays instead of structs)
671 for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
672 Value *Extract = ExtractValueInst::Create(Val, i, Val->getName(), SI);
673 new StoreInst(Extract, NewElts[i], SI);
674 }
675 DeadInsts.push_back(SI);
676 } else if (isa(SIType) &&
677 TD->getTypeAllocSize(SIType) ==
678 TD->getTypeAllocSize(AI->getAllocatedType())) {
679 // If this is a store of the entire alloca from an integer, rewrite it.
680 RewriteStoreUserOfWholeAlloca(SI, AI, NewElts);
681 }
682 }
683 }
684 }
685
686 /// RewriteBitCast - Update a bitcast reference to the alloca being replaced
687 /// and recursively continue updating all of its uses.
688 void SROA::RewriteBitCast(BitCastInst *BC, AllocaInst *AI, uint64_t Offset,
689 SmallVector &NewElts) {
690 RewriteForScalarRepl(BC, AI, Offset, NewElts);
691 if (BC->getOperand(0) != AI)
692 return;
693
694 // The bitcast references the original alloca. Replace its uses with
695 // references to the first new element alloca.
696 Instruction *Val = NewElts[0];
697 if (Val->getType() != BC->getDestTy()) {
698 Val = new BitCastInst(Val, BC->getDestTy(), "", BC);
699 Val->takeName(BC);
700 }
701 BC->replaceAllUsesWith(Val);
702 DeadInsts.push_back(BC);
703 }
704
705 /// FindElementAndOffset - Return the index of the element containing Offset
706 /// within the specified type, which must be either a struct or an array.
707 /// Sets T to the type of the element and Offset to the offset within that
708 /// element.
709 unsigned SROA::FindElementAndOffset(const Type *&T, uint64_t &Offset) {
710 unsigned Idx = 0;
711 if (const StructType *ST = dyn_cast(T)) {
712 const StructLayout *Layout = TD->getStructLayout(ST);
713 Idx = Layout->getElementContainingOffset(Offset);
714 T = ST->getContainedType(Idx);
715 Offset -= Layout->getElementOffset(Idx);
716 } else {
717 const ArrayType *AT = dyn_cast(T);
718 assert(AT && "unexpected type for scalar replacement");
719 T = AT->getElementType();
720 uint64_t EltSize = TD->getTypeAllocSize(T);
721 Idx = (unsigned)(Offset / EltSize);
722 Offset -= Idx * EltSize;
723 }
724 return Idx;
725 }
726
727 /// RewriteGEP - Check if this GEP instruction moves the pointer across
728 /// elements of the alloca that are being split apart, and if so, rewrite
729 /// the GEP to be relative to the new element.
730 void SROA::RewriteGEP(GetElementPtrInst *GEPI, AllocaInst *AI, uint64_t Offset,
731 SmallVector &NewElts) {
732 uint64_t OldOffset = Offset;
733 SmallVector Indices(GEPI->op_begin() + 1, GEPI->op_end());
734 Offset += TD->getIndexedOffset(GEPI->getPointerOperandType(),
735 &Indices[0], Indices.size());
736
737 RewriteForScalarRepl(GEPI, AI, Offset, NewElts);
738
739 const Type *T = AI->getAllocatedType();
740 unsigned OldIdx = FindElementAndOffset(T, OldOffset);
741 if (GEPI->getOperand(0) == AI)
742 OldIdx = ~0U; // Force the GEP to be rewritten.
743
744 T = AI->getAllocatedType();
745 uint64_t EltOffset = Offset;
746 unsigned Idx = FindElementAndOffset(T, EltOffset);
747
748 // If this GEP does not move the pointer across elements of the alloca
749 // being split, then it does not needs to be rewritten.
750 if (Idx == OldIdx)
751 return;
752
753 const Type *i32Ty = Type::getInt32Ty(AI->getContext());
754 SmallVector NewArgs;
755 NewArgs.push_back(Constant::getNullValue(i32Ty));
756 while (EltOffset != 0) {
757 unsigned EltIdx = FindElementAndOffset(T, EltOffset);
758 NewArgs.push_back(ConstantInt::get(i32Ty, EltIdx));
759 }
760 Instruction *Val = NewElts[Idx];
761 if (NewArgs.size() > 1) {
762 Val = GetElementPtrInst::CreateInBounds(Val, NewArgs.begin(),
763 NewArgs.end(), "", GEPI);
764 Val->takeName(GEPI);
765 }
766 if (Val->getType() != GEPI->getType())
767 Val = new BitCastInst(Val, GEPI->getType(), Val->getNameStr(), GEPI);
768 GEPI->replaceAllUsesWith(Val);
769 DeadInsts.push_back(GEPI);
747770 }
748771
749772 /// RewriteMemIntrinUserOfAlloca - MI is a memcpy/memset/memmove from or to AI.
750773 /// Rewrite it to copy or set the elements of the scalarized memory.
751 void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
774 void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst,
752775 AllocaInst *AI,
753776 SmallVector &NewElts) {
754
755777 // If this is a memcpy/memmove, construct the other pointer as the
756778 // appropriate type. The "Other" pointer is the pointer that goes to memory
757779 // that doesn't have anything to do with the alloca that we are promoting. For
760782 LLVMContext &Context = MI->getContext();
761783 unsigned MemAlignment = MI->getAlignment();
762784 if (MemTransferInst *MTI = dyn_cast(MI)) { // memmove/memcopy
763 if (BCInst == MTI->getRawDest())
785 if (Inst == MTI->getRawDest())
764786 OtherPtr = MTI->getRawSource();
765787 else {
766 assert(BCInst == MTI->getRawSource());
788 assert(Inst == MTI->getRawSource());
767789 OtherPtr = MTI->getRawDest();
768790 }
769791 }
770792
771 // Keep track of the other intrinsic argument, so it can be removed if it
772 // is dead when the intrinsic is replaced.
773 Value *PossiblyDead = OtherPtr;
774
775793 // If there is an other pointer, we want to convert it to the same pointer
776794 // type as AI has, so we can GEP through it safely.
777795 if (OtherPtr) {
778 // It is likely that OtherPtr is a bitcast, if so, remove it.
779 if (BitCastInst *BC = dyn_cast(OtherPtr))
780 OtherPtr = BC->getOperand(0);
781 // All zero GEPs are effectively bitcasts.
782 if (GetElementPtrInst *GEP = dyn_cast(OtherPtr))
783 if (GEP->hasAllZeroIndices())
784 OtherPtr = GEP->getOperand(0);
796
797 // Remove bitcasts and all-zero GEPs from OtherPtr. This is an
798 // optimization, but it's also required to detect the corner case where
799 // both pointer operands are referencing the same memory, and where
800 // OtherPtr may be a bitcast or GEP that currently being rewritten. (This
801 // function is only called for mem intrinsics that access the whole
802 // aggregate, so non-zero GEPs are not an issue here.)
803 while (1) {
804 if (BitCastInst *BC = dyn_cast(OtherPtr)) {
805 OtherPtr = BC->getOperand(0);
806 continue;
807 }
808 if (GetElementPtrInst *GEP = dyn_cast(OtherPtr)) {
809 // All zero GEPs are effectively bitcasts.
810 if (GEP->hasAllZeroIndices()) {
811 OtherPtr = GEP->getOperand(0);
812 continue;
813 }
814 }
815 break;
816 }
817 // If OtherPtr has already been rewritten, this intrinsic will be dead.
818 if (OtherPtr == NewElts[0])
819 return;
785820
786821 if (ConstantExpr *BCE = dyn_cast(OtherPtr))
787822 if (BCE->getOpcode() == Instruction::BitCast)
797832 // Process each element of the aggregate.
798833 Value *TheFn = MI->getOperand(0);
799834 const Type *BytePtrTy = MI->getRawDest()->getType();
800 bool SROADest = MI->getRawDest() == BCInst;
835 bool SROADest = MI->getRawDest() == Inst;
801836
802837 Constant *Zero = Constant::getNullValue(Type::getInt32Ty(MI->getContext()));
803838
806841 Value *OtherElt = 0;
807842 unsigned OtherEltAlign = MemAlignment;
808843
809 if (OtherPtr) {
844 if (OtherPtr == AI) {
845 OtherElt = NewElts[i];
846 OtherEltAlign = 0;
847 } else if (OtherPtr) {
810848 Value *Idx[2] = { Zero,
811849 ConstantInt::get(Type::getInt32Ty(MI->getContext()), i) };
812 OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2,
850 OtherElt = GetElementPtrInst::CreateInBounds(OtherPtr, Idx, Idx + 2,
813851 OtherPtr->getNameStr()+"."+Twine(i),
814 MI);
852 MI);
815853 uint64_t EltOffset;
816854 const PointerType *OtherPtrTy = cast(OtherPtr->getType());
817855 if (const StructType *ST =
923961 CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
924962 }
925963 }
926 MI->eraseFromParent();
927 if (PossiblyDead)
928 RecursivelyDeleteTriviallyDeadInstructions(PossiblyDead);
964 DeadInsts.push_back(MI);
929965 }
930966
931967 /// RewriteStoreUserOfWholeAlloca - We found a store of an integer that
936972 // Extract each element out of the integer according to its structure offset
937973 // and store the element value to the individual alloca.
938974 Value *SrcVal = SI->getOperand(0);
939 const Type *AllocaEltTy = AI->getType()->getElementType();
975 const Type *AllocaEltTy = AI->getAllocatedType();
940976 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
941977
942 // If this isn't a store of an integer to the whole alloca, it may be a store
943 // to the first element. Just ignore the store in this case and normal SROA
944 // will handle it.
945 if (!isa(SrcVal->getType()) ||
946 TD->getTypeAllocSizeInBits(SrcVal->getType()) != AllocaSizeBits)
947 return;
948978 // Handle tail padding by extending the operand
949979 if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits)
950980 SrcVal = new ZExtInst(SrcVal,
10491079 }
10501080 }
10511081
1052 SI->eraseFromParent();
1082 DeadInsts.push_back(SI);
10531083 }
10541084
10551085 /// RewriteLoadUserOfWholeAlloca - We found a load of the entire allocation to
10581088 SmallVector &NewElts) {
10591089 // Extract each element out of the NewElts according to its structure offset
10601090 // and form the result value.
1061 const Type *AllocaEltTy = AI->getType()->getElementType();
1091 const Type *AllocaEltTy = AI->getAllocatedType();
10621092 uint64_t AllocaSizeBits = TD->getTypeAllocSizeInBits(AllocaEltTy);
1063
1064 // If this isn't a load of the whole alloca to an integer, it may be a load
1065 // of the first element. Just ignore the load in this case and normal SROA
1066 // will handle it.
1067 if (!isa(LI->getType()) ||
1068 TD->getTypeAllocSizeInBits(LI->getType()) != AllocaSizeBits)
1069 return;
10701093
10711094 DEBUG(errs() << "PROMOTING LOAD OF WHOLE ALLOCA: " << *AI << '\n' << *LI
10721095 << '\n');
11381161 ResultVal = new TruncInst(ResultVal, LI->getType(), "", LI);
11391162
11401163 LI->replaceAllUsesWith(ResultVal);
1141 LI->eraseFromParent();
1142 }
1143
1164 DeadInsts.push_back(LI);
1165 }
11441166
11451167 /// HasPadding - Return true if the specified type has any structure or
11461168 /// alignment padding, false otherwise.
11911213 // the users are safe to transform.
11921214 AllocaInfo Info;
11931215
1194 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
1195 I != E; ++I) {
1196 isSafeUseOfAllocation(cast(*I), AI, Info);
1197 if (Info.isUnsafe) {
1198 DEBUG(errs() << "Cannot transform: " << *AI << "\n due to user: "
1199 << **I << '\n');
1200 return 0;
1201 }
1216 isSafeForScalarRepl(AI, AI, 0, 0, Info);
1217 if (Info.isUnsafe) {
1218 DEBUG(errs() << "Cannot transform: " << *AI << '\n');
1219 return 0;
12021220 }
12031221
12041222 // Okay, we know all the users are promotable. If the aggregate is a memcpy
12071225 // types, but may actually be used. In these cases, we refuse to promote the
12081226 // struct.
12091227 if (Info.isMemCpySrc && Info.isMemCpyDst &&
1210 HasPadding(AI->getType()->getElementType(), *TD))
1228 HasPadding(AI->getAllocatedType(), *TD))
12111229 return 0;
12121230
12131231 // If we require cleanup, return 1, otherwise return 3.
12441262 // Insert the new GEP instructions, which are properly indexed.
12451263 SmallVector Indices(GEPI->op_begin()+1, GEPI->op_end());
12461264 Indices[1] = Constant::getNullValue(Type::getInt32Ty(GEPI->getContext()));
1247 Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
1248 Indices.begin(),
1249 Indices.end(),
1250 GEPI->getName()+".0", GEPI);
1265 Value *ZeroIdx = GetElementPtrInst::CreateInBounds(GEPI->getOperand(0),
1266 Indices.begin(),
1267 Indices.end(),
1268 GEPI->getName()+".0",GEPI);
12511269 Indices[1] = ConstantInt::get(Type::getInt32Ty(GEPI->getContext()), 1);
1252 Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
1253 Indices.begin(),
1254 Indices.end(),
1255 GEPI->getName()+".1", GEPI);
1270 Value *OneIdx = GetElementPtrInst::CreateInBounds(GEPI->getOperand(0),
1271 Indices.begin(),
1272 Indices.end(),
1273 GEPI->getName()+".1", GEPI);
12561274 // Replace all loads of the variable index GEP with loads from both
12571275 // indexes and a select.
12581276 while (!GEPI->use_empty()) {
12631281 LI->replaceAllUsesWith(R);
12641282 LI->eraseFromParent();
12651283 }
1266 GEPI->eraseFromParent();
1267 }
1268
1284 }
12691285
12701286 /// CleanupAllocaUsers - If SROA reported that it can promote the specified
12711287 /// allocation, but only if cleaned up, perform the cleanups required.
1272 void SROA::CleanupAllocaUsers(AllocaInst *AI) {
1288 void SROA::CleanupAllocaUsers(Value *V) {
12731289 // At this point, we know that the end result will be SROA'd and promoted, so
12741290 // we can insert ugly code if required so long as sroa+mem2reg will clean it
12751291 // up.
1276 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end();
1292 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
12771293 UI != E; ) {
12781294 User *U = *UI++;
1279 if (GetElementPtrInst *GEPI = dyn_cast(U))
1295 if (isa(U)) {
1296 CleanupAllocaUsers(U);
1297 } else if (GetElementPtrInst *GEPI = dyn_cast(U)) {
12801298 CleanupGEP(GEPI);
1281 else {
1299 CleanupAllocaUsers(GEPI);
1300 if (GEPI->use_empty()) GEPI->eraseFromParent();
1301 } else {
12821302 Instruction *I = cast(U);
12831303 SmallVector DbgInUses;
12841304 if (!isa(I) && OnlyUsedByDbgInfoIntrinsics(I, &DbgInUses)) {
13941414
13951415 // Compute the offset that this GEP adds to the pointer.
13961416 SmallVector Indices(GEP->op_begin()+1, GEP->op_end());
1397 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(),
1417 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getPointerOperandType(),
13981418 &Indices[0], Indices.size());
13991419 // See if all uses can be converted.
14001420 if (!CanConvertToScalar(GEP, IsNotTrivial, VecTy, SawVec,Offset+GEPOffset,
14561476 if (GetElementPtrInst *GEP = dyn_cast(User)) {
14571477 // Compute the offset that this GEP adds to the pointer.
14581478 SmallVector Indices(GEP->op_begin()+1, GEP->op_end());
1459 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getOperand(0)->getType(),
1479 uint64_t GEPOffset = TD->getIndexedOffset(GEP->getPointerOperandType(),
14601480 &Indices[0], Indices.size());
14611481 ConvertUsesToScalar(GEP, NewAI, Offset+GEPOffset*8);
14621482 GEP->eraseFromParent();
0 ; RUN: opt < %s -scalarrepl -S | FileCheck %s
1 ; Radar 7441282
2
3 target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:32-n32"
4 target triple = "thumbv7-apple-darwin10"
5
6 %struct.__neon_int16x8x2_t = type { <8 x i16>, <8 x i16> }
7 %struct.int16x8_t = type { <8 x i16> }
8 %struct.int16x8x2_t = type { [2 x %struct.int16x8_t] }
9 %union..0anon = type { %struct.int16x8x2_t }
10
11 define arm_apcscc void @test(<8 x i16> %tmp.0, %struct.int16x8x2_t* %dst) nounwind {
12 ; CHECK: @test
13 ; CHECK-NOT: alloca
14 ; CHECK: "alloca point"
15 entry:
16 %tmp_addr = alloca %struct.int16x8_t ; <%struct.int16x8_t*> [#uses=3]
17 %dst_addr = alloca %struct.int16x8x2_t* ; <%struct.int16x8x2_t**> [#uses=2]
18 %__rv = alloca %union..0anon ; <%union..0anon*> [#uses=2]
19 %__bx = alloca %struct.int16x8_t ; <%struct.int16x8_t*> [#uses=2]
20 %__ax = alloca %struct.int16x8_t ; <%struct.int16x8_t*> [#uses=2]
21 %tmp2 = alloca %struct.int16x8x2_t ; <%struct.int16x8x2_t*> [#uses=2]
22 %0 = alloca %struct.int16x8x2_t ; <%struct.int16x8x2_t*> [#uses=2]
23 %"alloca point" = bitcast i32 0 to i32 ; [#uses=0]
24 %1 = getelementptr inbounds %struct.int16x8_t* %tmp_addr, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
25 store <8 x i16> %tmp.0, <8 x i16>* %1
26 store %struct.int16x8x2_t* %dst, %struct.int16x8x2_t** %dst_addr
27 %2 = getelementptr inbounds %struct.int16x8_t* %__ax, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
28 %3 = getelementptr inbounds %struct.int16x8_t* %tmp_addr, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
29 %4 = load <8 x i16>* %3, align 16 ; <<8 x i16>> [#uses=1]
30 store <8 x i16> %4, <8 x i16>* %2, align 16
31 %5 = getelementptr inbounds %struct.int16x8_t* %__bx, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
32 %6 = getelementptr inbounds %struct.int16x8_t* %tmp_addr, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
33 %7 = load <8 x i16>* %6, align 16 ; <<8 x i16>> [#uses=1]
34 store <8 x i16> %7, <8 x i16>* %5, align 16
35 %8 = getelementptr inbounds %struct.int16x8_t* %__ax, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
36 %9 = load <8 x i16>* %8, align 16 ; <<8 x i16>> [#uses=2]
37 %10 = getelementptr inbounds %struct.int16x8_t* %__bx, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
38 %11 = load <8 x i16>* %10, align 16 ; <<8 x i16>> [#uses=2]
39 %12 = getelementptr inbounds %union..0anon* %__rv, i32 0, i32 0 ; <%struct.int16x8x2_t*> [#uses=1]
40 %13 = bitcast %struct.int16x8x2_t* %12 to %struct.__neon_int16x8x2_t* ; <%struct.__neon_int16x8x2_t*> [#uses=2]
41 %14 = shufflevector <8 x i16> %9, <8 x i16> %11, <8 x i32> ; <<8 x i16>> [#uses=1]
42 %15 = getelementptr inbounds %struct.__neon_int16x8x2_t* %13, i32 0, i32 0 ; <<8 x i16>*> [#uses=1]
43 store <8 x i16> %14, <8 x i16>* %15
44 %16 = shufflevector <8 x i16> %9, <8 x i16> %11, <8 x i32> ; <<8 x i16>> [#uses=1]
45 %17 = getelementptr inbounds %struct.__neon_int16x8x2_t* %13, i32 0, i32 1 ; <<8 x i16>*> [#uses=1]
46 store <8 x i16> %16, <8 x i16>* %17
47 %18 = getelementptr inbounds %union..0anon* %__rv, i32 0, i32 0 ; <%struct.int16x8x2_t*> [#uses=1]
48 %19 = bitcast %struct.int16x8x2_t* %0 to i8* ; [#uses=1]
49 %20 = bitcast %struct.int16x8x2_t* %18 to i8* ; [#uses=1]
50 call void @llvm.memcpy.i32(i8* %19, i8* %20, i32 32, i32 16)
51 %tmp21 = bitcast %struct.int16x8x2_t* %tmp2 to i8* ; [#uses=1]
52 %21 = bitcast %struct.int16x8x2_t* %0 to i8* ; [#uses=1]
53 call void @llvm.memcpy.i32(i8* %tmp21, i8* %21, i32 32, i32 16)
54 %22 = load %struct.int16x8x2_t** %dst_addr, align 4 ; <%struct.int16x8x2_t*> [#uses=1]
55 %23 = bitcast %struct.int16x8x2_t* %22 to i8* ; [#uses=1]
56 %tmp22 = bitcast %struct.int16x8x2_t* %tmp2 to i8* ; [#uses=1]
57 call void @llvm.memcpy.i32(i8* %23, i8* %tmp22, i32 32, i32 16)
58 br label %return
59
60 ; CHECK: store <8 x i16>
61 ; CHECK: store <8 x i16>
62
63 return: ; preds = %entry
64 ret void
65 }
66
67 ; Radar 7466574
68 %struct._NSRange = type { i64 }
69
70 define arm_apcscc void @test_memcpy_self() nounwind {
71 ; CHECK: @test_memcpy_self
72 ; CHECK-NOT: alloca
73 ; CHECK: br i1
74 entry:
75 %range = alloca %struct._NSRange ; <%struct._NSRange*> [#uses=2]
76 br i1 undef, label %cond.true, label %cond.false
77
78 cond.true: ; preds = %entry
79 %tmp3 = bitcast %struct._NSRange* %range to i8* ; [#uses=1]
80 %tmp4 = bitcast %struct._NSRange* %range to i8* ; [#uses=1]
81 call void @llvm.memcpy.i32(i8* %tmp3, i8* %tmp4, i32 8, i32 8)
82 ret void
83
84 cond.false: ; preds = %entry
85 ret void
86 }
87
88 declare void @llvm.memcpy.i32(i8* nocapture, i8* nocapture, i32, i32) nounwind