Skip to content

Commit b4dd928

Browse files
committed
[InstCombine] Make combineLoadToNewType a method; NFC
So it can be reused as part of other combines. In particular for D71164.
1 parent 652cd7c commit b4dd928

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner
467467
/// \return true if successful.
468468
bool replacePointer(Instruction &I, Value *V);
469469

470+
LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
471+
const Twine &Suffix = "");
472+
470473
private:
471474
bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
472475
bool shouldChangeType(Type *From, Type *To) const;

llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ static bool isSupportedAtomicType(Type *Ty) {
449449
///
450450
/// Note that this will create all of the instructions with whatever insert
451451
/// point the \c InstCombiner currently is using.
452-
static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewTy,
453-
const Twine &Suffix = "") {
452+
LoadInst *InstCombiner::combineLoadToNewType(LoadInst &LI, Type *NewTy,
453+
const Twine &Suffix) {
454454
assert((!LI.isAtomic() || isSupportedAtomicType(NewTy)) &&
455455
"can't fold an atomic load to requested type");
456456

@@ -460,16 +460,16 @@ static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewT
460460
if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) &&
461461
NewPtr->getType()->getPointerElementType() == NewTy &&
462462
NewPtr->getType()->getPointerAddressSpace() == AS))
463-
NewPtr = IC.Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS));
463+
NewPtr = Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS));
464464

465465
unsigned Align = LI.getAlignment();
466466
if (!Align)
467467
// If old load did not have an explicit alignment specified,
468468
// manually preserve the implied (ABI) alignment of the load.
469469
// Else we may inadvertently incorrectly over-promise alignment.
470-
Align = IC.getDataLayout().getABITypeAlignment(LI.getType());
470+
Align = getDataLayout().getABITypeAlignment(LI.getType());
471471

472-
LoadInst *NewLoad = IC.Builder.CreateAlignedLoad(
472+
LoadInst *NewLoad = Builder.CreateAlignedLoad(
473473
NewTy, NewPtr, Align, LI.isVolatile(), LI.getName() + Suffix);
474474
NewLoad->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
475475
copyMetadataForLoad(*NewLoad, LI);
@@ -607,9 +607,8 @@ static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) {
607607
return SI && SI->getPointerOperand() != &LI &&
608608
!SI->getPointerOperand()->isSwiftError();
609609
})) {
610-
LoadInst *NewLoad = combineLoadToNewType(
611-
IC, LI,
612-
Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty)));
610+
LoadInst *NewLoad = IC.combineLoadToNewType(
611+
LI, Type::getIntNTy(LI.getContext(), DL.getTypeStoreSizeInBits(Ty)));
613612
// Replace all the stores with stores of the newly loaded value.
614613
for (auto UI = LI.user_begin(), UE = LI.user_end(); UI != UE;) {
615614
auto *SI = cast<StoreInst>(*UI++);
@@ -631,7 +630,7 @@ static Instruction *combineLoadToOperationType(InstCombiner &IC, LoadInst &LI) {
631630
if (auto* CI = dyn_cast<CastInst>(LI.user_back()))
632631
if (CI->isNoopCast(DL))
633632
if (!LI.isAtomic() || isSupportedAtomicType(CI->getDestTy())) {
634-
LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy());
633+
LoadInst *NewLoad = IC.combineLoadToNewType(LI, CI->getDestTy());
635634
CI->replaceAllUsesWith(NewLoad);
636635
IC.eraseInstFromFunction(*CI);
637636
return &LI;
@@ -659,8 +658,8 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
659658
// If the struct only have one element, we unpack.
660659
auto NumElements = ST->getNumElements();
661660
if (NumElements == 1) {
662-
LoadInst *NewLoad = combineLoadToNewType(IC, LI, ST->getTypeAtIndex(0U),
663-
".unpack");
661+
LoadInst *NewLoad = IC.combineLoadToNewType(LI, ST->getTypeAtIndex(0U),
662+
".unpack");
664663
AAMDNodes AAMD;
665664
LI.getAAMetadata(AAMD);
666665
NewLoad->setAAMetadata(AAMD);
@@ -709,7 +708,7 @@ static Instruction *unpackLoadToAggregate(InstCombiner &IC, LoadInst &LI) {
709708
auto *ET = AT->getElementType();
710709
auto NumElements = AT->getNumElements();
711710
if (NumElements == 1) {
712-
LoadInst *NewLoad = combineLoadToNewType(IC, LI, ET, ".unpack");
711+
LoadInst *NewLoad = IC.combineLoadToNewType(LI, ET, ".unpack");
713712
AAMDNodes AAMD;
714713
LI.getAAMetadata(AAMD);
715714
NewLoad->setAAMetadata(AAMD);
@@ -1352,7 +1351,7 @@ static bool removeBitcastsFromLoadStoreOnMinMax(InstCombiner &IC,
13521351
return false;
13531352

13541353
IC.Builder.SetInsertPoint(LI);
1355-
LoadInst *NewLI = combineLoadToNewType(IC, *LI, CmpLoadTy);
1354+
LoadInst *NewLI = IC.combineLoadToNewType(*LI, CmpLoadTy);
13561355
// Replace all the stores with stores of the newly loaded value.
13571356
for (auto *UI : LI->users()) {
13581357
auto *USI = cast<StoreInst>(UI);

0 commit comments

Comments
 (0)