77
77
#include " llvm/Analysis/MemoryBuiltins.h"
78
78
#include " llvm/Analysis/MemorySSA.h"
79
79
#include " llvm/Analysis/TargetLibraryInfo.h"
80
+ #include " llvm/Analysis/ValueTracking.h"
80
81
#include " llvm/IR/Argument.h"
81
82
#include " llvm/IR/BasicBlock.h"
82
83
#include " llvm/IR/Constant.h"
@@ -1736,18 +1737,18 @@ NewGVN::performSymbolicPHIEvaluation(ArrayRef<ValPair> PHIOps,
1736
1737
if (Filtered.empty ()) {
1737
1738
// If it has undef or poison at this point, it means there are no-non-undef
1738
1739
// arguments, and thus, the value of the phi node must be undef.
1739
- if (HasPoison && !HasUndef) {
1740
- LLVM_DEBUG (
1741
- dbgs () << " PHI Node " << *I
1742
- << " has no non-poison arguments, valuing it as poison\n " );
1743
- return createConstantExpression (PoisonValue::get (I->getType ()));
1744
- }
1745
1740
if (HasUndef) {
1746
1741
LLVM_DEBUG (
1747
1742
dbgs () << " PHI Node " << *I
1748
1743
<< " has no non-undef arguments, valuing it as undef\n " );
1749
1744
return createConstantExpression (UndefValue::get (I->getType ()));
1750
1745
}
1746
+ if (HasPoison) {
1747
+ LLVM_DEBUG (
1748
+ dbgs () << " PHI Node " << *I
1749
+ << " has no non-poison arguments, valuing it as poison\n " );
1750
+ return createConstantExpression (PoisonValue::get (I->getType ()));
1751
+ }
1751
1752
1752
1753
LLVM_DEBUG (dbgs () << " No arguments of PHI node " << *I << " are live\n " );
1753
1754
deleteExpression (E);
@@ -1757,15 +1758,20 @@ NewGVN::performSymbolicPHIEvaluation(ArrayRef<ValPair> PHIOps,
1757
1758
++Filtered.begin ();
1758
1759
// Can't use std::equal here, sadly, because filter.begin moves.
1759
1760
if (llvm::all_of (Filtered, [&](Value *Arg) { return Arg == AllSameValue; })) {
1761
+ // Can't fold phi(undef, X) -> X unless X can't be poison (thus X is undef
1762
+ // in the worst case).
1763
+ if (HasUndef && !isGuaranteedNotToBePoison (AllSameValue, AC, nullptr , DT))
1764
+ return E;
1765
+
1760
1766
// In LLVM's non-standard representation of phi nodes, it's possible to have
1761
1767
// phi nodes with cycles (IE dependent on other phis that are .... dependent
1762
1768
// on the original phi node), especially in weird CFG's where some arguments
1763
1769
// are unreachable, or uninitialized along certain paths. This can cause
1764
1770
// infinite loops during evaluation. We work around this by not trying to
1765
1771
// really evaluate them independently, but instead using a variable
1766
1772
// expression to say if one is equivalent to the other.
1767
- // We also special case undef, so that if we have an undef, we can't use the
1768
- // common value unless it dominates the phi block.
1773
+ // We also special case undef/poison , so that if we have an undef, we can't
1774
+ // use the common value unless it dominates the phi block.
1769
1775
if (HasPoison || HasUndef) {
1770
1776
// If we have undef and at least one other value, this is really a
1771
1777
// multivalued phi, and we need to know if it's cycle free in order to
@@ -2853,14 +2859,14 @@ NewGVN::makePossiblePHIOfOps(Instruction *I,
2853
2859
}
2854
2860
2855
2861
// The algorithm initially places the values of the routine in the TOP
2856
- // congruence class. The leader of TOP is the undetermined value `undef `.
2862
+ // congruence class. The leader of TOP is the undetermined value `poison `.
2857
2863
// When the algorithm has finished, values still in TOP are unreachable.
2858
2864
void NewGVN::initializeCongruenceClasses (Function &F) {
2859
2865
NextCongruenceNum = 0 ;
2860
2866
2861
2867
// Note that even though we use the live on entry def as a representative
2862
2868
// MemoryAccess, it is *not* the same as the actual live on entry def. We
2863
- // have no real equivalemnt to undef for MemoryAccesses, and so we really
2869
+ // have no real equivalent to poison for MemoryAccesses, and so we really
2864
2870
// should be checking whether the MemoryAccess is top if we want to know if it
2865
2871
// is equivalent to everything. Otherwise, what this really signifies is that
2866
2872
// the access "it reaches all the way back to the beginning of the function"
@@ -3031,7 +3037,7 @@ void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
3031
3037
!isMemoryAccessTOP (cast<MemoryAccess>(U)) &&
3032
3038
ReachableEdges.count ({MP->getIncomingBlock (U), PHIBlock});
3033
3039
});
3034
- // If all that is left is nothing, our memoryphi is undef . We keep it as
3040
+ // If all that is left is nothing, our memoryphi is poison . We keep it as
3035
3041
// InitialClass. Note: The only case this should happen is if we have at
3036
3042
// least one self-argument.
3037
3043
if (Filtered.begin () == Filtered.end ()) {
0 commit comments