@@ -2429,133 +2429,6 @@ static bool swapMayExposeCSEOpportunities(const Value * Op0,
2429
2429
return GlobalSwapBenefits > 0 ;
2430
2430
}
2431
2431
2432
- // / \brief Check that one use is in the same block as the definition and all
2433
- // / other uses are in blocks dominated by a given block
2434
- // /
2435
- // / \param DI Definition
2436
- // / \param UI Use
2437
- // / \param DB Block that must dominate all uses of \p DI outside
2438
- // / the parent block. Note there can be a use of \p DI in \p DB.
2439
- // / \return true when \p UI is the only use of \p DI in the parent block
2440
- // / and all other uses of \p DI are in blocks dominated by \p DB.
2441
- // /
2442
- bool InstCombiner::dominatesAllUses (const Instruction *DI,
2443
- const Instruction *UI,
2444
- const BasicBlock *DB) const {
2445
- assert (DI && UI && DI->getParent () == UI->getParent () &&
2446
- " definition and use must be in the same block" );
2447
- // DominatorTree available?
2448
- if (!DT)
2449
- return false ;
2450
- for (const User *U : DI->users ()) {
2451
- auto *Usr = cast<Instruction>(U);
2452
- if (Usr != UI && !DT->dominates (DB, Usr->getParent ()))
2453
- return false ;
2454
- }
2455
- return true ;
2456
- }
2457
-
2458
- // /
2459
- // / true when the instruction sequence within a block is select-cmp-br.
2460
- // /
2461
- static bool isChainSelectCmpBranch (const SelectInst *SI) {
2462
- const BasicBlock *BB = SI->getParent ();
2463
- if (!BB)
2464
- return false ;
2465
- auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator ());
2466
- if (!BI || BI->getNumSuccessors () != 2 )
2467
- return false ;
2468
- auto *IC = dyn_cast<ICmpInst>(BI->getCondition ());
2469
- if (!IC || (IC->getOperand (0 ) != SI && IC->getOperand (1 ) != SI))
2470
- return false ;
2471
- // FIXME: Conservatively suppress the optimization when the IC
2472
- // has a parent different from SI (including no parent). Otherwise
2473
- // the assertion in dominatesAllUses() fires and causes a build failure.
2474
- // Make the optimization safe w/o this condition.
2475
- if (SI->getParent () != IC->getParent ())
2476
- return false ;
2477
- return true ;
2478
- }
2479
-
2480
- // /
2481
- // / \brief True when a select result is replaced by one of its operands
2482
- // / in select-icmp sequence. This will eventually result in the elimination
2483
- // / of the select.
2484
- // /
2485
- // / \param SI Select instruction
2486
- // / \param Icmp Compare instruction
2487
- // / \param CI1 'true' when first select operand is equal to RHSC of Icmp
2488
- // / \param CI2 'true' when second select operand is equal to RHSC of Icmp
2489
- // /
2490
- // / Notes:
2491
- // / - The replacement is global and requires dominator information
2492
- // / - The caller is responsible for the actual replacement
2493
- // /
2494
- // / Example:
2495
- // /
2496
- // / entry:
2497
- // / %4 = select i1 %3, %C* %0, %C* null
2498
- // / %5 = icmp eq %C* %4, null
2499
- // / br i1 %5, label %9, label %7
2500
- // / ...
2501
- // / ; <label>:7 ; preds = %entry
2502
- // / %8 = getelementptr inbounds %C* %4, i64 0, i32 0
2503
- // / ...
2504
- // /
2505
- // / can be transformed to
2506
- // /
2507
- // / %5 = icmp eq %C* %0, null
2508
- // / %6 = select i1 %3, i1 %5, i1 true
2509
- // / br i1 %6, label %9, label %7
2510
- // / ...
2511
- // / ; <label>:7 ; preds = %entry
2512
- // / %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
2513
- // /
2514
- // / Similar when the first operand of the select is a constant or/and
2515
- // / the compare is for not equal rather than equal.
2516
- // /
2517
- // / FIXME: Currently the function considers equal compares only. It should be
2518
- // / possbile to extend it to not equal compares also.
2519
- // /
2520
- bool InstCombiner::replacedSelectWithOperand (SelectInst *SI,
2521
- const ICmpInst *Icmp,
2522
- const ConstantInt *CI1,
2523
- const ConstantInt *CI2) {
2524
- if (isChainSelectCmpBranch (SI) && Icmp->isEquality ()) {
2525
- // Code sequence is select - icmp.[eq|ne] - br
2526
- unsigned ReplaceWithOpd = 0 ;
2527
- if (CI1 && !CI1->isZero ())
2528
- // The first constant operand of the select and the RHS of
2529
- // the compare match, so try to substitute
2530
- // the select results with its second operand
2531
- // Example:
2532
- // %4 = select i1 %3, %C* null, %C* %0
2533
- // %5 = icmp eq %C* %4, null
2534
- // ==> could replace select with second operand
2535
- ReplaceWithOpd = 2 ;
2536
- else if (CI2 && !CI2->isZero ())
2537
- // Similar when the second operand of the select is a constant
2538
- // Example:
2539
- // %4 = select i1 %3, %C* %0, %C* null
2540
- // %5 = icmp eq %C* %4, null
2541
- // ==> could replace select with first operand
2542
- ReplaceWithOpd = 1 ;
2543
- if (ReplaceWithOpd) {
2544
- // Replace select with operand on else path for EQ compares.
2545
- // Replace select with operand on then path for NE compares.
2546
- BasicBlock *Succ =
2547
- Icmp->getPredicate () == ICmpInst::ICMP_EQ
2548
- ? SI->getParent ()->getTerminator ()->getSuccessor (1 )
2549
- : SI->getParent ()->getTerminator ()->getSuccessor (0 );
2550
- if (InstCombiner::dominatesAllUses (SI, Icmp, Succ)) {
2551
- SI->replaceAllUsesWith (SI->getOperand (ReplaceWithOpd));
2552
- return true ;
2553
- }
2554
- }
2555
- }
2556
- return false ;
2557
- }
2558
-
2559
2432
Instruction *InstCombiner::visitICmpInst (ICmpInst &I) {
2560
2433
bool Changed = false ;
2561
2434
Value *Op0 = I.getOperand (0 ), *Op1 = I.getOperand (1 );
@@ -3012,21 +2885,8 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
3012
2885
// fold to a constant (in which case the icmp is replaced with a select
3013
2886
// which will usually simplify) or this is the only user of the
3014
2887
// select (in which case we are trading a select+icmp for a simpler
3015
- // select+icmp) or all uses of the select can be replaced based on
3016
- // dominance information ("Global cases").
3017
- bool Transform = false ;
3018
- if (Op1 && Op2)
3019
- Transform = true ;
3020
- else if (Op1 || Op2) {
3021
- if (LHSI->hasOneUse ())
3022
- Transform = true ;
3023
- else
3024
- // Global cases
3025
- Transform = replacedSelectWithOperand (
3026
- cast<SelectInst>(LHSI), &I, dyn_cast_or_null<ConstantInt>(Op1),
3027
- dyn_cast_or_null<ConstantInt>(Op2));
3028
- }
3029
- if (Transform) {
2888
+ // select+icmp).
2889
+ if ((Op1 && Op2) || (LHSI->hasOneUse () && (Op1 || Op2))) {
3030
2890
if (!Op1)
3031
2891
Op1 = Builder->CreateICmp (I.getPredicate (), LHSI->getOperand (1 ),
3032
2892
RHSC, I.getName ());
0 commit comments