Skip to content

Commit 42eaf4f

Browse files
Janek van OirschotThomas Preud'homme
authored andcommitted
[HardwareLoops] Change order of SCEV expression construction for InitLoopCount.
Putting the +1 before the zero-extend will allow scalar evolution to fold the expression in some cases such as the one shown in PowerPC's `shrink-wrap.ll` test. Reviewed By: samparker Differential Revision: https://reviews.llvm.org/D91724
1 parent f6e5929 commit 42eaf4f

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct HardwareLoopInfo {
9393
Loop *L = nullptr;
9494
BasicBlock *ExitBlock = nullptr;
9595
BranchInst *ExitBranch = nullptr;
96-
const SCEV *ExitCount = nullptr;
96+
const SCEV *TripCount = nullptr;
9797
IntegerType *CountType = nullptr;
9898
Value *LoopDecrement = nullptr; // Decrement the loop counter by this
9999
// value in every iteration.

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
216216
// Note that this block may not be the loop latch block, even if the loop
217217
// has a latch block.
218218
ExitBlock = BB;
219-
ExitCount = EC;
219+
TripCount = SE.getAddExpr(EC, SE.getOne(EC->getType()));
220+
221+
if (!EC->getType()->isPointerTy() && EC->getType() != CountType)
222+
TripCount = SE.getZeroExtendExpr(TripCount, CountType);
223+
220224
break;
221225
}
222226

llvm/lib/CodeGen/HardwareLoops.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace {
187187
const DataLayout &DL,
188188
OptimizationRemarkEmitter *ORE) :
189189
SE(SE), DL(DL), ORE(ORE), L(Info.L), M(L->getHeader()->getModule()),
190-
ExitCount(Info.ExitCount),
190+
TripCount(Info.TripCount),
191191
CountType(Info.CountType),
192192
ExitBranch(Info.ExitBranch),
193193
LoopDecrement(Info.LoopDecrement),
@@ -202,7 +202,7 @@ namespace {
202202
OptimizationRemarkEmitter *ORE = nullptr;
203203
Loop *L = nullptr;
204204
Module *M = nullptr;
205-
const SCEV *ExitCount = nullptr;
205+
const SCEV *TripCount = nullptr;
206206
Type *CountType = nullptr;
207207
BranchInst *ExitBranch = nullptr;
208208
Value *LoopDecrement = nullptr;
@@ -298,7 +298,7 @@ bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
298298
}
299299

300300
assert(
301-
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) &&
301+
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.TripCount) &&
302302
"Hardware Loop must have set exit info.");
303303

304304
BasicBlock *Preheader = L->getLoopPreheader();
@@ -383,18 +383,13 @@ Value *HardwareLoop::InitLoopCount() {
383383
// loop counter and tests that is not zero?
384384

385385
SCEVExpander SCEVE(SE, DL, "loopcnt");
386-
if (!ExitCount->getType()->isPointerTy() &&
387-
ExitCount->getType() != CountType)
388-
ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
389-
390-
ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
391386

392387
// If we're trying to use the 'test and set' form of the intrinsic, we need
393388
// to replace a conditional branch that is controlling entry to the loop. It
394389
// is likely (guaranteed?) that the preheader has an unconditional branch to
395390
// the loop header, so also check if it has a single predecessor.
396-
if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
397-
SE.getZero(ExitCount->getType()))) {
391+
if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, TripCount,
392+
SE.getZero(TripCount->getType()))) {
398393
LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
399394
UseLoopGuard |= ForceGuardLoopEntry;
400395
} else
@@ -406,19 +401,19 @@ Value *HardwareLoop::InitLoopCount() {
406401
BasicBlock *Predecessor = BB->getSinglePredecessor();
407402
// If it's not safe to create a while loop then don't force it and create a
408403
// do-while loop instead
409-
if (!isSafeToExpandAt(ExitCount, Predecessor->getTerminator(), SE))
404+
if (!isSafeToExpandAt(TripCount, Predecessor->getTerminator(), SE))
410405
UseLoopGuard = false;
411406
else
412407
BB = Predecessor;
413408
}
414409

415-
if (!isSafeToExpandAt(ExitCount, BB->getTerminator(), SE)) {
416-
LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount "
417-
<< *ExitCount << "\n");
410+
if (!isSafeToExpandAt(TripCount, BB->getTerminator(), SE)) {
411+
LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand TripCount "
412+
<< *TripCount << "\n");
418413
return nullptr;
419414
}
420415

421-
Value *Count = SCEVE.expandCodeFor(ExitCount, CountType,
416+
Value *Count = SCEVE.expandCodeFor(TripCount, CountType,
422417
BB->getTerminator());
423418

424419
// FIXME: We've expanded Count where we hope to insert the counter setting

llvm/test/CodeGen/PowerPC/shrink-wrap.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ entry:
3232
; CHECK: std
3333
; CHECK: blt 0, .LBB0_3
3434
; CHECK: # %bb.1:
35-
; CHECK-NEXT: addi
3635
; CHECK-NEXT: clrldi
37-
; CHECK-NEXT: addi
3836
; CHECK-NEXT: mtctr
3937
; CHECK-NEXT: li
4038
; CHECK: .LBB0_2:

0 commit comments

Comments
 (0)