Skip to content

Commit 34badc4

Browse files
author
Chen Zheng
committed
Revert "[HardwareLoops] Change order of SCEV expression construction for InitLoopCount."
This causes https://bugs.llvm.org/show_bug.cgi?id=51714 and is not a right patch according to comments in D91724 This reverts commit 42eaf4f.
1 parent 79bcd4a commit 34badc4

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

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

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
167167
// Note that this block may not be the loop latch block, even if the loop
168168
// has a latch block.
169169
ExitBlock = BB;
170-
TripCount = SE.getAddExpr(EC, SE.getOne(EC->getType()));
171-
172-
if (!EC->getType()->isPointerTy() && EC->getType() != CountType)
173-
TripCount = SE.getZeroExtendExpr(TripCount, CountType);
174-
170+
ExitCount = EC;
175171
break;
176172
}
177173

llvm/lib/CodeGen/HardwareLoops.cpp

Lines changed: 15 additions & 10 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-
TripCount(Info.TripCount),
190+
ExitCount(Info.ExitCount),
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 *TripCount = nullptr;
205+
const SCEV *ExitCount = nullptr;
206206
Type *CountType = nullptr;
207207
BranchInst *ExitBranch = nullptr;
208208
Value *LoopDecrement = nullptr;
@@ -296,7 +296,7 @@ bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
296296
}
297297

298298
assert(
299-
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.TripCount) &&
299+
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) &&
300300
"Hardware Loop must have set exit info.");
301301

302302
BasicBlock *Preheader = L->getLoopPreheader();
@@ -381,13 +381,18 @@ Value *HardwareLoop::InitLoopCount() {
381381
// loop counter and tests that is not zero?
382382

383383
SCEVExpander SCEVE(SE, DL, "loopcnt");
384+
if (!ExitCount->getType()->isPointerTy() &&
385+
ExitCount->getType() != CountType)
386+
ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
387+
388+
ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
384389

385390
// If we're trying to use the 'test and set' form of the intrinsic, we need
386391
// to replace a conditional branch that is controlling entry to the loop. It
387392
// is likely (guaranteed?) that the preheader has an unconditional branch to
388393
// the loop header, so also check if it has a single predecessor.
389-
if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, TripCount,
390-
SE.getZero(TripCount->getType()))) {
394+
if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
395+
SE.getZero(ExitCount->getType()))) {
391396
LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
392397
UseLoopGuard |= ForceGuardLoopEntry;
393398
} else
@@ -399,19 +404,19 @@ Value *HardwareLoop::InitLoopCount() {
399404
BasicBlock *Predecessor = BB->getSinglePredecessor();
400405
// If it's not safe to create a while loop then don't force it and create a
401406
// do-while loop instead
402-
if (!isSafeToExpandAt(TripCount, Predecessor->getTerminator(), SE))
407+
if (!isSafeToExpandAt(ExitCount, Predecessor->getTerminator(), SE))
403408
UseLoopGuard = false;
404409
else
405410
BB = Predecessor;
406411
}
407412

408-
if (!isSafeToExpandAt(TripCount, BB->getTerminator(), SE)) {
409-
LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand TripCount " << *TripCount
410-
<< "\n");
413+
if (!isSafeToExpandAt(ExitCount, BB->getTerminator(), SE)) {
414+
LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount "
415+
<< *ExitCount << "\n");
411416
return nullptr;
412417
}
413418

