@@ -6236,7 +6236,8 @@ DbgRewriteSalvageableDVIs(llvm::Loop *L, ScalarEvolution &SE,
6236
6236
}
6237
6237
6238
6238
// / Identify and cache salvageable DVI locations and expressions along with the
6239
- // / corresponding SCEV(s). Also ensure that the DVI is not deleted before
6239
+ // / corresponding SCEV(s). Also ensure that the DVI is not deleted between
6240
+ // / cacheing and salvaging.
6240
6241
static void
6241
6242
DbgGatherSalvagableDVI (Loop *L, ScalarEvolution &SE,
6242
6243
SmallVector<DVIRecoveryRec, 2 > &SalvageableDVISCEVs,
@@ -6257,6 +6258,16 @@ DbgGatherSalvagableDVI(Loop *L, ScalarEvolution &SE,
6257
6258
!SE.isSCEVable (DVI->getVariableLocationOp (0 )->getType ()))
6258
6259
continue ;
6259
6260
6261
+ // SCEVUnknown wraps an llvm::Value, it does not have a start and stride.
6262
+ // Therefore no translation to DIExpression is performed.
6263
+ const SCEV *S = SE.getSCEV (DVI->getVariableLocationOp (0 ));
6264
+ if (isa<SCEVUnknown>(S))
6265
+ continue ;
6266
+
6267
+ // Avoid wasting resources generating an expression containing undef.
6268
+ if (SE.containsUndefs (S))
6269
+ continue ;
6270
+
6260
6271
SalvageableDVISCEVs.push_back (
6261
6272
{DVI, DVI->getExpression (), DVI->getRawLocation (),
6262
6273
SE.getSCEV (DVI->getVariableLocationOp (0 ))});
@@ -6270,33 +6281,32 @@ DbgGatherSalvagableDVI(Loop *L, ScalarEvolution &SE,
6270
6281
// / surviving subsequent transforms.
6271
6282
static llvm::PHINode *GetInductionVariable (const Loop &L, ScalarEvolution &SE,
6272
6283
const LSRInstance &LSR) {
6273
- // For now, just pick the first IV generated and inserted. Ideally pick an IV
6274
- // that is unlikely to be optimised away by subsequent transforms.
6284
+
6285
+ auto IsSuitableIV = [&](PHINode *P) {
6286
+ if (!SE.isSCEVable (P->getType ()))
6287
+ return false ;
6288
+ if (const SCEVAddRecExpr *Rec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV (P)))
6289
+ return Rec->isAffine () && !SE.containsUndefs (SE.getSCEV (P));
6290
+ return false ;
6291
+ };
6292
+
6293
+ // For now, just pick the first IV that was generated and inserted by
6294
+ // ScalarEvolution. Ideally pick an IV that is unlikely to be optimised away
6295
+ // by subsequent transforms.
6275
6296
for (const WeakVH &IV : LSR.getScalarEvolutionIVs ()) {
6276
6297
if (!IV)
6277
6298
continue ;
6278
6299
6279
- assert (isa<PHINode>(&*IV) && " Expected PhI node." );
6280
- if (SE.isSCEVable ((*IV).getType ())) {
6281
- PHINode *Phi = dyn_cast<PHINode>(&*IV);
6282
- LLVM_DEBUG (dbgs () << " scev-salvage: IV : " << *IV
6283
- << " with SCEV: " << *SE.getSCEV (Phi) << " \n " );
6284
- return Phi;
6285
- }
6286
- }
6300
+ // There should only be PHI node IVs.
6301
+ PHINode *P = cast<PHINode>(&*IV);
6287
6302
6288
- for (PHINode &Phi : L.getHeader ()->phis ()) {
6289
- if (!SE.isSCEVable (Phi.getType ()))
6290
- continue ;
6291
-
6292
- const llvm::SCEV *PhiSCEV = SE.getSCEV (&Phi);
6293
- if (const llvm::SCEVAddRecExpr *Rec = dyn_cast<SCEVAddRecExpr>(PhiSCEV))
6294
- if (!Rec->isAffine ())
6295
- continue ;
6303
+ if (IsSuitableIV (P))
6304
+ return P;
6305
+ }
6296
6306
6297
- LLVM_DEBUG ( dbgs () << " scev-salvage: Selected IV from loop header: " << Phi
6298
- << " with SCEV: " << *PhiSCEV << " \n " );
6299
- return &Phi ;
6307
+ for (PHINode &P : L. getHeader ()-> phis ()) {
6308
+ if ( IsSuitableIV (&P))
6309
+ return &P ;
6300
6310
}
6301
6311
return nullptr ;
6302
6312
}
0 commit comments