1515// /
1616// ===----------------------------------------------------------------------===//
1717
18- #include " llvm/Pass.h"
1918#include " llvm/PassRegistry.h"
2019#include " llvm/PassSupport.h"
2120#include " llvm/ADT/Statistic.h"
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
231226bool 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
258247void 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