Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit ccef78d

Browse files
committed
[MergedLoadStoreMotion] Small cleanup
No functional change is intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270824 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5c78f7d commit ccef78d

File tree

1 file changed

+45
-57
lines changed

1 file changed

+45
-57
lines changed

lib/Transforms/Scalar/MergedLoadStoreMotion.cpp

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
179179
// Notify the memory dependence analysis.
180180
if (MD) {
181181
MD->removeInstruction(Inst);
182-
if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
182+
if (auto *LI = dyn_cast<LoadInst>(Inst))
183183
MD->invalidateCachedPointerInfo(LI->getPointerOperand());
184-
if (Inst->getType()->getScalarType()->isPointerTy()) {
184+
if (Inst->getType()->isPtrOrPtrVectorTy()) {
185185
MD->invalidateCachedPointerInfo(Inst);
186186
}
187187
}
@@ -193,10 +193,7 @@ void MergedLoadStoreMotion::removeInstruction(Instruction *Inst) {
193193
///
194194
BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
195195
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();
200197
}
201198

202199
///
@@ -205,25 +202,22 @@ BasicBlock *MergedLoadStoreMotion::getDiamondTail(BasicBlock *BB) {
205202
bool MergedLoadStoreMotion::isDiamondHead(BasicBlock *BB) {
206203
if (!BB)
207204
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())
211207
return false;
212208

213-
BranchInst *BI = (BranchInst *)(BB->getTerminator());
214209
BasicBlock *Succ0 = BI->getSuccessor(0);
215210
BasicBlock *Succ1 = BI->getSuccessor(1);
216211

217-
if (!Succ0->getSinglePredecessor() ||
218-
Succ0->getTerminator()->getNumSuccessors() != 1)
212+
if (!Succ0->getSinglePredecessor())
219213
return false;
220-
if (!Succ1->getSinglePredecessor() ||
221-
Succ1->getTerminator()->getNumSuccessors() != 1)
214+
if (!Succ1->getSinglePredecessor())
222215
return false;
223216

224-
BasicBlock *Tail = Succ0->getTerminator()->getSuccessor(0);
217+
BasicBlock *Succ0Succ = Succ0->getSingleSuccessor();
218+
BasicBlock *Succ1Succ = Succ1->getSingleSuccessor();
225219
// Ignore triangles.
226-
if (Succ1->getTerminator()->getSuccessor(0) != Tail)
220+
if (!Succ0Succ || !Succ1Succ || Succ0Succ != Succ1Succ)
227221
return false;
228222
return true;
229223
}
@@ -260,7 +254,7 @@ LoadInst *MergedLoadStoreMotion::canHoistFromBlock(BasicBlock *BB1,
260254
if (!isa<LoadInst>(Inst) || Inst->isUsedOutsideOfBlock(BB1))
261255
continue;
262256

263-
LoadInst *Load1 = dyn_cast<LoadInst>(Inst);
257+
auto *Load1 = cast<LoadInst>(Inst);
264258
BasicBlock *BB0 = Load0->getParent();
265259

266260
MemoryLocation Loc0 = MemoryLocation::get(Load0);
@@ -314,11 +308,10 @@ void MergedLoadStoreMotion::hoistInstruction(BasicBlock *BB,
314308
///
315309
bool MergedLoadStoreMotion::isSafeToHoist(Instruction *I) const {
316310
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;
322315
return true;
323316
}
324317

@@ -328,8 +321,8 @@ bool MergedLoadStoreMotion::isSafeToHoist(Instruction *I) const {
328321
bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
329322
LoadInst *L1) {
330323
// 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());
333326
if (A0 && A1 && A0->isIdenticalTo(A1) && isSafeToHoist(A0) &&
334327
A0->hasOneUse() && (A0->getParent() == L0->getParent()) &&
335328
A1->hasOneUse() && (A1->getParent() == L1->getParent()) &&
@@ -340,8 +333,8 @@ bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
340333
hoistInstruction(BB, A0, A1);
341334
hoistInstruction(BB, L0, L1);
342335
return true;
343-
} else
344-
return false;
336+
}
337+
return false;
345338
}
346339

347340
///
@@ -353,7 +346,7 @@ bool MergedLoadStoreMotion::hoistLoad(BasicBlock *BB, LoadInst *L0,
353346
bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
354347
bool MergedLoads = false;
355348
assert(isDiamondHead(BB));
356-
BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
349+
BranchInst *BI = cast<BranchInst>(BB->getTerminator());
357350
BasicBlock *Succ0 = BI->getSuccessor(0);
358351
BasicBlock *Succ1 = BI->getSuccessor(1);
359352
// #Instructions in Succ1 for Compile Time Control
@@ -364,8 +357,8 @@ bool MergedLoadStoreMotion::mergeLoads(BasicBlock *BB) {
364357
Instruction *I = &*BBI;
365358
++BBI;
366359

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);
369362
if (!L0 || !L0->isSimple() || L0->isUsedOutsideOfBlock(Succ0))
370363
continue;
371364

@@ -418,10 +411,10 @@ StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
418411
MemoryLocation Loc0 = MemoryLocation::get(Store0);
419412
MemoryLocation Loc1 = MemoryLocation::get(Store1);
420413
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)) {
425418
return Store1;
426419
}
427420
}
@@ -434,17 +427,17 @@ StoreInst *MergedLoadStoreMotion::canSinkFromBlock(BasicBlock *BB1,
434427
PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
435428
StoreInst *S1) {
436429
// Create a phi if the values mismatch.
437-
PHINode *NewPN = nullptr;
438430
Value *Opd1 = S0->getValueOperand();
439431
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);
448441
return NewPN;
449442
}
450443

@@ -456,8 +449,8 @@ PHINode *MergedLoadStoreMotion::getPHIOperand(BasicBlock *BB, StoreInst *S0,
456449
bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
457450
StoreInst *S1) {
458451
// 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());
461454
if (A0 && A1 && A0->isIdenticalTo(A1) && A0->hasOneUse() &&
462455
(A0->getParent() == S0->getParent()) && A1->hasOneUse() &&
463456
(A1->getParent() == S1->getParent()) && isa<GetElementPtrInst>(A0)) {
@@ -471,17 +464,16 @@ bool MergedLoadStoreMotion::sinkStore(BasicBlock *BB, StoreInst *S0,
471464
S0->dropUnknownNonDebugMetadata();
472465

473466
// Create the new store to be inserted at the join point.
474-
StoreInst *SNew = (StoreInst *)(S0->clone());
467+
StoreInst *SNew = cast<StoreInst>(S0->clone());
475468
Instruction *ANew = A0->clone();
476469
SNew->insertBefore(&*InsertPt);
477470
ANew->insertBefore(SNew);
478471

479472
assert(S0->getParent() == A0->getParent());
480473
assert(S1->getParent() == A1->getParent());
481474

482-
PHINode *NewPN = getPHIOperand(BB, S0, S1);
483475
// New PHI operand? Use it.
484-
if (NewPN)
476+
if (PHINode *NewPN = getPHIOperand(BB, S0, S1))
485477
SNew->setOperand(0, NewPN);
486478
removeInstruction(S0);
487479
removeInstruction(S1);
@@ -527,11 +519,9 @@ bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
527519
Instruction *I = &*RBI;
528520
++RBI;
529521

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())
535525
continue;
536526

537527
++NStores;
@@ -546,11 +536,9 @@ bool MergedLoadStoreMotion::mergeStores(BasicBlock *T) {
546536
// is likely stale at this point.
547537
if (!Res)
548538
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());
554542
}
555543
}
556544
return MergedStores;

0 commit comments

Comments
 (0)