@@ -294,6 +294,8 @@ raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) {
294
294
}
295
295
}
296
296
297
+ static LVILatticeVal intersect (LVILatticeVal A, LVILatticeVal B);
298
+
297
299
// ===----------------------------------------------------------------------===//
298
300
// LazyValueInfoCache Decl
299
301
// ===----------------------------------------------------------------------===//
@@ -395,7 +397,7 @@ namespace {
395
397
SelectInst *S, BasicBlock *BB);
396
398
bool solveBlockValueConstantRange (LVILatticeVal &BBLV,
397
399
Instruction *BBI, BasicBlock *BB);
398
- void mergeAssumeBlockValueConstantRange (Value *Val, LVILatticeVal &BBLV,
400
+ void intersectAssumeBlockValueConstantRange (Value *Val, LVILatticeVal &BBLV,
399
401
Instruction *BBI);
400
402
401
403
void solve ();
@@ -554,10 +556,8 @@ static LVILatticeVal getFromRangeMetadata(Instruction *BBI) {
554
556
}
555
557
break ;
556
558
};
557
- // Nothing known - Note that we do not want overdefined here. We may know
558
- // something else about the value and not having range metadata shouldn't
559
- // cause us to throw away those facts.
560
- return LVILatticeVal ();
559
+ // Nothing known - will be intersected with other facts
560
+ return LVILatticeVal::getOverdefined ();
561
561
}
562
562
563
563
bool LazyValueInfoCache::solveBlockValue (Value *Val, BasicBlock *BB) {
@@ -609,9 +609,9 @@ bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) {
609
609
return true ;
610
610
}
611
611
612
- // If this is an instruction which supports range metadata, return the
613
- // implied range. TODO: This should be an intersection, not a union.
614
- Res. mergeIn ( getFromRangeMetadata (BBI), DL );
612
+ // If this is an instruction which supports range metadata, intersect the
613
+ // implied range.
614
+ Res = intersect (Res, getFromRangeMetadata (BBI));
615
615
616
616
// We can only analyze the definitions of certain classes of instructions
617
617
// (integral binops and casts at the moment), so bail if this isn't one.
@@ -793,10 +793,9 @@ static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI,
793
793
LVILatticeVal &Result,
794
794
bool isTrueDest = true );
795
795
796
- // If we can determine a constant range for the value Val in the context
797
- // provided by the instruction BBI, then merge it into BBLV. If we did find a
798
- // constant range, return true.
799
- void LazyValueInfoCache::mergeAssumeBlockValueConstantRange (Value *Val,
796
+ // If we can determine a constraint on the value given conditions assumed by
797
+ // the program, intersect those constraints with BBLV
798
+ void LazyValueInfoCache::intersectAssumeBlockValueConstantRange (Value *Val,
800
799
LVILatticeVal &BBLV,
801
800
Instruction *BBI) {
802
801
BBI = BBI ? BBI : dyn_cast<Instruction>(Val);
@@ -813,12 +812,8 @@ void LazyValueInfoCache::mergeAssumeBlockValueConstantRange(Value *Val,
813
812
Value *C = I->getArgOperand (0 );
814
813
if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) {
815
814
LVILatticeVal Result;
816
- if (getValueFromFromCondition (Val, ICI, Result)) {
817
- if (BBLV.isOverdefined ())
818
- BBLV = Result;
819
- else
820
- BBLV.mergeIn (Result, DL);
821
- }
815
+ if (getValueFromFromCondition (Val, ICI, Result))
816
+ BBLV = intersect (BBLV, Result);
822
817
}
823
818
}
824
819
}
@@ -874,7 +869,7 @@ bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV,
874
869
}
875
870
876
871
LVILatticeVal LHSVal = getBlockValue (BBI->getOperand (0 ), BB);
877
- mergeAssumeBlockValueConstantRange (BBI->getOperand (0 ), LHSVal, BBI);
872
+ intersectAssumeBlockValueConstantRange (BBI->getOperand (0 ), LHSVal, BBI);
878
873
if (!LHSVal.isConstantRange ()) {
879
874
BBLV.markOverdefined ();
880
875
return true ;
@@ -1141,7 +1136,7 @@ bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
1141
1136
1142
1137
// Try to intersect ranges of the BB and the constraint on the edge.
1143
1138
LVILatticeVal InBlock = getBlockValue (Val, BBFrom);
1144
- mergeAssumeBlockValueConstantRange (Val, InBlock, BBFrom->getTerminator ());
1139
+ intersectAssumeBlockValueConstantRange (Val, InBlock, BBFrom->getTerminator ());
1145
1140
// We can use the context instruction (generically the ultimate instruction
1146
1141
// the calling pass is trying to simplify) here, even though the result of
1147
1142
// this function is generally cached when called from the solve* functions
@@ -1150,7 +1145,7 @@ bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom,
1150
1145
// functions, the context instruction is not provided. When called from
1151
1146
// LazyValueInfoCache::getValueOnEdge, the context instruction is provided,
1152
1147
// but then the result is not cached.
1153
- mergeAssumeBlockValueConstantRange (Val, InBlock, CxtI);
1148
+ intersectAssumeBlockValueConstantRange (Val, InBlock, CxtI);
1154
1149
1155
1150
Result = intersect (LocalResult, InBlock);
1156
1151
return true ;
@@ -1166,7 +1161,7 @@ LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB,
1166
1161
1167
1162
solve ();
1168
1163
LVILatticeVal Result = getBlockValue (V, BB);
1169
- mergeAssumeBlockValueConstantRange (V, Result, CxtI);
1164
+ intersectAssumeBlockValueConstantRange (V, Result, CxtI);
1170
1165
1171
1166
DEBUG (dbgs () << " Result = " << Result << " \n " );
1172
1167
return Result;
@@ -1176,18 +1171,10 @@ LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) {
1176
1171
DEBUG (dbgs () << " LVI Getting value " << *V << " at '"
1177
1172
<< CxtI->getName () << " '\n " );
1178
1173
1179
- LVILatticeVal Result;
1174
+ LVILatticeVal Result = LVILatticeVal::getOverdefined () ;
1180
1175
if (auto *I = dyn_cast<Instruction>(V))
1181
1176
Result = getFromRangeMetadata (I);
1182
- mergeAssumeBlockValueConstantRange (V, Result, CxtI);
1183
-
1184
- // Note: What's actually happening here is that we're starting at overdefined
1185
- // and then intersecting two different types of facts. The code is not
1186
- // structured that way (FIXME), and we need to take particular care to not
1187
- // let the undefined state escape since we have *not* proven the particular
1188
- // value to be unreachable at the context instruction.
1189
- if (Result.isUndefined ())
1190
- Result.markOverdefined ();
1177
+ intersectAssumeBlockValueConstantRange (V, Result, CxtI);
1191
1178
1192
1179
DEBUG (dbgs () << " Result = " << Result << " \n " );
1193
1180
return Result;
0 commit comments