| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef LLVM_IR_BASICBLOCK_H |
| 14 |
#define LLVM_IR_BASICBLOCK_H |
| 15 |
|
| 16 |
#include "llvm-c/Types.h" |
| 17 |
#include "llvm/ADT/Twine.h" |
| 18 |
#include "llvm/ADT/ilist.h" |
| 19 |
#include "llvm/ADT/ilist_node.h" |
| 20 |
#include "llvm/ADT/iterator.h" |
| 21 |
#include "llvm/ADT/iterator_range.h" |
| 22 |
#include "llvm/IR/Instruction.h" |
| 23 |
#include "llvm/IR/SymbolTableListTraits.h" |
| 24 |
#include "llvm/IR/Value.h" |
| 25 |
#include "llvm/Support/CBindingWrapping.h" |
| 26 |
#include "llvm/Support/Casting.h" |
| 27 |
#include "llvm/Support/Compiler.h" |
| 28 |
#include <cassert> |
| 29 |
#include <cstddef> |
| 30 |
#include <iterator> |
| 31 |
|
| 32 |
namespace llvm { |
| 33 |
|
| 34 |
class AssemblyAnnotationWriter; |
| 35 |
class CallInst; |
| 36 |
class Function; |
| 37 |
class LandingPadInst; |
| 38 |
class LLVMContext; |
| 39 |
class Module; |
| 40 |
class PHINode; |
| 41 |
class ValueSymbolTable; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
class BasicBlock final : public Value, |
| 59 |
public ilist_node_with_parent<BasicBlock, Function> { |
| 60 |
public: |
| 61 |
using InstListType = SymbolTableList<Instruction>; |
| 62 |
|
| 63 |
private: |
| 64 |
friend class BlockAddress; |
| 65 |
friend class SymbolTableListTraits<BasicBlock>; |
| 66 |
|
| 67 |
InstListType InstList; |
| 68 |
Function *Parent; |
| 69 |
|
| 70 |
void setParent(Function *parent); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
explicit BasicBlock(LLVMContext &C, const Twine &Name = "", |
| 78 |
Function *Parent = nullptr, |
| 79 |
BasicBlock *InsertBefore = nullptr); |
| 80 |
|
| 81 |
public: |
| 82 |
BasicBlock(const BasicBlock &) = delete; |
| 83 |
BasicBlock &operator=(const BasicBlock &) = delete; |
| 84 |
~BasicBlock(); |
| 85 |
|
| 86 |
|
| 87 |
LLVMContext &getContext() const; |
| 88 |
|
| 89 |
|
| 90 |
using iterator = InstListType::iterator; |
| 91 |
using const_iterator = InstListType::const_iterator; |
| 92 |
using reverse_iterator = InstListType::reverse_iterator; |
| 93 |
using const_reverse_iterator = InstListType::const_reverse_iterator; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
static BasicBlock *Create(LLVMContext &Context, const Twine &Name = "", |
| 101 |
Function *Parent = nullptr, |
| 102 |
BasicBlock *InsertBefore = nullptr) { |
| 103 |
return new BasicBlock(Context, Name, Parent, InsertBefore); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
const Function *getParent() const { return Parent; } |
| 108 |
Function *getParent() { return Parent; } |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
const Module *getModule() const; |
| 115 |
Module *getModule() { |
| 116 |
return const_cast<Module *>( |
| 117 |
static_cast<const BasicBlock *>(this)->getModule()); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
const Instruction *getTerminator() const LLVM_READONLY; |
| 123 |
Instruction *getTerminator() { |
| 124 |
return const_cast<Instruction *>( |
| 125 |
static_cast<const BasicBlock *>(this)->getTerminator()); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
const CallInst *getTerminatingDeoptimizeCall() const; |
| 132 |
CallInst *getTerminatingDeoptimizeCall() { |
| 133 |
return const_cast<CallInst *>( |
| 134 |
static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall()); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
const CallInst *getPostdominatingDeoptimizeCall() const; |
| 141 |
CallInst *getPostdominatingDeoptimizeCall() { |
| 142 |
return const_cast<CallInst *>( |
| 143 |
static_cast<const BasicBlock *>(this)->getPostdominatingDeoptimizeCall()); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
const CallInst *getTerminatingMustTailCall() const; |
| 150 |
CallInst *getTerminatingMustTailCall() { |
| 151 |
return const_cast<CallInst *>( |
| 152 |
static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall()); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
const Instruction* getFirstNonPHI() const; |
| 162 |
Instruction* getFirstNonPHI() { |
| 163 |
return const_cast<Instruction *>( |
| 164 |
static_cast<const BasicBlock *>(this)->getFirstNonPHI()); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
const Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) const; |
| 171 |
Instruction *getFirstNonPHIOrDbg(bool SkipPseudoOp = true) { |
| 172 |
return const_cast<Instruction *>( |
| 173 |
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg( |
| 174 |
SkipPseudoOp)); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
const Instruction * |
| 181 |
getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) const; |
| 182 |
Instruction *getFirstNonPHIOrDbgOrLifetime(bool SkipPseudoOp = true) { |
| 183 |
return const_cast<Instruction *>( |
| 184 |
static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime( |
| 185 |
SkipPseudoOp)); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
const_iterator getFirstInsertionPt() const; |
| 193 |
iterator getFirstInsertionPt() { |
| 194 |
return static_cast<const BasicBlock *>(this) |
| 195 |
->getFirstInsertionPt().getNonConst(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
iterator_range<filter_iterator<BasicBlock::const_iterator, |
| 202 |
std::function<bool(const Instruction &)>>> |
| 203 |
instructionsWithoutDebug(bool SkipPseudoOp = true) const; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
iterator_range< |
| 209 |
filter_iterator<BasicBlock::iterator, std::function<bool(Instruction &)>>> |
| 210 |
instructionsWithoutDebug(bool SkipPseudoOp = true); |
| 211 |
|
| 212 |
|
| 213 |
filter_iterator<BasicBlock::const_iterator, |
| 214 |
std::function<bool(const Instruction &)>>::difference_type |
| 215 |
sizeWithoutDebug() const; |
| 216 |
|
| 217 |
|
| 218 |
void removeFromParent(); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
SymbolTableList<BasicBlock>::iterator eraseFromParent(); |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
void moveBefore(BasicBlock *MovePos); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
void moveAfter(BasicBlock *MovePos); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
void insertInto(Function *Parent, BasicBlock *InsertBefore = nullptr); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
const BasicBlock *getSinglePredecessor() const; |
| 244 |
BasicBlock *getSinglePredecessor() { |
| 245 |
return const_cast<BasicBlock *>( |
| 246 |
static_cast<const BasicBlock *>(this)->getSinglePredecessor()); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
const BasicBlock *getUniquePredecessor() const; |
| 256 |
BasicBlock *getUniquePredecessor() { |
| 257 |
return const_cast<BasicBlock *>( |
| 258 |
static_cast<const BasicBlock *>(this)->getUniquePredecessor()); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
bool hasNPredecessors(unsigned N) const; |
| 263 |
|
| 264 |
|
| 265 |
bool hasNPredecessorsOrMore(unsigned N) const; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
const BasicBlock *getSingleSuccessor() const; |
| 272 |
BasicBlock *getSingleSuccessor() { |
| 273 |
return const_cast<BasicBlock *>( |
| 274 |
static_cast<const BasicBlock *>(this)->getSingleSuccessor()); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
const BasicBlock *getUniqueSuccessor() const; |
| 282 |
BasicBlock *getUniqueSuccessor() { |
| 283 |
return const_cast<BasicBlock *>( |
| 284 |
static_cast<const BasicBlock *>(this)->getUniqueSuccessor()); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW = nullptr, |
| 290 |
bool ShouldPreserveUseListOrder = false, |
| 291 |
bool IsForDebug = false) const; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
inline iterator begin() { return InstList.begin(); } |
| 297 |
inline const_iterator begin() const { return InstList.begin(); } |
| 298 |
inline iterator end () { return InstList.end(); } |
| 299 |
inline const_iterator end () const { return InstList.end(); } |
| 300 |
|
| 301 |
inline reverse_iterator rbegin() { return InstList.rbegin(); } |
| 302 |
inline const_reverse_iterator rbegin() const { return InstList.rbegin(); } |
| 303 |
inline reverse_iterator rend () { return InstList.rend(); } |
| 304 |
inline const_reverse_iterator rend () const { return InstList.rend(); } |
| 305 |
|
| 306 |
inline size_t size() const { return InstList.size(); } |
| 307 |
inline bool empty() const { return InstList.empty(); } |
| 308 |
inline const Instruction &front() const { return InstList.front(); } |
| 309 |
inline Instruction &front() { return InstList.front(); } |
| 310 |
inline const Instruction &back() const { return InstList.back(); } |
| 311 |
inline Instruction &back() { return InstList.back(); } |
| 312 |
|
| 313 |
|
| 314 |
template <typename PHINodeT = PHINode, typename BBIteratorT = iterator> |
| 315 |
class phi_iterator_impl |
| 316 |
: public iterator_facade_base<phi_iterator_impl<PHINodeT, BBIteratorT>, |
| 317 |
std::forward_iterator_tag, PHINodeT> { |
| 318 |
friend BasicBlock; |
| 319 |
|
| 320 |
PHINodeT *PN; |
| 321 |
|
| 322 |
phi_iterator_impl(PHINodeT *PN) : PN(PN) {} |
| 323 |
|
| 324 |
public: |
| 325 |
|
| 326 |
|
| 327 |
phi_iterator_impl() = default; |
| 328 |
|
| 329 |
|
| 330 |
template <typename PHINodeU, typename BBIteratorU, |
| 331 |
typename = std::enable_if_t< |
| 332 |
std::is_convertible<PHINodeU *, PHINodeT *>::value>> |
| 333 |
phi_iterator_impl(const phi_iterator_impl<PHINodeU, BBIteratorU> &Arg) |
| 334 |
: PN(Arg.PN) {} |
| 335 |
|
| 336 |
bool operator==(const phi_iterator_impl &Arg) const { return PN == Arg.PN; } |
| 337 |
|
| 338 |
PHINodeT &operator*() const { return *PN; } |
| 339 |
|
| 340 |
using phi_iterator_impl::iterator_facade_base::operator++; |
| 341 |
phi_iterator_impl &operator++() { |
| 342 |
assert(PN && "Cannot increment the end iterator!"); |
| 343 |
PN = dyn_cast<PHINodeT>(std::next(BBIteratorT(PN))); |
| 344 |
return *this; |
| 345 |
} |
| 346 |
}; |
| 347 |
using phi_iterator = phi_iterator_impl<>; |
| 348 |
using const_phi_iterator = |
| 349 |
phi_iterator_impl<const PHINode, BasicBlock::const_iterator>; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
iterator_range<const_phi_iterator> phis() const { |
| 355 |
return const_cast<BasicBlock *>(this)->phis(); |
| 356 |
} |
| 357 |
iterator_range<phi_iterator> phis(); |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
const InstListType &getInstList() const { return InstList; } |
| 364 |
InstListType &getInstList() { return InstList; } |
| 365 |
|
| 366 |
|
| 367 |
static InstListType BasicBlock::*getSublistAccess(Instruction*) { |
| 368 |
return &BasicBlock::InstList; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
ValueSymbolTable *getValueSymbolTable(); |
| 373 |
|
| 374 |
|
| 375 |
static bool classof(const Value *V) { |
| 376 |
return V->getValueID() == Value::BasicBlockVal; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
void dropAllReferences(); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs = false); |
| 396 |
|
| 397 |
bool canSplitPredecessors() const; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
BasicBlock *splitBasicBlock(iterator I, const Twine &BBName = "", |
| 418 |
bool Before = false); |
| 419 |
BasicBlock *splitBasicBlock(Instruction *I, const Twine &BBName = "", |
| 420 |
bool Before = false) { |
| 421 |
return splitBasicBlock(I->getIterator(), BBName, Before); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
BasicBlock *splitBasicBlockBefore(iterator I, const Twine &BBName = ""); |
| 442 |
BasicBlock *splitBasicBlockBefore(Instruction *I, const Twine &BBName = "") { |
| 443 |
return splitBasicBlockBefore(I->getIterator(), BBName); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
bool hasAddressTaken() const { |
| 449 |
return getBasicBlockBits().BlockAddressRefCount != 0; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New); |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
void replaceSuccessorsPhiUsesWith(BasicBlock *Old, BasicBlock *New); |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
void replaceSuccessorsPhiUsesWith(BasicBlock *New); |
| 463 |
|
| 464 |
|
| 465 |
bool isEHPad() const { return getFirstNonPHI()->isEHPad(); } |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
bool isLandingPad() const; |
| 472 |
|
| 473 |
|
| 474 |
const LandingPadInst *getLandingPadInst() const; |
| 475 |
LandingPadInst *getLandingPadInst() { |
| 476 |
return const_cast<LandingPadInst *>( |
| 477 |
static_cast<const BasicBlock *>(this)->getLandingPadInst()); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
bool isLegalToHoistInto() const; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
bool isEntryBlock() const; |
| 486 |
|
| 487 |
Optional<uint64_t> getIrrLoopHeaderWeight() const; |
| 488 |
|
| 489 |
|
| 490 |
bool isInstrOrderValid() const { |
| 491 |
return getBasicBlockBits().InstrOrderValid; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
void invalidateOrders() { |
| 496 |
validateInstrOrdering(); |
| 497 |
BasicBlockBits Bits = getBasicBlockBits(); |
| 498 |
Bits.InstrOrderValid = false; |
| 499 |
setBasicBlockBits(Bits); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
void renumberInstructions(); |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
void validateInstrOrdering() const; |
| 514 |
|
| 515 |
private: |
| 516 |
#if defined(_AIX) && (!defined(__GNUC__) || defined(__clang__)) |
| 517 |
|
| 518 |
|
| 519 |
#define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)") |
| 520 |
#define END_TWO_BYTE_PACK() _Pragma("pack(pop)") |
| 521 |
#else |
| 522 |
#define BEGIN_TWO_BYTE_PACK() |
| 523 |
#define END_TWO_BYTE_PACK() |
| 524 |
#endif |
| 525 |
|
| 526 |
BEGIN_TWO_BYTE_PACK() |
| 527 |
|
| 528 |
struct BasicBlockBits { |
| 529 |
unsigned short BlockAddressRefCount : 15; |
| 530 |
unsigned short InstrOrderValid : 1; |
| 531 |
}; |
| 532 |
END_TWO_BYTE_PACK() |
| 533 |
|
| 534 |
#undef BEGIN_TWO_BYTE_PACK |
| 535 |
#undef END_TWO_BYTE_PACK |
| 536 |
|
| 537 |
|
| 538 |
BasicBlockBits getBasicBlockBits() const { |
| 539 |
static_assert(sizeof(BasicBlockBits) == sizeof(unsigned short), |
| 540 |
"too many bits for Value::SubclassData"); |
| 541 |
unsigned short ValueData = getSubclassDataFromValue(); |
| 542 |
BasicBlockBits AsBits; |
| 543 |
memcpy(&AsBits, &ValueData, sizeof(AsBits)); |
| 544 |
return AsBits; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
void setBasicBlockBits(BasicBlockBits AsBits) { |
| 549 |
unsigned short D; |
| 550 |
memcpy(&D, &AsBits, sizeof(D)); |
| 551 |
Value::setValueSubclassData(D); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
void AdjustBlockAddressRefCount(int Amt) { |
| 560 |
BasicBlockBits Bits = getBasicBlockBits(); |
| 561 |
Bits.BlockAddressRefCount += Amt; |
| 562 |
setBasicBlockBits(Bits); |
| 563 |
assert(Bits.BlockAddressRefCount < 255 && "Refcount wrap-around"); |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
void setValueSubclassData(unsigned short D) { |
| 569 |
Value::setValueSubclassData(D); |
| 570 |
} |
| 571 |
}; |
| 572 |
|
| 573 |
|
| 574 |
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(BasicBlock, LLVMBasicBlockRef) |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
BasicBlock::iterator skipDebugIntrinsics(BasicBlock::iterator It); |
| 579 |
|
| 580 |
#ifdef NDEBUG |
| 581 |
|
| 582 |
|
| 583 |
inline void BasicBlock::validateInstrOrdering() const {} |
| 584 |
#endif |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
#endif |