Skip to content

Commit d955573

Browse files
author
Chen Zheng
committed
[HardwareLoops] NFC - move hardware loop checking code to isHardwareLoopProfitable()
Differential Revision: https://reviews.llvm.org/D64197 llvm-svn: 365497
1 parent 781e3af commit d955573

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ struct HardwareLoopInfo {
9999
// produces an i1 to guard the loop entry.
100100
bool isHardwareLoopCandidate(ScalarEvolution &SE, LoopInfo &LI,
101101
DominatorTree &DT, bool ForceNestedLoop = false,
102-
bool ForceHardwareLoopPHI = false);
102+
bool ForceHardwareLoopPHI = false,
103+
bool ForceGuardLoopEntry = false);
103104
bool canAnalyze(LoopInfo &LI);
104105
};
105106

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "llvm/Support/ErrorHandling.h"
2121
#include "llvm/Analysis/CFG.h"
2222
#include "llvm/Analysis/LoopIterator.h"
23+
#include "llvm/Transforms/Utils.h"
24+
#include "llvm/Transforms/Utils/LoopUtils.h"
25+
#include "llvm/Analysis/ScalarEvolutionExpander.h"
2326
#include <utility>
2427

2528
using namespace llvm;
@@ -55,7 +58,8 @@ bool HardwareLoopInfo::canAnalyze(LoopInfo &LI) {
5558
bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
5659
LoopInfo &LI, DominatorTree &DT,
5760
bool ForceNestedLoop,
58-
bool ForceHardwareLoopPHI) {
61+
bool ForceHardwareLoopPHI,
62+
bool ForceGuardLoopEntry) {
5963
SmallVector<BasicBlock *, 4> ExitingBlocks;
6064
L->getExitingBlocks(ExitingBlocks);
6165

@@ -134,6 +138,33 @@ bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE,
134138

135139
if (!ExitBlock)
136140
return false;
141+
142+
BasicBlock *Preheader = L->getLoopPreheader();
143+
144+
// If we don't have a preheader, then insert one.
145+
if (!Preheader)
146+
Preheader = InsertPreheaderForLoop(L, &DT, &LI, nullptr, true);
147+
if (!Preheader)
148+
return false;
149+
150+
// Make sure we have a valid Loop Count
151+
if (!ExitCount->getType()->isPointerTy() && ExitCount->getType() != CountType)
152+
ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
153+
154+
ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
155+
156+
BasicBlock *BB = L->getLoopPreheader();
157+
158+
if (ForceGuardLoopEntry && BB->getSinglePredecessor() &&
159+
cast<BranchInst>(BB->getTerminator())->isUnconditional())
160+
BB = BB->getSinglePredecessor();
161+
162+
if (!isSafeToExpandAt(ExitCount, BB->getTerminator(), SE)) {
163+
LLVM_DEBUG(dbgs() << "Not a Hardware Loop: unsafe to expand ExitCount "
164+
<< *ExitCount << "\n");
165+
return false;
166+
}
167+
137168
return true;
138169
}
139170

llvm/lib/CodeGen/HardwareLoops.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
///
1616
//===----------------------------------------------------------------------===//
1717

18-
#include "llvm/Pass.h"
1918
#include "llvm/PassRegistry.h"
2019
#include "llvm/PassSupport.h"
2120
#include "llvm/ADT/Statistic.h"
@@ -36,10 +35,8 @@
3635
#include "llvm/IR/Value.h"
3736
#include "llvm/Support/Debug.h"
3837
#include "llvm/Transforms/Scalar.h"
39-
#include "llvm/Transforms/Utils.h"
4038
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4139
#include "llvm/Transforms/Utils/Local.h"
42-
#include "llvm/Transforms/Utils/LoopUtils.h"
4340

4441
#define DEBUG_TYPE "hardware-loops"
4542

