LLVM: include/llvm/Analysis/LoopInfo.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_ANALYSIS_LOOPINFO_H

14#define LLVM_ANALYSIS_LOOPINFO_H

15

22#include

23#include

24

25namespace llvm {

26

34

35

37

38

39

40class LLVM_ABI Loop : public LoopBase<BasicBlock, Loop> {

41public:

42

46

47 public:

52

55

56

57

58 explicit operator bool() const { return Start && End; }

59 };

60

61

62 bool isLoopInvariant(const Value *V) const;

63

64

65

66 bool hasLoopInvariantOperands(const Instruction *I) const;

67

68

69

70

71

72

73

74

75

76

77

78 bool makeLoopInvariant(Value *V, bool &Changed,

82

83

84

85

86

87

88

89

90

91

92

97

98

99

100

101

102

103

104

105 PHINode *getCanonicalInductionVariable() const;

106

107

108 ICmpInst *getLatchCmpInst() const;

109

110

111

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153 struct LoopBounds {

154

155

156

157

158

159

160

161 LLVM_ABI static std::optionalLoop::LoopBounds

163

164

166

167

169

170

171

173

174

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

211

212

213

214

215

217

218

220

221 private:

224 : L(Loop), InitialIVValue(I), StepInst(SI), StepValue(SV),

225 FinalIVValue(F), SE(SE) {}

226

227 const Loop &L;

228

229

230 Value &InitialIVValue;

231

232

234

235

236

237 Value *StepValue;

238

239

240 Value &FinalIVValue;

241

243 };

244

245

246

247 std::optional getBounds(ScalarEvolution &SE) const;

248

249

250

251

252

253

254

255

256 PHINode *getInductionVariable(ScalarEvolution &SE) const;

257

258

259

260 bool getInductionDescriptor(ScalarEvolution &SE,

261 InductionDescriptor &IndDesc) const;

262

263

264

265

266

267

268

269

270 bool isAuxiliaryInductionVariable(PHINode &AuxIndVar,

271 ScalarEvolution &SE) const;

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291 BranchInst *getLoopGuardBranch() const;

292

293

294

295

297

298

299

300

301

302

308

309

310

312

313

314

315 bool isLCSSAForm(const DominatorTree &DT, bool IgnoreTokens = true) const;

316

317

318

319

321 bool IgnoreTokens = true) const;

322

323

324

325 bool isLoopSimplifyForm() const;

326

327

328 bool isSafeToClone() const;

329

330

331

332

333

334

335

336

337

338

339

340

341

342 bool isAnnotatedParallel() const;

343

344

345

346

347

348

349

350 MDNode *getLoopID() const;

351

352

353

354

355

356

357

358 void setLoopID(MDNode *LoopID) const;

359

360

361

362

363

364

365

366 void setLoopAlreadyUnrolled();

367

368

369 void setLoopMustProgress();

370

371 void dump() const;

372 void dumpVerbose() const;

373

374

375

376

377

378

379 DebugLoc getStartLoc() const;

380

381

382 LocRange getLocRange() const;

383

384

385

386

387 std::string getLocStr() const;

388

391 if (Header->hasName())

392 return Header->getName();

393 return "";

394 }

395

396private:

397 Loop() = default;

398

400 friend class LoopBase<BasicBlock, Loop>;

402 ~Loop() = default;

403};

404

405

407

408class LoopInfo : public LoopInfoBase<BasicBlock, Loop> {

409 typedef LoopInfoBase<BasicBlock, Loop> BaseT;

410

412

413 void operator=(const LoopInfo &) = delete;

414 LoopInfo(const LoopInfo &) = delete;

415

416public:

420

421 LoopInfo(LoopInfo &&Arg) : BaseT(std::move(static_cast<BaseT &>(Arg))) {}

423 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));

424 return *this;

425 }

426

427

429 FunctionAnalysisManager::Invalidator &);

430

431

432

433

434

435

436

438

439

440

442

443

445 if (I)

446 return true;

447

448

449 if (I->getParent() == From->getParent())

450 return true;

451

452

454 if (!ToLoop)

455 return true;

456

457

458

460 }

461

462

463

464

465

466

469 "Can't reason about IPO!");

470

472 auto *NewBB = NewLoc->getParent();

473

474

475

476 if (OldBB == NewBB)

477 return true;

478

481

482 if (OldLoop == NewLoop)

483 return true;

484

485

486

488 return !Outer || Outer->contains(Inner);

489 };

490

491

492

493

494

495

496

497

498 if (Contains(NewLoop, OldLoop)) {

499 for (Use &U : Inst->uses()) {

502 : UI->getParent();

503 if (UBB != NewBB && getLoopFor(UBB) != NewLoop)

504 return false;

505 }

506 }

507

508

509

510

