llvm.org GIT mirror llvm / 6faf1a3
[EarlyCSE] Mark the condition of assume intrinsic as true EarlyCSE should not just ignore assumes. It should use the fact that its condition is true for all dominated instructions. Reviewers: sanjoy, reames, apilipenko, anna, skatkov Reviewed By: reames, sanjoy Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D32482 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301625 91177308-0d34-0410-b5e6-96231b3b80d8 Max Kazantsev 2 years ago
2 changed file(s) with 199 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
634634
635635 // Skip assume intrinsics, they don't really have side effects (although
636636 // they're marked as such to ensure preservation of control dependencies),
637 // and this pass will not disturb any of the assumption's control
638 // dependencies.
637 // and this pass will not bother with its removal. However, we should mark
638 // its condition as true for all dominated blocks.
639639 if (match(Inst, m_Intrinsic())) {
640 DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n');
640 auto *CondI =
641 dyn_cast(cast(Inst)->getArgOperand(0));
642 if (CondI && SimpleValue::canHandle(CondI)) {
643 DEBUG(dbgs() << "EarlyCSE considering assumption: " << *Inst << '\n');
644 AvailableValues.insert(CondI, ConstantInt::getTrue(BB->getContext()));
645 } else
646 DEBUG(dbgs() << "EarlyCSE skipping assumption: " << *Inst << '\n');
641647 continue;
642648 }
643649
11 ; RUN: opt < %s -S -basicaa -early-cse-memssa | FileCheck %s
22
33 declare void @llvm.experimental.guard(i1,...)
4
5 declare void @llvm.assume(i1)
46
57 define i32 @test0(i32* %ptr, i1 %cond) {
68 ; We can do store to load forwarding over a guard, since it does not
335337 merge:
336338 ret void
337339 }
340
341 define void @test12(i32 %a, i32 %b) {
342 ; Check that the assume marks its condition as being true (and thus allows to
343 ; eliminate the dominated guards).
344
345 ; CHECK-LABEL: @test12(
346 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
347 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
348 ; CHECK-NEXT: ret void
349
350 %cmp = icmp eq i32 %a, %b
351 call void @llvm.assume(i1 %cmp)
352 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
353 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
354 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
355 ret void
356 }
357
358 define void @test13(i32 %a, i32 %b, i32* %ptr) {
359 ; Check that we deal correctly with stores when removing guards due to assume.
360
361 ; CHECK-LABEL: @test13(
362 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
363 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
364 ; CHECK-NEXT: store i32 400, i32* %ptr
365 ; CHECK-NEXT: ret void
366
367 %cmp = icmp eq i32 %a, %b
368 call void @llvm.assume(i1 %cmp)
369 store i32 100, i32* %ptr
370 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
371 store i32 200, i32* %ptr
372 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
373 store i32 300, i32* %ptr
374 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
375 store i32 400, i32* %ptr
376 ret void
377 }
378
379 define void @test14(i32 %a, i32 %b, i1 %c, i32* %ptr) {
380 ; Similar to test13, but with more control flow.
381 ; TODO: Can we get rid of the store in the end of entry given that it is
382 ; post-dominated by other stores?
383
384 ; CHECK-LABEL: @test14(
385 ; CHECK: entry:
386 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
387 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
388 ; CHECK-NEXT: store i32 400, i32* %ptr
389 ; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
390 ; CHECK: if.true:
391 ; CHECK-NEXT: store i32 500, i32* %ptr
392 ; CHECK-NEXT: br label %merge
393 ; CHECK: if.false:
394 ; CHECK-NEXT: store i32 600, i32* %ptr
395 ; CHECK-NEXT: br label %merge
396 ; CHECK: merge:
397 ; CHECK-NEXT: ret void
398
399 entry:
400 %cmp = icmp eq i32 %a, %b
401 call void @llvm.assume(i1 %cmp)
402 store i32 100, i32* %ptr
403 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
404 store i32 200, i32* %ptr
405 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
406 store i32 300, i32* %ptr
407 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
408 store i32 400, i32* %ptr
409 br i1 %c, label %if.true, label %if.false
410
411 if.true:
412 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
413 store i32 500, i32* %ptr
414 br label %merge
415
416 if.false:
417 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
418 store i32 600, i32* %ptr
419 br label %merge
420
421 merge:
422 ret void
423 }
424
425 define void @test15(i32 %a, i32 %b, i1 %c, i32* %ptr) {
426 ; Make sure that non-dominating assumes do not cause guards removal.
427
428 ; CHECK-LABEL: @test15(
429 ; CHECK: entry:
430 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
431 ; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
432 ; CHECK: if.true:
433 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
434 ; CHECK-NEXT: store i32 100, i32* %ptr
435 ; CHECK-NEXT: br label %merge
436 ; CHECK: if.false:
437 ; CHECK-NEXT: store i32 200, i32* %ptr
438 ; CHECK-NEXT: br label %merge
439 ; CHECK: merge:
440 ; CHECK-NEXT: store i32 300, i32* %ptr
441 ; CHECK-NEXT: call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
442 ; CHECK-NEXT: store i32 400, i32* %ptr
443 ; CHECK-NEXT: ret void
444
445 entry:
446 %cmp = icmp eq i32 %a, %b
447 br i1 %c, label %if.true, label %if.false
448
449 if.true:
450 call void @llvm.assume(i1 %cmp)
451 store i32 100, i32* %ptr
452 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
453 br label %merge
454
455 if.false:
456 store i32 200, i32* %ptr
457 br label %merge
458
459 merge:
460 store i32 300, i32* %ptr
461 call void (i1, ...) @llvm.experimental.guard(i1 %cmp) [ "deopt"() ]
462 store i32 400, i32* %ptr
463 ret void
464 }
465
466 define void @test16(i32 %a, i32 %b) {
467 ; Check that we don't bother to do anything with assumes even if we know the
468 ; condition being true.
469
470 ; CHECK-LABEL: @test16(
471 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
472 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
473 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
474 ; CHECK-NEXT: ret void
475
476 %cmp = icmp eq i32 %a, %b
477 call void @llvm.assume(i1 %cmp)
478 call void @llvm.assume(i1 %cmp)
479 ret void
480 }
481
482 define void @test17(i32 %a, i32 %b, i1 %c, i32* %ptr) {
483 ; Check that we don't bother to do anything with assumes even if we know the
484 ; condition being true or false (includes come control flow).
485
486 ; CHECK-LABEL: @test17(
487 ; CHECK: entry:
488 ; CHECK-NEXT: %cmp = icmp eq i32 %a, %b
489 ; CHECK-NEXT: br i1 %c, label %if.true, label %if.false
490 ; CHECK: if.true:
491 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
492 ; CHECK-NEXT: br label %merge
493 ; CHECK: if.false:
494 ; CHECK-NEXT: call void @llvm.assume(i1 %cmp)
495 ; CHECK-NEXT: br label %merge
496 ; CHECK: merge:
497 ; CHECK-NEXT: ret void
498
499 entry:
500 %cmp = icmp eq i32 %a, %b
501 br i1 %c, label %if.true, label %if.false
502
503 if.true:
504 call void @llvm.assume(i1 %cmp)
505 br label %merge
506
507 if.false:
508 call void @llvm.assume(i1 %cmp)
509 br label %merge
510
511 merge:
512 ret void
513 }
514
515 define void @test18(i1 %c) {
516 ; Check that we don't bother to do anything with assumes even if we know the
517 ; condition being true and not being an instruction.
518
519 ; CHECK-LABEL: @test18(
520 ; CHECK-NEXT: call void @llvm.assume(i1 %c)
521 ; CHECK-NEXT: call void @llvm.assume(i1 %c)
522 ; CHECK-NEXT: ret void
523
524 call void @llvm.assume(i1 %c)
525 call void @llvm.assume(i1 %c)
526 ret void
527 }