clang: lib/CodeGen/CGCleanup.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H

14#define LLVM_CLANG_LIB_CODEGEN_CGCLEANUP_H

15

17

19#include "llvm/ADT/STLExtras.h"

20#include "llvm/ADT/SetVector.h"

21#include "llvm/ADT/SmallPtrSet.h"

22#include "llvm/ADT/SmallVector.h"

23#include "llvm/IR/Instruction.h"

24

25namespace llvm {

26class BasicBlock;

28class ConstantInt;

29}

30

36

37

38

43

44

46public:

48

49private:

50 llvm::BasicBlock *CachedLandingPad;

51 llvm::BasicBlock *CachedEHDispatchBlock;

52

54

55 class CommonBitFields {

57 LLVM_PREFERRED_TYPE(Kind)

58 unsigned Kind : 3;

59 };

60 enum { NumCommonBits = 3 };

61

62protected:

66

67 unsigned NumHandlers : 32 - NumCommonBits;

68 };

69

73

74

75 LLVM_PREFERRED_TYPE(bool)

76 unsigned IsNormalCleanup : 1;

77

78

79 LLVM_PREFERRED_TYPE(bool)

80 unsigned IsEHCleanup : 1;

81

82

83 LLVM_PREFERRED_TYPE(bool)

84 unsigned IsActive : 1;

85

86

87 LLVM_PREFERRED_TYPE(bool)

88 unsigned IsLifetimeMarker : 1;

89

90

91 LLVM_PREFERRED_TYPE(bool)

92 unsigned IsFakeUse : 1;

93

94

95 LLVM_PREFERRED_TYPE(bool)

96 unsigned TestFlagInNormalCleanup : 1;

97

98

99 LLVM_PREFERRED_TYPE(bool)

100 unsigned TestFlagInEHCleanup : 1;

101

102

103

104 unsigned CleanupSize : 12;

105 };

106

110

111 unsigned NumFilters : 32 - NumCommonBits;

112 };

113

114 union {

119 };

120

121public:

123 : CachedLandingPad(nullptr), CachedEHDispatchBlock(nullptr),

124 EnclosingEHScope(enclosingEHScope) {

126 }

127

129

131 return CachedLandingPad;

132 }

133

135 CachedLandingPad = block;

136 }

137

139 return CachedEHDispatchBlock;

140 }

141

143 CachedEHDispatchBlock = block;

144 }

145

148 return !block->use_empty();

149 return false;

150 }

151

153 return EnclosingEHScope;

154 }

155};

156

157

158

159

160

161

163

164

165

166

167

168public:

170

171

173

174

176

178 };

179

180private:

182

184 return reinterpret_cast<Handler*>(this+1);

185 }

186

187 const Handler *getHandlers() const {

188 return reinterpret_cast<const Handler*>(this+1);

189 }

190

191public:

195

199 CatchBits.NumHandlers = numHandlers;

200 assert(CatchBits.NumHandlers == numHandlers && "NumHandlers overflow?");

201 }

202

206

210

214 getHandlers()[I].Block = Block;

215 }

216

219 getHandlers()[I].Type = Type;

220 getHandlers()[I].Block = Block;

221 }

222

225 return getHandlers()[I];

226 }

227

228

229

230

231

236

240

244};

245

246

248

250

251

253

254

255

256 llvm::BasicBlock *NormalBlock;

257

258

259

261

262

263

264

265

266 struct ExtInfo {

267

269

270

272 BranchAfters;

273 };

274 mutable struct ExtInfo *ExtInfo;

275

276

277

278

279 struct AuxillaryAllocas {

281 bool used = false;

282

283

284 void Add(llvm::AllocaInst *Alloca) { AuxAllocas.push_back(Alloca); }

285

286

287 void MarkUsed() {

288 used = true;

289 AuxAllocas.clear();

290 }

291

292 ~AuxillaryAllocas() {

293 if (used)

294 return;

295 llvm::SetVector<llvm::Instruction *> Uses;

296 for (auto *Inst : llvm::reverse(AuxAllocas))

297 CollectUses(Inst, Uses);

298

299 for (auto *I : llvm::reverse(Uses))

300 I->eraseFromParent();

301 }

302

303 private:

304 void CollectUses(llvm::Instruction *I,

305 llvm::SetVector<llvm::Instruction *> &Uses) {

306 if (!I || !Uses.insert(I))

307 return;

308 for (auto *User : I->users())

310 }

311 };

