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