@@ -668,23 +668,8 @@ 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 () { assert (!ExtraDep && " unhandled ExtraDep" ); }
677
-
678
- operator bool () const { return Expr; }
679
-
680
- static ExprResult none () { return {nullptr , nullptr }; }
681
- static ExprResult some (const Expression *Expr, Value *ExtraDep = nullptr ) {
682
- return {Expr, ExtraDep};
683
- }
684
- };
685
-
686
671
// Expression handling.
687
- ExprResult createExpression (Instruction *) const ;
672
+ const Expression * createExpression (Instruction *) const ;
688
673
const Expression *createBinaryExpression (unsigned , Type *, Value *, Value *,
689
674
Instruction *) const ;
690
675
@@ -757,9 +742,10 @@ class NewGVN {
757
742
void valueNumberInstruction (Instruction *);
758
743
759
744
// Symbolic evaluation.
760
- ExprResult checkExprResults (Expression *, Instruction *, Value *) const ;
761
- ExprResult performSymbolicEvaluation (Value *,
762
- SmallPtrSetImpl<Value *> &) const ;
745
+ const Expression *checkSimplificationResults (Expression *, Instruction *,
746
+ Value *) const ;
747
+ const Expression *performSymbolicEvaluation (Value *,
748
+ SmallPtrSetImpl<Value *> &) const ;
763
749
const Expression *performSymbolicLoadCoercion (Type *, Value *, LoadInst *,
764
750
Instruction *,
765
751
MemoryAccess *) const ;
@@ -771,7 +757,7 @@ class NewGVN {
771
757
Instruction *I,
772
758
BasicBlock *PHIBlock) const ;
773
759
const Expression *performSymbolicAggrValueEvaluation (Instruction *) const ;
774
- ExprResult performSymbolicCmpEvaluation (Instruction *) const ;
760
+ const Expression * performSymbolicCmpEvaluation (Instruction *) const ;
775
761
const Expression *performSymbolicPredicateInfoEvaluation (Instruction *) const ;
776
762
777
763
// Congruence finding.
@@ -828,7 +814,6 @@ class NewGVN {
828
814
void addPredicateUsers (const PredicateBase *, Instruction *) const ;
829
815
void addMemoryUsers (const MemoryAccess *To, MemoryAccess *U) const ;
830
816
void addAdditionalUsers (Value *To, Value *User) const ;
831
- void addAdditionalUsers (ExprResult &Res, Value *User) const ;
832
817
833
818
// Main loop of value numbering
834
819
void iterateTouchedInstructions ();
@@ -1067,21 +1052,19 @@ const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
1067
1052
E->op_push_back (lookupOperandLeader (Arg2));
1068
1053
1069
1054
Value *V = SimplifyBinOp (Opcode, E->getOperand (0 ), E->getOperand (1 ), SQ);
1070
- if (auto Simplified = checkExprResults (E, I, V)) {
1071
- addAdditionalUsers (Simplified, I);
1072
- return Simplified.Expr ;
1073
- }
1055
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1056
+ return SimplifiedE;
1074
1057
return E;
1075
1058
}
1076
1059
1077
1060
// Take a Value returned by simplification of Expression E/Instruction
1078
1061
// I, and see if it resulted in a simpler expression. If so, return
1079
1062
// that expression.
1080
- NewGVN::ExprResult NewGVN::checkExprResults (Expression *E, Instruction *I,
1081
- Value *V) const {
1063
+ const Expression *NewGVN::checkSimplificationResults (Expression *E,
1064
+ Instruction *I,
1065
+ Value *V) const {
1082
1066
if (!V)
1083
- return ExprResult::none ();
1084
-
1067
+ return nullptr ;
1085
1068
if (auto *C = dyn_cast<Constant>(V)) {
1086
1069
if (I)
1087
1070
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
@@ -1090,37 +1073,52 @@ NewGVN::ExprResult NewGVN::checkExprResults(Expression *E, Instruction *I,
1090
1073
assert (isa<BasicExpression>(E) &&
1091
1074
" We should always have had a basic expression here" );
1092
1075
deleteExpression (E);
1093
- return ExprResult::some ( createConstantExpression (C) );
1076
+ return createConstantExpression (C);
1094
1077
} else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
1095
1078
if (I)
1096
1079
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
1097
1080
<< " variable " << *V << " \n " );
1098
1081
deleteExpression (E);
1099
- return ExprResult::some ( createVariableExpression (V) );
1082
+ return createVariableExpression (V);
1100
1083
}
1101
1084
1102
1085
CongruenceClass *CC = ValueToClass.lookup (V);
1103
1086
if (CC) {
1104
1087
if (CC->getLeader () && CC->getLeader () != I) {
1105
- return ExprResult::some (createVariableOrConstant (CC->getLeader ()), V);
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 ());
1106
1096
}
1107
1097
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
+
1108
1106
if (I)
1109
1107
LLVM_DEBUG (dbgs () << " Simplified " << *I << " to "
1110
1108
<< " expression " << *CC->getDefiningExpr () << " \n " );
1111
1109
NumGVNOpsSimplified++;
1112
1110
deleteExpression (E);
1113
- return ExprResult::some ( CC->getDefiningExpr (), V );
1111
+ return CC->getDefiningExpr ();
1114
1112
}
1115
1113
}
1116
1114
1117
- return ExprResult::none () ;
1115
+ return nullptr ;
1118
1116
}
1119
1117
1120
1118
// Create a value expression from the instruction I, replacing operands with
1121
1119
// their leaders.
1122
1120
1123
- NewGVN::ExprResult NewGVN::createExpression (Instruction *I) const {
1121
+ const Expression * NewGVN::createExpression (Instruction *I) const {
1124
1122
auto *E = new (ExpressionAllocator) BasicExpression (I->getNumOperands ());
1125
1123
1126
1124
bool AllConstant = setBasicExpressionInfo (I, E);
@@ -1151,33 +1149,33 @@ NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const {
1151
1149
E->getOperand (1 )->getType () == I->getOperand (1 )->getType ()));
1152
1150
Value *V =
1153
1151
SimplifyCmpInst (Predicate, E->getOperand (0 ), E->getOperand (1 ), SQ);
1154
- if (auto Simplified = checkExprResults (E, I, V))
1155
- return Simplified ;
1152
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1153
+ return SimplifiedE ;
1156
1154
} else if (isa<SelectInst>(I)) {
1157
1155
if (isa<Constant>(E->getOperand (0 )) ||
1158
1156
E->getOperand (1 ) == E->getOperand (2 )) {
1159
1157
assert (E->getOperand (1 )->getType () == I->getOperand (1 )->getType () &&
1160
1158
E->getOperand (2 )->getType () == I->getOperand (2 )->getType ());
1161
1159
Value *V = SimplifySelectInst (E->getOperand (0 ), E->getOperand (1 ),
1162
1160
E->getOperand (2 ), SQ);
1163
- if (auto Simplified = checkExprResults (E, I, V))
1164
- return Simplified ;
1161
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1162
+ return SimplifiedE ;
1165
1163
}
1166
1164
} else if (I->isBinaryOp ()) {
1167
1165
Value *V =
1168
1166
SimplifyBinOp (E->getOpcode (), E->getOperand (0 ), E->getOperand (1 ), SQ);
1169
- if (auto Simplified = checkExprResults (E, I, V))
1170
- return Simplified ;
1167
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1168
+ return SimplifiedE ;
1171
1169
} else if (auto *CI = dyn_cast<CastInst>(I)) {
1172
1170
Value *V =
1173
1171
SimplifyCastInst (CI->getOpcode (), E->getOperand (0 ), CI->getType (), SQ);
1174
- if (auto Simplified = checkExprResults (E, I, V))
1175
- return Simplified ;
1172
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1173
+ return SimplifiedE ;
1176
1174
} else if (isa<GetElementPtrInst>(I)) {
1177
1175
Value *V = SimplifyGEPInst (
1178
1176
E->getType (), ArrayRef<Value *>(E->op_begin (), E->op_end ()), SQ);
1179
- if (auto Simplified = checkExprResults (E, I, V))
1180
- return Simplified ;
1177
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1178
+ return SimplifiedE ;
1181
1179
} else if (AllConstant) {
1182
1180
// We don't bother trying to simplify unless all of the operands
1183
1181
// were constant.
@@ -1191,10 +1189,10 @@ NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const {
1191
1189
C.emplace_back (cast<Constant>(Arg));
1192
1190
1193
1191
if (Value *V = ConstantFoldInstOperands (I, C, DL, TLI))
1194
- if (auto Simplified = checkExprResults (E, I, V))
1195
- return Simplified ;
1192
+ if (const Expression *SimplifiedE = checkSimplificationResults (E, I, V))
1193
+ return SimplifiedE ;
1196
1194
}
1197
- return ExprResult::some (E) ;
1195
+ return E ;
1198
1196
}
1199
1197
1200
1198
const AggregateValueExpression *
@@ -1780,7 +1778,7 @@ NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) const {
1780
1778
return createAggregateValueExpression (I);
1781
1779
}
1782
1780
1783
- NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation (Instruction *I) const {
1781
+ const Expression * NewGVN::performSymbolicCmpEvaluation (Instruction *I) const {
1784
1782
assert (isa<CmpInst>(I) && " Expected a cmp instruction." );
1785
1783
1786
1784
auto *CI = cast<CmpInst>(I);
@@ -1800,17 +1798,14 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1800
1798
// of an assume.
1801
1799
auto *CmpPI = PredInfo->getPredicateInfoFor (I);
1802
1800
if (dyn_cast_or_null<PredicateAssume>(CmpPI))
1803
- return ExprResult::some (
1804
- createConstantExpression (ConstantInt::getTrue (CI->getType ())));
1801
+ return createConstantExpression (ConstantInt::getTrue (CI->getType ()));
1805
1802
1806
1803
if (Op0 == Op1) {
1807
1804
// This condition does not depend on predicates, no need to add users
1808
1805
if (CI->isTrueWhenEqual ())
1809
- return ExprResult::some (
1810
- createConstantExpression (ConstantInt::getTrue (CI->getType ())));
1806
+ return createConstantExpression (ConstantInt::getTrue (CI->getType ()));
1811
1807
else if (CI->isFalseWhenEqual ())
1812
- return ExprResult::some (
1813
- createConstantExpression (ConstantInt::getFalse (CI->getType ())));
1808
+ return createConstantExpression (ConstantInt::getFalse (CI->getType ()));
1814
1809
}
1815
1810
1816
1811
// NOTE: Because we are comparing both operands here and below, and using
@@ -1870,30 +1865,30 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1870
1865
if (CmpInst::isImpliedTrueByMatchingCmp (BranchPredicate,
1871
1866
OurPredicate)) {
1872
1867
addPredicateUsers (PI, I);
1873
- return ExprResult::some (
1874
- createConstantExpression ( ConstantInt::getTrue (CI->getType () )));
1868
+ return createConstantExpression (
1869
+ ConstantInt::getTrue (CI->getType ()));
1875
1870
}
1876
1871
1877
1872
if (CmpInst::isImpliedFalseByMatchingCmp (BranchPredicate,
1878
1873
OurPredicate)) {
1879
1874
addPredicateUsers (PI, I);
1880
- return ExprResult::some (
1881
- createConstantExpression ( ConstantInt::getFalse (CI->getType () )));
1875
+ return createConstantExpression (
1876
+ ConstantInt::getFalse (CI->getType ()));
1882
1877
}
1883
1878
} else {
1884
1879
// Just handle the ne and eq cases, where if we have the same
1885
1880
// operands, we may know something.
1886
1881
if (BranchPredicate == OurPredicate) {
1887
1882
addPredicateUsers (PI, I);
1888
1883
// Same predicate, same ops,we know it was false, so this is false.
1889
- return ExprResult::some (
1890
- createConstantExpression ( ConstantInt::getFalse (CI->getType () )));
1884
+ return createConstantExpression (
1885
+ ConstantInt::getFalse (CI->getType ()));
1891
1886
} else if (BranchPredicate ==
1892
1887
CmpInst::getInversePredicate (OurPredicate)) {
1893
1888
addPredicateUsers (PI, I);
1894
1889
// Inverse predicate, we know the other was false, so this is true.
1895
- return ExprResult::some (
1896
- createConstantExpression ( ConstantInt::getTrue (CI->getType () )));
1890
+ return createConstantExpression (
1891
+ ConstantInt::getTrue (CI->getType ()));
1897
1892
}
1898
1893
}
1899
1894
}
@@ -1904,10 +1899,9 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1904
1899
}
1905
1900
1906
1901
// Substitute and symbolize the value before value numbering.
1907
- NewGVN::ExprResult
1902
+ const Expression *
1908
1903
NewGVN::performSymbolicEvaluation (Value *V,
1909
1904
SmallPtrSetImpl<Value *> &Visited) const {
1910
-
1911
1905
const Expression *E = nullptr ;
1912
1906
if (auto *C = dyn_cast<Constant>(V))
1913
1907
E = createConstantExpression (C);
@@ -1943,11 +1937,11 @@ NewGVN::performSymbolicEvaluation(Value *V,
1943
1937
break ;
1944
1938
case Instruction::BitCast:
1945
1939
case Instruction::AddrSpaceCast:
1946
- return createExpression (I);
1940
+ E = createExpression (I);
1947
1941
break ;
1948
1942
case Instruction::ICmp:
1949
1943
case Instruction::FCmp:
1950
- return performSymbolicCmpEvaluation (I);
1944
+ E = performSymbolicCmpEvaluation (I);
1951
1945
break ;
1952
1946
case Instruction::FNeg:
1953
1947
case Instruction::Add:
@@ -1983,16 +1977,16 @@ NewGVN::performSymbolicEvaluation(Value *V,
1983
1977
case Instruction::ExtractElement:
1984
1978
case Instruction::InsertElement:
1985
1979
case Instruction::GetElementPtr:
1986
- return createExpression (I);
1980
+ E = createExpression (I);
1987
1981
break ;
1988
1982
case Instruction::ShuffleVector:
1989
1983
// FIXME: Add support for shufflevector to createExpression.
1990
- return ExprResult::none () ;
1984
+ return nullptr ;
1991
1985
default :
1992
- return ExprResult::none () ;
1986
+ return nullptr ;
1993
1987
}
1994
1988
}
1995
- return ExprResult::some (E) ;
1989
+ return E ;
1996
1990
}
1997
1991
1998
1992
// Look up a container of values/instructions in a map, and touch all the
@@ -2013,12 +2007,6 @@ void NewGVN::addAdditionalUsers(Value *To, Value *User) const {
2013
2007
AdditionalUsers[To].insert (User);
2014
2008
}
2015
2009
2016
- void NewGVN::addAdditionalUsers (ExprResult &Res, Value *User) const {
2017
- if (Res.ExtraDep && Res.ExtraDep != User)
2018
- addAdditionalUsers (Res.ExtraDep , User);
2019
- Res.ExtraDep = nullptr ;
2020
- }
2021
-
2022
2010
void NewGVN::markUsersTouched (Value *V) {
2023
2011
// Now mark the users as touched.
2024
2012
for (auto *User : V->users ()) {
@@ -2426,14 +2414,9 @@ void NewGVN::processOutgoingEdges(Instruction *TI, BasicBlock *B) {
2426
2414
Value *CondEvaluated = findConditionEquivalence (Cond);
2427
2415
if (!CondEvaluated) {
2428
2416
if (auto *I = dyn_cast<Instruction>(Cond)) {
2429
- auto Res = createExpression (I);
2430
- if (const auto *CE = dyn_cast<ConstantExpression>(Res. Expr )) {
2417
+ const Expression *E = createExpression (I);
2418
+ if (const auto *CE = dyn_cast<ConstantExpression>(E )) {
2431
2419
CondEvaluated = CE->getConstantValue ();
2432
- addAdditionalUsers (Res, I);
2433
- } else {
2434
- // Did not use simplification result, no need to add the extra
2435
- // dependency.
2436
- Res.ExtraDep = nullptr ;
2437
2420
}
2438
2421
} else if (isa<ConstantInt>(Cond)) {
2439
2422
CondEvaluated = Cond;
@@ -2617,9 +2600,7 @@ Value *NewGVN::findLeaderForInst(Instruction *TransInst,
2617
2600
TempToBlock.insert ({TransInst, PredBB});
2618
2601
InstrDFS.insert ({TransInst, IDFSNum});
2619
2602
2620
- auto Res = performSymbolicEvaluation (TransInst, Visited);
2621
- const Expression *E = Res.Expr ;
2622
- addAdditionalUsers (Res, OrigInst);
2603
+ const Expression *E = performSymbolicEvaluation (TransInst, Visited);
2623
2604
InstrDFS.erase (TransInst);
2624
2605
AllTempInstructions.erase (TransInst);
2625
2606
TempToBlock.erase (TransInst);
@@ -3046,10 +3027,7 @@ void NewGVN::valueNumberInstruction(Instruction *I) {
3046
3027
const Expression *Symbolized = nullptr ;
3047
3028
SmallPtrSet<Value *, 2 > Visited;
3048
3029
if (DebugCounter::shouldExecute (VNCounter)) {
3049
- auto Res = performSymbolicEvaluation (I, Visited);
3050
- Symbolized = Res.Expr ;
3051
- addAdditionalUsers (Res, I);
3052
-
3030
+ Symbolized = performSymbolicEvaluation (I, Visited);
3053
3031
// Make a phi of ops if necessary
3054
3032
if (Symbolized && !isa<ConstantExpression>(Symbolized) &&
3055
3033
!isa<VariableExpression>(Symbolized) && PHINodeUses.count (I)) {
0 commit comments