312 mutable struct AuxillaryAllocas *AuxAllocas;

313

314 AuxillaryAllocas &getAuxillaryAllocas() {

315 if (!AuxAllocas) {

316 AuxAllocas = new struct AuxillaryAllocas();

317 }

318 return *AuxAllocas;

319 }

320

321

322

323

324 unsigned FixupDepth;

325

326 struct ExtInfo &getExtInfo() {

327 if (!ExtInfo) ExtInfo = new struct ExtInfo();

328 return *ExtInfo;

329 }

330

331 const struct ExtInfo &getExtInfo() const {

332 if (!ExtInfo) ExtInfo = new struct ExtInfo();

333 return *ExtInfo;

334 }

335

336public:

337

338

342

346

348 unsigned fixupDepth,

352 EnclosingNormal(enclosingNormal), NormalBlock(nullptr),

354 FixupDepth(fixupDepth) {

360 CleanupBits.TestFlagInNormalCleanup = false;

363

364 assert(CleanupBits.CleanupSize == cleanupSize && "cleanup size overflow");

365 }

366

368 if (AuxAllocas)

369 delete AuxAllocas;

370 delete ExtInfo;

371 }

373 for (auto *Alloca : Allocas)

374 getAuxillaryAllocas().Add(Alloca);

375 }

376 void MarkEmitted() { getAuxillaryAllocas().MarkUsed(); }

377

379

383

385

388

391

394

397 return ActiveFlag;

398 }

401 ActiveFlag = Var;

402 }

403

410

417

420 return EnclosingNormal;

421 }

422

425

429

430

431 bool hasBranches() const { return ExtInfo && !ExtInfo->Branches.empty(); }

432

433

434

435

436

437

438

439

440

441

442

443

445 llvm::BasicBlock *Block) {

446 struct ExtInfo &ExtInfo = getExtInfo();

447 if (ExtInfo.Branches.insert(Block).second)

448 ExtInfo.BranchAfters.push_back(std::make_pair(Block, Index));

449 }

450

451

453 return ExtInfo ? ExtInfo->BranchAfters.size() : 0;

454 }

455

458 return ExtInfo->BranchAfters[I].first;

459 }

460

463 return ExtInfo->BranchAfters[I].second;

464 }

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

482 return getExtInfo().Branches.insert(Block).second;

483 }

484

485

487 if (!ExtInfo) return false;

488 return (ExtInfo->BranchAfters.size() != ExtInfo->Branches.size());

489 }

490

494};

495

496

497

498

499

500

502 "EHCleanupScope expected alignment");

503

504

505

506

507

508

510

511

512

513 llvm::Value **getFilters() {

514 return reinterpret_castllvm::Value\*\*\(this+1);

515 }

516

517 llvm::Value * const *getFilters() const {

518 return reinterpret_cast<llvm::Value* const *>(this+1);

519 }

520

521public:

525 assert(FilterBits.NumFilters == numFilters && "NumFilters overflow");

526 }

527

529 return sizeof(EHFilterScope) + numFilters * sizeof(llvm::Value*);

530 }

531

533

534 void setFilter(unsigned i, llvm::Value *filterValue) {

536 getFilters()[i] = filterValue;

537 }

538

541 return getFilters()[i];

542 }

543

547};

548

549

550

561

562

564 char *Ptr;

565

567 explicit iterator(char *Ptr) : Ptr(Ptr) {}

568

569public:

571

573 return reinterpret_cast<EHScope*>(Ptr);

574 }

575

578

580 size_t Size;

584 static_cast<const EHCatchScope *>(get())->getNumHandlers());

585 break;

586

590 break;

591

593 Size = static_cast<const EHCleanupScope *>(get())->getAllocatedSize();

594 break;

595

598 break;

599 }

601 return *this;

602 }

603

605 iterator copy = *this;

606 ++copy;

607 return copy;

608 }

609

611 iterator copy = *this;

613 return copy;

614 }

615

616 bool encloses(iterator other) const { return Ptr >= other.Ptr; }

618

619 bool operator==(iterator other) const { return Ptr == other.Ptr; }

620 bool operator!=(iterator other) const { return Ptr != other.Ptr; }