@@ -112,7 +109,6 @@ namespace {
112109
const DataLayout *DL = nullptr;
113110
const TargetTransformInfo *TTI = nullptr;
114111
DominatorTree *DT = nullptr;
115-
bool PreserveLCSSA = false;
116112
AssumptionCache *AC = nullptr;
117113
TargetLibraryInfo *LibInfo = nullptr;
118114
Module *M = nullptr;
@@ -184,7 +180,6 @@ bool HardwareLoops::runOnFunction(Function &F) {
184180
DL = &F.getParent()->getDataLayout();
185181
auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
186182
LibInfo = TLIP ? &TLIP->getTLI() : nullptr;
187-
PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
188183
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
189184
M = F.getParent();
190185

@@ -230,25 +225,19 @@ bool HardwareLoops::TryConvertLoop(Loop *L) {
230225

231226
bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
232227

233-
Loop *L = HWLoopInfo.L;
234-
LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: " << *L);
228+
LLVM_DEBUG(dbgs() << "HWLoops: Try to convert profitable loop: "
229+
<< *HWLoopInfo.L);
235230

236231
if (!HWLoopInfo.isHardwareLoopCandidate(*SE, *LI, *DT, ForceNestedLoop,
237-
ForceHardwareLoopPHI))
232+
ForceHardwareLoopPHI,
233+
ForceGuardLoopEntry))
238234
return false;
239235

240236
assert(
241237
(HWLoopInfo.ExitBlock && HWLoopInfo.ExitBranch && HWLoopInfo.ExitCount) &&
242238
"Hardware Loop must have set exit info.");
243239

244-
BasicBlock *Preheader = L->getLoopPreheader();
245-
246-
// If we don't have a preheader, then insert one.
247-
if (!Preheader)
248-
Preheader = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
249-
if (!Preheader)
250-
return false;
251-
240+
// Now start to converting...
252241
HardwareLoop HWLoop(HWLoopInfo, *SE, *DL);
253242
HWLoop.Create();
254243
++NumHWLoops;
@@ -257,10 +246,10 @@ bool HardwareLoops::TryConvertLoop(HardwareLoopInfo &HWLoopInfo) {
257246

258247
void HardwareLoop::Create() {
259248
LLVM_DEBUG(dbgs() << "HWLoops: Converting loop..\n");
260-
249+
261250
Value *LoopCountInit = InitLoopCount();
262-
if (!LoopCountInit)
263-
return;
251+
252+
assert(LoopCountInit && "Hardware Loop must have a loop count");
264253

265254
InsertIterationSetup(LoopCountInit);
266255

@@ -320,32 +309,22 @@ Value *HardwareLoop::InitLoopCount() {
320309
// loop counter and tests that is not zero?
321310

322311
SCEVExpander SCEVE(SE, DL, "loopcnt");
323-
if (!ExitCount->getType()->isPointerTy() &&
324-
ExitCount->getType() != CountType)
325-
ExitCount = SE.getZeroExtendExpr(ExitCount, CountType);
326-
327-
ExitCount = SE.getAddExpr(ExitCount, SE.getOne(CountType));
328312

329313
// If we're trying to use the 'test and set' form of the intrinsic, we need
330314
// to replace a conditional branch that is controlling entry to the loop. It
331315
// is likely (guaranteed?) that the preheader has an unconditional branch to
332316
// the loop header, so also check if it has a single predecessor.
333317
if (SE.isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
334-
SE.getZero(ExitCount->getType()))) {
335-
LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
318+
SE.getZero(ExitCount->getType())))
336319
UseLoopGuard |= ForceGuardLoopEntry;
337-
} else
320+
else
338321
UseLoopGuard = false;
339322

340323
BasicBlock *BB = L->getLoopPreheader();
341324
if (UseLoopGuard && BB->getSinglePredecessor() &&
342-
cast<BranchInst>(BB->getTerminator())->isUnconditional())
325+
cast<BranchInst>(BB->getTerminator())->isUnconditional()) {
326+
LLVM_DEBUG(dbgs() << " - Attempting to use test.set counter.\n");
343327
BB = BB->getSinglePredecessor();
344-
345-
if (!isSafeToExpandAt(ExitCount, BB->getTerminator(), SE)) {
346-
LLVM_DEBUG(dbgs() << "- Bailing, unsafe to expand ExitCount "
347-
<< *ExitCount << "\n");
348-
return nullptr;
349328
}
350329

351330
Value *Count = SCEVE.expandCodeFor(ExitCount, CountType,

0 commit comments

Comments
 (0)