Debian Codegraph @

llvm/unittests/Analysis/IRSimilarityIdentifierTest.cpp

raw
1 //===- IRSimilarityIdentifierTest.cpp - IRSimilarityIdentifier unit tests -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Tests for components for finding similarity such as the instruction mapper,
10 // suffix tree usage, and structural analysis.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/IRSimilarityIdentifier.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/AsmParser/Parser.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "gtest/gtest.h"
23
24 using namespace llvm;
25 using namespace IRSimilarity;
26
27 static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
28 StringRef ModuleStr) {
29 SMDiagnostic Err;
30 std::unique_ptr<Module> M = parseAssemblyString(ModuleStr, Err, Context);
31 assert(M && "Bad LLVM IR?");
32 return M;
33 }
34
35 void getVectors(Module &M, IRInstructionMapper &Mapper,
36 std::vector<IRInstructionData *> &InstrList,
37 std::vector<unsigned> &UnsignedVec) {
38 for (Function &F : M)
39 for (BasicBlock &BB : F)
40 Mapper.convertToUnsignedVec(BB, InstrList, UnsignedVec);
41 }
42
43 void getSimilarities(
44 Module &M,
45 std::vector<std::vector<IRSimilarityCandidate>> &SimilarityCandidates) {
46 // In order to keep the size of the tests from becoming too large, we do not
47 // recognize similarity for branches unless explicitly needed.
48 IRSimilarityIdentifier Identifier(/*EnableBranchMatching = */false);
49 SimilarityCandidates = Identifier.findSimilarity(M);
50 }
51
52 // Checks that different opcodes are mapped to different values
53 TEST(IRInstructionMapper, OpcodeDifferentiation) {
54 StringRef ModuleString = R"(
55 define i32 @f(i32 %a, i32 %b) {
56 bb0:
57 %0 = add i32 %a, %b
58 %1 = mul i32 %a, %b
59 ret i32 0
60 })";
61 LLVMContext Context;
62 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
63
64 std::vector<IRInstructionData *> InstrList;
65 std::vector<unsigned> UnsignedVec;
66
67 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
68 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
69 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
70 getVectors(*M, Mapper, InstrList, UnsignedVec);
71
72 // Check that the size of the unsigned vector and the instruction list are the
73 // same as a safety check.
74 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
75
76 // Make sure that the unsigned vector is the expected size.
77 ASSERT_TRUE(UnsignedVec.size() == 3);
78
79 // Check whether the instructions are not mapped to the same value.
80 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
81 }
82
83 // Checks that the same opcodes and types are mapped to the same values.
84 TEST(IRInstructionMapper, OpcodeTypeSimilarity) {
85 StringRef ModuleString = R"(
86 define i32 @f(i32 %a, i32 %b) {
87 bb0:
88 %0 = add i32 %a, %b
89 %1 = add i32 %b, %a
90 ret i32 0
91 })";
92 LLVMContext Context;
93 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
94
95 std::vector<IRInstructionData *> InstrList;
96 std::vector<unsigned> UnsignedVec;
97
98 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
99 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
100 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
101 getVectors(*M, Mapper, InstrList, UnsignedVec);
102
103 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
104 ASSERT_TRUE(UnsignedVec.size() == 3);
105
106 // Check whether the instructions are mapped to the same value.
107 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
108 }
109
110 // Checks that the same opcode and different types are mapped to different
111 // values.
112 TEST(IRInstructionMapper, TypeDifferentiation) {
113 StringRef ModuleString = R"(
114 define i32 @f(i32 %a, i32 %b, i64 %c, i64 %d) {
115 bb0:
116 %0 = add i32 %a, %b
117 %1 = add i64 %c, %d
118 ret i32 0
119 })";
120 LLVMContext Context;
121 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
122
123 std::vector<IRInstructionData *> InstrList;
124 std::vector<unsigned> UnsignedVec;
125
126 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
127 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
128 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
129 getVectors(*M, Mapper, InstrList, UnsignedVec);
130
131 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
132 ASSERT_TRUE(UnsignedVec.size() == 3);
133 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
134 }
135
136 // Checks that different predicates map to different values.
137 TEST(IRInstructionMapper, PredicateDifferentiation) {
138 StringRef ModuleString = R"(
139 define i32 @f(i32 %a, i32 %b) {
140 bb0:
141 %0 = icmp sge i32 %b, %a
142 %1 = icmp slt i32 %a, %b
143 ret i32 0
144 })";
145 LLVMContext Context;
146 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
147
148 std::vector<IRInstructionData *> InstrList;
149 std::vector<unsigned> UnsignedVec;
150
151 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
152 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
153 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
154 getVectors(*M, Mapper, InstrList, UnsignedVec);
155
156 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
157 ASSERT_TRUE(UnsignedVec.size() == 3);
158 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
159 }
160
161 // Checks that predicates where that can be considered the same when the
162 // operands are swapped, i.e. greater than to less than are mapped to the same
163 // unsigned integer.
164 TEST(IRInstructionMapper, PredicateIsomorphism) {
165 StringRef ModuleString = R"(
166 define i32 @f(i32 %a, i32 %b) {
167 bb0:
168 %0 = icmp sgt i32 %a, %b
169 %1 = icmp slt i32 %b, %a
170 ret i32 0
171 })";
172 LLVMContext Context;
173 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
174
175 std::vector<IRInstructionData *> InstrList;
176 std::vector<unsigned> UnsignedVec;
177
178 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
179 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
180 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
181 getVectors(*M, Mapper, InstrList, UnsignedVec);
182
183 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
184 ASSERT_TRUE(UnsignedVec.size() == 3);
185 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
186 }
187
188 // Checks that the same predicate maps to the same value.
189 TEST(IRInstructionMapper, PredicateSimilarity) {
190 StringRef ModuleString = R"(
191 define i32 @f(i32 %a, i32 %b) {
192 bb0:
193 %0 = icmp slt i32 %a, %b
194 %1 = icmp slt i32 %b, %a
195 ret i32 0
196 })";
197 LLVMContext Context;
198 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
199
200 std::vector<IRInstructionData *> InstrList;
201 std::vector<unsigned> UnsignedVec;
202
203 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
204 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
205 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
206 getVectors(*M, Mapper, InstrList, UnsignedVec);
207
208 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
209 ASSERT_TRUE(UnsignedVec.size() == 3);
210 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
211 }
212
213 // Checks that the same predicate maps to the same value for floating point
214 // CmpInsts.
215 TEST(IRInstructionMapper, FPPredicateSimilarity) {
216 StringRef ModuleString = R"(
217 define i32 @f(double %a, double %b) {
218 bb0:
219 %0 = fcmp olt double %a, %b
220 %1 = fcmp olt double %b, %a
221 ret i32 0
222 })";
223 LLVMContext Context;
224 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
225
226 std::vector<IRInstructionData *> InstrList;
227 std::vector<unsigned> UnsignedVec;
228
229 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
230 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
231 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
232 getVectors(*M, Mapper, InstrList, UnsignedVec);
233
234 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
235 ASSERT_TRUE(UnsignedVec.size() == 3);
236 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
237 }
238
239 // Checks that the different predicate maps to a different value for floating
240 // point CmpInsts.
241 TEST(IRInstructionMapper, FPPredicatDifference) {
242 StringRef ModuleString = R"(
243 define i32 @f(double %a, double %b) {
244 bb0:
245 %0 = fcmp olt double %a, %b
246 %1 = fcmp oge double %b, %a
247 ret i32 0
248 })";
249 LLVMContext Context;
250 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
251
252 std::vector<IRInstructionData *> InstrList;
253 std::vector<unsigned> UnsignedVec;
254
255 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
256 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
257 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
258 getVectors(*M, Mapper, InstrList, UnsignedVec);
259
260 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
261 ASSERT_TRUE(UnsignedVec.size() == 3);
262 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
263 }
264
265 // Checks that the zexts that have the same type parameters map to the same
266 // unsigned integer.
267 TEST(IRInstructionMapper, ZextTypeSimilarity) {
268 StringRef ModuleString = R"(
269 define i32 @f(i32 %a) {
270 bb0:
271 %0 = zext i32 %a to i64
272 %1 = zext i32 %a to i64
273 ret i32 0
274 })";
275 LLVMContext Context;
276 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
277
278 std::vector<IRInstructionData *> InstrList;
279 std::vector<unsigned> UnsignedVec;
280
281 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
282 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
283 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
284 getVectors(*M, Mapper, InstrList, UnsignedVec);
285
286 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
287 ASSERT_TRUE(UnsignedVec.size() == 3);
288 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
289 }
290
291 // Checks that the sexts that have the same type parameters map to the same
292 // unsigned integer.
293 TEST(IRInstructionMapper, SextTypeSimilarity) {
294 StringRef ModuleString = R"(
295 define i32 @f(i32 %a) {
296 bb0:
297 %0 = sext i32 %a to i64
298 %1 = sext i32 %a to i64
299 ret i32 0
300 })";
301 LLVMContext Context;
302 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
303
304 std::vector<IRInstructionData *> InstrList;
305 std::vector<unsigned> UnsignedVec;
306
307 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
308 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
309 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
310 getVectors(*M, Mapper, InstrList, UnsignedVec);
311
312 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
313 ASSERT_TRUE(UnsignedVec.size() == 3);
314 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
315 }
316
317 // Checks that the zexts that have the different type parameters map to the
318 // different unsigned integers.
319 TEST(IRInstructionMapper, ZextTypeDifference) {
320 StringRef ModuleString = R"(
321 define i32 @f(i32 %a, i8 %b) {
322 bb0:
323 %0 = zext i32 %a to i64
324 %1 = zext i8 %b to i32
325 ret i32 0
326 })";
327 LLVMContext Context;
328 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
329
330 std::vector<IRInstructionData *> InstrList;
331 std::vector<unsigned> UnsignedVec;
332
333 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
334 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
335 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
336 getVectors(*M, Mapper, InstrList, UnsignedVec);
337
338 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
339 ASSERT_TRUE(UnsignedVec.size() == 3);
340 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
341 }
342
343 // Checks that the sexts that have the different type parameters map to the
344 // different unsigned integers.
345 TEST(IRInstructionMapper, SextTypeDifference) {
346 StringRef ModuleString = R"(
347 define i32 @f(i32 %a, i8 %b) {
348 bb0:
349 %0 = sext i32 %a to i64
350 %1 = sext i8 %b to i32
351 ret i32 0
352 })";
353 LLVMContext Context;
354 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
355
356 std::vector<IRInstructionData *> InstrList;
357 std::vector<unsigned> UnsignedVec;
358
359 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
360 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
361 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
362 getVectors(*M, Mapper, InstrList, UnsignedVec);
363
364 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
365 ASSERT_TRUE(UnsignedVec.size() == 3);
366 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
367 }
368
369 // Checks that loads that have the same type are mapped to the same unsigned
370 // integer.
371 TEST(IRInstructionMapper, LoadSimilarType) {
372 StringRef ModuleString = R"(
373 define i32 @f(i32* %a, i32* %b) {
374 bb0:
375 %0 = load i32, i32* %a
376 %1 = load i32, i32* %b
377 ret i32 0
378 })";
379 LLVMContext Context;
380 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
381
382 std::vector<IRInstructionData *> InstrList;
383 std::vector<unsigned> UnsignedVec;
384
385 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
386 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
387 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
388 getVectors(*M, Mapper, InstrList, UnsignedVec);
389
390 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
391 ASSERT_TRUE(UnsignedVec.size() == 3);
392 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
393 }
394
395 // Checks that loads that have the different types are mapped to
396 // different unsigned integers.
397 TEST(IRInstructionMapper, LoadDifferentType) {
398 StringRef ModuleString = R"(
399 define i32 @f(i32* %a, i64* %b) {
400 bb0:
401 %0 = load i32, i32* %a
402 %1 = load i64, i64* %b
403 ret i32 0
404 })";
405 LLVMContext Context;
406 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
407
408 std::vector<IRInstructionData *> InstrList;
409 std::vector<unsigned> UnsignedVec;
410
411 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
412 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
413 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
414 getVectors(*M, Mapper, InstrList, UnsignedVec);
415
416 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
417 ASSERT_TRUE(UnsignedVec.size() == 3);
418 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
419 }
420
421 // Checks that loads that have the different aligns are mapped to different
422 // unsigned integers.
423 TEST(IRInstructionMapper, LoadDifferentAlign) {
424 StringRef ModuleString = R"(
425 define i32 @f(i32* %a, i32* %b) {
426 bb0:
427 %0 = load i32, i32* %a, align 4
428 %1 = load i32, i32* %b, align 8
429 ret i32 0
430 })";
431 LLVMContext Context;
432 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
433
434 std::vector<IRInstructionData *> InstrList;
435 std::vector<unsigned> UnsignedVec;
436
437 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
438 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
439 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
440 getVectors(*M, Mapper, InstrList, UnsignedVec);
441
442 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
443 ASSERT_TRUE(UnsignedVec.size() == 3);
444 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
445 }
446
447 // Checks that loads that have the different volatile settings are mapped to
448 // different unsigned integers.
449 TEST(IRInstructionMapper, LoadDifferentVolatile) {
450 StringRef ModuleString = R"(
451 define i32 @f(i32* %a, i32* %b) {
452 bb0:
453 %0 = load volatile i32, i32* %a
454 %1 = load i32, i32* %b
455 ret i32 0
456 })";
457 LLVMContext Context;
458 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
459
460 std::vector<IRInstructionData *> InstrList;
461 std::vector<unsigned> UnsignedVec;
462
463 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
464 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
465 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
466 getVectors(*M, Mapper, InstrList, UnsignedVec);
467
468 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
469 ASSERT_TRUE(UnsignedVec.size() == 3);
470 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
471 }
472
473 // Checks that loads that have the same volatile settings are mapped to
474 // different unsigned integers.
475 TEST(IRInstructionMapper, LoadSameVolatile) {
476 StringRef ModuleString = R"(
477 define i32 @f(i32* %a, i32* %b) {
478 bb0:
479 %0 = load volatile i32, i32* %a
480 %1 = load volatile i32, i32* %b
481 ret i32 0
482 })";
483 LLVMContext Context;
484 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
485
486 std::vector<IRInstructionData *> InstrList;
487 std::vector<unsigned> UnsignedVec;
488
489 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
490 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
491 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
492 getVectors(*M, Mapper, InstrList, UnsignedVec);
493
494 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
495 ASSERT_TRUE(UnsignedVec.size() == 3);
496 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
497 }
498
499 // Checks that loads that have the different atomicity settings are mapped to
500 // different unsigned integers.
501 TEST(IRInstructionMapper, LoadDifferentAtomic) {
502 StringRef ModuleString = R"(
503 define i32 @f(i32* %a, i32* %b) {
504 bb0:
505 %0 = load atomic i32, i32* %a unordered, align 4
506 %1 = load atomic i32, i32* %b monotonic, align 4
507 ret i32 0
508 })";
509 LLVMContext Context;
510 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
511
512 std::vector<IRInstructionData *> InstrList;
513 std::vector<unsigned> UnsignedVec;
514
515 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
516 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
517 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
518 getVectors(*M, Mapper, InstrList, UnsignedVec);
519
520 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
521 ASSERT_TRUE(UnsignedVec.size() == 3);
522 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
523 }
524
525 // Checks that loads that have the same atomicity settings are mapped to
526 // different unsigned integers.
527 TEST(IRInstructionMapper, LoadSameAtomic) {
528 StringRef ModuleString = R"(
529 define i32 @f(i32* %a, i32* %b) {
530 bb0:
531 %0 = load atomic i32, i32* %a unordered, align 4
532 %1 = load atomic i32, i32* %b unordered, align 4
533 ret i32 0
534 })";
535 LLVMContext Context;
536 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
537
538 std::vector<IRInstructionData *> InstrList;
539 std::vector<unsigned> UnsignedVec;
540
541 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
542 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
543 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
544 getVectors(*M, Mapper, InstrList, UnsignedVec);
545
546 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
547 ASSERT_TRUE(UnsignedVec.size() == 3);
548 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
549 }
550
551 // Checks that stores that have the same type are mapped to the same unsigned
552 // integer.
553 TEST(IRInstructionMapper, StoreSimilarType) {
554 StringRef ModuleString = R"(
555 define i32 @f(i32* %a, i32* %b) {
556 bb0:
557 store i32 1, i32* %a
558 store i32 2, i32* %a
559 ret i32 0
560 })";
561 LLVMContext Context;
562 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
563
564 std::vector<IRInstructionData *> InstrList;
565 std::vector<unsigned> UnsignedVec;
566
567 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
568 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
569 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
570 getVectors(*M, Mapper, InstrList, UnsignedVec);
571
572 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
573 ASSERT_TRUE(UnsignedVec.size() == 3);
574 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
575 }
576
577 // Checks that stores that have the different types are mapped to
578 // different unsigned integers.
579 TEST(IRInstructionMapper, StoreDifferentType) {
580 StringRef ModuleString = R"(
581 define i32 @f(i32* %a, i64* %b) {
582 bb0:
583 store i32 1, i32* %a
584 store i64 1, i64* %b
585 ret i32 0
586 })";
587 LLVMContext Context;
588 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
589
590 std::vector<IRInstructionData *> InstrList;
591 std::vector<unsigned> UnsignedVec;
592
593 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
594 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
595 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
596 getVectors(*M, Mapper, InstrList, UnsignedVec);
597
598 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
599 ASSERT_TRUE(UnsignedVec.size() == 3);
600 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
601 }
602
603 // Checks that stores that have the different aligns are mapped to different
604 // unsigned integers.
605 TEST(IRInstructionMapper, StoreDifferentAlign) {
606 StringRef ModuleString = R"(
607 define i32 @f(i32* %a, i32* %b) {
608 bb0:
609 store i32 1, i32* %a, align 4
610 store i32 1, i32* %b, align 8
611 ret i32 0
612 })";
613 LLVMContext Context;
614 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
615
616 std::vector<IRInstructionData *> InstrList;
617 std::vector<unsigned> UnsignedVec;
618
619 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
620 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
621 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
622 getVectors(*M, Mapper, InstrList, UnsignedVec);
623
624 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
625 ASSERT_TRUE(UnsignedVec.size() == 3);
626 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
627 }
628
629 // Checks that stores that have the different volatile settings are mapped to
630 // different unsigned integers.
631 TEST(IRInstructionMapper, StoreDifferentVolatile) {
632 StringRef ModuleString = R"(
633 define i32 @f(i32* %a, i32* %b) {
634 bb0:
635 store volatile i32 1, i32* %a
636 store i32 1, i32* %b
637 ret i32 0
638 })";
639 LLVMContext Context;
640 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
641
642 std::vector<IRInstructionData *> InstrList;
643 std::vector<unsigned> UnsignedVec;
644
645 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
646 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
647 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
648 getVectors(*M, Mapper, InstrList, UnsignedVec);
649
650 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
651 ASSERT_TRUE(UnsignedVec.size() == 3);
652 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
653 }
654
655 // Checks that stores that have the same volatile settings are mapped to
656 // different unsigned integers.
657 TEST(IRInstructionMapper, StoreSameVolatile) {
658 StringRef ModuleString = R"(
659 define i32 @f(i32* %a, i32* %b) {
660 bb0:
661 store volatile i32 1, i32* %a
662 store volatile i32 1, i32* %b
663 ret i32 0
664 })";
665 LLVMContext Context;
666 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
667
668 std::vector<IRInstructionData *> InstrList;
669 std::vector<unsigned> UnsignedVec;
670
671 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
672 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
673 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
674 getVectors(*M, Mapper, InstrList, UnsignedVec);
675
676 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
677 ASSERT_TRUE(UnsignedVec.size() == 3);
678 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
679 }
680
681 // Checks that loads that have the same atomicity settings are mapped to
682 // different unsigned integers.
683 TEST(IRInstructionMapper, StoreSameAtomic) {
684 StringRef ModuleString = R"(
685 define i32 @f(i32* %a, i32* %b) {
686 bb0:
687 store atomic i32 1, i32* %a unordered, align 4
688 store atomic i32 1, i32* %b unordered, align 4
689 ret i32 0
690 })";
691 LLVMContext Context;
692 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
693
694 std::vector<IRInstructionData *> InstrList;
695 std::vector<unsigned> UnsignedVec;
696
697 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
698 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
699 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
700 getVectors(*M, Mapper, InstrList, UnsignedVec);
701
702 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
703 ASSERT_TRUE(UnsignedVec.size() == 3);
704 ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
705 }
706
707 // Checks that loads that have the different atomicity settings are mapped to
708 // different unsigned integers.
709 TEST(IRInstructionMapper, StoreDifferentAtomic) {
710 StringRef ModuleString = R"(
711 define i32 @f(i32* %a, i32* %b) {
712 bb0:
713 store atomic i32 1, i32* %a unordered, align 4
714 store atomic i32 1, i32* %b monotonic, align 4
715 ret i32 0
716 })";
717 LLVMContext Context;
718 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
719
720 std::vector<IRInstructionData *> InstrList;
721 std::vector<unsigned> UnsignedVec;
722
723 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
724 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
725 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
726 getVectors(*M, Mapper, InstrList, UnsignedVec);
727
728 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
729 ASSERT_TRUE(UnsignedVec.size() == 3);
730 ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
731 }
732
733 // Checks that the branch is mapped to legal when the option is set.
734 TEST(IRInstructionMapper, BranchLegal) {
735 StringRef ModuleString = R"(
736 define i32 @f(i32 %a, i32 %b) {
737 bb0:
738 %0 = icmp slt i32 %a, %b
739 br i1 %0, label %bb0, label %bb1
740 bb1:
741 ret i32 0
742 })";
743 LLVMContext Context;
744 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
745
746 std::vector<IRInstructionData *> InstrList;
747 std::vector<unsigned> UnsignedVec;
748
749 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
750 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
751 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
752 Mapper.InstClassifier.EnableBranches = true;
753 Mapper.initializeForBBs(*M);
754 getVectors(*M, Mapper, InstrList, UnsignedVec);
755
756 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
757 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
758 ASSERT_TRUE(UnsignedVec[1] > UnsignedVec[0]);
759 ASSERT_TRUE(UnsignedVec[1] < UnsignedVec[2]);
760 }
761
762 // Checks that a PHINode is mapped to be legal.
763 TEST(IRInstructionMapper, PhiLegal) {
764 StringRef ModuleString = R"(
765 define i32 @f(i32 %a, i32 %b) {
766 bb0:
767 %0 = phi i1 [ 0, %bb0 ], [ %0, %bb1 ]
768 %1 = add i32 %a, %b
769 ret i32 0
770 bb1:
771 ret i32 1
772 })";
773 LLVMContext Context;
774 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
775
776 std::vector<IRInstructionData *> InstrList;
777 std::vector<unsigned> UnsignedVec;
778
779 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
780 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
781 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
782 Mapper.InstClassifier.EnableBranches = true;
783 Mapper.initializeForBBs(*M);
784 getVectors(*M, Mapper, InstrList, UnsignedVec);
785
786 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
787 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
788 }
789
790 // Checks that a PHINode is mapped to be legal.
791 TEST(IRInstructionMapper, PhiIllegal) {
792 StringRef ModuleString = R"(
793 define i32 @f(i32 %a, i32 %b) {
794 bb0:
795 %0 = phi i1 [ 0, %bb0 ], [ %0, %bb1 ]
796 %1 = add i32 %a, %b
797 ret i32 0
798 bb1:
799 ret i32 1
800 })";
801 LLVMContext Context;
802 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
803
804 std::vector<IRInstructionData *> InstrList;
805 std::vector<unsigned> UnsignedVec;
806
807 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
808 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
809 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
810 Mapper.initializeForBBs(*M);
811 getVectors(*M, Mapper, InstrList, UnsignedVec);
812
813 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
814 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
815 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
816 }
817
818 // In most cases, the illegal instructions we are collecting don't require any
819 // sort of setup. In these cases, we can just only have illegal instructions,
820 // and the mapper will create 0 length vectors, and we can check that.
821
822 // In cases where we have legal instructions needed to set up the illegal
823 // instruction, to check illegal instructions are assigned unsigned integers
824 // from the maximum value decreasing to 0, it will be greater than a legal
825 // instruction that comes after. So to check that we have an illegal
826 // instruction, we place a legal instruction after an illegal instruction, and
827 // check that the illegal unsigned integer is greater than the unsigned integer
828 // of the legal instruction.
829
830 // Checks that an alloca instruction is mapped to be illegal.
831 TEST(IRInstructionMapper, AllocaIllegal) {
832 StringRef ModuleString = R"(
833 define i32 @f(i32 %a, i32 %b) {
834 bb0:
835 %0 = alloca i32
836 ret i32 0
837 })";
838 LLVMContext Context;
839 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
840
841 std::vector<IRInstructionData *> InstrList;
842 std::vector<unsigned> UnsignedVec;
843
844 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
845 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
846 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
847 getVectors(*M, Mapper, InstrList, UnsignedVec);
848
849 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
850 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
851 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
852 }
853
854 // Checks that an getelementptr instruction is mapped to be legal. And that
855 // the operands in getelementpointer instructions are the exact same after the
856 // first element operand, which only requires the same type.
857 TEST(IRInstructionMapper, GetElementPtrSameEndOperands) {
858 StringRef ModuleString = R"(
859 %struct.RT = type { i8, [10 x [20 x i32]], i8 }
860 %struct.ST = type { i32, double, %struct.RT }
861 define i32 @f(%struct.ST* %s, i64 %a, i64 %b) {
862 bb0:
863 %0 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %a, i32 0
864 %1 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %b, i32 0
865 ret i32 0
866 })";
867 LLVMContext Context;
868 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
869
870 std::vector<IRInstructionData *> InstrList;
871 std::vector<unsigned> UnsignedVec;
872
873 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
874 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
875 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
876 getVectors(*M, Mapper, InstrList, UnsignedVec);
877
878 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
879 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
880 ASSERT_EQ(UnsignedVec[0], UnsignedVec[1]);
881 }
882
883 // Check that when the operands in getelementpointer instructions are not the
884 // exact same after the first element operand, the instructions are mapped to
885 // different values.
886 TEST(IRInstructionMapper, GetElementPtrDifferentEndOperands) {
887 StringRef ModuleString = R"(
888 %struct.RT = type { i8, [10 x [20 x i32]], i8 }
889 %struct.ST = type { i32, double, %struct.RT }
890 define i32 @f(%struct.ST* %s, i64 %a, i64 %b) {
891 bb0:
892 %0 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %a, i32 0
893 %1 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %b, i32 2
894 ret i32 0
895 })";
896 LLVMContext Context;
897 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
898
899 std::vector<IRInstructionData *> InstrList;
900 std::vector<unsigned> UnsignedVec;
901
902 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
903 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
904 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
905 getVectors(*M, Mapper, InstrList, UnsignedVec);
906
907 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
908 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
909 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
910 }
911
912 // Check that when the operands in getelementpointer instructions are not the
913 // same initial base type, each instruction is mapped to a different value.
914 TEST(IRInstructionMapper, GetElementPtrDifferentBaseType) {
915 StringRef ModuleString = R"(
916 %struct.RT = type { i8, [10 x [20 x i32]], i8 }
917 %struct.ST = type { i32, double, %struct.RT }
918 define i32 @f(%struct.ST* %s, %struct.RT* %r, i64 %a, i64 %b) {
919 bb0:
920 %0 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %a
921 %1 = getelementptr inbounds %struct.RT, %struct.RT* %r, i64 %b
922 ret i32 0
923 })";
924 LLVMContext Context;
925 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
926
927 std::vector<IRInstructionData *> InstrList;
928 std::vector<unsigned> UnsignedVec;
929
930 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
931 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
932 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
933 getVectors(*M, Mapper, InstrList, UnsignedVec);
934
935 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
936 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
937 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
938 }
939
940 // Check that when the operands in getelementpointer instructions do not have
941 // the same inbounds modifier, they are not counted as the same.
942 TEST(IRInstructionMapper, GetElementPtrDifferentInBounds) {
943 StringRef ModuleString = R"(
944 %struct.RT = type { i8, [10 x [20 x i32]], i8 }
945 %struct.ST = type { i32, double, %struct.RT }
946 define i32 @f(%struct.ST* %s, %struct.RT* %r, i64 %a, i64 %b) {
947 bb0:
948 %0 = getelementptr inbounds %struct.ST, %struct.ST* %s, i64 %a, i32 0
949 %1 = getelementptr %struct.ST, %struct.ST* %s, i64 %b, i32 0
950 ret i32 0
951 })";
952 LLVMContext Context;
953 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
954
955 std::vector<IRInstructionData *> InstrList;
956 std::vector<unsigned> UnsignedVec;
957
958 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
959 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
960 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
961 getVectors(*M, Mapper, InstrList, UnsignedVec);
962
963 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
964 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
965 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
966 }
967
968 // Checks that indirect call instructions are mapped to be illegal when it is
969 // specified to disallow them.
970 TEST(IRInstructionMapper, CallsIllegalIndirect) {
971 StringRef ModuleString = R"(
972 define i32 @f(void()* %func) {
973 bb0:
974 call void %func()
975 ret i32 0
976 })";
977 LLVMContext Context;
978 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
979
980 std::vector<IRInstructionData *> InstrList;
981 std::vector<unsigned> UnsignedVec;
982
983 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
984 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
985 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
986 Mapper.InstClassifier.EnableIndirectCalls = false;
987 getVectors(*M, Mapper, InstrList, UnsignedVec);
988
989 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
990 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
991 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
992 }
993
994 // Checks that indirect call instructions are mapped to be legal when it is not
995 // specified to disallow them.
996 TEST(IRInstructionMapper, CallsLegalIndirect) {
997 StringRef ModuleString = R"(
998 define i32 @f(void()* %func) {
999 bb0:
1000 call void %func()
1001 call void %func()
1002 ret i32 0
1003 })";
1004 LLVMContext Context;
1005 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1006
1007 std::vector<IRInstructionData *> InstrList;
1008 std::vector<unsigned> UnsignedVec;
1009
1010 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1011 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1012 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1013 Mapper.InstClassifier.EnableIndirectCalls = true;
1014 getVectors(*M, Mapper, InstrList, UnsignedVec);
1015
1016 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1017 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1018 }
1019
1020 // Checks that a call instruction is mapped to be legal. Here we check that
1021 // a call with the same name, and same types are mapped to the same
1022 // value.
1023 TEST(IRInstructionMapper, CallsSameTypeSameName) {
1024 StringRef ModuleString = R"(
1025 declare i32 @f1(i32, i32)
1026 define i32 @f(i32 %a, i32 %b) {
1027 bb0:
1028 %0 = call i32 @f1(i32 %a, i32 %b)
1029 %1 = call i32 @f1(i32 %a, i32 %b)
1030 ret i32 0
1031 })";
1032 LLVMContext Context;
1033 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1034
1035 std::vector<IRInstructionData *> InstrList;
1036 std::vector<unsigned> UnsignedVec;
1037
1038 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1039 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1040 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1041 getVectors(*M, Mapper, InstrList, UnsignedVec);
1042
1043 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1044 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1045 ASSERT_EQ(UnsignedVec[0], UnsignedVec[1]);
1046 }
1047
1048 // Here we check that a calls with different names, but the same arguments types
1049 // are mapped to different value when specified that the name must match.
1050 TEST(IRInstructionMapper, CallsSameArgTypeDifferentNameDisallowed) {
1051 StringRef ModuleString = R"(
1052 declare i32 @f1(i32, i32)
1053 declare i32 @f2(i32, i32)
1054 define i32 @f(i32 %a, i32 %b) {
1055 bb0:
1056 %0 = call i32 @f1(i32 %a, i32 %b)
1057 %1 = call i32 @f2(i32 %a, i32 %b)
1058 ret i32 0
1059 })";
1060 LLVMContext Context;
1061 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1062
1063 std::vector<IRInstructionData *> InstrList;
1064 std::vector<unsigned> UnsignedVec;
1065
1066 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1067 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1068 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1069 Mapper.EnableMatchCallsByName = true;
1070 getVectors(*M, Mapper, InstrList, UnsignedVec);
1071
1072 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1073 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1074 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
1075 }
1076
1077 // Here we check that a calls with different names, but the same arguments types
1078 // are mapped to the same value when it is not specifed that they must match.
1079 TEST(IRInstructionMapper, CallsSameArgTypeDifferentName) {
1080 StringRef ModuleString = R"(
1081 declare i32 @f1(i32, i32)
1082 declare i32 @f2(i32, i32)
1083 define i32 @f(i32 %a, i32 %b) {
1084 bb0:
1085 %0 = call i32 @f1(i32 %a, i32 %b)
1086 %1 = call i32 @f2(i32 %a, i32 %b)
1087 ret i32 0
1088 })";
1089 LLVMContext Context;
1090 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1091
1092 std::vector<IRInstructionData *> InstrList;
1093 std::vector<unsigned> UnsignedVec;
1094
1095 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1096 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1097 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1098 Mapper.EnableMatchCallsByName = false;
1099 getVectors(*M, Mapper, InstrList, UnsignedVec);
1100
1101 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1102 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1103 ASSERT_EQ(UnsignedVec[0], UnsignedVec[1]);
1104 }
1105
1106 // Here we check that a calls with different names, and different arguments
1107 // types are mapped to different value.
1108 TEST(IRInstructionMapper, CallsDifferentArgTypeDifferentName) {
1109 StringRef ModuleString = R"(
1110 declare i32 @f1(i32, i32)
1111 declare i32 @f2(i32)
1112 define i32 @f(i32 %a, i32 %b) {
1113 bb0:
1114 %0 = call i32 @f1(i32 %a, i32 %b)
1115 %1 = call i32 @f2(i32 %a)
1116 ret i32 0
1117 })";
1118 LLVMContext Context;
1119 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1120
1121 std::vector<IRInstructionData *> InstrList;
1122 std::vector<unsigned> UnsignedVec;
1123
1124 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1125 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1126 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1127 getVectors(*M, Mapper, InstrList, UnsignedVec);
1128
1129 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1130 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1131 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
1132 }
1133
1134 // Here we check that calls with different names, and different return
1135 // types are mapped to different value.
1136 TEST(IRInstructionMapper, CallsDifferentReturnTypeDifferentName) {
1137 StringRef ModuleString = R"(
1138 declare i64 @f1(i32, i32)
1139 declare i32 @f2(i32, i32)
1140 define i32 @f(i32 %a, i32 %b) {
1141 bb0:
1142 %0 = call i64 @f1(i32 %a, i32 %b)
1143 %1 = call i32 @f2(i32 %a, i32 %b)
1144 ret i32 0
1145 })";
1146 LLVMContext Context;
1147 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1148
1149 std::vector<IRInstructionData *> InstrList;
1150 std::vector<unsigned> UnsignedVec;
1151
1152 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1153 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1154 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1155 getVectors(*M, Mapper, InstrList, UnsignedVec);
1156
1157 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1158 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1159 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
1160 }
1161
1162 // Here we check that calls with the same name, types, and parameters map to the
1163 // same unsigned integer.
1164 TEST(IRInstructionMapper, CallsSameParameters) {
1165 StringRef ModuleString = R"(
1166 declare i32 @f1(i32, i32)
1167 define i32 @f(i32 %a, i32 %b) {
1168 bb0:
1169 %0 = tail call fastcc i32 @f1(i32 %a, i32 %b)
1170 %1 = tail call fastcc i32 @f1(i32 %a, i32 %b)
1171 ret i32 0
1172 })";
1173 LLVMContext Context;
1174 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1175
1176 std::vector<IRInstructionData *> InstrList;
1177 std::vector<unsigned> UnsignedVec;
1178
1179 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1180 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1181 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1182 getVectors(*M, Mapper, InstrList, UnsignedVec);
1183
1184 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1185 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1186 ASSERT_EQ(UnsignedVec[0], UnsignedVec[1]);
1187 }
1188
1189 // Here we check that calls with different tail call settings are mapped to
1190 // different values.
1191 TEST(IRInstructionMapper, CallsDifferentTails) {
1192 StringRef ModuleString = R"(
1193 declare i32 @f1(i32, i32)
1194 define i32 @f(i32 %a, i32 %b) {
1195 bb0:
1196 %0 = tail call i32 @f1(i32 %a, i32 %b)
1197 %1 = call i32 @f1(i32 %a, i32 %b)
1198 ret i32 0
1199 })";
1200 LLVMContext Context;
1201 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1202
1203 std::vector<IRInstructionData *> InstrList;
1204 std::vector<unsigned> UnsignedVec;
1205
1206 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1207 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1208 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1209 getVectors(*M, Mapper, InstrList, UnsignedVec);
1210
1211 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1212 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1213 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
1214 }
1215
1216 // Here we check that calls with different calling convention settings are
1217 // mapped to different values.
1218 TEST(IRInstructionMapper, CallsDifferentCallingConventions) {
1219 StringRef ModuleString = R"(
1220 declare i32 @f1(i32, i32)
1221 define i32 @f(i32 %a, i32 %b) {
1222 bb0:
1223 %0 = call fastcc i32 @f1(i32 %a, i32 %b)
1224 %1 = call i32 @f1(i32 %a, i32 %b)
1225 ret i32 0
1226 })";
1227 LLVMContext Context;
1228 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1229
1230 std::vector<IRInstructionData *> InstrList;
1231 std::vector<unsigned> UnsignedVec;
1232
1233 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1234 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1235 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1236 getVectors(*M, Mapper, InstrList, UnsignedVec);
1237
1238 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1239 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1240 ASSERT_NE(UnsignedVec[0], UnsignedVec[1]);
1241 }
1242
1243 // Checks that an invoke instruction is mapped to be illegal. Invoke
1244 // instructions are considered to be illegal because of the change in the
1245 // control flow that is currently not recognized.
1246 TEST(IRInstructionMapper, InvokeIllegal) {
1247 StringRef ModuleString = R"(
1248 define i32 @f(i8 *%gep1, i32 %b) {
1249 then:
1250 invoke i32 undef(i8* undef)
1251 to label %invoke unwind label %lpad
1252
1253 invoke:
1254 unreachable
1255
1256 lpad:
1257 landingpad { i8*, i32 }
1258 catch i8* null
1259 unreachable
1260 })";
1261 LLVMContext Context;
1262 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1263
1264 std::vector<IRInstructionData *> InstrList;
1265 std::vector<unsigned> UnsignedVec;
1266
1267 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1268 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1269 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1270 getVectors(*M, Mapper, InstrList, UnsignedVec);
1271
1272 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1273 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1274 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1275 }
1276
1277 // Checks that an callbr instructions are considered to be illegal. Callbr
1278 // instructions are considered to be illegal because of the change in the
1279 // control flow that is currently not recognized.
1280 TEST(IRInstructionMapper, CallBrInstIllegal) {
1281 StringRef ModuleString = R"(
1282 define void @test() {
1283 fail:
1284 ret void
1285 }
1286
1287 define i32 @f(i32 %a, i32 %b) {
1288 bb0:
1289 callbr void asm "xorl $0, $0; jmp ${1:l}", "r,X,~{dirflag},~{fpsr},~{flags}"(i32 %a, i8* blockaddress(@test, %fail)) to label %normal [label %fail]
1290 fail:
1291 ret i32 0
1292 normal:
1293 ret i32 0
1294 })";
1295 LLVMContext Context;
1296 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1297
1298 std::vector<IRInstructionData *> InstrList;
1299 std::vector<unsigned> UnsignedVec;
1300
1301 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1302 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1303 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1304 getVectors(*M, Mapper, InstrList, UnsignedVec);
1305
1306 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1307 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1308 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1309 }
1310
1311 // Checks that an debuginfo records are mapped to be invisible. Since they
1312 // do not semantically change the program, they can be recognized as similar.
1313 TEST(IRInstructionMapper, DebugInfoInvisible) {
1314 StringRef ModuleString = R"(
1315 define i32 @f(i32 %a, i32 %b) {
1316 then:
1317 %0 = add i32 %a, %b
1318 #dbg_value(i32 0, !0, !0, !0)
1319 %1 = add i32 %a, %b
1320 ret i32 0
1321 }
1322
1323 !0 = distinct !{!"test\00", i32 10})";
1324 LLVMContext Context;
1325 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1326
1327 std::vector<IRInstructionData *> InstrList;
1328 std::vector<unsigned> UnsignedVec;
1329
1330 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1331 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1332 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1333 getVectors(*M, Mapper, InstrList, UnsignedVec);
1334
1335 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1336 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(3));
1337 }
1338
1339 // The following are all exception handling intrinsics. We do not currently
1340 // handle these instruction because they are very context dependent.
1341
1342 // Checks that an eh.typeid.for intrinsic is mapped to be illegal.
1343 TEST(IRInstructionMapper, ExceptionHandlingTypeIdIllegal) {
1344 StringRef ModuleString = R"(
1345 @_ZTIi = external constant i8*
1346 define i32 @f() {
1347 then:
1348 %0 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
1349 ret i32 0
1350 }
1351
1352 declare i32 @llvm.eh.typeid.for(i8*))";
1353 LLVMContext Context;
1354 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1355
1356 std::vector<IRInstructionData *> InstrList;
1357 std::vector<unsigned> UnsignedVec;
1358
1359 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1360 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1361 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1362 getVectors(*M, Mapper, InstrList, UnsignedVec);
1363
1364 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1365 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1366 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1367 }
1368
1369 // Checks that an eh.exceptioncode intrinsic is mapped to be illegal.
1370 TEST(IRInstructionMapper, ExceptionHandlingExceptionCodeIllegal) {
1371 StringRef ModuleString = R"(
1372 define i32 @f(i32 %a, i32 %b) {
1373 entry:
1374 %0 = catchswitch within none [label %__except] unwind to caller
1375
1376 __except:
1377 %1 = catchpad within %0 [i8* null]
1378 catchret from %1 to label %__except
1379
1380 then:
1381 %2 = call i32 @llvm.eh.exceptioncode(token %1)
1382 ret i32 0
1383 }
1384
1385 declare i32 @llvm.eh.exceptioncode(token))";
1386 LLVMContext Context;
1387 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1388
1389 std::vector<IRInstructionData *> InstrList;
1390 std::vector<unsigned> UnsignedVec;
1391
1392 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1393 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1394 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1395 getVectors(*M, Mapper, InstrList, UnsignedVec);
1396
1397 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1398 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1399 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1400 }
1401
1402 // Checks that an eh.unwind intrinsic is mapped to be illegal.
1403 TEST(IRInstructionMapper, ExceptionHandlingUnwindIllegal) {
1404 StringRef ModuleString = R"(
1405 define i32 @f(i32 %a, i32 %b) {
1406 entry:
1407 call void @llvm.eh.unwind.init()
1408 ret i32 0
1409 }
1410
1411 declare void @llvm.eh.unwind.init())";
1412 LLVMContext Context;
1413 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1414
1415 std::vector<IRInstructionData *> InstrList;
1416 std::vector<unsigned> UnsignedVec;
1417
1418 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1419 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1420 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1421 getVectors(*M, Mapper, InstrList, UnsignedVec);
1422
1423 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1424 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1425 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1426 }
1427
1428 // Checks that an eh.exceptionpointer intrinsic is mapped to be illegal.
1429 TEST(IRInstructionMapper, ExceptionHandlingExceptionPointerIllegal) {
1430 StringRef ModuleString = R"(
1431 define i32 @f(i32 %a, i32 %b) {
1432 entry:
1433 %0 = call i8* @llvm.eh.exceptionpointer.p0i8(i32 0)
1434 ret i32 0
1435 }
1436
1437 declare i8* @llvm.eh.exceptionpointer.p0i8(i32))";
1438 LLVMContext Context;
1439 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1440
1441 std::vector<IRInstructionData *> InstrList;
1442 std::vector<unsigned> UnsignedVec;
1443
1444 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1445 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1446 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1447 getVectors(*M, Mapper, InstrList, UnsignedVec);
1448
1449 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1450 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1451 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1452 }
1453
1454 // Checks that a catchpad instruction is mapped to an illegal value.
1455 TEST(IRInstructionMapper, CatchpadIllegal) {
1456 StringRef ModuleString = R"(
1457 declare void @llvm.donothing() nounwind readnone
1458
1459 define void @function() personality i8 3 {
1460 entry:
1461 invoke void @llvm.donothing() to label %normal unwind label %exception
1462 exception:
1463 %cs1 = catchswitch within none [label %catchpad1] unwind to caller
1464 catchpad1:
1465 catchpad within %cs1 []
1466 br label %normal
1467 normal:
1468 ret void
1469 })";
1470 LLVMContext Context;
1471 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1472
1473 std::vector<IRInstructionData *> InstrList;
1474 std::vector<unsigned> UnsignedVec;
1475
1476 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1477 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1478 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1479 getVectors(*M, Mapper, InstrList, UnsignedVec);
1480
1481 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1482 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1483 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1484 }
1485
1486 // Checks that a cleanuppad instruction is mapped to an illegal value.
1487 TEST(IRInstructionMapper, CleanuppadIllegal) {
1488 StringRef ModuleString = R"(
1489 declare void @llvm.donothing() nounwind readnone
1490
1491 define void @function() personality i8 3 {
1492 entry:
1493 invoke void @llvm.donothing() to label %normal unwind label %exception
1494 exception:
1495 %cs1 = catchswitch within none [label %catchpad1] unwind to caller
1496 catchpad1:
1497 %clean = cleanuppad within none []
1498 br label %normal
1499 normal:
1500 ret void
1501 })";
1502 LLVMContext Context;
1503 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1504
1505 std::vector<IRInstructionData *> InstrList;
1506 std::vector<unsigned> UnsignedVec;
1507
1508 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1509 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1510 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1511 getVectors(*M, Mapper, InstrList, UnsignedVec);
1512
1513 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1514 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(1));
1515 ASSERT_GT(UnsignedVec[0], Mapper.IllegalInstrNumber);
1516 }
1517
1518 // The following three instructions are memory transfer and setting based, which
1519 // are considered illegal since is extra checking needed to handle the address
1520 // space checking.
1521
1522 // Checks that a memset instruction is mapped to an illegal value when
1523 // specified.
1524 TEST(IRInstructionMapper, MemSetIllegal) {
1525 StringRef ModuleString = R"(
1526 declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1527
1528 define i64 @function(i64 %x, i64 %z, i64 %n) {
1529 entry:
1530 %pool = alloca [59 x i64], align 4
1531 %tmp = bitcast [59 x i64]* %pool to i8*
1532 call void @llvm.memset.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1533 %cmp3 = icmp eq i64 %n, 0
1534 %a = add i64 %x, %z
1535 %c = add i64 %x, %z
1536 ret i64 0
1537 })";
1538 LLVMContext Context;
1539 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1540
1541 std::vector<IRInstructionData *> InstrList;
1542 std::vector<unsigned> UnsignedVec;
1543
1544 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1545 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1546 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1547 Mapper.InstClassifier.EnableIntrinsics = false;
1548 getVectors(*M, Mapper, InstrList, UnsignedVec);
1549
1550 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1551 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(7));
1552 ASSERT_TRUE(UnsignedVec[2] < UnsignedVec[0]);
1553 }
1554
1555 // Checks that a memcpy instruction is mapped to an illegal value when
1556 // specified.
1557 TEST(IRInstructionMapper, MemCpyIllegal) {
1558 StringRef ModuleString = R"(
1559 declare void @llvm.memcpy.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1560
1561 define i64 @function(i64 %x, i64 %z, i64 %n) {
1562 entry:
1563 %pool = alloca [59 x i64], align 4
1564 %tmp = bitcast [59 x i64]* %pool to i8*
1565 call void @llvm.memcpy.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1566 %cmp3 = icmp eq i64 %n, 0
1567 %a = add i64 %x, %z
1568 %c = add i64 %x, %z
1569 ret i64 0
1570 })";
1571 LLVMContext Context;
1572 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1573
1574 std::vector<IRInstructionData *> InstrList;
1575 std::vector<unsigned> UnsignedVec;
1576
1577 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1578 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1579 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1580 Mapper.InstClassifier.EnableIntrinsics = false;
1581 getVectors(*M, Mapper, InstrList, UnsignedVec);
1582
1583 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1584 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(7));
1585 ASSERT_GT(UnsignedVec[2], UnsignedVec[3]);
1586 ASSERT_LT(UnsignedVec[2], UnsignedVec[0]);
1587 }
1588
1589 // Checks that a memmove instruction is mapped to an illegal value when
1590 // specified.
1591 TEST(IRInstructionMapper, MemMoveIllegal) {
1592 StringRef ModuleString = R"(
1593 declare void @llvm.memmove.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1594
1595 define i64 @function(i64 %x, i64 %z, i64 %n) {
1596 entry:
1597 %pool = alloca [59 x i64], align 4
1598 %tmp = bitcast [59 x i64]* %pool to i8*
1599 call void @llvm.memmove.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1600 %cmp3 = icmp eq i64 %n, 0
1601 %a = add i64 %x, %z
1602 %c = add i64 %x, %z
1603 ret i64 0
1604 })";
1605 LLVMContext Context;
1606 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1607
1608 std::vector<IRInstructionData *> InstrList;
1609 std::vector<unsigned> UnsignedVec;
1610
1611 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1612 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1613 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1614 Mapper.InstClassifier.EnableIntrinsics = false;
1615 getVectors(*M, Mapper, InstrList, UnsignedVec);
1616
1617 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1618 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(7));
1619 ASSERT_LT(UnsignedVec[2], UnsignedVec[0]);
1620 }
1621
1622 // Checks that mem* instructions are mapped to an legal value when not
1623 // specified, and that all the intrinsics are marked differently.
1624 TEST(IRInstructionMapper, MemOpsLegal) {
1625 StringRef ModuleString = R"(
1626 declare void @llvm.memmove.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1627 declare void @llvm.memcpy.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1628 declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1)
1629
1630 define i64 @function(i64 %x, i64 %z, i64 %n) {
1631 entry:
1632 %pool = alloca [59 x i64], align 4
1633 %tmp = bitcast [59 x i64]* %pool to i8*
1634 call void @llvm.memmove.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1635 call void @llvm.memcpy.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1636 call void @llvm.memset.p0i8.i64(i8* nonnull %tmp, i8 0, i64 236, i32 4, i1 false)
1637 %cmp3 = icmp eq i64 %n, 0
1638 %a = add i64 %x, %z
1639 %c = add i64 %x, %z
1640 ret i64 0
1641 })";
1642 LLVMContext Context;
1643 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1644
1645 std::vector<IRInstructionData *> InstrList;
1646 std::vector<unsigned> UnsignedVec;
1647
1648 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1649 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1650 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1651 Mapper.InstClassifier.EnableIntrinsics = true;
1652 getVectors(*M, Mapper, InstrList, UnsignedVec);
1653
1654 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1655 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(9));
1656 ASSERT_LT(UnsignedVec[2], UnsignedVec[3]);
1657 ASSERT_LT(UnsignedVec[3], UnsignedVec[4]);
1658 ASSERT_LT(UnsignedVec[4], UnsignedVec[5]);
1659 }
1660
1661 // Checks that a variable argument instructions are mapped to an illegal value.
1662 // We exclude variable argument instructions since variable arguments
1663 // requires extra checking of the argument list.
1664 TEST(IRInstructionMapper, VarArgsIllegal) {
1665 StringRef ModuleString = R"(
1666 declare void @llvm.va_start(i8*)
1667 declare void @llvm.va_copy(i8*, i8*)
1668 declare void @llvm.va_end(i8*)
1669
1670 define i32 @func1(i32 %a, double %b, i8* %v, ...) nounwind {
1671 entry:
1672 %a.addr = alloca i32, align 4
1673 %b.addr = alloca double, align 8
1674 %ap = alloca i8*, align 4
1675 %c = alloca i32, align 4
1676 store i32 %a, i32* %a.addr, align 4
1677 store double %b, double* %b.addr, align 8
1678 %ap1 = bitcast i8** %ap to i8*
1679 call void @llvm.va_start(i8* %ap1)
1680 store double %b, double* %b.addr, align 8
1681 store double %b, double* %b.addr, align 8
1682 %0 = va_arg i8** %ap, i32
1683 store double %b, double* %b.addr, align 8
1684 store double %b, double* %b.addr, align 8
1685 call void @llvm.va_copy(i8* %v, i8* %ap1)
1686 store double %b, double* %b.addr, align 8
1687 store double %b, double* %b.addr, align 8
1688 call void @llvm.va_end(i8* %ap1)
1689 store i32 %0, i32* %c, align 4
1690 %tmp = load i32, i32* %c, align 4
1691 ret i32 %tmp
1692 })";
1693 LLVMContext Context;
1694 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1695
1696 std::vector<IRInstructionData *> InstrList;
1697 std::vector<unsigned> UnsignedVec;
1698
1699 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1700 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1701 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1702 Mapper.InstClassifier.EnableIntrinsics = false;
1703 getVectors(*M, Mapper, InstrList, UnsignedVec);
1704
1705 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
1706 ASSERT_EQ(UnsignedVec.size(), static_cast<unsigned>(17));
1707 ASSERT_TRUE(UnsignedVec[7] < UnsignedVec[0]);
1708 ASSERT_TRUE(UnsignedVec[13] < UnsignedVec[10]);
1709 ASSERT_TRUE(UnsignedVec[16] < UnsignedVec[13]);
1710 }
1711
1712 // Check the length of adding two illegal instructions one after th other. We
1713 // should find that only one element is added for each illegal range.
1714 TEST(IRInstructionMapper, RepeatedIllegalLength) {
1715 StringRef ModuleString = R"(
1716 define i32 @f(i32 %a, i32 %b) {
1717 bb0:
1718 %0 = add i32 %a, %b
1719 %1 = mul i32 %a, %b
1720 %2 = alloca i32
1721 %3 = alloca i32
1722 %4 = add i32 %a, %b
1723 %5 = mul i32 %a, %b
1724 ret i32 0
1725 })";
1726 LLVMContext Context;
1727 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1728
1729 std::vector<IRInstructionData *> InstrList;
1730 std::vector<unsigned> UnsignedVec;
1731
1732 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1733 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1734 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1735 getVectors(*M, Mapper, InstrList, UnsignedVec);
1736
1737 // Check that the size of the unsigned vector and the instruction list are the
1738 // same as a safety check.
1739 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1740
1741 // Make sure that the unsigned vector is the expected size.
1742 ASSERT_TRUE(UnsignedVec.size() == 6);
1743 }
1744
1745 // A helper function that accepts an instruction list from a module made up of
1746 // two blocks of two legal instructions and terminator, and checks them for
1747 // instruction similarity.
1748 static bool longSimCandCompare(std::vector<IRInstructionData *> &InstrList,
1749 bool Structure = false, unsigned Length = 2,
1750 unsigned StartIdxOne = 0,
1751 unsigned StartIdxTwo = 3) {
1752 std::vector<IRInstructionData *>::iterator Start, End;
1753
1754 Start = InstrList.begin();
1755 End = InstrList.begin();
1756
1757 std::advance(End, StartIdxOne + Length - 1);
1758 IRSimilarityCandidate Cand1(StartIdxOne, Length, *Start, *End);
1759
1760 Start = InstrList.begin();
1761 End = InstrList.begin();
1762
1763 std::advance(Start, StartIdxTwo);
1764 std::advance(End, StartIdxTwo + Length - 1);
1765 IRSimilarityCandidate Cand2(StartIdxTwo, Length, *Start, *End);
1766 if (Structure)
1767 return IRSimilarityCandidate::compareStructure(Cand1, Cand2);
1768 return IRSimilarityCandidate::isSimilar(Cand1, Cand2);
1769 }
1770
1771 // Checks that two adds with commuted operands are considered to be the same
1772 // instructions.
1773 TEST(IRSimilarityCandidate, CheckIdenticalInstructions) {
1774 StringRef ModuleString = R"(
1775 define i32 @f(i32 %a, i32 %b) {
1776 bb0:
1777 %0 = add i32 %a, %b
1778 %1 = add i32 %b, %a
1779 ret i32 0
1780 })";
1781 LLVMContext Context;
1782 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1783
1784 std::vector<IRInstructionData *> InstrList;
1785 std::vector<unsigned> UnsignedVec;
1786
1787 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1788 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1789 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1790 getVectors(*M, Mapper, InstrList, UnsignedVec);
1791
1792 // Check to make sure that we have a long enough region.
1793 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(3));
1794 // Check that the instructions were added correctly to both vectors.
1795 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1796
1797 std::vector<IRInstructionData *>::iterator Start, End;
1798 Start = InstrList.begin();
1799 End = InstrList.begin();
1800 std::advance(End, 1);
1801 IRSimilarityCandidate Cand1(0, 2, *Start, *End);
1802 IRSimilarityCandidate Cand2(0, 2, *Start, *End);
1803
1804 ASSERT_TRUE(IRSimilarityCandidate::isSimilar(Cand1, Cand2));
1805 }
1806
1807 // Checks that comparison instructions are found to be similar instructions
1808 // when the operands are flipped and the predicate is also swapped.
1809 TEST(IRSimilarityCandidate, PredicateIsomorphism) {
1810 StringRef ModuleString = R"(
1811 define i32 @f(i32 %a, i32 %b) {
1812 bb0:
1813 %0 = icmp sgt i32 %a, %b
1814 %1 = add i32 %b, %a
1815 br label %bb1
1816 bb1:
1817 %2 = icmp slt i32 %a, %b
1818 %3 = add i32 %a, %b
1819 ret i32 0
1820 })";
1821 LLVMContext Context;
1822 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1823
1824 std::vector<IRInstructionData *> InstrList;
1825 std::vector<unsigned> UnsignedVec;
1826
1827 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1828 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1829 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1830 getVectors(*M, Mapper, InstrList, UnsignedVec);
1831
1832 ASSERT_TRUE(InstrList.size() > 5);
1833 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1834
1835 std::vector<IRInstructionData *>::iterator Start, End;
1836 Start = InstrList.begin();
1837 End = InstrList.begin();
1838
1839 std::advance(End, 1);
1840 IRSimilarityCandidate Cand1(0, 2, *Start, *End);
1841
1842 Start = InstrList.begin();
1843 End = InstrList.begin();
1844
1845 std::advance(Start, 3);
1846 std::advance(End, 4);
1847 IRSimilarityCandidate Cand2(3, 2, *Start, *End);
1848
1849 ASSERT_TRUE(IRSimilarityCandidate::isSimilar(Cand1, Cand2));
1850 }
1851
1852 // Checks that IRSimilarityCandidates wrapping these two regions of instructions
1853 // are able to differentiate between instructions that have different opcodes.
1854 TEST(IRSimilarityCandidate, CheckRegionsDifferentInstruction) {
1855 StringRef ModuleString = R"(
1856 define i32 @f(i32 %a, i32 %b) {
1857 bb0:
1858 %0 = add i32 %a, %b
1859 %1 = add i32 %b, %a
1860 ret i32 0
1861 bb1:
1862 %2 = sub i32 %a, %b
1863 %3 = add i32 %b, %a
1864 ret i32 0
1865 })";
1866 LLVMContext Context;
1867 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1868
1869 std::vector<IRInstructionData *> InstrList;
1870 std::vector<unsigned> UnsignedVec;
1871
1872 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1873 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1874 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1875 getVectors(*M, Mapper, InstrList, UnsignedVec);
1876
1877 // Check to make sure that we have a long enough region.
1878 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
1879 // Check that the instructions were added correctly to both vectors.
1880 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1881
1882 ASSERT_FALSE(longSimCandCompare(InstrList));
1883 }
1884
1885 // Checks that IRSimilarityCandidates wrapping these two regions of instructions
1886 // are able to differentiate between instructions that have different types.
1887 TEST(IRSimilarityCandidate, CheckRegionsDifferentTypes) {
1888 StringRef ModuleString = R"(
1889 define i32 @f(i32 %a, i32 %b, i64 %c, i64 %d) {
1890 bb0:
1891 %0 = add i32 %a, %b
1892 %1 = add i32 %b, %a
1893 ret i32 0
1894 bb1:
1895 %2 = add i64 %c, %d
1896 %3 = add i64 %d, %c
1897 ret i32 0
1898 })";
1899 LLVMContext Context;
1900 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1901
1902 std::vector<IRInstructionData *> InstrList;
1903 std::vector<unsigned> UnsignedVec;
1904
1905 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1906 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1907 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1908 getVectors(*M, Mapper, InstrList, UnsignedVec);
1909
1910 // Check to make sure that we have a long enough region.
1911 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
1912 // Check that the instructions were added correctly to both vectors.
1913 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1914
1915 ASSERT_FALSE(longSimCandCompare(InstrList));
1916 }
1917
1918 // Check that debug records do not impact similarity. They are marked as
1919 // invisible.
1920 TEST(IRSimilarityCandidate, IdenticalWithDebug) {
1921 StringRef ModuleString = R"(
1922 define i32 @f(i32 %a, i32 %b) {
1923 bb0:
1924 %0 = add i32 %a, %b
1925 #dbg_value(i32 0, !0, !0, !0)
1926 %1 = add i32 %b, %a
1927 ret i32 0
1928 bb1:
1929 %2 = add i32 %a, %b
1930 #dbg_value(i32 1, !1, !1, !1)
1931 %3 = add i32 %b, %a
1932 ret i32 0
1933 bb2:
1934 %4 = add i32 %a, %b
1935 %5 = add i32 %b, %a
1936 ret i32 0
1937 }
1938
1939 !0 = distinct !{!"test\00", i32 10}
1940 !1 = distinct !{!"test\00", i32 11})";
1941 LLVMContext Context;
1942 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1943
1944 std::vector<IRInstructionData *> InstrList;
1945 std::vector<unsigned> UnsignedVec;
1946
1947 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1948 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1949 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1950 getVectors(*M, Mapper, InstrList, UnsignedVec);
1951
1952 // Check to make sure that we have a long enough region.
1953 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(9));
1954 // Check that the instructions were added correctly to both vectors.
1955 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1956
1957 ASSERT_TRUE(longSimCandCompare(InstrList));
1958 }
1959
1960 // Checks that IRSimilarityCandidates that include illegal instructions, are not
1961 // considered to be the same set of instructions. In these sets of instructions
1962 // the allocas are illegal.
1963 TEST(IRSimilarityCandidate, IllegalInCandidate) {
1964 StringRef ModuleString = R"(
1965 define i32 @f(i32 %a, i32 %b) {
1966 bb0:
1967 %0 = add i32 %a, %b
1968 %1 = add i32 %a, %b
1969 %2 = alloca i32
1970 ret i32 0
1971 bb1:
1972 %3 = add i32 %a, %b
1973 %4 = add i32 %a, %b
1974 %5 = alloca i32
1975 ret i32 0
1976 })";
1977 LLVMContext Context;
1978 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
1979
1980 std::vector<IRInstructionData *> InstrList;
1981 std::vector<unsigned> UnsignedVec;
1982
1983 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
1984 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
1985 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
1986 getVectors(*M, Mapper, InstrList, UnsignedVec);
1987
1988 // Check to make sure that we have a long enough region.
1989 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
1990 // Check that the instructions were added correctly to both vectors.
1991 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
1992
1993 std::vector<IRInstructionData *>::iterator Start, End;
1994
1995 Start = InstrList.begin();
1996 End = InstrList.begin();
1997
1998 std::advance(End, 2);
1999 IRSimilarityCandidate Cand1(0, 3, *Start, *End);
2000
2001 Start = InstrList.begin();
2002 End = InstrList.begin();
2003
2004 std::advance(Start, 3);
2005 std::advance(End, 5);
2006 IRSimilarityCandidate Cand2(3, 3, *Start, *End);
2007 ASSERT_FALSE(IRSimilarityCandidate::isSimilar(Cand1, Cand2));
2008 }
2009
2010 // Checks that different structure, in this case, where we introduce a new
2011 // needed input in one region, is recognized as different.
2012 TEST(IRSimilarityCandidate, DifferentStructure) {
2013 StringRef ModuleString = R"(
2014 define i32 @f(i32 %a, i32 %b) {
2015 bb0:
2016 %0 = add i32 %a, %b
2017 %1 = add i32 %b, %a
2018 ret i32 0
2019 bb1:
2020 %2 = add i32 %a, %b
2021 %3 = add i32 %b, %0
2022 ret i32 0
2023 })";
2024 LLVMContext Context;
2025 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2026
2027 std::vector<IRInstructionData *> InstrList;
2028 std::vector<unsigned> UnsignedVec;
2029
2030 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2031 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2032 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2033 getVectors(*M, Mapper, InstrList, UnsignedVec);
2034
2035 // Check to make sure that we have a long enough region.
2036 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
2037 // Check that the instructions were added correctly to both vectors.
2038 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2039
2040 ASSERT_FALSE(longSimCandCompare(InstrList, true));
2041 }
2042
2043 // Checks that comparison instructions are found to have the same structure
2044 // when the operands are flipped and the predicate is also swapped.
2045 TEST(IRSimilarityCandidate, PredicateIsomorphismStructure) {
2046 StringRef ModuleString = R"(
2047 define i32 @f(i32 %a, i32 %b) {
2048 bb0:
2049 %0 = icmp sgt i32 %a, %b
2050 %1 = add i32 %a, %b
2051 br label %bb1
2052 bb1:
2053 %2 = icmp slt i32 %b, %a
2054 %3 = add i32 %a, %b
2055 ret i32 0
2056 })";
2057 LLVMContext Context;
2058 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2059
2060 std::vector<IRInstructionData *> InstrList;
2061 std::vector<unsigned> UnsignedVec;
2062
2063 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2064 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2065 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2066 getVectors(*M, Mapper, InstrList, UnsignedVec);
2067
2068 ASSERT_TRUE(InstrList.size() > 5);
2069 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2070
2071 ASSERT_TRUE(longSimCandCompare(InstrList, true));
2072 }
2073
2074 // Checks that different predicates are counted as diferent.
2075 TEST(IRSimilarityCandidate, PredicateDifference) {
2076 StringRef ModuleString = R"(
2077 define i32 @f(i32 %a, i32 %b) {
2078 bb0:
2079 %0 = icmp sge i32 %a, %b
2080 %1 = add i32 %b, %a
2081 br label %bb1
2082 bb1:
2083 %2 = icmp slt i32 %b, %a
2084 %3 = add i32 %a, %b
2085 ret i32 0
2086 })";
2087 LLVMContext Context;
2088 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2089
2090 std::vector<IRInstructionData *> InstrList;
2091 std::vector<unsigned> UnsignedVec;
2092
2093 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2094 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2095 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2096 getVectors(*M, Mapper, InstrList, UnsignedVec);
2097
2098 ASSERT_TRUE(InstrList.size() > 5);
2099 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2100
2101 ASSERT_FALSE(longSimCandCompare(InstrList));
2102 }
2103
2104 // Checks that the same structure is recognized between two candidates. The
2105 // items %a and %b are used in the same way in both sets of instructions.
2106 TEST(IRSimilarityCandidate, SameStructure) {
2107 StringRef ModuleString = R"(
2108 define i32 @f(i32 %a, i32 %b) {
2109 bb0:
2110 %0 = add i32 %a, %b
2111 %1 = sub i32 %b, %a
2112 ret i32 0
2113 bb1:
2114 %2 = add i32 %a, %b
2115 %3 = sub i32 %b, %a
2116 ret i32 0
2117 })";
2118 LLVMContext Context;
2119 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2120
2121 std::vector<IRInstructionData *> InstrList;
2122 std::vector<unsigned> UnsignedVec;
2123
2124 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2125 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2126 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2127 getVectors(*M, Mapper, InstrList, UnsignedVec);
2128
2129 // Check to make sure that we have a long enough region.
2130 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
2131 // Check that the instructions were added correctly to both vectors.
2132 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2133
2134 ASSERT_TRUE(longSimCandCompare(InstrList, true));
2135 }
2136
2137 // Checks that the canonical numbering between two candidates matches the found
2138 // mapping between two candidates.
2139 TEST(IRSimilarityCandidate, CanonicalNumbering) {
2140 StringRef ModuleString = R"(
2141 define i32 @f(i32 %a, i32 %b) {
2142 bb0:
2143 %0 = add i32 %a, %b
2144 %1 = sub i32 %b, %a
2145 ret i32 0
2146 bb1:
2147 %2 = add i32 %a, %b
2148 %3 = sub i32 %b, %a
2149 ret i32 0
2150 })";
2151 LLVMContext Context;
2152 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2153
2154 std::vector<IRInstructionData *> InstrList;
2155 std::vector<unsigned> UnsignedVec;
2156
2157 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2158 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2159 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2160 getVectors(*M, Mapper, InstrList, UnsignedVec);
2161
2162 // Check to make sure that we have a long enough region.
2163 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
2164 // Check that the instructions were added correctly to both vectors.
2165 ASSERT_EQ(InstrList.size(), UnsignedVec.size());
2166
2167 std::vector<IRInstructionData *>::iterator Start, End;
2168
2169 Start = InstrList.begin();
2170 End = InstrList.begin();
2171
2172 std::advance(End, 1);
2173 IRSimilarityCandidate Cand1(0, 2, *Start, *End);
2174
2175 Start = InstrList.begin();
2176 End = InstrList.begin();
2177
2178 std::advance(Start, 3);
2179 std::advance(End, 4);
2180 IRSimilarityCandidate Cand2(3, 2, *Start, *End);
2181 DenseMap<unsigned, DenseSet<unsigned>> Mapping1;
2182 DenseMap<unsigned, DenseSet<unsigned>> Mapping2;
2183 ASSERT_TRUE(IRSimilarityCandidate::compareStructure(Cand1, Cand2, Mapping1,
2184 Mapping2));
2185 IRSimilarityCandidate::createCanonicalMappingFor(Cand1);
2186 Cand2.createCanonicalRelationFrom(Cand1, Mapping1, Mapping2);
2187
2188 for (std::pair<unsigned, DenseSet<unsigned>> &P : Mapping2) {
2189 unsigned Source = P.first;
2190
2191 ASSERT_TRUE(Cand2.getCanonicalNum(Source).has_value());
2192 unsigned Canon = *Cand2.getCanonicalNum(Source);
2193 ASSERT_TRUE(Cand1.fromCanonicalNum(Canon).has_value());
2194 unsigned Dest = *Cand1.fromCanonicalNum(Canon);
2195
2196 DenseSet<unsigned>::iterator It = P.second.find(Dest);
2197 ASSERT_NE(It, P.second.end());
2198 }
2199 }
2200
2201 // Checks that the same structure is recognized between two candidates. While
2202 // the input names are reversed, they still perform the same overall operation.
2203 TEST(IRSimilarityCandidate, DifferentNameSameStructure) {
2204 StringRef ModuleString = R"(
2205 define i32 @f(i32 %a, i32 %b) {
2206 bb0:
2207 %0 = add i32 %a, %b
2208 %1 = add i32 %b, %a
2209 ret i32 0
2210 bb1:
2211 %2 = add i32 %b, %a
2212 %3 = add i32 %a, %b
2213 ret i32 0
2214 })";
2215 LLVMContext Context;
2216 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2217
2218 std::vector<IRInstructionData *> InstrList;
2219 std::vector<unsigned> UnsignedVec;
2220
2221 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2222 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2223 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2224 getVectors(*M, Mapper, InstrList, UnsignedVec);
2225
2226 // Check to make sure that we have a long enough region.
2227 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(6));
2228 // Check that the instructions were added correctly to both vectors.
2229 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2230
2231 ASSERT_TRUE(longSimCandCompare(InstrList, true));
2232 }
2233
2234 // Checks that the same structure is recognized between two candidates when
2235 // the branches target other blocks inside the same region, the relative
2236 // distance between the blocks must be the same.
2237 TEST(IRSimilarityCandidate, SameBranchStructureInternal) {
2238 StringRef ModuleString = R"(
2239 define i32 @f(i32 %a, i32 %b) {
2240 bb0:
2241 %0 = add i32 %a, %b
2242 %1 = add i32 %b, %a
2243 br label %bb1
2244 bb1:
2245 %2 = add i32 %b, %a
2246 %3 = add i32 %a, %b
2247 ret i32 0
2248 }
2249
2250 define i32 @f2(i32 %a, i32 %b) {
2251 bb0:
2252 %0 = add i32 %a, %b
2253 %1 = add i32 %b, %a
2254 br label %bb1
2255 bb1:
2256 %2 = add i32 %b, %a
2257 %3 = add i32 %a, %b
2258 ret i32 0
2259 })";
2260 LLVMContext Context;
2261 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2262
2263 std::vector<IRInstructionData *> InstrList;
2264 std::vector<unsigned> UnsignedVec;
2265
2266 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2267 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2268 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2269 Mapper.InstClassifier.EnableBranches = true;
2270 Mapper.initializeForBBs(*M);
2271 getVectors(*M, Mapper, InstrList, UnsignedVec);
2272
2273 // Check to make sure that we have a long enough region.
2274 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(12));
2275 // Check that the instructions were added correctly to both vectors.
2276 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2277
2278 ASSERT_TRUE(longSimCandCompare(InstrList, true, 5, 0, 6));
2279 }
2280
2281 // Checks that the different structure is recognized between two candidates,
2282 // when the branches target other blocks inside the same region, the relative
2283 // distance between the blocks must be the same.
2284 TEST(IRSimilarityCandidate, DifferentBranchStructureInternal) {
2285 StringRef ModuleString = R"(
2286 define i32 @f(i32 %a, i32 %b) {
2287 bb0:
2288 %0 = add i32 %a, %b
2289 %1 = add i32 %b, %a
2290 br label %bb2
2291 bb1:
2292 %2 = add i32 %b, %a
2293 %3 = add i32 %a, %b
2294 br label %bb2
2295 bb2:
2296 %4 = add i32 %b, %a
2297 %5 = add i32 %a, %b
2298 ret i32 0
2299 }
2300
2301 define i32 @f2(i32 %a, i32 %b) {
2302 bb0:
2303 %0 = add i32 %a, %b
2304 %1 = add i32 %b, %a
2305 br label %bb1
2306 bb1:
2307 %2 = add i32 %b, %a
2308 %3 = add i32 %a, %b
2309 br label %bb2
2310 bb2:
2311 %4 = add i32 %b, %a
2312 %5 = add i32 %a, %b
2313 ret i32 0
2314 })";
2315 LLVMContext Context;
2316 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2317
2318 std::vector<IRInstructionData *> InstrList;
2319 std::vector<unsigned> UnsignedVec;
2320
2321 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2322 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2323 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2324 Mapper.InstClassifier.EnableBranches = true;
2325 Mapper.initializeForBBs(*M);
2326 getVectors(*M, Mapper, InstrList, UnsignedVec);
2327
2328 // Check to make sure that we have a long enough region.
2329 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(18));
2330 // Check that the instructions were added correctly to both vectors.
2331 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2332
2333 ASSERT_FALSE(longSimCandCompare(InstrList, true, 6, 0, 9));
2334 }
2335
2336 // Checks that the same structure is recognized between two candidates, when
2337 // the branches target other blocks outside region, the relative distance
2338 // does not need to be the same.
2339 TEST(IRSimilarityCandidate, SameBranchStructureOutside) {
2340 StringRef ModuleString = R"(
2341 define i32 @f(i32 %a, i32 %b) {
2342 bb0:
2343 %0 = add i32 %a, %b
2344 %1 = add i32 %b, %a
2345 br label %bb1
2346 bb1:
2347 %2 = add i32 %b, %a
2348 %3 = add i32 %a, %b
2349 ret i32 0
2350 }
2351
2352 define i32 @f2(i32 %a, i32 %b) {
2353 bb0:
2354 %0 = add i32 %a, %b
2355 %1 = add i32 %b, %a
2356 br label %bb1
2357 bb1:
2358 %2 = add i32 %b, %a
2359 %3 = add i32 %a, %b
2360 ret i32 0
2361 })";
2362 LLVMContext Context;
2363 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2364
2365 std::vector<IRInstructionData *> InstrList;
2366 std::vector<unsigned> UnsignedVec;
2367
2368 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2369 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2370 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2371 Mapper.InstClassifier.EnableBranches = true;
2372 Mapper.initializeForBBs(*M);
2373 getVectors(*M, Mapper, InstrList, UnsignedVec);
2374
2375 // Check to make sure that we have a long enough region.
2376 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(12));
2377 // Check that the instructions were added correctly to both vectors.
2378 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2379
2380 ASSERT_TRUE(longSimCandCompare(InstrList, true, 3, 0, 6));
2381 }
2382
2383 // Checks that the same structure is recognized between two candidates, when
2384 // the branches target other blocks outside region, the relative distance
2385 // does not need to be the same.
2386 TEST(IRSimilarityCandidate, DifferentBranchStructureOutside) {
2387 StringRef ModuleString = R"(
2388 define i32 @f(i32 %a, i32 %b) {
2389 bb0:
2390 %0 = add i32 %a, %b
2391 %1 = add i32 %b, %a
2392 br label %bb1
2393 bb1:
2394 %2 = add i32 %b, %a
2395 %3 = add i32 %a, %b
2396 ret i32 0
2397 }
2398
2399 define i32 @f2(i32 %a, i32 %b) {
2400 bb0:
2401 %0 = add i32 %a, %b
2402 %1 = add i32 %b, %a
2403 br label %bb2
2404 bb1:
2405 %2 = add i32 %b, %a
2406 %3 = add i32 %a, %b
2407 br label %bb2
2408 bb2:
2409 %4 = add i32 %b, %a
2410 %5 = add i32 %a, %b
2411 ret i32 0
2412 })";
2413 LLVMContext Context;
2414 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2415
2416 std::vector<IRInstructionData *> InstrList;
2417 std::vector<unsigned> UnsignedVec;
2418
2419 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2420 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2421 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2422 Mapper.InstClassifier.EnableBranches = true;
2423 Mapper.initializeForBBs(*M);
2424 getVectors(*M, Mapper, InstrList, UnsignedVec);
2425
2426 // Check to make sure that we have a long enough region.
2427 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(15));
2428 // Check that the instructions were added correctly to both vectors.
2429 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2430
2431 ASSERT_TRUE(longSimCandCompare(InstrList, true, 3, 0, 6));
2432 }
2433
2434 // Checks that the same structure is recognized between two candidates,
2435 // when the phi predecessor are other blocks inside the same region,
2436 // the relative distance between the blocks must be the same.
2437 TEST(IRSimilarityCandidate, SamePHIStructureInternal) {
2438 StringRef ModuleString = R"(
2439 define i32 @f(i32 %a, i32 %b) {
2440 bb0:
2441 br label %bb2
2442 bb1:
2443 br label %bb2
2444 bb2:
2445 %0 = phi i32 [ %a, %bb0 ], [ %b, %bb1 ]
2446 %1 = add i32 %b, %a
2447 %2 = add i32 %a, %b
2448 ret i32 0
2449 }
2450
2451 define i32 @f2(i32 %a, i32 %b) {
2452 bb0:
2453 br label %bb2
2454 bb1:
2455 br label %bb2
2456 bb2:
2457 %0 = phi i32 [ %a, %bb0 ], [ %b, %bb1 ]
2458 %1 = add i32 %b, %a
2459 %2 = add i32 %a, %b
2460 ret i32 0
2461 })";
2462 LLVMContext Context;
2463 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2464
2465 std::vector<IRInstructionData *> InstrList;
2466 std::vector<unsigned> UnsignedVec;
2467
2468 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2469 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2470 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2471 Mapper.InstClassifier.EnableBranches = true;
2472 Mapper.initializeForBBs(*M);
2473 getVectors(*M, Mapper, InstrList, UnsignedVec);
2474
2475 // Check to make sure that we have a long enough region.
2476 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(12));
2477 // Check that the instructions were added correctly to both vectors.
2478 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2479
2480 ASSERT_TRUE(longSimCandCompare(InstrList, true, 4, 0, 6));
2481 }
2482
2483 // Checks that the different structure is recognized between two candidates,
2484 // when the phi predecessor are other blocks inside the same region,
2485 // the relative distance between the blocks must be the same.
2486 TEST(IRSimilarityCandidate, DifferentPHIStructureInternal) {
2487 StringRef ModuleString = R"(
2488 define i32 @f(i32 %a, i32 %b) {
2489 bb0:
2490 br label %bb2
2491 bb1:
2492 br label %bb2
2493 bb3:
2494 br label %bb2
2495 bb2:
2496 %0 = phi i32 [ %a, %bb0 ], [ %b, %bb1 ]
2497 %1 = add i32 %b, %a
2498 %2 = add i32 %a, %b
2499 ret i32 0
2500 }
2501
2502 define i32 @f2(i32 %a, i32 %b) {
2503 bb0:
2504 br label %bb2
2505 bb1:
2506 br label %bb2
2507 bb3:
2508 br label %bb2
2509 bb2:
2510 %0 = phi i32 [ %a, %bb0 ], [ %b, %bb3 ]
2511 %1 = add i32 %b, %a
2512 %2 = add i32 %a, %b
2513 ret i32 0
2514 })";
2515 LLVMContext Context;
2516 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2517
2518 std::vector<IRInstructionData *> InstrList;
2519 std::vector<unsigned> UnsignedVec;
2520
2521 SpecificBumpPtrAllocator<IRInstructionData> InstDataAllocator;
2522 SpecificBumpPtrAllocator<IRInstructionDataList> IDLAllocator;
2523 IRInstructionMapper Mapper(&InstDataAllocator, &IDLAllocator);
2524 Mapper.InstClassifier.EnableBranches = true;
2525 Mapper.initializeForBBs(*M);
2526 getVectors(*M, Mapper, InstrList, UnsignedVec);
2527
2528 // Check to make sure that we have a long enough region.
2529 ASSERT_EQ(InstrList.size(), static_cast<unsigned>(14));
2530 // Check that the instructions were added correctly to both vectors.
2531 ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
2532
2533 ASSERT_FALSE(longSimCandCompare(InstrList, true, 5, 0, 7));
2534 }
2535
2536 // Checks that two sets of identical instructions are found to be the same.
2537 // Both sequences of adds have the same operand ordering, and the same
2538 // instructions, making them strcturally equivalent.
2539 TEST(IRSimilarityIdentifier, IdentitySimilarity) {
2540 StringRef ModuleString = R"(
2541 define i32 @f(i32 %a, i32 %b) {
2542 bb0:
2543 %0 = add i32 %a, %b
2544 %1 = sub i32 %b, %a
2545 br label %bb1
2546 bb1:
2547 %2 = add i32 %a, %b
2548 %3 = sub i32 %b, %a
2549 ret i32 0
2550 })";
2551 LLVMContext Context;
2552 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2553
2554 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2555 getSimilarities(*M, SimilarityCandidates);
2556
2557 ASSERT_TRUE(SimilarityCandidates.size() == 1);
2558 for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
2559 ASSERT_TRUE(Cands.size() == 2);
2560 unsigned InstIdx = 0;
2561 for (IRSimilarityCandidate &Cand : Cands) {
2562 ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
2563 InstIdx += 3;
2564 }
2565 }
2566 }
2567
2568 // Checks that incorrect sequences are not found as similar. In this case,
2569 // we have different sequences of instructions.
2570 TEST(IRSimilarityIdentifier, InstructionDifference) {
2571 StringRef ModuleString = R"(
2572 define i32 @f(i32 %a, i32 %b, i32 %c, i32 %d) {
2573 bb0:
2574 %0 = sub i32 %a, %b
2575 %1 = add i32 %b, %a
2576 br label %bb1
2577 bb1:
2578 %2 = add i32 %c, %d
2579 %3 = sub i32 %d, %c
2580 ret i32 0
2581 })";
2582 LLVMContext Context;
2583 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2584
2585 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2586 getSimilarities(*M, SimilarityCandidates);
2587
2588 ASSERT_TRUE(SimilarityCandidates.empty());
2589 }
2590
2591 // This test checks to see whether we can detect similarity for commutative
2592 // instructions where the operands have been reversed.
2593 TEST(IRSimilarityIdentifier, CommutativeSimilarity) {
2594 StringRef ModuleString = R"(
2595 define i32 @f(i32 %a, i32 %b) {
2596 bb0:
2597 %0 = add i32 %a, %b
2598 %1 = add i32 %b, %a
2599 br label %bb1
2600 bb1:
2601 %2 = add i32 %a, %b
2602 %3 = add i32 %a, %b
2603 ret i32 0
2604 })";
2605 LLVMContext Context;
2606 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2607
2608 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2609 getSimilarities(*M, SimilarityCandidates);
2610
2611 ASSERT_TRUE(SimilarityCandidates.size() == 1);
2612 for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
2613 ASSERT_TRUE(Cands.size() == 2);
2614 unsigned InstIdx = 0;
2615 for (IRSimilarityCandidate &Cand : Cands) {
2616 ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
2617 InstIdx += 3;
2618 }
2619 }
2620 }
2621
2622 // This test ensures that when the first instruction in a sequence is
2623 // a commutative instruction with the same value (mcomm_inst_same_val), but the
2624 // corresponding instruction (comm_inst_diff_val) is not, we mark the regions
2625 // and not similar.
2626 TEST(IRSimilarityIdentifier, CommutativeSameValueFirstMisMatch) {
2627 StringRef ModuleString = R"(
2628 define void @v_1_0(i64 %v_33) {
2629 entry:
2630 %comm_inst_same_val = mul i64 undef, undef
2631 %add = add i64 %comm_inst_same_val, %v_33
2632 %comm_inst_diff_val = mul i64 0, undef
2633 %mul.i = add i64 %comm_inst_diff_val, %comm_inst_diff_val
2634 unreachable
2635 })";
2636 LLVMContext Context;
2637 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2638
2639 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2640 getSimilarities(*M, SimilarityCandidates);
2641
2642 ASSERT_TRUE(SimilarityCandidates.size() == 0);
2643 }
2644
2645 // This test makes sure that intrinsic functions that are marked commutative
2646 // are still treated as non-commutative since they are function calls.
2647 TEST(IRSimilarityIdentifier, IntrinsicCommutative) {
2648 // If treated as commutative, we will fail to find a valid mapping, causing
2649 // an assertion error.
2650 StringRef ModuleString = R"(
2651 define void @foo() {
2652 entry:
2653 %0 = call i16 @llvm.smul.fix.i16(i16 16384, i16 16384, i32 15)
2654 store i16 %0, i16* undef, align 1
2655 %1 = icmp eq i16 undef, 8192
2656 call void @bar()
2657 %2 = call i16 @llvm.smul.fix.i16(i16 -16384, i16 16384, i32 15)
2658 store i16 %2, i16* undef, align 1
2659 %3 = icmp eq i16 undef, -8192
2660 call void @bar()
2661 %4 = call i16 @llvm.smul.fix.i16(i16 -16384, i16 -16384, i32 15)
2662 ret void
2663 }
2664
2665 declare void @bar()
2666
2667 ; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
2668 declare i16 @llvm.smul.fix.i16(i16, i16, i32 immarg))";
2669 LLVMContext Context;
2670 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2671
2672 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2673 getSimilarities(*M, SimilarityCandidates);
2674
2675 ASSERT_TRUE(SimilarityCandidates.size() == 0);
2676 }
2677
2678 // This test checks to see whether we can detect different structure in
2679 // commutative instructions. In this case, the second operand in the second
2680 // add is different.
2681 TEST(IRSimilarityIdentifier, NoCommutativeSimilarity) {
2682 StringRef ModuleString = R"(
2683 define i32 @f(i32 %a, i32 %b) {
2684 bb0:
2685 %0 = add i32 %a, %b
2686 %1 = add i32 %1, %b
2687 br label %bb1
2688 bb1:
2689 %2 = add i32 %a, %b
2690 %3 = add i32 %2, %a
2691 ret i32 0
2692 })";
2693 LLVMContext Context;
2694 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2695
2696 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2697 getSimilarities(*M, SimilarityCandidates);
2698
2699 ASSERT_TRUE(SimilarityCandidates.size() == 0);
2700 }
2701
2702 // Check that we are not finding similarity in non commutative
2703 // instructions. That is, while the instruction and operands used are the same
2704 // in the two subtraction sequences, they are in a different order, and cannot
2705 // be counted as the same since a subtraction is not commutative.
2706 TEST(IRSimilarityIdentifier, NonCommutativeDifference) {
2707 StringRef ModuleString = R"(
2708 define i32 @f(i32 %a, i32 %b) {
2709 bb0:
2710 %0 = sub i32 %a, %b
2711 %1 = sub i32 %b, %a
2712 br label %bb1
2713 bb1:
2714 %2 = sub i32 %a, %b
2715 %3 = sub i32 %a, %b
2716 ret i32 0
2717 })";
2718 LLVMContext Context;
2719 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2720
2721 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2722 getSimilarities(*M, SimilarityCandidates);
2723
2724 ASSERT_TRUE(SimilarityCandidates.empty());
2725 }
2726
2727 // Check that we find similarity despite changing the register names.
2728 TEST(IRSimilarityIdentifier, MappingSimilarity) {
2729 StringRef ModuleString = R"(
2730 define i32 @f(i32 %a, i32 %b, i32 %c, i32 %d) {
2731 bb0:
2732 %0 = add i32 %a, %b
2733 %1 = sub i32 %b, %a
2734 br label %bb1
2735 bb1:
2736 %2 = add i32 %c, %d
2737 %3 = sub i32 %d, %c
2738 ret i32 0
2739 })";
2740 LLVMContext Context;
2741 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2742
2743 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2744 getSimilarities(*M, SimilarityCandidates);
2745
2746 ASSERT_TRUE(SimilarityCandidates.size() == 1);
2747 for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
2748 ASSERT_TRUE(Cands.size() == 2);
2749 unsigned InstIdx = 0;
2750 for (IRSimilarityCandidate &Cand : Cands) {
2751 ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
2752 InstIdx += 3;
2753 }
2754 }
2755 }
2756
2757 // Check that we find instances of swapped predicate isomorphism. That is,
2758 // for predicates that can be flipped, e.g. greater than to less than,
2759 // we can identify that instances of these different literal predicates, but are
2760 // the same within a single swap can be found.
2761 TEST(IRSimilarityIdentifier, PredicateIsomorphism) {
2762 StringRef ModuleString = R"(
2763 define i32 @f(i32 %a, i32 %b) {
2764 bb0:
2765 %0 = add i32 %a, %b
2766 %1 = icmp sgt i32 %b, %a
2767 br label %bb1
2768 bb1:
2769 %2 = add i32 %a, %b
2770 %3 = icmp slt i32 %a, %b
2771 ret i32 0
2772 })";
2773 LLVMContext Context;
2774 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2775
2776 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2777 getSimilarities(*M, SimilarityCandidates);
2778
2779 ASSERT_TRUE(SimilarityCandidates.size() == 1);
2780 for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
2781 ASSERT_TRUE(Cands.size() == 2);
2782 unsigned InstIdx = 0;
2783 for (IRSimilarityCandidate &Cand : Cands) {
2784 ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
2785 InstIdx += 3;
2786 }
2787 }
2788 }
2789
2790 // Checks that constants are detected as the same operand in each use in the
2791 // sequences of instructions. Also checks that we can find structural
2792 // equivalence using constants. In this case the 1 has the same use pattern as
2793 // %a.
2794 TEST(IRSimilarityIdentifier, ConstantMappingSimilarity) {
2795 StringRef ModuleString = R"(
2796 define i32 @f(i32 %a, i32 %b) {
2797 bb0:
2798 %0 = add i32 1, %b
2799 %1 = icmp sgt i32 %b, 1
2800 br label %bb1
2801 bb1:
2802 %2 = add i32 %a, %b
2803 %3 = icmp sgt i32 %b, %a
2804 ret i32 0
2805 })";
2806 LLVMContext Context;
2807 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2808
2809 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2810 getSimilarities(*M, SimilarityCandidates);
2811
2812 ASSERT_TRUE(SimilarityCandidates.size() == 1);
2813 for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
2814 ASSERT_TRUE(Cands.size() == 2);
2815 unsigned InstIdx = 0;
2816 for (IRSimilarityCandidate &Cand : Cands) {
2817 ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
2818 InstIdx += 3;
2819 }
2820 }
2821 }
2822
2823 // Check that constants are uniquely identified. i.e. two different constants
2824 // are not considered the same. This means that this should not find any
2825 // structural similarity.
2826 TEST(IRSimilarityIdentifier, ConstantMappingDifference) {
2827 StringRef ModuleString = R"(
2828 define i32 @f(i32 %a, i32 %b) {
2829 bb0:
2830 %0 = add i32 1, %b
2831 %1 = icmp sgt i32 %b, 2
2832 br label %bb1
2833 bb1:
2834 %2 = add i32 %a, %b
2835 %3 = icmp slt i32 %a, %b
2836 ret i32 0
2837 })";
2838 LLVMContext Context;
2839 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);
2840
2841 std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
2842 getSimilarities(*M, SimilarityCandidates);
2843
2844 ASSERT_TRUE(SimilarityCandidates.empty());
2845 }