621};

622

626

630

632 assert(empty() && "popping exception stack when not empty");

633

637}

638

640 assert(empty() && "popping exception stack when not empty");

641

645}

646

648 assert(sp.isValid() && "finding invalid savepoint");

649 assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");

650 return iterator(EndOfBuffer - sp.Size);

651}

652

655 assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);

657}

658

659

662

663

664

665

667

670

689

690

691

695

700

702

704};

705}

706}

707

708#endif

static Decl::Kind getKind(const Decl *D)

bool isOne() const

isOne - Test whether the quantity equals one.

Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...

CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...

This class organizes the cross-function state that is used while generating LLVM code.

A scope which attempts to handle some, possibly all, types of exceptions.

Definition CGCleanup.h:162

EHCatchScope(unsigned numHandlers, EHScopeStack::stable_iterator enclosingEHScope)

Definition CGCleanup.h:196

iterator begin() const

Definition CGCleanup.h:238

const Handler & getHandler(unsigned I) const

Definition CGCleanup.h:223

void setHandler(unsigned I, llvm::Constant *Type, llvm::BasicBlock *Block)

Definition CGCleanup.h:211

friend class EHScopeStack

Definition CGCleanup.h:181

static size_t getSizeForNumHandlers(unsigned N)

Definition CGCleanup.h:192

void setHandler(unsigned I, CatchTypeInfo Type, llvm::BasicBlock *Block)

Definition CGCleanup.h:217

void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block)

Definition CGCleanup.h:207

void clearHandlerBlocks()

Definition CGCleanup.h:232

static bool classof(const EHScope *Scope)

Definition CGCleanup.h:241

const Handler * iterator

Definition CGCleanup.h:237

iterator end() const

Definition CGCleanup.h:239

unsigned getNumHandlers() const

Definition CGCleanup.h:203

A cleanup scope which generates the cleanup blocks lazily.

Definition CGCleanup.h:247

void setActive(bool A)

Definition CGCleanup.h:387

bool isFakeUse() const

Definition CGCleanup.h:392

void MarkEmitted()

Definition CGCleanup.h:376

bool shouldTestFlagInEHCleanup() const

Definition CGCleanup.h:414

void Destroy()

Definition CGCleanup.h:367

bool isLifetimeMarker() const

Definition CGCleanup.h:389

Address getActiveFlag() const

Definition CGCleanup.h:396

EHScopeStack::stable_iterator getEnclosingNormalCleanup() const

Definition CGCleanup.h:419

size_t getAllocatedSize() const

Definition CGCleanup.h:343

bool hasActiveFlag() const

Definition CGCleanup.h:395

void setNormalBlock(llvm::BasicBlock *BB)

Definition CGCleanup.h:382

llvm::ConstantInt * getBranchAfterIndex(unsigned I) const

Definition CGCleanup.h:461

bool shouldTestFlagInNormalCleanup() const

Definition CGCleanup.h:407

bool addBranchThrough(llvm::BasicBlock *Block)

Add a branch-through to this cleanup scope.

Definition CGCleanup.h:481

llvm::BasicBlock * getBranchAfterBlock(unsigned I) const

Definition CGCleanup.h:456

void AddAuxAllocas(llvm::SmallVector< llvm::AllocaInst * > Allocas)

Definition CGCleanup.h:372

void setTestFlagInNormalCleanup()

Definition CGCleanup.h:404

void setTestFlagInEHCleanup()

Definition CGCleanup.h:411

void * getCleanupBuffer()

Definition CGCleanup.h:424

unsigned getNumBranchAfters() const

Return the number of unique branch-afters on this scope.

Definition CGCleanup.h:452

bool isNormalCleanup() const

Definition CGCleanup.h:380

static size_t getSizeForCleanupSize(size_t Size)

Gets the size required for a lazy cleanup scope with the given cleanup-data requirements.

Definition CGCleanup.h:339

bool hasBranches() const

True if this cleanup scope has any branch-afters or branch-throughs.

Definition CGCleanup.h:431

void addBranchAfter(llvm::ConstantInt *Index, llvm::BasicBlock *Block)

Add a branch-after to this cleanup scope.

Definition CGCleanup.h:444

void setFakeUse()

Definition CGCleanup.h:393

void setActiveFlag(RawAddress Var)

Definition CGCleanup.h:399

