@@ -99,7 +99,7 @@ struct MemsetRange {
99
99
MaybeAlign Alignment;
100
100
101
101
// / TheStores - The actual stores that make up this range.
102
- SmallVector<Instruction*, 16 > TheStores;
102
+ SmallVector<Instruction *, 16 > TheStores;
103
103
104
104
bool isProfitableToUseMemset (const DataLayout &DL) const ;
105
105
};
@@ -108,10 +108,12 @@ struct MemsetRange {
108
108
109
109
bool MemsetRange::isProfitableToUseMemset (const DataLayout &DL) const {
110
110
// If we found more than 4 stores to merge or 16 bytes, use memset.
111
- if (TheStores.size () >= 4 || End-Start >= 16 ) return true ;
111
+ if (TheStores.size () >= 4 || End - Start >= 16 )
112
+ return true ;
112
113
113
114
// If there is nothing to merge, don't do anything.
114
- if (TheStores.size () < 2 ) return false ;
115
+ if (TheStores.size () < 2 )
116
+ return false ;
115
117
116
118
// If any of the stores are a memset, then it is always good to extend the
117
119
// memset.
@@ -121,7 +123,8 @@ bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
121
123
122
124
// Assume that the code generator is capable of merging pairs of stores
123
125
// together if it wants to.
124
- if (TheStores.size () == 2 ) return false ;
126
+ if (TheStores.size () == 2 )
127
+ return false ;
125
128
126
129
// If we have fewer than 8 stores, it can still be worthwhile to do this.
127
130
// For example, merging 4 i8 stores into an i32 store is useful almost always.
@@ -133,7 +136,7 @@ bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
133
136
// the maximum GPR width is the same size as the largest legal integer
134
137
// size. If so, check to see whether we will end up actually reducing the
135
138
// number of stores used.
136
- unsigned Bytes = unsigned (End- Start);
139
+ unsigned Bytes = unsigned (End - Start);
137
140
unsigned MaxIntSize = DL.getLargestLegalIntTypeSizeInBits () / 8 ;
138
141
if (MaxIntSize == 0 )
139
142
MaxIntSize = 1 ;
@@ -145,7 +148,7 @@ bool MemsetRange::isProfitableToUseMemset(const DataLayout &DL) const {
145
148
// If we will reduce the # stores (according to this heuristic), do the
146
149
// transformation. This encourages merging 4 x i8 -> i32 and 2 x i16 -> i32
147
150
// etc.
148
- return TheStores.size () > NumPointerStores+ NumByteStores;
151
+ return TheStores.size () > NumPointerStores + NumByteStores;
149
152
}
150
153
151
154
namespace {
@@ -197,7 +200,7 @@ class MemsetRanges {
197
200
// / existing ranges as appropriate.
198
201
void MemsetRanges::addRange (int64_t Start, int64_t Size , Value *Ptr ,
199
202
MaybeAlign Alignment, Instruction *Inst) {
200
- int64_t End = Start+ Size ;
203
+ int64_t End = Start + Size ;
201
204
202
205
range_iterator I = partition_point (
203
206
Ranges, [=](const MemsetRange &O) { return O.End < Start; });
@@ -207,10 +210,10 @@ void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
207
210
// to insert a new range. Handle this now.
208
211
if (I == Ranges.end () || End < I->Start ) {
209
212
MemsetRange &R = *Ranges.insert (I, MemsetRange ());
210
- R.Start = Start;
211
- R.End = End;
212
- R.StartPtr = Ptr ;
213
- R.Alignment = Alignment;
213
+ R.Start = Start;
214
+ R.End = End;
215
+ R.StartPtr = Ptr ;
216
+ R.Alignment = Alignment;
214
217
R.TheStores .push_back (Inst);
215
218
return ;
216
219
}
@@ -397,7 +400,8 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
397
400
398
401
if (auto *NextStore = dyn_cast<StoreInst>(BI)) {
399
402
// If this is a store, see if we can merge it in.
400
- if (!NextStore->isSimple ()) break ;
403
+ if (!NextStore->isSimple ())
404
+ break ;
401
405
402
406
Value *StoredVal = NextStore->getValueOperand ();
403
407
@@ -460,7 +464,8 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
460
464
// emit memset's for anything big enough to be worthwhile.
461
465
Instruction *AMemSet = nullptr ;
462
466
for (const MemsetRange &Range : Ranges) {
463
- if (Range.TheStores .size () == 1 ) continue ;
467
+ if (Range.TheStores .size () == 1 )
468
+ continue ;
464
469
465
470
// If it is profitable to lower this range to memset, do so now.
466
471
if (!Range.isProfitableToUseMemset (DL))
@@ -481,12 +486,10 @@ Instruction *MemCpyOptPass::tryMergingIntoMemset(Instruction *StartInst,
481
486
if (!Range.TheStores .empty ())
482
487
AMemSet->setDebugLoc (Range.TheStores [0 ]->getDebugLoc ());
483
488
484
- auto *NewDef =
485
- cast<MemoryDef>(MemInsertPoint->getMemoryInst () == &*BI
486
- ? MSSAU->createMemoryAccessBefore (
487
- AMemSet, nullptr , MemInsertPoint)
488
- : MSSAU->createMemoryAccessAfter (
489
- AMemSet, nullptr , MemInsertPoint));
489
+ auto *NewDef = cast<MemoryDef>(
490
+ MemInsertPoint->getMemoryInst () == &*BI
491
+ ? MSSAU->createMemoryAccessBefore (AMemSet, nullptr , MemInsertPoint)
492
+ : MSSAU->createMemoryAccessAfter (AMemSet, nullptr , MemInsertPoint));
490
493
MSSAU->insertDef (NewDef, /* RenameUses=*/ true );
491
494
MemInsertPoint = NewDef;
492
495
@@ -512,12 +515,13 @@ bool MemCpyOptPass::moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI) {
512
515
513
516
// Keep track of the arguments of all instruction we plan to lift
514
517
// so we can make sure to lift them as well if appropriate.
515
- DenseSet<Instruction*> Args;
518
+ DenseSet<Instruction *> Args;
516
519
auto AddArg = [&](Value *Arg) {
517
520
auto *I = dyn_cast<Instruction>(Arg);
518
521
if (I && I->getParent () == SI->getParent ()) {
519
522
// Cannot hoist user of P above P
520
- if (I == P) return false ;
523
+ if (I == P)
524
+ return false ;
521
525
Args.insert (I);
522
526
}
523
527
return true ;
@@ -630,8 +634,7 @@ bool MemCpyOptPass::moveUp(StoreInst *SI, Instruction *P, const LoadInst *LI) {
630
634
bool MemCpyOptPass::processStoreOfLoad (StoreInst *SI, LoadInst *LI,
631
635
const DataLayout &DL,
632
636
BasicBlock::iterator &BBI) {
633
- if (!LI->isSimple () || !LI->hasOneUse () ||
634
- LI->getParent () != SI->getParent ())
637
+ if (!LI->isSimple () || !LI->hasOneUse () || LI->getParent () != SI->getParent ())
635
638
return false ;
636
639
637
640
auto *T = LI->getType ();
@@ -678,21 +681,20 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
678
681
UseMemMove = true ;
679
682
680
683
IRBuilder<> Builder (P);
681
- Value *Size = Builder. CreateTypeSize (Builder. getInt64Ty (),
682
- DL.getTypeStoreSize (T));
684
+ Value *Size =
685
+ Builder. CreateTypeSize (Builder. getInt64Ty (), DL.getTypeStoreSize (T));
683
686
Instruction *M;
684
687
if (UseMemMove)
685
- M = Builder.CreateMemMove (
686
- SI ->getPointerOperand (), SI ->getAlign (),
687
- LI-> getPointerOperand (), LI-> getAlign (), Size );
688
+ M = Builder.CreateMemMove (SI-> getPointerOperand (), SI-> getAlign (),
689
+ LI ->getPointerOperand (), LI ->getAlign (),
690
+ Size );
688
691
else
689
- M = Builder.CreateMemCpy (
690
- SI->getPointerOperand (), SI->getAlign (),
691
- LI->getPointerOperand (), LI->getAlign (), Size );
692
+ M = Builder.CreateMemCpy (SI->getPointerOperand (), SI->getAlign (),
693
+ LI->getPointerOperand (), LI->getAlign (), Size );
692
694
M->copyMetadata (*SI, LLVMContext::MD_DIAssignID);
693
695
694
- LLVM_DEBUG (dbgs () << " Promoting " << *LI << " to " << *SI << " => "
695
- << *M << " \n " );
696
+ LLVM_DEBUG (dbgs () << " Promoting " << *LI << " to " << *SI << " => " << *M
697
+ << " \n " );
696
698
697
699
auto *LastDef =
698
700
cast<MemoryDef>(MSSAU->getMemorySSA ()->getMemoryAccess (SI));
@@ -755,7 +757,8 @@ bool MemCpyOptPass::processStoreOfLoad(StoreInst *SI, LoadInst *LI,
755
757
}
756
758
757
759
bool MemCpyOptPass::processStore (StoreInst *SI, BasicBlock::iterator &BBI) {
758
- if (!SI->isSimple ()) return false ;
760
+ if (!SI->isSimple ())
761
+ return false ;
759
762
760
763
// Avoid merging nontemporal stores since the resulting
761
764
// memcpy/memset would not be able to preserve the nontemporal hint.
@@ -794,8 +797,8 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
794
797
// 0xA0A0A0A0 and 0.0.
795
798
auto *V = SI->getOperand (0 );
796
799
if (Value *ByteVal = isBytewiseValue (V, DL)) {
797
- if (Instruction *I = tryMergingIntoMemset (SI, SI-> getPointerOperand (),
798
- ByteVal)) {
800
+ if (Instruction *I =
801
+ tryMergingIntoMemset (SI, SI-> getPointerOperand (), ByteVal)) {
799
802
BBI = I->getIterator (); // Don't invalidate iterator.
800
803
return true ;
801
804
}
@@ -816,8 +819,7 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
816
819
// The newly inserted memset is immediately overwritten by the original
817
820
// store, so we do not need to rename uses.
818
821
auto *StoreDef = cast<MemoryDef>(MSSA->getMemoryAccess (SI));
819
- auto *NewAccess = MSSAU->createMemoryAccessBefore (
820
- M, nullptr , StoreDef);
822
+ auto *NewAccess = MSSAU->createMemoryAccessBefore (M, nullptr , StoreDef);
821
823
MSSAU->insertDef (cast<MemoryDef>(NewAccess), /* RenameUses=*/ false );
822
824
823
825
eraseInstruction (SI);
@@ -836,8 +838,8 @@ bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
836
838
// See if there is another memset or store neighboring this memset which
837
839
// allows us to widen out the memset to do a single larger store.
838
840
if (isa<ConstantInt>(MSI->getLength ()) && !MSI->isVolatile ())
839
- if (Instruction *I = tryMergingIntoMemset (MSI, MSI-> getDest (),
840
- MSI->getValue ())) {
841
+ if (Instruction *I =
842
+ tryMergingIntoMemset (MSI, MSI-> getDest (), MSI->getValue ())) {
841
843
BBI = I->getIterator (); // Don't invalidate iterator.
842
844
return true ;
843
845
}
@@ -850,7 +852,8 @@ bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
850
852
bool MemCpyOptPass::performCallSlotOptzn (Instruction *cpyLoad,
851
853
Instruction *cpyStore, Value *cpyDest,
852
854
Value *cpySrc, TypeSize cpySize,
853
- Align cpyDestAlign, BatchAAResults &BAA,
855
+ Align cpyDestAlign,
856
+ BatchAAResults &BAA,
854
857
std::function<CallInst *()> GetC) {
855
858
// The general transformation to keep in mind is
856
859
//
@@ -898,15 +901,15 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
898
901
if (F->isIntrinsic () && F->getIntrinsicID () == Intrinsic::lifetime_start)
899
902
return false ;
900
903
901
-
902
904
if (C->getParent () != cpyStore->getParent ()) {
903
905
LLVM_DEBUG (dbgs () << " Call Slot: block local restriction\n " );
904
906
return false ;
905
907
}
906
908
907
- MemoryLocation DestLoc = isa<StoreInst>(cpyStore) ?
908
- MemoryLocation::get (cpyStore) :
909
- MemoryLocation::getForDest (cast<MemCpyInst>(cpyStore));
909
+ MemoryLocation DestLoc =
910
+ isa<StoreInst>(cpyStore)
911
+ ? MemoryLocation::get (cpyStore)
912
+ : MemoryLocation::getForDest (cast<MemCpyInst>(cpyStore));
910
913
911
914
// Check that nothing touches the dest of the copy between
912
915
// the call and the store/memcpy.
@@ -1175,7 +1178,8 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
1175
1178
1176
1179
// If all checks passed, then we can transform M.
1177
1180
LLVM_DEBUG (dbgs () << " MemCpyOptPass: Forwarding memcpy->memcpy src:\n "
1178
- << *MDep << ' \n ' << *M << ' \n ' );
1181
+ << *MDep << ' \n '
1182
+ << *M << ' \n ' );
1179
1183
1180
1184
// TODO: Is this worth it if we're creating a less aligned memcpy? For
1181
1185
// example we could be moving from movaps -> movq on x86.
@@ -1307,8 +1311,8 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1307
1311
// memcpy's defining access is the memset about to be removed.
1308
1312
auto *LastDef =
1309
1313
cast<MemoryDef>(MSSAU->getMemorySSA ()->getMemoryAccess (MemCpy));
1310
- auto *NewAccess = MSSAU-> createMemoryAccessBefore (
1311
- NewMemSet, nullptr , LastDef);
1314
+ auto *NewAccess =
1315
+ MSSAU-> createMemoryAccessBefore ( NewMemSet, nullptr , LastDef);
1312
1316
MSSAU->insertDef (cast<MemoryDef>(NewAccess), /* RenameUses=*/ true );
1313
1317
1314
1318
eraseInstruction (MemSet);
@@ -1384,7 +1388,7 @@ bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1384
1388
return false ;
1385
1389
1386
1390
// A known memcpy size is also required.
1387
- auto *CCopySize = dyn_cast<ConstantInt>(CopySize);
1391
+ auto *CCopySize = dyn_cast<ConstantInt>(CopySize);
1388
1392
if (!CCopySize)
1389
1393
return false ;
1390
1394
if (CCopySize->getZExtValue () > CMemSetSize->getZExtValue ()) {
@@ -1655,7 +1659,8 @@ static bool isZeroSize(Value *Size) {
1655
1659
// / altogether.
1656
1660
bool MemCpyOptPass::processMemCpy (MemCpyInst *M, BasicBlock::iterator &BBI) {
1657
1661
// We can only optimize non-volatile memcpy's.
1658
- if (M->isVolatile ()) return false ;
1662
+ if (M->isVolatile ())
1663
+ return false ;
1659
1664
1660
1665
// If the source and destination of the memcpy are the same, then zap it.
1661
1666
if (M->getSource () == M->getDest ()) {
@@ -1796,11 +1801,10 @@ bool MemCpyOptPass::processMemMove(MemMoveInst *M) {
1796
1801
<< " \n " );
1797
1802
1798
1803
// If not, then we know we can transform this.
1799
- Type *ArgTys[3 ] = { M->getRawDest ()->getType (),
1800
- M->getRawSource ()->getType (),
1801
- M->getLength ()->getType () };
1802
- M->setCalledFunction (Intrinsic::getDeclaration (M->getModule (),
1803
- Intrinsic::memcpy , ArgTys));
1804
+ Type *ArgTys[3 ] = {M->getRawDest ()->getType (), M->getRawSource ()->getType (),
1805
+ M->getLength ()->getType ()};
1806
+ M->setCalledFunction (
1807
+ Intrinsic::getDeclaration (M->getModule (), Intrinsic::memcpy , ArgTys));
1804
1808
1805
1809
// For MemorySSA nothing really changes (except that memcpy may imply stricter
1806
1810
// aliasing guarantees).
@@ -1843,7 +1847,8 @@ bool MemCpyOptPass::processByValArgument(CallBase &CB, unsigned ArgNo) {
1843
1847
// Get the alignment of the byval. If the call doesn't specify the alignment,
1844
1848
// then it is some target specific value that we can't know.
1845
1849
MaybeAlign ByValAlign = CB.getParamAlign (ArgNo);
1846
- if (!ByValAlign) return false ;
1850
+ if (!ByValAlign)
1851
+ return false ;
1847
1852
1848
1853
// If it is greater than the memcpy, then we check to see if we can force the
1849
1854
// source of the memcpy to the alignment we need. If we fail, we bail out.
@@ -1987,7 +1992,7 @@ bool MemCpyOptPass::iterateOnFunction(Function &F) {
1987
1992
continue ;
1988
1993
1989
1994
for (BasicBlock::iterator BI = BB.begin (), BE = BB.end (); BI != BE;) {
1990
- // Avoid invalidating the iterator.
1995
+ // Avoid invalidating the iterator.
1991
1996
Instruction *I = &*BI++;
1992
1997
1993
1998
bool RepeatInstruction = false ;
0 commit comments