@@ -157,16 +157,13 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
157
157
return 0 ;
158
158
}
159
159
160
- int FunctionComparator::cmpRangeMetadata (const MDNode *L,
161
- const MDNode *R) const {
160
+ int FunctionComparator::cmpMetadata (const MDNode *L, const MDNode *R) const {
162
161
if (L == R)
163
162
return 0 ;
164
163
if (!L)
165
164
return -1 ;
166
165
if (!R)
167
166
return 1 ;
168
- // Range metadata is a sequence of numbers. Make sure they are the same
169
- // sequence.
170
167
// TODO: Note that as this is metadata, it is possible to drop and/or merge
171
168
// this data when considering functions to merge. Thus this comparison would
172
169
// return 0 (i.e. equivalent), but merging would become more complicated
@@ -176,14 +173,48 @@ int FunctionComparator::cmpRangeMetadata(const MDNode *L,
176
173
if (int Res = cmpNumbers (L->getNumOperands (), R->getNumOperands ()))
177
174
return Res;
178
175
for (size_t I = 0 ; I < L->getNumOperands (); ++I) {
176
+ // TODO: the following routine coerce the metadata contents into numbers
177
+ // before comparison.
178
+ // It ignores any other cases, so that the metadata nodes are considered
179
+ // equal even though this is not correct.
180
+ // We should structurally compare the metadata nodes to be perfect here.
179
181
ConstantInt *LLow = mdconst::extract<ConstantInt>(L->getOperand (I));
180
182
ConstantInt *RLow = mdconst::extract<ConstantInt>(R->getOperand (I));
183
+ if (LLow == RLow)
184
+ continue ;
185
+ if (!LLow)
186
+ return -1 ;
187
+ if (!RLow)
188
+ return 1 ;
181
189
if (int Res = cmpAPInts (LLow->getValue (), RLow->getValue ()))
182
190
return Res;
183
191
}
184
192
return 0 ;
185
193
}
186
194
195
+ int FunctionComparator::cmpInstMetadata (Instruction const *L,
196
+ Instruction const *R) const {
197
+ // / These metadata affects the other optimization passes by making assertions
198
+ // / or constraints.
199
+ // / Values that carry different expectations should be considered different.
200
+ SmallVector<std::pair<unsigned , MDNode *>> MDL, MDR;
201
+ L->getAllMetadataOtherThanDebugLoc (MDL);
202
+ R->getAllMetadataOtherThanDebugLoc (MDR);
203
+ if (MDL.size () > MDR.size ())
204
+ return 1 ;
205
+ else if (MDL.size () < MDR.size ())
206
+ return -1 ;
207
+ for (size_t I = 0 , N = MDL.size (); I < N; ++I) {
208
+ auto const [KeyL, ML] = MDL[I];
209
+ auto const [KeyR, MR] = MDR[I];
210
+ if (int Res = cmpNumbers (KeyL, KeyR))
211
+ return Res;
212
+ if (int Res = cmpMetadata (ML, MR))
213
+ return Res;
214
+ }
215
+ return 0 ;
216
+ }
217
+
187
218
int FunctionComparator::cmpOperandBundlesSchema (const CallBase &LCS,
188
219
const CallBase &RCS) const {
189
220
assert (LCS.getOpcode () == RCS.getOpcode () && " Can't compare otherwise!" );
@@ -586,9 +617,7 @@ int FunctionComparator::cmpOperations(const Instruction *L,
586
617
if (int Res = cmpNumbers (LI->getSyncScopeID (),
587
618
cast<LoadInst>(R)->getSyncScopeID ()))
588
619
return Res;
589
- return cmpRangeMetadata (
590
- LI->getMetadata (LLVMContext::MD_range),
591
- cast<LoadInst>(R)->getMetadata (LLVMContext::MD_range));
620
+ return cmpInstMetadata (L, R);
592
621
}
593
622
if (const StoreInst *SI = dyn_cast<StoreInst>(L)) {
594
623
if (int Res =
@@ -616,8 +645,8 @@ int FunctionComparator::cmpOperations(const Instruction *L,
616
645
if (int Res = cmpNumbers (CI->getTailCallKind (),
617
646
cast<CallInst>(R)->getTailCallKind ()))
618
647
return Res;
619
- return cmpRangeMetadata (L->getMetadata (LLVMContext::MD_range),
620
- R->getMetadata (LLVMContext::MD_range));
648
+ return cmpMetadata (L->getMetadata (LLVMContext::MD_range),
649
+ R->getMetadata (LLVMContext::MD_range));
621
650
}
622
651
if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(L)) {
623
652
ArrayRef<unsigned > LIndices = IVI->getIndices ();
0 commit comments