bool isEHCleanup() const

Definition CGCleanup.h:384

EHCleanupScope(bool isNormal, bool isEH, unsigned cleanupSize, unsigned fixupDepth, EHScopeStack::stable_iterator enclosingNormal, EHScopeStack::stable_iterator enclosingEH)

Definition CGCleanup.h:347

EHScopeStack::Cleanup * getCleanup()

Definition CGCleanup.h:426

bool isActive() const

Definition CGCleanup.h:386

unsigned getFixupDepth() const

Definition CGCleanup.h:418

size_t getCleanupSize() const

Definition CGCleanup.h:423

static bool classof(const EHScope *Scope)

Definition CGCleanup.h:491

llvm::BasicBlock * getNormalBlock() const

Definition CGCleanup.h:381

bool hasBranchThroughs() const

Determines if this cleanup scope has any branch throughs.

Definition CGCleanup.h:486

void setLifetimeMarker()

Definition CGCleanup.h:390

An exceptions scope which filters exceptions thrown through it.

Definition CGCleanup.h:509

void setFilter(unsigned i, llvm::Value *filterValue)

Definition CGCleanup.h:534

static size_t getSizeForNumFilters(unsigned numFilters)

Definition CGCleanup.h:528

EHFilterScope(unsigned numFilters)

Definition CGCleanup.h:522

llvm::Value * getFilter(unsigned i) const

Definition CGCleanup.h:539

unsigned getNumFilters() const

Definition CGCleanup.h:532

static bool classof(const EHScope *scope)

Definition CGCleanup.h:544

A non-stable pointer into the scope stack.

Definition CGCleanup.h:563

bool operator!=(iterator other) const

Definition CGCleanup.h:620

iterator operator++(int)

Definition CGCleanup.h:610

EHScope & operator*() const

Definition CGCleanup.h:577

iterator()

Definition CGCleanup.h:570

bool encloses(iterator other) const

Definition CGCleanup.h:616

iterator & operator++()

Definition CGCleanup.h:579

friend class EHScopeStack

Definition CGCleanup.h:566

iterator next()

Definition CGCleanup.h:604

bool strictlyEncloses(iterator other) const

Definition CGCleanup.h:617

EHScope * operator->() const

Definition CGCleanup.h:576

bool operator==(iterator other) const

Definition CGCleanup.h:619

EHScope * get() const

Definition CGCleanup.h:572

A saved depth on the scope stack.

A stack of scopes which respond to exceptions, including cleanups and catch blocks.

stable_iterator stable_begin() const

Create a stable reference to the top of the EH stack.

bool empty() const

Determines whether the exception-scopes stack is empty.

iterator end() const

Returns an iterator pointing to the outermost EH scope.

Definition CGCleanup.h:627

iterator begin() const

Returns an iterator pointing to the innermost EH scope.

Definition CGCleanup.h:623

void popCatch()

Pops a catch scope off the stack. This is private to CGException.cpp.

Definition CGCleanup.h:631

iterator find(stable_iterator save) const

Turn a stable reference to a scope depth into a unstable pointer to the EH stack.

Definition CGCleanup.h:647

stable_iterator stabilize(iterator it) const

Translates an iterator into a stable_iterator.

Definition CGCleanup.h:654

void popTerminate()

Pops a terminate handler off the stack.

Definition CGCleanup.h:639

Definition CGCleanup.h:63

friend class EHCatchScope

Definition CGCleanup.h:64

Definition CGCleanup.h:70

friend class EHCleanupScope

Definition CGCleanup.h:71

Definition CGCleanup.h:107

friend class EHFilterScope

Definition CGCleanup.h:108

A protected scope for zero-cost EH handling.

Definition CGCleanup.h:45

llvm::BasicBlock * getCachedLandingPad() const

Definition CGCleanup.h:130

EHScope(Kind kind, EHScopeStack::stable_iterator enclosingEHScope)

Definition CGCleanup.h:122

void setCachedLandingPad(llvm::BasicBlock *block)

Definition CGCleanup.h:134

CleanupBitFields CleanupBits

Definition CGCleanup.h:117

Kind getKind() const

Definition CGCleanup.h:128

Kind

Definition CGCleanup.h:47

@ Filter

Definition CGCleanup.h:47

@ Catch

Definition CGCleanup.h:47

