@@ -189,7 +189,7 @@ struct LegacyLICMPass : public LoopPass {
189
189
// / Simple Analysis hook. Delete loop L from alias set map.
190
190
void deleteAnalysisLoop (Loop *L) override ;
191
191
};
192
- }
192
+ } // namespace
193
193
194
194
PreservedAnalyses LICMPass::run (Loop &L, LoopAnalysisManager &AM,
195
195
LoopStandardAnalysisResults &AR, LPMUpdater &) {
@@ -292,10 +292,26 @@ bool LoopInvariantCodeMotion::runOnLoop(Loop *L, AliasAnalysis *AA,
292
292
bool Promoted = false ;
293
293
294
294
// Loop over all of the alias sets in the tracker object.
295
- for (AliasSet &AS : *CurAST)
296
- Promoted |=
297
- promoteLoopAccessesToScalars (AS, ExitBlocks, InsertPts, PIC, LI, DT,
298
- TLI, L, CurAST, &SafetyInfo, ORE);
295
+ for (AliasSet &AS : *CurAST) {
296
+ // We can promote this alias set if it has a store, if it is a "Must"
297
+ // alias set, if the pointer is loop invariant, and if we are not
298
+ // eliminating any volatile loads or stores.
299
+ if (AS.isForwardingAliasSet () || !AS.isMod () || !AS.isMustAlias () ||
300
+ AS.isVolatile () || !L->isLoopInvariant (AS.begin ()->getValue ()))
301
+ continue ;
302
+
303
+ assert (
304
+ !AS.empty () &&
305
+ " Must alias set should have at least one pointer element in it!" );
306
+
307
+ SmallSetVector<Value *, 8 > PointerMustAliases;
308
+ for (const auto &ASI : AS)
309
+ PointerMustAliases.insert (ASI.getValue ());
310
+
311
+ Promoted |= promoteLoopAccessesToScalars (PointerMustAliases, ExitBlocks,
312
+ InsertPts, PIC, LI, DT, TLI, L,
313
+ CurAST, &SafetyInfo, ORE);
314
+ }
299
315
300
316
// Once we have promoted values across the loop body we have to
301
317
// recursively reform LCSSA as any nested loop may now have values defined
@@ -383,8 +399,8 @@ bool llvm::sinkRegion(DomTreeNode *N, AliasAnalysis *AA, LoopInfo *LI,
383
399
for (BasicBlock::iterator II = BB->end (); II != BB->begin ();) {
384
400
Instruction &I = *--II;
385
401
386
- // If the instruction is dead, we would try to sink it because it isn't used
387
- // in the loop, instead, just delete it.
402
+ // If the instruction is dead, we would try to sink it because it isn't
403
+ // used in the loop, instead, just delete it.
388
404
if (isInstructionTriviallyDead (&I, TLI)) {
389
405
DEBUG (dbgs () << " LICM deleting dead inst: " << I << ' \n ' );
390
406
++II;
@@ -509,7 +525,8 @@ void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
509
525
// Iterate over loop instructions and compute safety info.
510
526
// Skip header as it has been computed and stored in HeaderMayThrow.
511
527
// The first block in loopinfo.Blocks is guaranteed to be the header.
512
- assert (Header == *CurLoop->getBlocks ().begin () && " First block must be header" );
528
+ assert (Header == *CurLoop->getBlocks ().begin () &&
529
+ " First block must be header" );
513
530
for (Loop::block_iterator BB = std::next (CurLoop->block_begin ()),
514
531
BBE = CurLoop->block_end ();
515
532
(BB != BBE) && !SafetyInfo->MayThrow ; ++BB)
@@ -527,9 +544,9 @@ void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
527
544
}
528
545
529
546
// Return true if LI is invariant within scope of the loop. LI is invariant if
530
- // CurLoop is dominated by an invariant.start representing the same memory location
531
- // and size as the memory location LI loads from, and also the invariant.start
532
- // has no uses.
547
+ // CurLoop is dominated by an invariant.start representing the same memory
548
+ // location and size as the memory location LI loads from, and also the
549
+ // invariant.start has no uses.
533
550
static bool isLoadInvariantInLoop (LoadInst *LI, DominatorTree *DT,
534
551
Loop *CurLoop) {
535
552
Value *Addr = LI->getOperand (0 );
@@ -950,7 +967,7 @@ static bool isSafeToExecuteUnconditionally(Instruction &Inst,
950
967
namespace {
951
968
class LoopPromoter : public LoadAndStorePromoter {
952
969
Value *SomePtr; // Designated pointer to store to.
953
- SmallPtrSetImpl <Value *> &PointerMustAliases;
970
+ const SmallSetVector <Value *, 8 > &PointerMustAliases;
954
971
SmallVectorImpl<BasicBlock *> &LoopExitBlocks;
955
972
SmallVectorImpl<Instruction *> &LoopInsertPts;
956
973
PredIteratorCache &PredCache;
@@ -978,15 +995,15 @@ class LoopPromoter : public LoadAndStorePromoter {
978
995
979
996
public:
980
997
LoopPromoter (Value *SP, ArrayRef<const Instruction *> Insts, SSAUpdater &S,
981
- SmallPtrSetImpl <Value *> &PMA,
998
+ const SmallSetVector <Value *, 8 > &PMA,
982
999
SmallVectorImpl<BasicBlock *> &LEB,
983
1000
SmallVectorImpl<Instruction *> &LIP, PredIteratorCache &PIC,
984
1001
AliasSetTracker &ast, LoopInfo &li, DebugLoc dl, int alignment,
985
1002
bool UnorderedAtomic, const AAMDNodes &AATags)
986
1003
: LoadAndStorePromoter(Insts, S), SomePtr(SP), PointerMustAliases(PMA),
987
1004
LoopExitBlocks (LEB), LoopInsertPts(LIP), PredCache(PIC), AST(ast),
988
1005
LI(li), DL(std::move(dl)), Alignment(alignment),
989
- UnorderedAtomic(UnorderedAtomic),AATags(AATags) {}
1006
+ UnorderedAtomic(UnorderedAtomic), AATags(AATags) {}
990
1007
991
1008
bool isInstInList (Instruction *I,
992
1009
const SmallVectorImpl<Instruction *> &) const override {
@@ -1025,15 +1042,16 @@ class LoopPromoter : public LoadAndStorePromoter {
1025
1042
}
1026
1043
void instructionDeleted (Instruction *I) const override { AST.deleteValue (I); }
1027
1044
};
1028
- } // end anon namespace
1045
+ } // namespace
1029
1046
1030
1047
// / Try to promote memory values to scalars by sinking stores out of the
1031
1048
// / loop and moving loads to before the loop. We do this by looping over
1032
1049
// / the stores in the loop, looking for stores to Must pointers which are
1033
1050
// / loop invariant.
1034
1051
// /
1035
1052
bool llvm::promoteLoopAccessesToScalars (
1036
- AliasSet &AS, SmallVectorImpl<BasicBlock *> &ExitBlocks,
1053
+ const SmallSetVector<Value *, 8 > &PointerMustAliases,
1054
+ SmallVectorImpl<BasicBlock *> &ExitBlocks,
1037
1055
SmallVectorImpl<Instruction *> &InsertPts, PredIteratorCache &PIC,
1038
1056
LoopInfo *LI, DominatorTree *DT, const TargetLibraryInfo *TLI,
1039
1057
Loop *CurLoop, AliasSetTracker *CurAST, LoopSafetyInfo *SafetyInfo,
@@ -1043,17 +1061,7 @@ bool llvm::promoteLoopAccessesToScalars(
1043
1061
CurAST != nullptr && SafetyInfo != nullptr &&
1044
1062
" Unexpected Input to promoteLoopAccessesToScalars" );
1045
1063
1046
- // We can promote this alias set if it has a store, if it is a "Must" alias
1047
- // set, if the pointer is loop invariant, and if we are not eliminating any
1048
- // volatile loads or stores.
1049
- if (AS.isForwardingAliasSet () || !AS.isMod () || !AS.isMustAlias () ||
1050
- AS.isVolatile () || !CurLoop->isLoopInvariant (AS.begin ()->getValue ()))
1051
- return false ;
1052
-
1053
- assert (!AS.empty () &&
1054
- " Must alias set should have at least one pointer element in it!" );
1055
-
1056
- Value *SomePtr = AS.begin ()->getValue ();
1064
+ Value *SomePtr = *PointerMustAliases.begin ();
1057
1065
BasicBlock *Preheader = CurLoop->getLoopPreheader ();
1058
1066
1059
1067
// It isn't safe to promote a load/store from the loop if the load/store is
@@ -1082,8 +1090,8 @@ bool llvm::promoteLoopAccessesToScalars(
1082
1090
// is safe (i.e. proving dereferenceability on all paths through the loop). We
1083
1091
// can use any access within the alias set to prove dereferenceability,
1084
1092
// since they're all must alias.
1085
- //
1086
- // There are two ways establish (p2):
1093
+ //
1094
+ // There are two ways establish (p2):
1087
1095
// a) Prove the location is thread-local. In this case the memory model
1088
1096
// requirement does not apply, and stores are safe to insert.
1089
1097
// b) Prove a store dominates every exit block. In this case, if an exit
@@ -1097,13 +1105,12 @@ bool llvm::promoteLoopAccessesToScalars(
1097
1105
bool SafeToInsertStore = false ;
1098
1106
1099
1107
SmallVector<Instruction *, 64 > LoopUses;
1100
- SmallPtrSet<Value *, 4 > PointerMustAliases;
1101
1108
1102
1109
// We start with an alignment of one and try to find instructions that allow
1103
1110
// us to prove better alignment.
1104
1111
unsigned Alignment = 1 ;
1105
1112
// Keep track of which types of access we see
1106
- bool SawUnorderedAtomic = false ;
1113
+ bool SawUnorderedAtomic = false ;
1107
1114
bool SawNotAtomic = false ;
1108
1115
AAMDNodes AATags;
1109
1116
@@ -1124,15 +1131,16 @@ bool llvm::promoteLoopAccessesToScalars(
1124
1131
// If this is a base pointer we do not understand, simply bail.
1125
1132
// We only handle alloca and return value from alloc-like fn right now.
1126
1133
if (!isa<AllocaInst>(Object)) {
1127
- if (!isAllocLikeFn (Object, TLI))
1128
- return false ;
1129
- // If this is an alloc like fn. There are more constraints we need to verify.
1130
- // More specifically, we must make sure that the pointer can not escape.
1134
+ if (!isAllocLikeFn (Object, TLI))
1135
+ return false ;
1136
+ // If this is an alloc like fn. There are more constraints we need to
1137
+ // verify. More specifically, we must make sure that the pointer can not
1138
+ // escape.
1131
1139
//
1132
- // NOTE: PointerMayBeCaptured is not enough as the pointer may have escaped
1133
- // even though its not captured by the enclosing function. Standard allocation
1134
- // functions like malloc, calloc, and operator new return values which can
1135
- // be assumed not to have previously escaped.
1140
+ // NOTE: PointerMayBeCaptured is not enough as the pointer may have
1141
+ // escaped even though its not captured by the enclosing function.
1142
+ // Standard allocation functions like malloc, calloc, and operator new
1143
+ // return values which can be assumed not to have previously escaped.
1136
1144
if (PointerMayBeCaptured (Object, true , true ))
1137
1145
return false ;
1138
1146
IsKnownNonEscapingObject = true ;
@@ -1142,10 +1150,7 @@ bool llvm::promoteLoopAccessesToScalars(
1142
1150
// Check that all of the pointers in the alias set have the same type. We
1143
1151
// cannot (yet) promote a memory location that is loaded and stored in
1144
1152
// different sizes. While we are at it, collect alignment and AA info.
1145
- for (const auto &ASI : AS) {
1146
- Value *ASIV = ASI.getValue ();
1147
- PointerMustAliases.insert (ASIV);
1148
-
1153
+ for (Value *ASIV : PointerMustAliases) {
1149
1154
// Check that all of the pointers in the alias set have the same type. We
1150
1155
// cannot (yet) promote a memory location that is loaded and stored in
1151
1156
// different sizes.
@@ -1164,7 +1169,7 @@ bool llvm::promoteLoopAccessesToScalars(
1164
1169
assert (!Load->isVolatile () && " AST broken" );
1165
1170
if (!Load->isUnordered ())
1166
1171
return false ;
1167
-
1172
+
1168
1173
SawUnorderedAtomic |= Load->isAtomic ();
1169
1174
SawNotAtomic |= !Load->isAtomic ();
1170
1175
@@ -1257,8 +1262,8 @@ bool llvm::promoteLoopAccessesToScalars(
1257
1262
else {
1258
1263
Value *Object = GetUnderlyingObject (SomePtr, MDL);
1259
1264
SafeToInsertStore =
1260
- (isAllocLikeFn (Object, TLI) || isa<AllocaInst>(Object)) &&
1261
- !PointerMayBeCaptured (Object, true , true );
1265
+ (isAllocLikeFn (Object, TLI) || isa<AllocaInst>(Object)) &&
1266
+ !PointerMayBeCaptured (Object, true , true );
1262
1267
}
1263
1268
}
1264
1269
@@ -1350,7 +1355,7 @@ LoopInvariantCodeMotion::collectAliasInfoForLoop(Loop *L, LoopInfo *LI,
1350
1355
auto mergeLoop = [&](Loop *L) {
1351
1356
// Loop over the body of this loop, looking for calls, invokes, and stores.
1352
1357
for (BasicBlock *BB : L->blocks ())
1353
- CurAST->add (*BB); // Incorporate the specified basic block
1358
+ CurAST->add (*BB); // Incorporate the specified basic block
1354
1359
};
1355
1360
1356
1361
// Add everything from the sub loops that are no longer directly available.
0 commit comments