414-
Value *Count = SCEVE.expandCodeFor(TripCount, CountType,
419+
Value *Count = SCEVE.expandCodeFor(ExitCount, CountType,
415420
BB->getTerminator());
416421

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

llvm/test/CodeGen/PowerPC/loop-instr-prep-non-const-increasement.ll

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ define i64 @foo(i8* %p, i32 signext %n, i32 signext %count) {
2222
; CHECK-NEXT: cmpwi r4, 1
2323
; CHECK-NEXT: blt cr0, .LBB0_4
2424
; CHECK-NEXT: # %bb.1: # %for.body.preheader
25-
; CHECK-NEXT: clrldi r4, r4, 32
25+
; CHECK-NEXT: addi r4, r4, -1
2626
; CHECK-NEXT: extsw r5, r5
2727
; CHECK-NEXT: li r6, 0
2828
; CHECK-NEXT: li r7, 5
29-
; CHECK-NEXT: mtctr r4
3029
; CHECK-NEXT: li r8, 9
30+
; CHECK-NEXT: clrldi r4, r4, 32
31+
; CHECK-NEXT: addi r4, r4, 1
32+
; CHECK-NEXT: mtctr r4
3133
; CHECK-NEXT: li r4, 0
3234
; CHECK-NEXT: .p2align 5
3335
; CHECK-NEXT: .LBB0_2: # %for.body
@@ -93,21 +95,23 @@ define zeroext i8 @foo1(i8* %p, i32 signext %n, i32 signext %count) {
9395
; CHECK-NEXT: cmpwi r4, 1
9496
; CHECK-NEXT: blt cr0, .LBB1_4
9597
; CHECK-NEXT: # %bb.1: # %for.body.preheader
96-
; CHECK-NEXT: addi r6, r3, 1000
97-
; CHECK-NEXT: clrldi r3, r4, 32
98+
; CHECK-NEXT: addi r4, r4, -1
99+
; CHECK-NEXT: addi r3, r3, 1000
98100
; CHECK-NEXT: extsw r5, r5
101+
; CHECK-NEXT: li r6, 0
102+
; CHECK-NEXT: clrldi r4, r4, 32
103+
; CHECK-NEXT: addi r4, r4, 1
104+
; CHECK-NEXT: mtctr r4
99105
; CHECK-NEXT: li r4, 0
100-
; CHECK-NEXT: mtctr r3
101-
; CHECK-NEXT: li r3, 0
102106
; CHECK-NEXT: .p2align 4
103107
; CHECK-NEXT: .LBB1_2: # %for.body
104108
; CHECK-NEXT: #
105-
; CHECK-NEXT: lbzx r7, r6, r4
106-
; CHECK-NEXT: add r4, r4, r5
107-
; CHECK-NEXT: add r3, r7, r3
109+
; CHECK-NEXT: lbzx r7, r3, r6
110+
; CHECK-NEXT: add r6, r6, r5
111+
; CHECK-NEXT: add r4, r7, r4
108112
; CHECK-NEXT: bdnz .LBB1_2
109113
; CHECK-NEXT: # %bb.3: # %for.cond.cleanup
110-
; CHECK-NEXT: clrldi r3, r3, 56
114+
; CHECK-NEXT: clrldi r3, r4, 56
111115
; CHECK-NEXT: blr
112116
; CHECK-NEXT: .LBB1_4:
113117
; CHECK-NEXT: li r3, 0

llvm/test/CodeGen/PowerPC/mma-phi-accs.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ define dso_local signext i32 @testNestedPHI(i32 signext %cond, i32 signext %coun
263263
; CHECK-NEXT: cmpwi r4, 1
264264
; CHECK-NEXT: blt cr0, .LBB3_5
265265
; CHECK-NEXT: .LBB3_3: # %for.body.preheader
266-
; CHECK-NEXT: clrldi r3, r4, 32
266+
; CHECK-NEXT: addi r3, r4, -1
267+
; CHECK-NEXT: clrldi r3, r3, 32
268+
; CHECK-NEXT: addi r3, r3, 1
267269
; CHECK-NEXT: mtctr r3
268270
; CHECK-NEXT: .p2align 4
269271
; CHECK-NEXT: .LBB3_4: # %for.body
@@ -293,7 +295,9 @@ define dso_local signext i32 @testNestedPHI(i32 signext %cond, i32 signext %coun
293295
; CHECK-BE-NEXT: cmpwi r4, 1
294296
; CHECK-BE-NEXT: blt cr0, .LBB3_5
295297
; CHECK-BE-NEXT: .LBB3_3: # %for.body.preheader
296-
; CHECK-BE-NEXT: clrldi r3, r4, 32
298+
; CHECK-BE-NEXT: addi r3, r4, -1
299+
; CHECK-BE-NEXT: clrldi r3, r3, 32
300+
; CHECK-BE-NEXT: addi r3, r3, 1
297301
; CHECK-BE-NEXT: mtctr r3
298302
; CHECK-BE-NEXT: .p2align 4
299303
; CHECK-BE-NEXT: .LBB3_4: # %for.body

0 commit comments

Comments
 (0)