@ Terminate

Definition CGCleanup.h:47

@ Cleanup

Definition CGCleanup.h:47

FilterBitFields FilterBits

Definition CGCleanup.h:118

EHScopeStack::stable_iterator getEnclosingEHScope() const

Definition CGCleanup.h:152

CatchBitFields CatchBits

Definition CGCleanup.h:116

llvm::BasicBlock * getCachedEHDispatchBlock() const

Definition CGCleanup.h:138

void setCachedEHDispatchBlock(llvm::BasicBlock *block)

Definition CGCleanup.h:142

bool hasEHBranches() const

Definition CGCleanup.h:146

CommonBitFields CommonBits

Definition CGCleanup.h:115

An exceptions scope which calls std::terminate if any exception reaches it.

Definition CGCleanup.h:551

static size_t getSize()

Definition CGCleanup.h:555

EHTerminateScope(EHScopeStack::stable_iterator enclosingEHScope)

Definition CGCleanup.h:553

static bool classof(const EHScope *scope)

Definition CGCleanup.h:557

An abstract representation of an aligned address.

CharUnits getAlignment() const

Return the alignment of this pointer.

Represents a function declaration or definition.

Scope - A scope is a transient data structure that is used while parsing the program.

@ Type

The l-value was considered opaque, so the alignment was determined from a type.

The JSON file list parser is used to communicate input to InstallAPI.

nullptr

This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...

U cast(CodeGen::Address addr)

Diagnostic wrappers for TextAPI types for error reporting.

The MS C++ ABI needs a pointer to RTTI data plus some flags to describe the type of a catch handler,...

Definition CGCleanup.h:39

llvm::Constant * RTTI

Definition CGCleanup.h:40

unsigned Flags

Definition CGCleanup.h:41

Definition CGCleanup.h:169

bool isCatchAll() const

Definition CGCleanup.h:177

CatchTypeInfo Type

A type info value, or null (C++ null, not an LLVM null pointer) for a catch-all.

Definition CGCleanup.h:172

llvm::BasicBlock * Block

The catch handler for this type.

Definition CGCleanup.h:175

The exceptions personality for a function.

Definition CGCleanup.h:660

bool isMSVCXXPersonality() const

Definition CGCleanup.h:703

static const EHPersonality & get(CodeGenModule &CGM, const FunctionDecl *FD)

static const EHPersonality XL_CPlusPlus

Definition CGCleanup.h:687

static const EHPersonality GNU_ObjC_SJLJ

Definition CGCleanup.h:675

bool isWasmPersonality() const

Definition CGCleanup.h:701

static const EHPersonality ZOS_CPlusPlus

Definition CGCleanup.h:688

static const EHPersonality GNUstep_ObjC

Definition CGCleanup.h:677

static const EHPersonality MSVC_CxxFrameHandler3

Definition CGCleanup.h:685

bool usesFuncletPads() const

Does this personality use landingpads or the family of pad instructions designed to form funclets?

Definition CGCleanup.h:692

static const EHPersonality MSVC_C_specific_handler

Definition CGCleanup.h:684

static const EHPersonality GNU_CPlusPlus_SEH

Definition CGCleanup.h:682

static const EHPersonality GNU_ObjC

Definition CGCleanup.h:674

static const EHPersonality GNU_CPlusPlus_SJLJ

Definition CGCleanup.h:681

static const EHPersonality GNU_C_SJLJ

Definition CGCleanup.h:672

static const EHPersonality GNU_C

Definition CGCleanup.h:671

static const EHPersonality NeXT_ObjC

Definition CGCleanup.h:679

const char * CatchallRethrowFn

Definition CGCleanup.h:666

static const EHPersonality GNU_CPlusPlus

Definition CGCleanup.h:680

static const EHPersonality GNU_ObjCXX

Definition CGCleanup.h:678

bool isMSVCPersonality() const

Definition CGCleanup.h:696

static const EHPersonality GNU_C_SEH

Definition CGCleanup.h:673

static const EHPersonality MSVC_except_handler

Definition CGCleanup.h:683

static const EHPersonality GNU_ObjC_SEH

Definition CGCleanup.h:676

const char * PersonalityFn

Definition CGCleanup.h:661

static const EHPersonality GNU_Wasm_CPlusPlus

Definition CGCleanup.h:686