@@ -501,6 +501,8 @@ static bool moveUp(AliasAnalysis &AA, StoreInst *SI, Instruction *P,
501
501
return true ;
502
502
}
503
503
504
+ // / If changes are made, return true and set BBI to the next instruction to
505
+ // / visit.
504
506
bool MemCpyOptPass::processStore (StoreInst *SI, BasicBlock::iterator &BBI) {
505
507
if (!SI->isSimple ()) return false ;
506
508
@@ -578,7 +580,6 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
578
580
LI->eraseFromParent ();
579
581
++NumMemCpyInstr;
580
582
581
- // Make sure we do not invalidate the iterator.
582
583
BBI = M->getIterator ();
583
584
return true ;
584
585
}
@@ -642,7 +643,7 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
642
643
if (Value *ByteVal = isBytewiseValue (V, DL)) {
643
644
if (Instruction *I = tryMergingIntoMemset (SI, SI->getPointerOperand (),
644
645
ByteVal)) {
645
- BBI = I->getIterator (); // Don't invalidate iterator.
646
+ BBI = I->getIterator ();
646
647
return true ;
647
648
}
648
649
@@ -662,7 +663,6 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
662
663
SI->eraseFromParent ();
663
664
NumMemSetInfer++;
664
665
665
- // Make sure we do not invalidate the iterator.
666
666
BBI = M->getIterator ();
667
667
return true ;
668
668
}
@@ -671,13 +671,15 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
671
671
return false ;
672
672
}
673
673
674
+ // / If changes are made, return true and set BBI to the next instruction to
675
+ // / visit.
674
676
bool MemCpyOptPass::processMemSet (MemSetInst *MSI, BasicBlock::iterator &BBI) {
675
677
// See if there is another memset or store neighboring this memset which
676
678
// allows us to widen out the memset to do a single larger store.
677
679
if (isa<ConstantInt>(MSI->getLength ()) && !MSI->isVolatile ())
678
680
if (Instruction *I = tryMergingIntoMemset (MSI, MSI->getDest (),
679
681
MSI->getValue ())) {
680
- BBI = I->getIterator (); // Don't invalidate iterator.
682
+ BBI = I->getIterator ();
681
683
return true ;
682
684
}
683
685
return false ;
@@ -886,27 +888,27 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpy, Value *cpyDest,
886
888
887
889
// / We've found that the (upward scanning) memory dependence of memcpy 'M' is
888
890
// / the memcpy 'MDep'. Try to simplify M to copy from MDep's input if we can.
889
- bool MemCpyOptPass::processMemCpyMemCpyDependence (MemCpyInst *M,
890
- MemCpyInst *MDep) {
891
+ Instruction * MemCpyOptPass::processMemCpyMemCpyDependence (MemCpyInst *M,
892
+ MemCpyInst *MDep) {
891
893
// We can only transforms memcpy's where the dest of one is the source of the
892
894
// other.
893
895
if (M->getSource () != MDep->getDest () || MDep->isVolatile ())
894
- return false ;
896
+ return nullptr ;
895
897
896
898
// If dep instruction is reading from our current input, then it is a noop
897
899
// transfer and substituting the input won't change this instruction. Just
898
900
// ignore the input and let someone else zap MDep. This handles cases like:
899
901
// memcpy(a <- a)
900
902
// memcpy(b <- a)
901
903
if (M->getSource () == MDep->getSource ())
902
- return false ;
904
+ return nullptr ;
903
905
904
906
// Second, the length of the memcpy's must be the same, or the preceding one
905
907
// must be larger than the following one.
906
908
ConstantInt *MDepLen = dyn_cast<ConstantInt>(MDep->getLength ());
907
909
ConstantInt *MLen = dyn_cast<ConstantInt>(M->getLength ());
908
910
if (!MDepLen || !MLen || MDepLen->getZExtValue () < MLen->getZExtValue ())
909
- return false ;
911
+ return nullptr ;
910
912
911
913
AliasAnalysis &AA = LookupAliasAnalysis ();
912
914
@@ -926,7 +928,7 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
926
928
MD->getPointerDependencyFrom (MemoryLocation::getForSource (MDep), false ,
927
929
M->getIterator (), M->getParent ());
928
930
if (!SourceDep.isClobber () || SourceDep.getInst () != MDep)
929
- return false ;
931
+ return nullptr ;
930
932
931
933
// If the dest of the second might alias the source of the first, then the
932
934
// source and dest might overlap. We still want to eliminate the intermediate
@@ -943,20 +945,21 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
943
945
// TODO: Is this worth it if we're creating a less aligned memcpy? For
944
946
// example we could be moving from movaps -> movq on x86.
945
947
IRBuilder<> Builder (M);
948
+ Instruction *MC;
946
949
if (UseMemMove)
947
- Builder.CreateMemMove (M->getRawDest (), M->getDestAlign (),
948
- MDep->getRawSource (), MDep->getSourceAlign (),
949
- M->getLength (), M->isVolatile ());
950
+ MC = Builder.CreateMemMove (M->getRawDest (), M->getDestAlign (),
951
+ MDep->getRawSource (), MDep->getSourceAlign (),
952
+ M->getLength (), M->isVolatile ());
950
953
else
951
- Builder.CreateMemCpy (M->getRawDest (), M->getDestAlign (),
952
- MDep->getRawSource (), MDep->getSourceAlign (),
953
- M->getLength (), M->isVolatile ());
954
+ MC = Builder.CreateMemCpy (M->getRawDest (), M->getDestAlign (),
955
+ MDep->getRawSource (), MDep->getSourceAlign (),
956
+ M->getLength (), M->isVolatile ());
954
957
955
958
// Remove the instruction we're replacing.
956
959
MD->removeInstruction (M);
957
960
M->eraseFromParent ();
958
961
++NumMemCpyInstr;
959
- return true ;
962
+ return MC ;
960
963
}
961
964
962
965
// / We've found that the (upward scanning) memory dependence of \p MemCpy is
@@ -973,18 +976,18 @@ bool MemCpyOptPass::processMemCpyMemCpyDependence(MemCpyInst *M,
973
976
// / memcpy(dst, src, src_size);
974
977
// / memset(dst + src_size, c, dst_size <= src_size ? 0 : dst_size - src_size);
975
978
// / \endcode
976
- bool MemCpyOptPass::processMemSetMemCpyDependence (MemCpyInst *MemCpy,
977
- MemSetInst *MemSet) {
979
+ Instruction * MemCpyOptPass::processMemSetMemCpyDependence (MemCpyInst *MemCpy,
980
+ MemSetInst *MemSet) {
978
981
// We can only transform memset/memcpy with the same destination.
979
982
if (MemSet->getDest () != MemCpy->getDest ())
980
- return false ;
983
+ return nullptr ;
981
984
982
985
// Check that there are no other dependencies on the memset destination.
983
986
MemDepResult DstDepInfo =
984
987
MD->getPointerDependencyFrom (MemoryLocation::getForDest (MemSet), false ,
985
988
MemCpy->getIterator (), MemCpy->getParent ());
986
989
if (DstDepInfo.getInst () != MemSet)
987
- return false ;
990
+ return nullptr ;
988
991
989
992
// Use the same i8* dest as the memcpy, killing the memset dest if different.
990
993
Value *Dest = MemCpy->getRawDest ();
@@ -1016,14 +1019,14 @@ bool MemCpyOptPass::processMemSetMemCpyDependence(MemCpyInst *MemCpy,
1016
1019
Value *SizeDiff = Builder.CreateSub (DestSize, SrcSize);
1017
1020
Value *MemsetLen = Builder.CreateSelect (
1018
1021
Ule, ConstantInt::getNullValue (DestSize->getType ()), SizeDiff);
1019
- Builder.CreateMemSet (
1022
+ auto *MS = Builder.CreateMemSet (
1020
1023
Builder.CreateGEP (Dest->getType ()->getPointerElementType (), Dest,
1021
1024
SrcSize),
1022
1025
MemSet->getOperand (1 ), MemsetLen, MaybeAlign (Align));
1023
1026
1024
1027
MD->removeInstruction (MemSet);
1025
1028
MemSet->eraseFromParent ();
1026
- return true ;
1029
+ return MS ;
1027
1030
}
1028
1031
1029
1032
// / Determine whether the instruction has undefined content for the given Size,
@@ -1055,19 +1058,19 @@ static bool hasUndefContents(Instruction *I, ConstantInt *Size) {
1055
1058
// / When dst2_size <= dst1_size.
1056
1059
// /
1057
1060
// / The \p MemCpy must have a Constant length.
1058
- bool MemCpyOptPass::performMemCpyToMemSetOptzn (MemCpyInst *MemCpy,
1059
- MemSetInst *MemSet) {
1061
+ Instruction * MemCpyOptPass::performMemCpyToMemSetOptzn (MemCpyInst *MemCpy,
1062
+ MemSetInst *MemSet) {
1060
1063
AliasAnalysis &AA = LookupAliasAnalysis ();
1061
1064
1062
1065
// Make sure that memcpy(..., memset(...), ...), that is we are memsetting and
1063
1066
// memcpying from the same address. Otherwise it is hard to reason about.
1064
1067
if (!AA.isMustAlias (MemSet->getRawDest (), MemCpy->getRawSource ()))
1065
- return false ;
1068
+ return nullptr ;
1066
1069
1067
1070
// A known memset size is required.
1068
1071
ConstantInt *MemSetSize = dyn_cast<ConstantInt>(MemSet->getLength ());
1069
1072
if (!MemSetSize)
1070
- return false ;
1073
+ return nullptr ;
1071
1074
1072
1075
// Make sure the memcpy doesn't read any more than what the memset wrote.
1073
1076
// Don't worry about sizes larger than i64.
@@ -1083,54 +1086,62 @@ bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1083
1086
if (DepInfo.isDef () && hasUndefContents (DepInfo.getInst (), CopySize))
1084
1087
CopySize = MemSetSize;
1085
1088
else
1086
- return false ;
1089
+ return nullptr ;
1087
1090
}
1088
1091
1089
1092
IRBuilder<> Builder (MemCpy);
1090
- Builder.CreateMemSet (MemCpy->getRawDest (), MemSet->getOperand (1 ), CopySize,
1091
- MaybeAlign (MemCpy->getDestAlignment ()));
1092
- return true ;
1093
+ return Builder.CreateMemSet (MemCpy->getRawDest (), MemSet->getOperand (1 ),
1094
+ CopySize, MaybeAlign (MemCpy->getDestAlignment ()));
1093
1095
}
1094
1096
1095
1097
// / Perform simplification of memcpy's. If we have memcpy A
1096
1098
// / which copies X to Y, and memcpy B which copies Y to Z, then we can rewrite
1097
1099
// / B to be a memcpy from X to Z (or potentially a memmove, depending on
1098
1100
// / circumstances). This allows later passes to remove the first memcpy
1099
1101
// / altogether.
1102
+ // / If changes are made, return true and set BBI to the next instruction to
1103
+ // / visit.
1100
1104
bool MemCpyOptPass::processMemCpy (MemCpyInst *M, BasicBlock::iterator &BBI) {
1101
1105
// We can only optimize non-volatile memcpy's.
1102
1106
if (M->isVolatile ()) return false ;
1103
1107
1104
1108
// If the source and destination of the memcpy are the same, then zap it.
1105
1109
if (M->getSource () == M->getDest ()) {
1106
- ++BBI;
1107
1110
MD->removeInstruction (M);
1108
1111
M->eraseFromParent ();
1109
1112
return true ;
1110
1113
}
1111
1114
1112
1115
// If copying from a constant, try to turn the memcpy into a memset.
1113
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource ()))
1114
- if (GV->isConstant () && GV->hasDefinitiveInitializer ())
1116
+ if (GlobalVariable *GV = dyn_cast<GlobalVariable>(M->getSource ())) {
1117
+ if (GV->isConstant () && GV->hasDefinitiveInitializer ()) {
1115
1118
if (Value *ByteVal = isBytewiseValue (GV->getInitializer (),
1116
1119
M->getModule ()->getDataLayout ())) {
1117
1120
IRBuilder<> Builder (M);
1118
- Builder.CreateMemSet (M->getRawDest (), ByteVal, M->getLength (),
1119
- MaybeAlign (M->getDestAlignment ()), false );
1121
+ auto *MS =
1122
+ Builder.CreateMemSet (M->getRawDest (), ByteVal, M->getLength (),
1123
+ MaybeAlign (M->getDestAlignment ()), false );
1120
1124
MD->removeInstruction (M);
1121
1125
M->eraseFromParent ();
1122
1126
++NumCpyToSet;
1127
+ BBI = MS->getIterator ();
1123
1128
return true ;
1124
1129
}
1130
+ }
1131
+ }
1125
1132
1126
1133
MemDepResult DepInfo = MD->getDependency (M);
1127
1134
1128
1135
// Try to turn a partially redundant memset + memcpy into
1129
1136
// memcpy + smaller memset. We don't need the memcpy size for this.
1130
- if (DepInfo.isClobber ())
1131
- if (MemSetInst *MDep = dyn_cast<MemSetInst>(DepInfo.getInst ()))
1132
- if (processMemSetMemCpyDependence (M, MDep))
1137
+ if (DepInfo.isClobber ()) {
1138
+ if (MemSetInst *MDep = dyn_cast<MemSetInst>(DepInfo.getInst ())) {
1139
+ if (auto *MS = processMemSetMemCpyDependence (M, MDep)) {
1140
+ BBI = MS->getIterator ();
1133
1141
return true ;
1142
+ }
1143
+ }
1144
+ }
1134
1145
1135
1146
// The optimizations after this point require the memcpy size.
1136
1147
ConstantInt *CopySize = dyn_cast<ConstantInt>(M->getLength ());
@@ -1163,8 +1174,13 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
1163
1174
SrcLoc, true , M->getIterator (), M->getParent ());
1164
1175
1165
1176
if (SrcDepInfo.isClobber ()) {
1166
- if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst ()))
1167
- return processMemCpyMemCpyDependence (M, MDep);
1177
+ if (MemCpyInst *MDep = dyn_cast<MemCpyInst>(SrcDepInfo.getInst ())) {
1178
+ if (auto *MC = processMemCpyMemCpyDependence (M, MDep)) {
1179
+ BBI = MC->getIterator ();
1180
+ return true ;
1181
+ }
1182
+ return false ;
1183
+ }
1168
1184
} else if (SrcDepInfo.isDef ()) {
1169
1185
if (hasUndefContents (SrcDepInfo.getInst (), CopySize)) {
1170
1186
MD->removeInstruction (M);
@@ -1176,10 +1192,11 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
1176
1192
1177
1193
if (SrcDepInfo.isClobber ())
1178
1194
if (MemSetInst *MDep = dyn_cast<MemSetInst>(SrcDepInfo.getInst ()))
1179
- if (performMemCpyToMemSetOptzn (M, MDep)) {
1195
+ if (auto *MS = performMemCpyToMemSetOptzn (M, MDep)) {
1180
1196
MD->removeInstruction (M);
1181
1197
M->eraseFromParent ();
1182
1198
++NumCpyToSet;
1199
+ BBI = MS->getIterator ();
1183
1200
return true ;
1184
1201
}
1185
1202
@@ -1188,7 +1205,9 @@ bool MemCpyOptPass::processMemCpy(MemCpyInst *M, BasicBlock::iterator &BBI) {
1188
1205
1189
1206
// / Transforms memmove calls to memcpy calls when the src/dst are guaranteed
1190
1207
// / not to alias.
1191
- bool MemCpyOptPass::processMemMove (MemMoveInst *M) {
1208
+ // / If changes are made, return true and set BBI to the next instruction to
1209
+ // / visit.
1210
+ bool MemCpyOptPass::processMemMove (MemMoveInst *M, BasicBlock::iterator &BBI) {
1192
1211
AliasAnalysis &AA = LookupAliasAnalysis ();
1193
1212
1194
1213
if (!TLI->has (LibFunc_memmove))
@@ -1214,6 +1233,7 @@ bool MemCpyOptPass::processMemMove(MemMoveInst *M) {
1214
1233
MD->removeInstruction (M);
1215
1234
1216
1235
++NumMoveToCpy;
1236
+ BBI = M->getIterator ();
1217
1237
return true ;
1218
1238
}
1219
1239
@@ -1316,28 +1336,19 @@ bool MemCpyOptPass::iterateOnFunction(Function &F) {
1316
1336
// Avoid invalidating the iterator.
1317
1337
Instruction *I = &*BI++;
1318
1338
1319
- bool RepeatInstruction = false ;
1320
-
1321
1339
if (StoreInst *SI = dyn_cast<StoreInst>(I))
1322
1340
MadeChange |= processStore (SI, BI);
1323
1341
else if (MemSetInst *M = dyn_cast<MemSetInst>(I))
1324
- RepeatInstruction = processMemSet (M, BI);
1342
+ MadeChange = processMemSet (M, BI);
1325
1343
else if (MemCpyInst *M = dyn_cast<MemCpyInst>(I))
1326
- RepeatInstruction = processMemCpy (M, BI);
1344
+ MadeChange = processMemCpy (M, BI);
1327
1345
else if (MemMoveInst *M = dyn_cast<MemMoveInst>(I))
1328
- RepeatInstruction = processMemMove (M);
1346
+ MadeChange = processMemMove (M, BI );
1329
1347
else if (auto *CB = dyn_cast<CallBase>(I)) {
1330
1348
for (unsigned i = 0 , e = CB->arg_size (); i != e; ++i)
1331
1349
if (CB->isByValArgument (i))
1332
1350
MadeChange |= processByValArgument (*CB, i);
1333
1351
}
1334
-
1335
- // Reprocess the instruction if desired.
1336
- if (RepeatInstruction) {
1337
- if (BI != BB.begin ())
1338
- --BI;
1339
- MadeChange = true ;
1340
- }
1341
1352
}
1342
1353
}
1343
1354
0 commit comments