@@ -668,8 +668,34 @@ class NewGVN {
668
668
bool runGVN ();
669
669
670
670
private:
671
+ // / Helper struct return a Expression with an optional extra dependency.
672
+ struct ExprResult {
673
+ const Expression *Expr;
674
+ Value *ExtraDep;
675
+
676
+ ExprResult (const Expression *Expr, Value *ExtraDep = nullptr )
677
+ : Expr(Expr), ExtraDep(ExtraDep) {}
678
+ ExprResult (const ExprResult &) = delete ;
679
+ ExprResult (ExprResult &&Other)
680
+ : Expr(Other.Expr), ExtraDep(Other.ExtraDep) {
681
+ Other.Expr = nullptr ;
682
+ Other.ExtraDep = nullptr ;
683
+ }
684
+ ExprResult &operator =(const ExprResult &Other) = delete ;
685
+ ExprResult &operator =(ExprResult &&Other) = delete ;
686
+
687
+ ~ExprResult () { assert (!ExtraDep && " unhandled ExtraDep" ); }
688
+
689
+ operator bool () const { return Expr; }
690
+
691
+ static ExprResult none () { return {nullptr , nullptr }; }
692
+ static ExprResult some (const Expression *Expr, Value *ExtraDep = nullptr ) {
693
+ return {Expr, ExtraDep};
694
+ }
695
+ };
696
+
671
697
// Expression handling.
672
- const Expression * createExpression (Instruction *) const ;
698
+ ExprResult createExpression (Instruction *) const ;
673
699
const Expression *createBinaryExpression (unsigned , Type *, Value *, Value *,
674
700
Instruction *) const ;
675
701
@@ -742,10 +768,9 @@ class NewGVN {
742
768
void valueNumberInstruction (Instruction *);
743
769
744
770
// Symbolic evaluation.
745
- const Expression *checkSimplificationResults (Expression *, Instruction *,
746
- Value *) const ;
747
- const Expression *performSymbolicEvaluation (Value *,
748
- SmallPtrSetImpl<Value *> &) const ;
771
+ ExprResult checkExprResults (Expression *, Instruction *, Value *) const ;
772
+ ExprResult performSymbolicEvaluation (Value *,
773
+ SmallPtrSetImpl<Value *> &) const ;
749
774
const Expression *performSymbolicLoadCoercion (Type *, Value *, LoadInst *,
750
775
Instruction *,
751
776
MemoryAccess *) const ;
@@ -757,7 +782,7 @@ class NewGVN {
757
782
Instruction *I,
758
783
BasicBlock *PHIBlock) const ;
759
784
const Expression *performSymbolicAggrValueEvaluation (Instruction *) const ;
760
- const Expression * performSymbolicCmpEvaluation (Instruction *) const ;
785
+ ExprResult performSymbolicCmpEvaluation (Instruction *) const ;
761
786
const Expression *performSymbolicPredicateInfoEvaluation (Instruction *) const ;
762
787
763
788
// Congruence finding.
@@ -814,6 +839,7 @@ class NewGVN {
814
839
void addPredicateUsers (const PredicateBase *, Instruction *) const ;
815
840
void addMemoryUsers (const MemoryAccess *To, MemoryAccess *U) const ;
816
841
void addAdditionalUsers (Value *To, Value *User) const ;
842
+ void addAdditionalUsers (ExprResult &Res, Value *User) const ;
817
843
818
844
// Main loop of value numbering
819
845
void iterateTouchedInstructions ();
@@ -1052,19 +1078,21 @@ const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
1052
1078
E->op_push_back (lookupOperandLeader (Arg2));
1053
1079
1054
1080
Value *V = SimplifyBinOp (Opcode, E->getOperand (0 ), E->getOperand (1 ), SQ);
1055
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1056
- return SimplifiedE;
1081
+ if (auto Simplified = checkExprResults (E, I, V)) {
1082
+ addAdditionalUsers (Simplified, I);
1083
+ return Simplified.Expr ;
1084
+ }
1057
1085
return E;
1058
1086
}
1059
1087
1060
1088
// Take a Value returned by simplification of Expression E/Instruction
1061
1089
// I, and see if it resulted in a simpler expression. If so, return
1062
1090
// that expression.
1063
- const Expression *NewGVN::checkSimplificationResults (Expression *E,
1064
- Instruction *I,
1065
- Value *V) const {
1091
+ NewGVN::ExprResult NewGVN::checkExprResults (Expression *E, Instruction *I,
1092
+ Value *V) const {
1066
1093
if (!V)
1067
- return nullptr ;
1094
+ return ExprResult::none ();
1095
+
1068
1096
if (auto *C = dyn_cast<Constant>(V)) {
1069
1097
if (I)
1070
1098
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
@@ -1073,52 +1101,37 @@ const Expression *NewGVN::checkSimplificationResults(Expression *E,
1073
1101
assert (isa<BasicExpression>(E) &&
1074
1102
" We should always have had a basic expression here" );
1075
1103
deleteExpression (E);
1076
- return createConstantExpression (C);
1104
+ return ExprResult::some ( createConstantExpression (C) );
1077
1105
} else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
1078
1106
if (I)
1079
1107
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
1080
1108
<< " variable " << *V << " \n " );
1081
1109
deleteExpression (E);
1082
- return createVariableExpression (V);
1110
+ return ExprResult::some ( createVariableExpression (V) );
1083
1111
}
1084
1112
1085
1113
CongruenceClass *CC = ValueToClass.lookup (V);
1086
1114
if (CC) {
1087
1115
if (CC->getLeader () && CC->getLeader () != I) {
1088
- // If we simplified to something else, we need to communicate
1089
- // that we're users of the value we simplified to.
1090
- if (I != V) {
1091
- // Don't add temporary instructions to the user lists.
1092
- if (!AllTempInstructions.count (I))
1093
- addAdditionalUsers (V, I);
1094
- }
1095
- return createVariableOrConstant (CC->getLeader ());
1116
+ return ExprResult::some (createVariableOrConstant (CC->getLeader ()), V);
1096
1117
}
1097
1118
if (CC->getDefiningExpr ()) {
1098
- // If we simplified to something else, we need to communicate
1099
- // that we're users of the value we simplified to.
1100
- if (I != V) {
1101
- // Don't add temporary instructions to the user lists.
1102
- if (!AllTempInstructions.count (I))
1103
- addAdditionalUsers (V, I);
1104
- }
1105
-
1106
1119
if (I)
1107
1120
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
1108
1121
<< " expression " << *CC->getDefiningExpr () << " \n " );
1109
1122
NumGVNOpsSimplified++;
1110
1123
deleteExpression (E);
1111
- return CC->getDefiningExpr ();
1124
+ return ExprResult::some ( CC->getDefiningExpr (), V );
1112
1125
}
1113
1126
}
1114
1127
1115
- return nullptr ;
1128
+ return ExprResult::none () ;
1116
1129
}
1117
1130
1118
1131
// Create a value expression from the instruction I, replacing operands with
1119
1132
// their leaders.
1120
1133
1121
- const Expression * NewGVN::createExpression (Instruction *I) const {
1134
+ NewGVN::ExprResult NewGVN::createExpression (Instruction *I) const {
1122
1135
auto *E = new (ExpressionAllocator) BasicExpression (I->getNumOperands ());
1123
1136
1124
1137
bool AllConstant = setBasicExpressionInfo (I, E);
@@ -1149,33 +1162,33 @@ const Expression *NewGVN::createExpression(Instruction *I) const {
1149
1162
E->getOperand (1 )->getType () == I->getOperand (1 )->getType ()));
1150
1163
Value *V =
1151
1164
SimplifyCmpInst (Predicate, E->getOperand (0 ), E->getOperand (1 ), SQ);
1152
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1153
- return SimplifiedE ;
1165
+ if (auto Simplified = checkExprResults (E, I, V))
1166
+ return Simplified ;
1154
1167
} else if (isa<SelectInst>(I)) {
1155
1168
if (isa<Constant>(E->getOperand (0 )) ||
1156
1169
E->getOperand (1 ) == E->getOperand (2 )) {
1157
1170
assert (E->getOperand (1 )->getType () == I->getOperand (1 )->getType () &&
1158
1171
E->getOperand (2 )->getType () == I->getOperand (2 )->getType ());
1159
1172
Value *V = SimplifySelectInst (E->getOperand (0 ), E->getOperand (1 ),
1160
1173
E->getOperand (2 ), SQ);
1161
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1162
- return SimplifiedE ;
1174
+ if (auto Simplified = checkExprResults (E, I, V))
1175
+ return Simplified ;
1163
1176
}
1164
1177
} else if (I->isBinaryOp ()) {
1165
1178
Value *V =
1166
1179
SimplifyBinOp (E->getOpcode (), E->getOperand (0 ), E->getOperand (1 ), SQ);
1167
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1168
- return SimplifiedE ;
1180
+ if (auto Simplified = checkExprResults (E, I, V))
1181
+ return Simplified ;
1169
1182
} else if (auto *CI = dyn_cast<CastInst>(I)) {
1170
1183
Value *V =
1171
1184
SimplifyCastInst (CI->getOpcode (), E->getOperand (0 ), CI->getType (), SQ);
1172
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1173
- return SimplifiedE ;
1185
+ if (auto Simplified = checkExprResults (E, I, V))
1186
+ return Simplified ;
1174
1187
} else if (isa<GetElementPtrInst>(I)) {
1175
1188
Value *V = SimplifyGEPInst (
1176
1189
E->getType (), ArrayRef<Value *>(E->op_begin (), E->op_end ()), SQ);
1177
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1178
- return SimplifiedE ;
1190
+ if (auto Simplified = checkExprResults (E, I, V))
1191
+ return Simplified ;
1179
1192
} else if (AllConstant) {
1180
1193
// We don't bother trying to simplify unless all of the operands
1181
1194
// were constant.
@@ -1189,10 +1202,10 @@ const Expression *NewGVN::createExpression(Instruction *I) const {
1189
1202
C.emplace_back (cast<Constant>(Arg));
1190
1203
1191
1204
if (Value *V = ConstantFoldInstOperands (I, C, DL, TLI))
1192
- if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1193
- return SimplifiedE ;
1205
+ if (auto Simplified = checkExprResults (E, I, V))
1206
+ return Simplified ;
1194
1207
}
1195
- return E ;
1208
+ return ExprResult::some (E) ;
1196
1209
}
1197
1210
1198
1211
const AggregateValueExpression *
@@ -1778,7 +1791,7 @@ NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) const {
1778
1791
return createAggregateValueExpression (I);
1779
1792
}
1780
1793
1781
- const Expression * NewGVN::performSymbolicCmpEvaluation (Instruction *I) const {
1794
+ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation (Instruction *I) const {
1782
1795
assert (isa<CmpInst>(I) && " Expected a cmp instruction." );
1783
1796
1784
1797
auto *CI = cast<CmpInst>(I);
@@ -1798,14 +1811,17 @@ const Expression *NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1798
1811
// of an assume.
1799
1812
auto *CmpPI = PredInfo->getPredicateInfoFor (I);
1800
1813
if (dyn_cast_or_null<PredicateAssume>(CmpPI))
1801
- return createConstantExpression (ConstantInt::getTrue (CI->getType ()));
1814
+ return ExprResult::some (
1815
+ createConstantExpression (ConstantInt::getTrue (CI->getType ())));
1802
1816
1803
1817
if (Op0 == Op1) {
1804
1818
// This condition does not depend on predicates, no need to add users
1805
1819
if (CI->isTrueWhenEqual ())
1806
- return createConstantExpression (ConstantInt::getTrue (CI->getType ()));
1820
+ return ExprResult::some (
1821
+ createConstantExpression (ConstantInt::getTrue (CI->getType ())));
1807
1822
else if (CI->isFalseWhenEqual ())
1808
- return createConstantExpression (ConstantInt::getFalse (CI->getType ()));
1823
+ return ExprResult::some (
1824
+ createConstantExpression (ConstantInt::getFalse (CI->getType ())));
1809
1825
}
1810
1826
1811
1827
// NOTE: Because we are comparing both operands here and below, and using
@@ -1865,30 +1881,30 @@ const Expression *NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1865
1881
if (CmpInst::isImpliedTrueByMatchingCmp (BranchPredicate,
1866
1882
OurPredicate)) {
1867
1883
addPredicateUsers (PI, I);
1868
- return createConstantExpression (
1869
- ConstantInt::getTrue (CI->getType ()));
1884
+ return ExprResult::some (
1885
+ createConstantExpression ( ConstantInt::getTrue (CI->getType () )));
1870
1886
}
1871
1887
1872
1888
if (CmpInst::isImpliedFalseByMatchingCmp (BranchPredicate,
1873
1889
OurPredicate)) {
1874
1890
addPredicateUsers (PI, I);
1875
- return createConstantExpression (
1876
- ConstantInt::getFalse (CI->getType ()));
1891
+ return ExprResult::some (
1892
+ createConstantExpression ( ConstantInt::getFalse (CI->getType () )));
1877
1893
}
1878
1894
} else {
1879
1895
// Just handle the ne and eq cases, where if we have the same
1880
1896
// operands, we may know something.
1881
1897
if (BranchPredicate == OurPredicate) {
1882
1898
addPredicateUsers (PI, I);
1883
1899
// Same predicate, same ops,we know it was false, so this is false.
1884
- return createConstantExpression (
1885
- ConstantInt::getFalse (CI->getType ()));
1900
+ return ExprResult::some (
1901
+ createConstantExpression ( ConstantInt::getFalse (CI->getType () )));
1886
1902
} else if (BranchPredicate ==
1887
1903
CmpInst::getInversePredicate (OurPredicate)) {
1888
1904
addPredicateUsers (PI, I);
1889
1905
// Inverse predicate, we know the other was false, so this is true.
1890
- return createConstantExpression (
1891
- ConstantInt::getTrue (CI->getType ()));
1906
+ return ExprResult::some (
1907
+ createConstantExpression ( ConstantInt::getTrue (CI->getType () )));
1892
1908
}
1893
1909
}
1894
1910
}
@@ -1899,9 +1915,10 @@ const Expression *NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1899
1915
}
1900
1916
1901
1917
// Substitute and symbolize the value before value numbering.
1902
- const Expression *
1918
+ NewGVN::ExprResult
1903
1919
NewGVN::performSymbolicEvaluation (Value *V,
1904
1920
SmallPtrSetImpl<Value *> &Visited) const {
1921
+
1905
1922
const Expression *E = nullptr ;
1906
1923
if (auto *C = dyn_cast<Constant>(V))
1907
1924
E = createConstantExpression (C);
@@ -1937,11 +1954,11 @@ NewGVN::performSymbolicEvaluation(Value *V,
1937
1954
break ;
1938
1955
case Instruction::BitCast:
1939
1956
case Instruction::AddrSpaceCast:
1940
- E = createExpression (I);
1957
+ return createExpression (I);
1941
1958
break ;
1942
1959
case Instruction::ICmp:
1943
1960
case Instruction::FCmp:
1944
- E = performSymbolicCmpEvaluation (I);
1961
+ return performSymbolicCmpEvaluation (I);
1945
1962
break ;
1946
1963
case Instruction::FNeg:
1947
1964
case Instruction::Add:
@@ -1977,16 +1994,16 @@ NewGVN::performSymbolicEvaluation(Value *V,
1977
1994
case Instruction::ExtractElement:
1978
1995
case Instruction::InsertElement:
1979
1996
case Instruction::GetElementPtr:
1980
- E = createExpression (I);
1997
+ return createExpression (I);
1981
1998
break ;
1982
1999
case Instruction::ShuffleVector:
1983
2000
// FIXME: Add support for shufflevector to createExpression.
1984
- return nullptr ;
2001
+ return ExprResult::none () ;
1985
2002
default :
1986
- return nullptr ;
2003
+ return ExprResult::none () ;
1987
2004
}
1988
2005
}
1989
- return E ;
2006
+ return ExprResult::some (E) ;
1990
2007
}
1991
2008
1992
2009
// Look up a container of values/instructions in a map, and touch all the
@@ -2007,6 +2024,12 @@ void NewGVN::addAdditionalUsers(Value *To, Value *User) const {
2007
2024
AdditionalUsers[To].insert (User);
2008
2025
}
2009
2026
2027
+ void NewGVN::addAdditionalUsers (ExprResult &Res, Value *User) const {
2028
+ if (Res.ExtraDep && Res.ExtraDep != User)
2029
+ addAdditionalUsers (Res.ExtraDep , User);
2030
+ Res.ExtraDep = nullptr ;
2031
+ }
2032
+
2010
2033
void NewGVN::markUsersTouched (Value *V) {
2011
2034
// Now mark the users as touched.
2012
2035
for (auto *User : V->users ()) {
@@ -2414,9 +2437,14 @@ void NewGVN::processOutgoingEdges(Instruction *TI, BasicBlock *B) {
2414
2437
Value *CondEvaluated = findConditionEquivalence (Cond);
2415
2438
if (!CondEvaluated) {
2416
2439
if (auto *I = dyn_cast<Instruction>(Cond)) {
2417
- const Expression *E = createExpression (I);
2418
- if (const auto *CE = dyn_cast<ConstantExpression>(E )) {
2440
+ auto Res = createExpression (I);
2441
+ if (const auto *CE = dyn_cast<ConstantExpression>(Res. Expr )) {
2419
2442
CondEvaluated = CE->getConstantValue ();
2443
+ addAdditionalUsers (Res, I);
2444
+ } else {
2445
+ // Did not use simplification result, no need to add the extra
2446
+ // dependency.
2447
+ Res.ExtraDep = nullptr ;
2420
2448
}
2421
2449
} else if (isa<ConstantInt>(Cond)) {
2422
2450
CondEvaluated = Cond;
@@ -2600,7 +2628,9 @@ Value *NewGVN::findLeaderForInst(Instruction *TransInst,
2600
2628
TempToBlock.insert ({TransInst, PredBB});
2601
2629
InstrDFS.insert ({TransInst, IDFSNum});
2602
2630
2603
- const Expression *E = performSymbolicEvaluation (TransInst, Visited);
2631
+ auto Res = performSymbolicEvaluation (TransInst, Visited);
2632
+ const Expression *E = Res.Expr ;
2633
+ addAdditionalUsers (Res, OrigInst);
2604
2634
InstrDFS.erase (TransInst);
2605
2635
AllTempInstructions.erase (TransInst);
2606
2636
TempToBlock.erase (TransInst);
@@ -3027,7 +3057,10 @@ void NewGVN::valueNumberInstruction(Instruction *I) {
3027
3057
const Expression *Symbolized = nullptr ;
3028
3058
SmallPtrSet<Value *, 2 > Visited;
3029
3059
if (DebugCounter::shouldExecute (VNCounter)) {
3030
- Symbolized = performSymbolicEvaluation (I, Visited);
3060
+ auto Res = performSymbolicEvaluation (I, Visited);
3061
+ Symbolized = Res.Expr ;
3062
+ addAdditionalUsers (Res, I);
3063
+
3031
3064
// Make a phi of ops if necessary
3032
3065
if (Symbolized && !isa<ConstantExpression>(Symbolized) &&
3033
3066
!isa<VariableExpression>(Symbolized) && PHINodeUses.count (I)) {
0 commit comments