511 if (Contains(OldLoop, NewLoop)) {

512

514 return false;

515

518 if (!DefI)

519 return false;

520

521

522

523

524 auto *DefBlock = DefI->getParent();

525 if (DefBlock != NewBB && getLoopFor(DefBlock) != NewLoop)

526 return false;

527 }

528 }

529

530 return true;

531 }

532

533

534

535

536

538 wouldBeOutOfLoopUseRequiringLCSSA(const Value *V,

540};

541

542

543

544

545

546

548

549

558

567

568

578

579

588

589

594

595

598

599public:

600 static char ID;

601

603

606

607

609

610 void verifyAnalysis() const override;

611

613

615

616 void getAnalysisUsage(AnalysisUsage &AU) const override;

617};

618

619

621 const std::string &Banner = "");

622

623

624

626

627

628

629

630

631

633

635 StringRef Name);

636

637

639

640

642 StringRef Name);

643

644

645

648

649

650

651

652

653

654LLVM_ABI std::optional<const MDOperand *>

656

657

659

660

661

662

664

665

666

668

669

670

672

673

674

675

676

677

678

679

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

701}

702

703#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

#define LLVM_TEMPLATE_ABI

static bool runOnFunction(Function &F, bool PostInlining)

This file defines the little GraphTraits template class that should be specialized by classes that...

This header defines various interfaces for pass management in LLVM.

Loop::LoopBounds::Direction Direction

static bool Contains(directive::VersionRange V, int P)

Represent the analysis usage information of a pass.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

LLVM Basic Block Representation.

Predicate

This enumeration lists the possible predicates for CmpInst subclasses.

Core dominator tree base class.

Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.

This instruction compares its operands according to the predicate given to the constructor.

A struct for saving information about induction variables.

LLVM_ABI const Function * getFunction() const

Return the function this instruction belongs to.

This is an important class for using LLVM in a threaded context.

Analysis pass that exposes the LoopInfo for a function.

Definition LoopInfo.h:569

LoopInfo Result

Definition LoopInfo.h:574

LLVM_ABI LoopInfo run(Function &F, FunctionAnalysisManager &AM)

Instances of this class are used to represent loops that are detected in the flow graph.

bool contains(const LoopT *L) const

Return true if the specified loop is contained within in this loop.

BasicBlock * getLoopLatch() const

BasicBlock * getHeader() const

bool isLoopExiting(const BasicBlock *BB) const

This class builds and contains all of the top-level loop structures in the specified function.

typename std::vector< Loop * >::const_iterator iterator

Loop * getLoopFor(const BasicBlock *BB) const

const LoopInfo & getLoopInfo() const

Definition LoopInfo.h:605

LoopInfo & getLoopInfo()

Definition LoopInfo.h:604

void releaseMemory() override

releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...

Definition LoopInfo.h:612

static char ID

Definition LoopInfo.h:600

Definition LoopInfo.h:408

LoopInfo & operator=(LoopInfo &&RHS)

Definition LoopInfo.h:422

LoopInfo(LoopInfo &&Arg)

Definition LoopInfo.h:421

bool replacementPreservesLCSSAForm(Instruction *From, Value *To)

Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.

Definition LoopInfo.h:441

LLVM_ABI LoopInfo(const DominatorTreeBase< BasicBlock, false > &DomTree)

bool movementPreservesLCSSAForm(Instruction *Inst, Instruction *NewLoc)

Checks if moving a specific instruction can break LCSSA in any loop.

Definition LoopInfo.h:467

static bool isRequired()

Definition LoopInfo.h:586

LoopPrinterPass(raw_ostream &OS)

Definition LoopInfo.h:584

LocRange(DebugLoc Start, DebugLoc End)

Definition LoopInfo.h:50

const DebugLoc & getStart() const

Definition LoopInfo.h:53

LocRange(DebugLoc Start)

Definition LoopInfo.h:49

const DebugLoc & getEnd() const

Definition LoopInfo.h:54

Represents a single loop in the control flow graph.

Definition LoopInfo.h:40

bool isGuarded() const

Return true iff the loop is.

Definition LoopInfo.h:296

BranchInst * getLoopGuardBranch() const

Return the loop guard branch, if it exists.

StringRef getName() const

Definition LoopInfo.h:389

bool isRotatedForm() const

Return true if the loop is in rotated form.

Definition LoopInfo.h:303

A Module instance is used to store all the information related to an LLVM module.

A set of analyses that are preserved following a run of a transformation pass.

The main scalar evolution driver.

StringRef - Represent a constant reference to a string, i.e.

A Use represents the edge between a Value definition and its users.

LLVM Value Representation.

iterator_range< use_iterator > uses()

const ParentTy * getParent() const

This class implements an extremely fast bulk output stream that can only output to a stream.

This is an optimization pass for GlobalISel generic memory operations.

void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)

LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)

Returns true if Name is applied to TheLoop and enabled.

Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)

LLVM_ABI std::optional< bool > getOptionalBoolLoopAttribute(const Loop *TheLoop, StringRef Name)

