blob: f7d715c64471e102d31525263eb25a1a361fbe1c [file] [log] [blame]
James Molloy134bec22015-08-11 09:12:57 +00001//===- ValueTrackingTest.cpp - ValueTracking tests ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/ValueTracking.h"
11#include "llvm/AsmParser/Parser.h"
12#include "llvm/IR/Function.h"
13#include "llvm/IR/InstIterator.h"
14#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/SourceMgr.h"
Simon Dardis70dbd5f2017-12-09 23:25:57 +000018#include "llvm/Support/KnownBits.h"
James Molloy134bec22015-08-11 09:12:57 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22
23namespace {
24
25class MatchSelectPatternTest : public testing::Test {
26protected:
27 void parseAssembly(const char *Assembly) {
28 SMDiagnostic Error;
Mehdi Amini03b42e42016-04-14 21:59:01 +000029 M = parseAssemblyString(Assembly, Error, Context);
James Molloy134bec22015-08-11 09:12:57 +000030
31 std::string errMsg;
32 raw_string_ostream os(errMsg);
33 Error.print("", os);
34
35 // A failure here means that the test itself is buggy.
36 if (!M)
37 report_fatal_error(os.str());
38
39 Function *F = M->getFunction("test");
40 if (F == nullptr)
41 report_fatal_error("Test must have a function named @test");
42
43 A = nullptr;
44 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
45 if (I->hasName()) {
46 if (I->getName() == "A")
47 A = &*I;
48 }
49 }
50 if (A == nullptr)
51 report_fatal_error("@test must have an instruction %A");
52 }
53
54 void expectPattern(const SelectPatternResult &P) {
55 Value *LHS, *RHS;
56 Instruction::CastOps CastOp;
57 SelectPatternResult R = matchSelectPattern(A, LHS, RHS, &CastOp);
58 EXPECT_EQ(P.Flavor, R.Flavor);
59 EXPECT_EQ(P.NaNBehavior, R.NaNBehavior);
60 EXPECT_EQ(P.Ordered, R.Ordered);
61 }
62
Mehdi Amini03b42e42016-04-14 21:59:01 +000063 LLVMContext Context;
James Molloy134bec22015-08-11 09:12:57 +000064 std::unique_ptr<Module> M;
65 Instruction *A, *B;
66};
67
68}
69
70TEST_F(MatchSelectPatternTest, SimpleFMin) {
71 parseAssembly(
72 "define float @test(float %a) {\n"
73 " %1 = fcmp ult float %a, 5.0\n"
74 " %A = select i1 %1, float %a, float 5.0\n"
75 " ret float %A\n"
76 "}\n");
77 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
78}
79
80TEST_F(MatchSelectPatternTest, SimpleFMax) {
81 parseAssembly(
82 "define float @test(float %a) {\n"
83 " %1 = fcmp ogt float %a, 5.0\n"
84 " %A = select i1 %1, float %a, float 5.0\n"
85 " ret float %A\n"
86 "}\n");
87 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
88}
89
90TEST_F(MatchSelectPatternTest, SwappedFMax) {
91 parseAssembly(
92 "define float @test(float %a) {\n"
93 " %1 = fcmp olt float 5.0, %a\n"
94 " %A = select i1 %1, float %a, float 5.0\n"
95 " ret float %A\n"
96 "}\n");
97 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, false});
98}
99
100TEST_F(MatchSelectPatternTest, SwappedFMax2) {
101 parseAssembly(
102 "define float @test(float %a) {\n"
103 " %1 = fcmp olt float %a, 5.0\n"
104 " %A = select i1 %1, float 5.0, float %a\n"
105 " ret float %A\n"
106 "}\n");
107 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_NAN, false});
108}
109
110TEST_F(MatchSelectPatternTest, SwappedFMax3) {
111 parseAssembly(
112 "define float @test(float %a) {\n"
113 " %1 = fcmp ult float %a, 5.0\n"
114 " %A = select i1 %1, float 5.0, float %a\n"
115 " ret float %A\n"
116 "}\n");
117 expectPattern({SPF_FMAXNUM, SPNB_RETURNS_OTHER, true});
118}
119
120TEST_F(MatchSelectPatternTest, FastFMin) {
121 parseAssembly(
122 "define float @test(float %a) {\n"
123 " %1 = fcmp nnan olt float %a, 5.0\n"
124 " %A = select i1 %1, float %a, float 5.0\n"
125 " ret float %A\n"
126 "}\n");
127 expectPattern({SPF_FMINNUM, SPNB_RETURNS_ANY, false});
128}
129
130TEST_F(MatchSelectPatternTest, FMinConstantZero) {
131 parseAssembly(
132 "define float @test(float %a) {\n"
133 " %1 = fcmp ole float %a, 0.0\n"
134 " %A = select i1 %1, float %a, float 0.0\n"
135 " ret float %A\n"
136 "}\n");
137 // This shouldn't be matched, as %a could be -0.0.
138 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
139}
140
141TEST_F(MatchSelectPatternTest, FMinConstantZeroNsz) {
142 parseAssembly(
143 "define float @test(float %a) {\n"
144 " %1 = fcmp nsz ole float %a, 0.0\n"
145 " %A = select i1 %1, float %a, float 0.0\n"
146 " ret float %A\n"
147 "}\n");
148 // But this should be, because we've ignored signed zeroes.
149 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
150}
James Molloy569cea62015-09-02 17:25:25 +0000151
Sanjay Patel03da6e62018-10-31 21:11:59 +0000152TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero1) {
153 parseAssembly(
154 "define float @test(float %a) {\n"
155 " %1 = fcmp olt float -0.0, %a\n"
156 " %A = select i1 %1, float 0.0, float %a\n"
157 " ret float %A\n"
158 "}\n");
159 // FIXME: The sign of zero doesn't matter in fcmp.
160 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
161}
162
163TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero2) {
164 parseAssembly(
165 "define float @test(float %a) {\n"
166 " %1 = fcmp ogt float %a, -0.0\n"
167 " %A = select i1 %1, float 0.0, float %a\n"
168 " ret float %A\n"
169 "}\n");
170 // FIXME: The sign of zero doesn't matter in fcmp.
171 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
172}
173
174TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero3) {
175 parseAssembly(
176 "define float @test(float %a) {\n"
177 " %1 = fcmp olt float 0.0, %a\n"
178 " %A = select i1 %1, float -0.0, float %a\n"
179 " ret float %A\n"
180 "}\n");
181 // FIXME: The sign of zero doesn't matter in fcmp.
182 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
183}
184
185TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero4) {
186 parseAssembly(
187 "define float @test(float %a) {\n"
188 " %1 = fcmp ogt float %a, 0.0\n"
189 " %A = select i1 %1, float -0.0, float %a\n"
190 " ret float %A\n"
191 "}\n");
192 // FIXME: The sign of zero doesn't matter in fcmp.
193 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
194}
195
196TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero5) {
197 parseAssembly(
198 "define float @test(float %a) {\n"
199 " %1 = fcmp ogt float -0.0, %a\n"
200 " %A = select i1 %1, float %a, float 0.0\n"
201 " ret float %A\n"
202 "}\n");
203 // FIXME: The sign of zero doesn't matter in fcmp.
204 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
205}
206
207TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero6) {
208 parseAssembly(
209 "define float @test(float %a) {\n"
210 " %1 = fcmp olt float %a, -0.0\n"
211 " %A = select i1 %1, float %a, float 0.0\n"
212 " ret float %A\n"
213 "}\n");
214 // FIXME: The sign of zero doesn't matter in fcmp.
215 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
216}
217
218TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero7) {
219 parseAssembly(
220 "define float @test(float %a) {\n"
221 " %1 = fcmp ogt float 0.0, %a\n"
222 " %A = select i1 %1, float %a, float -0.0\n"
223 " ret float %A\n"
224 "}\n");
225 // FIXME: The sign of zero doesn't matter in fcmp.
226 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
227}
228
229TEST_F(MatchSelectPatternTest, FMinMismatchConstantZero8) {
230 parseAssembly(
231 "define float @test(float %a) {\n"
232 " %1 = fcmp olt float %a, 0.0\n"
233 " %A = select i1 %1, float %a, float -0.0\n"
234 " ret float %A\n"
235 "}\n");
236 // FIXME: The sign of zero doesn't matter in fcmp.
237 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
238}
239
240TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero1) {
241 parseAssembly(
242 "define float @test(float %a) {\n"
243 " %1 = fcmp ogt float -0.0, %a\n"
244 " %A = select i1 %1, float 0.0, float %a\n"
245 " ret float %A\n"
246 "}\n");
247 // FIXME: The sign of zero doesn't matter in fcmp.
248 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
249}
250
251TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero2) {
252 parseAssembly(
253 "define float @test(float %a) {\n"
254 " %1 = fcmp olt float %a, -0.0\n"
255 " %A = select i1 %1, float 0.0, float %a\n"
256 " ret float %A\n"
257 "}\n");
258 // FIXME: The sign of zero doesn't matter in fcmp.
259 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
260}
261
262TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero3) {
263 parseAssembly(
264 "define float @test(float %a) {\n"
265 " %1 = fcmp ogt float 0.0, %a\n"
266 " %A = select i1 %1, float -0.0, float %a\n"
267 " ret float %A\n"
268 "}\n");
269 // FIXME: The sign of zero doesn't matter in fcmp.
270 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
271}
272
273TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero4) {
274 parseAssembly(
275 "define float @test(float %a) {\n"
276 " %1 = fcmp olt float %a, 0.0\n"
277 " %A = select i1 %1, float -0.0, float %a\n"
278 " ret float %A\n"
279 "}\n");
280 // FIXME: The sign of zero doesn't matter in fcmp.
281 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
282}
283
284TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero5) {
285 parseAssembly(
286 "define float @test(float %a) {\n"
287 " %1 = fcmp olt float -0.0, %a\n"
288 " %A = select i1 %1, float %a, float 0.0\n"
289 " ret float %A\n"
290 "}\n");
291 // FIXME: The sign of zero doesn't matter in fcmp.
292 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
293}
294
295TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero6) {
296 parseAssembly(
297 "define float @test(float %a) {\n"
298 " %1 = fcmp ogt float %a, -0.0\n"
299 " %A = select i1 %1, float %a, float 0.0\n"
300 " ret float %A\n"
301 "}\n");
302 // FIXME: The sign of zero doesn't matter in fcmp.
303 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
304}
305
306TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero7) {
307 parseAssembly(
308 "define float @test(float %a) {\n"
309 " %1 = fcmp olt float 0.0, %a\n"
310 " %A = select i1 %1, float %a, float -0.0\n"
311 " ret float %A\n"
312 "}\n");
313 // FIXME: The sign of zero doesn't matter in fcmp.
314 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
315}
316
317TEST_F(MatchSelectPatternTest, FMaxMismatchConstantZero8) {
318 parseAssembly(
319 "define float @test(float %a) {\n"
320 " %1 = fcmp ogt float %a, 0.0\n"
321 " %A = select i1 %1, float %a, float -0.0\n"
322 " ret float %A\n"
323 "}\n");
324 // FIXME: The sign of zero doesn't matter in fcmp.
325 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
326}
327
Thomas Lively30f1d692018-10-24 22:49:55 +0000328TEST_F(MatchSelectPatternTest, VectorFMinimum) {
Thomas Livelyd47b5c72018-09-28 21:36:43 +0000329 parseAssembly(
330 "define <4 x float> @test(<4 x float> %a) {\n"
331 " %1 = fcmp ule <4 x float> %a, \n"
332 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
333 " %A = select <4 x i1> %1, <4 x float> %a,\n"
334 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
335 " ret <4 x float> %A\n"
336 "}\n");
337 // Check that pattern matching works on vectors where each lane has the same
338 // unordered pattern.
339 expectPattern({SPF_FMINNUM, SPNB_RETURNS_NAN, false});
340}
341
342TEST_F(MatchSelectPatternTest, VectorFMinOtherOrdered) {
343 parseAssembly(
344 "define <4 x float> @test(<4 x float> %a) {\n"
345 " %1 = fcmp ole <4 x float> %a, \n"
346 " <float 5.0, float 5.0, float 5.0, float 5.0>\n"
347 " %A = select <4 x i1> %1, <4 x float> %a,\n"
348 " <4 x float> <float 5.0, float 5.0, float 5.0, float 5.0>\n"
349 " ret <4 x float> %A\n"
350 "}\n");
351 // Check that pattern matching works on vectors where each lane has the same
352 // ordered pattern.
353 expectPattern({SPF_FMINNUM, SPNB_RETURNS_OTHER, true});
354}
355
Thomas Lively30f1d692018-10-24 22:49:55 +0000356TEST_F(MatchSelectPatternTest, VectorNotFMinimum) {
Thomas Livelyd47b5c72018-09-28 21:36:43 +0000357 parseAssembly(
358 "define <4 x float> @test(<4 x float> %a) {\n"
359 " %1 = fcmp ule <4 x float> %a, \n"
360 " <float 5.0, float 0x7ff8000000000000, float 5.0, float 5.0>\n"
361 " %A = select <4 x i1> %1, <4 x float> %a,\n"
362 " <4 x float> <float 5.0, float 0x7ff8000000000000, float 5.0, float "
363 "5.0>\n"
364 " ret <4 x float> %A\n"
365 "}\n");
366 // The lane that contains a NaN (0x7ff80...) behaves like a
367 // non-NaN-propagating min and the other lines behave like a NaN-propagating
368 // min, so check that neither is returned.
369 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
370}
371
372TEST_F(MatchSelectPatternTest, VectorNotFMinZero) {
373 parseAssembly(
374 "define <4 x float> @test(<4 x float> %a) {\n"
375 " %1 = fcmp ule <4 x float> %a, \n"
376 " <float 5.0, float -0.0, float 5.0, float 5.0>\n"
377 " %A = select <4 x i1> %1, <4 x float> %a,\n"
378 " <4 x float> <float 5.0, float 0.0, float 5.0, float 5.0>\n"
379 " ret <4 x float> %A\n"
380 "}\n");
381 // Always selects the second lane of %a if it is positive or negative zero, so
382 // this is stricter than a min.
383 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
384}
385
James Molloy569cea62015-09-02 17:25:25 +0000386TEST_F(MatchSelectPatternTest, DoubleCastU) {
387 parseAssembly(
388 "define i32 @test(i8 %a, i8 %b) {\n"
389 " %1 = icmp ult i8 %a, %b\n"
390 " %2 = zext i8 %a to i32\n"
391 " %3 = zext i8 %b to i32\n"
392 " %A = select i1 %1, i32 %2, i32 %3\n"
393 " ret i32 %A\n"
394 "}\n");
395 // We should be able to look through the situation where we cast both operands
396 // to the select.
397 expectPattern({SPF_UMIN, SPNB_NA, false});
398}
399
400TEST_F(MatchSelectPatternTest, DoubleCastS) {
401 parseAssembly(
402 "define i32 @test(i8 %a, i8 %b) {\n"
403 " %1 = icmp slt i8 %a, %b\n"
404 " %2 = sext i8 %a to i32\n"
405 " %3 = sext i8 %b to i32\n"
406 " %A = select i1 %1, i32 %2, i32 %3\n"
407 " ret i32 %A\n"
408 "}\n");
409 // We should be able to look through the situation where we cast both operands
410 // to the select.
411 expectPattern({SPF_SMIN, SPNB_NA, false});
412}
413
414TEST_F(MatchSelectPatternTest, DoubleCastBad) {
415 parseAssembly(
416 "define i32 @test(i8 %a, i8 %b) {\n"
417 " %1 = icmp ult i8 %a, %b\n"
418 " %2 = zext i8 %a to i32\n"
419 " %3 = sext i8 %b to i32\n"
420 " %A = select i1 %1, i32 %2, i32 %3\n"
421 " ret i32 %A\n"
422 "}\n");
James Molloy687a8442015-09-02 17:29:54 +0000423 // The cast types here aren't the same, so we cannot match an UMIN.
James Molloy569cea62015-09-02 17:25:25 +0000424 expectPattern({SPF_UNKNOWN, SPNB_NA, false});
425}
Sanjoy Das3bb2dbd2016-12-31 22:12:34 +0000426
427TEST(ValueTracking, GuaranteedToTransferExecutionToSuccessor) {
428 StringRef Assembly =
429 "declare void @nounwind_readonly(i32*) nounwind readonly "
430 "declare void @nounwind_argmemonly(i32*) nounwind argmemonly "
431 "declare void @throws_but_readonly(i32*) readonly "
432 "declare void @throws_but_argmemonly(i32*) argmemonly "
433 " "
434 "declare void @unknown(i32*) "
435 " "
436 "define void @f(i32* %p) { "
437 " call void @nounwind_readonly(i32* %p) "
438 " call void @nounwind_argmemonly(i32* %p) "
439 " call void @throws_but_readonly(i32* %p) "
440 " call void @throws_but_argmemonly(i32* %p) "
441 " call void @unknown(i32* %p) nounwind readonly "
442 " call void @unknown(i32* %p) nounwind argmemonly "
443 " call void @unknown(i32* %p) readonly "
444 " call void @unknown(i32* %p) argmemonly "
445 " ret void "
446 "} ";
447
448 LLVMContext Context;
449 SMDiagnostic Error;
450 auto M = parseAssemblyString(Assembly, Error, Context);
451 assert(M && "Bad assembly?");
452
453 auto *F = M->getFunction("f");
454 assert(F && "Bad assembly?");
455
456 auto &BB = F->getEntryBlock();
Craig Topper8580cd42017-04-14 17:59:19 +0000457 bool ExpectedAnswers[] = {
Sanjoy Das3bb2dbd2016-12-31 22:12:34 +0000458 true, // call void @nounwind_readonly(i32* %p)
459 true, // call void @nounwind_argmemonly(i32* %p)
460 false, // call void @throws_but_readonly(i32* %p)
461 false, // call void @throws_but_argmemonly(i32* %p)
462 true, // call void @unknown(i32* %p) nounwind readonly
463 true, // call void @unknown(i32* %p) nounwind argmemonly
464 false, // call void @unknown(i32* %p) readonly
465 false, // call void @unknown(i32* %p) argmemonly
466 false, // ret void
467 };
468
469 int Index = 0;
470 for (auto &I : BB) {
471 EXPECT_EQ(isGuaranteedToTransferExecutionToSuccessor(&I),
472 ExpectedAnswers[Index])
473 << "Incorrect answer at instruction " << Index << " = " << I;
474 Index++;
475 }
476}
Sanjoy Das39a684d2017-02-25 20:30:45 +0000477
478TEST(ValueTracking, ComputeNumSignBits_PR32045) {
479 StringRef Assembly = "define i32 @f(i32 %a) { "
480 " %val = ashr i32 %a, -1 "
481 " ret i32 %val "
482 "} ";
483
484 LLVMContext Context;
485 SMDiagnostic Error;
486 auto M = parseAssemblyString(Assembly, Error, Context);
487 assert(M && "Bad assembly?");
488
489 auto *F = M->getFunction("f");
490 assert(F && "Bad assembly?");
491
492 auto *RVal =
493 cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
Sanjoy Das4897cea2017-02-25 22:25:48 +0000494 EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
Sanjoy Das39a684d2017-02-25 20:30:45 +0000495}
Simon Dardis70dbd5f2017-12-09 23:25:57 +0000496
Sanjay Patela68096c2018-11-02 15:51:47 +0000497// No guarantees for canonical IR in this analysis, so this just bails out.
498TEST(ValueTracking, ComputeNumSignBits_Shuffle) {
499 StringRef Assembly = "define <2 x i32> @f() { "
500 " %val = shufflevector <2 x i32> undef, <2 x i32> undef, <2 x i32> <i32 0, i32 0> "
501 " ret <2 x i32> %val "
502 "} ";
503
504 LLVMContext Context;
505 SMDiagnostic Error;
506 auto M = parseAssemblyString(Assembly, Error, Context);
507 assert(M && "Bad assembly?");
508
509 auto *F = M->getFunction("f");
510 assert(F && "Bad assembly?");
511
512 auto *RVal =
513 cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
514 EXPECT_EQ(ComputeNumSignBits(RVal, M->getDataLayout()), 1u);
515}
516
Simon Dardis70dbd5f2017-12-09 23:25:57 +0000517TEST(ValueTracking, ComputeKnownBits) {
518 StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
519 " %ash = mul i32 %a, 8 "
520 " %aad = add i32 %ash, 7 "
521 " %aan = and i32 %aad, 4095 "
522 " %bsh = shl i32 %b, 4 "
523 " %bad = or i32 %bsh, 6 "
524 " %ban = and i32 %bad, 4095 "
525 " %mul = mul i32 %aan, %ban "
526 " ret i32 %mul "
527 "} ";
528
529 LLVMContext Context;
530 SMDiagnostic Error;
531 auto M = parseAssemblyString(Assembly, Error, Context);
532 assert(M && "Bad assembly?");
533
534 auto *F = M->getFunction("f");
535 assert(F && "Bad assembly?");
536
537 auto *RVal =
538 cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
539 auto Known = computeKnownBits(RVal, M->getDataLayout());
540 ASSERT_FALSE(Known.hasConflict());
541 EXPECT_EQ(Known.One.getZExtValue(), 10u);
542 EXPECT_EQ(Known.Zero.getZExtValue(), 4278190085u);
543}
544
545TEST(ValueTracking, ComputeKnownMulBits) {
546 StringRef Assembly = "define i32 @f(i32 %a, i32 %b) { "
547 " %aa = shl i32 %a, 5 "
548 " %bb = shl i32 %b, 5 "
549 " %aaa = or i32 %aa, 24 "
550 " %bbb = or i32 %bb, 28 "
551 " %mul = mul i32 %aaa, %bbb "
552 " ret i32 %mul "
553 "} ";
554
555 LLVMContext Context;
556 SMDiagnostic Error;
557 auto M = parseAssemblyString(Assembly, Error, Context);
558 assert(M && "Bad assembly?");
559
560 auto *F = M->getFunction("f");
561 assert(F && "Bad assembly?");
562
563 auto *RVal =
564 cast<ReturnInst>(F->getEntryBlock().getTerminator())->getOperand(0);
565 auto Known = computeKnownBits(RVal, M->getDataLayout());
566 ASSERT_FALSE(Known.hasConflict());
567 EXPECT_EQ(Known.One.getZExtValue(), 32u);
568 EXPECT_EQ(Known.Zero.getZExtValue(), 95u);
569}