|
21 | 21 | #include "llvm/ADT/SmallVector.h"
|
22 | 22 | #include "llvm/IR/Dominators.h"
|
23 | 23 | #include "llvm/IR/InstrTypes.h"
|
| 24 | +#include "llvm/IR/IntrinsicInst.h" |
24 | 25 | #include "llvm/IR/PassManager.h"
|
25 | 26 | #include "llvm/IR/ValueHandle.h"
|
26 | 27 | #include "llvm/Support/Allocator.h"
|
@@ -432,4 +433,141 @@ struct llvm::GVNPass::Expression {
|
432 | 433 | }
|
433 | 434 | };
|
434 | 435 |
|
| 436 | + |
| 437 | +/// Represents a particular available value that we know how to materialize. |
| 438 | +/// Materialization of an AvailableValue never fails. An AvailableValue is |
| 439 | +/// implicitly associated with a rematerialization point which is the |
| 440 | +/// location of the instruction from which it was formed. |
| 441 | +struct llvm::gvn::AvailableValue { |
| 442 | + enum class ValType { |
| 443 | + SimpleVal, // A simple offsetted value that is accessed. |
| 444 | + LoadVal, // A value produced by a load. |
| 445 | + MemIntrin, // A memory intrinsic which is loaded from. |
| 446 | + UndefVal, // A UndefValue representing a value from dead block (which |
| 447 | + // is not yet physically removed from the CFG). |
| 448 | + SelectVal, // A pointer select which is loaded from and for which the load |
| 449 | + // can be replace by a value select. |
| 450 | + }; |
| 451 | + |
| 452 | + /// Val - The value that is live out of the block. |
| 453 | + Value *Val; |
| 454 | + /// Kind of the live-out value. |
| 455 | + ValType Kind; |
| 456 | + |
| 457 | + /// Offset - The byte offset in Val that is interesting for the load query. |
| 458 | + unsigned Offset = 0; |
| 459 | + /// V1, V2 - The dominating non-clobbered values of SelectVal. |
| 460 | + Value *V1 = nullptr, *V2 = nullptr; |
| 461 | + |
| 462 | + static AvailableValue get(Value *V, unsigned Offset = 0) { |
| 463 | + AvailableValue Res; |
| 464 | + Res.Val = V; |
| 465 | + Res.Kind = ValType::SimpleVal; |
| 466 | + Res.Offset = Offset; |
| 467 | + return Res; |
| 468 | + } |
| 469 | + |
| 470 | + static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) { |
| 471 | + AvailableValue Res; |
| 472 | + Res.Val = MI; |
| 473 | + Res.Kind = ValType::MemIntrin; |
| 474 | + Res.Offset = Offset; |
| 475 | + return Res; |
| 476 | + } |
| 477 | + |
| 478 | + static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) { |
| 479 | + AvailableValue Res; |
| 480 | + Res.Val = Load; |
| 481 | + Res.Kind = ValType::LoadVal; |
| 482 | + Res.Offset = Offset; |
| 483 | + return Res; |
| 484 | + } |
| 485 | + |
| 486 | + static AvailableValue getUndef() { |
| 487 | + AvailableValue Res; |
| 488 | + Res.Val = nullptr; |
| 489 | + Res.Kind = ValType::UndefVal; |
| 490 | + Res.Offset = 0; |
| 491 | + return Res; |
| 492 | + } |
| 493 | + |
| 494 | + static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2) { |
| 495 | + AvailableValue Res; |
| 496 | + Res.Val = Sel; |
| 497 | + Res.Kind = ValType::SelectVal; |
| 498 | + Res.Offset = 0; |
| 499 | + Res.V1 = V1; |
| 500 | + Res.V2 = V2; |
| 501 | + return Res; |
| 502 | + } |
| 503 | + |
| 504 | + bool isSimpleValue() const { return Kind == ValType::SimpleVal; } |
| 505 | + bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; } |
| 506 | + bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; } |
| 507 | + bool isUndefValue() const { return Kind == ValType::UndefVal; } |
| 508 | + bool isSelectValue() const { return Kind == ValType::SelectVal; } |
| 509 | + |
| 510 | + Value *getSimpleValue() const { |
| 511 | + assert(isSimpleValue() && "Wrong accessor"); |
| 512 | + return Val; |
| 513 | + } |
| 514 | + |
| 515 | + LoadInst *getCoercedLoadValue() const { |
| 516 | + assert(isCoercedLoadValue() && "Wrong accessor"); |
| 517 | + return cast<LoadInst>(Val); |
| 518 | + } |
| 519 | + |
| 520 | + MemIntrinsic *getMemIntrinValue() const { |
| 521 | + assert(isMemIntrinValue() && "Wrong accessor"); |
| 522 | + return cast<MemIntrinsic>(Val); |
| 523 | + } |
| 524 | + |
| 525 | + SelectInst *getSelectValue() const { |
| 526 | + assert(isSelectValue() && "Wrong accessor"); |
| 527 | + return cast<SelectInst>(Val); |
| 528 | + } |
| 529 | + |
| 530 | + /// Emit code at the specified insertion point to adjust the value defined |
| 531 | + /// here to the specified type. This handles various coercion cases. |
| 532 | + Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt, |
| 533 | + GVNPass &gvn) const; |
| 534 | +}; |
| 535 | + |
| 536 | +/// Represents an AvailableValue which can be rematerialized at the end of |
| 537 | +/// the associated BasicBlock. |
| 538 | +struct llvm::gvn::AvailableValueInBlock { |
| 539 | + /// BB - The basic block in question. |
| 540 | + BasicBlock *BB = nullptr; |
| 541 | + |
| 542 | + /// AV - The actual available value |
| 543 | + AvailableValue AV; |
| 544 | + |
| 545 | + static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) { |
| 546 | + AvailableValueInBlock Res; |
| 547 | + Res.BB = BB; |
| 548 | + Res.AV = std::move(AV); |
| 549 | + return Res; |
| 550 | + } |
| 551 | + |
| 552 | + static AvailableValueInBlock get(BasicBlock *BB, Value *V, |
| 553 | + unsigned Offset = 0) { |
| 554 | + return get(BB, AvailableValue::get(V, Offset)); |
| 555 | + } |
| 556 | + |
| 557 | + static AvailableValueInBlock getUndef(BasicBlock *BB) { |
| 558 | + return get(BB, AvailableValue::getUndef()); |
| 559 | + } |
| 560 | + |
| 561 | + static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel, |
| 562 | + Value *V1, Value *V2) { |
| 563 | + return get(BB, AvailableValue::getSelect(Sel, V1, V2)); |
| 564 | + } |
| 565 | + |
| 566 | + /// Emit code at the end of this block to adjust the value defined here to |
| 567 | + /// the specified type. This handles various coercion cases. |
| 568 | + Value *MaterializeAdjustedValue(LoadInst *Load, GVNPass &gvn) const { |
| 569 | + return AV.MaterializeAdjustedValue(Load, BB->getTerminator(), gvn); |
| 570 | + } |
| 571 | +}; |
| 572 | + |
435 | 573 | #endif // LLVM_TRANSFORMS_SCALAR_GVN_H
|
0 commit comments