LLVM_ABI int getIntLoopAttribute(const Loop *TheLoop, StringRef Name, int Default=0)

Find named metadata for a loop with an integer value.

decltype(auto) dyn_cast(const From &Val)

dyn_cast - Return the argument parameter cast to the specified type.

LLVM_ABI std::optional< const MDOperand * > findStringMetadataForLoop(const Loop *TheLoop, StringRef Name)

Find string metadata for loop.

LLVM_ABI MDNode * findOptionMDForLoop(const Loop *TheLoop, StringRef Name)

Find string metadata for a loop.

LLVM_ABI bool hasMustProgress(const Loop *L)

Look for the loop attribute that requires progress within the loop.

void erase(Container &C, ValueType V)

Wrapper function to remove a value from a container:

LLVM_ABI bool isMustProgress(const Loop *L)

Return true if this loop can be assumed to make progress.

LLVM_ABI CallBase * getLoopConvergenceHeart(const Loop *TheLoop)

Find the convergence heart of the loop.

LLVM_ABI bool isFinite(const Loop *L)

Return true if this loop can be assumed to run for a finite number of iterations.

LLVM_ABI bool VerifyLoopInfo

Enable verification of loop info.

bool isa(const From &Val)

isa - Return true if the parameter to the template is an instance of one of the template type argu...

LLVM_ABI std::optional< int > getOptionalIntLoopAttribute(const Loop *TheLoop, StringRef Name)

Find named metadata for a loop with an integer value.

LLVM_ABI bool isValidAsAccessGroup(MDNode *AccGroup)

Return whether an MDNode might represent an access group.

LLVM_ABI void printLoop(const Loop &L, raw_ostream &OS, const std::string &Banner="")

Function to print a loop's contents as LLVM's text IR assembly.

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

decltype(auto) cast(const From &Val)

cast - Return the argument parameter cast to the specified type.

LLVM_ABI llvm::MDNode * makePostTransformationMetadata(llvm::LLVMContext &Context, MDNode *OrigLoopID, llvm::ArrayRef< llvm::StringRef > RemovePrefixes, llvm::ArrayRef< llvm::MDNode * > AddAttrs)

Create a new LoopID after the loop has been transformed.

template class LLVM_TEMPLATE_ABI LoopInfoBase< BasicBlock, Loop >

Definition LoopInfo.h:399

AnalysisManager< Function > FunctionAnalysisManager

Convenience typedef for the Function analysis manager.

LLVM_ABI MDNode * findOptionMDForLoopID(MDNode *LoopID, StringRef Name)

Find and return the loop attribute node for the attribute Name in LoopID.

Implement std::hash so that hash_code can be used in STL containers.

A CRTP mix-in that provides informational APIs needed for analysis passes.

A special type used by analysis passes to provide an address that identifies that particular analysis...

static NodeRef getEntryNode(Loop *L)

Definition LoopInfo.h:563

LoopInfo::iterator ChildIteratorType

Definition LoopInfo.h:561

Loop * NodeRef

Definition LoopInfo.h:560

static ChildIteratorType child_begin(NodeRef N)

Definition LoopInfo.h:564

static ChildIteratorType child_end(NodeRef N)

Definition LoopInfo.h:565

static ChildIteratorType child_begin(NodeRef N)

Definition LoopInfo.h:555

static ChildIteratorType child_end(NodeRef N)

Definition LoopInfo.h:556

static NodeRef getEntryNode(const Loop *L)

Definition LoopInfo.h:554

LoopInfo::iterator ChildIteratorType

Definition LoopInfo.h:552

const Loop * NodeRef

Definition LoopInfo.h:551

Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...

Verifier pass for the LoopAnalysis results.

Definition LoopInfo.h:590

LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)

static bool isRequired()

Definition LoopInfo.h:592

Below are some utilities to get the loop guard, loop bounds and induction variable,...

Definition LoopInfo.h:153

static LLVM_ABI std::optional< Loop::LoopBounds > getBounds(const Loop &L, PHINode &IndVar, ScalarEvolution &SE)

Return the LoopBounds object if.

Direction

An enum for the direction of the loop.

Definition LoopInfo.h:216

@ Decreasing

Definition LoopInfo.h:216

@ Unknown

Definition LoopInfo.h:216

@ Increasing

Definition LoopInfo.h:216

Value & getFinalIVValue() const

Get the final value of the loop induction variable.

Definition LoopInfo.h:175

Value * getStepValue() const

Get the step that the loop induction variable gets updated by in each loop iteration.

Definition LoopInfo.h:172

Instruction & getStepInst() const

Get the instruction that updates the loop induction variable.

Definition LoopInfo.h:168

Value & getInitialIVValue() const

Get the initial value of the loop induction variable.

Definition LoopInfo.h:165

A CRTP mix-in to automatically provide informational APIs needed for passes.