@@ -179,9 +179,9 @@ void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
179
179
// Notify the memory dependence analysis.
180
180
if (MD) {
181
181
MD->removeInstruction (Inst);
182
- if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
182
+ if (auto *LI = dyn_cast<LoadInst>(Inst))
183
183
MD->invalidateCachedPointerInfo (LI->getPointerOperand ());
184
- if (Inst->getType ()->getScalarType ()-> isPointerTy ()) {
184
+ if (Inst->getType ()->isPtrOrPtrVectorTy ()) {
185
185
MD->invalidateCachedPointerInfo (Inst);
186
186
}
187
187
}
@@ -193,10 +193,7 @@ void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
193
193
// /
194
194
BasicBlock *MergedLoadStoreMotion::getDiamondTail (BasicBlock *BB) {
195
195
assert (isDiamondHead (BB) && " Basic block is not head of a diamond" );
196
- BranchInst *BI = (BranchInst *)(BB->getTerminator ());
197
- BasicBlock *Succ0 = BI->getSuccessor (0 );
198
- BasicBlock *Tail = Succ0->getTerminator ()->getSuccessor (0 );
199
- return Tail;
196
+ return BB->getTerminator ()->getSuccessor (0 )->getSingleSuccessor ();
200
197
}
201
198
202
199
// /
@@ -205,25 +202,22 @@ BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
205
202
bool MergedLoadStoreMotion::isDiamondHead (BasicBlock *BB) {
206
203
if (!BB)
207
204
return false ;
208
- if (!isa<BranchInst>(BB->getTerminator ()))
209
- return false ;
210
- if (BB->getTerminator ()->getNumSuccessors () != 2 )
205
+ auto *BI = dyn_cast<BranchInst>(BB->getTerminator ());
206
+ if (!BI || !BI->isConditional ())
211
207
return false ;
212
208
213
- BranchInst *BI = (BranchInst *)(BB->getTerminator ());
214
209
BasicBlock *Succ0 = BI->getSuccessor (0 );
215
210
BasicBlock *Succ1 = BI->getSuccessor (1 );
216
211
217
- if (!Succ0->getSinglePredecessor () ||
218
- Succ0->getTerminator ()->getNumSuccessors () != 1 )
212
+ if (!Succ0->getSinglePredecessor ())
219
213
return false ;
220
- if (!Succ1->getSinglePredecessor () ||
221
- Succ1->getTerminator ()->getNumSuccessors () != 1 )
214
+ if (!Succ1->getSinglePredecessor ())
222
215
return false ;
223
216
224
- BasicBlock *Tail = Succ0->getTerminator ()->getSuccessor (0 );
217
+ BasicBlock *Succ0Succ = Succ0->getSingleSuccessor ();
218
+ BasicBlock *Succ1Succ = Succ1->getSingleSuccessor ();
225
219
// Ignore triangles.
226
- if (Succ1-> getTerminator ()-> getSuccessor ( 0 ) != Tail )
220
+ if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ )
227
221
return false ;
228
222
return true ;
229
223
}
@@ -260,7 +254,7 @@ LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,
260
254
if (!isa<LoadInst>(Inst) || Inst->isUsedOutsideOfBlock (BB1))
261
255
continue ;
262
256
263
- LoadInst *Load1 = dyn_cast <LoadInst>(Inst);
257
+ auto *Load1 = cast <LoadInst>(Inst);
264
258
BasicBlock *BB0 = Load0->getParent ();
265
259
266
260
MemoryLocation Loc0 = MemoryLocation::get (Load0);
@@ -314,11 +308,10 @@ void MergedLoadStoreMotion::hoistInstruction(BasicBlock *BB,
314
308
// /
315
309
bool MergedLoadStoreMotion::isSafeToHoist (Instruction *I) const {
316
310
BasicBlock *Parent = I->getParent ();
317
- for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
318
- Instruction *Instr = dyn_cast<Instruction>(I->getOperand (i));
319
- if (Instr && Instr->getParent () == Parent)
320
- return false ;
321
- }
311
+ for (Use &U : I->operands ())
312
+ if (auto *Instr = dyn_cast<Instruction>(&U))
313
+ if (Instr->getParent () == Parent)
314
+ return false ;
322
315
return true ;
323
316
}
324
317
@@ -328,8 +321,8 @@ bool MergedLoadStoreMotion::isSafeToHoist(Instruction *I) const {
328
321
bool MergedLoadStoreMotion::hoistLoad (BasicBlock *BB, LoadInst *L0,
329
322
LoadInst *L1) {
330
323
// Only one definition?
331
- Instruction *A0 = dyn_cast<Instruction>(L0->getPointerOperand ());
332
- Instruction *A1 = dyn_cast<Instruction>(L1->getPointerOperand ());
324
+ auto *A0 = dyn_cast<Instruction>(L0->getPointerOperand ());
325
+ auto *A1 = dyn_cast<Instruction>(L1->getPointerOperand ());
333
326
if (A0 && A1 && A0->isIdenticalTo (A1) && isSafeToHoist (A0) &&
334
327
A0->hasOneUse () && (A0->getParent () == L0->getParent ()) &&
335
328
A1->hasOneUse () && (A1->getParent () == L1->getParent ()) &&
@@ -340,8 +333,8 @@ bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
340
333
hoistInstruction (BB, A0, A1);
341
334
hoistInstruction (BB, L0, L1);
342
335
return true ;
343
- } else
344
- return false ;
336
+ }
337
+ return false ;
345
338
}
346
339
347
340
// /
@@ -353,7 +346,7 @@ bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
353
346
bool MergedLoadStoreMotion::mergeLoads (BasicBlock *BB) {
354
347
bool MergedLoads = false ;
355
348
assert (isDiamondHead (BB));
356
- BranchInst *BI = dyn_cast <BranchInst>(BB->getTerminator ());
349
+ BranchInst *BI = cast <BranchInst>(BB->getTerminator ());
357
350
BasicBlock *Succ0 = BI->getSuccessor (0 );
358
351
BasicBlock *Succ1 = BI->getSuccessor (1 );
359
352
// #Instructions in Succ1 for Compile Time Control
@@ -364,8 +357,8 @@ bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
364
357
Instruction *I = &*BBI;
365
358
++BBI;
366
359
367
- // Only move non-simple (atomic, volatile) loads.
368
- LoadInst *L0 = dyn_cast<LoadInst>(I);
360
+ // Don't move non-simple (atomic, volatile) loads.
361
+ auto *L0 = dyn_cast<LoadInst>(I);
369
362
if (!L0 || !L0->isSimple () || L0->isUsedOutsideOfBlock (Succ0))
370
363
continue ;
371
364
@@ -418,10 +411,10 @@ StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
418
411
MemoryLocation Loc0 = MemoryLocation::get (Store0);
419
412
MemoryLocation Loc1 = MemoryLocation::get (Store1);
420
413
if (AA->isMustAlias (Loc0, Loc1) && Store0->isSameOperationAs (Store1) &&
421
- !isStoreSinkBarrierInRange (*(std::next (BasicBlock::iterator (Store1))),
422
- BB1->back (), Loc1) &&
423
- !isStoreSinkBarrierInRange (*(std::next (BasicBlock::iterator (Store0))),
424
- BB0->back (), Loc0)) {
414
+ !isStoreSinkBarrierInRange (*(std::next (BasicBlock::iterator (Store1))),
415
+ BB1->back (), Loc1) &&
416
+ !isStoreSinkBarrierInRange (*(std::next (BasicBlock::iterator (Store0))),
417
+ BB0->back (), Loc0)) {
425
418
return Store1;
426
419
}
427
420
}
@@ -434,17 +427,17 @@ StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
434
427
PHINode *MergedLoadStoreMotion::getPHIOperand (BasicBlock *BB, StoreInst *S0,
435
428
StoreInst *S1) {
436
429
// Create a phi if the values mismatch.
437
- PHINode *NewPN = nullptr ;
438
430
Value *Opd1 = S0->getValueOperand ();
439
431
Value *Opd2 = S1->getValueOperand ();
440
- if (Opd1 != Opd2) {
441
- NewPN = PHINode::Create (Opd1->getType (), 2 , Opd2->getName () + " .sink" ,
442
- &BB->front ());
443
- NewPN->addIncoming (Opd1, S0->getParent ());
444
- NewPN->addIncoming (Opd2, S1->getParent ());
445
- if (MD && NewPN->getType ()->getScalarType ()->isPointerTy ())
446
- MD->invalidateCachedPointerInfo (NewPN);
447
- }
432
+ if (Opd1 == Opd2)
433
+ return nullptr ;
434
+
435
+ auto *NewPN = PHINode::Create (Opd1->getType (), 2 , Opd2->getName () + " .sink" ,
436
+ &BB->front ());
437
+ NewPN->addIncoming (Opd1, S0->getParent ());
438
+ NewPN->addIncoming (Opd2, S1->getParent ());
439
+ if (MD && NewPN->getType ()->getScalarType ()->isPointerTy ())
440
+ MD->invalidateCachedPointerInfo (NewPN);
448
441
return NewPN;
449
442
}
450
443
@@ -456,8 +449,8 @@ PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
456
449
bool MergedLoadStoreMotion::sinkStore (BasicBlock *BB, StoreInst *S0,
457
450
StoreInst *S1) {
458
451
// Only one definition?
459
- Instruction *A0 = dyn_cast<Instruction>(S0->getPointerOperand ());
460
- Instruction *A1 = dyn_cast<Instruction>(S1->getPointerOperand ());
452
+ auto *A0 = dyn_cast<Instruction>(S0->getPointerOperand ());
453
+ auto *A1 = dyn_cast<Instruction>(S1->getPointerOperand ());
461
454
if (A0 && A1 && A0->isIdenticalTo (A1) && A0->hasOneUse () &&
462
455
(A0->getParent () == S0->getParent ()) && A1->hasOneUse () &&
463
456
(A1->getParent () == S1->getParent ()) && isa<GetElementPtrInst>(A0)) {
@@ -471,17 +464,16 @@ bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
471
464
S0->dropUnknownNonDebugMetadata ();
472
465
473
466
// Create the new store to be inserted at the join point.
474
- StoreInst *SNew = ( StoreInst *) (S0->clone ());
467
+ StoreInst *SNew = cast< StoreInst> (S0->clone ());
475
468
Instruction *ANew = A0->clone ();
476
469
SNew->insertBefore (&*InsertPt);
477
470
ANew->insertBefore (SNew);
478
471
479
472
assert (S0->getParent () == A0->getParent ());
480
473
assert (S1->getParent () == A1->getParent ());
481
474
482
- PHINode *NewPN = getPHIOperand (BB, S0, S1);
483
475
// New PHI operand? Use it.
484
- if (NewPN)
476
+ if (PHINode * NewPN = getPHIOperand (BB, S0, S1) )
485
477
SNew->setOperand (0 , NewPN);
486
478
removeInstruction (S0);
487
479
removeInstruction (S1);
@@ -527,11 +519,9 @@ bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
527
519
Instruction *I = &*RBI;
528
520
++RBI;
529
521
530
- // Sink move non-simple (atomic, volatile) stores
531
- if (!isa<StoreInst>(I))
532
- continue ;
533
- StoreInst *S0 = (StoreInst *)I;
534
- if (!S0->isSimple ())
522
+ // Don't sink non-simple (atomic, volatile) stores.
523
+ auto *S0 = dyn_cast<StoreInst>(I);
524
+ if (!S0 || !S0->isSimple ())
535
525
continue ;
536
526
537
527
++NStores;
@@ -546,11 +536,9 @@ bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
546
536
// is likely stale at this point.
547
537
if (!Res)
548
538
break ;
549
- else {
550
- RBI = Pred0->rbegin ();
551
- RBE = Pred0->rend ();
552
- DEBUG (dbgs () << " Search again\n " ; Instruction *I = &*RBI; I->dump ());
553
- }
539
+ RBI = Pred0->rbegin ();
540
+ RBE = Pred0->rend ();
541
+ DEBUG (dbgs () << " Search again\n " ; Instruction *I = &*RBI; I->dump ());
554
542
}
555
543
}
556
544
return MergedStores;
0 commit comments